hello-algo/zh-hant/search/search_index.json
2024-04-29 15:19:09 +08:00

1 line
No EOL
2.8 MiB

{"config":{"lang":["en"],"separator":"[\\s\\u200b\\u3000\\-\u3001\u3002\uff0c\uff0e\uff1f\uff01\uff1b]+","pipeline":["stemmer"]},"docs":[{"location":"chapter_appendix/","title":"\u7b2c 16 \u7ae0 \u00a0 \u9644\u9304","text":""},{"location":"chapter_appendix/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>16.1 \u00a0 \u7a0b\u5f0f\u8a2d\u8a08\u74b0\u5883\u5b89\u88dd</li> <li>16.2 \u00a0 \u4e00\u8d77\u53c3\u8207\u5275\u4f5c</li> <li>16.3 \u00a0 \u8853\u8a9e\u8868</li> </ul>"},{"location":"chapter_appendix/contribution/","title":"16.2 \u00a0 \u4e00\u8d77\u53c3\u8207\u5275\u4f5c","text":"<p>\u7531\u65bc\u7b46\u8005\u80fd\u529b\u6709\u9650\uff0c\u66f8\u4e2d\u96e3\u514d\u5b58\u5728\u4e00\u4e9b\u907a\u6f0f\u548c\u932f\u8aa4\uff0c\u8acb\u60a8\u8ad2\u89e3\u3002\u5982\u679c\u60a8\u767c\u73fe\u4e86\u7b46\u8aa4\u3001\u9023\u7d50\u5931\u6548\u3001\u5167\u5bb9\u7f3a\u5931\u3001\u6587\u5b57\u6b67\u7fa9\u3001\u89e3\u91cb\u4e0d\u6e05\u6670\u6216\u884c\u6587\u7d50\u69cb\u4e0d\u5408\u7406\u7b49\u554f\u984c\uff0c\u8acb\u5354\u52a9\u6211\u5011\u9032\u884c\u4fee\u6b63\uff0c\u4ee5\u7d66\u8b80\u8005\u63d0\u4f9b\u66f4\u512a\u8cea\u7684\u5b78\u7fd2\u8cc7\u6e90\u3002</p> <p>\u6240\u6709\u64b0\u7a3f\u4eba\u7684 GitHub ID \u5c07\u5728\u672c\u66f8\u5009\u5eab\u3001\u7db2\u9801\u7248\u548c PDF \u7248\u7684\u4e3b\u9801\u4e0a\u9032\u884c\u5c55\u793a\uff0c\u4ee5\u611f\u8b1d\u4ed6\u5011\u5c0d\u958b\u6e90\u793e\u7fa4\u7684\u7121\u79c1\u5949\u737b\u3002</p> <p>\u958b\u6e90\u7684\u9b45\u529b</p> <p>\u7d19\u8cea\u5716\u66f8\u7684\u5169\u6b21\u5370\u5237\u7684\u9593\u9694\u6642\u9593\u5f80\u5f80\u8f03\u4e45\uff0c\u5167\u5bb9\u66f4\u65b0\u975e\u5e38\u4e0d\u65b9\u4fbf\u3002</p> <p>\u800c\u5728\u672c\u958b\u6e90\u66f8\u4e2d\uff0c\u5167\u5bb9\u66f4\u8fed\u7684\u6642\u9593\u88ab\u7e2e\u77ed\u81f3\u6578\u65e5\u751a\u81f3\u5e7e\u500b\u5c0f\u6642\u3002</p>"},{"location":"chapter_appendix/contribution/#1","title":"1. \u00a0 \u5167\u5bb9\u5fae\u8abf","text":"<p>\u5982\u5716 16-3 \u6240\u793a\uff0c\u6bcf\u500b\u9801\u9762\u7684\u53f3\u4e0a\u89d2\u90fd\u6709\u201c\u7de8\u8f2f\u5716\u793a\u201d\u3002\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9a5f\u4fee\u6539\u6587\u5b57\u6216\u7a0b\u5f0f\u78bc\u3002</p> <ol> <li>\u9ede\u9078\u201c\u7de8\u8f2f\u5716\u793a\u201d\uff0c\u5982\u679c\u9047\u5230\u201c\u9700\u8981 Fork \u6b64\u5009\u5eab\u201d\u7684\u63d0\u793a\uff0c\u8acb\u540c\u610f\u8a72\u64cd\u4f5c\u3002</li> <li>\u4fee\u6539 Markdown \u6e90\u6a94\u6848\u5167\u5bb9\uff0c\u6aa2\u67e5\u5167\u5bb9\u7684\u6b63\u78ba\u6027\uff0c\u4e26\u5118\u91cf\u4fdd\u6301\u6392\u7248\u683c\u5f0f\u7684\u7d71\u4e00\u3002</li> <li>\u5728\u9801\u9762\u5e95\u90e8\u586b\u5beb\u4fee\u6539\u8aaa\u660e\uff0c\u7136\u5f8c\u9ede\u9078\u201cPropose file change\u201d\u6309\u9215\u3002\u9801\u9762\u8df3\u8f49\u5f8c\uff0c\u9ede\u9078\u201cCreate pull request\u201d\u6309\u9215\u5373\u53ef\u767c\u8d77\u62c9\u53d6\u8acb\u6c42\u3002</li> </ol> <p></p> <p> \u5716 16-3 \u00a0 \u9801\u9762\u7de8\u8f2f\u6309\u9375 </p> <p>\u5716\u7247\u7121\u6cd5\u76f4\u63a5\u4fee\u6539\uff0c\u9700\u8981\u900f\u904e\u65b0\u5efa Issue \u6216\u8a55\u8ad6\u7559\u8a00\u4f86\u63cf\u8ff0\u554f\u984c\uff0c\u6211\u5011\u6703\u76e1\u5feb\u91cd\u65b0\u7e6a\u88fd\u4e26\u66ff\u63db\u5716\u7247\u3002</p>"},{"location":"chapter_appendix/contribution/#2","title":"2. \u00a0 \u5167\u5bb9\u5275\u4f5c","text":"<p>\u5982\u679c\u60a8\u6709\u8208\u8da3\u53c3\u8207\u6b64\u958b\u6e90\u5c08\u6848\uff0c\u5305\u62ec\u5c07\u7a0b\u5f0f\u78bc\u7ffb\u8b6f\u6210\u5176\u4ed6\u7a0b\u5f0f\u8a9e\u8a00\u3001\u64f4\u5c55\u6587\u7ae0\u5167\u5bb9\u7b49\uff0c\u90a3\u9ebc\u9700\u8981\u5be6\u65bd\u4ee5\u4e0b Pull Request \u5de5\u4f5c\u6d41\u7a0b\u3002</p> <ol> <li>\u767b\u5165 GitHub \uff0c\u5c07\u672c\u66f8\u7684\u7a0b\u5f0f\u78bc\u5009\u5eab Fork \u5230\u500b\u4eba\u5e33\u865f\u4e0b\u3002</li> <li>\u9032\u5165\u60a8\u7684 Fork \u5009\u5eab\u7db2\u9801\uff0c\u4f7f\u7528 <code>git clone</code> \u547d\u4ee4\u5c07\u5009\u5eab\u514b\u9686\u81f3\u672c\u5730\u3002</li> <li>\u5728\u672c\u5730\u9032\u884c\u5167\u5bb9\u5275\u4f5c\uff0c\u4e26\u9032\u884c\u5b8c\u6574\u6e2c\u8a66\uff0c\u9a57\u8b49\u7a0b\u5f0f\u78bc\u7684\u6b63\u78ba\u6027\u3002</li> <li>\u5c07\u672c\u5730\u6240\u505a\u66f4\u6539 Commit \uff0c\u7136\u5f8c Push \u81f3\u9060\u7aef\u5009\u5eab\u3002</li> <li>\u91cd\u65b0\u6574\u7406\u5009\u5eab\u7db2\u9801\uff0c\u9ede\u9078\u201cCreate pull request\u201d\u6309\u9215\u5373\u53ef\u767c\u8d77\u62c9\u53d6\u8acb\u6c42\u3002</li> </ol>"},{"location":"chapter_appendix/contribution/#3-docker","title":"3. \u00a0 Docker \u90e8\u7f72","text":"<p>\u5728 <code>hello-algo</code> \u6839\u76ee\u9304\u4e0b\uff0c\u57f7\u884c\u4ee5\u4e0b Docker \u6307\u4ee4\u78bc\uff0c\u5373\u53ef\u5728 <code>http://localhost:8000</code> \u8a2a\u554f\u672c\u5c08\u6848\uff1a</p> <pre><code>docker-compose up -d\n</code></pre> <p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\u522a\u9664\u90e8\u7f72\uff1a</p> <pre><code>docker-compose down\n</code></pre>"},{"location":"chapter_appendix/installation/","title":"16.1 \u00a0 \u7a0b\u5f0f\u8a2d\u8a08\u74b0\u5883\u5b89\u88dd","text":""},{"location":"chapter_appendix/installation/#1611-ide","title":"16.1.1 \u00a0 \u5b89\u88dd IDE","text":"<p>\u63a8\u85a6\u4f7f\u7528\u958b\u6e90\u3001\u8f15\u91cf\u7684 VS Code \u4f5c\u70ba\u672c\u5730\u6574\u5408\u958b\u767c\u74b0\u5883\uff08IDE\uff09\u3002\u8a2a\u554f VS Code \u5b98\u7db2\uff0c\u6839\u64da\u4f5c\u696d\u7cfb\u7d71\u9078\u64c7\u76f8\u61c9\u7248\u672c\u7684 VS Code \u9032\u884c\u4e0b\u8f09\u548c\u5b89\u88dd\u3002</p> <p></p> <p> \u5716 16-1 \u00a0 \u5f9e\u5b98\u7db2\u4e0b\u8f09 VS Code </p> <p>VS Code \u64c1\u6709\u5f37\u5927\u7684\u64f4\u5c55\u5305\u751f\u614b\u7cfb\u7d71\uff0c\u652f\u6301\u5927\u591a\u6578\u7a0b\u5f0f\u8a9e\u8a00\u7684\u57f7\u884c\u548c\u9664\u932f\u3002\u4ee5 Python \u70ba\u4f8b\uff0c\u5b89\u88dd\u201cPython Extension Pack\u201d\u64f4\u5c55\u5305\u4e4b\u5f8c\uff0c\u5373\u53ef\u9032\u884c Python \u7a0b\u5f0f\u78bc\u9664\u932f\u3002\u5b89\u88dd\u6b65\u9a5f\u5982\u5716 16-2 \u6240\u793a\u3002</p> <p></p> <p> \u5716 16-2 \u00a0 \u5b89\u88dd VS Code \u64f4\u5c55\u5305 </p>"},{"location":"chapter_appendix/installation/#1612","title":"16.1.2 \u00a0 \u5b89\u88dd\u8a9e\u8a00\u74b0\u5883","text":""},{"location":"chapter_appendix/installation/#1-python","title":"1. \u00a0 Python \u74b0\u5883","text":"<ol> <li>\u4e0b\u8f09\u4e26\u5b89\u88dd Miniconda3 \uff0c\u9700\u8981 Python 3.10 \u6216\u66f4\u65b0\u7248\u672c\u3002</li> <li>\u5728 VS Code \u7684\u64f4\u5145\u529f\u80fd\u5e02\u5834\u4e2d\u641c\u7d22 <code>python</code> \uff0c\u5b89\u88dd Python Extension Pack \u3002</li> <li>\uff08\u53ef\u9078\uff09\u5728\u547d\u4ee4\u5217\u8f38\u5165 <code>pip install black</code> \uff0c\u5b89\u88dd\u7a0b\u5f0f\u78bc\u683c\u5f0f\u5316\u5de5\u5177\u3002</li> </ol>"},{"location":"chapter_appendix/installation/#2-cc","title":"2. \u00a0 C/C++ \u74b0\u5883","text":"<ol> <li>Windows \u7cfb\u7d71\u9700\u8981\u5b89\u88dd MinGW\uff08\u914d\u7f6e\u6559\u7a0b\uff09\uff1bMacOS \u81ea\u5e36 Clang \uff0c\u7121\u9808\u5b89\u88dd\u3002</li> <li>\u5728 VS Code \u7684\u64f4\u5145\u529f\u80fd\u5e02\u5834\u4e2d\u641c\u7d22 <code>c++</code> \uff0c\u5b89\u88dd C/C++ Extension Pack \u3002</li> <li>\uff08\u53ef\u9078\uff09\u958b\u555f Settings \u9801\u9762\uff0c\u641c\u5c0b <code>Clang_format_fallback Style</code> \u7a0b\u5f0f\u78bc\u683c\u5f0f\u5316\u9078\u9805\uff0c\u8a2d\u5b9a\u70ba <code>{ BasedOnStyle: Microsoft, BreakBeforeBraces: Attach }</code> \u3002</li> </ol>"},{"location":"chapter_appendix/installation/#3-java","title":"3. \u00a0 Java \u74b0\u5883","text":"<ol> <li>\u4e0b\u8f09\u4e26\u5b89\u88dd OpenJDK\uff08\u7248\u672c\u9700\u6eff\u8db3 &gt; JDK 9\uff09\u3002</li> <li>\u5728 VS Code \u7684\u64f4\u5145\u529f\u80fd\u5e02\u5834\u4e2d\u641c\u7d22 <code>java</code> \uff0c\u5b89\u88dd Extension Pack for Java \u3002</li> </ol>"},{"location":"chapter_appendix/installation/#4-c","title":"4. \u00a0 C# \u74b0\u5883","text":"<ol> <li>\u4e0b\u8f09\u4e26\u5b89\u88dd .Net 8.0 \u3002</li> <li>\u5728 VS Code \u7684\u64f4\u5145\u529f\u80fd\u5e02\u5834\u4e2d\u641c\u7d22 <code>C# Dev Kit</code> \uff0c\u5b89\u88dd C# Dev Kit \uff08\u914d\u7f6e\u6559\u7a0b\uff09\u3002</li> <li>\u4e5f\u53ef\u4f7f\u7528 Visual Studio\uff08\u5b89\u88dd\u6559\u7a0b\uff09\u3002</li> </ol>"},{"location":"chapter_appendix/installation/#5-go","title":"5. \u00a0 Go \u74b0\u5883","text":"<ol> <li>\u4e0b\u8f09\u4e26\u5b89\u88dd go \u3002</li> <li>\u5728 VS Code \u7684\u64f4\u5145\u529f\u80fd\u5e02\u5834\u4e2d\u641c\u7d22 <code>go</code> \uff0c\u5b89\u88dd Go \u3002</li> <li>\u6309\u5feb\u6377\u9375 <code>Ctrl + Shift + P</code> \u64a5\u51fa\u547d\u4ee4\u6b04\uff0c\u8f38\u5165 go \uff0c\u9078\u64c7 <code>Go: Install/Update Tools</code> \uff0c\u5168\u90e8\u52fe\u9078\u4e26\u5b89\u88dd\u5373\u53ef\u3002</li> </ol>"},{"location":"chapter_appendix/installation/#6-swift","title":"6. \u00a0 Swift \u74b0\u5883","text":"<ol> <li>\u4e0b\u8f09\u4e26\u5b89\u88dd Swift \u3002</li> <li>\u5728 VS Code \u7684\u64f4\u5145\u529f\u80fd\u5e02\u5834\u4e2d\u641c\u7d22 <code>swift</code> \uff0c\u5b89\u88dd Swift for Visual Studio Code \u3002</li> </ol>"},{"location":"chapter_appendix/installation/#7-javascript","title":"7. \u00a0 JavaScript \u74b0\u5883","text":"<ol> <li>\u4e0b\u8f09\u4e26\u5b89\u88dd Node.js \u3002</li> <li>\uff08\u53ef\u9078\uff09\u5728 VS Code \u7684\u64f4\u5145\u529f\u80fd\u5e02\u5834\u4e2d\u641c\u7d22 <code>Prettier</code> \uff0c\u5b89\u88dd\u7a0b\u5f0f\u78bc\u683c\u5f0f\u5316\u5de5\u5177\u3002</li> </ol>"},{"location":"chapter_appendix/installation/#8-typescript","title":"8. \u00a0 TypeScript \u74b0\u5883","text":"<ol> <li>\u540c JavaScript \u74b0\u5883\u5b89\u88dd\u6b65\u9a5f\u3002</li> <li>\u5b89\u88dd TypeScript Execute (tsx) \u3002</li> <li>\u5728 VS Code \u7684\u64f4\u5145\u529f\u80fd\u5e02\u5834\u4e2d\u641c\u7d22 <code>typescript</code> \uff0c\u5b89\u88dd Pretty TypeScript Errors \u3002</li> </ol>"},{"location":"chapter_appendix/installation/#9-dart","title":"9. \u00a0 Dart \u74b0\u5883","text":"<ol> <li>\u4e0b\u8f09\u4e26\u5b89\u88dd Dart \u3002</li> <li>\u5728 VS Code \u7684\u64f4\u5145\u529f\u80fd\u5e02\u5834\u4e2d\u641c\u7d22 <code>dart</code> \uff0c\u5b89\u88dd Dart \u3002</li> </ol>"},{"location":"chapter_appendix/installation/#10-rust","title":"10. \u00a0 Rust \u74b0\u5883","text":"<ol> <li>\u4e0b\u8f09\u4e26\u5b89\u88dd Rust \u3002</li> <li>\u5728 VS Code \u7684\u64f4\u5145\u529f\u80fd\u5e02\u5834\u4e2d\u641c\u7d22 <code>rust</code> \uff0c\u5b89\u88dd rust-analyzer \u3002</li> </ol>"},{"location":"chapter_appendix/terminology/","title":"16.3 \u00a0 \u8853\u8a9e\u8868","text":"<p>\u8868 16-1 \u5217\u51fa\u4e86\u66f8\u4e2d\u51fa\u73fe\u7684\u91cd\u8981\u8853\u8a9e\uff0c\u503c\u5f97\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\u3002</p> <ul> <li>\u5efa\u8b70\u8a18\u4f4f\u540d\u8a5e\u7684\u82f1\u6587\u53eb\u6cd5\uff0c\u4ee5\u4fbf\u95b1\u8b80\u82f1\u6587\u6587\u737b\u3002</li> <li>\u90e8\u5206\u540d\u8a5e\u5728\u7c21\u9ad4\u4e2d\u6587\u548c\u7e41\u9ad4\u4e2d\u6587\u4e0b\u7684\u53eb\u6cd5\u4e0d\u540c\u3002</li> </ul> <p> \u8868 16-1 \u00a0 \u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u7684\u91cd\u8981\u540d\u8a5e </p> English \u7c21\u9ad4\u4e2d\u6587 \u7e41\u9ad4\u4e2d\u6587 algorithm \u7b97\u6cd5 \u6f14\u7b97\u6cd5 data structure \u6570\u636e\u7ed3\u6784 \u8cc7\u6599\u7d50\u69cb code \u4ee3\u7801 \u7a0b\u5f0f\u78bc file \u6587\u4ef6 \u6a94\u6848 function \u51fd\u6570 \u51fd\u5f0f method \u65b9\u6cd5 \u65b9\u6cd5 variable \u53d8\u91cf \u8b8a\u6578 asymptotic complexity analysis \u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 \u6f38\u8fd1\u8907\u96dc\u5ea6\u5206\u6790 time complexity \u65f6\u95f4\u590d\u6742\u5ea6 \u6642\u9593\u8907\u96dc\u5ea6 space complexity \u7a7a\u95f4\u590d\u6742\u5ea6 \u7a7a\u9593\u8907\u96dc\u5ea6 loop \u5faa\u73af \u8ff4\u5708 iteration \u8fed\u4ee3 \u8fed\u4ee3 recursion \u9012\u5f52 \u905e\u8ff4 tail recursion \u5c3e\u9012\u5f52 \u5c3e\u905e\u8ff4 recursion tree \u9012\u5f52\u6811 \u905e\u8ff4\u6a39 big-\\(O\\) notation \u5927 \\(O\\) \u8bb0\u53f7 \u5927 \\(O\\) \u8a18\u865f asymptotic upper bound \u6e10\u8fd1\u4e0a\u754c \u6f38\u8fd1\u4e0a\u754c sign-magnitude \u539f\u7801 \u539f\u78bc 1\u2019s complement \u53cd\u7801 \u4e00\u88dc\u6578 2\u2019s complement \u8865\u7801 \u4e8c\u88dc\u6578 array \u6570\u7ec4 \u9663\u5217 index \u7d22\u5f15 \u7d22\u5f15 linked list \u94fe\u8868 \u93c8\u7d50\u4e32\u5217 linked list node, list node \u94fe\u8868\u8282\u70b9 \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede head node \u5934\u8282\u70b9 \u982d\u7bc0\u9ede tail node \u5c3e\u8282\u70b9 \u5c3e\u7bc0\u9ede list \u5217\u8868 \u4e32\u5217 dynamic array \u52a8\u6001\u6570\u7ec4 \u52d5\u614b\u9663\u5217 hard disk \u786c\u76d8 \u786c\u789f random-access memory (RAM) \u5185\u5b58 \u8a18\u61b6\u9ad4 cache memory \u7f13\u5b58 \u5feb\u53d6 cache miss \u7f13\u5b58\u672a\u547d\u4e2d \u5feb\u53d6\u672a\u547d\u4e2d cache hit rate \u7f13\u5b58\u547d\u4e2d\u7387 \u5feb\u53d6\u547d\u4e2d\u7387 stack \u6808 \u5806\u758a top of the stack \u6808\u9876 \u5806\u758a\u9802 bottom of the stack \u6808\u5e95 \u5806\u758a\u5e95 queue \u961f\u5217 \u4f47\u5217 double-ended queue \u53cc\u5411\u961f\u5217 \u96d9\u5411\u4f47\u5217 front of the queue \u961f\u9996 \u4f47\u5217\u9996 rear of the queue \u961f\u5c3e \u4f47\u5217\u5c3e hash table \u54c8\u5e0c\u8868 \u96dc\u6e4a\u8868 bucket \u6876 \u6876 hash function \u54c8\u5e0c\u51fd\u6570 \u96dc\u6e4a\u51fd\u5f0f hash collision \u54c8\u5e0c\u51b2\u7a81 \u96dc\u6e4a\u885d\u7a81 load factor \u8d1f\u8f7d\u56e0\u5b50 \u8ca0\u8f09\u56e0\u5b50 separate chaining \u94fe\u5f0f\u5730\u5740 \u93c8\u7d50\u4f4d\u5740 open addressing \u5f00\u653e\u5bfb\u5740 \u958b\u653e\u5b9a\u5740 linear probing \u7ebf\u6027\u63a2\u6d4b \u7dda\u6027\u63a2\u67e5 lazy deletion \u61d2\u5220\u9664 \u61f6\u522a\u9664 binary tree \u4e8c\u53c9\u6811 \u4e8c\u5143\u6a39 tree node \u6811\u8282\u70b9 \u6a39\u7bc0\u9ede left-child node \u5de6\u5b50\u8282\u70b9 \u5de6\u5b50\u7bc0\u9ede right-child node \u53f3\u5b50\u8282\u70b9 \u53f3\u5b50\u7bc0\u9ede parent node \u7236\u8282\u70b9 \u7236\u7bc0\u9ede left subtree \u5de6\u5b50\u6811 \u5de6\u5b50\u6a39 right subtree \u53f3\u5b50\u6811 \u53f3\u5b50\u6a39 root node \u6839\u8282\u70b9 \u6839\u7bc0\u9ede leaf node \u53f6\u8282\u70b9 \u8449\u7bc0\u9ede edge \u8fb9 \u908a level \u5c42 \u5c64 degree \u5ea6 \u5ea6 height \u9ad8\u5ea6 \u9ad8\u5ea6 depth \u6df1\u5ea6 \u6df1\u5ea6 perfect binary tree \u5b8c\u7f8e\u4e8c\u53c9\u6811 \u5b8c\u7f8e\u4e8c\u5143\u6a39 complete binary tree \u5b8c\u5168\u4e8c\u53c9\u6811 \u5b8c\u5168\u4e8c\u5143\u6a39 full binary tree \u5b8c\u6ee1\u4e8c\u53c9\u6811 \u5b8c\u6eff\u4e8c\u5143\u6a39 balanced binary tree \u5e73\u8861\u4e8c\u53c9\u6811 \u5e73\u8861\u4e8c\u5143\u6a39 binary search tree \u4e8c\u53c9\u641c\u7d22\u6811 \u4e8c\u5143\u641c\u5c0b\u6a39 AVL tree AVL \u6811 AVL \u6a39 red-black tree \u7ea2\u9ed1\u6811 \u7d05\u9ed1\u6a39 level-order traversal \u5c42\u5e8f\u904d\u5386 \u5c64\u5e8f\u8d70\u8a2a breadth-first traversal \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a depth-first traversal \u6df1\u5ea6\u4f18\u5148\u904d\u5386 \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a binary search tree \u4e8c\u53c9\u641c\u7d22\u6811 \u4e8c\u5143\u641c\u5c0b\u6a39 balanced binary search tree \u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811 \u5e73\u8861\u4e8c\u5143\u641c\u5c0b\u6a39 balance factor \u5e73\u8861\u56e0\u5b50 \u5e73\u8861\u56e0\u5b50 heap \u5806 \u5806\u7a4d max heap \u5927\u9876\u5806 \u5927\u9802\u5806\u7a4d min heap \u5c0f\u9876\u5806 \u5c0f\u9802\u5806\u7a4d priority queue \u4f18\u5148\u961f\u5217 \u512a\u5148\u4f47\u5217 heapify \u5806\u5316 \u5806\u7a4d\u5316 top-\\(k\\) problem Top-\\(k\\) \u95ee\u9898 Top-\\(k\\) \u554f\u984c graph \u56fe \u5716 vertex \u9876\u70b9 \u9802\u9ede undirected graph \u65e0\u5411\u56fe \u7121\u5411\u5716 directed graph \u6709\u5411\u56fe \u6709\u5411\u5716 connected graph \u8fde\u901a\u56fe \u9023\u901a\u5716 disconnected graph \u975e\u8fde\u901a\u56fe \u975e\u9023\u901a\u5716 weighted graph \u6709\u6743\u56fe \u6709\u6b0a\u5716 adjacency \u90bb\u63a5 \u9130\u63a5 path \u8def\u5f84 \u8def\u5f91 in-degree \u5165\u5ea6 \u5165\u5ea6 out-degree \u51fa\u5ea6 \u51fa\u5ea6 adjacency matrix \u90bb\u63a5\u77e9\u9635 \u9130\u63a5\u77e9\u9663 adjacency list \u90bb\u63a5\u8868 \u9130\u63a5\u8868 breadth-first search \u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 \u5ee3\u5ea6\u512a\u5148\u641c\u5c0b depth-first search \u6df1\u5ea6\u4f18\u5148\u641c\u7d22 \u6df1\u5ea6\u512a\u5148\u641c\u5c0b binary search \u4e8c\u5206\u67e5\u627e \u4e8c\u5206\u641c\u5c0b searching algorithm \u641c\u7d22\u7b97\u6cd5 \u641c\u5c0b\u6f14\u7b97\u6cd5 sorting algorithm \u6392\u5e8f\u7b97\u6cd5 \u6392\u5e8f\u6f14\u7b97\u6cd5 selection sort \u9009\u62e9\u6392\u5e8f \u9078\u64c7\u6392\u5e8f bubble sort \u5192\u6ce1\u6392\u5e8f \u6ce1\u6cab\u6392\u5e8f insertion sort \u63d2\u5165\u6392\u5e8f \u63d2\u5165\u6392\u5e8f quick sort \u5feb\u901f\u6392\u5e8f \u5feb\u901f\u6392\u5e8f merge sort \u5f52\u5e76\u6392\u5e8f \u5408\u4f75\u6392\u5e8f heap sort \u5806\u6392\u5e8f \u5806\u7a4d\u6392\u5e8f bucket sort \u6876\u6392\u5e8f \u6876\u6392\u5e8f counting sort \u8ba1\u6570\u6392\u5e8f \u8a08\u6578\u6392\u5e8f radix sort \u57fa\u6570\u6392\u5e8f \u57fa\u6578\u6392\u5e8f divide and conquer \u5206\u6cbb \u5206\u6cbb hanota problem \u6c49\u8bfa\u5854\u95ee\u9898 \u6cb3\u5167\u5854\u554f\u984c backtracking algorithm \u56de\u6eaf\u7b97\u6cd5 \u56de\u6eaf\u6f14\u7b97\u6cd5 constraint \u7ea6\u675f \u7d04\u675f solution \u89e3 \u89e3 state \u72b6\u6001 \u72c0\u614b pruning \u526a\u679d \u526a\u679d permutations problem \u5168\u6392\u5217\u95ee\u9898 \u5168\u6392\u5217\u554f\u984c subset-sum problem \u5b50\u96c6\u548c\u95ee\u9898 \u5b50\u96c6\u5408\u554f\u984c \\(n\\)-queens problem \\(n\\) \u7687\u540e\u95ee\u9898 \\(n\\) \u7687\u540e\u554f\u984c dynamic programming \u52a8\u6001\u89c4\u5212 \u52d5\u614b\u898f\u5283 initial state \u521d\u59cb\u72b6\u6001 \u521d\u59cb\u72c0\u614b state-transition equation \u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b \u72c0\u614b\u8f49\u79fb\u65b9\u7a0b knapsack problem \u80cc\u5305\u95ee\u9898 \u80cc\u5305\u554f\u984c edit distance problem \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898 \u7de8\u8f2f\u8ddd\u96e2\u554f\u984c greedy algorithm \u8d2a\u5fc3\u7b97\u6cd5 \u8caa\u5a6a\u6f14\u7b97\u6cd5"},{"location":"chapter_array_and_linkedlist/","title":"\u7b2c 4 \u7ae0 \u00a0 \u9663\u5217\u8207\u93c8\u7d50\u4e32\u5217","text":"<p>Abstract</p> <p>\u8cc7\u6599\u7d50\u69cb\u7684\u4e16\u754c\u5982\u540c\u4e00\u5835\u539a\u5be6\u7684\u78da\u7246\u3002</p> <p>\u9663\u5217\u7684\u78da\u584a\u6574\u9f4a\u6392\u5217\uff0c\u9010\u500b\u7dca\u8cbc\u3002\u93c8\u7d50\u4e32\u5217\u7684\u78da\u584a\u5206\u6563\u5404\u8655\uff0c\u9023\u7dda\u7684\u85e4\u8513\u81ea\u7531\u5730\u7a7f\u68ad\u65bc\u78da\u7e2b\u4e4b\u9593\u3002</p>"},{"location":"chapter_array_and_linkedlist/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>4.1 \u00a0 \u9663\u5217</li> <li>4.2 \u00a0 \u93c8\u7d50\u4e32\u5217</li> <li>4.3 \u00a0 \u4e32\u5217</li> <li>4.4 \u00a0 \u8a18\u61b6\u9ad4\u8207\u5feb\u53d6 *</li> <li>4.5 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_array_and_linkedlist/array/","title":"4.1 \u00a0 \u9663\u5217","text":"<p>\u9663\u5217\uff08array\uff09\u662f\u4e00\u7a2e\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\uff0c\u5176\u5c07\u76f8\u540c\u578b\u5225\u7684\u5143\u7d20\u5132\u5b58\u5728\u9023\u7e8c\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u4e2d\u3002\u6211\u5011\u5c07\u5143\u7d20\u5728\u9663\u5217\u4e2d\u7684\u4f4d\u7f6e\u7a31\u70ba\u8a72\u5143\u7d20\u7684\u7d22\u5f15\uff08index\uff09\u3002\u5716 4-1 \u5c55\u793a\u4e86\u9663\u5217\u7684\u4e3b\u8981\u6982\u5ff5\u548c\u5132\u5b58\u65b9\u5f0f\u3002</p> <p></p> <p> \u5716 4-1 \u00a0 \u9663\u5217\u5b9a\u7fa9\u8207\u5132\u5b58\u65b9\u5f0f </p>"},{"location":"chapter_array_and_linkedlist/array/#411","title":"4.1.1 \u00a0 \u9663\u5217\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/array/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u9663\u5217","text":"<p>\u6211\u5011\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u7528\u9663\u5217\u7684\u5169\u7a2e\u521d\u59cb\u5316\u65b9\u5f0f\uff1a\u7121\u521d\u59cb\u503c\u3001\u7d66\u5b9a\u521d\u59cb\u503c\u3002\u5728\u672a\u6307\u5b9a\u521d\u59cb\u503c\u7684\u60c5\u6cc1\u4e0b\uff0c\u5927\u591a\u6578\u7a0b\u5f0f\u8a9e\u8a00\u6703\u5c07\u9663\u5217\u5143\u7d20\u521d\u59cb\u5316\u70ba \\(0\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.py<pre><code># \u521d\u59cb\u5316\u9663\u5217\narr: list[int] = [0] * 5 # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4] \n</code></pre> array.cpp<pre><code>/* \u521d\u59cb\u5316\u9663\u5217 */\n// \u5132\u5b58\u5728\u5806\u758a\u4e0a\nint arr[5];\nint nums[5] = { 1, 3, 2, 5, 4 };\n// \u5132\u5b58\u5728\u5806\u7a4d\u4e0a\uff08\u9700\u8981\u624b\u52d5\u91cb\u653e\u7a7a\u9593\uff09\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n</code></pre> array.java<pre><code>/* \u521d\u59cb\u5316\u9663\u5217 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n</code></pre> array.cs<pre><code>/* \u521d\u59cb\u5316\u9663\u5217 */\nint[] arr = new int[5]; // [ 0, 0, 0, 0, 0 ]\nint[] nums = [1, 3, 2, 5, 4];\n</code></pre> array.go<pre><code>/* \u521d\u59cb\u5316\u9663\u5217 */\nvar arr [5]int\n// \u5728 Go \u4e2d\uff0c\u6307\u5b9a\u9577\u5ea6\u6642\uff08[5]int\uff09\u70ba\u9663\u5217\uff0c\u4e0d\u6307\u5b9a\u9577\u5ea6\u6642\uff08[]int\uff09\u70ba\u5207\u7247\n// \u7531\u65bc Go \u7684\u9663\u5217\u88ab\u8a2d\u8a08\u70ba\u5728\u7de8\u8b6f\u671f\u78ba\u5b9a\u9577\u5ea6\uff0c\u56e0\u6b64\u53ea\u80fd\u4f7f\u7528\u5e38\u6578\u4f86\u6307\u5b9a\u9577\u5ea6\n// \u70ba\u4e86\u65b9\u4fbf\u5be6\u73fe\u64f4\u5bb9 extend() \u65b9\u6cd5\uff0c\u4ee5\u4e0b\u5c07\u5207\u7247\uff08Slice\uff09\u770b\u4f5c\u9663\u5217\uff08Array\uff09\nnums := []int{1, 3, 2, 5, 4}\n</code></pre> array.swift<pre><code>/* \u521d\u59cb\u5316\u9663\u5217 */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n</code></pre> array.js<pre><code>/* \u521d\u59cb\u5316\u9663\u5217 */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n</code></pre> array.ts<pre><code>/* \u521d\u59cb\u5316\u9663\u5217 */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n</code></pre> array.dart<pre><code>/* \u521d\u59cb\u5316\u9663\u5217 */\nList&lt;int&gt; arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList&lt;int&gt; nums = [1, 3, 2, 5, 4];\n</code></pre> array.rs<pre><code>/* \u521d\u59cb\u5316\u9663\u5217 */\nlet arr: Vec&lt;i32&gt; = vec![0; 5]; // [0, 0, 0, 0, 0]\nlet nums: Vec&lt;i32&gt; = vec![1, 3, 2, 5, 4];\n</code></pre> array.c<pre><code>/* \u521d\u59cb\u5316\u9663\u5217 */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n</code></pre> array.kt<pre><code>/* \u521d\u59cb\u5316\u9663\u5217 */\nvar arr = IntArray(5) // { 0, 0, 0, 0, 0 }\nvar nums = intArrayOf(1, 3, 2, 5, 4)\n</code></pre> array.rb<pre><code># \u521d\u59cb\u5316\u9663\u5217\narr = Array.new(5, 0)\nnums = [1, 3, 2, 5, 4]\n</code></pre> array.zig<pre><code>// \u521d\u59cb\u5316\u9663\u5217\nvar arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 }\nvar nums = [_]i32{ 1, 3, 2, 5, 4 };\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/array/#2","title":"2. \u00a0 \u8a2a\u554f\u5143\u7d20","text":"<p>\u9663\u5217\u5143\u7d20\u88ab\u5132\u5b58\u5728\u9023\u7e8c\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u4e2d\uff0c\u9019\u610f\u5473\u8457\u8a08\u7b97\u9663\u5217\u5143\u7d20\u7684\u8a18\u61b6\u9ad4\u4f4d\u5740\u975e\u5e38\u5bb9\u6613\u3002\u7d66\u5b9a\u9663\u5217\u8a18\u61b6\u9ad4\u4f4d\u5740\uff08\u9996\u5143\u7d20\u8a18\u61b6\u9ad4\u4f4d\u5740\uff09\u548c\u67d0\u500b\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u5716 4-2 \u6240\u793a\u7684\u516c\u5f0f\u8a08\u7b97\u5f97\u5230\u8a72\u5143\u7d20\u7684\u8a18\u61b6\u9ad4\u4f4d\u5740\uff0c\u5f9e\u800c\u76f4\u63a5\u8a2a\u554f\u8a72\u5143\u7d20\u3002</p> <p></p> <p> \u5716 4-2 \u00a0 \u9663\u5217\u5143\u7d20\u7684\u8a18\u61b6\u9ad4\u4f4d\u5740\u8a08\u7b97 </p> <p>\u89c0\u5bdf\u5716 4-2 \uff0c\u6211\u5011\u767c\u73fe\u9663\u5217\u9996\u500b\u5143\u7d20\u7684\u7d22\u5f15\u70ba \\(0\\) \uff0c\u9019\u4f3c\u4e4e\u6709\u4e9b\u53cd\u76f4\u89ba\uff0c\u56e0\u70ba\u5f9e \\(1\\) \u958b\u59cb\u8a08\u6578\u6703\u66f4\u81ea\u7136\u3002\u4f46\u5f9e\u4f4d\u5740\u8a08\u7b97\u516c\u5f0f\u7684\u89d2\u5ea6\u770b\uff0c\u7d22\u5f15\u672c\u8cea\u4e0a\u662f\u8a18\u61b6\u9ad4\u4f4d\u5740\u7684\u504f\u79fb\u91cf\u3002\u9996\u500b\u5143\u7d20\u7684\u4f4d\u5740\u504f\u79fb\u91cf\u662f \\(0\\) \uff0c\u56e0\u6b64\u5b83\u7684\u7d22\u5f15\u70ba \\(0\\) \u662f\u5408\u7406\u7684\u3002</p> <p>\u5728\u9663\u5217\u4e2d\u8a2a\u554f\u5143\u7d20\u975e\u5e38\u9ad8\u6548\uff0c\u6211\u5011\u53ef\u4ee5\u5728 \\(O(1)\\) \u6642\u9593\u5167\u96a8\u6a5f\u8a2a\u554f\u9663\u5217\u4e2d\u7684\u4efb\u610f\u4e00\u500b\u5143\u7d20\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.py<pre><code>def random_access(nums: list[int]) -&gt; int:\n \"\"\"\u96a8\u6a5f\u8a2a\u554f\u5143\u7d20\"\"\"\n # \u5728\u5340\u9593 [0, len(nums)-1] \u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u6578\u5b57\n random_index = random.randint(0, len(nums) - 1)\n # \u7372\u53d6\u4e26\u8fd4\u56de\u96a8\u6a5f\u5143\u7d20\n random_num = nums[random_index]\n return random_num\n</code></pre> array.cpp<pre><code>/* \u96a8\u6a5f\u8a2a\u554f\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n // \u5728\u5340\u9593 [0, size) \u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u6578\u5b57\n int randomIndex = rand() % size;\n // \u7372\u53d6\u4e26\u8fd4\u56de\u96a8\u6a5f\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n</code></pre> array.java<pre><code>/* \u96a8\u6a5f\u8a2a\u554f\u5143\u7d20 */\nint randomAccess(int[] nums) {\n // \u5728\u5340\u9593 [0, nums.length) \u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u6578\u5b57\n int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n // \u7372\u53d6\u4e26\u8fd4\u56de\u96a8\u6a5f\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n</code></pre> array.cs<pre><code>/* \u96a8\u6a5f\u8a2a\u554f\u5143\u7d20 */\nint RandomAccess(int[] nums) {\n Random random = new();\n // \u5728\u5340\u9593 [0, nums.Length) \u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u6578\u5b57\n int randomIndex = random.Next(nums.Length);\n // \u7372\u53d6\u4e26\u8fd4\u56de\u96a8\u6a5f\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n</code></pre> array.go<pre><code>/* \u96a8\u6a5f\u8a2a\u554f\u5143\u7d20 */\nfunc randomAccess(nums []int) (randomNum int) {\n // \u5728\u5340\u9593 [0, nums.length) \u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u6578\u5b57\n randomIndex := rand.Intn(len(nums))\n // \u7372\u53d6\u4e26\u8fd4\u56de\u96a8\u6a5f\u5143\u7d20\n randomNum = nums[randomIndex]\n return\n}\n</code></pre> array.swift<pre><code>/* \u96a8\u6a5f\u8a2a\u554f\u5143\u7d20 */\nfunc randomAccess(nums: [Int]) -&gt; Int {\n // \u5728\u5340\u9593 [0, nums.count) \u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u6578\u5b57\n let randomIndex = nums.indices.randomElement()!\n // \u7372\u53d6\u4e26\u8fd4\u56de\u96a8\u6a5f\u5143\u7d20\n let randomNum = nums[randomIndex]\n return randomNum\n}\n</code></pre> array.js<pre><code>/* \u96a8\u6a5f\u8a2a\u554f\u5143\u7d20 */\nfunction randomAccess(nums) {\n // \u5728\u5340\u9593 [0, nums.length) \u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u6578\u5b57\n const random_index = Math.floor(Math.random() * nums.length);\n // \u7372\u53d6\u4e26\u8fd4\u56de\u96a8\u6a5f\u5143\u7d20\n const random_num = nums[random_index];\n return random_num;\n}\n</code></pre> array.ts<pre><code>/* \u96a8\u6a5f\u8a2a\u554f\u5143\u7d20 */\nfunction randomAccess(nums: number[]): number {\n // \u5728\u5340\u9593 [0, nums.length) \u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u6578\u5b57\n const random_index = Math.floor(Math.random() * nums.length);\n // \u7372\u53d6\u4e26\u8fd4\u56de\u96a8\u6a5f\u5143\u7d20\n const random_num = nums[random_index];\n return random_num;\n}\n</code></pre> array.dart<pre><code>/* \u96a8\u6a5f\u8a2a\u554f\u5143\u7d20 */\nint randomAccess(List&lt;int&gt; nums) {\n // \u5728\u5340\u9593 [0, nums.length) \u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u6578\u5b57\n int randomIndex = Random().nextInt(nums.length);\n // \u7372\u53d6\u4e26\u8fd4\u56de\u96a8\u6a5f\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n</code></pre> array.rs<pre><code>/* \u96a8\u6a5f\u8a2a\u554f\u5143\u7d20 */\nfn random_access(nums: &amp;[i32]) -&gt; i32 {\n // \u5728\u5340\u9593 [0, nums.len()) \u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u6578\u5b57\n let random_index = rand::thread_rng().gen_range(0..nums.len());\n // \u7372\u53d6\u4e26\u8fd4\u56de\u96a8\u6a5f\u5143\u7d20\n let random_num = nums[random_index];\n random_num\n}\n</code></pre> array.c<pre><code>/* \u96a8\u6a5f\u8a2a\u554f\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n // \u5728\u5340\u9593 [0, size) \u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u6578\u5b57\n int randomIndex = rand() % size;\n // \u7372\u53d6\u4e26\u8fd4\u56de\u96a8\u6a5f\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n</code></pre> array.kt<pre><code>/* \u96a8\u6a5f\u8a2a\u554f\u5143\u7d20 */\nfun randomAccess(nums: IntArray): Int {\n // \u5728\u5340\u9593 [0, nums.size) \u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u6578\u5b57\n val randomIndex = ThreadLocalRandom.current().nextInt(0, nums.size)\n // \u7372\u53d6\u4e26\u8fd4\u56de\u96a8\u6a5f\u5143\u7d20\n val randomNum = nums[randomIndex]\n return randomNum\n}\n</code></pre> array.rb<pre><code>### \u96a8\u6a5f\u8a2a\u554f\u5143\u7d20 ###\ndef random_access(nums)\n # \u5728\u5340\u9593 [0, nums.length) \u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u6578\u5b57\n random_index = Random.rand(0...nums.length)\n\n # \u7372\u53d6\u4e26\u8fd4\u56de\u96a8\u6a5f\u5143\u7d20\n nums[random_index]\nend\n</code></pre> array.zig<pre><code>// \u96a8\u6a5f\u8a2a\u554f\u5143\u7d20\nfn randomAccess(nums: []i32) i32 {\n // \u5728\u5340\u9593 [0, nums.len) \u4e2d\u96a8\u6a5f\u62bd\u53d6\u4e00\u500b\u6574\u6578\n var randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len);\n // \u7372\u53d6\u4e26\u8fd4\u56de\u96a8\u6a5f\u5143\u7d20\n var randomNum = nums[randomIndex];\n return randomNum;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/array/#3","title":"3. \u00a0 \u63d2\u5165\u5143\u7d20","text":"<p>\u9663\u5217\u5143\u7d20\u5728\u8a18\u61b6\u9ad4\u4e2d\u662f\u201c\u7dca\u6328\u8457\u7684\u201d\uff0c\u5b83\u5011\u4e4b\u9593\u6c92\u6709\u7a7a\u9593\u518d\u5b58\u653e\u4efb\u4f55\u8cc7\u6599\u3002\u5982\u5716 4-3 \u6240\u793a\uff0c\u5982\u679c\u60f3\u5728\u9663\u5217\u4e2d\u9593\u63d2\u5165\u4e00\u500b\u5143\u7d20\uff0c\u5247\u9700\u8981\u5c07\u8a72\u5143\u7d20\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u90fd\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u4e4b\u5f8c\u518d\u628a\u5143\u7d20\u8ce6\u503c\u7d66\u8a72\u7d22\u5f15\u3002</p> <p></p> <p> \u5716 4-3 \u00a0 \u9663\u5217\u63d2\u5165\u5143\u7d20\u793a\u4f8b </p> <p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u65bc\u9663\u5217\u7684\u9577\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u6b64\u63d2\u5165\u4e00\u500b\u5143\u7d20\u5fc5\u5b9a\u6703\u5c0e\u81f4\u9663\u5217\u5c3e\u90e8\u5143\u7d20\u201c\u4e1f\u5931\u201d\u3002\u6211\u5011\u5c07\u9019\u500b\u554f\u984c\u7684\u89e3\u6c7a\u65b9\u6848\u7559\u5728\u201c\u4e32\u5217\u201d\u7ae0\u7bc0\u4e2d\u8a0e\u8ad6\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.py<pre><code>def insert(nums: list[int], num: int, index: int):\n \"\"\"\u5728\u9663\u5217\u7684\u7d22\u5f15 index \u8655\u63d2\u5165\u5143\u7d20 num\"\"\"\n # \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for i in range(len(nums) - 1, index, -1):\n nums[i] = nums[i - 1]\n # \u5c07 num \u8ce6\u7d66 index \u8655\u7684\u5143\u7d20\n nums[index] = num\n</code></pre> array.cpp<pre><code>/* \u5728\u9663\u5217\u7684\u7d22\u5f15 index \u8655\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (int i = size - 1; i &gt; index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c07 num \u8ce6\u7d66 index \u8655\u7684\u5143\u7d20\n nums[index] = num;\n}\n</code></pre> array.java<pre><code>/* \u5728\u9663\u5217\u7684\u7d22\u5f15 index \u8655\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int[] nums, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (int i = nums.length - 1; i &gt; index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c07 num \u8ce6\u7d66 index \u8655\u7684\u5143\u7d20\n nums[index] = num;\n}\n</code></pre> array.cs<pre><code>/* \u5728\u9663\u5217\u7684\u7d22\u5f15 index \u8655\u63d2\u5165\u5143\u7d20 num */\nvoid Insert(int[] nums, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (int i = nums.Length - 1; i &gt; index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c07 num \u8ce6\u7d66 index \u8655\u7684\u5143\u7d20\n nums[index] = num;\n}\n</code></pre> array.go<pre><code>/* \u5728\u9663\u5217\u7684\u7d22\u5f15 index \u8655\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums []int, num int, index int) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for i := len(nums) - 1; i &gt; index; i-- {\n nums[i] = nums[i-1]\n }\n // \u5c07 num \u8ce6\u7d66 index \u8655\u7684\u5143\u7d20\n nums[index] = num\n}\n</code></pre> array.swift<pre><code>/* \u5728\u9663\u5217\u7684\u7d22\u5f15 index \u8655\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums: inout [Int], num: Int, index: Int) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for i in nums.indices.dropFirst(index).reversed() {\n nums[i] = nums[i - 1]\n }\n // \u5c07 num \u8ce6\u7d66 index \u8655\u7684\u5143\u7d20\n nums[index] = num\n}\n</code></pre> array.js<pre><code>/* \u5728\u9663\u5217\u7684\u7d22\u5f15 index \u8655\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums, num, index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (let i = nums.length - 1; i &gt; index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c07 num \u8ce6\u7d66 index \u8655\u7684\u5143\u7d20\n nums[index] = num;\n}\n</code></pre> array.ts<pre><code>/* \u5728\u9663\u5217\u7684\u7d22\u5f15 index \u8655\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums: number[], num: number, index: number): void {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (let i = nums.length - 1; i &gt; index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c07 num \u8ce6\u7d66 index \u8655\u7684\u5143\u7d20\n nums[index] = num;\n}\n</code></pre> array.dart<pre><code>/* \u5728\u9663\u5217\u7684\u7d22\u5f15 index \u8655\u63d2\u5165\u5143\u7d20 _num */\nvoid insert(List&lt;int&gt; nums, int _num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (var i = nums.length - 1; i &gt; index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c07 _num \u8ce6\u7d66 index \u8655\u5143\u7d20\n nums[index] = _num;\n}\n</code></pre> array.rs<pre><code>/* \u5728\u9663\u5217\u7684\u7d22\u5f15 index \u8655\u63d2\u5165\u5143\u7d20 num */\nfn insert(nums: &amp;mut Vec&lt;i32&gt;, num: i32, index: usize) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for i in (index + 1..nums.len()).rev() {\n nums[i] = nums[i - 1];\n }\n // \u5c07 num \u8ce6\u7d66 index \u8655\u7684\u5143\u7d20\n nums[index] = num;\n}\n</code></pre> array.c<pre><code>/* \u5728\u9663\u5217\u7684\u7d22\u5f15 index \u8655\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (int i = size - 1; i &gt; index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c07 num \u8ce6\u7d66 index \u8655\u7684\u5143\u7d20\n nums[index] = num;\n}\n</code></pre> array.kt<pre><code>/* \u5728\u9663\u5217\u7684\u7d22\u5f15 index \u8655\u63d2\u5165\u5143\u7d20 num */\nfun insert(nums: IntArray, num: Int, index: Int) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (i in nums.size - 1 downTo index + 1) {\n nums[i] = nums[i - 1]\n }\n // \u5c07 num \u8ce6\u7d66 index \u8655\u7684\u5143\u7d20\n nums[index] = num\n}\n</code></pre> array.rb<pre><code>### \u5728\u9663\u5217\u7684\u7d22\u5f15 index \u8655\u63d2\u5165\u5143\u7d20 num ###\ndef insert(nums, num, index)\n # \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for i in (nums.length - 1).downto(index + 1)\n nums[i] = nums[i - 1]\n end\n\n # \u5c07 num \u8ce6\u7d66 index \u8655\u7684\u5143\u7d20\n nums[index] = num\nend\n</code></pre> array.zig<pre><code>// \u5728\u9663\u5217\u7684\u7d22\u5f15 index \u8655\u63d2\u5165\u5143\u7d20 num\nfn insert(nums: []i32, num: i32, index: usize) void {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n var i = nums.len - 1;\n while (i &gt; index) : (i -= 1) {\n nums[i] = nums[i - 1];\n }\n // \u5c07 num \u8ce6\u7d66 index \u8655\u7684\u5143\u7d20\n nums[index] = num;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/array/#4","title":"4. \u00a0 \u522a\u9664\u5143\u7d20","text":"<p>\u540c\u7406\uff0c\u5982\u5716 4-4 \u6240\u793a\uff0c\u82e5\u60f3\u522a\u9664\u7d22\u5f15 \\(i\\) \u8655\u7684\u5143\u7d20\uff0c\u5247\u9700\u8981\u628a\u7d22\u5f15 \\(i\\) \u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\u3002</p> <p></p> <p> \u5716 4-4 \u00a0 \u9663\u5217\u522a\u9664\u5143\u7d20\u793a\u4f8b </p> <p>\u8acb\u6ce8\u610f\uff0c\u522a\u9664\u5143\u7d20\u5b8c\u6210\u5f8c\uff0c\u539f\u5148\u672b\u5c3e\u7684\u5143\u7d20\u8b8a\u5f97\u201c\u7121\u610f\u7fa9\u201d\u4e86\uff0c\u6240\u4ee5\u6211\u5011\u7121\u9808\u7279\u610f\u53bb\u4fee\u6539\u5b83\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.py<pre><code>def remove(nums: list[int], index: int):\n \"\"\"\u522a\u9664\u7d22\u5f15 index \u8655\u7684\u5143\u7d20\"\"\"\n # \u628a\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for i in range(index, len(nums) - 1):\n nums[i] = nums[i + 1]\n</code></pre> array.cpp<pre><code>/* \u522a\u9664\u7d22\u5f15 index \u8655\u7684\u5143\u7d20 */\nvoid remove(int *nums, int size, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (int i = index; i &lt; size - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n</code></pre> array.java<pre><code>/* \u522a\u9664\u7d22\u5f15 index \u8655\u7684\u5143\u7d20 */\nvoid remove(int[] nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (int i = index; i &lt; nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n</code></pre> array.cs<pre><code>/* \u522a\u9664\u7d22\u5f15 index \u8655\u7684\u5143\u7d20 */\nvoid Remove(int[] nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (int i = index; i &lt; nums.Length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n</code></pre> array.go<pre><code>/* \u522a\u9664\u7d22\u5f15 index \u8655\u7684\u5143\u7d20 */\nfunc remove(nums []int, index int) {\n // \u628a\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for i := index; i &lt; len(nums)-1; i++ {\n nums[i] = nums[i+1]\n }\n}\n</code></pre> array.swift<pre><code>/* \u522a\u9664\u7d22\u5f15 index \u8655\u7684\u5143\u7d20 */\nfunc remove(nums: inout [Int], index: Int) {\n // \u628a\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for i in nums.indices.dropFirst(index).dropLast() {\n nums[i] = nums[i + 1]\n }\n}\n</code></pre> array.js<pre><code>/* \u522a\u9664\u7d22\u5f15 index \u8655\u7684\u5143\u7d20 */\nfunction remove(nums, index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (let i = index; i &lt; nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n</code></pre> array.ts<pre><code>/* \u522a\u9664\u7d22\u5f15 index \u8655\u7684\u5143\u7d20 */\nfunction remove(nums: number[], index: number): void {\n // \u628a\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (let i = index; i &lt; nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n</code></pre> array.dart<pre><code>/* \u522a\u9664\u7d22\u5f15 index \u8655\u7684\u5143\u7d20 */\nvoid remove(List&lt;int&gt; nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (var i = index; i &lt; nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n</code></pre> array.rs<pre><code>/* \u522a\u9664\u7d22\u5f15 index \u8655\u7684\u5143\u7d20 */\nfn remove(nums: &amp;mut Vec&lt;i32&gt;, index: usize) {\n // \u628a\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for i in index..nums.len() - 1 {\n nums[i] = nums[i + 1];\n }\n}\n</code></pre> array.c<pre><code>/* \u522a\u9664\u7d22\u5f15 index \u8655\u7684\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u4f54\u7528\u4e86 remove \u95dc\u9375\u8a5e\nvoid removeItem(int *nums, int size, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (int i = index; i &lt; size - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n</code></pre> array.kt<pre><code>/* \u522a\u9664\u7d22\u5f15 index \u8655\u7684\u5143\u7d20 */\nfun remove(nums: IntArray, index: Int) {\n // \u628a\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (i in index..&lt;nums.size - 1) {\n nums[i] = nums[i + 1]\n }\n}\n</code></pre> array.rb<pre><code>### \u522a\u9664\u7d22\u5f15 index \u8655\u7684\u5143\u7d20 ###\ndef remove(nums, index)\n # \u628a\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for i in index...(nums.length - 1)\n nums[i] = nums[i + 1]\n end\nend\n</code></pre> array.zig<pre><code>// \u522a\u9664\u7d22\u5f15 index \u8655\u7684\u5143\u7d20\nfn remove(nums: []i32, index: usize) void {\n // \u628a\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n var i = index;\n while (i &lt; nums.len - 1) : (i += 1) {\n nums[i] = nums[i + 1];\n }\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u7e3d\u7684\u4f86\u770b\uff0c\u9663\u5217\u7684\u63d2\u5165\u8207\u522a\u9664\u64cd\u4f5c\u6709\u4ee5\u4e0b\u7f3a\u9ede\u3002</p> <ul> <li>\u6642\u9593\u8907\u96dc\u5ea6\u9ad8\uff1a\u9663\u5217\u7684\u63d2\u5165\u548c\u522a\u9664\u7684\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u5747\u70ba \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u70ba\u9663\u5217\u9577\u5ea6\u3002</li> <li>\u4e1f\u5931\u5143\u7d20\uff1a\u7531\u65bc\u9663\u5217\u7684\u9577\u5ea6\u4e0d\u53ef\u8b8a\uff0c\u56e0\u6b64\u5728\u63d2\u5165\u5143\u7d20\u5f8c\uff0c\u8d85\u51fa\u9663\u5217\u9577\u5ea6\u7bc4\u570d\u7684\u5143\u7d20\u6703\u4e1f\u5931\u3002</li> <li>\u8a18\u61b6\u9ad4\u6d6a\u8cbb\uff1a\u6211\u5011\u53ef\u4ee5\u521d\u59cb\u5316\u4e00\u500b\u6bd4\u8f03\u9577\u7684\u9663\u5217\uff0c\u53ea\u7528\u524d\u9762\u4e00\u90e8\u5206\uff0c\u9019\u6a23\u5728\u63d2\u5165\u8cc7\u6599\u6642\uff0c\u4e1f\u5931\u7684\u672b\u5c3e\u5143\u7d20\u90fd\u662f\u201c\u7121\u610f\u7fa9\u201d\u7684\uff0c\u4f46\u9019\u6a23\u505a\u6703\u9020\u6210\u90e8\u5206\u8a18\u61b6\u9ad4\u7a7a\u9593\u6d6a\u8cbb\u3002</li> </ul>"},{"location":"chapter_array_and_linkedlist/array/#5","title":"5. \u00a0 \u8d70\u8a2a\u9663\u5217","text":"<p>\u5728\u5927\u591a\u6578\u7a0b\u5f0f\u8a9e\u8a00\u4e2d\uff0c\u6211\u5011\u65e2\u53ef\u4ee5\u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u8d70\u8a2a\u7372\u53d6\u9663\u5217\u4e2d\u7684\u6bcf\u500b\u5143\u7d20\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.py<pre><code>def traverse(nums: list[int]):\n \"\"\"\u8d70\u8a2a\u9663\u5217\"\"\"\n count = 0\n # \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\n for i in range(len(nums)):\n count += nums[i]\n # \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\u5143\u7d20\n for num in nums:\n count += num\n # \u540c\u6642\u8d70\u8a2a\u8cc7\u6599\u7d22\u5f15\u548c\u5143\u7d20\n for i, num in enumerate(nums):\n count += nums[i]\n count += num\n</code></pre> array.cpp<pre><code>/* \u8d70\u8a2a\u9663\u5217 */\nvoid traverse(int *nums, int size) {\n int count = 0;\n // \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\n for (int i = 0; i &lt; size; i++) {\n count += nums[i];\n }\n}\n</code></pre> array.java<pre><code>/* \u8d70\u8a2a\u9663\u5217 */\nvoid traverse(int[] nums) {\n int count = 0;\n // \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\n for (int i = 0; i &lt; nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\u5143\u7d20\n for (int num : nums) {\n count += num;\n }\n}\n</code></pre> array.cs<pre><code>/* \u8d70\u8a2a\u9663\u5217 */\nvoid Traverse(int[] nums) {\n int count = 0;\n // \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\n for (int i = 0; i &lt; nums.Length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\u5143\u7d20\n foreach (int num in nums) {\n count += num;\n }\n}\n</code></pre> array.go<pre><code>/* \u8d70\u8a2a\u9663\u5217 */\nfunc traverse(nums []int) {\n count := 0\n // \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\n for i := 0; i &lt; len(nums); i++ {\n count += nums[i]\n }\n count = 0\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\u5143\u7d20\n for _, num := range nums {\n count += num\n }\n // \u540c\u6642\u8d70\u8a2a\u8cc7\u6599\u7d22\u5f15\u548c\u5143\u7d20\n for i, num := range nums {\n count += nums[i]\n count += num\n }\n}\n</code></pre> array.swift<pre><code>/* \u8d70\u8a2a\u9663\u5217 */\nfunc traverse(nums: [Int]) {\n var count = 0\n // \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\n for i in nums.indices {\n count += nums[i]\n }\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\u5143\u7d20\n for num in nums {\n count += num\n }\n // \u540c\u6642\u8d70\u8a2a\u8cc7\u6599\u7d22\u5f15\u548c\u5143\u7d20\n for (i, num) in nums.enumerated() {\n count += nums[i]\n count += num\n }\n}\n</code></pre> array.js<pre><code>/* \u8d70\u8a2a\u9663\u5217 */\nfunction traverse(nums) {\n let count = 0;\n // \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\n for (let i = 0; i &lt; nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\u5143\u7d20\n for (const num of nums) {\n count += num;\n }\n}\n</code></pre> array.ts<pre><code>/* \u8d70\u8a2a\u9663\u5217 */\nfunction traverse(nums: number[]): void {\n let count = 0;\n // \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\n for (let i = 0; i &lt; nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\u5143\u7d20\n for (const num of nums) {\n count += num;\n }\n}\n</code></pre> array.dart<pre><code>/* \u8d70\u8a2a\u9663\u5217\u5143\u7d20 */\nvoid traverse(List&lt;int&gt; nums) {\n int count = 0;\n // \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\n for (var i = 0; i &lt; nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\u5143\u7d20\n for (int _num in nums) {\n count += _num;\n }\n // \u900f\u904e forEach \u65b9\u6cd5\u8d70\u8a2a\u9663\u5217\n nums.forEach((_num) {\n count += _num;\n });\n}\n</code></pre> array.rs<pre><code>/* \u8d70\u8a2a\u9663\u5217 */\nfn traverse(nums: &amp;[i32]) {\n let mut _count = 0;\n // \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\n for i in 0..nums.len() {\n _count += nums[i];\n }\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\u5143\u7d20\n for num in nums {\n _count += num;\n }\n}\n</code></pre> array.c<pre><code>/* \u8d70\u8a2a\u9663\u5217 */\nvoid traverse(int *nums, int size) {\n int count = 0;\n // \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\n for (int i = 0; i &lt; size; i++) {\n count += nums[i];\n }\n}\n</code></pre> array.kt<pre><code>/* \u8d70\u8a2a\u9663\u5217 */\nfun traverse(nums: IntArray) {\n var count = 0\n // \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\n for (i in nums.indices) {\n count += nums[i]\n }\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\u5143\u7d20\n for (j in nums) {\n count += j\n }\n}\n</code></pre> array.rb<pre><code>### \u8d70\u8a2a\u9663\u5217 ###\ndef traverse(nums)\n count = 0\n\n # \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\n for i in 0...nums.length\n count += nums[i]\n end\n\n # \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\u5143\u7d20\n for num in nums\n count += num\n end\nend\n</code></pre> array.zig<pre><code>// \u8d70\u8a2a\u9663\u5217\nfn traverse(nums: []i32) void {\n var count: i32 = 0;\n // \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u9663\u5217\n var i: i32 = 0;\n while (i &lt; nums.len) : (i += 1) {\n count += nums[i];\n }\n count = 0;\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\u5143\u7d20\n for (nums) |num| {\n count += num;\n }\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/array/#6","title":"6. \u00a0 \u67e5\u8a62\u5143\u7d20","text":"<p>\u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20\u9700\u8981\u8d70\u8a2a\u9663\u5217\uff0c\u6bcf\u8f2a\u5224\u65b7\u5143\u7d20\u503c\u662f\u5426\u5339\u914d\uff0c\u82e5\u5339\u914d\u5247\u8f38\u51fa\u5c0d\u61c9\u7d22\u5f15\u3002</p> <p>\u56e0\u70ba\u9663\u5217\u662f\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\uff0c\u6240\u4ee5\u4e0a\u8ff0\u67e5\u8a62\u64cd\u4f5c\u88ab\u7a31\u70ba\u201c\u7dda\u6027\u67e5\u8a62\u201d\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.py<pre><code>def find(nums: list[int], target: int) -&gt; int:\n \"\"\"\u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20\"\"\"\n for i in range(len(nums)):\n if nums[i] == target:\n return i\n return -1\n</code></pre> array.cpp<pre><code>/* \u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\n for (int i = 0; i &lt; size; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n</code></pre> array.java<pre><code>/* \u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target) {\n for (int i = 0; i &lt; nums.length; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n</code></pre> array.cs<pre><code>/* \u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20 */\nint Find(int[] nums, int target) {\n for (int i = 0; i &lt; nums.Length; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n</code></pre> array.go<pre><code>/* \u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums []int, target int) (index int) {\n index = -1\n for i := 0; i &lt; len(nums); i++ {\n if nums[i] == target {\n index = i\n break\n }\n }\n return\n}\n</code></pre> array.swift<pre><code>/* \u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums: [Int], target: Int) -&gt; Int {\n for i in nums.indices {\n if nums[i] == target {\n return i\n }\n }\n return -1\n}\n</code></pre> array.js<pre><code>/* \u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums, target) {\n for (let i = 0; i &lt; nums.length; i++) {\n if (nums[i] === target) return i;\n }\n return -1;\n}\n</code></pre> array.ts<pre><code>/* \u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums: number[], target: number): number {\n for (let i = 0; i &lt; nums.length; i++) {\n if (nums[i] === target) {\n return i;\n }\n }\n return -1;\n}\n</code></pre> array.dart<pre><code>/* \u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20 */\nint find(List&lt;int&gt; nums, int target) {\n for (var i = 0; i &lt; nums.length; i++) {\n if (nums[i] == target) return i;\n }\n return -1;\n}\n</code></pre> array.rs<pre><code>/* \u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20 */\nfn find(nums: &amp;[i32], target: i32) -&gt; Option&lt;usize&gt; {\n for i in 0..nums.len() {\n if nums[i] == target {\n return Some(i);\n }\n }\n None\n}\n</code></pre> array.c<pre><code>/* \u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\n for (int i = 0; i &lt; size; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n</code></pre> array.kt<pre><code>/* \u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20 */\nfun find(nums: IntArray, target: Int): Int {\n for (i in nums.indices) {\n if (nums[i] == target)\n return i\n }\n return -1\n}\n</code></pre> array.rb<pre><code>### \u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20 ###\ndef find(nums, target)\n for i in 0...nums.length\n return i if nums[i] == target\n end\n\n -1\nend\n</code></pre> array.zig<pre><code>// \u5728\u9663\u5217\u4e2d\u67e5\u8a62\u6307\u5b9a\u5143\u7d20\nfn find(nums: []i32, target: i32) i32 {\n for (nums, 0..) |num, i| {\n if (num == target) return @intCast(i);\n }\n return -1;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/array/#7","title":"7. \u00a0 \u64f4\u5bb9\u9663\u5217","text":"<p>\u5728\u8907\u96dc\u7684\u7cfb\u7d71\u74b0\u5883\u4e2d\uff0c\u7a0b\u5f0f\u96e3\u4ee5\u4fdd\u8b49\u9663\u5217\u4e4b\u5f8c\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u662f\u53ef\u7528\u7684\uff0c\u5f9e\u800c\u7121\u6cd5\u5b89\u5168\u5730\u64f4\u5c55\u9663\u5217\u5bb9\u91cf\u3002\u56e0\u6b64\u5728\u5927\u591a\u6578\u7a0b\u5f0f\u8a9e\u8a00\u4e2d\uff0c\u9663\u5217\u7684\u9577\u5ea6\u662f\u4e0d\u53ef\u8b8a\u7684\u3002</p> <p>\u5982\u679c\u6211\u5011\u5e0c\u671b\u64f4\u5bb9\u9663\u5217\uff0c\u5247\u9700\u91cd\u65b0\u5efa\u7acb\u4e00\u500b\u66f4\u5927\u7684\u9663\u5217\uff0c\u7136\u5f8c\u628a\u539f\u9663\u5217\u5143\u7d20\u4f9d\u6b21\u8907\u88fd\u5230\u65b0\u9663\u5217\u3002\u9019\u662f\u4e00\u500b \\(O(n)\\) \u7684\u64cd\u4f5c\uff0c\u5728\u9663\u5217\u5f88\u5927\u7684\u60c5\u6cc1\u4e0b\u975e\u5e38\u8017\u6642\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.py<pre><code>def extend(nums: list[int], enlarge: int) -&gt; list[int]:\n \"\"\"\u64f4\u5c55\u9663\u5217\u9577\u5ea6\"\"\"\n # \u521d\u59cb\u5316\u4e00\u500b\u64f4\u5c55\u9577\u5ea6\u5f8c\u7684\u9663\u5217\n res = [0] * (len(nums) + enlarge)\n # \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n for i in range(len(nums)):\n res[i] = nums[i]\n # \u8fd4\u56de\u64f4\u5c55\u5f8c\u7684\u65b0\u9663\u5217\n return res\n</code></pre> array.cpp<pre><code>/* \u64f4\u5c55\u9663\u5217\u9577\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u500b\u64f4\u5c55\u9577\u5ea6\u5f8c\u7684\u9663\u5217\n int *res = new int[size + enlarge];\n // \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n for (int i = 0; i &lt; size; i++) {\n res[i] = nums[i];\n }\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n delete[] nums;\n // \u8fd4\u56de\u64f4\u5c55\u5f8c\u7684\u65b0\u9663\u5217\n return res;\n}\n</code></pre> array.java<pre><code>/* \u64f4\u5c55\u9663\u5217\u9577\u5ea6 */\nint[] extend(int[] nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u500b\u64f4\u5c55\u9577\u5ea6\u5f8c\u7684\u9663\u5217\n int[] res = new int[nums.length + enlarge];\n // \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n for (int i = 0; i &lt; nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u64f4\u5c55\u5f8c\u7684\u65b0\u9663\u5217\n return res;\n}\n</code></pre> array.cs<pre><code>/* \u64f4\u5c55\u9663\u5217\u9577\u5ea6 */\nint[] Extend(int[] nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u500b\u64f4\u5c55\u9577\u5ea6\u5f8c\u7684\u9663\u5217\n int[] res = new int[nums.Length + enlarge];\n // \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n for (int i = 0; i &lt; nums.Length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u64f4\u5c55\u5f8c\u7684\u65b0\u9663\u5217\n return res;\n}\n</code></pre> array.go<pre><code>/* \u64f4\u5c55\u9663\u5217\u9577\u5ea6 */\nfunc extend(nums []int, enlarge int) []int {\n // \u521d\u59cb\u5316\u4e00\u500b\u64f4\u5c55\u9577\u5ea6\u5f8c\u7684\u9663\u5217\n res := make([]int, len(nums)+enlarge)\n // \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n for i, num := range nums {\n res[i] = num\n }\n // \u8fd4\u56de\u64f4\u5c55\u5f8c\u7684\u65b0\u9663\u5217\n return res\n}\n</code></pre> array.swift<pre><code>/* \u64f4\u5c55\u9663\u5217\u9577\u5ea6 */\nfunc extend(nums: [Int], enlarge: Int) -&gt; [Int] {\n // \u521d\u59cb\u5316\u4e00\u500b\u64f4\u5c55\u9577\u5ea6\u5f8c\u7684\u9663\u5217\n var res = Array(repeating: 0, count: nums.count + enlarge)\n // \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n for i in nums.indices {\n res[i] = nums[i]\n }\n // \u8fd4\u56de\u64f4\u5c55\u5f8c\u7684\u65b0\u9663\u5217\n return res\n}\n</code></pre> array.js<pre><code>/* \u64f4\u5c55\u9663\u5217\u9577\u5ea6 */\n// \u8acb\u6ce8\u610f\uff0cJavaScript \u7684 Array \u662f\u52d5\u614b\u9663\u5217\uff0c\u53ef\u4ee5\u76f4\u63a5\u64f4\u5c55\n// \u70ba\u4e86\u65b9\u4fbf\u5b78\u7fd2\uff0c\u672c\u51fd\u5f0f\u5c07 Array \u770b\u4f5c\u9577\u5ea6\u4e0d\u53ef\u8b8a\u7684\u9663\u5217\nfunction extend(nums, enlarge) {\n // \u521d\u59cb\u5316\u4e00\u500b\u64f4\u5c55\u9577\u5ea6\u5f8c\u7684\u9663\u5217\n const res = new Array(nums.length + enlarge).fill(0);\n // \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n for (let i = 0; i &lt; nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u64f4\u5c55\u5f8c\u7684\u65b0\u9663\u5217\n return res;\n}\n</code></pre> array.ts<pre><code>/* \u64f4\u5c55\u9663\u5217\u9577\u5ea6 */\n// \u8acb\u6ce8\u610f\uff0cTypeScript \u7684 Array \u662f\u52d5\u614b\u9663\u5217\uff0c\u53ef\u4ee5\u76f4\u63a5\u64f4\u5c55\n// \u70ba\u4e86\u65b9\u4fbf\u5b78\u7fd2\uff0c\u672c\u51fd\u5f0f\u5c07 Array \u770b\u4f5c\u9577\u5ea6\u4e0d\u53ef\u8b8a\u7684\u9663\u5217\nfunction extend(nums: number[], enlarge: number): number[] {\n // \u521d\u59cb\u5316\u4e00\u500b\u64f4\u5c55\u9577\u5ea6\u5f8c\u7684\u9663\u5217\n const res = new Array(nums.length + enlarge).fill(0);\n // \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n for (let i = 0; i &lt; nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u64f4\u5c55\u5f8c\u7684\u65b0\u9663\u5217\n return res;\n}\n</code></pre> array.dart<pre><code>/* \u64f4\u5c55\u9663\u5217\u9577\u5ea6 */\nList&lt;int&gt; extend(List&lt;int&gt; nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u500b\u64f4\u5c55\u9577\u5ea6\u5f8c\u7684\u9663\u5217\n List&lt;int&gt; res = List.filled(nums.length + enlarge, 0);\n // \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n for (var i = 0; i &lt; nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u64f4\u5c55\u5f8c\u7684\u65b0\u9663\u5217\n return res;\n}\n</code></pre> array.rs<pre><code>/* \u64f4\u5c55\u9663\u5217\u9577\u5ea6 */\nfn extend(nums: Vec&lt;i32&gt;, enlarge: usize) -&gt; Vec&lt;i32&gt; {\n // \u521d\u59cb\u5316\u4e00\u500b\u64f4\u5c55\u9577\u5ea6\u5f8c\u7684\u9663\u5217\n let mut res: Vec&lt;i32&gt; = vec![0; nums.len() + enlarge];\n // \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\n for i in 0..nums.len() {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u64f4\u5c55\u5f8c\u7684\u65b0\u9663\u5217\n res\n}\n</code></pre> array.c<pre><code>/* \u64f4\u5c55\u9663\u5217\u9577\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u500b\u64f4\u5c55\u9577\u5ea6\u5f8c\u7684\u9663\u5217\n int *res = (int *)malloc(sizeof(int) * (size + enlarge));\n // \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n for (int i = 0; i &lt; size; i++) {\n res[i] = nums[i];\n }\n // \u521d\u59cb\u5316\u64f4\u5c55\u5f8c\u7684\u7a7a\u9593\n for (int i = size; i &lt; size + enlarge; i++) {\n res[i] = 0;\n }\n // \u8fd4\u56de\u64f4\u5c55\u5f8c\u7684\u65b0\u9663\u5217\n return res;\n}\n</code></pre> array.kt<pre><code>/* \u64f4\u5c55\u9663\u5217\u9577\u5ea6 */\nfun extend(nums: IntArray, enlarge: Int): IntArray {\n // \u521d\u59cb\u5316\u4e00\u500b\u64f4\u5c55\u9577\u5ea6\u5f8c\u7684\u9663\u5217\n val res = IntArray(nums.size + enlarge)\n // \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n for (i in nums.indices) {\n res[i] = nums[i]\n }\n // \u8fd4\u56de\u64f4\u5c55\u5f8c\u7684\u65b0\u9663\u5217\n return res\n}\n</code></pre> array.rb<pre><code>### \u64f4\u5c55\u9663\u5217\u9577\u5ea6 ###\n# \u8acb\u6ce8\u610f\uff0cRuby \u7684 Array \u662f\u52d5\u614b\u9663\u5217\uff0c\u53ef\u4ee5\u76f4\u63a5\u64f4\u5c55\n# \u70ba\u4e86\u65b9\u4fbf\u5b78\u7fd2\uff0c\u672c\u51fd\u5f0f\u5c07 Array \u770b\u4f5c\u9577\u5ea6\u4e0d\u53ef\u8b8a\u7684\u9663\u5217\ndef extend(nums, enlarge)\n # \u521d\u59cb\u5316\u4e00\u500b\u64f4\u5c55\u9577\u5ea6\u5f8c\u7684\u9663\u5217\n res = Array.new(nums.length + enlarge, 0)\n\n # \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n for i in 0...nums.length\n res[i] = nums[i]\n end\n\n # \u8fd4\u56de\u64f4\u5c55\u5f8c\u7684\u65b0\u9663\u5217\n res\nend\n</code></pre> array.zig<pre><code>// \u64f4\u5c55\u9663\u5217\u9577\u5ea6\nfn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]i32 {\n // \u521d\u59cb\u5316\u4e00\u500b\u64f4\u5c55\u9577\u5ea6\u5f8c\u7684\u9663\u5217\n var res = try mem_allocator.alloc(i32, nums.len + enlarge);\n @memset(res, 0);\n // \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n std.mem.copy(i32, res, nums);\n // \u8fd4\u56de\u64f4\u5c55\u5f8c\u7684\u65b0\u9663\u5217\n return res;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/array/#412","title":"4.1.2 \u00a0 \u9663\u5217\u7684\u512a\u9ede\u8207\u4fb7\u9650\u6027","text":"<p>\u9663\u5217\u5132\u5b58\u5728\u9023\u7e8c\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u5167\uff0c\u4e14\u5143\u7d20\u578b\u5225\u76f8\u540c\u3002\u9019\u7a2e\u505a\u6cd5\u5305\u542b\u8c50\u5bcc\u7684\u5148\u9a57\u8cc7\u8a0a\uff0c\u7cfb\u7d71\u53ef\u4ee5\u5229\u7528\u9019\u4e9b\u8cc7\u8a0a\u4f86\u6700\u4f73\u5316\u8cc7\u6599\u7d50\u69cb\u7684\u64cd\u4f5c\u6548\u7387\u3002</p> <ul> <li>\u7a7a\u9593\u6548\u7387\u9ad8\uff1a\u9663\u5217\u70ba\u8cc7\u6599\u5206\u914d\u4e86\u9023\u7e8c\u7684\u8a18\u61b6\u9ad4\u584a\uff0c\u7121\u9808\u984d\u5916\u7684\u7d50\u69cb\u958b\u92b7\u3002</li> <li>\u652f\u6301\u96a8\u6a5f\u8a2a\u554f\uff1a\u9663\u5217\u5141\u8a31\u5728 \\(O(1)\\) \u6642\u9593\u5167\u8a2a\u554f\u4efb\u4f55\u5143\u7d20\u3002</li> <li>\u5feb\u53d6\u5340\u57df\u6027\uff1a\u7576\u8a2a\u554f\u9663\u5217\u5143\u7d20\u6642\uff0c\u8a08\u7b97\u6a5f\u4e0d\u50c5\u6703\u8f09\u5165\u5b83\uff0c\u9084\u6703\u5feb\u53d6\u5176\u5468\u570d\u7684\u5176\u4ed6\u8cc7\u6599\uff0c\u5f9e\u800c\u85c9\u52a9\u9ad8\u901f\u5feb\u53d6\u4f86\u63d0\u5347\u5f8c\u7e8c\u64cd\u4f5c\u7684\u57f7\u884c\u901f\u5ea6\u3002</li> </ul> <p>\u9023\u7e8c\u7a7a\u9593\u5132\u5b58\u662f\u4e00\u628a\u96d9\u5203\u528d\uff0c\u5176\u5b58\u5728\u4ee5\u4e0b\u4fb7\u9650\u6027\u3002</p> <ul> <li>\u63d2\u5165\u8207\u522a\u9664\u6548\u7387\u4f4e\uff1a\u7576\u9663\u5217\u4e2d\u5143\u7d20\u8f03\u591a\u6642\uff0c\u63d2\u5165\u8207\u522a\u9664\u64cd\u4f5c\u9700\u8981\u79fb\u52d5\u5927\u91cf\u7684\u5143\u7d20\u3002</li> <li>\u9577\u5ea6\u4e0d\u53ef\u8b8a\uff1a\u9663\u5217\u5728\u521d\u59cb\u5316\u5f8c\u9577\u5ea6\u5c31\u56fa\u5b9a\u4e86\uff0c\u64f4\u5bb9\u9663\u5217\u9700\u8981\u5c07\u6240\u6709\u8cc7\u6599\u8907\u88fd\u5230\u65b0\u9663\u5217\uff0c\u958b\u92b7\u5f88\u5927\u3002</li> <li>\u7a7a\u9593\u6d6a\u8cbb\uff1a\u5982\u679c\u9663\u5217\u5206\u914d\u7684\u5927\u5c0f\u8d85\u904e\u5be6\u969b\u6240\u9700\uff0c\u90a3\u9ebc\u591a\u9918\u7684\u7a7a\u9593\u5c31\u88ab\u6d6a\u8cbb\u4e86\u3002</li> </ul>"},{"location":"chapter_array_and_linkedlist/array/#413","title":"4.1.3 \u00a0 \u9663\u5217\u5178\u578b\u61c9\u7528","text":"<p>\u9663\u5217\u662f\u4e00\u7a2e\u57fa\u790e\u4e14\u5e38\u898b\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u65e2\u983b\u7e41\u61c9\u7528\u5728\u5404\u985e\u6f14\u7b97\u6cd5\u4e4b\u4e2d\uff0c\u4e5f\u53ef\u7528\u65bc\u5be6\u73fe\u5404\u7a2e\u8907\u96dc\u8cc7\u6599\u7d50\u69cb\u3002</p> <ul> <li>\u96a8\u6a5f\u8a2a\u554f\uff1a\u5982\u679c\u6211\u5011\u60f3\u96a8\u6a5f\u62bd\u53d6\u4e00\u4e9b\u6a23\u672c\uff0c\u90a3\u9ebc\u53ef\u4ee5\u7528\u9663\u5217\u5132\u5b58\uff0c\u4e26\u751f\u6210\u4e00\u500b\u96a8\u6a5f\u5e8f\u5217\uff0c\u6839\u64da\u7d22\u5f15\u5be6\u73fe\u96a8\u6a5f\u62bd\u6a23\u3002</li> <li>\u6392\u5e8f\u548c\u641c\u5c0b\uff1a\u9663\u5217\u662f\u6392\u5e8f\u548c\u641c\u5c0b\u6f14\u7b97\u6cd5\u6700\u5e38\u7528\u7684\u8cc7\u6599\u7d50\u69cb\u3002\u5feb\u901f\u6392\u5e8f\u3001\u5408\u4f75\u6392\u5e8f\u3001\u4e8c\u5206\u641c\u5c0b\u7b49\u90fd\u4e3b\u8981\u5728\u9663\u5217\u4e0a\u9032\u884c\u3002</li> <li>\u67e5\u8a62\u8868\uff1a\u7576\u9700\u8981\u5feb\u901f\u67e5\u8a62\u4e00\u500b\u5143\u7d20\u6216\u5176\u5c0d\u61c9\u95dc\u4fc2\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528\u9663\u5217\u4f5c\u70ba\u67e5\u8a62\u8868\u3002\u5047\u5982\u6211\u5011\u60f3\u5be6\u73fe\u5b57\u5143\u5230 ASCII \u78bc\u7684\u5c0d\u6620\uff0c\u5247\u53ef\u4ee5\u5c07\u5b57\u5143\u7684 ASCII \u78bc\u503c\u4f5c\u70ba\u7d22\u5f15\uff0c\u5c0d\u61c9\u7684\u5143\u7d20\u5b58\u653e\u5728\u9663\u5217\u4e2d\u7684\u5c0d\u61c9\u4f4d\u7f6e\u3002</li> <li>\u6a5f\u5668\u5b78\u7fd2\uff1a\u795e\u7d93\u7db2\u8def\u4e2d\u5927\u91cf\u4f7f\u7528\u4e86\u5411\u91cf\u3001\u77e9\u9663\u3001\u5f35\u91cf\u4e4b\u9593\u7684\u7dda\u6027\u4ee3\u6578\u904b\u7b97\uff0c\u9019\u4e9b\u8cc7\u6599\u90fd\u662f\u4ee5\u9663\u5217\u7684\u5f62\u5f0f\u69cb\u5efa\u7684\u3002\u9663\u5217\u662f\u795e\u7d93\u7db2\u8def\u7a0b\u5f0f\u8a2d\u8a08\u4e2d\u6700\u5e38\u4f7f\u7528\u7684\u8cc7\u6599\u7d50\u69cb\u3002</li> <li>\u8cc7\u6599\u7d50\u69cb\u5be6\u73fe\uff1a\u9663\u5217\u53ef\u4ee5\u7528\u65bc\u5be6\u73fe\u5806\u758a\u3001\u4f47\u5217\u3001\u96dc\u6e4a\u8868\u3001\u5806\u7a4d\u3001\u5716\u7b49\u8cc7\u6599\u7d50\u69cb\u3002\u4f8b\u5982\uff0c\u5716\u7684\u9130\u63a5\u77e9\u9663\u8868\u793a\u5be6\u969b\u4e0a\u662f\u4e00\u500b\u4e8c\u7dad\u9663\u5217\u3002</li> </ul>"},{"location":"chapter_array_and_linkedlist/linked_list/","title":"4.2 \u00a0 \u93c8\u7d50\u4e32\u5217","text":"<p>\u8a18\u61b6\u9ad4\u7a7a\u9593\u662f\u6240\u6709\u7a0b\u5f0f\u7684\u516c\u5171\u8cc7\u6e90\uff0c\u5728\u4e00\u500b\u8907\u96dc\u7684\u7cfb\u7d71\u57f7\u884c\u74b0\u5883\u4e0b\uff0c\u7a7a\u9592\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u53ef\u80fd\u6563\u843d\u5728\u8a18\u61b6\u9ad4\u5404\u8655\u3002\u6211\u5011\u77e5\u9053\uff0c\u5132\u5b58\u9663\u5217\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u5fc5\u9808\u662f\u9023\u7e8c\u7684\uff0c\u800c\u7576\u9663\u5217\u975e\u5e38\u5927\u6642\uff0c\u8a18\u61b6\u9ad4\u53ef\u80fd\u7121\u6cd5\u63d0\u4f9b\u5982\u6b64\u5927\u7684\u9023\u7e8c\u7a7a\u9593\u3002\u6b64\u6642\u93c8\u7d50\u4e32\u5217\u7684\u9748\u6d3b\u6027\u512a\u52e2\u5c31\u9ad4\u73fe\u51fa\u4f86\u4e86\u3002</p> <p>\u93c8\u7d50\u4e32\u5217\uff08linked list\uff09\u662f\u4e00\u7a2e\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\uff0c\u5176\u4e2d\u7684\u6bcf\u500b\u5143\u7d20\u90fd\u662f\u4e00\u500b\u7bc0\u9ede\u7269\u4ef6\uff0c\u5404\u500b\u7bc0\u9ede\u900f\u904e\u201c\u5f15\u7528\u201d\u76f8\u9023\u7dda\u3002\u5f15\u7528\u8a18\u9304\u4e86\u4e0b\u4e00\u500b\u7bc0\u9ede\u7684\u8a18\u61b6\u9ad4\u4f4d\u5740\uff0c\u900f\u904e\u5b83\u53ef\u4ee5\u5f9e\u7576\u524d\u7bc0\u9ede\u8a2a\u554f\u5230\u4e0b\u4e00\u500b\u7bc0\u9ede\u3002</p> <p>\u93c8\u7d50\u4e32\u5217\u7684\u8a2d\u8a08\u4f7f\u5f97\u5404\u500b\u7bc0\u9ede\u53ef\u4ee5\u5206\u6563\u5132\u5b58\u5728\u8a18\u61b6\u9ad4\u5404\u8655\uff0c\u5b83\u5011\u7684\u8a18\u61b6\u9ad4\u4f4d\u5740\u7121\u9808\u9023\u7e8c\u3002</p> <p></p> <p> \u5716 4-5 \u00a0 \u93c8\u7d50\u4e32\u5217\u5b9a\u7fa9\u8207\u5132\u5b58\u65b9\u5f0f </p> <p>\u89c0\u5bdf\u5716 4-5 \uff0c\u93c8\u7d50\u4e32\u5217\u7684\u7d44\u6210\u55ae\u4f4d\u662f\u7bc0\u9ede\uff08node\uff09\u7269\u4ef6\u3002\u6bcf\u500b\u7bc0\u9ede\u90fd\u5305\u542b\u5169\u9805\u8cc7\u6599\uff1a\u7bc0\u9ede\u7684\u201c\u503c\u201d\u548c\u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u201c\u5f15\u7528\u201d\u3002</p> <ul> <li>\u93c8\u7d50\u4e32\u5217\u7684\u9996\u500b\u7bc0\u9ede\u88ab\u7a31\u70ba\u201c\u982d\u7bc0\u9ede\u201d\uff0c\u6700\u5f8c\u4e00\u500b\u7bc0\u9ede\u88ab\u7a31\u70ba\u201c\u5c3e\u7bc0\u9ede\u201d\u3002</li> <li>\u5c3e\u7bc0\u9ede\u6307\u5411\u7684\u662f\u201c\u7a7a\u201d\uff0c\u5b83\u5728 Java\u3001C++ \u548c Python \u4e2d\u5206\u5225\u88ab\u8a18\u70ba <code>null</code>\u3001<code>nullptr</code> \u548c <code>None</code> \u3002</li> <li>\u5728 C\u3001C++\u3001Go \u548c Rust \u7b49\u652f\u6301\u6307\u6a19\u7684\u8a9e\u8a00\u4e2d\uff0c\u4e0a\u8ff0\u201c\u5f15\u7528\u201d\u61c9\u88ab\u66ff\u63db\u70ba\u201c\u6307\u6a19\u201d\u3002</li> </ul> <p>\u5982\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u6240\u793a\uff0c\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede <code>ListNode</code> \u9664\u4e86\u5305\u542b\u503c\uff0c\u9084\u9700\u984d\u5916\u5132\u5b58\u4e00\u500b\u5f15\u7528\uff08\u6307\u6a19\uff09\u3002\u56e0\u6b64\u5728\u76f8\u540c\u8cc7\u6599\u91cf\u4e0b\uff0c\u93c8\u7d50\u4e32\u5217\u6bd4\u9663\u5217\u4f54\u7528\u66f4\u591a\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code>class ListNode:\n \"\"\"\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225\"\"\"\n def __init__(self, val: int):\n self.val: int = val # \u7bc0\u9ede\u503c\n self.next: ListNode | None = None # \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u5f15\u7528\n</code></pre> <pre><code>/* \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u7d50\u69cb\u9ad4 */\nstruct ListNode {\n int val; // \u7bc0\u9ede\u503c\n ListNode *next; // \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u6307\u6a19\n ListNode(int x) : val(x), next(nullptr) {} // \u5efa\u69cb\u5b50\n};\n</code></pre> <pre><code>/* \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\nclass ListNode {\n int val; // \u7bc0\u9ede\u503c\n ListNode next; // \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u5f15\u7528\n ListNode(int x) { val = x; } // \u5efa\u69cb\u5b50\n}\n</code></pre> <pre><code>/* \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\nclass ListNode(int x) { //\u5efa\u69cb\u5b50\n int val = x; // \u7bc0\u9ede\u503c\n ListNode? next; // \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u5f15\u7528\n}\n</code></pre> <pre><code>/* \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u7d50\u69cb\u9ad4 */\ntype ListNode struct {\n Val int // \u7bc0\u9ede\u503c\n Next *ListNode // \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u6307\u6a19\n}\n\n// NewListNode \u5efa\u69cb\u5b50\uff0c\u5efa\u7acb\u4e00\u500b\u65b0\u7684\u93c8\u7d50\u4e32\u5217\nfunc NewListNode(val int) *ListNode {\n return &amp;ListNode{\n Val: val,\n Next: nil,\n }\n}\n</code></pre> <pre><code>/* \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\nclass ListNode {\n var val: Int // \u7bc0\u9ede\u503c\n var next: ListNode? // \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u5f15\u7528\n\n init(x: Int) { // \u5efa\u69cb\u5b50\n val = x\n }\n}\n</code></pre> <pre><code>/* \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\nclass ListNode {\n constructor(val, next) {\n this.val = (val === undefined ? 0 : val); // \u7bc0\u9ede\u503c\n this.next = (next === undefined ? null : next); // \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u5f15\u7528\n }\n}\n</code></pre> <pre><code>/* \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\nclass ListNode {\n val: number;\n next: ListNode | null;\n constructor(val?: number, next?: ListNode | null) {\n this.val = val === undefined ? 0 : val; // \u7bc0\u9ede\u503c\n this.next = next === undefined ? null : next; // \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u5f15\u7528\n }\n}\n</code></pre> <pre><code>/* \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\nclass ListNode {\n int val; // \u7bc0\u9ede\u503c\n ListNode? next; // \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u5f15\u7528\n ListNode(this.val, [this.next]); // \u5efa\u69cb\u5b50\n}\n</code></pre> <pre><code>use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\n#[derive(Debug)]\nstruct ListNode {\n val: i32, // \u7bc0\u9ede\u503c\n next: Option&lt;Rc&lt;RefCell&lt;ListNode&gt;&gt;&gt;, // \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u6307\u6a19\n}\n</code></pre> <pre><code>/* \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u7d50\u69cb\u9ad4 */\ntypedef struct ListNode {\n int val; // \u7bc0\u9ede\u503c\n struct ListNode *next; // \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u6307\u6a19\n} ListNode;\n\n/* \u5efa\u69cb\u5b50 */\nListNode *newListNode(int val) {\n ListNode *node;\n node = (ListNode *) malloc(sizeof(ListNode));\n node-&gt;val = val;\n node-&gt;next = NULL;\n return node;\n}\n</code></pre> <pre><code>/* \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\n// \u5efa\u69cb\u5b50\nclass ListNode(x: Int) {\n val _val: Int = x // \u7bc0\u9ede\u503c\n val next: ListNode? = null // \u6307\u5411\u4e0b\u4e00\u500b\u7bc0\u9ede\u7684\u5f15\u7528\n}\n</code></pre> <pre><code># \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225\nclass ListNode\n attr_accessor :val # \u7bc0\u9ede\u503c\n attr_accessor :next # \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u5f15\u7528\n\n def initialize(val=0, next_node=nil)\n @val = val\n @next = next_node\n end\nend\n</code></pre> <pre><code>// \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225\npub fn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = 0, // \u7bc0\u9ede\u503c\n next: ?*Self = null, // \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u6307\u6a19\n\n // \u5efa\u69cb\u5b50\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n }\n };\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1 \u00a0 \u93c8\u7d50\u4e32\u5217\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217","text":"<p>\u5efa\u7acb\u93c8\u7d50\u4e32\u5217\u5206\u70ba\u5169\u6b65\uff0c\u7b2c\u4e00\u6b65\u662f\u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\u7269\u4ef6\uff0c\u7b2c\u4e8c\u6b65\u662f\u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\u95dc\u4fc2\u3002\u521d\u59cb\u5316\u5b8c\u6210\u5f8c\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u5f9e\u93c8\u7d50\u4e32\u5217\u7684\u982d\u7bc0\u9ede\u51fa\u767c\uff0c\u900f\u904e\u5f15\u7528\u6307\u5411 <code>next</code> \u4f9d\u6b21\u8a2a\u554f\u6240\u6709\u7bc0\u9ede\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linked_list.py<pre><code># \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4\n# \u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n</code></pre> linked_list.cpp<pre><code>/* \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\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// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\nn0-&gt;next = n1;\nn1-&gt;next = n2;\nn2-&gt;next = n3;\nn3-&gt;next = n4;\n</code></pre> linked_list.java<pre><code>/* \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\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// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.cs<pre><code>/* \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\nListNode n0 = new(1);\nListNode n1 = new(3);\nListNode n2 = new(2);\nListNode n3 = new(5);\nListNode n4 = new(4);\n// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.go<pre><code>/* \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n</code></pre> linked_list.swift<pre><code>/* \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\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// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n</code></pre> linked_list.js<pre><code>/* \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\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// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.ts<pre><code>/* \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\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// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.dart<pre><code>/* \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\\\n// \u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\nListNode n0 = ListNode(1);\nListNode n1 = ListNode(3);\nListNode n2 = ListNode(2);\nListNode n3 = ListNode(5);\nListNode n4 = ListNode(4);\n// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.rs<pre><code>/* \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n\n// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n</code></pre> linked_list.c<pre><code>/* \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\nn0-&gt;next = n1;\nn1-&gt;next = n2;\nn2-&gt;next = n3;\nn3-&gt;next = n4;\n</code></pre> linked_list.kt<pre><code>/* \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\nval n0 = ListNode(1)\nval n1 = ListNode(3)\nval n2 = ListNode(2)\nval n3 = ListNode(5)\nval n4 = ListNode(4)\n// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.rb<pre><code># \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4\n# \u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\nn0 = ListNode.new(1)\nn1 = ListNode.new(3)\nn2 = ListNode.new(2)\nn3 = ListNode.new(5)\nn4 = ListNode.new(4)\n# \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n</code></pre> linked_list.zig<pre><code>// \u521d\u59cb\u5316\u93c8\u7d50\u4e32\u5217\n// \u521d\u59cb\u5316\u5404\u500b\u7bc0\u9ede\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// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\nn0.next = &amp;n1;\nn1.next = &amp;n2;\nn2.next = &amp;n3;\nn3.next = &amp;n4;\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u9663\u5217\u6574\u9ad4\u662f\u4e00\u500b\u8b8a\u6578\uff0c\u6bd4\u5982\u9663\u5217 <code>nums</code> \u5305\u542b\u5143\u7d20 <code>nums[0]</code> \u548c <code>nums[1]</code> \u7b49\uff0c\u800c\u93c8\u7d50\u4e32\u5217\u662f\u7531\u591a\u500b\u7368\u7acb\u7684\u7bc0\u9ede\u7269\u4ef6\u7d44\u6210\u7684\u3002\u6211\u5011\u901a\u5e38\u5c07\u982d\u7bc0\u9ede\u7576\u4f5c\u93c8\u7d50\u4e32\u5217\u7684\u4ee3\u7a31\uff0c\u6bd4\u5982\u4ee5\u4e0a\u7a0b\u5f0f\u78bc\u4e2d\u7684\u93c8\u7d50\u4e32\u5217\u53ef\u8a18\u4f5c\u93c8\u7d50\u4e32\u5217 <code>n0</code> \u3002</p>"},{"location":"chapter_array_and_linkedlist/linked_list/#2","title":"2. \u00a0 \u63d2\u5165\u7bc0\u9ede","text":"<p>\u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u63d2\u5165\u7bc0\u9ede\u975e\u5e38\u5bb9\u6613\u3002\u5982\u5716 4-6 \u6240\u793a\uff0c\u5047\u8a2d\u6211\u5011\u60f3\u5728\u76f8\u9130\u7684\u5169\u500b\u7bc0\u9ede <code>n0</code> \u548c <code>n1</code> \u4e4b\u9593\u63d2\u5165\u4e00\u500b\u65b0\u7bc0\u9ede <code>P</code> \uff0c\u5247\u53ea\u9700\u6539\u8b8a\u5169\u500b\u7bc0\u9ede\u5f15\u7528\uff08\u6307\u6a19\uff09\u5373\u53ef\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(1)\\) \u3002</p> <p>\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5728\u9663\u5217\u4e2d\u63d2\u5165\u5143\u7d20\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \uff0c\u5728\u5927\u8cc7\u6599\u91cf\u4e0b\u7684\u6548\u7387\u8f03\u4f4e\u3002</p> <p></p> <p> \u5716 4-6 \u00a0 \u93c8\u7d50\u4e32\u5217\u63d2\u5165\u7bc0\u9ede\u793a\u4f8b </p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linked_list.py<pre><code>def insert(n0: ListNode, P: ListNode):\n \"\"\"\u5728\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u63d2\u5165\u7bc0\u9ede P\"\"\"\n n1 = n0.next\n P.next = n1\n n0.next = P\n</code></pre> linked_list.cpp<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u63d2\u5165\u7bc0\u9ede P */\nvoid insert(ListNode *n0, ListNode *P) {\n ListNode *n1 = n0-&gt;next;\n P-&gt;next = n1;\n n0-&gt;next = P;\n}\n</code></pre> linked_list.java<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u63d2\u5165\u7bc0\u9ede P */\nvoid insert(ListNode n0, ListNode P) {\n ListNode n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n</code></pre> linked_list.cs<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u63d2\u5165\u7bc0\u9ede P */\nvoid Insert(ListNode n0, ListNode P) {\n ListNode? n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n</code></pre> linked_list.go<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u63d2\u5165\u7bc0\u9ede P */\nfunc insertNode(n0 *ListNode, P *ListNode) {\n n1 := n0.Next\n P.Next = n1\n n0.Next = P\n}\n</code></pre> linked_list.swift<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u63d2\u5165\u7bc0\u9ede P */\nfunc insert(n0: ListNode, P: ListNode) {\n let n1 = n0.next\n P.next = n1\n n0.next = P\n}\n</code></pre> linked_list.js<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u63d2\u5165\u7bc0\u9ede P */\nfunction insert(n0, P) {\n const n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n</code></pre> linked_list.ts<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u63d2\u5165\u7bc0\u9ede P */\nfunction insert(n0: ListNode, P: ListNode): void {\n const n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n</code></pre> linked_list.dart<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u63d2\u5165\u7bc0\u9ede P */\nvoid insert(ListNode n0, ListNode P) {\n ListNode? n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n</code></pre> linked_list.rs<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u63d2\u5165\u7bc0\u9ede P */\n#[allow(non_snake_case)]\npub fn insert&lt;T&gt;(n0: &amp;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;, P: Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;) {\n let n1 = n0.borrow_mut().next.take();\n P.borrow_mut().next = n1;\n n0.borrow_mut().next = Some(P);\n}\n</code></pre> linked_list.c<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u63d2\u5165\u7bc0\u9ede P */\nvoid insert(ListNode *n0, ListNode *P) {\n ListNode *n1 = n0-&gt;next;\n P-&gt;next = n1;\n n0-&gt;next = P;\n}\n</code></pre> linked_list.kt<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u63d2\u5165\u7bc0\u9ede P */\nfun insert(n0: ListNode?, p: ListNode?) {\n val n1 = n0?.next\n p?.next = n1\n n0?.next = p\n}\n</code></pre> linked_list.rb<pre><code>### \u5728\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u63d2\u5165\u7bc0\u9ede _p ###\n# Ruby \u7684 `p` \u662f\u4e00\u500b\u5167\u5efa\u51fd\u5f0f\uff0c `P` \u662f\u4e00\u500b\u5e38\u6578\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528 `_p` \u4ee3\u66ff\ndef insert(n0, _p)\n n1 = n0.next\n _p.next = n1\n n0.next = _p\nend\n</code></pre> linked_list.zig<pre><code>// \u5728\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u63d2\u5165\u7bc0\u9ede P\nfn insert(n0: ?*inc.ListNode(i32), P: ?*inc.ListNode(i32)) void {\n var n1 = n0.?.next;\n P.?.next = n1;\n n0.?.next = P;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/linked_list/#3","title":"3. \u00a0 \u522a\u9664\u7bc0\u9ede","text":"<p>\u5982\u5716 4-7 \u6240\u793a\uff0c\u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u522a\u9664\u7bc0\u9ede\u4e5f\u975e\u5e38\u65b9\u4fbf\uff0c\u53ea\u9700\u6539\u8b8a\u4e00\u500b\u7bc0\u9ede\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\u5373\u53ef\u3002</p> <p>\u8acb\u6ce8\u610f\uff0c\u5118\u7ba1\u5728\u522a\u9664\u64cd\u4f5c\u5b8c\u6210\u5f8c\u7bc0\u9ede <code>P</code> \u4ecd\u7136\u6307\u5411 <code>n1</code> \uff0c\u4f46\u5be6\u969b\u4e0a\u8d70\u8a2a\u6b64\u93c8\u7d50\u4e32\u5217\u5df2\u7d93\u7121\u6cd5\u8a2a\u554f\u5230 <code>P</code> \uff0c\u9019\u610f\u5473\u8457 <code>P</code> \u5df2\u7d93\u4e0d\u518d\u5c6c\u65bc\u8a72\u93c8\u7d50\u4e32\u5217\u4e86\u3002</p> <p></p> <p> \u5716 4-7 \u00a0 \u93c8\u7d50\u4e32\u5217\u522a\u9664\u7bc0\u9ede </p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linked_list.py<pre><code>def remove(n0: ListNode):\n \"\"\"\u522a\u9664\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u7684\u9996\u500b\u7bc0\u9ede\"\"\"\n if not n0.next:\n return\n # n0 -&gt; P -&gt; n1\n P = n0.next\n n1 = P.next\n n0.next = n1\n</code></pre> linked_list.cpp<pre><code>/* \u522a\u9664\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u7684\u9996\u500b\u7bc0\u9ede */\nvoid remove(ListNode *n0) {\n if (n0-&gt;next == nullptr)\n return;\n // n0 -&gt; P -&gt; n1\n ListNode *P = n0-&gt;next;\n ListNode *n1 = P-&gt;next;\n n0-&gt;next = n1;\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n delete P;\n}\n</code></pre> linked_list.java<pre><code>/* \u522a\u9664\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u7684\u9996\u500b\u7bc0\u9ede */\nvoid remove(ListNode n0) {\n if (n0.next == null)\n return;\n // n0 -&gt; P -&gt; n1\n ListNode P = n0.next;\n ListNode n1 = P.next;\n n0.next = n1;\n}\n</code></pre> linked_list.cs<pre><code>/* \u522a\u9664\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u7684\u9996\u500b\u7bc0\u9ede */\nvoid Remove(ListNode n0) {\n if (n0.next == null)\n return;\n // n0 -&gt; P -&gt; n1\n ListNode P = n0.next;\n ListNode? n1 = P.next;\n n0.next = n1;\n}\n</code></pre> linked_list.go<pre><code>/* \u522a\u9664\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u7684\u9996\u500b\u7bc0\u9ede */\nfunc removeItem(n0 *ListNode) {\n if n0.Next == nil {\n return\n }\n // n0 -&gt; P -&gt; n1\n P := n0.Next\n n1 := P.Next\n n0.Next = n1\n}\n</code></pre> linked_list.swift<pre><code>/* \u522a\u9664\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u7684\u9996\u500b\u7bc0\u9ede */\nfunc remove(n0: ListNode) {\n if n0.next == nil {\n return\n }\n // n0 -&gt; P -&gt; n1\n let P = n0.next\n let n1 = P?.next\n n0.next = n1\n}\n</code></pre> linked_list.js<pre><code>/* \u522a\u9664\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u7684\u9996\u500b\u7bc0\u9ede */\nfunction remove(n0) {\n if (!n0.next) return;\n // n0 -&gt; P -&gt; n1\n const P = n0.next;\n const n1 = P.next;\n n0.next = n1;\n}\n</code></pre> linked_list.ts<pre><code>/* \u522a\u9664\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u7684\u9996\u500b\u7bc0\u9ede */\nfunction remove(n0: ListNode): void {\n if (!n0.next) {\n return;\n }\n // n0 -&gt; P -&gt; n1\n const P = n0.next;\n const n1 = P.next;\n n0.next = n1;\n}\n</code></pre> linked_list.dart<pre><code>/* \u522a\u9664\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u7684\u9996\u500b\u7bc0\u9ede */\nvoid remove(ListNode n0) {\n if (n0.next == null) return;\n // n0 -&gt; P -&gt; n1\n ListNode P = n0.next!;\n ListNode? n1 = P.next;\n n0.next = n1;\n}\n</code></pre> linked_list.rs<pre><code>/* \u522a\u9664\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u7684\u9996\u500b\u7bc0\u9ede */\n#[allow(non_snake_case)]\npub fn remove&lt;T&gt;(n0: &amp;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;) {\n if n0.borrow().next.is_none() {\n return;\n };\n // n0 -&gt; P -&gt; n1\n let P = n0.borrow_mut().next.take();\n if let Some(node) = P {\n let n1 = node.borrow_mut().next.take();\n n0.borrow_mut().next = n1;\n }\n}\n</code></pre> linked_list.c<pre><code>/* \u522a\u9664\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u7684\u9996\u500b\u7bc0\u9ede */\n// \u6ce8\u610f\uff1astdio.h \u4f54\u7528\u4e86 remove \u95dc\u9375\u8a5e\nvoid removeItem(ListNode *n0) {\n if (!n0-&gt;next)\n return;\n // n0 -&gt; P -&gt; n1\n ListNode *P = n0-&gt;next;\n ListNode *n1 = P-&gt;next;\n n0-&gt;next = n1;\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(P);\n}\n</code></pre> linked_list.kt<pre><code>/* \u522a\u9664\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u7684\u9996\u500b\u7bc0\u9ede */\nfun remove(n0: ListNode?) {\n if (n0?.next == null)\n return\n // n0 -&gt; P -&gt; n1\n val p = n0.next\n val n1 = p?.next\n n0.next = n1\n}\n</code></pre> linked_list.rb<pre><code>### \u522a\u9664\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u7684\u9996\u500b\u7bc0\u9ede ###\ndef remove(n0)\n return if n0.next.nil?\n\n # n0 -&gt; remove_node -&gt; n1\n remove_node = n0.next\n n1 = remove_node.next\n n0.next = n1\nend\n</code></pre> linked_list.zig<pre><code>// \u522a\u9664\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede n0 \u4e4b\u5f8c\u7684\u9996\u500b\u7bc0\u9ede\nfn remove(n0: ?*inc.ListNode(i32)) void {\n if (n0.?.next == null) return;\n // n0 -&gt; P -&gt; n1\n var P = n0.?.next;\n var n1 = P.?.next;\n n0.?.next = n1;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/linked_list/#4","title":"4. \u00a0 \u8a2a\u554f\u7bc0\u9ede","text":"<p>\u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u8a2a\u554f\u7bc0\u9ede\u7684\u6548\u7387\u8f03\u4f4e\u3002\u5982\u4e0a\u4e00\u7bc0\u6240\u8ff0\uff0c\u6211\u5011\u53ef\u4ee5\u5728 \\(O(1)\\) \u6642\u9593\u4e0b\u8a2a\u554f\u9663\u5217\u4e2d\u7684\u4efb\u610f\u5143\u7d20\u3002\u93c8\u7d50\u4e32\u5217\u5247\u4e0d\u7136\uff0c\u7a0b\u5f0f\u9700\u8981\u5f9e\u982d\u7bc0\u9ede\u51fa\u767c\uff0c\u9010\u500b\u5411\u5f8c\u8d70\u8a2a\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6a19\u7bc0\u9ede\u3002\u4e5f\u5c31\u662f\u8aaa\uff0c\u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u7684\u7b2c \\(i\\) \u500b\u7bc0\u9ede\u9700\u8981\u8ff4\u5708 \\(i - 1\\) \u8f2a\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linked_list.py<pre><code>def access(head: ListNode, index: int) -&gt; ListNode | None:\n \"\"\"\u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u4e2d\u7d22\u5f15\u70ba index \u7684\u7bc0\u9ede\"\"\"\n for _ in range(index):\n if not head:\n return None\n head = head.next\n return head\n</code></pre> linked_list.cpp<pre><code>/* \u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u4e2d\u7d22\u5f15\u70ba index \u7684\u7bc0\u9ede */\nListNode *access(ListNode *head, int index) {\n for (int i = 0; i &lt; index; i++) {\n if (head == nullptr)\n return nullptr;\n head = head-&gt;next;\n }\n return head;\n}\n</code></pre> linked_list.java<pre><code>/* \u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u4e2d\u7d22\u5f15\u70ba index \u7684\u7bc0\u9ede */\nListNode access(ListNode head, int index) {\n for (int i = 0; i &lt; index; i++) {\n if (head == null)\n return null;\n head = head.next;\n }\n return head;\n}\n</code></pre> linked_list.cs<pre><code>/* \u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u4e2d\u7d22\u5f15\u70ba index \u7684\u7bc0\u9ede */\nListNode? Access(ListNode? head, int index) {\n for (int i = 0; i &lt; index; i++) {\n if (head == null)\n return null;\n head = head.next;\n }\n return head;\n}\n</code></pre> linked_list.go<pre><code>/* \u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u4e2d\u7d22\u5f15\u70ba index \u7684\u7bc0\u9ede */\nfunc access(head *ListNode, index int) *ListNode {\n for i := 0; i &lt; index; i++ {\n if head == nil {\n return nil\n }\n head = head.Next\n }\n return head\n}\n</code></pre> linked_list.swift<pre><code>/* \u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u4e2d\u7d22\u5f15\u70ba index \u7684\u7bc0\u9ede */\nfunc access(head: ListNode, index: Int) -&gt; ListNode? {\n var head: ListNode? = head\n for _ in 0 ..&lt; index {\n if head == nil {\n return nil\n }\n head = head?.next\n }\n return head\n}\n</code></pre> linked_list.js<pre><code>/* \u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u4e2d\u7d22\u5f15\u70ba index \u7684\u7bc0\u9ede */\nfunction access(head, index) {\n for (let i = 0; i &lt; index; i++) {\n if (!head) {\n return null;\n }\n head = head.next;\n }\n return head;\n}\n</code></pre> linked_list.ts<pre><code>/* \u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u4e2d\u7d22\u5f15\u70ba index \u7684\u7bc0\u9ede */\nfunction access(head: ListNode | null, index: number): ListNode | null {\n for (let i = 0; i &lt; index; i++) {\n if (!head) {\n return null;\n }\n head = head.next;\n }\n return head;\n}\n</code></pre> linked_list.dart<pre><code>/* \u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u4e2d\u7d22\u5f15\u70ba index \u7684\u7bc0\u9ede */\nListNode? access(ListNode? head, int index) {\n for (var i = 0; i &lt; index; i++) {\n if (head == null) return null;\n head = head.next;\n }\n return head;\n}\n</code></pre> linked_list.rs<pre><code>/* \u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u4e2d\u7d22\u5f15\u70ba index \u7684\u7bc0\u9ede */\npub fn access&lt;T&gt;(head: Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;, index: i32) -&gt; Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt; {\n if index &lt;= 0 {\n return head;\n };\n if let Some(node) = &amp;head.borrow().next {\n return access(node.clone(), index - 1);\n }\n\n return head;\n}\n</code></pre> linked_list.c<pre><code>/* \u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u4e2d\u7d22\u5f15\u70ba index \u7684\u7bc0\u9ede */\nListNode *access(ListNode *head, int index) {\n for (int i = 0; i &lt; index; i++) {\n if (head == NULL)\n return NULL;\n head = head-&gt;next;\n }\n return head;\n}\n</code></pre> linked_list.kt<pre><code>/* \u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u4e2d\u7d22\u5f15\u70ba index \u7684\u7bc0\u9ede */\nfun access(head: ListNode?, index: Int): ListNode? {\n var h = head\n for (i in 0..&lt;index) {\n if (h == null)\n return null\n h = h.next\n }\n return h\n}\n</code></pre> linked_list.rb<pre><code>### \u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u4e2d\u7d22\u5f15\u70ba index \u7684\u7bc0\u9ede ###\ndef access(head, index)\n for i in 0...index\n return nil if head.nil?\n head = head.next\n end\n\n head\nend\n</code></pre> linked_list.zig<pre><code>// \u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u4e2d\u7d22\u5f15\u70ba index \u7684\u7bc0\u9ede\nfn access(node: ?*inc.ListNode(i32), index: i32) ?*inc.ListNode(i32) {\n var head = node;\n var i: i32 = 0;\n while (i &lt; index) : (i += 1) {\n head = head.?.next;\n if (head == null) return null;\n }\n return head;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/linked_list/#5","title":"5. \u00a0 \u67e5\u8a62\u7bc0\u9ede","text":"<p>\u8d70\u8a2a\u93c8\u7d50\u4e32\u5217\uff0c\u67e5\u8a62\u5176\u4e2d\u503c\u70ba <code>target</code> \u7684\u7bc0\u9ede\uff0c\u8f38\u51fa\u8a72\u7bc0\u9ede\u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u7684\u7d22\u5f15\u3002\u6b64\u904e\u7a0b\u4e5f\u5c6c\u65bc\u7dda\u6027\u67e5\u8a62\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linked_list.py<pre><code>def find(head: ListNode, target: int) -&gt; int:\n \"\"\"\u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u503c\u70ba target \u7684\u9996\u500b\u7bc0\u9ede\"\"\"\n index = 0\n while head:\n if head.val == target:\n return index\n head = head.next\n index += 1\n return -1\n</code></pre> linked_list.cpp<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u503c\u70ba target \u7684\u9996\u500b\u7bc0\u9ede */\nint find(ListNode *head, int target) {\n int index = 0;\n while (head != nullptr) {\n if (head-&gt;val == target)\n return index;\n head = head-&gt;next;\n index++;\n }\n return -1;\n}\n</code></pre> linked_list.java<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u503c\u70ba target \u7684\u9996\u500b\u7bc0\u9ede */\nint find(ListNode head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target)\n return index;\n head = head.next;\n index++;\n }\n return -1;\n}\n</code></pre> linked_list.cs<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u503c\u70ba target \u7684\u9996\u500b\u7bc0\u9ede */\nint Find(ListNode? head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target)\n return index;\n head = head.next;\n index++;\n }\n return -1;\n}\n</code></pre> linked_list.go<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u503c\u70ba target \u7684\u9996\u500b\u7bc0\u9ede */\nfunc findNode(head *ListNode, target int) int {\n index := 0\n for head != nil {\n if head.Val == target {\n return index\n }\n head = head.Next\n index++\n }\n return -1\n}\n</code></pre> linked_list.swift<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u503c\u70ba target \u7684\u9996\u500b\u7bc0\u9ede */\nfunc find(head: ListNode, target: Int) -&gt; Int {\n var head: ListNode? = head\n var index = 0\n while head != nil {\n if head?.val == target {\n return index\n }\n head = head?.next\n index += 1\n }\n return -1\n}\n</code></pre> linked_list.js<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u503c\u70ba target \u7684\u9996\u500b\u7bc0\u9ede */\nfunction find(head, target) {\n let index = 0;\n while (head !== null) {\n if (head.val === target) {\n return index;\n }\n head = head.next;\n index += 1;\n }\n return -1;\n}\n</code></pre> linked_list.ts<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u503c\u70ba target \u7684\u9996\u500b\u7bc0\u9ede */\nfunction find(head: ListNode | null, target: number): number {\n let index = 0;\n while (head !== null) {\n if (head.val === target) {\n return index;\n }\n head = head.next;\n index += 1;\n }\n return -1;\n}\n</code></pre> linked_list.dart<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u503c\u70ba target \u7684\u9996\u500b\u7bc0\u9ede */\nint find(ListNode? head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target) {\n return index;\n }\n head = head.next;\n index++;\n }\n return -1;\n}\n</code></pre> linked_list.rs<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u503c\u70ba target \u7684\u9996\u500b\u7bc0\u9ede */\npub fn find&lt;T: PartialEq&gt;(head: Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;, target: T, index: i32) -&gt; i32 {\n if head.borrow().val == target {\n return index;\n };\n if let Some(node) = &amp;head.borrow_mut().next {\n return find(node.clone(), target, index + 1);\n }\n return -1;\n}\n</code></pre> linked_list.c<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u503c\u70ba target \u7684\u9996\u500b\u7bc0\u9ede */\nint find(ListNode *head, int target) {\n int index = 0;\n while (head) {\n if (head-&gt;val == target)\n return index;\n head = head-&gt;next;\n index++;\n }\n return -1;\n}\n</code></pre> linked_list.kt<pre><code>/* \u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u503c\u70ba target \u7684\u9996\u500b\u7bc0\u9ede */\nfun find(head: ListNode?, target: Int): Int {\n var index = 0\n var h = head\n while (h != null) {\n if (h._val == target)\n return index\n h = h.next\n index++\n }\n return -1\n}\n</code></pre> linked_list.rb<pre><code>### \u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u503c\u70ba target \u7684\u9996\u500b\u7bc0\u9ede ###\ndef find(head, target)\n index = 0\n while head\n return index if head.val == target\n head = head.next\n index += 1\n end\n\n -1\nend\n</code></pre> linked_list.zig<pre><code>// \u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u503c\u70ba target \u7684\u9996\u500b\u7bc0\u9ede\nfn find(node: ?*inc.ListNode(i32), target: i32) i32 {\n var head = node;\n var index: i32 = 0;\n while (head != null) {\n if (head.?.val == target) return index;\n head = head.?.next;\n index += 1;\n }\n return -1;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/linked_list/#422-vs","title":"4.2.2 \u00a0 \u9663\u5217 vs. \u93c8\u7d50\u4e32\u5217","text":"<p>\u8868 4-1 \u7e3d\u7d50\u4e86\u9663\u5217\u548c\u93c8\u7d50\u4e32\u5217\u7684\u5404\u9805\u7279\u9ede\u4e26\u5c0d\u6bd4\u4e86\u64cd\u4f5c\u6548\u7387\u3002\u7531\u65bc\u5b83\u5011\u63a1\u7528\u5169\u7a2e\u76f8\u53cd\u7684\u5132\u5b58\u7b56\u7565\uff0c\u56e0\u6b64\u5404\u7a2e\u6027\u8cea\u548c\u64cd\u4f5c\u6548\u7387\u4e5f\u5448\u73fe\u5c0d\u7acb\u7684\u7279\u9ede\u3002</p> <p> \u8868 4-1 \u00a0 \u9663\u5217\u8207\u93c8\u7d50\u4e32\u5217\u7684\u6548\u7387\u5c0d\u6bd4 </p> \u9663\u5217 \u93c8\u7d50\u4e32\u5217 \u5132\u5b58\u65b9\u5f0f \u9023\u7e8c\u8a18\u61b6\u9ad4\u7a7a\u9593 \u5206\u6563\u8a18\u61b6\u9ad4\u7a7a\u9593 \u5bb9\u91cf\u64f4\u5c55 \u9577\u5ea6\u4e0d\u53ef\u8b8a \u53ef\u9748\u6d3b\u64f4\u5c55 \u8a18\u61b6\u9ad4\u6548\u7387 \u5143\u7d20\u4f54\u7528\u8a18\u61b6\u9ad4\u5c11\u3001\u4f46\u53ef\u80fd\u6d6a\u8cbb\u7a7a\u9593 \u5143\u7d20\u4f54\u7528\u8a18\u61b6\u9ad4\u591a \u8a2a\u554f\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \u65b0\u589e\u5143\u7d20 \\(O(n)\\) \\(O(1)\\) \u522a\u9664\u5143\u7d20 \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3 \u00a0 \u5e38\u898b\u93c8\u7d50\u4e32\u5217\u578b\u5225","text":"<p>\u5982\u5716 4-8 \u6240\u793a\uff0c\u5e38\u898b\u7684\u93c8\u7d50\u4e32\u5217\u578b\u5225\u5305\u62ec\u4e09\u7a2e\u3002</p> <ul> <li>\u55ae\u5411\u93c8\u7d50\u4e32\u5217\uff1a\u5373\u524d\u9762\u4ecb\u7d39\u7684\u666e\u901a\u93c8\u7d50\u4e32\u5217\u3002\u55ae\u5411\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede\u5305\u542b\u503c\u548c\u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u5f15\u7528\u5169\u9805\u8cc7\u6599\u3002\u6211\u5011\u5c07\u9996\u500b\u7bc0\u9ede\u7a31\u70ba\u982d\u7bc0\u9ede\uff0c\u5c07\u6700\u5f8c\u4e00\u500b\u7bc0\u9ede\u7a31\u70ba\u5c3e\u7bc0\u9ede\uff0c\u5c3e\u7bc0\u9ede\u6307\u5411\u7a7a <code>None</code> \u3002</li> <li>\u74b0\u5f62\u93c8\u7d50\u4e32\u5217\uff1a\u5982\u679c\u6211\u5011\u4ee4\u55ae\u5411\u93c8\u7d50\u4e32\u5217\u7684\u5c3e\u7bc0\u9ede\u6307\u5411\u982d\u7bc0\u9ede\uff08\u9996\u5c3e\u76f8\u63a5\uff09\uff0c\u5247\u5f97\u5230\u4e00\u500b\u74b0\u5f62\u93c8\u7d50\u4e32\u5217\u3002\u5728\u74b0\u5f62\u93c8\u7d50\u4e32\u5217\u4e2d\uff0c\u4efb\u610f\u7bc0\u9ede\u90fd\u53ef\u4ee5\u8996\u4f5c\u982d\u7bc0\u9ede\u3002</li> <li>\u96d9\u5411\u93c8\u7d50\u4e32\u5217\uff1a\u8207\u55ae\u5411\u93c8\u7d50\u4e32\u5217\u76f8\u6bd4\uff0c\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u8a18\u9304\u4e86\u5169\u500b\u65b9\u5411\u7684\u5f15\u7528\u3002\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7684\u7bc0\u9ede\u5b9a\u7fa9\u540c\u6642\u5305\u542b\u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\uff08\u4e0b\u4e00\u500b\u7bc0\u9ede\uff09\u548c\u524d\u9a45\u7bc0\u9ede\uff08\u4e0a\u4e00\u500b\u7bc0\u9ede\uff09\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\u3002\u76f8\u8f03\u65bc\u55ae\u5411\u93c8\u7d50\u4e32\u5217\uff0c\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u66f4\u5177\u9748\u6d3b\u6027\uff0c\u53ef\u4ee5\u671d\u5169\u500b\u65b9\u5411\u8d70\u8a2a\u93c8\u7d50\u4e32\u5217\uff0c\u4f46\u76f8\u61c9\u5730\u4e5f\u9700\u8981\u4f54\u7528\u66f4\u591a\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u3002</li> </ul> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code>class ListNode:\n \"\"\"\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225\"\"\"\n def __init__(self, val: int):\n self.val: int = val # \u7bc0\u9ede\u503c\n self.next: ListNode | None = None # \u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\u7684\u5f15\u7528\n self.prev: ListNode | None = None # \u6307\u5411\u524d\u9a45\u7bc0\u9ede\u7684\u5f15\u7528\n</code></pre> <pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u7d50\u69cb\u9ad4 */\nstruct ListNode {\n int val; // \u7bc0\u9ede\u503c\n ListNode *next; // \u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\u7684\u6307\u6a19\n ListNode *prev; // \u6307\u5411\u524d\u9a45\u7bc0\u9ede\u7684\u6307\u6a19\n ListNode(int x) : val(x), next(nullptr), prev(nullptr) {} // \u5efa\u69cb\u5b50\n};\n</code></pre> <pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\nclass ListNode {\n int val; // \u7bc0\u9ede\u503c\n ListNode next; // \u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\u7684\u5f15\u7528\n ListNode prev; // \u6307\u5411\u524d\u9a45\u7bc0\u9ede\u7684\u5f15\u7528\n ListNode(int x) { val = x; } // \u5efa\u69cb\u5b50\n}\n</code></pre> <pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\nclass ListNode(int x) { // \u5efa\u69cb\u5b50\n int val = x; // \u7bc0\u9ede\u503c\n ListNode next; // \u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\u7684\u5f15\u7528\n ListNode prev; // \u6307\u5411\u524d\u9a45\u7bc0\u9ede\u7684\u5f15\u7528\n}\n</code></pre> <pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u7d50\u69cb\u9ad4 */\ntype DoublyListNode struct {\n Val int // \u7bc0\u9ede\u503c\n Next *DoublyListNode // \u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\u7684\u6307\u6a19\n Prev *DoublyListNode // \u6307\u5411\u524d\u9a45\u7bc0\u9ede\u7684\u6307\u6a19\n}\n\n// NewDoublyListNode \u521d\u59cb\u5316\nfunc NewDoublyListNode(val int) *DoublyListNode {\n return &amp;DoublyListNode{\n Val: val,\n Next: nil,\n Prev: nil,\n }\n}\n</code></pre> <pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\nclass ListNode {\n var val: Int // \u7bc0\u9ede\u503c\n var next: ListNode? // \u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\u7684\u5f15\u7528\n var prev: ListNode? // \u6307\u5411\u524d\u9a45\u7bc0\u9ede\u7684\u5f15\u7528\n\n init(x: Int) { // \u5efa\u69cb\u5b50\n val = x\n }\n}\n</code></pre> <pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\nclass ListNode {\n constructor(val, next, prev) {\n this.val = val === undefined ? 0 : val; // \u7bc0\u9ede\u503c\n this.next = next === undefined ? null : next; // \u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\u7684\u5f15\u7528\n this.prev = prev === undefined ? null : prev; // \u6307\u5411\u524d\u9a45\u7bc0\u9ede\u7684\u5f15\u7528\n }\n}\n</code></pre> <pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\nclass ListNode {\n val: number;\n next: ListNode | null;\n prev: ListNode | null;\n constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\n this.val = val === undefined ? 0 : val; // \u7bc0\u9ede\u503c\n this.next = next === undefined ? null : next; // \u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\u7684\u5f15\u7528\n this.prev = prev === undefined ? null : prev; // \u6307\u5411\u524d\u9a45\u7bc0\u9ede\u7684\u5f15\u7528\n }\n}\n</code></pre> <pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\nclass ListNode {\n int val; // \u7bc0\u9ede\u503c\n ListNode next; // \u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\u7684\u5f15\u7528\n ListNode prev; // \u6307\u5411\u524d\u9a45\u7bc0\u9ede\u7684\u5f15\u7528\n ListNode(this.val, [this.next, this.prev]); // \u5efa\u69cb\u5b50\n}\n</code></pre> <pre><code>use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u578b\u5225 */\n#[derive(Debug)]\nstruct ListNode {\n val: i32, // \u7bc0\u9ede\u503c\n next: Option&lt;Rc&lt;RefCell&lt;ListNode&gt;&gt;&gt;, // \u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\u7684\u6307\u6a19\n prev: Option&lt;Rc&lt;RefCell&lt;ListNode&gt;&gt;&gt;, // \u6307\u5411\u524d\u9a45\u7bc0\u9ede\u7684\u6307\u6a19\n}\n\n/* \u5efa\u69cb\u5b50 */\nimpl ListNode {\n fn new(val: i32) -&gt; Self {\n ListNode {\n val,\n next: None,\n prev: None,\n }\n }\n}\n</code></pre> <pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u7d50\u69cb\u9ad4 */\ntypedef struct ListNode {\n int val; // \u7bc0\u9ede\u503c\n struct ListNode *next; // \u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\u7684\u6307\u6a19\n struct ListNode *prev; // \u6307\u5411\u524d\u9a45\u7bc0\u9ede\u7684\u6307\u6a19\n} ListNode;\n\n/* \u5efa\u69cb\u5b50 */\nListNode *newListNode(int val) {\n ListNode *node;\n node = (ListNode *) malloc(sizeof(ListNode));\n node-&gt;val = val;\n node-&gt;next = NULL;\n node-&gt;prev = NULL;\n return node;\n}\n</code></pre> <pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225 */\n// \u5efa\u69cb\u5b50\nclass ListNode(x: Int) {\n val _val: Int = x // \u7bc0\u9ede\u503c\n val next: ListNode? = null // \u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\u7684\u5f15\u7528\n val prev: ListNode? = null // \u6307\u5411\u524d\u9a45\u7bc0\u9ede\u7684\u5f15\u7528\n}\n</code></pre> <pre><code># \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225\nclass ListNode\n attr_accessor :val # \u7bc0\u9ede\u503c\n attr_accessor :next # \u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\u7684\u5f15\u7528\n attr_accessor :prev # \u6307\u5411\u524d\u9a45\u7bc0\u9ede\u7684\u5f15\u7528\n\n def initialize(val=0, next_node=nil, prev_node=nil)\n @val = val\n @next = next_node\n @prev = prev_node\n end\nend\n</code></pre> <pre><code>// \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u985e\u5225\npub fn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = 0, // \u7bc0\u9ede\u503c\n next: ?*Self = null, // \u6307\u5411\u5f8c\u7e7c\u7bc0\u9ede\u7684\u6307\u6a19\n prev: ?*Self = null, // \u6307\u5411\u524d\u9a45\u7bc0\u9ede\u7684\u6307\u6a19\n\n // \u5efa\u69cb\u5b50\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n self.prev = null;\n }\n };\n}\n</code></pre> <p></p> <p> \u5716 4-8 \u00a0 \u5e38\u898b\u93c8\u7d50\u4e32\u5217\u7a2e\u985e </p>"},{"location":"chapter_array_and_linkedlist/linked_list/#424","title":"4.2.4 \u00a0 \u93c8\u7d50\u4e32\u5217\u5178\u578b\u61c9\u7528","text":"<p>\u55ae\u5411\u93c8\u7d50\u4e32\u5217\u901a\u5e38\u7528\u65bc\u5be6\u73fe\u5806\u758a\u3001\u4f47\u5217\u3001\u96dc\u6e4a\u8868\u548c\u5716\u7b49\u8cc7\u6599\u7d50\u69cb\u3002</p> <ul> <li>\u5806\u758a\u8207\u4f47\u5217\uff1a\u7576\u63d2\u5165\u548c\u522a\u9664\u64cd\u4f5c\u90fd\u5728\u93c8\u7d50\u4e32\u5217\u7684\u4e00\u7aef\u9032\u884c\u6642\uff0c\u5b83\u8868\u73fe\u7684\u7279\u6027\u70ba\u5148\u9032\u5f8c\u51fa\uff0c\u5c0d\u61c9\u5806\u758a\uff1b\u7576\u63d2\u5165\u64cd\u4f5c\u5728\u93c8\u7d50\u4e32\u5217\u7684\u4e00\u7aef\u9032\u884c\uff0c\u522a\u9664\u64cd\u4f5c\u5728\u93c8\u7d50\u4e32\u5217\u7684\u53e6\u4e00\u7aef\u9032\u884c\uff0c\u5b83\u8868\u73fe\u7684\u7279\u6027\u70ba\u5148\u9032\u5148\u51fa\uff0c\u5c0d\u61c9\u4f47\u5217\u3002</li> <li>\u96dc\u6e4a\u8868\uff1a\u93c8\u5f0f\u4f4d\u5740\u662f\u89e3\u6c7a\u96dc\u6e4a\u885d\u7a81\u7684\u4e3b\u6d41\u65b9\u6848\u4e4b\u4e00\uff0c\u5728\u8a72\u65b9\u6848\u4e2d\uff0c\u6240\u6709\u885d\u7a81\u7684\u5143\u7d20\u90fd\u6703\u88ab\u653e\u5230\u4e00\u500b\u93c8\u7d50\u4e32\u5217\u4e2d\u3002</li> <li>\u5716\uff1a\u9130\u63a5\u8868\u662f\u8868\u793a\u5716\u7684\u4e00\u7a2e\u5e38\u7528\u65b9\u5f0f\uff0c\u5176\u4e2d\u5716\u7684\u6bcf\u500b\u9802\u9ede\u90fd\u8207\u4e00\u500b\u93c8\u7d50\u4e32\u5217\u76f8\u95dc\u806f\uff0c\u93c8\u7d50\u4e32\u5217\u4e2d\u7684\u6bcf\u500b\u5143\u7d20\u90fd\u4ee3\u8868\u8207\u8a72\u9802\u9ede\u76f8\u9023\u7684\u5176\u4ed6\u9802\u9ede\u3002</li> </ul> <p>\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5e38\u7528\u65bc\u9700\u8981\u5feb\u901f\u67e5\u8a62\u524d\u4e00\u500b\u548c\u5f8c\u4e00\u500b\u5143\u7d20\u7684\u5834\u666f\u3002</p> <ul> <li>\u9ad8\u968e\u8cc7\u6599\u7d50\u69cb\uff1a\u6bd4\u5982\u5728\u7d05\u9ed1\u6a39\u3001B \u6a39\u4e2d\uff0c\u6211\u5011\u9700\u8981\u8a2a\u554f\u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\uff0c\u9019\u53ef\u4ee5\u900f\u904e\u5728\u7bc0\u9ede\u4e2d\u5132\u5b58\u4e00\u500b\u6307\u5411\u7236\u7bc0\u9ede\u7684\u5f15\u7528\u4f86\u5be6\u73fe\uff0c\u985e\u4f3c\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u3002</li> <li>\u700f\u89bd\u5668\u6b77\u53f2\uff1a\u5728\u7db2\u9801\u700f\u89bd\u5668\u4e2d\uff0c\u7576\u7528\u6236\u9ede\u9078\u524d\u9032\u6216\u5f8c\u9000\u6309\u9215\u6642\uff0c\u700f\u89bd\u5668\u9700\u8981\u77e5\u9053\u4f7f\u7528\u8005\u8a2a\u554f\u904e\u7684\u524d\u4e00\u500b\u548c\u5f8c\u4e00\u500b\u7db2\u9801\u3002\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7684\u7279\u6027\u4f7f\u5f97\u9019\u7a2e\u64cd\u4f5c\u8b8a\u5f97\u7c21\u55ae\u3002</li> <li>LRU \u6f14\u7b97\u6cd5\uff1a\u5728\u5feb\u53d6\u6dd8\u6c70\uff08LRU\uff09\u6f14\u7b97\u6cd5\u4e2d\uff0c\u6211\u5011\u9700\u8981\u5feb\u901f\u627e\u5230\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u8cc7\u6599\uff0c\u4ee5\u53ca\u652f\u6301\u5feb\u901f\u65b0\u589e\u548c\u522a\u9664\u7bc0\u9ede\u3002\u9019\u6642\u5019\u4f7f\u7528\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5c31\u975e\u5e38\u5408\u9069\u3002</li> </ul> <p>\u74b0\u5f62\u93c8\u7d50\u4e32\u5217\u5e38\u7528\u65bc\u9700\u8981\u9031\u671f\u6027\u64cd\u4f5c\u7684\u5834\u666f\uff0c\u6bd4\u5982\u4f5c\u696d\u7cfb\u7d71\u7684\u8cc7\u6e90\u6392\u7a0b\u3002</p> <ul> <li>\u6642\u9593\u7247\u8f2a\u8f49\u6392\u7a0b\u6f14\u7b97\u6cd5\uff1a\u5728\u4f5c\u696d\u7cfb\u7d71\u4e2d\uff0c\u6642\u9593\u7247\u8f2a\u8f49\u6392\u7a0b\u6f14\u7b97\u6cd5\u662f\u4e00\u7a2e\u5e38\u898b\u7684 CPU \u6392\u7a0b\u6f14\u7b97\u6cd5\uff0c\u5b83\u9700\u8981\u5c0d\u4e00\u7d44\u7a0b\u5e8f\u9032\u884c\u8ff4\u5708\u3002\u6bcf\u500b\u7a0b\u5e8f\u88ab\u8ce6\u4e88\u4e00\u500b\u6642\u9593\u7247\uff0c\u7576\u6642\u9593\u7247\u7528\u5b8c\u6642\uff0cCPU \u5c07\u5207\u63db\u5230\u4e0b\u4e00\u500b\u7a0b\u5e8f\u3002\u9019\u7a2e\u8ff4\u5708\u64cd\u4f5c\u53ef\u4ee5\u900f\u904e\u74b0\u5f62\u93c8\u7d50\u4e32\u5217\u4f86\u5be6\u73fe\u3002</li> <li>\u8cc7\u6599\u7de9\u885d\u5340\uff1a\u5728\u67d0\u4e9b\u8cc7\u6599\u7de9\u885d\u5340\u7684\u5be6\u73fe\u4e2d\uff0c\u4e5f\u53ef\u80fd\u6703\u4f7f\u7528\u74b0\u5f62\u93c8\u7d50\u4e32\u5217\u3002\u6bd4\u5982\u5728\u97f3\u8a0a\u3001\u5f71\u7247\u64ad\u653e\u5668\u4e2d\uff0c\u8cc7\u6599\u6d41\u53ef\u80fd\u6703\u88ab\u5206\u6210\u591a\u500b\u7de9\u885d\u584a\u4e26\u653e\u5165\u4e00\u500b\u74b0\u5f62\u93c8\u7d50\u4e32\u5217\uff0c\u4ee5\u4fbf\u5be6\u73fe\u7121\u7e2b\u64ad\u653e\u3002</li> </ul>"},{"location":"chapter_array_and_linkedlist/list/","title":"4.3 \u00a0 \u4e32\u5217","text":"<p>\u4e32\u5217\uff08list\uff09\u662f\u4e00\u500b\u62bd\u8c61\u7684\u8cc7\u6599\u7d50\u69cb\u6982\u5ff5\uff0c\u5b83\u8868\u793a\u5143\u7d20\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u652f\u6301\u5143\u7d20\u8a2a\u554f\u3001\u4fee\u6539\u3001\u65b0\u589e\u3001\u522a\u9664\u548c\u8d70\u8a2a\u7b49\u64cd\u4f5c\uff0c\u7121\u9808\u4f7f\u7528\u8005\u8003\u616e\u5bb9\u91cf\u9650\u5236\u7684\u554f\u984c\u3002\u4e32\u5217\u53ef\u4ee5\u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u6216\u9663\u5217\u5be6\u73fe\u3002</p> <ul> <li>\u93c8\u7d50\u4e32\u5217\u5929\u7136\u53ef\u4ee5\u770b\u4f5c\u4e00\u500b\u4e32\u5217\uff0c\u5176\u652f\u6301\u5143\u7d20\u589e\u522a\u67e5\u6539\u64cd\u4f5c\uff0c\u4e26\u4e14\u53ef\u4ee5\u9748\u6d3b\u52d5\u614b\u64f4\u5bb9\u3002</li> <li>\u9663\u5217\u4e5f\u652f\u6301\u5143\u7d20\u589e\u522a\u67e5\u6539\uff0c\u4f46\u7531\u65bc\u5176\u9577\u5ea6\u4e0d\u53ef\u8b8a\uff0c\u56e0\u6b64\u53ea\u80fd\u770b\u4f5c\u4e00\u500b\u5177\u6709\u9577\u5ea6\u9650\u5236\u7684\u4e32\u5217\u3002</li> </ul> <p>\u7576\u4f7f\u7528\u9663\u5217\u5be6\u73fe\u4e32\u5217\u6642\uff0c\u9577\u5ea6\u4e0d\u53ef\u8b8a\u7684\u6027\u8cea\u6703\u5c0e\u81f4\u4e32\u5217\u7684\u5be6\u7528\u6027\u964d\u4f4e\u3002\u9019\u662f\u56e0\u70ba\u6211\u5011\u901a\u5e38\u7121\u6cd5\u4e8b\u5148\u78ba\u5b9a\u9700\u8981\u5132\u5b58\u591a\u5c11\u8cc7\u6599\uff0c\u5f9e\u800c\u96e3\u4ee5\u9078\u64c7\u5408\u9069\u7684\u4e32\u5217\u9577\u5ea6\u3002\u82e5\u9577\u5ea6\u904e\u5c0f\uff0c\u5247\u5f88\u53ef\u80fd\u7121\u6cd5\u6eff\u8db3\u4f7f\u7528\u9700\u6c42\uff1b\u82e5\u9577\u5ea6\u904e\u5927\uff0c\u5247\u6703\u9020\u6210\u8a18\u61b6\u9ad4\u7a7a\u9593\u6d6a\u8cbb\u3002</p> <p>\u70ba\u89e3\u6c7a\u6b64\u554f\u984c\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u52d5\u614b\u9663\u5217\uff08dynamic array\uff09\u4f86\u5be6\u73fe\u4e32\u5217\u3002\u5b83\u7e7c\u627f\u4e86\u9663\u5217\u7684\u5404\u9805\u512a\u9ede\uff0c\u4e26\u4e14\u53ef\u4ee5\u5728\u7a0b\u5f0f\u57f7\u884c\u904e\u7a0b\u4e2d\u9032\u884c\u52d5\u614b\u64f4\u5bb9\u3002</p> <p>\u5be6\u969b\u4e0a\uff0c\u8a31\u591a\u7a0b\u5f0f\u8a9e\u8a00\u4e2d\u7684\u6a19\u6e96\u5eab\u63d0\u4f9b\u7684\u4e32\u5217\u662f\u57fa\u65bc\u52d5\u614b\u9663\u5217\u5be6\u73fe\u7684\uff0c\u4f8b\u5982 Python \u4e2d\u7684 <code>list</code> \u3001Java \u4e2d\u7684 <code>ArrayList</code> \u3001C++ \u4e2d\u7684 <code>vector</code> \u548c C# \u4e2d\u7684 <code>List</code> \u7b49\u3002\u5728\u63a5\u4e0b\u4f86\u7684\u8a0e\u8ad6\u4e2d\uff0c\u6211\u5011\u5c07\u628a\u201c\u4e32\u5217\u201d\u548c\u201c\u52d5\u614b\u9663\u5217\u201d\u8996\u70ba\u7b49\u540c\u7684\u6982\u5ff5\u3002</p>"},{"location":"chapter_array_and_linkedlist/list/#431","title":"4.3.1 \u00a0 \u4e32\u5217\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/list/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u4e32\u5217","text":"<p>\u6211\u5011\u901a\u5e38\u4f7f\u7528\u201c\u7121\u521d\u59cb\u503c\u201d\u548c\u201c\u6709\u521d\u59cb\u503c\u201d\u9019\u5169\u7a2e\u521d\u59cb\u5316\u65b9\u6cd5\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig list.py<pre><code># \u521d\u59cb\u5316\u4e32\u5217\n# \u7121\u521d\u59cb\u503c\nnums1: list[int] = []\n# \u6709\u521d\u59cb\u503c\nnums: list[int] = [1, 3, 2, 5, 4]\n</code></pre> list.cpp<pre><code>/* \u521d\u59cb\u5316\u4e32\u5217 */\n// \u9700\u6ce8\u610f\uff0cC++ \u4e2d vector \u5373\u662f\u672c\u6587\u63cf\u8ff0\u7684 nums\n// \u7121\u521d\u59cb\u503c\nvector&lt;int&gt; nums1;\n// \u6709\u521d\u59cb\u503c\nvector&lt;int&gt; nums = { 1, 3, 2, 5, 4 };\n</code></pre> list.java<pre><code>/* \u521d\u59cb\u5316\u4e32\u5217 */\n// \u7121\u521d\u59cb\u503c\nList&lt;Integer&gt; nums1 = new ArrayList&lt;&gt;();\n// \u6709\u521d\u59cb\u503c\uff08\u6ce8\u610f\u9663\u5217\u7684\u5143\u7d20\u578b\u5225\u9700\u70ba int[] \u7684\u5305\u88dd\u985e\u5225 Integer[]\uff09\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList&lt;Integer&gt; nums = new ArrayList&lt;&gt;(Arrays.asList(numbers));\n</code></pre> list.cs<pre><code>/* \u521d\u59cb\u5316\u4e32\u5217 */\n// \u7121\u521d\u59cb\u503c\nList&lt;int&gt; nums1 = [];\n// \u6709\u521d\u59cb\u503c\nint[] numbers = [1, 3, 2, 5, 4];\nList&lt;int&gt; nums = [.. numbers];\n</code></pre> list_test.go<pre><code>/* \u521d\u59cb\u5316\u4e32\u5217 */\n// \u7121\u521d\u59cb\u503c\nnums1 := []int{}\n// \u6709\u521d\u59cb\u503c\nnums := []int{1, 3, 2, 5, 4}\n</code></pre> list.swift<pre><code>/* \u521d\u59cb\u5316\u4e32\u5217 */\n// \u7121\u521d\u59cb\u503c\nlet nums1: [Int] = []\n// \u6709\u521d\u59cb\u503c\nvar nums = [1, 3, 2, 5, 4]\n</code></pre> list.js<pre><code>/* \u521d\u59cb\u5316\u4e32\u5217 */\n// \u7121\u521d\u59cb\u503c\nconst nums1 = [];\n// \u6709\u521d\u59cb\u503c\nconst nums = [1, 3, 2, 5, 4];\n</code></pre> list.ts<pre><code>/* \u521d\u59cb\u5316\u4e32\u5217 */\n// \u7121\u521d\u59cb\u503c\nconst nums1: number[] = [];\n// \u6709\u521d\u59cb\u503c\nconst nums: number[] = [1, 3, 2, 5, 4];\n</code></pre> list.dart<pre><code>/* \u521d\u59cb\u5316\u4e32\u5217 */\n// \u7121\u521d\u59cb\u503c\nList&lt;int&gt; nums1 = [];\n// \u6709\u521d\u59cb\u503c\nList&lt;int&gt; nums = [1, 3, 2, 5, 4];\n</code></pre> list.rs<pre><code>/* \u521d\u59cb\u5316\u4e32\u5217 */\n// \u7121\u521d\u59cb\u503c\nlet nums1: Vec&lt;i32&gt; = Vec::new();\n// \u6709\u521d\u59cb\u503c\nlet nums: Vec&lt;i32&gt; = vec![1, 3, 2, 5, 4];\n</code></pre> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5167\u5efa\u52d5\u614b\u9663\u5217\n</code></pre> list.kt<pre><code>/* \u521d\u59cb\u5316\u4e32\u5217 */\n// \u7121\u521d\u59cb\u503c\nvar nums1 = listOf&lt;Int&gt;()\n// \u6709\u521d\u59cb\u503c\nvar numbers = arrayOf(1, 3, 2, 5, 4)\nvar nums = numbers.toMutableList()\n</code></pre> list.rb<pre><code># \u521d\u59cb\u5316\u4e32\u5217\n# \u7121\u521d\u59cb\u503c\nnums1 = []\n# \u6709\u521d\u59cb\u503c\nnums = [1, 3, 2, 5, 4]\n</code></pre> list.zig<pre><code>// \u521d\u59cb\u5316\u4e32\u5217\nvar nums = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nums.deinit();\ntry nums.appendSlice(&amp;[_]i32{ 1, 3, 2, 5, 4 });\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/list/#2","title":"2. \u00a0 \u8a2a\u554f\u5143\u7d20","text":"<p>\u4e32\u5217\u672c\u8cea\u4e0a\u662f\u9663\u5217\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728 \\(O(1)\\) \u6642\u9593\u5167\u8a2a\u554f\u548c\u66f4\u65b0\u5143\u7d20\uff0c\u6548\u7387\u5f88\u9ad8\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig list.py<pre><code># \u8a2a\u554f\u5143\u7d20\nnum: int = nums[1] # \u8a2a\u554f\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\n\n# \u66f4\u65b0\u5143\u7d20\nnums[1] = 0 # \u5c07\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\u66f4\u65b0\u70ba 0\n</code></pre> list.cpp<pre><code>/* \u8a2a\u554f\u5143\u7d20 */\nint num = nums[1]; // \u8a2a\u554f\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c07\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\u66f4\u65b0\u70ba 0\n</code></pre> list.java<pre><code>/* \u8a2a\u554f\u5143\u7d20 */\nint num = nums.get(1); // \u8a2a\u554f\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums.set(1, 0); // \u5c07\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\u66f4\u65b0\u70ba 0\n</code></pre> list.cs<pre><code>/* \u8a2a\u554f\u5143\u7d20 */\nint num = nums[1]; // \u8a2a\u554f\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c07\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\u66f4\u65b0\u70ba 0\n</code></pre> list_test.go<pre><code>/* \u8a2a\u554f\u5143\u7d20 */\nnum := nums[1] // \u8a2a\u554f\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0 // \u5c07\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\u66f4\u65b0\u70ba 0\n</code></pre> list.swift<pre><code>/* \u8a2a\u554f\u5143\u7d20 */\nlet num = nums[1] // \u8a2a\u554f\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0 // \u5c07\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\u66f4\u65b0\u70ba 0\n</code></pre> list.js<pre><code>/* \u8a2a\u554f\u5143\u7d20 */\nconst num = nums[1]; // \u8a2a\u554f\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c07\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\u66f4\u65b0\u70ba 0\n</code></pre> list.ts<pre><code>/* \u8a2a\u554f\u5143\u7d20 */\nconst num: number = nums[1]; // \u8a2a\u554f\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c07\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\u66f4\u65b0\u70ba 0\n</code></pre> list.dart<pre><code>/* \u8a2a\u554f\u5143\u7d20 */\nint num = nums[1]; // \u8a2a\u554f\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\n\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c07\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\u66f4\u65b0\u70ba 0\n</code></pre> list.rs<pre><code>/* \u8a2a\u554f\u5143\u7d20 */\nlet num: i32 = nums[1]; // \u8a2a\u554f\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0; // \u5c07\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\u66f4\u65b0\u70ba 0\n</code></pre> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5167\u5efa\u52d5\u614b\u9663\u5217\n</code></pre> list.kt<pre><code>/* \u8a2a\u554f\u5143\u7d20 */\nval num = nums[1] // \u8a2a\u554f\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nnums[1] = 0 // \u5c07\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\u66f4\u65b0\u70ba 0\n</code></pre> list.rb<pre><code># \u8a2a\u554f\u5143\u7d20\nnum = nums[1] # \u8a2a\u554f\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\n# \u66f4\u65b0\u5143\u7d20\nnums[1] = 0 # \u5c07\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\u66f4\u65b0\u70ba 0\n</code></pre> list.zig<pre><code>// \u8a2a\u554f\u5143\u7d20\nvar num = nums.items[1]; // \u8a2a\u554f\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\n\n// \u66f4\u65b0\u5143\u7d20\nnums.items[1] = 0; // \u5c07\u7d22\u5f15 1 \u8655\u7684\u5143\u7d20\u66f4\u65b0\u70ba 0\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/list/#3","title":"3. \u00a0 \u63d2\u5165\u8207\u522a\u9664\u5143\u7d20","text":"<p>\u76f8\u8f03\u65bc\u9663\u5217\uff0c\u4e32\u5217\u53ef\u4ee5\u81ea\u7531\u5730\u65b0\u589e\u8207\u522a\u9664\u5143\u7d20\u3002\u5728\u4e32\u5217\u5c3e\u90e8\u65b0\u589e\u5143\u7d20\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(1)\\) \uff0c\u4f46\u63d2\u5165\u548c\u522a\u9664\u5143\u7d20\u7684\u6548\u7387\u4ecd\u8207\u9663\u5217\u76f8\u540c\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig list.py<pre><code># \u6e05\u7a7a\u4e32\u5217\nnums.clear()\n\n# \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n# \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20\nnums.insert(3, 6) # \u5728\u7d22\u5f15 3 \u8655\u63d2\u5165\u6578\u5b57 6\n\n# \u522a\u9664\u5143\u7d20\nnums.pop(3) # \u522a\u9664\u7d22\u5f15 3 \u8655\u7684\u5143\u7d20\n</code></pre> list.cpp<pre><code>/* \u6e05\u7a7a\u4e32\u5217 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\nnums.push_back(1);\nnums.push_back(3);\nnums.push_back(2);\nnums.push_back(5);\nnums.push_back(4);\n\n/* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\nnums.insert(nums.begin() + 3, 6); // \u5728\u7d22\u5f15 3 \u8655\u63d2\u5165\u6578\u5b57 6\n\n/* \u522a\u9664\u5143\u7d20 */\nnums.erase(nums.begin() + 3); // \u522a\u9664\u7d22\u5f15 3 \u8655\u7684\u5143\u7d20\n</code></pre> list.java<pre><code>/* \u6e05\u7a7a\u4e32\u5217 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\nnums.add(3, 6); // \u5728\u7d22\u5f15 3 \u8655\u63d2\u5165\u6578\u5b57 6\n\n/* \u522a\u9664\u5143\u7d20 */\nnums.remove(3); // \u522a\u9664\u7d22\u5f15 3 \u8655\u7684\u5143\u7d20\n</code></pre> list.cs<pre><code>/* \u6e05\u7a7a\u4e32\u5217 */\nnums.Clear();\n\n/* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\nnums.Add(1);\nnums.Add(3);\nnums.Add(2);\nnums.Add(5);\nnums.Add(4);\n\n/* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\nnums.Insert(3, 6); // \u5728\u7d22\u5f15 3 \u8655\u63d2\u5165\u6578\u5b57 6\n\n/* \u522a\u9664\u5143\u7d20 */\nnums.RemoveAt(3); // \u522a\u9664\u7d22\u5f15 3 \u8655\u7684\u5143\u7d20\n</code></pre> list_test.go<pre><code>/* \u6e05\u7a7a\u4e32\u5217 */\nnums = nil\n\n/* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\nnums = append(nums, 1)\nnums = append(nums, 3)\nnums = append(nums, 2)\nnums = append(nums, 5)\nnums = append(nums, 4)\n\n/* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\nnums = append(nums[:3], append([]int{6}, nums[3:]...)...) // \u5728\u7d22\u5f15 3 \u8655\u63d2\u5165\u6578\u5b57 6\n\n/* \u522a\u9664\u5143\u7d20 */\nnums = append(nums[:3], nums[4:]...) // \u522a\u9664\u7d22\u5f15 3 \u8655\u7684\u5143\u7d20\n</code></pre> list.swift<pre><code>/* \u6e05\u7a7a\u4e32\u5217 */\nnums.removeAll()\n\n/* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n/* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\nnums.insert(6, at: 3) // \u5728\u7d22\u5f15 3 \u8655\u63d2\u5165\u6578\u5b57 6\n\n/* \u522a\u9664\u5143\u7d20 */\nnums.remove(at: 3) // \u522a\u9664\u7d22\u5f15 3 \u8655\u7684\u5143\u7d20\n</code></pre> list.js<pre><code>/* \u6e05\u7a7a\u4e32\u5217 */\nnums.length = 0;\n\n/* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\nnums.splice(3, 0, 6); // \u5728\u7d22\u5f15 3 \u8655\u63d2\u5165\u6578\u5b57 6\n\n/* \u522a\u9664\u5143\u7d20 */\nnums.splice(3, 1); // \u522a\u9664\u7d22\u5f15 3 \u8655\u7684\u5143\u7d20\n</code></pre> list.ts<pre><code>/* \u6e05\u7a7a\u4e32\u5217 */\nnums.length = 0;\n\n/* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\nnums.splice(3, 0, 6); // \u5728\u7d22\u5f15 3 \u8655\u63d2\u5165\u6578\u5b57 6\n\n/* \u522a\u9664\u5143\u7d20 */\nnums.splice(3, 1); // \u522a\u9664\u7d22\u5f15 3 \u8655\u7684\u5143\u7d20\n</code></pre> list.dart<pre><code>/* \u6e05\u7a7a\u4e32\u5217 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\nnums.insert(3, 6); // \u5728\u7d22\u5f15 3 \u8655\u63d2\u5165\u6578\u5b57 6\n\n/* \u522a\u9664\u5143\u7d20 */\nnums.removeAt(3); // \u522a\u9664\u7d22\u5f15 3 \u8655\u7684\u5143\u7d20\n</code></pre> list.rs<pre><code>/* \u6e05\u7a7a\u4e32\u5217 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\nnums.insert(3, 6); // \u5728\u7d22\u5f15 3 \u8655\u63d2\u5165\u6578\u5b57 6\n\n/* \u522a\u9664\u5143\u7d20 */\nnums.remove(3); // \u522a\u9664\u7d22\u5f15 3 \u8655\u7684\u5143\u7d20\n</code></pre> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5167\u5efa\u52d5\u614b\u9663\u5217\n</code></pre> list.kt<pre><code>/* \u6e05\u7a7a\u4e32\u5217 */\nnums.clear();\n\n/* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\nnums.add(3, 6); // \u5728\u7d22\u5f15 3 \u8655\u63d2\u5165\u6578\u5b57 6\n\n/* \u522a\u9664\u5143\u7d20 */\nnums.remove(3); // \u522a\u9664\u7d22\u5f15 3 \u8655\u7684\u5143\u7d20\n</code></pre> list.rb<pre><code># \u6e05\u7a7a\u4e32\u5217\nnums.clear\n\n# \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20\nnums &lt;&lt; 1\nnums &lt;&lt; 3\nnums &lt;&lt; 2\nnums &lt;&lt; 5\nnums &lt;&lt; 4\n\n# \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20\nnums.insert(3, 6) # \u5728\u7d22\u5f15 3 \u8655\u63d2\u5165\u6578\u5b57 6\n\n# \u522a\u9664\u5143\u7d20\nnums.delete_at(3) # \u522a\u9664\u7d22\u5f15 3 \u8655\u7684\u5143\u7d20\n</code></pre> list.zig<pre><code>// \u6e05\u7a7a\u4e32\u5217\nnums.clearRetainingCapacity();\n\n// \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20\ntry nums.append(1);\ntry nums.append(3);\ntry nums.append(2);\ntry nums.append(5);\ntry nums.append(4);\n\n// \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20\ntry nums.insert(3, 6); // \u5728\u7d22\u5f15 3 \u8655\u63d2\u5165\u6578\u5b57 6\n\n// \u522a\u9664\u5143\u7d20\n_ = nums.orderedRemove(3); // \u522a\u9664\u7d22\u5f15 3 \u8655\u7684\u5143\u7d20\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/list/#4","title":"4. \u00a0 \u8d70\u8a2a\u4e32\u5217","text":"<p>\u8207\u9663\u5217\u4e00\u6a23\uff0c\u4e32\u5217\u53ef\u4ee5\u6839\u64da\u7d22\u5f15\u8d70\u8a2a\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u8d70\u8a2a\u5404\u5143\u7d20\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig list.py<pre><code># \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u4e32\u5217\ncount = 0\nfor i in range(len(nums)):\n count += nums[i]\n\n# \u76f4\u63a5\u8d70\u8a2a\u4e32\u5217\u5143\u7d20\nfor num in nums:\n count += num\n</code></pre> list.cpp<pre><code>/* \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u4e32\u5217 */\nint count = 0;\nfor (int i = 0; i &lt; nums.size(); i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u8d70\u8a2a\u4e32\u5217\u5143\u7d20 */\ncount = 0;\nfor (int num : nums) {\n count += num;\n}\n</code></pre> list.java<pre><code>/* \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u4e32\u5217 */\nint count = 0;\nfor (int i = 0; i &lt; nums.size(); i++) {\n count += nums.get(i);\n}\n\n/* \u76f4\u63a5\u8d70\u8a2a\u4e32\u5217\u5143\u7d20 */\nfor (int num : nums) {\n count += num;\n}\n</code></pre> list.cs<pre><code>/* \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u4e32\u5217 */\nint count = 0;\nfor (int i = 0; i &lt; nums.Count; i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u8d70\u8a2a\u4e32\u5217\u5143\u7d20 */\ncount = 0;\nforeach (int num in nums) {\n count += num;\n}\n</code></pre> list_test.go<pre><code>/* \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u4e32\u5217 */\ncount := 0\nfor i := 0; i &lt; len(nums); i++ {\n count += nums[i]\n}\n\n/* \u76f4\u63a5\u8d70\u8a2a\u4e32\u5217\u5143\u7d20 */\ncount = 0\nfor _, num := range nums {\n count += num\n}\n</code></pre> list.swift<pre><code>/* \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u4e32\u5217 */\nvar count = 0\nfor i in nums.indices {\n count += nums[i]\n}\n\n/* \u76f4\u63a5\u8d70\u8a2a\u4e32\u5217\u5143\u7d20 */\ncount = 0\nfor num in nums {\n count += num\n}\n</code></pre> list.js<pre><code>/* \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u4e32\u5217 */\nlet count = 0;\nfor (let i = 0; i &lt; nums.length; i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u8d70\u8a2a\u4e32\u5217\u5143\u7d20 */\ncount = 0;\nfor (const num of nums) {\n count += num;\n}\n</code></pre> list.ts<pre><code>/* \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u4e32\u5217 */\nlet count = 0;\nfor (let i = 0; i &lt; nums.length; i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u8d70\u8a2a\u4e32\u5217\u5143\u7d20 */\ncount = 0;\nfor (const num of nums) {\n count += num;\n}\n</code></pre> list.dart<pre><code>/* \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u4e32\u5217 */\nint count = 0;\nfor (var i = 0; i &lt; nums.length; i++) {\n count += nums[i];\n}\n\n/* \u76f4\u63a5\u8d70\u8a2a\u4e32\u5217\u5143\u7d20 */\ncount = 0;\nfor (var num in nums) {\n count += num;\n}\n</code></pre> list.rs<pre><code>// \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u4e32\u5217\nlet mut _count = 0;\nfor i in 0..nums.len() {\n _count += nums[i];\n}\n\n// \u76f4\u63a5\u8d70\u8a2a\u4e32\u5217\u5143\u7d20\n_count = 0;\nfor num in &amp;nums {\n _count += num;\n}\n</code></pre> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5167\u5efa\u52d5\u614b\u9663\u5217\n</code></pre> list.kt<pre><code>/* \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u4e32\u5217 */\nvar count = 0\nfor (i in nums.indices) {\n count += nums[i]\n}\n\n/* \u76f4\u63a5\u8d70\u8a2a\u4e32\u5217\u5143\u7d20 */\nfor (num in nums) {\n count += num\n}\n</code></pre> list.rb<pre><code># \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u4e32\u5217\ncount = 0\nfor i in 0...nums.length\n count += nums[i]\nend\n\n# \u76f4\u63a5\u8d70\u8a2a\u4e32\u5217\u5143\u7d20\ncount = 0\nfor num in nums\n count += num\nend\n</code></pre> list.zig<pre><code>// \u900f\u904e\u7d22\u5f15\u8d70\u8a2a\u4e32\u5217\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i &lt; nums.items.len) : (i += 1) {\n count += nums[i];\n}\n\n// \u76f4\u63a5\u8d70\u8a2a\u4e32\u5217\u5143\u7d20\ncount = 0;\nfor (nums.items) |num| {\n count += num;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/list/#5","title":"5. \u00a0 \u62fc\u63a5\u4e32\u5217","text":"<p>\u7d66\u5b9a\u4e00\u500b\u65b0\u4e32\u5217 <code>nums1</code> \uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u5176\u62fc\u63a5\u5230\u539f\u4e32\u5217\u7684\u5c3e\u90e8\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig list.py<pre><code># \u62fc\u63a5\u5169\u500b\u4e32\u5217\nnums1: list[int] = [6, 8, 7, 10, 9]\nnums += nums1 # \u5c07\u4e32\u5217 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u5f8c\n</code></pre> list.cpp<pre><code>/* \u62fc\u63a5\u5169\u500b\u4e32\u5217 */\nvector&lt;int&gt; nums1 = { 6, 8, 7, 10, 9 };\n// \u5c07\u4e32\u5217 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u5f8c\nnums.insert(nums.end(), nums1.begin(), nums1.end());\n</code></pre> list.java<pre><code>/* \u62fc\u63a5\u5169\u500b\u4e32\u5217 */\nList&lt;Integer&gt; nums1 = new ArrayList&lt;&gt;(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nnums.addAll(nums1); // \u5c07\u4e32\u5217 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u5f8c\n</code></pre> list.cs<pre><code>/* \u62fc\u63a5\u5169\u500b\u4e32\u5217 */\nList&lt;int&gt; nums1 = [6, 8, 7, 10, 9];\nnums.AddRange(nums1); // \u5c07\u4e32\u5217 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u5f8c\n</code></pre> list_test.go<pre><code>/* \u62fc\u63a5\u5169\u500b\u4e32\u5217 */\nnums1 := []int{6, 8, 7, 10, 9}\nnums = append(nums, nums1...) // \u5c07\u4e32\u5217 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u5f8c\n</code></pre> list.swift<pre><code>/* \u62fc\u63a5\u5169\u500b\u4e32\u5217 */\nlet nums1 = [6, 8, 7, 10, 9]\nnums.append(contentsOf: nums1) // \u5c07\u4e32\u5217 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u5f8c\n</code></pre> list.js<pre><code>/* \u62fc\u63a5\u5169\u500b\u4e32\u5217 */\nconst nums1 = [6, 8, 7, 10, 9];\nnums.push(...nums1); // \u5c07\u4e32\u5217 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u5f8c\n</code></pre> list.ts<pre><code>/* \u62fc\u63a5\u5169\u500b\u4e32\u5217 */\nconst nums1: number[] = [6, 8, 7, 10, 9];\nnums.push(...nums1); // \u5c07\u4e32\u5217 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u5f8c\n</code></pre> list.dart<pre><code>/* \u62fc\u63a5\u5169\u500b\u4e32\u5217 */\nList&lt;int&gt; nums1 = [6, 8, 7, 10, 9];\nnums.addAll(nums1); // \u5c07\u4e32\u5217 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u5f8c\n</code></pre> list.rs<pre><code>/* \u62fc\u63a5\u5169\u500b\u4e32\u5217 */\nlet nums1: Vec&lt;i32&gt; = vec![6, 8, 7, 10, 9];\nnums.extend(nums1);\n</code></pre> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5167\u5efa\u52d5\u614b\u9663\u5217\n</code></pre> list.kt<pre><code>/* \u62fc\u63a5\u5169\u500b\u4e32\u5217 */\nval nums1 = intArrayOf(6, 8, 7, 10, 9).toMutableList()\nnums.addAll(nums1) // \u5c07\u4e32\u5217 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u5f8c\n</code></pre> list.rb<pre><code># \u62fc\u63a5\u5169\u500b\u4e32\u5217\nnums1 = [6, 8, 7, 10, 9]\nnums += nums1\n</code></pre> list.zig<pre><code>// \u62fc\u63a5\u5169\u500b\u4e32\u5217\nvar nums1 = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nums1.deinit();\ntry nums1.appendSlice(&amp;[_]i32{ 6, 8, 7, 10, 9 });\ntry nums.insertSlice(nums.items.len, nums1.items); // \u5c07\u4e32\u5217 nums1 \u62fc\u63a5\u5230 nums \u4e4b\u5f8c\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/list/#6","title":"6. \u00a0 \u6392\u5e8f\u4e32\u5217","text":"<p>\u5b8c\u6210\u4e32\u5217\u6392\u5e8f\u5f8c\uff0c\u6211\u5011\u4fbf\u53ef\u4ee5\u4f7f\u7528\u5728\u9663\u5217\u985e\u5225\u6f14\u7b97\u6cd5\u984c\u4e2d\u7d93\u5e38\u8003\u67e5\u7684\u201c\u4e8c\u5206\u641c\u5c0b\u201d\u548c\u201c\u96d9\u6307\u6a19\u201d\u6f14\u7b97\u6cd5\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig list.py<pre><code># \u6392\u5e8f\u4e32\u5217\nnums.sort() # \u6392\u5e8f\u5f8c\uff0c\u4e32\u5217\u5143\u7d20\u5f9e\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.cpp<pre><code>/* \u6392\u5e8f\u4e32\u5217 */\nsort(nums.begin(), nums.end()); // \u6392\u5e8f\u5f8c\uff0c\u4e32\u5217\u5143\u7d20\u5f9e\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.java<pre><code>/* \u6392\u5e8f\u4e32\u5217 */\nCollections.sort(nums); // \u6392\u5e8f\u5f8c\uff0c\u4e32\u5217\u5143\u7d20\u5f9e\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.cs<pre><code>/* \u6392\u5e8f\u4e32\u5217 */\nnums.Sort(); // \u6392\u5e8f\u5f8c\uff0c\u4e32\u5217\u5143\u7d20\u5f9e\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list_test.go<pre><code>/* \u6392\u5e8f\u4e32\u5217 */\nsort.Ints(nums) // \u6392\u5e8f\u5f8c\uff0c\u4e32\u5217\u5143\u7d20\u5f9e\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.swift<pre><code>/* \u6392\u5e8f\u4e32\u5217 */\nnums.sort() // \u6392\u5e8f\u5f8c\uff0c\u4e32\u5217\u5143\u7d20\u5f9e\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.js<pre><code>/* \u6392\u5e8f\u4e32\u5217 */\nnums.sort((a, b) =&gt; a - b); // \u6392\u5e8f\u5f8c\uff0c\u4e32\u5217\u5143\u7d20\u5f9e\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.ts<pre><code>/* \u6392\u5e8f\u4e32\u5217 */\nnums.sort((a, b) =&gt; a - b); // \u6392\u5e8f\u5f8c\uff0c\u4e32\u5217\u5143\u7d20\u5f9e\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.dart<pre><code>/* \u6392\u5e8f\u4e32\u5217 */\nnums.sort(); // \u6392\u5e8f\u5f8c\uff0c\u4e32\u5217\u5143\u7d20\u5f9e\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.rs<pre><code>/* \u6392\u5e8f\u4e32\u5217 */\nnums.sort(); // \u6392\u5e8f\u5f8c\uff0c\u4e32\u5217\u5143\u7d20\u5f9e\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5167\u5efa\u52d5\u614b\u9663\u5217\n</code></pre> list.kt<pre><code>/* \u6392\u5e8f\u4e32\u5217 */\nnums.sort() // \u6392\u5e8f\u5f8c\uff0c\u4e32\u5217\u5143\u7d20\u5f9e\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.rb<pre><code># \u6392\u5e8f\u4e32\u5217\nnums = nums.sort { |a, b| a &lt;=&gt; b } # \u6392\u5e8f\u5f8c\uff0c\u4e32\u5217\u5143\u7d20\u5f9e\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.zig<pre><code>// \u6392\u5e8f\u4e32\u5217\nstd.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/list/#432","title":"4.3.2 \u00a0 \u4e32\u5217\u5be6\u73fe","text":"<p>\u8a31\u591a\u7a0b\u5f0f\u8a9e\u8a00\u5167\u5efa\u4e86\u4e32\u5217\uff0c\u4f8b\u5982 Java\u3001C++\u3001Python \u7b49\u3002\u5b83\u5011\u7684\u5be6\u73fe\u6bd4\u8f03\u8907\u96dc\uff0c\u5404\u500b\u53c3\u6578\u7684\u8a2d\u5b9a\u4e5f\u975e\u5e38\u8003\u7a76\uff0c\u4f8b\u5982\u521d\u59cb\u5bb9\u91cf\u3001\u64f4\u5bb9\u500d\u6578\u7b49\u3002\u611f\u8208\u8da3\u7684\u8b80\u8005\u53ef\u4ee5\u67e5\u95b1\u539f\u59cb\u78bc\u9032\u884c\u5b78\u7fd2\u3002</p> <p>\u70ba\u4e86\u52a0\u6df1\u5c0d\u4e32\u5217\u5de5\u4f5c\u539f\u7406\u7684\u7406\u89e3\uff0c\u6211\u5011\u5617\u8a66\u5be6\u73fe\u4e00\u500b\u7c21\u6613\u7248\u4e32\u5217\uff0c\u5305\u62ec\u4ee5\u4e0b\u4e09\u500b\u91cd\u9ede\u8a2d\u8a08\u3002</p> <ul> <li>\u521d\u59cb\u5bb9\u91cf\uff1a\u9078\u53d6\u4e00\u500b\u5408\u7406\u7684\u9663\u5217\u521d\u59cb\u5bb9\u91cf\u3002\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9078\u64c7 10 \u4f5c\u70ba\u521d\u59cb\u5bb9\u91cf\u3002</li> <li>\u6578\u91cf\u8a18\u9304\uff1a\u5ba3\u544a\u4e00\u500b\u8b8a\u6578 <code>size</code> \uff0c\u7528\u65bc\u8a18\u9304\u4e32\u5217\u7576\u524d\u5143\u7d20\u6578\u91cf\uff0c\u4e26\u96a8\u8457\u5143\u7d20\u63d2\u5165\u548c\u522a\u9664\u5be6\u6642\u66f4\u65b0\u3002\u6839\u64da\u6b64\u8b8a\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u5b9a\u4f4d\u4e32\u5217\u5c3e\u90e8\uff0c\u4ee5\u53ca\u5224\u65b7\u662f\u5426\u9700\u8981\u64f4\u5bb9\u3002</li> <li>\u64f4\u5bb9\u6a5f\u5236\uff1a\u82e5\u63d2\u5165\u5143\u7d20\u6642\u4e32\u5217\u5bb9\u91cf\u5df2\u6eff\uff0c\u5247\u9700\u8981\u9032\u884c\u64f4\u5bb9\u3002\u5148\u6839\u64da\u64f4\u5bb9\u500d\u6578\u5efa\u7acb\u4e00\u500b\u66f4\u5927\u7684\u9663\u5217\uff0c\u518d\u5c07\u7576\u524d\u9663\u5217\u7684\u6240\u6709\u5143\u7d20\u4f9d\u6b21\u79fb\u52d5\u81f3\u65b0\u9663\u5217\u3002\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u898f\u5b9a\u6bcf\u6b21\u5c07\u9663\u5217\u64f4\u5bb9\u81f3\u4e4b\u524d\u7684 2 \u500d\u3002</li> </ul> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_list.py<pre><code>class MyList:\n \"\"\"\u4e32\u5217\u985e\u5225\"\"\"\n\n def __init__(self):\n \"\"\"\u5efa\u69cb\u5b50\"\"\"\n self._capacity: int = 10 # \u4e32\u5217\u5bb9\u91cf\n self._arr: list[int] = [0] * self._capacity # \u9663\u5217\uff08\u5132\u5b58\u4e32\u5217\u5143\u7d20\uff09\n self._size: int = 0 # \u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\n self._extend_ratio: int = 2 # \u6bcf\u6b21\u4e32\u5217\u64f4\u5bb9\u7684\u500d\u6578\n\n def size(self) -&gt; int:\n \"\"\"\u7372\u53d6\u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\"\"\"\n return self._size\n\n def capacity(self) -&gt; int:\n \"\"\"\u7372\u53d6\u4e32\u5217\u5bb9\u91cf\"\"\"\n return self._capacity\n\n def get(self, index: int) -&gt; int:\n \"\"\"\u8a2a\u554f\u5143\u7d20\"\"\"\n # \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5247\u4e1f\u64f2\u7570\u5e38\uff0c\u4e0b\u540c\n if index &lt; 0 or index &gt;= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n return self._arr[index]\n\n def set(self, num: int, index: int):\n \"\"\"\u66f4\u65b0\u5143\u7d20\"\"\"\n if index &lt; 0 or index &gt;= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n self._arr[index] = num\n\n def add(self, num: int):\n \"\"\"\u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20\"\"\"\n # \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if self.size() == self.capacity():\n self.extend_capacity()\n self._arr[self._size] = num\n self._size += 1\n\n def insert(self, num: int, index: int):\n \"\"\"\u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20\"\"\"\n if index &lt; 0 or index &gt;= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n # \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if self._size == self.capacity():\n self.extend_capacity()\n # \u5c07\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for j in range(self._size - 1, index - 1, -1):\n self._arr[j + 1] = self._arr[j]\n self._arr[index] = num\n # \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n self._size += 1\n\n def remove(self, index: int) -&gt; int:\n \"\"\"\u522a\u9664\u5143\u7d20\"\"\"\n if index &lt; 0 or index &gt;= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n num = self._arr[index]\n # \u5c07\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for j in range(index, self._size - 1):\n self._arr[j] = self._arr[j + 1]\n # \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n self._size -= 1\n # \u8fd4\u56de\u88ab\u522a\u9664\u7684\u5143\u7d20\n return num\n\n def extend_capacity(self):\n \"\"\"\u4e32\u5217\u64f4\u5bb9\"\"\"\n # \u65b0\u5efa\u4e00\u500b\u9577\u5ea6\u70ba\u539f\u9663\u5217 _extend_ratio \u500d\u7684\u65b0\u9663\u5217\uff0c\u4e26\u5c07\u539f\u9663\u5217\u8907\u88fd\u5230\u65b0\u9663\u5217\n self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)\n # \u66f4\u65b0\u4e32\u5217\u5bb9\u91cf\n self._capacity = len(self._arr)\n\n def to_array(self) -&gt; list[int]:\n \"\"\"\u8fd4\u56de\u6709\u6548\u9577\u5ea6\u7684\u4e32\u5217\"\"\"\n return self._arr[: self._size]\n</code></pre> my_list.cpp<pre><code>/* \u4e32\u5217\u985e\u5225 */\nclass MyList {\n private:\n int *arr; // \u9663\u5217\uff08\u5132\u5b58\u4e32\u5217\u5143\u7d20\uff09\n int arrCapacity = 10; // \u4e32\u5217\u5bb9\u91cf\n int arrSize = 0; // \u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\n int extendRatio = 2; // \u6bcf\u6b21\u4e32\u5217\u64f4\u5bb9\u7684\u500d\u6578\n\n public:\n /* \u5efa\u69cb\u5b50 */\n MyList() {\n arr = new int[arrCapacity];\n }\n\n /* \u6790\u69cb\u65b9\u6cd5 */\n ~MyList() {\n delete[] arr;\n }\n\n /* \u7372\u53d6\u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09*/\n int size() {\n return arrSize;\n }\n\n /* \u7372\u53d6\u4e32\u5217\u5bb9\u91cf */\n int capacity() {\n return arrCapacity;\n }\n\n /* \u8a2a\u554f\u5143\u7d20 */\n int get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5247\u4e1f\u64f2\u7570\u5e38\uff0c\u4e0b\u540c\n if (index &lt; 0 || index &gt;= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n void set(int index, int num) {\n if (index &lt; 0 || index &gt;= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\n void add(int num) {\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (size() == capacity())\n extendCapacity();\n arr[size()] = num;\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n arrSize++;\n }\n\n /* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\n void insert(int index, int num) {\n if (index &lt; 0 || index &gt;= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (size() == capacity())\n extendCapacity();\n // \u5c07\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (int j = size() - 1; j &gt;= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n arrSize++;\n }\n\n /* \u522a\u9664\u5143\u7d20 */\n int remove(int index) {\n if (index &lt; 0 || index &gt;= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u5c07\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (int j = index; j &lt; size() - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n arrSize--;\n // \u8fd4\u56de\u88ab\u522a\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u4e32\u5217\u64f4\u5bb9 */\n void extendCapacity() {\n // \u65b0\u5efa\u4e00\u500b\u9577\u5ea6\u70ba\u539f\u9663\u5217 extendRatio \u500d\u7684\u65b0\u9663\u5217\n int newCapacity = capacity() * extendRatio;\n int *tmp = arr;\n arr = new int[newCapacity];\n // \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n for (int i = 0; i &lt; size(); i++) {\n arr[i] = tmp[i];\n }\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n delete[] tmp;\n arrCapacity = newCapacity;\n }\n\n /* \u5c07\u4e32\u5217\u8f49\u63db\u70ba Vector \u7528\u65bc\u5217\u5370 */\n vector&lt;int&gt; toVector() {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n vector&lt;int&gt; vec(size());\n for (int i = 0; i &lt; size(); i++) {\n vec[i] = arr[i];\n }\n return vec;\n }\n};\n</code></pre> my_list.java<pre><code>/* \u4e32\u5217\u985e\u5225 */\nclass MyList {\n private int[] arr; // \u9663\u5217\uff08\u5132\u5b58\u4e32\u5217\u5143\u7d20\uff09\n private int capacity = 10; // \u4e32\u5217\u5bb9\u91cf\n private int size = 0; // \u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\n private int extendRatio = 2; // \u6bcf\u6b21\u4e32\u5217\u64f4\u5bb9\u7684\u500d\u6578\n\n /* \u5efa\u69cb\u5b50 */\n public MyList() {\n arr = new int[capacity];\n }\n\n /* \u7372\u53d6\u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09 */\n public int size() {\n return size;\n }\n\n /* \u7372\u53d6\u4e32\u5217\u5bb9\u91cf */\n public int capacity() {\n return capacity;\n }\n\n /* \u8a2a\u554f\u5143\u7d20 */\n public int get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5247\u4e1f\u64f2\u7570\u5e38\uff0c\u4e0b\u540c\n if (index &lt; 0 || index &gt;= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public void set(int index, int num) {\n if (index &lt; 0 || index &gt;= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\n public void add(int num) {\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (size == capacity())\n extendCapacity();\n arr[size] = num;\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n size++;\n }\n\n /* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\n public void insert(int index, int num) {\n if (index &lt; 0 || index &gt;= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (size == capacity())\n extendCapacity();\n // \u5c07\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (int j = size - 1; j &gt;= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n size++;\n }\n\n /* \u522a\u9664\u5143\u7d20 */\n public int remove(int index) {\n if (index &lt; 0 || index &gt;= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u5c07\u5c07\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (int j = index; j &lt; size - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n size--;\n // \u8fd4\u56de\u88ab\u522a\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u4e32\u5217\u64f4\u5bb9 */\n public void extendCapacity() {\n // \u65b0\u5efa\u4e00\u500b\u9577\u5ea6\u70ba\u539f\u9663\u5217 extendRatio \u500d\u7684\u65b0\u9663\u5217\uff0c\u4e26\u5c07\u539f\u9663\u5217\u8907\u88fd\u5230\u65b0\u9663\u5217\n arr = Arrays.copyOf(arr, capacity() * extendRatio);\n // \u66f4\u65b0\u4e32\u5217\u5bb9\u91cf\n capacity = arr.length;\n }\n\n /* \u5c07\u4e32\u5217\u8f49\u63db\u70ba\u9663\u5217 */\n public int[] toArray() {\n int size = size();\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n int[] arr = new int[size];\n for (int i = 0; i &lt; size; i++) {\n arr[i] = get(i);\n }\n return arr;\n }\n}\n</code></pre> my_list.cs<pre><code>/* \u4e32\u5217\u985e\u5225 */\nclass MyList {\n private int[] arr; // \u9663\u5217\uff08\u5132\u5b58\u4e32\u5217\u5143\u7d20\uff09\n private int arrCapacity = 10; // \u4e32\u5217\u5bb9\u91cf\n private int arrSize = 0; // \u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\n private readonly int extendRatio = 2; // \u6bcf\u6b21\u4e32\u5217\u64f4\u5bb9\u7684\u500d\u6578\n\n /* \u5efa\u69cb\u5b50 */\n public MyList() {\n arr = new int[arrCapacity];\n }\n\n /* \u7372\u53d6\u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09*/\n public int Size() {\n return arrSize;\n }\n\n /* \u7372\u53d6\u4e32\u5217\u5bb9\u91cf */\n public int Capacity() {\n return arrCapacity;\n }\n\n /* \u8a2a\u554f\u5143\u7d20 */\n public int Get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5247\u4e1f\u64f2\u7570\u5e38\uff0c\u4e0b\u540c\n if (index &lt; 0 || index &gt;= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public void Set(int index, int num) {\n if (index &lt; 0 || index &gt;= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\n public void Add(int num) {\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (arrSize == arrCapacity)\n ExtendCapacity();\n arr[arrSize] = num;\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n arrSize++;\n }\n\n /* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\n public void Insert(int index, int num) {\n if (index &lt; 0 || index &gt;= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (arrSize == arrCapacity)\n ExtendCapacity();\n // \u5c07\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (int j = arrSize - 1; j &gt;= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n arrSize++;\n }\n\n /* \u522a\u9664\u5143\u7d20 */\n public int Remove(int index) {\n if (index &lt; 0 || index &gt;= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u5c07\u5c07\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (int j = index; j &lt; arrSize - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n arrSize--;\n // \u8fd4\u56de\u88ab\u522a\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u4e32\u5217\u64f4\u5bb9 */\n public void ExtendCapacity() {\n // \u65b0\u5efa\u4e00\u500b\u9577\u5ea6\u70ba arrCapacity * extendRatio \u7684\u9663\u5217\uff0c\u4e26\u5c07\u539f\u9663\u5217\u8907\u88fd\u5230\u65b0\u9663\u5217\n Array.Resize(ref arr, arrCapacity * extendRatio);\n // \u66f4\u65b0\u4e32\u5217\u5bb9\u91cf\n arrCapacity = arr.Length;\n }\n\n /* \u5c07\u4e32\u5217\u8f49\u63db\u70ba\u9663\u5217 */\n public int[] ToArray() {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n int[] arr = new int[arrSize];\n for (int i = 0; i &lt; arrSize; i++) {\n arr[i] = Get(i);\n }\n return arr;\n }\n}\n</code></pre> my_list.go<pre><code>/* \u4e32\u5217\u985e\u5225 */\ntype myList struct {\n arrCapacity int\n arr []int\n arrSize int\n extendRatio int\n}\n\n/* \u5efa\u69cb\u5b50 */\nfunc newMyList() *myList {\n return &amp;myList{\n arrCapacity: 10, // \u4e32\u5217\u5bb9\u91cf\n arr: make([]int, 10), // \u9663\u5217\uff08\u5132\u5b58\u4e32\u5217\u5143\u7d20\uff09\n arrSize: 0, // \u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\n extendRatio: 2, // \u6bcf\u6b21\u4e32\u5217\u64f4\u5bb9\u7684\u500d\u6578\n }\n}\n\n/* \u7372\u53d6\u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09 */\nfunc (l *myList) size() int {\n return l.arrSize\n}\n\n/* \u7372\u53d6\u4e32\u5217\u5bb9\u91cf */\nfunc (l *myList) capacity() int {\n return l.arrCapacity\n}\n\n/* \u8a2a\u554f\u5143\u7d20 */\nfunc (l *myList) get(index int) int {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5247\u4e1f\u64f2\u7570\u5e38\uff0c\u4e0b\u540c\n if index &lt; 0 || index &gt;= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n return l.arr[index]\n}\n\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc (l *myList) set(num, index int) {\n if index &lt; 0 || index &gt;= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n l.arr[index] = num\n}\n\n/* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\nfunc (l *myList) add(num int) {\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if l.arrSize == l.arrCapacity {\n l.extendCapacity()\n }\n l.arr[l.arrSize] = num\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n l.arrSize++\n}\n\n/* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\nfunc (l *myList) insert(num, index int) {\n if index &lt; 0 || index &gt;= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if l.arrSize == l.arrCapacity {\n l.extendCapacity()\n }\n // \u5c07\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for j := l.arrSize - 1; j &gt;= index; j-- {\n l.arr[j+1] = l.arr[j]\n }\n l.arr[index] = num\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n l.arrSize++\n}\n\n/* \u522a\u9664\u5143\u7d20 */\nfunc (l *myList) remove(index int) int {\n if index &lt; 0 || index &gt;= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n num := l.arr[index]\n // \u5c07\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for j := index; j &lt; l.arrSize-1; j++ {\n l.arr[j] = l.arr[j+1]\n }\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n l.arrSize--\n // \u8fd4\u56de\u88ab\u522a\u9664\u7684\u5143\u7d20\n return num\n}\n\n/* \u4e32\u5217\u64f4\u5bb9 */\nfunc (l *myList) extendCapacity() {\n // \u65b0\u5efa\u4e00\u500b\u9577\u5ea6\u70ba\u539f\u9663\u5217 extendRatio \u500d\u7684\u65b0\u9663\u5217\uff0c\u4e26\u5c07\u539f\u9663\u5217\u8907\u88fd\u5230\u65b0\u9663\u5217\n l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)\n // \u66f4\u65b0\u4e32\u5217\u5bb9\u91cf\n l.arrCapacity = len(l.arr)\n}\n\n/* \u8fd4\u56de\u6709\u6548\u9577\u5ea6\u7684\u4e32\u5217 */\nfunc (l *myList) toArray() []int {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n return l.arr[:l.arrSize]\n}\n</code></pre> my_list.swift<pre><code>/* \u4e32\u5217\u985e\u5225 */\nclass MyList {\n private var arr: [Int] // \u9663\u5217\uff08\u5132\u5b58\u4e32\u5217\u5143\u7d20\uff09\n private var _capacity: Int // \u4e32\u5217\u5bb9\u91cf\n private var _size: Int // \u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\n private let extendRatio: Int // \u6bcf\u6b21\u4e32\u5217\u64f4\u5bb9\u7684\u500d\u6578\n\n /* \u5efa\u69cb\u5b50 */\n init() {\n _capacity = 10\n _size = 0\n extendRatio = 2\n arr = Array(repeating: 0, count: _capacity)\n }\n\n /* \u7372\u53d6\u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09*/\n func size() -&gt; Int {\n _size\n }\n\n /* \u7372\u53d6\u4e32\u5217\u5bb9\u91cf */\n func capacity() -&gt; Int {\n _capacity\n }\n\n /* \u8a2a\u554f\u5143\u7d20 */\n func get(index: Int) -&gt; Int {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5247\u4e1f\u64f2\u932f\u8aa4\uff0c\u4e0b\u540c\n if index &lt; 0 || index &gt;= size() {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n return arr[index]\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n func set(index: Int, num: Int) {\n if index &lt; 0 || index &gt;= size() {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n arr[index] = num\n }\n\n /* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\n func add(num: Int) {\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if size() == capacity() {\n extendCapacity()\n }\n arr[size()] = num\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n _size += 1\n }\n\n /* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\n func insert(index: Int, num: Int) {\n if index &lt; 0 || index &gt;= size() {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if size() == capacity() {\n extendCapacity()\n }\n // \u5c07\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for j in (index ..&lt; size()).reversed() {\n arr[j + 1] = arr[j]\n }\n arr[index] = num\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n _size += 1\n }\n\n /* \u522a\u9664\u5143\u7d20 */\n @discardableResult\n func remove(index: Int) -&gt; Int {\n if index &lt; 0 || index &gt;= size() {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n let num = arr[index]\n // \u5c07\u5c07\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for j in index ..&lt; (size() - 1) {\n arr[j] = arr[j + 1]\n }\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n _size -= 1\n // \u8fd4\u56de\u88ab\u522a\u9664\u7684\u5143\u7d20\n return num\n }\n\n /* \u4e32\u5217\u64f4\u5bb9 */\n func extendCapacity() {\n // \u65b0\u5efa\u4e00\u500b\u9577\u5ea6\u70ba\u539f\u9663\u5217 extendRatio \u500d\u7684\u65b0\u9663\u5217\uff0c\u4e26\u5c07\u539f\u9663\u5217\u8907\u88fd\u5230\u65b0\u9663\u5217\n arr = arr + Array(repeating: 0, count: capacity() * (extendRatio - 1))\n // \u66f4\u65b0\u4e32\u5217\u5bb9\u91cf\n _capacity = arr.count\n }\n\n /* \u5c07\u4e32\u5217\u8f49\u63db\u70ba\u9663\u5217 */\n func toArray() -&gt; [Int] {\n Array(arr.prefix(size()))\n }\n}\n</code></pre> my_list.js<pre><code>/* \u4e32\u5217\u985e\u5225 */\nclass MyList {\n #arr = new Array(); // \u9663\u5217\uff08\u5132\u5b58\u4e32\u5217\u5143\u7d20\uff09\n #capacity = 10; // \u4e32\u5217\u5bb9\u91cf\n #size = 0; // \u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\n #extendRatio = 2; // \u6bcf\u6b21\u4e32\u5217\u64f4\u5bb9\u7684\u500d\u6578\n\n /* \u5efa\u69cb\u5b50 */\n constructor() {\n this.#arr = new Array(this.#capacity);\n }\n\n /* \u7372\u53d6\u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09*/\n size() {\n return this.#size;\n }\n\n /* \u7372\u53d6\u4e32\u5217\u5bb9\u91cf */\n capacity() {\n return this.#capacity;\n }\n\n /* \u8a2a\u554f\u5143\u7d20 */\n get(index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5247\u4e1f\u64f2\u7570\u5e38\uff0c\u4e0b\u540c\n if (index &lt; 0 || index &gt;= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n return this.#arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n set(index, num) {\n if (index &lt; 0 || index &gt;= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n this.#arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\n add(num) {\n // \u5982\u679c\u9577\u5ea6\u7b49\u65bc\u5bb9\u91cf\uff0c\u5247\u9700\u8981\u64f4\u5bb9\n if (this.#size === this.#capacity) {\n this.extendCapacity();\n }\n // \u5c07\u65b0\u5143\u7d20\u65b0\u589e\u5230\u4e32\u5217\u5c3e\u90e8\n this.#arr[this.#size] = num;\n this.#size++;\n }\n\n /* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\n insert(index, num) {\n if (index &lt; 0 || index &gt;= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (this.#size === this.#capacity) {\n this.extendCapacity();\n }\n // \u5c07\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (let j = this.#size - 1; j &gt;= index; j--) {\n this.#arr[j + 1] = this.#arr[j];\n }\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n this.#arr[index] = num;\n this.#size++;\n }\n\n /* \u522a\u9664\u5143\u7d20 */\n remove(index) {\n if (index &lt; 0 || index &gt;= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n let num = this.#arr[index];\n // \u5c07\u5c07\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (let j = index; j &lt; this.#size - 1; j++) {\n this.#arr[j] = this.#arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n this.#size--;\n // \u8fd4\u56de\u88ab\u522a\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u4e32\u5217\u64f4\u5bb9 */\n extendCapacity() {\n // \u65b0\u5efa\u4e00\u500b\u9577\u5ea6\u70ba\u539f\u9663\u5217 extendRatio \u500d\u7684\u65b0\u9663\u5217\uff0c\u4e26\u5c07\u539f\u9663\u5217\u8907\u88fd\u5230\u65b0\u9663\u5217\n this.#arr = this.#arr.concat(\n new Array(this.capacity() * (this.#extendRatio - 1))\n );\n // \u66f4\u65b0\u4e32\u5217\u5bb9\u91cf\n this.#capacity = this.#arr.length;\n }\n\n /* \u5c07\u4e32\u5217\u8f49\u63db\u70ba\u9663\u5217 */\n toArray() {\n let size = this.size();\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n const arr = new Array(size);\n for (let i = 0; i &lt; size; i++) {\n arr[i] = this.get(i);\n }\n return arr;\n }\n}\n</code></pre> my_list.ts<pre><code>/* \u4e32\u5217\u985e\u5225 */\nclass MyList {\n private arr: Array&lt;number&gt;; // \u9663\u5217\uff08\u5132\u5b58\u4e32\u5217\u5143\u7d20\uff09\n private _capacity: number = 10; // \u4e32\u5217\u5bb9\u91cf\n private _size: number = 0; // \u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\n private extendRatio: number = 2; // \u6bcf\u6b21\u4e32\u5217\u64f4\u5bb9\u7684\u500d\u6578\n\n /* \u5efa\u69cb\u5b50 */\n constructor() {\n this.arr = new Array(this._capacity);\n }\n\n /* \u7372\u53d6\u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09*/\n public size(): number {\n return this._size;\n }\n\n /* \u7372\u53d6\u4e32\u5217\u5bb9\u91cf */\n public capacity(): number {\n return this._capacity;\n }\n\n /* \u8a2a\u554f\u5143\u7d20 */\n public get(index: number): number {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5247\u4e1f\u64f2\u7570\u5e38\uff0c\u4e0b\u540c\n if (index &lt; 0 || index &gt;= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n return this.arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public set(index: number, num: number): void {\n if (index &lt; 0 || index &gt;= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n this.arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\n public add(num: number): void {\n // \u5982\u679c\u9577\u5ea6\u7b49\u65bc\u5bb9\u91cf\uff0c\u5247\u9700\u8981\u64f4\u5bb9\n if (this._size === this._capacity) this.extendCapacity();\n // \u5c07\u65b0\u5143\u7d20\u65b0\u589e\u5230\u4e32\u5217\u5c3e\u90e8\n this.arr[this._size] = num;\n this._size++;\n }\n\n /* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\n public insert(index: number, num: number): void {\n if (index &lt; 0 || index &gt;= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (this._size === this._capacity) {\n this.extendCapacity();\n }\n // \u5c07\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (let j = this._size - 1; j &gt;= index; j--) {\n this.arr[j + 1] = this.arr[j];\n }\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n this.arr[index] = num;\n this._size++;\n }\n\n /* \u522a\u9664\u5143\u7d20 */\n public remove(index: number): number {\n if (index &lt; 0 || index &gt;= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n let num = this.arr[index];\n // \u5c07\u5c07\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (let j = index; j &lt; this._size - 1; j++) {\n this.arr[j] = this.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n this._size--;\n // \u8fd4\u56de\u88ab\u522a\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u4e32\u5217\u64f4\u5bb9 */\n public extendCapacity(): void {\n // \u65b0\u5efa\u4e00\u500b\u9577\u5ea6\u70ba size \u7684\u9663\u5217\uff0c\u4e26\u5c07\u539f\u9663\u5217\u8907\u88fd\u5230\u65b0\u9663\u5217\n this.arr = this.arr.concat(\n new Array(this.capacity() * (this.extendRatio - 1))\n );\n // \u66f4\u65b0\u4e32\u5217\u5bb9\u91cf\n this._capacity = this.arr.length;\n }\n\n /* \u5c07\u4e32\u5217\u8f49\u63db\u70ba\u9663\u5217 */\n public toArray(): number[] {\n let size = this.size();\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n const arr = new Array(size);\n for (let i = 0; i &lt; size; i++) {\n arr[i] = this.get(i);\n }\n return arr;\n }\n}\n</code></pre> my_list.dart<pre><code>/* \u4e32\u5217\u985e\u5225 */\nclass MyList {\n late List&lt;int&gt; _arr; // \u9663\u5217\uff08\u5132\u5b58\u4e32\u5217\u5143\u7d20\uff09\n int _capacity = 10; // \u4e32\u5217\u5bb9\u91cf\n int _size = 0; // \u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\n int _extendRatio = 2; // \u6bcf\u6b21\u4e32\u5217\u64f4\u5bb9\u7684\u500d\u6578\n\n /* \u5efa\u69cb\u5b50 */\n MyList() {\n _arr = List.filled(_capacity, 0);\n }\n\n /* \u7372\u53d6\u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09*/\n int size() =&gt; _size;\n\n /* \u7372\u53d6\u4e32\u5217\u5bb9\u91cf */\n int capacity() =&gt; _capacity;\n\n /* \u8a2a\u554f\u5143\u7d20 */\n int get(int index) {\n if (index &gt;= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n return _arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n void set(int index, int _num) {\n if (index &gt;= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n _arr[index] = _num;\n }\n\n /* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\n void add(int _num) {\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (_size == _capacity) extendCapacity();\n _arr[_size] = _num;\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n _size++;\n }\n\n /* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\n void insert(int index, int _num) {\n if (index &gt;= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (_size == _capacity) extendCapacity();\n // \u5c07\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (var j = _size - 1; j &gt;= index; j--) {\n _arr[j + 1] = _arr[j];\n }\n _arr[index] = _num;\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n _size++;\n }\n\n /* \u522a\u9664\u5143\u7d20 */\n int remove(int index) {\n if (index &gt;= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n int _num = _arr[index];\n // \u5c07\u5c07\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (var j = index; j &lt; _size - 1; j++) {\n _arr[j] = _arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n _size--;\n // \u8fd4\u56de\u88ab\u522a\u9664\u7684\u5143\u7d20\n return _num;\n }\n\n /* \u4e32\u5217\u64f4\u5bb9 */\n void extendCapacity() {\n // \u65b0\u5efa\u4e00\u500b\u9577\u5ea6\u70ba\u539f\u9663\u5217 _extendRatio \u500d\u7684\u65b0\u9663\u5217\n final _newNums = List.filled(_capacity * _extendRatio, 0);\n // \u5c07\u539f\u9663\u5217\u8907\u88fd\u5230\u65b0\u9663\u5217\n List.copyRange(_newNums, 0, _arr);\n // \u66f4\u65b0 _arr \u7684\u5f15\u7528\n _arr = _newNums;\n // \u66f4\u65b0\u4e32\u5217\u5bb9\u91cf\n _capacity = _arr.length;\n }\n\n /* \u5c07\u4e32\u5217\u8f49\u63db\u70ba\u9663\u5217 */\n List&lt;int&gt; toArray() {\n List&lt;int&gt; arr = [];\n for (var i = 0; i &lt; _size; i++) {\n arr.add(get(i));\n }\n return arr;\n }\n}\n</code></pre> my_list.rs<pre><code>/* \u4e32\u5217\u985e\u5225 */\n#[allow(dead_code)]\nstruct MyList {\n arr: Vec&lt;i32&gt;, // \u9663\u5217\uff08\u5132\u5b58\u4e32\u5217\u5143\u7d20\uff09\n capacity: usize, // \u4e32\u5217\u5bb9\u91cf\n size: usize, // \u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\n extend_ratio: usize, // \u6bcf\u6b21\u4e32\u5217\u64f4\u5bb9\u7684\u500d\u6578\n}\n\n#[allow(unused, unused_comparisons)]\nimpl MyList {\n /* \u5efa\u69cb\u5b50 */\n pub fn new(capacity: usize) -&gt; Self {\n let mut vec = Vec::new();\n vec.resize(capacity, 0);\n Self {\n arr: vec,\n capacity,\n size: 0,\n extend_ratio: 2,\n }\n }\n\n /* \u7372\u53d6\u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09*/\n pub fn size(&amp;self) -&gt; usize {\n return self.size;\n }\n\n /* \u7372\u53d6\u4e32\u5217\u5bb9\u91cf */\n pub fn capacity(&amp;self) -&gt; usize {\n return self.capacity;\n }\n\n /* \u8a2a\u554f\u5143\u7d20 */\n pub fn get(&amp;self, index: usize) -&gt; i32 {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5247\u4e1f\u64f2\u7570\u5e38\uff0c\u4e0b\u540c\n if index &gt;= self.size {\n panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n };\n return self.arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n pub fn set(&amp;mut self, index: usize, num: i32) {\n if index &gt;= self.size {\n panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n };\n self.arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\n pub fn add(&amp;mut self, num: i32) {\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if self.size == self.capacity() {\n self.extend_capacity();\n }\n self.arr[self.size] = num;\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n self.size += 1;\n }\n\n /* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\n pub fn insert(&amp;mut self, index: usize, num: i32) {\n if index &gt;= self.size() {\n panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n };\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if self.size == self.capacity() {\n self.extend_capacity();\n }\n // \u5c07\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for j in (index..self.size).rev() {\n self.arr[j + 1] = self.arr[j];\n }\n self.arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n self.size += 1;\n }\n\n /* \u522a\u9664\u5143\u7d20 */\n pub fn remove(&amp;mut self, index: usize) -&gt; i32 {\n if index &gt;= self.size() {\n panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n };\n let num = self.arr[index];\n // \u5c07\u5c07\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for j in (index..self.size - 1) {\n self.arr[j] = self.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n self.size -= 1;\n // \u8fd4\u56de\u88ab\u522a\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u4e32\u5217\u64f4\u5bb9 */\n pub fn extend_capacity(&amp;mut self) {\n // \u65b0\u5efa\u4e00\u500b\u9577\u5ea6\u70ba\u539f\u9663\u5217 extend_ratio \u500d\u7684\u65b0\u9663\u5217\uff0c\u4e26\u5c07\u539f\u9663\u5217\u8907\u88fd\u5230\u65b0\u9663\u5217\n let new_capacity = self.capacity * self.extend_ratio;\n self.arr.resize(new_capacity, 0);\n // \u66f4\u65b0\u4e32\u5217\u5bb9\u91cf\n self.capacity = new_capacity;\n }\n\n /* \u5c07\u4e32\u5217\u8f49\u63db\u70ba\u9663\u5217 */\n pub fn to_array(&amp;mut self) -&gt; Vec&lt;i32&gt; {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n let mut arr = Vec::new();\n for i in 0..self.size {\n arr.push(self.get(i));\n }\n arr\n }\n}\n</code></pre> my_list.c<pre><code>/* \u4e32\u5217\u985e\u5225 */\ntypedef struct {\n int *arr; // \u9663\u5217\uff08\u5132\u5b58\u4e32\u5217\u5143\u7d20\uff09\n int capacity; // \u4e32\u5217\u5bb9\u91cf\n int size; // \u4e32\u5217\u5927\u5c0f\n int extendRatio; // \u4e32\u5217\u6bcf\u6b21\u64f4\u5bb9\u7684\u500d\u6578\n} MyList;\n\n/* \u5efa\u69cb\u5b50 */\nMyList *newMyList() {\n MyList *nums = malloc(sizeof(MyList));\n nums-&gt;capacity = 10;\n nums-&gt;arr = malloc(sizeof(int) * nums-&gt;capacity);\n nums-&gt;size = 0;\n nums-&gt;extendRatio = 2;\n return nums;\n}\n\n/* \u6790\u69cb\u51fd\u5f0f */\nvoid delMyList(MyList *nums) {\n free(nums-&gt;arr);\n free(nums);\n}\n\n/* \u7372\u53d6\u4e32\u5217\u9577\u5ea6 */\nint size(MyList *nums) {\n return nums-&gt;size;\n}\n\n/* \u7372\u53d6\u4e32\u5217\u5bb9\u91cf */\nint capacity(MyList *nums) {\n return nums-&gt;capacity;\n}\n\n/* \u8a2a\u554f\u5143\u7d20 */\nint get(MyList *nums, int index) {\n assert(index &gt;= 0 &amp;&amp; index &lt; nums-&gt;size);\n return nums-&gt;arr[index];\n}\n\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(MyList *nums, int index, int num) {\n assert(index &gt;= 0 &amp;&amp; index &lt; nums-&gt;size);\n nums-&gt;arr[index] = num;\n}\n\n/* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\nvoid add(MyList *nums, int num) {\n if (size(nums) == capacity(nums)) {\n extendCapacity(nums); // \u64f4\u5bb9\n }\n nums-&gt;arr[size(nums)] = num;\n nums-&gt;size++;\n}\n\n/* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\nvoid insert(MyList *nums, int index, int num) {\n assert(index &gt;= 0 &amp;&amp; index &lt; size(nums));\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (size(nums) == capacity(nums)) {\n extendCapacity(nums); // \u64f4\u5bb9\n }\n for (int i = size(nums); i &gt; index; --i) {\n nums-&gt;arr[i] = nums-&gt;arr[i - 1];\n }\n nums-&gt;arr[index] = num;\n nums-&gt;size++;\n}\n\n/* \u522a\u9664\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u4f54\u7528\u4e86 remove \u95dc\u9375\u8a5e\nint removeItem(MyList *nums, int index) {\n assert(index &gt;= 0 &amp;&amp; index &lt; size(nums));\n int num = nums-&gt;arr[index];\n for (int i = index; i &lt; size(nums) - 1; i++) {\n nums-&gt;arr[i] = nums-&gt;arr[i + 1];\n }\n nums-&gt;size--;\n return num;\n}\n\n/* \u4e32\u5217\u64f4\u5bb9 */\nvoid extendCapacity(MyList *nums) {\n // \u5148\u5206\u914d\u7a7a\u9593\n int newCapacity = capacity(nums) * nums-&gt;extendRatio;\n int *extend = (int *)malloc(sizeof(int) * newCapacity);\n int *temp = nums-&gt;arr;\n\n // \u8907\u88fd\u820a\u8cc7\u6599\u5230\u65b0\u8cc7\u6599\n for (int i = 0; i &lt; size(nums); i++)\n extend[i] = nums-&gt;arr[i];\n\n // \u91cb\u653e\u820a\u8cc7\u6599\n free(temp);\n\n // \u66f4\u65b0\u65b0\u8cc7\u6599\n nums-&gt;arr = extend;\n nums-&gt;capacity = newCapacity;\n}\n\n/* \u5c07\u4e32\u5217\u8f49\u63db\u70ba Array \u7528\u65bc\u5217\u5370 */\nint *toArray(MyList *nums) {\n return nums-&gt;arr;\n}\n</code></pre> my_list.kt<pre><code>/* \u4e32\u5217\u985e\u5225 */\nclass MyList {\n private var arr: IntArray = intArrayOf() // \u9663\u5217\uff08\u5132\u5b58\u4e32\u5217\u5143\u7d20\uff09\n private var capacity: Int = 10 // \u4e32\u5217\u5bb9\u91cf\n private var size: Int = 0 // \u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\n private var extendRatio: Int = 2 // \u6bcf\u6b21\u4e32\u5217\u64f4\u5bb9\u7684\u500d\u6578\n\n /* \u5efa\u69cb\u5b50 */\n init {\n arr = IntArray(capacity)\n }\n\n /* \u7372\u53d6\u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09 */\n fun size(): Int {\n return size\n }\n\n /* \u7372\u53d6\u4e32\u5217\u5bb9\u91cf */\n fun capacity(): Int {\n return capacity\n }\n\n /* \u8a2a\u554f\u5143\u7d20 */\n fun get(index: Int): Int {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5247\u4e1f\u64f2\u7570\u5e38\uff0c\u4e0b\u540c\n if (index &lt; 0 || index &gt;= size)\n throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n return arr[index]\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n fun set(index: Int, num: Int) {\n if (index &lt; 0 || index &gt;= size)\n throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n arr[index] = num\n }\n\n /* \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 */\n fun add(num: Int) {\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (size == capacity())\n extendCapacity()\n arr[size] = num\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n size++\n }\n\n /* \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 */\n fun insert(index: Int, num: Int) {\n if (index &lt; 0 || index &gt;= size)\n throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (size == capacity())\n extendCapacity()\n // \u5c07\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for (j in size - 1 downTo index)\n arr[j + 1] = arr[j]\n arr[index] = num\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n size++\n }\n\n /* \u522a\u9664\u5143\u7d20 */\n fun remove(index: Int): Int {\n if (index &lt; 0 || index &gt;= size)\n throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n val num = arr[index]\n // \u5c07\u5c07\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for (j in index..&lt;size - 1)\n arr[j] = arr[j + 1]\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n size--\n // \u8fd4\u56de\u88ab\u522a\u9664\u7684\u5143\u7d20\n return num\n }\n\n /* \u4e32\u5217\u64f4\u5bb9 */\n fun extendCapacity() {\n // \u65b0\u5efa\u4e00\u500b\u9577\u5ea6\u70ba\u539f\u9663\u5217 extendRatio \u500d\u7684\u65b0\u9663\u5217\uff0c\u4e26\u5c07\u539f\u9663\u5217\u8907\u88fd\u5230\u65b0\u9663\u5217\n arr = arr.copyOf(capacity() * extendRatio)\n // \u66f4\u65b0\u4e32\u5217\u5bb9\u91cf\n capacity = arr.size\n }\n\n /* \u5c07\u4e32\u5217\u8f49\u63db\u70ba\u9663\u5217 */\n fun toArray(): IntArray {\n val size = size()\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n val arr = IntArray(size)\n for (i in 0..&lt;size) {\n arr[i] = get(i)\n }\n return arr\n }\n}\n</code></pre> my_list.rb<pre><code>### \u4e32\u5217\u985e\u5225 ###\nclass MyList\n attr_reader :size # \u7372\u53d6\u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\n attr_reader :capacity # \u7372\u53d6\u4e32\u5217\u5bb9\u91cf\n\n ### \u5efa\u69cb\u5b50 ###\n def initialize\n @capacity = 10\n @size = 0\n @extend_ratio = 2\n @arr = Array.new(capacity)\n end\n\n ### \u8a2a\u554f\u5143\u7d20 ###\n def get(index)\n # \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5247\u4e1f\u64f2\u7570\u5e38\uff0c\u4e0b\u540c\n raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index &lt; 0 || index &gt;= size\n @arr[index]\n end\n\n ### \u8a2a\u554f\u5143\u7d20 ###\n def set(index, num)\n raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index &lt; 0 || index &gt;= size\n @arr[index] = num\n end\n\n ### \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20 ###\n def add(num)\n # \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n extend_capacity if size == capacity\n @arr[size] = num\n\n # \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n @size += 1\n end\n\n ### \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20 ###\n def insert(index, num)\n raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index &lt; 0 || index &gt;= size\n\n # \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n extend_capacity if size == capacity\n\n # \u5c07\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n for j in (size - 1).downto(index)\n @arr[j + 1] = @arr[j]\n end\n @arr[index] = num\n\n # \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n @size += 1\n end\n\n ### \u522a\u9664\u5143\u7d20 ###\n def remove(index)\n raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index &lt; 0 || index &gt;= size\n num = @arr[index]\n\n # \u5c07\u5c07\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n for j in index...size\n @arr[j] = @arr[j + 1]\n end\n\n # \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n @size -= 1\n\n # \u8fd4\u56de\u88ab\u522a\u9664\u7684\u5143\u7d20\n num\n end\n\n ### \u4e32\u5217\u64f4\u5bb9 ###\n def extend_capacity\n # \u65b0\u5efa\u4e00\u500b\u9577\u5ea6\u70ba\u539f\u9663\u5217 extend_ratio \u500d\u7684\u65b0\u9663\u5217\uff0c\u4e26\u5c07\u539f\u9663\u5217\u8907\u88fd\u5230\u65b0\u9663\u5217\n arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1))\n # \u66f4\u65b0\u4e32\u5217\u5bb9\u91cf\n @capacity = arr.length\n end\n\n ### \u5c07\u4e32\u5217\u8f49\u63db\u70ba\u9663\u5217 ###\n def to_array\n sz = size\n # \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n arr = Array.new(sz)\n for i in 0...sz\n arr[i] = get(i)\n end\n arr\n end\nend\n</code></pre> my_list.zig<pre><code>// \u4e32\u5217\u985e\u5225\nfn MyList(comptime T: type) type {\n return struct {\n const Self = @This();\n\n arr: []T = undefined, // \u9663\u5217\uff08\u5132\u5b58\u4e32\u5217\u5143\u7d20\uff09\n arrCapacity: usize = 10, // \u4e32\u5217\u5bb9\u91cf\n numSize: usize = 0, // \u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\n extendRatio: usize = 2, // \u6bcf\u6b21\u4e32\u5217\u64f4\u5bb9\u7684\u500d\u6578\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u8a18\u61b6\u9ad4\u5206\u914d\u5668\n\n // \u5efa\u69cb\u5b50\uff08\u5206\u914d\u8a18\u61b6\u9ad4+\u521d\u59cb\u5316\u4e32\u5217\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.arr = try self.mem_allocator.alloc(T, self.arrCapacity);\n @memset(self.arr, @as(T, 0));\n }\n\n // \u6790\u69cb\u51fd\u5f0f\uff08\u91cb\u653e\u8a18\u61b6\u9ad4\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u7372\u53d6\u4e32\u5217\u9577\u5ea6\uff08\u7576\u524d\u5143\u7d20\u6578\u91cf\uff09\n pub fn size(self: *Self) usize {\n return self.numSize;\n }\n\n // \u7372\u53d6\u4e32\u5217\u5bb9\u91cf\n pub fn capacity(self: *Self) usize {\n return self.arrCapacity;\n }\n\n // \u8a2a\u554f\u5143\u7d20\n pub fn get(self: *Self, index: usize) T {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5247\u4e1f\u64f2\u7570\u5e38\uff0c\u4e0b\u540c\n if (index &lt; 0 or index &gt;= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n return self.arr[index];\n } \n\n // \u66f4\u65b0\u5143\u7d20\n pub fn set(self: *Self, index: usize, num: T) void {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5247\u4e1f\u64f2\u7570\u5e38\uff0c\u4e0b\u540c\n if (index &lt; 0 or index &gt;= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n self.arr[index] = num;\n } \n\n // \u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20\n pub fn add(self: *Self, num: T) !void {\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (self.size() == self.capacity()) try self.extendCapacity();\n self.arr[self.size()] = num;\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n self.numSize += 1;\n } \n\n // \u5728\u4e2d\u9593\u63d2\u5165\u5143\u7d20\n pub fn insert(self: *Self, index: usize, num: T) !void {\n if (index &lt; 0 or index &gt;= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6578\u91cf\u8d85\u51fa\u5bb9\u91cf\u6642\uff0c\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\n if (self.size() == self.capacity()) try self.extendCapacity();\n // \u5c07\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n var j = self.size() - 1;\n while (j &gt;= index) : (j -= 1) {\n self.arr[j + 1] = self.arr[j];\n }\n self.arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n self.numSize += 1;\n }\n\n // \u522a\u9664\u5143\u7d20\n pub fn remove(self: *Self, index: usize) T {\n if (index &lt; 0 or index &gt;= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n var num = self.arr[index];\n // \u5c07\u7d22\u5f15 index \u4e4b\u5f8c\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52d5\u4e00\u4f4d\n var j = index;\n while (j &lt; self.size() - 1) : (j += 1) {\n self.arr[j] = self.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6578\u91cf\n self.numSize -= 1;\n // \u8fd4\u56de\u88ab\u522a\u9664\u7684\u5143\u7d20\n return num;\n }\n\n // \u4e32\u5217\u64f4\u5bb9\n pub fn extendCapacity(self: *Self) !void {\n // \u65b0\u5efa\u4e00\u500b\u9577\u5ea6\u70ba size * extendRatio \u7684\u9663\u5217\uff0c\u4e26\u5c07\u539f\u9663\u5217\u8907\u88fd\u5230\u65b0\u9663\u5217\n var newCapacity = self.capacity() * self.extendRatio;\n var extend = try self.mem_allocator.alloc(T, newCapacity);\n @memset(extend, @as(T, 0));\n // \u5c07\u539f\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8907\u88fd\u5230\u65b0\u9663\u5217\n std.mem.copy(T, extend, self.arr);\n self.arr = extend;\n // \u66f4\u65b0\u4e32\u5217\u5bb9\u91cf\n self.arrCapacity = newCapacity;\n }\n\n // \u5c07\u4e32\u5217\u8f49\u63db\u70ba\u9663\u5217\n pub fn toArray(self: *Self) ![]T {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n var arr = try self.mem_allocator.alloc(T, self.size());\n @memset(arr, @as(T, 0));\n for (arr, 0..) |*num, i| {\n num.* = self.get(i);\n }\n return arr;\n }\n };\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_array_and_linkedlist/ram_and_cache/","title":"4.4 \u00a0 \u8a18\u61b6\u9ad4\u8207\u5feb\u53d6 *","text":"<p>\u5728\u672c\u7ae0\u7684\u524d\u5169\u7bc0\u4e2d\uff0c\u6211\u5011\u63a2\u8a0e\u4e86\u9663\u5217\u548c\u93c8\u7d50\u4e32\u5217\u9019\u5169\u7a2e\u57fa\u790e\u4e14\u91cd\u8981\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u5b83\u5011\u5206\u5225\u4ee3\u8868\u4e86\u201c\u9023\u7e8c\u5132\u5b58\u201d\u548c\u201c\u5206\u6563\u5132\u5b58\u201d\u5169\u7a2e\u7269\u7406\u7d50\u69cb\u3002</p> <p>\u5be6\u969b\u4e0a\uff0c\u7269\u7406\u7d50\u69cb\u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u6c7a\u5b9a\u4e86\u7a0b\u5f0f\u5c0d\u8a18\u61b6\u9ad4\u548c\u5feb\u53d6\u7684\u4f7f\u7528\u6548\u7387\uff0c\u9032\u800c\u5f71\u97ff\u6f14\u7b97\u6cd5\u7a0b\u5f0f\u7684\u6574\u9ad4\u6548\u80fd\u3002</p>"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#441","title":"4.4.1 \u00a0 \u8a08\u7b97\u6a5f\u5132\u5b58\u88dd\u7f6e","text":"<p>\u8a08\u7b97\u6a5f\u4e2d\u5305\u62ec\u4e09\u7a2e\u985e\u578b\u7684\u5132\u5b58\u88dd\u7f6e\uff1a\u786c\u789f\uff08hard disk\uff09\u3001\u8a18\u61b6\u9ad4\uff08random-access memory, RAM\uff09\u3001\u5feb\u53d6\uff08cache memory\uff09\u3002\u8868 4-2 \u5c55\u793a\u4e86\u5b83\u5011\u5728\u8a08\u7b97\u6a5f\u7cfb\u7d71\u4e2d\u7684\u4e0d\u540c\u89d2\u8272\u548c\u6548\u80fd\u7279\u9ede\u3002</p> <p> \u8868 4-2 \u00a0 \u8a08\u7b97\u6a5f\u7684\u5132\u5b58\u88dd\u7f6e </p> \u786c\u789f \u8a18\u61b6\u9ad4 \u5feb\u53d6 \u7528\u9014 \u9577\u671f\u5132\u5b58\u8cc7\u6599\uff0c\u5305\u62ec\u4f5c\u696d\u7cfb\u7d71\u3001\u7a0b\u5f0f\u3001\u6a94\u6848\u7b49 \u81e8\u6642\u5132\u5b58\u7576\u524d\u57f7\u884c\u7684\u7a0b\u5f0f\u548c\u6b63\u5728\u8655\u7406\u7684\u8cc7\u6599 \u5132\u5b58\u7d93\u5e38\u8a2a\u554f\u7684\u8cc7\u6599\u548c\u6307\u4ee4\uff0c\u6e1b\u5c11 CPU \u8a2a\u554f\u8a18\u61b6\u9ad4\u7684\u6b21\u6578 \u6613\u5931\u6027 \u65b7\u96fb\u5f8c\u8cc7\u6599\u4e0d\u6703\u4e1f\u5931 \u65b7\u96fb\u5f8c\u8cc7\u6599\u6703\u4e1f\u5931 \u65b7\u96fb\u5f8c\u8cc7\u6599\u6703\u4e1f\u5931 \u5bb9\u91cf \u8f03\u5927\uff0cTB \u7d1a\u5225 \u8f03\u5c0f\uff0cGB \u7d1a\u5225 \u975e\u5e38\u5c0f\uff0cMB \u7d1a\u5225 \u901f\u5ea6 \u8f03\u6162\uff0c\u5e7e\u767e\u5230\u5e7e\u5343 MB/s \u8f03\u5feb\uff0c\u5e7e\u5341 GB/s \u975e\u5e38\u5feb\uff0c\u5e7e\u5341\u5230\u5e7e\u767e GB/s \u50f9\u683c \u8f03\u4fbf\u5b9c\uff0c\u5e7e\u6bdb\u5230\u5e7e\u5143 / GB \u8f03\u8cb4\uff0c\u5e7e\u5341\u5230\u5e7e\u767e\u5143 / GB \u975e\u5e38\u8cb4\uff0c\u96a8 CPU \u6253\u5305\u8a08\u50f9 <p>\u6211\u5011\u53ef\u4ee5\u5c07\u8a08\u7b97\u6a5f\u5132\u5b58\u7cfb\u7d71\u60f3\u8c61\u70ba\u5716 4-9 \u6240\u793a\u7684\u91d1\u5b57\u5854\u7d50\u69cb\u3002\u8d8a\u9760\u8fd1\u91d1\u5b57\u5854\u9802\u7aef\u7684\u5132\u5b58\u88dd\u7f6e\u7684\u901f\u5ea6\u8d8a\u5feb\u3001\u5bb9\u91cf\u8d8a\u5c0f\u3001\u6210\u672c\u8d8a\u9ad8\u3002\u9019\u7a2e\u591a\u5c64\u7d1a\u7684\u8a2d\u8a08\u4e26\u975e\u5076\u7136\uff0c\u800c\u662f\u8a08\u7b97\u6a5f\u79d1\u5b78\u5bb6\u548c\u5de5\u7a0b\u5e2b\u5011\u7d93\u904e\u6df1\u601d\u719f\u616e\u7684\u7d50\u679c\u3002</p> <ul> <li>\u786c\u789f\u96e3\u4ee5\u88ab\u8a18\u61b6\u9ad4\u53d6\u4ee3\u3002\u9996\u5148\uff0c\u8a18\u61b6\u9ad4\u4e2d\u7684\u8cc7\u6599\u5728\u65b7\u96fb\u5f8c\u6703\u4e1f\u5931\uff0c\u56e0\u6b64\u5b83\u4e0d\u9069\u5408\u9577\u671f\u5132\u5b58\u8cc7\u6599\uff1b\u5176\u6b21\uff0c\u8a18\u61b6\u9ad4\u7684\u6210\u672c\u662f\u786c\u789f\u7684\u5e7e\u5341\u500d\uff0c\u9019\u4f7f\u5f97\u5b83\u96e3\u4ee5\u5728\u6d88\u8cbb\u8005\u5e02\u5834\u666e\u53ca\u3002</li> <li>\u5feb\u53d6\u7684\u5927\u5bb9\u91cf\u548c\u9ad8\u901f\u5ea6\u96e3\u4ee5\u517c\u5f97\u3002\u96a8\u8457 L1\u3001L2\u3001L3 \u5feb\u53d6\u7684\u5bb9\u91cf\u9010\u6b65\u589e\u5927\uff0c\u5176\u7269\u7406\u5c3a\u5bf8\u6703\u8b8a\u5927\uff0c\u8207 CPU \u6838\u5fc3\u4e4b\u9593\u7684\u7269\u7406\u8ddd\u96e2\u6703\u8b8a\u9060\uff0c\u5f9e\u800c\u5c0e\u81f4\u8cc7\u6599\u50b3\u8f38\u6642\u9593\u589e\u52a0\uff0c\u5143\u7d20\u8a2a\u554f\u5ef6\u9072\u8b8a\u9ad8\u3002\u5728\u7576\u524d\u6280\u8853\u4e0b\uff0c\u591a\u5c64\u7d1a\u7684\u5feb\u53d6\u7d50\u69cb\u662f\u5bb9\u91cf\u3001\u901f\u5ea6\u548c\u6210\u672c\u4e4b\u9593\u7684\u6700\u4f73\u5e73\u8861\u9ede\u3002</li> </ul> <p></p> <p> \u5716 4-9 \u00a0 \u8a08\u7b97\u6a5f\u5132\u5b58\u7cfb\u7d71 </p> <p>Tip</p> <p>\u8a08\u7b97\u6a5f\u7684\u5132\u5b58\u5c64\u6b21\u7d50\u69cb\u9ad4\u73fe\u4e86\u901f\u5ea6\u3001\u5bb9\u91cf\u548c\u6210\u672c\u4e09\u8005\u4e4b\u9593\u7684\u7cbe\u5999\u5e73\u8861\u3002\u5be6\u969b\u4e0a\uff0c\u9019\u7a2e\u6b0a\u8861\u666e\u904d\u5b58\u5728\u65bc\u6240\u6709\u5de5\u696d\u9818\u57df\uff0c\u5b83\u8981\u6c42\u6211\u5011\u5728\u4e0d\u540c\u7684\u512a\u52e2\u548c\u9650\u5236\u4e4b\u9593\u627e\u5230\u6700\u4f73\u5e73\u8861\u9ede\u3002</p> <p>\u7e3d\u7684\u4f86\u8aaa\uff0c\u786c\u789f\u7528\u65bc\u9577\u671f\u5132\u5b58\u5927\u91cf\u8cc7\u6599\uff0c\u8a18\u61b6\u9ad4\u7528\u65bc\u81e8\u6642\u5132\u5b58\u7a0b\u5f0f\u57f7\u884c\u4e2d\u6b63\u5728\u8655\u7406\u7684\u8cc7\u6599\uff0c\u800c\u5feb\u53d6\u5247\u7528\u65bc\u5132\u5b58\u7d93\u5e38\u8a2a\u554f\u7684\u8cc7\u6599\u548c\u6307\u4ee4\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5f0f\u57f7\u884c\u6548\u7387\u3002\u4e09\u8005\u5171\u540c\u5354\u4f5c\uff0c\u78ba\u4fdd\u8a08\u7b97\u6a5f\u7cfb\u7d71\u9ad8\u6548\u57f7\u884c\u3002</p> <p>\u5982\u5716 4-10 \u6240\u793a\uff0c\u5728\u7a0b\u5f0f\u57f7\u884c\u6642\uff0c\u8cc7\u6599\u6703\u5f9e\u786c\u789f\u4e2d\u88ab\u8b80\u53d6\u5230\u8a18\u61b6\u9ad4\u4e2d\uff0c\u4f9b CPU \u8a08\u7b97\u4f7f\u7528\u3002\u5feb\u53d6\u53ef\u4ee5\u770b\u4f5c CPU \u7684\u4e00\u90e8\u5206\uff0c\u5b83\u900f\u904e\u667a\u6167\u5730\u5f9e\u8a18\u61b6\u9ad4\u8f09\u5165\u8cc7\u6599\uff0c\u7d66 CPU \u63d0\u4f9b\u9ad8\u901f\u7684\u8cc7\u6599\u8b80\u53d6\uff0c\u5f9e\u800c\u986f\u8457\u63d0\u5347\u7a0b\u5f0f\u7684\u57f7\u884c\u6548\u7387\uff0c\u6e1b\u5c11\u5c0d\u8f03\u6162\u7684\u8a18\u61b6\u9ad4\u7684\u4f9d\u8cf4\u3002</p> <p></p> <p> \u5716 4-10 \u00a0 \u786c\u789f\u3001\u8a18\u61b6\u9ad4\u548c\u5feb\u53d6\u4e4b\u9593\u7684\u8cc7\u6599\u6d41\u901a </p>"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#442","title":"4.4.2 \u00a0 \u8cc7\u6599\u7d50\u69cb\u7684\u8a18\u61b6\u9ad4\u6548\u7387","text":"<p>\u5728\u8a18\u61b6\u9ad4\u7a7a\u9593\u5229\u7528\u65b9\u9762\uff0c\u9663\u5217\u548c\u93c8\u7d50\u4e32\u5217\u5404\u81ea\u5177\u6709\u512a\u52e2\u548c\u4fb7\u9650\u6027\u3002</p> <p>\u4e00\u65b9\u9762\uff0c\u8a18\u61b6\u9ad4\u662f\u6709\u9650\u7684\uff0c\u4e14\u540c\u4e00\u584a\u8a18\u61b6\u9ad4\u4e0d\u80fd\u88ab\u591a\u500b\u7a0b\u5f0f\u5171\u4eab\uff0c\u56e0\u6b64\u6211\u5011\u5e0c\u671b\u8cc7\u6599\u7d50\u69cb\u80fd\u5920\u5118\u53ef\u80fd\u9ad8\u6548\u5730\u5229\u7528\u7a7a\u9593\u3002\u9663\u5217\u7684\u5143\u7d20\u7dca\u5bc6\u6392\u5217\uff0c\u4e0d\u9700\u8981\u984d\u5916\u7684\u7a7a\u9593\u4f86\u5132\u5b58\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u9593\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\uff0c\u56e0\u6b64\u7a7a\u9593\u6548\u7387\u66f4\u9ad8\u3002\u7136\u800c\uff0c\u9663\u5217\u9700\u8981\u4e00\u6b21\u6027\u5206\u914d\u8db3\u5920\u7684\u9023\u7e8c\u8a18\u61b6\u9ad4\u7a7a\u9593\uff0c\u9019\u53ef\u80fd\u5c0e\u81f4\u8a18\u61b6\u9ad4\u6d6a\u8cbb\uff0c\u9663\u5217\u64f4\u5bb9\u4e5f\u9700\u8981\u984d\u5916\u7684\u6642\u9593\u548c\u7a7a\u9593\u6210\u672c\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u93c8\u7d50\u4e32\u5217\u4ee5\u201c\u7bc0\u9ede\u201d\u70ba\u55ae\u4f4d\u9032\u884c\u52d5\u614b\u8a18\u61b6\u9ad4\u5206\u914d\u548c\u56de\u6536\uff0c\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u9748\u6d3b\u6027\u3002</p> <p>\u53e6\u4e00\u65b9\u9762\uff0c\u5728\u7a0b\u5f0f\u57f7\u884c\u6642\uff0c\u96a8\u8457\u53cd\u8986\u7533\u8acb\u8207\u91cb\u653e\u8a18\u61b6\u9ad4\uff0c\u7a7a\u9592\u8a18\u61b6\u9ad4\u7684\u788e\u7247\u5316\u7a0b\u5ea6\u6703\u8d8a\u4f86\u8d8a\u9ad8\uff0c\u5f9e\u800c\u5c0e\u81f4\u8a18\u61b6\u9ad4\u7684\u5229\u7528\u6548\u7387\u964d\u4f4e\u3002\u9663\u5217\u7531\u65bc\u5176\u9023\u7e8c\u7684\u5132\u5b58\u65b9\u5f0f\uff0c\u76f8\u5c0d\u4e0d\u5bb9\u6613\u5c0e\u81f4\u8a18\u61b6\u9ad4\u788e\u7247\u5316\u3002\u76f8\u53cd\uff0c\u93c8\u7d50\u4e32\u5217\u7684\u5143\u7d20\u662f\u5206\u6563\u5132\u5b58\u7684\uff0c\u5728\u983b\u7e41\u7684\u63d2\u5165\u8207\u522a\u9664\u64cd\u4f5c\u4e2d\uff0c\u66f4\u5bb9\u6613\u5c0e\u81f4\u8a18\u61b6\u9ad4\u788e\u7247\u5316\u3002</p>"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#443","title":"4.4.3 \u00a0 \u8cc7\u6599\u7d50\u69cb\u7684\u5feb\u53d6\u6548\u7387","text":"<p>\u5feb\u53d6\u96d6\u7136\u5728\u7a7a\u9593\u5bb9\u91cf\u4e0a\u9060\u5c0f\u65bc\u8a18\u61b6\u9ad4\uff0c\u4f46\u5b83\u6bd4\u8a18\u61b6\u9ad4\u5feb\u5f97\u591a\uff0c\u5728\u7a0b\u5f0f\u57f7\u884c\u901f\u5ea6\u4e0a\u8d77\u8457\u81f3\u95dc\u91cd\u8981\u7684\u4f5c\u7528\u3002\u7531\u65bc\u5feb\u53d6\u7684\u5bb9\u91cf\u6709\u9650\uff0c\u53ea\u80fd\u5132\u5b58\u4e00\u5c0f\u90e8\u5206\u983b\u7e41\u8a2a\u554f\u7684\u8cc7\u6599\uff0c\u56e0\u6b64\u7576 CPU \u5617\u8a66\u8a2a\u554f\u7684\u8cc7\u6599\u4e0d\u5728\u5feb\u53d6\u4e2d\u6642\uff0c\u5c31\u6703\u767c\u751f\u5feb\u53d6\u672a\u547d\u4e2d\uff08cache miss\uff09\uff0c\u6b64\u6642 CPU \u4e0d\u5f97\u4e0d\u5f9e\u901f\u5ea6\u8f03\u6162\u7684\u8a18\u61b6\u9ad4\u4e2d\u8f09\u5165\u6240\u9700\u8cc7\u6599\u3002</p> <p>\u986f\u7136\uff0c\u201c\u5feb\u53d6\u672a\u547d\u4e2d\u201d\u8d8a\u5c11\uff0cCPU \u8b80\u5beb\u8cc7\u6599\u7684\u6548\u7387\u5c31\u8d8a\u9ad8\uff0c\u7a0b\u5f0f\u6548\u80fd\u4e5f\u5c31\u8d8a\u597d\u3002\u6211\u5011\u5c07 CPU \u5f9e\u5feb\u53d6\u4e2d\u6210\u529f\u7372\u53d6\u8cc7\u6599\u7684\u6bd4\u4f8b\u7a31\u70ba\u5feb\u53d6\u547d\u4e2d\u7387\uff08cache hit rate\uff09\uff0c\u9019\u500b\u6307\u6a19\u901a\u5e38\u7528\u4f86\u8861\u91cf\u5feb\u53d6\u6548\u7387\u3002</p> <p>\u70ba\u4e86\u5118\u53ef\u80fd\u9054\u5230\u66f4\u9ad8\u7684\u6548\u7387\uff0c\u5feb\u53d6\u6703\u63a1\u53d6\u4ee5\u4e0b\u8cc7\u6599\u8f09\u5165\u6a5f\u5236\u3002</p> <ul> <li>\u5feb\u53d6\u884c\uff1a\u5feb\u53d6\u4e0d\u662f\u55ae\u500b\u4f4d\u5143\u7d44\u5730\u5132\u5b58\u8207\u8f09\u5165\u8cc7\u6599\uff0c\u800c\u662f\u4ee5\u5feb\u53d6\u884c\u70ba\u55ae\u4f4d\u3002\u76f8\u6bd4\u65bc\u55ae\u500b\u4f4d\u5143\u7d44\u7684\u50b3\u8f38\uff0c\u5feb\u53d6\u884c\u7684\u50b3\u8f38\u5f62\u5f0f\u66f4\u52a0\u9ad8\u6548\u3002</li> <li>\u9810\u53d6\u6a5f\u5236\uff1a\u8655\u7406\u5668\u6703\u5617\u8a66\u9810\u6e2c\u8cc7\u6599\u8a2a\u554f\u6a21\u5f0f\uff08\u4f8b\u5982\u9806\u5e8f\u8a2a\u554f\u3001\u56fa\u5b9a\u6b65\u9577\u8df3\u8e8d\u8a2a\u554f\u7b49\uff09\uff0c\u4e26\u6839\u64da\u7279\u5b9a\u6a21\u5f0f\u5c07\u8cc7\u6599\u8f09\u5165\u81f3\u5feb\u53d6\u4e4b\u4e2d\uff0c\u5f9e\u800c\u63d0\u5347\u547d\u4e2d\u7387\u3002</li> <li>\u7a7a\u9593\u5340\u57df\u6027\uff1a\u5982\u679c\u4e00\u500b\u6578\u64da\u88ab\u8a2a\u554f\uff0c\u90a3\u9ebc\u5b83\u9644\u8fd1\u7684\u8cc7\u6599\u53ef\u80fd\u8fd1\u671f\u4e5f\u6703\u88ab\u8a2a\u554f\u3002\u56e0\u6b64\uff0c\u5feb\u53d6\u5728\u8f09\u5165\u67d0\u4e00\u8cc7\u6599\u6642\uff0c\u4e5f\u6703\u8f09\u5165\u5176\u9644\u8fd1\u7684\u8cc7\u6599\uff0c\u4ee5\u63d0\u9ad8\u547d\u4e2d\u7387\u3002</li> <li>\u6642\u9593\u5340\u57df\u6027\uff1a\u5982\u679c\u4e00\u500b\u6578\u64da\u88ab\u8a2a\u554f\uff0c\u90a3\u9ebc\u5b83\u5728\u4e0d\u4e45\u7684\u5c07\u4f86\u5f88\u53ef\u80fd\u518d\u6b21\u88ab\u8a2a\u554f\u3002\u5feb\u53d6\u5229\u7528\u9019\u4e00\u539f\u7406\uff0c\u900f\u904e\u4fdd\u7559\u6700\u8fd1\u8a2a\u554f\u904e\u7684\u8cc7\u6599\u4f86\u63d0\u9ad8\u547d\u4e2d\u7387\u3002</li> </ul> <p>\u5be6\u969b\u4e0a\uff0c\u9663\u5217\u548c\u93c8\u7d50\u4e32\u5217\u5c0d\u5feb\u53d6\u7684\u5229\u7528\u6548\u7387\u662f\u4e0d\u540c\u7684\uff0c\u4e3b\u8981\u9ad4\u73fe\u5728\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\u3002</p> <ul> <li>\u4f54\u7528\u7a7a\u9593\uff1a\u93c8\u7d50\u4e32\u5217\u5143\u7d20\u6bd4\u9663\u5217\u5143\u7d20\u4f54\u7528\u7a7a\u9593\u66f4\u591a\uff0c\u5c0e\u81f4\u5feb\u53d6\u4e2d\u5bb9\u7d0d\u7684\u6709\u6548\u8cc7\u6599\u91cf\u66f4\u5c11\u3002</li> <li>\u5feb\u53d6\u884c\uff1a\u93c8\u7d50\u4e32\u5217\u8cc7\u6599\u5206\u6563\u5728\u8a18\u61b6\u9ad4\u5404\u8655\uff0c\u800c\u5feb\u53d6\u662f\u201c\u6309\u884c\u8f09\u5165\u201d\u7684\uff0c\u56e0\u6b64\u8f09\u5165\u5230\u7121\u6548\u8cc7\u6599\u7684\u6bd4\u4f8b\u66f4\u9ad8\u3002</li> <li>\u9810\u53d6\u6a5f\u5236\uff1a\u9663\u5217\u6bd4\u93c8\u7d50\u4e32\u5217\u7684\u8cc7\u6599\u8a2a\u554f\u6a21\u5f0f\u66f4\u5177\u201c\u53ef\u9810\u6e2c\u6027\u201d\uff0c\u5373\u7cfb\u7d71\u66f4\u5bb9\u6613\u731c\u51fa\u5373\u5c07\u88ab\u8f09\u5165\u7684\u8cc7\u6599\u3002</li> <li>\u7a7a\u9593\u5340\u57df\u6027\uff1a\u9663\u5217\u88ab\u5132\u5b58\u5728\u96c6\u4e2d\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u4e2d\uff0c\u56e0\u6b64\u88ab\u8f09\u5165\u8cc7\u6599\u9644\u8fd1\u7684\u8cc7\u6599\u66f4\u6709\u53ef\u80fd\u5373\u5c07\u88ab\u8a2a\u554f\u3002</li> </ul> <p>\u7e3d\u9ad4\u800c\u8a00\uff0c\u9663\u5217\u5177\u6709\u66f4\u9ad8\u7684\u5feb\u53d6\u547d\u4e2d\u7387\uff0c\u56e0\u6b64\u5b83\u5728\u64cd\u4f5c\u6548\u7387\u4e0a\u901a\u5e38\u512a\u65bc\u93c8\u7d50\u4e32\u5217\u3002\u9019\u4f7f\u5f97\u5728\u89e3\u6c7a\u6f14\u7b97\u6cd5\u554f\u984c\u6642\uff0c\u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u8cc7\u6599\u7d50\u69cb\u5f80\u5f80\u66f4\u53d7\u6b61\u8fce\u3002</p> <p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9ad8\u5feb\u53d6\u6548\u7387\u4e26\u4e0d\u610f\u5473\u8457\u9663\u5217\u5728\u6240\u6709\u60c5\u6cc1\u4e0b\u90fd\u512a\u65bc\u93c8\u7d50\u4e32\u5217\u3002\u5be6\u969b\u61c9\u7528\u4e2d\u9078\u64c7\u54ea\u7a2e\u8cc7\u6599\u7d50\u69cb\uff0c\u61c9\u6839\u64da\u5177\u9ad4\u9700\u6c42\u4f86\u6c7a\u5b9a\u3002\u4f8b\u5982\uff0c\u9663\u5217\u548c\u93c8\u7d50\u4e32\u5217\u90fd\u53ef\u4ee5\u5be6\u73fe\u201c\u5806\u758a\u201d\u8cc7\u6599\u7d50\u69cb\uff08\u4e0b\u4e00\u7ae0\u6703\u8a73\u7d30\u4ecb\u7d39\uff09\uff0c\u4f46\u5b83\u5011\u9069\u7528\u65bc\u4e0d\u540c\u5834\u666f\u3002</p> <ul> <li>\u5728\u505a\u6f14\u7b97\u6cd5\u984c\u6642\uff0c\u6211\u5011\u6703\u50be\u5411\u65bc\u9078\u64c7\u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a\uff0c\u56e0\u70ba\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u64cd\u4f5c\u6548\u7387\u548c\u96a8\u6a5f\u8a2a\u554f\u7684\u80fd\u529b\uff0c\u4ee3\u50f9\u50c5\u662f\u9700\u8981\u9810\u5148\u70ba\u9663\u5217\u5206\u914d\u4e00\u5b9a\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u3002</li> <li>\u5982\u679c\u8cc7\u6599\u91cf\u975e\u5e38\u5927\u3001\u52d5\u614b\u6027\u5f88\u9ad8\u3001\u5806\u758a\u7684\u9810\u671f\u5927\u5c0f\u96e3\u4ee5\u4f30\u8a08\uff0c\u90a3\u9ebc\u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a\u66f4\u52a0\u5408\u9069\u3002\u93c8\u7d50\u4e32\u5217\u80fd\u5920\u5c07\u5927\u91cf\u8cc7\u6599\u5206\u6563\u5132\u5b58\u65bc\u8a18\u61b6\u9ad4\u7684\u4e0d\u540c\u90e8\u5206\uff0c\u4e26\u4e14\u907f\u514d\u4e86\u9663\u5217\u64f4\u5bb9\u7522\u751f\u7684\u984d\u5916\u958b\u92b7\u3002</li> </ul>"},{"location":"chapter_array_and_linkedlist/summary/","title":"4.5 \u00a0 \u5c0f\u7d50","text":""},{"location":"chapter_array_and_linkedlist/summary/#1","title":"1. \u00a0 \u91cd\u9ede\u56de\u9867","text":"<ul> <li>\u9663\u5217\u548c\u93c8\u7d50\u4e32\u5217\u662f\u5169\u7a2e\u57fa\u672c\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u5206\u5225\u4ee3\u8868\u8cc7\u6599\u5728\u8a08\u7b97\u6a5f\u8a18\u61b6\u9ad4\u4e2d\u7684\u5169\u7a2e\u5132\u5b58\u65b9\u5f0f\uff1a\u9023\u7e8c\u7a7a\u9593\u5132\u5b58\u548c\u5206\u6563\u7a7a\u9593\u5132\u5b58\u3002\u5169\u8005\u7684\u7279\u9ede\u5448\u73fe\u51fa\u4e92\u88dc\u7684\u7279\u6027\u3002</li> <li>\u9663\u5217\u652f\u6301\u96a8\u6a5f\u8a2a\u554f\u3001\u4f54\u7528\u8a18\u61b6\u9ad4\u8f03\u5c11\uff1b\u4f46\u63d2\u5165\u548c\u522a\u9664\u5143\u7d20\u6548\u7387\u4f4e\uff0c\u4e14\u521d\u59cb\u5316\u5f8c\u9577\u5ea6\u4e0d\u53ef\u8b8a\u3002</li> <li>\u93c8\u7d50\u4e32\u5217\u900f\u904e\u66f4\u6539\u5f15\u7528\uff08\u6307\u6a19\uff09\u5be6\u73fe\u9ad8\u6548\u7684\u7bc0\u9ede\u63d2\u5165\u8207\u522a\u9664\uff0c\u4e14\u53ef\u4ee5\u9748\u6d3b\u8abf\u6574\u9577\u5ea6\uff1b\u4f46\u7bc0\u9ede\u8a2a\u554f\u6548\u7387\u4f4e\u3001\u4f54\u7528\u8a18\u61b6\u9ad4\u8f03\u591a\u3002\u5e38\u898b\u7684\u93c8\u7d50\u4e32\u5217\u578b\u5225\u5305\u62ec\u55ae\u5411\u93c8\u7d50\u4e32\u5217\u3001\u74b0\u5f62\u93c8\u7d50\u4e32\u5217\u3001\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u3002</li> <li>\u4e32\u5217\u662f\u4e00\u7a2e\u652f\u6301\u589e\u522a\u67e5\u6539\u7684\u5143\u7d20\u6709\u5e8f\u96c6\u5408\uff0c\u901a\u5e38\u57fa\u65bc\u52d5\u614b\u9663\u5217\u5be6\u73fe\u3002\u5b83\u4fdd\u7559\u4e86\u9663\u5217\u7684\u512a\u52e2\uff0c\u540c\u6642\u53ef\u4ee5\u9748\u6d3b\u8abf\u6574\u9577\u5ea6\u3002</li> <li>\u4e32\u5217\u7684\u51fa\u73fe\u5927\u5e45\u63d0\u9ad8\u4e86\u9663\u5217\u7684\u5be6\u7528\u6027\uff0c\u4f46\u53ef\u80fd\u5c0e\u81f4\u90e8\u5206\u8a18\u61b6\u9ad4\u7a7a\u9593\u6d6a\u8cbb\u3002</li> <li>\u7a0b\u5f0f\u57f7\u884c\u6642\uff0c\u8cc7\u6599\u4e3b\u8981\u5132\u5b58\u5728\u8a18\u61b6\u9ad4\u4e2d\u3002\u9663\u5217\u53ef\u63d0\u4f9b\u66f4\u9ad8\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u6548\u7387\uff0c\u800c\u93c8\u7d50\u4e32\u5217\u5247\u5728\u8a18\u61b6\u9ad4\u4f7f\u7528\u4e0a\u66f4\u52a0\u9748\u6d3b\u3002</li> <li>\u5feb\u53d6\u900f\u904e\u5feb\u53d6\u884c\u3001\u9810\u53d6\u6a5f\u5236\u4ee5\u53ca\u7a7a\u9593\u5340\u57df\u6027\u548c\u6642\u9593\u5340\u57df\u6027\u7b49\u8cc7\u6599\u8f09\u5165\u6a5f\u5236\uff0c\u70ba CPU \u63d0\u4f9b\u5feb\u901f\u8cc7\u6599\u8a2a\u554f\uff0c\u986f\u8457\u63d0\u5347\u7a0b\u5f0f\u7684\u57f7\u884c\u6548\u7387\u3002</li> <li>\u7531\u65bc\u9663\u5217\u5177\u6709\u66f4\u9ad8\u7684\u5feb\u53d6\u547d\u4e2d\u7387\uff0c\u56e0\u6b64\u5b83\u901a\u5e38\u6bd4\u93c8\u7d50\u4e32\u5217\u66f4\u9ad8\u6548\u3002\u5728\u9078\u64c7\u8cc7\u6599\u7d50\u69cb\u6642\uff0c\u61c9\u6839\u64da\u5177\u9ad4\u9700\u6c42\u548c\u5834\u666f\u505a\u51fa\u6070\u7576\u9078\u64c7\u3002</li> </ul>"},{"location":"chapter_array_and_linkedlist/summary/#2-q-a","title":"2. \u00a0 Q &amp; A","text":"<p>Q\uff1a\u9663\u5217\u5132\u5b58\u5728\u5806\u758a\u4e0a\u548c\u5132\u5b58\u5728\u5806\u7a4d\u4e0a\uff0c\u5c0d\u6642\u9593\u6548\u7387\u548c\u7a7a\u9593\u6548\u7387\u662f\u5426\u6709\u5f71\u97ff\uff1f</p> <p>\u5132\u5b58\u5728\u5806\u758a\u4e0a\u548c\u5806\u7a4d\u4e0a\u7684\u9663\u5217\u90fd\u88ab\u5132\u5b58\u5728\u9023\u7e8c\u8a18\u61b6\u9ad4\u7a7a\u9593\u5167\uff0c\u8cc7\u6599\u64cd\u4f5c\u6548\u7387\u57fa\u672c\u4e00\u81f4\u3002\u7136\u800c\uff0c\u5806\u758a\u548c\u5806\u7a4d\u5177\u6709\u5404\u81ea\u7684\u7279\u9ede\uff0c\u5f9e\u800c\u5c0e\u81f4\u4ee5\u4e0b\u4e0d\u540c\u9ede\u3002</p> <ol> <li>\u5206\u914d\u548c\u91cb\u653e\u6548\u7387\uff1a\u5806\u758a\u662f\u4e00\u584a\u8f03\u5c0f\u7684\u8a18\u61b6\u9ad4\uff0c\u5206\u914d\u7531\u7de8\u8b6f\u5668\u81ea\u52d5\u5b8c\u6210\uff1b\u800c\u5806\u7a4d\u8a18\u61b6\u9ad4\u76f8\u5c0d\u66f4\u5927\uff0c\u53ef\u4ee5\u5728\u7a0b\u5f0f\u78bc\u4e2d\u52d5\u614b\u5206\u914d\uff0c\u66f4\u5bb9\u6613\u788e\u7247\u5316\u3002\u56e0\u6b64\uff0c\u5806\u7a4d\u4e0a\u7684\u5206\u914d\u548c\u91cb\u653e\u64cd\u4f5c\u901a\u5e38\u6bd4\u5806\u758a\u4e0a\u7684\u6162\u3002</li> <li>\u5927\u5c0f\u9650\u5236\uff1a\u5806\u758a\u8a18\u61b6\u9ad4\u76f8\u5c0d\u8f03\u5c0f\uff0c\u5806\u7a4d\u7684\u5927\u5c0f\u4e00\u822c\u53d7\u9650\u65bc\u53ef\u7528\u8a18\u61b6\u9ad4\u3002\u56e0\u6b64\u5806\u7a4d\u66f4\u52a0\u9069\u5408\u5132\u5b58\u5927\u578b\u9663\u5217\u3002</li> <li>\u9748\u6d3b\u6027\uff1a\u5806\u758a\u4e0a\u7684\u9663\u5217\u7684\u5927\u5c0f\u9700\u8981\u5728\u7de8\u8b6f\u6642\u78ba\u5b9a\uff0c\u800c\u5806\u7a4d\u4e0a\u7684\u9663\u5217\u7684\u5927\u5c0f\u53ef\u4ee5\u5728\u57f7\u884c\u6642\u52d5\u614b\u78ba\u5b9a\u3002</li> </ol> <p>Q\uff1a\u70ba\u4ec0\u9ebc\u9663\u5217\u8981\u6c42\u76f8\u540c\u578b\u5225\u7684\u5143\u7d20\uff0c\u800c\u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u537b\u6c92\u6709\u5f37\u8abf\u76f8\u540c\u578b\u5225\u5462\uff1f</p> <p>\u93c8\u7d50\u4e32\u5217\u7531\u7bc0\u9ede\u7d44\u6210\uff0c\u7bc0\u9ede\u4e4b\u9593\u900f\u904e\u5f15\u7528\uff08\u6307\u6a19\uff09\u9023\u7dda\uff0c\u5404\u500b\u7bc0\u9ede\u53ef\u4ee5\u5132\u5b58\u4e0d\u540c\u578b\u5225\u7684\u8cc7\u6599\uff0c\u4f8b\u5982 <code>int</code>\u3001<code>double</code>\u3001<code>string</code>\u3001<code>object</code> \u7b49\u3002</p> <p>\u76f8\u5c0d\u5730\uff0c\u9663\u5217\u5143\u7d20\u5247\u5fc5\u9808\u662f\u76f8\u540c\u578b\u5225\u7684\uff0c\u9019\u6a23\u624d\u80fd\u900f\u904e\u8a08\u7b97\u504f\u79fb\u91cf\u4f86\u7372\u53d6\u5c0d\u61c9\u5143\u7d20\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u9663\u5217\u540c\u6642\u5305\u542b <code>int</code> \u548c <code>long</code> \u5169\u7a2e\u578b\u5225\uff0c\u55ae\u500b\u5143\u7d20\u5206\u5225\u4f54\u7528 4 \u4f4d\u5143\u7d44 \u548c 8 \u4f4d\u5143\u7d44 \uff0c\u6b64\u6642\u5c31\u4e0d\u80fd\u7528\u4ee5\u4e0b\u516c\u5f0f\u8a08\u7b97\u504f\u79fb\u91cf\u4e86\uff0c\u56e0\u70ba\u9663\u5217\u4e2d\u5305\u542b\u4e86\u5169\u7a2e\u201c\u5143\u7d20\u9577\u5ea6\u201d\u3002</p> <pre><code># \u5143\u7d20\u8a18\u61b6\u9ad4\u4f4d\u5740 = \u9663\u5217\u8a18\u61b6\u9ad4\u4f4d\u5740\uff08\u9996\u5143\u7d20\u8a18\u61b6\u9ad4\u4f4d\u5740\uff09 + \u5143\u7d20\u9577\u5ea6 * \u5143\u7d20\u7d22\u5f15\n</code></pre> <p>Q\uff1a\u522a\u9664\u7bc0\u9ede <code>P</code> \u5f8c\uff0c\u662f\u5426\u9700\u8981\u628a <code>P.next</code> \u8a2d\u70ba <code>None</code> \u5462\uff1f</p> <p>\u4e0d\u4fee\u6539 <code>P.next</code> \u4e5f\u53ef\u4ee5\u3002\u5f9e\u8a72\u93c8\u7d50\u4e32\u5217\u7684\u89d2\u5ea6\u770b\uff0c\u5f9e\u982d\u7bc0\u9ede\u8d70\u8a2a\u5230\u5c3e\u7bc0\u9ede\u5df2\u7d93\u4e0d\u6703\u9047\u5230 <code>P</code> \u4e86\u3002\u9019\u610f\u5473\u8457\u7bc0\u9ede <code>P</code> \u5df2\u7d93\u5f9e\u93c8\u7d50\u4e32\u5217\u4e2d\u522a\u9664\u4e86\uff0c\u6b64\u6642\u7bc0\u9ede <code>P</code> \u6307\u5411\u54ea\u88e1\u90fd\u4e0d\u6703\u5c0d\u8a72\u93c8\u7d50\u4e32\u5217\u7522\u751f\u5f71\u97ff\u3002</p> <p>\u5f9e\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\uff08\u505a\u984c\uff09\u7684\u89d2\u5ea6\u770b\uff0c\u4e0d\u65b7\u958b\u6c92\u6709\u95dc\u4fc2\uff0c\u53ea\u8981\u4fdd\u8b49\u7a0b\u5f0f\u7684\u908f\u8f2f\u662f\u6b63\u78ba\u7684\u5c31\u884c\u3002\u5f9e\u6a19\u6e96\u5eab\u7684\u89d2\u5ea6\u770b\uff0c\u65b7\u958b\u66f4\u52a0\u5b89\u5168\u3001\u908f\u8f2f\u66f4\u52a0\u6e05\u6670\u3002\u5982\u679c\u4e0d\u65b7\u958b\uff0c\u5047\u8a2d\u88ab\u522a\u9664\u7bc0\u9ede\u672a\u88ab\u6b63\u5e38\u56de\u6536\uff0c\u90a3\u9ebc\u5b83\u6703\u5f71\u97ff\u5f8c\u7e7c\u7bc0\u9ede\u7684\u8a18\u61b6\u9ad4\u56de\u6536\u3002</p> <p>Q\uff1a\u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u63d2\u5165\u548c\u522a\u9664\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(O(1)\\) \u3002\u4f46\u662f\u589e\u522a\u4e4b\u524d\u90fd\u9700\u8981 \\(O(n)\\) \u7684\u6642\u9593\u67e5\u8a62\u5143\u7d20\uff0c\u90a3\u70ba\u4ec0\u9ebc\u6642\u9593\u8907\u96dc\u5ea6\u4e0d\u662f \\(O(n)\\) \u5462\uff1f</p> <p>\u5982\u679c\u662f\u5148\u67e5\u8a62\u5143\u7d20\u3001\u518d\u522a\u9664\u5143\u7d20\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u78ba\u5be6\u662f \\(O(n)\\) \u3002\u7136\u800c\uff0c\u93c8\u7d50\u4e32\u5217\u7684 \\(O(1)\\) \u589e\u522a\u7684\u512a\u52e2\u53ef\u4ee5\u5728\u5176\u4ed6\u61c9\u7528\u4e0a\u5f97\u5230\u9ad4\u73fe\u3002\u4f8b\u5982\uff0c\u96d9\u5411\u4f47\u5217\u9069\u5408\u4f7f\u7528\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\uff0c\u6211\u5011\u7dad\u8b77\u4e00\u500b\u6307\u6a19\u8b8a\u6578\u59cb\u7d42\u6307\u5411\u982d\u7bc0\u9ede\u3001\u5c3e\u7bc0\u9ede\uff0c\u6bcf\u6b21\u63d2\u5165\u8207\u522a\u9664\u64cd\u4f5c\u90fd\u662f \\(O(1)\\) \u3002</p> <p>Q\uff1a\u5716\u201c\u93c8\u7d50\u4e32\u5217\u5b9a\u7fa9\u8207\u5132\u5b58\u65b9\u5f0f\u201d\u4e2d\uff0c\u6dfa\u85cd\u8272\u7684\u5132\u5b58\u7bc0\u9ede\u6307\u6a19\u662f\u4f54\u7528\u4e00\u584a\u8a18\u61b6\u9ad4\u4f4d\u5740\u55ce\uff1f\u9084\u662f\u548c\u7bc0\u9ede\u503c\u5404\u4f54\u4e00\u534a\u5462\uff1f</p> <p>\u8a72\u793a\u610f\u5716\u53ea\u662f\u5b9a\u6027\u8868\u793a\uff0c\u5b9a\u91cf\u8868\u793a\u9700\u8981\u6839\u64da\u5177\u9ad4\u60c5\u6cc1\u9032\u884c\u5206\u6790\u3002</p> <ul> <li>\u4e0d\u540c\u578b\u5225\u7684\u7bc0\u9ede\u503c\u4f54\u7528\u7684\u7a7a\u9593\u662f\u4e0d\u540c\u7684\uff0c\u6bd4\u5982 <code>int</code>\u3001<code>long</code>\u3001<code>double</code> \u548c\u4f8b\u9805\u7269\u4ef6\u7b49\u3002</li> <li>\u6307\u6a19\u8b8a\u6578\u4f54\u7528\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u5927\u5c0f\u6839\u64da\u6240\u4f7f\u7528\u7684\u4f5c\u696d\u7cfb\u7d71\u53ca\u7de8\u8b6f\u74b0\u5883\u800c\u5b9a\uff0c\u5927\u591a\u70ba 8 \u4f4d\u5143\u7d44\u6216 4 \u4f4d\u5143\u7d44\u3002</li> </ul> <p>Q\uff1a\u5728\u4e32\u5217\u672b\u5c3e\u65b0\u589e\u5143\u7d20\u662f\u5426\u6642\u6642\u523b\u523b\u90fd\u70ba \\(O(1)\\) \uff1f</p> <p>\u5982\u679c\u65b0\u589e\u5143\u7d20\u6642\u8d85\u51fa\u4e32\u5217\u9577\u5ea6\uff0c\u5247\u9700\u8981\u5148\u64f4\u5bb9\u4e32\u5217\u518d\u65b0\u589e\u3002\u7cfb\u7d71\u6703\u7533\u8acb\u4e00\u584a\u65b0\u7684\u8a18\u61b6\u9ad4\uff0c\u4e26\u5c07\u539f\u4e32\u5217\u7684\u6240\u6709\u5143\u7d20\u642c\u904b\u904e\u53bb\uff0c\u9019\u6642\u5019\u6642\u9593\u8907\u96dc\u5ea6\u5c31\u6703\u662f \\(O(n)\\) \u3002</p> <p>Q\uff1a\u201c\u4e32\u5217\u7684\u51fa\u73fe\u6975\u5927\u5730\u63d0\u9ad8\u4e86\u9663\u5217\u7684\u5be6\u7528\u6027\uff0c\u4f46\u53ef\u80fd\u5c0e\u81f4\u90e8\u5206\u8a18\u61b6\u9ad4\u7a7a\u9593\u6d6a\u8cbb\u201d\uff0c\u9019\u88e1\u7684\u7a7a\u9593\u6d6a\u8cbb\u662f\u6307\u984d\u5916\u589e\u52a0\u7684\u8b8a\u6578\u5982\u5bb9\u91cf\u3001\u9577\u5ea6\u3001\u64f4\u5bb9\u500d\u6578\u6240\u4f54\u7684\u8a18\u61b6\u9ad4\u55ce\uff1f</p> <p>\u9019\u88e1\u7684\u7a7a\u9593\u6d6a\u8cbb\u4e3b\u8981\u6709\u5169\u65b9\u9762\u542b\u7fa9\uff1a\u4e00\u65b9\u9762\uff0c\u4e32\u5217\u90fd\u6703\u8a2d\u5b9a\u4e00\u500b\u521d\u59cb\u9577\u5ea6\uff0c\u6211\u5011\u4e0d\u4e00\u5b9a\u9700\u8981\u7528\u9019\u9ebc\u591a\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u70ba\u4e86\u9632\u6b62\u983b\u7e41\u64f4\u5bb9\uff0c\u64f4\u5bb9\u4e00\u822c\u6703\u4e58\u4ee5\u4e00\u500b\u4fc2\u6578\uff0c\u6bd4\u5982 \\(\\times 1.5\\) \u3002\u9019\u6a23\u4e00\u4f86\uff0c\u4e5f\u6703\u51fa\u73fe\u5f88\u591a\u7a7a\u4f4d\uff0c\u6211\u5011\u901a\u5e38\u4e0d\u80fd\u5b8c\u5168\u586b\u6eff\u5b83\u5011\u3002</p> <p>Q\uff1a\u5728 Python \u4e2d\u521d\u59cb\u5316 <code>n = [1, 2, 3]</code> \u5f8c\uff0c\u9019 3 \u500b\u5143\u7d20\u7684\u4f4d\u5740\u662f\u76f8\u9023\u7684\uff0c\u4f46\u662f\u521d\u59cb\u5316 <code>m = [2, 1, 3]</code> \u6703\u767c\u73fe\u5b83\u5011\u6bcf\u500b\u5143\u7d20\u7684 id \u4e26\u4e0d\u662f\u9023\u7e8c\u7684\uff0c\u800c\u662f\u5206\u5225\u8ddf <code>n</code> \u4e2d\u7684\u76f8\u540c\u3002\u9019\u4e9b\u5143\u7d20\u7684\u4f4d\u5740\u4e0d\u9023\u7e8c\uff0c\u90a3\u9ebc <code>m</code> \u9084\u662f\u9663\u5217\u55ce\uff1f</p> <p>\u5047\u5982\u628a\u4e32\u5217\u5143\u7d20\u63db\u6210\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede <code>n = [n1, n2, n3, n4, n5]</code> \uff0c\u901a\u5e38\u60c5\u6cc1\u4e0b\u9019 5 \u500b\u7bc0\u9ede\u7269\u4ef6\u4e5f\u5206\u6563\u5132\u5b58\u5728\u8a18\u61b6\u9ad4\u5404\u8655\u3002\u7136\u800c\uff0c\u7d66\u5b9a\u4e00\u500b\u4e32\u5217\u7d22\u5f15\uff0c\u6211\u5011\u4ecd\u7136\u53ef\u4ee5\u5728 \\(O(1)\\) \u6642\u9593\u5167\u7372\u53d6\u7bc0\u9ede\u8a18\u61b6\u9ad4\u4f4d\u5740\uff0c\u5f9e\u800c\u8a2a\u554f\u5230\u5c0d\u61c9\u7684\u7bc0\u9ede\u3002\u9019\u662f\u56e0\u70ba\u9663\u5217\u4e2d\u5132\u5b58\u7684\u662f\u7bc0\u9ede\u7684\u5f15\u7528\uff0c\u800c\u975e\u7bc0\u9ede\u672c\u8eab\u3002</p> <p>\u8207\u8a31\u591a\u8a9e\u8a00\u4e0d\u540c\uff0cPython \u4e2d\u7684\u6578\u5b57\u4e5f\u88ab\u5305\u88dd\u70ba\u7269\u4ef6\uff0c\u4e32\u5217\u4e2d\u5132\u5b58\u7684\u4e0d\u662f\u6578\u5b57\u672c\u8eab\uff0c\u800c\u662f\u5c0d\u6578\u5b57\u7684\u5f15\u7528\u3002\u56e0\u6b64\uff0c\u6211\u5011\u6703\u767c\u73fe\u5169\u500b\u9663\u5217\u4e2d\u7684\u76f8\u540c\u6578\u5b57\u64c1\u6709\u540c\u4e00\u500b id \uff0c\u4e26\u4e14\u9019\u4e9b\u6578\u5b57\u7684\u8a18\u61b6\u9ad4\u4f4d\u5740\u7121\u9808\u9023\u7e8c\u3002</p> <p>Q\uff1aC++ STL \u88e1\u9762\u7684 <code>std::list</code> \u5df2\u7d93\u5be6\u73fe\u4e86\u96d9\u5411\u93c8\u7d50\u4e32\u5217\uff0c\u4f46\u597d\u50cf\u4e00\u4e9b\u6f14\u7b97\u6cd5\u66f8\u4e0a\u4e0d\u600e\u9ebc\u76f4\u63a5\u4f7f\u7528\u5b83\uff0c\u662f\u4e0d\u662f\u56e0\u70ba\u6709\u4ec0\u9ebc\u4fb7\u9650\u6027\u5462\uff1f</p> <p>\u4e00\u65b9\u9762\uff0c\u6211\u5011\u5f80\u5f80\u66f4\u9752\u775e\u4f7f\u7528\u9663\u5217\u5be6\u73fe\u6f14\u7b97\u6cd5\uff0c\u800c\u53ea\u5728\u5fc5\u8981\u6642\u624d\u4f7f\u7528\u93c8\u7d50\u4e32\u5217\uff0c\u4e3b\u8981\u6709\u5169\u500b\u539f\u56e0\u3002</p> <ul> <li>\u7a7a\u9593\u958b\u92b7\uff1a\u7531\u65bc\u6bcf\u500b\u5143\u7d20\u9700\u8981\u5169\u500b\u984d\u5916\u7684\u6307\u6a19\uff08\u4e00\u500b\u7528\u65bc\u524d\u4e00\u500b\u5143\u7d20\uff0c\u4e00\u500b\u7528\u65bc\u5f8c\u4e00\u500b\u5143\u7d20\uff09\uff0c\u6240\u4ee5 <code>std::list</code> \u901a\u5e38\u6bd4 <code>std::vector</code> \u66f4\u4f54\u7528\u7a7a\u9593\u3002</li> <li>\u5feb\u53d6\u4e0d\u53cb\u597d\uff1a\u7531\u65bc\u8cc7\u6599\u4e0d\u662f\u9023\u7e8c\u5b58\u653e\u7684\uff0c\u56e0\u6b64 <code>std::list</code> \u5c0d\u5feb\u53d6\u7684\u5229\u7528\u7387\u8f03\u4f4e\u3002\u4e00\u822c\u60c5\u6cc1\u4e0b\uff0c<code>std::vector</code> \u7684\u6548\u80fd\u6703\u66f4\u597d\u3002</li> </ul> <p>\u53e6\u4e00\u65b9\u9762\uff0c\u5fc5\u8981\u4f7f\u7528\u93c8\u7d50\u4e32\u5217\u7684\u60c5\u6cc1\u4e3b\u8981\u662f\u4e8c\u5143\u6a39\u548c\u5716\u3002\u5806\u758a\u548c\u4f47\u5217\u5f80\u5f80\u6703\u4f7f\u7528\u7a0b\u5f0f\u8a9e\u8a00\u63d0\u4f9b\u7684 <code>stack</code> \u548c <code>queue</code> \uff0c\u800c\u975e\u93c8\u7d50\u4e32\u5217\u3002</p> <p>Q\uff1a\u521d\u59cb\u5316\u4e32\u5217 <code>res = [0] * self.size()</code> \u64cd\u4f5c\uff0c\u6703\u5c0e\u81f4 <code>res</code> \u7684\u6bcf\u500b\u5143\u7d20\u5f15\u7528\u76f8\u540c\u7684\u4f4d\u5740\u55ce\uff1f</p> <p>\u4e0d\u6703\u3002\u4f46\u4e8c\u7dad\u9663\u5217\u6703\u6709\u9019\u500b\u554f\u984c\uff0c\u4f8b\u5982\u521d\u59cb\u5316\u4e8c\u7dad\u4e32\u5217 <code>res = [[0] * self.size()]</code> \uff0c\u5247\u591a\u6b21\u5f15\u7528\u4e86\u540c\u4e00\u500b\u4e32\u5217 <code>[0]</code> \u3002</p>"},{"location":"chapter_backtracking/","title":"\u7b2c 13 \u7ae0 \u00a0 \u56de\u6eaf","text":"<p>Abstract</p> <p>\u6211\u5011\u5982\u540c\u8ff7\u5bae\u4e2d\u7684\u63a2\u7d22\u8005\uff0c\u5728\u524d\u9032\u7684\u9053\u8def\u4e0a\u53ef\u80fd\u6703\u9047\u5230\u56f0\u96e3\u3002</p> <p>\u56de\u6eaf\u7684\u529b\u91cf\u8b93\u6211\u5011\u80fd\u5920\u91cd\u65b0\u958b\u59cb\uff0c\u4e0d\u65b7\u5617\u8a66\uff0c\u6700\u7d42\u627e\u5230\u901a\u5f80\u5149\u660e\u7684\u51fa\u53e3\u3002</p>"},{"location":"chapter_backtracking/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>13.1 \u00a0 \u56de\u6eaf\u6f14\u7b97\u6cd5</li> <li>13.2 \u00a0 \u5168\u6392\u5217\u554f\u984c</li> <li>13.3 \u00a0 \u5b50\u96c6\u548c\u554f\u984c</li> <li>13.4 \u00a0 N \u7687\u540e\u554f\u984c</li> <li>13.5 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_backtracking/backtracking_algorithm/","title":"13.1 \u00a0 \u56de\u6eaf\u6f14\u7b97\u6cd5","text":"<p>\u56de\u6eaf\u6f14\u7b97\u6cd5\uff08backtracking algorithm\uff09\u662f\u4e00\u7a2e\u900f\u904e\u7aae\u8209\u4f86\u89e3\u6c7a\u554f\u984c\u7684\u65b9\u6cd5\uff0c\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u662f\u5f9e\u4e00\u500b\u521d\u59cb\u72c0\u614b\u51fa\u767c\uff0c\u66b4\u529b\u641c\u5c0b\u6240\u6709\u53ef\u80fd\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u7576\u9047\u5230\u6b63\u78ba\u7684\u89e3\u5247\u5c07\u5176\u8a18\u9304\uff0c\u76f4\u5230\u627e\u5230\u89e3\u6216\u8005\u5617\u8a66\u4e86\u6240\u6709\u53ef\u80fd\u7684\u9078\u64c7\u90fd\u7121\u6cd5\u627e\u5230\u89e3\u70ba\u6b62\u3002</p> <p>\u56de\u6eaf\u6f14\u7b97\u6cd5\u901a\u5e38\u63a1\u7528\u201c\u6df1\u5ea6\u512a\u5148\u641c\u5c0b\u201d\u4f86\u8d70\u8a2a\u89e3\u7a7a\u9593\u3002\u5728\u201c\u4e8c\u5143\u6a39\u201d\u7ae0\u7bc0\u4e2d\uff0c\u6211\u5011\u63d0\u5230\u524d\u5e8f\u3001\u4e2d\u5e8f\u548c\u5f8c\u5e8f\u8d70\u8a2a\u90fd\u5c6c\u65bc\u6df1\u5ea6\u512a\u5148\u641c\u5c0b\u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5229\u7528\u524d\u5e8f\u8d70\u8a2a\u69cb\u9020\u4e00\u500b\u56de\u6eaf\u554f\u984c\uff0c\u9010\u6b65\u77ad\u89e3\u56de\u6eaf\u6f14\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002</p> <p>\u4f8b\u984c\u4e00</p> <p>\u7d66\u5b9a\u4e00\u68f5\u4e8c\u5143\u6a39\uff0c\u641c\u5c0b\u4e26\u8a18\u9304\u6240\u6709\u503c\u70ba \\(7\\) \u7684\u7bc0\u9ede\uff0c\u8acb\u8fd4\u56de\u7bc0\u9ede\u4e32\u5217\u3002</p> <p>\u5c0d\u65bc\u6b64\u984c\uff0c\u6211\u5011\u524d\u5e8f\u8d70\u8a2a\u9019\u68f5\u6a39\uff0c\u4e26\u5224\u65b7\u7576\u524d\u7bc0\u9ede\u7684\u503c\u662f\u5426\u70ba \\(7\\) \uff0c\u82e5\u662f\uff0c\u5247\u5c07\u8a72\u7bc0\u9ede\u7684\u503c\u52a0\u5165\u7d50\u679c\u4e32\u5217 <code>res</code> \u4e4b\u4e2d\u3002\u76f8\u95dc\u904e\u7a0b\u5be6\u73fe\u5982\u5716 13-1 \u548c\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig preorder_traversal_i_compact.py<pre><code>def pre_order(root: TreeNode):\n \"\"\"\u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e00\"\"\"\n if root is None:\n return\n if root.val == 7:\n # \u8a18\u9304\u89e3\n res.append(root)\n pre_order(root.left)\n pre_order(root.right)\n</code></pre> preorder_traversal_i_compact.cpp<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e00 */\nvoid preOrder(TreeNode *root) {\n if (root == nullptr) {\n return;\n }\n if (root-&gt;val == 7) {\n // \u8a18\u9304\u89e3\n res.push_back(root);\n }\n preOrder(root-&gt;left);\n preOrder(root-&gt;right);\n}\n</code></pre> preorder_traversal_i_compact.java<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e00 */\nvoid preOrder(TreeNode root) {\n if (root == null) {\n return;\n }\n if (root.val == 7) {\n // \u8a18\u9304\u89e3\n res.add(root);\n }\n preOrder(root.left);\n preOrder(root.right);\n}\n</code></pre> preorder_traversal_i_compact.cs<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e00 */\nvoid PreOrder(TreeNode? root) {\n if (root == null) {\n return;\n }\n if (root.val == 7) {\n // \u8a18\u9304\u89e3\n res.Add(root);\n }\n PreOrder(root.left);\n PreOrder(root.right);\n}\n</code></pre> preorder_traversal_i_compact.go<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e00 */\nfunc preOrderI(root *TreeNode, res *[]*TreeNode) {\n if root == nil {\n return\n }\n if (root.Val).(int) == 7 {\n // \u8a18\u9304\u89e3\n *res = append(*res, root)\n }\n preOrderI(root.Left, res)\n preOrderI(root.Right, res)\n}\n</code></pre> preorder_traversal_i_compact.swift<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e00 */\nfunc preOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n if root.val == 7 {\n // \u8a18\u9304\u89e3\n res.append(root)\n }\n preOrder(root: root.left)\n preOrder(root: root.right)\n}\n</code></pre> preorder_traversal_i_compact.js<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e00 */\nfunction preOrder(root, res) {\n if (root === null) {\n return;\n }\n if (root.val === 7) {\n // \u8a18\u9304\u89e3\n res.push(root);\n }\n preOrder(root.left, res);\n preOrder(root.right, res);\n}\n</code></pre> preorder_traversal_i_compact.ts<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e00 */\nfunction preOrder(root: TreeNode | null, res: TreeNode[]): void {\n if (root === null) {\n return;\n }\n if (root.val === 7) {\n // \u8a18\u9304\u89e3\n res.push(root);\n }\n preOrder(root.left, res);\n preOrder(root.right, res);\n}\n</code></pre> preorder_traversal_i_compact.dart<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e00 */\nvoid preOrder(TreeNode? root, List&lt;TreeNode&gt; res) {\n if (root == null) {\n return;\n }\n if (root.val == 7) {\n // \u8a18\u9304\u89e3\n res.add(root);\n }\n preOrder(root.left, res);\n preOrder(root.right, res);\n}\n</code></pre> preorder_traversal_i_compact.rs<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e00 */\nfn pre_order(res: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, root: Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;) {\n if root.is_none() {\n return;\n }\n if let Some(node) = root {\n if node.borrow().val == 7 {\n // \u8a18\u9304\u89e3\n res.push(node.clone());\n }\n pre_order(res, node.borrow().left.clone());\n pre_order(res, node.borrow().right.clone());\n }\n}\n</code></pre> preorder_traversal_i_compact.c<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e00 */\nvoid preOrder(TreeNode *root) {\n if (root == NULL) {\n return;\n }\n if (root-&gt;val == 7) {\n // \u8a18\u9304\u89e3\n res[resSize++] = root;\n }\n preOrder(root-&gt;left);\n preOrder(root-&gt;right);\n}\n</code></pre> preorder_traversal_i_compact.kt<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e00 */\nfun preOrder(root: TreeNode?) {\n if (root == null) {\n return\n }\n if (root._val == 7) {\n // \u8a18\u9304\u89e3\n res!!.add(root)\n }\n preOrder(root.left)\n preOrder(root.right)\n}\n</code></pre> preorder_traversal_i_compact.rb<pre><code>[class]{}-[func]{pre_order}\n</code></pre> preorder_traversal_i_compact.zig<pre><code>[class]{}-[func]{preOrder}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p></p> <p> \u5716 13-1 \u00a0 \u5728\u524d\u5e8f\u8d70\u8a2a\u4e2d\u641c\u7d22\u7bc0\u9ede </p>"},{"location":"chapter_backtracking/backtracking_algorithm/#1311","title":"13.1.1 \u00a0 \u5617\u8a66\u8207\u56de\u9000","text":"<p>\u4e4b\u6240\u4ee5\u7a31\u4e4b\u70ba\u56de\u6eaf\u6f14\u7b97\u6cd5\uff0c\u662f\u56e0\u70ba\u8a72\u6f14\u7b97\u6cd5\u5728\u641c\u5c0b\u89e3\u7a7a\u9593\u6642\u6703\u63a1\u7528\u201c\u5617\u8a66\u201d\u8207\u201c\u56de\u9000\u201d\u7684\u7b56\u7565\u3002\u7576\u6f14\u7b97\u6cd5\u5728\u641c\u5c0b\u904e\u7a0b\u4e2d\u9047\u5230\u67d0\u500b\u72c0\u614b\u7121\u6cd5\u7e7c\u7e8c\u524d\u9032\u6216\u7121\u6cd5\u5f97\u5230\u6eff\u8db3\u689d\u4ef6\u7684\u89e3\u6642\uff0c\u5b83\u6703\u64a4\u92b7\u4e0a\u4e00\u6b65\u7684\u9078\u64c7\uff0c\u9000\u56de\u5230\u4e4b\u524d\u7684\u72c0\u614b\uff0c\u4e26\u5617\u8a66\u5176\u4ed6\u53ef\u80fd\u7684\u9078\u64c7\u3002</p> <p>\u5c0d\u65bc\u4f8b\u984c\u4e00\uff0c\u8a2a\u554f\u6bcf\u500b\u7bc0\u9ede\u90fd\u4ee3\u8868\u4e00\u6b21\u201c\u5617\u8a66\u201d\uff0c\u800c\u8d8a\u904e\u8449\u7bc0\u9ede\u6216\u8fd4\u56de\u7236\u7bc0\u9ede\u7684 <code>return</code> \u5247\u8868\u793a\u201c\u56de\u9000\u201d\u3002</p> <p>\u503c\u5f97\u8aaa\u660e\u7684\u662f\uff0c\u56de\u9000\u4e26\u4e0d\u50c5\u50c5\u5305\u62ec\u51fd\u5f0f\u8fd4\u56de\u3002\u70ba\u89e3\u91cb\u9019\u4e00\u9ede\uff0c\u6211\u5011\u5c0d\u4f8b\u984c\u4e00\u7a0d\u4f5c\u62d3\u5c55\u3002</p> <p>\u4f8b\u984c\u4e8c</p> <p>\u5728\u4e8c\u5143\u6a39\u4e2d\u641c\u7d22\u6240\u6709\u503c\u70ba \\(7\\) \u7684\u7bc0\u9ede\uff0c\u8acb\u8fd4\u56de\u6839\u7bc0\u9ede\u5230\u9019\u4e9b\u7bc0\u9ede\u7684\u8def\u5f91\u3002</p> <p>\u5728\u4f8b\u984c\u4e00\u7a0b\u5f0f\u78bc\u7684\u57fa\u790e\u4e0a\uff0c\u6211\u5011\u9700\u8981\u85c9\u52a9\u4e00\u500b\u4e32\u5217 <code>path</code> \u8a18\u9304\u8a2a\u554f\u904e\u7684\u7bc0\u9ede\u8def\u5f91\u3002\u7576\u8a2a\u554f\u5230\u503c\u70ba \\(7\\) \u7684\u7bc0\u9ede\u6642\uff0c\u5247\u8907\u88fd <code>path</code> \u4e26\u65b0\u589e\u9032\u7d50\u679c\u4e32\u5217 <code>res</code> \u3002\u8d70\u8a2a\u5b8c\u6210\u5f8c\uff0c<code>res</code> \u4e2d\u5132\u5b58\u7684\u5c31\u662f\u6240\u6709\u7684\u89e3\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig preorder_traversal_ii_compact.py<pre><code>def pre_order(root: TreeNode):\n \"\"\"\u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e8c\"\"\"\n if root is None:\n return\n # \u5617\u8a66\n path.append(root)\n if root.val == 7:\n # \u8a18\u9304\u89e3\n res.append(list(path))\n pre_order(root.left)\n pre_order(root.right)\n # \u56de\u9000\n path.pop()\n</code></pre> preorder_traversal_ii_compact.cpp<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e8c */\nvoid preOrder(TreeNode *root) {\n if (root == nullptr) {\n return;\n }\n // \u5617\u8a66\n path.push_back(root);\n if (root-&gt;val == 7) {\n // \u8a18\u9304\u89e3\n res.push_back(path);\n }\n preOrder(root-&gt;left);\n preOrder(root-&gt;right);\n // \u56de\u9000\n path.pop_back();\n}\n</code></pre> preorder_traversal_ii_compact.java<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e8c */\nvoid preOrder(TreeNode root) {\n if (root == null) {\n return;\n }\n // \u5617\u8a66\n path.add(root);\n if (root.val == 7) {\n // \u8a18\u9304\u89e3\n res.add(new ArrayList&lt;&gt;(path));\n }\n preOrder(root.left);\n preOrder(root.right);\n // \u56de\u9000\n path.remove(path.size() - 1);\n}\n</code></pre> preorder_traversal_ii_compact.cs<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e8c */\nvoid PreOrder(TreeNode? root) {\n if (root == null) {\n return;\n }\n // \u5617\u8a66\n path.Add(root);\n if (root.val == 7) {\n // \u8a18\u9304\u89e3\n res.Add(new List&lt;TreeNode&gt;(path));\n }\n PreOrder(root.left);\n PreOrder(root.right);\n // \u56de\u9000\n path.RemoveAt(path.Count - 1);\n}\n</code></pre> preorder_traversal_ii_compact.go<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e8c */\nfunc preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n if root == nil {\n return\n }\n // \u5617\u8a66\n *path = append(*path, root)\n if root.Val.(int) == 7 {\n // \u8a18\u9304\u89e3\n *res = append(*res, append([]*TreeNode{}, *path...))\n }\n preOrderII(root.Left, res, path)\n preOrderII(root.Right, res, path)\n // \u56de\u9000\n *path = (*path)[:len(*path)-1]\n}\n</code></pre> preorder_traversal_ii_compact.swift<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e8c */\nfunc preOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u5617\u8a66\n path.append(root)\n if root.val == 7 {\n // \u8a18\u9304\u89e3\n res.append(path)\n }\n preOrder(root: root.left)\n preOrder(root: root.right)\n // \u56de\u9000\n path.removeLast()\n}\n</code></pre> preorder_traversal_ii_compact.js<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e8c */\nfunction preOrder(root, path, res) {\n if (root === null) {\n return;\n }\n // \u5617\u8a66\n path.push(root);\n if (root.val === 7) {\n // \u8a18\u9304\u89e3\n res.push([...path]);\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.pop();\n}\n</code></pre> preorder_traversal_ii_compact.ts<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e8c */\nfunction preOrder(\n root: TreeNode | null,\n path: TreeNode[],\n res: TreeNode[][]\n): void {\n if (root === null) {\n return;\n }\n // \u5617\u8a66\n path.push(root);\n if (root.val === 7) {\n // \u8a18\u9304\u89e3\n res.push([...path]);\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.pop();\n}\n</code></pre> preorder_traversal_ii_compact.dart<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e8c */\nvoid preOrder(\n TreeNode? root,\n List&lt;TreeNode&gt; path,\n List&lt;List&lt;TreeNode&gt;&gt; res,\n) {\n if (root == null) {\n return;\n }\n\n // \u5617\u8a66\n path.add(root);\n if (root.val == 7) {\n // \u8a18\u9304\u89e3\n res.add(List.from(path));\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.removeLast();\n}\n</code></pre> preorder_traversal_ii_compact.rs<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e8c */\nfn pre_order(\n res: &amp;mut Vec&lt;Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;&gt;,\n path: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;,\n root: Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;,\n) {\n if root.is_none() {\n return;\n }\n if let Some(node) = root {\n // \u5617\u8a66\n path.push(node.clone());\n if node.borrow().val == 7 {\n // \u8a18\u9304\u89e3\n res.push(path.clone());\n }\n pre_order(res, path, node.borrow().left.clone());\n pre_order(res, path, node.borrow().right.clone());\n // \u56de\u9000\n path.remove(path.len() - 1);\n }\n}\n</code></pre> preorder_traversal_ii_compact.c<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e8c */\nvoid preOrder(TreeNode *root) {\n if (root == NULL) {\n return;\n }\n // \u5617\u8a66\n path[pathSize++] = root;\n if (root-&gt;val == 7) {\n // \u8a18\u9304\u89e3\n for (int i = 0; i &lt; pathSize; ++i) {\n res[resSize][i] = path[i];\n }\n resSize++;\n }\n preOrder(root-&gt;left);\n preOrder(root-&gt;right);\n // \u56de\u9000\n pathSize--;\n}\n</code></pre> preorder_traversal_ii_compact.kt<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e8c */\nfun preOrder(root: TreeNode?) {\n if (root == null) {\n return\n }\n // \u5617\u8a66\n path!!.add(root)\n if (root._val == 7) {\n // \u8a18\u9304\u89e3\n res!!.add(path!!.toMutableList())\n }\n preOrder(root.left)\n preOrder(root.right)\n // \u56de\u9000\n path!!.removeAt(path!!.size - 1)\n}\n</code></pre> preorder_traversal_ii_compact.rb<pre><code>[class]{}-[func]{pre_order}\n</code></pre> preorder_traversal_ii_compact.zig<pre><code>[class]{}-[func]{preOrder}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5728\u6bcf\u6b21\u201c\u5617\u8a66\u201d\u4e2d\uff0c\u6211\u5011\u900f\u904e\u5c07\u7576\u524d\u7bc0\u9ede\u65b0\u589e\u9032 <code>path</code> \u4f86\u8a18\u9304\u8def\u5f91\uff1b\u800c\u5728\u201c\u56de\u9000\u201d\u524d\uff0c\u6211\u5011\u9700\u8981\u5c07\u8a72\u7bc0\u9ede\u5f9e <code>path</code> \u4e2d\u5f48\u51fa\uff0c\u4ee5\u6062\u5fa9\u672c\u6b21\u5617\u8a66\u4e4b\u524d\u7684\u72c0\u614b\u3002</p> <p>\u89c0\u5bdf\u5716 13-2 \u6240\u793a\u7684\u904e\u7a0b\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u5617\u8a66\u548c\u56de\u9000\u7406\u89e3\u70ba\u201c\u524d\u9032\u201d\u8207\u201c\u64a4\u92b7\u201d\uff0c\u5169\u500b\u64cd\u4f5c\u4e92\u70ba\u9006\u5411\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt;&lt;10&gt;&lt;11&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 13-2 \u00a0 \u5617\u8a66\u8207\u56de\u9000 </p>"},{"location":"chapter_backtracking/backtracking_algorithm/#1312","title":"13.1.2 \u00a0 \u526a\u679d","text":"<p>\u8907\u96dc\u7684\u56de\u6eaf\u554f\u984c\u901a\u5e38\u5305\u542b\u4e00\u500b\u6216\u591a\u500b\u7d04\u675f\u689d\u4ef6\uff0c\u7d04\u675f\u689d\u4ef6\u901a\u5e38\u53ef\u7528\u65bc\u201c\u526a\u679d\u201d\u3002</p> <p>\u4f8b\u984c\u4e09</p> <p>\u5728\u4e8c\u5143\u6a39\u4e2d\u641c\u7d22\u6240\u6709\u503c\u70ba \\(7\\) \u7684\u7bc0\u9ede\uff0c\u8acb\u8fd4\u56de\u6839\u7bc0\u9ede\u5230\u9019\u4e9b\u7bc0\u9ede\u7684\u8def\u5f91\uff0c\u4e26\u8981\u6c42\u8def\u5f91\u4e2d\u4e0d\u5305\u542b\u503c\u70ba \\(3\\) \u7684\u7bc0\u9ede\u3002</p> <p>\u70ba\u4e86\u6eff\u8db3\u4ee5\u4e0a\u7d04\u675f\u689d\u4ef6\uff0c\u6211\u5011\u9700\u8981\u65b0\u589e\u526a\u679d\u64cd\u4f5c\uff1a\u5728\u641c\u5c0b\u904e\u7a0b\u4e2d\uff0c\u82e5\u9047\u5230\u503c\u70ba \\(3\\) \u7684\u7bc0\u9ede\uff0c\u5247\u63d0\u524d\u8fd4\u56de\uff0c\u4e0d\u518d\u7e7c\u7e8c\u641c\u5c0b\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig preorder_traversal_iii_compact.py<pre><code>def pre_order(root: TreeNode):\n \"\"\"\u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e09\"\"\"\n # \u526a\u679d\n if root is None or root.val == 3:\n return\n # \u5617\u8a66\n path.append(root)\n if root.val == 7:\n # \u8a18\u9304\u89e3\n res.append(list(path))\n pre_order(root.left)\n pre_order(root.right)\n # \u56de\u9000\n path.pop()\n</code></pre> preorder_traversal_iii_compact.cpp<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e09 */\nvoid preOrder(TreeNode *root) {\n // \u526a\u679d\n if (root == nullptr || root-&gt;val == 3) {\n return;\n }\n // \u5617\u8a66\n path.push_back(root);\n if (root-&gt;val == 7) {\n // \u8a18\u9304\u89e3\n res.push_back(path);\n }\n preOrder(root-&gt;left);\n preOrder(root-&gt;right);\n // \u56de\u9000\n path.pop_back();\n}\n</code></pre> preorder_traversal_iii_compact.java<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e09 */\nvoid preOrder(TreeNode root) {\n // \u526a\u679d\n if (root == null || root.val == 3) {\n return;\n }\n // \u5617\u8a66\n path.add(root);\n if (root.val == 7) {\n // \u8a18\u9304\u89e3\n res.add(new ArrayList&lt;&gt;(path));\n }\n preOrder(root.left);\n preOrder(root.right);\n // \u56de\u9000\n path.remove(path.size() - 1);\n}\n</code></pre> preorder_traversal_iii_compact.cs<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e09 */\nvoid PreOrder(TreeNode? root) {\n // \u526a\u679d\n if (root == null || root.val == 3) {\n return;\n }\n // \u5617\u8a66\n path.Add(root);\n if (root.val == 7) {\n // \u8a18\u9304\u89e3\n res.Add(new List&lt;TreeNode&gt;(path));\n }\n PreOrder(root.left);\n PreOrder(root.right);\n // \u56de\u9000\n path.RemoveAt(path.Count - 1);\n}\n</code></pre> preorder_traversal_iii_compact.go<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e09 */\nfunc preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n // \u526a\u679d\n if root == nil || root.Val == 3 {\n return\n }\n // \u5617\u8a66\n *path = append(*path, root)\n if root.Val.(int) == 7 {\n // \u8a18\u9304\u89e3\n *res = append(*res, append([]*TreeNode{}, *path...))\n }\n preOrderIII(root.Left, res, path)\n preOrderIII(root.Right, res, path)\n // \u56de\u9000\n *path = (*path)[:len(*path)-1]\n}\n</code></pre> preorder_traversal_iii_compact.swift<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e09 */\nfunc preOrder(root: TreeNode?) {\n // \u526a\u679d\n guard let root = root, root.val != 3 else {\n return\n }\n // \u5617\u8a66\n path.append(root)\n if root.val == 7 {\n // \u8a18\u9304\u89e3\n res.append(path)\n }\n preOrder(root: root.left)\n preOrder(root: root.right)\n // \u56de\u9000\n path.removeLast()\n}\n</code></pre> preorder_traversal_iii_compact.js<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e09 */\nfunction preOrder(root, path, res) {\n // \u526a\u679d\n if (root === null || root.val === 3) {\n return;\n }\n // \u5617\u8a66\n path.push(root);\n if (root.val === 7) {\n // \u8a18\u9304\u89e3\n res.push([...path]);\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.pop();\n}\n</code></pre> preorder_traversal_iii_compact.ts<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e09 */\nfunction preOrder(\n root: TreeNode | null,\n path: TreeNode[],\n res: TreeNode[][]\n): void {\n // \u526a\u679d\n if (root === null || root.val === 3) {\n return;\n }\n // \u5617\u8a66\n path.push(root);\n if (root.val === 7) {\n // \u8a18\u9304\u89e3\n res.push([...path]);\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.pop();\n}\n</code></pre> preorder_traversal_iii_compact.dart<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e09 */\nvoid preOrder(\n TreeNode? root,\n List&lt;TreeNode&gt; path,\n List&lt;List&lt;TreeNode&gt;&gt; res,\n) {\n if (root == null || root.val == 3) {\n return;\n }\n\n // \u5617\u8a66\n path.add(root);\n if (root.val == 7) {\n // \u8a18\u9304\u89e3\n res.add(List.from(path));\n }\n preOrder(root.left, path, res);\n preOrder(root.right, path, res);\n // \u56de\u9000\n path.removeLast();\n}\n</code></pre> preorder_traversal_iii_compact.rs<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e09 */\nfn pre_order(\n res: &amp;mut Vec&lt;Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;&gt;,\n path: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;,\n root: Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;,\n) {\n // \u526a\u679d\n if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {\n return;\n }\n if let Some(node) = root {\n // \u5617\u8a66\n path.push(node.clone());\n if node.borrow().val == 7 {\n // \u8a18\u9304\u89e3\n res.push(path.clone());\n }\n pre_order(res, path, node.borrow().left.clone());\n pre_order(res, path, node.borrow().right.clone());\n // \u56de\u9000\n path.remove(path.len() - 1);\n }\n}\n</code></pre> preorder_traversal_iii_compact.c<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e09 */\nvoid preOrder(TreeNode *root) {\n // \u526a\u679d\n if (root == NULL || root-&gt;val == 3) {\n return;\n }\n // \u5617\u8a66\n path[pathSize++] = root;\n if (root-&gt;val == 7) {\n // \u8a18\u9304\u89e3\n for (int i = 0; i &lt; pathSize; i++) {\n res[resSize][i] = path[i];\n }\n resSize++;\n }\n preOrder(root-&gt;left);\n preOrder(root-&gt;right);\n // \u56de\u9000\n pathSize--;\n}\n</code></pre> preorder_traversal_iii_compact.kt<pre><code>/* \u524d\u5e8f\u8d70\u8a2a\uff1a\u4f8b\u984c\u4e09 */\nfun preOrder(root: TreeNode?) {\n // \u526a\u679d\n if (root == null || root._val == 3) {\n return\n }\n // \u5617\u8a66\n path!!.add(root)\n if (root._val == 7) {\n // \u8a18\u9304\u89e3\n res!!.add(path!!.toMutableList())\n }\n preOrder(root.left)\n preOrder(root.right)\n // \u56de\u9000\n path!!.removeAt(path!!.size - 1)\n}\n</code></pre> preorder_traversal_iii_compact.rb<pre><code>[class]{}-[func]{pre_order}\n</code></pre> preorder_traversal_iii_compact.zig<pre><code>[class]{}-[func]{preOrder}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u201c\u526a\u679d\u201d\u662f\u4e00\u500b\u975e\u5e38\u5f62\u8c61\u7684\u540d\u8a5e\u3002\u5982\u5716 13-3 \u6240\u793a\uff0c\u5728\u641c\u5c0b\u904e\u7a0b\u4e2d\uff0c\u6211\u5011\u201c\u526a\u6389\u201d\u4e86\u4e0d\u6eff\u8db3\u7d04\u675f\u689d\u4ef6\u7684\u641c\u5c0b\u5206\u652f\uff0c\u907f\u514d\u8a31\u591a\u7121\u610f\u7fa9\u7684\u5617\u8a66\uff0c\u5f9e\u800c\u63d0\u9ad8\u4e86\u641c\u5c0b\u6548\u7387\u3002</p> <p></p> <p> \u5716 13-3 \u00a0 \u6839\u64da\u7d04\u675f\u689d\u4ef6\u526a\u679d </p>"},{"location":"chapter_backtracking/backtracking_algorithm/#1313","title":"13.1.3 \u00a0 \u6846\u67b6\u7a0b\u5f0f\u78bc","text":"<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5617\u8a66\u5c07\u56de\u6eaf\u7684\u201c\u5617\u8a66\u3001\u56de\u9000\u3001\u526a\u679d\u201d\u7684\u4e3b\u9ad4\u6846\u67b6\u63d0\u7149\u51fa\u4f86\uff0c\u63d0\u5347\u7a0b\u5f0f\u78bc\u7684\u901a\u7528\u6027\u3002</p> <p>\u5728\u4ee5\u4e0b\u6846\u67b6\u7a0b\u5f0f\u78bc\u4e2d\uff0c<code>state</code> \u8868\u793a\u554f\u984c\u7684\u7576\u524d\u72c0\u614b\uff0c<code>choices</code> \u8868\u793a\u7576\u524d\u72c0\u614b\u4e0b\u53ef\u4ee5\u505a\u51fa\u7684\u9078\u64c7\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code>def backtrack(state: State, choices: list[choice], res: list[state]):\n \"\"\"\u56de\u6eaf\u6f14\u7b97\u6cd5\u6846\u67b6\"\"\"\n # \u5224\u65b7\u662f\u5426\u70ba\u89e3\n if is_solution(state):\n # \u8a18\u9304\u89e3\n record_solution(state, res)\n # \u4e0d\u518d\u7e7c\u7e8c\u641c\u5c0b\n return\n # \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for choice in choices:\n # \u526a\u679d\uff1a\u5224\u65b7\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if is_valid(state, choice):\n # \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n make_choice(state, choice)\n backtrack(state, choices, res)\n # \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undo_choice(state, choice)\n</code></pre> <pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State *state, vector&lt;Choice *&gt; &amp;choices, vector&lt;State *&gt; &amp;res) {\n // \u5224\u65b7\u662f\u5426\u70ba\u89e3\n if (isSolution(state)) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7e7c\u7e8c\u641c\u5c0b\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (Choice choice : choices) {\n // \u526a\u679d\uff1a\u5224\u65b7\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state, choice);\n }\n }\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List&lt;Choice&gt; choices, List&lt;State&gt; res) {\n // \u5224\u65b7\u662f\u5426\u70ba\u89e3\n if (isSolution(state)) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7e7c\u7e8c\u641c\u5c0b\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (Choice choice : choices) {\n // \u526a\u679d\uff1a\u5224\u65b7\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state, choice);\n }\n }\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\u6846\u67b6 */\nvoid Backtrack(State state, List&lt;Choice&gt; choices, List&lt;State&gt; res) {\n // \u5224\u65b7\u662f\u5426\u70ba\u89e3\n if (IsSolution(state)) {\n // \u8a18\u9304\u89e3\n RecordSolution(state, res);\n // \u4e0d\u518d\u7e7c\u7e8c\u641c\u5c0b\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n foreach (Choice choice in choices) {\n // \u526a\u679d\uff1a\u5224\u65b7\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (IsValid(state, choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n MakeChoice(state, choice);\n Backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n UndoChoice(state, choice);\n }\n }\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\u6846\u67b6 */\nfunc backtrack(state *State, choices []Choice, res *[]State) {\n // \u5224\u65b7\u662f\u5426\u70ba\u89e3\n if isSolution(state) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res)\n // \u4e0d\u518d\u7e7c\u7e8c\u641c\u5c0b\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for _, choice := range choices {\n // \u526a\u679d\uff1a\u5224\u65b7\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if isValid(state, choice) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, choice)\n backtrack(state, choices, res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state, choice)\n }\n }\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\u6846\u67b6 */\nfunc backtrack(state: inout State, choices: [Choice], res: inout [State]) {\n // \u5224\u65b7\u662f\u5426\u70ba\u89e3\n if isSolution(state: state) {\n // \u8a18\u9304\u89e3\n recordSolution(state: state, res: &amp;res)\n // \u4e0d\u518d\u7e7c\u7e8c\u641c\u5c0b\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for choice in choices {\n // \u526a\u679d\uff1a\u5224\u65b7\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if isValid(state: state, choice: choice) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state: &amp;state, choice: choice)\n backtrack(state: &amp;state, choices: choices, res: &amp;res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state: &amp;state, choice: choice)\n }\n }\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\u6846\u67b6 */\nfunction backtrack(state, choices, res) {\n // \u5224\u65b7\u662f\u5426\u70ba\u89e3\n if (isSolution(state)) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7e7c\u7e8c\u641c\u5c0b\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (let choice of choices) {\n // \u526a\u679d\uff1a\u5224\u65b7\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state, choice);\n }\n }\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\u6846\u67b6 */\nfunction backtrack(state: State, choices: Choice[], res: State[]): void {\n // \u5224\u65b7\u662f\u5426\u70ba\u89e3\n if (isSolution(state)) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7e7c\u7e8c\u641c\u5c0b\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (let choice of choices) {\n // \u526a\u679d\uff1a\u5224\u65b7\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state, choice);\n }\n }\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List&lt;Choice&gt;, List&lt;State&gt; res) {\n // \u5224\u65b7\u662f\u5426\u70ba\u89e3\n if (isSolution(state)) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res);\n // \u4e0d\u518d\u7e7c\u7e8c\u641c\u5c0b\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (Choice choice in choices) {\n // \u526a\u679d\uff1a\u5224\u65b7\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state, choice);\n }\n }\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\u6846\u67b6 */\nfn backtrack(state: &amp;mut State, choices: &amp;Vec&lt;Choice&gt;, res: &amp;mut Vec&lt;State&gt;) {\n // \u5224\u65b7\u662f\u5426\u70ba\u89e3\n if is_solution(state) {\n // \u8a18\u9304\u89e3\n record_solution(state, res);\n // \u4e0d\u518d\u7e7c\u7e8c\u641c\u5c0b\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for choice in choices {\n // \u526a\u679d\uff1a\u5224\u65b7\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if is_valid(state, choice) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n make_choice(state, choice);\n backtrack(state, choices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undo_choice(state, choice);\n }\n }\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {\n // \u5224\u65b7\u662f\u5426\u70ba\u89e3\n if (isSolution(state)) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res, numRes);\n // \u4e0d\u518d\u7e7c\u7e8c\u641c\u5c0b\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (int i = 0; i &lt; numChoices; i++) {\n // \u526a\u679d\uff1a\u5224\u65b7\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (isValid(state, &amp;choices[i])) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, &amp;choices[i]);\n backtrack(state, choices, numChoices, res, numRes);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state, &amp;choices[i]);\n }\n }\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\u6846\u67b6 */\nfun backtrack(state: State?, choices: List&lt;Choice?&gt;, res: List&lt;State?&gt;?) {\n // \u5224\u65b7\u662f\u5426\u70ba\u89e3\n if (isSolution(state)) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res)\n // \u4e0d\u518d\u7e7c\u7e8c\u641c\u5c0b\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (choice in choices) {\n // \u526a\u679d\uff1a\u5224\u65b7\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, choice)\n backtrack(state, choices, res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state, choice)\n }\n }\n}\n</code></pre> <pre><code>\n</code></pre> <pre><code>\n</code></pre> <p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u57fa\u65bc\u6846\u67b6\u7a0b\u5f0f\u78bc\u4f86\u89e3\u6c7a\u4f8b\u984c\u4e09\u3002\u72c0\u614b <code>state</code> \u70ba\u7bc0\u9ede\u8d70\u8a2a\u8def\u5f91\uff0c\u9078\u64c7 <code>choices</code> \u70ba\u7576\u524d\u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u548c\u53f3\u5b50\u7bc0\u9ede\uff0c\u7d50\u679c <code>res</code> \u662f\u8def\u5f91\u4e32\u5217\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig preorder_traversal_iii_template.py<pre><code>def is_solution(state: list[TreeNode]) -&gt; bool:\n \"\"\"\u5224\u65b7\u7576\u524d\u72c0\u614b\u662f\u5426\u70ba\u89e3\"\"\"\n return state and state[-1].val == 7\n\ndef record_solution(state: list[TreeNode], res: list[list[TreeNode]]):\n \"\"\"\u8a18\u9304\u89e3\"\"\"\n res.append(list(state))\n\ndef is_valid(state: list[TreeNode], choice: TreeNode) -&gt; bool:\n \"\"\"\u5224\u65b7\u5728\u7576\u524d\u72c0\u614b\u4e0b\uff0c\u8a72\u9078\u64c7\u662f\u5426\u5408\u6cd5\"\"\"\n return choice is not None and choice.val != 3\n\ndef make_choice(state: list[TreeNode], choice: TreeNode):\n \"\"\"\u66f4\u65b0\u72c0\u614b\"\"\"\n state.append(choice)\n\ndef undo_choice(state: list[TreeNode], choice: TreeNode):\n \"\"\"\u6062\u5fa9\u72c0\u614b\"\"\"\n state.pop()\n\ndef backtrack(\n state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]\n):\n \"\"\"\u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u4f8b\u984c\u4e09\"\"\"\n # \u6aa2\u67e5\u662f\u5426\u70ba\u89e3\n if is_solution(state):\n # \u8a18\u9304\u89e3\n record_solution(state, res)\n # \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for choice in choices:\n # \u526a\u679d\uff1a\u6aa2\u67e5\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if is_valid(state, choice):\n # \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n make_choice(state, choice)\n # \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, [choice.left, choice.right], res)\n # \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undo_choice(state, choice)\n</code></pre> preorder_traversal_iii_template.cpp<pre><code>/* \u5224\u65b7\u7576\u524d\u72c0\u614b\u662f\u5426\u70ba\u89e3 */\nbool isSolution(vector&lt;TreeNode *&gt; &amp;state) {\n return !state.empty() &amp;&amp; state.back()-&gt;val == 7;\n}\n\n/* \u8a18\u9304\u89e3 */\nvoid recordSolution(vector&lt;TreeNode *&gt; &amp;state, vector&lt;vector&lt;TreeNode *&gt;&gt; &amp;res) {\n res.push_back(state);\n}\n\n/* \u5224\u65b7\u5728\u7576\u524d\u72c0\u614b\u4e0b\uff0c\u8a72\u9078\u64c7\u662f\u5426\u5408\u6cd5 */\nbool isValid(vector&lt;TreeNode *&gt; &amp;state, TreeNode *choice) {\n return choice != nullptr &amp;&amp; choice-&gt;val != 3;\n}\n\n/* \u66f4\u65b0\u72c0\u614b */\nvoid makeChoice(vector&lt;TreeNode *&gt; &amp;state, TreeNode *choice) {\n state.push_back(choice);\n}\n\n/* \u6062\u5fa9\u72c0\u614b */\nvoid undoChoice(vector&lt;TreeNode *&gt; &amp;state, TreeNode *choice) {\n state.pop_back();\n}\n\n/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u4f8b\u984c\u4e09 */\nvoid backtrack(vector&lt;TreeNode *&gt; &amp;state, vector&lt;TreeNode *&gt; &amp;choices, vector&lt;vector&lt;TreeNode *&gt;&gt; &amp;res) {\n // \u6aa2\u67e5\u662f\u5426\u70ba\u89e3\n if (isSolution(state)) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res);\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (TreeNode *choice : choices) {\n // \u526a\u679d\uff1a\u6aa2\u67e5\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n vector&lt;TreeNode *&gt; nextChoices{choice-&gt;left, choice-&gt;right};\n backtrack(state, nextChoices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state, choice);\n }\n }\n}\n</code></pre> preorder_traversal_iii_template.java<pre><code>/* \u5224\u65b7\u7576\u524d\u72c0\u614b\u662f\u5426\u70ba\u89e3 */\nboolean isSolution(List&lt;TreeNode&gt; state) {\n return !state.isEmpty() &amp;&amp; state.get(state.size() - 1).val == 7;\n}\n\n/* \u8a18\u9304\u89e3 */\nvoid recordSolution(List&lt;TreeNode&gt; state, List&lt;List&lt;TreeNode&gt;&gt; res) {\n res.add(new ArrayList&lt;&gt;(state));\n}\n\n/* \u5224\u65b7\u5728\u7576\u524d\u72c0\u614b\u4e0b\uff0c\u8a72\u9078\u64c7\u662f\u5426\u5408\u6cd5 */\nboolean isValid(List&lt;TreeNode&gt; state, TreeNode choice) {\n return choice != null &amp;&amp; choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72c0\u614b */\nvoid makeChoice(List&lt;TreeNode&gt; state, TreeNode choice) {\n state.add(choice);\n}\n\n/* \u6062\u5fa9\u72c0\u614b */\nvoid undoChoice(List&lt;TreeNode&gt; state, TreeNode choice) {\n state.remove(state.size() - 1);\n}\n\n/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u4f8b\u984c\u4e09 */\nvoid backtrack(List&lt;TreeNode&gt; state, List&lt;TreeNode&gt; choices, List&lt;List&lt;TreeNode&gt;&gt; res) {\n // \u6aa2\u67e5\u662f\u5426\u70ba\u89e3\n if (isSolution(state)) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res);\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (TreeNode choice : choices) {\n // \u526a\u679d\uff1a\u6aa2\u67e5\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, Arrays.asList(choice.left, choice.right), res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state, choice);\n }\n }\n}\n</code></pre> preorder_traversal_iii_template.cs<pre><code>/* \u5224\u65b7\u7576\u524d\u72c0\u614b\u662f\u5426\u70ba\u89e3 */\nbool IsSolution(List&lt;TreeNode&gt; state) {\n return state.Count != 0 &amp;&amp; state[^1].val == 7;\n}\n\n/* \u8a18\u9304\u89e3 */\nvoid RecordSolution(List&lt;TreeNode&gt; state, List&lt;List&lt;TreeNode&gt;&gt; res) {\n res.Add(new List&lt;TreeNode&gt;(state));\n}\n\n/* \u5224\u65b7\u5728\u7576\u524d\u72c0\u614b\u4e0b\uff0c\u8a72\u9078\u64c7\u662f\u5426\u5408\u6cd5 */\nbool IsValid(List&lt;TreeNode&gt; state, TreeNode choice) {\n return choice != null &amp;&amp; choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72c0\u614b */\nvoid MakeChoice(List&lt;TreeNode&gt; state, TreeNode choice) {\n state.Add(choice);\n}\n\n/* \u6062\u5fa9\u72c0\u614b */\nvoid UndoChoice(List&lt;TreeNode&gt; state, TreeNode choice) {\n state.RemoveAt(state.Count - 1);\n}\n\n/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u4f8b\u984c\u4e09 */\nvoid Backtrack(List&lt;TreeNode&gt; state, List&lt;TreeNode&gt; choices, List&lt;List&lt;TreeNode&gt;&gt; res) {\n // \u6aa2\u67e5\u662f\u5426\u70ba\u89e3\n if (IsSolution(state)) {\n // \u8a18\u9304\u89e3\n RecordSolution(state, res);\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n foreach (TreeNode choice in choices) {\n // \u526a\u679d\uff1a\u6aa2\u67e5\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (IsValid(state, choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n MakeChoice(state, choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n Backtrack(state, [choice.left!, choice.right!], res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n UndoChoice(state, choice);\n }\n }\n}\n</code></pre> preorder_traversal_iii_template.go<pre><code>/* \u5224\u65b7\u7576\u524d\u72c0\u614b\u662f\u5426\u70ba\u89e3 */\nfunc isSolution(state *[]*TreeNode) bool {\n return len(*state) != 0 &amp;&amp; (*state)[len(*state)-1].Val == 7\n}\n\n/* \u8a18\u9304\u89e3 */\nfunc recordSolution(state *[]*TreeNode, res *[][]*TreeNode) {\n *res = append(*res, append([]*TreeNode{}, *state...))\n}\n\n/* \u5224\u65b7\u5728\u7576\u524d\u72c0\u614b\u4e0b\uff0c\u8a72\u9078\u64c7\u662f\u5426\u5408\u6cd5 */\nfunc isValid(state *[]*TreeNode, choice *TreeNode) bool {\n return choice != nil &amp;&amp; choice.Val != 3\n}\n\n/* \u66f4\u65b0\u72c0\u614b */\nfunc makeChoice(state *[]*TreeNode, choice *TreeNode) {\n *state = append(*state, choice)\n}\n\n/* \u6062\u5fa9\u72c0\u614b */\nfunc undoChoice(state *[]*TreeNode, choice *TreeNode) {\n *state = (*state)[:len(*state)-1]\n}\n\n/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u4f8b\u984c\u4e09 */\nfunc backtrackIII(state *[]*TreeNode, choices *[]*TreeNode, res *[][]*TreeNode) {\n // \u6aa2\u67e5\u662f\u5426\u70ba\u89e3\n if isSolution(state) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res)\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for _, choice := range *choices {\n // \u526a\u679d\uff1a\u6aa2\u67e5\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if isValid(state, choice) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, choice)\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n temp := make([]*TreeNode, 0)\n temp = append(temp, choice.Left, choice.Right)\n backtrackIII(state, &amp;temp, res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state, choice)\n }\n }\n}\n</code></pre> preorder_traversal_iii_template.swift<pre><code>/* \u5224\u65b7\u7576\u524d\u72c0\u614b\u662f\u5426\u70ba\u89e3 */\nfunc isSolution(state: [TreeNode]) -&gt; Bool {\n !state.isEmpty &amp;&amp; state.last!.val == 7\n}\n\n/* \u8a18\u9304\u89e3 */\nfunc recordSolution(state: [TreeNode], res: inout [[TreeNode]]) {\n res.append(state)\n}\n\n/* \u5224\u65b7\u5728\u7576\u524d\u72c0\u614b\u4e0b\uff0c\u8a72\u9078\u64c7\u662f\u5426\u5408\u6cd5 */\nfunc isValid(state: [TreeNode], choice: TreeNode?) -&gt; Bool {\n choice != nil &amp;&amp; choice!.val != 3\n}\n\n/* \u66f4\u65b0\u72c0\u614b */\nfunc makeChoice(state: inout [TreeNode], choice: TreeNode) {\n state.append(choice)\n}\n\n/* \u6062\u5fa9\u72c0\u614b */\nfunc undoChoice(state: inout [TreeNode], choice: TreeNode) {\n state.removeLast()\n}\n\n/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u4f8b\u984c\u4e09 */\nfunc backtrack(state: inout [TreeNode], choices: [TreeNode], res: inout [[TreeNode]]) {\n // \u6aa2\u67e5\u662f\u5426\u70ba\u89e3\n if isSolution(state: state) {\n recordSolution(state: state, res: &amp;res)\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for choice in choices {\n // \u526a\u679d\uff1a\u6aa2\u67e5\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if isValid(state: state, choice: choice) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state: &amp;state, choice: choice)\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state: &amp;state, choices: [choice.left, choice.right].compactMap { $0 }, res: &amp;res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state: &amp;state, choice: choice)\n }\n }\n}\n</code></pre> preorder_traversal_iii_template.js<pre><code>/* \u5224\u65b7\u7576\u524d\u72c0\u614b\u662f\u5426\u70ba\u89e3 */\nfunction isSolution(state) {\n return state &amp;&amp; state[state.length - 1]?.val === 7;\n}\n\n/* \u8a18\u9304\u89e3 */\nfunction recordSolution(state, res) {\n res.push([...state]);\n}\n\n/* \u5224\u65b7\u5728\u7576\u524d\u72c0\u614b\u4e0b\uff0c\u8a72\u9078\u64c7\u662f\u5426\u5408\u6cd5 */\nfunction isValid(state, choice) {\n return choice !== null &amp;&amp; choice.val !== 3;\n}\n\n/* \u66f4\u65b0\u72c0\u614b */\nfunction makeChoice(state, choice) {\n state.push(choice);\n}\n\n/* \u6062\u5fa9\u72c0\u614b */\nfunction undoChoice(state) {\n state.pop();\n}\n\n/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u4f8b\u984c\u4e09 */\nfunction backtrack(state, choices, res) {\n // \u6aa2\u67e5\u662f\u5426\u70ba\u89e3\n if (isSolution(state)) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res);\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (const choice of choices) {\n // \u526a\u679d\uff1a\u6aa2\u67e5\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, [choice.left, choice.right], res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state);\n }\n }\n}\n</code></pre> preorder_traversal_iii_template.ts<pre><code>/* \u5224\u65b7\u7576\u524d\u72c0\u614b\u662f\u5426\u70ba\u89e3 */\nfunction isSolution(state: TreeNode[]): boolean {\n return state &amp;&amp; state[state.length - 1]?.val === 7;\n}\n\n/* \u8a18\u9304\u89e3 */\nfunction recordSolution(state: TreeNode[], res: TreeNode[][]): void {\n res.push([...state]);\n}\n\n/* \u5224\u65b7\u5728\u7576\u524d\u72c0\u614b\u4e0b\uff0c\u8a72\u9078\u64c7\u662f\u5426\u5408\u6cd5 */\nfunction isValid(state: TreeNode[], choice: TreeNode): boolean {\n return choice !== null &amp;&amp; choice.val !== 3;\n}\n\n/* \u66f4\u65b0\u72c0\u614b */\nfunction makeChoice(state: TreeNode[], choice: TreeNode): void {\n state.push(choice);\n}\n\n/* \u6062\u5fa9\u72c0\u614b */\nfunction undoChoice(state: TreeNode[]): void {\n state.pop();\n}\n\n/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u4f8b\u984c\u4e09 */\nfunction backtrack(\n state: TreeNode[],\n choices: TreeNode[],\n res: TreeNode[][]\n): void {\n // \u6aa2\u67e5\u662f\u5426\u70ba\u89e3\n if (isSolution(state)) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res);\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (const choice of choices) {\n // \u526a\u679d\uff1a\u6aa2\u67e5\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, [choice.left, choice.right], res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state);\n }\n }\n}\n</code></pre> preorder_traversal_iii_template.dart<pre><code>/* \u5224\u65b7\u7576\u524d\u72c0\u614b\u662f\u5426\u70ba\u89e3 */\nbool isSolution(List&lt;TreeNode&gt; state) {\n return state.isNotEmpty &amp;&amp; state.last.val == 7;\n}\n\n/* \u8a18\u9304\u89e3 */\nvoid recordSolution(List&lt;TreeNode&gt; state, List&lt;List&lt;TreeNode&gt;&gt; res) {\n res.add(List.from(state));\n}\n\n/* \u5224\u65b7\u5728\u7576\u524d\u72c0\u614b\u4e0b\uff0c\u8a72\u9078\u64c7\u662f\u5426\u5408\u6cd5 */\nbool isValid(List&lt;TreeNode&gt; state, TreeNode? choice) {\n return choice != null &amp;&amp; choice.val != 3;\n}\n\n/* \u66f4\u65b0\u72c0\u614b */\nvoid makeChoice(List&lt;TreeNode&gt; state, TreeNode? choice) {\n state.add(choice!);\n}\n\n/* \u6062\u5fa9\u72c0\u614b */\nvoid undoChoice(List&lt;TreeNode&gt; state, TreeNode? choice) {\n state.removeLast();\n}\n\n/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u4f8b\u984c\u4e09 */\nvoid backtrack(\n List&lt;TreeNode&gt; state,\n List&lt;TreeNode?&gt; choices,\n List&lt;List&lt;TreeNode&gt;&gt; res,\n) {\n // \u6aa2\u67e5\u662f\u5426\u70ba\u89e3\n if (isSolution(state)) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res);\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (TreeNode? choice in choices) {\n // \u526a\u679d\uff1a\u6aa2\u67e5\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, [choice!.left, choice.right], res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state, choice);\n }\n }\n}\n</code></pre> preorder_traversal_iii_template.rs<pre><code>/* \u5224\u65b7\u7576\u524d\u72c0\u614b\u662f\u5426\u70ba\u89e3 */\nfn is_solution(state: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;) -&gt; bool {\n return !state.is_empty() &amp;&amp; state.get(state.len() - 1).unwrap().borrow().val == 7;\n}\n\n/* \u8a18\u9304\u89e3 */\nfn record_solution(\n state: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;,\n res: &amp;mut Vec&lt;Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;&gt;,\n) {\n res.push(state.clone());\n}\n\n/* \u5224\u65b7\u5728\u7576\u524d\u72c0\u614b\u4e0b\uff0c\u8a72\u9078\u64c7\u662f\u5426\u5408\u6cd5 */\nfn is_valid(_: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, choice: Rc&lt;RefCell&lt;TreeNode&gt;&gt;) -&gt; bool {\n return choice.borrow().val != 3;\n}\n\n/* \u66f4\u65b0\u72c0\u614b */\nfn make_choice(state: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, choice: Rc&lt;RefCell&lt;TreeNode&gt;&gt;) {\n state.push(choice);\n}\n\n/* \u6062\u5fa9\u72c0\u614b */\nfn undo_choice(state: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, _: Rc&lt;RefCell&lt;TreeNode&gt;&gt;) {\n state.remove(state.len() - 1);\n}\n\n/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u4f8b\u984c\u4e09 */\nfn backtrack(\n state: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;,\n choices: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;,\n res: &amp;mut Vec&lt;Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;&gt;,\n) {\n // \u6aa2\u67e5\u662f\u5426\u70ba\u89e3\n if is_solution(state) {\n // \u8a18\u9304\u89e3\n record_solution(state, res);\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for choice in choices {\n // \u526a\u679d\uff1a\u6aa2\u67e5\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if is_valid(state, choice.clone()) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n make_choice(state, choice.clone());\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(\n state,\n &amp;mut vec![\n choice.borrow().left.clone().unwrap(),\n choice.borrow().right.clone().unwrap(),\n ],\n res,\n );\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undo_choice(state, choice.clone());\n }\n }\n}\n</code></pre> preorder_traversal_iii_template.c<pre><code>/* \u5224\u65b7\u7576\u524d\u72c0\u614b\u662f\u5426\u70ba\u89e3 */\nbool isSolution(void) {\n return pathSize &gt; 0 &amp;&amp; path[pathSize - 1]-&gt;val == 7;\n}\n\n/* \u8a18\u9304\u89e3 */\nvoid recordSolution(void) {\n for (int i = 0; i &lt; pathSize; i++) {\n res[resSize][i] = path[i];\n }\n resSize++;\n}\n\n/* \u5224\u65b7\u5728\u7576\u524d\u72c0\u614b\u4e0b\uff0c\u8a72\u9078\u64c7\u662f\u5426\u5408\u6cd5 */\nbool isValid(TreeNode *choice) {\n return choice != NULL &amp;&amp; choice-&gt;val != 3;\n}\n\n/* \u66f4\u65b0\u72c0\u614b */\nvoid makeChoice(TreeNode *choice) {\n path[pathSize++] = choice;\n}\n\n/* \u6062\u5fa9\u72c0\u614b */\nvoid undoChoice(void) {\n pathSize--;\n}\n\n/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u4f8b\u984c\u4e09 */\nvoid backtrack(TreeNode *choices[2]) {\n // \u6aa2\u67e5\u662f\u5426\u70ba\u89e3\n if (isSolution()) {\n // \u8a18\u9304\u89e3\n recordSolution();\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (int i = 0; i &lt; 2; i++) {\n TreeNode *choice = choices[i];\n // \u526a\u679d\uff1a\u6aa2\u67e5\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (isValid(choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n TreeNode *nextChoices[2] = {choice-&gt;left, choice-&gt;right};\n backtrack(nextChoices);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice();\n }\n }\n}\n</code></pre> preorder_traversal_iii_template.kt<pre><code>/* \u5224\u65b7\u7576\u524d\u72c0\u614b\u662f\u5426\u70ba\u89e3 */\nfun isSolution(state: MutableList&lt;TreeNode?&gt;): Boolean {\n return state.isNotEmpty() &amp;&amp; state[state.size - 1]?._val == 7\n}\n\n/* \u8a18\u9304\u89e3 */\nfun recordSolution(state: MutableList&lt;TreeNode?&gt;?, res: MutableList&lt;MutableList&lt;TreeNode?&gt;?&gt;) {\n res.add(state!!.toMutableList())\n}\n\n/* \u5224\u65b7\u5728\u7576\u524d\u72c0\u614b\u4e0b\uff0c\u8a72\u9078\u64c7\u662f\u5426\u5408\u6cd5 */\nfun isValid(state: MutableList&lt;TreeNode?&gt;?, choice: TreeNode?): Boolean {\n return choice != null &amp;&amp; choice._val != 3\n}\n\n/* \u66f4\u65b0\u72c0\u614b */\nfun makeChoice(state: MutableList&lt;TreeNode?&gt;, choice: TreeNode?) {\n state.add(choice)\n}\n\n/* \u6062\u5fa9\u72c0\u614b */\nfun undoChoice(state: MutableList&lt;TreeNode?&gt;, choice: TreeNode?) {\n state.removeLast()\n}\n\n/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u4f8b\u984c\u4e09 */\nfun backtrack(\n state: MutableList&lt;TreeNode?&gt;,\n choices: MutableList&lt;TreeNode?&gt;,\n res: MutableList&lt;MutableList&lt;TreeNode?&gt;?&gt;\n) {\n // \u6aa2\u67e5\u662f\u5426\u70ba\u89e3\n if (isSolution(state)) {\n // \u8a18\u9304\u89e3\n recordSolution(state, res)\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (choice in choices) {\n // \u526a\u679d\uff1a\u6aa2\u67e5\u9078\u64c7\u662f\u5426\u5408\u6cd5\n if (isValid(state, choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n makeChoice(state, choice)\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, mutableListOf(choice!!.left, choice.right), res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n undoChoice(state, choice)\n }\n }\n}\n</code></pre> preorder_traversal_iii_template.rb<pre><code>[class]{}-[func]{is_solution}\n\n[class]{}-[func]{record_solution}\n\n[class]{}-[func]{is_valid}\n\n[class]{}-[func]{make_choice}\n\n[class]{}-[func]{undo_choice}\n\n[class]{}-[func]{backtrack}\n</code></pre> preorder_traversal_iii_template.zig<pre><code>[class]{}-[func]{isSolution}\n\n[class]{}-[func]{recordSolution}\n\n[class]{}-[func]{isValid}\n\n[class]{}-[func]{makeChoice}\n\n[class]{}-[func]{undoChoice}\n\n[class]{}-[func]{backtrack}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u6839\u64da\u984c\u610f\uff0c\u6211\u5011\u5728\u627e\u5230\u503c\u70ba \\(7\\) \u7684\u7bc0\u9ede\u5f8c\u61c9\u8a72\u7e7c\u7e8c\u641c\u5c0b\uff0c\u56e0\u6b64\u9700\u8981\u5c07\u8a18\u9304\u89e3\u4e4b\u5f8c\u7684 <code>return</code> \u8a9e\u53e5\u522a\u9664\u3002\u5716 13-4 \u5c0d\u6bd4\u4e86\u4fdd\u7559\u6216\u522a\u9664 <code>return</code> \u8a9e\u53e5\u7684\u641c\u5c0b\u904e\u7a0b\u3002</p> <p></p> <p> \u5716 13-4 \u00a0 \u4fdd\u7559\u8207\u522a\u9664 return \u7684\u641c\u5c0b\u904e\u7a0b\u5c0d\u6bd4 </p> <p>\u76f8\u6bd4\u57fa\u65bc\u524d\u5e8f\u8d70\u8a2a\u7684\u7a0b\u5f0f\u78bc\u5be6\u73fe\uff0c\u57fa\u65bc\u56de\u6eaf\u6f14\u7b97\u6cd5\u6846\u67b6\u7684\u7a0b\u5f0f\u78bc\u5be6\u73fe\u96d6\u7136\u986f\u5f97\u56c9\u5506\uff0c\u4f46\u901a\u7528\u6027\u66f4\u597d\u3002\u5be6\u969b\u4e0a\uff0c\u8a31\u591a\u56de\u6eaf\u554f\u984c\u53ef\u4ee5\u5728\u8a72\u6846\u67b6\u4e0b\u89e3\u6c7a\u3002\u6211\u5011\u53ea\u9700\u6839\u64da\u5177\u9ad4\u554f\u984c\u4f86\u5b9a\u7fa9 <code>state</code> \u548c <code>choices</code> \uff0c\u4e26\u5be6\u73fe\u6846\u67b6\u4e2d\u7684\u5404\u500b\u65b9\u6cd5\u5373\u53ef\u3002</p>"},{"location":"chapter_backtracking/backtracking_algorithm/#1314","title":"13.1.4 \u00a0 \u5e38\u7528\u8853\u8a9e","text":"<p>\u70ba\u4e86\u66f4\u6e05\u6670\u5730\u5206\u6790\u6f14\u7b97\u6cd5\u554f\u984c\uff0c\u6211\u5011\u7e3d\u7d50\u4e00\u4e0b\u56de\u6eaf\u6f14\u7b97\u6cd5\u4e2d\u5e38\u7528\u8853\u8a9e\u7684\u542b\u7fa9\uff0c\u4e26\u5c0d\u7167\u4f8b\u984c\u4e09\u7d66\u51fa\u5c0d\u61c9\u793a\u4f8b\uff0c\u5982\u8868 13-1 \u6240\u793a\u3002</p> <p> \u8868 13-1 \u00a0 \u5e38\u898b\u7684\u56de\u6eaf\u6f14\u7b97\u6cd5\u8853\u8a9e </p> \u540d\u8a5e \u5b9a\u7fa9 \u4f8b\u984c\u4e09 \u89e3\uff08solution\uff09 \u89e3\u662f\u6eff\u8db3\u554f\u984c\u7279\u5b9a\u689d\u4ef6\u7684\u7b54\u6848\uff0c\u53ef\u80fd\u6709\u4e00\u500b\u6216\u591a\u500b \u6839\u7bc0\u9ede\u5230\u7bc0\u9ede \\(7\\) \u7684\u6eff\u8db3\u7d04\u675f\u689d\u4ef6\u7684\u6240\u6709\u8def\u5f91 \u7d04\u675f\u689d\u4ef6\uff08constraint\uff09 \u7d04\u675f\u689d\u4ef6\u662f\u554f\u984c\u4e2d\u9650\u5236\u89e3\u7684\u53ef\u884c\u6027\u7684\u689d\u4ef6\uff0c\u901a\u5e38\u7528\u65bc\u526a\u679d \u8def\u5f91\u4e2d\u4e0d\u5305\u542b\u7bc0\u9ede \\(3\\) \u72c0\u614b\uff08state\uff09 \u72c0\u614b\u8868\u793a\u554f\u984c\u5728\u67d0\u4e00\u6642\u523b\u7684\u60c5\u6cc1\uff0c\u5305\u62ec\u5df2\u7d93\u505a\u51fa\u7684\u9078\u64c7 \u7576\u524d\u5df2\u8a2a\u554f\u7684\u7bc0\u9ede\u8def\u5f91\uff0c\u5373 <code>path</code> \u7bc0\u9ede\u4e32\u5217 \u5617\u8a66\uff08attempt\uff09 \u5617\u8a66\u662f\u6839\u64da\u53ef\u7528\u9078\u64c7\u4f86\u63a2\u7d22\u89e3\u7a7a\u9593\u7684\u904e\u7a0b\uff0c\u5305\u62ec\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\uff0c\u6aa2\u67e5\u662f\u5426\u70ba\u89e3 \u905e\u8ff4\u8a2a\u554f\u5de6\uff08\u53f3\uff09\u5b50\u7bc0\u9ede\uff0c\u5c07\u7bc0\u9ede\u65b0\u589e\u9032 <code>path</code> \uff0c\u5224\u65b7\u7bc0\u9ede\u7684\u503c\u662f\u5426\u70ba \\(7\\) \u56de\u9000\uff08backtracking\uff09 \u56de\u9000\u6307\u9047\u5230\u4e0d\u6eff\u8db3\u7d04\u675f\u689d\u4ef6\u7684\u72c0\u614b\u6642\uff0c\u64a4\u92b7\u524d\u9762\u505a\u51fa\u7684\u9078\u64c7\uff0c\u56de\u5230\u4e0a\u4e00\u500b\u72c0\u614b \u7576\u8d8a\u904e\u8449\u7bc0\u9ede\u3001\u7d50\u675f\u7bc0\u9ede\u8a2a\u554f\u3001\u9047\u5230\u503c\u70ba \\(3\\) \u7684\u7bc0\u9ede\u6642\u7d42\u6b62\u641c\u5c0b\uff0c\u51fd\u5f0f\u8fd4\u56de \u526a\u679d\uff08pruning\uff09 \u526a\u679d\u662f\u6839\u64da\u554f\u984c\u7279\u6027\u548c\u7d04\u675f\u689d\u4ef6\u907f\u514d\u7121\u610f\u7fa9\u7684\u641c\u5c0b\u8def\u5f91\u7684\u65b9\u6cd5\uff0c\u53ef\u63d0\u9ad8\u641c\u5c0b\u6548\u7387 \u7576\u9047\u5230\u503c\u70ba \\(3\\) \u7684\u7bc0\u9ede\u6642\uff0c\u5247\u4e0d\u518d\u7e7c\u7e8c\u641c\u5c0b <p>Tip</p> <p>\u554f\u984c\u3001\u89e3\u3001\u72c0\u614b\u7b49\u6982\u5ff5\u662f\u901a\u7528\u7684\uff0c\u5728\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52d5\u614b\u898f\u5283\u3001\u8caa\u5a6a\u7b49\u6f14\u7b97\u6cd5\u4e2d\u90fd\u6709\u6d89\u53ca\u3002</p>"},{"location":"chapter_backtracking/backtracking_algorithm/#1315","title":"13.1.5 \u00a0 \u512a\u9ede\u8207\u4fb7\u9650\u6027","text":"<p>\u56de\u6eaf\u6f14\u7b97\u6cd5\u672c\u8cea\u4e0a\u662f\u4e00\u7a2e\u6df1\u5ea6\u512a\u5148\u641c\u5c0b\u6f14\u7b97\u6cd5\uff0c\u5b83\u5617\u8a66\u6240\u6709\u53ef\u80fd\u7684\u89e3\u6c7a\u65b9\u6848\u76f4\u5230\u627e\u5230\u6eff\u8db3\u689d\u4ef6\u7684\u89e3\u3002\u9019\u7a2e\u65b9\u6cd5\u7684\u512a\u9ede\u5728\u65bc\u80fd\u5920\u627e\u5230\u6240\u6709\u53ef\u80fd\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u800c\u4e14\u5728\u5408\u7406\u7684\u526a\u679d\u64cd\u4f5c\u4e0b\uff0c\u5177\u6709\u5f88\u9ad8\u7684\u6548\u7387\u3002</p> <p>\u7136\u800c\uff0c\u5728\u8655\u7406\u5927\u898f\u6a21\u6216\u8005\u8907\u96dc\u554f\u984c\u6642\uff0c\u56de\u6eaf\u6f14\u7b97\u6cd5\u7684\u57f7\u884c\u6548\u7387\u53ef\u80fd\u96e3\u4ee5\u63a5\u53d7\u3002</p> <ul> <li>\u6642\u9593\uff1a\u56de\u6eaf\u6f14\u7b97\u6cd5\u901a\u5e38\u9700\u8981\u8d70\u8a2a\u72c0\u614b\u7a7a\u9593\u7684\u6240\u6709\u53ef\u80fd\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u53ef\u4ee5\u9054\u5230\u6307\u6578\u968e\u6216\u968e\u4e58\u968e\u3002</li> <li>\u7a7a\u9593\uff1a\u5728\u905e\u8ff4\u547c\u53eb\u4e2d\u9700\u8981\u5132\u5b58\u7576\u524d\u7684\u72c0\u614b\uff08\u4f8b\u5982\u8def\u5f91\u3001\u7528\u65bc\u526a\u679d\u7684\u8f14\u52a9\u8b8a\u6578\u7b49\uff09\uff0c\u7576\u6df1\u5ea6\u5f88\u5927\u6642\uff0c\u7a7a\u9593\u9700\u6c42\u53ef\u80fd\u6703\u8b8a\u5f97\u5f88\u5927\u3002</li> </ul> <p>\u5373\u4fbf\u5982\u6b64\uff0c\u56de\u6eaf\u6f14\u7b97\u6cd5\u4ecd\u7136\u662f\u67d0\u4e9b\u641c\u5c0b\u554f\u984c\u548c\u7d04\u675f\u6eff\u8db3\u554f\u984c\u7684\u6700\u4f73\u89e3\u6c7a\u65b9\u6848\u3002\u5c0d\u65bc\u9019\u4e9b\u554f\u984c\uff0c\u7531\u65bc\u7121\u6cd5\u9810\u6e2c\u54ea\u4e9b\u9078\u64c7\u53ef\u751f\u6210\u6709\u6548\u7684\u89e3\uff0c\u56e0\u6b64\u6211\u5011\u5fc5\u9808\u5c0d\u6240\u6709\u53ef\u80fd\u7684\u9078\u64c7\u9032\u884c\u8d70\u8a2a\u3002\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u95dc\u9375\u662f\u5982\u4f55\u6700\u4f73\u5316\u6548\u7387\uff0c\u5e38\u898b\u7684\u6548\u7387\u6700\u4f73\u5316\u65b9\u6cd5\u6709\u5169\u7a2e\u3002</p> <ul> <li>\u526a\u679d\uff1a\u907f\u514d\u641c\u5c0b\u90a3\u4e9b\u80af\u5b9a\u4e0d\u6703\u7522\u751f\u89e3\u7684\u8def\u5f91\uff0c\u5f9e\u800c\u7bc0\u7701\u6642\u9593\u548c\u7a7a\u9593\u3002</li> <li>\u555f\u767c\u5f0f\u641c\u5c0b\uff1a\u5728\u641c\u5c0b\u904e\u7a0b\u4e2d\u5f15\u5165\u4e00\u4e9b\u7b56\u7565\u6216\u8005\u4f30\u8a08\u503c\uff0c\u5f9e\u800c\u512a\u5148\u641c\u5c0b\u6700\u6709\u53ef\u80fd\u7522\u751f\u6709\u6548\u89e3\u7684\u8def\u5f91\u3002</li> </ul>"},{"location":"chapter_backtracking/backtracking_algorithm/#1316","title":"13.1.6 \u00a0 \u56de\u6eaf\u5178\u578b\u4f8b\u984c","text":"<p>\u56de\u6eaf\u6f14\u7b97\u6cd5\u53ef\u7528\u65bc\u89e3\u6c7a\u8a31\u591a\u641c\u5c0b\u554f\u984c\u3001\u7d04\u675f\u6eff\u8db3\u554f\u984c\u548c\u7d44\u5408\u6700\u4f73\u5316\u554f\u984c\u3002</p> <p>\u641c\u5c0b\u554f\u984c\uff1a\u9019\u985e\u554f\u984c\u7684\u76ee\u6a19\u662f\u627e\u5230\u6eff\u8db3\u7279\u5b9a\u689d\u4ef6\u7684\u89e3\u6c7a\u65b9\u6848\u3002</p> <ul> <li>\u5168\u6392\u5217\u554f\u984c\uff1a\u7d66\u5b9a\u4e00\u500b\u96c6\u5408\uff0c\u6c42\u51fa\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u7d44\u5408\u3002</li> <li>\u5b50\u96c6\u548c\u554f\u984c\uff1a\u7d66\u5b9a\u4e00\u500b\u96c6\u5408\u548c\u4e00\u500b\u76ee\u6a19\u548c\uff0c\u627e\u5230\u96c6\u5408\u4e2d\u6240\u6709\u548c\u70ba\u76ee\u6a19\u548c\u7684\u5b50\u96c6\u3002</li> <li>\u6cb3\u5167\u5854\u554f\u984c\uff1a\u7d66\u5b9a\u4e09\u6839\u67f1\u5b50\u548c\u4e00\u7cfb\u5217\u5927\u5c0f\u4e0d\u540c\u7684\u5713\u76e4\uff0c\u8981\u6c42\u5c07\u6240\u6709\u5713\u76e4\u5f9e\u4e00\u6839\u67f1\u5b50\u79fb\u52d5\u5230\u53e6\u4e00\u6839\u67f1\u5b50\uff0c\u6bcf\u6b21\u53ea\u80fd\u79fb\u52d5\u4e00\u500b\u5713\u76e4\uff0c\u4e14\u4e0d\u80fd\u5c07\u5927\u5713\u76e4\u653e\u5728\u5c0f\u5713\u76e4\u4e0a\u3002</li> </ul> <p>\u7d04\u675f\u6eff\u8db3\u554f\u984c\uff1a\u9019\u985e\u554f\u984c\u7684\u76ee\u6a19\u662f\u627e\u5230\u6eff\u8db3\u6240\u6709\u7d04\u675f\u689d\u4ef6\u7684\u89e3\u3002</p> <ul> <li>\\(n\\) \u7687\u540e\uff1a\u5728 \\(n \\times n\\) \u7684\u68cb\u76e4\u4e0a\u653e\u7f6e \\(n\\) \u500b\u7687\u540e\uff0c\u4f7f\u5f97\u5b83\u5011\u4e92\u4e0d\u653b\u64ca\u3002</li> <li>\u6578\u7368\uff1a\u5728 \\(9 \\times 9\\) \u7684\u7db2\u683c\u4e2d\u586b\u5165\u6578\u5b57 \\(1\\) ~ \\(9\\) \uff0c\u4f7f\u5f97\u6bcf\u884c\u3001\u6bcf\u5217\u548c\u6bcf\u500b \\(3 \\times 3\\) \u5b50\u7db2\u683c\u4e2d\u7684\u6578\u5b57\u4e0d\u91cd\u8907\u3002</li> <li>\u5716\u8457\u8272\u554f\u984c\uff1a\u7d66\u5b9a\u4e00\u500b\u7121\u5411\u5716\uff0c\u7528\u6700\u5c11\u7684\u984f\u8272\u7d66\u5716\u7684\u6bcf\u500b\u9802\u9ede\u8457\u8272\uff0c\u4f7f\u5f97\u76f8\u9130\u9802\u9ede\u984f\u8272\u4e0d\u540c\u3002</li> </ul> <p>\u7d44\u5408\u6700\u4f73\u5316\u554f\u984c\uff1a\u9019\u985e\u554f\u984c\u7684\u76ee\u6a19\u662f\u5728\u4e00\u500b\u7d44\u5408\u7a7a\u9593\u4e2d\u627e\u5230\u6eff\u8db3\u67d0\u4e9b\u689d\u4ef6\u7684\u6700\u512a\u89e3\u3002</p> <ul> <li>0-1 \u80cc\u5305\u554f\u984c\uff1a\u7d66\u5b9a\u4e00\u7d44\u7269\u54c1\u548c\u4e00\u500b\u80cc\u5305\uff0c\u6bcf\u500b\u7269\u54c1\u6709\u4e00\u5b9a\u7684\u50f9\u503c\u548c\u91cd\u91cf\uff0c\u8981\u6c42\u5728\u80cc\u5305\u5bb9\u91cf\u9650\u5236\u5167\uff0c\u9078\u64c7\u7269\u54c1\u4f7f\u5f97\u7e3d\u50f9\u503c\u6700\u5927\u3002</li> <li>\u65c5\u884c\u5546\u554f\u984c\uff1a\u5728\u4e00\u500b\u5716\u4e2d\uff0c\u5f9e\u4e00\u500b\u9ede\u51fa\u767c\uff0c\u8a2a\u554f\u6240\u6709\u5176\u4ed6\u9ede\u6070\u597d\u4e00\u6b21\u5f8c\u8fd4\u56de\u8d77\u9ede\uff0c\u6c42\u6700\u77ed\u8def\u5f91\u3002</li> <li>\u6700\u5927\u5718\u554f\u984c\uff1a\u7d66\u5b9a\u4e00\u500b\u7121\u5411\u5716\uff0c\u627e\u5230\u6700\u5927\u7684\u5b8c\u5168\u5b50\u5716\uff0c\u5373\u5b50\u5716\u4e2d\u7684\u4efb\u610f\u5169\u500b\u9802\u9ede\u4e4b\u9593\u90fd\u6709\u908a\u76f8\u9023\u3002</li> </ul> <p>\u8acb\u6ce8\u610f\uff0c\u5c0d\u65bc\u8a31\u591a\u7d44\u5408\u6700\u4f73\u5316\u554f\u984c\uff0c\u56de\u6eaf\u4e0d\u662f\u6700\u512a\u89e3\u6c7a\u65b9\u6848\u3002</p> <ul> <li>0-1 \u80cc\u5305\u554f\u984c\u901a\u5e38\u4f7f\u7528\u52d5\u614b\u898f\u5283\u89e3\u6c7a\uff0c\u4ee5\u9054\u5230\u66f4\u9ad8\u7684\u6642\u9593\u6548\u7387\u3002</li> <li>\u65c5\u884c\u5546\u662f\u4e00\u500b\u8457\u540d\u7684 NP-Hard \u554f\u984c\uff0c\u5e38\u7528\u89e3\u6cd5\u6709\u907a\u50b3\u6f14\u7b97\u6cd5\u548c\u87fb\u7fa4\u6f14\u7b97\u6cd5\u7b49\u3002</li> <li>\u6700\u5927\u5718\u554f\u984c\u662f\u5716\u8ad6\u4e2d\u7684\u4e00\u500b\u7d93\u5178\u554f\u984c\uff0c\u53ef\u7528\u8caa\u5a6a\u6f14\u7b97\u6cd5\u7b49\u555f\u767c\u5f0f\u6f14\u7b97\u6cd5\u4f86\u89e3\u6c7a\u3002</li> </ul>"},{"location":"chapter_backtracking/n_queens_problem/","title":"13.4 \u00a0 n \u7687\u540e\u554f\u984c","text":"<p>Question</p> <p>\u6839\u64da\u570b\u969b\u8c61\u68cb\u7684\u898f\u5247\uff0c\u7687\u540e\u53ef\u4ee5\u653b\u64ca\u8207\u540c\u8655\u4e00\u884c\u3001\u4e00\u5217\u6216\u4e00\u689d\u659c\u7dda\u4e0a\u7684\u68cb\u5b50\u3002\u7d66\u5b9a \\(n\\) \u500b\u7687\u540e\u548c\u4e00\u500b \\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76e4\uff0c\u5c0b\u627e\u4f7f\u5f97\u6240\u6709\u7687\u540e\u4e4b\u9593\u7121\u6cd5\u76f8\u4e92\u653b\u64ca\u7684\u64fa\u653e\u65b9\u6848\u3002</p> <p>\u5982\u5716 13-15 \u6240\u793a\uff0c\u7576 \\(n = 4\\) \u6642\uff0c\u5171\u53ef\u4ee5\u627e\u5230\u5169\u500b\u89e3\u3002\u5f9e\u56de\u6eaf\u6f14\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76e4\u5171\u6709 \\(n^2\\) \u500b\u683c\u5b50\uff0c\u7d66\u51fa\u4e86\u6240\u6709\u7684\u9078\u64c7 <code>choices</code> \u3002\u5728\u9010\u500b\u653e\u7f6e\u7687\u540e\u7684\u904e\u7a0b\u4e2d\uff0c\u68cb\u76e4\u72c0\u614b\u5728\u4e0d\u65b7\u5730\u8b8a\u5316\uff0c\u6bcf\u500b\u6642\u523b\u7684\u68cb\u76e4\u5c31\u662f\u72c0\u614b <code>state</code> \u3002</p> <p></p> <p> \u5716 13-15 \u00a0 4 \u7687\u540e\u554f\u984c\u7684\u89e3 </p> <p>\u5716 13-16 \u5c55\u793a\u4e86\u672c\u984c\u7684\u4e09\u500b\u7d04\u675f\u689d\u4ef6\uff1a\u591a\u500b\u7687\u540e\u4e0d\u80fd\u5728\u540c\u4e00\u884c\u3001\u540c\u4e00\u5217\u3001\u540c\u4e00\u689d\u5c0d\u89d2\u7dda\u4e0a\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5c0d\u89d2\u7dda\u5206\u70ba\u4e3b\u5c0d\u89d2\u7dda <code>\\</code> \u548c\u6b21\u5c0d\u89d2\u7dda <code>/</code> \u5169\u7a2e\u3002</p> <p></p> <p> \u5716 13-16 \u00a0 n \u7687\u540e\u554f\u984c\u7684\u7d04\u675f\u689d\u4ef6 </p>"},{"location":"chapter_backtracking/n_queens_problem/#1","title":"1. \u00a0 \u9010\u884c\u653e\u7f6e\u7b56\u7565","text":"<p>\u7687\u540e\u7684\u6578\u91cf\u548c\u68cb\u76e4\u7684\u884c\u6578\u90fd\u70ba \\(n\\) \uff0c\u56e0\u6b64\u6211\u5011\u5bb9\u6613\u5f97\u5230\u4e00\u500b\u63a8\u8ad6\uff1a\u68cb\u76e4\u6bcf\u884c\u90fd\u5141\u8a31\u4e14\u53ea\u5141\u8a31\u653e\u7f6e\u4e00\u500b\u7687\u540e\u3002</p> <p>\u4e5f\u5c31\u662f\u8aaa\uff0c\u6211\u5011\u53ef\u4ee5\u63a1\u53d6\u9010\u884c\u653e\u7f6e\u7b56\u7565\uff1a\u5f9e\u7b2c\u4e00\u884c\u958b\u59cb\uff0c\u5728\u6bcf\u884c\u653e\u7f6e\u4e00\u500b\u7687\u540e\uff0c\u76f4\u81f3\u6700\u5f8c\u4e00\u884c\u7d50\u675f\u3002</p> <p>\u5716 13-17 \u6240\u793a\u70ba \\(4\\) \u7687\u540e\u554f\u984c\u7684\u9010\u884c\u653e\u7f6e\u904e\u7a0b\u3002\u53d7\u756b\u5e45\u9650\u5236\uff0c\u5716 13-17 \u50c5\u5c55\u958b\u4e86\u7b2c\u4e00\u884c\u7684\u5176\u4e2d\u4e00\u500b\u641c\u5c0b\u5206\u652f\uff0c\u4e26\u4e14\u5c07\u4e0d\u6eff\u8db3\u5217\u7d04\u675f\u548c\u5c0d\u89d2\u7dda\u7d04\u675f\u7684\u65b9\u6848\u90fd\u9032\u884c\u4e86\u526a\u679d\u3002</p> <p></p> <p> \u5716 13-17 \u00a0 \u9010\u884c\u653e\u7f6e\u7b56\u7565 </p> <p>\u5f9e\u672c\u8cea\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\u73fe\u591a\u500b\u7687\u540e\u7684\u6240\u6709\u641c\u5c0b\u5206\u652f\u3002</p>"},{"location":"chapter_backtracking/n_queens_problem/#2","title":"2. \u00a0 \u5217\u8207\u5c0d\u89d2\u7dda\u526a\u679d","text":"<p>\u70ba\u4e86\u6eff\u8db3\u5217\u7d04\u675f\uff0c\u6211\u5011\u53ef\u4ee5\u5229\u7528\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u5e03\u6797\u578b\u9663\u5217 <code>cols</code> \u8a18\u9304\u6bcf\u4e00\u5217\u662f\u5426\u6709\u7687\u540e\u3002\u5728\u6bcf\u6b21\u6c7a\u5b9a\u653e\u7f6e\u524d\uff0c\u6211\u5011\u900f\u904e <code>cols</code> \u5c07\u5df2\u6709\u7687\u540e\u7684\u5217\u9032\u884c\u526a\u679d\uff0c\u4e26\u5728\u56de\u6eaf\u4e2d\u52d5\u614b\u66f4\u65b0 <code>cols</code> \u7684\u72c0\u614b\u3002</p> <p>\u90a3\u9ebc\uff0c\u5982\u4f55\u8655\u7406\u5c0d\u89d2\u7dda\u7d04\u675f\u5462\uff1f\u8a2d\u68cb\u76e4\u4e2d\u67d0\u500b\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u70ba \\((row, col)\\) \uff0c\u9078\u5b9a\u77e9\u9663\u4e2d\u7684\u67d0\u689d\u4e3b\u5c0d\u89d2\u7dda\uff0c\u6211\u5011\u767c\u73fe\u8a72\u5c0d\u89d2\u7dda\u4e0a\u6240\u6709\u683c\u5b50\u7684\u884c\u7d22\u5f15\u6e1b\u5217\u7d22\u5f15\u90fd\u76f8\u7b49\uff0c\u5373\u5c0d\u89d2\u7dda\u4e0a\u6240\u6709\u683c\u5b50\u7684 \\(row - col\\) \u70ba\u6046\u5b9a\u503c\u3002</p> <p>\u4e5f\u5c31\u662f\u8aaa\uff0c\u5982\u679c\u5169\u500b\u683c\u5b50\u6eff\u8db3 \\(row_1 - col_1 = row_2 - col_2\\) \uff0c\u5247\u5b83\u5011\u4e00\u5b9a\u8655\u5728\u540c\u4e00\u689d\u4e3b\u5c0d\u89d2\u7dda\u4e0a\u3002\u5229\u7528\u8a72\u898f\u5f8b\uff0c\u6211\u5011\u53ef\u4ee5\u85c9\u52a9\u5716 13-18 \u6240\u793a\u7684\u9663\u5217 <code>diags1</code> \u8a18\u9304\u6bcf\u689d\u4e3b\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\u3002</p> <p>\u540c\u7406\uff0c\u6b21\u5c0d\u89d2\u7dda\u4e0a\u7684\u6240\u6709\u683c\u5b50\u7684 \\(row + col\\) \u662f\u6046\u5b9a\u503c\u3002\u6211\u5011\u540c\u6a23\u4e5f\u53ef\u4ee5\u85c9\u52a9\u9663\u5217 <code>diags2</code> \u4f86\u8655\u7406\u6b21\u5c0d\u89d2\u7dda\u7d04\u675f\u3002</p> <p></p> <p> \u5716 13-18 \u00a0 \u8655\u7406\u5217\u7d04\u675f\u548c\u5c0d\u89d2\u7dda\u7d04\u675f </p>"},{"location":"chapter_backtracking/n_queens_problem/#3","title":"3. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"<p>\u8acb\u6ce8\u610f\uff0c\\(n\\) \u7dad\u65b9\u9663\u4e2d \\(row - col\\) \u7684\u7bc4\u570d\u662f \\([-n + 1, n - 1]\\) \uff0c\\(row + col\\) \u7684\u7bc4\u570d\u662f \\([0, 2n - 2]\\) \uff0c\u6240\u4ee5\u4e3b\u5c0d\u89d2\u7dda\u548c\u6b21\u5c0d\u89d2\u7dda\u7684\u6578\u91cf\u90fd\u70ba \\(2n - 1\\) \uff0c\u5373\u9663\u5217 <code>diags1</code> \u548c <code>diags2</code> \u7684\u9577\u5ea6\u90fd\u70ba \\(2n - 1\\) \u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig n_queens.py<pre><code>def backtrack(\n row: int,\n n: int,\n state: list[list[str]],\n res: list[list[list[str]]],\n cols: list[bool],\n diags1: list[bool],\n diags2: list[bool],\n):\n \"\"\"\u56de\u6eaf\u6f14\u7b97\u6cd5\uff1an \u7687\u540e\"\"\"\n # \u7576\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u6642\uff0c\u8a18\u9304\u89e3\n if row == n:\n res.append([list(row) for row in state])\n return\n # \u8d70\u8a2a\u6240\u6709\u5217\n for col in range(n):\n # \u8a08\u7b97\u8a72\u683c\u5b50\u5c0d\u61c9\u7684\u4e3b\u5c0d\u89d2\u7dda\u548c\u6b21\u5c0d\u89d2\u7dda\n diag1 = row - col + n - 1\n diag2 = row + col\n # \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8a72\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5c0d\u89d2\u7dda\u3001\u6b21\u5c0d\u89d2\u7dda\u4e0a\u5b58\u5728\u7687\u540e\n if not cols[col] and not diags1[diag1] and not diags2[diag2]:\n # \u5617\u8a66\uff1a\u5c07\u7687\u540e\u653e\u7f6e\u5728\u8a72\u683c\u5b50\n state[row][col] = \"Q\"\n cols[col] = diags1[diag1] = diags2[diag2] = True\n # \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2)\n # \u56de\u9000\uff1a\u5c07\u8a72\u683c\u5b50\u6062\u5fa9\u70ba\u7a7a\u4f4d\n state[row][col] = \"#\"\n cols[col] = diags1[diag1] = diags2[diag2] = False\n\ndef n_queens(n: int) -&gt; list[list[list[str]]]:\n \"\"\"\u6c42\u89e3 n \u7687\u540e\"\"\"\n # \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76e4\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n state = [[\"#\" for _ in range(n)] for _ in range(n)]\n cols = [False] * n # \u8a18\u9304\u5217\u662f\u5426\u6709\u7687\u540e\n diags1 = [False] * (2 * n - 1) # \u8a18\u9304\u4e3b\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n diags2 = [False] * (2 * n - 1) # \u8a18\u9304\u6b21\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n res = []\n backtrack(0, n, state, res, cols, diags1, diags2)\n\n return res\n</code></pre> n_queens.cpp<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid backtrack(int row, int n, vector&lt;vector&lt;string&gt;&gt; &amp;state, vector&lt;vector&lt;vector&lt;string&gt;&gt;&gt; &amp;res, vector&lt;bool&gt; &amp;cols,\n vector&lt;bool&gt; &amp;diags1, vector&lt;bool&gt; &amp;diags2) {\n // \u7576\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u6642\uff0c\u8a18\u9304\u89e3\n if (row == n) {\n res.push_back(state);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u5217\n for (int col = 0; col &lt; n; col++) {\n // \u8a08\u7b97\u8a72\u683c\u5b50\u5c0d\u61c9\u7684\u4e3b\u5c0d\u89d2\u7dda\u548c\u6b21\u5c0d\u89d2\u7dda\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8a72\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5c0d\u89d2\u7dda\u3001\u6b21\u5c0d\u89d2\u7dda\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2]) {\n // \u5617\u8a66\uff1a\u5c07\u7687\u540e\u653e\u7f6e\u5728\u8a72\u683c\u5b50\n state[row][col] = \"Q\";\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c07\u8a72\u683c\u5b50\u6062\u5fa9\u70ba\u7a7a\u4f4d\n state[row][col] = \"#\";\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nvector&lt;vector&lt;vector&lt;string&gt;&gt;&gt; nQueens(int n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76e4\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n vector&lt;vector&lt;string&gt;&gt; state(n, vector&lt;string&gt;(n, \"#\"));\n vector&lt;bool&gt; cols(n, false); // \u8a18\u9304\u5217\u662f\u5426\u6709\u7687\u540e\n vector&lt;bool&gt; diags1(2 * n - 1, false); // \u8a18\u9304\u4e3b\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n vector&lt;bool&gt; diags2(2 * n - 1, false); // \u8a18\u9304\u6b21\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n vector&lt;vector&lt;vector&lt;string&gt;&gt;&gt; res;\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n\n return res;\n}\n</code></pre> n_queens.java<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid backtrack(int row, int n, List&lt;List&lt;String&gt;&gt; state, List&lt;List&lt;List&lt;String&gt;&gt;&gt; res,\n boolean[] cols, boolean[] diags1, boolean[] diags2) {\n // \u7576\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u6642\uff0c\u8a18\u9304\u89e3\n if (row == n) {\n List&lt;List&lt;String&gt;&gt; copyState = new ArrayList&lt;&gt;();\n for (List&lt;String&gt; sRow : state) {\n copyState.add(new ArrayList&lt;&gt;(sRow));\n }\n res.add(copyState);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u5217\n for (int col = 0; col &lt; n; col++) {\n // \u8a08\u7b97\u8a72\u683c\u5b50\u5c0d\u61c9\u7684\u4e3b\u5c0d\u89d2\u7dda\u548c\u6b21\u5c0d\u89d2\u7dda\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8a72\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5c0d\u89d2\u7dda\u3001\u6b21\u5c0d\u89d2\u7dda\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2]) {\n // \u5617\u8a66\uff1a\u5c07\u7687\u540e\u653e\u7f6e\u5728\u8a72\u683c\u5b50\n state.get(row).set(col, \"Q\");\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c07\u8a72\u683c\u5b50\u6062\u5fa9\u70ba\u7a7a\u4f4d\n state.get(row).set(col, \"#\");\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nList&lt;List&lt;List&lt;String&gt;&gt;&gt; nQueens(int n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76e4\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n List&lt;List&lt;String&gt;&gt; state = new ArrayList&lt;&gt;();\n for (int i = 0; i &lt; n; i++) {\n List&lt;String&gt; row = new ArrayList&lt;&gt;();\n for (int j = 0; j &lt; n; j++) {\n row.add(\"#\");\n }\n state.add(row);\n }\n boolean[] cols = new boolean[n]; // \u8a18\u9304\u5217\u662f\u5426\u6709\u7687\u540e\n boolean[] diags1 = new boolean[2 * n - 1]; // \u8a18\u9304\u4e3b\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n boolean[] diags2 = new boolean[2 * n - 1]; // \u8a18\u9304\u6b21\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n List&lt;List&lt;List&lt;String&gt;&gt;&gt; res = new ArrayList&lt;&gt;();\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n\n return res;\n}\n</code></pre> n_queens.cs<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid Backtrack(int row, int n, List&lt;List&lt;string&gt;&gt; state, List&lt;List&lt;List&lt;string&gt;&gt;&gt; res,\n bool[] cols, bool[] diags1, bool[] diags2) {\n // \u7576\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u6642\uff0c\u8a18\u9304\u89e3\n if (row == n) {\n List&lt;List&lt;string&gt;&gt; copyState = [];\n foreach (List&lt;string&gt; sRow in state) {\n copyState.Add(new List&lt;string&gt;(sRow));\n }\n res.Add(copyState);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u5217\n for (int col = 0; col &lt; n; col++) {\n // \u8a08\u7b97\u8a72\u683c\u5b50\u5c0d\u61c9\u7684\u4e3b\u5c0d\u89d2\u7dda\u548c\u6b21\u5c0d\u89d2\u7dda\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8a72\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5c0d\u89d2\u7dda\u3001\u6b21\u5c0d\u89d2\u7dda\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2]) {\n // \u5617\u8a66\uff1a\u5c07\u7687\u540e\u653e\u7f6e\u5728\u8a72\u683c\u5b50\n state[row][col] = \"Q\";\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n Backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c07\u8a72\u683c\u5b50\u6062\u5fa9\u70ba\u7a7a\u4f4d\n state[row][col] = \"#\";\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nList&lt;List&lt;List&lt;string&gt;&gt;&gt; NQueens(int n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76e4\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n List&lt;List&lt;string&gt;&gt; state = [];\n for (int i = 0; i &lt; n; i++) {\n List&lt;string&gt; row = [];\n for (int j = 0; j &lt; n; j++) {\n row.Add(\"#\");\n }\n state.Add(row);\n }\n bool[] cols = new bool[n]; // \u8a18\u9304\u5217\u662f\u5426\u6709\u7687\u540e\n bool[] diags1 = new bool[2 * n - 1]; // \u8a18\u9304\u4e3b\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n bool[] diags2 = new bool[2 * n - 1]; // \u8a18\u9304\u6b21\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n List&lt;List&lt;List&lt;string&gt;&gt;&gt; res = [];\n\n Backtrack(0, n, state, res, cols, diags1, diags2);\n\n return res;\n}\n</code></pre> n_queens.go<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1an \u7687\u540e */\nfunc backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {\n // \u7576\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u6642\uff0c\u8a18\u9304\u89e3\n if row == n {\n newState := make([][]string, len(*state))\n for i, _ := range newState {\n newState[i] = make([]string, len((*state)[0]))\n copy(newState[i], (*state)[i])\n\n }\n *res = append(*res, newState)\n }\n // \u8d70\u8a2a\u6240\u6709\u5217\n for col := 0; col &lt; n; col++ {\n // \u8a08\u7b97\u8a72\u683c\u5b50\u5c0d\u61c9\u7684\u4e3b\u5c0d\u89d2\u7dda\u548c\u6b21\u5c0d\u89d2\u7dda\n diag1 := row - col + n - 1\n diag2 := row + col\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8a72\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5c0d\u89d2\u7dda\u3001\u6b21\u5c0d\u89d2\u7dda\u4e0a\u5b58\u5728\u7687\u540e\n if !(*cols)[col] &amp;&amp; !(*diags1)[diag1] &amp;&amp; !(*diags2)[diag2] {\n // \u5617\u8a66\uff1a\u5c07\u7687\u540e\u653e\u7f6e\u5728\u8a72\u683c\u5b50\n (*state)[row][col] = \"Q\"\n (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = true, true, true\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row+1, n, state, res, cols, diags1, diags2)\n // \u56de\u9000\uff1a\u5c07\u8a72\u683c\u5b50\u6062\u5fa9\u70ba\u7a7a\u4f4d\n (*state)[row][col] = \"#\"\n (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false\n }\n }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfunc nQueens(n int) [][][]string {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76e4\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n state := make([][]string, n)\n for i := 0; i &lt; n; i++ {\n row := make([]string, n)\n for i := 0; i &lt; n; i++ {\n row[i] = \"#\"\n }\n state[i] = row\n }\n // \u8a18\u9304\u5217\u662f\u5426\u6709\u7687\u540e\n cols := make([]bool, n)\n diags1 := make([]bool, 2*n-1)\n diags2 := make([]bool, 2*n-1)\n res := make([][][]string, 0)\n backtrack(0, n, &amp;state, &amp;res, &amp;cols, &amp;diags1, &amp;diags2)\n return res\n}\n</code></pre> n_queens.swift<pre><code>/* \u56de\u6eaf\u6f14\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 // \u7576\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u6642\uff0c\u8a18\u9304\u89e3\n if row == n {\n res.append(state)\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u5217\n for col in 0 ..&lt; n {\n // \u8a08\u7b97\u8a72\u683c\u5b50\u5c0d\u61c9\u7684\u4e3b\u5c0d\u89d2\u7dda\u548c\u6b21\u5c0d\u89d2\u7dda\n let diag1 = row - col + n - 1\n let diag2 = row + col\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8a72\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5c0d\u89d2\u7dda\u3001\u6b21\u5c0d\u89d2\u7dda\u4e0a\u5b58\u5728\u7687\u540e\n if !cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2] {\n // \u5617\u8a66\uff1a\u5c07\u7687\u540e\u653e\u7f6e\u5728\u8a72\u683c\u5b50\n state[row][col] = \"Q\"\n cols[col] = true\n diags1[diag1] = true\n diags2[diag2] = true\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row: row + 1, n: n, state: &amp;state, res: &amp;res, cols: &amp;cols, diags1: &amp;diags1, diags2: &amp;diags2)\n // \u56de\u9000\uff1a\u5c07\u8a72\u683c\u5b50\u6062\u5fa9\u70ba\u7a7a\u4f4d\n state[row][col] = \"#\"\n cols[col] = false\n diags1[diag1] = false\n diags2[diag2] = false\n }\n }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfunc nQueens(n: Int) -&gt; [[[String]]] {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76e4\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n var state = Array(repeating: Array(repeating: \"#\", count: n), count: n)\n var cols = Array(repeating: false, count: n) // \u8a18\u9304\u5217\u662f\u5426\u6709\u7687\u540e\n var diags1 = Array(repeating: false, count: 2 * n - 1) // \u8a18\u9304\u4e3b\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n var diags2 = Array(repeating: false, count: 2 * n - 1) // \u8a18\u9304\u6b21\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n var res: [[[String]]] = []\n\n backtrack(row: 0, n: n, state: &amp;state, res: &amp;res, cols: &amp;cols, diags1: &amp;diags1, diags2: &amp;diags2)\n\n return res\n}\n</code></pre> n_queens.js<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1an \u7687\u540e */\nfunction backtrack(row, n, state, res, cols, diags1, diags2) {\n // \u7576\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u6642\uff0c\u8a18\u9304\u89e3\n if (row === n) {\n res.push(state.map((row) =&gt; row.slice()));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u5217\n for (let col = 0; col &lt; n; col++) {\n // \u8a08\u7b97\u8a72\u683c\u5b50\u5c0d\u61c9\u7684\u4e3b\u5c0d\u89d2\u7dda\u548c\u6b21\u5c0d\u89d2\u7dda\n const diag1 = row - col + n - 1;\n const diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8a72\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5c0d\u89d2\u7dda\u3001\u6b21\u5c0d\u89d2\u7dda\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2]) {\n // \u5617\u8a66\uff1a\u5c07\u7687\u540e\u653e\u7f6e\u5728\u8a72\u683c\u5b50\n state[row][col] = 'Q';\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c07\u8a72\u683c\u5b50\u6062\u5fa9\u70ba\u7a7a\u4f4d\n state[row][col] = '#';\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfunction nQueens(n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76e4\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n const state = Array.from({ length: n }, () =&gt; Array(n).fill('#'));\n const cols = Array(n).fill(false); // \u8a18\u9304\u5217\u662f\u5426\u6709\u7687\u540e\n const diags1 = Array(2 * n - 1).fill(false); // \u8a18\u9304\u4e3b\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n const diags2 = Array(2 * n - 1).fill(false); // \u8a18\u9304\u6b21\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n const res = [];\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n return res;\n}\n</code></pre> n_queens.ts<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1an \u7687\u540e */\nfunction backtrack(\n row: number,\n n: number,\n state: string[][],\n res: string[][][],\n cols: boolean[],\n diags1: boolean[],\n diags2: boolean[]\n): void {\n // \u7576\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u6642\uff0c\u8a18\u9304\u89e3\n if (row === n) {\n res.push(state.map((row) =&gt; row.slice()));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u5217\n for (let col = 0; col &lt; n; col++) {\n // \u8a08\u7b97\u8a72\u683c\u5b50\u5c0d\u61c9\u7684\u4e3b\u5c0d\u89d2\u7dda\u548c\u6b21\u5c0d\u89d2\u7dda\n const diag1 = row - col + n - 1;\n const diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8a72\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5c0d\u89d2\u7dda\u3001\u6b21\u5c0d\u89d2\u7dda\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2]) {\n // \u5617\u8a66\uff1a\u5c07\u7687\u540e\u653e\u7f6e\u5728\u8a72\u683c\u5b50\n state[row][col] = 'Q';\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c07\u8a72\u683c\u5b50\u6062\u5fa9\u70ba\u7a7a\u4f4d\n state[row][col] = '#';\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfunction nQueens(n: number): string[][][] {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76e4\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n const state = Array.from({ length: n }, () =&gt; Array(n).fill('#'));\n const cols = Array(n).fill(false); // \u8a18\u9304\u5217\u662f\u5426\u6709\u7687\u540e\n const diags1 = Array(2 * n - 1).fill(false); // \u8a18\u9304\u4e3b\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n const diags2 = Array(2 * n - 1).fill(false); // \u8a18\u9304\u6b21\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n const res: string[][][] = [];\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n return res;\n}\n</code></pre> n_queens.dart<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid backtrack(\n int row,\n int n,\n List&lt;List&lt;String&gt;&gt; state,\n List&lt;List&lt;List&lt;String&gt;&gt;&gt; res,\n List&lt;bool&gt; cols,\n List&lt;bool&gt; diags1,\n List&lt;bool&gt; diags2,\n) {\n // \u7576\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u6642\uff0c\u8a18\u9304\u89e3\n if (row == n) {\n List&lt;List&lt;String&gt;&gt; copyState = [];\n for (List&lt;String&gt; sRow in state) {\n copyState.add(List.from(sRow));\n }\n res.add(copyState);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u5217\n for (int col = 0; col &lt; n; col++) {\n // \u8a08\u7b97\u8a72\u683c\u5b50\u5c0d\u61c9\u7684\u4e3b\u5c0d\u89d2\u7dda\u548c\u6b21\u5c0d\u89d2\u7dda\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8a72\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5c0d\u89d2\u7dda\u3001\u6b21\u5c0d\u89d2\u7dda\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2]) {\n // \u5617\u8a66\uff1a\u5c07\u7687\u540e\u653e\u7f6e\u5728\u8a72\u683c\u5b50\n state[row][col] = \"Q\";\n cols[col] = true;\n diags1[diag1] = true;\n diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c07\u8a72\u683c\u5b50\u6062\u5fa9\u70ba\u7a7a\u4f4d\n state[row][col] = \"#\";\n cols[col] = false;\n diags1[diag1] = false;\n diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nList&lt;List&lt;List&lt;String&gt;&gt;&gt; nQueens(int n) {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76e4\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n List&lt;List&lt;String&gt;&gt; state = List.generate(n, (index) =&gt; List.filled(n, \"#\"));\n List&lt;bool&gt; cols = List.filled(n, false); // \u8a18\u9304\u5217\u662f\u5426\u6709\u7687\u540e\n List&lt;bool&gt; diags1 = List.filled(2 * n - 1, false); // \u8a18\u9304\u4e3b\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n List&lt;bool&gt; diags2 = List.filled(2 * n - 1, false); // \u8a18\u9304\u6b21\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n List&lt;List&lt;List&lt;String&gt;&gt;&gt; res = [];\n\n backtrack(0, n, state, res, cols, diags1, diags2);\n\n return res;\n}\n</code></pre> n_queens.rs<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1an \u7687\u540e */\nfn backtrack(\n row: usize,\n n: usize,\n state: &amp;mut Vec&lt;Vec&lt;String&gt;&gt;,\n res: &amp;mut Vec&lt;Vec&lt;Vec&lt;String&gt;&gt;&gt;,\n cols: &amp;mut [bool],\n diags1: &amp;mut [bool],\n diags2: &amp;mut [bool],\n) {\n // \u7576\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u6642\uff0c\u8a18\u9304\u89e3\n if row == n {\n let mut copy_state: Vec&lt;Vec&lt;String&gt;&gt; = Vec::new();\n for s_row in state.clone() {\n copy_state.push(s_row);\n }\n res.push(copy_state);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u5217\n for col in 0..n {\n // \u8a08\u7b97\u8a72\u683c\u5b50\u5c0d\u61c9\u7684\u4e3b\u5c0d\u89d2\u7dda\u548c\u6b21\u5c0d\u89d2\u7dda\n let diag1 = row + n - 1 - col;\n let diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8a72\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5c0d\u89d2\u7dda\u3001\u6b21\u5c0d\u89d2\u7dda\u4e0a\u5b58\u5728\u7687\u540e\n if !cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2] {\n // \u5617\u8a66\uff1a\u5c07\u7687\u540e\u653e\u7f6e\u5728\u8a72\u683c\u5b50\n state.get_mut(row).unwrap()[col] = \"Q\".into();\n (cols[col], diags1[diag1], diags2[diag2]) = (true, true, true);\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c07\u8a72\u683c\u5b50\u6062\u5fa9\u70ba\u7a7a\u4f4d\n state.get_mut(row).unwrap()[col] = \"#\".into();\n (cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);\n }\n }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfn n_queens(n: usize) -&gt; Vec&lt;Vec&lt;Vec&lt;String&gt;&gt;&gt; {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76e4\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n let mut state: Vec&lt;Vec&lt;String&gt;&gt; = Vec::new();\n for _ in 0..n {\n let mut row: Vec&lt;String&gt; = Vec::new();\n for _ in 0..n {\n row.push(\"#\".into());\n }\n state.push(row);\n }\n let mut cols = vec![false; n]; // \u8a18\u9304\u5217\u662f\u5426\u6709\u7687\u540e\n let mut diags1 = vec![false; 2 * n - 1]; // \u8a18\u9304\u4e3b\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n let mut diags2 = vec![false; 2 * n - 1]; // \u8a18\u9304\u6b21\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n let mut res: Vec&lt;Vec&lt;Vec&lt;String&gt;&gt;&gt; = Vec::new();\n\n backtrack(\n 0,\n n,\n &amp;mut state,\n &amp;mut res,\n &amp;mut cols,\n &amp;mut diags1,\n &amp;mut diags2,\n );\n\n res\n}\n</code></pre> n_queens.c<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1an \u7687\u540e */\nvoid backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int *resSize, bool cols[MAX_SIZE],\n bool diags1[2 * MAX_SIZE - 1], bool diags2[2 * MAX_SIZE - 1]) {\n // \u7576\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u6642\uff0c\u8a18\u9304\u89e3\n if (row == n) {\n res[*resSize] = (char **)malloc(sizeof(char *) * n);\n for (int i = 0; i &lt; n; ++i) {\n res[*resSize][i] = (char *)malloc(sizeof(char) * (n + 1));\n strcpy(res[*resSize][i], state[i]);\n }\n (*resSize)++;\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u5217\n for (int col = 0; col &lt; n; col++) {\n // \u8a08\u7b97\u8a72\u683c\u5b50\u5c0d\u61c9\u7684\u4e3b\u5c0d\u89d2\u7dda\u548c\u6b21\u5c0d\u89d2\u7dda\n int diag1 = row - col + n - 1;\n int diag2 = row + col;\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8a72\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5c0d\u89d2\u7dda\u3001\u6b21\u5c0d\u89d2\u7dda\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2]) {\n // \u5617\u8a66\uff1a\u5c07\u7687\u540e\u653e\u7f6e\u5728\u8a72\u683c\u5b50\n state[row][col] = 'Q';\n cols[col] = diags1[diag1] = diags2[diag2] = true;\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, resSize, cols, diags1, diags2);\n // \u56de\u9000\uff1a\u5c07\u8a72\u683c\u5b50\u6062\u5fa9\u70ba\u7a7a\u4f4d\n state[row][col] = '#';\n cols[col] = diags1[diag1] = diags2[diag2] = false;\n }\n }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nchar ***nQueens(int n, int *returnSize) {\n char state[MAX_SIZE][MAX_SIZE];\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76e4\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n for (int i = 0; i &lt; n; ++i) {\n for (int j = 0; j &lt; n; ++j) {\n state[i][j] = '#';\n }\n state[i][n] = '\\0';\n }\n bool cols[MAX_SIZE] = {false}; // \u8a18\u9304\u5217\u662f\u5426\u6709\u7687\u540e\n bool diags1[2 * MAX_SIZE - 1] = {false}; // \u8a18\u9304\u4e3b\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n bool diags2[2 * MAX_SIZE - 1] = {false}; // \u8a18\u9304\u6b21\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n\n char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);\n *returnSize = 0;\n backtrack(0, n, state, res, returnSize, cols, diags1, diags2);\n return res;\n}\n</code></pre> n_queens.kt<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1an \u7687\u540e */\nfun backtrack(\n row: Int,\n n: Int,\n state: MutableList&lt;MutableList&lt;String&gt;&gt;,\n res: MutableList&lt;MutableList&lt;MutableList&lt;String&gt;&gt;?&gt;,\n cols: BooleanArray,\n diags1: BooleanArray,\n diags2: BooleanArray\n) {\n // \u7576\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u6642\uff0c\u8a18\u9304\u89e3\n if (row == n) {\n val copyState = mutableListOf&lt;MutableList&lt;String&gt;&gt;()\n for (sRow in state) {\n copyState.add(sRow.toMutableList())\n }\n res.add(copyState)\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u5217\n for (col in 0..&lt;n) {\n // \u8a08\u7b97\u8a72\u683c\u5b50\u5c0d\u61c9\u7684\u4e3b\u5c0d\u89d2\u7dda\u548c\u6b21\u5c0d\u89d2\u7dda\n val diag1 = row - col + n - 1\n val diag2 = row + col\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8a72\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5c0d\u89d2\u7dda\u3001\u6b21\u5c0d\u89d2\u7dda\u4e0a\u5b58\u5728\u7687\u540e\n if (!cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2]) {\n // \u5617\u8a66\uff1a\u5c07\u7687\u540e\u653e\u7f6e\u5728\u8a72\u683c\u5b50\n state[row][col] = \"Q\"\n diags2[diag2] = true\n diags1[diag1] = diags2[diag2]\n cols[col] = diags1[diag1]\n // \u653e\u7f6e\u4e0b\u4e00\u884c\n backtrack(row + 1, n, state, res, cols, diags1, diags2)\n // \u56de\u9000\uff1a\u5c07\u8a72\u683c\u5b50\u6062\u5fa9\u70ba\u7a7a\u4f4d\n state[row][col] = \"#\"\n diags2[diag2] = false\n diags1[diag1] = diags2[diag2]\n cols[col] = diags1[diag1]\n }\n }\n}\n\n/* \u6c42\u89e3 n \u7687\u540e */\nfun nQueens(n: Int): MutableList&lt;MutableList&lt;MutableList&lt;String&gt;&gt;?&gt; {\n // \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76e4\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\n val state = mutableListOf&lt;MutableList&lt;String&gt;&gt;()\n for (i in 0..&lt;n) {\n val row = mutableListOf&lt;String&gt;()\n for (j in 0..&lt;n) {\n row.add(\"#\")\n }\n state.add(row)\n }\n val cols = BooleanArray(n) // \u8a18\u9304\u5217\u662f\u5426\u6709\u7687\u540e\n val diags1 = BooleanArray(2 * n - 1) // \u8a18\u9304\u4e3b\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n val diags2 = BooleanArray(2 * n - 1) // \u8a18\u9304\u6b21\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u6709\u7687\u540e\n val res = mutableListOf&lt;MutableList&lt;MutableList&lt;String&gt;&gt;?&gt;()\n\n backtrack(0, n, state, res, cols, diags1, diags2)\n\n return res\n}\n</code></pre> n_queens.rb<pre><code>[class]{}-[func]{backtrack}\n\n[class]{}-[func]{n_queens}\n</code></pre> n_queens.zig<pre><code>[class]{}-[func]{backtrack}\n\n[class]{}-[func]{nQueens}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u9010\u884c\u653e\u7f6e \\(n\\) \u6b21\uff0c\u8003\u616e\u5217\u7d04\u675f\uff0c\u5247\u5f9e\u7b2c\u4e00\u884c\u5230\u6700\u5f8c\u4e00\u884c\u5206\u5225\u6709 \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u500b\u9078\u64c7\uff0c\u4f7f\u7528 \\(O(n!)\\) \u6642\u9593\u3002\u7576\u8a18\u9304\u89e3\u6642\uff0c\u9700\u8981\u8907\u88fd\u77e9\u9663 <code>state</code> \u4e26\u65b0\u589e\u9032 <code>res</code> \uff0c\u8907\u88fd\u64cd\u4f5c\u4f7f\u7528 \\(O(n^2)\\) \u6642\u9593\u3002\u56e0\u6b64\uff0c\u7e3d\u9ad4\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n! \\cdot n^2)\\) \u3002\u5be6\u969b\u4e0a\uff0c\u6839\u64da\u5c0d\u89d2\u7dda\u7d04\u675f\u7684\u526a\u679d\u4e5f\u80fd\u5920\u5927\u5e45\u7e2e\u5c0f\u641c\u5c0b\u7a7a\u9593\uff0c\u56e0\u800c\u641c\u5c0b\u6548\u7387\u5f80\u5f80\u512a\u65bc\u4ee5\u4e0a\u6642\u9593\u8907\u96dc\u5ea6\u3002</p> <p>\u9663\u5217 <code>state</code> \u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u9593\uff0c\u9663\u5217 <code>cols</code>\u3001<code>diags1</code> \u548c <code>diags2</code> \u7686\u4f7f\u7528 \\(O(n)\\) \u7a7a\u9593\u3002\u6700\u5927\u905e\u8ff4\u6df1\u5ea6\u70ba \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u5806\u758a\u5e40\u7a7a\u9593\u3002\u56e0\u6b64\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\) \u3002</p>"},{"location":"chapter_backtracking/permutations_problem/","title":"13.2 \u00a0 \u5168\u6392\u5217\u554f\u984c","text":"<p>\u5168\u6392\u5217\u554f\u984c\u662f\u56de\u6eaf\u6f14\u7b97\u6cd5\u7684\u4e00\u500b\u5178\u578b\u61c9\u7528\u3002\u5b83\u7684\u5b9a\u7fa9\u662f\u5728\u7d66\u5b9a\u4e00\u500b\u96c6\u5408\uff08\u5982\u4e00\u500b\u9663\u5217\u6216\u5b57\u4e32\uff09\u7684\u60c5\u6cc1\u4e0b\uff0c\u627e\u51fa\u5176\u4e2d\u5143\u7d20\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002</p> <p>\u8868 13-2 \u5217\u8209\u4e86\u5e7e\u500b\u793a\u4f8b\u8cc7\u6599\uff0c\u5305\u62ec\u8f38\u5165\u9663\u5217\u548c\u5c0d\u61c9\u7684\u6240\u6709\u6392\u5217\u3002</p> <p> \u8868 13-2 \u00a0 \u5168\u6392\u5217\u793a\u4f8b </p> \u8f38\u5165\u9663\u5217 \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 \u7121\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u6cc1","text":"<p>Question</p> <p>\u8f38\u5165\u4e00\u500b\u6574\u6578\u9663\u5217\uff0c\u5176\u4e2d\u4e0d\u5305\u542b\u91cd\u8907\u5143\u7d20\uff0c\u8fd4\u56de\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002</p> <p>\u5f9e\u56de\u6eaf\u6f14\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u6211\u5011\u53ef\u4ee5\u628a\u751f\u6210\u6392\u5217\u7684\u904e\u7a0b\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u9078\u64c7\u7684\u7d50\u679c\u3002\u5047\u8a2d\u8f38\u5165\u9663\u5217\u70ba \\([1, 2, 3]\\) \uff0c\u5982\u679c\u6211\u5011\u5148\u9078\u64c7 \\(1\\) \uff0c\u518d\u9078\u64c7 \\(3\\) \uff0c\u6700\u5f8c\u9078\u64c7 \\(2\\) \uff0c\u5247\u7372\u5f97\u6392\u5217 \\([1, 3, 2]\\) \u3002\u56de\u9000\u8868\u793a\u64a4\u92b7\u4e00\u500b\u9078\u64c7\uff0c\u4e4b\u5f8c\u7e7c\u7e8c\u5617\u8a66\u5176\u4ed6\u9078\u64c7\u3002</p> <p>\u5f9e\u56de\u6eaf\u7a0b\u5f0f\u78bc\u7684\u89d2\u5ea6\u770b\uff0c\u5019\u9078\u96c6\u5408 <code>choices</code> \u662f\u8f38\u5165\u9663\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u72c0\u614b <code>state</code> \u662f\u76f4\u81f3\u76ee\u524d\u5df2\u88ab\u9078\u64c7\u7684\u5143\u7d20\u3002\u8acb\u6ce8\u610f\uff0c\u6bcf\u500b\u5143\u7d20\u53ea\u5141\u8a31\u88ab\u9078\u64c7\u4e00\u6b21\uff0c\u56e0\u6b64 <code>state</code> \u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u61c9\u8a72\u662f\u552f\u4e00\u7684\u3002</p> <p>\u5982\u5716 13-5 \u6240\u793a\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u641c\u5c0b\u904e\u7a0b\u5c55\u958b\u6210\u4e00\u68f5\u905e\u8ff4\u6a39\uff0c\u6a39\u4e2d\u7684\u6bcf\u500b\u7bc0\u9ede\u4ee3\u8868\u7576\u524d\u72c0\u614b <code>state</code> \u3002\u5f9e\u6839\u7bc0\u9ede\u958b\u59cb\uff0c\u7d93\u904e\u4e09\u8f2a\u9078\u64c7\u5f8c\u5230\u9054\u8449\u7bc0\u9ede\uff0c\u6bcf\u500b\u8449\u7bc0\u9ede\u90fd\u5c0d\u61c9\u4e00\u500b\u6392\u5217\u3002</p> <p></p> <p> \u5716 13-5 \u00a0 \u5168\u6392\u5217\u7684\u905e\u8ff4\u6a39 </p>"},{"location":"chapter_backtracking/permutations_problem/#1","title":"1. \u00a0 \u91cd\u8907\u9078\u64c7\u526a\u679d","text":"<p>\u70ba\u4e86\u5be6\u73fe\u6bcf\u500b\u5143\u7d20\u53ea\u88ab\u9078\u64c7\u4e00\u6b21\uff0c\u6211\u5011\u8003\u616e\u5f15\u5165\u4e00\u500b\u5e03\u6797\u578b\u9663\u5217 <code>selected</code> \uff0c\u5176\u4e2d <code>selected[i]</code> \u8868\u793a <code>choices[i]</code> \u662f\u5426\u5df2\u88ab\u9078\u64c7\uff0c\u4e26\u57fa\u65bc\u5b83\u5be6\u73fe\u4ee5\u4e0b\u526a\u679d\u64cd\u4f5c\u3002</p> <ul> <li>\u5728\u505a\u51fa\u9078\u64c7 <code>choice[i]</code> \u5f8c\uff0c\u6211\u5011\u5c31\u5c07 <code>selected[i]</code> \u8ce6\u503c\u70ba \\(\\text{True}\\) \uff0c\u4ee3\u8868\u5b83\u5df2\u88ab\u9078\u64c7\u3002</li> <li>\u8d70\u8a2a\u9078\u64c7\u4e32\u5217 <code>choices</code> \u6642\uff0c\u8df3\u904e\u6240\u6709\u5df2\u88ab\u9078\u64c7\u7684\u7bc0\u9ede\uff0c\u5373\u526a\u679d\u3002</li> </ul> <p>\u5982\u5716 13-6 \u6240\u793a\uff0c\u5047\u8a2d\u6211\u5011\u7b2c\u4e00\u8f2a\u9078\u64c7 1 \uff0c\u7b2c\u4e8c\u8f2a\u9078\u64c7 3 \uff0c\u7b2c\u4e09\u8f2a\u9078\u64c7 2 \uff0c\u5247\u9700\u8981\u5728\u7b2c\u4e8c\u8f2a\u526a\u6389\u5143\u7d20 1 \u7684\u5206\u652f\uff0c\u5728\u7b2c\u4e09\u8f2a\u526a\u6389\u5143\u7d20 1 \u548c\u5143\u7d20 3 \u7684\u5206\u652f\u3002</p> <p></p> <p> \u5716 13-6 \u00a0 \u5168\u6392\u5217\u526a\u679d\u793a\u4f8b </p> <p>\u89c0\u5bdf\u5716 13-6 \u767c\u73fe\uff0c\u8a72\u526a\u679d\u64cd\u4f5c\u5c07\u641c\u5c0b\u7a7a\u9593\u5927\u5c0f\u5f9e \\(O(n^n)\\) \u6e1b\u5c0f\u81f3 \\(O(n!)\\) \u3002</p>"},{"location":"chapter_backtracking/permutations_problem/#2","title":"2. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"<p>\u60f3\u6e05\u695a\u4ee5\u4e0a\u8cc7\u8a0a\u4e4b\u5f8c\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u5728\u6846\u67b6\u7a0b\u5f0f\u78bc\u4e2d\u505a\u201c\u5b8c\u5f62\u586b\u7a7a\u201d\u4e86\u3002\u70ba\u4e86\u7e2e\u77ed\u6574\u9ad4\u7a0b\u5f0f\u78bc\uff0c\u6211\u5011\u4e0d\u55ae\u7368\u5be6\u73fe\u6846\u67b6\u7a0b\u5f0f\u78bc\u4e2d\u7684\u5404\u500b\u51fd\u5f0f\uff0c\u800c\u662f\u5c07\u5b83\u5011\u5c55\u958b\u5728 <code>backtrack()</code> \u51fd\u5f0f\u4e2d\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig permutations_i.py<pre><code>def backtrack(\n state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n \"\"\"\u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I\"\"\"\n # \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if len(state) == len(choices):\n res.append(list(state))\n return\n # \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for i, choice in enumerate(choices):\n # \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20\n if not selected[i]:\n # \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n selected[i] = True\n state.append(choice)\n # \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, choices, selected, res)\n # \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = False\n state.pop()\n\ndef permutations_i(nums: list[int]) -&gt; list[list[int]]:\n \"\"\"\u5168\u6392\u5217 I\"\"\"\n res = []\n backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n return res\n</code></pre> permutations_i.cpp<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(vector&lt;int&gt; &amp;state, const vector&lt;int&gt; &amp;choices, vector&lt;bool&gt; &amp;selected, vector&lt;vector&lt;int&gt;&gt; &amp;res) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (state.size() == choices.size()) {\n res.push_back(state);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (int i = 0; i &lt; choices.size(); i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20\n if (!selected[i]) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n selected[i] = true;\n state.push_back(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n state.pop_back();\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nvector&lt;vector&lt;int&gt;&gt; permutationsI(vector&lt;int&gt; nums) {\n vector&lt;int&gt; state;\n vector&lt;bool&gt; selected(nums.size(), false);\n vector&lt;vector&lt;int&gt;&gt; res;\n backtrack(state, nums, selected, res);\n return res;\n}\n</code></pre> permutations_i.java<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(List&lt;Integer&gt; state, int[] choices, boolean[] selected, List&lt;List&lt;Integer&gt;&gt; res) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (state.size() == choices.length) {\n res.add(new ArrayList&lt;Integer&gt;(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (int i = 0; i &lt; choices.length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20\n if (!selected[i]) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n selected[i] = true;\n state.add(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n state.remove(state.size() - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nList&lt;List&lt;Integer&gt;&gt; permutationsI(int[] nums) {\n List&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;List&lt;Integer&gt;&gt;();\n backtrack(new ArrayList&lt;Integer&gt;(), nums, new boolean[nums.length], res);\n return res;\n}\n</code></pre> permutations_i.cs<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid Backtrack(List&lt;int&gt; state, int[] choices, bool[] selected, List&lt;List&lt;int&gt;&gt; res) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (state.Count == choices.Length) {\n res.Add(new List&lt;int&gt;(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (int i = 0; i &lt; choices.Length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20\n if (!selected[i]) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n selected[i] = true;\n state.Add(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n Backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n state.RemoveAt(state.Count - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nList&lt;List&lt;int&gt;&gt; PermutationsI(int[] nums) {\n List&lt;List&lt;int&gt;&gt; res = [];\n Backtrack([], nums, new bool[nums.Length], res);\n return res;\n}\n</code></pre> permutations_i.go<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunc backtrackI(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if len(*state) == len(*choices) {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for i := 0; i &lt; len(*choices); i++ {\n choice := (*choices)[i]\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20\n if !(*selected)[i] {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n (*selected)[i] = true\n *state = append(*state, choice)\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrackI(state, choices, selected, res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n (*selected)[i] = false\n *state = (*state)[:len(*state)-1]\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums []int) [][]int {\n res := make([][]int, 0)\n state := make([]int, 0)\n selected := make([]bool, len(nums))\n backtrackI(&amp;state, &amp;nums, &amp;selected, &amp;res)\n return res\n}\n</code></pre> permutations_i.swift<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if state.count == choices.count {\n res.append(state)\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (i, choice) in choices.enumerated() {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20\n if !selected[i] {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n selected[i] = true\n state.append(choice)\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state: &amp;state, choices: choices, selected: &amp;selected, res: &amp;res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false\n state.removeLast()\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums: [Int]) -&gt; [[Int]] {\n var state: [Int] = []\n var selected = Array(repeating: false, count: nums.count)\n var res: [[Int]] = []\n backtrack(state: &amp;state, choices: nums, selected: &amp;selected, res: &amp;res)\n return res\n}\n</code></pre> permutations_i.js<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunction backtrack(state, choices, selected, res) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (state.length === choices.length) {\n res.push([...state]);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n choices.forEach((choice, i) =&gt; {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20\n if (!selected[i]) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n selected[i] = true;\n state.push(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n state.pop();\n }\n });\n}\n\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums) {\n const res = [];\n backtrack([], nums, Array(nums.length).fill(false), res);\n return res;\n}\n</code></pre> permutations_i.ts<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunction backtrack(\n state: number[],\n choices: number[],\n selected: boolean[],\n res: number[][]\n): void {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (state.length === choices.length) {\n res.push([...state]);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n choices.forEach((choice, i) =&gt; {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20\n if (!selected[i]) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n selected[i] = true;\n state.push(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n state.pop();\n }\n });\n}\n\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums: number[]): number[][] {\n const res: number[][] = [];\n backtrack([], nums, Array(nums.length).fill(false), res);\n return res;\n}\n</code></pre> permutations_i.dart<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(\n List&lt;int&gt; state,\n List&lt;int&gt; choices,\n List&lt;bool&gt; selected,\n List&lt;List&lt;int&gt;&gt; res,\n) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (state.length == choices.length) {\n res.add(List.from(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (int i = 0; i &lt; choices.length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20\n if (!selected[i]) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n selected[i] = true;\n state.add(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n state.removeLast();\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nList&lt;List&lt;int&gt;&gt; permutationsI(List&lt;int&gt; nums) {\n List&lt;List&lt;int&gt;&gt; res = [];\n backtrack([], nums, List.filled(nums.length, false), res);\n return res;\n}\n</code></pre> permutations_i.rs<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfn backtrack(mut state: Vec&lt;i32&gt;, choices: &amp;[i32], selected: &amp;mut [bool], res: &amp;mut Vec&lt;Vec&lt;i32&gt;&gt;) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if state.len() == choices.len() {\n res.push(state);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for i in 0..choices.len() {\n let choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20\n if !selected[i] {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n selected[i] = true;\n state.push(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state.clone(), choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n state.remove(state.len() - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nfn permutations_i(nums: &amp;mut [i32]) -&gt; Vec&lt;Vec&lt;i32&gt;&gt; {\n let mut res = Vec::new(); // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n backtrack(Vec::new(), nums, &amp;mut vec![false; nums.len()], &amp;mut res);\n res\n}\n</code></pre> permutations_i.c<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (stateSize == choicesSize) {\n res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n for (int i = 0; i &lt; choicesSize; i++) {\n res[*resSize][i] = state[i];\n }\n (*resSize)++;\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (int i = 0; i &lt; choicesSize; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20\n if (!selected[i]) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n selected[i] = true;\n state[stateSize] = choice;\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nint **permutationsI(int *nums, int numsSize, int *returnSize) {\n int *state = (int *)malloc(numsSize * sizeof(int));\n bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n for (int i = 0; i &lt; numsSize; i++) {\n selected[i] = false;\n }\n int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n *returnSize = 0;\n\n backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n free(state);\n free(selected);\n\n return res;\n}\n</code></pre> permutations_i.kt<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfun backtrack(\n state: MutableList&lt;Int&gt;,\n choices: IntArray,\n selected: BooleanArray,\n res: MutableList&lt;MutableList&lt;Int&gt;?&gt;\n) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (state.size == choices.size) {\n res.add(state.toMutableList())\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (i in choices.indices) {\n val choice = choices[i]\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20\n if (!selected[i]) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n selected[i] = true\n state.add(choice)\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, choices, selected, res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false\n state.removeAt(state.size - 1)\n }\n }\n}\n\n/* \u5168\u6392\u5217 I */\nfun permutationsI(nums: IntArray): MutableList&lt;MutableList&lt;Int&gt;?&gt; {\n val res = mutableListOf&lt;MutableList&lt;Int&gt;?&gt;()\n backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)\n return res\n}\n</code></pre> permutations_i.rb<pre><code>[class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutations_i}\n</code></pre> permutations_i.zig<pre><code>[class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsI}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_backtracking/permutations_problem/#1322","title":"13.2.2 \u00a0 \u8003\u616e\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u6cc1","text":"<p>Question</p> <p>\u8f38\u5165\u4e00\u500b\u6574\u6578\u9663\u5217\uff0c\u9663\u5217\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u8907\u5143\u7d20\uff0c\u8fd4\u56de\u6240\u6709\u4e0d\u91cd\u8907\u7684\u6392\u5217\u3002</p> <p>\u5047\u8a2d\u8f38\u5165\u9663\u5217\u70ba \\([1, 1, 2]\\) \u3002\u70ba\u4e86\u65b9\u4fbf\u5340\u5206\u5169\u500b\u91cd\u8907\u5143\u7d20 \\(1\\) \uff0c\u6211\u5011\u5c07\u7b2c\u4e8c\u500b \\(1\\) \u8a18\u70ba \\(\\hat{1}\\) \u3002</p> <p>\u5982\u5716 13-7 \u6240\u793a\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u751f\u6210\u7684\u6392\u5217\u6709\u4e00\u534a\u662f\u91cd\u8907\u7684\u3002</p> <p></p> <p> \u5716 13-7 \u00a0 \u91cd\u8907\u6392\u5217 </p> <p>\u90a3\u9ebc\u5982\u4f55\u53bb\u9664\u91cd\u8907\u7684\u6392\u5217\u5462\uff1f\u6700\u76f4\u63a5\u5730\uff0c\u8003\u616e\u85c9\u52a9\u4e00\u500b\u96dc\u6e4a\u96c6\u5408\uff0c\u76f4\u63a5\u5c0d\u6392\u5217\u7d50\u679c\u9032\u884c\u53bb\u91cd\u3002\u7136\u800c\u9019\u6a23\u505a\u4e0d\u5920\u512a\u96c5\uff0c\u56e0\u70ba\u751f\u6210\u91cd\u8907\u6392\u5217\u7684\u641c\u5c0b\u5206\u652f\u6c92\u6709\u5fc5\u8981\uff0c\u61c9\u7576\u63d0\u524d\u8b58\u5225\u4e26\u526a\u679d\uff0c\u9019\u6a23\u53ef\u4ee5\u9032\u4e00\u6b65\u63d0\u5347\u6f14\u7b97\u6cd5\u6548\u7387\u3002</p>"},{"location":"chapter_backtracking/permutations_problem/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"<p>\u89c0\u5bdf\u5716 13-8 \uff0c\u5728\u7b2c\u4e00\u8f2a\u4e2d\uff0c\u9078\u64c7 \\(1\\) \u6216\u9078\u64c7 \\(\\hat{1}\\) \u662f\u7b49\u50f9\u7684\uff0c\u5728\u9019\u5169\u500b\u9078\u64c7\u4e4b\u4e0b\u751f\u6210\u7684\u6240\u6709\u6392\u5217\u90fd\u662f\u91cd\u8907\u7684\u3002\u56e0\u6b64\u61c9\u8a72\u628a \\(\\hat{1}\\) \u526a\u679d\u3002</p> <p>\u540c\u7406\uff0c\u5728\u7b2c\u4e00\u8f2a\u9078\u64c7 \\(2\\) \u4e4b\u5f8c\uff0c\u7b2c\u4e8c\u8f2a\u9078\u64c7\u4e2d\u7684 \\(1\\) \u548c \\(\\hat{1}\\) \u4e5f\u6703\u7522\u751f\u91cd\u8907\u5206\u652f\uff0c\u56e0\u6b64\u4e5f\u61c9\u5c07\u7b2c\u4e8c\u8f2a\u7684 \\(\\hat{1}\\) \u526a\u679d\u3002</p> <p>\u5f9e\u672c\u8cea\u4e0a\u770b\uff0c\u6211\u5011\u7684\u76ee\u6a19\u662f\u5728\u67d0\u4e00\u8f2a\u9078\u64c7\u4e2d\uff0c\u4fdd\u8b49\u591a\u500b\u76f8\u7b49\u7684\u5143\u7d20\u50c5\u88ab\u9078\u64c7\u4e00\u6b21\u3002</p> <p></p> <p> \u5716 13-8 \u00a0 \u91cd\u8907\u6392\u5217\u526a\u679d </p>"},{"location":"chapter_backtracking/permutations_problem/#2_1","title":"2. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"<p>\u5728\u4e0a\u4e00\u984c\u7684\u7a0b\u5f0f\u78bc\u7684\u57fa\u790e\u4e0a\uff0c\u6211\u5011\u8003\u616e\u5728\u6bcf\u4e00\u8f2a\u9078\u64c7\u4e2d\u958b\u555f\u4e00\u500b\u96dc\u6e4a\u96c6\u5408 <code>duplicated</code> \uff0c\u7528\u65bc\u8a18\u9304\u8a72\u8f2a\u4e2d\u5df2\u7d93\u5617\u8a66\u904e\u7684\u5143\u7d20\uff0c\u4e26\u5c07\u91cd\u8907\u5143\u7d20\u526a\u679d\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig permutations_ii.py<pre><code>def backtrack(\n state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n \"\"\"\u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II\"\"\"\n # \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if len(state) == len(choices):\n res.append(list(state))\n return\n # \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n duplicated = set[int]()\n for i, choice in enumerate(choices):\n # \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20 \u4e14 \u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u76f8\u7b49\u5143\u7d20\n if not selected[i] and choice not in duplicated:\n # \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n duplicated.add(choice) # \u8a18\u9304\u9078\u64c7\u904e\u7684\u5143\u7d20\u503c\n selected[i] = True\n state.append(choice)\n # \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, choices, selected, res)\n # \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = False\n state.pop()\n\ndef permutations_ii(nums: list[int]) -&gt; list[list[int]]:\n \"\"\"\u5168\u6392\u5217 II\"\"\"\n res = []\n backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n return res\n</code></pre> permutations_ii.cpp<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(vector&lt;int&gt; &amp;state, const vector&lt;int&gt; &amp;choices, vector&lt;bool&gt; &amp;selected, vector&lt;vector&lt;int&gt;&gt; &amp;res) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (state.size() == choices.size()) {\n res.push_back(state);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n unordered_set&lt;int&gt; duplicated;\n for (int i = 0; i &lt; choices.size(); i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20 \u4e14 \u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u76f8\u7b49\u5143\u7d20\n if (!selected[i] &amp;&amp; duplicated.find(choice) == duplicated.end()) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n duplicated.emplace(choice); // \u8a18\u9304\u9078\u64c7\u904e\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.push_back(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n state.pop_back();\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nvector&lt;vector&lt;int&gt;&gt; permutationsII(vector&lt;int&gt; nums) {\n vector&lt;int&gt; state;\n vector&lt;bool&gt; selected(nums.size(), false);\n vector&lt;vector&lt;int&gt;&gt; res;\n backtrack(state, nums, selected, res);\n return res;\n}\n</code></pre> permutations_ii.java<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(List&lt;Integer&gt; state, int[] choices, boolean[] selected, List&lt;List&lt;Integer&gt;&gt; res) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (state.size() == choices.length) {\n res.add(new ArrayList&lt;Integer&gt;(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n Set&lt;Integer&gt; duplicated = new HashSet&lt;Integer&gt;();\n for (int i = 0; i &lt; choices.length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20 \u4e14 \u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u76f8\u7b49\u5143\u7d20\n if (!selected[i] &amp;&amp; !duplicated.contains(choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n duplicated.add(choice); // \u8a18\u9304\u9078\u64c7\u904e\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.add(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n state.remove(state.size() - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nList&lt;List&lt;Integer&gt;&gt; permutationsII(int[] nums) {\n List&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;List&lt;Integer&gt;&gt;();\n backtrack(new ArrayList&lt;Integer&gt;(), nums, new boolean[nums.length], res);\n return res;\n}\n</code></pre> permutations_ii.cs<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid Backtrack(List&lt;int&gt; state, int[] choices, bool[] selected, List&lt;List&lt;int&gt;&gt; res) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (state.Count == choices.Length) {\n res.Add(new List&lt;int&gt;(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n HashSet&lt;int&gt; duplicated = [];\n for (int i = 0; i &lt; choices.Length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20 \u4e14 \u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u76f8\u7b49\u5143\u7d20\n if (!selected[i] &amp;&amp; !duplicated.Contains(choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n duplicated.Add(choice); // \u8a18\u9304\u9078\u64c7\u904e\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.Add(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n Backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n state.RemoveAt(state.Count - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nList&lt;List&lt;int&gt;&gt; PermutationsII(int[] nums) {\n List&lt;List&lt;int&gt;&gt; res = [];\n Backtrack([], nums, new bool[nums.Length], res);\n return res;\n}\n</code></pre> permutations_ii.go<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunc backtrackII(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if len(*state) == len(*choices) {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n duplicated := make(map[int]struct{}, 0)\n for i := 0; i &lt; len(*choices); i++ {\n choice := (*choices)[i]\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20 \u4e14 \u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u76f8\u7b49\u5143\u7d20\n if _, ok := duplicated[choice]; !ok &amp;&amp; !(*selected)[i] {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n // \u8a18\u9304\u9078\u64c7\u904e\u7684\u5143\u7d20\u503c\n duplicated[choice] = struct{}{}\n (*selected)[i] = true\n *state = append(*state, choice)\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrackI(state, choices, selected, res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n (*selected)[i] = false\n *state = (*state)[:len(*state)-1]\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums []int) [][]int {\n res := make([][]int, 0)\n state := make([]int, 0)\n selected := make([]bool, len(nums))\n backtrackII(&amp;state, &amp;nums, &amp;selected, &amp;res)\n return res\n}\n</code></pre> permutations_ii.swift<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if state.count == choices.count {\n res.append(state)\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n var duplicated: Set&lt;Int&gt; = []\n for (i, choice) in choices.enumerated() {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20 \u4e14 \u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u76f8\u7b49\u5143\u7d20\n if !selected[i], !duplicated.contains(choice) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n duplicated.insert(choice) // \u8a18\u9304\u9078\u64c7\u904e\u7684\u5143\u7d20\u503c\n selected[i] = true\n state.append(choice)\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state: &amp;state, choices: choices, selected: &amp;selected, res: &amp;res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false\n state.removeLast()\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums: [Int]) -&gt; [[Int]] {\n var state: [Int] = []\n var selected = Array(repeating: false, count: nums.count)\n var res: [[Int]] = []\n backtrack(state: &amp;state, choices: nums, selected: &amp;selected, res: &amp;res)\n return res\n}\n</code></pre> permutations_ii.js<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunction backtrack(state, choices, selected, res) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (state.length === choices.length) {\n res.push([...state]);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n const duplicated = new Set();\n choices.forEach((choice, i) =&gt; {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20 \u4e14 \u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u76f8\u7b49\u5143\u7d20\n if (!selected[i] &amp;&amp; !duplicated.has(choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n duplicated.add(choice); // \u8a18\u9304\u9078\u64c7\u904e\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.push(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n state.pop();\n }\n });\n}\n\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums) {\n const res = [];\n backtrack([], nums, Array(nums.length).fill(false), res);\n return res;\n}\n</code></pre> permutations_ii.ts<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunction backtrack(\n state: number[],\n choices: number[],\n selected: boolean[],\n res: number[][]\n): void {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (state.length === choices.length) {\n res.push([...state]);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n const duplicated = new Set();\n choices.forEach((choice, i) =&gt; {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20 \u4e14 \u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u76f8\u7b49\u5143\u7d20\n if (!selected[i] &amp;&amp; !duplicated.has(choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n duplicated.add(choice); // \u8a18\u9304\u9078\u64c7\u904e\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.push(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n state.pop();\n }\n });\n}\n\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums: number[]): number[][] {\n const res: number[][] = [];\n backtrack([], nums, Array(nums.length).fill(false), res);\n return res;\n}\n</code></pre> permutations_ii.dart<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(\n List&lt;int&gt; state,\n List&lt;int&gt; choices,\n List&lt;bool&gt; selected,\n List&lt;List&lt;int&gt;&gt; res,\n) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (state.length == choices.length) {\n res.add(List.from(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n Set&lt;int&gt; duplicated = {};\n for (int i = 0; i &lt; choices.length; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20 \u4e14 \u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u76f8\u7b49\u5143\u7d20\n if (!selected[i] &amp;&amp; !duplicated.contains(choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n duplicated.add(choice); // \u8a18\u9304\u9078\u64c7\u904e\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.add(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n state.removeLast();\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nList&lt;List&lt;int&gt;&gt; permutationsII(List&lt;int&gt; nums) {\n List&lt;List&lt;int&gt;&gt; res = [];\n backtrack([], nums, List.filled(nums.length, false), res);\n return res;\n}\n</code></pre> permutations_ii.rs<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfn backtrack(mut state: Vec&lt;i32&gt;, choices: &amp;[i32], selected: &amp;mut [bool], res: &amp;mut Vec&lt;Vec&lt;i32&gt;&gt;) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if state.len() == choices.len() {\n res.push(state);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n let mut duplicated = HashSet::&lt;i32&gt;::new();\n for i in 0..choices.len() {\n let choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20 \u4e14 \u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u76f8\u7b49\u5143\u7d20\n if !selected[i] &amp;&amp; !duplicated.contains(&amp;choice) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n duplicated.insert(choice); // \u8a18\u9304\u9078\u64c7\u904e\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state.push(choice);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state.clone(), choices, selected, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n state.remove(state.len() - 1);\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nfn permutations_ii(nums: &amp;mut [i32]) -&gt; Vec&lt;Vec&lt;i32&gt;&gt; {\n let mut res = Vec::new();\n backtrack(Vec::new(), nums, &amp;mut vec![false; nums.len()], &amp;mut res);\n res\n}\n</code></pre> permutations_ii.c<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (stateSize == choicesSize) {\n res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n for (int i = 0; i &lt; choicesSize; i++) {\n res[*resSize][i] = state[i];\n }\n (*resSize)++;\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n bool duplicated[MAX_SIZE] = {false};\n for (int i = 0; i &lt; choicesSize; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20 \u4e14 \u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u76f8\u7b49\u5143\u7d20\n if (!selected[i] &amp;&amp; !duplicated[choice]) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n duplicated[choice] = true; // \u8a18\u9304\u9078\u64c7\u904e\u7684\u5143\u7d20\u503c\n selected[i] = true;\n state[stateSize] = choice;\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false;\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nint **permutationsII(int *nums, int numsSize, int *returnSize) {\n int *state = (int *)malloc(numsSize * sizeof(int));\n bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n for (int i = 0; i &lt; numsSize; i++) {\n selected[i] = false;\n }\n int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n *returnSize = 0;\n\n backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n free(state);\n free(selected);\n\n return res;\n}\n</code></pre> permutations_ii.kt<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfun backtrack(\n state: MutableList&lt;Int&gt;,\n choices: IntArray,\n selected: BooleanArray,\n res: MutableList&lt;MutableList&lt;Int&gt;?&gt;\n) {\n // \u7576\u72c0\u614b\u9577\u5ea6\u7b49\u65bc\u5143\u7d20\u6578\u91cf\u6642\uff0c\u8a18\u9304\u89e3\n if (state.size == choices.size) {\n res.add(state.toMutableList())\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n val duplicated = HashSet&lt;Int&gt;()\n for (i in choices.indices) {\n val choice = choices[i]\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u5143\u7d20 \u4e14 \u4e0d\u5141\u8a31\u91cd\u8907\u9078\u64c7\u76f8\u7b49\u5143\u7d20\n if (!selected[i] &amp;&amp; !duplicated.contains(choice)) {\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n duplicated.add(choice) // \u8a18\u9304\u9078\u64c7\u904e\u7684\u5143\u7d20\u503c\n selected[i] = true\n state.add(choice)\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, choices, selected, res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n selected[i] = false\n state.removeAt(state.size - 1)\n }\n }\n}\n\n/* \u5168\u6392\u5217 II */\nfun permutationsII(nums: IntArray): MutableList&lt;MutableList&lt;Int&gt;?&gt; {\n val res = mutableListOf&lt;MutableList&lt;Int&gt;?&gt;()\n backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)\n return res\n}\n</code></pre> permutations_ii.rb<pre><code>[class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutations_ii}\n</code></pre> permutations_ii.zig<pre><code>[class]{}-[func]{backtrack}\n\n[class]{}-[func]{permutationsII}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5047\u8a2d\u5143\u7d20\u5169\u5169\u4e4b\u9593\u4e92\u4e0d\u76f8\u540c\uff0c\u5247 \\(n\\) \u500b\u5143\u7d20\u5171\u6709 \\(n!\\) \u7a2e\u6392\u5217\uff08\u968e\u4e58\uff09\uff1b\u5728\u8a18\u9304\u7d50\u679c\u6642\uff0c\u9700\u8981\u8907\u88fd\u9577\u5ea6\u70ba \\(n\\) \u7684\u4e32\u5217\uff0c\u4f7f\u7528 \\(O(n)\\) \u6642\u9593\u3002\u56e0\u6b64\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n!n)\\) \u3002</p> <p>\u6700\u5927\u905e\u8ff4\u6df1\u5ea6\u70ba \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u5806\u758a\u5e40\u7a7a\u9593\u3002<code>selected</code> \u4f7f\u7528 \\(O(n)\\) \u7a7a\u9593\u3002\u540c\u4e00\u6642\u523b\u6700\u591a\u5171\u6709 \\(n\\) \u500b <code>duplicated</code> \uff0c\u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u9593\u3002\u56e0\u6b64\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\) \u3002</p>"},{"location":"chapter_backtracking/permutations_problem/#3","title":"3. \u00a0 \u5169\u7a2e\u526a\u679d\u5c0d\u6bd4","text":"<p>\u8acb\u6ce8\u610f\uff0c\u96d6\u7136 <code>selected</code> \u548c <code>duplicated</code> \u90fd\u7528\u65bc\u526a\u679d\uff0c\u4f46\u5169\u8005\u7684\u76ee\u6a19\u4e0d\u540c\u3002</p> <ul> <li>\u91cd\u8907\u9078\u64c7\u526a\u679d\uff1a\u6574\u500b\u641c\u5c0b\u904e\u7a0b\u4e2d\u53ea\u6709\u4e00\u500b <code>selected</code> \u3002\u5b83\u8a18\u9304\u7684\u662f\u7576\u524d\u72c0\u614b\u4e2d\u5305\u542b\u54ea\u4e9b\u5143\u7d20\uff0c\u5176\u4f5c\u7528\u662f\u907f\u514d\u67d0\u500b\u5143\u7d20\u5728 <code>state</code> \u4e2d\u91cd\u8907\u51fa\u73fe\u3002</li> <li>\u76f8\u7b49\u5143\u7d20\u526a\u679d\uff1a\u6bcf\u8f2a\u9078\u64c7\uff08\u6bcf\u500b\u547c\u53eb\u7684 <code>backtrack</code> \u51fd\u5f0f\uff09\u90fd\u5305\u542b\u4e00\u500b <code>duplicated</code> \u3002\u5b83\u8a18\u9304\u7684\u662f\u5728\u672c\u8f2a\u8d70\u8a2a\uff08<code>for</code> \u8ff4\u5708\uff09\u4e2d\u54ea\u4e9b\u5143\u7d20\u5df2\u88ab\u9078\u64c7\u904e\uff0c\u5176\u4f5c\u7528\u662f\u4fdd\u8b49\u76f8\u7b49\u5143\u7d20\u53ea\u88ab\u9078\u64c7\u4e00\u6b21\u3002</li> </ul> <p>\u5716 13-9 \u5c55\u793a\u4e86\u5169\u500b\u526a\u679d\u689d\u4ef6\u7684\u751f\u6548\u7bc4\u570d\u3002\u6ce8\u610f\uff0c\u6a39\u4e2d\u7684\u6bcf\u500b\u7bc0\u9ede\u4ee3\u8868\u4e00\u500b\u9078\u64c7\uff0c\u5f9e\u6839\u7bc0\u9ede\u5230\u8449\u7bc0\u9ede\u7684\u8def\u5f91\u4e0a\u7684\u5404\u500b\u7bc0\u9ede\u69cb\u6210\u4e00\u500b\u6392\u5217\u3002</p> <p></p> <p> \u5716 13-9 \u00a0 \u5169\u7a2e\u526a\u679d\u689d\u4ef6\u7684\u4f5c\u7528\u7bc4\u570d </p>"},{"location":"chapter_backtracking/subset_sum_problem/","title":"13.3 \u00a0 \u5b50\u96c6\u548c\u554f\u984c","text":""},{"location":"chapter_backtracking/subset_sum_problem/#1331","title":"13.3.1 \u00a0 \u7121\u91cd\u8907\u5143\u7d20\u7684\u60c5\u6cc1","text":"<p>Question</p> <p>\u7d66\u5b9a\u4e00\u500b\u6b63\u6574\u6578\u9663\u5217 <code>nums</code> \u548c\u4e00\u500b\u76ee\u6a19\u6b63\u6574\u6578 <code>target</code> \uff0c\u8acb\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7d44\u5408\uff0c\u4f7f\u5f97\u7d44\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u65bc <code>target</code> \u3002\u7d66\u5b9a\u9663\u5217\u7121\u91cd\u8907\u5143\u7d20\uff0c\u6bcf\u500b\u5143\u7d20\u53ef\u4ee5\u88ab\u9078\u53d6\u591a\u6b21\u3002\u8acb\u4ee5\u4e32\u5217\u5f62\u5f0f\u8fd4\u56de\u9019\u4e9b\u7d44\u5408\uff0c\u4e32\u5217\u4e2d\u4e0d\u61c9\u5305\u542b\u91cd\u8907\u7d44\u5408\u3002</p> <p>\u4f8b\u5982\uff0c\u8f38\u5165\u96c6\u5408 \\(\\{3, 4, 5\\}\\) \u548c\u76ee\u6a19\u6574\u6578 \\(9\\) \uff0c\u89e3\u70ba \\(\\{3, 3, 3\\}, \\{4, 5\\}\\) \u3002\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u5169\u9ede\u3002</p> <ul> <li>\u8f38\u5165\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u88ab\u7121\u9650\u6b21\u91cd\u8907\u9078\u53d6\u3002</li> <li>\u5b50\u96c6\u4e0d\u5340\u5206\u5143\u7d20\u9806\u5e8f\uff0c\u6bd4\u5982 \\(\\{4, 5\\}\\) \u548c \\(\\{5, 4\\}\\) \u662f\u540c\u4e00\u500b\u5b50\u96c6\u3002</li> </ul>"},{"location":"chapter_backtracking/subset_sum_problem/#1","title":"1. \u00a0 \u53c3\u8003\u5168\u6392\u5217\u89e3\u6cd5","text":"<p>\u985e\u4f3c\u65bc\u5168\u6392\u5217\u554f\u984c\uff0c\u6211\u5011\u53ef\u4ee5\u628a\u5b50\u96c6\u7684\u751f\u6210\u904e\u7a0b\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u9078\u64c7\u7684\u7d50\u679c\uff0c\u4e26\u5728\u9078\u64c7\u904e\u7a0b\u4e2d\u5be6\u6642\u66f4\u65b0\u201c\u5143\u7d20\u548c\u201d\uff0c\u7576\u5143\u7d20\u548c\u7b49\u65bc <code>target</code> \u6642\uff0c\u5c31\u5c07\u5b50\u96c6\u8a18\u9304\u81f3\u7d50\u679c\u4e32\u5217\u3002</p> <p>\u800c\u8207\u5168\u6392\u5217\u554f\u984c\u4e0d\u540c\u7684\u662f\uff0c\u672c\u984c\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u88ab\u7121\u9650\u6b21\u9078\u53d6\uff0c\u56e0\u6b64\u7121\u9808\u85c9\u52a9 <code>selected</code> \u5e03\u6797\u4e32\u5217\u4f86\u8a18\u9304\u5143\u7d20\u662f\u5426\u5df2\u88ab\u9078\u64c7\u3002\u6211\u5011\u53ef\u4ee5\u5c0d\u5168\u6392\u5217\u7a0b\u5f0f\u78bc\u9032\u884c\u5c0f\u5e45\u4fee\u6539\uff0c\u521d\u6b65\u5f97\u5230\u89e3\u984c\u7a0b\u5f0f\u78bc\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig subset_sum_i_naive.py<pre><code>def backtrack(\n state: list[int],\n target: int,\n total: int,\n choices: list[int],\n res: list[list[int]],\n):\n \"\"\"\u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n # \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if total == target:\n res.append(list(state))\n return\n # \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for i in range(len(choices)):\n # \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u8df3\u904e\u8a72\u9078\u64c7\n if total + choices[i] &gt; target:\n continue\n # \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.append(choices[i])\n # \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target, total + choices[i], choices, res)\n # \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop()\n\ndef subset_sum_i_naive(nums: list[int], target: int) -&gt; list[list[int]]:\n \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u8907\u5b50\u96c6\uff09\"\"\"\n state = [] # \u72c0\u614b\uff08\u5b50\u96c6\uff09\n total = 0 # \u5b50\u96c6\u548c\n res = [] # \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, total, nums, res)\n return res\n</code></pre> subset_sum_i_naive.cpp<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector&lt;int&gt; &amp;state, int target, int total, vector&lt;int&gt; &amp;choices, vector&lt;vector&lt;int&gt;&gt; &amp;res) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (total == target) {\n res.push_back(state);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (size_t i = 0; i &lt; choices.size(); i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u8df3\u904e\u8a72\u9078\u64c7\n if (total + choices[i] &gt; target) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.push_back(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop_back();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u8907\u5b50\u96c6\uff09 */\nvector&lt;vector&lt;int&gt;&gt; subsetSumINaive(vector&lt;int&gt; &amp;nums, int target) {\n vector&lt;int&gt; state; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n int total = 0; // \u5b50\u96c6\u548c\n vector&lt;vector&lt;int&gt;&gt; res; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n</code></pre> subset_sum_i_naive.java<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(List&lt;Integer&gt; state, int target, int total, int[] choices, List&lt;List&lt;Integer&gt;&gt; res) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (total == target) {\n res.add(new ArrayList&lt;&gt;(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (int i = 0; i &lt; choices.length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u8df3\u904e\u8a72\u9078\u64c7\n if (total + choices[i] &gt; target) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.add(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.remove(state.size() - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u8907\u5b50\u96c6\uff09 */\nList&lt;List&lt;Integer&gt;&gt; subsetSumINaive(int[] nums, int target) {\n List&lt;Integer&gt; state = new ArrayList&lt;&gt;(); // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n int total = 0; // \u5b50\u96c6\u548c\n List&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;&gt;(); // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n</code></pre> subset_sum_i_naive.cs<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid Backtrack(List&lt;int&gt; state, int target, int total, int[] choices, List&lt;List&lt;int&gt;&gt; res) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (total == target) {\n res.Add(new List&lt;int&gt;(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (int i = 0; i &lt; choices.Length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u8df3\u904e\u8a72\u9078\u64c7\n if (total + choices[i] &gt; target) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.Add(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n Backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.RemoveAt(state.Count - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u8907\u5b50\u96c6\uff09 */\nList&lt;List&lt;int&gt;&gt; SubsetSumINaive(int[] nums, int target) {\n List&lt;int&gt; state = []; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n int total = 0; // \u5b50\u96c6\u548c\n List&lt;List&lt;int&gt;&gt; res = []; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n Backtrack(state, target, total, nums, res);\n return res;\n}\n</code></pre> subset_sum_i_naive.go<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumINaive(total, target int, state, choices *[]int, res *[][]int) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if target == total {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for i := 0; i &lt; len(*choices); i++ {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u8df3\u904e\u8a72\u9078\u64c7\n if total+(*choices)[i] &gt; target {\n continue\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n *state = append(*state, (*choices)[i])\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrackSubsetSumINaive(total+(*choices)[i], target, state, choices, res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n *state = (*state)[:len(*state)-1]\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u8907\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums []int, target int) [][]int {\n state := make([]int, 0) // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n total := 0 // \u5b50\u96c6\u548c\n res := make([][]int, 0) // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrackSubsetSumINaive(total, target, &amp;state, &amp;nums, &amp;res)\n return res\n}\n</code></pre> subset_sum_i_naive.swift<pre><code>/* \u56de\u6eaf\u6f14\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\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if total == target {\n res.append(state)\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for i in choices.indices {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u8df3\u904e\u8a72\u9078\u64c7\n if total + choices[i] &gt; target {\n continue\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.append(choices[i])\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state: &amp;state, target: target, total: total + choices[i], choices: choices, res: &amp;res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.removeLast()\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u8907\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums: [Int], target: Int) -&gt; [[Int]] {\n var state: [Int] = [] // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n let total = 0 // \u5b50\u96c6\u548c\n var res: [[Int]] = [] // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state: &amp;state, target: target, total: total, choices: nums, res: &amp;res)\n return res\n}\n</code></pre> subset_sum_i_naive.js<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, total, choices, res) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (total === target) {\n res.push([...state]);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (let i = 0; i &lt; choices.length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u8df3\u904e\u8a72\u9078\u64c7\n if (total + choices[i] &gt; target) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.push(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u8907\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums, target) {\n const state = []; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n const total = 0; // \u5b50\u96c6\u548c\n const res = []; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n</code></pre> subset_sum_i_naive.ts<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\n state: number[],\n target: number,\n total: number,\n choices: number[],\n res: number[][]\n): void {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (total === target) {\n res.push([...state]);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (let i = 0; i &lt; choices.length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u8df3\u904e\u8a72\u9078\u64c7\n if (total + choices[i] &gt; target) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.push(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u8907\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums: number[], target: number): number[][] {\n const state = []; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n const total = 0; // \u5b50\u96c6\u548c\n const res = []; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n</code></pre> subset_sum_i_naive.dart<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\n List&lt;int&gt; state,\n int target,\n int total,\n List&lt;int&gt; choices,\n List&lt;List&lt;int&gt;&gt; res,\n) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (total == target) {\n res.add(List.from(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (int i = 0; i &lt; choices.length; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u8df3\u904e\u8a72\u9078\u64c7\n if (total + choices[i] &gt; target) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.add(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.removeLast();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u8907\u5b50\u96c6\uff09 */\nList&lt;List&lt;int&gt;&gt; subsetSumINaive(List&lt;int&gt; nums, int target) {\n List&lt;int&gt; state = []; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n int total = 0; // \u5143\u7d20\u548c\n List&lt;List&lt;int&gt;&gt; res = []; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, total, nums, res);\n return res;\n}\n</code></pre> subset_sum_i_naive.rs<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(\n mut state: Vec&lt;i32&gt;,\n target: i32,\n total: i32,\n choices: &amp;[i32],\n res: &amp;mut Vec&lt;Vec&lt;i32&gt;&gt;,\n) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if total == target {\n res.push(state);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for i in 0..choices.len() {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u8df3\u904e\u8a72\u9078\u64c7\n if total + choices[i] &gt; target {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.push(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state.clone(), target, total + choices[i], choices, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u8907\u5b50\u96c6\uff09 */\nfn subset_sum_i_naive(nums: &amp;[i32], target: i32) -&gt; Vec&lt;Vec&lt;i32&gt;&gt; {\n let state = Vec::new(); // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n let total = 0; // \u5b50\u96c6\u548c\n let mut res = Vec::new(); // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, total, nums, &amp;mut res);\n res\n}\n</code></pre> subset_sum_i_naive.c<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(int target, int total, int *choices, int choicesSize) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (total == target) {\n for (int i = 0; i &lt; stateSize; i++) {\n res[resSize][i] = state[i];\n }\n resColSizes[resSize++] = stateSize;\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (int i = 0; i &lt; choicesSize; i++) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u8df3\u904e\u8a72\u9078\u64c7\n if (total + choices[i] &gt; target) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state[stateSize++] = choices[i];\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(target, total + choices[i], choices, choicesSize);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n stateSize--;\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u8907\u5b50\u96c6\uff09 */\nvoid subsetSumINaive(int *nums, int numsSize, int target) {\n resSize = 0; // \u521d\u59cb\u5316\u89e3\u7684\u6578\u91cf\u70ba0\n backtrack(target, 0, nums, numsSize);\n}\n</code></pre> subset_sum_i_naive.kt<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfun backtrack(\n state: MutableList&lt;Int&gt;,\n target: Int,\n total: Int,\n choices: IntArray,\n res: MutableList&lt;MutableList&lt;Int&gt;?&gt;\n) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (total == target) {\n res.add(state.toMutableList())\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (i in choices.indices) {\n // \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u8df3\u904e\u8a72\u9078\u64c7\n if (total + choices[i] &gt; target) {\n continue\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n state.add(choices[i])\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target, total + choices[i], choices, res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.removeAt(state.size - 1)\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u8907\u5b50\u96c6\uff09 */\nfun subsetSumINaive(nums: IntArray, target: Int): MutableList&lt;MutableList&lt;Int&gt;?&gt; {\n val state = mutableListOf&lt;Int&gt;() // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n val total = 0 // \u5b50\u96c6\u548c\n val res = mutableListOf&lt;MutableList&lt;Int&gt;?&gt;() // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, total, nums, res)\n return res\n}\n</code></pre> subset_sum_i_naive.rb<pre><code>[class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_i_naive}\n</code></pre> subset_sum_i_naive.zig<pre><code>[class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumINaive}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5411\u4ee5\u4e0a\u7a0b\u5f0f\u78bc\u8f38\u5165\u9663\u5217 \\([3, 4, 5]\\) \u548c\u76ee\u6a19\u5143\u7d20 \\(9\\) \uff0c\u8f38\u51fa\u7d50\u679c\u70ba \\([3, 3, 3], [4, 5], [5, 4]\\) \u3002\u96d6\u7136\u6210\u529f\u627e\u51fa\u4e86\u6240\u6709\u548c\u70ba \\(9\\) \u7684\u5b50\u96c6\uff0c\u4f46\u5176\u4e2d\u5b58\u5728\u91cd\u8907\u7684\u5b50\u96c6 \\([4, 5]\\) \u548c \\([5, 4]\\) \u3002</p> <p>\u9019\u662f\u56e0\u70ba\u641c\u5c0b\u904e\u7a0b\u662f\u5340\u5206\u9078\u64c7\u9806\u5e8f\u7684\uff0c\u7136\u800c\u5b50\u96c6\u4e0d\u5340\u5206\u9078\u64c7\u9806\u5e8f\u3002\u5982\u5716 13-10 \u6240\u793a\uff0c\u5148\u9078 \\(4\\) \u5f8c\u9078 \\(5\\) \u8207\u5148\u9078 \\(5\\) \u5f8c\u9078 \\(4\\) \u662f\u4e0d\u540c\u7684\u5206\u652f\uff0c\u4f46\u5c0d\u61c9\u540c\u4e00\u500b\u5b50\u96c6\u3002</p> <p></p> <p> \u5716 13-10 \u00a0 \u5b50\u96c6\u641c\u5c0b\u8207\u8d8a\u754c\u526a\u679d </p> <p>\u70ba\u4e86\u53bb\u9664\u91cd\u8907\u5b50\u96c6\uff0c\u4e00\u7a2e\u76f4\u63a5\u7684\u601d\u8def\u662f\u5c0d\u7d50\u679c\u4e32\u5217\u9032\u884c\u53bb\u91cd\u3002\u4f46\u9019\u500b\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\uff0c\u6709\u5169\u65b9\u9762\u539f\u56e0\u3002</p> <ul> <li>\u7576\u9663\u5217\u5143\u7d20\u8f03\u591a\uff0c\u5c24\u5176\u662f\u7576 <code>target</code> \u8f03\u5927\u6642\uff0c\u641c\u5c0b\u904e\u7a0b\u6703\u7522\u751f\u5927\u91cf\u7684\u91cd\u8907\u5b50\u96c6\u3002</li> <li>\u6bd4\u8f03\u5b50\u96c6\uff08\u9663\u5217\uff09\u7684\u7570\u540c\u975e\u5e38\u8017\u6642\uff0c\u9700\u8981\u5148\u6392\u5e8f\u9663\u5217\uff0c\u518d\u6bd4\u8f03\u9663\u5217\u4e2d\u6bcf\u500b\u5143\u7d20\u7684\u7570\u540c\u3002</li> </ul>"},{"location":"chapter_backtracking/subset_sum_problem/#2","title":"2. \u00a0 \u91cd\u8907\u5b50\u96c6\u526a\u679d","text":"<p>\u6211\u5011\u8003\u616e\u5728\u641c\u5c0b\u904e\u7a0b\u4e2d\u900f\u904e\u526a\u679d\u9032\u884c\u53bb\u91cd\u3002\u89c0\u5bdf\u5716 13-11 \uff0c\u91cd\u8907\u5b50\u96c6\u662f\u5728\u4ee5\u4e0d\u540c\u9806\u5e8f\u9078\u64c7\u9663\u5217\u5143\u7d20\u6642\u7522\u751f\u7684\uff0c\u4f8b\u5982\u4ee5\u4e0b\u60c5\u6cc1\u3002</p> <ol> <li>\u7576\u7b2c\u4e00\u8f2a\u548c\u7b2c\u4e8c\u8f2a\u5206\u5225\u9078\u64c7 \\(3\\) \u548c \\(4\\) \u6642\uff0c\u6703\u751f\u6210\u5305\u542b\u9019\u5169\u500b\u5143\u7d20\u7684\u6240\u6709\u5b50\u96c6\uff0c\u8a18\u70ba \\([3, 4, \\dots]\\) \u3002</li> <li>\u4e4b\u5f8c\uff0c\u7576\u7b2c\u4e00\u8f2a\u9078\u64c7 \\(4\\) \u6642\uff0c\u5247\u7b2c\u4e8c\u8f2a\u61c9\u8a72\u8df3\u904e \\(3\\) \uff0c\u56e0\u70ba\u8a72\u9078\u64c7\u7522\u751f\u7684\u5b50\u96c6 \\([4, 3, \\dots]\\) \u548c\u7b2c <code>1.</code> \u6b65\u4e2d\u751f\u6210\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u8907\u3002</li> </ol> <p>\u5728\u641c\u5c0b\u904e\u7a0b\u4e2d\uff0c\u6bcf\u4e00\u5c64\u7684\u9078\u64c7\u90fd\u662f\u5f9e\u5de6\u5230\u53f3\u88ab\u9010\u500b\u5617\u8a66\u7684\uff0c\u56e0\u6b64\u8d8a\u9760\u53f3\u7684\u5206\u652f\u88ab\u526a\u6389\u7684\u8d8a\u591a\u3002</p> <ol> <li>\u524d\u5169\u8f2a\u9078\u64c7 \\(3\\) \u548c \\(5\\) \uff0c\u751f\u6210\u5b50\u96c6 \\([3, 5, \\dots]\\) \u3002</li> <li>\u524d\u5169\u8f2a\u9078\u64c7 \\(4\\) \u548c \\(5\\) \uff0c\u751f\u6210\u5b50\u96c6 \\([4, 5, \\dots]\\) \u3002</li> <li>\u82e5\u7b2c\u4e00\u8f2a\u9078\u64c7 \\(5\\) \uff0c\u5247\u7b2c\u4e8c\u8f2a\u61c9\u8a72\u8df3\u904e \\(3\\) \u548c \\(4\\) \uff0c\u56e0\u70ba\u5b50\u96c6 \\([5, 3, \\dots]\\) \u548c \\([5, 4, \\dots]\\) \u8207\u7b2c <code>1.</code> \u6b65\u548c\u7b2c <code>2.</code> \u6b65\u4e2d\u63cf\u8ff0\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u8907\u3002</li> </ol> <p></p> <p> \u5716 13-11 \u00a0 \u4e0d\u540c\u9078\u64c7\u9806\u5e8f\u5c0e\u81f4\u7684\u91cd\u8907\u5b50\u96c6 </p> <p>\u7e3d\u7d50\u4f86\u770b\uff0c\u7d66\u5b9a\u8f38\u5165\u9663\u5217 \\([x_1, x_2, \\dots, x_n]\\) \uff0c\u8a2d\u641c\u5c0b\u904e\u7a0b\u4e2d\u7684\u9078\u64c7\u5e8f\u5217\u70ba \\([x_{i_1}, x_{i_2}, \\dots, x_{i_m}]\\) \uff0c\u5247\u8a72\u9078\u64c7\u5e8f\u5217\u9700\u8981\u6eff\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u4e0d\u6eff\u8db3\u8a72\u689d\u4ef6\u7684\u9078\u64c7\u5e8f\u5217\u90fd\u6703\u9020\u6210\u91cd\u8907\uff0c\u61c9\u7576\u526a\u679d\u3002</p>"},{"location":"chapter_backtracking/subset_sum_problem/#3","title":"3. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"<p>\u70ba\u5be6\u73fe\u8a72\u526a\u679d\uff0c\u6211\u5011\u521d\u59cb\u5316\u8b8a\u6578 <code>start</code> \uff0c\u7528\u65bc\u6307\u793a\u8d70\u8a2a\u8d77\u59cb\u9ede\u3002\u7576\u505a\u51fa\u9078\u64c7 \\(x_{i}\\) \u5f8c\uff0c\u8a2d\u5b9a\u4e0b\u4e00\u8f2a\u5f9e\u7d22\u5f15 \\(i\\) \u958b\u59cb\u8d70\u8a2a\u3002\u9019\u6a23\u505a\u5c31\u53ef\u4ee5\u8b93\u9078\u64c7\u5e8f\u5217\u6eff\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u5f9e\u800c\u4fdd\u8b49\u5b50\u96c6\u552f\u4e00\u3002</p> <p>\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u5011\u9084\u5c0d\u7a0b\u5f0f\u78bc\u9032\u884c\u4e86\u4ee5\u4e0b\u5169\u9805\u6700\u4f73\u5316\u3002</p> <ul> <li>\u5728\u958b\u555f\u641c\u5c0b\u524d\uff0c\u5148\u5c07\u9663\u5217 <code>nums</code> \u6392\u5e8f\u3002\u5728\u8d70\u8a2a\u6240\u6709\u9078\u64c7\u6642\uff0c\u7576\u5b50\u96c6\u548c\u8d85\u904e <code>target</code> \u6642\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\uff0c\u56e0\u70ba\u5f8c\u908a\u7684\u5143\u7d20\u66f4\u5927\uff0c\u5176\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e <code>target</code> \u3002</li> <li>\u7701\u53bb\u5143\u7d20\u548c\u8b8a\u6578 <code>total</code> \uff0c\u900f\u904e\u5728 <code>target</code> \u4e0a\u57f7\u884c\u6e1b\u6cd5\u4f86\u7d71\u8a08\u5143\u7d20\u548c\uff0c\u7576 <code>target</code> \u7b49\u65bc \\(0\\) \u6642\u8a18\u9304\u89e3\u3002</li> </ul> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig subset_sum_i.py<pre><code>def backtrack(\n state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n \"\"\"\u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n # \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if target == 0:\n res.append(list(state))\n return\n # \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n # \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n for i in range(start, len(choices)):\n # \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n # \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if target - choices[i] &lt; 0:\n break\n # \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.append(choices[i])\n # \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target - choices[i], choices, i, res)\n # \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop()\n\ndef subset_sum_i(nums: list[int], target: int) -&gt; list[list[int]]:\n \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\"\"\"\n state = [] # \u72c0\u614b\uff08\u5b50\u96c6\uff09\n nums.sort() # \u5c0d nums \u9032\u884c\u6392\u5e8f\n start = 0 # \u8d70\u8a2a\u8d77\u59cb\u9ede\n res = [] # \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, res)\n return res\n</code></pre> subset_sum_i.cpp<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector&lt;int&gt; &amp;state, int target, vector&lt;int&gt; &amp;choices, int start, vector&lt;vector&lt;int&gt;&gt; &amp;res) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target == 0) {\n res.push_back(state);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n for (int i = start; i &lt; choices.size(); i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.push_back(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop_back();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvector&lt;vector&lt;int&gt;&gt; subsetSumI(vector&lt;int&gt; &amp;nums, int target) {\n vector&lt;int&gt; state; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n sort(nums.begin(), nums.end()); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n int start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n vector&lt;vector&lt;int&gt;&gt; res; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n</code></pre> subset_sum_i.java<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(List&lt;Integer&gt; state, int target, int[] choices, int start, List&lt;List&lt;Integer&gt;&gt; res) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target == 0) {\n res.add(new ArrayList&lt;&gt;(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n for (int i = start; i &lt; choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.add(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.remove(state.size() - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList&lt;List&lt;Integer&gt;&gt; subsetSumI(int[] nums, int target) {\n List&lt;Integer&gt; state = new ArrayList&lt;&gt;(); // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n Arrays.sort(nums); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n int start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n List&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;&gt;(); // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n</code></pre> subset_sum_i.cs<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid Backtrack(List&lt;int&gt; state, int target, int[] choices, int start, List&lt;List&lt;int&gt;&gt; res) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target == 0) {\n res.Add(new List&lt;int&gt;(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n for (int i = start; i &lt; choices.Length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.Add(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n Backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.RemoveAt(state.Count - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList&lt;List&lt;int&gt;&gt; SubsetSumI(int[] nums, int target) {\n List&lt;int&gt; state = []; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n Array.Sort(nums); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n int start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n List&lt;List&lt;int&gt;&gt; res = []; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n Backtrack(state, target, nums, start, res);\n return res;\n}\n</code></pre> subset_sum_i.go<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumI(start, target int, state, choices *[]int, res *[][]int) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if target == 0 {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n for i := start; i &lt; len(*choices); i++ {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if target-(*choices)[i] &lt; 0 {\n break\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n *state = append(*state, (*choices)[i])\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrackSubsetSumI(i, target-(*choices)[i], state, choices, res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n *state = (*state)[:len(*state)-1]\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums []int, target int) [][]int {\n state := make([]int, 0) // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n sort.Ints(nums) // \u5c0d nums \u9032\u884c\u6392\u5e8f\n start := 0 // \u8d70\u8a2a\u8d77\u59cb\u9ede\n res := make([][]int, 0) // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrackSubsetSumI(start, target, &amp;state, &amp;nums, &amp;res)\n return res\n}\n</code></pre> subset_sum_i.swift<pre><code>/* \u56de\u6eaf\u6f14\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\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if target == 0 {\n res.append(state)\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n for i in choices.indices.dropFirst(start) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if target - choices[i] &lt; 0 {\n break\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.append(choices[i])\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state: &amp;state, target: target - choices[i], choices: choices, start: i, res: &amp;res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.removeLast()\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums: [Int], target: Int) -&gt; [[Int]] {\n var state: [Int] = [] // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n let nums = nums.sorted() // \u5c0d nums \u9032\u884c\u6392\u5e8f\n let start = 0 // \u8d70\u8a2a\u8d77\u59cb\u9ede\n var res: [[Int]] = [] // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state: &amp;state, target: target, choices: nums, start: start, res: &amp;res)\n return res\n}\n</code></pre> subset_sum_i.js<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, choices, start, res) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target === 0) {\n res.push([...state]);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n for (let i = start; i &lt; choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums, target) {\n const state = []; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n nums.sort((a, b) =&gt; a - b); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n const start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n const res = []; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n</code></pre> subset_sum_i.ts<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\n state: number[],\n target: number,\n choices: number[],\n start: number,\n res: number[][]\n): void {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target === 0) {\n res.push([...state]);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n for (let i = start; i &lt; choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums: number[], target: number): number[][] {\n const state = []; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n nums.sort((a, b) =&gt; a - b); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n const start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n const res = []; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n</code></pre> subset_sum_i.dart<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\n List&lt;int&gt; state,\n int target,\n List&lt;int&gt; choices,\n int start,\n List&lt;List&lt;int&gt;&gt; res,\n) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target == 0) {\n res.add(List.from(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n for (int i = start; i &lt; choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.add(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.removeLast();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList&lt;List&lt;int&gt;&gt; subsetSumI(List&lt;int&gt; nums, int target) {\n List&lt;int&gt; state = []; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n nums.sort(); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n int start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n List&lt;List&lt;int&gt;&gt; res = []; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n</code></pre> subset_sum_i.rs<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(\n mut state: Vec&lt;i32&gt;,\n target: i32,\n choices: &amp;[i32],\n start: usize,\n res: &amp;mut Vec&lt;Vec&lt;i32&gt;&gt;,\n) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if target == 0 {\n res.push(state);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n for i in start..choices.len() {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if target - choices[i] &lt; 0 {\n break;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state.clone(), target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfn subset_sum_i(nums: &amp;mut [i32], target: i32) -&gt; Vec&lt;Vec&lt;i32&gt;&gt; {\n let state = Vec::new(); // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n nums.sort(); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n let start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n let mut res = Vec::new(); // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, &amp;mut res);\n res\n}\n</code></pre> subset_sum_i.c<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target == 0) {\n for (int i = 0; i &lt; stateSize; ++i) {\n res[resSize][i] = state[i];\n }\n resColSizes[resSize++] = stateSize;\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n for (int i = start; i &lt; choicesSize; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state[stateSize] = choices[i];\n stateSize++;\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(target - choices[i], choices, choicesSize, i);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n stateSize--;\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvoid subsetSumI(int *nums, int numsSize, int target) {\n qsort(nums, numsSize, sizeof(int), cmp); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n int start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n backtrack(target, nums, numsSize, start);\n}\n</code></pre> subset_sum_i.kt<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfun backtrack(\n state: MutableList&lt;Int&gt;,\n target: Int,\n choices: IntArray,\n start: Int,\n res: MutableList&lt;MutableList&lt;Int&gt;?&gt;\n) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target == 0) {\n res.add(state.toMutableList())\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n for (i in start..&lt;choices.size) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.add(choices[i])\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target - choices[i], choices, i, res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.removeAt(state.size - 1)\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfun subsetSumI(nums: IntArray, target: Int): MutableList&lt;MutableList&lt;Int&gt;?&gt; {\n val state = mutableListOf&lt;Int&gt;() // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n nums.sort() // \u5c0d nums \u9032\u884c\u6392\u5e8f\n val start = 0 // \u8d70\u8a2a\u8d77\u59cb\u9ede\n val res = mutableListOf&lt;MutableList&lt;Int&gt;?&gt;() // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, res)\n return res\n}\n</code></pre> subset_sum_i.rb<pre><code>[class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_i}\n</code></pre> subset_sum_i.zig<pre><code>[class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumI}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 13-12 \u6240\u793a\u70ba\u5c07\u9663\u5217 \\([3, 4, 5]\\) \u548c\u76ee\u6a19\u5143\u7d20 \\(9\\) \u8f38\u5165\u4ee5\u4e0a\u7a0b\u5f0f\u78bc\u5f8c\u7684\u6574\u9ad4\u56de\u6eaf\u904e\u7a0b\u3002</p> <p></p> <p> \u5716 13-12 \u00a0 \u5b50\u96c6\u548c I \u56de\u6eaf\u904e\u7a0b </p>"},{"location":"chapter_backtracking/subset_sum_problem/#1332","title":"13.3.2 \u00a0 \u8003\u616e\u91cd\u8907\u5143\u7d20\u7684\u60c5\u6cc1","text":"<p>Question</p> <p>\u7d66\u5b9a\u4e00\u500b\u6b63\u6574\u6578\u9663\u5217 <code>nums</code> \u548c\u4e00\u500b\u76ee\u6a19\u6b63\u6574\u6578 <code>target</code> \uff0c\u8acb\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7d44\u5408\uff0c\u4f7f\u5f97\u7d44\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u65bc <code>target</code> \u3002\u7d66\u5b9a\u9663\u5217\u53ef\u80fd\u5305\u542b\u91cd\u8907\u5143\u7d20\uff0c\u6bcf\u500b\u5143\u7d20\u53ea\u53ef\u88ab\u9078\u64c7\u4e00\u6b21\u3002\u8acb\u4ee5\u4e32\u5217\u5f62\u5f0f\u8fd4\u56de\u9019\u4e9b\u7d44\u5408\uff0c\u4e32\u5217\u4e2d\u4e0d\u61c9\u5305\u542b\u91cd\u8907\u7d44\u5408\u3002</p> <p>\u76f8\u6bd4\u65bc\u4e0a\u984c\uff0c\u672c\u984c\u7684\u8f38\u5165\u9663\u5217\u53ef\u80fd\u5305\u542b\u91cd\u8907\u5143\u7d20\uff0c\u9019\u5f15\u5165\u4e86\u65b0\u7684\u554f\u984c\u3002\u4f8b\u5982\uff0c\u7d66\u5b9a\u9663\u5217 \\([4, \\hat{4}, 5]\\) \u548c\u76ee\u6a19\u5143\u7d20 \\(9\\) \uff0c\u5247\u73fe\u6709\u7a0b\u5f0f\u78bc\u7684\u8f38\u51fa\u7d50\u679c\u70ba \\([4, 5], [\\hat{4}, 5]\\) \uff0c\u51fa\u73fe\u4e86\u91cd\u8907\u5b50\u96c6\u3002</p> <p>\u9020\u6210\u9019\u7a2e\u91cd\u8907\u7684\u539f\u56e0\u662f\u76f8\u7b49\u5143\u7d20\u5728\u67d0\u8f2a\u4e2d\u88ab\u591a\u6b21\u9078\u64c7\u3002\u5728\u5716 13-13 \u4e2d\uff0c\u7b2c\u4e00\u8f2a\u5171\u6709\u4e09\u500b\u9078\u64c7\uff0c\u5176\u4e2d\u5169\u500b\u90fd\u70ba \\(4\\) \uff0c\u6703\u7522\u751f\u5169\u500b\u91cd\u8907\u7684\u641c\u5c0b\u5206\u652f\uff0c\u5f9e\u800c\u8f38\u51fa\u91cd\u8907\u5b50\u96c6\uff1b\u540c\u7406\uff0c\u7b2c\u4e8c\u8f2a\u7684\u5169\u500b \\(4\\) \u4e5f\u6703\u7522\u751f\u91cd\u8907\u5b50\u96c6\u3002</p> <p></p> <p> \u5716 13-13 \u00a0 \u76f8\u7b49\u5143\u7d20\u5c0e\u81f4\u7684\u91cd\u8907\u5b50\u96c6 </p>"},{"location":"chapter_backtracking/subset_sum_problem/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"<p>\u70ba\u89e3\u6c7a\u6b64\u554f\u984c\uff0c\u6211\u5011\u9700\u8981\u9650\u5236\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u4e00\u8f2a\u4e2d\u53ea\u80fd\u88ab\u9078\u64c7\u4e00\u6b21\u3002\u5be6\u73fe\u65b9\u5f0f\u6bd4\u8f03\u5de7\u5999\uff1a\u7531\u65bc\u9663\u5217\u662f\u5df2\u6392\u5e8f\u7684\uff0c\u56e0\u6b64\u76f8\u7b49\u5143\u7d20\u90fd\u662f\u76f8\u9130\u7684\u3002\u9019\u610f\u5473\u8457\u5728\u67d0\u8f2a\u9078\u64c7\u4e2d\uff0c\u82e5\u7576\u524d\u5143\u7d20\u8207\u5176\u5de6\u908a\u5143\u7d20\u76f8\u7b49\uff0c\u5247\u8aaa\u660e\u5b83\u5df2\u7d93\u88ab\u9078\u64c7\u904e\uff0c\u56e0\u6b64\u76f4\u63a5\u8df3\u904e\u7576\u524d\u5143\u7d20\u3002</p> <p>\u8207\u6b64\u540c\u6642\uff0c\u672c\u984c\u898f\u5b9a\u6bcf\u500b\u9663\u5217\u5143\u7d20\u53ea\u80fd\u88ab\u9078\u64c7\u4e00\u6b21\u3002\u5e78\u904b\u7684\u662f\uff0c\u6211\u5011\u4e5f\u53ef\u4ee5\u5229\u7528\u8b8a\u6578 <code>start</code> \u4f86\u6eff\u8db3\u8a72\u7d04\u675f\uff1a\u7576\u505a\u51fa\u9078\u64c7 \\(x_{i}\\) \u5f8c\uff0c\u8a2d\u5b9a\u4e0b\u4e00\u8f2a\u5f9e\u7d22\u5f15 \\(i + 1\\) \u958b\u59cb\u5411\u5f8c\u8d70\u8a2a\u3002\u9019\u6a23\u65e2\u80fd\u53bb\u9664\u91cd\u8907\u5b50\u96c6\uff0c\u4e5f\u80fd\u907f\u514d\u91cd\u8907\u9078\u64c7\u5143\u7d20\u3002</p>"},{"location":"chapter_backtracking/subset_sum_problem/#2_1","title":"2. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig subset_sum_ii.py<pre><code>def backtrack(\n state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n \"\"\"\u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II\"\"\"\n # \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if target == 0:\n res.append(list(state))\n return\n # \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n # \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n # \u526a\u679d\u4e09\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u91cd\u8907\u9078\u64c7\u540c\u4e00\u5143\u7d20\n for i in range(start, len(choices)):\n # \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n # \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if target - choices[i] &lt; 0:\n break\n # \u526a\u679d\u56db\uff1a\u5982\u679c\u8a72\u5143\u7d20\u8207\u5de6\u908a\u5143\u7d20\u76f8\u7b49\uff0c\u8aaa\u660e\u8a72\u641c\u5c0b\u5206\u652f\u91cd\u8907\uff0c\u76f4\u63a5\u8df3\u904e\n if i &gt; start and choices[i] == choices[i - 1]:\n continue\n # \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.append(choices[i])\n # \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target - choices[i], choices, i + 1, res)\n # \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop()\n\ndef subset_sum_ii(nums: list[int], target: int) -&gt; list[list[int]]:\n \"\"\"\u6c42\u89e3\u5b50\u96c6\u548c II\"\"\"\n state = [] # \u72c0\u614b\uff08\u5b50\u96c6\uff09\n nums.sort() # \u5c0d nums \u9032\u884c\u6392\u5e8f\n start = 0 # \u8d70\u8a2a\u8d77\u59cb\u9ede\n res = [] # \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, res)\n return res\n</code></pre> subset_sum_ii.cpp<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(vector&lt;int&gt; &amp;state, int target, vector&lt;int&gt; &amp;choices, int start, vector&lt;vector&lt;int&gt;&gt; &amp;res) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target == 0) {\n res.push_back(state);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u91cd\u8907\u9078\u64c7\u540c\u4e00\u5143\u7d20\n for (int i = start; i &lt; choices.size(); i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8a72\u5143\u7d20\u8207\u5de6\u908a\u5143\u7d20\u76f8\u7b49\uff0c\u8aaa\u660e\u8a72\u641c\u5c0b\u5206\u652f\u91cd\u8907\uff0c\u76f4\u63a5\u8df3\u904e\n if (i &gt; start &amp;&amp; choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.push_back(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop_back();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvector&lt;vector&lt;int&gt;&gt; subsetSumII(vector&lt;int&gt; &amp;nums, int target) {\n vector&lt;int&gt; state; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n sort(nums.begin(), nums.end()); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n int start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n vector&lt;vector&lt;int&gt;&gt; res; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n</code></pre> subset_sum_ii.java<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(List&lt;Integer&gt; state, int target, int[] choices, int start, List&lt;List&lt;Integer&gt;&gt; res) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target == 0) {\n res.add(new ArrayList&lt;&gt;(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u91cd\u8907\u9078\u64c7\u540c\u4e00\u5143\u7d20\n for (int i = start; i &lt; choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8a72\u5143\u7d20\u8207\u5de6\u908a\u5143\u7d20\u76f8\u7b49\uff0c\u8aaa\u660e\u8a72\u641c\u5c0b\u5206\u652f\u91cd\u8907\uff0c\u76f4\u63a5\u8df3\u904e\n if (i &gt; start &amp;&amp; choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.add(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.remove(state.size() - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList&lt;List&lt;Integer&gt;&gt; subsetSumII(int[] nums, int target) {\n List&lt;Integer&gt; state = new ArrayList&lt;&gt;(); // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n Arrays.sort(nums); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n int start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n List&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;&gt;(); // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n</code></pre> subset_sum_ii.cs<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid Backtrack(List&lt;int&gt; state, int target, int[] choices, int start, List&lt;List&lt;int&gt;&gt; res) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target == 0) {\n res.Add(new List&lt;int&gt;(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u91cd\u8907\u9078\u64c7\u540c\u4e00\u5143\u7d20\n for (int i = start; i &lt; choices.Length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8a72\u5143\u7d20\u8207\u5de6\u908a\u5143\u7d20\u76f8\u7b49\uff0c\u8aaa\u660e\u8a72\u641c\u5c0b\u5206\u652f\u91cd\u8907\uff0c\u76f4\u63a5\u8df3\u904e\n if (i &gt; start &amp;&amp; choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.Add(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n Backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.RemoveAt(state.Count - 1);\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList&lt;List&lt;int&gt;&gt; SubsetSumII(int[] nums, int target) {\n List&lt;int&gt; state = []; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n Array.Sort(nums); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n int start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n List&lt;List&lt;int&gt;&gt; res = []; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n Backtrack(state, target, nums, start, res);\n return res;\n}\n</code></pre> subset_sum_ii.go<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrackSubsetSumII(start, target int, state, choices *[]int, res *[][]int) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if target == 0 {\n newState := append([]int{}, *state...)\n *res = append(*res, newState)\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u91cd\u8907\u9078\u64c7\u540c\u4e00\u5143\u7d20\n for i := start; i &lt; len(*choices); i++ {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if target-(*choices)[i] &lt; 0 {\n break\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8a72\u5143\u7d20\u8207\u5de6\u908a\u5143\u7d20\u76f8\u7b49\uff0c\u8aaa\u660e\u8a72\u641c\u5c0b\u5206\u652f\u91cd\u8907\uff0c\u76f4\u63a5\u8df3\u904e\n if i &gt; start &amp;&amp; (*choices)[i] == (*choices)[i-1] {\n continue\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n *state = append(*state, (*choices)[i])\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrackSubsetSumII(i+1, target-(*choices)[i], state, choices, res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n *state = (*state)[:len(*state)-1]\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums []int, target int) [][]int {\n state := make([]int, 0) // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n sort.Ints(nums) // \u5c0d nums \u9032\u884c\u6392\u5e8f\n start := 0 // \u8d70\u8a2a\u8d77\u59cb\u9ede\n res := make([][]int, 0) // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrackSubsetSumII(start, target, &amp;state, &amp;nums, &amp;res)\n return res\n}\n</code></pre> subset_sum_ii.swift<pre><code>/* \u56de\u6eaf\u6f14\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\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if target == 0 {\n res.append(state)\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u91cd\u8907\u9078\u64c7\u540c\u4e00\u5143\u7d20\n for i in choices.indices.dropFirst(start) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if target - choices[i] &lt; 0 {\n break\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8a72\u5143\u7d20\u8207\u5de6\u908a\u5143\u7d20\u76f8\u7b49\uff0c\u8aaa\u660e\u8a72\u641c\u5c0b\u5206\u652f\u91cd\u8907\uff0c\u76f4\u63a5\u8df3\u904e\n if i &gt; start, choices[i] == choices[i - 1] {\n continue\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.append(choices[i])\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state: &amp;state, target: target - choices[i], choices: choices, start: i + 1, res: &amp;res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.removeLast()\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums: [Int], target: Int) -&gt; [[Int]] {\n var state: [Int] = [] // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n let nums = nums.sorted() // \u5c0d nums \u9032\u884c\u6392\u5e8f\n let start = 0 // \u8d70\u8a2a\u8d77\u59cb\u9ede\n var res: [[Int]] = [] // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state: &amp;state, target: target, choices: nums, start: start, res: &amp;res)\n return res\n}\n</code></pre> subset_sum_ii.js<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(state, target, choices, start, res) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target === 0) {\n res.push([...state]);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u91cd\u8907\u9078\u64c7\u540c\u4e00\u5143\u7d20\n for (let i = start; i &lt; choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8a72\u5143\u7d20\u8207\u5de6\u908a\u5143\u7d20\u76f8\u7b49\uff0c\u8aaa\u660e\u8a72\u641c\u5c0b\u5206\u652f\u91cd\u8907\uff0c\u76f4\u63a5\u8df3\u904e\n if (i &gt; start &amp;&amp; choices[i] === choices[i - 1]) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums, target) {\n const state = []; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n nums.sort((a, b) =&gt; a - b); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n const start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n const res = []; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n</code></pre> subset_sum_ii.ts<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(\n state: number[],\n target: number,\n choices: number[],\n start: number,\n res: number[][]\n): void {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target === 0) {\n res.push([...state]);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u91cd\u8907\u9078\u64c7\u540c\u4e00\u5143\u7d20\n for (let i = start; i &lt; choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8a72\u5143\u7d20\u8207\u5de6\u908a\u5143\u7d20\u76f8\u7b49\uff0c\u8aaa\u660e\u8a72\u641c\u5c0b\u5206\u652f\u91cd\u8907\uff0c\u76f4\u63a5\u8df3\u904e\n if (i &gt; start &amp;&amp; choices[i] === choices[i - 1]) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums: number[], target: number): number[][] {\n const state = []; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n nums.sort((a, b) =&gt; a - b); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n const start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n const res = []; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n</code></pre> subset_sum_ii.dart<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(\n List&lt;int&gt; state,\n int target,\n List&lt;int&gt; choices,\n int start,\n List&lt;List&lt;int&gt;&gt; res,\n) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target == 0) {\n res.add(List.from(state));\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u91cd\u8907\u9078\u64c7\u540c\u4e00\u5143\u7d20\n for (int i = start; i &lt; choices.length; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8a72\u5143\u7d20\u8207\u5de6\u908a\u5143\u7d20\u76f8\u7b49\uff0c\u8aaa\u660e\u8a72\u641c\u5c0b\u5206\u652f\u91cd\u8907\uff0c\u76f4\u63a5\u8df3\u904e\n if (i &gt; start &amp;&amp; choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.add(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target - choices[i], choices, i + 1, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.removeLast();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList&lt;List&lt;int&gt;&gt; subsetSumII(List&lt;int&gt; nums, int target) {\n List&lt;int&gt; state = []; // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n nums.sort(); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n int start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n List&lt;List&lt;int&gt;&gt; res = []; // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, res);\n return res;\n}\n</code></pre> subset_sum_ii.rs<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfn backtrack(\n mut state: Vec&lt;i32&gt;,\n target: i32,\n choices: &amp;[i32],\n start: usize,\n res: &amp;mut Vec&lt;Vec&lt;i32&gt;&gt;,\n) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if target == 0 {\n res.push(state);\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u91cd\u8907\u9078\u64c7\u540c\u4e00\u5143\u7d20\n for i in start..choices.len() {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if target - choices[i] &lt; 0 {\n break;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8a72\u5143\u7d20\u8207\u5de6\u908a\u5143\u7d20\u76f8\u7b49\uff0c\u8aaa\u660e\u8a72\u641c\u5c0b\u5206\u652f\u91cd\u8907\uff0c\u76f4\u63a5\u8df3\u904e\n if i &gt; start &amp;&amp; choices[i] == choices[i - 1] {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.push(choices[i]);\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state.clone(), target - choices[i], choices, i, res);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.pop();\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfn subset_sum_ii(nums: &amp;mut [i32], target: i32) -&gt; Vec&lt;Vec&lt;i32&gt;&gt; {\n let state = Vec::new(); // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n nums.sort(); // \u5c0d nums \u9032\u884c\u6392\u5e8f\n let start = 0; // \u8d70\u8a2a\u8d77\u59cb\u9ede\n let mut res = Vec::new(); // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, &amp;mut res);\n res\n}\n</code></pre> subset_sum_ii.c<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target == 0) {\n for (int i = 0; i &lt; stateSize; i++) {\n res[resSize][i] = state[i];\n }\n resColSizes[resSize++] = stateSize;\n return;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u91cd\u8907\u9078\u64c7\u540c\u4e00\u5143\u7d20\n for (int i = start; i &lt; choicesSize; i++) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u8df3\u904e\n if (target - choices[i] &lt; 0) {\n continue;\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8a72\u5143\u7d20\u8207\u5de6\u908a\u5143\u7d20\u76f8\u7b49\uff0c\u8aaa\u660e\u8a72\u641c\u5c0b\u5206\u652f\u91cd\u8907\uff0c\u76f4\u63a5\u8df3\u904e\n if (i &gt; start &amp;&amp; choices[i] == choices[i - 1]) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state[stateSize] = choices[i];\n stateSize++;\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(target - choices[i], choices, choicesSize, i + 1);\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n stateSize--;\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvoid subsetSumII(int *nums, int numsSize, int target) {\n // \u5c0d nums \u9032\u884c\u6392\u5e8f\n qsort(nums, numsSize, sizeof(int), cmp);\n // \u958b\u59cb\u56de\u6eaf\n backtrack(target, nums, numsSize, 0);\n}\n</code></pre> subset_sum_ii.kt<pre><code>/* \u56de\u6eaf\u6f14\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfun backtrack(\n state: MutableList&lt;Int&gt;,\n target: Int,\n choices: IntArray,\n start: Int,\n res: MutableList&lt;MutableList&lt;Int&gt;?&gt;\n) {\n // \u5b50\u96c6\u548c\u7b49\u65bc target \u6642\uff0c\u8a18\u9304\u89e3\n if (target == 0) {\n res.add(state.toMutableList())\n return\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n // \u526a\u679d\u4e8c\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u751f\u6210\u91cd\u8907\u5b50\u96c6\n // \u526a\u679d\u4e09\uff1a\u5f9e start \u958b\u59cb\u8d70\u8a2a\uff0c\u907f\u514d\u91cd\u8907\u9078\u64c7\u540c\u4e00\u5143\u7d20\n for (i in start..&lt;choices.size) {\n // \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u904e target \uff0c\u5247\u76f4\u63a5\u7d50\u675f\u8ff4\u5708\n // \u9019\u662f\u56e0\u70ba\u9663\u5217\u5df2\u6392\u5e8f\uff0c\u5f8c\u908a\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u904e target\n if (target - choices[i] &lt; 0) {\n break\n }\n // \u526a\u679d\u56db\uff1a\u5982\u679c\u8a72\u5143\u7d20\u8207\u5de6\u908a\u5143\u7d20\u76f8\u7b49\uff0c\u8aaa\u660e\u8a72\u641c\u5c0b\u5206\u652f\u91cd\u8907\uff0c\u76f4\u63a5\u8df3\u904e\n if (i &gt; start &amp;&amp; choices[i] == choices[i - 1]) {\n continue\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0 target, start\n state.add(choices[i])\n // \u9032\u884c\u4e0b\u4e00\u8f2a\u9078\u64c7\n backtrack(state, target - choices[i], choices, i + 1, res)\n // \u56de\u9000\uff1a\u64a4\u92b7\u9078\u64c7\uff0c\u6062\u5fa9\u5230\u4e4b\u524d\u7684\u72c0\u614b\n state.removeAt(state.size - 1)\n }\n}\n\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfun subsetSumII(nums: IntArray, target: Int): MutableList&lt;MutableList&lt;Int&gt;?&gt; {\n val state = mutableListOf&lt;Int&gt;() // \u72c0\u614b\uff08\u5b50\u96c6\uff09\n nums.sort() // \u5c0d nums \u9032\u884c\u6392\u5e8f\n val start = 0 // \u8d70\u8a2a\u8d77\u59cb\u9ede\n val res = mutableListOf&lt;MutableList&lt;Int&gt;?&gt;() // \u7d50\u679c\u4e32\u5217\uff08\u5b50\u96c6\u4e32\u5217\uff09\n backtrack(state, target, nums, start, res)\n return res\n}\n</code></pre> subset_sum_ii.rb<pre><code>[class]{}-[func]{backtrack}\n\n[class]{}-[func]{subset_sum_ii}\n</code></pre> subset_sum_ii.zig<pre><code>[class]{}-[func]{backtrack}\n\n[class]{}-[func]{subsetSumII}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 13-14 \u5c55\u793a\u4e86\u9663\u5217 \\([4, 4, 5]\\) \u548c\u76ee\u6a19\u5143\u7d20 \\(9\\) \u7684\u56de\u6eaf\u904e\u7a0b\uff0c\u5171\u5305\u542b\u56db\u7a2e\u526a\u679d\u64cd\u4f5c\u3002\u8acb\u4f60\u5c07\u5716\u793a\u8207\u7a0b\u5f0f\u78bc\u8a3b\u91cb\u76f8\u7d50\u5408\uff0c\u7406\u89e3\u6574\u500b\u641c\u5c0b\u904e\u7a0b\uff0c\u4ee5\u53ca\u6bcf\u7a2e\u526a\u679d\u64cd\u4f5c\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002</p> <p></p> <p> \u5716 13-14 \u00a0 \u5b50\u96c6\u548c II \u56de\u6eaf\u904e\u7a0b </p>"},{"location":"chapter_backtracking/summary/","title":"13.5 \u00a0 \u5c0f\u7d50","text":""},{"location":"chapter_backtracking/summary/#1","title":"1. \u00a0 \u91cd\u9ede\u56de\u9867","text":"<ul> <li>\u56de\u6eaf\u6f14\u7b97\u6cd5\u672c\u8cea\u662f\u7aae\u8209\u6cd5\uff0c\u900f\u904e\u5c0d\u89e3\u7a7a\u9593\u9032\u884c\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u4f86\u5c0b\u627e\u7b26\u5408\u689d\u4ef6\u7684\u89e3\u3002\u5728\u641c\u5c0b\u904e\u7a0b\u4e2d\uff0c\u9047\u5230\u6eff\u8db3\u689d\u4ef6\u7684\u89e3\u5247\u8a18\u9304\uff0c\u76f4\u81f3\u627e\u5230\u6240\u6709\u89e3\u6216\u8d70\u8a2a\u5b8c\u6210\u5f8c\u7d50\u675f\u3002</li> <li>\u56de\u6eaf\u6f14\u7b97\u6cd5\u7684\u641c\u5c0b\u904e\u7a0b\u5305\u62ec\u5617\u8a66\u8207\u56de\u9000\u5169\u500b\u90e8\u5206\u3002\u5b83\u900f\u904e\u6df1\u5ea6\u512a\u5148\u641c\u5c0b\u4f86\u5617\u8a66\u5404\u7a2e\u9078\u64c7\uff0c\u7576\u9047\u5230\u4e0d\u6eff\u8db3\u7d04\u675f\u689d\u4ef6\u7684\u60c5\u6cc1\u6642\uff0c\u5247\u64a4\u92b7\u4e0a\u4e00\u6b65\u7684\u9078\u64c7\uff0c\u9000\u56de\u5230\u4e4b\u524d\u7684\u72c0\u614b\uff0c\u4e26\u7e7c\u7e8c\u5617\u8a66\u5176\u4ed6\u9078\u64c7\u3002\u5617\u8a66\u8207\u56de\u9000\u662f\u5169\u500b\u65b9\u5411\u76f8\u53cd\u7684\u64cd\u4f5c\u3002</li> <li>\u56de\u6eaf\u554f\u984c\u901a\u5e38\u5305\u542b\u591a\u500b\u7d04\u675f\u689d\u4ef6\uff0c\u5b83\u5011\u53ef\u7528\u65bc\u5be6\u73fe\u526a\u679d\u64cd\u4f5c\u3002\u526a\u679d\u53ef\u4ee5\u63d0\u524d\u7d50\u675f\u4e0d\u5fc5\u8981\u7684\u641c\u5c0b\u5206\u652f\uff0c\u5927\u5e45\u63d0\u5347\u641c\u5c0b\u6548\u7387\u3002</li> <li>\u56de\u6eaf\u6f14\u7b97\u6cd5\u4e3b\u8981\u53ef\u7528\u65bc\u89e3\u6c7a\u641c\u5c0b\u554f\u984c\u548c\u7d04\u675f\u6eff\u8db3\u554f\u984c\u3002\u7d44\u5408\u6700\u4f73\u5316\u554f\u984c\u96d6\u7136\u53ef\u4ee5\u7528\u56de\u6eaf\u6f14\u7b97\u6cd5\u89e3\u6c7a\uff0c\u4f46\u5f80\u5f80\u5b58\u5728\u6548\u7387\u66f4\u9ad8\u6216\u6548\u679c\u66f4\u597d\u7684\u89e3\u6cd5\u3002</li> <li>\u5168\u6392\u5217\u554f\u984c\u65e8\u5728\u641c\u5c0b\u7d66\u5b9a\u96c6\u5408\u5143\u7d20\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002\u6211\u5011\u85c9\u52a9\u4e00\u500b\u9663\u5217\u4f86\u8a18\u9304\u6bcf\u500b\u5143\u7d20\u662f\u5426\u88ab\u9078\u64c7\uff0c\u526a\u6389\u91cd\u8907\u9078\u64c7\u540c\u4e00\u5143\u7d20\u7684\u641c\u5c0b\u5206\u652f\uff0c\u78ba\u4fdd\u6bcf\u500b\u5143\u7d20\u53ea\u88ab\u9078\u64c7\u4e00\u6b21\u3002</li> <li>\u5728\u5168\u6392\u5217\u554f\u984c\u4e2d\uff0c\u5982\u679c\u96c6\u5408\u4e2d\u5b58\u5728\u91cd\u8907\u5143\u7d20\uff0c\u5247\u6700\u7d42\u7d50\u679c\u6703\u51fa\u73fe\u91cd\u8907\u6392\u5217\u3002\u6211\u5011\u9700\u8981\u7d04\u675f\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u8f2a\u4e2d\u53ea\u80fd\u88ab\u9078\u64c7\u4e00\u6b21\uff0c\u9019\u901a\u5e38\u85c9\u52a9\u4e00\u500b\u96dc\u6e4a\u96c6\u5408\u4f86\u5be6\u73fe\u3002</li> <li>\u5b50\u96c6\u548c\u554f\u984c\u7684\u76ee\u6a19\u662f\u5728\u7d66\u5b9a\u96c6\u5408\u4e2d\u627e\u5230\u548c\u70ba\u76ee\u6a19\u503c\u7684\u6240\u6709\u5b50\u96c6\u3002\u96c6\u5408\u4e0d\u5340\u5206\u5143\u7d20\u9806\u5e8f\uff0c\u800c\u641c\u5c0b\u904e\u7a0b\u6703\u8f38\u51fa\u6240\u6709\u9806\u5e8f\u7684\u7d50\u679c\uff0c\u7522\u751f\u91cd\u8907\u5b50\u96c6\u3002\u6211\u5011\u5728\u56de\u6eaf\u524d\u5c07\u8cc7\u6599\u9032\u884c\u6392\u5e8f\uff0c\u4e26\u8a2d\u5b9a\u4e00\u500b\u8b8a\u6578\u4f86\u6307\u793a\u6bcf\u4e00\u8f2a\u7684\u8d70\u8a2a\u8d77\u59cb\u9ede\uff0c\u5f9e\u800c\u5c07\u751f\u6210\u91cd\u8907\u5b50\u96c6\u7684\u641c\u5c0b\u5206\u652f\u9032\u884c\u526a\u679d\u3002</li> <li>\u5c0d\u65bc\u5b50\u96c6\u548c\u554f\u984c\uff0c\u9663\u5217\u4e2d\u7684\u76f8\u7b49\u5143\u7d20\u6703\u7522\u751f\u91cd\u8907\u96c6\u5408\u3002\u6211\u5011\u5229\u7528\u9663\u5217\u5df2\u6392\u5e8f\u7684\u524d\u7f6e\u689d\u4ef6\uff0c\u900f\u904e\u5224\u65b7\u76f8\u9130\u5143\u7d20\u662f\u5426\u76f8\u7b49\u5be6\u73fe\u526a\u679d\uff0c\u5f9e\u800c\u78ba\u4fdd\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u8f2a\u4e2d\u53ea\u80fd\u88ab\u9078\u4e2d\u4e00\u6b21\u3002</li> <li>\\(n\\) \u7687\u540e\u554f\u984c\u65e8\u5728\u5c0b\u627e\u5c07 \\(n\\) \u500b\u7687\u540e\u653e\u7f6e\u5230 \\(n \\times n\\) \u5c3a\u5bf8\u68cb\u76e4\u4e0a\u7684\u65b9\u6848\uff0c\u8981\u6c42\u6240\u6709\u7687\u540e\u5169\u5169\u4e4b\u9593\u7121\u6cd5\u653b\u64ca\u5c0d\u65b9\u3002\u8a72\u554f\u984c\u7684\u7d04\u675f\u689d\u4ef6\u6709\u884c\u7d04\u675f\u3001\u5217\u7d04\u675f\u3001\u4e3b\u5c0d\u89d2\u7dda\u548c\u6b21\u5c0d\u89d2\u7dda\u7d04\u675f\u3002\u70ba\u6eff\u8db3\u884c\u7d04\u675f\uff0c\u6211\u5011\u63a1\u7528\u6309\u884c\u653e\u7f6e\u7684\u7b56\u7565\uff0c\u4fdd\u8b49\u6bcf\u4e00\u884c\u653e\u7f6e\u4e00\u500b\u7687\u540e\u3002</li> <li>\u5217\u7d04\u675f\u548c\u5c0d\u89d2\u7dda\u7d04\u675f\u7684\u8655\u7406\u65b9\u5f0f\u985e\u4f3c\u3002\u5c0d\u65bc\u5217\u7d04\u675f\uff0c\u6211\u5011\u5229\u7528\u4e00\u500b\u9663\u5217\u4f86\u8a18\u9304\u6bcf\u4e00\u5217\u662f\u5426\u6709\u7687\u540e\uff0c\u5f9e\u800c\u6307\u793a\u9078\u4e2d\u7684\u683c\u5b50\u662f\u5426\u5408\u6cd5\u3002\u5c0d\u65bc\u5c0d\u89d2\u7dda\u7d04\u675f\uff0c\u6211\u5011\u85c9\u52a9\u5169\u500b\u9663\u5217\u4f86\u5206\u5225\u8a18\u9304\u8a72\u4e3b\u3001\u6b21\u5c0d\u89d2\u7dda\u4e0a\u662f\u5426\u5b58\u5728\u7687\u540e\uff1b\u96e3\u9ede\u5728\u65bc\u627e\u8655\u5728\u5230\u540c\u4e00\u4e3b\uff08\u526f\uff09\u5c0d\u89d2\u7dda\u4e0a\u683c\u5b50\u6eff\u8db3\u7684\u884c\u5217\u7d22\u5f15\u898f\u5f8b\u3002</li> </ul>"},{"location":"chapter_backtracking/summary/#2-q-a","title":"2. \u00a0 Q &amp; A","text":"<p>Q\uff1a\u600e\u9ebc\u7406\u89e3\u56de\u6eaf\u548c\u905e\u8ff4\u7684\u95dc\u4fc2\uff1f</p> <p>\u7e3d\u7684\u4f86\u770b\uff0c\u56de\u6eaf\u662f\u4e00\u7a2e\u201c\u6f14\u7b97\u6cd5\u7b56\u7565\u201d\uff0c\u800c\u905e\u8ff4\u66f4\u50cf\u662f\u4e00\u500b\u201c\u5de5\u5177\u201d\u3002</p> <ul> <li>\u56de\u6eaf\u6f14\u7b97\u6cd5\u901a\u5e38\u57fa\u65bc\u905e\u8ff4\u5be6\u73fe\u3002\u7136\u800c\uff0c\u56de\u6eaf\u662f\u905e\u8ff4\u7684\u61c9\u7528\u5834\u666f\u4e4b\u4e00\uff0c\u662f\u905e\u8ff4\u5728\u641c\u5c0b\u554f\u984c\u4e2d\u7684\u61c9\u7528\u3002</li> <li>\u905e\u8ff4\u7684\u7d50\u69cb\u9ad4\u73fe\u4e86\u201c\u5b50\u554f\u984c\u5206\u89e3\u201d\u7684\u89e3\u984c\u7bc4\u5f0f\uff0c\u5e38\u7528\u65bc\u89e3\u6c7a\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52d5\u614b\u898f\u5283\uff08\u8a18\u61b6\u5316\u905e\u8ff4\uff09\u7b49\u554f\u984c\u3002</li> </ul>"},{"location":"chapter_computational_complexity/","title":"\u7b2c 2 \u7ae0 \u00a0 \u8907\u96dc\u5ea6\u5206\u6790","text":"<p>Abstract</p> <p>\u8907\u96dc\u5ea6\u5206\u6790\u7336\u5982\u6d69\u701a\u7684\u6f14\u7b97\u6cd5\u5b87\u5b99\u4e2d\u7684\u6642\u7a7a\u56ae\u5c0e\u3002</p> <p>\u5b83\u5e36\u9818\u6211\u5011\u5728\u6642\u9593\u8207\u7a7a\u9593\u9019\u5169\u500b\u7dad\u5ea6\u4e0a\u6df1\u5165\u63a2\u7d22\uff0c\u5c0b\u627e\u66f4\u512a\u96c5\u7684\u89e3\u6c7a\u65b9\u6848\u3002</p>"},{"location":"chapter_computational_complexity/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>2.1 \u00a0 \u6f14\u7b97\u6cd5\u6548\u7387\u8a55\u4f30</li> <li>2.2 \u00a0 \u8fed\u4ee3\u8207\u905e\u8ff4</li> <li>2.3 \u00a0 \u6642\u9593\u8907\u96dc\u5ea6</li> <li>2.4 \u00a0 \u7a7a\u9593\u8907\u96dc\u5ea6</li> <li>2.5 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_computational_complexity/iteration_and_recursion/","title":"2.2 \u00a0 \u8fed\u4ee3\u8207\u905e\u8ff4","text":"<p>\u5728\u6f14\u7b97\u6cd5\u4e2d\uff0c\u91cd\u8907\u57f7\u884c\u67d0\u500b\u4efb\u52d9\u662f\u5f88\u5e38\u898b\u7684\uff0c\u5b83\u8207\u8907\u96dc\u5ea6\u5206\u6790\u606f\u606f\u76f8\u95dc\u3002\u56e0\u6b64\uff0c\u5728\u4ecb\u7d39\u6642\u9593\u8907\u96dc\u5ea6\u548c\u7a7a\u9593\u8907\u96dc\u5ea6\u4e4b\u524d\uff0c\u6211\u5011\u5148\u4f86\u4e86\u89e3\u5982\u4f55\u5728\u7a0b\u5f0f\u4e2d\u5be6\u73fe\u91cd\u8907\u57f7\u884c\u4efb\u52d9\uff0c\u5373\u5169\u7a2e\u57fa\u672c\u7684\u7a0b\u5f0f\u63a7\u5236\u7d50\u69cb\uff1a\u8fed\u4ee3\u3001\u905e\u8ff4\u3002</p>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#221","title":"2.2.1 \u00a0 \u8fed\u4ee3","text":"<p>\u8fed\u4ee3\uff08iteration\uff09\u662f\u4e00\u7a2e\u91cd\u8907\u57f7\u884c\u67d0\u500b\u4efb\u52d9\u7684\u63a7\u5236\u7d50\u69cb\u3002\u5728\u8fed\u4ee3\u4e2d\uff0c\u7a0b\u5f0f\u6703\u5728\u6eff\u8db3\u4e00\u5b9a\u7684\u689d\u4ef6\u4e0b\u91cd\u8907\u57f7\u884c\u67d0\u6bb5\u7a0b\u5f0f\u78bc\uff0c\u76f4\u5230\u9019\u500b\u689d\u4ef6\u4e0d\u518d\u6eff\u8db3\u3002</p>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for","title":"1. \u00a0 for \u8ff4\u5708","text":"<p><code>for</code> \u8ff4\u5708\u662f\u6700\u5e38\u898b\u7684\u8fed\u4ee3\u5f62\u5f0f\u4e4b\u4e00\uff0c\u9069\u5408\u5728\u9810\u5148\u77e5\u9053\u8fed\u4ee3\u6b21\u6578\u6642\u4f7f\u7528\u3002</p> <p>\u4ee5\u4e0b\u51fd\u5f0f\u57fa\u65bc <code>for</code> \u8ff4\u5708\u5be6\u73fe\u4e86\u6c42\u548c \\(1 + 2 + \\dots + n\\) \uff0c\u6c42\u548c\u7d50\u679c\u4f7f\u7528\u8b8a\u6578 <code>res</code> \u8a18\u9304\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPython \u4e2d <code>range(a, b)</code> \u5c0d\u61c9\u7684\u5340\u9593\u662f\u201c\u5de6\u9589\u53f3\u958b\u201d\u7684\uff0c\u5c0d\u61c9\u7684\u8d70\u8a2a\u7bc4\u570d\u70ba \\(a, a + 1, \\dots, b-1\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig iteration.py<pre><code>def for_loop(n: int) -&gt; int:\n \"\"\"for \u8ff4\u5708\"\"\"\n res = 0\n # \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for i in range(1, n + 1):\n res += i\n return res\n</code></pre> iteration.cpp<pre><code>/* for \u8ff4\u5708 */\nint forLoop(int n) {\n int res = 0;\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i &lt;= n; ++i) {\n res += i;\n }\n return res;\n}\n</code></pre> iteration.java<pre><code>/* for \u8ff4\u5708 */\nint forLoop(int n) {\n int res = 0;\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i &lt;= n; i++) {\n res += i;\n }\n return res;\n}\n</code></pre> iteration.cs<pre><code>/* for \u8ff4\u5708 */\nint ForLoop(int n) {\n int res = 0;\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i &lt;= n; i++) {\n res += i;\n }\n return res;\n}\n</code></pre> iteration.go<pre><code>/* for \u8ff4\u5708 */\nfunc forLoop(n int) int {\n res := 0\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for i := 1; i &lt;= n; i++ {\n res += i\n }\n return res\n}\n</code></pre> iteration.swift<pre><code>/* for \u8ff4\u5708 */\nfunc forLoop(n: Int) -&gt; Int {\n var res = 0\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for i in 1 ... n {\n res += i\n }\n return res\n}\n</code></pre> iteration.js<pre><code>/* for \u8ff4\u5708 */\nfunction forLoop(n) {\n let res = 0;\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for (let i = 1; i &lt;= n; i++) {\n res += i;\n }\n return res;\n}\n</code></pre> iteration.ts<pre><code>/* for \u8ff4\u5708 */\nfunction forLoop(n: number): number {\n let res = 0;\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for (let i = 1; i &lt;= n; i++) {\n res += i;\n }\n return res;\n}\n</code></pre> iteration.dart<pre><code>/* for \u8ff4\u5708 */\nint forLoop(int n) {\n int res = 0;\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i &lt;= n; i++) {\n res += i;\n }\n return res;\n}\n</code></pre> iteration.rs<pre><code>/* for \u8ff4\u5708 */\nfn for_loop(n: i32) -&gt; i32 {\n let mut res = 0;\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for i in 1..=n {\n res += i;\n }\n res\n}\n</code></pre> iteration.c<pre><code>/* for \u8ff4\u5708 */\nint forLoop(int n) {\n int res = 0;\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i &lt;= n; i++) {\n res += i;\n }\n return res;\n}\n</code></pre> iteration.kt<pre><code>/* for \u8ff4\u5708 */\nfun forLoop(n: Int): Int {\n var res = 0\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for (i in 1..n) {\n res += i\n }\n return res\n}\n</code></pre> iteration.rb<pre><code>### for \u8ff4\u5708 ###\ndef for_loop(n)\n res = 0\n\n # \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for i in 1..n\n res += i\n end\n\n res\nend\n</code></pre> iteration.zig<pre><code>// for \u8ff4\u5708\nfn forLoop(n: usize) i32 {\n var res: i32 = 0;\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for (1..n+1) |i| {\n res = res + @as(i32, @intCast(i));\n }\n return res;\n} \n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 2-1 \u662f\u8a72\u6c42\u548c\u51fd\u5f0f\u7684\u6d41\u7a0b\u6846\u5716\u3002</p> <p></p> <p> \u5716 2-1 \u00a0 \u6c42\u548c\u51fd\u5f0f\u7684\u6d41\u7a0b\u6846\u5716 </p> <p>\u6b64\u6c42\u548c\u51fd\u5f0f\u7684\u64cd\u4f5c\u6578\u91cf\u8207\u8f38\u5165\u8cc7\u6599\u5927\u5c0f \\(n\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8aaa\u6210\u201c\u7dda\u6027\u95dc\u4fc2\u201d\u3002\u5be6\u969b\u4e0a\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u63cf\u8ff0\u7684\u5c31\u662f\u9019\u500b\u201c\u7dda\u6027\u95dc\u4fc2\u201d\u3002\u76f8\u95dc\u5167\u5bb9\u5c07\u6703\u5728\u4e0b\u4e00\u7bc0\u4e2d\u8a73\u7d30\u4ecb\u7d39\u3002</p>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while","title":"2. \u00a0 while \u8ff4\u5708","text":"<p>\u8207 <code>for</code> \u8ff4\u5708\u985e\u4f3c\uff0c<code>while</code> \u8ff4\u5708\u4e5f\u662f\u4e00\u7a2e\u5be6\u73fe\u8fed\u4ee3\u7684\u65b9\u6cd5\u3002\u5728 <code>while</code> \u8ff4\u5708\u4e2d\uff0c\u7a0b\u5f0f\u6bcf\u8f2a\u90fd\u6703\u5148\u6aa2\u67e5\u689d\u4ef6\uff0c\u5982\u679c\u689d\u4ef6\u70ba\u771f\uff0c\u5247\u7e7c\u7e8c\u57f7\u884c\uff0c\u5426\u5247\u5c31\u7d50\u675f\u8ff4\u5708\u3002</p> <p>\u4e0b\u9762\u6211\u5011\u7528 <code>while</code> \u8ff4\u5708\u4f86\u5be6\u73fe\u6c42\u548c \\(1 + 2 + \\dots + n\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig iteration.py<pre><code>def while_loop(n: int) -&gt; int:\n \"\"\"while \u8ff4\u5708\"\"\"\n res = 0\n i = 1 # \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n # \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n while i &lt;= n:\n res += i\n i += 1 # \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n return res\n</code></pre> iteration.cpp<pre><code>/* while \u8ff4\u5708 */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n while (i &lt;= n) {\n res += i;\n i++; // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n }\n return res;\n}\n</code></pre> iteration.java<pre><code>/* while \u8ff4\u5708 */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n while (i &lt;= n) {\n res += i;\n i++; // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n }\n return res;\n}\n</code></pre> iteration.cs<pre><code>/* while \u8ff4\u5708 */\nint WhileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n while (i &lt;= n) {\n res += i;\n i += 1; // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n }\n return res;\n}\n</code></pre> iteration.go<pre><code>/* while \u8ff4\u5708 */\nfunc whileLoop(n int) int {\n res := 0\n // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n i := 1\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n for i &lt;= n {\n res += i\n // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i++\n }\n return res\n}\n</code></pre> iteration.swift<pre><code>/* while \u8ff4\u5708 */\nfunc whileLoop(n: Int) -&gt; Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n while i &lt;= n {\n res += i\n i += 1 // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n }\n return res\n}\n</code></pre> iteration.js<pre><code>/* while \u8ff4\u5708 */\nfunction whileLoop(n) {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n while (i &lt;= n) {\n res += i;\n i++; // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n }\n return res;\n}\n</code></pre> iteration.ts<pre><code>/* while \u8ff4\u5708 */\nfunction whileLoop(n: number): number {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n while (i &lt;= n) {\n res += i;\n i++; // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n }\n return res;\n}\n</code></pre> iteration.dart<pre><code>/* while \u8ff4\u5708 */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n while (i &lt;= n) {\n res += i;\n i++; // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n }\n return res;\n}\n</code></pre> iteration.rs<pre><code>/* while \u8ff4\u5708 */\nfn while_loop(n: i32) -&gt; i32 {\n let mut res = 0;\n let mut i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n while i &lt;= n {\n res += i;\n i += 1; // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n }\n res\n}\n</code></pre> iteration.c<pre><code>/* while \u8ff4\u5708 */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n while (i &lt;= n) {\n res += i;\n i++; // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n }\n return res;\n}\n</code></pre> iteration.kt<pre><code>/* while \u8ff4\u5708 */\nfun whileLoop(n: Int): Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n while (i &lt;= n) {\n res += i\n i++ // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n }\n return res\n}\n</code></pre> iteration.rb<pre><code>### while \u8ff4\u5708 ###\ndef while_loop(n)\n res = 0\n i = 1 # \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n\n # \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n while i &lt;= n\n res += i\n i += 1 # \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n end\n\n res\nend\n</code></pre> iteration.zig<pre><code>// while \u8ff4\u5708\nfn whileLoop(n: i32) i32 {\n var res: i32 = 0;\n var i: i32 = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 2, ..., n-1, n\n while (i &lt;= n) {\n res += @intCast(i);\n i += 1;\n }\n return res;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p><code>while</code> \u8ff4\u5708\u6bd4 <code>for</code> \u8ff4\u5708\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\u3002\u5728 <code>while</code> \u8ff4\u5708\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u81ea\u7531\u5730\u8a2d\u8a08\u689d\u4ef6\u8b8a\u6578\u7684\u521d\u59cb\u5316\u548c\u66f4\u65b0\u6b65\u9a5f\u3002</p> <p>\u4f8b\u5982\u5728\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u689d\u4ef6\u8b8a\u6578 \\(i\\) \u6bcf\u8f2a\u9032\u884c\u5169\u6b21\u66f4\u65b0\uff0c\u9019\u7a2e\u60c5\u6cc1\u5c31\u4e0d\u592a\u65b9\u4fbf\u7528 <code>for</code> \u8ff4\u5708\u5be6\u73fe\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig iteration.py<pre><code>def while_loop_ii(n: int) -&gt; int:\n \"\"\"while \u8ff4\u5708\uff08\u5169\u6b21\u66f4\u65b0\uff09\"\"\"\n res = 0\n i = 1 # \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n # \u8ff4\u5708\u6c42\u548c 1, 4, 10, ...\n while i &lt;= n:\n res += i\n # \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i += 1\n i *= 2\n return res\n</code></pre> iteration.cpp<pre><code>/* while \u8ff4\u5708\uff08\u5169\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 4, 10, ...\n while (i &lt;= n) {\n res += i;\n // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i++;\n i *= 2;\n }\n return res;\n}\n</code></pre> iteration.java<pre><code>/* while \u8ff4\u5708\uff08\u5169\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 4, 10, ...\n while (i &lt;= n) {\n res += i;\n // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i++;\n i *= 2;\n }\n return res;\n}\n</code></pre> iteration.cs<pre><code>/* while \u8ff4\u5708\uff08\u5169\u6b21\u66f4\u65b0\uff09 */\nint WhileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 4, 10, ...\n while (i &lt;= n) {\n res += i;\n // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i += 1; \n i *= 2;\n }\n return res;\n}\n</code></pre> iteration.go<pre><code>/* while \u8ff4\u5708\uff08\u5169\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n int) int {\n res := 0\n // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n i := 1\n // \u8ff4\u5708\u6c42\u548c 1, 4, 10, ...\n for i &lt;= n {\n res += i\n // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i++\n i *= 2\n }\n return res\n}\n</code></pre> iteration.swift<pre><code>/* while \u8ff4\u5708\uff08\u5169\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n: Int) -&gt; Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 4, 10, ...\n while i &lt;= n {\n res += i\n // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i += 1\n i *= 2\n }\n return res\n}\n</code></pre> iteration.js<pre><code>/* while \u8ff4\u5708\uff08\u5169\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n) {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 4, 10, ...\n while (i &lt;= n) {\n res += i;\n // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i++;\n i *= 2;\n }\n return res;\n}\n</code></pre> iteration.ts<pre><code>/* while \u8ff4\u5708\uff08\u5169\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n: number): number {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 4, 10, ...\n while (i &lt;= n) {\n res += i;\n // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i++;\n i *= 2;\n }\n return res;\n}\n</code></pre> iteration.dart<pre><code>/* while \u8ff4\u5708\uff08\u5169\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 4, 10, ...\n while (i &lt;= n) {\n res += i;\n // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i++;\n i *= 2;\n }\n return res;\n}\n</code></pre> iteration.rs<pre><code>/* while \u8ff4\u5708\uff08\u5169\u6b21\u66f4\u65b0\uff09 */\nfn while_loop_ii(n: i32) -&gt; i32 {\n let mut res = 0;\n let mut i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n\n // \u8ff4\u5708\u6c42\u548c 1, 4, 10, ...\n while i &lt;= n {\n res += i;\n // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i += 1;\n i *= 2;\n }\n res\n}\n</code></pre> iteration.c<pre><code>/* while \u8ff4\u5708\uff08\u5169\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 4, 10, ...\n while (i &lt;= n) {\n res += i;\n // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i++;\n i *= 2;\n }\n return res;\n}\n</code></pre> iteration.kt<pre><code>/* while \u8ff4\u5708\uff08\u5169\u6b21\u66f4\u65b0\uff09 */\nfun whileLoopII(n: Int): Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 4, 10, ...\n while (i &lt;= n) {\n res += i\n // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i++\n i *= 2\n }\n return res\n}\n</code></pre> iteration.rb<pre><code>### while \u8ff4\u5708\uff08\u5169\u6b21\u66f4\u65b0\uff09###\ndef while_loop_ii(n)\n res = 0\n i = 1 # \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n\n # \u8ff4\u5708\u6c42\u548c 1, 4, 10, ...\n while i &lt;= n\n res += i\n # \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i += 1\n i *= 2\n end\n\n res\nend\n</code></pre> iteration.zig<pre><code>// while \u8ff4\u5708\uff08\u5169\u6b21\u66f4\u65b0\uff09\nfn whileLoopII(n: i32) i32 {\n var res: i32 = 0;\n var i: i32 = 1; // \u521d\u59cb\u5316\u689d\u4ef6\u8b8a\u6578\n // \u8ff4\u5708\u6c42\u548c 1, 4, 10, ...\n while (i &lt;= n) {\n res += @intCast(i);\n // \u66f4\u65b0\u689d\u4ef6\u8b8a\u6578\n i += 1;\n i *= 2;\n }\n return res;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u7e3d\u7684\u4f86\u8aaa\uff0c<code>for</code> \u8ff4\u5708\u7684\u7a0b\u5f0f\u78bc\u66f4\u52a0\u7dca\u6e4a\uff0c<code>while</code> \u8ff4\u5708\u66f4\u52a0\u9748\u6d3b\uff0c\u5169\u8005\u90fd\u53ef\u4ee5\u5be6\u73fe\u8fed\u4ee3\u7d50\u69cb\u3002\u9078\u64c7\u4f7f\u7528\u54ea\u4e00\u500b\u61c9\u8a72\u6839\u64da\u7279\u5b9a\u554f\u984c\u7684\u9700\u6c42\u4f86\u6c7a\u5b9a\u3002</p>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#3","title":"3. \u00a0 \u5de2\u72c0\u8ff4\u5708","text":"<p>\u6211\u5011\u53ef\u4ee5\u5728\u4e00\u500b\u8ff4\u5708\u7d50\u69cb\u5167\u5de2\u72c0\u53e6\u4e00\u500b\u8ff4\u5708\u7d50\u69cb\uff0c\u4e0b\u9762\u4ee5 <code>for</code> \u8ff4\u5708\u70ba\u4f8b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig iteration.py<pre><code>def nested_for_loop(n: int) -&gt; str:\n \"\"\"\u96d9\u5c64 for \u8ff4\u5708\"\"\"\n res = \"\"\n # \u8ff4\u5708 i = 1, 2, ..., n-1, n\n for i in range(1, n + 1):\n # \u8ff4\u5708 j = 1, 2, ..., n-1, n\n for j in range(1, n + 1):\n res += f\"({i}, {j}), \"\n return res\n</code></pre> iteration.cpp<pre><code>/* \u96d9\u5c64 for \u8ff4\u5708 */\nstring nestedForLoop(int n) {\n ostringstream res;\n // \u8ff4\u5708 i = 1, 2, ..., n-1, n\n for (int i = 1; i &lt;= n; ++i) {\n // \u8ff4\u5708 j = 1, 2, ..., n-1, n\n for (int j = 1; j &lt;= n; ++j) {\n res &lt;&lt; \"(\" &lt;&lt; i &lt;&lt; \", \" &lt;&lt; j &lt;&lt; \"), \";\n }\n }\n return res.str();\n}\n</code></pre> iteration.java<pre><code>/* \u96d9\u5c64 for \u8ff4\u5708 */\nString nestedForLoop(int n) {\n StringBuilder res = new StringBuilder();\n // \u8ff4\u5708 i = 1, 2, ..., n-1, n\n for (int i = 1; i &lt;= n; i++) {\n // \u8ff4\u5708 j = 1, 2, ..., n-1, n\n for (int j = 1; j &lt;= n; j++) {\n res.append(\"(\" + i + \", \" + j + \"), \");\n }\n }\n return res.toString();\n}\n</code></pre> iteration.cs<pre><code>/* \u96d9\u5c64 for \u8ff4\u5708 */\nstring NestedForLoop(int n) {\n StringBuilder res = new();\n // \u8ff4\u5708 i = 1, 2, ..., n-1, n\n for (int i = 1; i &lt;= n; i++) {\n // \u8ff4\u5708 j = 1, 2, ..., n-1, n\n for (int j = 1; j &lt;= n; j++) {\n res.Append($\"({i}, {j}), \");\n }\n }\n return res.ToString();\n}\n</code></pre> iteration.go<pre><code>/* \u96d9\u5c64 for \u8ff4\u5708 */\nfunc nestedForLoop(n int) string {\n res := \"\"\n // \u8ff4\u5708 i = 1, 2, ..., n-1, n\n for i := 1; i &lt;= n; i++ {\n for j := 1; j &lt;= n; j++ {\n // \u8ff4\u5708 j = 1, 2, ..., n-1, n\n res += fmt.Sprintf(\"(%d, %d), \", i, j)\n }\n }\n return res\n}\n</code></pre> iteration.swift<pre><code>/* \u96d9\u5c64 for \u8ff4\u5708 */\nfunc nestedForLoop(n: Int) -&gt; String {\n var res = \"\"\n // \u8ff4\u5708 i = 1, 2, ..., n-1, n\n for i in 1 ... n {\n // \u8ff4\u5708 j = 1, 2, ..., n-1, n\n for j in 1 ... n {\n res.append(\"(\\(i), \\(j)), \")\n }\n }\n return res\n}\n</code></pre> iteration.js<pre><code>/* \u96d9\u5c64 for \u8ff4\u5708 */\nfunction nestedForLoop(n) {\n let res = '';\n // \u8ff4\u5708 i = 1, 2, ..., n-1, n\n for (let i = 1; i &lt;= n; i++) {\n // \u8ff4\u5708 j = 1, 2, ..., n-1, n\n for (let j = 1; j &lt;= n; j++) {\n res += `(${i}, ${j}), `;\n }\n }\n return res;\n}\n</code></pre> iteration.ts<pre><code>/* \u96d9\u5c64 for \u8ff4\u5708 */\nfunction nestedForLoop(n: number): string {\n let res = '';\n // \u8ff4\u5708 i = 1, 2, ..., n-1, n\n for (let i = 1; i &lt;= n; i++) {\n // \u8ff4\u5708 j = 1, 2, ..., n-1, n\n for (let j = 1; j &lt;= n; j++) {\n res += `(${i}, ${j}), `;\n }\n }\n return res;\n}\n</code></pre> iteration.dart<pre><code>/* \u96d9\u5c64 for \u8ff4\u5708 */\nString nestedForLoop(int n) {\n String res = \"\";\n // \u8ff4\u5708 i = 1, 2, ..., n-1, n\n for (int i = 1; i &lt;= n; i++) {\n // \u8ff4\u5708 j = 1, 2, ..., n-1, n\n for (int j = 1; j &lt;= n; j++) {\n res += \"($i, $j), \";\n }\n }\n return res;\n}\n</code></pre> iteration.rs<pre><code>/* \u96d9\u5c64 for \u8ff4\u5708 */\nfn nested_for_loop(n: i32) -&gt; String {\n let mut res = vec![];\n // \u8ff4\u5708 i = 1, 2, ..., n-1, n\n for i in 1..=n {\n // \u8ff4\u5708 j = 1, 2, ..., n-1, n\n for j in 1..=n {\n res.push(format!(\"({}, {}), \", i, j));\n }\n }\n res.join(\"\")\n}\n</code></pre> iteration.c<pre><code>/* \u96d9\u5c64 for \u8ff4\u5708 */\nchar *nestedForLoop(int n) {\n // n * n \u70ba\u5c0d\u61c9\u9ede\u6578\u91cf\uff0c\"(i, j), \" \u5c0d\u61c9\u5b57\u4e32\u9577\u6700\u5927\u70ba 6+10*2\uff0c\u52a0\u4e0a\u6700\u5f8c\u4e00\u500b\u7a7a\u5b57\u5143 \\0 \u7684\u984d\u5916\u7a7a\u9593\n int size = n * n * 26 + 1;\n char *res = malloc(size * sizeof(char));\n // \u8ff4\u5708 i = 1, 2, ..., n-1, n\n for (int i = 1; i &lt;= n; i++) {\n // \u8ff4\u5708 j = 1, 2, ..., n-1, n\n for (int j = 1; j &lt;= n; j++) {\n char tmp[26];\n snprintf(tmp, sizeof(tmp), \"(%d, %d), \", i, j);\n strncat(res, tmp, size - strlen(res) - 1);\n }\n }\n return res;\n}\n</code></pre> iteration.kt<pre><code>/* \u96d9\u5c64 for \u8ff4\u5708 */\nfun nestedForLoop(n: Int): String {\n val res = StringBuilder()\n // \u8ff4\u5708 i = 1, 2, ..., n-1, n\n for (i in 1..n) {\n // \u8ff4\u5708 j = 1, 2, ..., n-1, n\n for (j in 1..n) {\n res.append(\" ($i, $j), \")\n }\n }\n return res.toString()\n}\n</code></pre> iteration.rb<pre><code>### \u96d9\u5c64 for \u8ff4\u5708 ###\ndef nested_for_loop(n)\n res = \"\"\n\n # \u8ff4\u5708 i = 1, 2, ..., n-1, n\n for i in 1..n\n # \u8ff4\u5708 j = 1, 2, ..., n-1, n\n for j in 1..n\n res += \"(#{i}, #{j}), \"\n end\n end\n\n res\nend\n</code></pre> iteration.zig<pre><code>// \u96d9\u5c64 for \u8ff4\u5708\nfn nestedForLoop(allocator: Allocator, n: usize) ![]const u8 {\n var res = std.ArrayList(u8).init(allocator);\n defer res.deinit();\n var buffer: [20]u8 = undefined;\n // \u8ff4\u5708 i = 1, 2, ..., n-1, n\n for (1..n+1) |i| {\n // \u8ff4\u5708 j = 1, 2, ..., n-1, n\n for (1..n+1) |j| {\n var _str = try std.fmt.bufPrint(&amp;buffer, \"({d}, {d}), \", .{i, j});\n try res.appendSlice(_str);\n }\n }\n return res.toOwnedSlice();\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 2-2 \u662f\u8a72\u5de2\u72c0\u8ff4\u5708\u7684\u6d41\u7a0b\u6846\u5716\u3002</p> <p></p> <p> \u5716 2-2 \u00a0 \u5de2\u72c0\u8ff4\u5708\u7684\u6d41\u7a0b\u6846\u5716 </p> <p>\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u51fd\u5f0f\u7684\u64cd\u4f5c\u6578\u91cf\u8207 \\(n^2\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8aaa\u6f14\u7b97\u6cd5\u57f7\u884c\u6642\u9593\u548c\u8f38\u5165\u8cc7\u6599\u5927\u5c0f \\(n\\) \u6210\u201c\u5e73\u65b9\u95dc\u4fc2\u201d\u3002</p> <p>\u6211\u5011\u53ef\u4ee5\u7e7c\u7e8c\u65b0\u589e\u5de2\u72c0\u8ff4\u5708\uff0c\u6bcf\u4e00\u6b21\u5de2\u72c0\u90fd\u662f\u4e00\u6b21\u201c\u5347\u7dad\u201d\uff0c\u5c07\u6703\u4f7f\u6642\u9593\u8907\u96dc\u5ea6\u63d0\u9ad8\u81f3\u201c\u7acb\u65b9\u95dc\u4fc2\u201d\u201c\u56db\u6b21\u65b9\u95dc\u4fc2\u201d\uff0c\u4ee5\u6b64\u985e\u63a8\u3002</p>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#222","title":"2.2.2 \u00a0 \u905e\u8ff4","text":"<p>\u905e\u8ff4\uff08recursion\uff09\u662f\u4e00\u7a2e\u6f14\u7b97\u6cd5\u7b56\u7565\uff0c\u900f\u904e\u51fd\u5f0f\u547c\u53eb\u81ea\u8eab\u4f86\u89e3\u6c7a\u554f\u984c\u3002\u5b83\u4e3b\u8981\u5305\u542b\u5169\u500b\u968e\u6bb5\u3002</p> <ol> <li>\u905e\uff1a\u7a0b\u5f0f\u4e0d\u65b7\u6df1\u5165\u5730\u547c\u53eb\u81ea\u8eab\uff0c\u901a\u5e38\u50b3\u5165\u66f4\u5c0f\u6216\u66f4\u7c21\u5316\u7684\u53c3\u6578\uff0c\u76f4\u5230\u9054\u5230\u201c\u7d42\u6b62\u689d\u4ef6\u201d\u3002</li> <li>\u8ff4\uff1a\u89f8\u767c\u201c\u7d42\u6b62\u689d\u4ef6\u201d\u5f8c\uff0c\u7a0b\u5f0f\u5f9e\u6700\u6df1\u5c64\u7684\u905e\u8ff4\u51fd\u5f0f\u958b\u59cb\u9010\u5c64\u8fd4\u56de\uff0c\u532f\u805a\u6bcf\u4e00\u5c64\u7684\u7d50\u679c\u3002</li> </ol> <p>\u800c\u5f9e\u5be6\u73fe\u7684\u89d2\u5ea6\u770b\uff0c\u905e\u8ff4\u7a0b\u5f0f\u78bc\u4e3b\u8981\u5305\u542b\u4e09\u500b\u8981\u7d20\u3002</p> <ol> <li>\u7d42\u6b62\u689d\u4ef6\uff1a\u7528\u65bc\u6c7a\u5b9a\u4ec0\u9ebc\u6642\u5019\u7531\u201c\u905e\u201d\u8f49\u201c\u8ff4\u201d\u3002</li> <li>\u905e\u8ff4\u547c\u53eb\uff1a\u5c0d\u61c9\u201c\u905e\u201d\uff0c\u51fd\u5f0f\u547c\u53eb\u81ea\u8eab\uff0c\u901a\u5e38\u8f38\u5165\u66f4\u5c0f\u6216\u66f4\u7c21\u5316\u7684\u53c3\u6578\u3002</li> <li>\u8fd4\u56de\u7d50\u679c\uff1a\u5c0d\u61c9\u201c\u8ff4\u201d\uff0c\u5c07\u7576\u524d\u905e\u8ff4\u5c64\u7d1a\u7684\u7d50\u679c\u8fd4\u56de\u81f3\u4e0a\u4e00\u5c64\u3002</li> </ol> <p>\u89c0\u5bdf\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff0c\u6211\u5011\u53ea\u9700\u547c\u53eb\u51fd\u5f0f <code>recur(n)</code> \uff0c\u5c31\u53ef\u4ee5\u5b8c\u6210 \\(1 + 2 + \\dots + n\\) \u7684\u8a08\u7b97\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig recursion.py<pre><code>def recur(n: int) -&gt; int:\n \"\"\"\u905e\u8ff4\"\"\"\n # \u7d42\u6b62\u689d\u4ef6\n if n == 1:\n return 1\n # \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n res = recur(n - 1)\n # \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n return n + res\n</code></pre> recursion.cpp<pre><code>/* \u905e\u8ff4 */\nint recur(int n) {\n // \u7d42\u6b62\u689d\u4ef6\n if (n == 1)\n return 1;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n int res = recur(n - 1);\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n return n + res;\n}\n</code></pre> recursion.java<pre><code>/* \u905e\u8ff4 */\nint recur(int n) {\n // \u7d42\u6b62\u689d\u4ef6\n if (n == 1)\n return 1;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n int res = recur(n - 1);\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n return n + res;\n}\n</code></pre> recursion.cs<pre><code>/* \u905e\u8ff4 */\nint Recur(int n) {\n // \u7d42\u6b62\u689d\u4ef6\n if (n == 1)\n return 1;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n int res = Recur(n - 1);\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n return n + res;\n}\n</code></pre> recursion.go<pre><code>/* \u905e\u8ff4 */\nfunc recur(n int) int {\n // \u7d42\u6b62\u689d\u4ef6\n if n == 1 {\n return 1\n }\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n res := recur(n - 1)\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n return n + res\n}\n</code></pre> recursion.swift<pre><code>/* \u905e\u8ff4 */\nfunc recur(n: Int) -&gt; Int {\n // \u7d42\u6b62\u689d\u4ef6\n if n == 1 {\n return 1\n }\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n let res = recur(n: n - 1)\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n return n + res\n}\n</code></pre> recursion.js<pre><code>/* \u905e\u8ff4 */\nfunction recur(n) {\n // \u7d42\u6b62\u689d\u4ef6\n if (n === 1) return 1;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n const res = recur(n - 1);\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n return n + res;\n}\n</code></pre> recursion.ts<pre><code>/* \u905e\u8ff4 */\nfunction recur(n: number): number {\n // \u7d42\u6b62\u689d\u4ef6\n if (n === 1) return 1;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n const res = recur(n - 1);\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n return n + res;\n}\n</code></pre> recursion.dart<pre><code>/* \u905e\u8ff4 */\nint recur(int n) {\n // \u7d42\u6b62\u689d\u4ef6\n if (n == 1) return 1;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n int res = recur(n - 1);\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n return n + res;\n}\n</code></pre> recursion.rs<pre><code>/* \u905e\u8ff4 */\nfn recur(n: i32) -&gt; i32 {\n // \u7d42\u6b62\u689d\u4ef6\n if n == 1 {\n return 1;\n }\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n let res = recur(n - 1);\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n n + res\n}\n</code></pre> recursion.c<pre><code>/* \u905e\u8ff4 */\nint recur(int n) {\n // \u7d42\u6b62\u689d\u4ef6\n if (n == 1)\n return 1;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n int res = recur(n - 1);\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n return n + res;\n}\n</code></pre> recursion.kt<pre><code>/* \u905e\u8ff4 */\nfun recur(n: Int): Int {\n // \u7d42\u6b62\u689d\u4ef6\n if (n == 1)\n return 1\n // \u905e: \u905e\u8ff4\u547c\u53eb\n val res = recur(n - 1)\n // \u8ff4: \u8fd4\u56de\u7d50\u679c\n return n + res\n}\n</code></pre> recursion.rb<pre><code>### \u905e\u8ff4 ###\ndef recur(n)\n # \u7d42\u6b62\u689d\u4ef6\n return 1 if n == 1\n # \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n res = recur(n - 1)\n # \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n n + res\nend\n</code></pre> recursion.zig<pre><code>// \u905e\u8ff4\u51fd\u5f0f\nfn recur(n: i32) i32 {\n // \u7d42\u6b62\u689d\u4ef6\n if (n == 1) {\n return 1;\n }\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n var res: i32 = recur(n - 1);\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n return n + res;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 2-3 \u5c55\u793a\u4e86\u8a72\u51fd\u5f0f\u7684\u905e\u8ff4\u904e\u7a0b\u3002</p> <p></p> <p> \u5716 2-3 \u00a0 \u6c42\u548c\u51fd\u5f0f\u7684\u905e\u8ff4\u904e\u7a0b </p> <p>\u96d6\u7136\u5f9e\u8a08\u7b97\u89d2\u5ea6\u770b\uff0c\u8fed\u4ee3\u8207\u905e\u8ff4\u53ef\u4ee5\u5f97\u5230\u76f8\u540c\u7684\u7d50\u679c\uff0c\u4f46\u5b83\u5011\u4ee3\u8868\u4e86\u5169\u7a2e\u5b8c\u5168\u4e0d\u540c\u7684\u601d\u8003\u548c\u89e3\u6c7a\u554f\u984c\u7684\u7bc4\u5f0f\u3002</p> <ul> <li>\u8fed\u4ee3\uff1a\u201c\u81ea\u4e0b\u800c\u4e0a\u201d\u5730\u89e3\u6c7a\u554f\u984c\u3002\u5f9e\u6700\u57fa\u790e\u7684\u6b65\u9a5f\u958b\u59cb\uff0c\u7136\u5f8c\u4e0d\u65b7\u91cd\u8907\u6216\u7d2f\u52a0\u9019\u4e9b\u6b65\u9a5f\uff0c\u76f4\u5230\u4efb\u52d9\u5b8c\u6210\u3002</li> <li>\u905e\u8ff4\uff1a\u201c\u81ea\u4e0a\u800c\u4e0b\u201d\u5730\u89e3\u6c7a\u554f\u984c\u3002\u5c07\u539f\u554f\u984c\u5206\u89e3\u70ba\u66f4\u5c0f\u7684\u5b50\u554f\u984c\uff0c\u9019\u4e9b\u5b50\u554f\u984c\u548c\u539f\u554f\u984c\u5177\u6709\u76f8\u540c\u7684\u5f62\u5f0f\u3002\u63a5\u4e0b\u4f86\u5c07\u5b50\u554f\u984c\u7e7c\u7e8c\u5206\u89e3\u70ba\u66f4\u5c0f\u7684\u5b50\u554f\u984c\uff0c\u76f4\u5230\u57fa\u672c\u60c5\u6cc1\u6642\u505c\u6b62\uff08\u57fa\u672c\u60c5\u6cc1\u7684\u89e3\u662f\u5df2\u77e5\u7684\uff09\u3002</li> </ul> <p>\u4ee5\u4e0a\u8ff0\u6c42\u548c\u51fd\u5f0f\u70ba\u4f8b\uff0c\u8a2d\u554f\u984c \\(f(n) = 1 + 2 + \\dots + n\\) \u3002</p> <ul> <li>\u8fed\u4ee3\uff1a\u5728\u8ff4\u5708\u4e2d\u6a21\u64ec\u6c42\u548c\u904e\u7a0b\uff0c\u5f9e \\(1\\) \u8d70\u8a2a\u5230 \\(n\\) \uff0c\u6bcf\u8f2a\u57f7\u884c\u6c42\u548c\u64cd\u4f5c\uff0c\u5373\u53ef\u6c42\u5f97 \\(f(n)\\) \u3002</li> <li>\u905e\u8ff4\uff1a\u5c07\u554f\u984c\u5206\u89e3\u70ba\u5b50\u554f\u984c \\(f(n) = n + f(n-1)\\) \uff0c\u4e0d\u65b7\uff08\u905e\u8ff4\u5730\uff09\u5206\u89e3\u4e0b\u53bb\uff0c\u76f4\u81f3\u57fa\u672c\u60c5\u6cc1 \\(f(1) = 1\\) \u6642\u7d42\u6b62\u3002</li> </ul>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#1","title":"1. \u00a0 \u547c\u53eb\u5806\u758a","text":"<p>\u905e\u8ff4\u51fd\u5f0f\u6bcf\u6b21\u547c\u53eb\u81ea\u8eab\u6642\uff0c\u7cfb\u7d71\u90fd\u6703\u70ba\u65b0\u958b\u555f\u7684\u51fd\u5f0f\u5206\u914d\u8a18\u61b6\u9ad4\uff0c\u4ee5\u5132\u5b58\u5340\u57df\u6027\u8b8a\u6578\u3001\u547c\u53eb\u4f4d\u5740\u548c\u5176\u4ed6\u8cc7\u8a0a\u7b49\u3002\u9019\u5c07\u5c0e\u81f4\u5169\u65b9\u9762\u7684\u7d50\u679c\u3002</p> <ul> <li>\u51fd\u5f0f\u7684\u4e0a\u4e0b\u6587\u8cc7\u6599\u90fd\u5132\u5b58\u5728\u7a31\u70ba\u201c\u5806\u758a\u5e40\u7a7a\u9593\u201d\u7684\u8a18\u61b6\u9ad4\u5340\u57df\u4e2d\uff0c\u76f4\u81f3\u51fd\u5f0f\u8fd4\u56de\u5f8c\u624d\u6703\u88ab\u91cb\u653e\u3002\u56e0\u6b64\uff0c\u905e\u8ff4\u901a\u5e38\u6bd4\u8fed\u4ee3\u66f4\u52a0\u8017\u8cbb\u8a18\u61b6\u9ad4\u7a7a\u9593\u3002</li> <li>\u905e\u8ff4\u547c\u53eb\u51fd\u5f0f\u6703\u7522\u751f\u984d\u5916\u7684\u958b\u92b7\u3002\u56e0\u6b64\u905e\u8ff4\u901a\u5e38\u6bd4\u8ff4\u5708\u7684\u6642\u9593\u6548\u7387\u66f4\u4f4e\u3002</li> </ul> <p>\u5982\u5716 2-4 \u6240\u793a\uff0c\u5728\u89f8\u767c\u7d42\u6b62\u689d\u4ef6\u524d\uff0c\u540c\u6642\u5b58\u5728 \\(n\\) \u500b\u672a\u8fd4\u56de\u7684\u905e\u8ff4\u51fd\u5f0f\uff0c\u905e\u8ff4\u6df1\u5ea6\u70ba \\(n\\) \u3002</p> <p></p> <p> \u5716 2-4 \u00a0 \u905e\u8ff4\u547c\u53eb\u6df1\u5ea6 </p> <p>\u5728\u5be6\u969b\u4e2d\uff0c\u7a0b\u5f0f\u8a9e\u8a00\u5141\u8a31\u7684\u905e\u8ff4\u6df1\u5ea6\u901a\u5e38\u662f\u6709\u9650\u7684\uff0c\u904e\u6df1\u7684\u905e\u8ff4\u53ef\u80fd\u5c0e\u81f4\u5806\u758a\u6ea2\u4f4d\u932f\u8aa4\u3002</p>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2","title":"2. \u00a0 \u5c3e\u905e\u8ff4","text":"<p>\u6709\u8da3\u7684\u662f\uff0c\u5982\u679c\u51fd\u5f0f\u5728\u8fd4\u56de\u524d\u7684\u6700\u5f8c\u4e00\u6b65\u624d\u9032\u884c\u905e\u8ff4\u547c\u53eb\uff0c\u5247\u8a72\u51fd\u5f0f\u53ef\u4ee5\u88ab\u7de8\u8b6f\u5668\u6216\u76f4\u8b6f\u5668\u6700\u4f73\u5316\uff0c\u4f7f\u5176\u5728\u7a7a\u9593\u6548\u7387\u4e0a\u8207\u8fed\u4ee3\u76f8\u7576\u3002\u9019\u7a2e\u60c5\u6cc1\u88ab\u7a31\u70ba\u5c3e\u905e\u8ff4\uff08tail recursion\uff09\u3002</p> <ul> <li>\u666e\u901a\u905e\u8ff4\uff1a\u7576\u51fd\u5f0f\u8fd4\u56de\u5230\u4e0a\u4e00\u5c64\u7d1a\u7684\u51fd\u5f0f\u5f8c\uff0c\u9700\u8981\u7e7c\u7e8c\u57f7\u884c\u7a0b\u5f0f\u78bc\uff0c\u56e0\u6b64\u7cfb\u7d71\u9700\u8981\u5132\u5b58\u4e0a\u4e00\u5c64\u547c\u53eb\u7684\u4e0a\u4e0b\u6587\u3002</li> <li>\u5c3e\u905e\u8ff4\uff1a\u905e\u8ff4\u547c\u53eb\u662f\u51fd\u5f0f\u8fd4\u56de\u524d\u7684\u6700\u5f8c\u4e00\u500b\u64cd\u4f5c\uff0c\u9019\u610f\u5473\u8457\u51fd\u5f0f\u8fd4\u56de\u5230\u4e0a\u4e00\u5c64\u7d1a\u5f8c\uff0c\u7121\u9808\u7e7c\u7e8c\u57f7\u884c\u5176\u4ed6\u64cd\u4f5c\uff0c\u56e0\u6b64\u7cfb\u7d71\u7121\u9808\u5132\u5b58\u4e0a\u4e00\u5c64\u51fd\u5f0f\u7684\u4e0a\u4e0b\u6587\u3002</li> </ul> <p>\u4ee5\u8a08\u7b97 \\(1 + 2 + \\dots + n\\) \u70ba\u4f8b\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u7d50\u679c\u8b8a\u6578 <code>res</code> \u8a2d\u70ba\u51fd\u5f0f\u53c3\u6578\uff0c\u5f9e\u800c\u5be6\u73fe\u5c3e\u905e\u8ff4\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig recursion.py<pre><code>def tail_recur(n, res):\n \"\"\"\u5c3e\u905e\u8ff4\"\"\"\n # \u7d42\u6b62\u689d\u4ef6\n if n == 0:\n return res\n # \u5c3e\u905e\u8ff4\u547c\u53eb\n return tail_recur(n - 1, res + n)\n</code></pre> recursion.cpp<pre><code>/* \u5c3e\u905e\u8ff4 */\nint tailRecur(int n, int res) {\n // \u7d42\u6b62\u689d\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u905e\u8ff4\u547c\u53eb\n return tailRecur(n - 1, res + n);\n}\n</code></pre> recursion.java<pre><code>/* \u5c3e\u905e\u8ff4 */\nint tailRecur(int n, int res) {\n // \u7d42\u6b62\u689d\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u905e\u8ff4\u547c\u53eb\n return tailRecur(n - 1, res + n);\n}\n</code></pre> recursion.cs<pre><code>/* \u5c3e\u905e\u8ff4 */\nint TailRecur(int n, int res) {\n // \u7d42\u6b62\u689d\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u905e\u8ff4\u547c\u53eb\n return TailRecur(n - 1, res + n);\n}\n</code></pre> recursion.go<pre><code>/* \u5c3e\u905e\u8ff4 */\nfunc tailRecur(n int, res int) int {\n // \u7d42\u6b62\u689d\u4ef6\n if n == 0 {\n return res\n }\n // \u5c3e\u905e\u8ff4\u547c\u53eb\n return tailRecur(n-1, res+n)\n}\n</code></pre> recursion.swift<pre><code>/* \u5c3e\u905e\u8ff4 */\nfunc tailRecur(n: Int, res: Int) -&gt; Int {\n // \u7d42\u6b62\u689d\u4ef6\n if n == 0 {\n return res\n }\n // \u5c3e\u905e\u8ff4\u547c\u53eb\n return tailRecur(n: n - 1, res: res + n)\n}\n</code></pre> recursion.js<pre><code>/* \u5c3e\u905e\u8ff4 */\nfunction tailRecur(n, res) {\n // \u7d42\u6b62\u689d\u4ef6\n if (n === 0) return res;\n // \u5c3e\u905e\u8ff4\u547c\u53eb\n return tailRecur(n - 1, res + n);\n}\n</code></pre> recursion.ts<pre><code>/* \u5c3e\u905e\u8ff4 */\nfunction tailRecur(n: number, res: number): number {\n // \u7d42\u6b62\u689d\u4ef6\n if (n === 0) return res;\n // \u5c3e\u905e\u8ff4\u547c\u53eb\n return tailRecur(n - 1, res + n);\n}\n</code></pre> recursion.dart<pre><code>/* \u5c3e\u905e\u8ff4 */\nint tailRecur(int n, int res) {\n // \u7d42\u6b62\u689d\u4ef6\n if (n == 0) return res;\n // \u5c3e\u905e\u8ff4\u547c\u53eb\n return tailRecur(n - 1, res + n);\n}\n</code></pre> recursion.rs<pre><code>/* \u5c3e\u905e\u8ff4 */\nfn tail_recur(n: i32, res: i32) -&gt; i32 {\n // \u7d42\u6b62\u689d\u4ef6\n if n == 0 {\n return res;\n }\n // \u5c3e\u905e\u8ff4\u547c\u53eb\n tail_recur(n - 1, res + n)\n}\n</code></pre> recursion.c<pre><code>/* \u5c3e\u905e\u8ff4 */\nint tailRecur(int n, int res) {\n // \u7d42\u6b62\u689d\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u905e\u8ff4\u547c\u53eb\n return tailRecur(n - 1, res + n);\n}\n</code></pre> recursion.kt<pre><code>/* \u5c3e\u905e\u8ff4 */\ntailrec fun tailRecur(n: Int, res: Int): Int {\n // \u65b0\u589e tailrec \u95dc\u9375\u8a5e\uff0c\u4ee5\u958b\u555f\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\n // \u7d42\u6b62\u689d\u4ef6\n if (n == 0)\n return res\n // \u5c3e\u905e\u8ff4\u547c\u53eb\n return tailRecur(n - 1, res + n)\n}\n</code></pre> recursion.rb<pre><code>### \u5c3e\u905e\u8ff4 ###\ndef tail_recur(n, res)\n # \u7d42\u6b62\u689d\u4ef6\n return res if n == 0\n # \u5c3e\u905e\u8ff4\u547c\u53eb\n tail_recur(n - 1, res + n)\nend\n</code></pre> recursion.zig<pre><code>// \u5c3e\u905e\u8ff4\u51fd\u5f0f\nfn tailRecur(n: i32, res: i32) i32 {\n // \u7d42\u6b62\u689d\u4ef6\n if (n == 0) {\n return res;\n }\n // \u5c3e\u905e\u8ff4\u547c\u53eb\n return tailRecur(n - 1, res + n);\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5c3e\u905e\u8ff4\u7684\u57f7\u884c\u904e\u7a0b\u5982\u5716 2-5 \u6240\u793a\u3002\u5c0d\u6bd4\u666e\u901a\u905e\u8ff4\u548c\u5c3e\u905e\u8ff4\uff0c\u5169\u8005\u7684\u6c42\u548c\u64cd\u4f5c\u7684\u57f7\u884c\u9ede\u662f\u4e0d\u540c\u7684\u3002</p> <ul> <li>\u666e\u901a\u905e\u8ff4\uff1a\u6c42\u548c\u64cd\u4f5c\u662f\u5728\u201c\u8ff4\u201d\u7684\u904e\u7a0b\u4e2d\u57f7\u884c\u7684\uff0c\u6bcf\u5c64\u8fd4\u56de\u5f8c\u90fd\u8981\u518d\u57f7\u884c\u4e00\u6b21\u6c42\u548c\u64cd\u4f5c\u3002</li> <li>\u5c3e\u905e\u8ff4\uff1a\u6c42\u548c\u64cd\u4f5c\u662f\u5728\u201c\u905e\u201d\u7684\u904e\u7a0b\u4e2d\u57f7\u884c\u7684\uff0c\u201c\u8ff4\u201d\u7684\u904e\u7a0b\u53ea\u9700\u5c64\u5c64\u8fd4\u56de\u3002</li> </ul> <p></p> <p> \u5716 2-5 \u00a0 \u5c3e\u905e\u8ff4\u904e\u7a0b </p> <p>Tip</p> <p>\u8acb\u6ce8\u610f\uff0c\u8a31\u591a\u7de8\u8b6f\u5668\u6216\u76f4\u8b6f\u5668\u4e26\u4e0d\u652f\u6301\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\u3002\u4f8b\u5982\uff0cPython \u9810\u8a2d\u4e0d\u652f\u6301\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff0c\u56e0\u6b64\u5373\u4f7f\u51fd\u5f0f\u662f\u5c3e\u905e\u8ff4\u5f62\u5f0f\uff0c\u4ecd\u7136\u53ef\u80fd\u6703\u9047\u5230\u5806\u758a\u6ea2\u4f4d\u554f\u984c\u3002</p>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#3_1","title":"3. \u00a0 \u905e\u8ff4\u6a39","text":"<p>\u7576\u8655\u7406\u8207\u201c\u5206\u6cbb\u201d\u76f8\u95dc\u7684\u6f14\u7b97\u6cd5\u554f\u984c\u6642\uff0c\u905e\u8ff4\u5f80\u5f80\u6bd4\u8fed\u4ee3\u7684\u601d\u8def\u66f4\u52a0\u76f4\u89c0\u3001\u7a0b\u5f0f\u78bc\u66f4\u52a0\u6613\u8b80\u3002\u4ee5\u201c\u8cbb\u6ce2\u90a3\u5951\u6578\u5217\u201d\u70ba\u4f8b\u3002</p> <p>Question</p> <p>\u7d66\u5b9a\u4e00\u500b\u8cbb\u6ce2\u90a3\u5951\u6578\u5217 \\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\) \uff0c\u6c42\u8a72\u6578\u5217\u7684\u7b2c \\(n\\) \u500b\u6578\u5b57\u3002</p> <p>\u8a2d\u8cbb\u6ce2\u90a3\u5951\u6578\u5217\u7684\u7b2c \\(n\\) \u500b\u6578\u5b57\u70ba \\(f(n)\\) \uff0c\u6613\u5f97\u5169\u500b\u7d50\u8ad6\u3002</p> <ul> <li>\u6578\u5217\u7684\u524d\u5169\u500b\u6578\u5b57\u70ba \\(f(1) = 0\\) \u548c \\(f(2) = 1\\) \u3002</li> <li>\u6578\u5217\u4e2d\u7684\u6bcf\u500b\u6578\u5b57\u662f\u524d\u5169\u500b\u6578\u5b57\u7684\u548c\uff0c\u5373 \\(f(n) = f(n - 1) + f(n - 2)\\) \u3002</li> </ul> <p>\u6309\u7167\u905e\u63a8\u95dc\u4fc2\u9032\u884c\u905e\u8ff4\u547c\u53eb\uff0c\u5c07\u524d\u5169\u500b\u6578\u5b57\u4f5c\u70ba\u7d42\u6b62\u689d\u4ef6\uff0c\u4fbf\u53ef\u5beb\u51fa\u905e\u8ff4\u7a0b\u5f0f\u78bc\u3002\u547c\u53eb <code>fib(n)</code> \u5373\u53ef\u5f97\u5230\u8cbb\u6ce2\u90a3\u5951\u6578\u5217\u7684\u7b2c \\(n\\) \u500b\u6578\u5b57\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig recursion.py<pre><code>def fib(n: int) -&gt; int:\n \"\"\"\u8cbb\u6ce2\u90a3\u5951\u6578\u5217\uff1a\u905e\u8ff4\"\"\"\n # \u7d42\u6b62\u689d\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 or n == 2:\n return n - 1\n # \u905e\u8ff4\u547c\u53eb f(n) = f(n-1) + f(n-2)\n res = fib(n - 1) + fib(n - 2)\n # \u8fd4\u56de\u7d50\u679c f(n)\n return res\n</code></pre> recursion.cpp<pre><code>/* \u8cbb\u6ce2\u90a3\u5951\u6578\u5217\uff1a\u905e\u8ff4 */\nint fib(int n) {\n // \u7d42\u6b62\u689d\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u905e\u8ff4\u547c\u53eb f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7d50\u679c f(n)\n return res;\n}\n</code></pre> recursion.java<pre><code>/* \u8cbb\u6ce2\u90a3\u5951\u6578\u5217\uff1a\u905e\u8ff4 */\nint fib(int n) {\n // \u7d42\u6b62\u689d\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u905e\u8ff4\u547c\u53eb f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7d50\u679c f(n)\n return res;\n}\n</code></pre> recursion.cs<pre><code>/* \u8cbb\u6ce2\u90a3\u5951\u6578\u5217\uff1a\u905e\u8ff4 */\nint Fib(int n) {\n // \u7d42\u6b62\u689d\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u905e\u8ff4\u547c\u53eb f(n) = f(n-1) + f(n-2)\n int res = Fib(n - 1) + Fib(n - 2);\n // \u8fd4\u56de\u7d50\u679c f(n)\n return res;\n}\n</code></pre> recursion.go<pre><code>/* \u8cbb\u6ce2\u90a3\u5951\u6578\u5217\uff1a\u905e\u8ff4 */\nfunc fib(n int) int {\n // \u7d42\u6b62\u689d\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1\n }\n // \u905e\u8ff4\u547c\u53eb f(n) = f(n-1) + f(n-2)\n res := fib(n-1) + fib(n-2)\n // \u8fd4\u56de\u7d50\u679c f(n)\n return res\n}\n</code></pre> recursion.swift<pre><code>/* \u8cbb\u6ce2\u90a3\u5951\u6578\u5217\uff1a\u905e\u8ff4 */\nfunc fib(n: Int) -&gt; Int {\n // \u7d42\u6b62\u689d\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1\n }\n // \u905e\u8ff4\u547c\u53eb f(n) = f(n-1) + f(n-2)\n let res = fib(n: n - 1) + fib(n: n - 2)\n // \u8fd4\u56de\u7d50\u679c f(n)\n return res\n}\n</code></pre> recursion.js<pre><code>/* \u8cbb\u6ce2\u90a3\u5951\u6578\u5217\uff1a\u905e\u8ff4 */\nfunction fib(n) {\n // \u7d42\u6b62\u689d\u4ef6 f(1) = 0, f(2) = 1\n if (n === 1 || n === 2) return n - 1;\n // \u905e\u8ff4\u547c\u53eb f(n) = f(n-1) + f(n-2)\n const res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7d50\u679c f(n)\n return res;\n}\n</code></pre> recursion.ts<pre><code>/* \u8cbb\u6ce2\u90a3\u5951\u6578\u5217\uff1a\u905e\u8ff4 */\nfunction fib(n: number): number {\n // \u7d42\u6b62\u689d\u4ef6 f(1) = 0, f(2) = 1\n if (n === 1 || n === 2) return n - 1;\n // \u905e\u8ff4\u547c\u53eb f(n) = f(n-1) + f(n-2)\n const res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7d50\u679c f(n)\n return res;\n}\n</code></pre> recursion.dart<pre><code>/* \u8cbb\u6ce2\u90a3\u5951\u6578\u5217\uff1a\u905e\u8ff4 */\nint fib(int n) {\n // \u7d42\u6b62\u689d\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2) return n - 1;\n // \u905e\u8ff4\u547c\u53eb f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7d50\u679c f(n)\n return res;\n}\n</code></pre> recursion.rs<pre><code>/* \u8cbb\u6ce2\u90a3\u5951\u6578\u5217\uff1a\u905e\u8ff4 */\nfn fib(n: i32) -&gt; i32 {\n // \u7d42\u6b62\u689d\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1;\n }\n // \u905e\u8ff4\u547c\u53eb f(n) = f(n-1) + f(n-2)\n let res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7d50\u679c\n res\n}\n</code></pre> recursion.c<pre><code>/* \u8cbb\u6ce2\u90a3\u5951\u6578\u5217\uff1a\u905e\u8ff4 */\nint fib(int n) {\n // \u7d42\u6b62\u689d\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u905e\u8ff4\u547c\u53eb f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7d50\u679c f(n)\n return res;\n}\n</code></pre> recursion.kt<pre><code>/* \u8cbb\u6ce2\u90a3\u5951\u6578\u5217\uff1a\u905e\u8ff4 */\nfun fib(n: Int): Int {\n // \u7d42\u6b62\u689d\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1\n // \u905e\u8ff4\u547c\u53eb f(n) = f(n-1) + f(n-2)\n val res = fib(n - 1) + fib(n - 2)\n // \u8fd4\u56de\u7d50\u679c f(n)\n return res\n}\n</code></pre> recursion.rb<pre><code>### \u8cbb\u6ce2\u90a3\u5951\u6578\u5217\uff1a\u905e\u8ff4 ###\ndef fib(n)\n # \u7d42\u6b62\u689d\u4ef6 f(1) = 0, f(2) = 1\n return n - 1 if n == 1 || n == 2\n # \u905e\u8ff4\u547c\u53eb f(n) = f(n-1) + f(n-2)\n res = fib(n - 1) + fib(n - 2)\n # \u8fd4\u56de\u7d50\u679c f(n)\n res\nend\n</code></pre> recursion.zig<pre><code>// \u8cbb\u6ce2\u90a3\u5951\u6578\u5217\nfn fib(n: i32) i32 {\n // \u7d42\u6b62\u689d\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 or n == 2) {\n return n - 1;\n }\n // \u905e\u8ff4\u547c\u53eb f(n) = f(n-1) + f(n-2)\n var res: i32 = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7d50\u679c f(n)\n return res;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u89c0\u5bdf\u4ee5\u4e0a\u7a0b\u5f0f\u78bc\uff0c\u6211\u5011\u5728\u51fd\u5f0f\u5167\u905e\u8ff4\u547c\u53eb\u4e86\u5169\u500b\u51fd\u5f0f\uff0c\u9019\u610f\u5473\u8457\u5f9e\u4e00\u500b\u547c\u53eb\u7522\u751f\u4e86\u5169\u500b\u547c\u53eb\u5206\u652f\u3002\u5982\u5716 2-6 \u6240\u793a\uff0c\u9019\u6a23\u4e0d\u65b7\u905e\u8ff4\u547c\u53eb\u4e0b\u53bb\uff0c\u6700\u7d42\u5c07\u7522\u751f\u4e00\u68f5\u5c64\u6578\u70ba \\(n\\) \u7684\u905e\u8ff4\u6a39\uff08recursion tree\uff09\u3002</p> <p></p> <p> \u5716 2-6 \u00a0 \u8cbb\u6ce2\u90a3\u5951\u6578\u5217\u7684\u905e\u8ff4\u6a39 </p> <p>\u5f9e\u672c\u8cea\u4e0a\u770b\uff0c\u905e\u8ff4\u9ad4\u73fe\u4e86\u201c\u5c07\u554f\u984c\u5206\u89e3\u70ba\u66f4\u5c0f\u5b50\u554f\u984c\u201d\u7684\u601d\u7dad\u7bc4\u5f0f\uff0c\u9019\u7a2e\u5206\u6cbb\u7b56\u7565\u81f3\u95dc\u91cd\u8981\u3002</p> <ul> <li>\u5f9e\u6f14\u7b97\u6cd5\u89d2\u5ea6\u770b\uff0c\u641c\u5c0b\u3001\u6392\u5e8f\u3001\u56de\u6eaf\u3001\u5206\u6cbb\u3001\u52d5\u614b\u898f\u5283\u7b49\u8a31\u591a\u91cd\u8981\u6f14\u7b97\u6cd5\u7b56\u7565\u76f4\u63a5\u6216\u9593\u63a5\u5730\u61c9\u7528\u4e86\u9019\u7a2e\u601d\u7dad\u65b9\u5f0f\u3002</li> <li>\u5f9e\u8cc7\u6599\u7d50\u69cb\u89d2\u5ea6\u770b\uff0c\u905e\u8ff4\u5929\u7136\u9069\u5408\u8655\u7406\u93c8\u7d50\u4e32\u5217\u3001\u6a39\u548c\u5716\u7684\u76f8\u95dc\u554f\u984c\uff0c\u56e0\u70ba\u5b83\u5011\u975e\u5e38\u9069\u5408\u7528\u5206\u6cbb\u601d\u60f3\u9032\u884c\u5206\u6790\u3002</li> </ul>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#223","title":"2.2.3 \u00a0 \u5169\u8005\u5c0d\u6bd4","text":"<p>\u7e3d\u7d50\u4ee5\u4e0a\u5167\u5bb9\uff0c\u5982\u8868 2-1 \u6240\u793a\uff0c\u8fed\u4ee3\u548c\u905e\u8ff4\u5728\u5be6\u73fe\u3001\u6548\u80fd\u548c\u9069\u7528\u6027\u4e0a\u6709\u6240\u4e0d\u540c\u3002</p> <p> \u8868 2-1 \u00a0 \u8fed\u4ee3\u8207\u905e\u8ff4\u7279\u9ede\u5c0d\u6bd4 </p> \u8fed\u4ee3 \u905e\u8ff4 \u5be6\u73fe\u65b9\u5f0f \u8ff4\u5708\u7d50\u69cb \u51fd\u5f0f\u547c\u53eb\u81ea\u8eab \u6642\u9593\u6548\u7387 \u6548\u7387\u901a\u5e38\u8f03\u9ad8\uff0c\u7121\u51fd\u5f0f\u547c\u53eb\u958b\u92b7 \u6bcf\u6b21\u51fd\u5f0f\u547c\u53eb\u90fd\u6703\u7522\u751f\u958b\u92b7 \u8a18\u61b6\u9ad4\u4f7f\u7528 \u901a\u5e38\u4f7f\u7528\u56fa\u5b9a\u5927\u5c0f\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593 \u7d2f\u7a4d\u51fd\u5f0f\u547c\u53eb\u53ef\u80fd\u4f7f\u7528\u5927\u91cf\u7684\u5806\u758a\u5e40\u7a7a\u9593 \u9069\u7528\u554f\u984c \u9069\u7528\u65bc\u7c21\u55ae\u8ff4\u5708\u4efb\u52d9\uff0c\u7a0b\u5f0f\u78bc\u76f4\u89c0\u3001\u53ef\u8b80\u6027\u597d \u9069\u7528\u65bc\u5b50\u554f\u984c\u5206\u89e3\uff0c\u5982\u6a39\u3001\u5716\u3001\u5206\u6cbb\u3001\u56de\u6eaf\u7b49\uff0c\u7a0b\u5f0f\u78bc\u7d50\u69cb\u7c21\u6f54\u3001\u6e05\u6670 <p>Tip</p> <p>\u5982\u679c\u611f\u89ba\u4ee5\u4e0b\u5167\u5bb9\u7406\u89e3\u56f0\u96e3\uff0c\u53ef\u4ee5\u5728\u8b80\u5b8c\u201c\u5806\u758a\u201d\u7ae0\u7bc0\u5f8c\u518d\u4f86\u8907\u7fd2\u3002</p> <p>\u90a3\u9ebc\uff0c\u8fed\u4ee3\u548c\u905e\u8ff4\u5177\u6709\u4ec0\u9ebc\u5167\u5728\u95dc\u806f\u5462\uff1f\u4ee5\u4e0a\u8ff0\u905e\u8ff4\u51fd\u5f0f\u70ba\u4f8b\uff0c\u6c42\u548c\u64cd\u4f5c\u5728\u905e\u8ff4\u7684\u201c\u8ff4\u201d\u968e\u6bb5\u9032\u884c\u3002\u9019\u610f\u5473\u8457\u6700\u521d\u88ab\u547c\u53eb\u7684\u51fd\u5f0f\u5be6\u969b\u4e0a\u662f\u6700\u5f8c\u5b8c\u6210\u5176\u6c42\u548c\u64cd\u4f5c\u7684\uff0c\u9019\u7a2e\u5de5\u4f5c\u6a5f\u5236\u8207\u5806\u758a\u7684\u201c\u5148\u5165\u5f8c\u51fa\u201d\u539f\u5247\u7570\u66f2\u540c\u5de5\u3002</p> <p>\u4e8b\u5be6\u4e0a\uff0c\u201c\u547c\u53eb\u5806\u758a\u201d\u548c\u201c\u5806\u758a\u5e40\u7a7a\u9593\u201d\u9019\u985e\u905e\u8ff4\u8853\u8a9e\u5df2\u7d93\u6697\u793a\u4e86\u905e\u8ff4\u8207\u5806\u758a\u4e4b\u9593\u7684\u5bc6\u5207\u95dc\u4fc2\u3002</p> <ol> <li>\u905e\uff1a\u7576\u51fd\u5f0f\u88ab\u547c\u53eb\u6642\uff0c\u7cfb\u7d71\u6703\u5728\u201c\u547c\u53eb\u5806\u758a\u201d\u4e0a\u70ba\u8a72\u51fd\u5f0f\u5206\u914d\u65b0\u7684\u5806\u758a\u5e40\uff0c\u7528\u65bc\u5132\u5b58\u51fd\u5f0f\u7684\u5340\u57df\u6027\u8b8a\u6578\u3001\u53c3\u6578\u3001\u8fd4\u56de\u4f4d\u5740\u7b49\u8cc7\u6599\u3002</li> <li>\u8ff4\uff1a\u7576\u51fd\u5f0f\u5b8c\u6210\u57f7\u884c\u4e26\u8fd4\u56de\u6642\uff0c\u5c0d\u61c9\u7684\u5806\u758a\u5e40\u6703\u88ab\u5f9e\u201c\u547c\u53eb\u5806\u758a\u201d\u4e0a\u79fb\u9664\uff0c\u6062\u5fa9\u4e4b\u524d\u51fd\u5f0f\u7684\u57f7\u884c\u74b0\u5883\u3002</li> </ol> <p>\u56e0\u6b64\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e00\u500b\u986f\u5f0f\u7684\u5806\u758a\u4f86\u6a21\u64ec\u547c\u53eb\u5806\u758a\u7684\u884c\u70ba\uff0c\u5f9e\u800c\u5c07\u905e\u8ff4\u8f49\u5316\u70ba\u8fed\u4ee3\u5f62\u5f0f\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig recursion.py<pre><code>def for_loop_recur(n: int) -&gt; int:\n \"\"\"\u4f7f\u7528\u8fed\u4ee3\u6a21\u64ec\u905e\u8ff4\"\"\"\n # \u4f7f\u7528\u4e00\u500b\u986f\u5f0f\u7684\u5806\u758a\u4f86\u6a21\u64ec\u7cfb\u7d71\u547c\u53eb\u5806\u758a\n stack = []\n res = 0\n # \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n for i in range(n, 0, -1):\n # \u900f\u904e\u201c\u5165\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u905e\u201d\n stack.append(i)\n # \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n while stack:\n # \u900f\u904e\u201c\u51fa\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u8ff4\u201d\n res += stack.pop()\n # res = 1+2+3+...+n\n return res\n</code></pre> recursion.cpp<pre><code>/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u64ec\u905e\u8ff4 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u500b\u986f\u5f0f\u7684\u5806\u758a\u4f86\u6a21\u64ec\u7cfb\u7d71\u547c\u53eb\u5806\u758a\n stack&lt;int&gt; stack;\n int res = 0;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n for (int i = n; i &gt; 0; i--) {\n // \u900f\u904e\u201c\u5165\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u905e\u201d\n stack.push(i);\n }\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n while (!stack.empty()) {\n // \u900f\u904e\u201c\u51fa\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u8ff4\u201d\n res += stack.top();\n stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n</code></pre> recursion.java<pre><code>/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u64ec\u905e\u8ff4 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u500b\u986f\u5f0f\u7684\u5806\u758a\u4f86\u6a21\u64ec\u7cfb\u7d71\u547c\u53eb\u5806\u758a\n Stack&lt;Integer&gt; stack = new Stack&lt;&gt;();\n int res = 0;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n for (int i = n; i &gt; 0; i--) {\n // \u900f\u904e\u201c\u5165\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u905e\u201d\n stack.push(i);\n }\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n while (!stack.isEmpty()) {\n // \u900f\u904e\u201c\u51fa\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u8ff4\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n</code></pre> recursion.cs<pre><code>/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u64ec\u905e\u8ff4 */\nint ForLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u500b\u986f\u5f0f\u7684\u5806\u758a\u4f86\u6a21\u64ec\u7cfb\u7d71\u547c\u53eb\u5806\u758a\n Stack&lt;int&gt; stack = new();\n int res = 0;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n for (int i = n; i &gt; 0; i--) {\n // \u900f\u904e\u201c\u5165\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u905e\u201d\n stack.Push(i);\n }\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n while (stack.Count &gt; 0) {\n // \u900f\u904e\u201c\u51fa\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u8ff4\u201d\n res += stack.Pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n</code></pre> recursion.go<pre><code>/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u64ec\u905e\u8ff4 */\nfunc forLoopRecur(n int) int {\n // \u4f7f\u7528\u4e00\u500b\u986f\u5f0f\u7684\u5806\u758a\u4f86\u6a21\u64ec\u7cfb\u7d71\u547c\u53eb\u5806\u758a\n stack := list.New()\n res := 0\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n for i := n; i &gt; 0; i-- {\n // \u900f\u904e\u201c\u5165\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u905e\u201d\n stack.PushBack(i)\n }\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n for stack.Len() != 0 {\n // \u900f\u904e\u201c\u51fa\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u8ff4\u201d\n res += stack.Back().Value.(int)\n stack.Remove(stack.Back())\n }\n // res = 1+2+3+...+n\n return res\n}\n</code></pre> recursion.swift<pre><code>/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u64ec\u905e\u8ff4 */\nfunc forLoopRecur(n: Int) -&gt; Int {\n // \u4f7f\u7528\u4e00\u500b\u986f\u5f0f\u7684\u5806\u758a\u4f86\u6a21\u64ec\u7cfb\u7d71\u547c\u53eb\u5806\u758a\n var stack: [Int] = []\n var res = 0\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n for i in (1 ... n).reversed() {\n // \u900f\u904e\u201c\u5165\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u905e\u201d\n stack.append(i)\n }\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n while !stack.isEmpty {\n // \u900f\u904e\u201c\u51fa\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u8ff4\u201d\n res += stack.removeLast()\n }\n // res = 1+2+3+...+n\n return res\n}\n</code></pre> recursion.js<pre><code>/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u64ec\u905e\u8ff4 */\nfunction forLoopRecur(n) {\n // \u4f7f\u7528\u4e00\u500b\u986f\u5f0f\u7684\u5806\u758a\u4f86\u6a21\u64ec\u7cfb\u7d71\u547c\u53eb\u5806\u758a\n const stack = [];\n let res = 0;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n for (let i = n; i &gt; 0; i--) {\n // \u900f\u904e\u201c\u5165\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u905e\u201d\n stack.push(i);\n }\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n while (stack.length) {\n // \u900f\u904e\u201c\u51fa\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u8ff4\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n</code></pre> recursion.ts<pre><code>/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u64ec\u905e\u8ff4 */\nfunction forLoopRecur(n: number): number {\n // \u4f7f\u7528\u4e00\u500b\u986f\u5f0f\u7684\u5806\u758a\u4f86\u6a21\u64ec\u7cfb\u7d71\u547c\u53eb\u5806\u758a \n const stack: number[] = [];\n let res: number = 0;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n for (let i = n; i &gt; 0; i--) {\n // \u900f\u904e\u201c\u5165\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u905e\u201d\n stack.push(i);\n }\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n while (stack.length) {\n // \u900f\u904e\u201c\u51fa\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u8ff4\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n</code></pre> recursion.dart<pre><code>/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u64ec\u905e\u8ff4 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u500b\u986f\u5f0f\u7684\u5806\u758a\u4f86\u6a21\u64ec\u7cfb\u7d71\u547c\u53eb\u5806\u758a\n List&lt;int&gt; stack = [];\n int res = 0;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n for (int i = n; i &gt; 0; i--) {\n // \u900f\u904e\u201c\u5165\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u905e\u201d\n stack.add(i);\n }\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n while (!stack.isEmpty) {\n // \u900f\u904e\u201c\u51fa\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u8ff4\u201d\n res += stack.removeLast();\n }\n // res = 1+2+3+...+n\n return res;\n}\n</code></pre> recursion.rs<pre><code>/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u64ec\u905e\u8ff4 */\nfn for_loop_recur(n: i32) -&gt; i32 {\n // \u4f7f\u7528\u4e00\u500b\u986f\u5f0f\u7684\u5806\u758a\u4f86\u6a21\u64ec\u7cfb\u7d71\u547c\u53eb\u5806\u758a\n let mut stack = Vec::new();\n let mut res = 0;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n for i in (1..=n).rev() {\n // \u900f\u904e\u201c\u5165\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u905e\u201d\n stack.push(i);\n }\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n while !stack.is_empty() {\n // \u900f\u904e\u201c\u51fa\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u8ff4\u201d\n res += stack.pop().unwrap();\n }\n // res = 1+2+3+...+n\n res\n}\n</code></pre> recursion.c<pre><code>/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u64ec\u905e\u8ff4 */\nint forLoopRecur(int n) {\n int stack[1000]; // \u85c9\u52a9\u4e00\u500b\u5927\u9663\u5217\u4f86\u6a21\u64ec\u5806\u758a\n int top = -1; // \u5806\u758a\u9802\u7d22\u5f15\n int res = 0;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n for (int i = n; i &gt; 0; i--) {\n // \u900f\u904e\u201c\u5165\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u905e\u201d\n stack[1 + top++] = i;\n }\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n while (top &gt;= 0) {\n // \u900f\u904e\u201c\u51fa\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u8ff4\u201d\n res += stack[top--];\n }\n // res = 1+2+3+...+n\n return res;\n}\n</code></pre> recursion.kt<pre><code>/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u64ec\u905e\u8ff4 */\nfun forLoopRecur(n: Int): Int {\n // \u4f7f\u7528\u4e00\u500b\u986f\u5f0f\u7684\u5806\u758a\u4f86\u6a21\u64ec\u7cfb\u7d71\u547c\u53eb\u5806\u758a\n val stack = Stack&lt;Int&gt;()\n var res = 0\n // \u905e: \u905e\u8ff4\u547c\u53eb\n for (i in n downTo 0) {\n // \u900f\u904e\u201c\u5165\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u905e\u201d\n stack.push(i)\n }\n // \u8ff4: \u8fd4\u56de\u7d50\u679c\n while (stack.isNotEmpty()) {\n // \u900f\u904e\u201c\u51fa\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u8ff4\u201d\n res += stack.pop()\n }\n // res = 1+2+3+...+n\n return res\n}\n</code></pre> recursion.rb<pre><code>### \u4f7f\u7528\u8fed\u4ee3\u6a21\u64ec\u905e\u8ff4 ###\ndef for_loop_recur(n)\n # \u4f7f\u7528\u4e00\u500b\u986f\u5f0f\u7684\u5806\u758a\u4f86\u6a21\u64ec\u7cfb\u7d71\u547c\u53eb\u5806\u758a\n stack = []\n res = 0\n\n # \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n for i in n.downto(0)\n # \u900f\u904e\u201c\u5165\u5806\u758a\u64cd\u4f5c\u201d\u6a21\u64ec\u201c\u905e\u201d\n stack &lt;&lt; i\n end\n # \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n while !stack.empty?\n res += stack.pop\n end\n\n # res = 1+2+3+...+n\n res\nend\n</code></pre> recursion.zig<pre><code>// \u4f7f\u7528\u8fed\u4ee3\u6a21\u64ec\u905e\u8ff4\nfn forLoopRecur(comptime n: i32) i32 {\n // \u4f7f\u7528\u4e00\u500b\u986f\u5f0f\u7684\u5806\u758a\u4f86\u6a21\u64ec\u7cfb\u7d71\u547c\u53eb\u5806\u758a\n var stack: [n]i32 = undefined;\n var res: i32 = 0;\n // \u905e\uff1a\u905e\u8ff4\u547c\u53eb\n var i: usize = n;\n while (i &gt; 0) {\n stack[i - 1] = @intCast(i);\n i -= 1;\n }\n // \u8ff4\uff1a\u8fd4\u56de\u7d50\u679c\n var index: usize = n;\n while (index &gt; 0) {\n index -= 1;\n res += stack[index];\n }\n // res = 1+2+3+...+n\n return res;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u89c0\u5bdf\u4ee5\u4e0a\u7a0b\u5f0f\u78bc\uff0c\u7576\u905e\u8ff4\u8f49\u5316\u70ba\u8fed\u4ee3\u5f8c\uff0c\u7a0b\u5f0f\u78bc\u8b8a\u5f97\u66f4\u52a0\u8907\u96dc\u4e86\u3002\u5118\u7ba1\u8fed\u4ee3\u548c\u905e\u8ff4\u5728\u5f88\u591a\u60c5\u6cc1\u4e0b\u53ef\u4ee5\u4e92\u76f8\u8f49\u5316\uff0c\u4f46\u4e0d\u4e00\u5b9a\u503c\u5f97\u9019\u6a23\u505a\uff0c\u6709\u4ee5\u4e0b\u5169\u9ede\u539f\u56e0\u3002</p> <ul> <li>\u8f49\u5316\u5f8c\u7684\u7a0b\u5f0f\u78bc\u53ef\u80fd\u66f4\u52a0\u96e3\u4ee5\u7406\u89e3\uff0c\u53ef\u8b80\u6027\u66f4\u5dee\u3002</li> <li>\u5c0d\u65bc\u67d0\u4e9b\u8907\u96dc\u554f\u984c\uff0c\u6a21\u64ec\u7cfb\u7d71\u547c\u53eb\u5806\u758a\u7684\u884c\u70ba\u53ef\u80fd\u975e\u5e38\u56f0\u96e3\u3002</li> </ul> <p>\u7e3d\u4e4b\uff0c\u9078\u64c7\u8fed\u4ee3\u9084\u662f\u905e\u8ff4\u53d6\u6c7a\u65bc\u7279\u5b9a\u554f\u984c\u7684\u6027\u8cea\u3002\u5728\u7a0b\u5f0f\u8a2d\u8a08\u5be6\u8e10\u4e2d\uff0c\u6b0a\u8861\u5169\u8005\u7684\u512a\u52a3\u4e26\u6839\u64da\u60c5\u5883\u9078\u64c7\u5408\u9069\u7684\u65b9\u6cd5\u81f3\u95dc\u91cd\u8981\u3002</p>"},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1 \u00a0 \u6f14\u7b97\u6cd5\u6548\u7387\u8a55\u4f30","text":"<p>\u5728\u6f14\u7b97\u6cd5\u8a2d\u8a08\u4e2d\uff0c\u6211\u5011\u5148\u5f8c\u8ffd\u6c42\u4ee5\u4e0b\u5169\u500b\u5c64\u9762\u7684\u76ee\u6a19\u3002</p> <ol> <li>\u627e\u5230\u554f\u984c\u89e3\u6cd5\uff1a\u6f14\u7b97\u6cd5\u9700\u8981\u5728\u898f\u5b9a\u7684\u8f38\u5165\u7bc4\u570d\u5167\u53ef\u9760\u5730\u6c42\u5f97\u554f\u984c\u7684\u6b63\u78ba\u89e3\u3002</li> <li>\u5c0b\u6c42\u6700\u512a\u89e3\u6cd5\uff1a\u540c\u4e00\u500b\u554f\u984c\u53ef\u80fd\u5b58\u5728\u591a\u7a2e\u89e3\u6cd5\uff0c\u6211\u5011\u5e0c\u671b\u627e\u5230\u5118\u53ef\u80fd\u9ad8\u6548\u7684\u6f14\u7b97\u6cd5\u3002</li> </ol> <p>\u4e5f\u5c31\u662f\u8aaa\uff0c\u5728\u80fd\u5920\u89e3\u6c7a\u554f\u984c\u7684\u524d\u63d0\u4e0b\uff0c\u6f14\u7b97\u6cd5\u6548\u7387\u5df2\u6210\u70ba\u8861\u91cf\u6f14\u7b97\u6cd5\u512a\u52a3\u7684\u4e3b\u8981\u8a55\u50f9\u6307\u6a19\uff0c\u5b83\u5305\u62ec\u4ee5\u4e0b\u5169\u500b\u7dad\u5ea6\u3002</p> <ul> <li>\u6642\u9593\u6548\u7387\uff1a\u6f14\u7b97\u6cd5\u57f7\u884c\u901f\u5ea6\u7684\u5feb\u6162\u3002</li> <li>\u7a7a\u9593\u6548\u7387\uff1a\u6f14\u7b97\u6cd5\u4f54\u7528\u8a18\u61b6\u9ad4\u7a7a\u9593\u7684\u5927\u5c0f\u3002</li> </ul> <p>\u7c21\u800c\u8a00\u4e4b\uff0c\u6211\u5011\u7684\u76ee\u6a19\u662f\u8a2d\u8a08\u201c\u65e2\u5feb\u53c8\u7701\u201d\u7684\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u3002\u800c\u6709\u6548\u5730\u8a55\u4f30\u6f14\u7b97\u6cd5\u6548\u7387\u81f3\u95dc\u91cd\u8981\uff0c\u56e0\u70ba\u53ea\u6709\u9019\u6a23\uff0c\u6211\u5011\u624d\u80fd\u5c07\u5404\u7a2e\u6f14\u7b97\u6cd5\u9032\u884c\u5c0d\u6bd4\uff0c\u9032\u800c\u6307\u5c0e\u6f14\u7b97\u6cd5\u8a2d\u8a08\u8207\u6700\u4f73\u5316\u904e\u7a0b\u3002</p> <p>\u6548\u7387\u8a55\u4f30\u65b9\u6cd5\u4e3b\u8981\u5206\u70ba\u5169\u7a2e\uff1a\u5be6\u969b\u6e2c\u8a66\u3001\u7406\u8ad6\u4f30\u7b97\u3002</p>"},{"location":"chapter_computational_complexity/performance_evaluation/#211","title":"2.1.1 \u00a0 \u5be6\u969b\u6e2c\u8a66","text":"<p>\u5047\u8a2d\u6211\u5011\u73fe\u5728\u6709\u6f14\u7b97\u6cd5 <code>A</code> \u548c\u6f14\u7b97\u6cd5 <code>B</code> \uff0c\u5b83\u5011\u90fd\u80fd\u89e3\u6c7a\u540c\u4e00\u554f\u984c\uff0c\u73fe\u5728\u9700\u8981\u5c0d\u6bd4\u9019\u5169\u500b\u6f14\u7b97\u6cd5\u7684\u6548\u7387\u3002\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u627e\u4e00\u81fa\u8a08\u7b97\u6a5f\uff0c\u57f7\u884c\u9019\u5169\u500b\u6f14\u7b97\u6cd5\uff0c\u4e26\u76e3\u63a7\u8a18\u9304\u5b83\u5011\u7684\u57f7\u884c\u6642\u9593\u548c\u8a18\u61b6\u9ad4\u4f54\u7528\u60c5\u6cc1\u3002\u9019\u7a2e\u8a55\u4f30\u65b9\u5f0f\u80fd\u5920\u53cd\u6620\u771f\u5be6\u60c5\u6cc1\uff0c\u4f46\u4e5f\u5b58\u5728\u8f03\u5927\u7684\u4fb7\u9650\u6027\u3002</p> <p>\u4e00\u65b9\u9762\uff0c\u96e3\u4ee5\u6392\u9664\u6e2c\u8a66\u74b0\u5883\u7684\u5e72\u64fe\u56e0\u7d20\u3002\u786c\u9ad4\u914d\u7f6e\u6703\u5f71\u97ff\u6f14\u7b97\u6cd5\u7684\u6548\u80fd\u3002\u6bd4\u5982\u5728\u67d0\u81fa\u8a08\u7b97\u6a5f\u4e2d\uff0c\u6f14\u7b97\u6cd5 <code>A</code> \u7684\u57f7\u884c\u6642\u9593\u6bd4\u6f14\u7b97\u6cd5 <code>B</code> \u77ed\uff1b\u4f46\u5728\u53e6\u4e00\u81fa\u914d\u7f6e\u4e0d\u540c\u7684\u8a08\u7b97\u6a5f\u4e2d\uff0c\u53ef\u80fd\u5f97\u5230\u76f8\u53cd\u7684\u6e2c\u8a66\u7d50\u679c\u3002\u9019\u610f\u5473\u8457\u6211\u5011\u9700\u8981\u5728\u5404\u7a2e\u6a5f\u5668\u4e0a\u9032\u884c\u6e2c\u8a66\uff0c\u7d71\u8a08\u5e73\u5747\u6548\u7387\uff0c\u800c\u9019\u662f\u4e0d\u73fe\u5be6\u7684\u3002</p> <p>\u53e6\u4e00\u65b9\u9762\uff0c\u5c55\u958b\u5b8c\u6574\u6e2c\u8a66\u975e\u5e38\u8017\u8cbb\u8cc7\u6e90\u3002\u96a8\u8457\u8f38\u5165\u8cc7\u6599\u91cf\u7684\u8b8a\u5316\uff0c\u6f14\u7b97\u6cd5\u6703\u8868\u73fe\u51fa\u4e0d\u540c\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u8f38\u5165\u8cc7\u6599\u91cf\u8f03\u5c0f\u6642\uff0c\u6f14\u7b97\u6cd5 <code>A</code> \u7684\u57f7\u884c\u6642\u9593\u6bd4\u6f14\u7b97\u6cd5 <code>B</code> \u77ed\uff1b\u800c\u5728\u8f38\u5165\u8cc7\u6599\u91cf\u8f03\u5927\u6642\uff0c\u6e2c\u8a66\u7d50\u679c\u53ef\u80fd\u6070\u6070\u76f8\u53cd\u3002\u56e0\u6b64\uff0c\u70ba\u4e86\u5f97\u5230\u6709\u8aaa\u670d\u529b\u7684\u7d50\u8ad6\uff0c\u6211\u5011\u9700\u8981\u6e2c\u8a66\u5404\u7a2e\u898f\u6a21\u7684\u8f38\u5165\u8cc7\u6599\uff0c\u800c\u9019\u9700\u8981\u8017\u8cbb\u5927\u91cf\u7684\u8a08\u7b97\u8cc7\u6e90\u3002</p>"},{"location":"chapter_computational_complexity/performance_evaluation/#212","title":"2.1.2 \u00a0 \u7406\u8ad6\u4f30\u7b97","text":"<p>\u7531\u65bc\u5be6\u969b\u6e2c\u8a66\u5177\u6709\u8f03\u5927\u7684\u4fb7\u9650\u6027\uff0c\u56e0\u6b64\u6211\u5011\u53ef\u4ee5\u8003\u616e\u50c5\u900f\u904e\u4e00\u4e9b\u8a08\u7b97\u4f86\u8a55\u4f30\u6f14\u7b97\u6cd5\u7684\u6548\u7387\u3002\u9019\u7a2e\u4f30\u7b97\u65b9\u6cd5\u88ab\u7a31\u70ba\u6f38\u8fd1\u8907\u96dc\u5ea6\u5206\u6790\uff08asymptotic complexity analysis\uff09\uff0c\u7c21\u7a31\u8907\u96dc\u5ea6\u5206\u6790\u3002</p> <p>\u8907\u96dc\u5ea6\u5206\u6790\u80fd\u5920\u9ad4\u73fe\u6f14\u7b97\u6cd5\u57f7\u884c\u6240\u9700\u7684\u6642\u9593\u548c\u7a7a\u9593\u8cc7\u6e90\u8207\u8f38\u5165\u8cc7\u6599\u5927\u5c0f\u4e4b\u9593\u7684\u95dc\u4fc2\u3002\u5b83\u63cf\u8ff0\u4e86\u96a8\u8457\u8f38\u5165\u8cc7\u6599\u5927\u5c0f\u7684\u589e\u52a0\uff0c\u6f14\u7b97\u6cd5\u57f7\u884c\u6240\u9700\u6642\u9593\u548c\u7a7a\u9593\u7684\u589e\u9577\u8da8\u52e2\u3002\u9019\u500b\u5b9a\u7fa9\u6709\u4e9b\u62d7\u53e3\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u5176\u5206\u70ba\u4e09\u500b\u91cd\u9ede\u4f86\u7406\u89e3\u3002</p> <ul> <li>\u201c\u6642\u9593\u548c\u7a7a\u9593\u8cc7\u6e90\u201d\u5206\u5225\u5c0d\u61c9\u6642\u9593\u8907\u96dc\u5ea6\uff08time complexity\uff09\u548c\u7a7a\u9593\u8907\u96dc\u5ea6\uff08space complexity\uff09\u3002</li> <li>\u201c\u96a8\u8457\u8f38\u5165\u8cc7\u6599\u5927\u5c0f\u7684\u589e\u52a0\u201d\u610f\u5473\u8457\u8907\u96dc\u5ea6\u53cd\u6620\u4e86\u6f14\u7b97\u6cd5\u57f7\u884c\u6548\u7387\u8207\u8f38\u5165\u8cc7\u6599\u9ad4\u91cf\u4e4b\u9593\u7684\u95dc\u4fc2\u3002</li> <li>\u201c\u6642\u9593\u548c\u7a7a\u9593\u7684\u589e\u9577\u8da8\u52e2\u201d\u8868\u793a\u8907\u96dc\u5ea6\u5206\u6790\u95dc\u6ce8\u7684\u4e0d\u662f\u57f7\u884c\u6642\u9593\u6216\u4f54\u7528\u7a7a\u9593\u7684\u5177\u9ad4\u503c\uff0c\u800c\u662f\u6642\u9593\u6216\u7a7a\u9593\u589e\u9577\u7684\u201c\u5feb\u6162\u201d\u3002</li> </ul> <p>\u8907\u96dc\u5ea6\u5206\u6790\u514b\u670d\u4e86\u5be6\u969b\u6e2c\u8a66\u65b9\u6cd5\u7684\u5f0a\u7aef\uff0c\u9ad4\u73fe\u5728\u4ee5\u4e0b\u5169\u500b\u65b9\u9762\u3002</p> <ul> <li>\u5b83\u7368\u7acb\u65bc\u6e2c\u8a66\u74b0\u5883\uff0c\u5206\u6790\u7d50\u679c\u9069\u7528\u65bc\u6240\u6709\u57f7\u884c\u5e73\u81fa\u3002</li> <li>\u5b83\u53ef\u4ee5\u9ad4\u73fe\u4e0d\u540c\u8cc7\u6599\u91cf\u4e0b\u7684\u6f14\u7b97\u6cd5\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u5927\u8cc7\u6599\u91cf\u4e0b\u7684\u6f14\u7b97\u6cd5\u6548\u80fd\u3002</li> </ul> <p>Tip</p> <p>\u5982\u679c\u4f60\u4ecd\u5c0d\u8907\u96dc\u5ea6\u7684\u6982\u5ff5\u611f\u5230\u56f0\u60d1\uff0c\u7121\u9808\u64d4\u5fc3\uff0c\u6211\u5011\u6703\u5728\u5f8c\u7e8c\u7ae0\u7bc0\u4e2d\u8a73\u7d30\u4ecb\u7d39\u3002</p> <p>\u8907\u96dc\u5ea6\u5206\u6790\u70ba\u6211\u5011\u63d0\u4f9b\u4e86\u4e00\u628a\u8a55\u4f30\u6f14\u7b97\u6cd5\u6548\u7387\u7684\u201c\u6a19\u5c3a\u201d\uff0c\u4f7f\u6211\u5011\u53ef\u4ee5\u8861\u91cf\u57f7\u884c\u67d0\u500b\u6f14\u7b97\u6cd5\u6240\u9700\u7684\u6642\u9593\u548c\u7a7a\u9593\u8cc7\u6e90\uff0c\u5c0d\u6bd4\u4e0d\u540c\u6f14\u7b97\u6cd5\u4e4b\u9593\u7684\u6548\u7387\u3002</p> <p>\u8907\u96dc\u5ea6\u662f\u500b\u6578\u5b78\u6982\u5ff5\uff0c\u5c0d\u65bc\u521d\u5b78\u8005\u53ef\u80fd\u6bd4\u8f03\u62bd\u8c61\uff0c\u5b78\u7fd2\u96e3\u5ea6\u76f8\u5c0d\u8f03\u9ad8\u3002\u5f9e\u9019\u500b\u89d2\u5ea6\u770b\uff0c\u8907\u96dc\u5ea6\u5206\u6790\u53ef\u80fd\u4e0d\u592a\u9069\u5408\u4f5c\u70ba\u6700\u5148\u4ecb\u7d39\u7684\u5167\u5bb9\u3002\u7136\u800c\uff0c\u7576\u6211\u5011\u8a0e\u8ad6\u67d0\u500b\u8cc7\u6599\u7d50\u69cb\u6216\u6f14\u7b97\u6cd5\u7684\u7279\u9ede\u6642\uff0c\u96e3\u4ee5\u907f\u514d\u8981\u5206\u6790\u5176\u57f7\u884c\u901f\u5ea6\u548c\u7a7a\u9593\u4f7f\u7528\u60c5\u6cc1\u3002</p> <p>\u7d9c\u4e0a\u6240\u8ff0\uff0c\u5efa\u8b70\u4f60\u5728\u6df1\u5165\u5b78\u7fd2\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u4e4b\u524d\uff0c\u5148\u5c0d\u8907\u96dc\u5ea6\u5206\u6790\u5efa\u7acb\u521d\u6b65\u7684\u77ad\u89e3\uff0c\u4ee5\u4fbf\u80fd\u5920\u5b8c\u6210\u7c21\u55ae\u6f14\u7b97\u6cd5\u7684\u8907\u96dc\u5ea6\u5206\u6790\u3002</p>"},{"location":"chapter_computational_complexity/space_complexity/","title":"2.4 \u00a0 \u7a7a\u9593\u8907\u96dc\u5ea6","text":"<p>\u7a7a\u9593\u8907\u96dc\u5ea6\uff08space complexity\uff09\u7528\u65bc\u8861\u91cf\u6f14\u7b97\u6cd5\u4f54\u7528\u8a18\u61b6\u9ad4\u7a7a\u9593\u96a8\u8457\u8cc7\u6599\u91cf\u8b8a\u5927\u6642\u7684\u589e\u9577\u8da8\u52e2\u3002\u9019\u500b\u6982\u5ff5\u8207\u6642\u9593\u8907\u96dc\u5ea6\u975e\u5e38\u985e\u4f3c\uff0c\u53ea\u9700\u5c07\u201c\u57f7\u884c\u6642\u9593\u201d\u66ff\u63db\u70ba\u201c\u4f54\u7528\u8a18\u61b6\u9ad4\u7a7a\u9593\u201d\u3002</p>"},{"location":"chapter_computational_complexity/space_complexity/#241","title":"2.4.1 \u00a0 \u6f14\u7b97\u6cd5\u76f8\u95dc\u7a7a\u9593","text":"<p>\u6f14\u7b97\u6cd5\u5728\u57f7\u884c\u904e\u7a0b\u4e2d\u4f7f\u7528\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u5e7e\u7a2e\u3002</p> <ul> <li>\u8f38\u5165\u7a7a\u9593\uff1a\u7528\u65bc\u5132\u5b58\u6f14\u7b97\u6cd5\u7684\u8f38\u5165\u8cc7\u6599\u3002</li> <li>\u66ab\u5b58\u7a7a\u9593\uff1a\u7528\u65bc\u5132\u5b58\u6f14\u7b97\u6cd5\u5728\u57f7\u884c\u904e\u7a0b\u4e2d\u7684\u8b8a\u6578\u3001\u7269\u4ef6\u3001\u51fd\u5f0f\u4e0a\u4e0b\u6587\u7b49\u8cc7\u6599\u3002</li> <li>\u8f38\u51fa\u7a7a\u9593\uff1a\u7528\u65bc\u5132\u5b58\u6f14\u7b97\u6cd5\u7684\u8f38\u51fa\u8cc7\u6599\u3002</li> </ul> <p>\u4e00\u822c\u60c5\u6cc1\u4e0b\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u7684\u7d71\u8a08\u7bc4\u570d\u662f\u201c\u66ab\u5b58\u7a7a\u9593\u201d\u52a0\u4e0a\u201c\u8f38\u51fa\u7a7a\u9593\u201d\u3002</p> <p>\u66ab\u5b58\u7a7a\u9593\u53ef\u4ee5\u9032\u4e00\u6b65\u5283\u5206\u70ba\u4e09\u500b\u90e8\u5206\u3002</p> <ul> <li>\u66ab\u5b58\u8cc7\u6599\uff1a\u7528\u65bc\u5132\u5b58\u6f14\u7b97\u6cd5\u57f7\u884c\u904e\u7a0b\u4e2d\u7684\u5404\u7a2e\u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u7b49\u3002</li> <li>\u5806\u758a\u5e40\u7a7a\u9593\uff1a\u7528\u65bc\u5132\u5b58\u547c\u53eb\u51fd\u5f0f\u7684\u4e0a\u4e0b\u6587\u8cc7\u6599\u3002\u7cfb\u7d71\u5728\u6bcf\u6b21\u547c\u53eb\u51fd\u5f0f\u6642\u90fd\u6703\u5728\u5806\u758a\u9802\u90e8\u5efa\u7acb\u4e00\u500b\u5806\u758a\u5e40\uff0c\u51fd\u5f0f\u8fd4\u56de\u5f8c\uff0c\u5806\u758a\u5e40\u7a7a\u9593\u6703\u88ab\u91cb\u653e\u3002</li> <li>\u6307\u4ee4\u7a7a\u9593\uff1a\u7528\u65bc\u5132\u5b58\u7de8\u8b6f\u5f8c\u7684\u7a0b\u5f0f\u6307\u4ee4\uff0c\u5728\u5be6\u969b\u7d71\u8a08\u4e2d\u901a\u5e38\u5ffd\u7565\u4e0d\u8a08\u3002</li> </ul> <p>\u5728\u5206\u6790\u4e00\u6bb5\u7a0b\u5f0f\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u6642\uff0c\u6211\u5011\u901a\u5e38\u7d71\u8a08\u66ab\u5b58\u8cc7\u6599\u3001\u5806\u758a\u5e40\u7a7a\u9593\u548c\u8f38\u51fa\u8cc7\u6599\u4e09\u90e8\u5206\uff0c\u5982\u5716 2-15 \u6240\u793a\u3002</p> <p></p> <p> \u5716 2-15 \u00a0 \u6f14\u7b97\u6cd5\u4f7f\u7528\u7684\u76f8\u95dc\u7a7a\u9593 </p> <p>\u76f8\u95dc\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code>class Node:\n \"\"\"\u985e\u5225\"\"\"\n def __init__(self, x: int):\n self.val: int = x # \u7bc0\u9ede\u503c\n self.next: Node | None = None # \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u5f15\u7528\n\ndef function() -&gt; int:\n \"\"\"\u51fd\u5f0f\"\"\"\n # \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n\ndef algorithm(n) -&gt; int: # \u8f38\u5165\u8cc7\u6599\n A = 0 # \u66ab\u5b58\u8cc7\u6599\uff08\u5e38\u6578\uff0c\u4e00\u822c\u7528\u5927\u5beb\u5b57\u6bcd\u8868\u793a\uff09\n b = 0 # \u66ab\u5b58\u8cc7\u6599\uff08\u8b8a\u6578\uff09\n node = Node(0) # \u66ab\u5b58\u8cc7\u6599\uff08\u7269\u4ef6\uff09\n c = function() # \u5806\u758a\u5e40\u7a7a\u9593\uff08\u547c\u53eb\u51fd\u5f0f\uff09\n return A + b + c # \u8f38\u51fa\u8cc7\u6599\n</code></pre> <pre><code>/* \u7d50\u69cb\u9ad4 */\nstruct Node {\n int val;\n Node *next;\n Node(int x) : val(x), next(nullptr) {}\n};\n\n/* \u51fd\u5f0f */\nint func() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint algorithm(int n) { // \u8f38\u5165\u8cc7\u6599\n const int a = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u5e38\u6578\uff09\n int b = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u8b8a\u6578\uff09\n Node* node = new Node(0); // \u66ab\u5b58\u8cc7\u6599\uff08\u7269\u4ef6\uff09\n int c = func(); // \u5806\u758a\u5e40\u7a7a\u9593\uff08\u547c\u53eb\u51fd\u5f0f\uff09\n return a + b + c; // \u8f38\u51fa\u8cc7\u6599\n}\n</code></pre> <pre><code>/* \u985e\u5225 */\nclass Node {\n int val;\n Node next;\n Node(int x) { val = x; }\n}\n\n/* \u51fd\u5f0f */\nint function() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint algorithm(int n) { // \u8f38\u5165\u8cc7\u6599\n final int a = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u5e38\u6578\uff09\n int b = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u8b8a\u6578\uff09\n Node node = new Node(0); // \u66ab\u5b58\u8cc7\u6599\uff08\u7269\u4ef6\uff09\n int c = function(); // \u5806\u758a\u5e40\u7a7a\u9593\uff08\u547c\u53eb\u51fd\u5f0f\uff09\n return a + b + c; // \u8f38\u51fa\u8cc7\u6599\n}\n</code></pre> <pre><code>/* \u985e\u5225 */\nclass Node(int x) {\n int val = x;\n Node next;\n}\n\n/* \u51fd\u5f0f */\nint Function() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint Algorithm(int n) { // \u8f38\u5165\u8cc7\u6599\n const int a = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u5e38\u6578\uff09\n int b = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u8b8a\u6578\uff09\n Node node = new(0); // \u66ab\u5b58\u8cc7\u6599\uff08\u7269\u4ef6\uff09\n int c = Function(); // \u5806\u758a\u5e40\u7a7a\u9593\uff08\u547c\u53eb\u51fd\u5f0f\uff09\n return a + b + c; // \u8f38\u51fa\u8cc7\u6599\n}\n</code></pre> <pre><code>/* \u7d50\u69cb\u9ad4 */\ntype node struct {\n val int\n next *node\n}\n\n/* \u5efa\u7acb node \u7d50\u69cb\u9ad4 */\nfunc newNode(val int) *node {\n return &amp;node{val: val}\n}\n\n/* \u51fd\u5f0f */\nfunc function() int {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n}\n\nfunc algorithm(n int) int { // \u8f38\u5165\u8cc7\u6599\n const a = 0 // \u66ab\u5b58\u8cc7\u6599\uff08\u5e38\u6578\uff09\n b := 0 // \u66ab\u5b58\u8cc7\u6599\uff08\u8b8a\u6578\uff09\n newNode(0) // \u66ab\u5b58\u8cc7\u6599\uff08\u7269\u4ef6\uff09\n c := function() // \u5806\u758a\u5e40\u7a7a\u9593\uff08\u547c\u53eb\u51fd\u5f0f\uff09\n return a + b + c // \u8f38\u51fa\u8cc7\u6599\n}\n</code></pre> <pre><code>/* \u985e\u5225 */\nclass Node {\n var val: Int\n var next: Node?\n\n init(x: Int) {\n val = x\n }\n}\n\n/* \u51fd\u5f0f */\nfunc function() -&gt; Int {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n}\n\nfunc algorithm(n: Int) -&gt; Int { // \u8f38\u5165\u8cc7\u6599\n let a = 0 // \u66ab\u5b58\u8cc7\u6599\uff08\u5e38\u6578\uff09\n var b = 0 // \u66ab\u5b58\u8cc7\u6599\uff08\u8b8a\u6578\uff09\n let node = Node(x: 0) // \u66ab\u5b58\u8cc7\u6599\uff08\u7269\u4ef6\uff09\n let c = function() // \u5806\u758a\u5e40\u7a7a\u9593\uff08\u547c\u53eb\u51fd\u5f0f\uff09\n return a + b + c // \u8f38\u51fa\u8cc7\u6599\n}\n</code></pre> <pre><code>/* \u985e\u5225 */\nclass Node {\n val;\n next;\n constructor(val) {\n this.val = val === undefined ? 0 : val; // \u7bc0\u9ede\u503c\n this.next = null; // \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u5f15\u7528\n }\n}\n\n/* \u51fd\u5f0f */\nfunction constFunc() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\nfunction algorithm(n) { // \u8f38\u5165\u8cc7\u6599\n const a = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u5e38\u6578\uff09\n let b = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u8b8a\u6578\uff09\n const node = new Node(0); // \u66ab\u5b58\u8cc7\u6599\uff08\u7269\u4ef6\uff09\n const c = constFunc(); // \u5806\u758a\u5e40\u7a7a\u9593\uff08\u547c\u53eb\u51fd\u5f0f\uff09\n return a + b + c; // \u8f38\u51fa\u8cc7\u6599\n}\n</code></pre> <pre><code>/* \u985e\u5225 */\nclass Node {\n val: number;\n next: Node | null;\n constructor(val?: number) {\n this.val = val === undefined ? 0 : val; // \u7bc0\u9ede\u503c\n this.next = null; // \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u5f15\u7528\n }\n}\n\n/* \u51fd\u5f0f */\nfunction constFunc(): number {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\nfunction algorithm(n: number): number { // \u8f38\u5165\u8cc7\u6599\n const a = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u5e38\u6578\uff09\n let b = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u8b8a\u6578\uff09\n const node = new Node(0); // \u66ab\u5b58\u8cc7\u6599\uff08\u7269\u4ef6\uff09\n const c = constFunc(); // \u5806\u758a\u5e40\u7a7a\u9593\uff08\u547c\u53eb\u51fd\u5f0f\uff09\n return a + b + c; // \u8f38\u51fa\u8cc7\u6599\n}\n</code></pre> <pre><code>/* \u985e\u5225 */\nclass Node {\n int val;\n Node next;\n Node(this.val, [this.next]);\n}\n\n/* \u51fd\u5f0f */\nint function() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint algorithm(int n) { // \u8f38\u5165\u8cc7\u6599\n const int a = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u5e38\u6578\uff09\n int b = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u8b8a\u6578\uff09\n Node node = Node(0); // \u66ab\u5b58\u8cc7\u6599\uff08\u7269\u4ef6\uff09\n int c = function(); // \u5806\u758a\u5e40\u7a7a\u9593\uff08\u547c\u53eb\u51fd\u5f0f\uff09\n return a + b + c; // \u8f38\u51fa\u8cc7\u6599\n}\n</code></pre> <pre><code>use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u7d50\u69cb\u9ad4 */\nstruct Node {\n val: i32,\n next: Option&lt;Rc&lt;RefCell&lt;Node&gt;&gt;&gt;,\n}\n\n/* \u5efa\u7acb Node \u7d50\u69cb\u9ad4 */\nimpl Node {\n fn new(val: i32) -&gt; Self {\n Self { val: val, next: None }\n }\n}\n\n/* \u51fd\u5f0f */\nfn function() -&gt; i32 { \n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nfn algorithm(n: i32) -&gt; i32 { // \u8f38\u5165\u8cc7\u6599\n const a: i32 = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u5e38\u6578\uff09\n let mut b = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u8b8a\u6578\uff09\n let node = Node::new(0); // \u66ab\u5b58\u8cc7\u6599\uff08\u7269\u4ef6\uff09\n let c = function(); // \u5806\u758a\u5e40\u7a7a\u9593\uff08\u547c\u53eb\u51fd\u5f0f\uff09\n return a + b + c; // \u8f38\u51fa\u8cc7\u6599\n}\n</code></pre> <pre><code>/* \u51fd\u5f0f */\nint func() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0;\n}\n\nint algorithm(int n) { // \u8f38\u5165\u8cc7\u6599\n const int a = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u5e38\u6578\uff09\n int b = 0; // \u66ab\u5b58\u8cc7\u6599\uff08\u8b8a\u6578\uff09\n int c = func(); // \u5806\u758a\u5e40\u7a7a\u9593\uff08\u547c\u53eb\u51fd\u5f0f\uff09\n return a + b + c; // \u8f38\u51fa\u8cc7\u6599\n}\n</code></pre> <pre><code>/* \u985e\u5225 */\nclass Node(var _val: Int) {\n var next: Node? = null\n}\n\n/* \u51fd\u5f0f */\nfun function(): Int {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n}\n\nfun algorithm(n: Int): Int { // \u8f38\u5165\u8cc7\u6599\n val a = 0 // \u66ab\u5b58\u8cc7\u6599\uff08\u5e38\u6578\uff09\n var b = 0 // \u66ab\u5b58\u8cc7\u6599\uff08\u8b8a\u6578\uff09\n val node = Node(0) // \u66ab\u5b58\u8cc7\u6599\uff08\u7269\u4ef6\uff09\n val c = function() // \u5806\u758a\u5e40\u7a7a\u9593\uff08\u547c\u53eb\u51fd\u5f0f\uff09\n return a + b + c // \u8f38\u51fa\u8cc7\u6599\n}\n</code></pre> <pre><code>### \u985e\u5225 ###\nclass Node\n attr_accessor :val # \u7bc0\u9ede\u503c\n attr_accessor :next # \u6307\u5411\u4e0b\u4e00\u7bc0\u9ede\u7684\u5f15\u7528\n\n def initialize(x)\n @val = x\n end\nend\n\n### \u51fd\u5f0f ###\ndef function\n # \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c...\n 0\nend\n\n### \u6f14\u7b97\u6cd5 ###\ndef algorithm(n) # \u8f38\u5165\u8cc7\u6599\n a = 0 # \u66ab\u5b58\u8cc7\u6599\uff08\u5e38\u6578\uff09\n b = 0 # \u66ab\u5b58\u8cc7\u6599\uff08\u8b8a\u6578\uff09\n node = Node.new(0) # \u66ab\u5b58\u8cc7\u6599\uff08\u7269\u4ef6\uff09\n c = function # \u5806\u758a\u5e40\u7a7a\u9593\uff08\u547c\u53eb\u51fd\u5f0f\uff09\n a + b + c # \u8f38\u51fa\u8cc7\u6599\nend\n</code></pre> <pre><code>\n</code></pre>"},{"location":"chapter_computational_complexity/space_complexity/#242","title":"2.4.2 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"<p>\u7a7a\u9593\u8907\u96dc\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u8207\u6642\u9593\u8907\u96dc\u5ea6\u5927\u81f4\u76f8\u540c\uff0c\u53ea\u9700\u5c07\u7d71\u8a08\u7269\u4ef6\u5f9e\u201c\u64cd\u4f5c\u6578\u91cf\u201d\u8f49\u70ba\u201c\u4f7f\u7528\u7a7a\u9593\u5927\u5c0f\u201d\u3002</p> <p>\u800c\u8207\u6642\u9593\u8907\u96dc\u5ea6\u4e0d\u540c\u7684\u662f\uff0c\u6211\u5011\u901a\u5e38\u53ea\u95dc\u6ce8\u6700\u5dee\u7a7a\u9593\u8907\u96dc\u5ea6\u3002\u9019\u662f\u56e0\u70ba\u8a18\u61b6\u9ad4\u7a7a\u9593\u662f\u4e00\u9805\u786c\u6027\u8981\u6c42\uff0c\u6211\u5011\u5fc5\u9808\u78ba\u4fdd\u5728\u6240\u6709\u8f38\u5165\u8cc7\u6599\u4e0b\u90fd\u6709\u8db3\u5920\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u9810\u7559\u3002</p> <p>\u89c0\u5bdf\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff0c\u6700\u5dee\u7a7a\u9593\u8907\u96dc\u5ea6\u4e2d\u7684\u201c\u6700\u5dee\u201d\u6709\u5169\u5c64\u542b\u7fa9\u3002</p> <ol> <li>\u4ee5\u6700\u5dee\u8f38\u5165\u8cc7\u6599\u70ba\u6e96\uff1a\u7576 \\(n &lt; 10\\) \u6642\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(1)\\) \uff1b\u4f46\u7576 \\(n &gt; 10\\) \u6642\uff0c\u521d\u59cb\u5316\u7684\u9663\u5217 <code>nums</code> \u4f54\u7528 \\(O(n)\\) \u7a7a\u9593\uff0c\u56e0\u6b64\u6700\u5dee\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \u3002</li> <li>\u4ee5\u6f14\u7b97\u6cd5\u57f7\u884c\u4e2d\u7684\u5cf0\u503c\u8a18\u61b6\u9ad4\u70ba\u6e96\uff1a\u4f8b\u5982\uff0c\u7a0b\u5f0f\u5728\u57f7\u884c\u6700\u5f8c\u4e00\u884c\u4e4b\u524d\uff0c\u4f54\u7528 \\(O(1)\\) \u7a7a\u9593\uff1b\u7576\u521d\u59cb\u5316\u9663\u5217 <code>nums</code> \u6642\uff0c\u7a0b\u5f0f\u4f54\u7528 \\(O(n)\\) \u7a7a\u9593\uff0c\u56e0\u6b64\u6700\u5dee\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \u3002</li> </ol> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code>def algorithm(n: int):\n a = 0 # O(1)\n b = [0] * 10000 # O(1)\n if n &gt; 10:\n nums = [0] * n # O(n)\n</code></pre> <pre><code>void algorithm(int n) {\n int a = 0; // O(1)\n vector&lt;int&gt; b(10000); // O(1)\n if (n &gt; 10)\n vector&lt;int&gt; nums(n); // O(n)\n}\n</code></pre> <pre><code>void algorithm(int n) {\n int a = 0; // O(1)\n int[] b = new int[10000]; // O(1)\n if (n &gt; 10)\n int[] nums = new int[n]; // O(n)\n}\n</code></pre> <pre><code>void Algorithm(int n) {\n int a = 0; // O(1)\n int[] b = new int[10000]; // O(1)\n if (n &gt; 10) {\n int[] nums = new int[n]; // O(n)\n }\n}\n</code></pre> <pre><code>func algorithm(n int) {\n a := 0 // O(1)\n b := make([]int, 10000) // O(1)\n var nums []int\n if n &gt; 10 {\n nums := make([]int, n) // O(n)\n }\n fmt.Println(a, b, nums)\n}\n</code></pre> <pre><code>func algorithm(n: Int) {\n let a = 0 // O(1)\n let b = Array(repeating: 0, count: 10000) // O(1)\n if n &gt; 10 {\n let nums = Array(repeating: 0, count: n) // O(n)\n }\n}\n</code></pre> <pre><code>function algorithm(n) {\n const a = 0; // O(1)\n const b = new Array(10000); // O(1)\n if (n &gt; 10) {\n const nums = new Array(n); // O(n)\n }\n}\n</code></pre> <pre><code>function algorithm(n: number): void {\n const a = 0; // O(1)\n const b = new Array(10000); // O(1)\n if (n &gt; 10) {\n const nums = new Array(n); // O(n)\n }\n}\n</code></pre> <pre><code>void algorithm(int n) {\n int a = 0; // O(1)\n List&lt;int&gt; b = List.filled(10000, 0); // O(1)\n if (n &gt; 10) {\n List&lt;int&gt; nums = List.filled(n, 0); // O(n)\n }\n}\n</code></pre> <pre><code>fn algorithm(n: i32) {\n let a = 0; // O(1)\n let b = [0; 10000]; // O(1)\n if n &gt; 10 {\n let nums = vec![0; n as usize]; // O(n)\n }\n}\n</code></pre> <pre><code>void algorithm(int n) {\n int a = 0; // O(1)\n int b[10000]; // O(1)\n if (n &gt; 10)\n int nums[n] = {0}; // O(n)\n}\n</code></pre> <pre><code>fun algorithm(n: Int) {\n val a = 0 // O(1)\n val b = IntArray(10000) // O(1)\n if (n &gt; 10) {\n val nums = IntArray(n) // O(n)\n }\n}\n</code></pre> <pre><code>def algorithm(n)\n a = 0 # O(1)\n b = Array.new(10000) # O(1)\n nums = Array.new(n) if n &gt; 10 # O(n)\nend\n</code></pre> <pre><code>\n</code></pre> <p>\u5728\u905e\u8ff4\u51fd\u5f0f\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u7d71\u8a08\u5806\u758a\u5e40\u7a7a\u9593\u3002\u89c0\u5bdf\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code>def function() -&gt; int:\n # \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n\ndef loop(n: int):\n \"\"\"\u8ff4\u5708\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(1)\"\"\"\n for _ in range(n):\n function()\n\ndef recur(n: int):\n \"\"\"\u905e\u8ff4\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n)\"\"\"\n if n == 1:\n return\n return recur(n - 1)\n</code></pre> <pre><code>int func() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u8ff4\u5708\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(1) */\nvoid loop(int n) {\n for (int i = 0; i &lt; n; i++) {\n func();\n }\n}\n/* \u905e\u8ff4\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n</code></pre> <pre><code>int function() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u8ff4\u5708\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(1) */\nvoid loop(int n) {\n for (int i = 0; i &lt; n; i++) {\n function();\n }\n}\n/* \u905e\u8ff4\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n</code></pre> <pre><code>int Function() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u8ff4\u5708\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(1) */\nvoid Loop(int n) {\n for (int i = 0; i &lt; n; i++) {\n Function();\n }\n}\n/* \u905e\u8ff4\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n) */\nint Recur(int n) {\n if (n == 1) return 1;\n return Recur(n - 1);\n}\n</code></pre> <pre><code>func function() int {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n\n/* \u8ff4\u5708\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(1) */\nfunc loop(n int) {\n for i := 0; i &lt; n; i++ {\n function()\n }\n}\n\n/* \u905e\u8ff4\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n) */\nfunc recur(n int) {\n if n == 1 {\n return\n }\n recur(n - 1)\n}\n</code></pre> <pre><code>@discardableResult\nfunc function() -&gt; Int {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n\n/* \u8ff4\u5708\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(1) */\nfunc loop(n: Int) {\n for _ in 0 ..&lt; n {\n function()\n }\n}\n\n/* \u905e\u8ff4\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n) */\nfunc recur(n: Int) {\n if n == 1 {\n return\n }\n recur(n: n - 1)\n}\n</code></pre> <pre><code>function constFunc() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u8ff4\u5708\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(1) */\nfunction loop(n) {\n for (let i = 0; i &lt; n; i++) {\n constFunc();\n }\n}\n/* \u905e\u8ff4\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n) */\nfunction recur(n) {\n if (n === 1) return;\n return recur(n - 1);\n}\n</code></pre> <pre><code>function constFunc(): number {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u8ff4\u5708\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(1) */\nfunction loop(n: number): void {\n for (let i = 0; i &lt; n; i++) {\n constFunc();\n }\n}\n/* \u905e\u8ff4\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n) */\nfunction recur(n: number): void {\n if (n === 1) return;\n return recur(n - 1);\n}\n</code></pre> <pre><code>int function() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u8ff4\u5708\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(1) */\nvoid loop(int n) {\n for (int i = 0; i &lt; n; i++) {\n function();\n }\n}\n/* \u905e\u8ff4\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n</code></pre> <pre><code>fn function() -&gt; i32 {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u8ff4\u5708\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(1) */\nfn loop(n: i32) {\n for i in 0..n {\n function();\n }\n}\n/* \u905e\u8ff4\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n) */\nfn recur(n: i32) {\n if n == 1 {\n return;\n }\n recur(n - 1);\n}\n</code></pre> <pre><code>int func() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n/* \u8ff4\u5708\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(1) */\nvoid loop(int n) {\n for (int i = 0; i &lt; n; i++) {\n func();\n }\n}\n/* \u905e\u8ff4\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n</code></pre> <pre><code>fun function(): Int {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n/* \u8ff4\u5708\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(1) */\nfun loop(n: Int) {\n for (i in 0..&lt;n) {\n function()\n }\n}\n/* \u905e\u8ff4\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n) */\nfun recur(n: Int) {\n if (n == 1) return\n return recur(n - 1)\n}\n</code></pre> <pre><code>def function\n # \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n 0\nend\n\n### \u8ff4\u5708\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(1) ###\ndef loop(n)\n (0...n).each { function }\nend\n\n### \u905e\u8ff4\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n) ###\ndef recur(n)\n return if n == 1\n recur(n - 1)\nend\n</code></pre> <pre><code>\n</code></pre> <p>\u51fd\u5f0f <code>loop()</code> \u548c <code>recur()</code> \u7684\u6642\u9593\u8907\u96dc\u5ea6\u90fd\u70ba \\(O(n)\\) \uff0c\u4f46\u7a7a\u9593\u8907\u96dc\u5ea6\u4e0d\u540c\u3002</p> <ul> <li>\u51fd\u5f0f <code>loop()</code> \u5728\u8ff4\u5708\u4e2d\u547c\u53eb\u4e86 \\(n\\) \u6b21 <code>function()</code> \uff0c\u6bcf\u8f2a\u4e2d\u7684 <code>function()</code> \u90fd\u8fd4\u56de\u4e26\u91cb\u653e\u4e86\u5806\u758a\u5e40\u7a7a\u9593\uff0c\u56e0\u6b64\u7a7a\u9593\u8907\u96dc\u5ea6\u4ecd\u70ba \\(O(1)\\) \u3002</li> <li>\u905e\u8ff4\u51fd\u5f0f <code>recur()</code> \u5728\u57f7\u884c\u904e\u7a0b\u4e2d\u6703\u540c\u6642\u5b58\u5728 \\(n\\) \u500b\u672a\u8fd4\u56de\u7684 <code>recur()</code> \uff0c\u5f9e\u800c\u4f54\u7528 \\(O(n)\\) \u7684\u5806\u758a\u5e40\u7a7a\u9593\u3002</li> </ul>"},{"location":"chapter_computational_complexity/space_complexity/#243","title":"2.4.3 \u00a0 \u5e38\u898b\u578b\u5225","text":"<p>\u8a2d\u8f38\u5165\u8cc7\u6599\u5927\u5c0f\u70ba \\(n\\) \uff0c\u5716 2-16 \u5c55\u793a\u4e86\u5e38\u898b\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u578b\u5225\uff08\u5f9e\u4f4e\u5230\u9ad8\u6392\u5217\uff09\u3002</p> \\[ \\begin{aligned} O(1) &lt; O(\\log n) &lt; O(n) &lt; O(n^2) &lt; O(2^n) \\newline \\text{\u5e38\u6578\u968e} &lt; \\text{\u5c0d\u6578\u968e} &lt; \\text{\u7dda\u6027\u968e} &lt; \\text{\u5e73\u65b9\u968e} &lt; \\text{\u6307\u6578\u968e} \\end{aligned} \\] <p></p> <p> \u5716 2-16 \u00a0 \u5e38\u898b\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u578b\u5225 </p>"},{"location":"chapter_computational_complexity/space_complexity/#1-o1","title":"1. \u00a0 \u5e38\u6578\u968e \\(O(1)\\)","text":"<p>\u5e38\u6578\u968e\u5e38\u898b\u65bc\u6578\u91cf\u8207\u8f38\u5165\u8cc7\u6599\u5927\u5c0f \\(n\\) \u7121\u95dc\u7684\u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u3002</p> <p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u8ff4\u5708\u4e2d\u521d\u59cb\u5316\u8b8a\u6578\u6216\u547c\u53eb\u51fd\u5f0f\u800c\u4f54\u7528\u7684\u8a18\u61b6\u9ad4\uff0c\u5728\u9032\u5165\u4e0b\u4e00\u8ff4\u5708\u5f8c\u5c31\u6703\u88ab\u91cb\u653e\uff0c\u56e0\u6b64\u4e0d\u6703\u7d2f\u7a4d\u4f54\u7528\u7a7a\u9593\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u4ecd\u70ba \\(O(1)\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.py<pre><code>def function() -&gt; int:\n \"\"\"\u51fd\u5f0f\"\"\"\n # \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n\ndef constant(n: int):\n \"\"\"\u5e38\u6578\u968e\"\"\"\n # \u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u4f54\u7528 O(1) \u7a7a\u9593\n a = 0\n nums = [0] * 10000\n node = ListNode(0)\n # \u8ff4\u5708\u4e2d\u7684\u8b8a\u6578\u4f54\u7528 O(1) \u7a7a\u9593\n for _ in range(n):\n c = 0\n # \u8ff4\u5708\u4e2d\u7684\u51fd\u5f0f\u4f54\u7528 O(1) \u7a7a\u9593\n for _ in range(n):\n function()\n</code></pre> space_complexity.cpp<pre><code>/* \u51fd\u5f0f */\nint func() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6578\u968e */\nvoid constant(int n) {\n // \u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u4f54\u7528 O(1) \u7a7a\u9593\n const int a = 0;\n int b = 0;\n vector&lt;int&gt; nums(10000);\n ListNode node(0);\n // \u8ff4\u5708\u4e2d\u7684\u8b8a\u6578\u4f54\u7528 O(1) \u7a7a\u9593\n for (int i = 0; i &lt; n; i++) {\n int c = 0;\n }\n // \u8ff4\u5708\u4e2d\u7684\u51fd\u5f0f\u4f54\u7528 O(1) \u7a7a\u9593\n for (int i = 0; i &lt; n; i++) {\n func();\n }\n}\n</code></pre> space_complexity.java<pre><code>/* \u51fd\u5f0f */\nint function() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6578\u968e */\nvoid constant(int n) {\n // \u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u4f54\u7528 O(1) \u7a7a\u9593\n final int a = 0;\n int b = 0;\n int[] nums = new int[10000];\n ListNode node = new ListNode(0);\n // \u8ff4\u5708\u4e2d\u7684\u8b8a\u6578\u4f54\u7528 O(1) \u7a7a\u9593\n for (int i = 0; i &lt; n; i++) {\n int c = 0;\n }\n // \u8ff4\u5708\u4e2d\u7684\u51fd\u5f0f\u4f54\u7528 O(1) \u7a7a\u9593\n for (int i = 0; i &lt; n; i++) {\n function();\n }\n}\n</code></pre> space_complexity.cs<pre><code>/* \u51fd\u5f0f */\nint Function() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6578\u968e */\nvoid Constant(int n) {\n // \u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u4f54\u7528 O(1) \u7a7a\u9593\n int a = 0;\n int b = 0;\n int[] nums = new int[10000];\n ListNode node = new(0);\n // \u8ff4\u5708\u4e2d\u7684\u8b8a\u6578\u4f54\u7528 O(1) \u7a7a\u9593\n for (int i = 0; i &lt; n; i++) {\n int c = 0;\n }\n // \u8ff4\u5708\u4e2d\u7684\u51fd\u5f0f\u4f54\u7528 O(1) \u7a7a\u9593\n for (int i = 0; i &lt; n; i++) {\n Function();\n }\n}\n</code></pre> space_complexity.go<pre><code>/* \u51fd\u5f0f */\nfunc function() int {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n}\n\n/* \u5e38\u6578\u968e */\nfunc spaceConstant(n int) {\n // \u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u4f54\u7528 O(1) \u7a7a\u9593\n const a = 0\n b := 0\n nums := make([]int, 10000)\n node := newNode(0)\n // \u8ff4\u5708\u4e2d\u7684\u8b8a\u6578\u4f54\u7528 O(1) \u7a7a\u9593\n var c int\n for i := 0; i &lt; n; i++ {\n c = 0\n }\n // \u8ff4\u5708\u4e2d\u7684\u51fd\u5f0f\u4f54\u7528 O(1) \u7a7a\u9593\n for i := 0; i &lt; n; i++ {\n function()\n }\n b += 0\n c += 0\n nums[0] = 0\n node.val = 0\n}\n</code></pre> space_complexity.swift<pre><code>/* \u51fd\u5f0f */\n@discardableResult\nfunc function() -&gt; Int {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n\n/* \u5e38\u6578\u968e */\nfunc constant(n: Int) {\n // \u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u4f54\u7528 O(1) \u7a7a\u9593\n let a = 0\n var b = 0\n let nums = Array(repeating: 0, count: 10000)\n let node = ListNode(x: 0)\n // \u8ff4\u5708\u4e2d\u7684\u8b8a\u6578\u4f54\u7528 O(1) \u7a7a\u9593\n for _ in 0 ..&lt; n {\n let c = 0\n }\n // \u8ff4\u5708\u4e2d\u7684\u51fd\u5f0f\u4f54\u7528 O(1) \u7a7a\u9593\n for _ in 0 ..&lt; n {\n function()\n }\n}\n</code></pre> space_complexity.js<pre><code>/* \u51fd\u5f0f */\nfunction constFunc() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6578\u968e */\nfunction constant(n) {\n // \u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u4f54\u7528 O(1) \u7a7a\u9593\n const a = 0;\n const b = 0;\n const nums = new Array(10000);\n const node = new ListNode(0);\n // \u8ff4\u5708\u4e2d\u7684\u8b8a\u6578\u4f54\u7528 O(1) \u7a7a\u9593\n for (let i = 0; i &lt; n; i++) {\n const c = 0;\n }\n // \u8ff4\u5708\u4e2d\u7684\u51fd\u5f0f\u4f54\u7528 O(1) \u7a7a\u9593\n for (let i = 0; i &lt; n; i++) {\n constFunc();\n }\n}\n</code></pre> space_complexity.ts<pre><code>/* \u51fd\u5f0f */\nfunction constFunc(): number {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6578\u968e */\nfunction constant(n: number): void {\n // \u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u4f54\u7528 O(1) \u7a7a\u9593\n const a = 0;\n const b = 0;\n const nums = new Array(10000);\n const node = new ListNode(0);\n // \u8ff4\u5708\u4e2d\u7684\u8b8a\u6578\u4f54\u7528 O(1) \u7a7a\u9593\n for (let i = 0; i &lt; n; i++) {\n const c = 0;\n }\n // \u8ff4\u5708\u4e2d\u7684\u51fd\u5f0f\u4f54\u7528 O(1) \u7a7a\u9593\n for (let i = 0; i &lt; n; i++) {\n constFunc();\n }\n}\n</code></pre> space_complexity.dart<pre><code>/* \u51fd\u5f0f */\nint function() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6578\u968e */\nvoid constant(int n) {\n // \u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u4f54\u7528 O(1) \u7a7a\u9593\n final int a = 0;\n int b = 0;\n List&lt;int&gt; nums = List.filled(10000, 0);\n ListNode node = ListNode(0);\n // \u8ff4\u5708\u4e2d\u7684\u8b8a\u6578\u4f54\u7528 O(1) \u7a7a\u9593\n for (var i = 0; i &lt; n; i++) {\n int c = 0;\n }\n // \u8ff4\u5708\u4e2d\u7684\u51fd\u5f0f\u4f54\u7528 O(1) \u7a7a\u9593\n for (var i = 0; i &lt; n; i++) {\n function();\n }\n}\n</code></pre> space_complexity.rs<pre><code>/* \u51fd\u5f0f */\nfn function() -&gt; i32 {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6578\u968e */\n#[allow(unused)]\nfn constant(n: i32) {\n // \u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u4f54\u7528 O(1) \u7a7a\u9593\n const A: i32 = 0;\n let b = 0;\n let nums = vec![0; 10000];\n let node = ListNode::new(0);\n // \u8ff4\u5708\u4e2d\u7684\u8b8a\u6578\u4f54\u7528 O(1) \u7a7a\u9593\n for i in 0..n {\n let c = 0;\n }\n // \u8ff4\u5708\u4e2d\u7684\u51fd\u5f0f\u4f54\u7528 O(1) \u7a7a\u9593\n for i in 0..n {\n function();\n }\n}\n</code></pre> space_complexity.c<pre><code>/* \u51fd\u5f0f */\nint func() {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6578\u968e */\nvoid constant(int n) {\n // \u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u4f54\u7528 O(1) \u7a7a\u9593\n const int a = 0;\n int b = 0;\n int nums[1000];\n ListNode *node = newListNode(0);\n free(node);\n // \u8ff4\u5708\u4e2d\u7684\u8b8a\u6578\u4f54\u7528 O(1) \u7a7a\u9593\n for (int i = 0; i &lt; n; i++) {\n int c = 0;\n }\n // \u8ff4\u5708\u4e2d\u7684\u51fd\u5f0f\u4f54\u7528 O(1) \u7a7a\u9593\n for (int i = 0; i &lt; n; i++) {\n func();\n }\n}\n</code></pre> space_complexity.kt<pre><code>/* \u51fd\u5f0f */\nfun function(): Int {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n\n/* \u5e38\u6578\u968e */\nfun constant(n: Int) {\n // \u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u4f54\u7528 O(1) \u7a7a\u9593\n val a = 0\n var b = 0\n val nums = Array(10000) { 0 }\n val node = ListNode(0)\n // \u8ff4\u5708\u4e2d\u7684\u8b8a\u6578\u4f54\u7528 O(1) \u7a7a\u9593\n for (i in 0..&lt;n) {\n val c = 0\n }\n // \u8ff4\u5708\u4e2d\u7684\u51fd\u5f0f\u4f54\u7528 O(1) \u7a7a\u9593\n for (i in 0..&lt;n) {\n function()\n }\n}\n</code></pre> space_complexity.rb<pre><code>### \u51fd\u5f0f ###\ndef function\n # \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n 0\nend\n\n### \u5e38\u6578\u968e ###\ndef constant(n)\n # \u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u4f54\u7528 O(1) \u7a7a\u9593\n a = 0\n nums = [0] * 10000\n node = ListNode.new\n\n # \u8ff4\u5708\u4e2d\u7684\u8b8a\u6578\u4f54\u7528 O(1) \u7a7a\u9593\n (0...n).each { c = 0 }\n # \u8ff4\u5708\u4e2d\u7684\u51fd\u5f0f\u4f54\u7528 O(1) \u7a7a\u9593\n (0...n).each { function }\nend\n</code></pre> space_complexity.zig<pre><code>// \u51fd\u5f0f\nfn function() i32 {\n // \u57f7\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n// \u5e38\u6578\u968e\nfn constant(n: i32) void {\n // \u5e38\u6578\u3001\u8b8a\u6578\u3001\u7269\u4ef6\u4f54\u7528 O(1) \u7a7a\u9593\n const a: i32 = 0;\n var b: i32 = 0;\n var nums = [_]i32{0}**10000;\n var node = inc.ListNode(i32){.val = 0};\n var i: i32 = 0;\n // \u8ff4\u5708\u4e2d\u7684\u8b8a\u6578\u4f54\u7528 O(1) \u7a7a\u9593\n while (i &lt; n) : (i += 1) {\n var c: i32 = 0;\n _ = c;\n }\n // \u8ff4\u5708\u4e2d\u7684\u51fd\u5f0f\u4f54\u7528 O(1) \u7a7a\u9593\n i = 0;\n while (i &lt; n) : (i += 1) {\n _ = function();\n }\n _ = a;\n _ = b;\n _ = nums;\n _ = node;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_computational_complexity/space_complexity/#2-on","title":"2. \u00a0 \u7dda\u6027\u968e \\(O(n)\\)","text":"<p>\u7dda\u6027\u968e\u5e38\u898b\u65bc\u5143\u7d20\u6578\u91cf\u8207 \\(n\\) \u6210\u6b63\u6bd4\u7684\u9663\u5217\u3001\u93c8\u7d50\u4e32\u5217\u3001\u5806\u758a\u3001\u4f47\u5217\u7b49\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.py<pre><code>def linear(n: int):\n \"\"\"\u7dda\u6027\u968e\"\"\"\n # \u9577\u5ea6\u70ba n \u7684\u4e32\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n nums = [0] * n\n # \u9577\u5ea6\u70ba n \u7684\u96dc\u6e4a\u8868\u4f54\u7528 O(n) \u7a7a\u9593\n hmap = dict[int, str]()\n for i in range(n):\n hmap[i] = str(i)\n</code></pre> space_complexity.cpp<pre><code>/* \u7dda\u6027\u968e */\nvoid linear(int n) {\n // \u9577\u5ea6\u70ba n \u7684\u9663\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n vector&lt;int&gt; nums(n);\n // \u9577\u5ea6\u70ba n \u7684\u4e32\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n vector&lt;ListNode&gt; nodes;\n for (int i = 0; i &lt; n; i++) {\n nodes.push_back(ListNode(i));\n }\n // \u9577\u5ea6\u70ba n \u7684\u96dc\u6e4a\u8868\u4f54\u7528 O(n) \u7a7a\u9593\n unordered_map&lt;int, string&gt; map;\n for (int i = 0; i &lt; n; i++) {\n map[i] = to_string(i);\n }\n}\n</code></pre> space_complexity.java<pre><code>/* \u7dda\u6027\u968e */\nvoid linear(int n) {\n // \u9577\u5ea6\u70ba n \u7684\u9663\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n int[] nums = new int[n];\n // \u9577\u5ea6\u70ba n \u7684\u4e32\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n List&lt;ListNode&gt; nodes = new ArrayList&lt;&gt;();\n for (int i = 0; i &lt; n; i++) {\n nodes.add(new ListNode(i));\n }\n // \u9577\u5ea6\u70ba n \u7684\u96dc\u6e4a\u8868\u4f54\u7528 O(n) \u7a7a\u9593\n Map&lt;Integer, String&gt; map = new HashMap&lt;&gt;();\n for (int i = 0; i &lt; n; i++) {\n map.put(i, String.valueOf(i));\n }\n}\n</code></pre> space_complexity.cs<pre><code>/* \u7dda\u6027\u968e */\nvoid Linear(int n) {\n // \u9577\u5ea6\u70ba n \u7684\u9663\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n int[] nums = new int[n];\n // \u9577\u5ea6\u70ba n \u7684\u4e32\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n List&lt;ListNode&gt; nodes = [];\n for (int i = 0; i &lt; n; i++) {\n nodes.Add(new ListNode(i));\n }\n // \u9577\u5ea6\u70ba n \u7684\u96dc\u6e4a\u8868\u4f54\u7528 O(n) \u7a7a\u9593\n Dictionary&lt;int, string&gt; map = [];\n for (int i = 0; i &lt; n; i++) {\n map.Add(i, i.ToString());\n }\n}\n</code></pre> space_complexity.go<pre><code>/* \u7dda\u6027\u968e */\nfunc spaceLinear(n int) {\n // \u9577\u5ea6\u70ba n \u7684\u9663\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n _ = make([]int, n)\n // \u9577\u5ea6\u70ba n \u7684\u4e32\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n var nodes []*node\n for i := 0; i &lt; n; i++ {\n nodes = append(nodes, newNode(i))\n }\n // \u9577\u5ea6\u70ba n \u7684\u96dc\u6e4a\u8868\u4f54\u7528 O(n) \u7a7a\u9593\n m := make(map[int]string, n)\n for i := 0; i &lt; n; i++ {\n m[i] = strconv.Itoa(i)\n }\n}\n</code></pre> space_complexity.swift<pre><code>/* \u7dda\u6027\u968e */\nfunc linear(n: Int) {\n // \u9577\u5ea6\u70ba n \u7684\u9663\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n let nums = Array(repeating: 0, count: n)\n // \u9577\u5ea6\u70ba n \u7684\u4e32\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n let nodes = (0 ..&lt; n).map { ListNode(x: $0) }\n // \u9577\u5ea6\u70ba n \u7684\u96dc\u6e4a\u8868\u4f54\u7528 O(n) \u7a7a\u9593\n let map = Dictionary(uniqueKeysWithValues: (0 ..&lt; n).map { ($0, \"\\($0)\") })\n}\n</code></pre> space_complexity.js<pre><code>/* \u7dda\u6027\u968e */\nfunction linear(n) {\n // \u9577\u5ea6\u70ba n \u7684\u9663\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n const nums = new Array(n);\n // \u9577\u5ea6\u70ba n \u7684\u4e32\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n const nodes = [];\n for (let i = 0; i &lt; n; i++) {\n nodes.push(new ListNode(i));\n }\n // \u9577\u5ea6\u70ba n \u7684\u96dc\u6e4a\u8868\u4f54\u7528 O(n) \u7a7a\u9593\n const map = new Map();\n for (let i = 0; i &lt; n; i++) {\n map.set(i, i.toString());\n }\n}\n</code></pre> space_complexity.ts<pre><code>/* \u7dda\u6027\u968e */\nfunction linear(n: number): void {\n // \u9577\u5ea6\u70ba n \u7684\u9663\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n const nums = new Array(n);\n // \u9577\u5ea6\u70ba n \u7684\u4e32\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n const nodes: ListNode[] = [];\n for (let i = 0; i &lt; n; i++) {\n nodes.push(new ListNode(i));\n }\n // \u9577\u5ea6\u70ba n \u7684\u96dc\u6e4a\u8868\u4f54\u7528 O(n) \u7a7a\u9593\n const map = new Map();\n for (let i = 0; i &lt; n; i++) {\n map.set(i, i.toString());\n }\n}\n</code></pre> space_complexity.dart<pre><code>/* \u7dda\u6027\u968e */\nvoid linear(int n) {\n // \u9577\u5ea6\u70ba n \u7684\u9663\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n List&lt;int&gt; nums = List.filled(n, 0);\n // \u9577\u5ea6\u70ba n \u7684\u4e32\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n List&lt;ListNode&gt; nodes = [];\n for (var i = 0; i &lt; n; i++) {\n nodes.add(ListNode(i));\n }\n // \u9577\u5ea6\u70ba n \u7684\u96dc\u6e4a\u8868\u4f54\u7528 O(n) \u7a7a\u9593\n Map&lt;int, String&gt; map = HashMap();\n for (var i = 0; i &lt; n; i++) {\n map.putIfAbsent(i, () =&gt; i.toString());\n }\n}\n</code></pre> space_complexity.rs<pre><code>/* \u7dda\u6027\u968e */\n#[allow(unused)]\nfn linear(n: i32) {\n // \u9577\u5ea6\u70ba n \u7684\u9663\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n let mut nums = vec![0; n as usize];\n // \u9577\u5ea6\u70ba n \u7684\u4e32\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n let mut nodes = Vec::new();\n for i in 0..n {\n nodes.push(ListNode::new(i))\n }\n // \u9577\u5ea6\u70ba n \u7684\u96dc\u6e4a\u8868\u4f54\u7528 O(n) \u7a7a\u9593\n let mut map = HashMap::new();\n for i in 0..n {\n map.insert(i, i.to_string());\n }\n}\n</code></pre> space_complexity.c<pre><code>/* \u96dc\u6e4a\u8868 */\ntypedef struct {\n int key;\n int val;\n UT_hash_handle hh; // \u57fa\u65bc uthash.h \u5be6\u73fe\n} HashTable;\n\n/* \u7dda\u6027\u968e */\nvoid linear(int n) {\n // \u9577\u5ea6\u70ba n \u7684\u9663\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n int *nums = malloc(sizeof(int) * n);\n free(nums);\n\n // \u9577\u5ea6\u70ba n \u7684\u4e32\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n ListNode **nodes = malloc(sizeof(ListNode *) * n);\n for (int i = 0; i &lt; n; i++) {\n nodes[i] = newListNode(i);\n }\n // \u8a18\u61b6\u9ad4\u91cb\u653e\n for (int i = 0; i &lt; n; i++) {\n free(nodes[i]);\n }\n free(nodes);\n\n // \u9577\u5ea6\u70ba n \u7684\u96dc\u6e4a\u8868\u4f54\u7528 O(n) \u7a7a\u9593\n HashTable *h = NULL;\n for (int i = 0; i &lt; n; i++) {\n HashTable *tmp = malloc(sizeof(HashTable));\n tmp-&gt;key = i;\n tmp-&gt;val = i;\n HASH_ADD_INT(h, key, tmp);\n }\n\n // \u8a18\u61b6\u9ad4\u91cb\u653e\n HashTable *curr, *tmp;\n HASH_ITER(hh, h, curr, tmp) {\n HASH_DEL(h, curr);\n free(curr);\n }\n}\n</code></pre> space_complexity.kt<pre><code>/* \u7dda\u6027\u968e */\nfun linear(n: Int) {\n // \u9577\u5ea6\u70ba n \u7684\u9663\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n val nums = Array(n) { 0 }\n // \u9577\u5ea6\u70ba n \u7684\u4e32\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n val nodes = mutableListOf&lt;ListNode&gt;()\n for (i in 0..&lt;n) {\n nodes.add(ListNode(i))\n }\n // \u9577\u5ea6\u70ba n \u7684\u96dc\u6e4a\u8868\u4f54\u7528 O(n) \u7a7a\u9593\n val map = mutableMapOf&lt;Int, String&gt;()\n for (i in 0..&lt;n) {\n map[i] = i.toString()\n }\n}\n</code></pre> space_complexity.rb<pre><code>### \u7dda\u6027\u968e ###\ndef linear(n)\n # \u9577\u5ea6\u70ba n \u7684\u4e32\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n nums = Array.new(n, 0)\n\n # \u9577\u5ea6\u70ba n \u7684\u96dc\u6e4a\u8868\u4f54\u7528 O(n) \u7a7a\u9593\n hmap = {}\n for i in 0...n\n hmap[i] = i.to_s\n end\nend\n</code></pre> space_complexity.zig<pre><code>// \u7dda\u6027\u968e\nfn linear(comptime n: i32) !void {\n // \u9577\u5ea6\u70ba n \u7684\u9663\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n var nums = [_]i32{0}**n;\n // \u9577\u5ea6\u70ba n \u7684\u4e32\u5217\u4f54\u7528 O(n) \u7a7a\u9593\n var nodes = std.ArrayList(i32).init(std.heap.page_allocator);\n defer nodes.deinit();\n var i: i32 = 0;\n while (i &lt; n) : (i += 1) {\n try nodes.append(i);\n }\n // \u9577\u5ea6\u70ba n \u7684\u96dc\u6e4a\u8868\u4f54\u7528 O(n) \u7a7a\u9593\n var map = std.AutoArrayHashMap(i32, []const u8).init(std.heap.page_allocator);\n defer map.deinit();\n var j: i32 = 0;\n while (j &lt; n) : (j += 1) {\n const string = try std.fmt.allocPrint(std.heap.page_allocator, \"{d}\", .{j});\n defer std.heap.page_allocator.free(string);\n try map.put(i, string);\n }\n _ = nums;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5982\u5716 2-17 \u6240\u793a\uff0c\u6b64\u51fd\u5f0f\u7684\u905e\u8ff4\u6df1\u5ea6\u70ba \\(n\\) \uff0c\u5373\u540c\u6642\u5b58\u5728 \\(n\\) \u500b\u672a\u8fd4\u56de\u7684 <code>linear_recur()</code> \u51fd\u5f0f\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u5806\u758a\u5e40\u7a7a\u9593\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.py<pre><code>def linear_recur(n: int):\n \"\"\"\u7dda\u6027\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09\"\"\"\n print(\"\u905e\u8ff4 n =\", n)\n if n == 1:\n return\n linear_recur(n - 1)\n</code></pre> space_complexity.cpp<pre><code>/* \u7dda\u6027\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nvoid linearRecur(int n) {\n cout &lt;&lt; \"\u905e\u8ff4 n = \" &lt;&lt; n &lt;&lt; endl;\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n</code></pre> space_complexity.java<pre><code>/* \u7dda\u6027\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nvoid linearRecur(int n) {\n System.out.println(\"\u905e\u8ff4 n = \" + n);\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n</code></pre> space_complexity.cs<pre><code>/* \u7dda\u6027\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nvoid LinearRecur(int n) {\n Console.WriteLine(\"\u905e\u8ff4 n = \" + n);\n if (n == 1) return;\n LinearRecur(n - 1);\n}\n</code></pre> space_complexity.go<pre><code>/* \u7dda\u6027\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunc spaceLinearRecur(n int) {\n fmt.Println(\"\u905e\u8ff4 n =\", n)\n if n == 1 {\n return\n }\n spaceLinearRecur(n - 1)\n}\n</code></pre> space_complexity.swift<pre><code>/* \u7dda\u6027\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunc linearRecur(n: Int) {\n print(\"\u905e\u8ff4 n = \\(n)\")\n if n == 1 {\n return\n }\n linearRecur(n: n - 1)\n}\n</code></pre> space_complexity.js<pre><code>/* \u7dda\u6027\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunction linearRecur(n) {\n console.log(`\u905e\u8ff4 n = ${n}`);\n if (n === 1) return;\n linearRecur(n - 1);\n}\n</code></pre> space_complexity.ts<pre><code>/* \u7dda\u6027\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunction linearRecur(n: number): void {\n console.log(`\u905e\u8ff4 n = ${n}`);\n if (n === 1) return;\n linearRecur(n - 1);\n}\n</code></pre> space_complexity.dart<pre><code>/* \u7dda\u6027\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nvoid linearRecur(int n) {\n print('\u905e\u8ff4 n = $n');\n if (n == 1) return;\n linearRecur(n - 1);\n}\n</code></pre> space_complexity.rs<pre><code>/* \u7dda\u6027\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfn linear_recur(n: i32) {\n println!(\"\u905e\u8ff4 n = {}\", n);\n if n == 1 {\n return;\n };\n linear_recur(n - 1);\n}\n</code></pre> space_complexity.c<pre><code>/* \u7dda\u6027\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nvoid linearRecur(int n) {\n printf(\"\u905e\u8ff4 n = %d\\r\\n\", n);\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n</code></pre> space_complexity.kt<pre><code>/* \u7dda\u6027\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfun linearRecur(n: Int) {\n println(\"\u905e\u8ff4 n = $n\")\n if (n == 1)\n return\n linearRecur(n - 1)\n}\n</code></pre> space_complexity.rb<pre><code>### \u7dda\u6027\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09###\ndef linear_recur(n)\n puts \"\u905e\u8ff4 n = #{n}\"\n return if n == 1\n linear_recur(n - 1)\nend\n</code></pre> space_complexity.zig<pre><code>// \u7dda\u6027\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09\nfn linearRecur(comptime n: i32) void {\n std.debug.print(\"\u905e\u8ff4 n = {}\\n\", .{n});\n if (n == 1) return;\n linearRecur(n - 1);\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p></p> <p> \u5716 2-17 \u00a0 \u905e\u8ff4\u51fd\u5f0f\u7522\u751f\u7684\u7dda\u6027\u968e\u7a7a\u9593\u8907\u96dc\u5ea6 </p>"},{"location":"chapter_computational_complexity/space_complexity/#3-on2","title":"3. \u00a0 \u5e73\u65b9\u968e \\(O(n^2)\\)","text":"<p>\u5e73\u65b9\u968e\u5e38\u898b\u65bc\u77e9\u9663\u548c\u5716\uff0c\u5143\u7d20\u6578\u91cf\u8207 \\(n\\) \u6210\u5e73\u65b9\u95dc\u4fc2\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.py<pre><code>def quadratic(n: int):\n \"\"\"\u5e73\u65b9\u968e\"\"\"\n # \u4e8c\u7dad\u4e32\u5217\u4f54\u7528 O(n^2) \u7a7a\u9593\n num_matrix = [[0] * n for _ in range(n)]\n</code></pre> space_complexity.cpp<pre><code>/* \u5e73\u65b9\u968e */\nvoid quadratic(int n) {\n // \u4e8c\u7dad\u4e32\u5217\u4f54\u7528 O(n^2) \u7a7a\u9593\n vector&lt;vector&lt;int&gt;&gt; numMatrix;\n for (int i = 0; i &lt; n; i++) {\n vector&lt;int&gt; tmp;\n for (int j = 0; j &lt; n; j++) {\n tmp.push_back(0);\n }\n numMatrix.push_back(tmp);\n }\n}\n</code></pre> space_complexity.java<pre><code>/* \u5e73\u65b9\u968e */\nvoid quadratic(int n) {\n // \u77e9\u9663\u4f54\u7528 O(n^2) \u7a7a\u9593\n int[][] numMatrix = new int[n][n];\n // \u4e8c\u7dad\u4e32\u5217\u4f54\u7528 O(n^2) \u7a7a\u9593\n List&lt;List&lt;Integer&gt;&gt; numList = new ArrayList&lt;&gt;();\n for (int i = 0; i &lt; n; i++) {\n List&lt;Integer&gt; tmp = new ArrayList&lt;&gt;();\n for (int j = 0; j &lt; n; j++) {\n tmp.add(0);\n }\n numList.add(tmp);\n }\n}\n</code></pre> space_complexity.cs<pre><code>/* \u5e73\u65b9\u968e */\nvoid Quadratic(int n) {\n // \u77e9\u9663\u4f54\u7528 O(n^2) \u7a7a\u9593\n int[,] numMatrix = new int[n, n];\n // \u4e8c\u7dad\u4e32\u5217\u4f54\u7528 O(n^2) \u7a7a\u9593\n List&lt;List&lt;int&gt;&gt; numList = [];\n for (int i = 0; i &lt; n; i++) {\n List&lt;int&gt; tmp = [];\n for (int j = 0; j &lt; n; j++) {\n tmp.Add(0);\n }\n numList.Add(tmp);\n }\n}\n</code></pre> space_complexity.go<pre><code>/* \u5e73\u65b9\u968e */\nfunc spaceQuadratic(n int) {\n // \u77e9\u9663\u4f54\u7528 O(n^2) \u7a7a\u9593\n numMatrix := make([][]int, n)\n for i := 0; i &lt; n; i++ {\n numMatrix[i] = make([]int, n)\n }\n}\n</code></pre> space_complexity.swift<pre><code>/* \u5e73\u65b9\u968e */\nfunc quadratic(n: Int) {\n // \u4e8c\u7dad\u4e32\u5217\u4f54\u7528 O(n^2) \u7a7a\u9593\n let numList = Array(repeating: Array(repeating: 0, count: n), count: n)\n}\n</code></pre> space_complexity.js<pre><code>/* \u5e73\u65b9\u968e */\nfunction quadratic(n) {\n // \u77e9\u9663\u4f54\u7528 O(n^2) \u7a7a\u9593\n const numMatrix = Array(n)\n .fill(null)\n .map(() =&gt; Array(n).fill(null));\n // \u4e8c\u7dad\u4e32\u5217\u4f54\u7528 O(n^2) \u7a7a\u9593\n const numList = [];\n for (let i = 0; i &lt; n; i++) {\n const tmp = [];\n for (let j = 0; j &lt; n; j++) {\n tmp.push(0);\n }\n numList.push(tmp);\n }\n}\n</code></pre> space_complexity.ts<pre><code>/* \u5e73\u65b9\u968e */\nfunction quadratic(n: number): void {\n // \u77e9\u9663\u4f54\u7528 O(n^2) \u7a7a\u9593\n const numMatrix = Array(n)\n .fill(null)\n .map(() =&gt; Array(n).fill(null));\n // \u4e8c\u7dad\u4e32\u5217\u4f54\u7528 O(n^2) \u7a7a\u9593\n const numList = [];\n for (let i = 0; i &lt; n; i++) {\n const tmp = [];\n for (let j = 0; j &lt; n; j++) {\n tmp.push(0);\n }\n numList.push(tmp);\n }\n}\n</code></pre> space_complexity.dart<pre><code>/* \u5e73\u65b9\u968e */\nvoid quadratic(int n) {\n // \u77e9\u9663\u4f54\u7528 O(n^2) \u7a7a\u9593\n List&lt;List&lt;int&gt;&gt; numMatrix = List.generate(n, (_) =&gt; List.filled(n, 0));\n // \u4e8c\u7dad\u4e32\u5217\u4f54\u7528 O(n^2) \u7a7a\u9593\n List&lt;List&lt;int&gt;&gt; numList = [];\n for (var i = 0; i &lt; n; i++) {\n List&lt;int&gt; tmp = [];\n for (int j = 0; j &lt; n; j++) {\n tmp.add(0);\n }\n numList.add(tmp);\n }\n}\n</code></pre> space_complexity.rs<pre><code>/* \u5e73\u65b9\u968e */\n#[allow(unused)]\nfn quadratic(n: i32) {\n // \u77e9\u9663\u4f54\u7528 O(n^2) \u7a7a\u9593\n let num_matrix = vec![vec![0; n as usize]; n as usize];\n // \u4e8c\u7dad\u4e32\u5217\u4f54\u7528 O(n^2) \u7a7a\u9593\n let mut num_list = Vec::new();\n for i in 0..n {\n let mut tmp = Vec::new();\n for j in 0..n {\n tmp.push(0);\n }\n num_list.push(tmp);\n }\n}\n</code></pre> space_complexity.c<pre><code>/* \u5e73\u65b9\u968e */\nvoid quadratic(int n) {\n // \u4e8c\u7dad\u4e32\u5217\u4f54\u7528 O(n^2) \u7a7a\u9593\n int **numMatrix = malloc(sizeof(int *) * n);\n for (int i = 0; i &lt; n; i++) {\n int *tmp = malloc(sizeof(int) * n);\n for (int j = 0; j &lt; n; j++) {\n tmp[j] = 0;\n }\n numMatrix[i] = tmp;\n }\n\n // \u8a18\u61b6\u9ad4\u91cb\u653e\n for (int i = 0; i &lt; n; i++) {\n free(numMatrix[i]);\n }\n free(numMatrix);\n}\n</code></pre> space_complexity.kt<pre><code>/* \u5e73\u65b9\u968e */\nfun quadratic(n: Int) {\n // \u77e9\u9663\u4f54\u7528 O(n^2) \u7a7a\u9593\n val numMatrix = arrayOfNulls&lt;Array&lt;Int&gt;?&gt;(n)\n // \u4e8c\u7dad\u4e32\u5217\u4f54\u7528 O(n^2) \u7a7a\u9593\n val numList = mutableListOf&lt;MutableList&lt;Int&gt;&gt;()\n for (i in 0..&lt;n) {\n val tmp = mutableListOf&lt;Int&gt;()\n for (j in 0..&lt;n) {\n tmp.add(0)\n }\n numList.add(tmp)\n }\n}\n</code></pre> space_complexity.rb<pre><code>### \u5e73\u65b9\u968e ###\ndef quadratic(n)\n # \u4e8c\u7dad\u4e32\u5217\u4f54\u7528 O(n^2) \u7a7a\u9593\n Array.new(n) { Array.new(n, 0) }\nend\n</code></pre> space_complexity.zig<pre><code>// \u5e73\u65b9\u968e\nfn quadratic(n: i32) !void {\n // \u4e8c\u7dad\u4e32\u5217\u4f54\u7528 O(n^2) \u7a7a\u9593\n var nodes = std.ArrayList(std.ArrayList(i32)).init(std.heap.page_allocator);\n defer nodes.deinit();\n var i: i32 = 0;\n while (i &lt; n) : (i += 1) {\n var tmp = std.ArrayList(i32).init(std.heap.page_allocator);\n defer tmp.deinit();\n var j: i32 = 0;\n while (j &lt; n) : (j += 1) {\n try tmp.append(0);\n }\n try nodes.append(tmp);\n }\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5982\u5716 2-18 \u6240\u793a\uff0c\u8a72\u51fd\u5f0f\u7684\u905e\u8ff4\u6df1\u5ea6\u70ba \\(n\\) \uff0c\u5728\u6bcf\u500b\u905e\u8ff4\u51fd\u5f0f\u4e2d\u90fd\u521d\u59cb\u5316\u4e86\u4e00\u500b\u9663\u5217\uff0c\u9577\u5ea6\u5206\u5225\u70ba \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u5e73\u5747\u9577\u5ea6\u70ba \\(n / 2\\) \uff0c\u56e0\u6b64\u7e3d\u9ad4\u4f54\u7528 \\(O(n^2)\\) \u7a7a\u9593\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.py<pre><code>def quadratic_recur(n: int) -&gt; int:\n \"\"\"\u5e73\u65b9\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09\"\"\"\n if n &lt;= 0:\n return 0\n # \u9663\u5217 nums \u9577\u5ea6\u70ba n, n-1, ..., 2, 1\n nums = [0] * n\n return quadratic_recur(n - 1)\n</code></pre> space_complexity.cpp<pre><code>/* \u5e73\u65b9\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint quadraticRecur(int n) {\n if (n &lt;= 0)\n return 0;\n vector&lt;int&gt; nums(n);\n cout &lt;&lt; \"\u905e\u8ff4 n = \" &lt;&lt; n &lt;&lt; \" \u4e2d\u7684 nums \u9577\u5ea6 = \" &lt;&lt; nums.size() &lt;&lt; endl;\n return quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.java<pre><code>/* \u5e73\u65b9\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint quadraticRecur(int n) {\n if (n &lt;= 0)\n return 0;\n // \u9663\u5217 nums \u9577\u5ea6\u70ba n, n-1, ..., 2, 1\n int[] nums = new int[n];\n System.out.println(\"\u905e\u8ff4 n = \" + n + \" \u4e2d\u7684 nums \u9577\u5ea6 = \" + nums.length);\n return quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.cs<pre><code>/* \u5e73\u65b9\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint QuadraticRecur(int n) {\n if (n &lt;= 0) return 0;\n int[] nums = new int[n];\n Console.WriteLine(\"\u905e\u8ff4 n = \" + n + \" \u4e2d\u7684 nums \u9577\u5ea6 = \" + nums.Length);\n return QuadraticRecur(n - 1);\n}\n</code></pre> space_complexity.go<pre><code>/* \u5e73\u65b9\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunc spaceQuadraticRecur(n int) int {\n if n &lt;= 0 {\n return 0\n }\n nums := make([]int, n)\n fmt.Printf(\"\u905e\u8ff4 n = %d \u4e2d\u7684 nums \u9577\u5ea6 = %d \\n\", n, len(nums))\n return spaceQuadraticRecur(n - 1)\n}\n</code></pre> space_complexity.swift<pre><code>/* \u5e73\u65b9\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\n@discardableResult\nfunc quadraticRecur(n: Int) -&gt; Int {\n if n &lt;= 0 {\n return 0\n }\n // \u9663\u5217 nums \u9577\u5ea6\u70ba n, n-1, ..., 2, 1\n let nums = Array(repeating: 0, count: n)\n print(\"\u905e\u8ff4 n = \\(n) \u4e2d\u7684 nums \u9577\u5ea6 = \\(nums.count)\")\n return quadraticRecur(n: n - 1)\n}\n</code></pre> space_complexity.js<pre><code>/* \u5e73\u65b9\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunction quadraticRecur(n) {\n if (n &lt;= 0) return 0;\n const nums = new Array(n);\n console.log(`\u905e\u8ff4 n = ${n} \u4e2d\u7684 nums \u9577\u5ea6 = ${nums.length}`);\n return quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.ts<pre><code>/* \u5e73\u65b9\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunction quadraticRecur(n: number): number {\n if (n &lt;= 0) return 0;\n const nums = new Array(n);\n console.log(`\u905e\u8ff4 n = ${n} \u4e2d\u7684 nums \u9577\u5ea6 = ${nums.length}`);\n return quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.dart<pre><code>/* \u5e73\u65b9\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint quadraticRecur(int n) {\n if (n &lt;= 0) return 0;\n List&lt;int&gt; nums = List.filled(n, 0);\n print('\u905e\u8ff4 n = $n \u4e2d\u7684 nums \u9577\u5ea6 = ${nums.length}');\n return quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.rs<pre><code>/* \u5e73\u65b9\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfn quadratic_recur(n: i32) -&gt; i32 {\n if n &lt;= 0 {\n return 0;\n };\n // \u9663\u5217 nums \u9577\u5ea6\u70ba n, n-1, ..., 2, 1\n let nums = vec![0; n as usize];\n println!(\"\u905e\u8ff4 n = {} \u4e2d\u7684 nums \u9577\u5ea6 = {}\", n, nums.len());\n return quadratic_recur(n - 1);\n}\n</code></pre> space_complexity.c<pre><code>/* \u5e73\u65b9\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint quadraticRecur(int n) {\n if (n &lt;= 0)\n return 0;\n int *nums = malloc(sizeof(int) * n);\n printf(\"\u905e\u8ff4 n = %d \u4e2d\u7684 nums \u9577\u5ea6 = %d\\r\\n\", n, n);\n int res = quadraticRecur(n - 1);\n free(nums);\n return res;\n}\n</code></pre> space_complexity.kt<pre><code>/* \u5e73\u65b9\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\ntailrec fun quadraticRecur(n: Int): Int {\n if (n &lt;= 0)\n return 0\n // \u9663\u5217 nums \u9577\u5ea6\u70ba n, n-1, ..., 2, 1\n val nums = Array(n) { 0 }\n println(\"\u905e\u8ff4 n = $n \u4e2d\u7684 nums \u9577\u5ea6 = ${nums.size}\")\n return quadraticRecur(n - 1)\n}\n</code></pre> space_complexity.rb<pre><code>### \u5e73\u65b9\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09###\ndef quadratic_recur(n)\n return 0 unless n &gt; 0\n\n # \u9663\u5217 nums \u9577\u5ea6\u70ba n, n-1, ..., 2, 1\n nums = Array.new(n, 0)\n quadratic_recur(n - 1)\nend\n</code></pre> space_complexity.zig<pre><code>// \u5e73\u65b9\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09\nfn quadraticRecur(comptime n: i32) i32 {\n if (n &lt;= 0) return 0;\n var nums = [_]i32{0}**n;\n std.debug.print(\"\u905e\u8ff4 n = {} \u4e2d\u7684 nums \u9577\u5ea6 = {}\\n\", .{n, nums.len});\n return quadraticRecur(n - 1);\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p></p> <p> \u5716 2-18 \u00a0 \u905e\u8ff4\u51fd\u5f0f\u7522\u751f\u7684\u5e73\u65b9\u968e\u7a7a\u9593\u8907\u96dc\u5ea6 </p>"},{"location":"chapter_computational_complexity/space_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6578\u968e \\(O(2^n)\\)","text":"<p>\u6307\u6578\u968e\u5e38\u898b\u65bc\u4e8c\u5143\u6a39\u3002\u89c0\u5bdf\u5716 2-19 \uff0c\u5c64\u6578\u70ba \\(n\\) \u7684\u201c\u6eff\u4e8c\u5143\u6a39\u201d\u7684\u7bc0\u9ede\u6578\u91cf\u70ba \\(2^n - 1\\) \uff0c\u4f54\u7528 \\(O(2^n)\\) \u7a7a\u9593\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.py<pre><code>def build_tree(n: int) -&gt; TreeNode | None:\n \"\"\"\u6307\u6578\u968e\uff08\u5efa\u7acb\u6eff\u4e8c\u5143\u6a39\uff09\"\"\"\n if n == 0:\n return None\n root = TreeNode(0)\n root.left = build_tree(n - 1)\n root.right = build_tree(n - 1)\n return root\n</code></pre> space_complexity.cpp<pre><code>/* \u6307\u6578\u968e\uff08\u5efa\u7acb\u6eff\u4e8c\u5143\u6a39\uff09 */\nTreeNode *buildTree(int n) {\n if (n == 0)\n return nullptr;\n TreeNode *root = new TreeNode(0);\n root-&gt;left = buildTree(n - 1);\n root-&gt;right = buildTree(n - 1);\n return root;\n}\n</code></pre> space_complexity.java<pre><code>/* \u6307\u6578\u968e\uff08\u5efa\u7acb\u6eff\u4e8c\u5143\u6a39\uff09 */\nTreeNode buildTree(int n) {\n if (n == 0)\n return null;\n TreeNode root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n</code></pre> space_complexity.cs<pre><code>/* \u6307\u6578\u968e\uff08\u5efa\u7acb\u6eff\u4e8c\u5143\u6a39\uff09 */\nTreeNode? BuildTree(int n) {\n if (n == 0) return null;\n TreeNode root = new(0) {\n left = BuildTree(n - 1),\n right = BuildTree(n - 1)\n };\n return root;\n}\n</code></pre> space_complexity.go<pre><code>/* \u6307\u6578\u968e\uff08\u5efa\u7acb\u6eff\u4e8c\u5143\u6a39\uff09 */\nfunc buildTree(n int) *TreeNode {\n if n == 0 {\n return nil\n }\n root := NewTreeNode(0)\n root.Left = buildTree(n - 1)\n root.Right = buildTree(n - 1)\n return root\n}\n</code></pre> space_complexity.swift<pre><code>/* \u6307\u6578\u968e\uff08\u5efa\u7acb\u6eff\u4e8c\u5143\u6a39\uff09 */\nfunc buildTree(n: Int) -&gt; TreeNode? {\n if n == 0 {\n return nil\n }\n let root = TreeNode(x: 0)\n root.left = buildTree(n: n - 1)\n root.right = buildTree(n: n - 1)\n return root\n}\n</code></pre> space_complexity.js<pre><code>/* \u6307\u6578\u968e\uff08\u5efa\u7acb\u6eff\u4e8c\u5143\u6a39\uff09 */\nfunction buildTree(n) {\n if (n === 0) return null;\n const root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n</code></pre> space_complexity.ts<pre><code>/* \u6307\u6578\u968e\uff08\u5efa\u7acb\u6eff\u4e8c\u5143\u6a39\uff09 */\nfunction buildTree(n: number): TreeNode | null {\n if (n === 0) return null;\n const root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n</code></pre> space_complexity.dart<pre><code>/* \u6307\u6578\u968e\uff08\u5efa\u7acb\u6eff\u4e8c\u5143\u6a39\uff09 */\nTreeNode? buildTree(int n) {\n if (n == 0) return null;\n TreeNode root = TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n</code></pre> space_complexity.rs<pre><code>/* \u6307\u6578\u968e\uff08\u5efa\u7acb\u6eff\u4e8c\u5143\u6a39\uff09 */\nfn build_tree(n: i32) -&gt; Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt; {\n if n == 0 {\n return None;\n };\n let root = TreeNode::new(0);\n root.borrow_mut().left = build_tree(n - 1);\n root.borrow_mut().right = build_tree(n - 1);\n return Some(root);\n}\n</code></pre> space_complexity.c<pre><code>/* \u6307\u6578\u968e\uff08\u5efa\u7acb\u6eff\u4e8c\u5143\u6a39\uff09 */\nTreeNode *buildTree(int n) {\n if (n == 0)\n return NULL;\n TreeNode *root = newTreeNode(0);\n root-&gt;left = buildTree(n - 1);\n root-&gt;right = buildTree(n - 1);\n return root;\n}\n</code></pre> space_complexity.kt<pre><code>/* \u6307\u6578\u968e\uff08\u5efa\u7acb\u6eff\u4e8c\u5143\u6a39\uff09 */\nfun buildTree(n: Int): TreeNode? {\n if (n == 0)\n return null\n val root = TreeNode(0)\n root.left = buildTree(n - 1)\n root.right = buildTree(n - 1)\n return root\n}\n</code></pre> space_complexity.rb<pre><code>### \u6307\u6578\u968e\uff08\u5efa\u7acb\u6eff\u4e8c\u5143\u6a39\uff09###\ndef build_tree(n)\n return if n == 0\n\n TreeNode.new.tap do |root|\n root.left = build_tree(n - 1)\n root.right = build_tree(n - 1)\n end\nend\n</code></pre> space_complexity.zig<pre><code>// \u6307\u6578\u968e\uff08\u5efa\u7acb\u6eff\u4e8c\u5143\u6a39\uff09\nfn buildTree(mem_allocator: std.mem.Allocator, n: i32) !?*inc.TreeNode(i32) {\n if (n == 0) return null;\n const root = try mem_allocator.create(inc.TreeNode(i32));\n root.init(0);\n root.left = try buildTree(mem_allocator, n - 1);\n root.right = try buildTree(mem_allocator, n - 1);\n return root;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p></p> <p> \u5716 2-19 \u00a0 \u6eff\u4e8c\u5143\u6a39\u7522\u751f\u7684\u6307\u6578\u968e\u7a7a\u9593\u8907\u96dc\u5ea6 </p>"},{"location":"chapter_computational_complexity/space_complexity/#5-olog-n","title":"5. \u00a0 \u5c0d\u6578\u968e \\(O(\\log n)\\)","text":"<p>\u5c0d\u6578\u968e\u5e38\u898b\u65bc\u5206\u6cbb\u6f14\u7b97\u6cd5\u3002\u4f8b\u5982\u5408\u4f75\u6392\u5e8f\uff0c\u8f38\u5165\u9577\u5ea6\u70ba \\(n\\) \u7684\u9663\u5217\uff0c\u6bcf\u8f2a\u905e\u8ff4\u5c07\u9663\u5217\u5f9e\u4e2d\u9ede\u8655\u5283\u5206\u70ba\u5169\u534a\uff0c\u5f62\u6210\u9ad8\u5ea6\u70ba \\(\\log n\\) \u7684\u905e\u8ff4\u6a39\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u5806\u758a\u5e40\u7a7a\u9593\u3002</p> <p>\u518d\u4f8b\u5982\u5c07\u6578\u5b57\u8f49\u5316\u70ba\u5b57\u4e32\uff0c\u8f38\u5165\u4e00\u500b\u6b63\u6574\u6578 \\(n\\) \uff0c\u5b83\u7684\u4f4d\u6578\u70ba \\(\\lfloor \\log_{10} n \\rfloor + 1\\) \uff0c\u5373\u5c0d\u61c9\u5b57\u4e32\u9577\u5ea6\u70ba \\(\\lfloor \\log_{10} n \\rfloor + 1\\) \uff0c\u56e0\u6b64\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(\\log_{10} n + 1) = O(\\log n)\\) \u3002</p>"},{"location":"chapter_computational_complexity/space_complexity/#244","title":"2.4.4 \u00a0 \u6b0a\u8861\u6642\u9593\u8207\u7a7a\u9593","text":"<p>\u7406\u60f3\u60c5\u6cc1\u4e0b\uff0c\u6211\u5011\u5e0c\u671b\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u548c\u7a7a\u9593\u8907\u96dc\u5ea6\u90fd\u80fd\u9054\u5230\u6700\u512a\u3002\u7136\u800c\u5728\u5be6\u969b\u60c5\u6cc1\u4e2d\uff0c\u540c\u6642\u6700\u4f73\u5316\u6642\u9593\u8907\u96dc\u5ea6\u548c\u7a7a\u9593\u8907\u96dc\u5ea6\u901a\u5e38\u975e\u5e38\u56f0\u96e3\u3002</p> <p>\u964d\u4f4e\u6642\u9593\u8907\u96dc\u5ea6\u901a\u5e38\u9700\u8981\u4ee5\u63d0\u5347\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba\u4ee3\u50f9\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u6211\u5011\u5c07\u72a7\u7272\u8a18\u61b6\u9ad4\u7a7a\u9593\u4f86\u63d0\u5347\u6f14\u7b97\u6cd5\u57f7\u884c\u901f\u5ea6\u7684\u601d\u8def\u7a31\u70ba\u201c\u4ee5\u7a7a\u9593\u63db\u6642\u9593\u201d\uff1b\u53cd\u4e4b\uff0c\u5247\u7a31\u70ba\u201c\u4ee5\u6642\u9593\u63db\u7a7a\u9593\u201d\u3002</p> <p>\u9078\u64c7\u54ea\u7a2e\u601d\u8def\u53d6\u6c7a\u65bc\u6211\u5011\u66f4\u770b\u91cd\u54ea\u500b\u65b9\u9762\u3002\u5728\u5927\u591a\u6578\u60c5\u6cc1\u4e0b\uff0c\u6642\u9593\u6bd4\u7a7a\u9593\u66f4\u5bf6\u8cb4\uff0c\u56e0\u6b64\u201c\u4ee5\u7a7a\u9593\u63db\u6642\u9593\u201d\u901a\u5e38\u662f\u66f4\u5e38\u7528\u7684\u7b56\u7565\u3002\u7576\u7136\uff0c\u5728\u8cc7\u6599\u91cf\u5f88\u5927\u7684\u60c5\u6cc1\u4e0b\uff0c\u63a7\u5236\u7a7a\u9593\u8907\u96dc\u5ea6\u4e5f\u975e\u5e38\u91cd\u8981\u3002</p>"},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 \u5c0f\u7d50","text":""},{"location":"chapter_computational_complexity/summary/#1","title":"1. \u00a0 \u91cd\u9ede\u56de\u9867","text":"<p>\u6f14\u7b97\u6cd5\u6548\u7387\u8a55\u4f30</p> <ul> <li>\u6642\u9593\u6548\u7387\u548c\u7a7a\u9593\u6548\u7387\u662f\u8861\u91cf\u6f14\u7b97\u6cd5\u512a\u52a3\u7684\u5169\u500b\u4e3b\u8981\u8a55\u50f9\u6307\u6a19\u3002</li> <li>\u6211\u5011\u53ef\u4ee5\u900f\u904e\u5be6\u969b\u6e2c\u8a66\u4f86\u8a55\u4f30\u6f14\u7b97\u6cd5\u6548\u7387\uff0c\u4f46\u96e3\u4ee5\u6d88\u9664\u6e2c\u8a66\u74b0\u5883\u7684\u5f71\u97ff\uff0c\u4e14\u6703\u8017\u8cbb\u5927\u91cf\u8a08\u7b97\u8cc7\u6e90\u3002</li> <li>\u8907\u96dc\u5ea6\u5206\u6790\u53ef\u4ee5\u6d88\u9664\u5be6\u969b\u6e2c\u8a66\u7684\u5f0a\u7aef\uff0c\u5206\u6790\u7d50\u679c\u9069\u7528\u65bc\u6240\u6709\u57f7\u884c\u5e73\u81fa\uff0c\u4e26\u4e14\u80fd\u5920\u63ed\u793a\u6f14\u7b97\u6cd5\u5728\u4e0d\u540c\u8cc7\u6599\u898f\u6a21\u4e0b\u7684\u6548\u7387\u3002</li> </ul> <p>\u6642\u9593\u8907\u96dc\u5ea6</p> <ul> <li>\u6642\u9593\u8907\u96dc\u5ea6\u7528\u65bc\u8861\u91cf\u6f14\u7b97\u6cd5\u57f7\u884c\u6642\u9593\u96a8\u8cc7\u6599\u91cf\u589e\u9577\u7684\u8da8\u52e2\uff0c\u53ef\u4ee5\u6709\u6548\u8a55\u4f30\u6f14\u7b97\u6cd5\u6548\u7387\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\u53ef\u80fd\u5931\u6548\uff0c\u5982\u5728\u8f38\u5165\u7684\u8cc7\u6599\u91cf\u8f03\u5c0f\u6216\u6642\u9593\u8907\u96dc\u5ea6\u76f8\u540c\u6642\uff0c\u7121\u6cd5\u7cbe\u78ba\u5c0d\u6bd4\u6f14\u7b97\u6cd5\u6548\u7387\u7684\u512a\u52a3\u3002</li> <li>\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6\u4f7f\u7528\u5927 \\(O\\) \u7b26\u865f\u8868\u793a\uff0c\u5c0d\u61c9\u51fd\u5f0f\u6f38\u8fd1\u4e0a\u754c\uff0c\u53cd\u6620\u7576 \\(n\\) \u8da8\u5411\u6b63\u7121\u7aae\u6642\uff0c\u64cd\u4f5c\u6578\u91cf \\(T(n)\\) \u7684\u589e\u9577\u7d1a\u5225\u3002</li> <li>\u63a8\u7b97\u6642\u9593\u8907\u96dc\u5ea6\u5206\u70ba\u5169\u6b65\uff0c\u9996\u5148\u7d71\u8a08\u64cd\u4f5c\u6578\u91cf\uff0c\u7136\u5f8c\u5224\u65b7\u6f38\u8fd1\u4e0a\u754c\u3002</li> <li>\u5e38\u898b\u6642\u9593\u8907\u96dc\u5ea6\u5f9e\u4f4e\u5230\u9ad8\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</li> <li>\u67d0\u4e9b\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u975e\u56fa\u5b9a\uff0c\u800c\u662f\u8207\u8f38\u5165\u8cc7\u6599\u7684\u5206\u4f48\u6709\u95dc\u3002\u6642\u9593\u8907\u96dc\u5ea6\u5206\u70ba\u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\uff0c\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6\u5e7e\u4e4e\u4e0d\u7528\uff0c\u56e0\u70ba\u8f38\u5165\u8cc7\u6599\u4e00\u822c\u9700\u8981\u6eff\u8db3\u56b4\u683c\u689d\u4ef6\u624d\u80fd\u9054\u5230\u6700\u4f73\u60c5\u6cc1\u3002</li> <li>\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u53cd\u6620\u6f14\u7b97\u6cd5\u5728\u96a8\u6a5f\u8cc7\u6599\u8f38\u5165\u4e0b\u7684\u57f7\u884c\u6548\u7387\uff0c\u6700\u63a5\u8fd1\u5be6\u969b\u61c9\u7528\u4e2d\u7684\u6f14\u7b97\u6cd5\u6548\u80fd\u3002\u8a08\u7b97\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u9700\u8981\u7d71\u8a08\u8f38\u5165\u8cc7\u6599\u5206\u4f48\u4ee5\u53ca\u7d9c\u5408\u5f8c\u7684\u6578\u5b78\u671f\u671b\u3002</li> </ul> <p>\u7a7a\u9593\u8907\u96dc\u5ea6</p> <ul> <li>\u7a7a\u9593\u8907\u96dc\u5ea6\u7684\u4f5c\u7528\u985e\u4f3c\u65bc\u6642\u9593\u8907\u96dc\u5ea6\uff0c\u7528\u65bc\u8861\u91cf\u6f14\u7b97\u6cd5\u4f54\u7528\u8a18\u61b6\u9ad4\u7a7a\u9593\u96a8\u8cc7\u6599\u91cf\u589e\u9577\u7684\u8da8\u52e2\u3002</li> <li>\u6f14\u7b97\u6cd5\u57f7\u884c\u904e\u7a0b\u4e2d\u7684\u76f8\u95dc\u8a18\u61b6\u9ad4\u7a7a\u9593\u53ef\u5206\u70ba\u8f38\u5165\u7a7a\u9593\u3001\u66ab\u5b58\u7a7a\u9593\u3001\u8f38\u51fa\u7a7a\u9593\u3002\u901a\u5e38\u60c5\u6cc1\u4e0b\uff0c\u8f38\u5165\u7a7a\u9593\u4e0d\u7d0d\u5165\u7a7a\u9593\u8907\u96dc\u5ea6\u8a08\u7b97\u3002\u66ab\u5b58\u7a7a\u9593\u53ef\u5206\u70ba\u66ab\u5b58\u8cc7\u6599\u3001\u5806\u758a\u5e40\u7a7a\u9593\u548c\u6307\u4ee4\u7a7a\u9593\uff0c\u5176\u4e2d\u5806\u758a\u5e40\u7a7a\u9593\u901a\u5e38\u50c5\u5728\u905e\u8ff4\u51fd\u5f0f\u4e2d\u5f71\u97ff\u7a7a\u9593\u8907\u96dc\u5ea6\u3002</li> <li>\u6211\u5011\u901a\u5e38\u53ea\u95dc\u6ce8\u6700\u5dee\u7a7a\u9593\u8907\u96dc\u5ea6\uff0c\u5373\u7d71\u8a08\u6f14\u7b97\u6cd5\u5728\u6700\u5dee\u8f38\u5165\u8cc7\u6599\u548c\u6700\u5dee\u57f7\u884c\u6642\u523b\u4e0b\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u3002</li> <li>\u5e38\u898b\u7a7a\u9593\u8907\u96dc\u5ea6\u5f9e\u4f4e\u5230\u9ad8\u6392\u5217\u6709 \\(O(1)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n^2)\\) \u548c \\(O(2^n)\\) \u7b49\u3002</li> </ul>"},{"location":"chapter_computational_complexity/summary/#2-q-a","title":"2. \u00a0 Q &amp; A","text":"<p>Q\uff1a\u5c3e\u905e\u8ff4\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u662f \\(O(1)\\) \u55ce\uff1f</p> <p>\u7406\u8ad6\u4e0a\uff0c\u5c3e\u905e\u8ff4\u51fd\u5f0f\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u53ef\u4ee5\u6700\u4f73\u5316\u81f3 \\(O(1)\\) \u3002\u4e0d\u904e\u7d55\u5927\u591a\u6578\u7a0b\u5f0f\u8a9e\u8a00\uff08\u4f8b\u5982 Java\u3001Python\u3001C++\u3001Go\u3001C# \u7b49\uff09\u4e0d\u652f\u6301\u81ea\u52d5\u6700\u4f73\u5316\u5c3e\u905e\u8ff4\uff0c\u56e0\u6b64\u901a\u5e38\u8a8d\u70ba\u7a7a\u9593\u8907\u96dc\u5ea6\u662f \\(O(n)\\) \u3002</p> <p>Q\uff1a\u51fd\u5f0f\u548c\u65b9\u6cd5\u9019\u5169\u500b\u8853\u8a9e\u7684\u5340\u5225\u662f\u4ec0\u9ebc\uff1f</p> <p>\u51fd\u5f0f\uff08function\uff09\u53ef\u4ee5\u88ab\u7368\u7acb\u57f7\u884c\uff0c\u6240\u6709\u53c3\u6578\u90fd\u4ee5\u986f\u5f0f\u50b3\u905e\u3002\u65b9\u6cd5\uff08method\uff09\u8207\u4e00\u500b\u7269\u4ef6\u95dc\u806f\uff0c\u88ab\u96b1\u5f0f\u50b3\u905e\u7d66\u547c\u53eb\u5b83\u7684\u7269\u4ef6\uff0c\u80fd\u5920\u5c0d\u985e\u5225\u7684\u4f8b\u9805\u4e2d\u5305\u542b\u7684\u8cc7\u6599\u9032\u884c\u64cd\u4f5c\u3002</p> <p>\u4e0b\u9762\u4ee5\u5e7e\u7a2e\u5e38\u898b\u7684\u7a0b\u5f0f\u8a9e\u8a00\u70ba\u4f8b\u4f86\u8aaa\u660e\u3002</p> <ul> <li>C \u8a9e\u8a00\u662f\u7a0b\u5e8f\u5f0f\u7a0b\u5f0f\u8a2d\u8a08\u8a9e\u8a00\uff0c\u6c92\u6709\u7269\u4ef6\u5c0e\u5411\u7684\u6982\u5ff5\uff0c\u6240\u4ee5\u53ea\u6709\u51fd\u5f0f\u3002\u4f46\u6211\u5011\u53ef\u4ee5\u900f\u904e\u5efa\u7acb\u7d50\u69cb\u9ad4\uff08struct\uff09\u4f86\u6a21\u64ec\u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08\uff0c\u8207\u7d50\u69cb\u9ad4\u76f8\u95dc\u806f\u7684\u51fd\u5f0f\u5c31\u76f8\u7576\u65bc\u5176\u4ed6\u7a0b\u5f0f\u8a9e\u8a00\u4e2d\u7684\u65b9\u6cd5\u3002</li> <li>Java \u548c C# \u662f\u7269\u4ef6\u5c0e\u5411\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u7a0b\u5f0f\u78bc\u584a\uff08\u65b9\u6cd5\uff09\u901a\u5e38\u4f5c\u70ba\u67d0\u500b\u985e\u5225\u7684\u4e00\u90e8\u5206\u3002\u975c\u614b\u65b9\u6cd5\u7684\u884c\u70ba\u985e\u4f3c\u65bc\u51fd\u5f0f\uff0c\u56e0\u70ba\u5b83\u88ab\u7e6b\u7d50\u5728\u985e\u5225\u4e0a\uff0c\u4e0d\u80fd\u8a2a\u554f\u7279\u5b9a\u7684\u4f8b\u9805\u8b8a\u6578\u3002</li> <li>C++ \u548c Python \u65e2\u652f\u6301\u7a0b\u5e8f\u5f0f\u7a0b\u5f0f\u8a2d\u8a08\uff08\u51fd\u5f0f\uff09\uff0c\u4e5f\u652f\u6301\u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08\uff08\u65b9\u6cd5\uff09\u3002</li> </ul> <p>Q\uff1a\u5716\u89e3\u201c\u5e38\u898b\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u578b\u5225\u201d\u53cd\u6620\u7684\u662f\u5426\u662f\u4f54\u7528\u7a7a\u9593\u7684\u7d55\u5c0d\u5927\u5c0f\uff1f</p> <p>\u4e0d\u662f\uff0c\u8a72\u5716\u5c55\u793a\u7684\u662f\u7a7a\u9593\u8907\u96dc\u5ea6\uff0c\u5176\u53cd\u6620\u7684\u662f\u589e\u9577\u8da8\u52e2\uff0c\u800c\u4e0d\u662f\u4f54\u7528\u7a7a\u9593\u7684\u7d55\u5c0d\u5927\u5c0f\u3002</p> <p>\u5047\u8a2d\u53d6 \\(n = 8\\) \uff0c\u4f60\u53ef\u80fd\u6703\u767c\u73fe\u6bcf\u689d\u66f2\u7dda\u7684\u503c\u8207\u51fd\u5f0f\u5c0d\u61c9\u4e0d\u4e0a\u3002\u9019\u662f\u56e0\u70ba\u6bcf\u689d\u66f2\u7dda\u90fd\u5305\u542b\u4e00\u500b\u5e38\u6578\u9805\uff0c\u7528\u65bc\u5c07\u53d6\u503c\u7bc4\u570d\u58d3\u7e2e\u5230\u4e00\u500b\u8996\u89ba\u8212\u9069\u7684\u7bc4\u570d\u5167\u3002</p> <p>\u5728\u5be6\u969b\u4e2d\uff0c\u56e0\u70ba\u6211\u5011\u901a\u5e38\u4e0d\u77e5\u9053\u6bcf\u500b\u65b9\u6cd5\u7684\u201c\u5e38\u6578\u9805\u201d\u8907\u96dc\u5ea6\u662f\u591a\u5c11\uff0c\u6240\u4ee5\u4e00\u822c\u7121\u6cd5\u50c5\u6191\u8907\u96dc\u5ea6\u4f86\u9078\u64c7 \\(n = 8\\) \u4e4b\u4e0b\u7684\u6700\u512a\u89e3\u6cd5\u3002\u4f46\u5c0d\u65bc \\(n = 8^5\\) \u5c31\u5f88\u597d\u9078\u4e86\uff0c\u9019\u6642\u589e\u9577\u8da8\u52e2\u5df2\u7d93\u4f54\u4e3b\u5c0e\u4e86\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/","title":"2.3 \u00a0 \u6642\u9593\u8907\u96dc\u5ea6","text":"<p>\u57f7\u884c\u6642\u9593\u53ef\u4ee5\u76f4\u89c0\u4e14\u6e96\u78ba\u5730\u53cd\u6620\u6f14\u7b97\u6cd5\u7684\u6548\u7387\u3002\u5982\u679c\u6211\u5011\u60f3\u6e96\u78ba\u9810\u4f30\u4e00\u6bb5\u7a0b\u5f0f\u78bc\u7684\u57f7\u884c\u6642\u9593\uff0c\u61c9\u8a72\u5982\u4f55\u64cd\u4f5c\u5462\uff1f</p> <ol> <li>\u78ba\u5b9a\u57f7\u884c\u5e73\u81fa\uff0c\u5305\u62ec\u786c\u9ad4\u914d\u7f6e\u3001\u7a0b\u5f0f\u8a9e\u8a00\u3001\u7cfb\u7d71\u74b0\u5883\u7b49\uff0c\u9019\u4e9b\u56e0\u7d20\u90fd\u6703\u5f71\u97ff\u7a0b\u5f0f\u78bc\u7684\u57f7\u884c\u6548\u7387\u3002</li> <li>\u8a55\u4f30\u5404\u7a2e\u8a08\u7b97\u64cd\u4f5c\u6240\u9700\u7684\u57f7\u884c\u6642\u9593\uff0c\u4f8b\u5982\u52a0\u6cd5\u64cd\u4f5c <code>+</code> \u9700\u8981 1 ns \uff0c\u4e58\u6cd5\u64cd\u4f5c <code>*</code> \u9700\u8981 10 ns \uff0c\u5217\u5370\u64cd\u4f5c <code>print()</code> \u9700\u8981 5 ns \u7b49\u3002</li> <li>\u7d71\u8a08\u7a0b\u5f0f\u78bc\u4e2d\u6240\u6709\u7684\u8a08\u7b97\u64cd\u4f5c\uff0c\u4e26\u5c07\u6240\u6709\u64cd\u4f5c\u7684\u57f7\u884c\u6642\u9593\u6c42\u548c\uff0c\u5f9e\u800c\u5f97\u5230\u57f7\u884c\u6642\u9593\u3002</li> </ol> <p>\u4f8b\u5982\u5728\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u8f38\u5165\u8cc7\u6599\u5927\u5c0f\u70ba \\(n\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code># \u5728\u67d0\u57f7\u884c\u5e73\u81fa\u4e0b\ndef algorithm(n: int):\n a = 2 # 1 ns\n a = a + 1 # 1 ns\n a = a * 2 # 10 ns\n # \u8ff4\u5708 n \u6b21\n for _ in range(n): # 1 ns\n print(0) # 5 ns\n</code></pre> <pre><code>// \u5728\u67d0\u57f7\u884c\u5e73\u81fa\u4e0b\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u8ff4\u5708 n \u6b21\n for (int i = 0; i &lt; n; i++) { // 1 ns \uff0c\u6bcf\u8f2a\u90fd\u8981\u57f7\u884c i++\n cout &lt;&lt; 0 &lt;&lt; endl; // 5 ns\n }\n}\n</code></pre> <pre><code>// \u5728\u67d0\u57f7\u884c\u5e73\u81fa\u4e0b\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u8ff4\u5708 n \u6b21\n for (int i = 0; i &lt; n; i++) { // 1 ns \uff0c\u6bcf\u8f2a\u90fd\u8981\u57f7\u884c i++\n System.out.println(0); // 5 ns\n }\n}\n</code></pre> <pre><code>// \u5728\u67d0\u57f7\u884c\u5e73\u81fa\u4e0b\nvoid Algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u8ff4\u5708 n \u6b21\n for (int i = 0; i &lt; n; i++) { // 1 ns \uff0c\u6bcf\u8f2a\u90fd\u8981\u57f7\u884c i++\n Console.WriteLine(0); // 5 ns\n }\n}\n</code></pre> <pre><code>// \u5728\u67d0\u57f7\u884c\u5e73\u81fa\u4e0b\nfunc algorithm(n int) {\n a := 2 // 1 ns\n a = a + 1 // 1 ns\n a = a * 2 // 10 ns\n // \u8ff4\u5708 n \u6b21\n for i := 0; i &lt; n; i++ { // 1 ns\n fmt.Println(a) // 5 ns\n }\n}\n</code></pre> <pre><code>// \u5728\u67d0\u57f7\u884c\u5e73\u81fa\u4e0b\nfunc algorithm(n: Int) {\n var a = 2 // 1 ns\n a = a + 1 // 1 ns\n a = a * 2 // 10 ns\n // \u8ff4\u5708 n \u6b21\n for _ in 0 ..&lt; n { // 1 ns\n print(0) // 5 ns\n }\n}\n</code></pre> <pre><code>// \u5728\u67d0\u57f7\u884c\u5e73\u81fa\u4e0b\nfunction algorithm(n) {\n var a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u8ff4\u5708 n \u6b21\n for(let i = 0; i &lt; n; i++) { // 1 ns \uff0c\u6bcf\u8f2a\u90fd\u8981\u57f7\u884c i++\n console.log(0); // 5 ns\n }\n}\n</code></pre> <pre><code>// \u5728\u67d0\u57f7\u884c\u5e73\u81fa\u4e0b\nfunction algorithm(n: number): void {\n var a: number = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u8ff4\u5708 n \u6b21\n for(let i = 0; i &lt; n; i++) { // 1 ns \uff0c\u6bcf\u8f2a\u90fd\u8981\u57f7\u884c i++\n console.log(0); // 5 ns\n }\n}\n</code></pre> <pre><code>// \u5728\u67d0\u57f7\u884c\u5e73\u81fa\u4e0b\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u8ff4\u5708 n \u6b21\n for (int i = 0; i &lt; n; i++) { // 1 ns \uff0c\u6bcf\u8f2a\u90fd\u8981\u57f7\u884c i++\n print(0); // 5 ns\n }\n}\n</code></pre> <pre><code>// \u5728\u67d0\u57f7\u884c\u5e73\u81fa\u4e0b\nfn algorithm(n: i32) {\n let mut a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u8ff4\u5708 n \u6b21\n for _ in 0..n { // 1 ns \uff0c\u6bcf\u8f2a\u90fd\u8981\u57f7\u884c i++\n println!(\"{}\", 0); // 5 ns\n }\n}\n</code></pre> <pre><code>// \u5728\u67d0\u57f7\u884c\u5e73\u81fa\u4e0b\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // \u8ff4\u5708 n \u6b21\n for (int i = 0; i &lt; n; i++) { // 1 ns \uff0c\u6bcf\u8f2a\u90fd\u8981\u57f7\u884c i++\n printf(\"%d\", 0); // 5 ns\n }\n}\n</code></pre> <pre><code>// \u5728\u67d0\u57f7\u884c\u5e73\u81fa\u4e0b\nfun algorithm(n: Int) {\n var a = 2 // 1 ns\n a = a + 1 // 1 ns\n a = a * 2 // 10 ns\n // \u8ff4\u5708 n \u6b21\n for (i in 0..&lt;n) { // 1 ns \uff0c\u6bcf\u8f2a\u90fd\u8981\u57f7\u884c i++\n println(0) // 5 ns\n }\n}\n</code></pre> <pre><code># \u5728\u67d0\u57f7\u884c\u5e73\u81fa\u4e0b\ndef algorithm(n)\n a = 2 # 1 ns\n a = a + 1 # 1 ns\n a = a * 2 # 10 ns\n # \u8ff4\u5708 n \u6b21\n (0...n).each do # 1 ns\n puts 0 # 5 ns\n end\nend\n</code></pre> <pre><code>// \u5728\u67d0\u57f7\u884c\u5e73\u81fa\u4e0b\nfn algorithm(n: usize) void {\n var a: i32 = 2; // 1 ns\n a += 1; // 1 ns\n a *= 2; // 10 ns\n // \u8ff4\u5708 n \u6b21\n for (0..n) |_| { // 1 ns\n std.debug.print(\"{}\\n\", .{0}); // 5 ns\n }\n}\n</code></pre> <p>\u6839\u64da\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f97\u5230\u6f14\u7b97\u6cd5\u7684\u57f7\u884c\u6642\u9593\u70ba \\((6n + 12)\\) ns \uff1a</p> \\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\] <p>\u4f46\u5be6\u969b\u4e0a\uff0c\u7d71\u8a08\u6f14\u7b97\u6cd5\u7684\u57f7\u884c\u6642\u9593\u65e2\u4e0d\u5408\u7406\u4e5f\u4e0d\u73fe\u5be6\u3002\u9996\u5148\uff0c\u6211\u5011\u4e0d\u5e0c\u671b\u5c07\u9810\u4f30\u6642\u9593\u548c\u57f7\u884c\u5e73\u81fa\u7e6b\u7d50\uff0c\u56e0\u70ba\u6f14\u7b97\u6cd5\u9700\u8981\u5728\u5404\u7a2e\u4e0d\u540c\u7684\u5e73\u81fa\u4e0a\u57f7\u884c\u3002\u5176\u6b21\uff0c\u6211\u5011\u5f88\u96e3\u7372\u77e5\u6bcf\u7a2e\u64cd\u4f5c\u7684\u57f7\u884c\u6642\u9593\uff0c\u9019\u7d66\u9810\u4f30\u904e\u7a0b\u5e36\u4f86\u4e86\u6975\u5927\u7684\u96e3\u5ea6\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#231","title":"2.3.1 \u00a0 \u7d71\u8a08\u6642\u9593\u589e\u9577\u8da8\u52e2","text":"<p>\u6642\u9593\u8907\u96dc\u5ea6\u5206\u6790\u7d71\u8a08\u7684\u4e0d\u662f\u6f14\u7b97\u6cd5\u57f7\u884c\u6642\u9593\uff0c\u800c\u662f\u6f14\u7b97\u6cd5\u57f7\u884c\u6642\u9593\u96a8\u8457\u8cc7\u6599\u91cf\u8b8a\u5927\u6642\u7684\u589e\u9577\u8da8\u52e2\u3002</p> <p>\u201c\u6642\u9593\u589e\u9577\u8da8\u52e2\u201d\u9019\u500b\u6982\u5ff5\u6bd4\u8f03\u62bd\u8c61\uff0c\u6211\u5011\u900f\u904e\u4e00\u500b\u4f8b\u5b50\u4f86\u52a0\u4ee5\u7406\u89e3\u3002\u5047\u8a2d\u8f38\u5165\u8cc7\u6599\u5927\u5c0f\u70ba \\(n\\) \uff0c\u7d66\u5b9a\u4e09\u500b\u6f14\u7b97\u6cd5 <code>A</code>\u3001<code>B</code> \u548c <code>C</code> \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code># \u6f14\u7b97\u6cd5 A \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\ndef algorithm_A(n: int):\n print(0)\n# \u6f14\u7b97\u6cd5 B \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u7dda\u6027\u968e\ndef algorithm_B(n: int):\n for _ in range(n):\n print(0)\n# \u6f14\u7b97\u6cd5 C \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\ndef algorithm_C(n: int):\n for _ in range(1000000):\n print(0)\n</code></pre> <pre><code>// \u6f14\u7b97\u6cd5 A \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nvoid algorithm_A(int n) {\n cout &lt;&lt; 0 &lt;&lt; endl;\n}\n// \u6f14\u7b97\u6cd5 B \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u7dda\u6027\u968e\nvoid algorithm_B(int n) {\n for (int i = 0; i &lt; n; i++) {\n cout &lt;&lt; 0 &lt;&lt; endl;\n }\n}\n// \u6f14\u7b97\u6cd5 C \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nvoid algorithm_C(int n) {\n for (int i = 0; i &lt; 1000000; i++) {\n cout &lt;&lt; 0 &lt;&lt; endl;\n }\n}\n</code></pre> <pre><code>// \u6f14\u7b97\u6cd5 A \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nvoid algorithm_A(int n) {\n System.out.println(0);\n}\n// \u6f14\u7b97\u6cd5 B \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u7dda\u6027\u968e\nvoid algorithm_B(int n) {\n for (int i = 0; i &lt; n; i++) {\n System.out.println(0);\n }\n}\n// \u6f14\u7b97\u6cd5 C \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nvoid algorithm_C(int n) {\n for (int i = 0; i &lt; 1000000; i++) {\n System.out.println(0);\n }\n}\n</code></pre> <pre><code>// \u6f14\u7b97\u6cd5 A \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nvoid AlgorithmA(int n) {\n Console.WriteLine(0);\n}\n// \u6f14\u7b97\u6cd5 B \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u7dda\u6027\u968e\nvoid AlgorithmB(int n) {\n for (int i = 0; i &lt; n; i++) {\n Console.WriteLine(0);\n }\n}\n// \u6f14\u7b97\u6cd5 C \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nvoid AlgorithmC(int n) {\n for (int i = 0; i &lt; 1000000; i++) {\n Console.WriteLine(0);\n }\n}\n</code></pre> <pre><code>// \u6f14\u7b97\u6cd5 A \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nfunc algorithm_A(n int) {\n fmt.Println(0)\n}\n// \u6f14\u7b97\u6cd5 B \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u7dda\u6027\u968e\nfunc algorithm_B(n int) {\n for i := 0; i &lt; n; i++ {\n fmt.Println(0)\n }\n}\n// \u6f14\u7b97\u6cd5 C \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nfunc algorithm_C(n int) {\n for i := 0; i &lt; 1000000; i++ {\n fmt.Println(0)\n }\n}\n</code></pre> <pre><code>// \u6f14\u7b97\u6cd5 A \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nfunc algorithmA(n: Int) {\n print(0)\n}\n\n// \u6f14\u7b97\u6cd5 B \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u7dda\u6027\u968e\nfunc algorithmB(n: Int) {\n for _ in 0 ..&lt; n {\n print(0)\n }\n}\n\n// \u6f14\u7b97\u6cd5 C \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nfunc algorithmC(n: Int) {\n for _ in 0 ..&lt; 1_000_000 {\n print(0)\n }\n}\n</code></pre> <pre><code>// \u6f14\u7b97\u6cd5 A \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nfunction algorithm_A(n) {\n console.log(0);\n}\n// \u6f14\u7b97\u6cd5 B \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u7dda\u6027\u968e\nfunction algorithm_B(n) {\n for (let i = 0; i &lt; n; i++) {\n console.log(0);\n }\n}\n// \u6f14\u7b97\u6cd5 C \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nfunction algorithm_C(n) {\n for (let i = 0; i &lt; 1000000; i++) {\n console.log(0);\n }\n}\n</code></pre> <pre><code>// \u6f14\u7b97\u6cd5 A \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nfunction algorithm_A(n: number): void {\n console.log(0);\n}\n// \u6f14\u7b97\u6cd5 B \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u7dda\u6027\u968e\nfunction algorithm_B(n: number): void {\n for (let i = 0; i &lt; n; i++) {\n console.log(0);\n }\n}\n// \u6f14\u7b97\u6cd5 C \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nfunction algorithm_C(n: number): void {\n for (let i = 0; i &lt; 1000000; i++) {\n console.log(0);\n }\n}\n</code></pre> <pre><code>// \u6f14\u7b97\u6cd5 A \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nvoid algorithmA(int n) {\n print(0);\n}\n// \u6f14\u7b97\u6cd5 B \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u7dda\u6027\u968e\nvoid algorithmB(int n) {\n for (int i = 0; i &lt; n; i++) {\n print(0);\n }\n}\n// \u6f14\u7b97\u6cd5 C \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nvoid algorithmC(int n) {\n for (int i = 0; i &lt; 1000000; i++) {\n print(0);\n }\n}\n</code></pre> <pre><code>// \u6f14\u7b97\u6cd5 A \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nfn algorithm_A(n: i32) {\n println!(\"{}\", 0);\n}\n// \u6f14\u7b97\u6cd5 B \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u7dda\u6027\u968e\nfn algorithm_B(n: i32) {\n for _ in 0..n {\n println!(\"{}\", 0);\n }\n}\n// \u6f14\u7b97\u6cd5 C \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nfn algorithm_C(n: i32) {\n for _ in 0..1000000 {\n println!(\"{}\", 0);\n }\n}\n</code></pre> <pre><code>// \u6f14\u7b97\u6cd5 A \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nvoid algorithm_A(int n) {\n printf(\"%d\", 0);\n}\n// \u6f14\u7b97\u6cd5 B \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u7dda\u6027\u968e\nvoid algorithm_B(int n) {\n for (int i = 0; i &lt; n; i++) {\n printf(\"%d\", 0);\n }\n}\n// \u6f14\u7b97\u6cd5 C \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nvoid algorithm_C(int n) {\n for (int i = 0; i &lt; 1000000; i++) {\n printf(\"%d\", 0);\n }\n}\n</code></pre> <pre><code>// \u6f14\u7b97\u6cd5 A \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nfun algoritm_A(n: Int) {\n println(0)\n}\n// \u6f14\u7b97\u6cd5 B \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u7dda\u6027\u968e\nfun algorithm_B(n: Int) {\n for (i in 0..&lt;n){\n println(0)\n }\n}\n// \u6f14\u7b97\u6cd5 C \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nfun algorithm_C(n: Int) {\n for (i in 0..&lt;1000000) {\n println(0)\n }\n}\n</code></pre> <pre><code># \u6f14\u7b97\u6cd5 A \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\ndef algorithm_A(n)\n puts 0\nend\n\n# \u6f14\u7b97\u6cd5 B \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u7dda\u6027\u968e\ndef algorithm_B(n)\n (0...n).each { puts 0 }\nend\n\n# \u6f14\u7b97\u6cd5 C \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\ndef algorithm_C(n)\n (0...1_000_000).each { puts 0 }\nend\n</code></pre> <pre><code>// \u6f14\u7b97\u6cd5 A \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nfn algorithm_A(n: usize) void {\n _ = n;\n std.debug.print(\"{}\\n\", .{0});\n}\n// \u6f14\u7b97\u6cd5 B \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u7dda\u6027\u968e\nfn algorithm_B(n: i32) void {\n for (0..n) |_| {\n std.debug.print(\"{}\\n\", .{0});\n }\n}\n// \u6f14\u7b97\u6cd5 C \u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u5e38\u6578\u968e\nfn algorithm_C(n: i32) void {\n _ = n;\n for (0..1000000) |_| {\n std.debug.print(\"{}\\n\", .{0});\n }\n}\n</code></pre> <p>\u5716 2-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4e09\u500b\u6f14\u7b97\u6cd5\u51fd\u5f0f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u3002</p> <ul> <li>\u6f14\u7b97\u6cd5 <code>A</code> \u53ea\u6709 \\(1\\) \u500b\u5217\u5370\u64cd\u4f5c\uff0c\u6f14\u7b97\u6cd5\u57f7\u884c\u6642\u9593\u4e0d\u96a8\u8457 \\(n\\) \u589e\u5927\u800c\u589e\u9577\u3002\u6211\u5011\u7a31\u6b64\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba\u201c\u5e38\u6578\u968e\u201d\u3002</li> <li>\u6f14\u7b97\u6cd5 <code>B</code> \u4e2d\u7684\u5217\u5370\u64cd\u4f5c\u9700\u8981\u8ff4\u5708 \\(n\\) \u6b21\uff0c\u6f14\u7b97\u6cd5\u57f7\u884c\u6642\u9593\u96a8\u8457 \\(n\\) \u589e\u5927\u5448\u7dda\u6027\u589e\u9577\u3002\u6b64\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u88ab\u7a31\u70ba\u201c\u7dda\u6027\u968e\u201d\u3002</li> <li>\u6f14\u7b97\u6cd5 <code>C</code> \u4e2d\u7684\u5217\u5370\u64cd\u4f5c\u9700\u8981\u8ff4\u5708 \\(1000000\\) \u6b21\uff0c\u96d6\u7136\u57f7\u884c\u6642\u9593\u5f88\u9577\uff0c\u4f46\u5b83\u8207\u8f38\u5165\u8cc7\u6599\u5927\u5c0f \\(n\\) \u7121\u95dc\u3002\u56e0\u6b64 <code>C</code> \u7684\u6642\u9593\u8907\u96dc\u5ea6\u548c <code>A</code> \u76f8\u540c\uff0c\u4ecd\u70ba\u201c\u5e38\u6578\u968e\u201d\u3002</li> </ul> <p></p> <p> \u5716 2-7 \u00a0 \u6f14\u7b97\u6cd5 A\u3001B \u548c C \u7684\u6642\u9593\u589e\u9577\u8da8\u52e2 </p> <p>\u76f8\u8f03\u65bc\u76f4\u63a5\u7d71\u8a08\u6f14\u7b97\u6cd5\u7684\u57f7\u884c\u6642\u9593\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u5206\u6790\u6709\u54ea\u4e9b\u7279\u9ede\u5462\uff1f</p> <ul> <li>\u6642\u9593\u8907\u96dc\u5ea6\u80fd\u5920\u6709\u6548\u8a55\u4f30\u6f14\u7b97\u6cd5\u6548\u7387\u3002\u4f8b\u5982\uff0c\u6f14\u7b97\u6cd5 <code>B</code> \u7684\u57f7\u884c\u6642\u9593\u5448\u7dda\u6027\u589e\u9577\uff0c\u5728 \\(n &gt; 1\\) \u6642\u6bd4\u6f14\u7b97\u6cd5 <code>A</code> \u66f4\u6162\uff0c\u5728 \\(n &gt; 1000000\\) \u6642\u6bd4\u6f14\u7b97\u6cd5 <code>C</code> \u66f4\u6162\u3002\u4e8b\u5be6\u4e0a\uff0c\u53ea\u8981\u8f38\u5165\u8cc7\u6599\u5927\u5c0f \\(n\\) \u8db3\u5920\u5927\uff0c\u8907\u96dc\u5ea6\u70ba\u201c\u5e38\u6578\u968e\u201d\u7684\u6f14\u7b97\u6cd5\u4e00\u5b9a\u512a\u65bc\u201c\u7dda\u6027\u968e\u201d\u7684\u6f14\u7b97\u6cd5\uff0c\u9019\u6b63\u662f\u6642\u9593\u589e\u9577\u8da8\u52e2\u7684\u542b\u7fa9\u3002</li> <li>\u6642\u9593\u8907\u96dc\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u66f4\u7c21\u4fbf\u3002\u986f\u7136\uff0c\u57f7\u884c\u5e73\u81fa\u548c\u8a08\u7b97\u64cd\u4f5c\u578b\u5225\u90fd\u8207\u6f14\u7b97\u6cd5\u57f7\u884c\u6642\u9593\u7684\u589e\u9577\u8da8\u52e2\u7121\u95dc\u3002\u56e0\u6b64\u5728\u6642\u9593\u8907\u96dc\u5ea6\u5206\u6790\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u7c21\u55ae\u5730\u5c07\u6240\u6709\u8a08\u7b97\u64cd\u4f5c\u7684\u57f7\u884c\u6642\u9593\u8996\u70ba\u76f8\u540c\u7684\u201c\u55ae\u4f4d\u6642\u9593\u201d\uff0c\u5f9e\u800c\u5c07\u201c\u8a08\u7b97\u64cd\u4f5c\u57f7\u884c\u6642\u9593\u7d71\u8a08\u201d\u7c21\u5316\u70ba\u201c\u8a08\u7b97\u64cd\u4f5c\u6578\u91cf\u7d71\u8a08\u201d\uff0c\u9019\u6a23\u4e00\u4f86\u4f30\u7b97\u96e3\u5ea6\u5c31\u5927\u5927\u964d\u4f4e\u4e86\u3002</li> <li>\u6642\u9593\u8907\u96dc\u5ea6\u4e5f\u5b58\u5728\u4e00\u5b9a\u7684\u4fb7\u9650\u6027\u3002\u4f8b\u5982\uff0c\u5118\u7ba1\u6f14\u7b97\u6cd5 <code>A</code> \u548c <code>C</code> \u7684\u6642\u9593\u8907\u96dc\u5ea6\u76f8\u540c\uff0c\u4f46\u5be6\u969b\u57f7\u884c\u6642\u9593\u5dee\u5225\u5f88\u5927\u3002\u540c\u6a23\uff0c\u5118\u7ba1\u6f14\u7b97\u6cd5 <code>B</code> \u7684\u6642\u9593\u8907\u96dc\u5ea6\u6bd4 <code>C</code> \u9ad8\uff0c\u4f46\u5728\u8f38\u5165\u8cc7\u6599\u5927\u5c0f \\(n\\) \u8f03\u5c0f\u6642\uff0c\u6f14\u7b97\u6cd5 <code>B</code> \u660e\u986f\u512a\u65bc\u6f14\u7b97\u6cd5 <code>C</code> \u3002\u5728\u9019\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u6211\u5011\u5f88\u96e3\u50c5\u6191\u6642\u9593\u8907\u96dc\u5ea6\u5224\u65b7\u6f14\u7b97\u6cd5\u6548\u7387\u7684\u9ad8\u4f4e\u3002\u7576\u7136\uff0c\u5118\u7ba1\u5b58\u5728\u4e0a\u8ff0\u554f\u984c\uff0c\u8907\u96dc\u5ea6\u5206\u6790\u4ecd\u7136\u662f\u8a55\u5224\u6f14\u7b97\u6cd5\u6548\u7387\u6700\u6709\u6548\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002</li> </ul>"},{"location":"chapter_computational_complexity/time_complexity/#232","title":"2.3.2 \u00a0 \u51fd\u5f0f\u6f38\u8fd1\u4e0a\u754c","text":"<p>\u7d66\u5b9a\u4e00\u500b\u8f38\u5165\u5927\u5c0f\u70ba \\(n\\) \u7684\u51fd\u5f0f\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code>def algorithm(n: int):\n a = 1 # +1\n a = a + 1 # +1\n a = a * 2 # +1\n # \u8ff4\u5708 n \u6b21\n for i in range(n): # +1\n print(0) # +1\n</code></pre> <pre><code>void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u8ff4\u5708 n \u6b21\n for (int i = 0; i &lt; n; i++) { // +1\uff08\u6bcf\u8f2a\u90fd\u57f7\u884c i ++\uff09\n cout &lt;&lt; 0 &lt;&lt; endl; // +1\n }\n}\n</code></pre> <pre><code>void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u8ff4\u5708 n \u6b21\n for (int i = 0; i &lt; n; i++) { // +1\uff08\u6bcf\u8f2a\u90fd\u57f7\u884c i ++\uff09\n System.out.println(0); // +1\n }\n}\n</code></pre> <pre><code>void Algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u8ff4\u5708 n \u6b21\n for (int i = 0; i &lt; n; i++) { // +1\uff08\u6bcf\u8f2a\u90fd\u57f7\u884c i ++\uff09\n Console.WriteLine(0); // +1\n }\n}\n</code></pre> <pre><code>func algorithm(n int) {\n a := 1 // +1\n a = a + 1 // +1\n a = a * 2 // +1\n // \u8ff4\u5708 n \u6b21\n for i := 0; i &lt; n; i++ { // +1\n fmt.Println(a) // +1\n }\n}\n</code></pre> <pre><code>func algorithm(n: Int) {\n var a = 1 // +1\n a = a + 1 // +1\n a = a * 2 // +1\n // \u8ff4\u5708 n \u6b21\n for _ in 0 ..&lt; n { // +1\n print(0) // +1\n }\n}\n</code></pre> <pre><code>function algorithm(n) {\n var a = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // \u8ff4\u5708 n \u6b21\n for(let i = 0; i &lt; n; i++){ // +1\uff08\u6bcf\u8f2a\u90fd\u57f7\u884c i ++\uff09\n console.log(0); // +1\n }\n}\n</code></pre> <pre><code>function algorithm(n: number): void{\n var a: number = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // \u8ff4\u5708 n \u6b21\n for(let i = 0; i &lt; n; i++){ // +1\uff08\u6bcf\u8f2a\u90fd\u57f7\u884c i ++\uff09\n console.log(0); // +1\n }\n}\n</code></pre> <pre><code>void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u8ff4\u5708 n \u6b21\n for (int i = 0; i &lt; n; i++) { // +1\uff08\u6bcf\u8f2a\u90fd\u57f7\u884c i ++\uff09\n print(0); // +1\n }\n}\n</code></pre> <pre><code>fn algorithm(n: i32) {\n let mut a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n\n // \u8ff4\u5708 n \u6b21\n for _ in 0..n { // +1\uff08\u6bcf\u8f2a\u90fd\u57f7\u884c i ++\uff09\n println!(\"{}\", 0); // +1\n }\n}\n</code></pre> <pre><code>void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // \u8ff4\u5708 n \u6b21\n for (int i = 0; i &lt; n; i++) { // +1\uff08\u6bcf\u8f2a\u90fd\u57f7\u884c i ++\uff09\n printf(\"%d\", 0); // +1\n }\n}\n</code></pre> <pre><code>fun algorithm(n: Int) {\n var a = 1 // +1\n a = a + 1 // +1\n a = a * 2 // +1\n // \u8ff4\u5708 n \u6b21\n for (i in 0..&lt;n) { // +1\uff08\u6bcf\u8f2a\u90fd\u57f7\u884c i ++\uff09\n println(0) // +1\n }\n}\n</code></pre> <pre><code>def algorithm(n)\n a = 1 # +1\n a = a + 1 # +1\n a = a * 2 # +1\n # \u8ff4\u5708 n \u6b21\n (0...n).each do # +1\n puts 0 # +1\n end\nend\n</code></pre> <pre><code>fn algorithm(n: usize) void {\n var a: i32 = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // \u8ff4\u5708 n \u6b21\n for (0..n) |_| { // +1\uff08\u6bcf\u8f2a\u90fd\u57f7\u884c i ++\uff09\n std.debug.print(\"{}\\n\", .{0}); // +1\n }\n}\n</code></pre> <p>\u8a2d\u6f14\u7b97\u6cd5\u7684\u64cd\u4f5c\u6578\u91cf\u662f\u4e00\u500b\u95dc\u65bc\u8f38\u5165\u8cc7\u6599\u5927\u5c0f \\(n\\) \u7684\u51fd\u5f0f\uff0c\u8a18\u70ba \\(T(n)\\) \uff0c\u5247\u4ee5\u4e0a\u51fd\u5f0f\u7684\u64cd\u4f5c\u6578\u91cf\u70ba\uff1a</p> \\[ T(n) = 3 + 2n \\] <p>\\(T(n)\\) \u662f\u4e00\u6b21\u51fd\u5f0f\uff0c\u8aaa\u660e\u5176\u57f7\u884c\u6642\u9593\u7684\u589e\u9577\u8da8\u52e2\u662f\u7dda\u6027\u7684\uff0c\u56e0\u6b64\u5b83\u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f\u7dda\u6027\u968e\u3002</p> <p>\u6211\u5011\u5c07\u7dda\u6027\u968e\u7684\u6642\u9593\u8907\u96dc\u5ea6\u8a18\u70ba \\(O(n)\\) \uff0c\u9019\u500b\u6578\u5b78\u7b26\u865f\u7a31\u70ba\u5927 \\(O\\) \u8a18\u865f\uff08big-\\(O\\) notation\uff09\uff0c\u8868\u793a\u51fd\u5f0f \\(T(n)\\) \u7684\u6f38\u8fd1\u4e0a\u754c\uff08asymptotic upper bound\uff09\u3002</p> <p>\u6642\u9593\u8907\u96dc\u5ea6\u5206\u6790\u672c\u8cea\u4e0a\u662f\u8a08\u7b97\u201c\u64cd\u4f5c\u6578\u91cf \\(T(n)\\)\u201d\u7684\u6f38\u8fd1\u4e0a\u754c\uff0c\u5b83\u5177\u6709\u660e\u78ba\u7684\u6578\u5b78\u5b9a\u7fa9\u3002</p> <p>\u51fd\u5f0f\u6f38\u8fd1\u4e0a\u754c</p> <p>\u82e5\u5b58\u5728\u6b63\u5be6\u6578 \\(c\\) \u548c\u5be6\u6578 \\(n_0\\) \uff0c\u4f7f\u5f97\u5c0d\u65bc\u6240\u6709\u7684 \\(n &gt; n_0\\) \uff0c\u5747\u6709 \\(T(n) \\leq c \\cdot f(n)\\) \uff0c\u5247\u53ef\u8a8d\u70ba \\(f(n)\\) \u7d66\u51fa\u4e86 \\(T(n)\\) \u7684\u4e00\u500b\u6f38\u8fd1\u4e0a\u754c\uff0c\u8a18\u70ba \\(T(n) = O(f(n))\\) \u3002</p> <p>\u5982\u5716 2-8 \u6240\u793a\uff0c\u8a08\u7b97\u6f38\u8fd1\u4e0a\u754c\u5c31\u662f\u5c0b\u627e\u4e00\u500b\u51fd\u5f0f \\(f(n)\\) \uff0c\u4f7f\u5f97\u7576 \\(n\\) \u8da8\u5411\u65bc\u7121\u7aae\u5927\u6642\uff0c\\(T(n)\\) \u548c \\(f(n)\\) \u8655\u65bc\u76f8\u540c\u7684\u589e\u9577\u7d1a\u5225\uff0c\u50c5\u76f8\u5dee\u4e00\u500b\u5e38\u6578\u9805 \\(c\\) \u7684\u500d\u6578\u3002</p> <p></p> <p> \u5716 2-8 \u00a0 \u51fd\u5f0f\u7684\u6f38\u8fd1\u4e0a\u754c </p>"},{"location":"chapter_computational_complexity/time_complexity/#233","title":"2.3.3 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"<p>\u6f38\u8fd1\u4e0a\u754c\u7684\u6578\u5b78\u5473\u5152\u6709\u9ede\u91cd\uff0c\u5982\u679c\u4f60\u611f\u89ba\u6c92\u6709\u5b8c\u5168\u7406\u89e3\uff0c\u4e5f\u7121\u9808\u64d4\u5fc3\u3002\u6211\u5011\u53ef\u4ee5\u5148\u638c\u63e1\u63a8\u7b97\u65b9\u6cd5\uff0c\u5728\u4e0d\u65b7\u7684\u5be6\u8e10\u4e2d\uff0c\u5c31\u53ef\u4ee5\u9010\u6f38\u9818\u609f\u5176\u6578\u5b78\u610f\u7fa9\u3002</p> <p>\u6839\u64da\u5b9a\u7fa9\uff0c\u78ba\u5b9a \\(f(n)\\) \u4e4b\u5f8c\uff0c\u6211\u5011\u4fbf\u53ef\u5f97\u5230\u6642\u9593\u8907\u96dc\u5ea6 \\(O(f(n))\\) \u3002\u90a3\u9ebc\u5982\u4f55\u78ba\u5b9a\u6f38\u8fd1\u4e0a\u754c \\(f(n)\\) \u5462\uff1f\u7e3d\u9ad4\u5206\u70ba\u5169\u6b65\uff1a\u9996\u5148\u7d71\u8a08\u64cd\u4f5c\u6578\u91cf\uff0c\u7136\u5f8c\u5224\u65b7\u6f38\u8fd1\u4e0a\u754c\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#1","title":"1. \u00a0 \u7b2c\u4e00\u6b65\uff1a\u7d71\u8a08\u64cd\u4f5c\u6578\u91cf","text":"<p>\u91dd\u5c0d\u7a0b\u5f0f\u78bc\uff0c\u9010\u884c\u5f9e\u4e0a\u5230\u4e0b\u8a08\u7b97\u5373\u53ef\u3002\u7136\u800c\uff0c\u7531\u65bc\u4e0a\u8ff0 \\(c \\cdot f(n)\\) \u4e2d\u7684\u5e38\u6578\u9805 \\(c\\) \u53ef\u4ee5\u53d6\u4efb\u610f\u5927\u5c0f\uff0c\u56e0\u6b64\u64cd\u4f5c\u6578\u91cf \\(T(n)\\) \u4e2d\u7684\u5404\u7a2e\u4fc2\u6578\u3001\u5e38\u6578\u9805\u90fd\u53ef\u4ee5\u5ffd\u7565\u3002\u6839\u64da\u6b64\u539f\u5247\uff0c\u53ef\u4ee5\u7e3d\u7d50\u51fa\u4ee5\u4e0b\u8a08\u6578\u7c21\u5316\u6280\u5de7\u3002</p> <ol> <li>\u5ffd\u7565 \\(T(n)\\) \u4e2d\u7684\u5e38\u6578\u9805\u3002\u56e0\u70ba\u5b83\u5011\u90fd\u8207 \\(n\\) \u7121\u95dc\uff0c\u6240\u4ee5\u5c0d\u6642\u9593\u8907\u96dc\u5ea6\u4e0d\u7522\u751f\u5f71\u97ff\u3002</li> <li>\u7701\u7565\u6240\u6709\u4fc2\u6578\u3002\u4f8b\u5982\uff0c\u8ff4\u5708 \\(2n\\) \u6b21\u3001\\(5n + 1\\) \u6b21\u7b49\uff0c\u90fd\u53ef\u4ee5\u7c21\u5316\u8a18\u70ba \\(n\\) \u6b21\uff0c\u56e0\u70ba \\(n\\) \u524d\u9762\u7684\u4fc2\u6578\u5c0d\u6642\u9593\u8907\u96dc\u5ea6\u6c92\u6709\u5f71\u97ff\u3002</li> <li>\u8ff4\u5708\u5de2\u72c0\u6642\u4f7f\u7528\u4e58\u6cd5\u3002\u7e3d\u64cd\u4f5c\u6578\u91cf\u7b49\u65bc\u5916\u5c64\u8ff4\u5708\u548c\u5167\u5c64\u8ff4\u5708\u64cd\u4f5c\u6578\u91cf\u4e4b\u7a4d\uff0c\u6bcf\u4e00\u5c64\u8ff4\u5708\u4f9d\u7136\u53ef\u4ee5\u5206\u5225\u5957\u7528\u7b2c <code>1.</code> \u9ede\u548c\u7b2c <code>2.</code> \u9ede\u7684\u6280\u5de7\u3002</li> </ol> <p>\u7d66\u5b9a\u4e00\u500b\u51fd\u5f0f\uff0c\u6211\u5011\u53ef\u4ee5\u7528\u4e0a\u8ff0\u6280\u5de7\u4f86\u7d71\u8a08\u64cd\u4f5c\u6578\u91cf\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code>def algorithm(n: int):\n a = 1 # +0\uff08\u6280\u5de7 1\uff09\n a = a + n # +0\uff08\u6280\u5de7 1\uff09\n # +n\uff08\u6280\u5de7 2\uff09\n for i in range(5 * n + 1):\n print(0)\n # +n*n\uff08\u6280\u5de7 3\uff09\n for i in range(2 * n):\n for j in range(n + 1):\n print(0)\n</code></pre> <pre><code>void algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i &lt; 5 * n + 1; i++) {\n cout &lt;&lt; 0 &lt;&lt; endl;\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i &lt; 2 * n; i++) {\n for (int j = 0; j &lt; n + 1; j++) {\n cout &lt;&lt; 0 &lt;&lt; endl;\n }\n }\n}\n</code></pre> <pre><code>void algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i &lt; 5 * n + 1; i++) {\n System.out.println(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i &lt; 2 * n; i++) {\n for (int j = 0; j &lt; n + 1; j++) {\n System.out.println(0);\n }\n }\n}\n</code></pre> <pre><code>void Algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i &lt; 5 * n + 1; i++) {\n Console.WriteLine(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i &lt; 2 * n; i++) {\n for (int j = 0; j &lt; n + 1; j++) {\n Console.WriteLine(0);\n }\n }\n}\n</code></pre> <pre><code>func algorithm(n int) {\n a := 1 // +0\uff08\u6280\u5de7 1\uff09\n a = a + n // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for i := 0; i &lt; 5 * n + 1; i++ {\n fmt.Println(0)\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for i := 0; i &lt; 2 * n; i++ {\n for j := 0; j &lt; n + 1; j++ {\n fmt.Println(0)\n }\n }\n}\n</code></pre> <pre><code>func algorithm(n: Int) {\n var a = 1 // +0\uff08\u6280\u5de7 1\uff09\n a = a + n // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for _ in 0 ..&lt; (5 * n + 1) {\n print(0)\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for _ in 0 ..&lt; (2 * n) {\n for _ in 0 ..&lt; (n + 1) {\n print(0)\n }\n }\n}\n</code></pre> <pre><code>function algorithm(n) {\n let a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (let i = 0; i &lt; 5 * n + 1; i++) {\n console.log(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (let i = 0; i &lt; 2 * n; i++) {\n for (let j = 0; j &lt; n + 1; j++) {\n console.log(0);\n }\n }\n}\n</code></pre> <pre><code>function algorithm(n: number): void {\n let a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (let i = 0; i &lt; 5 * n + 1; i++) {\n console.log(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (let i = 0; i &lt; 2 * n; i++) {\n for (let j = 0; j &lt; n + 1; j++) {\n console.log(0);\n }\n }\n}\n</code></pre> <pre><code>void algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i &lt; 5 * n + 1; i++) {\n print(0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i &lt; 2 * n; i++) {\n for (int j = 0; j &lt; n + 1; j++) {\n print(0);\n }\n }\n}\n</code></pre> <pre><code>fn algorithm(n: i32) {\n let mut a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n\n // +n\uff08\u6280\u5de7 2\uff09\n for i in 0..(5 * n + 1) {\n println!(\"{}\", 0);\n }\n\n // +n*n\uff08\u6280\u5de7 3\uff09\n for i in 0..(2 * n) {\n for j in 0..(n + 1) {\n println!(\"{}\", 0);\n }\n }\n}\n</code></pre> <pre><code>void algorithm(int n) {\n int a = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + n; // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (int i = 0; i &lt; 5 * n + 1; i++) {\n printf(\"%d\", 0);\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (int i = 0; i &lt; 2 * n; i++) {\n for (int j = 0; j &lt; n + 1; j++) {\n printf(\"%d\", 0);\n }\n }\n}\n</code></pre> <pre><code>fun algorithm(n: Int) {\n var a = 1 // +0\uff08\u6280\u5de7 1\uff09\n a = a + n // +0\uff08\u6280\u5de7 1\uff09\n // +n\uff08\u6280\u5de7 2\uff09\n for (i in 0..&lt;5 * n + 1) {\n println(0)\n }\n // +n*n\uff08\u6280\u5de7 3\uff09\n for (i in 0..&lt;2 * n) {\n for (j in 0..&lt;n + 1) {\n println(0)\n }\n }\n}\n</code></pre> <pre><code>def algorithm(n)\n a = 1 # +0\uff08\u6280\u5de7 1\uff09\n a = a + n # +0\uff08\u6280\u5de7 1\uff09\n # +n\uff08\u6280\u5de7 2\uff09\n (0...(5 * n + 1)).each do { puts 0 }\n # +n*n\uff08\u6280\u5de7 3\uff09\n (0...(2 * n)).each do\n (0...(n + 1)).each do { puts 0 }\n end\nend\n</code></pre> <pre><code>fn algorithm(n: usize) void {\n var a: i32 = 1; // +0\uff08\u6280\u5de7 1\uff09\n a = a + @as(i32, @intCast(n)); // +0\uff08\u6280\u5de7 1\uff09\n\n // +n\uff08\u6280\u5de7 2\uff09\n for(0..(5 * n + 1)) |_| {\n std.debug.print(\"{}\\n\", .{0});\n }\n\n // +n*n\uff08\u6280\u5de7 3\uff09\n for(0..(2 * n)) |_| {\n for(0..(n + 1)) |_| {\n std.debug.print(\"{}\\n\", .{0});\n }\n }\n}\n</code></pre> <p>\u4ee5\u4e0b\u516c\u5f0f\u5c55\u793a\u4e86\u4f7f\u7528\u4e0a\u8ff0\u6280\u5de7\u524d\u5f8c\u7684\u7d71\u8a08\u7d50\u679c\uff0c\u5169\u8005\u63a8\u7b97\u51fa\u7684\u6642\u9593\u8907\u96dc\u5ea6\u90fd\u70ba \\(O(n^2)\\) \u3002</p> \\[ \\begin{aligned} T(n) &amp; = 2n(n + 1) + (5n + 1) + 2 &amp; \\text{\u5b8c\u6574\u7d71\u8a08 (-.-|||)} \\newline &amp; = 2n^2 + 7n + 3 \\newline T(n) &amp; = n^2 + n &amp; \\text{\u5077\u61f6\u7d71\u8a08 (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2","title":"2. \u00a0 \u7b2c\u4e8c\u6b65\uff1a\u5224\u65b7\u6f38\u8fd1\u4e0a\u754c","text":"<p>\u6642\u9593\u8907\u96dc\u5ea6\u7531 \\(T(n)\\) \u4e2d\u6700\u9ad8\u968e\u7684\u9805\u4f86\u6c7a\u5b9a\u3002\u9019\u662f\u56e0\u70ba\u5728 \\(n\\) \u8da8\u65bc\u7121\u7aae\u5927\u6642\uff0c\u6700\u9ad8\u968e\u7684\u9805\u5c07\u767c\u63ee\u4e3b\u5c0e\u4f5c\u7528\uff0c\u5176\u4ed6\u9805\u7684\u5f71\u97ff\u90fd\u53ef\u4ee5\u5ffd\u7565\u3002</p> <p>\u8868 2-2 \u5c55\u793a\u4e86\u4e00\u4e9b\u4f8b\u5b50\uff0c\u5176\u4e2d\u4e00\u4e9b\u8a87\u5f35\u7684\u503c\u662f\u70ba\u4e86\u5f37\u8abf\u201c\u4fc2\u6578\u7121\u6cd5\u64bc\u52d5\u968e\u6578\u201d\u9019\u4e00\u7d50\u8ad6\u3002\u7576 \\(n\\) \u8da8\u65bc\u7121\u7aae\u5927\u6642\uff0c\u9019\u4e9b\u5e38\u6578\u8b8a\u5f97\u7121\u8db3\u8f15\u91cd\u3002</p> <p> \u8868 2-2 \u00a0 \u4e0d\u540c\u64cd\u4f5c\u6578\u91cf\u5c0d\u61c9\u7684\u6642\u9593\u8907\u96dc\u5ea6 </p> \u64cd\u4f5c\u6578\u91cf \\(T(n)\\) \u6642\u9593\u8907\u96dc\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\u898b\u578b\u5225","text":"<p>\u8a2d\u8f38\u5165\u8cc7\u6599\u5927\u5c0f\u70ba \\(n\\) \uff0c\u5e38\u898b\u7684\u6642\u9593\u8907\u96dc\u5ea6\u578b\u5225\u5982\u5716 2-9 \u6240\u793a\uff08\u6309\u7167\u5f9e\u4f4e\u5230\u9ad8\u7684\u9806\u5e8f\u6392\u5217\uff09\u3002</p> \\[ \\begin{aligned} O(1) &lt; O(\\log n) &lt; O(n) &lt; O(n \\log n) &lt; O(n^2) &lt; O(2^n) &lt; O(n!) \\newline \\text{\u5e38\u6578\u968e} &lt; \\text{\u5c0d\u6578\u968e} &lt; \\text{\u7dda\u6027\u968e} &lt; \\text{\u7dda\u6027\u5c0d\u6578\u968e} &lt; \\text{\u5e73\u65b9\u968e} &lt; \\text{\u6307\u6578\u968e} &lt; \\text{\u968e\u4e58\u968e} \\end{aligned} \\] <p></p> <p> \u5716 2-9 \u00a0 \u5e38\u898b\u7684\u6642\u9593\u8907\u96dc\u5ea6\u578b\u5225 </p>"},{"location":"chapter_computational_complexity/time_complexity/#1-o1","title":"1. \u00a0 \u5e38\u6578\u968e \\(O(1)\\)","text":"<p>\u5e38\u6578\u968e\u7684\u64cd\u4f5c\u6578\u91cf\u8207\u8f38\u5165\u8cc7\u6599\u5927\u5c0f \\(n\\) \u7121\u95dc\uff0c\u5373\u4e0d\u96a8\u8457 \\(n\\) \u7684\u8b8a\u5316\u800c\u8b8a\u5316\u3002</p> <p>\u5728\u4ee5\u4e0b\u51fd\u5f0f\u4e2d\uff0c\u5118\u7ba1\u64cd\u4f5c\u6578\u91cf <code>size</code> \u53ef\u80fd\u5f88\u5927\uff0c\u4f46\u7531\u65bc\u5176\u8207\u8f38\u5165\u8cc7\u6599\u5927\u5c0f \\(n\\) \u7121\u95dc\uff0c\u56e0\u6b64\u6642\u9593\u8907\u96dc\u5ea6\u4ecd\u70ba \\(O(1)\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.py<pre><code>def constant(n: int) -&gt; int:\n \"\"\"\u5e38\u6578\u968e\"\"\"\n count = 0\n size = 100000\n for _ in range(size):\n count += 1\n return count\n</code></pre> time_complexity.cpp<pre><code>/* \u5e38\u6578\u968e */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i &lt; size; i++)\n count++;\n return count;\n}\n</code></pre> time_complexity.java<pre><code>/* \u5e38\u6578\u968e */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i &lt; size; i++)\n count++;\n return count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u5e38\u6578\u968e */\nint Constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i &lt; size; i++)\n count++;\n return count;\n}\n</code></pre> time_complexity.go<pre><code>/* \u5e38\u6578\u968e */\nfunc constant(n int) int {\n count := 0\n size := 100000\n for i := 0; i &lt; size; i++ {\n count++\n }\n return count\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5e38\u6578\u968e */\nfunc constant(n: Int) -&gt; Int {\n var count = 0\n let size = 100_000\n for _ in 0 ..&lt; size {\n count += 1\n }\n return count\n}\n</code></pre> time_complexity.js<pre><code>/* \u5e38\u6578\u968e */\nfunction constant(n) {\n let count = 0;\n const size = 100000;\n for (let i = 0; i &lt; size; i++) count++;\n return count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5e38\u6578\u968e */\nfunction constant(n: number): number {\n let count = 0;\n const size = 100000;\n for (let i = 0; i &lt; size; i++) count++;\n return count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u5e38\u6578\u968e */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (var i = 0; i &lt; size; i++) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u5e38\u6578\u968e */\nfn constant(n: i32) -&gt; i32 {\n _ = n;\n let mut count = 0;\n let size = 100_000;\n for _ in 0..size {\n count += 1;\n }\n count\n}\n</code></pre> time_complexity.c<pre><code>/* \u5e38\u6578\u968e */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n int i = 0;\n for (int i = 0; i &lt; size; i++) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.kt<pre><code>/* \u5e38\u6578\u968e */\nfun constant(n: Int): Int {\n var count = 0\n val size = 100000\n for (i in 0..&lt;size)\n count++\n return count\n}\n</code></pre> time_complexity.rb<pre><code>### \u5e38\u6578\u968e ###\ndef constant(n)\n count = 0\n size = 100000\n\n (0...size).each { count += 1 }\n\n count\nend\n</code></pre> time_complexity.zig<pre><code>// \u5e38\u6578\u968e\nfn constant(n: i32) i32 {\n _ = n;\n var count: i32 = 0;\n const size: i32 = 100_000;\n var i: i32 = 0;\n while(i&lt;size) : (i += 1) {\n count += 1;\n }\n return count;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_computational_complexity/time_complexity/#2-on","title":"2. \u00a0 \u7dda\u6027\u968e \\(O(n)\\)","text":"<p>\u7dda\u6027\u968e\u7684\u64cd\u4f5c\u6578\u91cf\u76f8\u5c0d\u65bc\u8f38\u5165\u8cc7\u6599\u5927\u5c0f \\(n\\) \u4ee5\u7dda\u6027\u7d1a\u5225\u589e\u9577\u3002\u7dda\u6027\u968e\u901a\u5e38\u51fa\u73fe\u5728\u55ae\u5c64\u8ff4\u5708\u4e2d\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.py<pre><code>def linear(n: int) -&gt; int:\n \"\"\"\u7dda\u6027\u968e\"\"\"\n count = 0\n for _ in range(n):\n count += 1\n return count\n</code></pre> time_complexity.cpp<pre><code>/* \u7dda\u6027\u968e */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i &lt; n; i++)\n count++;\n return count;\n}\n</code></pre> time_complexity.java<pre><code>/* \u7dda\u6027\u968e */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i &lt; n; i++)\n count++;\n return count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u7dda\u6027\u968e */\nint Linear(int n) {\n int count = 0;\n for (int i = 0; i &lt; n; i++)\n count++;\n return count;\n}\n</code></pre> time_complexity.go<pre><code>/* \u7dda\u6027\u968e */\nfunc linear(n int) int {\n count := 0\n for i := 0; i &lt; n; i++ {\n count++\n }\n return count\n}\n</code></pre> time_complexity.swift<pre><code>/* \u7dda\u6027\u968e */\nfunc linear(n: Int) -&gt; Int {\n var count = 0\n for _ in 0 ..&lt; n {\n count += 1\n }\n return count\n}\n</code></pre> time_complexity.js<pre><code>/* \u7dda\u6027\u968e */\nfunction linear(n) {\n let count = 0;\n for (let i = 0; i &lt; n; i++) count++;\n return count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u7dda\u6027\u968e */\nfunction linear(n: number): number {\n let count = 0;\n for (let i = 0; i &lt; n; i++) count++;\n return count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u7dda\u6027\u968e */\nint linear(int n) {\n int count = 0;\n for (var i = 0; i &lt; n; i++) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u7dda\u6027\u968e */\nfn linear(n: i32) -&gt; i32 {\n let mut count = 0;\n for _ in 0..n {\n count += 1;\n }\n count\n}\n</code></pre> time_complexity.c<pre><code>/* \u7dda\u6027\u968e */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i &lt; n; i++) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.kt<pre><code>/* \u7dda\u6027\u968e */\nfun linear(n: Int): Int {\n var count = 0\n for (i in 0..&lt;n)\n count++\n return count\n}\n</code></pre> time_complexity.rb<pre><code>### \u7dda\u6027\u968e ###\ndef linear(n)\n count = 0\n (0...n).each { count += 1 }\n count\nend\n</code></pre> time_complexity.zig<pre><code>// \u7dda\u6027\u968e\nfn linear(n: i32) i32 {\n var count: i32 = 0;\n var i: i32 = 0;\n while (i &lt; n) : (i += 1) {\n count += 1;\n }\n return count;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u8d70\u8a2a\u9663\u5217\u548c\u8d70\u8a2a\u93c8\u7d50\u4e32\u5217\u7b49\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u5747\u70ba \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u70ba\u9663\u5217\u6216\u93c8\u7d50\u4e32\u5217\u7684\u9577\u5ea6\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.py<pre><code>def array_traversal(nums: list[int]) -&gt; int:\n \"\"\"\u7dda\u6027\u968e\uff08\u8d70\u8a2a\u9663\u5217\uff09\"\"\"\n count = 0\n # \u8ff4\u5708\u6b21\u6578\u8207\u9663\u5217\u9577\u5ea6\u6210\u6b63\u6bd4\n for num in nums:\n count += 1\n return count\n</code></pre> time_complexity.cpp<pre><code>/* \u7dda\u6027\u968e\uff08\u8d70\u8a2a\u9663\u5217\uff09 */\nint arrayTraversal(vector&lt;int&gt; &amp;nums) {\n int count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u9663\u5217\u9577\u5ea6\u6210\u6b63\u6bd4\n for (int num : nums) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.java<pre><code>/* \u7dda\u6027\u968e\uff08\u8d70\u8a2a\u9663\u5217\uff09 */\nint arrayTraversal(int[] nums) {\n int count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u9663\u5217\u9577\u5ea6\u6210\u6b63\u6bd4\n for (int num : nums) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u7dda\u6027\u968e\uff08\u8d70\u8a2a\u9663\u5217\uff09 */\nint ArrayTraversal(int[] nums) {\n int count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u9663\u5217\u9577\u5ea6\u6210\u6b63\u6bd4\n foreach (int num in nums) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.go<pre><code>/* \u7dda\u6027\u968e\uff08\u8d70\u8a2a\u9663\u5217\uff09 */\nfunc arrayTraversal(nums []int) int {\n count := 0\n // \u8ff4\u5708\u6b21\u6578\u8207\u9663\u5217\u9577\u5ea6\u6210\u6b63\u6bd4\n for range nums {\n count++\n }\n return count\n}\n</code></pre> time_complexity.swift<pre><code>/* \u7dda\u6027\u968e\uff08\u8d70\u8a2a\u9663\u5217\uff09 */\nfunc arrayTraversal(nums: [Int]) -&gt; Int {\n var count = 0\n // \u8ff4\u5708\u6b21\u6578\u8207\u9663\u5217\u9577\u5ea6\u6210\u6b63\u6bd4\n for _ in nums {\n count += 1\n }\n return count\n}\n</code></pre> time_complexity.js<pre><code>/* \u7dda\u6027\u968e\uff08\u8d70\u8a2a\u9663\u5217\uff09 */\nfunction arrayTraversal(nums) {\n let count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u9663\u5217\u9577\u5ea6\u6210\u6b63\u6bd4\n for (let i = 0; i &lt; nums.length; i++) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u7dda\u6027\u968e\uff08\u8d70\u8a2a\u9663\u5217\uff09 */\nfunction arrayTraversal(nums: number[]): number {\n let count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u9663\u5217\u9577\u5ea6\u6210\u6b63\u6bd4\n for (let i = 0; i &lt; nums.length; i++) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u7dda\u6027\u968e\uff08\u8d70\u8a2a\u9663\u5217\uff09 */\nint arrayTraversal(List&lt;int&gt; nums) {\n int count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u9663\u5217\u9577\u5ea6\u6210\u6b63\u6bd4\n for (var _num in nums) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u7dda\u6027\u968e\uff08\u8d70\u8a2a\u9663\u5217\uff09 */\nfn array_traversal(nums: &amp;[i32]) -&gt; i32 {\n let mut count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u9663\u5217\u9577\u5ea6\u6210\u6b63\u6bd4\n for _ in nums {\n count += 1;\n }\n count\n}\n</code></pre> time_complexity.c<pre><code>/* \u7dda\u6027\u968e\uff08\u8d70\u8a2a\u9663\u5217\uff09 */\nint arrayTraversal(int *nums, int n) {\n int count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u9663\u5217\u9577\u5ea6\u6210\u6b63\u6bd4\n for (int i = 0; i &lt; n; i++) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.kt<pre><code>/* \u7dda\u6027\u968e\uff08\u8d70\u8a2a\u9663\u5217\uff09 */\nfun arrayTraversal(nums: IntArray): Int {\n var count = 0\n // \u8ff4\u5708\u6b21\u6578\u8207\u9663\u5217\u9577\u5ea6\u6210\u6b63\u6bd4\n for (num in nums) {\n count++\n }\n return count\n}\n</code></pre> time_complexity.rb<pre><code>### \u7dda\u6027\u968e\uff08\u8d70\u8a2a\u9663\u5217\uff09###\ndef array_traversal(nums)\n count = 0\n\n # \u8ff4\u5708\u6b21\u6578\u8207\u9663\u5217\u9577\u5ea6\u6210\u6b63\u6bd4\n for num in nums\n count += 1\n end\n\n count\nend\n</code></pre> time_complexity.zig<pre><code>// \u7dda\u6027\u968e\uff08\u8d70\u8a2a\u9663\u5217\uff09\nfn arrayTraversal(nums: []i32) i32 {\n var count: i32 = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u9663\u5217\u9577\u5ea6\u6210\u6b63\u6bd4\n for (nums) |_| {\n count += 1;\n }\n return count;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8f38\u5165\u8cc7\u6599\u5927\u5c0f \\(n\\) \u9700\u6839\u64da\u8f38\u5165\u8cc7\u6599\u7684\u578b\u5225\u4f86\u5177\u9ad4\u78ba\u5b9a\u3002\u6bd4\u5982\u5728\u7b2c\u4e00\u500b\u793a\u4f8b\u4e2d\uff0c\u8b8a\u6578 \\(n\\) \u70ba\u8f38\u5165\u8cc7\u6599\u5927\u5c0f\uff1b\u5728\u7b2c\u4e8c\u500b\u793a\u4f8b\u4e2d\uff0c\u9663\u5217\u9577\u5ea6 \\(n\\) \u70ba\u8cc7\u6599\u5927\u5c0f\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#3-on2","title":"3. \u00a0 \u5e73\u65b9\u968e \\(O(n^2)\\)","text":"<p>\u5e73\u65b9\u968e\u7684\u64cd\u4f5c\u6578\u91cf\u76f8\u5c0d\u65bc\u8f38\u5165\u8cc7\u6599\u5927\u5c0f \\(n\\) \u4ee5\u5e73\u65b9\u7d1a\u5225\u589e\u9577\u3002\u5e73\u65b9\u968e\u901a\u5e38\u51fa\u73fe\u5728\u5de2\u72c0\u8ff4\u5708\u4e2d\uff0c\u5916\u5c64\u8ff4\u5708\u548c\u5167\u5c64\u8ff4\u5708\u7684\u6642\u9593\u8907\u96dc\u5ea6\u90fd\u70ba \\(O(n)\\) \uff0c\u56e0\u6b64\u7e3d\u9ad4\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.py<pre><code>def quadratic(n: int) -&gt; int:\n \"\"\"\u5e73\u65b9\u968e\"\"\"\n count = 0\n # \u8ff4\u5708\u6b21\u6578\u8207\u8cc7\u6599\u5927\u5c0f n \u6210\u5e73\u65b9\u95dc\u4fc2\n for i in range(n):\n for j in range(n):\n count += 1\n return count\n</code></pre> time_complexity.cpp<pre><code>/* \u5e73\u65b9\u968e */\nint quadratic(int n) {\n int count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u8cc7\u6599\u5927\u5c0f n \u6210\u5e73\u65b9\u95dc\u4fc2\n for (int i = 0; i &lt; n; i++) {\n for (int j = 0; j &lt; n; j++) {\n count++;\n }\n }\n return count;\n}\n</code></pre> time_complexity.java<pre><code>/* \u5e73\u65b9\u968e */\nint quadratic(int n) {\n int count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u8cc7\u6599\u5927\u5c0f n \u6210\u5e73\u65b9\u95dc\u4fc2\n for (int i = 0; i &lt; n; i++) {\n for (int j = 0; j &lt; n; j++) {\n count++;\n }\n }\n return count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u5e73\u65b9\u968e */\nint Quadratic(int n) {\n int count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u8cc7\u6599\u5927\u5c0f n \u6210\u5e73\u65b9\u95dc\u4fc2\n for (int i = 0; i &lt; n; i++) {\n for (int j = 0; j &lt; n; j++) {\n count++;\n }\n }\n return count;\n}\n</code></pre> time_complexity.go<pre><code>/* \u5e73\u65b9\u968e */\nfunc quadratic(n int) int {\n count := 0\n // \u8ff4\u5708\u6b21\u6578\u8207\u8cc7\u6599\u5927\u5c0f n \u6210\u5e73\u65b9\u95dc\u4fc2\n for i := 0; i &lt; n; i++ {\n for j := 0; j &lt; n; j++ {\n count++\n }\n }\n return count\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5e73\u65b9\u968e */\nfunc quadratic(n: Int) -&gt; Int {\n var count = 0\n // \u8ff4\u5708\u6b21\u6578\u8207\u8cc7\u6599\u5927\u5c0f n \u6210\u5e73\u65b9\u95dc\u4fc2\n for _ in 0 ..&lt; n {\n for _ in 0 ..&lt; n {\n count += 1\n }\n }\n return count\n}\n</code></pre> time_complexity.js<pre><code>/* \u5e73\u65b9\u968e */\nfunction quadratic(n) {\n let count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u8cc7\u6599\u5927\u5c0f n \u6210\u5e73\u65b9\u95dc\u4fc2\n for (let i = 0; i &lt; n; i++) {\n for (let j = 0; j &lt; n; j++) {\n count++;\n }\n }\n return count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5e73\u65b9\u968e */\nfunction quadratic(n: number): number {\n let count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u8cc7\u6599\u5927\u5c0f n \u6210\u5e73\u65b9\u95dc\u4fc2\n for (let i = 0; i &lt; n; i++) {\n for (let j = 0; j &lt; n; j++) {\n count++;\n }\n }\n return count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u5e73\u65b9\u968e */\nint quadratic(int n) {\n int count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u8cc7\u6599\u5927\u5c0f n \u6210\u5e73\u65b9\u95dc\u4fc2\n for (int i = 0; i &lt; n; i++) {\n for (int j = 0; j &lt; n; j++) {\n count++;\n }\n }\n return count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u5e73\u65b9\u968e */\nfn quadratic(n: i32) -&gt; i32 {\n let mut count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u8cc7\u6599\u5927\u5c0f n \u6210\u5e73\u65b9\u95dc\u4fc2\n for _ in 0..n {\n for _ in 0..n {\n count += 1;\n }\n }\n count\n}\n</code></pre> time_complexity.c<pre><code>/* \u5e73\u65b9\u968e */\nint quadratic(int n) {\n int count = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u8cc7\u6599\u5927\u5c0f n \u6210\u5e73\u65b9\u95dc\u4fc2\n for (int i = 0; i &lt; n; i++) {\n for (int j = 0; j &lt; n; j++) {\n count++;\n }\n }\n return count;\n}\n</code></pre> time_complexity.kt<pre><code>/* \u5e73\u65b9\u968e */\nfun quadratic(n: Int): Int {\n var count = 0\n // \u8ff4\u5708\u6b21\u6578\u8207\u8cc7\u6599\u5927\u5c0f n \u6210\u5e73\u65b9\u95dc\u4fc2\n for (i in 0..&lt;n) {\n for (j in 0..&lt;n) {\n count++\n }\n }\n return count\n}\n</code></pre> time_complexity.rb<pre><code>### \u5e73\u65b9\u968e ###\ndef quadratic(n)\n count = 0\n\n # \u8ff4\u5708\u6b21\u6578\u8207\u8cc7\u6599\u5927\u5c0f n \u6210\u5e73\u65b9\u95dc\u4fc2\n for i in 0...n\n for j in 0...n\n count += 1\n end\n end\n\n count\nend\n</code></pre> time_complexity.zig<pre><code>// \u5e73\u65b9\u968e\nfn quadratic(n: i32) i32 {\n var count: i32 = 0;\n var i: i32 = 0;\n // \u8ff4\u5708\u6b21\u6578\u8207\u8cc7\u6599\u5927\u5c0f n \u6210\u5e73\u65b9\u95dc\u4fc2\n while (i &lt; n) : (i += 1) {\n var j: i32 = 0;\n while (j &lt; n) : (j += 1) {\n count += 1;\n }\n }\n return count;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 2-10 \u5c0d\u6bd4\u4e86\u5e38\u6578\u968e\u3001\u7dda\u6027\u968e\u548c\u5e73\u65b9\u968e\u4e09\u7a2e\u6642\u9593\u8907\u96dc\u5ea6\u3002</p> <p></p> <p> \u5716 2-10 \u00a0 \u5e38\u6578\u968e\u3001\u7dda\u6027\u968e\u548c\u5e73\u65b9\u968e\u7684\u6642\u9593\u8907\u96dc\u5ea6 </p> <p>\u4ee5\u6ce1\u6cab\u6392\u5e8f\u70ba\u4f8b\uff0c\u5916\u5c64\u8ff4\u5708\u57f7\u884c \\(n - 1\\) \u6b21\uff0c\u5167\u5c64\u8ff4\u5708\u57f7\u884c \\(n-1\\)\u3001\\(n-2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u6b21\uff0c\u5e73\u5747\u70ba \\(n / 2\\) \u6b21\uff0c\u56e0\u6b64\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O((n - 1) n / 2) = O(n^2)\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.py<pre><code>def bubble_sort(nums: list[int]) -&gt; int:\n \"\"\"\u5e73\u65b9\u968e\uff08\u6ce1\u6cab\u6392\u5e8f\uff09\"\"\"\n count = 0 # \u8a08\u6578\u5668\n # \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for i in range(len(nums) - 1, 0, -1):\n # \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for j in range(i):\n if nums[j] &gt; nums[j + 1]:\n # \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n tmp: int = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n count += 3 # \u5143\u7d20\u4ea4\u63db\u5305\u542b 3 \u500b\u55ae\u5143\u64cd\u4f5c\n return count\n</code></pre> time_complexity.cpp<pre><code>/* \u5e73\u65b9\u968e\uff08\u6ce1\u6cab\u6392\u5e8f\uff09 */\nint bubbleSort(vector&lt;int&gt; &amp;nums) {\n int count = 0; // \u8a08\u6578\u5668\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (int i = nums.size() - 1; i &gt; 0; i--) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (int j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u63db\u5305\u542b 3 \u500b\u55ae\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n</code></pre> time_complexity.java<pre><code>/* \u5e73\u65b9\u968e\uff08\u6ce1\u6cab\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums) {\n int count = 0; // \u8a08\u6578\u5668\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (int i = nums.length - 1; i &gt; 0; i--) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (int j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u63db\u5305\u542b 3 \u500b\u55ae\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u5e73\u65b9\u968e\uff08\u6ce1\u6cab\u6392\u5e8f\uff09 */\nint BubbleSort(int[] nums) {\n int count = 0; // \u8a08\u6578\u5668\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (int i = nums.Length - 1; i &gt; 0; i--) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (int j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n count += 3; // \u5143\u7d20\u4ea4\u63db\u5305\u542b 3 \u500b\u55ae\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n</code></pre> time_complexity.go<pre><code>/* \u5e73\u65b9\u968e\uff08\u6ce1\u6cab\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums []int) int {\n count := 0 // \u8a08\u6578\u5668\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for i := len(nums) - 1; i &gt; 0; i-- {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for j := 0; j &lt; i; j++ {\n if nums[j] &gt; nums[j+1] {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n tmp := nums[j]\n nums[j] = nums[j+1]\n nums[j+1] = tmp\n count += 3 // \u5143\u7d20\u4ea4\u63db\u5305\u542b 3 \u500b\u55ae\u5143\u64cd\u4f5c\n }\n }\n }\n return count\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5e73\u65b9\u968e\uff08\u6ce1\u6cab\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums: inout [Int]) -&gt; Int {\n var count = 0 // \u8a08\u6578\u5668\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for i in nums.indices.dropFirst().reversed() {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for j in 0 ..&lt; i {\n if nums[j] &gt; nums[j + 1] {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n let tmp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n count += 3 // \u5143\u7d20\u4ea4\u63db\u5305\u542b 3 \u500b\u55ae\u5143\u64cd\u4f5c\n }\n }\n }\n return count\n}\n</code></pre> time_complexity.js<pre><code>/* \u5e73\u65b9\u968e\uff08\u6ce1\u6cab\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums) {\n let count = 0; // \u8a08\u6578\u5668\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (let i = nums.length - 1; i &gt; 0; i--) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (let j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u63db\u5305\u542b 3 \u500b\u55ae\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5e73\u65b9\u968e\uff08\u6ce1\u6cab\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums: number[]): number {\n let count = 0; // \u8a08\u6578\u5668\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (let i = nums.length - 1; i &gt; 0; i--) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (let j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u63db\u5305\u542b 3 \u500b\u55ae\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u5e73\u65b9\u968e\uff08\u6ce1\u6cab\u6392\u5e8f\uff09 */\nint bubbleSort(List&lt;int&gt; nums) {\n int count = 0; // \u8a08\u6578\u5668\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (var i = nums.length - 1; i &gt; 0; i--) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (var j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u63db\u5305\u542b 3 \u500b\u55ae\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u5e73\u65b9\u968e\uff08\u6ce1\u6cab\u6392\u5e8f\uff09 */\nfn bubble_sort(nums: &amp;mut [i32]) -&gt; i32 {\n let mut count = 0; // \u8a08\u6578\u5668\n\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for i in (1..nums.len()).rev() {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for j in 0..i {\n if nums[j] &gt; nums[j + 1] {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u63db\u5305\u542b 3 \u500b\u55ae\u5143\u64cd\u4f5c\n }\n }\n }\n count\n}\n</code></pre> time_complexity.c<pre><code>/* \u5e73\u65b9\u968e\uff08\u6ce1\u6cab\u6392\u5e8f\uff09 */\nint bubbleSort(int *nums, int n) {\n int count = 0; // \u8a08\u6578\u5668\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (int i = n - 1; i &gt; 0; i--) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (int j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u63db\u5305\u542b 3 \u500b\u55ae\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n</code></pre> time_complexity.kt<pre><code>/* \u5e73\u65b9\u968e\uff08\u6ce1\u6cab\u6392\u5e8f\uff09 */\nfun bubbleSort(nums: IntArray): Int {\n var count = 0 // \u8a08\u6578\u5668\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (i in nums.size - 1 downTo 1) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (j in 0..&lt;i) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n val temp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = temp\n count += 3 // \u5143\u7d20\u4ea4\u63db\u5305\u542b 3 \u500b\u55ae\u5143\u64cd\u4f5c\n }\n }\n }\n return count\n}\n</code></pre> time_complexity.rb<pre><code>### \u5e73\u65b9\u968e\uff08\u6ce1\u6cab\u6392\u5e8f\uff09###\ndef bubble_sort(nums)\n count = 0 # \u8a08\u6578\u5668\n\n # \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for i in (nums.length - 1).downto(0)\n # \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for j in 0...i\n if nums[j] &gt; nums[j + 1]\n # \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n tmp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n count += 3 # \u5143\u7d20\u4ea4\u63db\u5305\u542b 3 \u500b\u55ae\u5143\u64cd\u4f5c\n end\n end\n end\n\n count\nend\n</code></pre> time_complexity.zig<pre><code>// \u5e73\u65b9\u968e\uff08\u6ce1\u6cab\u6392\u5e8f\uff09\nfn bubbleSort(nums: []i32) i32 {\n var count: i32 = 0; // \u8a08\u6578\u5668 \n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n var i: i32 = @as(i32, @intCast(nums.len)) - 1;\n while (i &gt; 0) : (i -= 1) {\n var j: usize = 0;\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n while (j &lt; i) : (j += 1) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n var tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u63db\u5305\u542b 3 \u500b\u55ae\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_computational_complexity/time_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6578\u968e \\(O(2^n)\\)","text":"<p>\u751f\u7269\u5b78\u7684\u201c\u7d30\u80de\u5206\u88c2\u201d\u662f\u6307\u6578\u968e\u589e\u9577\u7684\u5178\u578b\u4f8b\u5b50\uff1a\u521d\u59cb\u72c0\u614b\u70ba \\(1\\) \u500b\u7d30\u80de\uff0c\u5206\u88c2\u4e00\u8f2a\u5f8c\u8b8a\u70ba \\(2\\) \u500b\uff0c\u5206\u88c2\u5169\u8f2a\u5f8c\u8b8a\u70ba \\(4\\) \u500b\uff0c\u4ee5\u6b64\u985e\u63a8\uff0c\u5206\u88c2 \\(n\\) \u8f2a\u5f8c\u6709 \\(2^n\\) \u500b\u7d30\u80de\u3002</p> <p>\u5716 2-11 \u548c\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u6a21\u64ec\u4e86\u7d30\u80de\u5206\u88c2\u7684\u904e\u7a0b\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(2^n)\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.py<pre><code>def exponential(n: int) -&gt; int:\n \"\"\"\u6307\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09\"\"\"\n count = 0\n base = 1\n # \u7d30\u80de\u6bcf\u8f2a\u4e00\u5206\u70ba\u4e8c\uff0c\u5f62\u6210\u6578\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in range(n):\n for _ in range(base):\n count += 1\n base *= 2\n # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n</code></pre> time_complexity.cpp<pre><code>/* \u6307\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7d30\u80de\u6bcf\u8f2a\u4e00\u5206\u70ba\u4e8c\uff0c\u5f62\u6210\u6578\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i &lt; n; i++) {\n for (int j = 0; j &lt; base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n</code></pre> time_complexity.java<pre><code>/* \u6307\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7d30\u80de\u6bcf\u8f2a\u4e00\u5206\u70ba\u4e8c\uff0c\u5f62\u6210\u6578\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i &lt; n; i++) {\n for (int j = 0; j &lt; base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u6307\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nint Exponential(int n) {\n int count = 0, bas = 1;\n // \u7d30\u80de\u6bcf\u8f2a\u4e00\u5206\u70ba\u4e8c\uff0c\u5f62\u6210\u6578\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i &lt; n; i++) {\n for (int j = 0; j &lt; bas; j++) {\n count++;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n</code></pre> time_complexity.go<pre><code>/* \u6307\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09*/\nfunc exponential(n int) int {\n count, base := 0, 1\n // \u7d30\u80de\u6bcf\u8f2a\u4e00\u5206\u70ba\u4e8c\uff0c\u5f62\u6210\u6578\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for i := 0; i &lt; n; i++ {\n for j := 0; j &lt; base; j++ {\n count++\n }\n base *= 2\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n}\n</code></pre> time_complexity.swift<pre><code>/* \u6307\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nfunc exponential(n: Int) -&gt; Int {\n var count = 0\n var base = 1\n // \u7d30\u80de\u6bcf\u8f2a\u4e00\u5206\u70ba\u4e8c\uff0c\u5f62\u6210\u6578\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in 0 ..&lt; n {\n for _ in 0 ..&lt; base {\n count += 1\n }\n base *= 2\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n}\n</code></pre> time_complexity.js<pre><code>/* \u6307\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nfunction exponential(n) {\n let count = 0,\n base = 1;\n // \u7d30\u80de\u6bcf\u8f2a\u4e00\u5206\u70ba\u4e8c\uff0c\u5f62\u6210\u6578\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (let i = 0; i &lt; n; i++) {\n for (let j = 0; j &lt; base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u6307\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nfunction exponential(n: number): number {\n let count = 0,\n base = 1;\n // \u7d30\u80de\u6bcf\u8f2a\u4e00\u5206\u70ba\u4e8c\uff0c\u5f62\u6210\u6578\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (let i = 0; i &lt; n; i++) {\n for (let j = 0; j &lt; base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u6307\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7d30\u80de\u6bcf\u8f2a\u4e00\u5206\u70ba\u4e8c\uff0c\u5f62\u6210\u6578\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (var i = 0; i &lt; n; i++) {\n for (var j = 0; j &lt; base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u6307\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nfn exponential(n: i32) -&gt; i32 {\n let mut count = 0;\n let mut base = 1;\n // \u7d30\u80de\u6bcf\u8f2a\u4e00\u5206\u70ba\u4e8c\uff0c\u5f62\u6210\u6578\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in 0..n {\n for _ in 0..base {\n count += 1\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n count\n}\n</code></pre> time_complexity.c<pre><code>/* \u6307\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nint exponential(int n) {\n int count = 0;\n int bas = 1;\n // \u7d30\u80de\u6bcf\u8f2a\u4e00\u5206\u70ba\u4e8c\uff0c\u5f62\u6210\u6578\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i &lt; n; i++) {\n for (int j = 0; j &lt; bas; j++) {\n count++;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n</code></pre> time_complexity.kt<pre><code>/* \u6307\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nfun exponential(n: Int): Int {\n var count = 0\n var base = 1\n // \u7d30\u80de\u6bcf\u8f2a\u4e00\u5206\u70ba\u4e8c\uff0c\u5f62\u6210\u6578\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (i in 0..&lt;n) {\n for (j in 0..&lt;base) {\n count++\n }\n base *= 2\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n}\n</code></pre> time_complexity.rb<pre><code>### \u6307\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09###\ndef exponential(n)\n count, base = 0, 1\n\n # \u7d30\u80de\u6bcf\u8f2a\u4e00\u5206\u70ba\u4e8c\uff0c\u5f62\u6210\u6578\u5217 1, 2, 4, 8, ..., 2^(n-1)\n (0...n).each do\n (0...base).each { count += 1 }\n base *= 2\n end\n\n # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n count\nend\n</code></pre> time_complexity.zig<pre><code>// \u6307\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09\nfn exponential(n: i32) i32 {\n var count: i32 = 0;\n var bas: i32 = 1;\n var i: i32 = 0;\n // \u7d30\u80de\u6bcf\u8f2a\u4e00\u5206\u70ba\u4e8c\uff0c\u5f62\u6210\u6578\u5217 1, 2, 4, 8, ..., 2^(n-1)\n while (i &lt; n) : (i += 1) {\n var j: i32 = 0;\n while (j &lt; bas) : (j += 1) {\n count += 1;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p></p> <p> \u5716 2-11 \u00a0 \u6307\u6578\u968e\u7684\u6642\u9593\u8907\u96dc\u5ea6 </p> <p>\u5728\u5be6\u969b\u6f14\u7b97\u6cd5\u4e2d\uff0c\u6307\u6578\u968e\u5e38\u51fa\u73fe\u65bc\u905e\u8ff4\u51fd\u5f0f\u4e2d\u3002\u4f8b\u5982\u5728\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u5176\u905e\u8ff4\u5730\u4e00\u5206\u70ba\u4e8c\uff0c\u7d93\u904e \\(n\\) \u6b21\u5206\u88c2\u5f8c\u505c\u6b62\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.py<pre><code>def exp_recur(n: int) -&gt; int:\n \"\"\"\u6307\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09\"\"\"\n if n == 1:\n return 1\n return exp_recur(n - 1) + exp_recur(n - 1) + 1\n</code></pre> time_complexity.cpp<pre><code>/* \u6307\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.java<pre><code>/* \u6307\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u6307\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint ExpRecur(int n) {\n if (n == 1) return 1;\n return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.go<pre><code>/* \u6307\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09*/\nfunc expRecur(n int) int {\n if n == 1 {\n return 1\n }\n return expRecur(n-1) + expRecur(n-1) + 1\n}\n</code></pre> time_complexity.swift<pre><code>/* \u6307\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunc expRecur(n: Int) -&gt; Int {\n if n == 1 {\n return 1\n }\n return expRecur(n: n - 1) + expRecur(n: n - 1) + 1\n}\n</code></pre> time_complexity.js<pre><code>/* \u6307\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunction expRecur(n) {\n if (n === 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u6307\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunction expRecur(n: number): number {\n if (n === 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u6307\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint expRecur(int n) {\n if (n == 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u6307\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfn exp_recur(n: i32) -&gt; i32 {\n if n == 1 {\n return 1;\n }\n exp_recur(n - 1) + exp_recur(n - 1) + 1\n}\n</code></pre> time_complexity.c<pre><code>/* \u6307\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.kt<pre><code>/* \u6307\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfun expRecur(n: Int): Int {\n if (n == 1) {\n return 1\n }\n return expRecur(n - 1) + expRecur(n - 1) + 1\n}\n</code></pre> time_complexity.rb<pre><code>### \u6307\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09###\ndef exp_recur(n)\n return 1 if n == 1\n exp_recur(n - 1) + exp_recur(n - 1) + 1\nend\n</code></pre> time_complexity.zig<pre><code>// \u6307\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09\nfn expRecur(n: i32) i32 {\n if (n == 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u6307\u6578\u968e\u589e\u9577\u975e\u5e38\u8fc5\u901f\uff0c\u5728\u7aae\u8209\u6cd5\uff08\u66b4\u529b\u641c\u5c0b\u3001\u56de\u6eaf\u7b49\uff09\u4e2d\u6bd4\u8f03\u5e38\u898b\u3002\u5c0d\u65bc\u8cc7\u6599\u898f\u6a21\u8f03\u5927\u7684\u554f\u984c\uff0c\u6307\u6578\u968e\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u52d5\u614b\u898f\u5283\u6216\u8caa\u5a6a\u6f14\u7b97\u6cd5\u7b49\u4f86\u89e3\u6c7a\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#5-olog-n","title":"5. \u00a0 \u5c0d\u6578\u968e \\(O(\\log n)\\)","text":"<p>\u8207\u6307\u6578\u968e\u76f8\u53cd\uff0c\u5c0d\u6578\u968e\u53cd\u6620\u4e86\u201c\u6bcf\u8f2a\u7e2e\u6e1b\u5230\u4e00\u534a\u201d\u7684\u60c5\u6cc1\u3002\u8a2d\u8f38\u5165\u8cc7\u6599\u5927\u5c0f\u70ba \\(n\\) \uff0c\u7531\u65bc\u6bcf\u8f2a\u7e2e\u6e1b\u5230\u4e00\u534a\uff0c\u56e0\u6b64\u8ff4\u5708\u6b21\u6578\u662f \\(\\log_2 n\\) \uff0c\u5373 \\(2^n\\) \u7684\u53cd\u51fd\u5f0f\u3002</p> <p>\u5716 2-12 \u548c\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u6a21\u64ec\u4e86\u201c\u6bcf\u8f2a\u7e2e\u6e1b\u5230\u4e00\u534a\u201d\u7684\u904e\u7a0b\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(\\log_2 n)\\) \uff0c\u7c21\u8a18\u70ba \\(O(\\log n)\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.py<pre><code>def logarithmic(n: int) -&gt; int:\n \"\"\"\u5c0d\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09\"\"\"\n count = 0\n while n &gt; 1:\n n = n / 2\n count += 1\n return count\n</code></pre> time_complexity.cpp<pre><code>/* \u5c0d\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nint logarithmic(int n) {\n int count = 0;\n while (n &gt; 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.java<pre><code>/* \u5c0d\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nint logarithmic(int n) {\n int count = 0;\n while (n &gt; 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u5c0d\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nint Logarithmic(int n) {\n int count = 0;\n while (n &gt; 1) {\n n /= 2;\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.go<pre><code>/* \u5c0d\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09*/\nfunc logarithmic(n int) int {\n count := 0\n for n &gt; 1 {\n n = n / 2\n count++\n }\n return count\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5c0d\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nfunc logarithmic(n: Int) -&gt; Int {\n var count = 0\n var n = n\n while n &gt; 1 {\n n = n / 2\n count += 1\n }\n return count\n}\n</code></pre> time_complexity.js<pre><code>/* \u5c0d\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nfunction logarithmic(n) {\n let count = 0;\n while (n &gt; 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5c0d\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nfunction logarithmic(n: number): number {\n let count = 0;\n while (n &gt; 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u5c0d\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nint logarithmic(int n) {\n int count = 0;\n while (n &gt; 1) {\n n = n ~/ 2;\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u5c0d\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nfn logarithmic(mut n: i32) -&gt; i32 {\n let mut count = 0;\n while n &gt; 1 {\n n = n / 2;\n count += 1;\n }\n count\n}\n</code></pre> time_complexity.c<pre><code>/* \u5c0d\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nint logarithmic(int n) {\n int count = 0;\n while (n &gt; 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.kt<pre><code>/* \u5c0d\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09 */\nfun logarithmic(n: Int): Int {\n var n1 = n\n var count = 0\n while (n1 &gt; 1) {\n n1 /= 2\n count++\n }\n return count\n}\n</code></pre> time_complexity.rb<pre><code>### \u5c0d\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09###\ndef logarithmic(n)\n count = 0\n\n while n &gt; 1\n n /= 2\n count += 1\n end\n\n count\nend\n</code></pre> time_complexity.zig<pre><code>// \u5c0d\u6578\u968e\uff08\u8ff4\u5708\u5be6\u73fe\uff09\nfn logarithmic(n: i32) i32 {\n var count: i32 = 0;\n var n_var = n;\n while (n_var &gt; 1)\n {\n n_var = n_var / 2;\n count +=1;\n }\n return count;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p></p> <p> \u5716 2-12 \u00a0 \u5c0d\u6578\u968e\u7684\u6642\u9593\u8907\u96dc\u5ea6 </p> <p>\u8207\u6307\u6578\u968e\u985e\u4f3c\uff0c\u5c0d\u6578\u968e\u4e5f\u5e38\u51fa\u73fe\u65bc\u905e\u8ff4\u51fd\u5f0f\u4e2d\u3002\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u5f62\u6210\u4e86\u4e00\u68f5\u9ad8\u5ea6\u70ba \\(\\log_2 n\\) \u7684\u905e\u8ff4\u6a39\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.py<pre><code>def log_recur(n: int) -&gt; int:\n \"\"\"\u5c0d\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09\"\"\"\n if n &lt;= 1:\n return 0\n return log_recur(n / 2) + 1\n</code></pre> time_complexity.cpp<pre><code>/* \u5c0d\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint logRecur(int n) {\n if (n &lt;= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.java<pre><code>/* \u5c0d\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint logRecur(int n) {\n if (n &lt;= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u5c0d\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint LogRecur(int n) {\n if (n &lt;= 1) return 0;\n return LogRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.go<pre><code>/* \u5c0d\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09*/\nfunc logRecur(n int) int {\n if n &lt;= 1 {\n return 0\n }\n return logRecur(n/2) + 1\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5c0d\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunc logRecur(n: Int) -&gt; Int {\n if n &lt;= 1 {\n return 0\n }\n return logRecur(n: n / 2) + 1\n}\n</code></pre> time_complexity.js<pre><code>/* \u5c0d\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunction logRecur(n) {\n if (n &lt;= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5c0d\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunction logRecur(n: number): number {\n if (n &lt;= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u5c0d\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint logRecur(int n) {\n if (n &lt;= 1) return 0;\n return logRecur(n ~/ 2) + 1;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u5c0d\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfn log_recur(n: i32) -&gt; i32 {\n if n &lt;= 1 {\n return 0;\n }\n log_recur(n / 2) + 1\n}\n</code></pre> time_complexity.c<pre><code>/* \u5c0d\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint logRecur(int n) {\n if (n &lt;= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.kt<pre><code>/* \u5c0d\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfun logRecur(n: Int): Int {\n if (n &lt;= 1)\n return 0\n return logRecur(n / 2) + 1\n}\n</code></pre> time_complexity.rb<pre><code>### \u5c0d\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09###\ndef log_recur(n)\n return 0 unless n &gt; 1\n log_recur(n / 2) + 1\nend\n</code></pre> time_complexity.zig<pre><code>// \u5c0d\u6578\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09\nfn logRecur(n: i32) i32 {\n if (n &lt;= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5c0d\u6578\u968e\u5e38\u51fa\u73fe\u65bc\u57fa\u65bc\u5206\u6cbb\u7b56\u7565\u7684\u6f14\u7b97\u6cd5\u4e2d\uff0c\u9ad4\u73fe\u4e86\u201c\u4e00\u5206\u70ba\u591a\u201d\u548c\u201c\u5316\u7e41\u70ba\u7c21\u201d\u7684\u6f14\u7b97\u6cd5\u601d\u60f3\u3002\u5b83\u589e\u9577\u7de9\u6162\uff0c\u662f\u50c5\u6b21\u65bc\u5e38\u6578\u968e\u7684\u7406\u60f3\u7684\u6642\u9593\u8907\u96dc\u5ea6\u3002</p> <p>\\(O(\\log n)\\) \u7684\u5e95\u6578\u662f\u591a\u5c11\uff1f</p> <p>\u6e96\u78ba\u4f86\u8aaa\uff0c\u201c\u4e00\u5206\u70ba \\(m\\)\u201d\u5c0d\u61c9\u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(O(\\log_m n)\\) \u3002\u800c\u900f\u904e\u5c0d\u6578\u63db\u5e95\u516c\u5f0f\uff0c\u6211\u5011\u53ef\u4ee5\u5f97\u5230\u5177\u6709\u4e0d\u540c\u5e95\u6578\u3001\u76f8\u7b49\u7684\u6642\u9593\u8907\u96dc\u5ea6\uff1a</p> \\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\] <p>\u4e5f\u5c31\u662f\u8aaa\uff0c\u5e95\u6578 \\(m\\) \u53ef\u4ee5\u5728\u4e0d\u5f71\u97ff\u8907\u96dc\u5ea6\u7684\u524d\u63d0\u4e0b\u8f49\u63db\u3002\u56e0\u6b64\u6211\u5011\u901a\u5e38\u6703\u7701\u7565\u5e95\u6578 \\(m\\) \uff0c\u5c07\u5c0d\u6578\u968e\u76f4\u63a5\u8a18\u70ba \\(O(\\log n)\\) \u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#6-on-log-n","title":"6. \u00a0 \u7dda\u6027\u5c0d\u6578\u968e \\(O(n \\log n)\\)","text":"<p>\u7dda\u6027\u5c0d\u6578\u968e\u5e38\u51fa\u73fe\u65bc\u5de2\u72c0\u8ff4\u5708\u4e2d\uff0c\u5169\u5c64\u8ff4\u5708\u7684\u6642\u9593\u8907\u96dc\u5ea6\u5206\u5225\u70ba \\(O(\\log n)\\) \u548c \\(O(n)\\) \u3002\u76f8\u95dc\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.py<pre><code>def linear_log_recur(n: int) -&gt; int:\n \"\"\"\u7dda\u6027\u5c0d\u6578\u968e\"\"\"\n if n &lt;= 1:\n return 1\n count: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)\n for _ in range(n):\n count += 1\n return count\n</code></pre> time_complexity.cpp<pre><code>/* \u7dda\u6027\u5c0d\u6578\u968e */\nint linearLogRecur(int n) {\n if (n &lt;= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i &lt; n; i++) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.java<pre><code>/* \u7dda\u6027\u5c0d\u6578\u968e */\nint linearLogRecur(int n) {\n if (n &lt;= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i &lt; n; i++) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u7dda\u6027\u5c0d\u6578\u968e */\nint LinearLogRecur(int n) {\n if (n &lt;= 1) return 1;\n int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);\n for (int i = 0; i &lt; n; i++) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.go<pre><code>/* \u7dda\u6027\u5c0d\u6578\u968e */\nfunc linearLogRecur(n int) int {\n if n &lt;= 1 {\n return 1\n }\n count := linearLogRecur(n/2) + linearLogRecur(n/2)\n for i := 0; i &lt; n; i++ {\n count++\n }\n return count\n}\n</code></pre> time_complexity.swift<pre><code>/* \u7dda\u6027\u5c0d\u6578\u968e */\nfunc linearLogRecur(n: Int) -&gt; Int {\n if n &lt;= 1 {\n return 1\n }\n var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)\n for _ in stride(from: 0, to: n, by: 1) {\n count += 1\n }\n return count\n}\n</code></pre> time_complexity.js<pre><code>/* \u7dda\u6027\u5c0d\u6578\u968e */\nfunction linearLogRecur(n) {\n if (n &lt;= 1) return 1;\n let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (let i = 0; i &lt; n; i++) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u7dda\u6027\u5c0d\u6578\u968e */\nfunction linearLogRecur(n: number): number {\n if (n &lt;= 1) return 1;\n let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (let i = 0; i &lt; n; i++) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u7dda\u6027\u5c0d\u6578\u968e */\nint linearLogRecur(int n) {\n if (n &lt;= 1) return 1;\n int count = linearLogRecur(n ~/ 2) + linearLogRecur(n ~/ 2);\n for (var i = 0; i &lt; n; i++) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u7dda\u6027\u5c0d\u6578\u968e */\nfn linear_log_recur(n: i32) -&gt; i32 {\n if n &lt;= 1 {\n return 1;\n }\n let mut count = linear_log_recur(n / 2) + linear_log_recur(n / 2);\n for _ in 0..n as i32 {\n count += 1;\n }\n return count;\n}\n</code></pre> time_complexity.c<pre><code>/* \u7dda\u6027\u5c0d\u6578\u968e */\nint linearLogRecur(int n) {\n if (n &lt;= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i &lt; n; i++) {\n count++;\n }\n return count;\n}\n</code></pre> time_complexity.kt<pre><code>/* \u7dda\u6027\u5c0d\u6578\u968e */\nfun linearLogRecur(n: Int): Int {\n if (n &lt;= 1)\n return 1\n var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)\n for (i in 0..&lt;n) {\n count++\n }\n return count\n}\n</code></pre> time_complexity.rb<pre><code>### \u7dda\u6027\u5c0d\u6578\u968e ###\ndef linear_log_recur(n)\n return 1 unless n &gt; 1\n\n count = linear_log_recur(n / 2) + linear_log_recur(n / 2)\n (0...n).each { count += 1 }\n\n count\nend\n</code></pre> time_complexity.zig<pre><code>// \u7dda\u6027\u5c0d\u6578\u968e\nfn linearLogRecur(n: i32) i32 {\n if (n &lt;= 1) return 1;\n var count: i32 = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n var i: i32 = 0;\n while (i &lt; n) : (i += 1) {\n count += 1;\n }\n return count;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 2-13 \u5c55\u793a\u4e86\u7dda\u6027\u5c0d\u6578\u968e\u7684\u751f\u6210\u65b9\u5f0f\u3002\u4e8c\u5143\u6a39\u7684\u6bcf\u4e00\u5c64\u7684\u64cd\u4f5c\u7e3d\u6578\u90fd\u70ba \\(n\\) \uff0c\u6a39\u5171\u6709 \\(\\log_2 n + 1\\) \u5c64\uff0c\u56e0\u6b64\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n \\log n)\\) \u3002</p> <p></p> <p> \u5716 2-13 \u00a0 \u7dda\u6027\u5c0d\u6578\u968e\u7684\u6642\u9593\u8907\u96dc\u5ea6 </p> <p>\u4e3b\u6d41\u6392\u5e8f\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u901a\u5e38\u70ba \\(O(n \\log n)\\) \uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\u3001\u5408\u4f75\u6392\u5e8f\u3001\u5806\u7a4d\u6392\u5e8f\u7b49\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#7-on","title":"7. \u00a0 \u968e\u4e58\u968e \\(O(n!)\\)","text":"<p>\u968e\u4e58\u968e\u5c0d\u61c9\u6578\u5b78\u4e0a\u7684\u201c\u5168\u6392\u5217\u201d\u554f\u984c\u3002\u7d66\u5b9a \\(n\\) \u500b\u4e92\u4e0d\u91cd\u8907\u7684\u5143\u7d20\uff0c\u6c42\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u65b9\u6848\uff0c\u65b9\u6848\u6578\u91cf\u70ba\uff1a</p> \\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\dots \\times 2 \\times 1 \\] <p>\u968e\u4e58\u901a\u5e38\u4f7f\u7528\u905e\u8ff4\u5be6\u73fe\u3002\u5982\u5716 2-14 \u548c\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u6240\u793a\uff0c\u7b2c\u4e00\u5c64\u5206\u88c2\u51fa \\(n\\) \u500b\uff0c\u7b2c\u4e8c\u5c64\u5206\u88c2\u51fa \\(n - 1\\) \u500b\uff0c\u4ee5\u6b64\u985e\u63a8\uff0c\u76f4\u81f3\u7b2c \\(n\\) \u5c64\u6642\u505c\u6b62\u5206\u88c2\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.py<pre><code>def factorial_recur(n: int) -&gt; int:\n \"\"\"\u968e\u4e58\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09\"\"\"\n if n == 0:\n return 1\n count = 0\n # \u5f9e 1 \u500b\u5206\u88c2\u51fa n \u500b\n for _ in range(n):\n count += factorial_recur(n - 1)\n return count\n</code></pre> time_complexity.cpp<pre><code>/* \u968e\u4e58\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n // \u5f9e 1 \u500b\u5206\u88c2\u51fa n \u500b\n for (int i = 0; i &lt; n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n</code></pre> time_complexity.java<pre><code>/* \u968e\u4e58\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n // \u5f9e 1 \u500b\u5206\u88c2\u51fa n \u500b\n for (int i = 0; i &lt; n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u968e\u4e58\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint FactorialRecur(int n) {\n if (n == 0) return 1;\n int count = 0;\n // \u5f9e 1 \u500b\u5206\u88c2\u51fa n \u500b\n for (int i = 0; i &lt; n; i++) {\n count += FactorialRecur(n - 1);\n }\n return count;\n}\n</code></pre> time_complexity.go<pre><code>/* \u968e\u4e58\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunc factorialRecur(n int) int {\n if n == 0 {\n return 1\n }\n count := 0\n // \u5f9e 1 \u500b\u5206\u88c2\u51fa n \u500b\n for i := 0; i &lt; n; i++ {\n count += factorialRecur(n - 1)\n }\n return count\n}\n</code></pre> time_complexity.swift<pre><code>/* \u968e\u4e58\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunc factorialRecur(n: Int) -&gt; Int {\n if n == 0 {\n return 1\n }\n var count = 0\n // \u5f9e 1 \u500b\u5206\u88c2\u51fa n \u500b\n for _ in 0 ..&lt; n {\n count += factorialRecur(n: n - 1)\n }\n return count\n}\n</code></pre> time_complexity.js<pre><code>/* \u968e\u4e58\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunction factorialRecur(n) {\n if (n === 0) return 1;\n let count = 0;\n // \u5f9e 1 \u500b\u5206\u88c2\u51fa n \u500b\n for (let i = 0; i &lt; n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u968e\u4e58\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfunction factorialRecur(n: number): number {\n if (n === 0) return 1;\n let count = 0;\n // \u5f9e 1 \u500b\u5206\u88c2\u51fa n \u500b\n for (let i = 0; i &lt; n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u968e\u4e58\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint factorialRecur(int n) {\n if (n == 0) return 1;\n int count = 0;\n // \u5f9e 1 \u500b\u5206\u88c2\u51fa n \u500b\n for (var i = 0; i &lt; n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u968e\u4e58\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfn factorial_recur(n: i32) -&gt; i32 {\n if n == 0 {\n return 1;\n }\n let mut count = 0;\n // \u5f9e 1 \u500b\u5206\u88c2\u51fa n \u500b\n for _ in 0..n {\n count += factorial_recur(n - 1);\n }\n count\n}\n</code></pre> time_complexity.c<pre><code>/* \u968e\u4e58\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n for (int i = 0; i &lt; n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n</code></pre> time_complexity.kt<pre><code>/* \u968e\u4e58\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09 */\nfun factorialRecur(n: Int): Int {\n if (n == 0)\n return 1\n var count = 0\n // \u5f9e 1 \u500b\u5206\u88c2\u51fa n \u500b\n for (i in 0..&lt;n) {\n count += factorialRecur(n - 1)\n }\n return count\n}\n</code></pre> time_complexity.rb<pre><code>### \u968e\u4e58\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09###\ndef factorial_recur(n)\n return 1 if n == 0\n\n count = 0\n # \u5f9e 1 \u500b\u5206\u88c2\u51fa n \u500b\n (0...n).each { count += factorial_recur(n - 1) }\n\n count\nend\n</code></pre> time_complexity.zig<pre><code>// \u968e\u4e58\u968e\uff08\u905e\u8ff4\u5be6\u73fe\uff09\nfn factorialRecur(n: i32) i32 {\n if (n == 0) return 1;\n var count: i32 = 0;\n var i: i32 = 0;\n // \u5f9e 1 \u500b\u5206\u88c2\u51fa n \u500b\n while (i &lt; n) : (i += 1) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p></p> <p> \u5716 2-14 \u00a0 \u968e\u4e58\u968e\u7684\u6642\u9593\u8907\u96dc\u5ea6 </p> <p>\u8acb\u6ce8\u610f\uff0c\u56e0\u70ba\u7576 \\(n \\geq 4\\) \u6642\u6046\u6709 \\(n! &gt; 2^n\\) \uff0c\u6240\u4ee5\u968e\u4e58\u968e\u6bd4\u6307\u6578\u968e\u589e\u9577\u5f97\u66f4\u5feb\uff0c\u5728 \\(n\\) \u8f03\u5927\u6642\u4e5f\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#235","title":"2.3.5 \u00a0 \u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6","text":"<p>\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u6548\u7387\u5f80\u5f80\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u662f\u8207\u8f38\u5165\u8cc7\u6599\u7684\u5206\u4f48\u6709\u95dc\u3002\u5047\u8a2d\u8f38\u5165\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u9663\u5217 <code>nums</code> \uff0c\u5176\u4e2d <code>nums</code> \u7531\u5f9e \\(1\\) \u81f3 \\(n\\) \u7684\u6578\u5b57\u7d44\u6210\uff0c\u6bcf\u500b\u6578\u5b57\u53ea\u51fa\u73fe\u4e00\u6b21\uff1b\u4f46\u5143\u7d20\u9806\u5e8f\u662f\u96a8\u6a5f\u6253\u4e82\u7684\uff0c\u4efb\u52d9\u76ee\u6a19\u662f\u8fd4\u56de\u5143\u7d20 \\(1\\) \u7684\u7d22\u5f15\u3002\u6211\u5011\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7d50\u8ad6\u3002</p> <ul> <li>\u7576 <code>nums = [?, ?, ..., 1]</code> \uff0c\u5373\u7576\u672b\u5c3e\u5143\u7d20\u662f \\(1\\) \u6642\uff0c\u9700\u8981\u5b8c\u6574\u8d70\u8a2a\u9663\u5217\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 \\(O(n)\\) \u3002</li> <li>\u7576 <code>nums = [1, ?, ?, ...]</code> \uff0c\u5373\u7576\u9996\u500b\u5143\u7d20\u70ba \\(1\\) \u6642\uff0c\u7121\u8ad6\u9663\u5217\u591a\u9577\u90fd\u4e0d\u9700\u8981\u7e7c\u7e8c\u8d70\u8a2a\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 \\(\\Omega(1)\\) \u3002</li> </ul> <p>\u201c\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6\u201d\u5c0d\u61c9\u51fd\u5f0f\u6f38\u8fd1\u4e0a\u754c\uff0c\u4f7f\u7528\u5927 \\(O\\) \u8a18\u865f\u8868\u793a\u3002\u76f8\u61c9\u5730\uff0c\u201c\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6\u201d\u5c0d\u61c9\u51fd\u5f0f\u6f38\u8fd1\u4e0b\u754c\uff0c\u7528 \\(\\Omega\\) \u8a18\u865f\u8868\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig worst_best_time_complexity.py<pre><code>def random_numbers(n: int) -&gt; list[int]:\n \"\"\"\u751f\u6210\u4e00\u500b\u9663\u5217\uff0c\u5143\u7d20\u70ba: 1, 2, ..., n \uff0c\u9806\u5e8f\u88ab\u6253\u4e82\"\"\"\n # \u751f\u6210\u9663\u5217 nums =: 1, 2, 3, ..., n\n nums = [i for i in range(1, n + 1)]\n # \u96a8\u6a5f\u6253\u4e82\u9663\u5217\u5143\u7d20\n random.shuffle(nums)\n return nums\n\ndef find_one(nums: list[int]) -&gt; int:\n \"\"\"\u67e5\u8a62\u9663\u5217 nums \u4e2d\u6578\u5b57 1 \u6240\u5728\u7d22\u5f15\"\"\"\n for i in range(len(nums)):\n # \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u982d\u90e8\u6642\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 O(1)\n # \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 O(n)\n if nums[i] == 1:\n return i\n return -1\n</code></pre> worst_best_time_complexity.cpp<pre><code>/* \u751f\u6210\u4e00\u500b\u9663\u5217\uff0c\u5143\u7d20\u70ba { 1, 2, ..., n }\uff0c\u9806\u5e8f\u88ab\u6253\u4e82 */\nvector&lt;int&gt; randomNumbers(int n) {\n vector&lt;int&gt; nums(n);\n // \u751f\u6210\u9663\u5217 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i &lt; n; i++) {\n nums[i] = i + 1;\n }\n // \u4f7f\u7528\u7cfb\u7d71\u6642\u9593\u751f\u6210\u96a8\u6a5f\u7a2e\u5b50\n unsigned seed = chrono::system_clock::now().time_since_epoch().count();\n // \u96a8\u6a5f\u6253\u4e82\u9663\u5217\u5143\u7d20\n shuffle(nums.begin(), nums.end(), default_random_engine(seed));\n return nums;\n}\n\n/* \u67e5\u8a62\u9663\u5217 nums \u4e2d\u6578\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(vector&lt;int&gt; &amp;nums) {\n for (int i = 0; i &lt; nums.size(); i++) {\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u982d\u90e8\u6642\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 O(1)\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n</code></pre> worst_best_time_complexity.java<pre><code>/* \u751f\u6210\u4e00\u500b\u9663\u5217\uff0c\u5143\u7d20\u70ba { 1, 2, ..., n }\uff0c\u9806\u5e8f\u88ab\u6253\u4e82 */\nint[] randomNumbers(int n) {\n Integer[] nums = new Integer[n];\n // \u751f\u6210\u9663\u5217 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i &lt; n; i++) {\n nums[i] = i + 1;\n }\n // \u96a8\u6a5f\u6253\u4e82\u9663\u5217\u5143\u7d20\n Collections.shuffle(Arrays.asList(nums));\n // Integer[] -&gt; int[]\n int[] res = new int[n];\n for (int i = 0; i &lt; n; i++) {\n res[i] = nums[i];\n }\n return res;\n}\n\n/* \u67e5\u8a62\u9663\u5217 nums \u4e2d\u6578\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums) {\n for (int i = 0; i &lt; nums.length; i++) {\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u982d\u90e8\u6642\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 O(1)\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n</code></pre> worst_best_time_complexity.cs<pre><code>/* \u751f\u6210\u4e00\u500b\u9663\u5217\uff0c\u5143\u7d20\u70ba { 1, 2, ..., n }\uff0c\u9806\u5e8f\u88ab\u6253\u4e82 */\nint[] RandomNumbers(int n) {\n int[] nums = new int[n];\n // \u751f\u6210\u9663\u5217 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i &lt; n; i++) {\n nums[i] = i + 1;\n }\n\n // \u96a8\u6a5f\u6253\u4e82\u9663\u5217\u5143\u7d20\n for (int i = 0; i &lt; nums.Length; i++) {\n int index = new Random().Next(i, nums.Length);\n (nums[i], nums[index]) = (nums[index], nums[i]);\n }\n return nums;\n}\n\n/* \u67e5\u8a62\u9663\u5217 nums \u4e2d\u6578\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint FindOne(int[] nums) {\n for (int i = 0; i &lt; nums.Length; i++) {\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u982d\u90e8\u6642\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 O(1)\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n</code></pre> worst_best_time_complexity.go<pre><code>/* \u751f\u6210\u4e00\u500b\u9663\u5217\uff0c\u5143\u7d20\u70ba { 1, 2, ..., n }\uff0c\u9806\u5e8f\u88ab\u6253\u4e82 */\nfunc randomNumbers(n int) []int {\n nums := make([]int, n)\n // \u751f\u6210\u9663\u5217 nums = { 1, 2, 3, ..., n }\n for i := 0; i &lt; n; i++ {\n nums[i] = i + 1\n }\n // \u96a8\u6a5f\u6253\u4e82\u9663\u5217\u5143\u7d20\n rand.Shuffle(len(nums), func(i, j int) {\n nums[i], nums[j] = nums[j], nums[i]\n })\n return nums\n}\n\n/* \u67e5\u8a62\u9663\u5217 nums \u4e2d\u6578\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums []int) int {\n for i := 0; i &lt; len(nums); i++ {\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u982d\u90e8\u6642\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 O(1)\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 O(n)\n if nums[i] == 1 {\n return i\n }\n }\n return -1\n}\n</code></pre> worst_best_time_complexity.swift<pre><code>/* \u751f\u6210\u4e00\u500b\u9663\u5217\uff0c\u5143\u7d20\u70ba { 1, 2, ..., n }\uff0c\u9806\u5e8f\u88ab\u6253\u4e82 */\nfunc randomNumbers(n: Int) -&gt; [Int] {\n // \u751f\u6210\u9663\u5217 nums = { 1, 2, 3, ..., n }\n var nums = Array(1 ... n)\n // \u96a8\u6a5f\u6253\u4e82\u9663\u5217\u5143\u7d20\n nums.shuffle()\n return nums\n}\n\n/* \u67e5\u8a62\u9663\u5217 nums \u4e2d\u6578\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums: [Int]) -&gt; Int {\n for i in nums.indices {\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u982d\u90e8\u6642\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 O(1)\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 O(n)\n if nums[i] == 1 {\n return i\n }\n }\n return -1\n}\n</code></pre> worst_best_time_complexity.js<pre><code>/* \u751f\u6210\u4e00\u500b\u9663\u5217\uff0c\u5143\u7d20\u70ba { 1, 2, ..., n }\uff0c\u9806\u5e8f\u88ab\u6253\u4e82 */\nfunction randomNumbers(n) {\n const nums = Array(n);\n // \u751f\u6210\u9663\u5217 nums = { 1, 2, 3, ..., n }\n for (let i = 0; i &lt; n; i++) {\n nums[i] = i + 1;\n }\n // \u96a8\u6a5f\u6253\u4e82\u9663\u5217\u5143\u7d20\n for (let i = 0; i &lt; n; i++) {\n const r = Math.floor(Math.random() * (i + 1));\n const temp = nums[i];\n nums[i] = nums[r];\n nums[r] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u8a62\u9663\u5217 nums \u4e2d\u6578\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums) {\n for (let i = 0; i &lt; nums.length; i++) {\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u982d\u90e8\u6642\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 O(1)\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 O(n)\n if (nums[i] === 1) {\n return i;\n }\n }\n return -1;\n}\n</code></pre> worst_best_time_complexity.ts<pre><code>/* \u751f\u6210\u4e00\u500b\u9663\u5217\uff0c\u5143\u7d20\u70ba { 1, 2, ..., n }\uff0c\u9806\u5e8f\u88ab\u6253\u4e82 */\nfunction randomNumbers(n: number): number[] {\n const nums = Array(n);\n // \u751f\u6210\u9663\u5217 nums = { 1, 2, 3, ..., n }\n for (let i = 0; i &lt; n; i++) {\n nums[i] = i + 1;\n }\n // \u96a8\u6a5f\u6253\u4e82\u9663\u5217\u5143\u7d20\n for (let i = 0; i &lt; n; i++) {\n const r = Math.floor(Math.random() * (i + 1));\n const temp = nums[i];\n nums[i] = nums[r];\n nums[r] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u8a62\u9663\u5217 nums \u4e2d\u6578\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums: number[]): number {\n for (let i = 0; i &lt; nums.length; i++) {\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u982d\u90e8\u6642\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 O(1)\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 O(n)\n if (nums[i] === 1) {\n return i;\n }\n }\n return -1;\n}\n</code></pre> worst_best_time_complexity.dart<pre><code>/* \u751f\u6210\u4e00\u500b\u9663\u5217\uff0c\u5143\u7d20\u70ba { 1, 2, ..., n }\uff0c\u9806\u5e8f\u88ab\u6253\u4e82 */\nList&lt;int&gt; randomNumbers(int n) {\n final nums = List.filled(n, 0);\n // \u751f\u6210\u9663\u5217 nums = { 1, 2, 3, ..., n }\n for (var i = 0; i &lt; n; i++) {\n nums[i] = i + 1;\n }\n // \u96a8\u6a5f\u6253\u4e82\u9663\u5217\u5143\u7d20\n nums.shuffle();\n\n return nums;\n}\n\n/* \u67e5\u8a62\u9663\u5217 nums \u4e2d\u6578\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(List&lt;int&gt; nums) {\n for (var i = 0; i &lt; nums.length; i++) {\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u982d\u90e8\u6642\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 O(1)\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 O(n)\n if (nums[i] == 1) return i;\n }\n\n return -1;\n}\n</code></pre> worst_best_time_complexity.rs<pre><code>/* \u751f\u6210\u4e00\u500b\u9663\u5217\uff0c\u5143\u7d20\u70ba { 1, 2, ..., n }\uff0c\u9806\u5e8f\u88ab\u6253\u4e82 */\nfn random_numbers(n: i32) -&gt; Vec&lt;i32&gt; {\n // \u751f\u6210\u9663\u5217 nums = { 1, 2, 3, ..., n }\n let mut nums = (1..=n).collect::&lt;Vec&lt;i32&gt;&gt;();\n // \u96a8\u6a5f\u6253\u4e82\u9663\u5217\u5143\u7d20\n nums.shuffle(&amp;mut thread_rng());\n nums\n}\n\n/* \u67e5\u8a62\u9663\u5217 nums \u4e2d\u6578\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfn find_one(nums: &amp;[i32]) -&gt; Option&lt;usize&gt; {\n for i in 0..nums.len() {\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u982d\u90e8\u6642\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 O(1)\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 O(n)\n if nums[i] == 1 {\n return Some(i);\n }\n }\n None\n}\n</code></pre> worst_best_time_complexity.c<pre><code>/* \u751f\u6210\u4e00\u500b\u9663\u5217\uff0c\u5143\u7d20\u70ba { 1, 2, ..., n }\uff0c\u9806\u5e8f\u88ab\u6253\u4e82 */\nint *randomNumbers(int n) {\n // \u5206\u914d\u5806\u7a4d\u5340\u8a18\u61b6\u9ad4\uff08\u5efa\u7acb\u4e00\u7dad\u53ef\u8b8a\u9577\u9663\u5217\uff1a\u9663\u5217\u4e2d\u5143\u7d20\u6578\u91cf\u70ba n \uff0c\u5143\u7d20\u578b\u5225\u70ba int \uff09\n int *nums = (int *)malloc(n * sizeof(int));\n // \u751f\u6210\u9663\u5217 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i &lt; n; i++) {\n nums[i] = i + 1;\n }\n // \u96a8\u6a5f\u6253\u4e82\u9663\u5217\u5143\u7d20\n for (int i = n - 1; i &gt; 0; i--) {\n int j = rand() % (i + 1);\n int temp = nums[i];\n nums[i] = nums[j];\n nums[j] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u8a62\u9663\u5217 nums \u4e2d\u6578\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int *nums, int n) {\n for (int i = 0; i &lt; n; i++) {\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u982d\u90e8\u6642\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 O(1)\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n</code></pre> worst_best_time_complexity.kt<pre><code>/* \u751f\u6210\u4e00\u500b\u9663\u5217\uff0c\u5143\u7d20\u70ba { 1, 2, ..., n }\uff0c\u9806\u5e8f\u88ab\u6253\u4e82 */\nfun randomNumbers(n: Int): Array&lt;Int?&gt; {\n val nums = IntArray(n)\n // \u751f\u6210\u9663\u5217 nums = { 1, 2, 3, ..., n }\n for (i in 0..&lt;n) {\n nums[i] = i + 1\n }\n // \u96a8\u6a5f\u6253\u4e82\u9663\u5217\u5143\u7d20\n nums.shuffle()\n val res = arrayOfNulls&lt;Int&gt;(n)\n for (i in 0..&lt;n) {\n res[i] = nums[i]\n }\n return res\n}\n\n/* \u67e5\u8a62\u9663\u5217 nums \u4e2d\u6578\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfun findOne(nums: Array&lt;Int?&gt;): Int {\n for (i in nums.indices) {\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u982d\u90e8\u6642\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 O(1)\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 O(n)\n if (nums[i] == 1)\n return i\n }\n return -1\n}\n</code></pre> worst_best_time_complexity.rb<pre><code>### \u751f\u6210\u4e00\u500b\u9663\u5217\uff0c\u5143\u7d20\u70ba: 1, 2, ..., n \uff0c\u9806\u5e8f\u88ab\u6253\u4e82 ###\ndef random_numbers(n)\n # \u751f\u6210\u9663\u5217 nums =: 1, 2, 3, ..., n\n nums = Array.new(n) { |i| i + 1 }\n # \u96a8\u6a5f\u6253\u4e82\u9663\u5217\u5143\u7d20\n nums.shuffle!\nend\n\n### \u67e5\u8a62\u9663\u5217 nums \u4e2d\u6578\u5b57 1 \u6240\u5728\u7d22\u5f15 ###\ndef find_one(nums)\n for i in 0...nums.length\n # \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u982d\u90e8\u6642\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 O(1)\n # \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 O(n)\n return i if nums[i] == 1\n end\n\n -1\nend\n</code></pre> worst_best_time_complexity.zig<pre><code>// \u751f\u6210\u4e00\u500b\u9663\u5217\uff0c\u5143\u7d20\u70ba { 1, 2, ..., n }\uff0c\u9806\u5e8f\u88ab\u6253\u4e82\nfn randomNumbers(comptime n: usize) [n]i32 {\n var nums: [n]i32 = undefined;\n // \u751f\u6210\u9663\u5217 nums = { 1, 2, 3, ..., n }\n for (&amp;nums, 0..) |*num, i| {\n num.* = @as(i32, @intCast(i)) + 1;\n }\n // \u96a8\u6a5f\u6253\u4e82\u9663\u5217\u5143\u7d20\n const rand = std.crypto.random;\n rand.shuffle(i32, &amp;nums);\n return nums;\n}\n\n// \u67e5\u8a62\u9663\u5217 nums \u4e2d\u6578\u5b57 1 \u6240\u5728\u7d22\u5f15\nfn findOne(nums: []i32) i32 {\n for (nums, 0..) |num, i| {\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u982d\u90e8\u6642\uff0c\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 O(1)\n // \u7576\u5143\u7d20 1 \u5728\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u9054\u5230\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6 O(n)\n if (num == 1) return @intCast(i);\n }\n return -1;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u503c\u5f97\u8aaa\u660e\u7684\u662f\uff0c\u6211\u5011\u5728\u5be6\u969b\u4e2d\u5f88\u5c11\u4f7f\u7528\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6\uff0c\u56e0\u70ba\u901a\u5e38\u53ea\u6709\u5728\u5f88\u5c0f\u6a5f\u7387\u4e0b\u624d\u80fd\u9054\u5230\uff0c\u53ef\u80fd\u6703\u5e36\u4f86\u4e00\u5b9a\u7684\u8aa4\u5c0e\u6027\u3002\u800c\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6\u66f4\u70ba\u5be6\u7528\uff0c\u56e0\u70ba\u5b83\u7d66\u51fa\u4e86\u4e00\u500b\u6548\u7387\u5b89\u5168\u503c\uff0c\u8b93\u6211\u5011\u53ef\u4ee5\u653e\u5fc3\u5730\u4f7f\u7528\u6f14\u7b97\u6cd5\u3002</p> <p>\u5f9e\u4e0a\u8ff0\u793a\u4f8b\u53ef\u4ee5\u770b\u51fa\uff0c\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6\u548c\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6\u53ea\u51fa\u73fe\u65bc\u201c\u7279\u6b8a\u7684\u8cc7\u6599\u5206\u4f48\u201d\uff0c\u9019\u4e9b\u60c5\u6cc1\u7684\u51fa\u73fe\u6a5f\u7387\u53ef\u80fd\u5f88\u5c0f\uff0c\u4e26\u4e0d\u80fd\u771f\u5be6\u5730\u53cd\u6620\u6f14\u7b97\u6cd5\u57f7\u884c\u6548\u7387\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u53ef\u4ee5\u9ad4\u73fe\u6f14\u7b97\u6cd5\u5728\u96a8\u6a5f\u8f38\u5165\u8cc7\u6599\u4e0b\u7684\u57f7\u884c\u6548\u7387\uff0c\u7528 \\(\\Theta\\) \u8a18\u865f\u4f86\u8868\u793a\u3002</p> <p>\u5c0d\u65bc\u90e8\u5206\u6f14\u7b97\u6cd5\uff0c\u6211\u5011\u53ef\u4ee5\u7c21\u55ae\u5730\u63a8\u7b97\u51fa\u96a8\u6a5f\u8cc7\u6599\u5206\u4f48\u4e0b\u7684\u5e73\u5747\u60c5\u6cc1\u3002\u6bd4\u5982\u4e0a\u8ff0\u793a\u4f8b\uff0c\u7531\u65bc\u8f38\u5165\u9663\u5217\u662f\u88ab\u6253\u4e82\u7684\uff0c\u56e0\u6b64\u5143\u7d20 \\(1\\) \u51fa\u73fe\u5728\u4efb\u610f\u7d22\u5f15\u7684\u6a5f\u7387\u90fd\u662f\u76f8\u7b49\u7684\uff0c\u90a3\u9ebc\u6f14\u7b97\u6cd5\u7684\u5e73\u5747\u8ff4\u5708\u6b21\u6578\u5c31\u662f\u9663\u5217\u9577\u5ea6\u7684\u4e00\u534a \\(n / 2\\) \uff0c\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(\\Theta(n / 2) = \\Theta(n)\\) \u3002</p> <p>\u4f46\u5c0d\u65bc\u8f03\u70ba\u8907\u96dc\u7684\u6f14\u7b97\u6cd5\uff0c\u8a08\u7b97\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u5f80\u5f80\u6bd4\u8f03\u56f0\u96e3\uff0c\u56e0\u70ba\u5f88\u96e3\u5206\u6790\u51fa\u5728\u8cc7\u6599\u5206\u4f48\u4e0b\u7684\u6574\u9ad4\u6578\u5b78\u671f\u671b\u3002\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u6211\u5011\u901a\u5e38\u4f7f\u7528\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6\u4f5c\u70ba\u6f14\u7b97\u6cd5\u6548\u7387\u7684\u8a55\u5224\u6a19\u6e96\u3002</p> <p>\u70ba\u4ec0\u9ebc\u5f88\u5c11\u770b\u5230 \\(\\Theta\\) \u7b26\u865f\uff1f</p> <p>\u53ef\u80fd\u7531\u65bc \\(O\\) \u7b26\u865f\u904e\u65bc\u6717\u6717\u4e0a\u53e3\uff0c\u56e0\u6b64\u6211\u5011\u5e38\u5e38\u4f7f\u7528\u5b83\u4f86\u8868\u793a\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u3002\u4f46\u5f9e\u56b4\u683c\u610f\u7fa9\u4e0a\u8b1b\uff0c\u9019\u7a2e\u505a\u6cd5\u4e26\u4e0d\u898f\u7bc4\u3002\u5728\u672c\u66f8\u548c\u5176\u4ed6\u8cc7\u6599\u4e2d\uff0c\u82e5\u9047\u5230\u985e\u4f3c\u201c\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6 \\(O(n)\\)\u201d\u7684\u8868\u8ff0\uff0c\u8acb\u5c07\u5176\u76f4\u63a5\u7406\u89e3\u70ba \\(\\Theta(n)\\) \u3002</p>"},{"location":"chapter_data_structure/","title":"\u7b2c 3 \u7ae0 \u00a0 \u8cc7\u6599\u7d50\u69cb","text":"<p>Abstract</p> <p>\u8cc7\u6599\u7d50\u69cb\u5982\u540c\u4e00\u526f\u7a69\u56fa\u800c\u591a\u6a23\u7684\u6846\u67b6\u3002</p> <p>\u5b83\u70ba\u8cc7\u6599\u7684\u6709\u5e8f\u7d44\u7e54\u63d0\u4f9b\u4e86\u85cd\u5716\uff0c\u6f14\u7b97\u6cd5\u5f97\u4ee5\u5728\u6b64\u57fa\u790e\u4e0a\u751f\u52d5\u8d77\u4f86\u3002</p>"},{"location":"chapter_data_structure/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>3.1 \u00a0 \u8cc7\u6599\u7d50\u69cb\u5206\u985e</li> <li>3.2 \u00a0 \u57fa\u672c\u8cc7\u6599\u578b\u5225</li> <li>3.3 \u00a0 \u6578\u5b57\u7de8\u78bc *</li> <li>3.4 \u00a0 \u5b57\u5143\u7de8\u78bc *</li> <li>3.5 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_data_structure/basic_data_types/","title":"3.2 \u00a0 \u57fa\u672c\u8cc7\u6599\u578b\u5225","text":"<p>\u7576\u8ac7\u53ca\u8a08\u7b97\u6a5f\u4e2d\u7684\u8cc7\u6599\u6642\uff0c\u6211\u5011\u6703\u60f3\u5230\u6587\u5b57\u3001\u5716\u7247\u3001\u5f71\u7247\u3001\u8a9e\u97f3\u30013D \u6a21\u578b\u7b49\u5404\u7a2e\u5f62\u5f0f\u3002\u5118\u7ba1\u9019\u4e9b\u8cc7\u6599\u7684\u7d44\u7e54\u5f62\u5f0f\u5404\u7570\uff0c\u4f46\u5b83\u5011\u90fd\u7531\u5404\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u69cb\u6210\u3002</p> <p>\u57fa\u672c\u8cc7\u6599\u578b\u5225\u662f CPU \u53ef\u4ee5\u76f4\u63a5\u9032\u884c\u904b\u7b97\u7684\u578b\u5225\uff0c\u5728\u6f14\u7b97\u6cd5\u4e2d\u76f4\u63a5\u88ab\u4f7f\u7528\uff0c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u5e7e\u7a2e\u3002</p> <ul> <li>\u6574\u6578\u578b\u5225 <code>byte</code>\u3001<code>short</code>\u3001<code>int</code>\u3001<code>long</code> \u3002</li> <li>\u6d6e\u9ede\u6578\u578b\u5225 <code>float</code>\u3001<code>double</code> \uff0c\u7528\u65bc\u8868\u793a\u5c0f\u6578\u3002</li> <li>\u5b57\u5143\u578b\u5225 <code>char</code> \uff0c\u7528\u65bc\u8868\u793a\u5404\u7a2e\u8a9e\u8a00\u7684\u5b57\u6bcd\u3001\u6a19\u9ede\u7b26\u865f\u751a\u81f3\u8868\u60c5\u7b26\u865f\u7b49\u3002</li> <li>\u5e03\u6797\u578b\u5225 <code>bool</code> \uff0c\u7528\u65bc\u8868\u793a\u201c\u662f\u201d\u8207\u201c\u5426\u201d\u5224\u65b7\u3002</li> </ul> <p>\u57fa\u672c\u8cc7\u6599\u578b\u5225\u4ee5\u4e8c\u9032\u4f4d\u5236\u7684\u5f62\u5f0f\u5132\u5b58\u5728\u8a08\u7b97\u6a5f\u4e2d\u3002\u4e00\u500b\u4e8c\u9032\u4f4d\u5236\u4f4d\u5373\u70ba \\(1\\) \u4f4d\u5143\u3002\u5728\u7d55\u5927\u591a\u6578\u73fe\u4ee3\u4f5c\u696d\u7cfb\u7d71\u4e2d\uff0c\\(1\\) \u4f4d\u5143\u7d44\uff08byte\uff09\u7531 \\(8\\) \u4f4d\u5143\uff08bit\uff09\u7d44\u6210\u3002</p> <p>\u57fa\u672c\u8cc7\u6599\u578b\u5225\u7684\u53d6\u503c\u7bc4\u570d\u53d6\u6c7a\u65bc\u5176\u4f54\u7528\u7684\u7a7a\u9593\u5927\u5c0f\u3002\u4e0b\u9762\u4ee5 Java \u70ba\u4f8b\u3002</p> <ul> <li>\u6574\u6578\u578b\u5225 <code>byte</code> \u4f54\u7528 \\(1\\) \u4f4d\u5143\u7d44 = \\(8\\) \u4f4d\u5143 \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{8}\\) \u500b\u6578\u5b57\u3002</li> <li>\u6574\u6578\u578b\u5225 <code>int</code> \u4f54\u7528 \\(4\\) \u4f4d\u5143\u7d44 = \\(32\\) \u4f4d\u5143 \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{32}\\) \u500b\u6578\u5b57\u3002</li> </ul> <p>\u8868 3-1 \u5217\u8209\u4e86 Java \u4e2d\u5404\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u7684\u4f54\u7528\u7a7a\u9593\u3001\u53d6\u503c\u7bc4\u570d\u548c\u9810\u8a2d\u503c\u3002\u6b64\u8868\u683c\u7121\u9808\u6b7b\u8a18\u786c\u80cc\uff0c\u5927\u81f4\u7406\u89e3\u5373\u53ef\uff0c\u9700\u8981\u6642\u53ef\u4ee5\u900f\u904e\u67e5\u8868\u4f86\u56de\u61b6\u3002</p> <p> \u8868 3-1 \u00a0 \u57fa\u672c\u8cc7\u6599\u578b\u5225\u7684\u4f54\u7528\u7a7a\u9593\u548c\u53d6\u503c\u7bc4\u570d </p> \u578b\u5225 \u7b26\u865f \u4f54\u7528\u7a7a\u9593 \u6700\u5c0f\u503c \u6700\u5927\u503c \u9810\u8a2d\u503c \u6574\u6578 <code>byte</code> 1 \u4f4d\u5143\u7d44 \\(-2^7\\) (\\(-128\\)) \\(2^7 - 1\\) (\\(127\\)) \\(0\\) <code>short</code> 2 \u4f4d\u5143\u7d44 \\(-2^{15}\\) \\(2^{15} - 1\\) \\(0\\) <code>int</code> 4 \u4f4d\u5143\u7d44 \\(-2^{31}\\) \\(2^{31} - 1\\) \\(0\\) <code>long</code> 8 \u4f4d\u5143\u7d44 \\(-2^{63}\\) \\(2^{63} - 1\\) \\(0\\) \u6d6e\u9ede\u6578 <code>float</code> 4 \u4f4d\u5143\u7d44 \\(1.175 \\times 10^{-38}\\) \\(3.403 \\times 10^{38}\\) \\(0.0\\text{f}\\) <code>double</code> 8 \u4f4d\u5143\u7d44 \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) \\(0.0\\) \u5b57\u5143 <code>char</code> 2 \u4f4d\u5143\u7d44 \\(0\\) \\(2^{16} - 1\\) \\(0\\) \u5e03\u6797 <code>bool</code> 1 \u4f4d\u5143\u7d44 \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\) <p>\u8acb\u6ce8\u610f\uff0c\u8868 3-1 \u91dd\u5c0d\u7684\u662f Java \u7684\u57fa\u672c\u8cc7\u6599\u578b\u5225\u7684\u60c5\u6cc1\u3002\u6bcf\u7a2e\u7a0b\u5f0f\u8a9e\u8a00\u90fd\u6709\u5404\u81ea\u7684\u8cc7\u6599\u578b\u5225\u5b9a\u7fa9\uff0c\u5b83\u5011\u7684\u4f54\u7528\u7a7a\u9593\u3001\u53d6\u503c\u7bc4\u570d\u548c\u9810\u8a2d\u503c\u53ef\u80fd\u6703\u6709\u6240\u4e0d\u540c\u3002</p> <ul> <li>\u5728 Python \u4e2d\uff0c\u6574\u6578\u578b\u5225 <code>int</code> \u53ef\u4ee5\u662f\u4efb\u610f\u5927\u5c0f\uff0c\u53ea\u53d7\u9650\u65bc\u53ef\u7528\u8a18\u61b6\u9ad4\uff1b\u6d6e\u9ede\u6578 <code>float</code> \u662f\u96d9\u7cbe\u5ea6 64 \u4f4d\uff1b\u6c92\u6709 <code>char</code> \u578b\u5225\uff0c\u55ae\u500b\u5b57\u5143\u5be6\u969b\u4e0a\u662f\u9577\u5ea6\u70ba 1 \u7684\u5b57\u4e32 <code>str</code> \u3002</li> <li>C \u548c C++ \u672a\u660e\u78ba\u898f\u5b9a\u57fa\u672c\u8cc7\u6599\u578b\u5225\u7684\u5927\u5c0f\uff0c\u800c\u56e0\u5be6\u73fe\u548c\u5e73\u81fa\u5404\u7570\u3002\u8868 3-1 \u9075\u5faa LP64 \u8cc7\u6599\u6a21\u578b\uff0c\u5176\u7528\u65bc\u5305\u62ec Linux \u548c macOS \u5728\u5167\u7684 Unix 64 \u4f4d\u4f5c\u696d\u7cfb\u7d71\u3002</li> <li>\u5b57\u5143 <code>char</code> \u7684\u5927\u5c0f\u5728 C \u548c C++ \u4e2d\u70ba 1 \u4f4d\u5143\u7d44\uff0c\u5728\u5927\u591a\u6578\u7a0b\u5f0f\u8a9e\u8a00\u4e2d\u53d6\u6c7a\u65bc\u7279\u5b9a\u7684\u5b57\u5143\u7de8\u78bc\u65b9\u6cd5\uff0c\u8a73\u898b\u201c\u5b57\u5143\u7de8\u78bc\u201d\u7ae0\u7bc0\u3002</li> <li>\u5373\u4f7f\u8868\u793a\u5e03\u6797\u91cf\u50c5\u9700 1 \u4f4d\uff08\\(0\\) \u6216 \\(1\\)\uff09\uff0c\u5b83\u5728\u8a18\u61b6\u9ad4\u4e2d\u901a\u5e38\u4e5f\u5132\u5b58\u70ba 1 \u4f4d\u5143\u7d44\u3002\u9019\u662f\u56e0\u70ba\u73fe\u4ee3\u8a08\u7b97\u6a5f CPU \u901a\u5e38\u5c07 1 \u4f4d\u5143\u7d44\u4f5c\u70ba\u6700\u5c0f\u5b9a\u5740\u8a18\u61b6\u9ad4\u55ae\u5143\u3002</li> </ul> <p>\u90a3\u9ebc\uff0c\u57fa\u672c\u8cc7\u6599\u578b\u5225\u8207\u8cc7\u6599\u7d50\u69cb\u4e4b\u9593\u6709\u4ec0\u9ebc\u95dc\u806f\u5462\uff1f\u6211\u5011\u77e5\u9053\uff0c\u8cc7\u6599\u7d50\u69cb\u662f\u5728\u8a08\u7b97\u6a5f\u4e2d\u7d44\u7e54\u8207\u5132\u5b58\u8cc7\u6599\u7684\u65b9\u5f0f\u3002\u9019\u53e5\u8a71\u7684\u4e3b\u8a9e\u662f\u201c\u7d50\u69cb\u201d\u800c\u975e\u201c\u8cc7\u6599\u201d\u3002</p> <p>\u5982\u679c\u60f3\u8868\u793a\u201c\u4e00\u6392\u6578\u5b57\u201d\uff0c\u6211\u5011\u81ea\u7136\u6703\u60f3\u5230\u4f7f\u7528\u9663\u5217\u3002\u9019\u662f\u56e0\u70ba\u9663\u5217\u7684\u7dda\u6027\u7d50\u69cb\u53ef\u4ee5\u8868\u793a\u6578\u5b57\u7684\u76f8\u9130\u95dc\u4fc2\u548c\u9806\u5e8f\u95dc\u4fc2\uff0c\u4f46\u81f3\u65bc\u5132\u5b58\u7684\u5167\u5bb9\u662f\u6574\u6578 <code>int</code>\u3001\u5c0f\u6578 <code>float</code> \u9084\u662f\u5b57\u5143 <code>char</code> \uff0c\u5247\u8207\u201c\u8cc7\u6599\u7d50\u69cb\u201d\u7121\u95dc\u3002</p> <p>\u63db\u53e5\u8a71\u8aaa\uff0c\u57fa\u672c\u8cc7\u6599\u578b\u5225\u63d0\u4f9b\u4e86\u8cc7\u6599\u7684\u201c\u5167\u5bb9\u578b\u5225\u201d\uff0c\u800c\u8cc7\u6599\u7d50\u69cb\u63d0\u4f9b\u4e86\u8cc7\u6599\u7684\u201c\u7d44\u7e54\u65b9\u5f0f\u201d\u3002\u4f8b\u5982\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff0c\u6211\u5011\u7528\u76f8\u540c\u7684\u8cc7\u6599\u7d50\u69cb\uff08\u9663\u5217\uff09\u4f86\u5132\u5b58\u8207\u8868\u793a\u4e0d\u540c\u7684\u57fa\u672c\u8cc7\u6599\u578b\u5225\uff0c\u5305\u62ec <code>int</code>\u3001<code>float</code>\u3001<code>char</code>\u3001<code>bool</code> \u7b49\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code># \u4f7f\u7528\u591a\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u4f86\u521d\u59cb\u5316\u9663\u5217\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# Python \u7684\u5b57\u5143\u5be6\u969b\u4e0a\u662f\u9577\u5ea6\u70ba 1 \u7684\u5b57\u4e32\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# Python \u7684\u4e32\u5217\u53ef\u4ee5\u81ea\u7531\u5132\u5b58\u5404\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u548c\u7269\u4ef6\u5f15\u7528\ndata = [0, 0.0, 'a', False, ListNode(0)]\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u4f86\u521d\u59cb\u5316\u9663\u5217\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u4f86\u521d\u59cb\u5316\u9663\u5217\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u4f86\u521d\u59cb\u5316\u9663\u5217\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u4f86\u521d\u59cb\u5316\u9663\u5217\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u4f86\u521d\u59cb\u5316\u9663\u5217\nlet numbers = Array(repeating: 0, count: 5)\nlet decimals = Array(repeating: 0.0, count: 5)\nlet characters: [Character] = Array(repeating: \"a\", count: 5)\nlet bools = Array(repeating: false, count: 5)\n</code></pre> <pre><code>// JavaScript \u7684\u9663\u5217\u53ef\u4ee5\u81ea\u7531\u5132\u5b58\u5404\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u548c\u7269\u4ef6\nconst array = [0, 0.0, 'a', false];\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u4f86\u521d\u59cb\u5316\u9663\u5217\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u4f86\u521d\u59cb\u5316\u9663\u5217\nList&lt;int&gt; numbers = List.filled(5, 0);\nList&lt;double&gt; decimals = List.filled(5, 0.0);\nList&lt;String&gt; characters = List.filled(5, 'a');\nList&lt;bool&gt; bools = List.filled(5, false);\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u4f86\u521d\u59cb\u5316\u9663\u5217\nlet numbers: Vec&lt;i32&gt; = vec![0; 5];\nlet decimals: Vec&lt;f32&gt; = vec![0.0; 5];\nlet characters: Vec&lt;char&gt; = vec!['0'; 5];\nlet bools: Vec&lt;bool&gt; = vec![false; 5];\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u4f86\u521d\u59cb\u5316\u9663\u5217\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u4f86\u521d\u59cb\u5316\u9663\u5217\nval numbers = IntArray(5)\nval decinals = FloatArray(5)\nval characters = CharArray(5)\nval bools = BooleanArray(5)\n</code></pre> <pre><code># Ruby \u7684\u4e32\u5217\u53ef\u4ee5\u81ea\u7531\u5132\u5b58\u5404\u7a2e\u57fa\u672c\u8cc7\u6599\u578b\u5225\u548c\u7269\u4ef6\u5f15\u7528\ndata = [0, 0.0, 'a', false, ListNode(0)]\n</code></pre> <pre><code>\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_data_structure/character_encoding/","title":"3.4 \u00a0 \u5b57\u5143\u7de8\u78bc *","text":"<p>\u5728\u8a08\u7b97\u6a5f\u4e2d\uff0c\u6240\u6709\u8cc7\u6599\u90fd\u662f\u4ee5\u4e8c\u9032\u4f4d\u5236\u6578\u7684\u5f62\u5f0f\u5132\u5b58\u7684\uff0c\u5b57\u5143 <code>char</code> \u4e5f\u4e0d\u4f8b\u5916\u3002\u70ba\u4e86\u8868\u793a\u5b57\u5143\uff0c\u6211\u5011\u9700\u8981\u5efa\u7acb\u4e00\u5957\u201c\u5b57\u7b26\u96c6\u201d\uff0c\u898f\u5b9a\u6bcf\u500b\u5b57\u5143\u548c\u4e8c\u9032\u4f4d\u5236\u6578\u4e4b\u9593\u7684\u4e00\u4e00\u5c0d\u61c9\u95dc\u4fc2\u3002\u6709\u4e86\u5b57\u7b26\u96c6\u4e4b\u5f8c\uff0c\u8a08\u7b97\u6a5f\u5c31\u53ef\u4ee5\u900f\u904e\u67e5\u8868\u5b8c\u6210\u4e8c\u9032\u4f4d\u5236\u6578\u5230\u5b57\u5143\u7684\u8f49\u63db\u3002</p>"},{"location":"chapter_data_structure/character_encoding/#341-ascii","title":"3.4.1 \u00a0 ASCII \u5b57\u7b26\u96c6","text":"<p>ASCII \u78bc\u662f\u6700\u65e9\u51fa\u73fe\u7684\u5b57\u7b26\u96c6\uff0c\u5176\u5168\u7a31\u70ba American Standard Code for Information Interchange\uff08\u7f8e\u570b\u6a19\u6e96\u8cc7\u8a0a\u4ea4\u63db\u7a0b\u5f0f\u78bc\uff09\u3002\u5b83\u4f7f\u7528 7 \u4f4d\u4e8c\u9032\u4f4d\u5236\u6578\uff08\u4e00\u500b\u4f4d\u5143\u7d44\u7684\u4f4e 7 \u4f4d\uff09\u8868\u793a\u4e00\u500b\u5b57\u5143\uff0c\u6700\u591a\u80fd\u5920\u8868\u793a 128 \u500b\u4e0d\u540c\u7684\u5b57\u5143\u3002\u5982\u5716 3-6 \u6240\u793a\uff0cASCII \u78bc\u5305\u62ec\u82f1\u6587\u5b57\u6bcd\u7684\u5927\u5c0f\u5beb\u3001\u6578\u5b57 0 ~ 9\u3001\u4e00\u4e9b\u6a19\u9ede\u7b26\u865f\uff0c\u4ee5\u53ca\u4e00\u4e9b\u63a7\u5236\u5b57\u5143\uff08\u5982\u63db\u884c\u7b26\u548c\u88fd\u8868\u7b26\uff09\u3002</p> <p></p> <p> \u5716 3-6 \u00a0 ASCII \u78bc </p> <p>\u7136\u800c\uff0cASCII \u78bc\u50c5\u80fd\u5920\u8868\u793a\u82f1\u6587\u3002\u96a8\u8457\u8a08\u7b97\u6a5f\u7684\u5168\u7403\u5316\uff0c\u8a95\u751f\u4e86\u4e00\u7a2e\u80fd\u5920\u8868\u793a\u66f4\u591a\u8a9e\u8a00\u7684 EASCII \u5b57\u7b26\u96c6\u3002\u5b83\u5728 ASCII \u7684 7 \u4f4d\u57fa\u790e\u4e0a\u64f4\u5c55\u5230 8 \u4f4d\uff0c\u80fd\u5920\u8868\u793a 256 \u500b\u4e0d\u540c\u7684\u5b57\u5143\u3002</p> <p>\u5728\u4e16\u754c\u7bc4\u570d\u5167\uff0c\u9678\u7e8c\u51fa\u73fe\u4e86\u4e00\u6279\u9069\u7528\u65bc\u4e0d\u540c\u5730\u5340\u7684 EASCII \u5b57\u7b26\u96c6\u3002\u9019\u4e9b\u5b57\u7b26\u96c6\u7684\u524d 128 \u500b\u5b57\u5143\u7d71\u4e00\u70ba ASCII \u78bc\uff0c\u5f8c 128 \u500b\u5b57\u5143\u5b9a\u7fa9\u4e0d\u540c\uff0c\u4ee5\u9069\u61c9\u4e0d\u540c\u8a9e\u8a00\u7684\u9700\u6c42\u3002</p>"},{"location":"chapter_data_structure/character_encoding/#342-gbk","title":"3.4.2 \u00a0 GBK \u5b57\u7b26\u96c6","text":"<p>\u5f8c\u4f86\u4eba\u5011\u767c\u73fe\uff0cEASCII \u78bc\u4ecd\u7136\u7121\u6cd5\u6eff\u8db3\u8a31\u591a\u8a9e\u8a00\u7684\u5b57\u5143\u6578\u91cf\u8981\u6c42\u3002\u6bd4\u5982\u6f22\u5b57\u6709\u8fd1\u5341\u842c\u500b\uff0c\u5149\u65e5\u5e38\u4f7f\u7528\u7684\u5c31\u6709\u5e7e\u5343\u500b\u3002\u4e2d\u570b\u570b\u5bb6\u6a19\u6e96\u7e3d\u5c40\u65bc 1980 \u5e74\u91cb\u51fa\u4e86 GB2312 \u5b57\u7b26\u96c6\uff0c\u5176\u6536\u9304\u4e86 6763 \u500b\u6f22\u5b57\uff0c\u57fa\u672c\u6eff\u8db3\u4e86\u6f22\u5b57\u7684\u8a08\u7b97\u6a5f\u8655\u7406\u9700\u8981\u3002</p> <p>\u7136\u800c\uff0cGB2312 \u7121\u6cd5\u8655\u7406\u90e8\u5206\u7f55\u898b\u5b57\u548c\u7e41\u9ad4\u5b57\u3002GBK \u5b57\u7b26\u96c6\u662f\u5728 GB2312 \u7684\u57fa\u790e\u4e0a\u64f4\u5c55\u5f97\u5230\u7684\uff0c\u5b83\u5171\u6536\u9304\u4e86 21886 \u500b\u6f22\u5b57\u3002\u5728 GBK \u7684\u7de8\u78bc\u65b9\u6848\u4e2d\uff0cASCII \u5b57\u5143\u4f7f\u7528\u4e00\u500b\u4f4d\u5143\u7d44\u8868\u793a\uff0c\u6f22\u5b57\u4f7f\u7528\u5169\u500b\u4f4d\u5143\u7d44\u8868\u793a\u3002</p>"},{"location":"chapter_data_structure/character_encoding/#343-unicode","title":"3.4.3 \u00a0 Unicode \u5b57\u7b26\u96c6","text":"<p>\u96a8\u8457\u8a08\u7b97\u6a5f\u6280\u8853\u7684\u84ec\u52c3\u767c\u5c55\uff0c\u5b57\u7b26\u96c6\u8207\u7de8\u78bc\u6a19\u6e96\u767e\u82b1\u9f4a\u653e\uff0c\u800c\u9019\u5e36\u4f86\u4e86\u8a31\u591a\u554f\u984c\u3002\u4e00\u65b9\u9762\uff0c\u9019\u4e9b\u5b57\u7b26\u96c6\u4e00\u822c\u53ea\u5b9a\u7fa9\u4e86\u7279\u5b9a\u8a9e\u8a00\u7684\u5b57\u5143\uff0c\u7121\u6cd5\u5728\u591a\u8a9e\u8a00\u74b0\u5883\u4e0b\u6b63\u5e38\u5de5\u4f5c\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u540c\u4e00\u7a2e\u8a9e\u8a00\u5b58\u5728\u591a\u7a2e\u5b57\u7b26\u96c6\u6a19\u6e96\uff0c\u5982\u679c\u5169\u81fa\u8a08\u7b97\u6a5f\u4f7f\u7528\u7684\u662f\u4e0d\u540c\u7684\u7de8\u78bc\u6a19\u6e96\uff0c\u5247\u5728\u8cc7\u8a0a\u50b3\u905e\u6642\u5c31\u6703\u51fa\u73fe\u4e82\u78bc\u3002</p> <p>\u90a3\u500b\u6642\u4ee3\u7684\u7814\u7a76\u4eba\u54e1\u5c31\u5728\u60f3\uff1a\u5982\u679c\u63a8\u51fa\u4e00\u500b\u8db3\u5920\u5b8c\u6574\u7684\u5b57\u7b26\u96c6\uff0c\u5c07\u4e16\u754c\u7bc4\u570d\u5167\u7684\u6240\u6709\u8a9e\u8a00\u548c\u7b26\u865f\u90fd\u6536\u9304\u5176\u4e2d\uff0c\u4e0d\u5c31\u53ef\u4ee5\u89e3\u6c7a\u8de8\u8a9e\u8a00\u74b0\u5883\u548c\u4e82\u78bc\u554f\u984c\u4e86\u55ce\uff1f\u5728\u9019\u7a2e\u60f3\u6cd5\u7684\u9a45\u52d5\u4e0b\uff0c\u4e00\u500b\u5927\u800c\u5168\u7684\u5b57\u7b26\u96c6 Unicode \u61c9\u904b\u800c\u751f\u3002</p> <p>Unicode \u7684\u4e2d\u6587\u540d\u7a31\u70ba\u201c\u7d71\u4e00\u78bc\u201d\uff0c\u7406\u8ad6\u4e0a\u80fd\u5bb9\u7d0d 100 \u591a\u842c\u500b\u5b57\u5143\u3002\u5b83\u81f4\u529b\u65bc\u5c07\u5168\u7403\u7bc4\u570d\u5167\u7684\u5b57\u5143\u7d0d\u5165\u7d71\u4e00\u7684\u5b57\u7b26\u96c6\u4e4b\u4e2d\uff0c\u63d0\u4f9b\u4e00\u7a2e\u901a\u7528\u7684\u5b57\u7b26\u96c6\u4f86\u8655\u7406\u548c\u986f\u793a\u5404\u7a2e\u8a9e\u8a00\u6587\u5b57\uff0c\u6e1b\u5c11\u56e0\u70ba\u7de8\u78bc\u6a19\u6e96\u4e0d\u540c\u800c\u7522\u751f\u7684\u4e82\u78bc\u554f\u984c\u3002</p> <p>\u81ea 1991 \u5e74\u91cb\u51fa\u4ee5\u4f86\uff0cUnicode \u4e0d\u65b7\u64f4\u5145\u65b0\u7684\u8a9e\u8a00\u8207\u5b57\u5143\u3002\u622a\u81f3 2022 \u5e74 9 \u6708\uff0cUnicode \u5df2\u7d93\u5305\u542b 149186 \u500b\u5b57\u5143\uff0c\u5305\u62ec\u5404\u7a2e\u8a9e\u8a00\u7684\u5b57\u5143\u3001\u7b26\u865f\u751a\u81f3\u8868\u60c5\u7b26\u865f\u7b49\u3002\u5728\u9f90\u5927\u7684 Unicode \u5b57\u7b26\u96c6\u4e2d\uff0c\u5e38\u7528\u7684\u5b57\u5143\u4f54\u7528 2 \u4f4d\u5143\u7d44\uff0c\u6709\u4e9b\u751f\u50fb\u7684\u5b57\u5143\u4f54\u7528 3 \u4f4d\u5143\u7d44\u751a\u81f3 4 \u4f4d\u5143\u7d44\u3002</p> <p>Unicode \u662f\u4e00\u7a2e\u901a\u7528\u5b57\u7b26\u96c6\uff0c\u672c\u8cea\u4e0a\u662f\u7d66\u6bcf\u500b\u5b57\u5143\u5206\u914d\u4e00\u500b\u7de8\u865f\uff08\u7a31\u70ba\u201c\u78bc\u9ede\u201d\uff09\uff0c\u4f46\u5b83\u4e26\u6c92\u6709\u898f\u5b9a\u5728\u8a08\u7b97\u6a5f\u4e2d\u5982\u4f55\u5132\u5b58\u9019\u4e9b\u5b57\u5143\u78bc\u9ede\u3002\u6211\u5011\u4e0d\u7981\u6703\u554f\uff1a\u7576\u591a\u7a2e\u9577\u5ea6\u7684 Unicode \u78bc\u9ede\u540c\u6642\u51fa\u73fe\u5728\u4e00\u500b\u6587\u5b57\u4e2d\u6642\uff0c\u7cfb\u7d71\u5982\u4f55\u89e3\u6790\u5b57\u5143\uff1f\u4f8b\u5982\u7d66\u5b9a\u4e00\u500b\u9577\u5ea6\u70ba 2 \u4f4d\u5143\u7d44\u7684\u7de8\u78bc\uff0c\u7cfb\u7d71\u5982\u4f55\u78ba\u8a8d\u5b83\u662f\u4e00\u500b 2 \u4f4d\u5143\u7d44\u7684\u5b57\u5143\u9084\u662f\u5169\u500b 1 \u4f4d\u5143\u7d44\u7684\u5b57\u5143\uff1f</p> <p>\u5c0d\u65bc\u4ee5\u4e0a\u554f\u984c\uff0c\u4e00\u7a2e\u76f4\u63a5\u7684\u89e3\u6c7a\u65b9\u6848\u662f\u5c07\u6240\u6709\u5b57\u5143\u5132\u5b58\u70ba\u7b49\u9577\u7684\u7de8\u78bc\u3002\u5982\u5716 3-7 \u6240\u793a\uff0c\u201cHello\u201d\u4e2d\u7684\u6bcf\u500b\u5b57\u5143\u4f54\u7528 1 \u4f4d\u5143\u7d44\uff0c\u201c\u6f14\u7b97\u6cd5\u201d\u4e2d\u7684\u6bcf\u500b\u5b57\u5143\u4f54\u7528 2 \u4f4d\u5143\u7d44\u3002\u6211\u5011\u53ef\u4ee5\u900f\u904e\u9ad8\u4f4d\u586b 0 \u5c07\u201cHello \u6f14\u7b97\u6cd5\u201d\u4e2d\u7684\u6240\u6709\u5b57\u5143\u90fd\u7de8\u78bc\u70ba 2 \u4f4d\u5143\u7d44\u9577\u5ea6\u3002\u9019\u6a23\u7cfb\u7d71\u5c31\u53ef\u4ee5\u6bcf\u9694 2 \u4f4d\u5143\u7d44\u89e3\u6790\u4e00\u500b\u5b57\u5143\uff0c\u6062\u5fa9\u9019\u500b\u77ed\u8a9e\u7684\u5167\u5bb9\u4e86\u3002</p> <p></p> <p> \u5716 3-7 \u00a0 Unicode \u7de8\u78bc\u793a\u4f8b </p> <p>\u7136\u800c ASCII \u78bc\u5df2\u7d93\u5411\u6211\u5011\u8b49\u660e\uff0c\u7de8\u78bc\u82f1\u6587\u53ea\u9700 1 \u4f4d\u5143\u7d44\u3002\u82e5\u63a1\u7528\u4e0a\u8ff0\u65b9\u6848\uff0c\u82f1\u6587\u6587\u5b57\u4f54\u7528\u7a7a\u9593\u7684\u5927\u5c0f\u5c07\u6703\u662f ASCII \u7de8\u78bc\u4e0b\u7684\u5169\u500d\uff0c\u975e\u5e38\u6d6a\u8cbb\u8a18\u61b6\u9ad4\u7a7a\u9593\u3002\u56e0\u6b64\uff0c\u6211\u5011\u9700\u8981\u4e00\u7a2e\u66f4\u52a0\u9ad8\u6548\u7684 Unicode \u7de8\u78bc\u65b9\u6cd5\u3002</p>"},{"location":"chapter_data_structure/character_encoding/#344-utf-8","title":"3.4.4 \u00a0 UTF-8 \u7de8\u78bc","text":"<p>\u76ee\u524d\uff0cUTF-8 \u5df2\u6210\u70ba\u570b\u969b\u4e0a\u4f7f\u7528\u6700\u5ee3\u6cdb\u7684 Unicode \u7de8\u78bc\u65b9\u6cd5\u3002\u5b83\u662f\u4e00\u7a2e\u53ef\u8b8a\u9577\u5ea6\u7684\u7de8\u78bc\uff0c\u4f7f\u7528 1 \u5230 4 \u4f4d\u5143\u7d44\u4f86\u8868\u793a\u4e00\u500b\u5b57\u5143\uff0c\u6839\u64da\u5b57\u5143\u7684\u8907\u96dc\u6027\u800c\u8b8a\u3002ASCII \u5b57\u5143\u53ea\u9700 1 \u4f4d\u5143\u7d44\uff0c\u62c9\u4e01\u5b57\u6bcd\u548c\u5e0c\u81d8\u5b57\u6bcd\u9700\u8981 2 \u4f4d\u5143\u7d44\uff0c\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u5143\u9700\u8981 3 \u4f4d\u5143\u7d44\uff0c\u5176\u4ed6\u7684\u4e00\u4e9b\u751f\u50fb\u5b57\u5143\u9700\u8981 4 \u4f4d\u5143\u7d44\u3002</p> <p>UTF-8 \u7684\u7de8\u78bc\u898f\u5247\u4e26\u4e0d\u8907\u96dc\uff0c\u5206\u70ba\u4ee5\u4e0b\u5169\u7a2e\u60c5\u6cc1\u3002</p> <ul> <li>\u5c0d\u65bc\u9577\u5ea6\u70ba 1 \u4f4d\u5143\u7d44\u7684\u5b57\u5143\uff0c\u5c07\u6700\u9ad8\u4f4d\u8a2d\u5b9a\u70ba \\(0\\) \uff0c\u5176\u9918 7 \u4f4d\u8a2d\u5b9a\u70ba Unicode \u78bc\u9ede\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cASCII \u5b57\u5143\u5728 Unicode \u5b57\u7b26\u96c6\u4e2d\u4f54\u64da\u4e86\u524d 128 \u500b\u78bc\u9ede\u3002\u4e5f\u5c31\u662f\u8aaa\uff0cUTF-8 \u7de8\u78bc\u53ef\u4ee5\u5411\u4e0b\u76f8\u5bb9 ASCII \u78bc\u3002\u9019\u610f\u5473\u8457\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 UTF-8 \u4f86\u89e3\u6790\u5e74\u4ee3\u4e45\u9060\u7684 ASCII \u78bc\u6587\u5b57\u3002</li> <li>\u5c0d\u65bc\u9577\u5ea6\u70ba \\(n\\) \u4f4d\u5143\u7d44\u7684\u5b57\u5143\uff08\u5176\u4e2d \\(n &gt; 1\\)\uff09\uff0c\u5c07\u9996\u500b\u4f4d\u5143\u7d44\u7684\u9ad8 \\(n\\) \u4f4d\u90fd\u8a2d\u5b9a\u70ba \\(1\\) \uff0c\u7b2c \\(n + 1\\) \u4f4d\u8a2d\u5b9a\u70ba \\(0\\) \uff1b\u5f9e\u7b2c\u4e8c\u500b\u4f4d\u5143\u7d44\u958b\u59cb\uff0c\u5c07\u6bcf\u500b\u4f4d\u5143\u7d44\u7684\u9ad8 2 \u4f4d\u90fd\u8a2d\u5b9a\u70ba \\(10\\) \uff1b\u5176\u9918\u6240\u6709\u4f4d\u7528\u65bc\u586b\u5145\u5b57\u5143\u7684 Unicode \u78bc\u9ede\u3002</li> </ul> <p>\u5716 3-8 \u5c55\u793a\u4e86\u201cHello\u6f14\u7b97\u6cd5\u201d\u5c0d\u61c9\u7684 UTF-8 \u7de8\u78bc\u3002\u89c0\u5bdf\u767c\u73fe\uff0c\u7531\u65bc\u6700\u9ad8 \\(n\\) \u4f4d\u90fd\u8a2d\u5b9a\u70ba \\(1\\) \uff0c\u56e0\u6b64\u7cfb\u7d71\u53ef\u4ee5\u900f\u904e\u8b80\u53d6\u6700\u9ad8\u4f4d \\(1\\) \u7684\u500b\u6578\u4f86\u89e3\u6790\u51fa\u5b57\u5143\u7684\u9577\u5ea6\u70ba \\(n\\) \u3002</p> <p>\u4f46\u70ba\u4ec0\u9ebc\u8981\u5c07\u5176\u9918\u6240\u6709\u4f4d\u5143\u7d44\u7684\u9ad8 2 \u4f4d\u90fd\u8a2d\u5b9a\u70ba \\(10\\) \u5462\uff1f\u5be6\u969b\u4e0a\uff0c\u9019\u500b \\(10\\) \u80fd\u5920\u8d77\u5230\u6821\u9a57\u7b26\u7684\u4f5c\u7528\u3002\u5047\u8a2d\u7cfb\u7d71\u5f9e\u4e00\u500b\u932f\u8aa4\u7684\u4f4d\u5143\u7d44\u958b\u59cb\u89e3\u6790\u6587\u5b57\uff0c\u4f4d\u5143\u7d44\u982d\u90e8\u7684 \\(10\\) \u80fd\u5920\u5e6b\u52a9\u7cfb\u7d71\u5feb\u901f\u5224\u65b7\u51fa\u7570\u5e38\u3002</p> <p>\u4e4b\u6240\u4ee5\u5c07 \\(10\\) \u7576\u4f5c\u6821\u9a57\u7b26\uff0c\u662f\u56e0\u70ba\u5728 UTF-8 \u7de8\u78bc\u898f\u5247\u4e0b\uff0c\u4e0d\u53ef\u80fd\u6709\u5b57\u5143\u7684\u6700\u9ad8\u5169\u4f4d\u662f \\(10\\) \u3002\u9019\u500b\u7d50\u8ad6\u53ef\u4ee5\u7528\u53cd\u8b49\u6cd5\u4f86\u8b49\u660e\uff1a\u5047\u8a2d\u4e00\u500b\u5b57\u5143\u7684\u6700\u9ad8\u5169\u4f4d\u662f \\(10\\) \uff0c\u8aaa\u660e\u8a72\u5b57\u5143\u7684\u9577\u5ea6\u70ba \\(1\\) \uff0c\u5c0d\u61c9 ASCII \u78bc\u3002\u800c ASCII \u78bc\u7684\u6700\u9ad8\u4f4d\u61c9\u8a72\u662f \\(0\\) \uff0c\u8207\u5047\u8a2d\u77db\u76fe\u3002</p> <p></p> <p> \u5716 3-8 \u00a0 UTF-8 \u7de8\u78bc\u793a\u4f8b </p> <p>\u9664\u4e86 UTF-8 \u4e4b\u5916\uff0c\u5e38\u898b\u7684\u7de8\u78bc\u65b9\u5f0f\u9084\u5305\u62ec\u4ee5\u4e0b\u5169\u7a2e\u3002</p> <ul> <li>UTF-16 \u7de8\u78bc\uff1a\u4f7f\u7528 2 \u6216 4 \u4f4d\u5143\u7d44\u4f86\u8868\u793a\u4e00\u500b\u5b57\u5143\u3002\u6240\u6709\u7684 ASCII \u5b57\u5143\u548c\u5e38\u7528\u7684\u975e\u82f1\u6587\u5b57\u5143\uff0c\u90fd\u7528 2 \u4f4d\u5143\u7d44\u8868\u793a\uff1b\u5c11\u6578\u5b57\u7b26\u9700\u8981\u7528\u5230 4 \u4f4d\u5143\u7d44\u8868\u793a\u3002\u5c0d\u65bc 2 \u4f4d\u5143\u7d44\u7684\u5b57\u5143\uff0cUTF-16 \u7de8\u78bc\u8207 Unicode \u78bc\u9ede\u76f8\u7b49\u3002</li> <li>UTF-32 \u7de8\u78bc\uff1a\u6bcf\u500b\u5b57\u5143\u90fd\u4f7f\u7528 4 \u4f4d\u5143\u7d44\u3002\u9019\u610f\u5473\u8457 UTF-32 \u6bd4 UTF-8 \u548c UTF-16 \u66f4\u4f54\u7528\u7a7a\u9593\uff0c\u7279\u5225\u662f\u5c0d\u65bc ASCII \u5b57\u5143\u4f54\u6bd4\u8f03\u9ad8\u7684\u6587\u5b57\u3002</li> </ul> <p>\u5f9e\u5132\u5b58\u7a7a\u9593\u4f54\u7528\u7684\u89d2\u5ea6\u770b\uff0c\u4f7f\u7528 UTF-8 \u8868\u793a\u82f1\u6587\u5b57\u5143\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u70ba\u5b83\u50c5\u9700 1 \u4f4d\u5143\u7d44\uff1b\u4f7f\u7528 UTF-16 \u7de8\u78bc\u67d0\u4e9b\u975e\u82f1\u6587\u5b57\u5143\uff08\u4f8b\u5982\u4e2d\u6587\uff09\u6703\u66f4\u52a0\u9ad8\u6548\uff0c\u56e0\u70ba\u5b83\u50c5\u9700 2 \u4f4d\u5143\u7d44\uff0c\u800c UTF-8 \u53ef\u80fd\u9700\u8981 3 \u4f4d\u5143\u7d44\u3002</p> <p>\u5f9e\u76f8\u5bb9\u6027\u7684\u89d2\u5ea6\u770b\uff0cUTF-8 \u7684\u901a\u7528\u6027\u6700\u4f73\uff0c\u8a31\u591a\u5de5\u5177\u548c\u5eab\u512a\u5148\u652f\u6301 UTF-8 \u3002</p>"},{"location":"chapter_data_structure/character_encoding/#345","title":"3.4.5 \u00a0 \u7a0b\u5f0f\u8a9e\u8a00\u7684\u5b57\u5143\u7de8\u78bc","text":"<p>\u5c0d\u65bc\u4ee5\u5f80\u7684\u5927\u591a\u6578\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u7a0b\u5f0f\u57f7\u884c\u4e2d\u7684\u5b57\u4e32\u90fd\u63a1\u7528 UTF-16 \u6216 UTF-32 \u9019\u985e\u7b49\u9577\u7de8\u78bc\u3002\u5728\u7b49\u9577\u7de8\u78bc\u4e0b\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u5b57\u4e32\u770b\u4f5c\u9663\u5217\u4f86\u8655\u7406\uff0c\u9019\u7a2e\u505a\u6cd5\u5177\u6709\u4ee5\u4e0b\u512a\u9ede\u3002</p> <ul> <li>\u96a8\u6a5f\u8a2a\u554f\uff1aUTF-16 \u7de8\u78bc\u7684\u5b57\u4e32\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u9032\u884c\u96a8\u6a5f\u8a2a\u554f\u3002UTF-8 \u662f\u4e00\u7a2e\u8b8a\u9577\u7de8\u78bc\uff0c\u8981\u60f3\u627e\u5230\u7b2c \\(i\\) \u500b\u5b57\u5143\uff0c\u6211\u5011\u9700\u8981\u5f9e\u5b57\u4e32\u7684\u958b\u59cb\u8655\u8d70\u8a2a\u5230\u7b2c \\(i\\) \u500b\u5b57\u5143\uff0c\u9019\u9700\u8981 \\(O(n)\\) \u7684\u6642\u9593\u3002</li> <li>\u5b57\u5143\u8a08\u6578\uff1a\u8207\u96a8\u6a5f\u8a2a\u554f\u985e\u4f3c\uff0c\u8a08\u7b97 UTF-16 \u7de8\u78bc\u7684\u5b57\u4e32\u7684\u9577\u5ea6\u4e5f\u662f \\(O(1)\\) \u7684\u64cd\u4f5c\u3002\u4f46\u662f\uff0c\u8a08\u7b97 UTF-8 \u7de8\u78bc\u7684\u5b57\u4e32\u7684\u9577\u5ea6\u9700\u8981\u8d70\u8a2a\u6574\u500b\u5b57\u4e32\u3002</li> <li>\u5b57\u4e32\u64cd\u4f5c\uff1a\u5728 UTF-16 \u7de8\u78bc\u7684\u5b57\u4e32\u4e0a\uff0c\u5f88\u591a\u5b57\u4e32\u64cd\u4f5c\uff08\u5982\u5206\u5272\u3001\u9023\u7dda\u3001\u63d2\u5165\u3001\u522a\u9664\u7b49\uff09\u66f4\u5bb9\u6613\u9032\u884c\u3002\u5728 UTF-8 \u7de8\u78bc\u7684\u5b57\u4e32\u4e0a\uff0c\u9032\u884c\u9019\u4e9b\u64cd\u4f5c\u901a\u5e38\u9700\u8981\u984d\u5916\u7684\u8a08\u7b97\uff0c\u4ee5\u78ba\u4fdd\u4e0d\u6703\u7522\u751f\u7121\u6548\u7684 UTF-8 \u7de8\u78bc\u3002</li> </ul> <p>\u5be6\u969b\u4e0a\uff0c\u7a0b\u5f0f\u8a9e\u8a00\u7684\u5b57\u5143\u7de8\u78bc\u65b9\u6848\u8a2d\u8a08\u662f\u4e00\u500b\u5f88\u6709\u8da3\u7684\u8a71\u984c\uff0c\u6d89\u53ca\u8a31\u591a\u56e0\u7d20\u3002</p> <ul> <li>Java \u7684 <code>String</code> \u578b\u5225\u4f7f\u7528 UTF-16 \u7de8\u78bc\uff0c\u6bcf\u500b\u5b57\u5143\u4f54\u7528 2 \u4f4d\u5143\u7d44\u3002\u9019\u662f\u56e0\u70ba Java \u8a9e\u8a00\u8a2d\u8a08\u4e4b\u521d\uff0c\u4eba\u5011\u8a8d\u70ba 16 \u4f4d\u8db3\u4ee5\u8868\u793a\u6240\u6709\u53ef\u80fd\u7684\u5b57\u5143\u3002\u7136\u800c\uff0c\u9019\u662f\u4e00\u500b\u4e0d\u6b63\u78ba\u7684\u5224\u65b7\u3002\u5f8c\u4f86 Unicode \u898f\u7bc4\u64f4\u5c55\u5230\u4e86\u8d85\u904e 16 \u4f4d\uff0c\u6240\u4ee5 Java \u4e2d\u7684\u5b57\u5143\u73fe\u5728\u53ef\u80fd\u7531\u4e00\u5c0d 16 \u4f4d\u7684\u503c\uff08\u7a31\u70ba\u201c\u4ee3\u7406\u5c0d\u201d\uff09\u8868\u793a\u3002</li> <li>JavaScript \u548c TypeScript \u7684\u5b57\u4e32\u4f7f\u7528 UTF-16 \u7de8\u78bc\u7684\u539f\u56e0\u8207 Java \u985e\u4f3c\u3002\u7576 1995 \u5e74 Netscape \u516c\u53f8\u9996\u6b21\u63a8\u51fa JavaScript \u8a9e\u8a00\u6642\uff0cUnicode \u9084\u8655\u65bc\u767c\u5c55\u65e9\u671f\uff0c\u90a3\u6642\u5019\u4f7f\u7528 16 \u4f4d\u7684\u7de8\u78bc\u5c31\u8db3\u4ee5\u8868\u793a\u6240\u6709\u7684 Unicode \u5b57\u5143\u4e86\u3002</li> <li>C# \u4f7f\u7528 UTF-16 \u7de8\u78bc\uff0c\u4e3b\u8981\u662f\u56e0\u70ba .NET \u5e73\u81fa\u662f\u7531 Microsoft \u8a2d\u8a08\u7684\uff0c\u800c Microsoft \u7684\u5f88\u591a\u6280\u8853\uff08\u5305\u62ec Windows \u4f5c\u696d\u7cfb\u7d71\uff09\u90fd\u5ee3\u6cdb\u4f7f\u7528 UTF-16 \u7de8\u78bc\u3002</li> </ul> <p>\u7531\u65bc\u4ee5\u4e0a\u7a0b\u5f0f\u8a9e\u8a00\u5c0d\u5b57\u5143\u6578\u91cf\u7684\u4f4e\u4f30\uff0c\u5b83\u5011\u4e0d\u5f97\u4e0d\u63a1\u53d6\u201c\u4ee3\u7406\u5c0d\u201d\u7684\u65b9\u5f0f\u4f86\u8868\u793a\u8d85\u904e 16 \u4f4d\u9577\u5ea6\u7684 Unicode \u5b57\u5143\u3002\u9019\u662f\u4e00\u500b\u4e0d\u5f97\u5df2\u70ba\u4e4b\u7684\u7121\u5948\u4e4b\u8209\u3002\u4e00\u65b9\u9762\uff0c\u5305\u542b\u4ee3\u7406\u5c0d\u7684\u5b57\u4e32\u4e2d\uff0c\u4e00\u500b\u5b57\u5143\u53ef\u80fd\u4f54\u7528 2 \u4f4d\u5143\u7d44\u6216 4 \u4f4d\u5143\u7d44\uff0c\u5f9e\u800c\u55aa\u5931\u4e86\u7b49\u9577\u7de8\u78bc\u7684\u512a\u52e2\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u8655\u7406\u4ee3\u7406\u5c0d\u9700\u8981\u984d\u5916\u589e\u52a0\u7a0b\u5f0f\u78bc\uff0c\u9019\u63d0\u9ad8\u4e86\u7a0b\u5f0f\u8a2d\u8a08\u7684\u8907\u96dc\u6027\u548c\u9664\u932f\u96e3\u5ea6\u3002</p> <p>\u51fa\u65bc\u4ee5\u4e0a\u539f\u56e0\uff0c\u90e8\u5206\u7a0b\u5f0f\u8a9e\u8a00\u63d0\u51fa\u4e86\u4e00\u4e9b\u4e0d\u540c\u7684\u7de8\u78bc\u65b9\u6848\u3002</p> <ul> <li>Python \u4e2d\u7684 <code>str</code> \u4f7f\u7528 Unicode \u7de8\u78bc\uff0c\u4e26\u63a1\u7528\u4e00\u7a2e\u9748\u6d3b\u7684\u5b57\u4e32\u8868\u793a\uff0c\u5132\u5b58\u7684\u5b57\u5143\u9577\u5ea6\u53d6\u6c7a\u65bc\u5b57\u4e32\u4e2d\u6700\u5927\u7684 Unicode \u78bc\u9ede\u3002\u82e5\u5b57\u4e32\u4e2d\u5168\u90e8\u662f ASCII \u5b57\u5143\uff0c\u5247\u6bcf\u500b\u5b57\u5143\u4f54\u7528 1 \u4f4d\u5143\u7d44\uff1b\u5982\u679c\u6709\u5b57\u5143\u8d85\u51fa\u4e86 ASCII \u7bc4\u570d\uff0c\u4f46\u5168\u90e8\u5728\u57fa\u672c\u591a\u8a9e\u8a00\u5e73\u9762\uff08BMP\uff09\u5167\uff0c\u5247\u6bcf\u500b\u5b57\u5143\u4f54\u7528 2 \u4f4d\u5143\u7d44\uff1b\u5982\u679c\u6709\u8d85\u51fa BMP \u7684\u5b57\u5143\uff0c\u5247\u6bcf\u500b\u5b57\u5143\u4f54\u7528 4 \u4f4d\u5143\u7d44\u3002</li> <li>Go \u8a9e\u8a00\u7684 <code>string</code> \u578b\u5225\u5728\u5167\u90e8\u4f7f\u7528 UTF-8 \u7de8\u78bc\u3002Go \u8a9e\u8a00\u9084\u63d0\u4f9b\u4e86 <code>rune</code> \u578b\u5225\uff0c\u5b83\u7528\u65bc\u8868\u793a\u55ae\u500b Unicode \u78bc\u9ede\u3002</li> <li>Rust \u8a9e\u8a00\u7684 <code>str</code> \u548c <code>String</code> \u578b\u5225\u5728\u5167\u90e8\u4f7f\u7528 UTF-8 \u7de8\u78bc\u3002Rust \u4e5f\u63d0\u4f9b\u4e86 <code>char</code> \u578b\u5225\uff0c\u7528\u65bc\u8868\u793a\u55ae\u500b Unicode \u78bc\u9ede\u3002</li> </ul> <p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ee5\u4e0a\u8a0e\u8ad6\u7684\u90fd\u662f\u5b57\u4e32\u5728\u7a0b\u5f0f\u8a9e\u8a00\u4e2d\u7684\u5132\u5b58\u65b9\u5f0f\uff0c\u9019\u548c\u5b57\u4e32\u5982\u4f55\u5728\u6a94\u6848\u4e2d\u5132\u5b58\u6216\u5728\u7db2\u8def\u4e2d\u50b3\u8f38\u662f\u4e0d\u540c\u7684\u554f\u984c\u3002\u5728\u6a94\u6848\u5132\u5b58\u6216\u7db2\u8def\u50b3\u8f38\u4e2d\uff0c\u6211\u5011\u901a\u5e38\u6703\u5c07\u5b57\u4e32\u7de8\u78bc\u70ba UTF-8 \u683c\u5f0f\uff0c\u4ee5\u9054\u5230\u6700\u512a\u7684\u76f8\u5bb9\u6027\u548c\u7a7a\u9593\u6548\u7387\u3002</p>"},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1 \u00a0 \u8cc7\u6599\u7d50\u69cb\u5206\u985e","text":"<p>\u5e38\u898b\u7684\u8cc7\u6599\u7d50\u69cb\u5305\u62ec\u9663\u5217\u3001\u93c8\u7d50\u4e32\u5217\u3001\u5806\u758a\u3001\u4f47\u5217\u3001\u96dc\u6e4a\u8868\u3001\u6a39\u3001\u5806\u7a4d\u3001\u5716\uff0c\u5b83\u5011\u53ef\u4ee5\u5f9e\u201c\u908f\u8f2f\u7d50\u69cb\u201d\u548c\u201c\u7269\u7406\u7d50\u69cb\u201d\u5169\u500b\u7dad\u5ea6\u9032\u884c\u5206\u985e\u3002</p>"},{"location":"chapter_data_structure/classification_of_data_structure/#311","title":"3.1.1 \u00a0 \u908f\u8f2f\u7d50\u69cb\uff1a\u7dda\u6027\u8207\u975e\u7dda\u6027","text":"<p>\u908f\u8f2f\u7d50\u69cb\u63ed\u793a\u4e86\u8cc7\u6599\u5143\u7d20\u4e4b\u9593\u7684\u908f\u8f2f\u95dc\u4fc2\u3002\u5728\u9663\u5217\u548c\u93c8\u7d50\u4e32\u5217\u4e2d\uff0c\u8cc7\u6599\u6309\u7167\u4e00\u5b9a\u9806\u5e8f\u6392\u5217\uff0c\u9ad4\u73fe\u4e86\u8cc7\u6599\u4e4b\u9593\u7684\u7dda\u6027\u95dc\u4fc2\uff1b\u800c\u5728\u6a39\u4e2d\uff0c\u8cc7\u6599\u5f9e\u9802\u90e8\u5411\u4e0b\u6309\u5c64\u6b21\u6392\u5217\uff0c\u8868\u73fe\u51fa\u201c\u7956\u5148\u201d\u8207\u201c\u5f8c\u4ee3\u201d\u4e4b\u9593\u7684\u6d3e\u751f\u95dc\u4fc2\uff1b\u5716\u5247\u7531\u7bc0\u9ede\u548c\u908a\u69cb\u6210\uff0c\u53cd\u6620\u4e86\u8907\u96dc\u7684\u7db2\u8def\u95dc\u4fc2\u3002</p> <p>\u5982\u5716 3-1 \u6240\u793a\uff0c\u908f\u8f2f\u7d50\u69cb\u53ef\u5206\u70ba\u201c\u7dda\u6027\u201d\u548c\u201c\u975e\u7dda\u6027\u201d\u5169\u5927\u985e\u3002\u7dda\u6027\u7d50\u69cb\u6bd4\u8f03\u76f4\u89c0\uff0c\u6307\u8cc7\u6599\u5728\u908f\u8f2f\u95dc\u4fc2\u4e0a\u5448\u7dda\u6027\u6392\u5217\uff1b\u975e\u7dda\u6027\u7d50\u69cb\u5247\u76f8\u53cd\uff0c\u5448\u975e\u7dda\u6027\u6392\u5217\u3002</p> <ul> <li>\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\uff1a\u9663\u5217\u3001\u93c8\u7d50\u4e32\u5217\u3001\u5806\u758a\u3001\u4f47\u5217\u3001\u96dc\u6e4a\u8868\uff0c\u5143\u7d20\u4e4b\u9593\u662f\u4e00\u5c0d\u4e00\u7684\u9806\u5e8f\u95dc\u4fc2\u3002</li> <li>\u975e\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\uff1a\u6a39\u3001\u5806\u7a4d\u3001\u5716\u3001\u96dc\u6e4a\u8868\u3002</li> </ul> <p>\u975e\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\u53ef\u4ee5\u9032\u4e00\u6b65\u5283\u5206\u70ba\u6a39\u5f62\u7d50\u69cb\u548c\u7db2\u72c0\u7d50\u69cb\u3002</p> <ul> <li>\u6a39\u5f62\u7d50\u69cb\uff1a\u6a39\u3001\u5806\u7a4d\u3001\u96dc\u6e4a\u8868\uff0c\u5143\u7d20\u4e4b\u9593\u662f\u4e00\u5c0d\u591a\u7684\u95dc\u4fc2\u3002</li> <li>\u7db2\u72c0\u7d50\u69cb\uff1a\u5716\uff0c\u5143\u7d20\u4e4b\u9593\u662f\u591a\u5c0d\u591a\u7684\u95dc\u4fc2\u3002</li> </ul> <p></p> <p> \u5716 3-1 \u00a0 \u7dda\u6027\u8cc7\u6599\u7d50\u69cb\u8207\u975e\u7dda\u6027\u8cc7\u6599\u7d50\u69cb </p>"},{"location":"chapter_data_structure/classification_of_data_structure/#312","title":"3.1.2 \u00a0 \u7269\u7406\u7d50\u69cb\uff1a\u9023\u7e8c\u8207\u5206\u6563","text":"<p>\u7576\u6f14\u7b97\u6cd5\u7a0b\u5f0f\u57f7\u884c\u6642\uff0c\u6b63\u5728\u8655\u7406\u7684\u8cc7\u6599\u4e3b\u8981\u5132\u5b58\u5728\u8a18\u61b6\u9ad4\u4e2d\u3002\u5716 3-2 \u5c55\u793a\u4e86\u4e00\u500b\u8a08\u7b97\u6a5f\u8a18\u61b6\u9ad4\u689d\uff0c\u5176\u4e2d\u6bcf\u500b\u9ed1\u8272\u65b9\u584a\u90fd\u5305\u542b\u4e00\u584a\u8a18\u61b6\u9ad4\u7a7a\u9593\u3002\u6211\u5011\u53ef\u4ee5\u5c07\u8a18\u61b6\u9ad4\u60f3\u8c61\u6210\u4e00\u500b\u5de8\u5927\u7684 Excel \u8868\u683c\uff0c\u5176\u4e2d\u6bcf\u500b\u55ae\u5143\u683c\u90fd\u53ef\u4ee5\u5132\u5b58\u4e00\u5b9a\u5927\u5c0f\u7684\u8cc7\u6599\u3002</p> <p>\u7cfb\u7d71\u900f\u904e\u8a18\u61b6\u9ad4\u4f4d\u5740\u4f86\u8a2a\u554f\u76ee\u6a19\u4f4d\u7f6e\u7684\u8cc7\u6599\u3002\u5982\u5716 3-2 \u6240\u793a\uff0c\u8a08\u7b97\u6a5f\u6839\u64da\u7279\u5b9a\u898f\u5247\u70ba\u8868\u683c\u4e2d\u7684\u6bcf\u500b\u55ae\u5143\u683c\u5206\u914d\u7de8\u865f\uff0c\u78ba\u4fdd\u6bcf\u500b\u8a18\u61b6\u9ad4\u7a7a\u9593\u90fd\u6709\u552f\u4e00\u7684\u8a18\u61b6\u9ad4\u4f4d\u5740\u3002\u6709\u4e86\u9019\u4e9b\u4f4d\u5740\uff0c\u7a0b\u5f0f\u4fbf\u53ef\u4ee5\u8a2a\u554f\u8a18\u61b6\u9ad4\u4e2d\u7684\u8cc7\u6599\u3002</p> <p></p> <p> \u5716 3-2 \u00a0 \u8a18\u61b6\u9ad4\u689d\u3001\u8a18\u61b6\u9ad4\u7a7a\u9593\u3001\u8a18\u61b6\u9ad4\u4f4d\u5740 </p> <p>Tip</p> <p>\u503c\u5f97\u8aaa\u660e\u7684\u662f\uff0c\u5c07\u8a18\u61b6\u9ad4\u6bd4\u4f5c Excel \u8868\u683c\u662f\u4e00\u500b\u7c21\u5316\u7684\u985e\u6bd4\uff0c\u5be6\u969b\u8a18\u61b6\u9ad4\u7684\u5de5\u4f5c\u6a5f\u5236\u6bd4\u8f03\u8907\u96dc\uff0c\u6d89\u53ca\u4f4d\u5740\u7a7a\u9593\u3001\u8a18\u61b6\u9ad4\u7ba1\u7406\u3001\u5feb\u53d6\u6a5f\u5236\u3001\u865b\u64ec\u8a18\u61b6\u9ad4\u548c\u7269\u7406\u8a18\u61b6\u9ad4\u7b49\u6982\u5ff5\u3002</p> <p>\u8a18\u61b6\u9ad4\u662f\u6240\u6709\u7a0b\u5f0f\u7684\u5171\u4eab\u8cc7\u6e90\uff0c\u7576\u67d0\u584a\u8a18\u61b6\u9ad4\u88ab\u67d0\u500b\u7a0b\u5f0f\u4f54\u7528\u6642\uff0c\u5247\u7121\u6cd5\u88ab\u5176\u4ed6\u7a0b\u5f0f\u540c\u6642\u4f7f\u7528\u4e86\u3002\u56e0\u6b64\u5728\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u7684\u8a2d\u8a08\u4e2d\uff0c\u8a18\u61b6\u9ad4\u8cc7\u6e90\u662f\u4e00\u500b\u91cd\u8981\u7684\u8003\u616e\u56e0\u7d20\u3002\u6bd4\u5982\uff0c\u6f14\u7b97\u6cd5\u6240\u4f54\u7528\u7684\u8a18\u61b6\u9ad4\u5cf0\u503c\u4e0d\u61c9\u8d85\u904e\u7cfb\u7d71\u5269\u9918\u7a7a\u9592\u8a18\u61b6\u9ad4\uff1b\u5982\u679c\u7f3a\u5c11\u9023\u7e8c\u5927\u584a\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\uff0c\u90a3\u9ebc\u6240\u9078\u7528\u7684\u8cc7\u6599\u7d50\u69cb\u5fc5\u9808\u80fd\u5920\u5132\u5b58\u5728\u5206\u6563\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u5167\u3002</p> <p>\u5982\u5716 3-3 \u6240\u793a\uff0c\u7269\u7406\u7d50\u69cb\u53cd\u6620\u4e86\u8cc7\u6599\u5728\u8a08\u7b97\u6a5f\u8a18\u61b6\u9ad4\u4e2d\u7684\u5132\u5b58\u65b9\u5f0f\uff0c\u53ef\u5206\u70ba\u9023\u7e8c\u7a7a\u9593\u5132\u5b58\uff08\u9663\u5217\uff09\u548c\u5206\u6563\u7a7a\u9593\u5132\u5b58\uff08\u93c8\u7d50\u4e32\u5217\uff09\u3002\u7269\u7406\u7d50\u69cb\u5f9e\u5e95\u5c64\u6c7a\u5b9a\u4e86\u8cc7\u6599\u7684\u8a2a\u554f\u3001\u66f4\u65b0\u3001\u589e\u522a\u7b49\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5169\u7a2e\u7269\u7406\u7d50\u69cb\u5728\u6642\u9593\u6548\u7387\u548c\u7a7a\u9593\u6548\u7387\u65b9\u9762\u5448\u73fe\u51fa\u4e92\u88dc\u7684\u7279\u9ede\u3002</p> <p></p> <p> \u5716 3-3 \u00a0 \u9023\u7e8c\u7a7a\u9593\u5132\u5b58\u8207\u5206\u6563\u7a7a\u9593\u5132\u5b58 </p> <p>\u503c\u5f97\u8aaa\u660e\u7684\u662f\uff0c\u6240\u6709\u8cc7\u6599\u7d50\u69cb\u90fd\u662f\u57fa\u65bc\u9663\u5217\u3001\u93c8\u7d50\u4e32\u5217\u6216\u4e8c\u8005\u7684\u7d44\u5408\u5be6\u73fe\u7684\u3002\u4f8b\u5982\uff0c\u5806\u758a\u548c\u4f47\u5217\u65e2\u53ef\u4ee5\u4f7f\u7528\u9663\u5217\u5be6\u73fe\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\uff1b\u800c\u96dc\u6e4a\u8868\u7684\u5be6\u73fe\u53ef\u80fd\u540c\u6642\u5305\u542b\u9663\u5217\u548c\u93c8\u7d50\u4e32\u5217\u3002</p> <ul> <li>\u57fa\u65bc\u9663\u5217\u53ef\u5be6\u73fe\uff1a\u5806\u758a\u3001\u4f47\u5217\u3001\u96dc\u6e4a\u8868\u3001\u6a39\u3001\u5806\u7a4d\u3001\u5716\u3001\u77e9\u9663\u3001\u5f35\u91cf\uff08\u7dad\u5ea6 \\(\\geq 3\\) \u7684\u9663\u5217\uff09\u7b49\u3002</li> <li>\u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u53ef\u5be6\u73fe\uff1a\u5806\u758a\u3001\u4f47\u5217\u3001\u96dc\u6e4a\u8868\u3001\u6a39\u3001\u5806\u7a4d\u3001\u5716\u7b49\u3002</li> </ul> <p>\u93c8\u7d50\u4e32\u5217\u5728\u521d\u59cb\u5316\u5f8c\uff0c\u4ecd\u53ef\u4ee5\u5728\u7a0b\u5f0f\u57f7\u884c\u904e\u7a0b\u4e2d\u5c0d\u5176\u9577\u5ea6\u9032\u884c\u8abf\u6574\uff0c\u56e0\u6b64\u4e5f\u7a31\u201c\u52d5\u614b\u8cc7\u6599\u7d50\u69cb\u201d\u3002\u9663\u5217\u5728\u521d\u59cb\u5316\u5f8c\u9577\u5ea6\u4e0d\u53ef\u8b8a\uff0c\u56e0\u6b64\u4e5f\u7a31\u201c\u975c\u614b\u8cc7\u6599\u7d50\u69cb\u201d\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u9663\u5217\u53ef\u900f\u904e\u91cd\u65b0\u5206\u914d\u8a18\u61b6\u9ad4\u5be6\u73fe\u9577\u5ea6\u8b8a\u5316\uff0c\u5f9e\u800c\u5177\u5099\u4e00\u5b9a\u7684\u201c\u52d5\u614b\u6027\u201d\u3002</p> <p>Tip</p> <p>\u5982\u679c\u4f60\u611f\u89ba\u7269\u7406\u7d50\u69cb\u7406\u89e3\u8d77\u4f86\u6709\u56f0\u96e3\uff0c\u5efa\u8b70\u5148\u95b1\u8b80\u4e0b\u4e00\u7ae0\uff0c\u7136\u5f8c\u518d\u56de\u9867\u672c\u7bc0\u5167\u5bb9\u3002</p>"},{"location":"chapter_data_structure/number_encoding/","title":"3.3 \u00a0 \u6578\u5b57\u7de8\u78bc *","text":"<p>Tip</p> <p>\u5728\u672c\u66f8\u4e2d\uff0c\u6a19\u984c\u5e36\u6709 * \u7b26\u865f\u7684\u662f\u9078\u8b80\u7ae0\u7bc0\u3002\u5982\u679c\u4f60\u6642\u9593\u6709\u9650\u6216\u611f\u5230\u7406\u89e3\u56f0\u96e3\uff0c\u53ef\u4ee5\u5148\u8df3\u904e\uff0c\u7b49\u5b78\u5b8c\u5fc5\u8b80\u7ae0\u7bc0\u5f8c\u518d\u55ae\u7368\u653b\u514b\u3002</p>"},{"location":"chapter_data_structure/number_encoding/#331","title":"3.3.1 \u00a0 \u539f\u78bc\u3001\u4e00\u88dc\u6578\u548c\u4e8c\u88dc\u6578","text":"<p>\u5728\u4e0a\u4e00\u7bc0\u7684\u8868\u683c\u4e2d\u6211\u5011\u767c\u73fe\uff0c\u6240\u6709\u6574\u6578\u578b\u5225\u80fd\u5920\u8868\u793a\u7684\u8ca0\u6578\u90fd\u6bd4\u6b63\u6578\u591a\u4e00\u500b\uff0c\u4f8b\u5982 <code>byte</code> \u7684\u53d6\u503c\u7bc4\u570d\u662f \\([-128, 127]\\) \u3002\u9019\u500b\u73fe\u8c61\u6bd4\u8f03\u53cd\u76f4\u89ba\uff0c\u5b83\u7684\u5167\u5728\u539f\u56e0\u6d89\u53ca\u539f\u78bc\u3001\u4e00\u88dc\u6578\u3001\u4e8c\u88dc\u6578\u7684\u76f8\u95dc\u77e5\u8b58\u3002</p> <p>\u9996\u5148\u9700\u8981\u6307\u51fa\uff0c\u6578\u5b57\u662f\u4ee5\u201c\u4e8c\u88dc\u6578\u201d\u7684\u5f62\u5f0f\u5132\u5b58\u5728\u8a08\u7b97\u6a5f\u4e2d\u7684\u3002\u5728\u5206\u6790\u9019\u6a23\u505a\u7684\u539f\u56e0\u4e4b\u524d\uff0c\u9996\u5148\u7d66\u51fa\u4e09\u8005\u7684\u5b9a\u7fa9\u3002</p> <ul> <li>\u539f\u78bc\uff1a\u6211\u5011\u5c07\u6578\u5b57\u7684\u4e8c\u9032\u4f4d\u5236\u8868\u793a\u7684\u6700\u9ad8\u4f4d\u8996\u70ba\u7b26\u865f\u4f4d\uff0c\u5176\u4e2d \\(0\\) \u8868\u793a\u6b63\u6578\uff0c\\(1\\) \u8868\u793a\u8ca0\u6578\uff0c\u5176\u9918\u4f4d\u8868\u793a\u6578\u5b57\u7684\u503c\u3002</li> <li>\u4e00\u88dc\u6578\uff1a\u6b63\u6578\u7684\u4e00\u88dc\u6578\u8207\u5176\u539f\u78bc\u76f8\u540c\uff0c\u8ca0\u6578\u7684\u4e00\u88dc\u6578\u662f\u5c0d\u5176\u539f\u78bc\u9664\u7b26\u865f\u4f4d\u5916\u7684\u6240\u6709\u4f4d\u53d6\u53cd\u3002</li> <li>\u4e8c\u88dc\u6578\uff1a\u6b63\u6578\u7684\u4e8c\u88dc\u6578\u8207\u5176\u539f\u78bc\u76f8\u540c\uff0c\u8ca0\u6578\u7684\u4e8c\u88dc\u6578\u662f\u5728\u5176\u4e00\u88dc\u6578\u7684\u57fa\u790e\u4e0a\u52a0 \\(1\\) \u3002</li> </ul> <p>\u5716 3-4 \u5c55\u793a\u4e86\u539f\u78bc\u3001\u4e00\u88dc\u6578\u548c\u4e8c\u88dc\u6578\u4e4b\u9593\u7684\u8f49\u63db\u65b9\u6cd5\u3002</p> <p></p> <p> \u5716 3-4 \u00a0 \u539f\u78bc\u3001\u4e00\u88dc\u6578\u8207\u4e8c\u88dc\u6578\u4e4b\u9593\u7684\u76f8\u4e92\u8f49\u63db </p> <p>\u539f\u78bc\uff08sign-magnitude\uff09\u96d6\u7136\u6700\u76f4\u89c0\uff0c\u4f46\u5b58\u5728\u4e00\u4e9b\u4fb7\u9650\u6027\u3002\u4e00\u65b9\u9762\uff0c\u8ca0\u6578\u7684\u539f\u78bc\u4e0d\u80fd\u76f4\u63a5\u7528\u65bc\u904b\u7b97\u3002\u4f8b\u5982\u5728\u539f\u78bc\u4e0b\u8a08\u7b97 \\(1 + (-2)\\) \uff0c\u5f97\u5230\u7684\u7d50\u679c\u662f \\(-3\\) \uff0c\u9019\u986f\u7136\u662f\u4e0d\u5c0d\u7684\u3002</p> \\[ \\begin{aligned} &amp; 1 + (-2) \\newline &amp; \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline &amp; = 1000 \\; 0011 \\newline &amp; \\rightarrow -3 \\end{aligned} \\] <p>\u70ba\u4e86\u89e3\u6c7a\u6b64\u554f\u984c\uff0c\u8a08\u7b97\u6a5f\u5f15\u5165\u4e86\u4e00\u88dc\u6578\uff081's complement\uff09\u3002\u5982\u679c\u6211\u5011\u5148\u5c07\u539f\u78bc\u8f49\u63db\u70ba\u4e00\u88dc\u6578\uff0c\u4e26\u5728\u4e00\u88dc\u6578\u4e0b\u8a08\u7b97 \\(1 + (-2)\\) \uff0c\u6700\u5f8c\u5c07\u7d50\u679c\u5f9e\u4e00\u88dc\u6578\u8f49\u63db\u56de\u539f\u78bc\uff0c\u5247\u53ef\u5f97\u5230\u6b63\u78ba\u7d50\u679c \\(-1\\) \u3002</p> \\[ \\begin{aligned} &amp; 1 + (-2) \\newline &amp; \\rightarrow 0000 \\; 0001 \\; \\text{(\u539f\u78bc)} + 1000 \\; 0010 \\; \\text{(\u539f\u78bc)} \\newline &amp; = 0000 \\; 0001 \\; \\text{(\u4e00\u88dc\u6578)} + 1111 \\; 1101 \\; \\text{(\u4e00\u88dc\u6578)} \\newline &amp; = 1111 \\; 1110 \\; \\text{(\u4e00\u88dc\u6578)} \\newline &amp; = 1000 \\; 0001 \\; \\text{(\u539f\u78bc)} \\newline &amp; \\rightarrow -1 \\end{aligned} \\] <p>\u53e6\u4e00\u65b9\u9762\uff0c\u6578\u5b57\u96f6\u7684\u539f\u78bc\u6709 \\(+0\\) \u548c \\(-0\\) \u5169\u7a2e\u8868\u793a\u65b9\u5f0f\u3002\u9019\u610f\u5473\u8457\u6578\u5b57\u96f6\u5c0d\u61c9\u5169\u500b\u4e0d\u540c\u7684\u4e8c\u9032\u4f4d\u5236\u7de8\u78bc\uff0c\u9019\u53ef\u80fd\u6703\u5e36\u4f86\u6b67\u7fa9\u3002\u6bd4\u5982\u5728\u689d\u4ef6\u5224\u65b7\u4e2d\uff0c\u5982\u679c\u6c92\u6709\u5340\u5206\u6b63\u96f6\u548c\u8ca0\u96f6\uff0c\u5247\u53ef\u80fd\u6703\u5c0e\u81f4\u5224\u65b7\u7d50\u679c\u51fa\u932f\u3002\u800c\u5982\u679c\u6211\u5011\u60f3\u8655\u7406\u6b63\u96f6\u548c\u8ca0\u96f6\u6b67\u7fa9\uff0c\u5247\u9700\u8981\u5f15\u5165\u984d\u5916\u7684\u5224\u65b7\u64cd\u4f5c\uff0c\u9019\u53ef\u80fd\u6703\u964d\u4f4e\u8a08\u7b97\u6a5f\u7684\u904b\u7b97\u6548\u7387\u3002</p> \\[ \\begin{aligned} +0 &amp; \\rightarrow 0000 \\; 0000 \\newline -0 &amp; \\rightarrow 1000 \\; 0000 \\end{aligned} \\] <p>\u8207\u539f\u78bc\u4e00\u6a23\uff0c\u4e00\u88dc\u6578\u4e5f\u5b58\u5728\u6b63\u8ca0\u96f6\u6b67\u7fa9\u554f\u984c\uff0c\u56e0\u6b64\u8a08\u7b97\u6a5f\u9032\u4e00\u6b65\u5f15\u5165\u4e86\u4e8c\u88dc\u6578\uff082's complement\uff09\u3002\u6211\u5011\u5148\u4f86\u89c0\u5bdf\u4e00\u4e0b\u8ca0\u96f6\u7684\u539f\u78bc\u3001\u4e00\u88dc\u6578\u3001\u4e8c\u88dc\u6578\u7684\u8f49\u63db\u904e\u7a0b\uff1a</p> \\[ \\begin{aligned} -0 \\rightarrow \\; &amp; 1000 \\; 0000 \\; \\text{(\u539f\u78bc)} \\newline = \\; &amp; 1111 \\; 1111 \\; \\text{(\u4e00\u88dc\u6578)} \\newline = 1 \\; &amp; 0000 \\; 0000 \\; \\text{(\u4e8c\u88dc\u6578)} \\newline \\end{aligned} \\] <p>\u5728\u8ca0\u96f6\u7684\u4e00\u88dc\u6578\u57fa\u790e\u4e0a\u52a0 \\(1\\) \u6703\u7522\u751f\u9032\u4f4d\uff0c\u4f46 <code>byte</code> \u578b\u5225\u7684\u9577\u5ea6\u53ea\u6709 8 \u4f4d\uff0c\u56e0\u6b64\u6ea2\u4f4d\u5230\u7b2c 9 \u4f4d\u7684 \\(1\\) \u6703\u88ab\u6368\u68c4\u3002\u4e5f\u5c31\u662f\u8aaa\uff0c\u8ca0\u96f6\u7684\u4e8c\u88dc\u6578\u70ba \\(0000 \\; 0000\\) \uff0c\u8207\u6b63\u96f6\u7684\u4e8c\u88dc\u6578\u76f8\u540c\u3002\u9019\u610f\u5473\u8457\u5728\u4e8c\u88dc\u6578\u8868\u793a\u4e2d\u53ea\u5b58\u5728\u4e00\u500b\u96f6\uff0c\u6b63\u8ca0\u96f6\u6b67\u7fa9\u5f9e\u800c\u5f97\u5230\u89e3\u6c7a\u3002</p> <p>\u9084\u5269\u6700\u5f8c\u4e00\u500b\u7591\u60d1\uff1a<code>byte</code> \u578b\u5225\u7684\u53d6\u503c\u7bc4\u570d\u662f \\([-128, 127]\\) \uff0c\u591a\u51fa\u4f86\u7684\u4e00\u500b\u8ca0\u6578 \\(-128\\) \u662f\u5982\u4f55\u5f97\u5230\u7684\u5462\uff1f\u6211\u5011\u6ce8\u610f\u5230\uff0c\u5340\u9593 \\([-127, +127]\\) \u5167\u7684\u6240\u6709\u6574\u6578\u90fd\u6709\u5c0d\u61c9\u7684\u539f\u78bc\u3001\u4e00\u88dc\u6578\u548c\u4e8c\u88dc\u6578\uff0c\u4e26\u4e14\u539f\u78bc\u548c\u4e8c\u88dc\u6578\u4e4b\u9593\u53ef\u4ee5\u4e92\u76f8\u8f49\u63db\u3002</p> <p>\u7136\u800c\uff0c\u4e8c\u88dc\u6578 \\(1000 \\; 0000\\) \u662f\u4e00\u500b\u4f8b\u5916\uff0c\u5b83\u4e26\u6c92\u6709\u5c0d\u61c9\u7684\u539f\u78bc\u3002\u6839\u64da\u8f49\u63db\u65b9\u6cd5\uff0c\u6211\u5011\u5f97\u5230\u8a72\u4e8c\u88dc\u6578\u7684\u539f\u78bc\u70ba \\(0000 \\; 0000\\) \u3002\u9019\u986f\u7136\u662f\u77db\u76fe\u7684\uff0c\u56e0\u70ba\u8a72\u539f\u78bc\u8868\u793a\u6578\u5b57 \\(0\\) \uff0c\u5b83\u7684\u4e8c\u88dc\u6578\u61c9\u8a72\u662f\u81ea\u8eab\u3002\u8a08\u7b97\u6a5f\u898f\u5b9a\u9019\u500b\u7279\u6b8a\u7684\u4e8c\u88dc\u6578 \\(1000 \\; 0000\\) \u4ee3\u8868 \\(-128\\) \u3002\u5be6\u969b\u4e0a\uff0c\\((-1) + (-127)\\) \u5728\u4e8c\u88dc\u6578\u4e0b\u7684\u8a08\u7b97\u7d50\u679c\u5c31\u662f \\(-128\\) \u3002</p> \\[ \\begin{aligned} &amp; (-127) + (-1) \\newline &amp; \\rightarrow 1111 \\; 1111 \\; \\text{(\u539f\u78bc)} + 1000 \\; 0001 \\; \\text{(\u539f\u78bc)} \\newline &amp; = 1000 \\; 0000 \\; \\text{(\u4e00\u88dc\u6578)} + 1111 \\; 1110 \\; \\text{(\u4e00\u88dc\u6578)} \\newline &amp; = 1000 \\; 0001 \\; \\text{(\u4e8c\u88dc\u6578)} + 1111 \\; 1111 \\; \\text{(\u4e8c\u88dc\u6578)} \\newline &amp; = 1000 \\; 0000 \\; \\text{(\u4e8c\u88dc\u6578)} \\newline &amp; \\rightarrow -128 \\end{aligned} \\] <p>\u4f60\u53ef\u80fd\u5df2\u7d93\u767c\u73fe\u4e86\uff0c\u4e0a\u8ff0\u6240\u6709\u8a08\u7b97\u90fd\u662f\u52a0\u6cd5\u904b\u7b97\u3002\u9019\u6697\u793a\u8457\u4e00\u500b\u91cd\u8981\u4e8b\u5be6\uff1a\u8a08\u7b97\u6a5f\u5167\u90e8\u7684\u786c\u9ad4\u96fb\u8def\u4e3b\u8981\u662f\u57fa\u65bc\u52a0\u6cd5\u904b\u7b97\u8a2d\u8a08\u7684\u3002\u9019\u662f\u56e0\u70ba\u52a0\u6cd5\u904b\u7b97\u76f8\u5c0d\u65bc\u5176\u4ed6\u904b\u7b97\uff08\u6bd4\u5982\u4e58\u6cd5\u3001\u9664\u6cd5\u548c\u6e1b\u6cd5\uff09\u4f86\u8aaa\uff0c\u786c\u9ad4\u5be6\u73fe\u8d77\u4f86\u66f4\u7c21\u55ae\uff0c\u66f4\u5bb9\u6613\u9032\u884c\u4e26\u884c\u5316\u8655\u7406\uff0c\u904b\u7b97\u901f\u5ea6\u66f4\u5feb\u3002</p> <p>\u8acb\u6ce8\u610f\uff0c\u9019\u4e26\u4e0d\u610f\u5473\u8457\u8a08\u7b97\u6a5f\u53ea\u80fd\u505a\u52a0\u6cd5\u3002\u900f\u904e\u5c07\u52a0\u6cd5\u8207\u4e00\u4e9b\u57fa\u672c\u908f\u8f2f\u904b\u7b97\u7d50\u5408\uff0c\u8a08\u7b97\u6a5f\u80fd\u5920\u5be6\u73fe\u5404\u7a2e\u5176\u4ed6\u7684\u6578\u5b78\u904b\u7b97\u3002\u4f8b\u5982\uff0c\u8a08\u7b97\u6e1b\u6cd5 \\(a - b\\) \u53ef\u4ee5\u8f49\u63db\u70ba\u8a08\u7b97\u52a0\u6cd5 \\(a + (-b)\\) \uff1b\u8a08\u7b97\u4e58\u6cd5\u548c\u9664\u6cd5\u53ef\u4ee5\u8f49\u63db\u70ba\u8a08\u7b97\u591a\u6b21\u52a0\u6cd5\u6216\u6e1b\u6cd5\u3002</p> <p>\u73fe\u5728\u6211\u5011\u53ef\u4ee5\u7e3d\u7d50\u51fa\u8a08\u7b97\u6a5f\u4f7f\u7528\u4e8c\u88dc\u6578\u7684\u539f\u56e0\uff1a\u57fa\u65bc\u4e8c\u88dc\u6578\u8868\u793a\uff0c\u8a08\u7b97\u6a5f\u53ef\u4ee5\u7528\u540c\u6a23\u7684\u96fb\u8def\u548c\u64cd\u4f5c\u4f86\u8655\u7406\u6b63\u6578\u548c\u8ca0\u6578\u7684\u52a0\u6cd5\uff0c\u4e0d\u9700\u8981\u8a2d\u8a08\u7279\u6b8a\u7684\u786c\u9ad4\u96fb\u8def\u4f86\u8655\u7406\u6e1b\u6cd5\uff0c\u4e26\u4e14\u7121\u9808\u7279\u5225\u8655\u7406\u6b63\u8ca0\u96f6\u7684\u6b67\u7fa9\u554f\u984c\u3002\u9019\u5927\u5927\u7c21\u5316\u4e86\u786c\u9ad4\u8a2d\u8a08\uff0c\u63d0\u9ad8\u4e86\u904b\u7b97\u6548\u7387\u3002</p> <p>\u4e8c\u88dc\u6578\u7684\u8a2d\u8a08\u975e\u5e38\u7cbe\u5999\uff0c\u56e0\u7bc7\u5e45\u95dc\u4fc2\u6211\u5011\u5c31\u5148\u4ecb\u7d39\u5230\u9019\u88e1\uff0c\u5efa\u8b70\u6709\u8208\u8da3\u7684\u8b80\u8005\u9032\u4e00\u6b65\u6df1\u5165\u77ad\u89e3\u3002</p>"},{"location":"chapter_data_structure/number_encoding/#332","title":"3.3.2 \u00a0 \u6d6e\u9ede\u6578\u7de8\u78bc","text":"<p>\u7d30\u5fc3\u7684\u4f60\u53ef\u80fd\u6703\u767c\u73fe\uff1a<code>int</code> \u548c <code>float</code> \u9577\u5ea6\u76f8\u540c\uff0c\u90fd\u662f 4 \u4f4d\u5143\u7d44 \uff0c\u4f46\u70ba\u4ec0\u9ebc <code>float</code> \u7684\u53d6\u503c\u7bc4\u570d\u9060\u5927\u65bc <code>int</code> \uff1f\u9019\u975e\u5e38\u53cd\u76f4\u89ba\uff0c\u56e0\u70ba\u6309\u7406\u8aaa <code>float</code> \u9700\u8981\u8868\u793a\u5c0f\u6578\uff0c\u53d6\u503c\u7bc4\u570d\u61c9\u8a72\u8b8a\u5c0f\u624d\u5c0d\u3002</p> <p>\u5be6\u969b\u4e0a\uff0c\u9019\u662f\u56e0\u70ba\u6d6e\u9ede\u6578 <code>float</code> \u63a1\u7528\u4e86\u4e0d\u540c\u7684\u8868\u793a\u65b9\u5f0f\u3002\u8a18\u4e00\u500b 32 \u4f4d\u5143\u9577\u5ea6\u7684\u4e8c\u9032\u4f4d\u5236\u6578\u70ba\uff1a</p> \\[ b_{31} b_{30} b_{29} \\ldots b_2 b_1 b_0 \\] <p>\u6839\u64da IEEE 754 \u6a19\u6e96\uff0c32-bit \u9577\u5ea6\u7684 <code>float</code> \u7531\u4ee5\u4e0b\u4e09\u500b\u90e8\u5206\u69cb\u6210\u3002</p> <ul> <li>\u7b26\u865f\u4f4d \\(\\mathrm{S}\\) \uff1a\u4f54 1 \u4f4d \uff0c\u5c0d\u61c9 \\(b_{31}\\) \u3002</li> <li>\u6307\u6578\u4f4d \\(\\mathrm{E}\\) \uff1a\u4f54 8 \u4f4d \uff0c\u5c0d\u61c9 \\(b_{30} b_{29} \\ldots b_{23}\\) \u3002</li> <li>\u5206\u6578\u4f4d \\(\\mathrm{N}\\) \uff1a\u4f54 23 \u4f4d \uff0c\u5c0d\u61c9 \\(b_{22} b_{21} \\ldots b_0\\) \u3002</li> </ul> <p>\u4e8c\u9032\u4f4d\u5236\u6578 <code>float</code> \u5c0d\u61c9\u503c\u7684\u8a08\u7b97\u65b9\u6cd5\u70ba\uff1a</p> \\[ \\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 \\] <p>\u8f49\u5316\u5230\u5341\u9032\u4f4d\u5236\u4e0b\u7684\u8a08\u7b97\u516c\u5f0f\u70ba\uff1a</p> \\[ \\text {val}=(-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} -127} \\times (1 + \\mathrm{N}) \\] <p>\u5176\u4e2d\u5404\u9805\u7684\u53d6\u503c\u7bc4\u570d\u70ba\uff1a</p> \\[ \\begin{aligned} \\mathrm{S} \\in &amp; \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = &amp; (1 + \\sum_{i=1}^{23} b_{23-i} 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\] <p></p> <p> \u5716 3-5 \u00a0 IEEE 754 \u6a19\u6e96\u4e0b\u7684 float \u7684\u8a08\u7b97\u793a\u4f8b </p> <p>\u89c0\u5bdf\u5716 3-5 \uff0c\u7d66\u5b9a\u4e00\u500b\u793a\u4f8b\u8cc7\u6599 \\(\\mathrm{S} = 0\\) \uff0c \\(\\mathrm{E} = 124\\) \uff0c\\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\) \uff0c\u5247\u6709\uff1a</p> \\[ \\text { val } = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\] <p>\u73fe\u5728\u6211\u5011\u53ef\u4ee5\u56de\u7b54\u6700\u521d\u7684\u554f\u984c\uff1a<code>float</code> \u7684\u8868\u793a\u65b9\u5f0f\u5305\u542b\u6307\u6578\u4f4d\uff0c\u5c0e\u81f4\u5176\u53d6\u503c\u7bc4\u570d\u9060\u5927\u65bc <code>int</code> \u3002\u6839\u64da\u4ee5\u4e0a\u8a08\u7b97\uff0c<code>float</code> \u53ef\u8868\u793a\u7684\u6700\u5927\u6b63\u6578\u70ba \\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\) \uff0c\u5207\u63db\u7b26\u865f\u4f4d\u4fbf\u53ef\u5f97\u5230\u6700\u5c0f\u8ca0\u6578\u3002</p> <p>\u5118\u7ba1\u6d6e\u9ede\u6578 <code>float</code> \u64f4\u5c55\u4e86\u53d6\u503c\u7bc4\u570d\uff0c\u4f46\u5176\u526f\u4f5c\u7528\u662f\u72a7\u7272\u4e86\u7cbe\u5ea6\u3002\u6574\u6578\u578b\u5225 <code>int</code> \u5c07\u5168\u90e8 32 \u4f4d\u5143\u7528\u65bc\u8868\u793a\u6578\u5b57\uff0c\u6578\u5b57\u662f\u5747\u52fb\u5206\u4f48\u7684\uff1b\u800c\u7531\u65bc\u6307\u6578\u4f4d\u7684\u5b58\u5728\uff0c\u6d6e\u9ede\u6578 <code>float</code> \u7684\u6578\u503c\u8d8a\u5927\uff0c\u76f8\u9130\u5169\u500b\u6578\u5b57\u4e4b\u9593\u7684\u5dee\u503c\u5c31\u6703\u8da8\u5411\u8d8a\u5927\u3002</p> <p>\u5982\u8868 3-2 \u6240\u793a\uff0c\u6307\u6578\u4f4d \\(\\mathrm{E} = 0\\) \u548c \\(\\mathrm{E} = 255\\) \u5177\u6709\u7279\u6b8a\u542b\u7fa9\uff0c\u7528\u65bc\u8868\u793a\u96f6\u3001\u7121\u7aae\u5927\u3001\\(\\mathrm{NaN}\\) \u7b49\u3002</p> <p> \u8868 3-2 \u00a0 \u6307\u6578\u4f4d\u542b\u7fa9 </p> \u6307\u6578\u4f4d E \u5206\u6578\u4f4d \\(\\mathrm{N} = 0\\) \u5206\u6578\u4f4d \\(\\mathrm{N} \\ne 0\\) \u8a08\u7b97\u516c\u5f0f \\(0\\) \\(\\pm 0\\) \u6b21\u6b63\u898f\u6578 \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) \u6b63\u898f\u6578 \u6b63\u898f\u6578 \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\) <p>\u503c\u5f97\u8aaa\u660e\u7684\u662f\uff0c\u6b21\u6b63\u898f\u6578\u986f\u8457\u63d0\u5347\u4e86\u6d6e\u9ede\u6578\u7684\u7cbe\u5ea6\u3002\u6700\u5c0f\u6b63\u6b63\u898f\u6578\u70ba \\(2^{-126}\\) \uff0c\u6700\u5c0f\u6b63\u6b21\u6b63\u898f\u6578\u70ba \\(2^{-126} \\times 2^{-23}\\) \u3002</p> <p>\u96d9\u7cbe\u5ea6 <code>double</code> \u4e5f\u63a1\u7528\u985e\u4f3c\u65bc <code>float</code> \u7684\u8868\u793a\u65b9\u6cd5\uff0c\u5728\u6b64\u4e0d\u505a\u8d05\u8ff0\u3002</p>"},{"location":"chapter_data_structure/summary/","title":"3.5 \u00a0 \u5c0f\u7d50","text":""},{"location":"chapter_data_structure/summary/#1","title":"1. \u00a0 \u91cd\u9ede\u56de\u9867","text":"<ul> <li>\u8cc7\u6599\u7d50\u69cb\u53ef\u4ee5\u5f9e\u908f\u8f2f\u7d50\u69cb\u548c\u7269\u7406\u7d50\u69cb\u5169\u500b\u89d2\u5ea6\u9032\u884c\u5206\u985e\u3002\u908f\u8f2f\u7d50\u69cb\u63cf\u8ff0\u4e86\u8cc7\u6599\u5143\u7d20\u4e4b\u9593\u7684\u908f\u8f2f\u95dc\u4fc2\uff0c\u800c\u7269\u7406\u7d50\u69cb\u63cf\u8ff0\u4e86\u8cc7\u6599\u5728\u8a08\u7b97\u6a5f\u8a18\u61b6\u9ad4\u4e2d\u7684\u5132\u5b58\u65b9\u5f0f\u3002</li> <li>\u5e38\u898b\u7684\u908f\u8f2f\u7d50\u69cb\u5305\u62ec\u7dda\u6027\u3001\u6a39\u72c0\u548c\u7db2\u72c0\u7b49\u3002\u901a\u5e38\u6211\u5011\u6839\u64da\u908f\u8f2f\u7d50\u69cb\u5c07\u8cc7\u6599\u7d50\u69cb\u5206\u70ba\u7dda\u6027\uff08\u9663\u5217\u3001\u93c8\u7d50\u4e32\u5217\u3001\u5806\u758a\u3001\u4f47\u5217\uff09\u548c\u975e\u7dda\u6027\uff08\u6a39\u3001\u5716\u3001\u5806\u7a4d\uff09\u5169\u7a2e\u3002\u96dc\u6e4a\u8868\u7684\u5be6\u73fe\u53ef\u80fd\u540c\u6642\u5305\u542b\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\u548c\u975e\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\u3002</li> <li>\u7576\u7a0b\u5f0f\u57f7\u884c\u6642\uff0c\u8cc7\u6599\u88ab\u5132\u5b58\u5728\u8a08\u7b97\u6a5f\u8a18\u61b6\u9ad4\u4e2d\u3002\u6bcf\u500b\u8a18\u61b6\u9ad4\u7a7a\u9593\u90fd\u64c1\u6709\u5c0d\u61c9\u7684\u8a18\u61b6\u9ad4\u4f4d\u5740\uff0c\u7a0b\u5f0f\u900f\u904e\u9019\u4e9b\u8a18\u61b6\u9ad4\u4f4d\u5740\u8a2a\u554f\u8cc7\u6599\u3002</li> <li>\u7269\u7406\u7d50\u69cb\u4e3b\u8981\u5206\u70ba\u9023\u7e8c\u7a7a\u9593\u5132\u5b58\uff08\u9663\u5217\uff09\u548c\u5206\u6563\u7a7a\u9593\u5132\u5b58\uff08\u93c8\u7d50\u4e32\u5217\uff09\u3002\u6240\u6709\u8cc7\u6599\u7d50\u69cb\u90fd\u662f\u7531\u9663\u5217\u3001\u93c8\u7d50\u4e32\u5217\u6216\u5169\u8005\u7684\u7d44\u5408\u5be6\u73fe\u7684\u3002</li> <li>\u8a08\u7b97\u6a5f\u4e2d\u7684\u57fa\u672c\u8cc7\u6599\u578b\u5225\u5305\u62ec\u6574\u6578 <code>byte</code>\u3001<code>short</code>\u3001<code>int</code>\u3001<code>long</code> \uff0c\u6d6e\u9ede\u6578 <code>float</code>\u3001<code>double</code> \uff0c\u5b57\u5143 <code>char</code> \u548c\u5e03\u6797 <code>bool</code> \u3002\u5b83\u5011\u7684\u53d6\u503c\u7bc4\u570d\u53d6\u6c7a\u65bc\u4f54\u7528\u7a7a\u9593\u5927\u5c0f\u548c\u8868\u793a\u65b9\u5f0f\u3002</li> <li>\u539f\u78bc\u3001\u4e00\u88dc\u6578\u548c\u4e8c\u88dc\u6578\u662f\u5728\u8a08\u7b97\u6a5f\u4e2d\u7de8\u78bc\u6578\u5b57\u7684\u4e09\u7a2e\u65b9\u6cd5\uff0c\u5b83\u5011\u4e4b\u9593\u53ef\u4ee5\u76f8\u4e92\u8f49\u63db\u3002\u6574\u6578\u7684\u539f\u78bc\u7684\u6700\u9ad8\u4f4d\u662f\u7b26\u865f\u4f4d\uff0c\u5176\u9918\u4f4d\u662f\u6578\u5b57\u7684\u503c\u3002</li> <li>\u6574\u6578\u5728\u8a08\u7b97\u6a5f\u4e2d\u662f\u4ee5\u4e8c\u88dc\u6578\u7684\u5f62\u5f0f\u5132\u5b58\u7684\u3002\u5728\u4e8c\u88dc\u6578\u8868\u793a\u4e0b\uff0c\u8a08\u7b97\u6a5f\u53ef\u4ee5\u5c0d\u6b63\u6578\u548c\u8ca0\u6578\u7684\u52a0\u6cd5\u4e00\u8996\u540c\u4ec1\uff0c\u4e0d\u9700\u8981\u70ba\u6e1b\u6cd5\u64cd\u4f5c\u55ae\u7368\u8a2d\u8a08\u7279\u6b8a\u7684\u786c\u9ad4\u96fb\u8def\uff0c\u4e26\u4e14\u4e0d\u5b58\u5728\u6b63\u8ca0\u96f6\u6b67\u7fa9\u7684\u554f\u984c\u3002</li> <li>\u6d6e\u9ede\u6578\u7684\u7de8\u78bc\u7531 1 \u4f4d\u7b26\u865f\u4f4d\u30018 \u4f4d\u6307\u6578\u4f4d\u548c 23 \u4f4d\u5206\u6578\u4f4d\u69cb\u6210\u3002\u7531\u65bc\u5b58\u5728\u6307\u6578\u4f4d\uff0c\u56e0\u6b64\u6d6e\u9ede\u6578\u7684\u53d6\u503c\u7bc4\u570d\u9060\u5927\u65bc\u6574\u6578\uff0c\u4ee3\u50f9\u662f\u72a7\u7272\u4e86\u7cbe\u5ea6\u3002</li> <li>ASCII \u78bc\u662f\u6700\u65e9\u51fa\u73fe\u7684\u82f1\u6587\u5b57\u7b26\u96c6\uff0c\u9577\u5ea6\u70ba 1 \u4f4d\u5143\u7d44\uff0c\u5171\u6536\u9304 127 \u500b\u5b57\u5143\u3002GBK \u5b57\u7b26\u96c6\u662f\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u7b26\u96c6\uff0c\u5171\u6536\u9304\u5169\u842c\u591a\u500b\u6f22\u5b57\u3002Unicode \u81f4\u529b\u65bc\u63d0\u4f9b\u4e00\u500b\u5b8c\u6574\u7684\u5b57\u7b26\u96c6\u6a19\u6e96\uff0c\u6536\u9304\u4e16\u754c\u4e0a\u5404\u7a2e\u8a9e\u8a00\u7684\u5b57\u5143\uff0c\u5f9e\u800c\u89e3\u6c7a\u7531\u65bc\u5b57\u5143\u7de8\u78bc\u65b9\u6cd5\u4e0d\u4e00\u81f4\u800c\u5c0e\u81f4\u7684\u4e82\u78bc\u554f\u984c\u3002</li> <li>UTF-8 \u662f\u6700\u53d7\u6b61\u8fce\u7684 Unicode \u7de8\u78bc\u65b9\u6cd5\uff0c\u901a\u7528\u6027\u975e\u5e38\u597d\u3002\u5b83\u662f\u4e00\u7a2e\u8b8a\u9577\u7684\u7de8\u78bc\u65b9\u6cd5\uff0c\u5177\u6709\u5f88\u597d\u7684\u64f4\u5c55\u6027\uff0c\u6709\u6548\u63d0\u5347\u4e86\u5132\u5b58\u7a7a\u9593\u7684\u4f7f\u7528\u6548\u7387\u3002UTF-16 \u548c UTF-32 \u662f\u7b49\u9577\u7684\u7de8\u78bc\u65b9\u6cd5\u3002\u5728\u7de8\u78bc\u4e2d\u6587\u6642\uff0cUTF-16 \u4f54\u7528\u7684\u7a7a\u9593\u6bd4 UTF-8 \u66f4\u5c0f\u3002Java \u548c C# \u7b49\u7a0b\u5f0f\u8a9e\u8a00\u9810\u8a2d\u4f7f\u7528 UTF-16 \u7de8\u78bc\u3002</li> </ul>"},{"location":"chapter_data_structure/summary/#2-q-a","title":"2. \u00a0 Q &amp; A","text":"<p>Q\uff1a\u70ba\u4ec0\u9ebc\u96dc\u6e4a\u8868\u540c\u6642\u5305\u542b\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\u548c\u975e\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\uff1f</p> <p>\u96dc\u6e4a\u8868\u5e95\u5c64\u662f\u9663\u5217\uff0c\u800c\u70ba\u4e86\u89e3\u6c7a\u96dc\u6e4a\u885d\u7a81\uff0c\u6211\u5011\u53ef\u80fd\u6703\u4f7f\u7528\u201c\u93c8\u5f0f\u4f4d\u5740\u201d\uff08\u5f8c\u7e8c\u201c\u96dc\u6e4a\u885d\u7a81\u201d\u7ae0\u7bc0\u6703\u8b1b\uff09\uff1a\u9663\u5217\u4e2d\u6bcf\u500b\u6876\u6307\u5411\u4e00\u500b\u93c8\u7d50\u4e32\u5217\uff0c\u7576\u93c8\u7d50\u4e32\u5217\u9577\u5ea6\u8d85\u904e\u4e00\u5b9a\u95be\u503c\u6642\uff0c\u53c8\u53ef\u80fd\u88ab\u8f49\u5316\u70ba\u6a39\uff08\u901a\u5e38\u70ba\u7d05\u9ed1\u6a39\uff09\u3002</p> <p>\u5f9e\u5132\u5b58\u7684\u89d2\u5ea6\u4f86\u770b\uff0c\u96dc\u6e4a\u8868\u7684\u5e95\u5c64\u662f\u9663\u5217\uff0c\u5176\u4e2d\u6bcf\u4e00\u500b\u6876\u69fd\u4f4d\u53ef\u80fd\u5305\u542b\u4e00\u500b\u503c\uff0c\u4e5f\u53ef\u80fd\u5305\u542b\u4e00\u500b\u93c8\u7d50\u4e32\u5217\u6216\u4e00\u68f5\u6a39\u3002\u56e0\u6b64\uff0c\u96dc\u6e4a\u8868\u53ef\u80fd\u540c\u6642\u5305\u542b\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\uff08\u9663\u5217\u3001\u93c8\u7d50\u4e32\u5217\uff09\u548c\u975e\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\uff08\u6a39\uff09\u3002</p> <p>Q\uff1a<code>char</code> \u578b\u5225\u7684\u9577\u5ea6\u662f 1 \u4f4d\u5143\u7d44\u55ce\uff1f</p> <p><code>char</code> \u578b\u5225\u7684\u9577\u5ea6\u7531\u7a0b\u5f0f\u8a9e\u8a00\u63a1\u7528\u7684\u7de8\u78bc\u65b9\u6cd5\u6c7a\u5b9a\u3002\u4f8b\u5982\uff0cJava\u3001JavaScript\u3001TypeScript\u3001C# \u90fd\u63a1\u7528 UTF-16 \u7de8\u78bc\uff08\u5132\u5b58 Unicode \u78bc\u9ede\uff09\uff0c\u56e0\u6b64 <code>char</code> \u578b\u5225\u7684\u9577\u5ea6\u70ba 2 \u4f4d\u5143\u7d44\u3002</p> <p>Q\uff1a\u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u8cc7\u6599\u7d50\u69cb\u4e5f\u7a31\u201c\u975c\u614b\u8cc7\u6599\u7d50\u69cb\u201d \u662f\u5426\u6709\u6b67\u7fa9\uff1f\u5806\u758a\u4e5f\u53ef\u4ee5\u9032\u884c\u51fa\u5806\u758a\u548c\u5165\u5806\u758a\u7b49\u64cd\u4f5c\uff0c\u9019\u4e9b\u64cd\u4f5c\u90fd\u662f\u201c\u52d5\u614b\u201d\u7684\u3002</p> <p>\u5806\u758a\u78ba\u5be6\u53ef\u4ee5\u5be6\u73fe\u52d5\u614b\u7684\u8cc7\u6599\u64cd\u4f5c\uff0c\u4f46\u8cc7\u6599\u7d50\u69cb\u4ecd\u7136\u662f\u201c\u975c\u614b\u201d\uff08\u9577\u5ea6\u4e0d\u53ef\u8b8a\uff09\u7684\u3002\u5118\u7ba1\u57fa\u65bc\u9663\u5217\u7684\u8cc7\u6599\u7d50\u69cb\u53ef\u4ee5\u52d5\u614b\u5730\u65b0\u589e\u6216\u522a\u9664\u5143\u7d20\uff0c\u4f46\u5b83\u5011\u7684\u5bb9\u91cf\u662f\u56fa\u5b9a\u7684\u3002\u5982\u679c\u8cc7\u6599\u91cf\u8d85\u51fa\u4e86\u9810\u5206\u914d\u7684\u5927\u5c0f\uff0c\u5c31\u9700\u8981\u5efa\u7acb\u4e00\u500b\u65b0\u7684\u66f4\u5927\u7684\u9663\u5217\uff0c\u4e26\u5c07\u820a\u9663\u5217\u7684\u5167\u5bb9\u8907\u88fd\u5230\u65b0\u9663\u5217\u4e2d\u3002</p> <p>Q\uff1a\u5728\u69cb\u5efa\u5806\u758a\uff08\u4f47\u5217\uff09\u7684\u6642\u5019\uff0c\u672a\u6307\u5b9a\u5b83\u7684\u5927\u5c0f\uff0c\u70ba\u4ec0\u9ebc\u5b83\u5011\u662f\u201c\u975c\u614b\u8cc7\u6599\u7d50\u69cb\u201d\u5462\uff1f</p> <p>\u5728\u9ad8\u968e\u7a0b\u5f0f\u8a9e\u8a00\u4e2d\uff0c\u6211\u5011\u7121\u9808\u4eba\u5de5\u6307\u5b9a\u5806\u758a\uff08\u4f47\u5217\uff09\u7684\u521d\u59cb\u5bb9\u91cf\uff0c\u9019\u500b\u5de5\u4f5c\u7531\u985e\u5225\u5167\u90e8\u81ea\u52d5\u5b8c\u6210\u3002\u4f8b\u5982\uff0cJava \u7684 <code>ArrayList</code> \u7684\u521d\u59cb\u5bb9\u91cf\u901a\u5e38\u70ba 10\u3002\u53e6\u5916\uff0c\u64f4\u5bb9\u64cd\u4f5c\u4e5f\u662f\u81ea\u52d5\u5be6\u73fe\u7684\u3002\u8a73\u898b\u5f8c\u7e8c\u7684\u201c\u4e32\u5217\u201d\u7ae0\u7bc0\u3002</p> <p>Q\uff1a\u539f\u78bc\u8f49\u4e8c\u88dc\u6578\u7684\u65b9\u6cd5\u662f\u201c\u5148\u53d6\u53cd\u5f8c\u52a0 1\u201d\uff0c\u90a3\u9ebc\u4e8c\u88dc\u6578\u8f49\u539f\u78bc\u61c9\u8a72\u662f\u9006\u904b\u7b97\u201c\u5148\u6e1b 1 \u5f8c\u53d6\u53cd\u201d\uff0c\u800c\u4e8c\u88dc\u6578\u8f49\u539f\u78bc\u4e5f\u4e00\u6a23\u53ef\u4ee5\u900f\u904e\u201c\u5148\u53d6\u53cd\u5f8c\u52a0 1\u201d\u5f97\u5230\uff0c\u9019\u662f\u70ba\u4ec0\u9ebc\u5462\uff1f</p> <p>A\uff1a\u9019\u662f\u56e0\u70ba\u539f\u78bc\u548c\u4e8c\u88dc\u6578\u7684\u76f8\u4e92\u8f49\u63db\u5be6\u969b\u4e0a\u662f\u8a08\u7b97\u201c\u88dc\u6578\u201d\u7684\u904e\u7a0b\u3002\u6211\u5011\u5148\u7d66\u51fa\u88dc\u6578\u7684\u5b9a\u7fa9\uff1a\u5047\u8a2d \\(a + b = c\\) \uff0c\u90a3\u9ebc\u6211\u5011\u7a31 \\(a\\) \u662f \\(b\\) \u5230 \\(c\\) \u7684\u88dc\u6578\uff0c\u53cd\u4e4b\u4e5f\u7a31 \\(b\\) \u662f \\(a\\) \u5230 \\(c\\) \u7684\u88dc\u6578\u3002</p> <p>\u7d66\u5b9a\u4e00\u500b \\(n = 4\\) \u4f4d\u9577\u5ea6\u7684\u4e8c\u9032\u4f4d\u5236\u6578 \\(0010\\) \uff0c\u5982\u679c\u5c07\u9019\u500b\u6578\u5b57\u770b\u4f5c\u539f\u78bc\uff08\u4e0d\u8003\u616e\u7b26\u865f\u4f4d\uff09\uff0c\u90a3\u9ebc\u5b83\u7684\u4e8c\u88dc\u6578\u9700\u900f\u904e\u201c\u5148\u53d6\u53cd\u5f8c\u52a0 1\u201d\u5f97\u5230\uff1a</p> \\[ 0010 \\rightarrow 1101 \\rightarrow 1110 \\] <p>\u6211\u5011\u6703\u767c\u73fe\uff0c\u539f\u78bc\u548c\u4e8c\u88dc\u6578\u7684\u548c\u662f \\(0010 + 1110 = 10000\\) \uff0c\u4e5f\u5c31\u662f\u8aaa\uff0c\u4e8c\u88dc\u6578 \\(1110\\) \u662f\u539f\u78bc \\(0010\\) \u5230 \\(10000\\) \u7684\u201c\u88dc\u6578\u201d\u3002\u9019\u610f\u5473\u8457\u4e0a\u8ff0\u201c\u5148\u53d6\u53cd\u5f8c\u52a0 1\u201d\u5be6\u969b\u4e0a\u662f\u8a08\u7b97\u5230 \\(10000\\) \u7684\u88dc\u6578\u7684\u904e\u7a0b\u3002</p> <p>\u90a3\u9ebc\uff0c\u4e8c\u88dc\u6578 \\(1110\\) \u5230 \\(10000\\) \u7684\u201c\u88dc\u6578\u201d\u662f\u591a\u5c11\u5462\uff1f\u6211\u5011\u4f9d\u7136\u53ef\u4ee5\u7528\u201c\u5148\u53d6\u53cd\u5f8c\u52a0 1\u201d\u5f97\u5230\u5b83\uff1a</p> \\[ 1110 \\rightarrow 0001 \\rightarrow 0010 \\] <p>\u63db\u53e5\u8a71\u8aaa\uff0c\u539f\u78bc\u548c\u4e8c\u88dc\u6578\u4e92\u70ba\u5c0d\u65b9\u5230 \\(10000\\) \u7684\u201c\u88dc\u6578\u201d\uff0c\u56e0\u6b64\u201c\u539f\u78bc\u8f49\u4e8c\u88dc\u6578\u201d\u548c\u201c\u4e8c\u88dc\u6578\u8f49\u539f\u78bc\u201d\u53ef\u4ee5\u7528\u76f8\u540c\u7684\u64cd\u4f5c\uff08\u5148\u53d6\u53cd\u5f8c\u52a0 1 \uff09\u5be6\u73fe\u3002</p> <p>\u7576\u7136\uff0c\u6211\u5011\u4e5f\u53ef\u4ee5\u7528\u9006\u904b\u7b97\u4f86\u6c42\u4e8c\u88dc\u6578 \\(1110\\) \u7684\u539f\u78bc\uff0c\u5373\u201c\u5148\u6e1b 1 \u5f8c\u53d6\u53cd\u201d\uff1a</p> \\[ 1110 \\rightarrow 1101 \\rightarrow 0010 \\] <p>\u7e3d\u7d50\u4f86\u770b\uff0c\u201c\u5148\u53d6\u53cd\u5f8c\u52a0 1\u201d\u548c\u201c\u5148\u6e1b 1 \u5f8c\u53d6\u53cd\u201d\u9019\u5169\u7a2e\u904b\u7b97\u90fd\u662f\u5728\u8a08\u7b97\u5230 \\(10000\\) \u7684\u88dc\u6578\uff0c\u5b83\u5011\u662f\u7b49\u50f9\u7684\u3002</p> <p>\u672c\u8cea\u4e0a\u770b\uff0c\u201c\u53d6\u53cd\u201d\u64cd\u4f5c\u5be6\u969b\u4e0a\u662f\u6c42\u5230 \\(1111\\) \u7684\u88dc\u6578\uff08\u56e0\u70ba\u6046\u6709 <code>\u539f\u78bc + \u4e00\u88dc\u6578 = 1111</code>\uff09\uff1b\u800c\u5728\u4e00\u88dc\u6578\u57fa\u790e\u4e0a\u518d\u52a0 1 \u5f97\u5230\u7684\u4e8c\u88dc\u6578\uff0c\u5c31\u662f\u5230 \\(10000\\) \u7684\u88dc\u6578\u3002</p> <p>\u4e0a\u8ff0 \\(n = 4\\) \u70ba\u4f8b\uff0c\u5176\u53ef\u63a8\u5ee3\u81f3\u4efb\u610f\u4f4d\u6578\u7684\u4e8c\u9032\u4f4d\u5236\u6578\u3002</p>"},{"location":"chapter_divide_and_conquer/","title":"\u7b2c 12 \u7ae0 \u00a0 \u5206\u6cbb","text":"<p>Abstract</p> <p>\u96e3\u984c\u88ab\u9010\u5c64\u62c6\u89e3\uff0c\u6bcf\u4e00\u6b21\u7684\u62c6\u89e3\u90fd\u4f7f\u5b83\u8b8a\u5f97\u66f4\u70ba\u7c21\u55ae\u3002</p> <p>\u5206\u800c\u6cbb\u4e4b\u63ed\u793a\u4e86\u4e00\u500b\u91cd\u8981\u7684\u4e8b\u5be6\uff1a\u5f9e\u7c21\u55ae\u505a\u8d77\uff0c\u4e00\u5207\u90fd\u4e0d\u518d\u8907\u96dc\u3002</p>"},{"location":"chapter_divide_and_conquer/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>12.1 \u00a0 \u5206\u6cbb\u6f14\u7b97\u6cd5</li> <li>12.2 \u00a0 \u5206\u6cbb\u641c\u5c0b\u7b56\u7565</li> <li>12.3 \u00a0 \u69cb\u5efa\u6a39\u554f\u984c</li> <li>12.4 \u00a0 \u6cb3\u5167\u5854\u554f\u984c</li> <li>12.5 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_divide_and_conquer/binary_search_recur/","title":"12.2 \u00a0 \u5206\u6cbb\u641c\u5c0b\u7b56\u7565","text":"<p>\u6211\u5011\u5df2\u7d93\u5b78\u904e\uff0c\u641c\u5c0b\u6f14\u7b97\u6cd5\u5206\u70ba\u5169\u5927\u985e\u3002</p> <ul> <li>\u66b4\u529b\u641c\u5c0b\uff1a\u5b83\u900f\u904e\u8d70\u8a2a\u8cc7\u6599\u7d50\u69cb\u5be6\u73fe\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \u3002</li> <li>\u81ea\u9069\u61c9\u641c\u5c0b\uff1a\u5b83\u5229\u7528\u7279\u6709\u7684\u8cc7\u6599\u7d44\u7e54\u5f62\u5f0f\u6216\u5148\u9a57\u8cc7\u8a0a\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u53ef\u9054\u5230 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \u3002</li> </ul> <p>\u5be6\u969b\u4e0a\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(\\log n)\\) \u7684\u641c\u5c0b\u6f14\u7b97\u6cd5\u901a\u5e38\u662f\u57fa\u65bc\u5206\u6cbb\u7b56\u7565\u5be6\u73fe\u7684\uff0c\u4f8b\u5982\u4e8c\u5206\u641c\u5c0b\u548c\u6a39\u3002</p> <ul> <li>\u4e8c\u5206\u641c\u5c0b\u7684\u6bcf\u4e00\u6b65\u90fd\u5c07\u554f\u984c\uff08\u5728\u9663\u5217\u4e2d\u641c\u7d22\u76ee\u6a19\u5143\u7d20\uff09\u5206\u89e3\u70ba\u4e00\u500b\u5c0f\u554f\u984c\uff08\u5728\u9663\u5217\u7684\u4e00\u534a\u4e2d\u641c\u7d22\u76ee\u6a19\u5143\u7d20\uff09\uff0c\u9019\u500b\u904e\u7a0b\u4e00\u76f4\u6301\u7e8c\u5230\u9663\u5217\u70ba\u7a7a\u6216\u627e\u5230\u76ee\u6a19\u5143\u7d20\u70ba\u6b62\u3002</li> <li>\u6a39\u662f\u5206\u6cbb\u601d\u60f3\u7684\u4ee3\u8868\uff0c\u5728\u4e8c\u5143\u641c\u5c0b\u6a39\u3001AVL \u6a39\u3001\u5806\u7a4d\u7b49\u8cc7\u6599\u7d50\u69cb\u4e2d\uff0c\u5404\u7a2e\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u7686\u70ba \\(O(\\log n)\\) \u3002</li> </ul> <p>\u4e8c\u5206\u641c\u5c0b\u7684\u5206\u6cbb\u7b56\u7565\u5982\u4e0b\u6240\u793a\u3002</p> <ul> <li>\u554f\u984c\u53ef\u4ee5\u5206\u89e3\uff1a\u4e8c\u5206\u641c\u5c0b\u905e\u8ff4\u5730\u5c07\u539f\u554f\u984c\uff08\u5728\u9663\u5217\u4e2d\u9032\u884c\u67e5\u8a62\uff09\u5206\u89e3\u70ba\u5b50\u554f\u984c\uff08\u5728\u9663\u5217\u7684\u4e00\u534a\u4e2d\u9032\u884c\u67e5\u8a62\uff09\uff0c\u9019\u662f\u900f\u904e\u6bd4\u8f03\u4e2d\u9593\u5143\u7d20\u548c\u76ee\u6a19\u5143\u7d20\u4f86\u5be6\u73fe\u7684\u3002</li> <li>\u5b50\u554f\u984c\u662f\u7368\u7acb\u7684\uff1a\u5728\u4e8c\u5206\u641c\u5c0b\u4e2d\uff0c\u6bcf\u8f2a\u53ea\u8655\u7406\u4e00\u500b\u5b50\u554f\u984c\uff0c\u5b83\u4e0d\u53d7\u5176\u4ed6\u5b50\u554f\u984c\u7684\u5f71\u97ff\u3002</li> <li>\u5b50\u554f\u984c\u7684\u89e3\u7121\u9808\u5408\u4f75\uff1a\u4e8c\u5206\u641c\u5c0b\u65e8\u5728\u67e5\u8a62\u4e00\u500b\u7279\u5b9a\u5143\u7d20\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5c07\u5b50\u554f\u984c\u7684\u89e3\u9032\u884c\u5408\u4f75\u3002\u7576\u5b50\u554f\u984c\u5f97\u5230\u89e3\u6c7a\u6642\uff0c\u539f\u554f\u984c\u4e5f\u6703\u540c\u6642\u5f97\u5230\u89e3\u6c7a\u3002</li> </ul> <p>\u5206\u6cbb\u80fd\u5920\u63d0\u5347\u641c\u5c0b\u6548\u7387\uff0c\u672c\u8cea\u4e0a\u662f\u56e0\u70ba\u66b4\u529b\u641c\u5c0b\u6bcf\u8f2a\u53ea\u80fd\u6392\u9664\u4e00\u500b\u9078\u9805\uff0c\u800c\u5206\u6cbb\u641c\u5c0b\u6bcf\u8f2a\u53ef\u4ee5\u6392\u9664\u4e00\u534a\u9078\u9805\u3002</p>"},{"location":"chapter_divide_and_conquer/binary_search_recur/#1","title":"1. \u00a0 \u57fa\u65bc\u5206\u6cbb\u5be6\u73fe\u4e8c\u5206\u641c\u5c0b","text":"<p>\u5728\u4e4b\u524d\u7684\u7ae0\u7bc0\u4e2d\uff0c\u4e8c\u5206\u641c\u5c0b\u662f\u57fa\u65bc\u905e\u63a8\uff08\u8fed\u4ee3\uff09\u5be6\u73fe\u7684\u3002\u73fe\u5728\u6211\u5011\u57fa\u65bc\u5206\u6cbb\uff08\u905e\u8ff4\uff09\u4f86\u5be6\u73fe\u5b83\u3002</p> <p>Question</p> <p>\u7d66\u5b9a\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u6709\u5e8f\u9663\u5217 <code>nums</code> \uff0c\u5176\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u662f\u552f\u4e00\u7684\uff0c\u8acb\u67e5\u8a62\u5143\u7d20 <code>target</code> \u3002</p> <p>\u5f9e\u5206\u6cbb\u89d2\u5ea6\uff0c\u6211\u5011\u5c07\u641c\u5c0b\u5340\u9593 \\([i, j]\\) \u5c0d\u61c9\u7684\u5b50\u554f\u984c\u8a18\u70ba \\(f(i, j)\\) \u3002</p> <p>\u4ee5\u539f\u554f\u984c \\(f(0, n-1)\\) \u70ba\u8d77\u59cb\u9ede\uff0c\u900f\u904e\u4ee5\u4e0b\u6b65\u9a5f\u9032\u884c\u4e8c\u5206\u641c\u5c0b\u3002</p> <ol> <li>\u8a08\u7b97\u641c\u5c0b\u5340\u9593 \\([i, j]\\) \u7684\u4e2d\u9ede \\(m\\) \uff0c\u6839\u64da\u5b83\u6392\u9664\u4e00\u534a\u641c\u5c0b\u5340\u9593\u3002</li> <li>\u905e\u8ff4\u6c42\u89e3\u898f\u6a21\u6e1b\u5c0f\u4e00\u534a\u7684\u5b50\u554f\u984c\uff0c\u53ef\u80fd\u70ba \\(f(i, m-1)\\) \u6216 \\(f(m+1, j)\\) \u3002</li> <li>\u8ff4\u5708\u7b2c <code>1.</code> \u6b65\u548c\u7b2c <code>2.</code> \u6b65\uff0c\u76f4\u81f3\u627e\u5230 <code>target</code> \u6216\u5340\u9593\u70ba\u7a7a\u6642\u8fd4\u56de\u3002</li> </ol> <p>\u5716 12-4 \u5c55\u793a\u4e86\u5728\u9663\u5217\u4e2d\u4e8c\u5206\u641c\u5c0b\u5143\u7d20 \\(6\\) \u7684\u5206\u6cbb\u904e\u7a0b\u3002</p> <p></p> <p> \u5716 12-4 \u00a0 \u4e8c\u5206\u641c\u5c0b\u7684\u5206\u6cbb\u904e\u7a0b </p> <p>\u5728\u5be6\u73fe\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5ba3\u544a\u4e00\u500b\u905e\u8ff4\u51fd\u5f0f <code>dfs()</code> \u4f86\u6c42\u89e3\u554f\u984c \\(f(i, j)\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_search_recur.py<pre><code>def dfs(nums: list[int], target: int, i: int, j: int) -&gt; int:\n \"\"\"\u4e8c\u5206\u641c\u5c0b\uff1a\u554f\u984c f(i, j)\"\"\"\n # \u82e5\u5340\u9593\u70ba\u7a7a\uff0c\u4ee3\u8868\u7121\u76ee\u6a19\u5143\u7d20\uff0c\u5247\u8fd4\u56de -1\n if i &gt; j:\n return -1\n # \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n m = (i + j) // 2\n if nums[m] &lt; target:\n # \u905e\u8ff4\u5b50\u554f\u984c f(m+1, j)\n return dfs(nums, target, m + 1, j)\n elif nums[m] &gt; target:\n # \u905e\u8ff4\u5b50\u554f\u984c f(i, m-1)\n return dfs(nums, target, i, m - 1)\n else:\n # \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n\ndef binary_search(nums: list[int], target: int) -&gt; int:\n \"\"\"\u4e8c\u5206\u641c\u5c0b\"\"\"\n n = len(nums)\n # \u6c42\u89e3\u554f\u984c f(0, n-1)\n return dfs(nums, target, 0, n - 1)\n</code></pre> binary_search_recur.cpp<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff1a\u554f\u984c f(i, j) */\nint dfs(vector&lt;int&gt; &amp;nums, int target, int i, int j) {\n // \u82e5\u5340\u9593\u70ba\u7a7a\uff0c\u4ee3\u8868\u7121\u76ee\u6a19\u5143\u7d20\uff0c\u5247\u8fd4\u56de -1\n if (i &gt; j) {\n return -1;\n }\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n int m = (i + j) / 2;\n if (nums[m] &lt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] &gt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u641c\u5c0b */\nint binarySearch(vector&lt;int&gt; &amp;nums, int target) {\n int n = nums.size();\n // \u6c42\u89e3\u554f\u984c f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n</code></pre> binary_search_recur.java<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff1a\u554f\u984c f(i, j) */\nint dfs(int[] nums, int target, int i, int j) {\n // \u82e5\u5340\u9593\u70ba\u7a7a\uff0c\u4ee3\u8868\u7121\u76ee\u6a19\u5143\u7d20\uff0c\u5247\u8fd4\u56de -1\n if (i &gt; j) {\n return -1;\n }\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n int m = (i + j) / 2;\n if (nums[m] &lt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] &gt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u641c\u5c0b */\nint binarySearch(int[] nums, int target) {\n int n = nums.length;\n // \u6c42\u89e3\u554f\u984c f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n</code></pre> binary_search_recur.cs<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff1a\u554f\u984c f(i, j) */\nint DFS(int[] nums, int target, int i, int j) {\n // \u82e5\u5340\u9593\u70ba\u7a7a\uff0c\u4ee3\u8868\u7121\u76ee\u6a19\u5143\u7d20\uff0c\u5247\u8fd4\u56de -1\n if (i &gt; j) {\n return -1;\n }\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n int m = (i + j) / 2;\n if (nums[m] &lt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(m+1, j)\n return DFS(nums, target, m + 1, j);\n } else if (nums[m] &gt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(i, m-1)\n return DFS(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u641c\u5c0b */\nint BinarySearch(int[] nums, int target) {\n int n = nums.Length;\n // \u6c42\u89e3\u554f\u984c f(0, n-1)\n return DFS(nums, target, 0, n - 1);\n}\n</code></pre> binary_search_recur.go<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff1a\u554f\u984c f(i, j) */\nfunc dfs(nums []int, target, i, j int) int {\n // \u5982\u679c\u5340\u9593\u70ba\u7a7a\uff0c\u4ee3\u8868\u6c92\u6709\u76ee\u6a19\u5143\u7d20\uff0c\u5247\u8fd4\u56de -1\n if i &gt; j {\n return -1\n }\n // \u8a08\u7b97\u7d22\u5f15\u4e2d\u9ede\n m := i + ((j - i) &gt;&gt; 1)\n //\u5224\u65b7\u4e2d\u9ede\u8207\u76ee\u6a19\u5143\u7d20\u5927\u5c0f\n if nums[m] &lt; target {\n // \u5c0f\u65bc\u5247\u905e\u8ff4\u53f3\u534a\u9663\u5217\n // \u905e\u8ff4\u5b50\u554f\u984c f(m+1, j)\n return dfs(nums, target, m+1, j)\n } else if nums[m] &gt; target {\n // \u5c0f\u65bc\u5247\u905e\u8ff4\u5de6\u534a\u9663\u5217\n // \u905e\u8ff4\u5b50\u554f\u984c f(i, m-1)\n return dfs(nums, target, i, m-1)\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n}\n\n/* \u4e8c\u5206\u641c\u5c0b */\nfunc binarySearch(nums []int, target int) int {\n n := len(nums)\n return dfs(nums, target, 0, n-1)\n}\n</code></pre> binary_search_recur.swift<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff1a\u554f\u984c f(i, j) */\nfunc dfs(nums: [Int], target: Int, i: Int, j: Int) -&gt; Int {\n // \u82e5\u5340\u9593\u70ba\u7a7a\uff0c\u4ee3\u8868\u7121\u76ee\u6a19\u5143\u7d20\uff0c\u5247\u8fd4\u56de -1\n if i &gt; j {\n return -1\n }\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n let m = (i + j) / 2\n if nums[m] &lt; target {\n // \u905e\u8ff4\u5b50\u554f\u984c f(m+1, j)\n return dfs(nums: nums, target: target, i: m + 1, j: j)\n } else if nums[m] &gt; target {\n // \u905e\u8ff4\u5b50\u554f\u984c f(i, m-1)\n return dfs(nums: nums, target: target, i: i, j: m - 1)\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n}\n\n/* \u4e8c\u5206\u641c\u5c0b */\nfunc binarySearch(nums: [Int], target: Int) -&gt; Int {\n // \u6c42\u89e3\u554f\u984c f(0, n-1)\n dfs(nums: nums, target: target, i: nums.startIndex, j: nums.endIndex - 1)\n}\n</code></pre> binary_search_recur.js<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff1a\u554f\u984c f(i, j) */\nfunction dfs(nums, target, i, j) {\n // \u82e5\u5340\u9593\u70ba\u7a7a\uff0c\u4ee3\u8868\u7121\u76ee\u6a19\u5143\u7d20\uff0c\u5247\u8fd4\u56de -1\n if (i &gt; j) {\n return -1;\n }\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n const m = i + ((j - i) &gt;&gt; 1);\n if (nums[m] &lt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] &gt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u641c\u5c0b */\nfunction binarySearch(nums, target) {\n const n = nums.length;\n // \u6c42\u89e3\u554f\u984c f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n</code></pre> binary_search_recur.ts<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff1a\u554f\u984c f(i, j) */\nfunction dfs(nums: number[], target: number, i: number, j: number): number {\n // \u82e5\u5340\u9593\u70ba\u7a7a\uff0c\u4ee3\u8868\u7121\u76ee\u6a19\u5143\u7d20\uff0c\u5247\u8fd4\u56de -1\n if (i &gt; j) {\n return -1;\n }\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n const m = i + ((j - i) &gt;&gt; 1);\n if (nums[m] &lt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] &gt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u641c\u5c0b */\nfunction binarySearch(nums: number[], target: number): number {\n const n = nums.length;\n // \u6c42\u89e3\u554f\u984c f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n</code></pre> binary_search_recur.dart<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff1a\u554f\u984c f(i, j) */\nint dfs(List&lt;int&gt; nums, int target, int i, int j) {\n // \u82e5\u5340\u9593\u70ba\u7a7a\uff0c\u4ee3\u8868\u7121\u76ee\u6a19\u5143\u7d20\uff0c\u5247\u8fd4\u56de -1\n if (i &gt; j) {\n return -1;\n }\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n int m = (i + j) ~/ 2;\n if (nums[m] &lt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] &gt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u641c\u5c0b */\nint binarySearch(List&lt;int&gt; nums, int target) {\n int n = nums.length;\n // \u6c42\u89e3\u554f\u984c f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n</code></pre> binary_search_recur.rs<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff1a\u554f\u984c f(i, j) */\nfn dfs(nums: &amp;[i32], target: i32, i: i32, j: i32) -&gt; i32 {\n // \u82e5\u5340\u9593\u70ba\u7a7a\uff0c\u4ee3\u8868\u7121\u76ee\u6a19\u5143\u7d20\uff0c\u5247\u8fd4\u56de -1\n if i &gt; j {\n return -1;\n }\n let m: i32 = (i + j) / 2;\n if nums[m as usize] &lt; target {\n // \u905e\u8ff4\u5b50\u554f\u984c f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if nums[m as usize] &gt; target {\n // \u905e\u8ff4\u5b50\u554f\u984c f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u641c\u5c0b */\nfn binary_search(nums: &amp;[i32], target: i32) -&gt; i32 {\n let n = nums.len() as i32;\n // \u6c42\u89e3\u554f\u984c f(0, n-1)\n dfs(nums, target, 0, n - 1)\n}\n</code></pre> binary_search_recur.c<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff1a\u554f\u984c f(i, j) */\nint dfs(int nums[], int target, int i, int j) {\n // \u82e5\u5340\u9593\u70ba\u7a7a\uff0c\u4ee3\u8868\u7121\u76ee\u6a19\u5143\u7d20\uff0c\u5247\u8fd4\u56de -1\n if (i &gt; j) {\n return -1;\n }\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n int m = (i + j) / 2;\n if (nums[m] &lt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(m+1, j)\n return dfs(nums, target, m + 1, j);\n } else if (nums[m] &gt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(i, m-1)\n return dfs(nums, target, i, m - 1);\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n}\n\n/* \u4e8c\u5206\u641c\u5c0b */\nint binarySearch(int nums[], int target, int numsSize) {\n int n = numsSize;\n // \u6c42\u89e3\u554f\u984c f(0, n-1)\n return dfs(nums, target, 0, n - 1);\n}\n</code></pre> binary_search_recur.kt<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff1a\u554f\u984c f(i, j) */\nfun dfs(\n nums: IntArray,\n target: Int,\n i: Int,\n j: Int\n): Int {\n // \u82e5\u5340\u9593\u70ba\u7a7a\uff0c\u4ee3\u8868\u7121\u76ee\u6a19\u5143\u7d20\uff0c\u5247\u8fd4\u56de -1\n if (i &gt; j) {\n return -1\n }\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n val m = (i + j) / 2\n return if (nums[m] &lt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(m+1, j)\n dfs(nums, target, m + 1, j)\n } else if (nums[m] &gt; target) {\n // \u905e\u8ff4\u5b50\u554f\u984c f(i, m-1)\n dfs(nums, target, i, m - 1)\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n m\n }\n}\n\n/* \u4e8c\u5206\u641c\u5c0b */\nfun binarySearch(nums: IntArray, target: Int): Int {\n val n = nums.size\n // \u6c42\u89e3\u554f\u984c f(0, n-1)\n return dfs(nums, target, 0, n - 1)\n}\n</code></pre> binary_search_recur.rb<pre><code>[class]{}-[func]{dfs}\n\n[class]{}-[func]{binary_search}\n</code></pre> binary_search_recur.zig<pre><code>[class]{}-[func]{dfs}\n\n[class]{}-[func]{binarySearch}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/","title":"12.3 \u00a0 \u69cb\u5efa\u4e8c\u5143\u6a39\u554f\u984c","text":"<p>Question</p> <p>\u7d66\u5b9a\u4e00\u68f5\u4e8c\u5143\u6a39\u7684\u524d\u5e8f\u8d70\u8a2a <code>preorder</code> \u548c\u4e2d\u5e8f\u8d70\u8a2a <code>inorder</code> \uff0c\u8acb\u5f9e\u4e2d\u69cb\u5efa\u4e8c\u5143\u6a39\uff0c\u8fd4\u56de\u4e8c\u5143\u6a39\u7684\u6839\u7bc0\u9ede\u3002\u5047\u8a2d\u4e8c\u5143\u6a39\u4e2d\u6c92\u6709\u503c\u91cd\u8907\u7684\u7bc0\u9ede\uff08\u5982\u5716 12-5 \u6240\u793a\uff09\u3002</p> <p></p> <p> \u5716 12-5 \u00a0 \u69cb\u5efa\u4e8c\u5143\u6a39\u7684\u793a\u4f8b\u8cc7\u6599 </p>"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#1","title":"1. \u00a0 \u5224\u65b7\u662f\u5426\u70ba\u5206\u6cbb\u554f\u984c","text":"<p>\u539f\u554f\u984c\u5b9a\u7fa9\u70ba\u5f9e <code>preorder</code> \u548c <code>inorder</code> \u69cb\u5efa\u4e8c\u5143\u6a39\uff0c\u662f\u4e00\u500b\u5178\u578b\u7684\u5206\u6cbb\u554f\u984c\u3002</p> <ul> <li>\u554f\u984c\u53ef\u4ee5\u5206\u89e3\uff1a\u5f9e\u5206\u6cbb\u7684\u89d2\u5ea6\u5207\u5165\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u539f\u554f\u984c\u5283\u5206\u70ba\u5169\u500b\u5b50\u554f\u984c\uff1a\u69cb\u5efa\u5de6\u5b50\u6a39\u3001\u69cb\u5efa\u53f3\u5b50\u6a39\uff0c\u52a0\u4e0a\u4e00\u6b65\u64cd\u4f5c\uff1a\u521d\u59cb\u5316\u6839\u7bc0\u9ede\u3002\u800c\u5c0d\u65bc\u6bcf\u68f5\u5b50\u6a39\uff08\u5b50\u554f\u984c\uff09\uff0c\u6211\u5011\u4ecd\u7136\u53ef\u4ee5\u8907\u7528\u4ee5\u4e0a\u5283\u5206\u65b9\u6cd5\uff0c\u5c07\u5176\u5283\u5206\u70ba\u66f4\u5c0f\u7684\u5b50\u6a39\uff08\u5b50\u554f\u984c\uff09\uff0c\u76f4\u81f3\u9054\u5230\u6700\u5c0f\u5b50\u554f\u984c\uff08\u7a7a\u5b50\u6a39\uff09\u6642\u7d42\u6b62\u3002</li> <li>\u5b50\u554f\u984c\u662f\u7368\u7acb\u7684\uff1a\u5de6\u5b50\u6a39\u548c\u53f3\u5b50\u6a39\u662f\u76f8\u4e92\u7368\u7acb\u7684\uff0c\u5b83\u5011\u4e4b\u9593\u6c92\u6709\u4ea4\u96c6\u3002\u5728\u69cb\u5efa\u5de6\u5b50\u6a39\u6642\uff0c\u6211\u5011\u53ea\u9700\u95dc\u6ce8\u4e2d\u5e8f\u8d70\u8a2a\u548c\u524d\u5e8f\u8d70\u8a2a\u4e2d\u8207\u5de6\u5b50\u6a39\u5c0d\u61c9\u7684\u90e8\u5206\u3002\u53f3\u5b50\u6a39\u540c\u7406\u3002</li> <li>\u5b50\u554f\u984c\u7684\u89e3\u53ef\u4ee5\u5408\u4f75\uff1a\u4e00\u65e6\u5f97\u5230\u4e86\u5de6\u5b50\u6a39\u548c\u53f3\u5b50\u6a39\uff08\u5b50\u554f\u984c\u7684\u89e3\uff09\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u5c07\u5b83\u5011\u9023\u7d50\u5230\u6839\u7bc0\u9ede\u4e0a\uff0c\u5f97\u5230\u539f\u554f\u984c\u7684\u89e3\u3002</li> </ul>"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#2","title":"2. \u00a0 \u5982\u4f55\u5283\u5206\u5b50\u6a39","text":"<p>\u6839\u64da\u4ee5\u4e0a\u5206\u6790\uff0c\u9019\u9053\u984c\u53ef\u4ee5\u4f7f\u7528\u5206\u6cbb\u4f86\u6c42\u89e3\uff0c\u4f46\u5982\u4f55\u900f\u904e\u524d\u5e8f\u8d70\u8a2a <code>preorder</code> \u548c\u4e2d\u5e8f\u8d70\u8a2a <code>inorder</code> \u4f86\u5283\u5206\u5de6\u5b50\u6a39\u548c\u53f3\u5b50\u6a39\u5462\uff1f</p> <p>\u6839\u64da\u5b9a\u7fa9\uff0c<code>preorder</code> \u548c <code>inorder</code> \u90fd\u53ef\u4ee5\u5283\u5206\u70ba\u4e09\u500b\u90e8\u5206\u3002</p> <ul> <li>\u524d\u5e8f\u8d70\u8a2a\uff1a<code>[ \u6839\u7bc0\u9ede | \u5de6\u5b50\u6a39 | \u53f3\u5b50\u6a39 ]</code> \uff0c\u4f8b\u5982\u5716 12-5 \u7684\u6a39\u5c0d\u61c9 <code>[ 3 | 9 | 2 1 7 ]</code> \u3002</li> <li>\u4e2d\u5e8f\u8d70\u8a2a\uff1a<code>[ \u5de6\u5b50\u6a39 | \u6839\u7bc0\u9ede \uff5c \u53f3\u5b50\u6a39 ]</code> \uff0c\u4f8b\u5982\u5716 12-5 \u7684\u6a39\u5c0d\u61c9 <code>[ 9 | 3 | 1 2 7 ]</code> \u3002</li> </ul> <p>\u4ee5\u4e0a\u5716\u8cc7\u6599\u70ba\u4f8b\uff0c\u6211\u5011\u53ef\u4ee5\u900f\u904e\u5716 12-6 \u6240\u793a\u7684\u6b65\u9a5f\u5f97\u5230\u5283\u5206\u7d50\u679c\u3002</p> <ol> <li>\u524d\u5e8f\u8d70\u8a2a\u7684\u9996\u5143\u7d20 3 \u662f\u6839\u7bc0\u9ede\u7684\u503c\u3002</li> <li>\u67e5\u8a62\u6839\u7bc0\u9ede 3 \u5728 <code>inorder</code> \u4e2d\u7684\u7d22\u5f15\uff0c\u5229\u7528\u8a72\u7d22\u5f15\u53ef\u5c07 <code>inorder</code> \u5283\u5206\u70ba <code>[ 9 | 3 \uff5c 1 2 7 ]</code> \u3002</li> <li>\u6839\u64da <code>inorder</code> \u7684\u5283\u5206\u7d50\u679c\uff0c\u6613\u5f97\u5de6\u5b50\u6a39\u548c\u53f3\u5b50\u6a39\u7684\u7bc0\u9ede\u6578\u91cf\u5206\u5225\u70ba 1 \u548c 3 \uff0c\u5f9e\u800c\u53ef\u5c07 <code>preorder</code> \u5283\u5206\u70ba <code>[ 3 | 9 | 2 1 7 ]</code> \u3002</li> </ol> <p></p> <p> \u5716 12-6 \u00a0 \u5728\u524d\u5e8f\u8d70\u8a2a\u548c\u4e2d\u5e8f\u8d70\u8a2a\u4e2d\u5283\u5206\u5b50\u6a39 </p>"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#3","title":"3. \u00a0 \u57fa\u65bc\u8b8a\u6578\u63cf\u8ff0\u5b50\u6a39\u5340\u9593","text":"<p>\u6839\u64da\u4ee5\u4e0a\u5283\u5206\u65b9\u6cd5\uff0c\u6211\u5011\u5df2\u7d93\u5f97\u5230\u6839\u7bc0\u9ede\u3001\u5de6\u5b50\u6a39\u3001\u53f3\u5b50\u6a39\u5728 <code>preorder</code> \u548c <code>inorder</code> \u4e2d\u7684\u7d22\u5f15\u5340\u9593\u3002\u800c\u70ba\u4e86\u63cf\u8ff0\u9019\u4e9b\u7d22\u5f15\u5340\u9593\uff0c\u6211\u5011\u9700\u8981\u85c9\u52a9\u5e7e\u500b\u6307\u6a19\u8b8a\u6578\u3002</p> <ul> <li>\u5c07\u7576\u524d\u6a39\u7684\u6839\u7bc0\u9ede\u5728 <code>preorder</code> \u4e2d\u7684\u7d22\u5f15\u8a18\u70ba \\(i\\) \u3002</li> <li>\u5c07\u7576\u524d\u6a39\u7684\u6839\u7bc0\u9ede\u5728 <code>inorder</code> \u4e2d\u7684\u7d22\u5f15\u8a18\u70ba \\(m\\) \u3002</li> <li>\u5c07\u7576\u524d\u6a39\u5728 <code>inorder</code> \u4e2d\u7684\u7d22\u5f15\u5340\u9593\u8a18\u70ba \\([l, r]\\) \u3002</li> </ul> <p>\u5982\u8868 12-1 \u6240\u793a\uff0c\u900f\u904e\u4ee5\u4e0a\u8b8a\u6578\u5373\u53ef\u8868\u793a\u6839\u7bc0\u9ede\u5728 <code>preorder</code> \u4e2d\u7684\u7d22\u5f15\uff0c\u4ee5\u53ca\u5b50\u6a39\u5728 <code>inorder</code> \u4e2d\u7684\u7d22\u5f15\u5340\u9593\u3002</p> <p> \u8868 12-1 \u00a0 \u6839\u7bc0\u9ede\u548c\u5b50\u6a39\u5728\u524d\u5e8f\u8d70\u8a2a\u548c\u4e2d\u5e8f\u8d70\u8a2a\u4e2d\u7684\u7d22\u5f15 </p> \u6839\u7bc0\u9ede\u5728 <code>preorder</code> \u4e2d\u7684\u7d22\u5f15 \u5b50\u6a39\u5728 <code>inorder</code> \u4e2d\u7684\u7d22\u5f15\u5340\u9593 \u7576\u524d\u6a39 \\(i\\) \\([l, r]\\) \u5de6\u5b50\u6a39 \\(i + 1\\) \\([l, m-1]\\) \u53f3\u5b50\u6a39 \\(i + 1 + (m - l)\\) \\([m+1, r]\\) <p>\u8acb\u6ce8\u610f\uff0c\u53f3\u5b50\u6a39\u6839\u7bc0\u9ede\u7d22\u5f15\u4e2d\u7684 \\((m-l)\\) \u7684\u542b\u7fa9\u662f\u201c\u5de6\u5b50\u6a39\u7684\u7bc0\u9ede\u6578\u91cf\u201d\uff0c\u5efa\u8b70\u7d50\u5408\u5716 12-7 \u7406\u89e3\u3002</p> <p></p> <p> \u5716 12-7 \u00a0 \u6839\u7bc0\u9ede\u548c\u5de6\u53f3\u5b50\u6a39\u7684\u7d22\u5f15\u5340\u9593\u8868\u793a </p>"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#4","title":"4. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"<p>\u70ba\u4e86\u63d0\u5347\u67e5\u8a62 \\(m\\) \u7684\u6548\u7387\uff0c\u6211\u5011\u85c9\u52a9\u4e00\u500b\u96dc\u6e4a\u8868 <code>hmap</code> \u4f86\u5132\u5b58\u9663\u5217 <code>inorder</code> \u4e2d\u5143\u7d20\u5230\u7d22\u5f15\u7684\u5c0d\u6620\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig build_tree.py<pre><code>def dfs(\n preorder: list[int],\n inorder_map: dict[int, int],\n i: int,\n l: int,\n r: int,\n) -&gt; TreeNode | None:\n \"\"\"\u69cb\u5efa\u4e8c\u5143\u6a39\uff1a\u5206\u6cbb\"\"\"\n # \u5b50\u6a39\u5340\u9593\u70ba\u7a7a\u6642\u7d42\u6b62\n if r - l &lt; 0:\n return None\n # \u521d\u59cb\u5316\u6839\u7bc0\u9ede\n root = TreeNode(preorder[i])\n # \u67e5\u8a62 m \uff0c\u5f9e\u800c\u5283\u5206\u5de6\u53f3\u5b50\u6a39\n m = inorder_map[preorder[i]]\n # \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u5de6\u5b50\u6a39\n root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n # \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u53f3\u5b50\u6a39\n root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n # \u8fd4\u56de\u6839\u7bc0\u9ede\n return root\n\ndef build_tree(preorder: list[int], inorder: list[int]) -&gt; TreeNode | None:\n \"\"\"\u69cb\u5efa\u4e8c\u5143\u6a39\"\"\"\n # \u521d\u59cb\u5316\u96dc\u6e4a\u8868\uff0c\u5132\u5b58 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u5c0d\u6620\n inorder_map = {val: i for i, val in enumerate(inorder)}\n root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)\n return root\n</code></pre> build_tree.cpp<pre><code>/* \u69cb\u5efa\u4e8c\u5143\u6a39\uff1a\u5206\u6cbb */\nTreeNode *dfs(vector&lt;int&gt; &amp;preorder, unordered_map&lt;int, int&gt; &amp;inorderMap, int i, int l, int r) {\n // \u5b50\u6a39\u5340\u9593\u70ba\u7a7a\u6642\u7d42\u6b62\n if (r - l &lt; 0)\n return NULL;\n // \u521d\u59cb\u5316\u6839\u7bc0\u9ede\n TreeNode *root = new TreeNode(preorder[i]);\n // \u67e5\u8a62 m \uff0c\u5f9e\u800c\u5283\u5206\u5de6\u53f3\u5b50\u6a39\n int m = inorderMap[preorder[i]];\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u5de6\u5b50\u6a39\n root-&gt;left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u53f3\u5b50\u6a39\n root-&gt;right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u7bc0\u9ede\n return root;\n}\n\n/* \u69cb\u5efa\u4e8c\u5143\u6a39 */\nTreeNode *buildTree(vector&lt;int&gt; &amp;preorder, vector&lt;int&gt; &amp;inorder) {\n // \u521d\u59cb\u5316\u96dc\u6e4a\u8868\uff0c\u5132\u5b58 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u5c0d\u6620\n unordered_map&lt;int, int&gt; inorderMap;\n for (int i = 0; i &lt; inorder.size(); i++) {\n inorderMap[inorder[i]] = i;\n }\n TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1);\n return root;\n}\n</code></pre> build_tree.java<pre><code>/* \u69cb\u5efa\u4e8c\u5143\u6a39\uff1a\u5206\u6cbb */\nTreeNode dfs(int[] preorder, Map&lt;Integer, Integer&gt; inorderMap, int i, int l, int r) {\n // \u5b50\u6a39\u5340\u9593\u70ba\u7a7a\u6642\u7d42\u6b62\n if (r - l &lt; 0)\n return null;\n // \u521d\u59cb\u5316\u6839\u7bc0\u9ede\n TreeNode root = new TreeNode(preorder[i]);\n // \u67e5\u8a62 m \uff0c\u5f9e\u800c\u5283\u5206\u5de6\u53f3\u5b50\u6a39\n int m = inorderMap.get(preorder[i]);\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u5de6\u5b50\u6a39\n root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u53f3\u5b50\u6a39\n root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u7bc0\u9ede\n return root;\n}\n\n/* \u69cb\u5efa\u4e8c\u5143\u6a39 */\nTreeNode buildTree(int[] preorder, int[] inorder) {\n // \u521d\u59cb\u5316\u96dc\u6e4a\u8868\uff0c\u5132\u5b58 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u5c0d\u6620\n Map&lt;Integer, Integer&gt; inorderMap = new HashMap&lt;&gt;();\n for (int i = 0; i &lt; inorder.length; i++) {\n inorderMap.put(inorder[i], i);\n }\n TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n return root;\n}\n</code></pre> build_tree.cs<pre><code>/* \u69cb\u5efa\u4e8c\u5143\u6a39\uff1a\u5206\u6cbb */\nTreeNode? DFS(int[] preorder, Dictionary&lt;int, int&gt; inorderMap, int i, int l, int r) {\n // \u5b50\u6a39\u5340\u9593\u70ba\u7a7a\u6642\u7d42\u6b62\n if (r - l &lt; 0)\n return null;\n // \u521d\u59cb\u5316\u6839\u7bc0\u9ede\n TreeNode root = new(preorder[i]);\n // \u67e5\u8a62 m \uff0c\u5f9e\u800c\u5283\u5206\u5de6\u53f3\u5b50\u6a39\n int m = inorderMap[preorder[i]];\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u5de6\u5b50\u6a39\n root.left = DFS(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u53f3\u5b50\u6a39\n root.right = DFS(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u7bc0\u9ede\n return root;\n}\n\n/* \u69cb\u5efa\u4e8c\u5143\u6a39 */\nTreeNode? BuildTree(int[] preorder, int[] inorder) {\n // \u521d\u59cb\u5316\u96dc\u6e4a\u8868\uff0c\u5132\u5b58 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u5c0d\u6620\n Dictionary&lt;int, int&gt; inorderMap = [];\n for (int i = 0; i &lt; inorder.Length; i++) {\n inorderMap.TryAdd(inorder[i], i);\n }\n TreeNode? root = DFS(preorder, inorderMap, 0, 0, inorder.Length - 1);\n return root;\n}\n</code></pre> build_tree.go<pre><code>/* \u69cb\u5efa\u4e8c\u5143\u6a39\uff1a\u5206\u6cbb */\nfunc dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode {\n // \u5b50\u6a39\u5340\u9593\u70ba\u7a7a\u6642\u7d42\u6b62\n if r-l &lt; 0 {\n return nil\n }\n // \u521d\u59cb\u5316\u6839\u7bc0\u9ede\n root := NewTreeNode(preorder[i])\n // \u67e5\u8a62 m \uff0c\u5f9e\u800c\u5283\u5206\u5de6\u53f3\u5b50\u6a39\n m := inorderMap[preorder[i]]\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u5de6\u5b50\u6a39\n root.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1)\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u53f3\u5b50\u6a39\n root.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r)\n // \u8fd4\u56de\u6839\u7bc0\u9ede\n return root\n}\n\n/* \u69cb\u5efa\u4e8c\u5143\u6a39 */\nfunc buildTree(preorder, inorder []int) *TreeNode {\n // \u521d\u59cb\u5316\u96dc\u6e4a\u8868\uff0c\u5132\u5b58 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u5c0d\u6620\n inorderMap := make(map[int]int, len(inorder))\n for i := 0; i &lt; len(inorder); i++ {\n inorderMap[inorder[i]] = i\n }\n\n root := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1)\n return root\n}\n</code></pre> build_tree.swift<pre><code>/* \u69cb\u5efa\u4e8c\u5143\u6a39\uff1a\u5206\u6cbb */\nfunc dfs(preorder: [Int], inorderMap: [Int: Int], i: Int, l: Int, r: Int) -&gt; TreeNode? {\n // \u5b50\u6a39\u5340\u9593\u70ba\u7a7a\u6642\u7d42\u6b62\n if r - l &lt; 0 {\n return nil\n }\n // \u521d\u59cb\u5316\u6839\u7bc0\u9ede\n let root = TreeNode(x: preorder[i])\n // \u67e5\u8a62 m \uff0c\u5f9e\u800c\u5283\u5206\u5de6\u53f3\u5b50\u6a39\n let m = inorderMap[preorder[i]]!\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u5de6\u5b50\u6a39\n root.left = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1, l: l, r: m - 1)\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u53f3\u5b50\u6a39\n root.right = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1 + m - l, l: m + 1, r: r)\n // \u8fd4\u56de\u6839\u7bc0\u9ede\n return root\n}\n\n/* \u69cb\u5efa\u4e8c\u5143\u6a39 */\nfunc buildTree(preorder: [Int], inorder: [Int]) -&gt; TreeNode? {\n // \u521d\u59cb\u5316\u96dc\u6e4a\u8868\uff0c\u5132\u5b58 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u5c0d\u6620\n let inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }\n return dfs(preorder: preorder, inorderMap: inorderMap, i: inorder.startIndex, l: inorder.startIndex, r: inorder.endIndex - 1)\n}\n</code></pre> build_tree.js<pre><code>/* \u69cb\u5efa\u4e8c\u5143\u6a39\uff1a\u5206\u6cbb */\nfunction dfs(preorder, inorderMap, i, l, r) {\n // \u5b50\u6a39\u5340\u9593\u70ba\u7a7a\u6642\u7d42\u6b62\n if (r - l &lt; 0) return null;\n // \u521d\u59cb\u5316\u6839\u7bc0\u9ede\n const root = new TreeNode(preorder[i]);\n // \u67e5\u8a62 m \uff0c\u5f9e\u800c\u5283\u5206\u5de6\u53f3\u5b50\u6a39\n const m = inorderMap.get(preorder[i]);\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u5de6\u5b50\u6a39\n root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u53f3\u5b50\u6a39\n root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u7bc0\u9ede\n return root;\n}\n\n/* \u69cb\u5efa\u4e8c\u5143\u6a39 */\nfunction buildTree(preorder, inorder) {\n // \u521d\u59cb\u5316\u96dc\u6e4a\u8868\uff0c\u5132\u5b58 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u5c0d\u6620\n let inorderMap = new Map();\n for (let i = 0; i &lt; inorder.length; i++) {\n inorderMap.set(inorder[i], i);\n }\n const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n return root;\n}\n</code></pre> build_tree.ts<pre><code>/* \u69cb\u5efa\u4e8c\u5143\u6a39\uff1a\u5206\u6cbb */\nfunction dfs(\n preorder: number[],\n inorderMap: Map&lt;number, number&gt;,\n i: number,\n l: number,\n r: number\n): TreeNode | null {\n // \u5b50\u6a39\u5340\u9593\u70ba\u7a7a\u6642\u7d42\u6b62\n if (r - l &lt; 0) return null;\n // \u521d\u59cb\u5316\u6839\u7bc0\u9ede\n const root: TreeNode = new TreeNode(preorder[i]);\n // \u67e5\u8a62 m \uff0c\u5f9e\u800c\u5283\u5206\u5de6\u53f3\u5b50\u6a39\n const m = inorderMap.get(preorder[i]);\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u5de6\u5b50\u6a39\n root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u53f3\u5b50\u6a39\n root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u7bc0\u9ede\n return root;\n}\n\n/* \u69cb\u5efa\u4e8c\u5143\u6a39 */\nfunction buildTree(preorder: number[], inorder: number[]): TreeNode | null {\n // \u521d\u59cb\u5316\u96dc\u6e4a\u8868\uff0c\u5132\u5b58 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u5c0d\u6620\n let inorderMap = new Map&lt;number, number&gt;();\n for (let i = 0; i &lt; inorder.length; i++) {\n inorderMap.set(inorder[i], i);\n }\n const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n return root;\n}\n</code></pre> build_tree.dart<pre><code>/* \u69cb\u5efa\u4e8c\u5143\u6a39\uff1a\u5206\u6cbb */\nTreeNode? dfs(\n List&lt;int&gt; preorder,\n Map&lt;int, int&gt; inorderMap,\n int i,\n int l,\n int r,\n) {\n // \u5b50\u6a39\u5340\u9593\u70ba\u7a7a\u6642\u7d42\u6b62\n if (r - l &lt; 0) {\n return null;\n }\n // \u521d\u59cb\u5316\u6839\u7bc0\u9ede\n TreeNode? root = TreeNode(preorder[i]);\n // \u67e5\u8a62 m \uff0c\u5f9e\u800c\u5283\u5206\u5de6\u53f3\u5b50\u6a39\n int m = inorderMap[preorder[i]]!;\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u5de6\u5b50\u6a39\n root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u53f3\u5b50\u6a39\n root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u7bc0\u9ede\n return root;\n}\n\n/* \u69cb\u5efa\u4e8c\u5143\u6a39 */\nTreeNode? buildTree(List&lt;int&gt; preorder, List&lt;int&gt; inorder) {\n // \u521d\u59cb\u5316\u96dc\u6e4a\u8868\uff0c\u5132\u5b58 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u5c0d\u6620\n Map&lt;int, int&gt; inorderMap = {};\n for (int i = 0; i &lt; inorder.length; i++) {\n inorderMap[inorder[i]] = i;\n }\n TreeNode? root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n return root;\n}\n</code></pre> build_tree.rs<pre><code>/* \u69cb\u5efa\u4e8c\u5143\u6a39\uff1a\u5206\u6cbb */\nfn dfs(\n preorder: &amp;[i32],\n inorder_map: &amp;HashMap&lt;i32, i32&gt;,\n i: i32,\n l: i32,\n r: i32,\n) -&gt; Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt; {\n // \u5b50\u6a39\u5340\u9593\u70ba\u7a7a\u6642\u7d42\u6b62\n if r - l &lt; 0 {\n return None;\n }\n // \u521d\u59cb\u5316\u6839\u7bc0\u9ede\n let root = TreeNode::new(preorder[i as usize]);\n // \u67e5\u8a62 m \uff0c\u5f9e\u800c\u5283\u5206\u5de6\u53f3\u5b50\u6a39\n let m = inorder_map.get(&amp;preorder[i as usize]).unwrap();\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u5de6\u5b50\u6a39\n root.borrow_mut().left = dfs(preorder, inorder_map, i + 1, l, m - 1);\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u53f3\u5b50\u6a39\n root.borrow_mut().right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r);\n // \u8fd4\u56de\u6839\u7bc0\u9ede\n Some(root)\n}\n\n/* \u69cb\u5efa\u4e8c\u5143\u6a39 */\nfn build_tree(preorder: &amp;[i32], inorder: &amp;[i32]) -&gt; Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt; {\n // \u521d\u59cb\u5316\u96dc\u6e4a\u8868\uff0c\u5132\u5b58 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u5c0d\u6620\n let mut inorder_map: HashMap&lt;i32, i32&gt; = HashMap::new();\n for i in 0..inorder.len() {\n inorder_map.insert(inorder[i], i as i32);\n }\n let root = dfs(preorder, &amp;inorder_map, 0, 0, inorder.len() as i32 - 1);\n root\n}\n</code></pre> build_tree.c<pre><code>/* \u69cb\u5efa\u4e8c\u5143\u6a39\uff1a\u5206\u6cbb */\nTreeNode *dfs(int *preorder, int *inorderMap, int i, int l, int r, int size) {\n // \u5b50\u6a39\u5340\u9593\u70ba\u7a7a\u6642\u7d42\u6b62\n if (r - l &lt; 0)\n return NULL;\n // \u521d\u59cb\u5316\u6839\u7bc0\u9ede\n TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));\n root-&gt;val = preorder[i];\n root-&gt;left = NULL;\n root-&gt;right = NULL;\n // \u67e5\u8a62 m \uff0c\u5f9e\u800c\u5283\u5206\u5de6\u53f3\u5b50\u6a39\n int m = inorderMap[preorder[i]];\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u5de6\u5b50\u6a39\n root-&gt;left = dfs(preorder, inorderMap, i + 1, l, m - 1, size);\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u53f3\u5b50\u6a39\n root-&gt;right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r, size);\n // \u8fd4\u56de\u6839\u7bc0\u9ede\n return root;\n}\n\n/* \u69cb\u5efa\u4e8c\u5143\u6a39 */\nTreeNode *buildTree(int *preorder, int preorderSize, int *inorder, int inorderSize) {\n // \u521d\u59cb\u5316\u96dc\u6e4a\u8868\uff0c\u5132\u5b58 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u5c0d\u6620\n int *inorderMap = (int *)malloc(sizeof(int) * MAX_SIZE);\n for (int i = 0; i &lt; inorderSize; i++) {\n inorderMap[inorder[i]] = i;\n }\n TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorderSize - 1, inorderSize);\n free(inorderMap);\n return root;\n}\n</code></pre> build_tree.kt<pre><code>/* \u69cb\u5efa\u4e8c\u5143\u6a39\uff1a\u5206\u6cbb */\nfun dfs(\n preorder: IntArray,\n inorderMap: Map&lt;Int?, Int?&gt;,\n i: Int,\n l: Int,\n r: Int\n): TreeNode? {\n // \u5b50\u6a39\u5340\u9593\u70ba\u7a7a\u6642\u7d42\u6b62\n if (r - l &lt; 0) return null\n // \u521d\u59cb\u5316\u6839\u7bc0\u9ede\n val root = TreeNode(preorder[i])\n // \u67e5\u8a62 m \uff0c\u5f9e\u800c\u5283\u5206\u5de6\u53f3\u5b50\u6a39\n val m = inorderMap[preorder[i]]!!\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u5de6\u5b50\u6a39\n root.left = dfs(preorder, inorderMap, i + 1, l, m - 1)\n // \u5b50\u554f\u984c\uff1a\u69cb\u5efa\u53f3\u5b50\u6a39\n root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r)\n // \u8fd4\u56de\u6839\u7bc0\u9ede\n return root\n}\n\n/* \u69cb\u5efa\u4e8c\u5143\u6a39 */\nfun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {\n // \u521d\u59cb\u5316\u96dc\u6e4a\u8868\uff0c\u5132\u5b58 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u5c0d\u6620\n val inorderMap = HashMap&lt;Int?, Int?&gt;()\n for (i in inorder.indices) {\n inorderMap[inorder[i]] = i\n }\n val root = dfs(preorder, inorderMap, 0, 0, inorder.size - 1)\n return root\n}\n</code></pre> build_tree.rb<pre><code>[class]{}-[func]{dfs}\n\n[class]{}-[func]{build_tree}\n</code></pre> build_tree.zig<pre><code>[class]{}-[func]{dfs}\n\n[class]{}-[func]{buildTree}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 12-8 \u5c55\u793a\u4e86\u69cb\u5efa\u4e8c\u5143\u6a39\u7684\u905e\u8ff4\u904e\u7a0b\uff0c\u5404\u500b\u7bc0\u9ede\u662f\u5728\u5411\u4e0b\u201c\u905e\u201d\u7684\u904e\u7a0b\u4e2d\u5efa\u7acb\u7684\uff0c\u800c\u5404\u689d\u908a\uff08\u5f15\u7528\uff09\u662f\u5728\u5411\u4e0a\u201c\u8ff4\u201d\u7684\u904e\u7a0b\u4e2d\u5efa\u7acb\u7684\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 12-8 \u00a0 \u69cb\u5efa\u4e8c\u5143\u6a39\u7684\u905e\u8ff4\u904e\u7a0b </p> <p>\u6bcf\u500b\u905e\u8ff4\u51fd\u5f0f\u5167\u7684\u524d\u5e8f\u8d70\u8a2a <code>preorder</code> \u548c\u4e2d\u5e8f\u8d70\u8a2a <code>inorder</code> \u7684\u5283\u5206\u7d50\u679c\u5982\u5716 12-9 \u6240\u793a\u3002</p> <p></p> <p> \u5716 12-9 \u00a0 \u6bcf\u500b\u905e\u8ff4\u51fd\u5f0f\u4e2d\u7684\u5283\u5206\u7d50\u679c </p> <p>\u8a2d\u6a39\u7684\u7bc0\u9ede\u6578\u91cf\u70ba \\(n\\) \uff0c\u521d\u59cb\u5316\u6bcf\u4e00\u500b\u7bc0\u9ede\uff08\u57f7\u884c\u4e00\u500b\u905e\u8ff4\u51fd\u5f0f <code>dfs()</code> \uff09\u4f7f\u7528 \\(O(1)\\) \u6642\u9593\u3002\u56e0\u6b64\u7e3d\u9ad4\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \u3002</p> <p>\u96dc\u6e4a\u8868\u5132\u5b58 <code>inorder</code> \u5143\u7d20\u5230\u7d22\u5f15\u7684\u5c0d\u6620\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \u3002\u5728\u6700\u5dee\u60c5\u6cc1\u4e0b\uff0c\u5373\u4e8c\u5143\u6a39\u9000\u5316\u70ba\u93c8\u7d50\u4e32\u5217\u6642\uff0c\u905e\u8ff4\u6df1\u5ea6\u9054\u5230 \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u7684\u5806\u758a\u5e40\u7a7a\u9593\u3002\u56e0\u6b64\u7e3d\u9ad4\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \u3002</p>"},{"location":"chapter_divide_and_conquer/divide_and_conquer/","title":"12.1 \u00a0 \u5206\u6cbb\u6f14\u7b97\u6cd5","text":"<p>\u5206\u6cbb\uff08divide and conquer\uff09\uff0c\u5168\u7a31\u5206\u800c\u6cbb\u4e4b\uff0c\u662f\u4e00\u7a2e\u975e\u5e38\u91cd\u8981\u4e14\u5e38\u898b\u7684\u6f14\u7b97\u6cd5\u7b56\u7565\u3002\u5206\u6cbb\u901a\u5e38\u57fa\u65bc\u905e\u8ff4\u5be6\u73fe\uff0c\u5305\u62ec\u201c\u5206\u201d\u548c\u201c\u6cbb\u201d\u5169\u500b\u6b65\u9a5f\u3002</p> <ol> <li>\u5206\uff08\u5283\u5206\u968e\u6bb5\uff09\uff1a\u905e\u8ff4\u5730\u5c07\u539f\u554f\u984c\u5206\u89e3\u70ba\u5169\u500b\u6216\u591a\u500b\u5b50\u554f\u984c\uff0c\u76f4\u81f3\u5230\u9054\u6700\u5c0f\u5b50\u554f\u984c\u6642\u7d42\u6b62\u3002</li> <li>\u6cbb\uff08\u5408\u4f75\u968e\u6bb5\uff09\uff1a\u5f9e\u5df2\u77e5\u89e3\u7684\u6700\u5c0f\u5b50\u554f\u984c\u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5730\u5c07\u5b50\u554f\u984c\u7684\u89e3\u9032\u884c\u5408\u4f75\uff0c\u5f9e\u800c\u69cb\u5efa\u51fa\u539f\u554f\u984c\u7684\u89e3\u3002</li> </ol> <p>\u5982\u5716 12-1 \u6240\u793a\uff0c\u201c\u5408\u4f75\u6392\u5e8f\u201d\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u61c9\u7528\u4e4b\u4e00\u3002</p> <ol> <li>\u5206\uff1a\u905e\u8ff4\u5730\u5c07\u539f\u9663\u5217\uff08\u539f\u554f\u984c\uff09\u5283\u5206\u70ba\u5169\u500b\u5b50\u9663\u5217\uff08\u5b50\u554f\u984c\uff09\uff0c\u76f4\u5230\u5b50\u9663\u5217\u53ea\u5269\u4e00\u500b\u5143\u7d20\uff08\u6700\u5c0f\u5b50\u554f\u984c\uff09\u3002</li> <li>\u6cbb\uff1a\u5f9e\u5e95\u81f3\u9802\u5730\u5c07\u6709\u5e8f\u7684\u5b50\u9663\u5217\uff08\u5b50\u554f\u984c\u7684\u89e3\uff09\u9032\u884c\u5408\u4f75\uff0c\u5f9e\u800c\u5f97\u5230\u6709\u5e8f\u7684\u539f\u9663\u5217\uff08\u539f\u554f\u984c\u7684\u89e3\uff09\u3002</li> </ol> <p></p> <p> \u5716 12-1 \u00a0 \u5408\u4f75\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565 </p>"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1211","title":"12.1.1 \u00a0 \u5982\u4f55\u5224\u65b7\u5206\u6cbb\u554f\u984c","text":"<p>\u4e00\u500b\u554f\u984c\u662f\u5426\u9069\u5408\u4f7f\u7528\u5206\u6cbb\u89e3\u6c7a\uff0c\u901a\u5e38\u53ef\u4ee5\u53c3\u8003\u4ee5\u4e0b\u5e7e\u500b\u5224\u65b7\u4f9d\u64da\u3002</p> <ol> <li>\u554f\u984c\u53ef\u4ee5\u5206\u89e3\uff1a\u539f\u554f\u984c\u53ef\u4ee5\u5206\u89e3\u6210\u898f\u6a21\u66f4\u5c0f\u3001\u985e\u4f3c\u7684\u5b50\u554f\u984c\uff0c\u4ee5\u53ca\u80fd\u5920\u4ee5\u76f8\u540c\u65b9\u5f0f\u905e\u8ff4\u5730\u9032\u884c\u5283\u5206\u3002</li> <li>\u5b50\u554f\u984c\u662f\u7368\u7acb\u7684\uff1a\u5b50\u554f\u984c\u4e4b\u9593\u6c92\u6709\u91cd\u758a\uff0c\u4e92\u4e0d\u4f9d\u8cf4\uff0c\u53ef\u4ee5\u7368\u7acb\u89e3\u6c7a\u3002</li> <li>\u5b50\u554f\u984c\u7684\u89e3\u53ef\u4ee5\u5408\u4f75\uff1a\u539f\u554f\u984c\u7684\u89e3\u900f\u904e\u5408\u4f75\u5b50\u554f\u984c\u7684\u89e3\u5f97\u4f86\u3002</li> </ol> <p>\u986f\u7136\uff0c\u5408\u4f75\u6392\u5e8f\u6eff\u8db3\u4ee5\u4e0a\u4e09\u500b\u5224\u65b7\u4f9d\u64da\u3002</p> <ol> <li>\u554f\u984c\u53ef\u4ee5\u5206\u89e3\uff1a\u905e\u8ff4\u5730\u5c07\u9663\u5217\uff08\u539f\u554f\u984c\uff09\u5283\u5206\u70ba\u5169\u500b\u5b50\u9663\u5217\uff08\u5b50\u554f\u984c\uff09\u3002</li> <li>\u5b50\u554f\u984c\u662f\u7368\u7acb\u7684\uff1a\u6bcf\u500b\u5b50\u9663\u5217\u90fd\u53ef\u4ee5\u7368\u7acb\u5730\u9032\u884c\u6392\u5e8f\uff08\u5b50\u554f\u984c\u53ef\u4ee5\u7368\u7acb\u9032\u884c\u6c42\u89e3\uff09\u3002</li> <li>\u5b50\u554f\u984c\u7684\u89e3\u53ef\u4ee5\u5408\u4f75\uff1a\u5169\u500b\u6709\u5e8f\u5b50\u9663\u5217\uff08\u5b50\u554f\u984c\u7684\u89e3\uff09\u53ef\u4ee5\u5408\u4f75\u70ba\u4e00\u500b\u6709\u5e8f\u9663\u5217\uff08\u539f\u554f\u984c\u7684\u89e3\uff09\u3002</li> </ol>"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1212","title":"12.1.2 \u00a0 \u900f\u904e\u5206\u6cbb\u63d0\u5347\u6548\u7387","text":"<p>\u5206\u6cbb\u4e0d\u50c5\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u6c7a\u6f14\u7b97\u6cd5\u554f\u984c\uff0c\u5f80\u5f80\u9084\u53ef\u4ee5\u63d0\u5347\u6f14\u7b97\u6cd5\u6548\u7387\u3002\u5728\u6392\u5e8f\u6f14\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u3001\u5408\u4f75\u6392\u5e8f\u3001\u5806\u7a4d\u6392\u5e8f\u76f8\u8f03\u65bc\u9078\u64c7\u3001\u5192\u6ce1\u3001\u63d2\u5165\u6392\u5e8f\u66f4\u5feb\uff0c\u5c31\u662f\u56e0\u70ba\u5b83\u5011\u61c9\u7528\u4e86\u5206\u6cbb\u7b56\u7565\u3002</p> <p>\u90a3\u9ebc\uff0c\u6211\u5011\u4e0d\u7981\u767c\u554f\uff1a\u70ba\u4ec0\u9ebc\u5206\u6cbb\u53ef\u4ee5\u63d0\u5347\u6f14\u7b97\u6cd5\u6548\u7387\uff0c\u5176\u5e95\u5c64\u908f\u8f2f\u662f\u4ec0\u9ebc\uff1f\u63db\u53e5\u8a71\u8aaa\uff0c\u5c07\u5927\u554f\u984c\u5206\u89e3\u70ba\u591a\u500b\u5b50\u554f\u984c\u3001\u89e3\u6c7a\u5b50\u554f\u984c\u3001\u5c07\u5b50\u554f\u984c\u7684\u89e3\u5408\u4f75\u70ba\u539f\u554f\u984c\u7684\u89e3\uff0c\u9019\u5e7e\u6b65\u7684\u6548\u7387\u70ba\u4ec0\u9ebc\u6bd4\u76f4\u63a5\u89e3\u6c7a\u539f\u554f\u984c\u7684\u6548\u7387\u66f4\u9ad8\uff1f\u9019\u500b\u554f\u984c\u53ef\u4ee5\u5f9e\u64cd\u4f5c\u6578\u91cf\u548c\u5e73\u884c\u8a08\u7b97\u5169\u65b9\u9762\u4f86\u8a0e\u8ad6\u3002</p>"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1","title":"1. \u00a0 \u64cd\u4f5c\u6578\u91cf\u6700\u4f73\u5316","text":"<p>\u4ee5\u201c\u6ce1\u6cab\u6392\u5e8f\u201d\u70ba\u4f8b\uff0c\u5176\u8655\u7406\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u9663\u5217\u9700\u8981 \\(O(n^2)\\) \u6642\u9593\u3002\u5047\u8a2d\u6211\u5011\u6309\u7167\u5716 12-2 \u6240\u793a\u7684\u65b9\u5f0f\uff0c\u5c07\u9663\u5217\u5f9e\u4e2d\u9ede\u8655\u5206\u70ba\u5169\u500b\u5b50\u9663\u5217\uff0c\u5247\u5283\u5206\u9700\u8981 \\(O(n)\\) \u6642\u9593\uff0c\u6392\u5e8f\u6bcf\u500b\u5b50\u9663\u5217\u9700\u8981 \\(O((n / 2)^2)\\) \u6642\u9593\uff0c\u5408\u4f75\u5169\u500b\u5b50\u9663\u5217\u9700\u8981 \\(O(n)\\) \u6642\u9593\uff0c\u7e3d\u9ad4\u6642\u9593\u8907\u96dc\u5ea6\u70ba\uff1a</p> \\[ O(n + (\\frac{n}{2})^2 \\times 2 + n) = O(\\frac{n^2}{2} + 2n) \\] <p></p> <p> \u5716 12-2 \u00a0 \u5283\u5206\u9663\u5217\u524d\u5f8c\u7684\u6ce1\u6cab\u6392\u5e8f </p> <p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u8a08\u7b97\u4ee5\u4e0b\u4e0d\u7b49\u5f0f\uff0c\u5176\u5de6\u908a\u548c\u53f3\u908a\u5206\u5225\u70ba\u5283\u5206\u524d\u548c\u5283\u5206\u5f8c\u7684\u64cd\u4f5c\u7e3d\u6578\uff1a</p> \\[ \\begin{aligned} n^2 &amp; &gt; \\frac{n^2}{2} + 2n \\newline n^2 - \\frac{n^2}{2} - 2n &amp; &gt; 0 \\newline n(n - 4) &amp; &gt; 0 \\end{aligned} \\] <p>\u9019\u610f\u5473\u8457\u7576 \\(n &gt; 4\\) \u6642\uff0c\u5283\u5206\u5f8c\u7684\u64cd\u4f5c\u6578\u91cf\u66f4\u5c11\uff0c\u6392\u5e8f\u6548\u7387\u61c9\u8a72\u66f4\u9ad8\u3002\u8acb\u6ce8\u610f\uff0c\u5283\u5206\u5f8c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u4ecd\u7136\u662f\u5e73\u65b9\u968e \\(O(n^2)\\) \uff0c\u53ea\u662f\u8907\u96dc\u5ea6\u4e2d\u7684\u5e38\u6578\u9805\u8b8a\u5c0f\u4e86\u3002</p> <p>\u9032\u4e00\u6b65\u60f3\uff0c\u5982\u679c\u6211\u5011\u628a\u5b50\u9663\u5217\u4e0d\u65b7\u5730\u518d\u5f9e\u4e2d\u9ede\u8655\u5283\u5206\u70ba\u5169\u500b\u5b50\u9663\u5217\uff0c\u76f4\u81f3\u5b50\u9663\u5217\u53ea\u5269\u4e00\u500b\u5143\u7d20\u6642\u505c\u6b62\u5283\u5206\u5462\uff1f\u9019\u7a2e\u601d\u8def\u5be6\u969b\u4e0a\u5c31\u662f\u201c\u5408\u4f75\u6392\u5e8f\u201d\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n \\log n)\\) \u3002</p> <p>\u518d\u601d\u8003\uff0c\u5982\u679c\u6211\u5011\u591a\u8a2d\u5b9a\u5e7e\u500b\u5283\u5206\u9ede\uff0c\u5c07\u539f\u9663\u5217\u5e73\u5747\u5283\u5206\u70ba \\(k\\) \u500b\u5b50\u9663\u5217\u5462\uff1f\u9019\u7a2e\u60c5\u6cc1\u8207\u201c\u6876\u6392\u5e8f\u201d\u975e\u5e38\u985e\u4f3c\uff0c\u5b83\u975e\u5e38\u9069\u5408\u6392\u5e8f\u6d77\u91cf\u8cc7\u6599\uff0c\u7406\u8ad6\u4e0a\u6642\u9593\u8907\u96dc\u5ea6\u53ef\u4ee5\u9054\u5230 \\(O(n + k)\\) \u3002</p>"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#2","title":"2. \u00a0 \u5e73\u884c\u8a08\u7b97\u6700\u4f73\u5316","text":"<p>\u6211\u5011\u77e5\u9053\uff0c\u5206\u6cbb\u751f\u6210\u7684\u5b50\u554f\u984c\u662f\u76f8\u4e92\u7368\u7acb\u7684\uff0c\u56e0\u6b64\u901a\u5e38\u53ef\u4ee5\u4e26\u884c\u89e3\u6c7a\u3002\u4e5f\u5c31\u662f\u8aaa\uff0c\u5206\u6cbb\u4e0d\u50c5\u53ef\u4ee5\u964d\u4f4e\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\uff0c\u9084\u6709\u5229\u65bc\u4f5c\u696d\u7cfb\u7d71\u7684\u4e26\u884c\u6700\u4f73\u5316\u3002</p> <p>\u4e26\u884c\u6700\u4f73\u5316\u5728\u591a\u6838\u6216\u591a\u8655\u7406\u5668\u7684\u74b0\u5883\u4e2d\u5c24\u5176\u6709\u6548\uff0c\u56e0\u70ba\u7cfb\u7d71\u53ef\u4ee5\u540c\u6642\u8655\u7406\u591a\u500b\u5b50\u554f\u984c\uff0c\u66f4\u52a0\u5145\u5206\u5730\u5229\u7528\u8a08\u7b97\u8cc7\u6e90\uff0c\u5f9e\u800c\u986f\u8457\u6e1b\u5c11\u7e3d\u9ad4\u7684\u57f7\u884c\u6642\u9593\u3002</p> <p>\u6bd4\u5982\u5728\u5716 12-3 \u6240\u793a\u7684\u201c\u6876\u6392\u5e8f\u201d\u4e2d\uff0c\u6211\u5011\u5c07\u6d77\u91cf\u7684\u8cc7\u6599\u5e73\u5747\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\uff0c\u5247\u53ef\u5c07\u6240\u6709\u6876\u7684\u6392\u5e8f\u4efb\u52d9\u5206\u6563\u5230\u5404\u500b\u8a08\u7b97\u55ae\u5143\uff0c\u5b8c\u6210\u5f8c\u518d\u5408\u4f75\u7d50\u679c\u3002</p> <p></p> <p> \u5716 12-3 \u00a0 \u6876\u6392\u5e8f\u7684\u5e73\u884c\u8a08\u7b97 </p>"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1213","title":"12.1.3 \u00a0 \u5206\u6cbb\u5e38\u898b\u61c9\u7528","text":"<p>\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u53ef\u4ee5\u7528\u4f86\u89e3\u6c7a\u8a31\u591a\u7d93\u5178\u6f14\u7b97\u6cd5\u554f\u984c\u3002</p> <ul> <li>\u5c0b\u627e\u6700\u8fd1\u9ede\u5c0d\uff1a\u8a72\u6f14\u7b97\u6cd5\u9996\u5148\u5c07\u9ede\u96c6\u5206\u6210\u5169\u90e8\u5206\uff0c\u7136\u5f8c\u5206\u5225\u627e\u51fa\u5169\u90e8\u5206\u4e2d\u7684\u6700\u8fd1\u9ede\u5c0d\uff0c\u6700\u5f8c\u627e\u51fa\u8de8\u8d8a\u5169\u90e8\u5206\u7684\u6700\u8fd1\u9ede\u5c0d\u3002</li> <li>\u5927\u6574\u6578\u4e58\u6cd5\uff1a\u4f8b\u5982 Karatsuba \u6f14\u7b97\u6cd5\uff0c\u5b83\u5c07\u5927\u6574\u6578\u4e58\u6cd5\u5206\u89e3\u70ba\u5e7e\u500b\u8f03\u5c0f\u7684\u6574\u6578\u7684\u4e58\u6cd5\u548c\u52a0\u6cd5\u3002</li> <li>\u77e9\u9663\u4e58\u6cd5\uff1a\u4f8b\u5982 Strassen \u6f14\u7b97\u6cd5\uff0c\u5b83\u5c07\u5927\u77e9\u9663\u4e58\u6cd5\u5206\u89e3\u70ba\u591a\u500b\u5c0f\u77e9\u9663\u7684\u4e58\u6cd5\u548c\u52a0\u6cd5\u3002</li> <li>\u6cb3\u5167\u5854\u554f\u984c\uff1a\u6cb3\u5167\u5854\u554f\u984c\u53ef\u4ee5\u900f\u904e\u905e\u8ff4\u89e3\u6c7a\uff0c\u9019\u662f\u5178\u578b\u7684\u5206\u6cbb\u7b56\u7565\u61c9\u7528\u3002</li> <li>\u6c42\u89e3\u9006\u5e8f\u5c0d\uff1a\u5728\u4e00\u500b\u5e8f\u5217\u4e2d\uff0c\u5982\u679c\u524d\u9762\u7684\u6578\u5b57\u5927\u65bc\u5f8c\u9762\u7684\u6578\u5b57\uff0c\u90a3\u9ebc\u9019\u5169\u500b\u6578\u5b57\u69cb\u6210\u4e00\u500b\u9006\u5e8f\u5c0d\u3002\u6c42\u89e3\u9006\u5e8f\u5c0d\u554f\u984c\u53ef\u4ee5\u5229\u7528\u5206\u6cbb\u7684\u601d\u60f3\uff0c\u85c9\u52a9\u5408\u4f75\u6392\u5e8f\u9032\u884c\u6c42\u89e3\u3002</li> </ul> <p>\u53e6\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u5728\u6f14\u7b97\u6cd5\u548c\u8cc7\u6599\u7d50\u69cb\u7684\u8a2d\u8a08\u4e2d\u61c9\u7528\u5f97\u975e\u5e38\u5ee3\u6cdb\u3002</p> <ul> <li>\u4e8c\u5206\u641c\u5c0b\uff1a\u4e8c\u5206\u641c\u5c0b\u662f\u5c07\u6709\u5e8f\u9663\u5217\u5f9e\u4e2d\u9ede\u7d22\u5f15\u8655\u5206\u70ba\u5169\u90e8\u5206\uff0c\u7136\u5f8c\u6839\u64da\u76ee\u6a19\u503c\u8207\u4e2d\u9593\u5143\u7d20\u503c\u6bd4\u8f03\u7d50\u679c\uff0c\u6c7a\u5b9a\u6392\u9664\u54ea\u4e00\u534a\u5340\u9593\uff0c\u4e26\u5728\u5269\u9918\u5340\u9593\u57f7\u884c\u76f8\u540c\u7684\u4e8c\u5206\u64cd\u4f5c\u3002</li> <li>\u5408\u4f75\u6392\u5e8f\uff1a\u672c\u7bc0\u958b\u982d\u5df2\u4ecb\u7d39\uff0c\u4e0d\u518d\u8d05\u8ff0\u3002</li> <li>\u5feb\u901f\u6392\u5e8f\uff1a\u5feb\u901f\u6392\u5e8f\u662f\u9078\u53d6\u4e00\u500b\u57fa\u6e96\u503c\uff0c\u7136\u5f8c\u628a\u9663\u5217\u5206\u70ba\u5169\u500b\u5b50\u9663\u5217\uff0c\u4e00\u500b\u5b50\u9663\u5217\u7684\u5143\u7d20\u6bd4\u57fa\u6e96\u503c\u5c0f\uff0c\u53e6\u4e00\u5b50\u9663\u5217\u7684\u5143\u7d20\u6bd4\u57fa\u6e96\u503c\u5927\uff0c\u518d\u5c0d\u9019\u5169\u90e8\u5206\u9032\u884c\u76f8\u540c\u7684\u5283\u5206\u64cd\u4f5c\uff0c\u76f4\u81f3\u5b50\u9663\u5217\u53ea\u5269\u4e0b\u4e00\u500b\u5143\u7d20\u3002</li> <li>\u6876\u6392\u5e8f\uff1a\u6876\u6392\u5e8f\u7684\u57fa\u672c\u601d\u60f3\u662f\u5c07\u8cc7\u6599\u5206\u6563\u5230\u591a\u500b\u6876\uff0c\u7136\u5f8c\u5c0d\u6bcf\u500b\u6876\u5167\u7684\u5143\u7d20\u9032\u884c\u6392\u5e8f\uff0c\u6700\u5f8c\u5c07\u5404\u500b\u6876\u7684\u5143\u7d20\u4f9d\u6b21\u53d6\u51fa\uff0c\u5f9e\u800c\u5f97\u5230\u4e00\u500b\u6709\u5e8f\u9663\u5217\u3002</li> <li>\u6a39\uff1a\u4f8b\u5982\u4e8c\u5143\u641c\u5c0b\u6a39\u3001AVL \u6a39\u3001\u7d05\u9ed1\u6a39\u3001B \u6a39\u3001B+ \u6a39\u7b49\uff0c\u5b83\u5011\u7684\u67e5\u8a62\u3001\u63d2\u5165\u548c\u522a\u9664\u7b49\u64cd\u4f5c\u90fd\u53ef\u4ee5\u8996\u70ba\u5206\u6cbb\u7b56\u7565\u7684\u61c9\u7528\u3002</li> <li>\u5806\u7a4d\uff1a\u5806\u7a4d\u662f\u4e00\u7a2e\u7279\u6b8a\u7684\u5b8c\u5168\u4e8c\u5143\u6a39\uff0c\u5176\u5404\u7a2e\u64cd\u4f5c\uff0c\u5982\u63d2\u5165\u3001\u522a\u9664\u548c\u5806\u7a4d\u5316\uff0c\u5be6\u969b\u4e0a\u90fd\u96b1\u542b\u4e86\u5206\u6cbb\u7684\u601d\u60f3\u3002</li> <li>\u96dc\u6e4a\u8868\uff1a\u96d6\u7136\u96dc\u6e4a\u8868\u4e26\u4e0d\u76f4\u63a5\u61c9\u7528\u5206\u6cbb\uff0c\u4f46\u67d0\u4e9b\u96dc\u6e4a\u885d\u7a81\u89e3\u6c7a\u65b9\u6848\u9593\u63a5\u61c9\u7528\u4e86\u5206\u6cbb\u7b56\u7565\uff0c\u4f8b\u5982\uff0c\u93c8\u5f0f\u4f4d\u5740\u4e2d\u7684\u9577\u93c8\u7d50\u4e32\u5217\u6703\u88ab\u8f49\u5316\u70ba\u7d05\u9ed1\u6a39\uff0c\u4ee5\u63d0\u5347\u67e5\u8a62\u6548\u7387\u3002</li> </ul> <p>\u53ef\u4ee5\u770b\u51fa\uff0c\u5206\u6cbb\u662f\u4e00\u7a2e\u201c\u6f64\u7269\u7d30\u7121\u8072\u201d\u7684\u6f14\u7b97\u6cd5\u601d\u60f3\uff0c\u96b1\u542b\u5728\u5404\u7a2e\u6f14\u7b97\u6cd5\u8207\u8cc7\u6599\u7d50\u69cb\u4e4b\u4e2d\u3002</p>"},{"location":"chapter_divide_and_conquer/hanota_problem/","title":"12.4 \u00a0 \u6cb3\u5167\u5854\u554f\u984c","text":"<p>\u5728\u5408\u4f75\u6392\u5e8f\u548c\u69cb\u5efa\u4e8c\u5143\u6a39\u4e2d\uff0c\u6211\u5011\u90fd\u662f\u5c07\u539f\u554f\u984c\u5206\u89e3\u70ba\u5169\u500b\u898f\u6a21\u70ba\u539f\u554f\u984c\u4e00\u534a\u7684\u5b50\u554f\u984c\u3002\u7136\u800c\u5c0d\u65bc\u6cb3\u5167\u5854\u554f\u984c\uff0c\u6211\u5011\u63a1\u7528\u4e0d\u540c\u7684\u5206\u89e3\u7b56\u7565\u3002</p> <p>Question</p> <p>\u7d66\u5b9a\u4e09\u6839\u67f1\u5b50\uff0c\u8a18\u70ba <code>A</code>\u3001<code>B</code> \u548c <code>C</code> \u3002\u8d77\u59cb\u72c0\u614b\u4e0b\uff0c\u67f1\u5b50 <code>A</code> \u4e0a\u5957\u8457 \\(n\\) \u500b\u5713\u76e4\uff0c\u5b83\u5011\u5f9e\u4e0a\u5230\u4e0b\u6309\u7167\u5f9e\u5c0f\u5230\u5927\u7684\u9806\u5e8f\u6392\u5217\u3002\u6211\u5011\u7684\u4efb\u52d9\u662f\u8981\u628a\u9019 \\(n\\) \u500b\u5713\u76e4\u79fb\u5230\u67f1\u5b50 <code>C</code> \u4e0a\uff0c\u4e26\u4fdd\u6301\u5b83\u5011\u7684\u539f\u6709\u9806\u5e8f\u4e0d\u8b8a\uff08\u5982\u5716 12-10 \u6240\u793a\uff09\u3002\u5728\u79fb\u52d5\u5713\u76e4\u7684\u904e\u7a0b\u4e2d\uff0c\u9700\u8981\u9075\u5b88\u4ee5\u4e0b\u898f\u5247\u3002</p> <ol> <li>\u5713\u76e4\u53ea\u80fd\u5f9e\u4e00\u6839\u67f1\u5b50\u9802\u90e8\u62ff\u51fa\uff0c\u5f9e\u53e6\u4e00\u6839\u67f1\u5b50\u9802\u90e8\u653e\u5165\u3002</li> <li>\u6bcf\u6b21\u53ea\u80fd\u79fb\u52d5\u4e00\u500b\u5713\u76e4\u3002</li> <li>\u5c0f\u5713\u76e4\u5fc5\u9808\u6642\u523b\u4f4d\u65bc\u5927\u5713\u76e4\u4e4b\u4e0a\u3002</li> </ol> <p></p> <p> \u5716 12-10 \u00a0 \u6cb3\u5167\u5854\u554f\u984c\u793a\u4f8b </p> <p>\u6211\u5011\u5c07\u898f\u6a21\u70ba \\(i\\) \u7684\u6cb3\u5167\u5854\u554f\u984c\u8a18\u4f5c \\(f(i)\\) \u3002\u4f8b\u5982 \\(f(3)\\) \u4ee3\u8868\u5c07 \\(3\\) \u500b\u5713\u76e4\u5f9e <code>A</code> \u79fb\u52d5\u81f3 <code>C</code> \u7684\u6cb3\u5167\u5854\u554f\u984c\u3002</p>"},{"location":"chapter_divide_and_conquer/hanota_problem/#1","title":"1. \u00a0 \u8003\u616e\u57fa\u672c\u60c5\u6cc1","text":"<p>\u5982\u5716 12-11 \u6240\u793a\uff0c\u5c0d\u65bc\u554f\u984c \\(f(1)\\) \uff0c\u5373\u7576\u53ea\u6709\u4e00\u500b\u5713\u76e4\u6642\uff0c\u6211\u5011\u5c07\u5b83\u76f4\u63a5\u5f9e <code>A</code> \u79fb\u52d5\u81f3 <code>C</code> \u5373\u53ef\u3002</p> &lt;1&gt;&lt;2&gt; <p></p> <p></p> <p> \u5716 12-11 \u00a0 \u898f\u6a21\u70ba 1 \u7684\u554f\u984c\u7684\u89e3 </p> <p>\u5982\u5716 12-12 \u6240\u793a\uff0c\u5c0d\u65bc\u554f\u984c \\(f(2)\\) \uff0c\u5373\u7576\u6709\u5169\u500b\u5713\u76e4\u6642\uff0c\u7531\u65bc\u8981\u6642\u523b\u6eff\u8db3\u5c0f\u5713\u76e4\u5728\u5927\u5713\u76e4\u4e4b\u4e0a\uff0c\u56e0\u6b64\u9700\u8981\u85c9\u52a9 <code>B</code> \u4f86\u5b8c\u6210\u79fb\u52d5\u3002</p> <ol> <li>\u5148\u5c07\u4e0a\u9762\u7684\u5c0f\u5713\u76e4\u5f9e <code>A</code> \u79fb\u81f3 <code>B</code> \u3002</li> <li>\u518d\u5c07\u5927\u5713\u76e4\u5f9e <code>A</code> \u79fb\u81f3 <code>C</code> \u3002</li> <li>\u6700\u5f8c\u5c07\u5c0f\u5713\u76e4\u5f9e <code>B</code> \u79fb\u81f3 <code>C</code> \u3002</li> </ol> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt; <p></p> <p></p> <p></p> <p></p> <p> \u5716 12-12 \u00a0 \u898f\u6a21\u70ba 2 \u7684\u554f\u984c\u7684\u89e3 </p> <p>\u89e3\u6c7a\u554f\u984c \\(f(2)\\) \u7684\u904e\u7a0b\u53ef\u7e3d\u7d50\u70ba\uff1a\u5c07\u5169\u500b\u5713\u76e4\u85c9\u52a9 <code>B</code> \u5f9e <code>A</code> \u79fb\u81f3 <code>C</code> \u3002\u5176\u4e2d\uff0c<code>C</code> \u7a31\u70ba\u76ee\u6a19\u67f1\u3001<code>B</code> \u7a31\u70ba\u7de9\u885d\u67f1\u3002</p>"},{"location":"chapter_divide_and_conquer/hanota_problem/#2","title":"2. \u00a0 \u5b50\u554f\u984c\u5206\u89e3","text":"<p>\u5c0d\u65bc\u554f\u984c \\(f(3)\\) \uff0c\u5373\u7576\u6709\u4e09\u500b\u5713\u76e4\u6642\uff0c\u60c5\u6cc1\u8b8a\u5f97\u7a0d\u5fae\u8907\u96dc\u4e86\u4e00\u4e9b\u3002</p> <p>\u56e0\u70ba\u5df2\u77e5 \\(f(1)\\) \u548c \\(f(2)\\) \u7684\u89e3\uff0c\u6240\u4ee5\u6211\u5011\u53ef\u5f9e\u5206\u6cbb\u89d2\u5ea6\u601d\u8003\uff0c\u5c07 <code>A</code> \u9802\u90e8\u7684\u5169\u500b\u5713\u76e4\u770b\u4f5c\u4e00\u500b\u6574\u9ad4\uff0c\u57f7\u884c\u5716 12-13 \u6240\u793a\u7684\u6b65\u9a5f\u3002\u9019\u6a23\u4e09\u500b\u5713\u76e4\u5c31\u88ab\u9806\u5229\u5730\u5f9e <code>A</code> \u79fb\u81f3 <code>C</code> \u4e86\u3002</p> <ol> <li>\u4ee4 <code>B</code> \u70ba\u76ee\u6a19\u67f1\u3001<code>C</code> \u70ba\u7de9\u885d\u67f1\uff0c\u5c07\u5169\u500b\u5713\u76e4\u5f9e <code>A</code> \u79fb\u81f3 <code>B</code> \u3002</li> <li>\u5c07 <code>A</code> \u4e2d\u5269\u9918\u7684\u4e00\u500b\u5713\u76e4\u5f9e <code>A</code> \u76f4\u63a5\u79fb\u52d5\u81f3 <code>C</code> \u3002</li> <li>\u4ee4 <code>C</code> \u70ba\u76ee\u6a19\u67f1\u3001<code>A</code> \u70ba\u7de9\u885d\u67f1\uff0c\u5c07\u5169\u500b\u5713\u76e4\u5f9e <code>B</code> \u79fb\u81f3 <code>C</code> \u3002</li> </ol> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt; <p></p> <p></p> <p></p> <p></p> <p> \u5716 12-13 \u00a0 \u898f\u6a21\u70ba 3 \u7684\u554f\u984c\u7684\u89e3 </p> <p>\u5f9e\u672c\u8cea\u4e0a\u770b\uff0c\u6211\u5011\u5c07\u554f\u984c \\(f(3)\\) \u5283\u5206\u70ba\u5169\u500b\u5b50\u554f\u984c \\(f(2)\\) \u548c\u4e00\u500b\u5b50\u554f\u984c \\(f(1)\\) \u3002\u6309\u9806\u5e8f\u89e3\u6c7a\u9019\u4e09\u500b\u5b50\u554f\u984c\u4e4b\u5f8c\uff0c\u539f\u554f\u984c\u96a8\u4e4b\u5f97\u5230\u89e3\u6c7a\u3002\u9019\u8aaa\u660e\u5b50\u554f\u984c\u662f\u7368\u7acb\u7684\uff0c\u800c\u4e14\u89e3\u53ef\u4ee5\u5408\u4f75\u3002</p> <p>\u81f3\u6b64\uff0c\u6211\u5011\u53ef\u7e3d\u7d50\u51fa\u5716 12-14 \u6240\u793a\u7684\u89e3\u6c7a\u6cb3\u5167\u5854\u554f\u984c\u7684\u5206\u6cbb\u7b56\u7565\uff1a\u5c07\u539f\u554f\u984c \\(f(n)\\) \u5283\u5206\u70ba\u5169\u500b\u5b50\u554f\u984c \\(f(n-1)\\) \u548c\u4e00\u500b\u5b50\u554f\u984c \\(f(1)\\) \uff0c\u4e26\u6309\u7167\u4ee5\u4e0b\u9806\u5e8f\u89e3\u6c7a\u9019\u4e09\u500b\u5b50\u554f\u984c\u3002</p> <ol> <li>\u5c07 \\(n-1\\) \u500b\u5713\u76e4\u85c9\u52a9 <code>C</code> \u5f9e <code>A</code> \u79fb\u81f3 <code>B</code> \u3002</li> <li>\u5c07\u5269\u9918 \\(1\\) \u500b\u5713\u76e4\u5f9e <code>A</code> \u76f4\u63a5\u79fb\u81f3 <code>C</code> \u3002</li> <li>\u5c07 \\(n-1\\) \u500b\u5713\u76e4\u85c9\u52a9 <code>A</code> \u5f9e <code>B</code> \u79fb\u81f3 <code>C</code> \u3002</li> </ol> <p>\u5c0d\u65bc\u9019\u5169\u500b\u5b50\u554f\u984c \\(f(n-1)\\) \uff0c\u53ef\u4ee5\u900f\u904e\u76f8\u540c\u7684\u65b9\u5f0f\u9032\u884c\u905e\u8ff4\u5283\u5206\uff0c\u76f4\u81f3\u9054\u5230\u6700\u5c0f\u5b50\u554f\u984c \\(f(1)\\) \u3002\u800c \\(f(1)\\) \u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u53ea\u9700\u4e00\u6b21\u79fb\u52d5\u64cd\u4f5c\u5373\u53ef\u3002</p> <p></p> <p> \u5716 12-14 \u00a0 \u89e3\u6c7a\u6cb3\u5167\u5854\u554f\u984c\u7684\u5206\u6cbb\u7b56\u7565 </p>"},{"location":"chapter_divide_and_conquer/hanota_problem/#3","title":"3. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"<p>\u5728\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5ba3\u544a\u4e00\u500b\u905e\u8ff4\u51fd\u5f0f <code>dfs(i, src, buf, tar)</code> \uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5c07\u67f1 <code>src</code> \u9802\u90e8\u7684 \\(i\\) \u500b\u5713\u76e4\u85c9\u52a9\u7de9\u885d\u67f1 <code>buf</code> \u79fb\u52d5\u81f3\u76ee\u6a19\u67f1 <code>tar</code> \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig hanota.py<pre><code>def move(src: list[int], tar: list[int]):\n \"\"\"\u79fb\u52d5\u4e00\u500b\u5713\u76e4\"\"\"\n # \u5f9e src \u9802\u90e8\u62ff\u51fa\u4e00\u500b\u5713\u76e4\n pan = src.pop()\n # \u5c07\u5713\u76e4\u653e\u5165 tar \u9802\u90e8\n tar.append(pan)\n\ndef dfs(i: int, src: list[int], buf: list[int], tar: list[int]):\n \"\"\"\u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c f(i)\"\"\"\n # \u82e5 src \u53ea\u5269\u4e0b\u4e00\u500b\u5713\u76e4\uff0c\u5247\u76f4\u63a5\u5c07\u5176\u79fb\u5230 tar\n if i == 1:\n move(src, tar)\n return\n # \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 src \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf)\n # \u5b50\u554f\u984c f(1) \uff1a\u5c07 src \u5269\u9918\u4e00\u500b\u5713\u76e4\u79fb\u5230 tar\n move(src, tar)\n # \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 buf \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar)\n\ndef solve_hanota(A: list[int], B: list[int], C: list[int]):\n \"\"\"\u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c\"\"\"\n n = len(A)\n # \u5c07 A \u9802\u90e8 n \u500b\u5713\u76e4\u85c9\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C)\n</code></pre> hanota.cpp<pre><code>/* \u79fb\u52d5\u4e00\u500b\u5713\u76e4 */\nvoid move(vector&lt;int&gt; &amp;src, vector&lt;int&gt; &amp;tar) {\n // \u5f9e src \u9802\u90e8\u62ff\u51fa\u4e00\u500b\u5713\u76e4\n int pan = src.back();\n src.pop_back();\n // \u5c07\u5713\u76e4\u653e\u5165 tar \u9802\u90e8\n tar.push_back(pan);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c f(i) */\nvoid dfs(int i, vector&lt;int&gt; &amp;src, vector&lt;int&gt; &amp;buf, vector&lt;int&gt; &amp;tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u500b\u5713\u76e4\uff0c\u5247\u76f4\u63a5\u5c07\u5176\u79fb\u5230 tar\n if (i == 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 src \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u554f\u984c f(1) \uff1a\u5c07 src \u5269\u9918\u4e00\u500b\u5713\u76e4\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 buf \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c */\nvoid solveHanota(vector&lt;int&gt; &amp;A, vector&lt;int&gt; &amp;B, vector&lt;int&gt; &amp;C) {\n int n = A.size();\n // \u5c07 A \u9802\u90e8 n \u500b\u5713\u76e4\u85c9\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n</code></pre> hanota.java<pre><code>/* \u79fb\u52d5\u4e00\u500b\u5713\u76e4 */\nvoid move(List&lt;Integer&gt; src, List&lt;Integer&gt; tar) {\n // \u5f9e src \u9802\u90e8\u62ff\u51fa\u4e00\u500b\u5713\u76e4\n Integer pan = src.remove(src.size() - 1);\n // \u5c07\u5713\u76e4\u653e\u5165 tar \u9802\u90e8\n tar.add(pan);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c f(i) */\nvoid dfs(int i, List&lt;Integer&gt; src, List&lt;Integer&gt; buf, List&lt;Integer&gt; tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u500b\u5713\u76e4\uff0c\u5247\u76f4\u63a5\u5c07\u5176\u79fb\u5230 tar\n if (i == 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 src \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u554f\u984c f(1) \uff1a\u5c07 src \u5269\u9918\u4e00\u500b\u5713\u76e4\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 buf \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c */\nvoid solveHanota(List&lt;Integer&gt; A, List&lt;Integer&gt; B, List&lt;Integer&gt; C) {\n int n = A.size();\n // \u5c07 A \u9802\u90e8 n \u500b\u5713\u76e4\u85c9\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n</code></pre> hanota.cs<pre><code>/* \u79fb\u52d5\u4e00\u500b\u5713\u76e4 */\nvoid Move(List&lt;int&gt; src, List&lt;int&gt; tar) {\n // \u5f9e src \u9802\u90e8\u62ff\u51fa\u4e00\u500b\u5713\u76e4\n int pan = src[^1];\n src.RemoveAt(src.Count - 1);\n // \u5c07\u5713\u76e4\u653e\u5165 tar \u9802\u90e8\n tar.Add(pan);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c f(i) */\nvoid DFS(int i, List&lt;int&gt; src, List&lt;int&gt; buf, List&lt;int&gt; tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u500b\u5713\u76e4\uff0c\u5247\u76f4\u63a5\u5c07\u5176\u79fb\u5230 tar\n if (i == 1) {\n Move(src, tar);\n return;\n }\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 src \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 tar \u79fb\u5230 buf\n DFS(i - 1, src, tar, buf);\n // \u5b50\u554f\u984c f(1) \uff1a\u5c07 src \u5269\u9918\u4e00\u500b\u5713\u76e4\u79fb\u5230 tar\n Move(src, tar);\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 buf \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 src \u79fb\u5230 tar\n DFS(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c */\nvoid SolveHanota(List&lt;int&gt; A, List&lt;int&gt; B, List&lt;int&gt; C) {\n int n = A.Count;\n // \u5c07 A \u9802\u90e8 n \u500b\u5713\u76e4\u85c9\u52a9 B \u79fb\u5230 C\n DFS(n, A, B, C);\n}\n</code></pre> hanota.go<pre><code>/* \u79fb\u52d5\u4e00\u500b\u5713\u76e4 */\nfunc move(src, tar *list.List) {\n // \u5f9e src \u9802\u90e8\u62ff\u51fa\u4e00\u500b\u5713\u76e4\n pan := src.Back()\n // \u5c07\u5713\u76e4\u653e\u5165 tar \u9802\u90e8\n tar.PushBack(pan.Value)\n // \u79fb\u9664 src \u9802\u90e8\u5713\u76e4\n src.Remove(pan)\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c f(i) */\nfunc dfsHanota(i int, src, buf, tar *list.List) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u500b\u5713\u76e4\uff0c\u5247\u76f4\u63a5\u5c07\u5176\u79fb\u5230 tar\n if i == 1 {\n move(src, tar)\n return\n }\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 src \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 tar \u79fb\u5230 buf\n dfsHanota(i-1, src, tar, buf)\n // \u5b50\u554f\u984c f(1) \uff1a\u5c07 src \u5269\u9918\u4e00\u500b\u5713\u76e4\u79fb\u5230 tar\n move(src, tar)\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 buf \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 src \u79fb\u5230 tar\n dfsHanota(i-1, buf, src, tar)\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c */\nfunc solveHanota(A, B, C *list.List) {\n n := A.Len()\n // \u5c07 A \u9802\u90e8 n \u500b\u5713\u76e4\u85c9\u52a9 B \u79fb\u5230 C\n dfsHanota(n, A, B, C)\n}\n</code></pre> hanota.swift<pre><code>/* \u79fb\u52d5\u4e00\u500b\u5713\u76e4 */\nfunc move(src: inout [Int], tar: inout [Int]) {\n // \u5f9e src \u9802\u90e8\u62ff\u51fa\u4e00\u500b\u5713\u76e4\n let pan = src.popLast()!\n // \u5c07\u5713\u76e4\u653e\u5165 tar \u9802\u90e8\n tar.append(pan)\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c f(i) */\nfunc dfs(i: Int, src: inout [Int], buf: inout [Int], tar: inout [Int]) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u500b\u5713\u76e4\uff0c\u5247\u76f4\u63a5\u5c07\u5176\u79fb\u5230 tar\n if i == 1 {\n move(src: &amp;src, tar: &amp;tar)\n return\n }\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 src \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 tar \u79fb\u5230 buf\n dfs(i: i - 1, src: &amp;src, buf: &amp;tar, tar: &amp;buf)\n // \u5b50\u554f\u984c f(1) \uff1a\u5c07 src \u5269\u9918\u4e00\u500b\u5713\u76e4\u79fb\u5230 tar\n move(src: &amp;src, tar: &amp;tar)\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 buf \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 src \u79fb\u5230 tar\n dfs(i: i - 1, src: &amp;buf, buf: &amp;src, tar: &amp;tar)\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c */\nfunc solveHanota(A: inout [Int], B: inout [Int], C: inout [Int]) {\n let n = A.count\n // \u4e32\u5217\u5c3e\u90e8\u662f\u67f1\u5b50\u9802\u90e8\n // \u5c07 src \u9802\u90e8 n \u500b\u5713\u76e4\u85c9\u52a9 B \u79fb\u5230 C\n dfs(i: n, src: &amp;A, buf: &amp;B, tar: &amp;C)\n}\n</code></pre> hanota.js<pre><code>/* \u79fb\u52d5\u4e00\u500b\u5713\u76e4 */\nfunction move(src, tar) {\n // \u5f9e src \u9802\u90e8\u62ff\u51fa\u4e00\u500b\u5713\u76e4\n const pan = src.pop();\n // \u5c07\u5713\u76e4\u653e\u5165 tar \u9802\u90e8\n tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c f(i) */\nfunction dfs(i, src, buf, tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u500b\u5713\u76e4\uff0c\u5247\u76f4\u63a5\u5c07\u5176\u79fb\u5230 tar\n if (i === 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 src \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u554f\u984c f(1) \uff1a\u5c07 src \u5269\u9918\u4e00\u500b\u5713\u76e4\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 buf \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c */\nfunction solveHanota(A, B, C) {\n const n = A.length;\n // \u5c07 A \u9802\u90e8 n \u500b\u5713\u76e4\u85c9\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n</code></pre> hanota.ts<pre><code>/* \u79fb\u52d5\u4e00\u500b\u5713\u76e4 */\nfunction move(src: number[], tar: number[]): void {\n // \u5f9e src \u9802\u90e8\u62ff\u51fa\u4e00\u500b\u5713\u76e4\n const pan = src.pop();\n // \u5c07\u5713\u76e4\u653e\u5165 tar \u9802\u90e8\n tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c f(i) */\nfunction dfs(i: number, src: number[], buf: number[], tar: number[]): void {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u500b\u5713\u76e4\uff0c\u5247\u76f4\u63a5\u5c07\u5176\u79fb\u5230 tar\n if (i === 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 src \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u554f\u984c f(1) \uff1a\u5c07 src \u5269\u9918\u4e00\u500b\u5713\u76e4\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 buf \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c */\nfunction solveHanota(A: number[], B: number[], C: number[]): void {\n const n = A.length;\n // \u5c07 A \u9802\u90e8 n \u500b\u5713\u76e4\u85c9\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n</code></pre> hanota.dart<pre><code>/* \u79fb\u52d5\u4e00\u500b\u5713\u76e4 */\nvoid move(List&lt;int&gt; src, List&lt;int&gt; tar) {\n // \u5f9e src \u9802\u90e8\u62ff\u51fa\u4e00\u500b\u5713\u76e4\n int pan = src.removeLast();\n // \u5c07\u5713\u76e4\u653e\u5165 tar \u9802\u90e8\n tar.add(pan);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c f(i) */\nvoid dfs(int i, List&lt;int&gt; src, List&lt;int&gt; buf, List&lt;int&gt; tar) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u500b\u5713\u76e4\uff0c\u5247\u76f4\u63a5\u5c07\u5176\u79fb\u5230 tar\n if (i == 1) {\n move(src, tar);\n return;\n }\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 src \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u554f\u984c f(1) \uff1a\u5c07 src \u5269\u9918\u4e00\u500b\u5713\u76e4\u79fb\u5230 tar\n move(src, tar);\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 buf \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c */\nvoid solveHanota(List&lt;int&gt; A, List&lt;int&gt; B, List&lt;int&gt; C) {\n int n = A.length;\n // \u5c07 A \u9802\u90e8 n \u500b\u5713\u76e4\u85c9\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n</code></pre> hanota.rs<pre><code>/* \u79fb\u52d5\u4e00\u500b\u5713\u76e4 */\nfn move_pan(src: &amp;mut Vec&lt;i32&gt;, tar: &amp;mut Vec&lt;i32&gt;) {\n // \u5f9e src \u9802\u90e8\u62ff\u51fa\u4e00\u500b\u5713\u76e4\n let pan = src.remove(src.len() - 1);\n // \u5c07\u5713\u76e4\u653e\u5165 tar \u9802\u90e8\n tar.push(pan);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c f(i) */\nfn dfs(i: i32, src: &amp;mut Vec&lt;i32&gt;, buf: &amp;mut Vec&lt;i32&gt;, tar: &amp;mut Vec&lt;i32&gt;) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u500b\u5713\u76e4\uff0c\u5247\u76f4\u63a5\u5c07\u5176\u79fb\u5230 tar\n if i == 1 {\n move_pan(src, tar);\n return;\n }\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 src \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf);\n // \u5b50\u554f\u984c f(1) \uff1a\u5c07 src \u5269\u9918\u4e00\u500b\u5713\u76e4\u79fb\u5230 tar\n move_pan(src, tar);\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 buf \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c */\nfn solve_hanota(A: &amp;mut Vec&lt;i32&gt;, B: &amp;mut Vec&lt;i32&gt;, C: &amp;mut Vec&lt;i32&gt;) {\n let n = A.len() as i32;\n // \u5c07 A \u9802\u90e8 n \u500b\u5713\u76e4\u85c9\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C);\n}\n</code></pre> hanota.c<pre><code>/* \u79fb\u52d5\u4e00\u500b\u5713\u76e4 */\nvoid move(int *src, int *srcSize, int *tar, int *tarSize) {\n // \u5f9e src \u9802\u90e8\u62ff\u51fa\u4e00\u500b\u5713\u76e4\n int pan = src[*srcSize - 1];\n src[*srcSize - 1] = 0;\n (*srcSize)--;\n // \u5c07\u5713\u76e4\u653e\u5165 tar \u9802\u90e8\n tar[*tarSize] = pan;\n (*tarSize)++;\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c f(i) */\nvoid dfs(int i, int *src, int *srcSize, int *buf, int *bufSize, int *tar, int *tarSize) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u500b\u5713\u76e4\uff0c\u5247\u76f4\u63a5\u5c07\u5176\u79fb\u5230 tar\n if (i == 1) {\n move(src, srcSize, tar, tarSize);\n return;\n }\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 src \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, srcSize, tar, tarSize, buf, bufSize);\n // \u5b50\u554f\u984c f(1) \uff1a\u5c07 src \u5269\u9918\u4e00\u500b\u5713\u76e4\u79fb\u5230 tar\n move(src, srcSize, tar, tarSize);\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 buf \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, bufSize, src, srcSize, tar, tarSize);\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c */\nvoid solveHanota(int *A, int *ASize, int *B, int *BSize, int *C, int *CSize) {\n // \u5c07 A \u9802\u90e8 n \u500b\u5713\u76e4\u85c9\u52a9 B \u79fb\u5230 C\n dfs(*ASize, A, ASize, B, BSize, C, CSize);\n}\n</code></pre> hanota.kt<pre><code>/* \u79fb\u52d5\u4e00\u500b\u5713\u76e4 */\nfun move(src: MutableList&lt;Int&gt;, tar: MutableList&lt;Int&gt;) {\n // \u5f9e src \u9802\u90e8\u62ff\u51fa\u4e00\u500b\u5713\u76e4\n val pan = src.removeAt(src.size - 1)\n // \u5c07\u5713\u76e4\u653e\u5165 tar \u9802\u90e8\n tar.add(pan)\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c f(i) */\nfun dfs(i: Int, src: MutableList&lt;Int&gt;, buf: MutableList&lt;Int&gt;, tar: MutableList&lt;Int&gt;) {\n // \u82e5 src \u53ea\u5269\u4e0b\u4e00\u500b\u5713\u76e4\uff0c\u5247\u76f4\u63a5\u5c07\u5176\u79fb\u5230 tar\n if (i == 1) {\n move(src, tar)\n return\n }\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 src \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 tar \u79fb\u5230 buf\n dfs(i - 1, src, tar, buf)\n // \u5b50\u554f\u984c f(1) \uff1a\u5c07 src \u5269\u9918\u4e00\u500b\u5713\u76e4\u79fb\u5230 tar\n move(src, tar)\n // \u5b50\u554f\u984c f(i-1) \uff1a\u5c07 buf \u9802\u90e8 i-1 \u500b\u5713\u76e4\u85c9\u52a9 src \u79fb\u5230 tar\n dfs(i - 1, buf, src, tar)\n}\n\n/* \u6c42\u89e3\u6cb3\u5167\u5854\u554f\u984c */\nfun solveHanota(A: MutableList&lt;Int&gt;, B: MutableList&lt;Int&gt;, C: MutableList&lt;Int&gt;) {\n val n = A.size\n // \u5c07 A \u9802\u90e8 n \u500b\u5713\u76e4\u85c9\u52a9 B \u79fb\u5230 C\n dfs(n, A, B, C)\n}\n</code></pre> hanota.rb<pre><code>[class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solve_hanota}\n</code></pre> hanota.zig<pre><code>[class]{}-[func]{move}\n\n[class]{}-[func]{dfs}\n\n[class]{}-[func]{solveHanota}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5982\u5716 12-15 \u6240\u793a\uff0c\u6cb3\u5167\u5854\u554f\u984c\u5f62\u6210\u4e00\u68f5\u9ad8\u5ea6\u70ba \\(n\\) \u7684\u905e\u8ff4\u6a39\uff0c\u6bcf\u500b\u7bc0\u9ede\u4ee3\u8868\u4e00\u500b\u5b50\u554f\u984c\uff0c\u5c0d\u61c9\u4e00\u500b\u958b\u555f\u7684 <code>dfs()</code> \u51fd\u5f0f\uff0c\u56e0\u6b64\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(2^n)\\) \uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \u3002</p> <p></p> <p> \u5716 12-15 \u00a0 \u6cb3\u5167\u5854\u554f\u984c\u7684\u905e\u8ff4\u6a39 </p> <p>Quote</p> <p>\u6cb3\u5167\u5854\u554f\u984c\u6e90\u81ea\u4e00\u500b\u53e4\u8001\u7684\u50b3\u8aaa\u3002\u5728\u53e4\u5370\u5ea6\u7684\u4e00\u500b\u5bfa\u5edf\u88e1\uff0c\u50e7\u4fb6\u5011\u6709\u4e09\u6839\u9ad8\u5927\u7684\u947d\u77f3\u67f1\u5b50\uff0c\u4ee5\u53ca \\(64\\) \u500b\u5927\u5c0f\u4e0d\u4e00\u7684\u91d1\u5713\u76e4\u3002\u50e7\u4fb6\u5011\u4e0d\u65b7\u5730\u79fb\u52d5\u5713\u76e4\uff0c\u4ed6\u5011\u76f8\u4fe1\u5728\u6700\u5f8c\u4e00\u500b\u5713\u76e4\u88ab\u6b63\u78ba\u653e\u7f6e\u7684\u90a3\u4e00\u523b\uff0c\u9019\u500b\u4e16\u754c\u5c31\u6703\u7d50\u675f\u3002</p> <p>\u7136\u800c\uff0c\u5373\u4f7f\u50e7\u4fb6\u5011\u6bcf\u79d2\u9418\u79fb\u52d5\u4e00\u6b21\uff0c\u7e3d\u5171\u9700\u8981\u5927\u7d04 \\(2^{64} \\approx 1.84\u00d710^{19}\\) \u79d2\uff0c\u5408\u7d04 \\(5850\\) \u5104\u5e74\uff0c\u9060\u9060\u8d85\u904e\u4e86\u73fe\u5728\u5c0d\u5b87\u5b99\u5e74\u9f61\u7684\u4f30\u8a08\u3002\u6240\u4ee5\uff0c\u5018\u82e5\u9019\u500b\u50b3\u8aaa\u662f\u771f\u7684\uff0c\u6211\u5011\u61c9\u8a72\u4e0d\u9700\u8981\u64d4\u5fc3\u4e16\u754c\u672b\u65e5\u7684\u5230\u4f86\u3002</p>"},{"location":"chapter_divide_and_conquer/summary/","title":"12.5 \u00a0 \u5c0f\u7d50","text":"<ul> <li>\u5206\u6cbb\u662f\u4e00\u7a2e\u5e38\u898b\u7684\u6f14\u7b97\u6cd5\u8a2d\u8a08\u7b56\u7565\uff0c\u5305\u62ec\u5206\uff08\u5283\u5206\uff09\u548c\u6cbb\uff08\u5408\u4f75\uff09\u5169\u500b\u968e\u6bb5\uff0c\u901a\u5e38\u57fa\u65bc\u905e\u8ff4\u5be6\u73fe\u3002</li> <li>\u5224\u65b7\u662f\u5426\u662f\u5206\u6cbb\u6f14\u7b97\u6cd5\u554f\u984c\u7684\u4f9d\u64da\u5305\u62ec\uff1a\u554f\u984c\u80fd\u5426\u5206\u89e3\u3001\u5b50\u554f\u984c\u662f\u5426\u7368\u7acb\u3001\u5b50\u554f\u984c\u80fd\u5426\u5408\u4f75\u3002</li> <li>\u5408\u4f75\u6392\u5e8f\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u61c9\u7528\uff0c\u5176\u905e\u8ff4\u5730\u5c07\u9663\u5217\u5283\u5206\u70ba\u7b49\u9577\u7684\u5169\u500b\u5b50\u9663\u5217\uff0c\u76f4\u5230\u53ea\u5269\u4e00\u500b\u5143\u7d20\u6642\u958b\u59cb\u9010\u5c64\u5408\u4f75\uff0c\u5f9e\u800c\u5b8c\u6210\u6392\u5e8f\u3002</li> <li>\u5f15\u5165\u5206\u6cbb\u7b56\u7565\u5f80\u5f80\u53ef\u4ee5\u63d0\u5347\u6f14\u7b97\u6cd5\u6548\u7387\u3002\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u7b56\u7565\u6e1b\u5c11\u4e86\u64cd\u4f5c\u6578\u91cf\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u5f8c\u6709\u5229\u65bc\u7cfb\u7d71\u7684\u4e26\u884c\u6700\u4f73\u5316\u3002</li> <li>\u5206\u6cbb\u65e2\u53ef\u4ee5\u89e3\u6c7a\u8a31\u591a\u6f14\u7b97\u6cd5\u554f\u984c\uff0c\u4e5f\u5ee3\u6cdb\u61c9\u7528\u65bc\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u8a2d\u8a08\u4e2d\uff0c\u8655\u8655\u53ef\u898b\u5176\u8eab\u5f71\u3002</li> <li>\u76f8\u8f03\u65bc\u66b4\u529b\u641c\u5c0b\uff0c\u81ea\u9069\u61c9\u641c\u5c0b\u6548\u7387\u66f4\u9ad8\u3002\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(\\log n)\\) \u7684\u641c\u5c0b\u6f14\u7b97\u6cd5\u901a\u5e38\u662f\u57fa\u65bc\u5206\u6cbb\u7b56\u7565\u5be6\u73fe\u7684\u3002</li> <li>\u4e8c\u5206\u641c\u5c0b\u662f\u5206\u6cbb\u7b56\u7565\u7684\u53e6\u4e00\u500b\u5178\u578b\u61c9\u7528\uff0c\u5b83\u4e0d\u5305\u542b\u5c07\u5b50\u554f\u984c\u7684\u89e3\u9032\u884c\u5408\u4f75\u7684\u6b65\u9a5f\u3002\u6211\u5011\u53ef\u4ee5\u900f\u904e\u905e\u8ff4\u5206\u6cbb\u5be6\u73fe\u4e8c\u5206\u641c\u5c0b\u3002</li> <li>\u5728\u69cb\u5efa\u4e8c\u5143\u6a39\u7684\u554f\u984c\u4e2d\uff0c\u69cb\u5efa\u6a39\uff08\u539f\u554f\u984c\uff09\u53ef\u4ee5\u5283\u5206\u70ba\u69cb\u5efa\u5de6\u5b50\u6a39\u548c\u53f3\u5b50\u6a39\uff08\u5b50\u554f\u984c\uff09\uff0c\u9019\u53ef\u4ee5\u900f\u904e\u5283\u5206\u524d\u5e8f\u8d70\u8a2a\u548c\u4e2d\u5e8f\u8d70\u8a2a\u7684\u7d22\u5f15\u5340\u9593\u4f86\u5be6\u73fe\u3002</li> <li>\u5728\u6cb3\u5167\u5854\u554f\u984c\u4e2d\uff0c\u4e00\u500b\u898f\u6a21\u70ba \\(n\\) \u7684\u554f\u984c\u53ef\u4ee5\u5283\u5206\u70ba\u5169\u500b\u898f\u6a21\u70ba \\(n-1\\) \u7684\u5b50\u554f\u984c\u548c\u4e00\u500b\u898f\u6a21\u70ba \\(1\\) \u7684\u5b50\u554f\u984c\u3002\u6309\u9806\u5e8f\u89e3\u6c7a\u9019\u4e09\u500b\u5b50\u554f\u984c\u5f8c\uff0c\u539f\u554f\u984c\u96a8\u4e4b\u5f97\u5230\u89e3\u6c7a\u3002</li> </ul>"},{"location":"chapter_dynamic_programming/","title":"\u7b2c 14 \u7ae0 \u00a0 \u52d5\u614b\u898f\u5283","text":"<p>Abstract</p> <p>\u5c0f\u6eaa\u532f\u5165\u6cb3\u6d41\uff0c\u6c5f\u6cb3\u532f\u5165\u5927\u6d77\u3002</p> <p>\u52d5\u614b\u898f\u5283\u5c07\u5c0f\u554f\u984c\u7684\u89e3\u5f59\u96c6\u6210\u5927\u554f\u984c\u7684\u7b54\u6848\uff0c\u4e00\u6b65\u6b65\u5f15\u9818\u6211\u5011\u8d70\u5411\u89e3\u6c7a\u554f\u984c\u7684\u5f7c\u5cb8\u3002</p>"},{"location":"chapter_dynamic_programming/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>14.1 \u00a0 \u521d\u63a2\u52d5\u614b\u898f\u5283</li> <li>14.2 \u00a0 DP \u554f\u984c\u7279\u6027</li> <li>14.3 \u00a0 DP \u89e3\u984c\u601d\u8def</li> <li>14.4 \u00a0 0-1 \u80cc\u5305\u554f\u984c</li> <li>14.5 \u00a0 \u5b8c\u5168\u80cc\u5305\u554f\u984c</li> <li>14.6 \u00a0 \u7de8\u8f2f\u8ddd\u96e2\u554f\u984c</li> <li>14.7 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_dynamic_programming/dp_problem_features/","title":"14.2 \u00a0 \u52d5\u614b\u898f\u5283\u554f\u984c\u7279\u6027","text":"<p>\u5728\u4e0a\u4e00\u7bc0\u4e2d\uff0c\u6211\u5011\u5b78\u7fd2\u4e86\u52d5\u614b\u898f\u5283\u662f\u5982\u4f55\u900f\u904e\u5b50\u554f\u984c\u5206\u89e3\u4f86\u6c42\u89e3\u539f\u554f\u984c\u7684\u3002\u5be6\u969b\u4e0a\uff0c\u5b50\u554f\u984c\u5206\u89e3\u662f\u4e00\u7a2e\u901a\u7528\u7684\u6f14\u7b97\u6cd5\u601d\u8def\uff0c\u5728\u5206\u6cbb\u3001\u52d5\u614b\u898f\u5283\u3001\u56de\u6eaf\u4e2d\u7684\u5074\u91cd\u9ede\u4e0d\u540c\u3002</p> <ul> <li>\u5206\u6cbb\u6f14\u7b97\u6cd5\u905e\u8ff4\u5730\u5c07\u539f\u554f\u984c\u5283\u5206\u70ba\u591a\u500b\u76f8\u4e92\u7368\u7acb\u7684\u5b50\u554f\u984c\uff0c\u76f4\u81f3\u6700\u5c0f\u5b50\u554f\u984c\uff0c\u4e26\u5728\u56de\u6eaf\u4e2d\u5408\u4f75\u5b50\u554f\u984c\u7684\u89e3\uff0c\u6700\u7d42\u5f97\u5230\u539f\u554f\u984c\u7684\u89e3\u3002</li> <li>\u52d5\u614b\u898f\u5283\u4e5f\u5c0d\u554f\u984c\u9032\u884c\u905e\u8ff4\u5206\u89e3\uff0c\u4f46\u8207\u5206\u6cbb\u6f14\u7b97\u6cd5\u7684\u4e3b\u8981\u5340\u5225\u662f\uff0c\u52d5\u614b\u898f\u5283\u4e2d\u7684\u5b50\u554f\u984c\u662f\u76f8\u4e92\u4f9d\u8cf4\u7684\uff0c\u5728\u5206\u89e3\u904e\u7a0b\u4e2d\u6703\u51fa\u73fe\u8a31\u591a\u91cd\u758a\u5b50\u554f\u984c\u3002</li> <li>\u56de\u6eaf\u6f14\u7b97\u6cd5\u5728\u5617\u8a66\u548c\u56de\u9000\u4e2d\u7aae\u8209\u6240\u6709\u53ef\u80fd\u7684\u89e3\uff0c\u4e26\u900f\u904e\u526a\u679d\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u641c\u5c0b\u5206\u652f\u3002\u539f\u554f\u984c\u7684\u89e3\u7531\u4e00\u7cfb\u5217\u6c7a\u7b56\u6b65\u9a5f\u69cb\u6210\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u6bcf\u500b\u6c7a\u7b56\u6b65\u9a5f\u4e4b\u524d\u7684\u5b50\u5e8f\u5217\u770b\u4f5c\u4e00\u500b\u5b50\u554f\u984c\u3002</li> </ul> <p>\u5be6\u969b\u4e0a\uff0c\u52d5\u614b\u898f\u5283\u5e38\u7528\u4f86\u6c42\u89e3\u6700\u6700\u4f73\u5316\u554f\u984c\uff0c\u5b83\u5011\u4e0d\u50c5\u5305\u542b\u91cd\u758a\u5b50\u554f\u984c\uff0c\u9084\u5177\u6709\u53e6\u5916\u5169\u5927\u7279\u6027\uff1a\u6700\u512a\u5b50\u7d50\u69cb\u3001\u7121\u5f8c\u6548\u6027\u3002</p>"},{"location":"chapter_dynamic_programming/dp_problem_features/#1421","title":"14.2.1 \u00a0 \u6700\u512a\u5b50\u7d50\u69cb","text":"<p>\u6211\u5011\u5c0d\u722c\u6a13\u68af\u554f\u984c\u7a0d\u4f5c\u6539\u52d5\uff0c\u4f7f\u4e4b\u66f4\u52a0\u9069\u5408\u5c55\u793a\u6700\u512a\u5b50\u7d50\u69cb\u6982\u5ff5\u3002</p> <p>\u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9</p> <p>\u7d66\u5b9a\u4e00\u500b\u6a13\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u968e\u6216\u8005 \\(2\\) \u968e\uff0c\u6bcf\u4e00\u968e\u6a13\u68af\u4e0a\u90fd\u8cbc\u6709\u4e00\u500b\u975e\u8ca0\u6574\u6578\uff0c\u8868\u793a\u4f60\u5728\u8a72\u81fa\u968e\u6240\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u50f9\u3002\u7d66\u5b9a\u4e00\u500b\u975e\u8ca0\u6574\u6578\u9663\u5217 \\(cost\\) \uff0c\u5176\u4e2d \\(cost[i]\\) \u8868\u793a\u5728\u7b2c \\(i\\) \u500b\u81fa\u968e\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u50f9\uff0c\\(cost[0]\\) \u70ba\u5730\u9762\uff08\u8d77\u59cb\u9ede\uff09\u3002\u8acb\u8a08\u7b97\u6700\u5c11\u9700\u8981\u4ed8\u51fa\u591a\u5c11\u4ee3\u50f9\u624d\u80fd\u5230\u9054\u9802\u90e8\uff1f</p> <p>\u5982\u5716 14-6 \u6240\u793a\uff0c\u82e5\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u968e\u7684\u4ee3\u50f9\u5206\u5225\u70ba \\(1\\)\u3001\\(10\\)\u3001\\(1\\) \uff0c\u5247\u5f9e\u5730\u9762\u722c\u5230\u7b2c \\(3\\) \u968e\u7684\u6700\u5c0f\u4ee3\u50f9\u70ba \\(2\\) \u3002</p> <p></p> <p> \u5716 14-6 \u00a0 \u722c\u5230\u7b2c 3 \u968e\u7684\u6700\u5c0f\u4ee3\u50f9 </p> <p>\u8a2d \\(dp[i]\\) \u70ba\u722c\u5230\u7b2c \\(i\\) \u968e\u7d2f\u8a08\u4ed8\u51fa\u7684\u4ee3\u50f9\uff0c\u7531\u65bc\u7b2c \\(i\\) \u968e\u53ea\u53ef\u80fd\u5f9e \\(i - 1\\) \u968e\u6216 \\(i - 2\\) \u968e\u8d70\u4f86\uff0c\u56e0\u6b64 \\(dp[i]\\) \u53ea\u53ef\u80fd\u7b49\u65bc \\(dp[i - 1] + cost[i]\\) \u6216 \\(dp[i - 2] + cost[i]\\) \u3002\u70ba\u4e86\u5118\u53ef\u80fd\u6e1b\u5c11\u4ee3\u50f9\uff0c\u6211\u5011\u61c9\u8a72\u9078\u64c7\u5169\u8005\u4e2d\u8f03\u5c0f\u7684\u90a3\u4e00\u500b\uff1a</p> \\[ dp[i] = \\min(dp[i-1], dp[i-2]) + cost[i] \\] <p>\u9019\u4fbf\u53ef\u4ee5\u5f15\u51fa\u6700\u512a\u5b50\u7d50\u69cb\u7684\u542b\u7fa9\uff1a\u539f\u554f\u984c\u7684\u6700\u512a\u89e3\u662f\u5f9e\u5b50\u554f\u984c\u7684\u6700\u512a\u89e3\u69cb\u5efa\u5f97\u4f86\u7684\u3002</p> <p>\u672c\u984c\u986f\u7136\u5177\u6709\u6700\u512a\u5b50\u7d50\u69cb\uff1a\u6211\u5011\u5f9e\u5169\u500b\u5b50\u554f\u984c\u6700\u512a\u89e3 \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u4e2d\u6311\u9078\u51fa\u8f03\u512a\u7684\u90a3\u4e00\u500b\uff0c\u4e26\u7528\u5b83\u69cb\u5efa\u51fa\u539f\u554f\u984c \\(dp[i]\\) \u7684\u6700\u512a\u89e3\u3002</p> <p>\u90a3\u9ebc\uff0c\u4e0a\u4e00\u7bc0\u7684\u722c\u6a13\u68af\u984c\u76ee\u6709\u6c92\u6709\u6700\u512a\u5b50\u7d50\u69cb\u5462\uff1f\u5b83\u7684\u76ee\u6a19\u662f\u6c42\u89e3\u65b9\u6848\u6578\u91cf\uff0c\u770b\u4f3c\u662f\u4e00\u500b\u8a08\u6578\u554f\u984c\uff0c\u4f46\u5982\u679c\u63db\u4e00\u7a2e\u554f\u6cd5\uff1a\u201c\u6c42\u89e3\u6700\u5927\u65b9\u6848\u6578\u91cf\u201d\u3002\u6211\u5011\u610f\u5916\u5730\u767c\u73fe\uff0c\u96d6\u7136\u984c\u76ee\u4fee\u6539\u524d\u5f8c\u662f\u7b49\u50f9\u7684\uff0c\u4f46\u6700\u512a\u5b50\u7d50\u69cb\u6d6e\u73fe\u51fa\u4f86\u4e86\uff1a\u7b2c \\(n\\) \u968e\u6700\u5927\u65b9\u6848\u6578\u91cf\u7b49\u65bc\u7b2c \\(n-1\\) \u968e\u548c\u7b2c \\(n-2\\) \u968e\u6700\u5927\u65b9\u6848\u6578\u91cf\u4e4b\u548c\u3002\u6240\u4ee5\u8aaa\uff0c\u6700\u512a\u5b50\u7d50\u69cb\u7684\u89e3\u91cb\u65b9\u5f0f\u6bd4\u8f03\u9748\u6d3b\uff0c\u5728\u4e0d\u540c\u554f\u984c\u4e2d\u6703\u6709\u4e0d\u540c\u7684\u542b\u7fa9\u3002</p> <p>\u6839\u64da\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\uff0c\u4ee5\u53ca\u521d\u59cb\u72c0\u614b \\(dp[1] = cost[1]\\) \u548c \\(dp[2] = cost[2]\\) \uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u5f97\u5230\u52d5\u614b\u898f\u5283\u7a0b\u5f0f\u78bc\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig min_cost_climbing_stairs_dp.py<pre><code>def min_cost_climbing_stairs_dp(cost: list[int]) -&gt; int:\n \"\"\"\u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u52d5\u614b\u898f\u5283\"\"\"\n n = len(cost) - 1\n if n == 1 or n == 2:\n return cost[n]\n # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n dp = [0] * (n + 1)\n # \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1], dp[2] = cost[1], cost[2]\n # \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for i in range(3, n + 1):\n dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n return dp[n]\n</code></pre> min_cost_climbing_stairs_dp.cpp<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u52d5\u614b\u898f\u5283 */\nint minCostClimbingStairsDP(vector&lt;int&gt; &amp;cost) {\n int n = cost.size() - 1;\n if (n == 1 || n == 2)\n return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n vector&lt;int&gt; dp(n + 1);\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n</code></pre> min_cost_climbing_stairs_dp.java<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u52d5\u614b\u898f\u5283 */\nint minCostClimbingStairsDP(int[] cost) {\n int n = cost.length - 1;\n if (n == 1 || n == 2)\n return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n int[] dp = new int[n + 1];\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n</code></pre> min_cost_climbing_stairs_dp.cs<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u52d5\u614b\u898f\u5283 */\nint MinCostClimbingStairsDP(int[] cost) {\n int n = cost.Length - 1;\n if (n == 1 || n == 2)\n return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n int[] dp = new int[n + 1];\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i] = Math.Min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n</code></pre> min_cost_climbing_stairs_dp.go<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u52d5\u614b\u898f\u5283 */\nfunc minCostClimbingStairsDP(cost []int) int {\n n := len(cost) - 1\n if n == 1 || n == 2 {\n return cost[n]\n }\n min := func(a, b int) int {\n if a &lt; b {\n return a\n }\n return b\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n dp := make([]int, n+1)\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = cost[1]\n dp[2] = cost[2]\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for i := 3; i &lt;= n; i++ {\n dp[i] = min(dp[i-1], dp[i-2]) + cost[i]\n }\n return dp[n]\n}\n</code></pre> min_cost_climbing_stairs_dp.swift<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u52d5\u614b\u898f\u5283 */\nfunc minCostClimbingStairsDP(cost: [Int]) -&gt; Int {\n let n = cost.count - 1\n if n == 1 || n == 2 {\n return cost[n]\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n var dp = Array(repeating: 0, count: n + 1)\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = cost[1]\n dp[2] = cost[2]\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for i in 3 ... n {\n dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n }\n return dp[n]\n}\n</code></pre> min_cost_climbing_stairs_dp.js<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u52d5\u614b\u898f\u5283 */\nfunction minCostClimbingStairsDP(cost) {\n const n = cost.length - 1;\n if (n === 1 || n === 2) {\n return cost[n];\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n const dp = new Array(n + 1);\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (let i = 3; i &lt;= n; i++) {\n dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n</code></pre> min_cost_climbing_stairs_dp.ts<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u52d5\u614b\u898f\u5283 */\nfunction minCostClimbingStairsDP(cost: Array&lt;number&gt;): number {\n const n = cost.length - 1;\n if (n === 1 || n === 2) {\n return cost[n];\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n const dp = new Array(n + 1);\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (let i = 3; i &lt;= n; i++) {\n dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n</code></pre> min_cost_climbing_stairs_dp.dart<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u52d5\u614b\u898f\u5283 */\nint minCostClimbingStairsDP(List&lt;int&gt; cost) {\n int n = cost.length - 1;\n if (n == 1 || n == 2) return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n List&lt;int&gt; dp = List.filled(n + 1, 0);\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n</code></pre> min_cost_climbing_stairs_dp.rs<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u52d5\u614b\u898f\u5283 */\nfn min_cost_climbing_stairs_dp(cost: &amp;[i32]) -&gt; i32 {\n let n = cost.len() - 1;\n if n == 1 || n == 2 {\n return cost[n];\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n let mut dp = vec![-1; n + 1];\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for i in 3..=n {\n dp[i] = cmp::min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n dp[n]\n}\n</code></pre> min_cost_climbing_stairs_dp.c<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u52d5\u614b\u898f\u5283 */\nint minCostClimbingStairsDP(int cost[], int costSize) {\n int n = costSize - 1;\n if (n == 1 || n == 2)\n return cost[n];\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n int *dp = calloc(n + 1, sizeof(int));\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i] = myMin(dp[i - 1], dp[i - 2]) + cost[i];\n }\n int res = dp[n];\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(dp);\n return res;\n}\n</code></pre> min_cost_climbing_stairs_dp.kt<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u52d5\u614b\u898f\u5283 */\nfun minCostClimbingStairsDP(cost: IntArray): Int {\n val n = cost.size - 1\n if (n == 1 || n == 2) return cost[n]\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n val dp = IntArray(n + 1)\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = cost[1]\n dp[2] = cost[2]\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (i in 3..n) {\n dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n }\n return dp[n]\n}\n</code></pre> min_cost_climbing_stairs_dp.rb<pre><code>[class]{}-[func]{min_cost_climbing_stairs_dp}\n</code></pre> min_cost_climbing_stairs_dp.zig<pre><code>// \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u52d5\u614b\u898f\u5283\nfn minCostClimbingStairsDP(comptime cost: []i32) i32 {\n comptime var n = cost.len - 1;\n if (n == 1 or n == 2) {\n return cost[n];\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n var dp = [_]i32{-1} ** (n + 1);\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = cost[1];\n dp[2] = cost[2];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (3..n + 1) |i| {\n dp[i] = @min(dp[i - 1], dp[i - 2]) + cost[i];\n }\n return dp[n];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 14-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u7a0b\u5f0f\u78bc\u7684\u52d5\u614b\u898f\u5283\u904e\u7a0b\u3002</p> <p></p> <p> \u5716 14-7 \u00a0 \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\u7684\u52d5\u614b\u898f\u5283\u904e\u7a0b </p> <p>\u672c\u984c\u4e5f\u53ef\u4ee5\u9032\u884c\u7a7a\u9593\u6700\u4f73\u5316\uff0c\u5c07\u4e00\u7dad\u58d3\u7e2e\u81f3\u96f6\u7dad\uff0c\u4f7f\u5f97\u7a7a\u9593\u8907\u96dc\u5ea6\u5f9e \\(O(n)\\) \u964d\u81f3 \\(O(1)\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig min_cost_climbing_stairs_dp.py<pre><code>def min_cost_climbing_stairs_dp_comp(cost: list[int]) -&gt; int:\n \"\"\"\u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\"\"\"\n n = len(cost) - 1\n if n == 1 or n == 2:\n return cost[n]\n a, b = cost[1], cost[2]\n for i in range(3, n + 1):\n a, b = b, min(a, b) + cost[i]\n return b\n</code></pre> min_cost_climbing_stairs_dp.cpp<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint minCostClimbingStairsDPComp(vector&lt;int&gt; &amp;cost) {\n int n = cost.size() - 1;\n if (n == 1 || n == 2)\n return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i &lt;= n; i++) {\n int tmp = b;\n b = min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n</code></pre> min_cost_climbing_stairs_dp.java<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint minCostClimbingStairsDPComp(int[] cost) {\n int n = cost.length - 1;\n if (n == 1 || n == 2)\n return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i &lt;= n; i++) {\n int tmp = b;\n b = Math.min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n</code></pre> min_cost_climbing_stairs_dp.cs<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint MinCostClimbingStairsDPComp(int[] cost) {\n int n = cost.Length - 1;\n if (n == 1 || n == 2)\n return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i &lt;= n; i++) {\n int tmp = b;\n b = Math.Min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n</code></pre> min_cost_climbing_stairs_dp.go<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc minCostClimbingStairsDPComp(cost []int) int {\n n := len(cost) - 1\n if n == 1 || n == 2 {\n return cost[n]\n }\n min := func(a, b int) int {\n if a &lt; b {\n return a\n }\n return b\n }\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n a, b := cost[1], cost[2]\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for i := 3; i &lt;= n; i++ {\n tmp := b\n b = min(a, tmp) + cost[i]\n a = tmp\n }\n return b\n}\n</code></pre> min_cost_climbing_stairs_dp.swift<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc minCostClimbingStairsDPComp(cost: [Int]) -&gt; Int {\n let n = cost.count - 1\n if n == 1 || n == 2 {\n return cost[n]\n }\n var (a, b) = (cost[1], cost[2])\n for i in 3 ... n {\n (a, b) = (b, min(a, b) + cost[i])\n }\n return b\n}\n</code></pre> min_cost_climbing_stairs_dp.js<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u72c0\u614b\u58d3\u7e2e\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction minCostClimbingStairsDPComp(cost) {\n const n = cost.length - 1;\n if (n === 1 || n === 2) {\n return cost[n];\n }\n let a = cost[1],\n b = cost[2];\n for (let i = 3; i &lt;= n; i++) {\n const tmp = b;\n b = Math.min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n</code></pre> min_cost_climbing_stairs_dp.ts<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u72c0\u614b\u58d3\u7e2e\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction minCostClimbingStairsDPComp(cost: Array&lt;number&gt;): number {\n const n = cost.length - 1;\n if (n === 1 || n === 2) {\n return cost[n];\n }\n let a = cost[1],\n b = cost[2];\n for (let i = 3; i &lt;= n; i++) {\n const tmp = b;\n b = Math.min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n</code></pre> min_cost_climbing_stairs_dp.dart<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint minCostClimbingStairsDPComp(List&lt;int&gt; cost) {\n int n = cost.length - 1;\n if (n == 1 || n == 2) return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i &lt;= n; i++) {\n int tmp = b;\n b = min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n</code></pre> min_cost_climbing_stairs_dp.rs<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfn min_cost_climbing_stairs_dp_comp(cost: &amp;[i32]) -&gt; i32 {\n let n = cost.len() - 1;\n if n == 1 || n == 2 {\n return cost[n];\n };\n let (mut a, mut b) = (cost[1], cost[2]);\n for i in 3..=n {\n let tmp = b;\n b = cmp::min(a, tmp) + cost[i];\n a = tmp;\n }\n b\n}\n</code></pre> min_cost_climbing_stairs_dp.c<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint minCostClimbingStairsDPComp(int cost[], int costSize) {\n int n = costSize - 1;\n if (n == 1 || n == 2)\n return cost[n];\n int a = cost[1], b = cost[2];\n for (int i = 3; i &lt;= n; i++) {\n int tmp = b;\n b = myMin(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n</code></pre> min_cost_climbing_stairs_dp.kt<pre><code>/* \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfun minCostClimbingStairsDPComp(cost: IntArray): Int {\n val n = cost.size - 1\n if (n == 1 || n == 2) return cost[n]\n var a = cost[1]\n var b = cost[2]\n for (i in 3..n) {\n val tmp = b\n b = min(a, tmp) + cost[i]\n a = tmp\n }\n return b\n}\n</code></pre> min_cost_climbing_stairs_dp.rb<pre><code>[class]{}-[func]{min_cost_climbing_stairs_dp_comp}\n</code></pre> min_cost_climbing_stairs_dp.zig<pre><code>// \u722c\u6a13\u68af\u6700\u5c0f\u4ee3\u50f9\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\nfn minCostClimbingStairsDPComp(cost: []i32) i32 {\n var n = cost.len - 1;\n if (n == 1 or n == 2) {\n return cost[n];\n }\n var a = cost[1];\n var b = cost[2];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (3..n + 1) |i| {\n var tmp = b;\n b = @min(a, tmp) + cost[i];\n a = tmp;\n }\n return b;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_dynamic_programming/dp_problem_features/#1422","title":"14.2.2 \u00a0 \u7121\u5f8c\u6548\u6027","text":"<p>\u7121\u5f8c\u6548\u6027\u662f\u52d5\u614b\u898f\u5283\u80fd\u5920\u6709\u6548\u89e3\u6c7a\u554f\u984c\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c\u5176\u5b9a\u7fa9\u70ba\uff1a\u7d66\u5b9a\u4e00\u500b\u78ba\u5b9a\u7684\u72c0\u614b\uff0c\u5b83\u7684\u672a\u4f86\u767c\u5c55\u53ea\u8207\u7576\u524d\u72c0\u614b\u6709\u95dc\uff0c\u800c\u8207\u904e\u53bb\u7d93\u6b77\u7684\u6240\u6709\u72c0\u614b\u7121\u95dc\u3002</p> <p>\u4ee5\u722c\u6a13\u68af\u554f\u984c\u70ba\u4f8b\uff0c\u7d66\u5b9a\u72c0\u614b \\(i\\) \uff0c\u5b83\u6703\u767c\u5c55\u51fa\u72c0\u614b \\(i+1\\) \u548c\u72c0\u614b \\(i+2\\) \uff0c\u5206\u5225\u5c0d\u61c9\u8df3 \\(1\\) \u6b65\u548c\u8df3 \\(2\\) \u6b65\u3002\u5728\u505a\u51fa\u9019\u5169\u7a2e\u9078\u64c7\u6642\uff0c\u6211\u5011\u7121\u9808\u8003\u616e\u72c0\u614b \\(i\\) \u4e4b\u524d\u7684\u72c0\u614b\uff0c\u5b83\u5011\u5c0d\u72c0\u614b \\(i\\) \u7684\u672a\u4f86\u6c92\u6709\u5f71\u97ff\u3002</p> <p>\u7136\u800c\uff0c\u5982\u679c\u6211\u5011\u7d66\u722c\u6a13\u68af\u554f\u984c\u65b0\u589e\u4e00\u500b\u7d04\u675f\uff0c\u60c5\u6cc1\u5c31\u4e0d\u4e00\u6a23\u4e86\u3002</p> <p>\u5e36\u7d04\u675f\u722c\u6a13\u68af</p> <p>\u7d66\u5b9a\u4e00\u500b\u5171\u6709 \\(n\\) \u968e\u7684\u6a13\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u968e\u6216\u8005 \\(2\\) \u968e\uff0c\u4f46\u4e0d\u80fd\u9023\u7e8c\u5169\u8f2a\u8df3 \\(1\\) \u968e\uff0c\u8acb\u554f\u6709\u591a\u5c11\u7a2e\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u6a13\u9802\uff1f</p> <p>\u5982\u5716 14-8 \u6240\u793a\uff0c\u722c\u4e0a\u7b2c \\(3\\) \u968e\u50c5\u5269 \\(2\\) \u7a2e\u53ef\u884c\u65b9\u6848\uff0c\u5176\u4e2d\u9023\u7e8c\u4e09\u6b21\u8df3 \\(1\\) \u968e\u7684\u65b9\u6848\u4e0d\u6eff\u8db3\u7d04\u675f\u689d\u4ef6\uff0c\u56e0\u6b64\u88ab\u6368\u68c4\u3002</p> <p></p> <p> \u5716 14-8 \u00a0 \u5e36\u7d04\u675f\u722c\u5230\u7b2c 3 \u968e\u7684\u65b9\u6848\u6578\u91cf </p> <p>\u5728\u8a72\u554f\u984c\u4e2d\uff0c\u5982\u679c\u4e0a\u4e00\u8f2a\u662f\u8df3 \\(1\\) \u968e\u4e0a\u4f86\u7684\uff0c\u90a3\u9ebc\u4e0b\u4e00\u8f2a\u5c31\u5fc5\u9808\u8df3 \\(2\\) \u968e\u3002\u9019\u610f\u5473\u8457\uff0c\u4e0b\u4e00\u6b65\u9078\u64c7\u4e0d\u80fd\u7531\u7576\u524d\u72c0\u614b\uff08\u7576\u524d\u6240\u5728\u6a13\u68af\u968e\u6578\uff09\u7368\u7acb\u6c7a\u5b9a\uff0c\u9084\u548c\u524d\u4e00\u500b\u72c0\u614b\uff08\u4e0a\u4e00\u8f2a\u6240\u5728\u6a13\u68af\u968e\u6578\uff09\u6709\u95dc\u3002</p> <p>\u4e0d\u96e3\u767c\u73fe\uff0c\u6b64\u554f\u984c\u5df2\u4e0d\u6eff\u8db3\u7121\u5f8c\u6548\u6027\uff0c\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b \\(dp[i] = dp[i-1] + dp[i-2]\\) \u4e5f\u5931\u6548\u4e86\uff0c\u56e0\u70ba \\(dp[i-1]\\) \u4ee3\u8868\u672c\u8f2a\u8df3 \\(1\\) \u968e\uff0c\u4f46\u5176\u4e2d\u5305\u542b\u4e86\u8a31\u591a\u201c\u4e0a\u4e00\u8f2a\u662f\u8df3 \\(1\\) \u968e\u4e0a\u4f86\u7684\u201d\u65b9\u6848\uff0c\u800c\u70ba\u4e86\u6eff\u8db3\u7d04\u675f\uff0c\u6211\u5011\u5c31\u4e0d\u80fd\u5c07 \\(dp[i-1]\\) \u76f4\u63a5\u8a08\u5165 \\(dp[i]\\) \u4e2d\u3002</p> <p>\u70ba\u6b64\uff0c\u6211\u5011\u9700\u8981\u64f4\u5c55\u72c0\u614b\u5b9a\u7fa9\uff1a\u72c0\u614b \\([i, j]\\) \u8868\u793a\u8655\u5728\u7b2c \\(i\\) \u968e\u4e26\u4e14\u4e0a\u4e00\u8f2a\u8df3\u4e86 \\(j\\) \u968e\uff0c\u5176\u4e2d \\(j \\in \\{1, 2\\}\\) \u3002\u6b64\u72c0\u614b\u5b9a\u7fa9\u6709\u6548\u5730\u5340\u5206\u4e86\u4e0a\u4e00\u8f2a\u8df3\u4e86 \\(1\\) \u968e\u9084\u662f \\(2\\) \u968e\uff0c\u6211\u5011\u53ef\u4ee5\u64da\u6b64\u5224\u65b7\u7576\u524d\u72c0\u614b\u662f\u5f9e\u4f55\u800c\u4f86\u7684\u3002</p> <ul> <li>\u7576\u4e0a\u4e00\u8f2a\u8df3\u4e86 \\(1\\) \u968e\u6642\uff0c\u4e0a\u4e0a\u4e00\u8f2a\u53ea\u80fd\u9078\u64c7\u8df3 \\(2\\) \u968e\uff0c\u5373 \\(dp[i, 1]\\) \u53ea\u80fd\u5f9e \\(dp[i-1, 2]\\) \u8f49\u79fb\u904e\u4f86\u3002</li> <li>\u7576\u4e0a\u4e00\u8f2a\u8df3\u4e86 \\(2\\) \u968e\u6642\uff0c\u4e0a\u4e0a\u4e00\u8f2a\u53ef\u9078\u64c7\u8df3 \\(1\\) \u968e\u6216\u8df3 \\(2\\) \u968e\uff0c\u5373 \\(dp[i, 2]\\) \u53ef\u4ee5\u5f9e \\(dp[i-2, 1]\\) \u6216 \\(dp[i-2, 2]\\) \u8f49\u79fb\u904e\u4f86\u3002</li> </ul> <p>\u5982\u5716 14-9 \u6240\u793a\uff0c\u5728\u8a72\u5b9a\u7fa9\u4e0b\uff0c\\(dp[i, j]\\) \u8868\u793a\u72c0\u614b \\([i, j]\\) \u5c0d\u61c9\u7684\u65b9\u6848\u6578\u3002\u6b64\u6642\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\u70ba\uff1a</p> \\[ \\begin{cases} dp[i, 1] = dp[i-1, 2] \\\\ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2] \\end{cases} \\] <p></p> <p> \u5716 14-9 \u00a0 \u8003\u616e\u7d04\u675f\u4e0b\u7684\u905e\u63a8\u95dc\u4fc2 </p> <p>\u6700\u7d42\uff0c\u8fd4\u56de \\(dp[n, 1] + dp[n, 2]\\) \u5373\u53ef\uff0c\u5169\u8005\u4e4b\u548c\u4ee3\u8868\u722c\u5230\u7b2c \\(n\\) \u968e\u7684\u65b9\u6848\u7e3d\u6578\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig climbing_stairs_constraint_dp.py<pre><code>def climbing_stairs_constraint_dp(n: int) -&gt; int:\n \"\"\"\u5e36\u7d04\u675f\u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283\"\"\"\n if n == 1 or n == 2:\n return 1\n # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n dp = [[0] * 3 for _ in range(n + 1)]\n # \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1][1], dp[1][2] = 1, 0\n dp[2][1], dp[2][2] = 0, 1\n # \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for i in range(3, n + 1):\n dp[i][1] = dp[i - 1][2]\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n return dp[n][1] + dp[n][2]\n</code></pre> climbing_stairs_constraint_dp.cpp<pre><code>/* \u5e36\u7d04\u675f\u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nint climbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(3, 0));\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n</code></pre> climbing_stairs_constraint_dp.java<pre><code>/* \u5e36\u7d04\u675f\u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nint climbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n int[][] dp = new int[n + 1][3];\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n</code></pre> climbing_stairs_constraint_dp.cs<pre><code>/* \u5e36\u7d04\u675f\u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nint ClimbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n int[,] dp = new int[n + 1, 3];\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1, 1] = 1;\n dp[1, 2] = 0;\n dp[2, 1] = 0;\n dp[2, 2] = 1;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i, 1] = dp[i - 1, 2];\n dp[i, 2] = dp[i - 2, 1] + dp[i - 2, 2];\n }\n return dp[n, 1] + dp[n, 2];\n}\n</code></pre> climbing_stairs_constraint_dp.go<pre><code>/* \u5e36\u7d04\u675f\u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nfunc climbingStairsConstraintDP(n int) int {\n if n == 1 || n == 2 {\n return 1\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n dp := make([][3]int, n+1)\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1][1] = 1\n dp[1][2] = 0\n dp[2][1] = 0\n dp[2][2] = 1\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for i := 3; i &lt;= n; i++ {\n dp[i][1] = dp[i-1][2]\n dp[i][2] = dp[i-2][1] + dp[i-2][2]\n }\n return dp[n][1] + dp[n][2]\n}\n</code></pre> climbing_stairs_constraint_dp.swift<pre><code>/* \u5e36\u7d04\u675f\u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nfunc climbingStairsConstraintDP(n: Int) -&gt; Int {\n if n == 1 || n == 2 {\n return 1\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n var dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1][1] = 1\n dp[1][2] = 0\n dp[2][1] = 0\n dp[2][2] = 1\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for i in 3 ... n {\n dp[i][1] = dp[i - 1][2]\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n }\n return dp[n][1] + dp[n][2]\n}\n</code></pre> climbing_stairs_constraint_dp.js<pre><code>/* \u5e36\u7d04\u675f\u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nfunction climbingStairsConstraintDP(n) {\n if (n === 1 || n === 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n const dp = Array.from(new Array(n + 1), () =&gt; new Array(3));\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (let i = 3; i &lt;= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n</code></pre> climbing_stairs_constraint_dp.ts<pre><code>/* \u5e36\u7d04\u675f\u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nfunction climbingStairsConstraintDP(n: number): number {\n if (n === 1 || n === 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n const dp = Array.from({ length: n + 1 }, () =&gt; new Array(3));\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (let i = 3; i &lt;= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n</code></pre> climbing_stairs_constraint_dp.dart<pre><code>/* \u5e36\u7d04\u675f\u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nint climbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n List&lt;List&lt;int&gt;&gt; dp = List.generate(n + 1, (index) =&gt; List.filled(3, 0));\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n</code></pre> climbing_stairs_constraint_dp.rs<pre><code>/* \u5e36\u7d04\u675f\u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nfn climbing_stairs_constraint_dp(n: usize) -&gt; i32 {\n if n == 1 || n == 2 {\n return 1;\n };\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n let mut dp = vec![vec![-1; 3]; n + 1];\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for i in 3..=n {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n dp[n][1] + dp[n][2]\n}\n</code></pre> climbing_stairs_constraint_dp.c<pre><code>/* \u5e36\u7d04\u675f\u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nint climbingStairsConstraintDP(int n) {\n if (n == 1 || n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i &lt;= n; i++) {\n dp[i] = calloc(3, sizeof(int));\n }\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n int res = dp[n][1] + dp[n][2];\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n for (int i = 0; i &lt;= n; i++) {\n free(dp[i]);\n }\n free(dp);\n return res;\n}\n</code></pre> climbing_stairs_constraint_dp.kt<pre><code>/* \u5e36\u7d04\u675f\u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nfun climbingStairsConstraintDP(n: Int): Int {\n if (n == 1 || n == 2) {\n return 1\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n val dp = Array(n + 1) { IntArray(3) }\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1][1] = 1\n dp[1][2] = 0\n dp[2][1] = 0\n dp[2][2] = 1\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (i in 3..n) {\n dp[i][1] = dp[i - 1][2]\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n }\n return dp[n][1] + dp[n][2]\n}\n</code></pre> climbing_stairs_constraint_dp.rb<pre><code>[class]{}-[func]{climbing_stairs_constraint_dp}\n</code></pre> climbing_stairs_constraint_dp.zig<pre><code>// \u5e36\u7d04\u675f\u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283\nfn climbingStairsConstraintDP(comptime n: usize) i32 {\n if (n == 1 or n == 2) {\n return 1;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n var dp = [_][3]i32{ [_]i32{ -1, -1, -1 } } ** (n + 1);\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1][1] = 1;\n dp[1][2] = 0;\n dp[2][1] = 0;\n dp[2][2] = 1;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (3..n + 1) |i| {\n dp[i][1] = dp[i - 1][2];\n dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n }\n return dp[n][1] + dp[n][2];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5728\u4e0a\u9762\u7684\u6848\u4f8b\u4e2d\uff0c\u7531\u65bc\u50c5\u9700\u591a\u8003\u616e\u524d\u9762\u4e00\u500b\u72c0\u614b\uff0c\u56e0\u6b64\u6211\u5011\u4ecd\u7136\u53ef\u4ee5\u900f\u904e\u64f4\u5c55\u72c0\u614b\u5b9a\u7fa9\uff0c\u4f7f\u5f97\u554f\u984c\u91cd\u65b0\u6eff\u8db3\u7121\u5f8c\u6548\u6027\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u554f\u984c\u5177\u6709\u975e\u5e38\u56b4\u91cd\u7684\u201c\u6709\u5f8c\u6548\u6027\u201d\u3002</p> <p>\u722c\u6a13\u68af\u8207\u969c\u7919\u751f\u6210</p> <p>\u7d66\u5b9a\u4e00\u500b\u5171\u6709 \\(n\\) \u968e\u7684\u6a13\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u968e\u6216\u8005 \\(2\\) \u968e\u3002\u898f\u5b9a\u7576\u722c\u5230\u7b2c \\(i\\) \u968e\u6642\uff0c\u7cfb\u7d71\u81ea\u52d5\u6703\u5728\u7b2c \\(2i\\) \u968e\u4e0a\u653e\u4e0a\u969c\u7919\u7269\uff0c\u4e4b\u5f8c\u6240\u6709\u8f2a\u90fd\u4e0d\u5141\u8a31\u8df3\u5230\u7b2c \\(2i\\) \u968e\u4e0a\u3002\u4f8b\u5982\uff0c\u524d\u5169\u8f2a\u5206\u5225\u8df3\u5230\u4e86\u7b2c \\(2\\)\u3001\\(3\\) \u968e\u4e0a\uff0c\u5247\u4e4b\u5f8c\u5c31\u4e0d\u80fd\u8df3\u5230\u7b2c \\(4\\)\u3001\\(6\\) \u968e\u4e0a\u3002\u8acb\u554f\u6709\u591a\u5c11\u7a2e\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u6a13\u9802\uff1f</p> <p>\u5728\u9019\u500b\u554f\u984c\u4e2d\uff0c\u4e0b\u6b21\u8df3\u8e8d\u4f9d\u8cf4\u904e\u53bb\u6240\u6709\u7684\u72c0\u614b\uff0c\u56e0\u70ba\u6bcf\u4e00\u6b21\u8df3\u8e8d\u90fd\u6703\u5728\u66f4\u9ad8\u7684\u968e\u68af\u4e0a\u8a2d\u5b9a\u969c\u7919\uff0c\u4e26\u5f71\u97ff\u672a\u4f86\u7684\u8df3\u8e8d\u3002\u5c0d\u65bc\u9019\u985e\u554f\u984c\uff0c\u52d5\u614b\u898f\u5283\u5f80\u5f80\u96e3\u4ee5\u89e3\u6c7a\u3002</p> <p>\u5be6\u969b\u4e0a\uff0c\u8a31\u591a\u8907\u96dc\u7684\u7d44\u5408\u6700\u4f73\u5316\u554f\u984c\uff08\u4f8b\u5982\u65c5\u884c\u5546\u554f\u984c\uff09\u4e0d\u6eff\u8db3\u7121\u5f8c\u6548\u6027\u3002\u5c0d\u65bc\u9019\u985e\u554f\u984c\uff0c\u6211\u5011\u901a\u5e38\u6703\u9078\u64c7\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\uff0c\u4f8b\u5982\u555f\u767c\u5f0f\u641c\u5c0b\u3001\u907a\u50b3\u6f14\u7b97\u6cd5\u3001\u5f37\u5316\u5b78\u7fd2\u7b49\uff0c\u5f9e\u800c\u5728\u6709\u9650\u6642\u9593\u5167\u5f97\u5230\u53ef\u7528\u7684\u5340\u57df\u6027\u6700\u512a\u89e3\u3002</p>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/","title":"14.3 \u00a0 \u52d5\u614b\u898f\u5283\u89e3\u984c\u601d\u8def","text":"<p>\u4e0a\u5169\u7bc0\u4ecb\u7d39\u4e86\u52d5\u614b\u898f\u5283\u554f\u984c\u7684\u4e3b\u8981\u7279\u5fb5\uff0c\u63a5\u4e0b\u4f86\u6211\u5011\u4e00\u8d77\u63a2\u7a76\u5169\u500b\u66f4\u52a0\u5be6\u7528\u7684\u554f\u984c\u3002</p> <ol> <li>\u5982\u4f55\u5224\u65b7\u4e00\u500b\u554f\u984c\u662f\u4e0d\u662f\u52d5\u614b\u898f\u5283\u554f\u984c\uff1f</li> <li>\u6c42\u89e3\u52d5\u614b\u898f\u5283\u554f\u984c\u8a72\u5f9e\u4f55\u8655\u5165\u624b\uff0c\u5b8c\u6574\u6b65\u9a5f\u662f\u4ec0\u9ebc\uff1f</li> </ol>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1431","title":"14.3.1 \u00a0 \u554f\u984c\u5224\u65b7","text":"<p>\u7e3d\u7684\u4f86\u8aaa\uff0c\u5982\u679c\u4e00\u500b\u554f\u984c\u5305\u542b\u91cd\u758a\u5b50\u554f\u984c\u3001\u6700\u512a\u5b50\u7d50\u69cb\uff0c\u4e26\u6eff\u8db3\u7121\u5f8c\u6548\u6027\uff0c\u90a3\u9ebc\u5b83\u901a\u5e38\u9069\u5408\u7528\u52d5\u614b\u898f\u5283\u6c42\u89e3\u3002\u7136\u800c\uff0c\u6211\u5011\u5f88\u96e3\u5f9e\u554f\u984c\u63cf\u8ff0\u4e2d\u76f4\u63a5\u63d0\u53d6\u51fa\u9019\u4e9b\u7279\u6027\u3002\u56e0\u6b64\u6211\u5011\u901a\u5e38\u6703\u653e\u5bec\u689d\u4ef6\uff0c\u5148\u89c0\u5bdf\u554f\u984c\u662f\u5426\u9069\u5408\u4f7f\u7528\u56de\u6eaf\uff08\u7aae\u8209\uff09\u89e3\u6c7a\u3002</p> <p>\u9069\u5408\u7528\u56de\u6eaf\u89e3\u6c7a\u7684\u554f\u984c\u901a\u5e38\u6eff\u8db3\u201c\u6c7a\u7b56\u6a39\u6a21\u578b\u201d\uff0c\u9019\u7a2e\u554f\u984c\u53ef\u4ee5\u4f7f\u7528\u6a39\u5f62\u7d50\u69cb\u4f86\u63cf\u8ff0\uff0c\u5176\u4e2d\u6bcf\u4e00\u500b\u7bc0\u9ede\u4ee3\u8868\u4e00\u500b\u6c7a\u7b56\uff0c\u6bcf\u4e00\u689d\u8def\u5f91\u4ee3\u8868\u4e00\u500b\u6c7a\u7b56\u5e8f\u5217\u3002</p> <p>\u63db\u53e5\u8a71\u8aaa\uff0c\u5982\u679c\u554f\u984c\u5305\u542b\u660e\u78ba\u7684\u6c7a\u7b56\u6982\u5ff5\uff0c\u4e26\u4e14\u89e3\u662f\u900f\u904e\u4e00\u7cfb\u5217\u6c7a\u7b56\u7522\u751f\u7684\uff0c\u90a3\u9ebc\u5b83\u5c31\u6eff\u8db3\u6c7a\u7b56\u6a39\u6a21\u578b\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u56de\u6eaf\u4f86\u89e3\u6c7a\u3002</p> <p>\u5728\u6b64\u57fa\u790e\u4e0a\uff0c\u52d5\u614b\u898f\u5283\u554f\u984c\u9084\u6709\u4e00\u4e9b\u5224\u65b7\u7684\u201c\u52a0\u5206\u9805\u201d\u3002</p> <ul> <li>\u554f\u984c\u5305\u542b\u6700\u5927\uff08\u5c0f\uff09\u6216\u6700\u591a\uff08\u5c11\uff09\u7b49\u6700\u6700\u4f73\u5316\u63cf\u8ff0\u3002</li> <li>\u554f\u984c\u7684\u72c0\u614b\u80fd\u5920\u4f7f\u7528\u4e00\u500b\u4e32\u5217\u3001\u591a\u7dad\u77e9\u9663\u6216\u6a39\u4f86\u8868\u793a\uff0c\u4e26\u4e14\u4e00\u500b\u72c0\u614b\u8207\u5176\u5468\u570d\u7684\u72c0\u614b\u5b58\u5728\u905e\u63a8\u95dc\u4fc2\u3002</li> </ul> <p>\u76f8\u61c9\u5730\uff0c\u4e5f\u5b58\u5728\u4e00\u4e9b\u201c\u6e1b\u5206\u9805\u201d\u3002</p> <ul> <li>\u554f\u984c\u7684\u76ee\u6a19\u662f\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u800c\u4e0d\u662f\u627e\u51fa\u6700\u512a\u89e3\u3002</li> <li>\u554f\u984c\u63cf\u8ff0\u4e2d\u6709\u660e\u986f\u7684\u6392\u5217\u7d44\u5408\u7684\u7279\u5fb5\uff0c\u9700\u8981\u8fd4\u56de\u5177\u9ad4\u7684\u591a\u500b\u65b9\u6848\u3002</li> </ul> <p>\u5982\u679c\u4e00\u500b\u554f\u984c\u6eff\u8db3\u6c7a\u7b56\u6a39\u6a21\u578b\uff0c\u4e26\u5177\u6709\u8f03\u70ba\u660e\u986f\u7684\u201c\u52a0\u5206\u9805\u201d\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u5047\u8a2d\u5b83\u662f\u4e00\u500b\u52d5\u614b\u898f\u5283\u554f\u984c\uff0c\u4e26\u5728\u6c42\u89e3\u904e\u7a0b\u4e2d\u9a57\u8b49\u5b83\u3002</p>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1432","title":"14.3.2 \u00a0 \u554f\u984c\u6c42\u89e3\u6b65\u9a5f","text":"<p>\u52d5\u614b\u898f\u5283\u7684\u89e3\u984c\u6d41\u7a0b\u6703\u56e0\u554f\u984c\u7684\u6027\u8cea\u548c\u96e3\u5ea6\u800c\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u901a\u5e38\u9075\u5faa\u4ee5\u4e0b\u6b65\u9a5f\uff1a\u63cf\u8ff0\u6c7a\u7b56\uff0c\u5b9a\u7fa9\u72c0\u614b\uff0c\u5efa\u7acb \\(dp\\) \u8868\uff0c\u63a8\u5c0e\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\uff0c\u78ba\u5b9a\u908a\u754c\u689d\u4ef6\u7b49\u3002</p> <p>\u70ba\u4e86\u66f4\u5f62\u8c61\u5730\u5c55\u793a\u89e3\u984c\u6b65\u9a5f\uff0c\u6211\u5011\u4f7f\u7528\u4e00\u500b\u7d93\u5178\u554f\u984c\u201c\u6700\u5c0f\u8def\u5f91\u548c\u201d\u4f86\u8209\u4f8b\u3002</p> <p>Question</p> <p>\u7d66\u5b9a\u4e00\u500b \\(n \\times m\\) \u7684\u4e8c\u7dad\u7db2\u683c <code>grid</code> \uff0c\u7db2\u683c\u4e2d\u7684\u6bcf\u500b\u55ae\u5143\u683c\u5305\u542b\u4e00\u500b\u975e\u8ca0\u6574\u6578\uff0c\u8868\u793a\u8a72\u55ae\u5143\u683c\u7684\u4ee3\u50f9\u3002\u6a5f\u5668\u4eba\u4ee5\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\u70ba\u8d77\u59cb\u9ede\uff0c\u6bcf\u6b21\u53ea\u80fd\u5411\u4e0b\u6216\u8005\u5411\u53f3\u79fb\u52d5\u4e00\u6b65\uff0c\u76f4\u81f3\u5230\u9054\u53f3\u4e0b\u89d2\u55ae\u5143\u683c\u3002\u8acb\u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230\u53f3\u4e0b\u89d2\u7684\u6700\u5c0f\u8def\u5f91\u548c\u3002</p> <p>\u5716 14-10 \u5c55\u793a\u4e86\u4e00\u500b\u4f8b\u5b50\uff0c\u7d66\u5b9a\u7db2\u683c\u7684\u6700\u5c0f\u8def\u5f91\u548c\u70ba \\(13\\) \u3002</p> <p></p> <p> \u5716 14-10 \u00a0 \u6700\u5c0f\u8def\u5f91\u548c\u793a\u4f8b\u8cc7\u6599 </p> <p>\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f2a\u7684\u6c7a\u7b56\uff0c\u5b9a\u7fa9\u72c0\u614b\uff0c\u5f9e\u800c\u5f97\u5230 \\(dp\\) \u8868</p> <p>\u672c\u984c\u7684\u6bcf\u4e00\u8f2a\u7684\u6c7a\u7b56\u5c31\u662f\u5f9e\u7576\u524d\u683c\u5b50\u5411\u4e0b\u6216\u5411\u53f3\u8d70\u4e00\u6b65\u3002\u8a2d\u7576\u524d\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u70ba \\([i, j]\\) \uff0c\u5247\u5411\u4e0b\u6216\u5411\u53f3\u8d70\u4e00\u6b65\u5f8c\uff0c\u7d22\u5f15\u8b8a\u70ba \\([i+1, j]\\) \u6216 \\([i, j+1]\\) \u3002\u56e0\u6b64\uff0c\u72c0\u614b\u61c9\u5305\u542b\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u5169\u500b\u8b8a\u6578\uff0c\u8a18\u70ba \\([i, j]\\) \u3002</p> <p>\u72c0\u614b \\([i, j]\\) \u5c0d\u61c9\u7684\u5b50\u554f\u984c\u70ba\uff1a\u5f9e\u8d77\u59cb\u9ede \\([0, 0]\\) \u8d70\u5230 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f91\u548c\uff0c\u89e3\u8a18\u70ba \\(dp[i, j]\\) \u3002</p> <p>\u81f3\u6b64\uff0c\u6211\u5011\u5c31\u5f97\u5230\u4e86\u5716 14-11 \u6240\u793a\u7684\u4e8c\u7dad \\(dp\\) \u77e9\u9663\uff0c\u5176\u5c3a\u5bf8\u8207\u8f38\u5165\u7db2\u683c \\(grid\\) \u76f8\u540c\u3002</p> <p></p> <p> \u5716 14-11 \u00a0 \u72c0\u614b\u5b9a\u7fa9\u8207 dp \u8868 </p> <p>Note</p> <p>\u52d5\u614b\u898f\u5283\u548c\u56de\u6eaf\u904e\u7a0b\u53ef\u4ee5\u63cf\u8ff0\u70ba\u4e00\u500b\u6c7a\u7b56\u5e8f\u5217\uff0c\u800c\u72c0\u614b\u7531\u6240\u6709\u6c7a\u7b56\u8b8a\u6578\u69cb\u6210\u3002\u5b83\u61c9\u7576\u5305\u542b\u63cf\u8ff0\u89e3\u984c\u9032\u5ea6\u7684\u6240\u6709\u8b8a\u6578\uff0c\u5176\u5305\u542b\u4e86\u8db3\u5920\u7684\u8cc7\u8a0a\uff0c\u80fd\u5920\u7528\u4f86\u63a8\u5c0e\u51fa\u4e0b\u4e00\u500b\u72c0\u614b\u3002</p> <p>\u6bcf\u500b\u72c0\u614b\u90fd\u5c0d\u61c9\u4e00\u500b\u5b50\u554f\u984c\uff0c\u6211\u5011\u6703\u5b9a\u7fa9\u4e00\u500b \\(dp\\) \u8868\u4f86\u5132\u5b58\u6240\u6709\u5b50\u554f\u984c\u7684\u89e3\uff0c\u72c0\u614b\u7684\u6bcf\u500b\u7368\u7acb\u8b8a\u6578\u90fd\u662f \\(dp\\) \u8868\u7684\u4e00\u500b\u7dad\u5ea6\u3002\u5f9e\u672c\u8cea\u4e0a\u770b\uff0c\\(dp\\) \u8868\u662f\u72c0\u614b\u548c\u5b50\u554f\u984c\u7684\u89e3\u4e4b\u9593\u7684\u5c0d\u6620\u3002</p> <p>\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u512a\u5b50\u7d50\u69cb\uff0c\u9032\u800c\u63a8\u5c0e\u51fa\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b</p> <p>\u5c0d\u65bc\u72c0\u614b \\([i, j]\\) \uff0c\u5b83\u53ea\u80fd\u5f9e\u4e0a\u908a\u683c\u5b50 \\([i-1, j]\\) \u548c\u5de6\u908a\u683c\u5b50 \\([i, j-1]\\) \u8f49\u79fb\u800c\u4f86\u3002\u56e0\u6b64\u6700\u512a\u5b50\u7d50\u69cb\u70ba\uff1a\u5230\u9054 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f91\u548c\u7531 \\([i, j-1]\\) \u7684\u6700\u5c0f\u8def\u5f91\u548c\u8207 \\([i-1, j]\\) \u7684\u6700\u5c0f\u8def\u5f91\u548c\u4e2d\u8f03\u5c0f\u7684\u90a3\u4e00\u500b\u6c7a\u5b9a\u3002</p> <p>\u6839\u64da\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u63a8\u51fa\u5716 14-12 \u6240\u793a\u7684\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\uff1a</p> \\[ dp[i, j] = \\min(dp[i-1, j], dp[i, j-1]) + grid[i, j] \\] <p></p> <p> \u5716 14-12 \u00a0 \u6700\u512a\u5b50\u7d50\u69cb\u8207\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b </p> <p>Note</p> <p>\u6839\u64da\u5b9a\u7fa9\u597d\u7684 \\(dp\\) \u8868\uff0c\u601d\u8003\u539f\u554f\u984c\u548c\u5b50\u554f\u984c\u7684\u95dc\u4fc2\uff0c\u627e\u51fa\u900f\u904e\u5b50\u554f\u984c\u7684\u6700\u512a\u89e3\u4f86\u69cb\u9020\u539f\u554f\u984c\u7684\u6700\u512a\u89e3\u7684\u65b9\u6cd5\uff0c\u5373\u6700\u512a\u5b50\u7d50\u69cb\u3002</p> <p>\u4e00\u65e6\u6211\u5011\u627e\u5230\u4e86\u6700\u512a\u5b50\u7d50\u69cb\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u4f86\u69cb\u5efa\u51fa\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\u3002</p> <p>\u7b2c\u4e09\u6b65\uff1a\u78ba\u5b9a\u908a\u754c\u689d\u4ef6\u548c\u72c0\u614b\u8f49\u79fb\u9806\u5e8f</p> <p>\u5728\u672c\u984c\u4e2d\uff0c\u8655\u5728\u9996\u884c\u7684\u72c0\u614b\u53ea\u80fd\u5f9e\u5176\u5de6\u908a\u7684\u72c0\u614b\u5f97\u4f86\uff0c\u8655\u5728\u9996\u5217\u7684\u72c0\u614b\u53ea\u80fd\u5f9e\u5176\u4e0a\u908a\u7684\u72c0\u614b\u5f97\u4f86\uff0c\u56e0\u6b64\u9996\u884c \\(i = 0\\) \u548c\u9996\u5217 \\(j = 0\\) \u662f\u908a\u754c\u689d\u4ef6\u3002</p> <p>\u5982\u5716 14-13 \u6240\u793a\uff0c\u7531\u65bc\u6bcf\u500b\u683c\u5b50\u662f\u7531\u5176\u5de6\u65b9\u683c\u5b50\u548c\u4e0a\u65b9\u683c\u5b50\u8f49\u79fb\u800c\u4f86\uff0c\u56e0\u6b64\u6211\u5011\u4f7f\u7528\u8ff4\u5708\u4f86\u8d70\u8a2a\u77e9\u9663\uff0c\u5916\u8ff4\u5708\u8d70\u8a2a\u5404\u884c\uff0c\u5167\u8ff4\u5708\u8d70\u8a2a\u5404\u5217\u3002</p> <p></p> <p> \u5716 14-13 \u00a0 \u908a\u754c\u689d\u4ef6\u8207\u72c0\u614b\u8f49\u79fb\u9806\u5e8f </p> <p>Note</p> <p>\u908a\u754c\u689d\u4ef6\u5728\u52d5\u614b\u898f\u5283\u4e2d\u7528\u65bc\u521d\u59cb\u5316 \\(dp\\) \u8868\uff0c\u5728\u641c\u5c0b\u4e2d\u7528\u65bc\u526a\u679d\u3002</p> <p>\u72c0\u614b\u8f49\u79fb\u9806\u5e8f\u7684\u6838\u5fc3\u662f\u8981\u4fdd\u8b49\u5728\u8a08\u7b97\u7576\u524d\u554f\u984c\u7684\u89e3\u6642\uff0c\u6240\u6709\u5b83\u4f9d\u8cf4\u7684\u66f4\u5c0f\u5b50\u554f\u984c\u7684\u89e3\u90fd\u5df2\u7d93\u88ab\u6b63\u78ba\u5730\u8a08\u7b97\u51fa\u4f86\u3002</p> <p>\u6839\u64da\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u5011\u5df2\u7d93\u53ef\u4ee5\u76f4\u63a5\u5beb\u51fa\u52d5\u614b\u898f\u5283\u7a0b\u5f0f\u78bc\u3002\u7136\u800c\u5b50\u554f\u984c\u5206\u89e3\u662f\u4e00\u7a2e\u5f9e\u9802\u81f3\u5e95\u7684\u601d\u60f3\uff0c\u56e0\u6b64\u6309\u7167\u201c\u66b4\u529b\u641c\u5c0b \\(\\rightarrow\\) \u8a18\u61b6\u5316\u641c\u5c0b \\(\\rightarrow\\) \u52d5\u614b\u898f\u5283\u201d\u7684\u9806\u5e8f\u5be6\u73fe\u66f4\u52a0\u7b26\u5408\u601d\u7dad\u7fd2\u6163\u3002</p>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1","title":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u5c0b","text":"<p>\u5f9e\u72c0\u614b \\([i, j]\\) \u958b\u59cb\u641c\u5c0b\uff0c\u4e0d\u65b7\u5206\u89e3\u70ba\u66f4\u5c0f\u7684\u72c0\u614b \\([i-1, j]\\) \u548c \\([i, j-1]\\) \uff0c\u905e\u8ff4\u51fd\u5f0f\u5305\u62ec\u4ee5\u4e0b\u8981\u7d20\u3002</p> <ul> <li>\u905e\u8ff4\u53c3\u6578\uff1a\u72c0\u614b \\([i, j]\\) \u3002</li> <li>\u8fd4\u56de\u503c\uff1a\u5f9e \\([0, 0]\\) \u5230 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f91\u548c \\(dp[i, j]\\) \u3002</li> <li>\u7d42\u6b62\u689d\u4ef6\uff1a\u7576 \\(i = 0\\) \u4e14 \\(j = 0\\) \u6642\uff0c\u8fd4\u56de\u4ee3\u50f9 \\(grid[0, 0]\\) \u3002</li> <li>\u526a\u679d\uff1a\u7576 \\(i &lt; 0\\) \u6642\u6216 \\(j &lt; 0\\) \u6642\u7d22\u5f15\u8d8a\u754c\uff0c\u6b64\u6642\u8fd4\u56de\u4ee3\u50f9 \\(+\\infty\\) \uff0c\u4ee3\u8868\u4e0d\u53ef\u884c\u3002</li> </ul> <p>\u5be6\u73fe\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig min_path_sum.py<pre><code>def min_path_sum_dfs(grid: list[list[int]], i: int, j: int) -&gt; int:\n \"\"\"\u6700\u5c0f\u8def\u5f91\u548c\uff1a\u66b4\u529b\u641c\u5c0b\"\"\"\n # \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if i == 0 and j == 0:\n return grid[0][0]\n # \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if i &lt; 0 or j &lt; 0:\n return inf\n # \u8a08\u7b97\u5f9e\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n up = min_path_sum_dfs(grid, i - 1, j)\n left = min_path_sum_dfs(grid, i, j - 1)\n # \u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n return min(left, up) + grid[i][j]\n</code></pre> min_path_sum.cpp<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u66b4\u529b\u641c\u5c0b */\nint minPathSumDFS(vector&lt;vector&lt;int&gt;&gt; &amp;grid, int i, int j) {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i == 0 &amp;&amp; j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n return INT_MAX;\n }\n // \u8a08\u7b97\u5f9e\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n int up = minPathSumDFS(grid, i - 1, j);\n int left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n}\n</code></pre> min_path_sum.java<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u66b4\u529b\u641c\u5c0b */\nint minPathSumDFS(int[][] grid, int i, int j) {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i == 0 &amp;&amp; j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n return Integer.MAX_VALUE;\n }\n // \u8a08\u7b97\u5f9e\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n int up = minPathSumDFS(grid, i - 1, j);\n int left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n return Math.min(left, up) + grid[i][j];\n}\n</code></pre> min_path_sum.cs<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u66b4\u529b\u641c\u5c0b */\nint MinPathSumDFS(int[][] grid, int i, int j) {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i == 0 &amp;&amp; j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n return int.MaxValue;\n }\n // \u8a08\u7b97\u5f9e\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n int up = MinPathSumDFS(grid, i - 1, j);\n int left = MinPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n return Math.Min(left, up) + grid[i][j];\n}\n</code></pre> min_path_sum.go<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u66b4\u529b\u641c\u5c0b */\nfunc minPathSumDFS(grid [][]int, i, j int) int {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if i == 0 &amp;&amp; j == 0 {\n return grid[0][0]\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if i &lt; 0 || j &lt; 0 {\n return math.MaxInt\n }\n // \u8a08\u7b97\u5f9e\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n up := minPathSumDFS(grid, i-1, j)\n left := minPathSumDFS(grid, i, j-1)\n // \u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n return int(math.Min(float64(left), float64(up))) + grid[i][j]\n}\n</code></pre> min_path_sum.swift<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u66b4\u529b\u641c\u5c0b */\nfunc minPathSumDFS(grid: [[Int]], i: Int, j: Int) -&gt; Int {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if i == 0, j == 0 {\n return grid[0][0]\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if i &lt; 0 || j &lt; 0 {\n return .max\n }\n // \u8a08\u7b97\u5f9e\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n let up = minPathSumDFS(grid: grid, i: i - 1, j: j)\n let left = minPathSumDFS(grid: grid, i: i, j: j - 1)\n // \u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n return min(left, up) + grid[i][j]\n}\n</code></pre> min_path_sum.js<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u66b4\u529b\u641c\u5c0b */\nfunction minPathSumDFS(grid, i, j) {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i === 0 &amp;&amp; j === 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n return Infinity;\n }\n // \u8a08\u7b97\u5f9e\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n const up = minPathSumDFS(grid, i - 1, j);\n const left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n return Math.min(left, up) + grid[i][j];\n}\n</code></pre> min_path_sum.ts<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u66b4\u529b\u641c\u5c0b */\nfunction minPathSumDFS(\n grid: Array&lt;Array&lt;number&gt;&gt;,\n i: number,\n j: number\n): number {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i === 0 &amp;&amp; j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n return Infinity;\n }\n // \u8a08\u7b97\u5f9e\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n const up = minPathSumDFS(grid, i - 1, j);\n const left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n return Math.min(left, up) + grid[i][j];\n}\n</code></pre> min_path_sum.dart<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u66b4\u529b\u641c\u5c0b */\nint minPathSumDFS(List&lt;List&lt;int&gt;&gt; grid, int i, int j) {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i == 0 &amp;&amp; j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n // \u5728 Dart \u4e2d\uff0cint \u578b\u5225\u662f\u56fa\u5b9a\u7bc4\u570d\u7684\u6574\u6578\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u7121\u7aae\u5927\u201d\u7684\u503c\n return BigInt.from(2).pow(31).toInt();\n }\n // \u8a08\u7b97\u5f9e\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n int up = minPathSumDFS(grid, i - 1, j);\n int left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n return min(left, up) + grid[i][j];\n}\n</code></pre> min_path_sum.rs<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u66b4\u529b\u641c\u5c0b */\nfn min_path_sum_dfs(grid: &amp;Vec&lt;Vec&lt;i32&gt;&gt;, i: i32, j: i32) -&gt; i32 {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if i == 0 &amp;&amp; j == 0 {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if i &lt; 0 || j &lt; 0 {\n return i32::MAX;\n }\n // \u8a08\u7b97\u5f9e\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n let up = min_path_sum_dfs(grid, i - 1, j);\n let left = min_path_sum_dfs(grid, i, j - 1);\n // \u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n std::cmp::min(left, up) + grid[i as usize][j as usize]\n}\n</code></pre> min_path_sum.c<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u66b4\u529b\u641c\u5c0b */\nint minPathSumDFS(int grid[MAX_SIZE][MAX_SIZE], int i, int j) {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i == 0 &amp;&amp; j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n return INT_MAX;\n }\n // \u8a08\u7b97\u5f9e\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n int up = minPathSumDFS(grid, i - 1, j);\n int left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n return myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n}\n</code></pre> min_path_sum.kt<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u66b4\u529b\u641c\u5c0b */\nfun minPathSumDFS(grid: Array&lt;IntArray&gt;, i: Int, j: Int): Int {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i == 0 &amp;&amp; j == 0) {\n return grid[0][0]\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n return Int.MAX_VALUE\n }\n // \u8a08\u7b97\u5f9e\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n val up = minPathSumDFS(grid, i - 1, j)\n val left = minPathSumDFS(grid, i, j - 1)\n // \u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n return min(left, up) + grid[i][j]\n}\n</code></pre> min_path_sum.rb<pre><code>[class]{}-[func]{min_path_sum_dfs}\n</code></pre> min_path_sum.zig<pre><code>// \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u66b4\u529b\u641c\u5c0b\nfn minPathSumDFS(grid: anytype, i: i32, j: i32) i32 {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i == 0 and j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 or j &lt; 0) {\n return std.math.maxInt(i32);\n }\n // \u8a08\u7b97\u5f9e\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n var up = minPathSumDFS(grid, i - 1, j);\n var left = minPathSumDFS(grid, i, j - 1);\n // \u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n return @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 14-14 \u7d66\u51fa\u4e86\u4ee5 \\(dp[2, 1]\\) \u70ba\u6839\u7bc0\u9ede\u7684\u905e\u8ff4\u6a39\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e9b\u91cd\u758a\u5b50\u554f\u984c\uff0c\u5176\u6578\u91cf\u6703\u96a8\u8457\u7db2\u683c <code>grid</code> \u7684\u5c3a\u5bf8\u8b8a\u5927\u800c\u6025\u5287\u589e\u591a\u3002</p> <p>\u5f9e\u672c\u8cea\u4e0a\u770b\uff0c\u9020\u6210\u91cd\u758a\u5b50\u554f\u984c\u7684\u539f\u56e0\u70ba\uff1a\u5b58\u5728\u591a\u689d\u8def\u5f91\u53ef\u4ee5\u5f9e\u5de6\u4e0a\u89d2\u5230\u9054\u67d0\u4e00\u55ae\u5143\u683c\u3002</p> <p></p> <p> \u5716 14-14 \u00a0 \u66b4\u529b\u641c\u5c0b\u905e\u8ff4\u6a39 </p> <p>\u6bcf\u500b\u72c0\u614b\u90fd\u6709\u5411\u4e0b\u548c\u5411\u53f3\u5169\u7a2e\u9078\u64c7\uff0c\u5f9e\u5de6\u4e0a\u89d2\u8d70\u5230\u53f3\u4e0b\u89d2\u7e3d\u5171\u9700\u8981 \\(m + n - 2\\) \u6b65\uff0c\u6240\u4ee5\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(2^{m + n})\\) \u3002\u8acb\u6ce8\u610f\uff0c\u9019\u7a2e\u8a08\u7b97\u65b9\u5f0f\u672a\u8003\u616e\u81e8\u8fd1\u7db2\u683c\u908a\u754c\u7684\u60c5\u6cc1\uff0c\u7576\u5230\u9054\u7db2\u8def\u908a\u754c\u6642\u53ea\u5269\u4e0b\u4e00\u7a2e\u9078\u64c7\uff0c\u56e0\u6b64\u5be6\u969b\u7684\u8def\u5f91\u6578\u91cf\u6703\u5c11\u4e00\u4e9b\u3002</p>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#2","title":"2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b","text":"<p>\u6211\u5011\u5f15\u5165\u4e00\u500b\u548c\u7db2\u683c <code>grid</code> \u76f8\u540c\u5c3a\u5bf8\u7684\u8a18\u61b6\u4e32\u5217 <code>mem</code> \uff0c\u7528\u65bc\u8a18\u9304\u5404\u500b\u5b50\u554f\u984c\u7684\u89e3\uff0c\u4e26\u5c07\u91cd\u758a\u5b50\u554f\u984c\u9032\u884c\u526a\u679d\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig min_path_sum.py<pre><code>def min_path_sum_dfs_mem(\n grid: list[list[int]], mem: list[list[int]], i: int, j: int\n) -&gt; int:\n \"\"\"\u6700\u5c0f\u8def\u5f91\u548c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b\"\"\"\n # \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if i == 0 and j == 0:\n return grid[0][0]\n # \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if i &lt; 0 or j &lt; 0:\n return inf\n # \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if mem[i][j] != -1:\n return mem[i][j]\n # \u5de6\u908a\u548c\u4e0a\u908a\u55ae\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n # \u8a18\u9304\u4e26\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n mem[i][j] = min(left, up) + grid[i][j]\n return mem[i][j]\n</code></pre> min_path_sum.cpp<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint minPathSumDFSMem(vector&lt;vector&lt;int&gt;&gt; &amp;grid, vector&lt;vector&lt;int&gt;&gt; &amp;mem, int i, int j) {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i == 0 &amp;&amp; j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n return INT_MAX;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u908a\u548c\u4e0a\u908a\u55ae\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n int up = minPathSumDFSMem(grid, mem, i - 1, j);\n int left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n return mem[i][j];\n}\n</code></pre> min_path_sum.java<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i == 0 &amp;&amp; j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n return Integer.MAX_VALUE;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u908a\u548c\u4e0a\u908a\u55ae\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n int up = minPathSumDFSMem(grid, mem, i - 1, j);\n int left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n mem[i][j] = Math.min(left, up) + grid[i][j];\n return mem[i][j];\n}\n</code></pre> min_path_sum.cs<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i == 0 &amp;&amp; j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n return int.MaxValue;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u908a\u548c\u4e0a\u908a\u55ae\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n int up = MinPathSumDFSMem(grid, mem, i - 1, j);\n int left = MinPathSumDFSMem(grid, mem, i, j - 1);\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n mem[i][j] = Math.Min(left, up) + grid[i][j];\n return mem[i][j];\n}\n</code></pre> min_path_sum.go<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfunc minPathSumDFSMem(grid, mem [][]int, i, j int) int {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if i == 0 &amp;&amp; j == 0 {\n return grid[0][0]\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if i &lt; 0 || j &lt; 0 {\n return math.MaxInt\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if mem[i][j] != -1 {\n return mem[i][j]\n }\n // \u5de6\u908a\u548c\u4e0a\u908a\u55ae\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n up := minPathSumDFSMem(grid, mem, i-1, j)\n left := minPathSumDFSMem(grid, mem, i, j-1)\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n mem[i][j] = int(math.Min(float64(left), float64(up))) + grid[i][j]\n return mem[i][j]\n}\n</code></pre> min_path_sum.swift<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfunc minPathSumDFSMem(grid: [[Int]], mem: inout [[Int]], i: Int, j: Int) -&gt; Int {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if i == 0, j == 0 {\n return grid[0][0]\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if i &lt; 0 || j &lt; 0 {\n return .max\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if mem[i][j] != -1 {\n return mem[i][j]\n }\n // \u5de6\u908a\u548c\u4e0a\u908a\u55ae\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n let up = minPathSumDFSMem(grid: grid, mem: &amp;mem, i: i - 1, j: j)\n let left = minPathSumDFSMem(grid: grid, mem: &amp;mem, i: i, j: j - 1)\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n mem[i][j] = min(left, up) + grid[i][j]\n return mem[i][j]\n}\n</code></pre> min_path_sum.js<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfunction minPathSumDFSMem(grid, mem, i, j) {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i === 0 &amp;&amp; j === 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n return Infinity;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] !== -1) {\n return mem[i][j];\n }\n // \u5de6\u908a\u548c\u4e0a\u908a\u55ae\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n const up = minPathSumDFSMem(grid, mem, i - 1, j);\n const left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n mem[i][j] = Math.min(left, up) + grid[i][j];\n return mem[i][j];\n}\n</code></pre> min_path_sum.ts<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfunction minPathSumDFSMem(\n grid: Array&lt;Array&lt;number&gt;&gt;,\n mem: Array&lt;Array&lt;number&gt;&gt;,\n i: number,\n j: number\n): number {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i === 0 &amp;&amp; j === 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n return Infinity;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u908a\u548c\u4e0a\u908a\u55ae\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n const up = minPathSumDFSMem(grid, mem, i - 1, j);\n const left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n mem[i][j] = Math.min(left, up) + grid[i][j];\n return mem[i][j];\n}\n</code></pre> min_path_sum.dart<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint minPathSumDFSMem(List&lt;List&lt;int&gt;&gt; grid, List&lt;List&lt;int&gt;&gt; mem, int i, int j) {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i == 0 &amp;&amp; j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n // \u5728 Dart \u4e2d\uff0cint \u578b\u5225\u662f\u56fa\u5b9a\u7bc4\u570d\u7684\u6574\u6578\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u7121\u7aae\u5927\u201d\u7684\u503c\n return BigInt.from(2).pow(31).toInt();\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u908a\u548c\u4e0a\u908a\u55ae\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n int up = minPathSumDFSMem(grid, mem, i - 1, j);\n int left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n mem[i][j] = min(left, up) + grid[i][j];\n return mem[i][j];\n}\n</code></pre> min_path_sum.rs<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfn min_path_sum_dfs_mem(grid: &amp;Vec&lt;Vec&lt;i32&gt;&gt;, mem: &amp;mut Vec&lt;Vec&lt;i32&gt;&gt;, i: i32, j: i32) -&gt; i32 {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if i == 0 &amp;&amp; j == 0 {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if i &lt; 0 || j &lt; 0 {\n return i32::MAX;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if mem[i as usize][j as usize] != -1 {\n return mem[i as usize][j as usize];\n }\n // \u5de6\u908a\u548c\u4e0a\u908a\u55ae\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n let up = min_path_sum_dfs_mem(grid, mem, i - 1, j);\n let left = min_path_sum_dfs_mem(grid, mem, i, j - 1);\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n mem[i as usize][j as usize] = std::cmp::min(left, up) + grid[i as usize][j as usize];\n mem[i as usize][j as usize]\n}\n</code></pre> min_path_sum.c<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint minPathSumDFSMem(int grid[MAX_SIZE][MAX_SIZE], int mem[MAX_SIZE][MAX_SIZE], int i, int j) {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i == 0 &amp;&amp; j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n return INT_MAX;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j];\n }\n // \u5de6\u908a\u548c\u4e0a\u908a\u55ae\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n int up = minPathSumDFSMem(grid, mem, i - 1, j);\n int left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n mem[i][j] = myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n return mem[i][j];\n}\n</code></pre> min_path_sum.kt<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfun minPathSumDFSMem(\n grid: Array&lt;IntArray&gt;,\n mem: Array&lt;IntArray&gt;,\n i: Int,\n j: Int\n): Int {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i == 0 &amp;&amp; j == 0) {\n return grid[0][0]\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 || j &lt; 0) {\n return Int.MAX_VALUE\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][j] != -1) {\n return mem[i][j]\n }\n // \u5de6\u908a\u548c\u4e0a\u908a\u55ae\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n val up = minPathSumDFSMem(grid, mem, i - 1, j)\n val left = minPathSumDFSMem(grid, mem, i, j - 1)\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n mem[i][j] = min(left, up) + grid[i][j]\n return mem[i][j]\n}\n</code></pre> min_path_sum.rb<pre><code>[class]{}-[func]{min_path_sum_dfs_mem}\n</code></pre> min_path_sum.zig<pre><code>// \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b\nfn minPathSumDFSMem(grid: anytype, mem: anytype, i: i32, j: i32) i32 {\n // \u82e5\u70ba\u5de6\u4e0a\u89d2\u55ae\u5143\u683c\uff0c\u5247\u7d42\u6b62\u641c\u5c0b\n if (i == 0 and j == 0) {\n return grid[0][0];\n }\n // \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de +\u221e \u4ee3\u50f9\n if (i &lt; 0 or j &lt; 0) {\n return std.math.maxInt(i32);\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] != -1) {\n return mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n }\n // \u8a08\u7b97\u5f9e\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n var up = minPathSumDFSMem(grid, mem, i - 1, j);\n var left = minPathSumDFSMem(grid, mem, i, j - 1);\n // \u8fd4\u56de\u5f9e\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f91\u4ee3\u50f9\n mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] = @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n return mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5982\u5716 14-15 \u6240\u793a\uff0c\u5728\u5f15\u5165\u8a18\u61b6\u5316\u5f8c\uff0c\u6240\u6709\u5b50\u554f\u984c\u7684\u89e3\u53ea\u9700\u8a08\u7b97\u4e00\u6b21\uff0c\u56e0\u6b64\u6642\u9593\u8907\u96dc\u5ea6\u53d6\u6c7a\u65bc\u72c0\u614b\u7e3d\u6578\uff0c\u5373\u7db2\u683c\u5c3a\u5bf8 \\(O(nm)\\) \u3002</p> <p></p> <p> \u5716 14-15 \u00a0 \u8a18\u61b6\u5316\u641c\u5c0b\u905e\u8ff4\u6a39 </p>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52d5\u614b\u898f\u5283","text":"<p>\u57fa\u65bc\u8fed\u4ee3\u5be6\u73fe\u52d5\u614b\u898f\u5283\u89e3\u6cd5\uff0c\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig min_path_sum.py<pre><code>def min_path_sum_dp(grid: list[list[int]]) -&gt; int:\n \"\"\"\u6700\u5c0f\u8def\u5f91\u548c\uff1a\u52d5\u614b\u898f\u5283\"\"\"\n n, m = len(grid), len(grid[0])\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * m for _ in range(n)]\n dp[0][0] = grid[0][0]\n # \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for j in range(1, m):\n dp[0][j] = dp[0][j - 1] + grid[0][j]\n # \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n for i in range(1, n):\n dp[i][0] = dp[i - 1][0] + grid[i][0]\n # \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for i in range(1, n):\n for j in range(1, m):\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n return dp[n - 1][m - 1]\n</code></pre> min_path_sum.cpp<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u52d5\u614b\u898f\u5283 */\nint minPathSumDP(vector&lt;vector&lt;int&gt;&gt; &amp;grid) {\n int n = grid.size(), m = grid[0].size();\n // \u521d\u59cb\u5316 dp \u8868\n vector&lt;vector&lt;int&gt;&gt; dp(n, vector&lt;int&gt;(m));\n dp[0][0] = grid[0][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (int j = 1; j &lt; m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n for (int i = 1; i &lt; n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt; n; i++) {\n for (int j = 1; j &lt; m; j++) {\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n</code></pre> min_path_sum.java<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u52d5\u614b\u898f\u5283 */\nint minPathSumDP(int[][] grid) {\n int n = grid.length, m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n][m];\n dp[0][0] = grid[0][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (int j = 1; j &lt; m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n for (int i = 1; i &lt; n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt; n; i++) {\n for (int j = 1; j &lt; m; j++) {\n dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n</code></pre> min_path_sum.cs<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u52d5\u614b\u898f\u5283 */\nint MinPathSumDP(int[][] grid) {\n int n = grid.Length, m = grid[0].Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n, m];\n dp[0, 0] = grid[0][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (int j = 1; j &lt; m; j++) {\n dp[0, j] = dp[0, j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n for (int i = 1; i &lt; n; i++) {\n dp[i, 0] = dp[i - 1, 0] + grid[i][0];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt; n; i++) {\n for (int j = 1; j &lt; m; j++) {\n dp[i, j] = Math.Min(dp[i, j - 1], dp[i - 1, j]) + grid[i][j];\n }\n }\n return dp[n - 1, m - 1];\n}\n</code></pre> min_path_sum.go<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u52d5\u614b\u898f\u5283 */\nfunc minPathSumDP(grid [][]int) int {\n n, m := len(grid), len(grid[0])\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n)\n for i := 0; i &lt; n; i++ {\n dp[i] = make([]int, m)\n }\n dp[0][0] = grid[0][0]\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for j := 1; j &lt; m; j++ {\n dp[0][j] = dp[0][j-1] + grid[0][j]\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n for i := 1; i &lt; n; i++ {\n dp[i][0] = dp[i-1][0] + grid[i][0]\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for i := 1; i &lt; n; i++ {\n for j := 1; j &lt; m; j++ {\n dp[i][j] = int(math.Min(float64(dp[i][j-1]), float64(dp[i-1][j]))) + grid[i][j]\n }\n }\n return dp[n-1][m-1]\n}\n</code></pre> min_path_sum.swift<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u52d5\u614b\u898f\u5283 */\nfunc minPathSumDP(grid: [[Int]]) -&gt; Int {\n let n = grid.count\n let m = grid[0].count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: m), count: n)\n dp[0][0] = grid[0][0]\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for j in 1 ..&lt; m {\n dp[0][j] = dp[0][j - 1] + grid[0][j]\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n for i in 1 ..&lt; n {\n dp[i][0] = dp[i - 1][0] + grid[i][0]\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for i in 1 ..&lt; n {\n for j in 1 ..&lt; m {\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n }\n }\n return dp[n - 1][m - 1]\n}\n</code></pre> min_path_sum.js<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u52d5\u614b\u898f\u5283 */\nfunction minPathSumDP(grid) {\n const n = grid.length,\n m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n }, () =&gt;\n Array.from({ length: m }, () =&gt; 0)\n );\n dp[0][0] = grid[0][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (let j = 1; j &lt; m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n for (let i = 1; i &lt; n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (let i = 1; i &lt; n; i++) {\n for (let j = 1; j &lt; m; j++) {\n dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n</code></pre> min_path_sum.ts<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u52d5\u614b\u898f\u5283 */\nfunction minPathSumDP(grid: Array&lt;Array&lt;number&gt;&gt;): number {\n const n = grid.length,\n m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n }, () =&gt;\n Array.from({ length: m }, () =&gt; 0)\n );\n dp[0][0] = grid[0][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (let j = 1; j &lt; m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n for (let i = 1; i &lt; n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (let i = 1; i &lt; n; i++) {\n for (let j: number = 1; j &lt; m; j++) {\n dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n</code></pre> min_path_sum.dart<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u52d5\u614b\u898f\u5283 */\nint minPathSumDP(List&lt;List&lt;int&gt;&gt; grid) {\n int n = grid.length, m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n List&lt;List&lt;int&gt;&gt; dp = List.generate(n, (i) =&gt; List.filled(m, 0));\n dp[0][0] = grid[0][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (int j = 1; j &lt; m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n for (int i = 1; i &lt; n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt; n; i++) {\n for (int j = 1; j &lt; m; j++) {\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n</code></pre> min_path_sum.rs<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u52d5\u614b\u898f\u5283 */\nfn min_path_sum_dp(grid: &amp;Vec&lt;Vec&lt;i32&gt;&gt;) -&gt; i32 {\n let (n, m) = (grid.len(), grid[0].len());\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; m]; n];\n dp[0][0] = grid[0][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for j in 1..m {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n for i in 1..n {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for i in 1..n {\n for j in 1..m {\n dp[i][j] = std::cmp::min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n dp[n - 1][m - 1]\n}\n</code></pre> min_path_sum.c<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u52d5\u614b\u898f\u5283 */\nint minPathSumDP(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc(n * sizeof(int *));\n for (int i = 0; i &lt; n; i++) {\n dp[i] = calloc(m, sizeof(int));\n }\n dp[0][0] = grid[0][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (int j = 1; j &lt; m; j++) {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n for (int i = 1; i &lt; n; i++) {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt; n; i++) {\n for (int j = 1; j &lt; m; j++) {\n dp[i][j] = myMin(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n int res = dp[n - 1][m - 1];\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n for (int i = 0; i &lt; n; i++) {\n free(dp[i]);\n }\n return res;\n}\n</code></pre> min_path_sum.kt<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u52d5\u614b\u898f\u5283 */\nfun minPathSumDP(grid: Array&lt;IntArray&gt;): Int {\n val n = grid.size\n val m = grid[0].size\n // \u521d\u59cb\u5316 dp \u8868\n val dp = Array(n) { IntArray(m) }\n dp[0][0] = grid[0][0]\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (j in 1..&lt;m) {\n dp[0][j] = dp[0][j - 1] + grid[0][j]\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n for (i in 1..&lt;n) {\n dp[i][0] = dp[i - 1][0] + grid[i][0]\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (i in 1..&lt;n) {\n for (j in 1..&lt;m) {\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n }\n }\n return dp[n - 1][m - 1]\n}\n</code></pre> min_path_sum.rb<pre><code>[class]{}-[func]{min_path_sum_dp}\n</code></pre> min_path_sum.zig<pre><code>// \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u52d5\u614b\u898f\u5283\nfn minPathSumDP(comptime grid: anytype) i32 {\n comptime var n = grid.len;\n comptime var m = grid[0].len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][m]i32{[_]i32{0} ** m} ** n;\n dp[0][0] = grid[0][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (1..m) |j| {\n dp[0][j] = dp[0][j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n for (1..n) |i| {\n dp[i][0] = dp[i - 1][0] + grid[i][0];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (1..n) |i| {\n for (1..m) |j| {\n dp[i][j] = @min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n }\n }\n return dp[n - 1][m - 1];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 14-16 \u5c55\u793a\u4e86\u6700\u5c0f\u8def\u5f91\u548c\u7684\u72c0\u614b\u8f49\u79fb\u904e\u7a0b\uff0c\u5176\u8d70\u8a2a\u4e86\u6574\u500b\u7db2\u683c\uff0c\u56e0\u6b64\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(nm)\\) \u3002</p> <p>\u9663\u5217 <code>dp</code> \u5927\u5c0f\u70ba \\(n \\times m\\) \uff0c\u56e0\u6b64\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(nm)\\) \u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt;&lt;10&gt;&lt;11&gt;&lt;12&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 14-16 \u00a0 \u6700\u5c0f\u8def\u5f91\u548c\u7684\u52d5\u614b\u898f\u5283\u904e\u7a0b </p>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#4","title":"4. \u00a0 \u7a7a\u9593\u6700\u4f73\u5316","text":"<p>\u7531\u65bc\u6bcf\u500b\u683c\u5b50\u53ea\u8207\u5176\u5de6\u908a\u548c\u4e0a\u908a\u7684\u683c\u5b50\u6709\u95dc\uff0c\u56e0\u6b64\u6211\u5011\u53ef\u4ee5\u53ea\u7528\u4e00\u500b\u55ae\u884c\u9663\u5217\u4f86\u5be6\u73fe \\(dp\\) \u8868\u3002</p> <p>\u8acb\u6ce8\u610f\uff0c\u56e0\u70ba\u9663\u5217 <code>dp</code> \u53ea\u80fd\u8868\u793a\u4e00\u884c\u7684\u72c0\u614b\uff0c\u6240\u4ee5\u6211\u5011\u7121\u6cd5\u63d0\u524d\u521d\u59cb\u5316\u9996\u5217\u72c0\u614b\uff0c\u800c\u662f\u5728\u8d70\u8a2a\u6bcf\u884c\u6642\u66f4\u65b0\u5b83\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig min_path_sum.py<pre><code>def min_path_sum_dp_comp(grid: list[list[int]]) -&gt; int:\n \"\"\"\u6700\u5c0f\u8def\u5f91\u548c\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\"\"\"\n n, m = len(grid), len(grid[0])\n # \u521d\u59cb\u5316 dp \u8868\n dp = [0] * m\n # \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0]\n for j in range(1, m):\n dp[j] = dp[j - 1] + grid[0][j]\n # \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for i in range(1, n):\n # \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0]\n # \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for j in range(1, m):\n dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n return dp[m - 1]\n</code></pre> min_path_sum.cpp<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint minPathSumDPComp(vector&lt;vector&lt;int&gt;&gt; &amp;grid) {\n int n = grid.size(), m = grid[0].size();\n // \u521d\u59cb\u5316 dp \u8868\n vector&lt;int&gt; dp(m);\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (int j = 1; j &lt; m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (int i = 1; i &lt; n; i++) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (int j = 1; j &lt; m; j++) {\n dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n</code></pre> min_path_sum.java<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint minPathSumDPComp(int[][] grid) {\n int n = grid.length, m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[m];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (int j = 1; j &lt; m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (int i = 1; i &lt; n; i++) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (int j = 1; j &lt; m; j++) {\n dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n</code></pre> min_path_sum.cs<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint MinPathSumDPComp(int[][] grid) {\n int n = grid.Length, m = grid[0].Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[m];\n dp[0] = grid[0][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (int j = 1; j &lt; m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (int i = 1; i &lt; n; i++) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (int j = 1; j &lt; m; j++) {\n dp[j] = Math.Min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n</code></pre> min_path_sum.go<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc minPathSumDPComp(grid [][]int) int {\n n, m := len(grid), len(grid[0])\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, m)\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0]\n for j := 1; j &lt; m; j++ {\n dp[j] = dp[j-1] + grid[0][j]\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for i := 1; i &lt; n; i++ {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0]\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for j := 1; j &lt; m; j++ {\n dp[j] = int(math.Min(float64(dp[j-1]), float64(dp[j]))) + grid[i][j]\n }\n }\n return dp[m-1]\n}\n</code></pre> min_path_sum.swift<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc minPathSumDPComp(grid: [[Int]]) -&gt; Int {\n let n = grid.count\n let m = grid[0].count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: 0, count: m)\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0]\n for j in 1 ..&lt; m {\n dp[j] = dp[j - 1] + grid[0][j]\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for i in 1 ..&lt; n {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0]\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for j in 1 ..&lt; m {\n dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n }\n }\n return dp[m - 1]\n}\n</code></pre> min_path_sum.js<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u72c0\u614b\u58d3\u7e2e\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction minPathSumDPComp(grid) {\n const n = grid.length,\n m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = new Array(m);\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (let j = 1; j &lt; m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (let i = 1; i &lt; n; i++) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (let j = 1; j &lt; m; j++) {\n dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n</code></pre> min_path_sum.ts<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u72c0\u614b\u58d3\u7e2e\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction minPathSumDPComp(grid: Array&lt;Array&lt;number&gt;&gt;): number {\n const n = grid.length,\n m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = new Array(m);\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (let j = 1; j &lt; m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (let i = 1; i &lt; n; i++) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (let j = 1; j &lt; m; j++) {\n dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n</code></pre> min_path_sum.dart<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint minPathSumDPComp(List&lt;List&lt;int&gt;&gt; grid) {\n int n = grid.length, m = grid[0].length;\n // \u521d\u59cb\u5316 dp \u8868\n List&lt;int&gt; dp = List.filled(m, 0);\n dp[0] = grid[0][0];\n for (int j = 1; j &lt; m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (int i = 1; i &lt; n; i++) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (int j = 1; j &lt; m; j++) {\n dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n</code></pre> min_path_sum.rs<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfn min_path_sum_dp_comp(grid: &amp;Vec&lt;Vec&lt;i32&gt;&gt;) -&gt; i32 {\n let (n, m) = (grid.len(), grid[0].len());\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; m];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for j in 1..m {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for i in 1..n {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for j in 1..m {\n dp[j] = std::cmp::min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n dp[m - 1]\n}\n</code></pre> min_path_sum.c<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint minPathSumDPComp(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = calloc(m, sizeof(int));\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (int j = 1; j &lt; m; j++) {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (int i = 1; i &lt; n; i++) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (int j = 1; j &lt; m; j++) {\n dp[j] = myMin(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n int res = dp[m - 1];\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(dp);\n return res;\n}\n</code></pre> min_path_sum.kt<pre><code>/* \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfun minPathSumDPComp(grid: Array&lt;IntArray&gt;): Int {\n val n = grid.size\n val m = grid[0].size\n // \u521d\u59cb\u5316 dp \u8868\n val dp = IntArray(m)\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0]\n for (j in 1..&lt;m) {\n dp[j] = dp[j - 1] + grid[0][j]\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (i in 1..&lt;n) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0]\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (j in 1..&lt;m) {\n dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n }\n }\n return dp[m - 1]\n}\n</code></pre> min_path_sum.rb<pre><code>[class]{}-[func]{min_path_sum_dp_comp}\n</code></pre> min_path_sum.zig<pre><code>// \u6700\u5c0f\u8def\u5f91\u548c\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\nfn minPathSumDPComp(comptime grid: anytype) i32 {\n comptime var n = grid.len;\n comptime var m = grid[0].len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** m;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n dp[0] = grid[0][0];\n for (1..m) |j| {\n dp[j] = dp[j - 1] + grid[0][j];\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (1..n) |i| {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n dp[0] = dp[0] + grid[i][0];\n for (1..m) |j| {\n dp[j] = @min(dp[j - 1], dp[j]) + grid[i][j];\n }\n }\n return dp[m - 1];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_dynamic_programming/edit_distance_problem/","title":"14.6 \u00a0 \u7de8\u8f2f\u8ddd\u96e2\u554f\u984c","text":"<p>\u7de8\u8f2f\u8ddd\u96e2\uff0c\u4e5f\u7a31 Levenshtein \u8ddd\u96e2\uff0c\u6307\u5169\u500b\u5b57\u4e32\u4e4b\u9593\u4e92\u76f8\u8f49\u63db\u7684\u6700\u5c11\u4fee\u6539\u6b21\u6578\uff0c\u901a\u5e38\u7528\u65bc\u5728\u8cc7\u8a0a\u6aa2\u7d22\u548c\u81ea\u7136\u8a9e\u8a00\u8655\u7406\u4e2d\u5ea6\u91cf\u5169\u500b\u5e8f\u5217\u7684\u76f8\u4f3c\u5ea6\u3002</p> <p>Question</p> <p>\u8f38\u5165\u5169\u500b\u5b57\u4e32 \\(s\\) \u548c \\(t\\) \uff0c\u8fd4\u56de\u5c07 \\(s\\) \u8f49\u63db\u70ba \\(t\\) \u6240\u9700\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578\u3002</p> <p>\u4f60\u53ef\u4ee5\u5728\u4e00\u500b\u5b57\u4e32\u4e2d\u9032\u884c\u4e09\u7a2e\u7de8\u8f2f\u64cd\u4f5c\uff1a\u63d2\u5165\u4e00\u500b\u5b57\u5143\u3001\u522a\u9664\u4e00\u500b\u5b57\u5143\u3001\u5c07\u5b57\u5143\u66ff\u63db\u70ba\u4efb\u610f\u4e00\u500b\u5b57\u5143\u3002</p> <p>\u5982\u5716 14-27 \u6240\u793a\uff0c\u5c07 <code>kitten</code> \u8f49\u63db\u70ba <code>sitting</code> \u9700\u8981\u7de8\u8f2f 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u63db\u64cd\u4f5c\u8207 1 \u6b21\u65b0\u589e\u64cd\u4f5c\uff1b\u5c07 <code>hello</code> \u8f49\u63db\u70ba <code>algo</code> \u9700\u8981 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u63db\u64cd\u4f5c\u548c 1 \u6b21\u522a\u9664\u64cd\u4f5c\u3002</p> <p></p> <p> \u5716 14-27 \u00a0 \u7de8\u8f2f\u8ddd\u96e2\u7684\u793a\u4f8b\u8cc7\u6599 </p> <p>\u7de8\u8f2f\u8ddd\u96e2\u554f\u984c\u53ef\u4ee5\u5f88\u81ea\u7136\u5730\u7528\u6c7a\u7b56\u6a39\u6a21\u578b\u4f86\u89e3\u91cb\u3002\u5b57\u4e32\u5c0d\u61c9\u6a39\u7bc0\u9ede\uff0c\u4e00\u8f2a\u6c7a\u7b56\uff08\u4e00\u6b21\u7de8\u8f2f\u64cd\u4f5c\uff09\u5c0d\u61c9\u6a39\u7684\u4e00\u689d\u908a\u3002</p> <p>\u5982\u5716 14-28 \u6240\u793a\uff0c\u5728\u4e0d\u9650\u5236\u64cd\u4f5c\u7684\u60c5\u6cc1\u4e0b\uff0c\u6bcf\u500b\u7bc0\u9ede\u90fd\u53ef\u4ee5\u6d3e\u751f\u51fa\u8a31\u591a\u689d\u908a\uff0c\u6bcf\u689d\u908a\u5c0d\u61c9\u4e00\u7a2e\u64cd\u4f5c\uff0c\u9019\u610f\u5473\u8457\u5f9e <code>hello</code> \u8f49\u63db\u5230 <code>algo</code> \u6709\u8a31\u591a\u7a2e\u53ef\u80fd\u7684\u8def\u5f91\u3002</p> <p>\u5f9e\u6c7a\u7b56\u6a39\u7684\u89d2\u5ea6\u770b\uff0c\u672c\u984c\u7684\u76ee\u6a19\u662f\u6c42\u89e3\u7bc0\u9ede <code>hello</code> \u548c\u7bc0\u9ede <code>algo</code> \u4e4b\u9593\u7684\u6700\u77ed\u8def\u5f91\u3002</p> <p></p> <p> \u5716 14-28 \u00a0 \u57fa\u65bc\u6c7a\u7b56\u6a39\u6a21\u578b\u8868\u793a\u7de8\u8f2f\u8ddd\u96e2\u554f\u984c </p>"},{"location":"chapter_dynamic_programming/edit_distance_problem/#1","title":"1. \u00a0 \u52d5\u614b\u898f\u5283\u601d\u8def","text":"<p>\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f2a\u7684\u6c7a\u7b56\uff0c\u5b9a\u7fa9\u72c0\u614b\uff0c\u5f9e\u800c\u5f97\u5230 \\(dp\\) \u8868</p> <p>\u6bcf\u4e00\u8f2a\u7684\u6c7a\u7b56\u662f\u5c0d\u5b57\u4e32 \\(s\\) \u9032\u884c\u4e00\u6b21\u7de8\u8f2f\u64cd\u4f5c\u3002</p> <p>\u6211\u5011\u5e0c\u671b\u5728\u7de8\u8f2f\u64cd\u4f5c\u7684\u904e\u7a0b\u4e2d\uff0c\u554f\u984c\u7684\u898f\u6a21\u9010\u6f38\u7e2e\u5c0f\uff0c\u9019\u6a23\u624d\u80fd\u69cb\u5efa\u5b50\u554f\u984c\u3002\u8a2d\u5b57\u4e32 \\(s\\) \u548c \\(t\\) \u7684\u9577\u5ea6\u5206\u5225\u70ba \\(n\\) \u548c \\(m\\) \uff0c\u6211\u5011\u5148\u8003\u616e\u5169\u5b57\u4e32\u5c3e\u90e8\u7684\u5b57\u5143 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u3002</p> <ul> <li>\u82e5 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u76f8\u540c\uff0c\u6211\u5011\u53ef\u4ee5\u8df3\u904e\u5b83\u5011\uff0c\u76f4\u63a5\u8003\u616e \\(s[n-2]\\) \u548c \\(t[m-2]\\) \u3002</li> <li>\u82e5 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u4e0d\u540c\uff0c\u6211\u5011\u9700\u8981\u5c0d \\(s\\) \u9032\u884c\u4e00\u6b21\u7de8\u8f2f\uff08\u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\uff09\uff0c\u4f7f\u5f97\u5169\u5b57\u4e32\u5c3e\u90e8\u7684\u5b57\u5143\u76f8\u540c\uff0c\u5f9e\u800c\u53ef\u4ee5\u8df3\u904e\u5b83\u5011\uff0c\u8003\u616e\u898f\u6a21\u66f4\u5c0f\u7684\u554f\u984c\u3002</li> </ul> <p>\u4e5f\u5c31\u662f\u8aaa\uff0c\u6211\u5011\u5728\u5b57\u4e32 \\(s\\) \u4e2d\u9032\u884c\u7684\u6bcf\u4e00\u8f2a\u6c7a\u7b56\uff08\u7de8\u8f2f\u64cd\u4f5c\uff09\uff0c\u90fd\u6703\u4f7f\u5f97 \\(s\\) \u548c \\(t\\) \u4e2d\u5269\u9918\u7684\u5f85\u5339\u914d\u5b57\u5143\u767c\u751f\u8b8a\u5316\u3002\u56e0\u6b64\uff0c\u72c0\u614b\u70ba\u7576\u524d\u5728 \\(s\\) \u548c \\(t\\) \u4e2d\u8003\u616e\u7684\u7b2c \\(i\\) \u548c\u7b2c \\(j\\) \u500b\u5b57\u5143\uff0c\u8a18\u70ba \\([i, j]\\) \u3002</p> <p>\u72c0\u614b \\([i, j]\\) \u5c0d\u61c9\u7684\u5b50\u554f\u984c\uff1a\u5c07 \\(s\\) \u7684\u524d \\(i\\) \u500b\u5b57\u5143\u66f4\u6539\u70ba \\(t\\) \u7684\u524d \\(j\\) \u500b\u5b57\u5143\u6240\u9700\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578\u3002</p> <p>\u81f3\u6b64\uff0c\u5f97\u5230\u4e00\u500b\u5c3a\u5bf8\u70ba \\((i+1) \\times (j+1)\\) \u7684\u4e8c\u7dad \\(dp\\) \u8868\u3002</p> <p>\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u512a\u5b50\u7d50\u69cb\uff0c\u9032\u800c\u63a8\u5c0e\u51fa\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b</p> <p>\u8003\u616e\u5b50\u554f\u984c \\(dp[i, j]\\) \uff0c\u5176\u5c0d\u61c9\u7684\u5169\u500b\u5b57\u4e32\u7684\u5c3e\u90e8\u5b57\u5143\u70ba \\(s[i-1]\\) \u548c \\(t[j-1]\\) \uff0c\u53ef\u6839\u64da\u4e0d\u540c\u7de8\u8f2f\u64cd\u4f5c\u5206\u70ba\u5716 14-29 \u6240\u793a\u7684\u4e09\u7a2e\u60c5\u6cc1\u3002</p> <ol> <li>\u5728 \\(s[i-1]\\) \u4e4b\u5f8c\u65b0\u589e \\(t[j-1]\\) \uff0c\u5247\u5269\u9918\u5b50\u554f\u984c \\(dp[i, j-1]\\) \u3002</li> <li>\u522a\u9664 \\(s[i-1]\\) \uff0c\u5247\u5269\u9918\u5b50\u554f\u984c \\(dp[i-1, j]\\) \u3002</li> <li>\u5c07 \\(s[i-1]\\) \u66ff\u63db\u70ba \\(t[j-1]\\) \uff0c\u5247\u5269\u9918\u5b50\u554f\u984c \\(dp[i-1, j-1]\\) \u3002</li> </ol> <p></p> <p> \u5716 14-29 \u00a0 \u7de8\u8f2f\u8ddd\u96e2\u7684\u72c0\u614b\u8f49\u79fb </p> <p>\u6839\u64da\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u5f97\u6700\u512a\u5b50\u7d50\u69cb\uff1a\\(dp[i, j]\\) \u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578\u7b49\u65bc \\(dp[i, j-1]\\)\u3001\\(dp[i-1, j]\\)\u3001\\(dp[i-1, j-1]\\) \u4e09\u8005\u4e2d\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578\uff0c\u518d\u52a0\u4e0a\u672c\u6b21\u7684\u7de8\u8f2f\u6b65\u6578 \\(1\\) \u3002\u5c0d\u61c9\u7684\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\u70ba\uff1a</p> \\[ dp[i, j] = \\min(dp[i, j-1], dp[i-1, j], dp[i-1, j-1]) + 1 \\] <p>\u8acb\u6ce8\u610f\uff0c\u7576 \\(s[i-1]\\) \u548c \\(t[j-1]\\) \u76f8\u540c\u6642\uff0c\u7121\u9808\u7de8\u8f2f\u7576\u524d\u5b57\u5143\uff0c\u9019\u7a2e\u60c5\u6cc1\u4e0b\u7684\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\u70ba\uff1a</p> \\[ dp[i, j] = dp[i-1, j-1] \\] <p>\u7b2c\u4e09\u6b65\uff1a\u78ba\u5b9a\u908a\u754c\u689d\u4ef6\u548c\u72c0\u614b\u8f49\u79fb\u9806\u5e8f</p> <p>\u7576\u5169\u5b57\u4e32\u90fd\u70ba\u7a7a\u6642\uff0c\u7de8\u8f2f\u6b65\u6578\u70ba \\(0\\) \uff0c\u5373 \\(dp[0, 0] = 0\\) \u3002\u7576 \\(s\\) \u70ba\u7a7a\u4f46 \\(t\\) \u4e0d\u70ba\u7a7a\u6642\uff0c\u6700\u5c11\u7de8\u8f2f\u6b65\u6578\u7b49\u65bc \\(t\\) \u7684\u9577\u5ea6\uff0c\u5373\u9996\u884c \\(dp[0, j] = j\\) \u3002\u7576 \\(s\\) \u4e0d\u70ba\u7a7a\u4f46 \\(t\\) \u70ba\u7a7a\u6642\uff0c\u6700\u5c11\u7de8\u8f2f\u6b65\u6578\u7b49\u65bc \\(s\\) \u7684\u9577\u5ea6\uff0c\u5373\u9996\u5217 \\(dp[i, 0] = i\\) \u3002</p> <p>\u89c0\u5bdf\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\uff0c\u89e3 \\(dp[i, j]\\) \u4f9d\u8cf4\u5de6\u65b9\u3001\u4e0a\u65b9\u3001\u5de6\u4e0a\u65b9\u7684\u89e3\uff0c\u56e0\u6b64\u900f\u904e\u5169\u5c64\u8ff4\u5708\u6b63\u5e8f\u8d70\u8a2a\u6574\u500b \\(dp\\) \u8868\u5373\u53ef\u3002</p>"},{"location":"chapter_dynamic_programming/edit_distance_problem/#2","title":"2. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig edit_distance.py<pre><code>def edit_distance_dp(s: str, t: str) -&gt; int:\n \"\"\"\u7de8\u8f2f\u8ddd\u96e2\uff1a\u52d5\u614b\u898f\u5283\"\"\"\n n, m = len(s), len(t)\n dp = [[0] * (m + 1) for _ in range(n + 1)]\n # \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for i in range(1, n + 1):\n dp[i][0] = i\n for j in range(1, m + 1):\n dp[0][j] = j\n # \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for i in range(1, n + 1):\n for j in range(1, m + 1):\n if s[i - 1] == t[j - 1]:\n # \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[i][j] = dp[i - 1][j - 1]\n else:\n # \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1\n return dp[n][m]\n</code></pre> edit_distance.cpp<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u52d5\u614b\u898f\u5283 */\nint editDistanceDP(string s, string t) {\n int n = s.length(), m = t.length();\n vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(m + 1, 0));\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i &lt;= n; i++) {\n dp[i][0] = i;\n }\n for (int j = 1; j &lt;= m; j++) {\n dp[0][j] = j;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt;= n; i++) {\n for (int j = 1; j &lt;= m; j++) {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n</code></pre> edit_distance.java<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u52d5\u614b\u898f\u5283 */\nint editDistanceDP(String s, String t) {\n int n = s.length(), m = t.length();\n int[][] dp = new int[n + 1][m + 1];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i &lt;= n; i++) {\n dp[i][0] = i;\n }\n for (int j = 1; j &lt;= m; j++) {\n dp[0][j] = j;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt;= n; i++) {\n for (int j = 1; j &lt;= m; j++) {\n if (s.charAt(i - 1) == t.charAt(j - 1)) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n</code></pre> edit_distance.cs<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u52d5\u614b\u898f\u5283 */\nint EditDistanceDP(string s, string t) {\n int n = s.Length, m = t.Length;\n int[,] dp = new int[n + 1, m + 1];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i &lt;= n; i++) {\n dp[i, 0] = i;\n }\n for (int j = 1; j &lt;= m; j++) {\n dp[0, j] = j;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt;= n; i++) {\n for (int j = 1; j &lt;= m; j++) {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[i, j] = dp[i - 1, j - 1];\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[i, j] = Math.Min(Math.Min(dp[i, j - 1], dp[i - 1, j]), dp[i - 1, j - 1]) + 1;\n }\n }\n }\n return dp[n, m];\n}\n</code></pre> edit_distance.go<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u52d5\u614b\u898f\u5283 */\nfunc editDistanceDP(s string, t string) int {\n n := len(s)\n m := len(t)\n dp := make([][]int, n+1)\n for i := 0; i &lt;= n; i++ {\n dp[i] = make([]int, m+1)\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for i := 1; i &lt;= n; i++ {\n dp[i][0] = i\n }\n for j := 1; j &lt;= m; j++ {\n dp[0][j] = j\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for i := 1; i &lt;= n; i++ {\n for j := 1; j &lt;= m; j++ {\n if s[i-1] == t[j-1] {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[i][j] = dp[i-1][j-1]\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[i][j] = MinInt(MinInt(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1\n }\n }\n }\n return dp[n][m]\n}\n</code></pre> edit_distance.swift<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u52d5\u614b\u898f\u5283 */\nfunc editDistanceDP(s: String, t: String) -&gt; Int {\n let n = s.utf8CString.count\n let m = t.utf8CString.count\n var dp = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for i in 1 ... n {\n dp[i][0] = i\n }\n for j in 1 ... m {\n dp[0][j] = j\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for i in 1 ... n {\n for j in 1 ... m {\n if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[i][j] = dp[i - 1][j - 1]\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n }\n }\n }\n return dp[n][m]\n}\n</code></pre> edit_distance.js<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u52d5\u614b\u898f\u5283 */\nfunction editDistanceDP(s, t) {\n const n = s.length,\n m = t.length;\n const dp = Array.from({ length: n + 1 }, () =&gt; new Array(m + 1).fill(0));\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (let i = 1; i &lt;= n; i++) {\n dp[i][0] = i;\n }\n for (let j = 1; j &lt;= m; j++) {\n dp[0][j] = j;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (let i = 1; i &lt;= n; i++) {\n for (let j = 1; j &lt;= m; j++) {\n if (s.charAt(i - 1) === t.charAt(j - 1)) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[i][j] =\n Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n</code></pre> edit_distance.ts<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u52d5\u614b\u898f\u5283 */\nfunction editDistanceDP(s: string, t: string): number {\n const n = s.length,\n m = t.length;\n const dp = Array.from({ length: n + 1 }, () =&gt;\n Array.from({ length: m + 1 }, () =&gt; 0)\n );\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (let i = 1; i &lt;= n; i++) {\n dp[i][0] = i;\n }\n for (let j = 1; j &lt;= m; j++) {\n dp[0][j] = j;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (let i = 1; i &lt;= n; i++) {\n for (let j = 1; j &lt;= m; j++) {\n if (s.charAt(i - 1) === t.charAt(j - 1)) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[i][j] =\n Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n</code></pre> edit_distance.dart<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u52d5\u614b\u898f\u5283 */\nint editDistanceDP(String s, String t) {\n int n = s.length, m = t.length;\n List&lt;List&lt;int&gt;&gt; dp = List.generate(n + 1, (_) =&gt; List.filled(m + 1, 0));\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i &lt;= n; i++) {\n dp[i][0] = i;\n }\n for (int j = 1; j &lt;= m; j++) {\n dp[0][j] = j;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt;= n; i++) {\n for (int j = 1; j &lt;= m; j++) {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n</code></pre> edit_distance.rs<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u52d5\u614b\u898f\u5283 */\nfn edit_distance_dp(s: &amp;str, t: &amp;str) -&gt; i32 {\n let (n, m) = (s.len(), t.len());\n let mut dp = vec![vec![0; m + 1]; n + 1];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for i in 1..=n {\n dp[i][0] = i as i32;\n }\n for j in 1..m {\n dp[0][j] = j as i32;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for i in 1..=n {\n for j in 1..=m {\n if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[i][j] =\n std::cmp::min(std::cmp::min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n dp[n][m]\n}\n</code></pre> edit_distance.c<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u52d5\u614b\u898f\u5283 */\nint editDistanceDP(char *s, char *t, int n, int m) {\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i &lt;= n; i++) {\n dp[i] = calloc(m + 1, sizeof(int));\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int i = 1; i &lt;= n; i++) {\n dp[i][0] = i;\n }\n for (int j = 1; j &lt;= m; j++) {\n dp[0][j] = j;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt;= n; i++) {\n for (int j = 1; j &lt;= m; j++) {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[i][j] = myMin(myMin(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n int res = dp[n][m];\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n for (int i = 0; i &lt;= n; i++) {\n free(dp[i]);\n }\n return res;\n}\n</code></pre> edit_distance.kt<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u52d5\u614b\u898f\u5283 */\nfun editDistanceDP(s: String, t: String): Int {\n val n = s.length\n val m = t.length\n val dp = Array(n + 1) { IntArray(m + 1) }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (i in 1..n) {\n dp[i][0] = i\n }\n for (j in 1..m) {\n dp[0][j] = j\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (i in 1..n) {\n for (j in 1..m) {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[i][j] = dp[i - 1][j - 1]\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n }\n }\n }\n return dp[n][m]\n}\n</code></pre> edit_distance.rb<pre><code>[class]{}-[func]{edit_distance_dp}\n</code></pre> edit_distance.zig<pre><code>// \u7de8\u8f2f\u8ddd\u96e2\uff1a\u52d5\u614b\u898f\u5283\nfn editDistanceDP(comptime s: []const u8, comptime t: []const u8) i32 {\n comptime var n = s.len;\n comptime var m = t.len;\n var dp = [_][m + 1]i32{[_]i32{0} ** (m + 1)} ** (n + 1);\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (1..n + 1) |i| {\n dp[i][0] = @intCast(i);\n }\n for (1..m + 1) |j| {\n dp[0][j] = @intCast(j);\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (1..n + 1) |i| {\n for (1..m + 1) |j| {\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[i][j] = dp[i - 1][j - 1];\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[i][j] = @min(@min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n }\n }\n }\n return dp[n][m];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5982\u5716 14-30 \u6240\u793a\uff0c\u7de8\u8f2f\u8ddd\u96e2\u554f\u984c\u7684\u72c0\u614b\u8f49\u79fb\u904e\u7a0b\u8207\u80cc\u5305\u554f\u984c\u975e\u5e38\u985e\u4f3c\uff0c\u90fd\u53ef\u4ee5\u770b\u4f5c\u586b\u5beb\u4e00\u500b\u4e8c\u7dad\u7db2\u683c\u7684\u904e\u7a0b\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt;&lt;10&gt;&lt;11&gt;&lt;12&gt;&lt;13&gt;&lt;14&gt;&lt;15&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 14-30 \u00a0 \u7de8\u8f2f\u8ddd\u96e2\u7684\u52d5\u614b\u898f\u5283\u904e\u7a0b </p>"},{"location":"chapter_dynamic_programming/edit_distance_problem/#3","title":"3. \u00a0 \u7a7a\u9593\u6700\u4f73\u5316","text":"<p>\u7531\u65bc \\(dp[i,j]\\) \u662f\u7531\u4e0a\u65b9 \\(dp[i-1, j]\\)\u3001\u5de6\u65b9 \\(dp[i, j-1]\\)\u3001\u5de6\u4e0a\u65b9 \\(dp[i-1, j-1]\\) \u8f49\u79fb\u800c\u4f86\u7684\uff0c\u800c\u6b63\u5e8f\u8d70\u8a2a\u6703\u4e1f\u5931\u5de6\u4e0a\u65b9 \\(dp[i-1, j-1]\\) \uff0c\u5012\u5e8f\u8d70\u8a2a\u7121\u6cd5\u63d0\u524d\u69cb\u5efa \\(dp[i, j-1]\\) \uff0c\u56e0\u6b64\u5169\u7a2e\u8d70\u8a2a\u9806\u5e8f\u90fd\u4e0d\u53ef\u53d6\u3002</p> <p>\u70ba\u6b64\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e00\u500b\u8b8a\u6578 <code>leftup</code> \u4f86\u66ab\u5b58\u5de6\u4e0a\u65b9\u7684\u89e3 \\(dp[i-1, j-1]\\) \uff0c\u5f9e\u800c\u53ea\u9700\u8003\u616e\u5de6\u65b9\u548c\u4e0a\u65b9\u7684\u89e3\u3002\u6b64\u6642\u7684\u60c5\u6cc1\u8207\u5b8c\u5168\u80cc\u5305\u554f\u984c\u76f8\u540c\uff0c\u53ef\u4f7f\u7528\u6b63\u5e8f\u8d70\u8a2a\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig edit_distance.py<pre><code>def edit_distance_dp_comp(s: str, t: str) -&gt; int:\n \"\"\"\u7de8\u8f2f\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\"\"\"\n n, m = len(s), len(t)\n dp = [0] * (m + 1)\n # \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for j in range(1, m + 1):\n dp[j] = j\n # \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for i in range(1, n + 1):\n # \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n leftup = dp[0] # \u66ab\u5b58 dp[i-1, j-1]\n dp[0] += 1\n # \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for j in range(1, m + 1):\n temp = dp[j]\n if s[i - 1] == t[j - 1]:\n # \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[j] = leftup\n else:\n # \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[j] = min(dp[j - 1], dp[j], leftup) + 1\n leftup = temp # \u66f4\u65b0\u70ba\u4e0b\u4e00\u8f2a\u7684 dp[i-1, j-1]\n return dp[m]\n</code></pre> edit_distance.cpp<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint editDistanceDPComp(string s, string t) {\n int n = s.length(), m = t.length();\n vector&lt;int&gt; dp(m + 1, 0);\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (int j = 1; j &lt;= m; j++) {\n dp[j] = j;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (int i = 1; i &lt;= n; i++) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u66ab\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (int j = 1; j &lt;= m; j++) {\n int temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u70ba\u4e0b\u4e00\u8f2a\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n</code></pre> edit_distance.java<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint editDistanceDPComp(String s, String t) {\n int n = s.length(), m = t.length();\n int[] dp = new int[m + 1];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (int j = 1; j &lt;= m; j++) {\n dp[j] = j;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (int i = 1; i &lt;= n; i++) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u66ab\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (int j = 1; j &lt;= m; j++) {\n int temp = dp[j];\n if (s.charAt(i - 1) == t.charAt(j - 1)) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u70ba\u4e0b\u4e00\u8f2a\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n</code></pre> edit_distance.cs<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint EditDistanceDPComp(string s, string t) {\n int n = s.Length, m = t.Length;\n int[] dp = new int[m + 1];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (int j = 1; j &lt;= m; j++) {\n dp[j] = j;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (int i = 1; i &lt;= n; i++) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u66ab\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (int j = 1; j &lt;= m; j++) {\n int temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[j] = Math.Min(Math.Min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u70ba\u4e0b\u4e00\u8f2a\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n</code></pre> edit_distance.go<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc editDistanceDPComp(s string, t string) int {\n n := len(s)\n m := len(t)\n dp := make([]int, m+1)\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for j := 1; j &lt;= m; j++ {\n dp[j] = j\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for i := 1; i &lt;= n; i++ {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n leftUp := dp[0] // \u66ab\u5b58 dp[i-1, j-1]\n dp[0] = i\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for j := 1; j &lt;= m; j++ {\n temp := dp[j]\n if s[i-1] == t[j-1] {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[j] = leftUp\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[j] = MinInt(MinInt(dp[j-1], dp[j]), leftUp) + 1\n }\n leftUp = temp // \u66f4\u65b0\u70ba\u4e0b\u4e00\u8f2a\u7684 dp[i-1, j-1]\n }\n }\n return dp[m]\n}\n</code></pre> edit_distance.swift<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc editDistanceDPComp(s: String, t: String) -&gt; Int {\n let n = s.utf8CString.count\n let m = t.utf8CString.count\n var dp = Array(repeating: 0, count: m + 1)\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for j in 1 ... m {\n dp[j] = j\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for i in 1 ... n {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n var leftup = dp[0] // \u66ab\u5b58 dp[i-1, j-1]\n dp[0] = i\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for j in 1 ... m {\n let temp = dp[j]\n if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[j] = leftup\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n }\n leftup = temp // \u66f4\u65b0\u70ba\u4e0b\u4e00\u8f2a\u7684 dp[i-1, j-1]\n }\n }\n return dp[m]\n}\n</code></pre> edit_distance.js<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u72c0\u614b\u58d3\u7e2e\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction editDistanceDPComp(s, t) {\n const n = s.length,\n m = t.length;\n const dp = new Array(m + 1).fill(0);\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (let j = 1; j &lt;= m; j++) {\n dp[j] = j;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (let i = 1; i &lt;= n; i++) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n let leftup = dp[0]; // \u66ab\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (let j = 1; j &lt;= m; j++) {\n const temp = dp[j];\n if (s.charAt(i - 1) === t.charAt(j - 1)) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u70ba\u4e0b\u4e00\u8f2a\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n</code></pre> edit_distance.ts<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u72c0\u614b\u58d3\u7e2e\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction editDistanceDPComp(s: string, t: string): number {\n const n = s.length,\n m = t.length;\n const dp = new Array(m + 1).fill(0);\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (let j = 1; j &lt;= m; j++) {\n dp[j] = j;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (let i = 1; i &lt;= n; i++) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n let leftup = dp[0]; // \u66ab\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (let j = 1; j &lt;= m; j++) {\n const temp = dp[j];\n if (s.charAt(i - 1) === t.charAt(j - 1)) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u70ba\u4e0b\u4e00\u8f2a\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n</code></pre> edit_distance.dart<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint editDistanceDPComp(String s, String t) {\n int n = s.length, m = t.length;\n List&lt;int&gt; dp = List.filled(m + 1, 0);\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (int j = 1; j &lt;= m; j++) {\n dp[j] = j;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (int i = 1; i &lt;= n; i++) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u66ab\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (int j = 1; j &lt;= m; j++) {\n int temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u70ba\u4e0b\u4e00\u8f2a\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n</code></pre> edit_distance.rs<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfn edit_distance_dp_comp(s: &amp;str, t: &amp;str) -&gt; i32 {\n let (n, m) = (s.len(), t.len());\n let mut dp = vec![0; m + 1];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for j in 1..m {\n dp[j] = j as i32;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for i in 1..=n {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n let mut leftup = dp[0]; // \u66ab\u5b58 dp[i-1, j-1]\n dp[0] = i as i32;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for j in 1..=m {\n let temp = dp[j];\n if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[j] = std::cmp::min(std::cmp::min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u70ba\u4e0b\u4e00\u8f2a\u7684 dp[i-1, j-1]\n }\n }\n dp[m]\n}\n</code></pre> edit_distance.c<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint editDistanceDPComp(char *s, char *t, int n, int m) {\n int *dp = calloc(m + 1, sizeof(int));\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (int j = 1; j &lt;= m; j++) {\n dp[j] = j;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (int i = 1; i &lt;= n; i++) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n int leftup = dp[0]; // \u66ab\u5b58 dp[i-1, j-1]\n dp[0] = i;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (int j = 1; j &lt;= m; j++) {\n int temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[j] = myMin(myMin(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u70ba\u4e0b\u4e00\u8f2a\u7684 dp[i-1, j-1]\n }\n }\n int res = dp[m];\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(dp);\n return res;\n}\n</code></pre> edit_distance.kt<pre><code>/* \u7de8\u8f2f\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfun editDistanceDPComp(s: String, t: String): Int {\n val n = s.length\n val m = t.length\n val dp = IntArray(m + 1)\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (j in 1..m) {\n dp[j] = j\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (i in 1..n) {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n var leftup = dp[0] // \u66ab\u5b58 dp[i-1, j-1]\n dp[0] = i\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (j in 1..m) {\n val temp = dp[j]\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[j] = leftup\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n }\n leftup = temp // \u66f4\u65b0\u70ba\u4e0b\u4e00\u8f2a\u7684 dp[i-1, j-1]\n }\n }\n return dp[m]\n}\n</code></pre> edit_distance.rb<pre><code>[class]{}-[func]{edit_distance_dp_comp}\n</code></pre> edit_distance.zig<pre><code>// \u7de8\u8f2f\u8ddd\u96e2\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\nfn editDistanceDPComp(comptime s: []const u8, comptime t: []const u8) i32 {\n comptime var n = s.len;\n comptime var m = t.len;\n var dp = [_]i32{0} ** (m + 1);\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\n for (1..m + 1) |j| {\n dp[j] = @intCast(j);\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\n for (1..n + 1) |i| {\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u5217\n var leftup = dp[0]; // \u66ab\u5b58 dp[i-1, j-1]\n dp[0] = @intCast(i);\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u5217\n for (1..m + 1) |j| {\n var temp = dp[j];\n if (s[i - 1] == t[j - 1]) {\n // \u82e5\u5169\u5b57\u5143\u76f8\u7b49\uff0c\u5247\u76f4\u63a5\u8df3\u904e\u6b64\u5169\u5b57\u5143\n dp[j] = leftup;\n } else {\n // \u6700\u5c11\u7de8\u8f2f\u6b65\u6578 = \u63d2\u5165\u3001\u522a\u9664\u3001\u66ff\u63db\u9019\u4e09\u7a2e\u64cd\u4f5c\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578 + 1\n dp[j] = @min(@min(dp[j - 1], dp[j]), leftup) + 1;\n }\n leftup = temp; // \u66f4\u65b0\u70ba\u4e0b\u4e00\u8f2a\u7684 dp[i-1, j-1]\n }\n }\n return dp[m];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/","title":"14.1 \u00a0 \u521d\u63a2\u52d5\u614b\u898f\u5283","text":"<p>\u52d5\u614b\u898f\u5283\uff08dynamic programming\uff09\u662f\u4e00\u500b\u91cd\u8981\u7684\u6f14\u7b97\u6cd5\u7bc4\u5f0f\uff0c\u5b83\u5c07\u4e00\u500b\u554f\u984c\u5206\u89e3\u70ba\u4e00\u7cfb\u5217\u66f4\u5c0f\u7684\u5b50\u554f\u984c\uff0c\u4e26\u900f\u904e\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\u4f86\u907f\u514d\u91cd\u8907\u8a08\u7b97\uff0c\u5f9e\u800c\u5927\u5e45\u63d0\u5347\u6642\u9593\u6548\u7387\u3002</p> <p>\u5728\u672c\u7bc0\u4e2d\uff0c\u6211\u5011\u5f9e\u4e00\u500b\u7d93\u5178\u4f8b\u984c\u5165\u624b\uff0c\u5148\u7d66\u51fa\u5b83\u7684\u66b4\u529b\u56de\u6eaf\u89e3\u6cd5\uff0c\u89c0\u5bdf\u5176\u4e2d\u5305\u542b\u7684\u91cd\u758a\u5b50\u554f\u984c\uff0c\u518d\u9010\u6b65\u5c0e\u51fa\u66f4\u9ad8\u6548\u7684\u52d5\u614b\u898f\u5283\u89e3\u6cd5\u3002</p> <p>\u722c\u6a13\u68af</p> <p>\u7d66\u5b9a\u4e00\u500b\u5171\u6709 \\(n\\) \u968e\u7684\u6a13\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u968e\u6216\u8005 \\(2\\) \u968e\uff0c\u8acb\u554f\u6709\u591a\u5c11\u7a2e\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u6a13\u9802\uff1f</p> <p>\u5982\u5716 14-1 \u6240\u793a\uff0c\u5c0d\u65bc\u4e00\u500b \\(3\\) \u968e\u6a13\u68af\uff0c\u5171\u6709 \\(3\\) \u7a2e\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u6a13\u9802\u3002</p> <p></p> <p> \u5716 14-1 \u00a0 \u722c\u5230\u7b2c 3 \u968e\u7684\u65b9\u6848\u6578\u91cf </p> <p>\u672c\u984c\u7684\u76ee\u6a19\u662f\u6c42\u89e3\u65b9\u6848\u6578\u91cf\uff0c\u6211\u5011\u53ef\u4ee5\u8003\u616e\u900f\u904e\u56de\u6eaf\u4f86\u7aae\u8209\u6240\u6709\u53ef\u80fd\u6027\u3002\u5177\u9ad4\u4f86\u8aaa\uff0c\u5c07\u722c\u6a13\u68af\u60f3\u8c61\u70ba\u4e00\u500b\u591a\u8f2a\u9078\u64c7\u7684\u904e\u7a0b\uff1a\u5f9e\u5730\u9762\u51fa\u767c\uff0c\u6bcf\u8f2a\u9078\u64c7\u4e0a \\(1\\) \u968e\u6216 \\(2\\) \u968e\uff0c\u6bcf\u7576\u5230\u9054\u6a13\u68af\u9802\u90e8\u6642\u5c31\u5c07\u65b9\u6848\u6578\u91cf\u52a0 \\(1\\) \uff0c\u7576\u8d8a\u904e\u6a13\u68af\u9802\u90e8\u6642\u5c31\u5c07\u5176\u526a\u679d\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig climbing_stairs_backtrack.py<pre><code>def backtrack(choices: list[int], state: int, n: int, res: list[int]) -&gt; int:\n \"\"\"\u56de\u6eaf\"\"\"\n # \u7576\u722c\u5230\u7b2c n \u968e\u6642\uff0c\u65b9\u6848\u6578\u91cf\u52a0 1\n if state == n:\n res[0] += 1\n # \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for choice in choices:\n # \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8d8a\u904e\u7b2c n \u968e\n if state + choice &gt; n:\n continue\n # \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n backtrack(choices, state + choice, n, res)\n # \u56de\u9000\n\ndef climbing_stairs_backtrack(n: int) -&gt; int:\n \"\"\"\u722c\u6a13\u68af\uff1a\u56de\u6eaf\"\"\"\n choices = [1, 2] # \u53ef\u9078\u64c7\u5411\u4e0a\u722c 1 \u968e\u6216 2 \u968e\n state = 0 # \u5f9e\u7b2c 0 \u968e\u958b\u59cb\u722c\n res = [0] # \u4f7f\u7528 res[0] \u8a18\u9304\u65b9\u6848\u6578\u91cf\n backtrack(choices, state, n, res)\n return res[0]\n</code></pre> climbing_stairs_backtrack.cpp<pre><code>/* \u56de\u6eaf */\nvoid backtrack(vector&lt;int&gt; &amp;choices, int state, int n, vector&lt;int&gt; &amp;res) {\n // \u7576\u722c\u5230\u7b2c n \u968e\u6642\uff0c\u65b9\u6848\u6578\u91cf\u52a0 1\n if (state == n)\n res[0]++;\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (auto &amp;choice : choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8d8a\u904e\u7b2c n \u968e\n if (state + choice &gt; n)\n continue;\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u6a13\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n vector&lt;int&gt; choices = {1, 2}; // \u53ef\u9078\u64c7\u5411\u4e0a\u722c 1 \u968e\u6216 2 \u968e\n int state = 0; // \u5f9e\u7b2c 0 \u968e\u958b\u59cb\u722c\n vector&lt;int&gt; res = {0}; // \u4f7f\u7528 res[0] \u8a18\u9304\u65b9\u6848\u6578\u91cf\n backtrack(choices, state, n, res);\n return res[0];\n}\n</code></pre> climbing_stairs_backtrack.java<pre><code>/* \u56de\u6eaf */\nvoid backtrack(List&lt;Integer&gt; choices, int state, int n, List&lt;Integer&gt; res) {\n // \u7576\u722c\u5230\u7b2c n \u968e\u6642\uff0c\u65b9\u6848\u6578\u91cf\u52a0 1\n if (state == n)\n res.set(0, res.get(0) + 1);\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (Integer choice : choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8d8a\u904e\u7b2c n \u968e\n if (state + choice &gt; n)\n continue;\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u6a13\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n List&lt;Integer&gt; choices = Arrays.asList(1, 2); // \u53ef\u9078\u64c7\u5411\u4e0a\u722c 1 \u968e\u6216 2 \u968e\n int state = 0; // \u5f9e\u7b2c 0 \u968e\u958b\u59cb\u722c\n List&lt;Integer&gt; res = new ArrayList&lt;&gt;();\n res.add(0); // \u4f7f\u7528 res[0] \u8a18\u9304\u65b9\u6848\u6578\u91cf\n backtrack(choices, state, n, res);\n return res.get(0);\n}\n</code></pre> climbing_stairs_backtrack.cs<pre><code>/* \u56de\u6eaf */\nvoid Backtrack(List&lt;int&gt; choices, int state, int n, List&lt;int&gt; res) {\n // \u7576\u722c\u5230\u7b2c n \u968e\u6642\uff0c\u65b9\u6848\u6578\u91cf\u52a0 1\n if (state == n)\n res[0]++;\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n foreach (int choice in choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8d8a\u904e\u7b2c n \u968e\n if (state + choice &gt; n)\n continue;\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n Backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u6a13\u68af\uff1a\u56de\u6eaf */\nint ClimbingStairsBacktrack(int n) {\n List&lt;int&gt; choices = [1, 2]; // \u53ef\u9078\u64c7\u5411\u4e0a\u722c 1 \u968e\u6216 2 \u968e\n int state = 0; // \u5f9e\u7b2c 0 \u968e\u958b\u59cb\u722c\n List&lt;int&gt; res = [0]; // \u4f7f\u7528 res[0] \u8a18\u9304\u65b9\u6848\u6578\u91cf\n Backtrack(choices, state, n, res);\n return res[0];\n}\n</code></pre> climbing_stairs_backtrack.go<pre><code>/* \u56de\u6eaf */\nfunc backtrack(choices []int, state, n int, res []int) {\n // \u7576\u722c\u5230\u7b2c n \u968e\u6642\uff0c\u65b9\u6848\u6578\u91cf\u52a0 1\n if state == n {\n res[0] = res[0] + 1\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for _, choice := range choices {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8d8a\u904e\u7b2c n \u968e\n if state+choice &gt; n {\n continue\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n backtrack(choices, state+choice, n, res)\n // \u56de\u9000\n }\n}\n\n/* \u722c\u6a13\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n int) int {\n // \u53ef\u9078\u64c7\u5411\u4e0a\u722c 1 \u968e\u6216 2 \u968e\n choices := []int{1, 2}\n // \u5f9e\u7b2c 0 \u968e\u958b\u59cb\u722c\n state := 0\n res := make([]int, 1)\n // \u4f7f\u7528 res[0] \u8a18\u9304\u65b9\u6848\u6578\u91cf\n res[0] = 0\n backtrack(choices, state, n, res)\n return res[0]\n}\n</code></pre> climbing_stairs_backtrack.swift<pre><code>/* \u56de\u6eaf */\nfunc backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {\n // \u7576\u722c\u5230\u7b2c n \u968e\u6642\uff0c\u65b9\u6848\u6578\u91cf\u52a0 1\n if state == n {\n res[0] += 1\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for choice in choices {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8d8a\u904e\u7b2c n \u968e\n if state + choice &gt; n {\n continue\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n backtrack(choices: choices, state: state + choice, n: n, res: &amp;res)\n // \u56de\u9000\n }\n}\n\n/* \u722c\u6a13\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n: Int) -&gt; Int {\n let choices = [1, 2] // \u53ef\u9078\u64c7\u5411\u4e0a\u722c 1 \u968e\u6216 2 \u968e\n let state = 0 // \u5f9e\u7b2c 0 \u968e\u958b\u59cb\u722c\n var res: [Int] = []\n res.append(0) // \u4f7f\u7528 res[0] \u8a18\u9304\u65b9\u6848\u6578\u91cf\n backtrack(choices: choices, state: state, n: n, res: &amp;res)\n return res[0]\n}\n</code></pre> climbing_stairs_backtrack.js<pre><code>/* \u56de\u6eaf */\nfunction backtrack(choices, state, n, res) {\n // \u7576\u722c\u5230\u7b2c n \u968e\u6642\uff0c\u65b9\u6848\u6578\u91cf\u52a0 1\n if (state === n) res.set(0, res.get(0) + 1);\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (const choice of choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8d8a\u904e\u7b2c n \u968e\n if (state + choice &gt; n) continue;\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u6a13\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n) {\n const choices = [1, 2]; // \u53ef\u9078\u64c7\u5411\u4e0a\u722c 1 \u968e\u6216 2 \u968e\n const state = 0; // \u5f9e\u7b2c 0 \u968e\u958b\u59cb\u722c\n const res = new Map();\n res.set(0, 0); // \u4f7f\u7528 res[0] \u8a18\u9304\u65b9\u6848\u6578\u91cf\n backtrack(choices, state, n, res);\n return res.get(0);\n}\n</code></pre> climbing_stairs_backtrack.ts<pre><code>/* \u56de\u6eaf */\nfunction backtrack(\n choices: number[],\n state: number,\n n: number,\n res: Map&lt;0, any&gt;\n): void {\n // \u7576\u722c\u5230\u7b2c n \u968e\u6642\uff0c\u65b9\u6848\u6578\u91cf\u52a0 1\n if (state === n) res.set(0, res.get(0) + 1);\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (const choice of choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8d8a\u904e\u7b2c n \u968e\n if (state + choice &gt; n) continue;\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u6a13\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n: number): number {\n const choices = [1, 2]; // \u53ef\u9078\u64c7\u5411\u4e0a\u722c 1 \u968e\u6216 2 \u968e\n const state = 0; // \u5f9e\u7b2c 0 \u968e\u958b\u59cb\u722c\n const res = new Map();\n res.set(0, 0); // \u4f7f\u7528 res[0] \u8a18\u9304\u65b9\u6848\u6578\u91cf\n backtrack(choices, state, n, res);\n return res.get(0);\n}\n</code></pre> climbing_stairs_backtrack.dart<pre><code>/* \u56de\u6eaf */\nvoid backtrack(List&lt;int&gt; choices, int state, int n, List&lt;int&gt; res) {\n // \u7576\u722c\u5230\u7b2c n \u968e\u6642\uff0c\u65b9\u6848\u6578\u91cf\u52a0 1\n if (state == n) {\n res[0]++;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (int choice in choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8d8a\u904e\u7b2c n \u968e\n if (state + choice &gt; n) continue;\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u6a13\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n List&lt;int&gt; choices = [1, 2]; // \u53ef\u9078\u64c7\u5411\u4e0a\u722c 1 \u968e\u6216 2 \u968e\n int state = 0; // \u5f9e\u7b2c 0 \u968e\u958b\u59cb\u722c\n List&lt;int&gt; res = [];\n res.add(0); // \u4f7f\u7528 res[0] \u8a18\u9304\u65b9\u6848\u6578\u91cf\n backtrack(choices, state, n, res);\n return res[0];\n}\n</code></pre> climbing_stairs_backtrack.rs<pre><code>/* \u56de\u6eaf */\nfn backtrack(choices: &amp;[i32], state: i32, n: i32, res: &amp;mut [i32]) {\n // \u7576\u722c\u5230\u7b2c n \u968e\u6642\uff0c\u65b9\u6848\u6578\u91cf\u52a0 1\n if state == n {\n res[0] = res[0] + 1;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for &amp;choice in choices {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8d8a\u904e\u7b2c n \u968e\n if state + choice &gt; n {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u6a13\u68af\uff1a\u56de\u6eaf */\nfn climbing_stairs_backtrack(n: usize) -&gt; i32 {\n let choices = vec![1, 2]; // \u53ef\u9078\u64c7\u5411\u4e0a\u722c 1 \u968e\u6216 2 \u968e\n let state = 0; // \u5f9e\u7b2c 0 \u968e\u958b\u59cb\u722c\n let mut res = Vec::new();\n res.push(0); // \u4f7f\u7528 res[0] \u8a18\u9304\u65b9\u6848\u6578\u91cf\n backtrack(&amp;choices, state, n as i32, &amp;mut res);\n res[0]\n}\n</code></pre> climbing_stairs_backtrack.c<pre><code>/* \u56de\u6eaf */\nvoid backtrack(int *choices, int state, int n, int *res, int len) {\n // \u7576\u722c\u5230\u7b2c n \u968e\u6642\uff0c\u65b9\u6848\u6578\u91cf\u52a0 1\n if (state == n)\n res[0]++;\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (int i = 0; i &lt; len; i++) {\n int choice = choices[i];\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8d8a\u904e\u7b2c n \u968e\n if (state + choice &gt; n)\n continue;\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n backtrack(choices, state + choice, n, res, len);\n // \u56de\u9000\n }\n}\n\n/* \u722c\u6a13\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\n int choices[2] = {1, 2}; // \u53ef\u9078\u64c7\u5411\u4e0a\u722c 1 \u968e\u6216 2 \u968e\n int state = 0; // \u5f9e\u7b2c 0 \u968e\u958b\u59cb\u722c\n int *res = (int *)malloc(sizeof(int));\n *res = 0; // \u4f7f\u7528 res[0] \u8a18\u9304\u65b9\u6848\u6578\u91cf\n int len = sizeof(choices) / sizeof(int);\n backtrack(choices, state, n, res, len);\n int result = *res;\n free(res);\n return result;\n}\n</code></pre> climbing_stairs_backtrack.kt<pre><code>/* \u56de\u6eaf */\nfun backtrack(\n choices: MutableList&lt;Int&gt;,\n state: Int,\n n: Int,\n res: MutableList&lt;Int&gt;\n) {\n // \u7576\u722c\u5230\u7b2c n \u968e\u6642\uff0c\u65b9\u6848\u6578\u91cf\u52a0 1\n if (state == n)\n res[0] = res[0] + 1\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (choice in choices) {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8d8a\u904e\u7b2c n \u968e\n if (state + choice &gt; n) continue\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n backtrack(choices, state + choice, n, res)\n // \u56de\u9000\n }\n}\n\n/* \u722c\u6a13\u68af\uff1a\u56de\u6eaf */\nfun climbingStairsBacktrack(n: Int): Int {\n val choices = mutableListOf(1, 2) // \u53ef\u9078\u64c7\u5411\u4e0a\u722c 1 \u968e\u6216 2 \u968e\n val state = 0 // \u5f9e\u7b2c 0 \u968e\u958b\u59cb\u722c\n val res = mutableListOf&lt;Int&gt;()\n res.add(0) // \u4f7f\u7528 res[0] \u8a18\u9304\u65b9\u6848\u6578\u91cf\n backtrack(choices, state, n, res)\n return res[0]\n}\n</code></pre> climbing_stairs_backtrack.rb<pre><code>[class]{}-[func]{backtrack}\n\n[class]{}-[func]{climbing_stairs_backtrack}\n</code></pre> climbing_stairs_backtrack.zig<pre><code>// \u56de\u6eaf\nfn backtrack(choices: []i32, state: i32, n: i32, res: std.ArrayList(i32)) void {\n // \u7576\u722c\u5230\u7b2c n \u968e\u6642\uff0c\u65b9\u6848\u6578\u91cf\u52a0 1\n if (state == n) {\n res.items[0] = res.items[0] + 1;\n }\n // \u8d70\u8a2a\u6240\u6709\u9078\u64c7\n for (choices) |choice| {\n // \u526a\u679d\uff1a\u4e0d\u5141\u8a31\u8d8a\u904e\u7b2c n \u968e\n if (state + choice &gt; n) {\n continue;\n }\n // \u5617\u8a66\uff1a\u505a\u51fa\u9078\u64c7\uff0c\u66f4\u65b0\u72c0\u614b\n backtrack(choices, state + choice, n, res);\n // \u56de\u9000\n }\n}\n\n// \u722c\u6a13\u68af\uff1a\u56de\u6eaf\nfn climbingStairsBacktrack(n: usize) !i32 {\n var choices = [_]i32{ 1, 2 }; // \u53ef\u9078\u64c7\u5411\u4e0a\u722c 1 \u968e\u6216 2 \u968e\n var state: i32 = 0; // \u5f9e\u7b2c 0 \u968e\u958b\u59cb\u722c\n var res = std.ArrayList(i32).init(std.heap.page_allocator);\n defer res.deinit();\n try res.append(0); // \u4f7f\u7528 res[0] \u8a18\u9304\u65b9\u6848\u6578\u91cf\n backtrack(&amp;choices, state, @intCast(n), res);\n return res.items[0];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1411","title":"14.1.1 \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u5c0b","text":"<p>\u56de\u6eaf\u6f14\u7b97\u6cd5\u901a\u5e38\u4e26\u4e0d\u986f\u5f0f\u5730\u5c0d\u554f\u984c\u9032\u884c\u62c6\u89e3\uff0c\u800c\u662f\u5c07\u6c42\u89e3\u554f\u984c\u770b\u4f5c\u4e00\u7cfb\u5217\u6c7a\u7b56\u6b65\u9a5f\uff0c\u900f\u904e\u8a66\u63a2\u548c\u526a\u679d\uff0c\u641c\u5c0b\u6240\u6709\u53ef\u80fd\u7684\u89e3\u3002</p> <p>\u6211\u5011\u53ef\u4ee5\u5617\u8a66\u5f9e\u554f\u984c\u5206\u89e3\u7684\u89d2\u5ea6\u5206\u6790\u9019\u9053\u984c\u3002\u8a2d\u722c\u5230\u7b2c \\(i\\) \u968e\u5171\u6709 \\(dp[i]\\) \u7a2e\u65b9\u6848\uff0c\u90a3\u9ebc \\(dp[i]\\) \u5c31\u662f\u539f\u554f\u984c\uff0c\u5176\u5b50\u554f\u984c\u5305\u62ec\uff1a</p> \\[ dp[i-1], dp[i-2], \\dots, dp[2], dp[1] \\] <p>\u7531\u65bc\u6bcf\u8f2a\u53ea\u80fd\u4e0a \\(1\\) \u968e\u6216 \\(2\\) \u968e\uff0c\u56e0\u6b64\u7576\u6211\u5011\u7ad9\u5728\u7b2c \\(i\\) \u968e\u6a13\u68af\u4e0a\u6642\uff0c\u4e0a\u4e00\u8f2a\u53ea\u53ef\u80fd\u7ad9\u5728\u7b2c \\(i - 1\\) \u968e\u6216\u7b2c \\(i - 2\\) \u968e\u4e0a\u3002\u63db\u53e5\u8a71\u8aaa\uff0c\u6211\u5011\u53ea\u80fd\u5f9e\u7b2c \\(i -1\\) \u968e\u6216\u7b2c \\(i - 2\\) \u968e\u9081\u5411\u7b2c \\(i\\) \u968e\u3002</p> <p>\u7531\u6b64\u4fbf\u53ef\u5f97\u51fa\u4e00\u500b\u91cd\u8981\u63a8\u8ad6\uff1a\u722c\u5230\u7b2c \\(i - 1\\) \u968e\u7684\u65b9\u6848\u6578\u52a0\u4e0a\u722c\u5230\u7b2c \\(i - 2\\) \u968e\u7684\u65b9\u6848\u6578\u5c31\u7b49\u65bc\u722c\u5230\u7b2c \\(i\\) \u968e\u7684\u65b9\u6848\u6578\u3002\u516c\u5f0f\u5982\u4e0b\uff1a</p> \\[ dp[i] = dp[i-1] + dp[i-2] \\] <p>\u9019\u610f\u5473\u8457\u5728\u722c\u6a13\u68af\u554f\u984c\u4e2d\uff0c\u5404\u500b\u5b50\u554f\u984c\u4e4b\u9593\u5b58\u5728\u905e\u63a8\u95dc\u4fc2\uff0c\u539f\u554f\u984c\u7684\u89e3\u53ef\u4ee5\u7531\u5b50\u554f\u984c\u7684\u89e3\u69cb\u5efa\u5f97\u4f86\u3002\u5716 14-2 \u5c55\u793a\u4e86\u8a72\u905e\u63a8\u95dc\u4fc2\u3002</p> <p></p> <p> \u5716 14-2 \u00a0 \u65b9\u6848\u6578\u91cf\u905e\u63a8\u95dc\u4fc2 </p> <p>\u6211\u5011\u53ef\u4ee5\u6839\u64da\u905e\u63a8\u516c\u5f0f\u5f97\u5230\u66b4\u529b\u641c\u5c0b\u89e3\u6cd5\u3002\u4ee5 \\(dp[n]\\) \u70ba\u8d77\u59cb\u9ede\uff0c\u905e\u8ff4\u5730\u5c07\u4e00\u500b\u8f03\u5927\u554f\u984c\u62c6\u89e3\u70ba\u5169\u500b\u8f03\u5c0f\u554f\u984c\u7684\u548c\uff0c\u76f4\u81f3\u5230\u9054\u6700\u5c0f\u5b50\u554f\u984c \\(dp[1]\\) \u548c \\(dp[2]\\) \u6642\u8fd4\u56de\u3002\u5176\u4e2d\uff0c\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u5373 \\(dp[1] = 1\\)\u3001\\(dp[2] = 2\\) \uff0c\u8868\u793a\u722c\u5230\u7b2c \\(1\\)\u3001\\(2\\) \u968e\u5206\u5225\u6709 \\(1\\)\u3001\\(2\\) \u7a2e\u65b9\u6848\u3002</p> <p>\u89c0\u5bdf\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff0c\u5b83\u548c\u6a19\u6e96\u56de\u6eaf\u7a0b\u5f0f\u78bc\u90fd\u5c6c\u65bc\u6df1\u5ea6\u512a\u5148\u641c\u5c0b\uff0c\u4f46\u66f4\u52a0\u7c21\u6f54\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig climbing_stairs_dfs.py<pre><code>def dfs(i: int) -&gt; int:\n \"\"\"\u641c\u5c0b\"\"\"\n # \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 or i == 2:\n return i\n # dp[i] = dp[i-1] + dp[i-2]\n count = dfs(i - 1) + dfs(i - 2)\n return count\n\ndef climbing_stairs_dfs(n: int) -&gt; int:\n \"\"\"\u722c\u6a13\u68af\uff1a\u641c\u5c0b\"\"\"\n return dfs(n)\n</code></pre> climbing_stairs_dfs.cpp<pre><code>/* \u641c\u5c0b */\nint dfs(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u6a13\u68af\uff1a\u641c\u5c0b */\nint climbingStairsDFS(int n) {\n return dfs(n);\n}\n</code></pre> climbing_stairs_dfs.java<pre><code>/* \u641c\u5c0b */\nint dfs(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u6a13\u68af\uff1a\u641c\u5c0b */\nint climbingStairsDFS(int n) {\n return dfs(n);\n}\n</code></pre> climbing_stairs_dfs.cs<pre><code>/* \u641c\u5c0b */\nint DFS(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = DFS(i - 1) + DFS(i - 2);\n return count;\n}\n\n/* \u722c\u6a13\u68af\uff1a\u641c\u5c0b */\nint ClimbingStairsDFS(int n) {\n return DFS(n);\n}\n</code></pre> climbing_stairs_dfs.go<pre><code>/* \u641c\u5c0b */\nfunc dfs(i int) int {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 {\n return i\n }\n // dp[i] = dp[i-1] + dp[i-2]\n count := dfs(i-1) + dfs(i-2)\n return count\n}\n\n/* \u722c\u6a13\u68af\uff1a\u641c\u5c0b */\nfunc climbingStairsDFS(n int) int {\n return dfs(n)\n}\n</code></pre> climbing_stairs_dfs.swift<pre><code>/* \u641c\u5c0b */\nfunc dfs(i: Int) -&gt; Int {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 {\n return i\n }\n // dp[i] = dp[i-1] + dp[i-2]\n let count = dfs(i: i - 1) + dfs(i: i - 2)\n return count\n}\n\n/* \u722c\u6a13\u68af\uff1a\u641c\u5c0b */\nfunc climbingStairsDFS(n: Int) -&gt; Int {\n dfs(i: n)\n}\n</code></pre> climbing_stairs_dfs.js<pre><code>/* \u641c\u5c0b */\nfunction dfs(i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i === 1 || i === 2) return i;\n // dp[i] = dp[i-1] + dp[i-2]\n const count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u6a13\u68af\uff1a\u641c\u5c0b */\nfunction climbingStairsDFS(n) {\n return dfs(n);\n}\n</code></pre> climbing_stairs_dfs.ts<pre><code>/* \u641c\u5c0b */\nfunction dfs(i: number): number {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i === 1 || i === 2) return i;\n // dp[i] = dp[i-1] + dp[i-2]\n const count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u6a13\u68af\uff1a\u641c\u5c0b */\nfunction climbingStairsDFS(n: number): number {\n return dfs(n);\n}\n</code></pre> climbing_stairs_dfs.dart<pre><code>/* \u641c\u5c0b */\nint dfs(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2) return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u6a13\u68af\uff1a\u641c\u5c0b */\nint climbingStairsDFS(int n) {\n return dfs(n);\n}\n</code></pre> climbing_stairs_dfs.rs<pre><code>/* \u641c\u5c0b */\nfn dfs(i: usize) -&gt; i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 {\n return i as i32;\n }\n // dp[i] = dp[i-1] + dp[i-2]\n let count = dfs(i - 1) + dfs(i - 2);\n count\n}\n\n/* \u722c\u6a13\u68af\uff1a\u641c\u5c0b */\nfn climbing_stairs_dfs(n: usize) -&gt; i32 {\n dfs(n)\n}\n</code></pre> climbing_stairs_dfs.c<pre><code>/* \u641c\u5c0b */\nint dfs(int i) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n/* \u722c\u6a13\u68af\uff1a\u641c\u5c0b */\nint climbingStairsDFS(int n) {\n return dfs(n);\n}\n</code></pre> climbing_stairs_dfs.kt<pre><code>/* \u641c\u5c0b */\nfun dfs(i: Int): Int {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2) return i\n // dp[i] = dp[i-1] + dp[i-2]\n val count = dfs(i - 1) + dfs(i - 2)\n return count\n}\n\n/* \u722c\u6a13\u68af\uff1a\u641c\u5c0b */\nfun climbingStairsDFS(n: Int): Int {\n return dfs(n)\n}\n</code></pre> climbing_stairs_dfs.rb<pre><code>[class]{}-[func]{dfs}\n\n[class]{}-[func]{climbing_stairs_dfs}\n</code></pre> climbing_stairs_dfs.zig<pre><code>// \u641c\u5c0b\nfn dfs(i: usize) i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 or i == 2) {\n return @intCast(i);\n }\n // dp[i] = dp[i-1] + dp[i-2]\n var count = dfs(i - 1) + dfs(i - 2);\n return count;\n}\n\n// \u722c\u6a13\u68af\uff1a\u641c\u5c0b\nfn climbingStairsDFS(comptime n: usize) i32 {\n return dfs(n);\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 14-3 \u5c55\u793a\u4e86\u66b4\u529b\u641c\u5c0b\u5f62\u6210\u7684\u905e\u8ff4\u6a39\u3002\u5c0d\u65bc\u554f\u984c \\(dp[n]\\) \uff0c\u5176\u905e\u8ff4\u6a39\u7684\u6df1\u5ea6\u70ba \\(n\\) \uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(2^n)\\) \u3002\u6307\u6578\u968e\u5c6c\u65bc\u7206\u70b8\u5f0f\u589e\u9577\uff0c\u5982\u679c\u6211\u5011\u8f38\u5165\u4e00\u500b\u6bd4\u8f03\u5927\u7684 \\(n\\) \uff0c\u5247\u6703\u9677\u5165\u6f2b\u9577\u7684\u7b49\u5f85\u4e4b\u4e2d\u3002</p> <p></p> <p> \u5716 14-3 \u00a0 \u722c\u6a13\u68af\u5c0d\u61c9\u905e\u8ff4\u6a39 </p> <p>\u89c0\u5bdf\u5716 14-3 \uff0c\u6307\u6578\u968e\u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f\u201c\u91cd\u758a\u5b50\u554f\u984c\u201d\u5c0e\u81f4\u7684\u3002\u4f8b\u5982 \\(dp[9]\\) \u88ab\u5206\u89e3\u70ba \\(dp[8]\\) \u548c \\(dp[7]\\) \uff0c\\(dp[8]\\) \u88ab\u5206\u89e3\u70ba \\(dp[7]\\) \u548c \\(dp[6]\\) \uff0c\u5169\u8005\u90fd\u5305\u542b\u5b50\u554f\u984c \\(dp[7]\\) \u3002</p> <p>\u4ee5\u6b64\u985e\u63a8\uff0c\u5b50\u554f\u984c\u4e2d\u5305\u542b\u66f4\u5c0f\u7684\u91cd\u758a\u5b50\u554f\u984c\uff0c\u5b50\u5b50\u5b6b\u5b6b\u7121\u7aae\u76e1\u4e5f\u3002\u7d55\u5927\u90e8\u5206\u8a08\u7b97\u8cc7\u6e90\u90fd\u6d6a\u8cbb\u5728\u9019\u4e9b\u91cd\u758a\u7684\u5b50\u554f\u984c\u4e0a\u3002</p>"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1412","title":"14.1.2 \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b","text":"<p>\u70ba\u4e86\u63d0\u5347\u6f14\u7b97\u6cd5\u6548\u7387\uff0c\u6211\u5011\u5e0c\u671b\u6240\u6709\u7684\u91cd\u758a\u5b50\u554f\u984c\u90fd\u53ea\u88ab\u8a08\u7b97\u4e00\u6b21\u3002\u70ba\u6b64\uff0c\u6211\u5011\u5ba3\u544a\u4e00\u500b\u9663\u5217 <code>mem</code> \u4f86\u8a18\u9304\u6bcf\u500b\u5b50\u554f\u984c\u7684\u89e3\uff0c\u4e26\u5728\u641c\u5c0b\u904e\u7a0b\u4e2d\u5c07\u91cd\u758a\u5b50\u554f\u984c\u526a\u679d\u3002</p> <ol> <li>\u7576\u9996\u6b21\u8a08\u7b97 \\(dp[i]\\) \u6642\uff0c\u6211\u5011\u5c07\u5176\u8a18\u9304\u81f3 <code>mem[i]</code> \uff0c\u4ee5\u4fbf\u4e4b\u5f8c\u4f7f\u7528\u3002</li> <li>\u7576\u518d\u6b21\u9700\u8981\u8a08\u7b97 \\(dp[i]\\) \u6642\uff0c\u6211\u5011\u4fbf\u53ef\u76f4\u63a5\u5f9e <code>mem[i]</code> \u4e2d\u7372\u53d6\u7d50\u679c\uff0c\u5f9e\u800c\u907f\u514d\u91cd\u8907\u8a08\u7b97\u8a72\u5b50\u554f\u984c\u3002</li> </ol> <p>\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig climbing_stairs_dfs_mem.py<pre><code>def dfs(i: int, mem: list[int]) -&gt; int:\n \"\"\"\u8a18\u61b6\u5316\u641c\u5c0b\"\"\"\n # \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 or i == 2:\n return i\n # \u82e5\u5b58\u5728\u8a18\u9304 dp[i] \uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u4e4b\n if mem[i] != -1:\n return mem[i]\n # dp[i] = dp[i-1] + dp[i-2]\n count = dfs(i - 1, mem) + dfs(i - 2, mem)\n # \u8a18\u9304 dp[i]\n mem[i] = count\n return count\n\ndef climbing_stairs_dfs_mem(n: int) -&gt; int:\n \"\"\"\u722c\u6a13\u68af\uff1a\u8a18\u61b6\u5316\u641c\u5c0b\"\"\"\n # mem[i] \u8a18\u9304\u722c\u5230\u7b2c i \u968e\u7684\u65b9\u6848\u7e3d\u6578\uff0c-1 \u4ee3\u8868\u7121\u8a18\u9304\n mem = [-1] * (n + 1)\n return dfs(n, mem)\n</code></pre> climbing_stairs_dfs_mem.cpp<pre><code>/* \u8a18\u61b6\u5316\u641c\u5c0b */\nint dfs(int i, vector&lt;int&gt; &amp;mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // \u82e5\u5b58\u5728\u8a18\u9304 dp[i] \uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1)\n return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8a18\u9304 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u6a13\u68af\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint climbingStairsDFSMem(int n) {\n // mem[i] \u8a18\u9304\u722c\u5230\u7b2c i \u968e\u7684\u65b9\u6848\u7e3d\u6578\uff0c-1 \u4ee3\u8868\u7121\u8a18\u9304\n vector&lt;int&gt; mem(n + 1, -1);\n return dfs(n, mem);\n}\n</code></pre> climbing_stairs_dfs_mem.java<pre><code>/* \u8a18\u61b6\u5316\u641c\u5c0b */\nint dfs(int i, int[] mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // \u82e5\u5b58\u5728\u8a18\u9304 dp[i] \uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1)\n return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8a18\u9304 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u6a13\u68af\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint climbingStairsDFSMem(int n) {\n // mem[i] \u8a18\u9304\u722c\u5230\u7b2c i \u968e\u7684\u65b9\u6848\u7e3d\u6578\uff0c-1 \u4ee3\u8868\u7121\u8a18\u9304\n int[] mem = new int[n + 1];\n Arrays.fill(mem, -1);\n return dfs(n, mem);\n}\n</code></pre> climbing_stairs_dfs_mem.cs<pre><code>/* \u8a18\u61b6\u5316\u641c\u5c0b */\nint DFS(int i, int[] mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // \u82e5\u5b58\u5728\u8a18\u9304 dp[i] \uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1)\n return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = DFS(i - 1, mem) + DFS(i - 2, mem);\n // \u8a18\u9304 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u6a13\u68af\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint ClimbingStairsDFSMem(int n) {\n // mem[i] \u8a18\u9304\u722c\u5230\u7b2c i \u968e\u7684\u65b9\u6848\u7e3d\u6578\uff0c-1 \u4ee3\u8868\u7121\u8a18\u9304\n int[] mem = new int[n + 1];\n Array.Fill(mem, -1);\n return DFS(n, mem);\n}\n</code></pre> climbing_stairs_dfs_mem.go<pre><code>/* \u8a18\u61b6\u5316\u641c\u5c0b */\nfunc dfsMem(i int, mem []int) int {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 {\n return i\n }\n // \u82e5\u5b58\u5728\u8a18\u9304 dp[i] \uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u4e4b\n if mem[i] != -1 {\n return mem[i]\n }\n // dp[i] = dp[i-1] + dp[i-2]\n count := dfsMem(i-1, mem) + dfsMem(i-2, mem)\n // \u8a18\u9304 dp[i]\n mem[i] = count\n return count\n}\n\n/* \u722c\u6a13\u68af\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfunc climbingStairsDFSMem(n int) int {\n // mem[i] \u8a18\u9304\u722c\u5230\u7b2c i \u968e\u7684\u65b9\u6848\u7e3d\u6578\uff0c-1 \u4ee3\u8868\u7121\u8a18\u9304\n mem := make([]int, n+1)\n for i := range mem {\n mem[i] = -1\n }\n return dfsMem(n, mem)\n}\n</code></pre> climbing_stairs_dfs_mem.swift<pre><code>/* \u8a18\u61b6\u5316\u641c\u5c0b */\nfunc dfs(i: Int, mem: inout [Int]) -&gt; Int {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 {\n return i\n }\n // \u82e5\u5b58\u5728\u8a18\u9304 dp[i] \uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u4e4b\n if mem[i] != -1 {\n return mem[i]\n }\n // dp[i] = dp[i-1] + dp[i-2]\n let count = dfs(i: i - 1, mem: &amp;mem) + dfs(i: i - 2, mem: &amp;mem)\n // \u8a18\u9304 dp[i]\n mem[i] = count\n return count\n}\n\n/* \u722c\u6a13\u68af\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfunc climbingStairsDFSMem(n: Int) -&gt; Int {\n // mem[i] \u8a18\u9304\u722c\u5230\u7b2c i \u968e\u7684\u65b9\u6848\u7e3d\u6578\uff0c-1 \u4ee3\u8868\u7121\u8a18\u9304\n var mem = Array(repeating: -1, count: n + 1)\n return dfs(i: n, mem: &amp;mem)\n}\n</code></pre> climbing_stairs_dfs_mem.js<pre><code>/* \u8a18\u61b6\u5316\u641c\u5c0b */\nfunction dfs(i, mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i === 1 || i === 2) return i;\n // \u82e5\u5b58\u5728\u8a18\u9304 dp[i] \uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1) return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8a18\u9304 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u6a13\u68af\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfunction climbingStairsDFSMem(n) {\n // mem[i] \u8a18\u9304\u722c\u5230\u7b2c i \u968e\u7684\u65b9\u6848\u7e3d\u6578\uff0c-1 \u4ee3\u8868\u7121\u8a18\u9304\n const mem = new Array(n + 1).fill(-1);\n return dfs(n, mem);\n}\n</code></pre> climbing_stairs_dfs_mem.ts<pre><code>/* \u8a18\u61b6\u5316\u641c\u5c0b */\nfunction dfs(i: number, mem: number[]): number {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i === 1 || i === 2) return i;\n // \u82e5\u5b58\u5728\u8a18\u9304 dp[i] \uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1) return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8a18\u9304 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u6a13\u68af\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfunction climbingStairsDFSMem(n: number): number {\n // mem[i] \u8a18\u9304\u722c\u5230\u7b2c i \u968e\u7684\u65b9\u6848\u7e3d\u6578\uff0c-1 \u4ee3\u8868\u7121\u8a18\u9304\n const mem = new Array(n + 1).fill(-1);\n return dfs(n, mem);\n}\n</code></pre> climbing_stairs_dfs_mem.dart<pre><code>/* \u8a18\u61b6\u5316\u641c\u5c0b */\nint dfs(int i, List&lt;int&gt; mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2) return i;\n // \u82e5\u5b58\u5728\u8a18\u9304 dp[i] \uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1) return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8a18\u9304 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u6a13\u68af\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint climbingStairsDFSMem(int n) {\n // mem[i] \u8a18\u9304\u722c\u5230\u7b2c i \u968e\u7684\u65b9\u6848\u7e3d\u6578\uff0c-1 \u4ee3\u8868\u7121\u8a18\u9304\n List&lt;int&gt; mem = List.filled(n + 1, -1);\n return dfs(n, mem);\n}\n</code></pre> climbing_stairs_dfs_mem.rs<pre><code>/* \u8a18\u61b6\u5316\u641c\u5c0b */\nfn dfs(i: usize, mem: &amp;mut [i32]) -&gt; i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if i == 1 || i == 2 {\n return i as i32;\n }\n // \u82e5\u5b58\u5728\u8a18\u9304 dp[i] \uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u4e4b\n if mem[i] != -1 {\n return mem[i];\n }\n // dp[i] = dp[i-1] + dp[i-2]\n let count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8a18\u9304 dp[i]\n mem[i] = count;\n count\n}\n\n/* \u722c\u6a13\u68af\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfn climbing_stairs_dfs_mem(n: usize) -&gt; i32 {\n // mem[i] \u8a18\u9304\u722c\u5230\u7b2c i \u968e\u7684\u65b9\u6848\u7e3d\u6578\uff0c-1 \u4ee3\u8868\u7121\u8a18\u9304\n let mut mem = vec![-1; n + 1];\n dfs(n, &amp;mut mem)\n}\n</code></pre> climbing_stairs_dfs_mem.c<pre><code>/* \u8a18\u61b6\u5316\u641c\u5c0b */\nint dfs(int i, int *mem) {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2)\n return i;\n // \u82e5\u5b58\u5728\u8a18\u9304 dp[i] \uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1)\n return mem[i];\n // dp[i] = dp[i-1] + dp[i-2]\n int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8a18\u9304 dp[i]\n mem[i] = count;\n return count;\n}\n\n/* \u722c\u6a13\u68af\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint climbingStairsDFSMem(int n) {\n // mem[i] \u8a18\u9304\u722c\u5230\u7b2c i \u968e\u7684\u65b9\u6848\u7e3d\u6578\uff0c-1 \u4ee3\u8868\u7121\u8a18\u9304\n int *mem = (int *)malloc((n + 1) * sizeof(int));\n for (int i = 0; i &lt;= n; i++) {\n mem[i] = -1;\n }\n int result = dfs(n, mem);\n free(mem);\n return result;\n}\n</code></pre> climbing_stairs_dfs_mem.kt<pre><code>/* \u8a18\u61b6\u5316\u641c\u5c0b */\nfun dfs(i: Int, mem: IntArray): Int {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 || i == 2) return i\n // \u82e5\u5b58\u5728\u8a18\u9304 dp[i] \uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1) return mem[i]\n // dp[i] = dp[i-1] + dp[i-2]\n val count = dfs(i - 1, mem) + dfs(i - 2, mem)\n // \u8a18\u9304 dp[i]\n mem[i] = count\n return count\n}\n\n/* \u722c\u6a13\u68af\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfun climbingStairsDFSMem(n: Int): Int {\n // mem[i] \u8a18\u9304\u722c\u5230\u7b2c i \u968e\u7684\u65b9\u6848\u7e3d\u6578\uff0c-1 \u4ee3\u8868\u7121\u8a18\u9304\n val mem = IntArray(n + 1)\n mem.fill(-1)\n return dfs(n, mem)\n}\n</code></pre> climbing_stairs_dfs_mem.rb<pre><code>[class]{}-[func]{dfs}\n\n[class]{}-[func]{climbing_stairs_dfs_mem}\n</code></pre> climbing_stairs_dfs_mem.zig<pre><code>// \u8a18\u61b6\u5316\u641c\u5c0b\nfn dfs(i: usize, mem: []i32) i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (i == 1 or i == 2) {\n return @intCast(i);\n }\n // \u82e5\u5b58\u5728\u8a18\u9304 dp[i] \uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u4e4b\n if (mem[i] != -1) {\n return mem[i];\n }\n // dp[i] = dp[i-1] + dp[i-2]\n var count = dfs(i - 1, mem) + dfs(i - 2, mem);\n // \u8a18\u9304 dp[i]\n mem[i] = count;\n return count;\n}\n\n// \u722c\u6a13\u68af\uff1a\u8a18\u61b6\u5316\u641c\u5c0b\nfn climbingStairsDFSMem(comptime n: usize) i32 {\n // mem[i] \u8a18\u9304\u722c\u5230\u7b2c i \u968e\u7684\u65b9\u6848\u7e3d\u6578\uff0c-1 \u4ee3\u8868\u7121\u8a18\u9304\n var mem = [_]i32{ -1 } ** (n + 1);\n return dfs(n, &amp;mem);\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u89c0\u5bdf\u5716 14-4 \uff0c\u7d93\u904e\u8a18\u61b6\u5316\u8655\u7406\u5f8c\uff0c\u6240\u6709\u91cd\u758a\u5b50\u554f\u984c\u90fd\u53ea\u9700\u8a08\u7b97\u4e00\u6b21\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u6700\u4f73\u5316\u81f3 \\(O(n)\\) \uff0c\u9019\u662f\u4e00\u500b\u5de8\u5927\u7684\u98db\u8e8d\u3002</p> <p></p> <p> \u5716 14-4 \u00a0 \u8a18\u61b6\u5316\u641c\u5c0b\u5c0d\u61c9\u905e\u8ff4\u6a39 </p>"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1413","title":"14.1.3 \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52d5\u614b\u898f\u5283","text":"<p>\u8a18\u61b6\u5316\u641c\u5c0b\u662f\u4e00\u7a2e\u201c\u5f9e\u9802\u81f3\u5e95\u201d\u7684\u65b9\u6cd5\uff1a\u6211\u5011\u5f9e\u539f\u554f\u984c\uff08\u6839\u7bc0\u9ede\uff09\u958b\u59cb\uff0c\u905e\u8ff4\u5730\u5c07\u8f03\u5927\u5b50\u554f\u984c\u5206\u89e3\u70ba\u8f03\u5c0f\u5b50\u554f\u984c\uff0c\u76f4\u81f3\u89e3\u5df2\u77e5\u7684\u6700\u5c0f\u5b50\u554f\u984c\uff08\u8449\u7bc0\u9ede\uff09\u3002\u4e4b\u5f8c\uff0c\u900f\u904e\u56de\u6eaf\u9010\u5c64\u6536\u96c6\u5b50\u554f\u984c\u7684\u89e3\uff0c\u69cb\u5efa\u51fa\u539f\u554f\u984c\u7684\u89e3\u3002</p> <p>\u8207\u4e4b\u76f8\u53cd\uff0c\u52d5\u614b\u898f\u5283\u662f\u4e00\u7a2e\u201c\u5f9e\u5e95\u81f3\u9802\u201d\u7684\u65b9\u6cd5\uff1a\u5f9e\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\u958b\u59cb\uff0c\u8fed\u4ee3\u5730\u69cb\u5efa\u66f4\u5927\u5b50\u554f\u984c\u7684\u89e3\uff0c\u76f4\u81f3\u5f97\u5230\u539f\u554f\u984c\u7684\u89e3\u3002</p> <p>\u7531\u65bc\u52d5\u614b\u898f\u5283\u4e0d\u5305\u542b\u56de\u6eaf\u904e\u7a0b\uff0c\u56e0\u6b64\u53ea\u9700\u4f7f\u7528\u8ff4\u5708\u8fed\u4ee3\u5be6\u73fe\uff0c\u7121\u9808\u4f7f\u7528\u905e\u8ff4\u3002\u5728\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u521d\u59cb\u5316\u4e00\u500b\u9663\u5217 <code>dp</code> \u4f86\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\uff0c\u5b83\u8d77\u5230\u4e86\u8207\u8a18\u61b6\u5316\u641c\u5c0b\u4e2d\u9663\u5217 <code>mem</code> \u76f8\u540c\u7684\u8a18\u9304\u4f5c\u7528\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig climbing_stairs_dp.py<pre><code>def climbing_stairs_dp(n: int) -&gt; int:\n \"\"\"\u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283\"\"\"\n if n == 1 or n == 2:\n return n\n # \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n dp = [0] * (n + 1)\n # \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1], dp[2] = 1, 2\n # \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for i in range(3, n + 1):\n dp[i] = dp[i - 1] + dp[i - 2]\n return dp[n]\n</code></pre> climbing_stairs_dp.cpp<pre><code>/* \u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nint climbingStairsDP(int n) {\n if (n == 1 || n == 2)\n return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n vector&lt;int&gt; dp(n + 1);\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n</code></pre> climbing_stairs_dp.java<pre><code>/* \u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nint climbingStairsDP(int n) {\n if (n == 1 || n == 2)\n return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n int[] dp = new int[n + 1];\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n</code></pre> climbing_stairs_dp.cs<pre><code>/* \u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nint ClimbingStairsDP(int n) {\n if (n == 1 || n == 2)\n return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n int[] dp = new int[n + 1];\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n</code></pre> climbing_stairs_dp.go<pre><code>/* \u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nfunc climbingStairsDP(n int) int {\n if n == 1 || n == 2 {\n return n\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n dp := make([]int, n+1)\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = 1\n dp[2] = 2\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for i := 3; i &lt;= n; i++ {\n dp[i] = dp[i-1] + dp[i-2]\n }\n return dp[n]\n}\n</code></pre> climbing_stairs_dp.swift<pre><code>/* \u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nfunc climbingStairsDP(n: Int) -&gt; Int {\n if n == 1 || n == 2 {\n return n\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n var dp = Array(repeating: 0, count: n + 1)\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = 1\n dp[2] = 2\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for i in 3 ... n {\n dp[i] = dp[i - 1] + dp[i - 2]\n }\n return dp[n]\n}\n</code></pre> climbing_stairs_dp.js<pre><code>/* \u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nfunction climbingStairsDP(n) {\n if (n === 1 || n === 2) return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n const dp = new Array(n + 1).fill(-1);\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (let i = 3; i &lt;= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n</code></pre> climbing_stairs_dp.ts<pre><code>/* \u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nfunction climbingStairsDP(n: number): number {\n if (n === 1 || n === 2) return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n const dp = new Array(n + 1).fill(-1);\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (let i = 3; i &lt;= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n</code></pre> climbing_stairs_dp.dart<pre><code>/* \u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nint climbingStairsDP(int n) {\n if (n == 1 || n == 2) return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n List&lt;int&gt; dp = List.filled(n + 1, 0);\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n</code></pre> climbing_stairs_dp.rs<pre><code>/* \u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nfn climbing_stairs_dp(n: usize) -&gt; i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if n == 1 || n == 2 {\n return n as i32;\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n let mut dp = vec![-1; n + 1];\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for i in 3..=n {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n dp[n]\n}\n</code></pre> climbing_stairs_dp.c<pre><code>/* \u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nint climbingStairsDP(int n) {\n if (n == 1 || n == 2)\n return n;\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n int *dp = (int *)malloc((n + 1) * sizeof(int));\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (int i = 3; i &lt;= n; i++) {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n int result = dp[n];\n free(dp);\n return result;\n}\n</code></pre> climbing_stairs_dp.kt<pre><code>/* \u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283 */\nfun climbingStairsDP(n: Int): Int {\n if (n == 1 || n == 2) return n\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n val dp = IntArray(n + 1)\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = 1\n dp[2] = 2\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (i in 3..n) {\n dp[i] = dp[i - 1] + dp[i - 2]\n }\n return dp[n]\n}\n</code></pre> climbing_stairs_dp.rb<pre><code>[class]{}-[func]{climbing_stairs_dp}\n</code></pre> climbing_stairs_dp.zig<pre><code>// \u722c\u6a13\u68af\uff1a\u52d5\u614b\u898f\u5283\nfn climbingStairsDP(comptime n: usize) i32 {\n // \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\n if (n == 1 or n == 2) {\n return @intCast(n);\n }\n // \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u65bc\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\n var dp = [_]i32{-1} ** (n + 1);\n // \u521d\u59cb\u72c0\u614b\uff1a\u9810\u8a2d\u6700\u5c0f\u5b50\u554f\u984c\u7684\u89e3\n dp[1] = 1;\n dp[2] = 2;\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for (3..n + 1) |i| {\n dp[i] = dp[i - 1] + dp[i - 2];\n }\n return dp[n];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 14-5 \u6a21\u64ec\u4e86\u4ee5\u4e0a\u7a0b\u5f0f\u78bc\u7684\u57f7\u884c\u904e\u7a0b\u3002</p> <p></p> <p> \u5716 14-5 \u00a0 \u722c\u6a13\u68af\u7684\u52d5\u614b\u898f\u5283\u904e\u7a0b </p> <p>\u8207\u56de\u6eaf\u6f14\u7b97\u6cd5\u4e00\u6a23\uff0c\u52d5\u614b\u898f\u5283\u4e5f\u4f7f\u7528\u201c\u72c0\u614b\u201d\u6982\u5ff5\u4f86\u8868\u793a\u554f\u984c\u6c42\u89e3\u7684\u7279\u5b9a\u968e\u6bb5\uff0c\u6bcf\u500b\u72c0\u614b\u90fd\u5c0d\u61c9\u4e00\u500b\u5b50\u554f\u984c\u4ee5\u53ca\u76f8\u61c9\u7684\u5340\u57df\u6027\u6700\u512a\u89e3\u3002\u4f8b\u5982\uff0c\u722c\u6a13\u68af\u554f\u984c\u7684\u72c0\u614b\u5b9a\u7fa9\u70ba\u7576\u524d\u6240\u5728\u6a13\u68af\u968e\u6578 \\(i\\) \u3002</p> <p>\u6839\u64da\u4ee5\u4e0a\u5167\u5bb9\uff0c\u6211\u5011\u53ef\u4ee5\u7e3d\u7d50\u51fa\u52d5\u614b\u898f\u5283\u7684\u5e38\u7528\u8853\u8a9e\u3002</p> <ul> <li>\u5c07\u9663\u5217 <code>dp</code> \u7a31\u70ba dp \u8868\uff0c\\(dp[i]\\) \u8868\u793a\u72c0\u614b \\(i\\) \u5c0d\u61c9\u5b50\u554f\u984c\u7684\u89e3\u3002</li> <li>\u5c07\u6700\u5c0f\u5b50\u554f\u984c\u5c0d\u61c9\u7684\u72c0\u614b\uff08\u7b2c \\(1\\) \u968e\u548c\u7b2c \\(2\\) \u968e\u6a13\u68af\uff09\u7a31\u70ba\u521d\u59cb\u72c0\u614b\u3002</li> <li>\u5c07\u905e\u63a8\u516c\u5f0f \\(dp[i] = dp[i-1] + dp[i-2]\\) \u7a31\u70ba\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\u3002</li> </ul>"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1414","title":"14.1.4 \u00a0 \u7a7a\u9593\u6700\u4f73\u5316","text":"<p>\u7d30\u5fc3\u7684\u8b80\u8005\u53ef\u80fd\u767c\u73fe\u4e86\uff0c\u7531\u65bc \\(dp[i]\\) \u53ea\u8207 \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u6709\u95dc\uff0c\u56e0\u6b64\u6211\u5011\u7121\u9808\u4f7f\u7528\u4e00\u500b\u9663\u5217 <code>dp</code> \u4f86\u5132\u5b58\u6240\u6709\u5b50\u554f\u984c\u7684\u89e3\uff0c\u800c\u53ea\u9700\u5169\u500b\u8b8a\u6578\u6efe\u52d5\u524d\u9032\u5373\u53ef\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig climbing_stairs_dp.py<pre><code>def climbing_stairs_dp_comp(n: int) -&gt; int:\n \"\"\"\u722c\u6a13\u68af\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\"\"\"\n if n == 1 or n == 2:\n return n\n a, b = 1, 2\n for _ in range(3, n + 1):\n a, b = b, a + b\n return b\n</code></pre> climbing_stairs_dp.cpp<pre><code>/* \u722c\u6a13\u68af\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint climbingStairsDPComp(int n) {\n if (n == 1 || n == 2)\n return n;\n int a = 1, b = 2;\n for (int i = 3; i &lt;= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n</code></pre> climbing_stairs_dp.java<pre><code>/* \u722c\u6a13\u68af\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint climbingStairsDPComp(int n) {\n if (n == 1 || n == 2)\n return n;\n int a = 1, b = 2;\n for (int i = 3; i &lt;= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n</code></pre> climbing_stairs_dp.cs<pre><code>/* \u722c\u6a13\u68af\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint ClimbingStairsDPComp(int n) {\n if (n == 1 || n == 2)\n return n;\n int a = 1, b = 2;\n for (int i = 3; i &lt;= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n</code></pre> climbing_stairs_dp.go<pre><code>/* \u722c\u6a13\u68af\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc climbingStairsDPComp(n int) int {\n if n == 1 || n == 2 {\n return n\n }\n a, b := 1, 2\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5f9e\u8f03\u5c0f\u5b50\u554f\u984c\u9010\u6b65\u6c42\u89e3\u8f03\u5927\u5b50\u554f\u984c\n for i := 3; i &lt;= n; i++ {\n a, b = b, a+b\n }\n return b\n}\n</code></pre> climbing_stairs_dp.swift<pre><code>/* \u722c\u6a13\u68af\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc climbingStairsDPComp(n: Int) -&gt; Int {\n if n == 1 || n == 2 {\n return n\n }\n var a = 1\n var b = 2\n for _ in 3 ... n {\n (a, b) = (b, a + b)\n }\n return b\n}\n</code></pre> climbing_stairs_dp.js<pre><code>/* \u722c\u6a13\u68af\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction climbingStairsDPComp(n) {\n if (n === 1 || n === 2) return n;\n let a = 1,\n b = 2;\n for (let i = 3; i &lt;= n; i++) {\n const tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n</code></pre> climbing_stairs_dp.ts<pre><code>/* \u722c\u6a13\u68af\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction climbingStairsDPComp(n: number): number {\n if (n === 1 || n === 2) return n;\n let a = 1,\n b = 2;\n for (let i = 3; i &lt;= n; i++) {\n const tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n</code></pre> climbing_stairs_dp.dart<pre><code>/* \u722c\u6a13\u68af\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint climbingStairsDPComp(int n) {\n if (n == 1 || n == 2) return n;\n int a = 1, b = 2;\n for (int i = 3; i &lt;= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n</code></pre> climbing_stairs_dp.rs<pre><code>/* \u722c\u6a13\u68af\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfn climbing_stairs_dp_comp(n: usize) -&gt; i32 {\n if n == 1 || n == 2 {\n return n as i32;\n }\n let (mut a, mut b) = (1, 2);\n for _ in 3..=n {\n let tmp = b;\n b = a + b;\n a = tmp;\n }\n b\n}\n</code></pre> climbing_stairs_dp.c<pre><code>/* \u722c\u6a13\u68af\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint climbingStairsDPComp(int n) {\n if (n == 1 || n == 2)\n return n;\n int a = 1, b = 2;\n for (int i = 3; i &lt;= n; i++) {\n int tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n</code></pre> climbing_stairs_dp.kt<pre><code>/* \u722c\u6a13\u68af\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfun climbingStairsDPComp(n: Int): Int {\n if (n == 1 || n == 2) return n\n var a = 1\n var b = 2\n for (i in 3..n) {\n val temp = b\n b += a\n a = temp\n }\n return b\n}\n</code></pre> climbing_stairs_dp.rb<pre><code>[class]{}-[func]{climbing_stairs_dp_comp}\n</code></pre> climbing_stairs_dp.zig<pre><code>// \u722c\u6a13\u68af\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\nfn climbingStairsDPComp(comptime n: usize) i32 {\n if (n == 1 or n == 2) {\n return @intCast(n);\n }\n var a: i32 = 1;\n var b: i32 = 2;\n for (3..n + 1) |_| {\n var tmp = b;\n b = a + b;\n a = tmp;\n }\n return b;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u89c0\u5bdf\u4ee5\u4e0a\u7a0b\u5f0f\u78bc\uff0c\u7531\u65bc\u7701\u53bb\u4e86\u9663\u5217 <code>dp</code> \u4f54\u7528\u7684\u7a7a\u9593\uff0c\u56e0\u6b64\u7a7a\u9593\u8907\u96dc\u5ea6\u5f9e \\(O(n)\\) \u964d\u81f3 \\(O(1)\\) \u3002</p> <p>\u5728\u52d5\u614b\u898f\u5283\u554f\u984c\u4e2d\uff0c\u7576\u524d\u72c0\u614b\u5f80\u5f80\u50c5\u8207\u524d\u9762\u6709\u9650\u500b\u72c0\u614b\u6709\u95dc\uff0c\u9019\u6642\u6211\u5011\u53ef\u4ee5\u53ea\u4fdd\u7559\u5fc5\u8981\u7684\u72c0\u614b\uff0c\u900f\u904e\u201c\u964d\u7dad\u201d\u4f86\u7bc0\u7701\u8a18\u61b6\u9ad4\u7a7a\u9593\u3002\u9019\u7a2e\u7a7a\u9593\u6700\u4f73\u5316\u6280\u5de7\u88ab\u7a31\u70ba\u201c\u6efe\u52d5\u8b8a\u6578\u201d\u6216\u201c\u6efe\u52d5\u9663\u5217\u201d\u3002</p>"},{"location":"chapter_dynamic_programming/knapsack_problem/","title":"14.4 \u00a0 0-1 \u80cc\u5305\u554f\u984c","text":"<p>\u80cc\u5305\u554f\u984c\u662f\u4e00\u500b\u975e\u5e38\u597d\u7684\u52d5\u614b\u898f\u5283\u5165\u9580\u984c\u76ee\uff0c\u662f\u52d5\u614b\u898f\u5283\u4e2d\u6700\u5e38\u898b\u7684\u554f\u984c\u5f62\u5f0f\u3002\u5176\u5177\u6709\u5f88\u591a\u8b8a\u7a2e\uff0c\u4f8b\u5982 0-1 \u80cc\u5305\u554f\u984c\u3001\u5b8c\u5168\u80cc\u5305\u554f\u984c\u3001\u591a\u91cd\u80cc\u5305\u554f\u984c\u7b49\u3002</p> <p>\u5728\u672c\u7bc0\u4e2d\uff0c\u6211\u5011\u5148\u4f86\u6c42\u89e3\u6700\u5e38\u898b\u7684 0-1 \u80cc\u5305\u554f\u984c\u3002</p> <p>Question</p> <p>\u7d66\u5b9a \\(n\\) \u500b\u7269\u54c1\uff0c\u7b2c \\(i\\) \u500b\u7269\u54c1\u7684\u91cd\u91cf\u70ba \\(wgt[i-1]\\)\u3001\u50f9\u503c\u70ba \\(val[i-1]\\) \uff0c\u548c\u4e00\u500b\u5bb9\u91cf\u70ba \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u500b\u7269\u54c1\u53ea\u80fd\u9078\u64c7\u4e00\u6b21\uff0c\u554f\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u50f9\u503c\u3002</p> <p>\u89c0\u5bdf\u5716 14-17 \uff0c\u7531\u65bc\u7269\u54c1\u7de8\u865f \\(i\\) \u5f9e \\(1\\) \u958b\u59cb\u8a08\u6578\uff0c\u9663\u5217\u7d22\u5f15\u5f9e \\(0\\) \u958b\u59cb\u8a08\u6578\uff0c\u56e0\u6b64\u7269\u54c1 \\(i\\) \u5c0d\u61c9\u91cd\u91cf \\(wgt[i-1]\\) \u548c\u50f9\u503c \\(val[i-1]\\) \u3002</p> <p></p> <p> \u5716 14-17 \u00a0 0-1 \u80cc\u5305\u7684\u793a\u4f8b\u8cc7\u6599 </p> <p>\u6211\u5011\u53ef\u4ee5\u5c07 0-1 \u80cc\u5305\u554f\u984c\u770b\u4f5c\u4e00\u500b\u7531 \\(n\\) \u8f2a\u6c7a\u7b56\u7d44\u6210\u7684\u904e\u7a0b\uff0c\u5c0d\u65bc\u6bcf\u500b\u7269\u9ad4\u90fd\u6709\u4e0d\u653e\u5165\u548c\u653e\u5165\u5169\u7a2e\u6c7a\u7b56\uff0c\u56e0\u6b64\u8a72\u554f\u984c\u6eff\u8db3\u6c7a\u7b56\u6a39\u6a21\u578b\u3002</p> <p>\u8a72\u554f\u984c\u7684\u76ee\u6a19\u662f\u6c42\u89e3\u201c\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u50f9\u503c\u201d\uff0c\u56e0\u6b64\u8f03\u5927\u6a5f\u7387\u662f\u4e00\u500b\u52d5\u614b\u898f\u5283\u554f\u984c\u3002</p> <p>\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f2a\u7684\u6c7a\u7b56\uff0c\u5b9a\u7fa9\u72c0\u614b\uff0c\u5f9e\u800c\u5f97\u5230 \\(dp\\) \u8868</p> <p>\u5c0d\u65bc\u6bcf\u500b\u7269\u54c1\u4f86\u8aaa\uff0c\u4e0d\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u4e0d\u8b8a\uff1b\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u6e1b\u5c0f\u3002\u7531\u6b64\u53ef\u5f97\u72c0\u614b\u5b9a\u7fa9\uff1a\u7576\u524d\u7269\u54c1\u7de8\u865f \\(i\\) \u548c\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u8a18\u70ba \\([i, c]\\) \u3002</p> <p>\u72c0\u614b \\([i, c]\\) \u5c0d\u61c9\u7684\u5b50\u554f\u984c\u70ba\uff1a\u524d \\(i\\) \u500b\u7269\u54c1\u5728\u5bb9\u91cf\u70ba \\(c\\) \u7684\u80cc\u5305\u4e2d\u7684\u6700\u5927\u50f9\u503c\uff0c\u8a18\u70ba \\(dp[i, c]\\) \u3002</p> <p>\u5f85\u6c42\u89e3\u7684\u662f \\(dp[n, cap]\\) \uff0c\u56e0\u6b64\u9700\u8981\u4e00\u500b\u5c3a\u5bf8\u70ba \\((n+1) \\times (cap+1)\\) \u7684\u4e8c\u7dad \\(dp\\) \u8868\u3002</p> <p>\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u512a\u5b50\u7d50\u69cb\uff0c\u9032\u800c\u63a8\u5c0e\u51fa\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b</p> <p>\u7576\u6211\u5011\u505a\u51fa\u7269\u54c1 \\(i\\) \u7684\u6c7a\u7b56\u5f8c\uff0c\u5269\u9918\u7684\u662f\u524d \\(i-1\\) \u500b\u7269\u54c1\u6c7a\u7b56\u7684\u5b50\u554f\u984c\uff0c\u53ef\u5206\u70ba\u4ee5\u4e0b\u5169\u7a2e\u60c5\u6cc1\u3002</p> <ul> <li>\u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u80cc\u5305\u5bb9\u91cf\u4e0d\u8b8a\uff0c\u72c0\u614b\u8b8a\u5316\u70ba \\([i-1, c]\\) \u3002</li> <li>\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u80cc\u5305\u5bb9\u91cf\u6e1b\u5c11 \\(wgt[i-1]\\) \uff0c\u50f9\u503c\u589e\u52a0 \\(val[i-1]\\) \uff0c\u72c0\u614b\u8b8a\u5316\u70ba \\([i-1, c-wgt[i-1]]\\) \u3002</li> </ul> <p>\u4e0a\u8ff0\u5206\u6790\u5411\u6211\u5011\u63ed\u793a\u4e86\u672c\u984c\u7684\u6700\u512a\u5b50\u7d50\u69cb\uff1a\u6700\u5927\u50f9\u503c \\(dp[i, c]\\) \u7b49\u65bc\u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \u548c\u653e\u5165\u7269\u54c1 \\(i\\) \u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\u3002\u7531\u6b64\u53ef\u63a8\u5c0e\u51fa\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\uff1a</p> \\[ dp[i, c] = \\max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1]) \\] <p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u82e5\u7576\u524d\u7269\u54c1\u91cd\u91cf \\(wgt[i - 1]\\) \u8d85\u51fa\u5269\u9918\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\u3002</p> <p>\u7b2c\u4e09\u6b65\uff1a\u78ba\u5b9a\u908a\u754c\u689d\u4ef6\u548c\u72c0\u614b\u8f49\u79fb\u9806\u5e8f</p> <p>\u7576\u7121\u7269\u54c1\u6216\u80cc\u5305\u5bb9\u91cf\u70ba \\(0\\) \u6642\u6700\u5927\u50f9\u503c\u70ba \\(0\\) \uff0c\u5373\u9996\u5217 \\(dp[i, 0]\\) \u548c\u9996\u884c \\(dp[0, c]\\) \u90fd\u7b49\u65bc \\(0\\) \u3002</p> <p>\u7576\u524d\u72c0\u614b \\([i, c]\\) \u5f9e\u4e0a\u65b9\u7684\u72c0\u614b \\([i-1, c]\\) \u548c\u5de6\u4e0a\u65b9\u7684\u72c0\u614b \\([i-1, c-wgt[i-1]]\\) \u8f49\u79fb\u800c\u4f86\uff0c\u56e0\u6b64\u900f\u904e\u5169\u5c64\u8ff4\u5708\u6b63\u5e8f\u8d70\u8a2a\u6574\u500b \\(dp\\) \u8868\u5373\u53ef\u3002</p> <p>\u6839\u64da\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u5011\u63a5\u4e0b\u4f86\u6309\u9806\u5e8f\u5be6\u73fe\u66b4\u529b\u641c\u5c0b\u3001\u8a18\u61b6\u5316\u641c\u5c0b\u3001\u52d5\u614b\u898f\u5283\u89e3\u6cd5\u3002</p>"},{"location":"chapter_dynamic_programming/knapsack_problem/#1","title":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u5c0b","text":"<p>\u641c\u5c0b\u7a0b\u5f0f\u78bc\u5305\u542b\u4ee5\u4e0b\u8981\u7d20\u3002</p> <ul> <li>\u905e\u8ff4\u53c3\u6578\uff1a\u72c0\u614b \\([i, c]\\) \u3002</li> <li>\u8fd4\u56de\u503c\uff1a\u5b50\u554f\u984c\u7684\u89e3 \\(dp[i, c]\\) \u3002</li> <li>\u7d42\u6b62\u689d\u4ef6\uff1a\u7576\u7269\u54c1\u7de8\u865f\u8d8a\u754c \\(i = 0\\) \u6216\u80cc\u5305\u5269\u9918\u5bb9\u91cf\u70ba \\(0\\) \u6642\uff0c\u7d42\u6b62\u905e\u8ff4\u4e26\u8fd4\u56de\u50f9\u503c \\(0\\) \u3002</li> <li>\u526a\u679d\uff1a\u82e5\u7576\u524d\u7269\u54c1\u91cd\u91cf\u8d85\u51fa\u80cc\u5305\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\u3002</li> </ul> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig knapsack.py<pre><code>def knapsack_dfs(wgt: list[int], val: list[int], i: int, c: int) -&gt; int:\n \"\"\"0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u5c0b\"\"\"\n # \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if i == 0 or c == 0:\n return 0\n # \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] &gt; c:\n return knapsack_dfs(wgt, val, i - 1, c)\n # \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n no = knapsack_dfs(wgt, val, i - 1, c)\n yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n # \u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n return max(no, yes)\n</code></pre> knapsack.cpp<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u5c0b */\nint knapsackDFS(vector&lt;int&gt; &amp;wgt, vector&lt;int&gt; &amp;val, int i, int c) {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n int no = knapsackDFS(wgt, val, i - 1, c);\n int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n return max(no, yes);\n}\n</code></pre> knapsack.java<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u5c0b */\nint knapsackDFS(int[] wgt, int[] val, int i, int c) {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n int no = knapsackDFS(wgt, val, i - 1, c);\n int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n return Math.max(no, yes);\n}\n</code></pre> knapsack.cs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u5c0b */\nint KnapsackDFS(int[] weight, int[] val, int i, int c) {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (weight[i - 1] &gt; c) {\n return KnapsackDFS(weight, val, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n int no = KnapsackDFS(weight, val, i - 1, c);\n int yes = KnapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n return Math.Max(no, yes);\n}\n</code></pre> knapsack.go<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u5c0b */\nfunc knapsackDFS(wgt, val []int, i, c int) int {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if i == 0 || c == 0 {\n return 0\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i-1] &gt; c {\n return knapsackDFS(wgt, val, i-1, c)\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n no := knapsackDFS(wgt, val, i-1, c)\n yes := knapsackDFS(wgt, val, i-1, c-wgt[i-1]) + val[i-1]\n // \u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n return int(math.Max(float64(no), float64(yes)))\n}\n</code></pre> knapsack.swift<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u5c0b */\nfunc knapsackDFS(wgt: [Int], val: [Int], i: Int, c: Int) -&gt; Int {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if i == 0 || c == 0 {\n return 0\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] &gt; c {\n return knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n let no = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n let yes = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n // \u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n return max(no, yes)\n}\n</code></pre> knapsack.js<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u5c0b */\nfunction knapsackDFS(wgt, val, i, c) {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i === 0 || c === 0) {\n return 0;\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n const no = knapsackDFS(wgt, val, i - 1, c);\n const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n return Math.max(no, yes);\n}\n</code></pre> knapsack.ts<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u5c0b */\nfunction knapsackDFS(\n wgt: Array&lt;number&gt;,\n val: Array&lt;number&gt;,\n i: number,\n c: number\n): number {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i === 0 || c === 0) {\n return 0;\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n const no = knapsackDFS(wgt, val, i - 1, c);\n const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n return Math.max(no, yes);\n}\n</code></pre> knapsack.dart<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u5c0b */\nint knapsackDFS(List&lt;int&gt; wgt, List&lt;int&gt; val, int i, int c) {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n int no = knapsackDFS(wgt, val, i - 1, c);\n int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n return max(no, yes);\n}\n</code></pre> knapsack.rs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u5c0b */\nfn knapsack_dfs(wgt: &amp;[i32], val: &amp;[i32], i: usize, c: usize) -&gt; i32 {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if i == 0 || c == 0 {\n return 0;\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] &gt; c as i32 {\n return knapsack_dfs(wgt, val, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n let no = knapsack_dfs(wgt, val, i - 1, c);\n let yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n // \u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n std::cmp::max(no, yes)\n}\n</code></pre> knapsack.c<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u5c0b */\nint knapsackDFS(int wgt[], int val[], int i, int c) {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n int no = knapsackDFS(wgt, val, i - 1, c);\n int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n return myMax(no, yes);\n}\n</code></pre> knapsack.kt<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u5c0b */\nfun knapsackDFS(\n wgt: IntArray,\n _val: IntArray,\n i: Int,\n c: Int\n): Int {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i == 0 || c == 0) {\n return 0\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFS(wgt, _val, i - 1, c)\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n val no = knapsackDFS(wgt, _val, i - 1, c)\n val yes = knapsackDFS(wgt, _val, i - 1, c - wgt[i - 1]) + _val[i - 1]\n // \u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n return max(no, yes)\n}\n</code></pre> knapsack.rb<pre><code>[class]{}-[func]{knapsack_dfs}\n</code></pre> knapsack.zig<pre><code>// 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u5c0b\nfn knapsackDFS(wgt: []i32, val: []i32, i: usize, c: usize) i32 {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i == 0 or c == 0) {\n return 0;\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFS(wgt, val, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n var no = knapsackDFS(wgt, val, i - 1, c);\n var yes = knapsackDFS(wgt, val, i - 1, c - @as(usize, @intCast(wgt[i - 1]))) + val[i - 1];\n // \u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n return @max(no, yes);\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5982\u5716 14-18 \u6240\u793a\uff0c\u7531\u65bc\u6bcf\u500b\u7269\u54c1\u90fd\u6703\u7522\u751f\u4e0d\u9078\u548c\u9078\u5169\u689d\u641c\u5c0b\u5206\u652f\uff0c\u56e0\u6b64\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(2^n)\\) \u3002</p> <p>\u89c0\u5bdf\u905e\u8ff4\u6a39\uff0c\u5bb9\u6613\u767c\u73fe\u5176\u4e2d\u5b58\u5728\u91cd\u758a\u5b50\u554f\u984c\uff0c\u4f8b\u5982 \\(dp[1, 10]\\) \u7b49\u3002\u800c\u7576\u7269\u54c1\u8f03\u591a\u3001\u80cc\u5305\u5bb9\u91cf\u8f03\u5927\uff0c\u5c24\u5176\u662f\u76f8\u540c\u91cd\u91cf\u7684\u7269\u54c1\u8f03\u591a\u6642\uff0c\u91cd\u758a\u5b50\u554f\u984c\u7684\u6578\u91cf\u5c07\u6703\u5927\u5e45\u589e\u591a\u3002</p> <p></p> <p> \u5716 14-18 \u00a0 0-1 \u80cc\u5305\u554f\u984c\u7684\u66b4\u529b\u641c\u5c0b\u905e\u8ff4\u6a39 </p>"},{"location":"chapter_dynamic_programming/knapsack_problem/#2","title":"2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8a18\u61b6\u5316\u641c\u5c0b","text":"<p>\u70ba\u4e86\u4fdd\u8b49\u91cd\u758a\u5b50\u554f\u984c\u53ea\u88ab\u8a08\u7b97\u4e00\u6b21\uff0c\u6211\u5011\u85c9\u52a9\u8a18\u61b6\u4e32\u5217 <code>mem</code> \u4f86\u8a18\u9304\u5b50\u554f\u984c\u7684\u89e3\uff0c\u5176\u4e2d <code>mem[i][c]</code> \u5c0d\u61c9 \\(dp[i, c]\\) \u3002</p> <p>\u5f15\u5165\u8a18\u61b6\u5316\u4e4b\u5f8c\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u53d6\u6c7a\u65bc\u5b50\u554f\u984c\u6578\u91cf\uff0c\u4e5f\u5c31\u662f \\(O(n \\times cap)\\) \u3002\u5be6\u73fe\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig knapsack.py<pre><code>def knapsack_dfs_mem(\n wgt: list[int], val: list[int], mem: list[list[int]], i: int, c: int\n) -&gt; int:\n \"\"\"0-1 \u80cc\u5305\uff1a\u8a18\u61b6\u5316\u641c\u5c0b\"\"\"\n # \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if i == 0 or c == 0:\n return 0\n # \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if mem[i][c] != -1:\n return mem[i][c]\n # \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] &gt; c:\n return knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n # \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n # \u8a18\u9304\u4e26\u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n mem[i][c] = max(no, yes)\n return mem[i][c]\n</code></pre> knapsack.cpp<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint knapsackDFSMem(vector&lt;int&gt; &amp;wgt, vector&lt;int&gt; &amp;val, vector&lt;vector&lt;int&gt;&gt; &amp;mem, int i, int c) {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n mem[i][c] = max(no, yes);\n return mem[i][c];\n}\n</code></pre> knapsack.java<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n mem[i][c] = Math.max(no, yes);\n return mem[i][c];\n}\n</code></pre> knapsack.cs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (weight[i - 1] &gt; c) {\n return KnapsackDFSMem(weight, val, mem, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n int no = KnapsackDFSMem(weight, val, mem, i - 1, c);\n int yes = KnapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n mem[i][c] = Math.Max(no, yes);\n return mem[i][c];\n}\n</code></pre> knapsack.go<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfunc knapsackDFSMem(wgt, val []int, mem [][]int, i, c int) int {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if i == 0 || c == 0 {\n return 0\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if mem[i][c] != -1 {\n return mem[i][c]\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i-1] &gt; c {\n return knapsackDFSMem(wgt, val, mem, i-1, c)\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n no := knapsackDFSMem(wgt, val, mem, i-1, c)\n yes := knapsackDFSMem(wgt, val, mem, i-1, c-wgt[i-1]) + val[i-1]\n // \u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n mem[i][c] = int(math.Max(float64(no), float64(yes)))\n return mem[i][c]\n}\n</code></pre> knapsack.swift<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfunc knapsackDFSMem(wgt: [Int], val: [Int], mem: inout [[Int]], i: Int, c: Int) -&gt; Int {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if i == 0 || c == 0 {\n return 0\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if mem[i][c] != -1 {\n return mem[i][c]\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] &gt; c {\n return knapsackDFSMem(wgt: wgt, val: val, mem: &amp;mem, i: i - 1, c: c)\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n let no = knapsackDFSMem(wgt: wgt, val: val, mem: &amp;mem, i: i - 1, c: c)\n let yes = knapsackDFSMem(wgt: wgt, val: val, mem: &amp;mem, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n mem[i][c] = max(no, yes)\n return mem[i][c]\n}\n</code></pre> knapsack.js<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfunction knapsackDFSMem(wgt, val, mem, i, c) {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i === 0 || c === 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] !== -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n const yes =\n knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n mem[i][c] = Math.max(no, yes);\n return mem[i][c];\n}\n</code></pre> knapsack.ts<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfunction knapsackDFSMem(\n wgt: Array&lt;number&gt;,\n val: Array&lt;number&gt;,\n mem: Array&lt;Array&lt;number&gt;&gt;,\n i: number,\n c: number\n): number {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i === 0 || c === 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] !== -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n const yes =\n knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n mem[i][c] = Math.max(no, yes);\n return mem[i][c];\n}\n</code></pre> knapsack.dart<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint knapsackDFSMem(\n List&lt;int&gt; wgt,\n List&lt;int&gt; val,\n List&lt;List&lt;int&gt;&gt; mem,\n int i,\n int c,\n) {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n mem[i][c] = max(no, yes);\n return mem[i][c];\n}\n</code></pre> knapsack.rs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfn knapsack_dfs_mem(wgt: &amp;[i32], val: &amp;[i32], mem: &amp;mut Vec&lt;Vec&lt;i32&gt;&gt;, i: usize, c: usize) -&gt; i32 {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if i == 0 || c == 0 {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if mem[i][c] != -1 {\n return mem[i][c];\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if wgt[i - 1] &gt; c as i32 {\n return knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n let no = knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n let yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n mem[i][c] = std::cmp::max(no, yes);\n mem[i][c]\n}\n</code></pre> knapsack.c<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nint knapsackDFSMem(int wgt[], int val[], int memCols, int **mem, int i, int c) {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i == 0 || c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n int no = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n int yes = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n mem[i][c] = myMax(no, yes);\n return mem[i][c];\n}\n</code></pre> knapsack.kt<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8a18\u61b6\u5316\u641c\u5c0b */\nfun knapsackDFSMem(\n wgt: IntArray,\n _val: IntArray,\n mem: Array&lt;IntArray&gt;,\n i: Int,\n c: Int\n): Int {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i == 0 || c == 0) {\n return 0\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c]\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFSMem(wgt, _val, mem, i - 1, c)\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n val no = knapsackDFSMem(wgt, _val, mem, i - 1, c)\n val yes = knapsackDFSMem(wgt, _val, mem, i - 1, c - wgt[i - 1]) + _val[i - 1]\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n mem[i][c] = max(no, yes)\n return mem[i][c]\n}\n</code></pre> knapsack.rb<pre><code>[class]{}-[func]{knapsack_dfs_mem}\n</code></pre> knapsack.zig<pre><code>// 0-1 \u80cc\u5305\uff1a\u8a18\u61b6\u5316\u641c\u5c0b\nfn knapsackDFSMem(wgt: []i32, val: []i32, mem: anytype, i: usize, c: usize) i32 {\n // \u82e5\u5df2\u9078\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u5247\u8fd4\u56de\u50f9\u503c 0\n if (i == 0 or c == 0) {\n return 0;\n }\n // \u82e5\u5df2\u6709\u8a18\u9304\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (mem[i][c] != -1) {\n return mem[i][c];\n }\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u53ea\u80fd\u9078\u64c7\u4e0d\u653e\u5165\u80cc\u5305\n if (wgt[i - 1] &gt; c) {\n return knapsackDFSMem(wgt, val, mem, i - 1, c);\n }\n // \u8a08\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u50f9\u503c\n var no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n var yes = knapsackDFSMem(wgt, val, mem, i - 1, c - @as(usize, @intCast(wgt[i - 1]))) + val[i - 1];\n // \u8a18\u9304\u4e26\u8fd4\u56de\u5169\u7a2e\u65b9\u6848\u4e2d\u50f9\u503c\u66f4\u5927\u7684\u90a3\u4e00\u500b\n mem[i][c] = @max(no, yes);\n return mem[i][c];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 14-19 \u5c55\u793a\u4e86\u5728\u8a18\u61b6\u5316\u641c\u5c0b\u4e2d\u88ab\u526a\u6389\u7684\u641c\u5c0b\u5206\u652f\u3002</p> <p></p> <p> \u5716 14-19 \u00a0 0-1 \u80cc\u5305\u554f\u984c\u7684\u8a18\u61b6\u5316\u641c\u5c0b\u905e\u8ff4\u6a39 </p>"},{"location":"chapter_dynamic_programming/knapsack_problem/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52d5\u614b\u898f\u5283","text":"<p>\u52d5\u614b\u898f\u5283\u5be6\u8cea\u4e0a\u5c31\u662f\u5728\u72c0\u614b\u8f49\u79fb\u4e2d\u586b\u5145 \\(dp\\) \u8868\u7684\u904e\u7a0b\uff0c\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig knapsack.py<pre><code>def knapsack_dp(wgt: list[int], val: list[int], cap: int) -&gt; int:\n \"\"\"0-1 \u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283\"\"\"\n n = len(wgt)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * (cap + 1) for _ in range(n + 1)]\n # \u72c0\u614b\u8f49\u79fb\n for i in range(1, n + 1):\n for c in range(1, cap + 1):\n if wgt[i - 1] &gt; c:\n # \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c]\n else:\n # \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n return dp[n][cap]\n</code></pre> knapsack.cpp<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nint knapsackDP(vector&lt;int&gt; &amp;wgt, vector&lt;int&gt; &amp;val, int cap) {\n int n = wgt.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(cap + 1, 0));\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n</code></pre> knapsack.java<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nint knapsackDP(int[] wgt, int[] val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n + 1][cap + 1];\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n</code></pre> knapsack.cs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nint KnapsackDP(int[] weight, int[] val, int cap) {\n int n = weight.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n + 1, cap + 1];\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (weight[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i, c] = dp[i - 1, c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i, c] = Math.Max(dp[i - 1, c - weight[i - 1]] + val[i - 1], dp[i - 1, c]);\n }\n }\n }\n return dp[n, cap];\n}\n</code></pre> knapsack.go<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nfunc knapsackDP(wgt, val []int, cap int) int {\n n := len(wgt)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n+1)\n for i := 0; i &lt;= n; i++ {\n dp[i] = make([]int, cap+1)\n }\n // \u72c0\u614b\u8f49\u79fb\n for i := 1; i &lt;= n; i++ {\n for c := 1; c &lt;= cap; c++ {\n if wgt[i-1] &gt; c {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i-1][c]\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i-1][c-wgt[i-1]]+val[i-1])))\n }\n }\n }\n return dp[n][cap]\n}\n</code></pre> knapsack.swift<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nfunc knapsackDP(wgt: [Int], val: [Int], cap: Int) -&gt; Int {\n let n = wgt.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n // \u72c0\u614b\u8f49\u79fb\n for i in 1 ... n {\n for c in 1 ... cap {\n if wgt[i - 1] &gt; c {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c]\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n }\n }\n }\n return dp[n][cap]\n}\n</code></pre> knapsack.js<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nfunction knapsackDP(wgt, val, cap) {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array(n + 1)\n .fill(0)\n .map(() =&gt; Array(cap + 1).fill(0));\n // \u72c0\u614b\u8f49\u79fb\n for (let i = 1; i &lt;= n; i++) {\n for (let c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = Math.max(\n dp[i - 1][c],\n dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n );\n }\n }\n }\n return dp[n][cap];\n}\n</code></pre> knapsack.ts<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nfunction knapsackDP(\n wgt: Array&lt;number&gt;,\n val: Array&lt;number&gt;,\n cap: number\n): number {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =&gt;\n Array.from({ length: cap + 1 }, () =&gt; 0)\n );\n // \u72c0\u614b\u8f49\u79fb\n for (let i = 1; i &lt;= n; i++) {\n for (let c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = Math.max(\n dp[i - 1][c],\n dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n );\n }\n }\n }\n return dp[n][cap];\n}\n</code></pre> knapsack.dart<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nint knapsackDP(List&lt;int&gt; wgt, List&lt;int&gt; val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n List&lt;List&lt;int&gt;&gt; dp = List.generate(n + 1, (index) =&gt; List.filled(cap + 1, 0));\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n</code></pre> knapsack.rs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nfn knapsack_dp(wgt: &amp;[i32], val: &amp;[i32], cap: usize) -&gt; i32 {\n let n = wgt.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; cap + 1]; n + 1];\n // \u72c0\u614b\u8f49\u79fb\n for i in 1..=n {\n for c in 1..=cap {\n if wgt[i - 1] &gt; c as i32 {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = std::cmp::max(\n dp[i - 1][c],\n dp[i - 1][c - wgt[i - 1] as usize] + val[i - 1],\n );\n }\n }\n }\n dp[n][cap]\n}\n</code></pre> knapsack.c<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nint knapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n int n = wgtSize;\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i &lt;= n; i++) {\n dp[i] = calloc(cap + 1, sizeof(int));\n }\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = myMax(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n int res = dp[n][cap];\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n for (int i = 0; i &lt;= n; i++) {\n free(dp[i]);\n }\n return res;\n}\n</code></pre> knapsack.kt<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nfun knapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {\n val n = wgt.size\n // \u521d\u59cb\u5316 dp \u8868\n val dp = Array(n + 1) { IntArray(cap + 1) }\n // \u72c0\u614b\u8f49\u79fb\n for (i in 1..n) {\n for (c in 1..cap) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c]\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + _val[i - 1])\n }\n }\n }\n return dp[n][cap]\n}\n</code></pre> knapsack.rb<pre><code>[class]{}-[func]{knapsack_dp}\n</code></pre> knapsack.zig<pre><code>// 0-1 \u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283\nfn knapsackDP(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\n comptime var n = wgt.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][cap + 1]i32{[_]i32{0} ** (cap + 1)} ** (n + 1);\n // \u72c0\u614b\u8f49\u79fb\n for (1..n + 1) |i| {\n for (1..cap + 1) |c| {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = @max(dp[i - 1][c], dp[i - 1][c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5982\u5716 14-20 \u6240\u793a\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u548c\u7a7a\u9593\u8907\u96dc\u5ea6\u90fd\u7531\u9663\u5217 <code>dp</code> \u5927\u5c0f\u6c7a\u5b9a\uff0c\u5373 \\(O(n \\times cap)\\) \u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt;&lt;10&gt;&lt;11&gt;&lt;12&gt;&lt;13&gt;&lt;14&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 14-20 \u00a0 0-1 \u80cc\u5305\u554f\u984c\u7684\u52d5\u614b\u898f\u5283\u904e\u7a0b </p>"},{"location":"chapter_dynamic_programming/knapsack_problem/#4","title":"4. \u00a0 \u7a7a\u9593\u6700\u4f73\u5316","text":"<p>\u7531\u65bc\u6bcf\u500b\u72c0\u614b\u90fd\u53ea\u8207\u5176\u4e0a\u4e00\u884c\u7684\u72c0\u614b\u6709\u95dc\uff0c\u56e0\u6b64\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u5169\u500b\u9663\u5217\u6efe\u52d5\u524d\u9032\uff0c\u5c07\u7a7a\u9593\u8907\u96dc\u5ea6\u5f9e \\(O(n^2)\\) \u964d\u81f3 \\(O(n)\\) \u3002</p> <p>\u9032\u4e00\u6b65\u601d\u8003\uff0c\u6211\u5011\u80fd\u5426\u50c5\u7528\u4e00\u500b\u9663\u5217\u5be6\u73fe\u7a7a\u9593\u6700\u4f73\u5316\u5462\uff1f\u89c0\u5bdf\u53ef\u77e5\uff0c\u6bcf\u500b\u72c0\u614b\u90fd\u662f\u7531\u6b63\u4e0a\u65b9\u6216\u5de6\u4e0a\u65b9\u7684\u683c\u5b50\u8f49\u79fb\u904e\u4f86\u7684\u3002\u5047\u8a2d\u53ea\u6709\u4e00\u500b\u9663\u5217\uff0c\u7576\u958b\u59cb\u8d70\u8a2a\u7b2c \\(i\\) \u884c\u6642\uff0c\u8a72\u9663\u5217\u5132\u5b58\u7684\u4ecd\u7136\u662f\u7b2c \\(i-1\\) \u884c\u7684\u72c0\u614b\u3002</p> <ul> <li>\u5982\u679c\u63a1\u53d6\u6b63\u5e8f\u8d70\u8a2a\uff0c\u90a3\u9ebc\u8d70\u8a2a\u5230 \\(dp[i, j]\\) \u6642\uff0c\u5de6\u4e0a\u65b9 \\(dp[i-1, 1]\\) ~ \\(dp[i-1, j-1]\\) \u503c\u53ef\u80fd\u5df2\u7d93\u88ab\u8986\u84cb\uff0c\u6b64\u6642\u5c31\u7121\u6cd5\u5f97\u5230\u6b63\u78ba\u7684\u72c0\u614b\u8f49\u79fb\u7d50\u679c\u3002</li> <li>\u5982\u679c\u63a1\u53d6\u5012\u5e8f\u8d70\u8a2a\uff0c\u5247\u4e0d\u6703\u767c\u751f\u8986\u84cb\u554f\u984c\uff0c\u72c0\u614b\u8f49\u79fb\u53ef\u4ee5\u6b63\u78ba\u9032\u884c\u3002</li> </ul> <p>\u5716 14-21 \u5c55\u793a\u4e86\u5728\u55ae\u500b\u9663\u5217\u4e0b\u5f9e\u7b2c \\(i = 1\\) \u884c\u8f49\u63db\u81f3\u7b2c \\(i = 2\\) \u884c\u7684\u904e\u7a0b\u3002\u8acb\u601d\u8003\u6b63\u5e8f\u8d70\u8a2a\u548c\u5012\u5e8f\u8d70\u8a2a\u7684\u5340\u5225\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 14-21 \u00a0 0-1 \u80cc\u5305\u7684\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\u904e\u7a0b </p> <p>\u5728\u7a0b\u5f0f\u78bc\u5be6\u73fe\u4e2d\uff0c\u6211\u5011\u50c5\u9700\u5c07\u9663\u5217 <code>dp</code> \u7684\u7b2c\u4e00\u7dad \\(i\\) \u76f4\u63a5\u522a\u9664\uff0c\u4e26\u4e14\u628a\u5167\u8ff4\u5708\u66f4\u6539\u70ba\u5012\u5e8f\u8d70\u8a2a\u5373\u53ef\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig knapsack.py<pre><code>def knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -&gt; int:\n \"\"\"0-1 \u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\"\"\"\n n = len(wgt)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [0] * (cap + 1)\n # \u72c0\u614b\u8f49\u79fb\n for i in range(1, n + 1):\n # \u5012\u5e8f\u8d70\u8a2a\n for c in range(cap, 0, -1):\n if wgt[i - 1] &gt; c:\n # \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c]\n else:\n # \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n return dp[cap]\n</code></pre> knapsack.cpp<pre><code>/* 0-1 \u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint knapsackDPComp(vector&lt;int&gt; &amp;wgt, vector&lt;int&gt; &amp;val, int cap) {\n int n = wgt.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector&lt;int&gt; dp(cap + 1, 0);\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n // \u5012\u5e8f\u8d70\u8a2a\n for (int c = cap; c &gt;= 1; c--) {\n if (wgt[i - 1] &lt;= c) {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n</code></pre> knapsack.java<pre><code>/* 0-1 \u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint knapsackDPComp(int[] wgt, int[] val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[cap + 1];\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n // \u5012\u5e8f\u8d70\u8a2a\n for (int c = cap; c &gt;= 1; c--) {\n if (wgt[i - 1] &lt;= c) {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n</code></pre> knapsack.cs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint KnapsackDPComp(int[] weight, int[] val, int cap) {\n int n = weight.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[cap + 1];\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n // \u5012\u5e8f\u8d70\u8a2a\n for (int c = cap; c &gt; 0; c--) {\n if (weight[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = Math.Max(dp[c], dp[c - weight[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n</code></pre> knapsack.go<pre><code>/* 0-1 \u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc knapsackDPComp(wgt, val []int, cap int) int {\n n := len(wgt)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, cap+1)\n // \u72c0\u614b\u8f49\u79fb\n for i := 1; i &lt;= n; i++ {\n // \u5012\u5e8f\u8d70\u8a2a\n for c := cap; c &gt;= 1; c-- {\n if wgt[i-1] &lt;= c {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n }\n }\n }\n return dp[cap]\n}\n</code></pre> knapsack.swift<pre><code>/* 0-1 \u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc knapsackDPComp(wgt: [Int], val: [Int], cap: Int) -&gt; Int {\n let n = wgt.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: 0, count: cap + 1)\n // \u72c0\u614b\u8f49\u79fb\n for i in 1 ... n {\n // \u5012\u5e8f\u8d70\u8a2a\n for c in (1 ... cap).reversed() {\n if wgt[i - 1] &lt;= c {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n }\n }\n }\n return dp[cap]\n}\n</code></pre> knapsack.js<pre><code>/* 0-1 \u80cc\u5305\uff1a\u72c0\u614b\u58d3\u7e2e\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction knapsackDPComp(wgt, val, cap) {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array(cap + 1).fill(0);\n // \u72c0\u614b\u8f49\u79fb\n for (let i = 1; i &lt;= n; i++) {\n // \u5012\u5e8f\u8d70\u8a2a\n for (let c = cap; c &gt;= 1; c--) {\n if (wgt[i - 1] &lt;= c) {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n</code></pre> knapsack.ts<pre><code>/* 0-1 \u80cc\u5305\uff1a\u72c0\u614b\u58d3\u7e2e\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction knapsackDPComp(\n wgt: Array&lt;number&gt;,\n val: Array&lt;number&gt;,\n cap: number\n): number {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array(cap + 1).fill(0);\n // \u72c0\u614b\u8f49\u79fb\n for (let i = 1; i &lt;= n; i++) {\n // \u5012\u5e8f\u8d70\u8a2a\n for (let c = cap; c &gt;= 1; c--) {\n if (wgt[i - 1] &lt;= c) {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n</code></pre> knapsack.dart<pre><code>/* 0-1 \u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint knapsackDPComp(List&lt;int&gt; wgt, List&lt;int&gt; val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n List&lt;int&gt; dp = List.filled(cap + 1, 0);\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n // \u5012\u5e8f\u8d70\u8a2a\n for (int c = cap; c &gt;= 1; c--) {\n if (wgt[i - 1] &lt;= c) {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n</code></pre> knapsack.rs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfn knapsack_dp_comp(wgt: &amp;[i32], val: &amp;[i32], cap: usize) -&gt; i32 {\n let n = wgt.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; cap + 1];\n // \u72c0\u614b\u8f49\u79fb\n for i in 1..=n {\n // \u5012\u5e8f\u8d70\u8a2a\n for c in (1..=cap).rev() {\n if wgt[i - 1] &lt;= c as i32 {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n }\n }\n }\n dp[cap]\n}\n</code></pre> knapsack.c<pre><code>/* 0-1 \u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint knapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n int n = wgtSize;\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = calloc(cap + 1, sizeof(int));\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n // \u5012\u5e8f\u8d70\u8a2a\n for (int c = cap; c &gt;= 1; c--) {\n if (wgt[i - 1] &lt;= c) {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n int res = dp[cap];\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(dp);\n return res;\n}\n</code></pre> knapsack.kt<pre><code>/* 0-1 \u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfun knapsackDPComp(wgt: IntArray, _val: IntArray, cap: Int): Int {\n val n = wgt.size\n // \u521d\u59cb\u5316 dp \u8868\n val dp = IntArray(cap + 1)\n // \u72c0\u614b\u8f49\u79fb\n for (i in 1..n) {\n // \u5012\u5e8f\u8d70\u8a2a\n for (c in cap downTo 1) {\n if (wgt[i - 1] &lt;= c) {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])\n }\n }\n }\n return dp[cap]\n}\n</code></pre> knapsack.rb<pre><code>[class]{}-[func]{knapsack_dp_comp}\n</code></pre> knapsack.zig<pre><code>// 0-1 \u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\nfn knapsackDPComp(wgt: []i32, val: []i32, comptime cap: usize) i32 {\n var n = wgt.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** (cap + 1);\n // \u72c0\u614b\u8f49\u79fb\n for (1..n + 1) |i| {\n // \u5012\u5e8f\u8d70\u8a2a\n var c = cap;\n while (c &gt; 0) : (c -= 1) {\n if (wgt[i - 1] &lt; c) {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = @max(dp[c], dp[c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_dynamic_programming/summary/","title":"14.7 \u00a0 \u5c0f\u7d50","text":"<ul> <li>\u52d5\u614b\u898f\u5283\u5c0d\u554f\u984c\u9032\u884c\u5206\u89e3\uff0c\u4e26\u900f\u904e\u5132\u5b58\u5b50\u554f\u984c\u7684\u89e3\u4f86\u898f\u907f\u91cd\u8907\u8a08\u7b97\uff0c\u63d0\u9ad8\u8a08\u7b97\u6548\u7387\u3002</li> <li>\u4e0d\u8003\u616e\u6642\u9593\u7684\u524d\u63d0\u4e0b\uff0c\u6240\u6709\u52d5\u614b\u898f\u5283\u554f\u984c\u90fd\u53ef\u4ee5\u7528\u56de\u6eaf\uff08\u66b4\u529b\u641c\u5c0b\uff09\u9032\u884c\u6c42\u89e3\uff0c\u4f46\u905e\u8ff4\u6a39\u4e2d\u5b58\u5728\u5927\u91cf\u7684\u91cd\u758a\u5b50\u554f\u984c\uff0c\u6548\u7387\u6975\u4f4e\u3002\u900f\u904e\u5f15\u5165\u8a18\u61b6\u5316\u4e32\u5217\uff0c\u53ef\u4ee5\u5132\u5b58\u6240\u6709\u8a08\u7b97\u904e\u7684\u5b50\u554f\u984c\u7684\u89e3\uff0c\u5f9e\u800c\u4fdd\u8b49\u91cd\u758a\u5b50\u554f\u984c\u53ea\u88ab\u8a08\u7b97\u4e00\u6b21\u3002</li> <li>\u8a18\u61b6\u5316\u641c\u5c0b\u662f\u4e00\u7a2e\u5f9e\u9802\u81f3\u5e95\u7684\u905e\u8ff4\u5f0f\u89e3\u6cd5\uff0c\u800c\u8207\u4e4b\u5c0d\u61c9\u7684\u52d5\u614b\u898f\u5283\u662f\u4e00\u7a2e\u5f9e\u5e95\u81f3\u9802\u7684\u905e\u63a8\u5f0f\u89e3\u6cd5\uff0c\u5176\u5982\u540c\u201c\u586b\u5beb\u8868\u683c\u201d\u4e00\u6a23\u3002\u7531\u65bc\u7576\u524d\u72c0\u614b\u50c5\u4f9d\u8cf4\u67d0\u4e9b\u5340\u57df\u6027\u72c0\u614b\uff0c\u56e0\u6b64\u6211\u5011\u53ef\u4ee5\u6d88\u9664 \\(dp\\) \u8868\u7684\u4e00\u500b\u7dad\u5ea6\uff0c\u5f9e\u800c\u964d\u4f4e\u7a7a\u9593\u8907\u96dc\u5ea6\u3002</li> <li>\u5b50\u554f\u984c\u5206\u89e3\u662f\u4e00\u7a2e\u901a\u7528\u7684\u6f14\u7b97\u6cd5\u601d\u8def\uff0c\u5728\u5206\u6cbb\u3001\u52d5\u614b\u898f\u5283\u3001\u56de\u6eaf\u4e2d\u5177\u6709\u4e0d\u540c\u7684\u6027\u8cea\u3002</li> <li>\u52d5\u614b\u898f\u5283\u554f\u984c\u6709\u4e09\u5927\u7279\u6027\uff1a\u91cd\u758a\u5b50\u554f\u984c\u3001\u6700\u512a\u5b50\u7d50\u69cb\u3001\u7121\u5f8c\u6548\u6027\u3002</li> <li>\u5982\u679c\u539f\u554f\u984c\u7684\u6700\u512a\u89e3\u53ef\u4ee5\u5f9e\u5b50\u554f\u984c\u7684\u6700\u512a\u89e3\u69cb\u5efa\u5f97\u4f86\uff0c\u5247\u5b83\u5c31\u5177\u6709\u6700\u512a\u5b50\u7d50\u69cb\u3002</li> <li>\u7121\u5f8c\u6548\u6027\u6307\u5c0d\u65bc\u4e00\u500b\u72c0\u614b\uff0c\u5176\u672a\u4f86\u767c\u5c55\u53ea\u8207\u8a72\u72c0\u614b\u6709\u95dc\uff0c\u800c\u8207\u904e\u53bb\u7d93\u6b77\u7684\u6240\u6709\u72c0\u614b\u7121\u95dc\u3002\u8a31\u591a\u7d44\u5408\u6700\u4f73\u5316\u554f\u984c\u4e0d\u5177\u6709\u7121\u5f8c\u6548\u6027\uff0c\u7121\u6cd5\u4f7f\u7528\u52d5\u614b\u898f\u5283\u5feb\u901f\u6c42\u89e3\u3002</li> </ul> <p>\u80cc\u5305\u554f\u984c</p> <ul> <li>\u80cc\u5305\u554f\u984c\u662f\u6700\u5178\u578b\u7684\u52d5\u614b\u898f\u5283\u554f\u984c\u4e4b\u4e00\uff0c\u5177\u6709 0-1 \u80cc\u5305\u3001\u5b8c\u5168\u80cc\u5305\u3001\u591a\u91cd\u80cc\u5305\u7b49\u8b8a\u7a2e\u3002</li> <li>0-1 \u80cc\u5305\u7684\u72c0\u614b\u5b9a\u7fa9\u70ba\u524d \\(i\\) \u500b\u7269\u54c1\u5728\u5bb9\u91cf\u70ba \\(c\\) \u7684\u80cc\u5305\u4e2d\u7684\u6700\u5927\u50f9\u503c\u3002\u6839\u64da\u4e0d\u653e\u5165\u80cc\u5305\u548c\u653e\u5165\u80cc\u5305\u5169\u7a2e\u6c7a\u7b56\uff0c\u53ef\u5f97\u5230\u6700\u512a\u5b50\u7d50\u69cb\uff0c\u4e26\u69cb\u5efa\u51fa\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\u3002\u5728\u7a7a\u9593\u6700\u4f73\u5316\u4e2d\uff0c\u7531\u65bc\u6bcf\u500b\u72c0\u614b\u4f9d\u8cf4\u6b63\u4e0a\u65b9\u548c\u5de6\u4e0a\u65b9\u7684\u72c0\u614b\uff0c\u56e0\u6b64\u9700\u8981\u5012\u5e8f\u8d70\u8a2a\u4e32\u5217\uff0c\u907f\u514d\u5de6\u4e0a\u65b9\u72c0\u614b\u88ab\u8986\u84cb\u3002</li> <li>\u5b8c\u5168\u80cc\u5305\u554f\u984c\u7684\u6bcf\u7a2e\u7269\u54c1\u7684\u9078\u53d6\u6578\u91cf\u7121\u9650\u5236\uff0c\u56e0\u6b64\u9078\u64c7\u653e\u5165\u7269\u54c1\u7684\u72c0\u614b\u8f49\u79fb\u8207 0-1 \u80cc\u5305\u554f\u984c\u4e0d\u540c\u3002\u7531\u65bc\u72c0\u614b\u4f9d\u8cf4\u6b63\u4e0a\u65b9\u548c\u6b63\u5de6\u65b9\u7684\u72c0\u614b\uff0c\u56e0\u6b64\u5728\u7a7a\u9593\u6700\u4f73\u5316\u4e2d\u61c9\u7576\u6b63\u5e8f\u8d70\u8a2a\u3002</li> <li>\u96f6\u9322\u514c\u63db\u554f\u984c\u662f\u5b8c\u5168\u80cc\u5305\u554f\u984c\u7684\u4e00\u500b\u8b8a\u7a2e\u3002\u5b83\u5f9e\u6c42\u201c\u6700\u5927\u201d\u50f9\u503c\u8b8a\u70ba\u6c42\u201c\u6700\u5c0f\u201d\u786c\u5e63\u6578\u91cf\uff0c\u56e0\u6b64\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(\\max()\\) \u61c9\u6539\u70ba \\(\\min()\\) \u3002\u5f9e\u8ffd\u6c42\u201c\u4e0d\u8d85\u904e\u201d\u80cc\u5305\u5bb9\u91cf\u5230\u8ffd\u6c42\u201c\u6070\u597d\u201d\u6e4a\u51fa\u76ee\u6a19\u91d1\u984d\uff0c\u56e0\u6b64\u4f7f\u7528 \\(amt + 1\\) \u4f86\u8868\u793a\u201c\u7121\u6cd5\u6e4a\u51fa\u76ee\u6a19\u91d1\u984d\u201d\u7684\u7121\u6548\u89e3\u3002</li> <li>\u96f6\u9322\u514c\u63db\u554f\u984c II \u5f9e\u6c42\u201c\u6700\u5c11\u786c\u5e63\u6578\u91cf\u201d\u6539\u70ba\u6c42\u201c\u786c\u5e63\u7d44\u5408\u6578\u91cf\u201d\uff0c\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\u76f8\u61c9\u5730\u5f9e \\(\\min()\\) \u6539\u70ba\u6c42\u548c\u904b\u7b97\u5b50\u3002</li> </ul> <p>\u7de8\u8f2f\u8ddd\u96e2\u554f\u984c</p> <ul> <li>\u7de8\u8f2f\u8ddd\u96e2\uff08Levenshtein \u8ddd\u96e2\uff09\u7528\u65bc\u8861\u91cf\u5169\u500b\u5b57\u4e32\u4e4b\u9593\u7684\u76f8\u4f3c\u5ea6\uff0c\u5176\u5b9a\u7fa9\u70ba\u5f9e\u4e00\u500b\u5b57\u4e32\u5230\u53e6\u4e00\u500b\u5b57\u4e32\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578\uff0c\u7de8\u8f2f\u64cd\u4f5c\u5305\u62ec\u65b0\u589e\u3001\u522a\u9664\u3001\u66ff\u63db\u3002</li> <li>\u7de8\u8f2f\u8ddd\u96e2\u554f\u984c\u7684\u72c0\u614b\u5b9a\u7fa9\u70ba\u5c07 \\(s\\) \u7684\u524d \\(i\\) \u500b\u5b57\u5143\u66f4\u6539\u70ba \\(t\\) \u7684\u524d \\(j\\) \u500b\u5b57\u5143\u6240\u9700\u7684\u6700\u5c11\u7de8\u8f2f\u6b65\u6578\u3002\u7576 \\(s[i] \\ne t[j]\\) \u6642\uff0c\u5177\u6709\u4e09\u7a2e\u6c7a\u7b56\uff1a\u65b0\u589e\u3001\u522a\u9664\u3001\u66ff\u63db\uff0c\u5b83\u5011\u90fd\u6709\u76f8\u61c9\u7684\u5269\u9918\u5b50\u554f\u984c\u3002\u64da\u6b64\u4fbf\u53ef\u4ee5\u627e\u51fa\u6700\u512a\u5b50\u7d50\u69cb\u8207\u69cb\u5efa\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\u3002\u800c\u7576 \\(s[i] = t[j]\\) \u6642\uff0c\u7121\u9808\u7de8\u8f2f\u7576\u524d\u5b57\u5143\u3002</li> <li>\u5728\u7de8\u8f2f\u8ddd\u96e2\u4e2d\uff0c\u72c0\u614b\u4f9d\u8cf4\u5176\u6b63\u4e0a\u65b9\u3001\u6b63\u5de6\u65b9\u3001\u5de6\u4e0a\u65b9\u7684\u72c0\u614b\uff0c\u56e0\u6b64\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u6b63\u5e8f\u6216\u5012\u5e8f\u8d70\u8a2a\u90fd\u7121\u6cd5\u6b63\u78ba\u5730\u9032\u884c\u72c0\u614b\u8f49\u79fb\u3002\u70ba\u6b64\uff0c\u6211\u5011\u5229\u7528\u4e00\u500b\u8b8a\u6578\u66ab\u5b58\u5de6\u4e0a\u65b9\u72c0\u614b\uff0c\u5f9e\u800c\u8f49\u5316\u5230\u8207\u5b8c\u5168\u80cc\u5305\u554f\u984c\u7b49\u50f9\u7684\u60c5\u6cc1\uff0c\u53ef\u4ee5\u5728\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u9032\u884c\u6b63\u5e8f\u8d70\u8a2a\u3002</li> </ul>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/","title":"14.5 \u00a0 \u5b8c\u5168\u80cc\u5305\u554f\u984c","text":"<p>\u5728\u672c\u7bc0\u4e2d\uff0c\u6211\u5011\u5148\u6c42\u89e3\u53e6\u4e00\u500b\u5e38\u898b\u7684\u80cc\u5305\u554f\u984c\uff1a\u5b8c\u5168\u80cc\u5305\uff0c\u518d\u77ad\u89e3\u5b83\u7684\u4e00\u7a2e\u7279\u4f8b\uff1a\u96f6\u9322\u514c\u63db\u3002</p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1451","title":"14.5.1 \u00a0 \u5b8c\u5168\u80cc\u5305\u554f\u984c","text":"<p>Question</p> <p>\u7d66\u5b9a \\(n\\) \u500b\u7269\u54c1\uff0c\u7b2c \\(i\\) \u500b\u7269\u54c1\u7684\u91cd\u91cf\u70ba \\(wgt[i-1]\\)\u3001\u50f9\u503c\u70ba \\(val[i-1]\\) \uff0c\u548c\u4e00\u500b\u5bb9\u91cf\u70ba \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u500b\u7269\u54c1\u53ef\u4ee5\u91cd\u8907\u9078\u53d6\uff0c\u554f\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u50f9\u503c\u3002\u793a\u4f8b\u5982\u5716 14-22 \u6240\u793a\u3002</p> <p></p> <p> \u5716 14-22 \u00a0 \u5b8c\u5168\u80cc\u5305\u554f\u984c\u7684\u793a\u4f8b\u8cc7\u6599 </p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1","title":"1. \u00a0 \u52d5\u614b\u898f\u5283\u601d\u8def","text":"<p>\u5b8c\u5168\u80cc\u5305\u554f\u984c\u548c 0-1 \u80cc\u5305\u554f\u984c\u975e\u5e38\u76f8\u4f3c\uff0c\u5340\u5225\u50c5\u5728\u65bc\u4e0d\u9650\u5236\u7269\u54c1\u7684\u9078\u64c7\u6b21\u6578\u3002</p> <ul> <li>\u5728 0-1 \u80cc\u5305\u554f\u984c\u4e2d\uff0c\u6bcf\u7a2e\u7269\u54c1\u53ea\u6709\u4e00\u500b\uff0c\u56e0\u6b64\u5c07\u7269\u54c1 \\(i\\) \u653e\u5165\u80cc\u5305\u5f8c\uff0c\u53ea\u80fd\u5f9e\u524d \\(i-1\\) \u500b\u7269\u54c1\u4e2d\u9078\u64c7\u3002</li> <li>\u5728\u5b8c\u5168\u80cc\u5305\u554f\u984c\u4e2d\uff0c\u6bcf\u7a2e\u7269\u54c1\u7684\u6578\u91cf\u662f\u7121\u9650\u7684\uff0c\u56e0\u6b64\u5c07\u7269\u54c1 \\(i\\) \u653e\u5165\u80cc\u5305\u5f8c\uff0c\u4ecd\u53ef\u4ee5\u5f9e\u524d \\(i\\) \u500b\u7269\u54c1\u4e2d\u9078\u64c7\u3002</li> </ul> <p>\u5728\u5b8c\u5168\u80cc\u5305\u554f\u984c\u7684\u898f\u5b9a\u4e0b\uff0c\u72c0\u614b \\([i, c]\\) \u7684\u8b8a\u5316\u5206\u70ba\u5169\u7a2e\u60c5\u6cc1\u3002</p> <ul> <li>\u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u8207 0-1 \u80cc\u5305\u554f\u984c\u76f8\u540c\uff0c\u8f49\u79fb\u81f3 \\([i-1, c]\\) \u3002</li> <li>\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u8207 0-1 \u80cc\u5305\u554f\u984c\u4e0d\u540c\uff0c\u8f49\u79fb\u81f3 \\([i, c-wgt[i-1]]\\) \u3002</li> </ul> <p>\u5f9e\u800c\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\u8b8a\u70ba\uff1a</p> \\[ 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 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"<p>\u5c0d\u6bd4\u5169\u9053\u984c\u76ee\u7684\u7a0b\u5f0f\u78bc\uff0c\u72c0\u614b\u8f49\u79fb\u4e2d\u6709\u4e00\u8655\u5f9e \\(i-1\\) \u8b8a\u70ba \\(i\\) \uff0c\u5176\u9918\u5b8c\u5168\u4e00\u81f4\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig unbounded_knapsack.py<pre><code>def unbounded_knapsack_dp(wgt: list[int], val: list[int], cap: int) -&gt; int:\n \"\"\"\u5b8c\u5168\u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283\"\"\"\n n = len(wgt)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * (cap + 1) for _ in range(n + 1)]\n # \u72c0\u614b\u8f49\u79fb\n for i in range(1, n + 1):\n for c in range(1, cap + 1):\n if wgt[i - 1] &gt; c:\n # \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c]\n else:\n # \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n return dp[n][cap]\n</code></pre> unbounded_knapsack.cpp<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nint unboundedKnapsackDP(vector&lt;int&gt; &amp;wgt, vector&lt;int&gt; &amp;val, int cap) {\n int n = wgt.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(cap + 1, 0));\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n</code></pre> unbounded_knapsack.java<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nint unboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n + 1][cap + 1];\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n</code></pre> unbounded_knapsack.cs<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nint UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n int n = wgt.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n + 1, cap + 1];\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i, c] = dp[i - 1, c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n, cap];\n}\n</code></pre> unbounded_knapsack.go<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nfunc unboundedKnapsackDP(wgt, val []int, cap int) int {\n n := len(wgt)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n+1)\n for i := 0; i &lt;= n; i++ {\n dp[i] = make([]int, cap+1)\n }\n // \u72c0\u614b\u8f49\u79fb\n for i := 1; i &lt;= n; i++ {\n for c := 1; c &lt;= cap; c++ {\n if wgt[i-1] &gt; c {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i-1][c]\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i][c-wgt[i-1]]+val[i-1])))\n }\n }\n }\n return dp[n][cap]\n}\n</code></pre> unbounded_knapsack.swift<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nfunc unboundedKnapsackDP(wgt: [Int], val: [Int], cap: Int) -&gt; Int {\n let n = wgt.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n // \u72c0\u614b\u8f49\u79fb\n for i in 1 ... n {\n for c in 1 ... cap {\n if wgt[i - 1] &gt; c {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c]\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n }\n }\n }\n return dp[n][cap]\n}\n</code></pre> unbounded_knapsack.js<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nfunction unboundedKnapsackDP(wgt, val, cap) {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =&gt;\n Array.from({ length: cap + 1 }, () =&gt; 0)\n );\n // \u72c0\u614b\u8f49\u79fb\n for (let i = 1; i &lt;= n; i++) {\n for (let c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = Math.max(\n dp[i - 1][c],\n dp[i][c - wgt[i - 1]] + val[i - 1]\n );\n }\n }\n }\n return dp[n][cap];\n}\n</code></pre> unbounded_knapsack.ts<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nfunction unboundedKnapsackDP(\n wgt: Array&lt;number&gt;,\n val: Array&lt;number&gt;,\n cap: number\n): number {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =&gt;\n Array.from({ length: cap + 1 }, () =&gt; 0)\n );\n // \u72c0\u614b\u8f49\u79fb\n for (let i = 1; i &lt;= n; i++) {\n for (let c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = Math.max(\n dp[i - 1][c],\n dp[i][c - wgt[i - 1]] + val[i - 1]\n );\n }\n }\n }\n return dp[n][cap];\n}\n</code></pre> unbounded_knapsack.dart<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nint unboundedKnapsackDP(List&lt;int&gt; wgt, List&lt;int&gt; val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n List&lt;List&lt;int&gt;&gt; dp = List.generate(n + 1, (index) =&gt; List.filled(cap + 1, 0));\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n</code></pre> unbounded_knapsack.rs<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nfn unbounded_knapsack_dp(wgt: &amp;[i32], val: &amp;[i32], cap: usize) -&gt; i32 {\n let n = wgt.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; cap + 1]; n + 1];\n // \u72c0\u614b\u8f49\u79fb\n for i in 1..=n {\n for c in 1..=cap {\n if wgt[i - 1] &gt; c as i32 {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = std::cmp::max(dp[i - 1][c], dp[i][c - wgt[i - 1] as usize] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n</code></pre> unbounded_knapsack.c<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nint unboundedKnapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n int n = wgtSize;\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i &lt;= n; i++) {\n dp[i] = calloc(cap + 1, sizeof(int));\n }\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = myMax(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n int res = dp[n][cap];\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n for (int i = 0; i &lt;= n; i++) {\n free(dp[i]);\n }\n return res;\n}\n</code></pre> unbounded_knapsack.kt<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283 */\nfun unboundedKnapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {\n val n = wgt.size\n // \u521d\u59cb\u5316 dp \u8868\n val dp = Array(n + 1) { IntArray(cap + 1) }\n // \u72c0\u614b\u8f49\u79fb\n for (i in 1..n) {\n for (c in 1..cap) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c]\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + _val[i - 1])\n }\n }\n }\n return dp[n][cap]\n}\n</code></pre> unbounded_knapsack.rb<pre><code>[class]{}-[func]{unbounded_knapsack_dp}\n</code></pre> unbounded_knapsack.zig<pre><code>// \u5b8c\u5168\u80cc\u5305\uff1a\u52d5\u614b\u898f\u5283\nfn unboundedKnapsackDP(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\n comptime var n = wgt.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][cap + 1]i32{[_]i32{0} ** (cap + 1)} ** (n + 1);\n // \u72c0\u614b\u8f49\u79fb\n for (1..n + 1) |i| {\n for (1..cap + 1) |c| {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[i][c] = dp[i - 1][c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[i][c] = @max(dp[i - 1][c], dp[i][c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n }\n }\n }\n return dp[n][cap];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3","title":"3. \u00a0 \u7a7a\u9593\u6700\u4f73\u5316","text":"<p>\u7531\u65bc\u7576\u524d\u72c0\u614b\u662f\u5f9e\u5de6\u908a\u548c\u4e0a\u908a\u7684\u72c0\u614b\u8f49\u79fb\u800c\u4f86\u7684\uff0c\u56e0\u6b64\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u61c9\u8a72\u5c0d \\(dp\\) \u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u9032\u884c\u6b63\u5e8f\u8d70\u8a2a\u3002</p> <p>\u9019\u500b\u8d70\u8a2a\u9806\u5e8f\u8207 0-1 \u80cc\u5305\u6b63\u597d\u76f8\u53cd\u3002\u8acb\u85c9\u52a9\u5716 14-23 \u4f86\u7406\u89e3\u5169\u8005\u7684\u5340\u5225\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 14-23 \u00a0 \u5b8c\u5168\u80cc\u5305\u554f\u984c\u5728\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\u904e\u7a0b </p> <p>\u7a0b\u5f0f\u78bc\u5be6\u73fe\u6bd4\u8f03\u7c21\u55ae\uff0c\u50c5\u9700\u5c07\u9663\u5217 <code>dp</code> \u7684\u7b2c\u4e00\u7dad\u522a\u9664\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig unbounded_knapsack.py<pre><code>def unbounded_knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -&gt; int:\n \"\"\"\u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\"\"\"\n n = len(wgt)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [0] * (cap + 1)\n # \u72c0\u614b\u8f49\u79fb\n for i in range(1, n + 1):\n # \u6b63\u5e8f\u8d70\u8a2a\n for c in range(1, cap + 1):\n if wgt[i - 1] &gt; c:\n # \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c]\n else:\n # \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n return dp[cap]\n</code></pre> unbounded_knapsack.cpp<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint unboundedKnapsackDPComp(vector&lt;int&gt; &amp;wgt, vector&lt;int&gt; &amp;val, int cap) {\n int n = wgt.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector&lt;int&gt; dp(cap + 1, 0);\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n</code></pre> unbounded_knapsack.java<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[cap + 1];\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n</code></pre> unbounded_knapsack.cs<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n int n = wgt.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[cap + 1];\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n</code></pre> unbounded_knapsack.go<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc unboundedKnapsackDPComp(wgt, val []int, cap int) int {\n n := len(wgt)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, cap+1)\n // \u72c0\u614b\u8f49\u79fb\n for i := 1; i &lt;= n; i++ {\n for c := 1; c &lt;= cap; c++ {\n if wgt[i-1] &gt; c {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c]\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n }\n }\n }\n return dp[cap]\n}\n</code></pre> unbounded_knapsack.swift<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc unboundedKnapsackDPComp(wgt: [Int], val: [Int], cap: Int) -&gt; Int {\n let n = wgt.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: 0, count: cap + 1)\n // \u72c0\u614b\u8f49\u79fb\n for i in 1 ... n {\n for c in 1 ... cap {\n if wgt[i - 1] &gt; c {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c]\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n }\n }\n }\n return dp[cap]\n}\n</code></pre> unbounded_knapsack.js<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u72c0\u614b\u58d3\u7e2e\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction unboundedKnapsackDPComp(wgt, val, cap) {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: cap + 1 }, () =&gt; 0);\n // \u72c0\u614b\u8f49\u79fb\n for (let i = 1; i &lt;= n; i++) {\n for (let c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n</code></pre> unbounded_knapsack.ts<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u72c0\u614b\u58d3\u7e2e\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction unboundedKnapsackDPComp(\n wgt: Array&lt;number&gt;,\n val: Array&lt;number&gt;,\n cap: number\n): number {\n const n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: cap + 1 }, () =&gt; 0);\n // \u72c0\u614b\u8f49\u79fb\n for (let i = 1; i &lt;= n; i++) {\n for (let c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n</code></pre> unbounded_knapsack.dart<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint unboundedKnapsackDPComp(List&lt;int&gt; wgt, List&lt;int&gt; val, int cap) {\n int n = wgt.length;\n // \u521d\u59cb\u5316 dp \u8868\n List&lt;int&gt; dp = List.filled(cap + 1, 0);\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n</code></pre> unbounded_knapsack.rs<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfn unbounded_knapsack_dp_comp(wgt: &amp;[i32], val: &amp;[i32], cap: usize) -&gt; i32 {\n let n = wgt.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; cap + 1];\n // \u72c0\u614b\u8f49\u79fb\n for i in 1..=n {\n for c in 1..=cap {\n if wgt[i - 1] &gt; c as i32 {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n }\n }\n }\n dp[cap]\n}\n</code></pre> unbounded_knapsack.c<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint unboundedKnapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n int n = wgtSize;\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = calloc(cap + 1, sizeof(int));\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int c = 1; c &lt;= cap; c++) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n }\n }\n }\n int res = dp[cap];\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(dp);\n return res;\n}\n</code></pre> unbounded_knapsack.kt<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfun unboundedKnapsackDPComp(\n wgt: IntArray,\n _val: IntArray,\n cap: Int\n): Int {\n val n = wgt.size\n // \u521d\u59cb\u5316 dp \u8868\n val dp = IntArray(cap + 1)\n // \u72c0\u614b\u8f49\u79fb\n for (i in 1..n) {\n for (c in 1..cap) {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c]\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])\n }\n }\n }\n return dp[cap]\n}\n</code></pre> unbounded_knapsack.rb<pre><code>[class]{}-[func]{unbounded_knapsack_dp_comp}\n</code></pre> unbounded_knapsack.zig<pre><code>// \u5b8c\u5168\u80cc\u5305\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\nfn unboundedKnapsackDPComp(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\n comptime var n = wgt.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** (cap + 1);\n // \u72c0\u614b\u8f49\u79fb\n for (1..n + 1) |i| {\n for (1..cap + 1) |c| {\n if (wgt[i - 1] &gt; c) {\n // \u82e5\u8d85\u904e\u80cc\u5305\u5bb9\u91cf\uff0c\u5247\u4e0d\u9078\u7269\u54c1 i\n dp[c] = dp[c];\n } else {\n // \u4e0d\u9078\u548c\u9078\u7269\u54c1 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5927\u503c\n dp[c] = @max(dp[c], dp[c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n }\n }\n }\n return dp[cap];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1452","title":"14.5.2 \u00a0 \u96f6\u9322\u514c\u63db\u554f\u984c","text":"<p>\u80cc\u5305\u554f\u984c\u662f\u4e00\u5927\u985e\u52d5\u614b\u898f\u5283\u554f\u984c\u7684\u4ee3\u8868\uff0c\u5176\u64c1\u6709\u5f88\u591a\u8b8a\u7a2e\uff0c\u4f8b\u5982\u96f6\u9322\u514c\u63db\u554f\u984c\u3002</p> <p>Question</p> <p>\u7d66\u5b9a \\(n\\) \u7a2e\u786c\u5e63\uff0c\u7b2c \\(i\\) \u7a2e\u786c\u5e63\u7684\u9762\u503c\u70ba \\(coins[i - 1]\\) \uff0c\u76ee\u6a19\u91d1\u984d\u70ba \\(amt\\) \uff0c\u6bcf\u7a2e\u786c\u5e63\u53ef\u4ee5\u91cd\u8907\u9078\u53d6\uff0c\u554f\u80fd\u5920\u6e4a\u51fa\u76ee\u6a19\u91d1\u984d\u7684\u6700\u5c11\u786c\u5e63\u6578\u91cf\u3002\u5982\u679c\u7121\u6cd5\u6e4a\u51fa\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u8fd4\u56de \\(-1\\) \u3002\u793a\u4f8b\u5982\u5716 14-24 \u6240\u793a\u3002</p> <p></p> <p> \u5716 14-24 \u00a0 \u96f6\u9322\u514c\u63db\u554f\u984c\u7684\u793a\u4f8b\u8cc7\u6599 </p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_1","title":"1. \u00a0 \u52d5\u614b\u898f\u5283\u601d\u8def","text":"<p>\u96f6\u9322\u514c\u63db\u53ef\u4ee5\u770b\u4f5c\u5b8c\u5168\u80cc\u5305\u554f\u984c\u7684\u4e00\u7a2e\u7279\u6b8a\u60c5\u6cc1\uff0c\u5169\u8005\u5177\u6709\u4ee5\u4e0b\u95dc\u806f\u8207\u4e0d\u540c\u9ede\u3002</p> <ul> <li>\u5169\u9053\u984c\u53ef\u4ee5\u76f8\u4e92\u8f49\u63db\uff0c\u201c\u7269\u54c1\u201d\u5c0d\u61c9\u201c\u786c\u5e63\u201d\u3001\u201c\u7269\u54c1\u91cd\u91cf\u201d\u5c0d\u61c9\u201c\u786c\u5e63\u9762\u503c\u201d\u3001\u201c\u80cc\u5305\u5bb9\u91cf\u201d\u5c0d\u61c9\u201c\u76ee\u6a19\u91d1\u984d\u201d\u3002</li> <li>\u6700\u4f73\u5316\u76ee\u6a19\u76f8\u53cd\uff0c\u5b8c\u5168\u80cc\u5305\u554f\u984c\u662f\u8981\u6700\u5927\u5316\u7269\u54c1\u50f9\u503c\uff0c\u96f6\u9322\u514c\u63db\u554f\u984c\u662f\u8981\u6700\u5c0f\u5316\u786c\u5e63\u6578\u91cf\u3002</li> <li>\u5b8c\u5168\u80cc\u5305\u554f\u984c\u662f\u6c42\u201c\u4e0d\u8d85\u904e\u201d\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u89e3\uff0c\u96f6\u9322\u514c\u63db\u662f\u6c42\u201c\u6070\u597d\u201d\u6e4a\u5230\u76ee\u6a19\u91d1\u984d\u7684\u89e3\u3002</li> </ul> <p>\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f2a\u7684\u6c7a\u7b56\uff0c\u5b9a\u7fa9\u72c0\u614b\uff0c\u5f9e\u800c\u5f97\u5230 \\(dp\\) \u8868</p> <p>\u72c0\u614b \\([i, a]\\) \u5c0d\u61c9\u7684\u5b50\u554f\u984c\u70ba\uff1a\u524d \\(i\\) \u7a2e\u786c\u5e63\u80fd\u5920\u6e4a\u51fa\u91d1\u984d \\(a\\) \u7684\u6700\u5c11\u786c\u5e63\u6578\u91cf\uff0c\u8a18\u70ba \\(dp[i, a]\\) \u3002</p> <p>\u4e8c\u7dad \\(dp\\) \u8868\u7684\u5c3a\u5bf8\u70ba \\((n+1) \\times (amt+1)\\) \u3002</p> <p>\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u512a\u5b50\u7d50\u69cb\uff0c\u9032\u800c\u63a8\u5c0e\u51fa\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b</p> <p>\u672c\u984c\u8207\u5b8c\u5168\u80cc\u5305\u554f\u984c\u7684\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\u5b58\u5728\u4ee5\u4e0b\u5169\u9ede\u5dee\u7570\u3002</p> <ul> <li>\u672c\u984c\u8981\u6c42\u6700\u5c0f\u503c\uff0c\u56e0\u6b64\u9700\u5c07\u904b\u7b97\u5b50 \\(\\max()\\) \u66f4\u6539\u70ba \\(\\min()\\) \u3002</li> <li>\u6700\u4f73\u5316\u4e3b\u9ad4\u662f\u786c\u5e63\u6578\u91cf\u800c\u975e\u5546\u54c1\u50f9\u503c\uff0c\u56e0\u6b64\u5728\u9078\u4e2d\u786c\u5e63\u6642\u57f7\u884c \\(+1\\) \u5373\u53ef\u3002</li> </ul> \\[ dp[i, a] = \\min(dp[i-1, a], dp[i, a - coins[i-1]] + 1) \\] <p>\u7b2c\u4e09\u6b65\uff1a\u78ba\u5b9a\u908a\u754c\u689d\u4ef6\u548c\u72c0\u614b\u8f49\u79fb\u9806\u5e8f</p> <p>\u7576\u76ee\u6a19\u91d1\u984d\u70ba \\(0\\) \u6642\uff0c\u6e4a\u51fa\u5b83\u7684\u6700\u5c11\u786c\u5e63\u6578\u91cf\u70ba \\(0\\) \uff0c\u5373\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u7b49\u65bc \\(0\\) \u3002</p> <p>\u7576\u7121\u786c\u5e63\u6642\uff0c\u7121\u6cd5\u6e4a\u51fa\u4efb\u610f \\(&gt; 0\\) \u7684\u76ee\u6a19\u91d1\u984d\uff0c\u5373\u662f\u7121\u6548\u89e3\u3002\u70ba\u4f7f\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(\\min()\\) \u51fd\u5f0f\u80fd\u5920\u8b58\u5225\u4e26\u904e\u6ffe\u7121\u6548\u89e3\uff0c\u6211\u5011\u8003\u616e\u4f7f\u7528 \\(+ \\infty\\) \u4f86\u8868\u793a\u5b83\u5011\uff0c\u5373\u4ee4\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u65bc \\(+ \\infty\\) \u3002</p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_1","title":"2. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"<p>\u5927\u591a\u6578\u7a0b\u5f0f\u8a9e\u8a00\u4e26\u672a\u63d0\u4f9b \\(+ \\infty\\) \u8b8a\u6578\uff0c\u53ea\u80fd\u4f7f\u7528\u6574\u578b <code>int</code> \u7684\u6700\u5927\u503c\u4f86\u4ee3\u66ff\u3002\u800c\u9019\u53c8\u6703\u5c0e\u81f4\u5927\u6578\u8d8a\u754c\uff1a\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(+ 1\\) \u64cd\u4f5c\u53ef\u80fd\u767c\u751f\u6ea2\u4f4d\u3002</p> <p>\u70ba\u6b64\uff0c\u6211\u5011\u63a1\u7528\u6578\u5b57 \\(amt + 1\\) \u4f86\u8868\u793a\u7121\u6548\u89e3\uff0c\u56e0\u70ba\u6e4a\u51fa \\(amt\\) \u7684\u786c\u5e63\u6578\u91cf\u6700\u591a\u70ba \\(amt\\) \u3002\u6700\u5f8c\u8fd4\u56de\u524d\uff0c\u5224\u65b7 \\(dp[n, amt]\\) \u662f\u5426\u7b49\u65bc \\(amt + 1\\) \uff0c\u82e5\u662f\u5247\u8fd4\u56de \\(-1\\) \uff0c\u4ee3\u8868\u7121\u6cd5\u6e4a\u51fa\u76ee\u6a19\u91d1\u984d\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig coin_change.py<pre><code>def coin_change_dp(coins: list[int], amt: int) -&gt; int:\n \"\"\"\u96f6\u9322\u514c\u63db\uff1a\u52d5\u614b\u898f\u5283\"\"\"\n n = len(coins)\n MAX = amt + 1\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * (amt + 1) for _ in range(n + 1)]\n # \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for a in range(1, amt + 1):\n dp[0][a] = MAX\n # \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for i in range(1, n + 1):\n for a in range(1, amt + 1):\n if coins[i - 1] &gt; a:\n # \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a]\n else:\n # \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n return dp[n][amt] if dp[n][amt] != MAX else -1\n</code></pre> coin_change.cpp<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u52d5\u614b\u898f\u5283 */\nint coinChangeDP(vector&lt;int&gt; &amp;coins, int amt) {\n int n = coins.size();\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(amt + 1, 0));\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a &lt;= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n</code></pre> coin_change.java<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u52d5\u614b\u898f\u5283 */\nint coinChangeDP(int[] coins, int amt) {\n int n = coins.length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n + 1][amt + 1];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a &lt;= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n</code></pre> coin_change.cs<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u52d5\u614b\u898f\u5283 */\nint CoinChangeDP(int[] coins, int amt) {\n int n = coins.Length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n + 1, amt + 1];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a &lt;= amt; a++) {\n dp[0, a] = MAX;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i, a] = dp[i - 1, a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[i, a] = Math.Min(dp[i - 1, a], dp[i, a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n, amt] != MAX ? dp[n, amt] : -1;\n}\n</code></pre> coin_change.go<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u52d5\u614b\u898f\u5283 */\nfunc coinChangeDP(coins []int, amt int) int {\n n := len(coins)\n max := amt + 1\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n+1)\n for i := 0; i &lt;= n; i++ {\n dp[i] = make([]int, amt+1)\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for a := 1; a &lt;= amt; a++ {\n dp[0][a] = max\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for i := 1; i &lt;= n; i++ {\n for a := 1; a &lt;= amt; a++ {\n if coins[i-1] &gt; a {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i-1][a]\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[i][a] = int(math.Min(float64(dp[i-1][a]), float64(dp[i][a-coins[i-1]]+1)))\n }\n }\n }\n if dp[n][amt] != max {\n return dp[n][amt]\n }\n return -1\n}\n</code></pre> coin_change.swift<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u52d5\u614b\u898f\u5283 */\nfunc coinChangeDP(coins: [Int], amt: Int) -&gt; Int {\n let n = coins.count\n let MAX = amt + 1\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for a in 1 ... amt {\n dp[0][a] = MAX\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for i in 1 ... n {\n for a in 1 ... amt {\n if coins[i - 1] &gt; a {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a]\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n }\n }\n }\n return dp[n][amt] != MAX ? dp[n][amt] : -1\n}\n</code></pre> coin_change.js<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u52d5\u614b\u898f\u5283 */\nfunction coinChangeDP(coins, amt) {\n const n = coins.length;\n const MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =&gt;\n Array.from({ length: amt + 1 }, () =&gt; 0)\n );\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (let a = 1; a &lt;= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (let i = 1; i &lt;= n; i++) {\n for (let a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n</code></pre> coin_change.ts<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u52d5\u614b\u898f\u5283 */\nfunction coinChangeDP(coins: Array&lt;number&gt;, amt: number): number {\n const n = coins.length;\n const MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =&gt;\n Array.from({ length: amt + 1 }, () =&gt; 0)\n );\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (let a = 1; a &lt;= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (let i = 1; i &lt;= n; i++) {\n for (let a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n</code></pre> coin_change.dart<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u52d5\u614b\u898f\u5283 */\nint coinChangeDP(List&lt;int&gt; coins, int amt) {\n int n = coins.length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n List&lt;List&lt;int&gt;&gt; dp = List.generate(n + 1, (index) =&gt; List.filled(amt + 1, 0));\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a &lt;= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n</code></pre> coin_change.rs<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u52d5\u614b\u898f\u5283 */\nfn coin_change_dp(coins: &amp;[i32], amt: usize) -&gt; i32 {\n let n = coins.len();\n let max = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; amt + 1]; n + 1];\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for a in 1..=amt {\n dp[0][a] = max;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for i in 1..=n {\n for a in 1..=amt {\n if coins[i - 1] &gt; a as i32 {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[i][a] = std::cmp::min(dp[i - 1][a], dp[i][a - coins[i - 1] as usize] + 1);\n }\n }\n }\n if dp[n][amt] != max {\n return dp[n][amt] as i32;\n } else {\n -1\n }\n}\n</code></pre> coin_change.c<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u52d5\u614b\u898f\u5283 */\nint coinChangeDP(int coins[], int amt, int coinsSize) {\n int n = coinsSize;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i &lt;= n; i++) {\n dp[i] = calloc(amt + 1, sizeof(int));\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (int a = 1; a &lt;= amt; a++) {\n dp[0][a] = MAX;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[i][a] = myMin(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n }\n }\n }\n int res = dp[n][amt] != MAX ? dp[n][amt] : -1;\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n for (int i = 0; i &lt;= n; i++) {\n free(dp[i]);\n }\n free(dp);\n return res;\n}\n</code></pre> coin_change.kt<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u52d5\u614b\u898f\u5283 */\nfun coinChangeDP(coins: IntArray, amt: Int): Int {\n val n = coins.size\n val MAX = amt + 1\n // \u521d\u59cb\u5316 dp \u8868\n val dp = Array(n + 1) { IntArray(amt + 1) }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (a in 1..amt) {\n dp[0][a] = MAX\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (i in 1..n) {\n for (a in 1..amt) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a]\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n }\n }\n }\n return if (dp[n][amt] != MAX) dp[n][amt] else -1\n}\n</code></pre> coin_change.rb<pre><code>[class]{}-[func]{coin_change_dp}\n</code></pre> coin_change.zig<pre><code>// \u96f6\u9322\u514c\u63db\uff1a\u52d5\u614b\u898f\u5283\nfn coinChangeDP(comptime coins: []i32, comptime amt: usize) i32 {\n comptime var n = coins.len;\n comptime var max = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][amt + 1]i32{[_]i32{0} ** (amt + 1)} ** (n + 1);\n // \u72c0\u614b\u8f49\u79fb\uff1a\u9996\u884c\u9996\u5217\n for (1..amt + 1) |a| {\n dp[0][a] = max;\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for (1..n + 1) |i| {\n for (1..amt + 1) |a| {\n if (coins[i - 1] &gt; @as(i32, @intCast(a))) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[i][a] = @min(dp[i - 1][a], dp[i][a - @as(usize, @intCast(coins[i - 1]))] + 1);\n }\n }\n }\n if (dp[n][amt] != max) {\n return @intCast(dp[n][amt]);\n } else {\n return -1;\n }\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5716 14-25 \u5c55\u793a\u4e86\u96f6\u9322\u514c\u63db\u7684\u52d5\u614b\u898f\u5283\u904e\u7a0b\uff0c\u548c\u5b8c\u5168\u80cc\u5305\u554f\u984c\u975e\u5e38\u76f8\u4f3c\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt;&lt;10&gt;&lt;11&gt;&lt;12&gt;&lt;13&gt;&lt;14&gt;&lt;15&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 14-25 \u00a0 \u96f6\u9322\u514c\u63db\u554f\u984c\u7684\u52d5\u614b\u898f\u5283\u904e\u7a0b </p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_1","title":"3. \u00a0 \u7a7a\u9593\u6700\u4f73\u5316","text":"<p>\u96f6\u9322\u514c\u63db\u7684\u7a7a\u9593\u6700\u4f73\u5316\u7684\u8655\u7406\u65b9\u5f0f\u548c\u5b8c\u5168\u80cc\u5305\u554f\u984c\u4e00\u81f4\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig coin_change.py<pre><code>def coin_change_dp_comp(coins: list[int], amt: int) -&gt; int:\n \"\"\"\u96f6\u9322\u514c\u63db\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\"\"\"\n n = len(coins)\n MAX = amt + 1\n # \u521d\u59cb\u5316 dp \u8868\n dp = [MAX] * (amt + 1)\n dp[0] = 0\n # \u72c0\u614b\u8f49\u79fb\n for i in range(1, n + 1):\n # \u6b63\u5e8f\u8d70\u8a2a\n for a in range(1, amt + 1):\n if coins[i - 1] &gt; a:\n # \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a]\n else:\n # \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n return dp[amt] if dp[amt] != MAX else -1\n</code></pre> coin_change.cpp<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint coinChangeDPComp(vector&lt;int&gt; &amp;coins, int amt) {\n int n = coins.size();\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n vector&lt;int&gt; dp(amt + 1, MAX);\n dp[0] = 0;\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1;\n}\n</code></pre> coin_change.java<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint coinChangeDPComp(int[] coins, int amt) {\n int n = coins.length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[amt + 1];\n Arrays.fill(dp, MAX);\n dp[0] = 0;\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1;\n}\n</code></pre> coin_change.cs<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint CoinChangeDPComp(int[] coins, int amt) {\n int n = coins.Length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[amt + 1];\n Array.Fill(dp, MAX);\n dp[0] = 0;\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[a] = Math.Min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1;\n}\n</code></pre> coin_change.go<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u52d5\u614b\u898f\u5283 */\nfunc coinChangeDPComp(coins []int, amt int) int {\n n := len(coins)\n max := amt + 1\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, amt+1)\n for i := 1; i &lt;= amt; i++ {\n dp[i] = max\n }\n // \u72c0\u614b\u8f49\u79fb\n for i := 1; i &lt;= n; i++ {\n // \u6b63\u5e8f\u8d70\u8a2a\n for a := 1; a &lt;= amt; a++ {\n if coins[i-1] &gt; a {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a]\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[a] = int(math.Min(float64(dp[a]), float64(dp[a-coins[i-1]]+1)))\n }\n }\n }\n if dp[amt] != max {\n return dp[amt]\n }\n return -1\n}\n</code></pre> coin_change.swift<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc coinChangeDPComp(coins: [Int], amt: Int) -&gt; Int {\n let n = coins.count\n let MAX = amt + 1\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: MAX, count: amt + 1)\n dp[0] = 0\n // \u72c0\u614b\u8f49\u79fb\n for i in 1 ... n {\n for a in 1 ... amt {\n if coins[i - 1] &gt; a {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a]\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1\n}\n</code></pre> coin_change.js<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u72c0\u614b\u58d3\u7e2e\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction coinChangeDPComp(coins, amt) {\n const n = coins.length;\n const MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: amt + 1 }, () =&gt; MAX);\n dp[0] = 0;\n // \u72c0\u614b\u8f49\u79fb\n for (let i = 1; i &lt;= n; i++) {\n for (let a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] !== MAX ? dp[amt] : -1;\n}\n</code></pre> coin_change.ts<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u72c0\u614b\u58d3\u7e2e\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction coinChangeDPComp(coins: Array&lt;number&gt;, amt: number): number {\n const n = coins.length;\n const MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: amt + 1 }, () =&gt; MAX);\n dp[0] = 0;\n // \u72c0\u614b\u8f49\u79fb\n for (let i = 1; i &lt;= n; i++) {\n for (let a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] !== MAX ? dp[amt] : -1;\n}\n</code></pre> coin_change.dart<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint coinChangeDPComp(List&lt;int&gt; coins, int amt) {\n int n = coins.length;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n List&lt;int&gt; dp = List.filled(amt + 1, MAX);\n dp[0] = 0;\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n return dp[amt] != MAX ? dp[amt] : -1;\n}\n</code></pre> coin_change.rs<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfn coin_change_dp_comp(coins: &amp;[i32], amt: usize) -&gt; i32 {\n let n = coins.len();\n let max = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; amt + 1];\n dp.fill(max);\n dp[0] = 0;\n // \u72c0\u614b\u8f49\u79fb\n for i in 1..=n {\n for a in 1..=amt {\n if coins[i - 1] &gt; a as i32 {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[a] = std::cmp::min(dp[a], dp[a - coins[i - 1] as usize] + 1);\n }\n }\n }\n if dp[amt] != max {\n return dp[amt] as i32;\n } else {\n -1\n }\n}\n</code></pre> coin_change.c<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint coinChangeDPComp(int coins[], int amt, int coinsSize) {\n int n = coinsSize;\n int MAX = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = malloc((amt + 1) * sizeof(int));\n for (int j = 1; j &lt;= amt; j++) {\n dp[j] = MAX;\n } \n dp[0] = 0;\n\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[a] = myMin(dp[a], dp[a - coins[i - 1]] + 1);\n }\n }\n }\n int res = dp[amt] != MAX ? dp[amt] : -1;\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(dp);\n return res;\n}\n</code></pre> coin_change.kt<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfun coinChangeDPComp(coins: IntArray, amt: Int): Int {\n val n = coins.size\n val MAX = amt + 1\n // \u521d\u59cb\u5316 dp \u8868\n val dp = IntArray(amt + 1)\n dp.fill(MAX)\n dp[0] = 0\n // \u72c0\u614b\u8f49\u79fb\n for (i in 1..n) {\n for (a in 1..amt) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a]\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n }\n }\n }\n return if (dp[amt] != MAX) dp[amt] else -1\n}\n</code></pre> coin_change.rb<pre><code>[class]{}-[func]{coin_change_dp_comp}\n</code></pre> coin_change.zig<pre><code>// \u96f6\u9322\u514c\u63db\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\nfn coinChangeDPComp(comptime coins: []i32, comptime amt: usize) i32 {\n comptime var n = coins.len;\n comptime var max = amt + 1;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** (amt + 1);\n @memset(&amp;dp, max);\n dp[0] = 0;\n // \u72c0\u614b\u8f49\u79fb\n for (1..n + 1) |i| {\n for (1..amt + 1) |a| {\n if (coins[i - 1] &gt; @as(i32, @intCast(a))) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[a] = @min(dp[a], dp[a - @as(usize, @intCast(coins[i - 1]))] + 1);\n }\n }\n }\n if (dp[amt] != max) {\n return @intCast(dp[amt]);\n } else {\n return -1;\n }\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1453-ii","title":"14.5.3 \u00a0 \u96f6\u9322\u514c\u63db\u554f\u984c II","text":"<p>Question</p> <p>\u7d66\u5b9a \\(n\\) \u7a2e\u786c\u5e63\uff0c\u7b2c \\(i\\) \u7a2e\u786c\u5e63\u7684\u9762\u503c\u70ba \\(coins[i - 1]\\) \uff0c\u76ee\u6a19\u91d1\u984d\u70ba \\(amt\\) \uff0c\u6bcf\u7a2e\u786c\u5e63\u53ef\u4ee5\u91cd\u8907\u9078\u53d6\uff0c\u554f\u6e4a\u51fa\u76ee\u6a19\u91d1\u984d\u7684\u786c\u5e63\u7d44\u5408\u6578\u91cf\u3002\u793a\u4f8b\u5982\u5716 14-26 \u6240\u793a\u3002</p> <p></p> <p> \u5716 14-26 \u00a0 \u96f6\u9322\u514c\u63db\u554f\u984c II \u7684\u793a\u4f8b\u8cc7\u6599 </p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_2","title":"1. \u00a0 \u52d5\u614b\u898f\u5283\u601d\u8def","text":"<p>\u76f8\u6bd4\u65bc\u4e0a\u4e00\u984c\uff0c\u672c\u984c\u76ee\u6a19\u662f\u6c42\u7d44\u5408\u6578\u91cf\uff0c\u56e0\u6b64\u5b50\u554f\u984c\u8b8a\u70ba\uff1a\u524d \\(i\\) \u7a2e\u786c\u5e63\u80fd\u5920\u6e4a\u51fa\u91d1\u984d \\(a\\) \u7684\u7d44\u5408\u6578\u91cf\u3002\u800c \\(dp\\) \u8868\u4ecd\u7136\u662f\u5c3a\u5bf8\u70ba \\((n+1) \\times (amt + 1)\\) \u7684\u4e8c\u7dad\u77e9\u9663\u3002</p> <p>\u7576\u524d\u72c0\u614b\u7684\u7d44\u5408\u6578\u91cf\u7b49\u65bc\u4e0d\u9078\u7576\u524d\u786c\u5e63\u8207\u9078\u7576\u524d\u786c\u5e63\u9019\u5169\u7a2e\u6c7a\u7b56\u7684\u7d44\u5408\u6578\u91cf\u4e4b\u548c\u3002\u72c0\u614b\u8f49\u79fb\u65b9\u7a0b\u70ba\uff1a</p> \\[ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]] \\] <p>\u7576\u76ee\u6a19\u91d1\u984d\u70ba \\(0\\) \u6642\uff0c\u7121\u9808\u9078\u64c7\u4efb\u4f55\u786c\u5e63\u5373\u53ef\u6e4a\u51fa\u76ee\u6a19\u91d1\u984d\uff0c\u56e0\u6b64\u61c9\u5c07\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u521d\u59cb\u5316\u70ba \\(1\\) \u3002\u7576\u7121\u786c\u5e63\u6642\uff0c\u7121\u6cd5\u6e4a\u51fa\u4efb\u4f55 \\(&gt;0\\) \u7684\u76ee\u6a19\u91d1\u984d\uff0c\u56e0\u6b64\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u65bc \\(0\\) \u3002</p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_2","title":"2. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig coin_change_ii.py<pre><code>def coin_change_ii_dp(coins: list[int], amt: int) -&gt; int:\n \"\"\"\u96f6\u9322\u514c\u63db II\uff1a\u52d5\u614b\u898f\u5283\"\"\"\n n = len(coins)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [[0] * (amt + 1) for _ in range(n + 1)]\n # \u521d\u59cb\u5316\u9996\u5217\n for i in range(n + 1):\n dp[i][0] = 1\n # \u72c0\u614b\u8f49\u79fb\n for i in range(1, n + 1):\n for a in range(1, amt + 1):\n if coins[i - 1] &gt; a:\n # \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a]\n else:\n # \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n return dp[n][amt]\n</code></pre> coin_change_ii.cpp<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u52d5\u614b\u898f\u5283 */\nint coinChangeIIDP(vector&lt;int&gt; &amp;coins, int amt) {\n int n = coins.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(amt + 1, 0));\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i &lt;= n; i++) {\n dp[i][0] = 1;\n }\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n</code></pre> coin_change_ii.java<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u52d5\u614b\u898f\u5283 */\nint coinChangeIIDP(int[] coins, int amt) {\n int n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[][] dp = new int[n + 1][amt + 1];\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i &lt;= n; i++) {\n dp[i][0] = 1;\n }\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n</code></pre> coin_change_ii.cs<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u52d5\u614b\u898f\u5283 */\nint CoinChangeIIDP(int[] coins, int amt) {\n int n = coins.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[,] dp = new int[n + 1, amt + 1];\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i &lt;= n; i++) {\n dp[i, 0] = 1;\n }\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i, a] = dp[i - 1, a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[i, a] = dp[i - 1, a] + dp[i, a - coins[i - 1]];\n }\n }\n }\n return dp[n, amt];\n}\n</code></pre> coin_change_ii.go<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u52d5\u614b\u898f\u5283 */\nfunc coinChangeIIDP(coins []int, amt int) int {\n n := len(coins)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([][]int, n+1)\n for i := 0; i &lt;= n; i++ {\n dp[i] = make([]int, amt+1)\n }\n // \u521d\u59cb\u5316\u9996\u5217\n for i := 0; i &lt;= n; i++ {\n dp[i][0] = 1\n }\n // \u72c0\u614b\u8f49\u79fb\uff1a\u5176\u9918\u884c\u548c\u5217\n for i := 1; i &lt;= n; i++ {\n for a := 1; a &lt;= amt; a++ {\n if coins[i-1] &gt; a {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i-1][a]\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i-1][a] + dp[i][a-coins[i-1]]\n }\n }\n }\n return dp[n][amt]\n}\n</code></pre> coin_change_ii.swift<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u52d5\u614b\u898f\u5283 */\nfunc coinChangeIIDP(coins: [Int], amt: Int) -&gt; Int {\n let n = coins.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n // \u521d\u59cb\u5316\u9996\u5217\n for i in 0 ... n {\n dp[i][0] = 1\n }\n // \u72c0\u614b\u8f49\u79fb\n for i in 1 ... n {\n for a in 1 ... amt {\n if coins[i - 1] &gt; a {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a]\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n }\n }\n }\n return dp[n][amt]\n}\n</code></pre> coin_change_ii.js<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u52d5\u614b\u898f\u5283 */\nfunction coinChangeIIDP(coins, amt) {\n const n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =&gt;\n Array.from({ length: amt + 1 }, () =&gt; 0)\n );\n // \u521d\u59cb\u5316\u9996\u5217\n for (let i = 0; i &lt;= n; i++) {\n dp[i][0] = 1;\n }\n // \u72c0\u614b\u8f49\u79fb\n for (let i = 1; i &lt;= n; i++) {\n for (let a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n</code></pre> coin_change_ii.ts<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u52d5\u614b\u898f\u5283 */\nfunction coinChangeIIDP(coins: Array&lt;number&gt;, amt: number): number {\n const n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: n + 1 }, () =&gt;\n Array.from({ length: amt + 1 }, () =&gt; 0)\n );\n // \u521d\u59cb\u5316\u9996\u5217\n for (let i = 0; i &lt;= n; i++) {\n dp[i][0] = 1;\n }\n // \u72c0\u614b\u8f49\u79fb\n for (let i = 1; i &lt;= n; i++) {\n for (let a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n</code></pre> coin_change_ii.dart<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u52d5\u614b\u898f\u5283 */\nint coinChangeIIDP(List&lt;int&gt; coins, int amt) {\n int n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n List&lt;List&lt;int&gt;&gt; dp = List.generate(n + 1, (index) =&gt; List.filled(amt + 1, 0));\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i &lt;= n; i++) {\n dp[i][0] = 1;\n }\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n return dp[n][amt];\n}\n</code></pre> coin_change_ii.rs<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u52d5\u614b\u898f\u5283 */\nfn coin_change_ii_dp(coins: &amp;[i32], amt: usize) -&gt; i32 {\n let n = coins.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![vec![0; amt + 1]; n + 1];\n // \u521d\u59cb\u5316\u9996\u5217\n for i in 0..=n {\n dp[i][0] = 1;\n }\n // \u72c0\u614b\u8f49\u79fb\n for i in 1..=n {\n for a in 1..=amt {\n if coins[i - 1] &gt; a as i32 {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1] as usize];\n }\n }\n }\n dp[n][amt]\n}\n</code></pre> coin_change_ii.c<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u52d5\u614b\u898f\u5283 */\nint coinChangeIIDP(int coins[], int amt, int coinsSize) {\n int n = coinsSize;\n // \u521d\u59cb\u5316 dp \u8868\n int **dp = malloc((n + 1) * sizeof(int *));\n for (int i = 0; i &lt;= n; i++) {\n dp[i] = calloc(amt + 1, sizeof(int));\n }\n // \u521d\u59cb\u5316\u9996\u5217\n for (int i = 0; i &lt;= n; i++) {\n dp[i][0] = 1;\n }\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n }\n }\n }\n int res = dp[n][amt];\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n for (int i = 0; i &lt;= n; i++) {\n free(dp[i]);\n }\n free(dp);\n return res;\n}\n</code></pre> coin_change_ii.kt<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u52d5\u614b\u898f\u5283 */\nfun coinChangeIIDP(coins: IntArray, amt: Int): Int {\n val n = coins.size\n // \u521d\u59cb\u5316 dp \u8868\n val dp = Array(n + 1) { IntArray(amt + 1) }\n // \u521d\u59cb\u5316\u9996\u5217\n for (i in 0..n) {\n dp[i][0] = 1\n }\n // \u72c0\u614b\u8f49\u79fb\n for (i in 1..n) {\n for (a in 1..amt) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a]\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n }\n }\n }\n return dp[n][amt]\n}\n</code></pre> coin_change_ii.rb<pre><code>[class]{}-[func]{coin_change_ii_dp}\n</code></pre> coin_change_ii.zig<pre><code>// \u96f6\u9322\u514c\u63db II\uff1a\u52d5\u614b\u898f\u5283\nfn coinChangeIIDP(comptime coins: []i32, comptime amt: usize) i32 {\n comptime var n = coins.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_][amt + 1]i32{[_]i32{0} ** (amt + 1)} ** (n + 1);\n // \u521d\u59cb\u5316\u9996\u5217\n for (0..n + 1) |i| {\n dp[i][0] = 1;\n }\n // \u72c0\u614b\u8f49\u79fb\n for (1..n + 1) |i| {\n for (1..amt + 1) |a| {\n if (coins[i - 1] &gt; @as(i32, @intCast(a))) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[i][a] = dp[i - 1][a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[i][a] = dp[i - 1][a] + dp[i][a - @as(usize, @intCast(coins[i - 1]))];\n }\n }\n }\n return dp[n][amt];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_2","title":"3. \u00a0 \u7a7a\u9593\u6700\u4f73\u5316","text":"<p>\u7a7a\u9593\u6700\u4f73\u5316\u8655\u7406\u65b9\u5f0f\u76f8\u540c\uff0c\u522a\u9664\u786c\u5e63\u7dad\u5ea6\u5373\u53ef\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig coin_change_ii.py<pre><code>def coin_change_ii_dp_comp(coins: list[int], amt: int) -&gt; int:\n \"\"\"\u96f6\u9322\u514c\u63db II\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\"\"\"\n n = len(coins)\n # \u521d\u59cb\u5316 dp \u8868\n dp = [0] * (amt + 1)\n dp[0] = 1\n # \u72c0\u614b\u8f49\u79fb\n for i in range(1, n + 1):\n # \u6b63\u5e8f\u8d70\u8a2a\n for a in range(1, amt + 1):\n if coins[i - 1] &gt; a:\n # \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a]\n else:\n # \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]]\n return dp[amt]\n</code></pre> coin_change_ii.cpp<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint coinChangeIIDPComp(vector&lt;int&gt; &amp;coins, int amt) {\n int n = coins.size();\n // \u521d\u59cb\u5316 dp \u8868\n vector&lt;int&gt; dp(amt + 1, 0);\n dp[0] = 1;\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n</code></pre> coin_change_ii.java<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint coinChangeIIDPComp(int[] coins, int amt) {\n int n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[amt + 1];\n dp[0] = 1;\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n</code></pre> coin_change_ii.cs<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint CoinChangeIIDPComp(int[] coins, int amt) {\n int n = coins.Length;\n // \u521d\u59cb\u5316 dp \u8868\n int[] dp = new int[amt + 1];\n dp[0] = 1;\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n</code></pre> coin_change_ii.go<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc coinChangeIIDPComp(coins []int, amt int) int {\n n := len(coins)\n // \u521d\u59cb\u5316 dp \u8868\n dp := make([]int, amt+1)\n dp[0] = 1\n // \u72c0\u614b\u8f49\u79fb\n for i := 1; i &lt;= n; i++ {\n // \u6b63\u5e8f\u8d70\u8a2a\n for a := 1; a &lt;= amt; a++ {\n if coins[i-1] &gt; a {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a]\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a-coins[i-1]]\n }\n }\n }\n return dp[amt]\n}\n</code></pre> coin_change_ii.swift<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunc coinChangeIIDPComp(coins: [Int], amt: Int) -&gt; Int {\n let n = coins.count\n // \u521d\u59cb\u5316 dp \u8868\n var dp = Array(repeating: 0, count: amt + 1)\n dp[0] = 1\n // \u72c0\u614b\u8f49\u79fb\n for i in 1 ... n {\n for a in 1 ... amt {\n if coins[i - 1] &gt; a {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a]\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]]\n }\n }\n }\n return dp[amt]\n}\n</code></pre> coin_change_ii.js<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u72c0\u614b\u58d3\u7e2e\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction coinChangeIIDPComp(coins, amt) {\n const n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: amt + 1 }, () =&gt; 0);\n dp[0] = 1;\n // \u72c0\u614b\u8f49\u79fb\n for (let i = 1; i &lt;= n; i++) {\n for (let a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n</code></pre> coin_change_ii.ts<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u72c0\u614b\u58d3\u7e2e\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfunction coinChangeIIDPComp(coins: Array&lt;number&gt;, amt: number): number {\n const n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n const dp = Array.from({ length: amt + 1 }, () =&gt; 0);\n dp[0] = 1;\n // \u72c0\u614b\u8f49\u79fb\n for (let i = 1; i &lt;= n; i++) {\n for (let a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n</code></pre> coin_change_ii.dart<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint coinChangeIIDPComp(List&lt;int&gt; coins, int amt) {\n int n = coins.length;\n // \u521d\u59cb\u5316 dp \u8868\n List&lt;int&gt; dp = List.filled(amt + 1, 0);\n dp[0] = 1;\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n return dp[amt];\n}\n</code></pre> coin_change_ii.rs<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfn coin_change_ii_dp_comp(coins: &amp;[i32], amt: usize) -&gt; i32 {\n let n = coins.len();\n // \u521d\u59cb\u5316 dp \u8868\n let mut dp = vec![0; amt + 1];\n dp[0] = 1;\n // \u72c0\u614b\u8f49\u79fb\n for i in 1..=n {\n for a in 1..=amt {\n if coins[i - 1] &gt; a as i32 {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1] as usize];\n }\n }\n }\n dp[amt]\n}\n</code></pre> coin_change_ii.c<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nint coinChangeIIDPComp(int coins[], int amt, int coinsSize) {\n int n = coinsSize;\n // \u521d\u59cb\u5316 dp \u8868\n int *dp = calloc(amt + 1, sizeof(int));\n dp[0] = 1;\n // \u72c0\u614b\u8f49\u79fb\n for (int i = 1; i &lt;= n; i++) {\n for (int a = 1; a &lt;= amt; a++) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]];\n }\n }\n }\n int res = dp[amt];\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(dp);\n return res;\n}\n</code></pre> coin_change_ii.kt<pre><code>/* \u96f6\u9322\u514c\u63db II\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283 */\nfun coinChangeIIDPComp(coins: IntArray, amt: Int): Int {\n val n = coins.size\n // \u521d\u59cb\u5316 dp \u8868\n val dp = IntArray(amt + 1)\n dp[0] = 1\n // \u72c0\u614b\u8f49\u79fb\n for (i in 1..n) {\n for (a in 1..amt) {\n if (coins[i - 1] &gt; a) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a]\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u4e4b\u548c\n dp[a] = dp[a] + dp[a - coins[i - 1]]\n }\n }\n }\n return dp[amt]\n}\n</code></pre> coin_change_ii.rb<pre><code>[class]{}-[func]{coin_change_ii_dp_comp}\n</code></pre> coin_change_ii.zig<pre><code>// \u96f6\u9322\u514c\u63db II\uff1a\u7a7a\u9593\u6700\u4f73\u5316\u5f8c\u7684\u52d5\u614b\u898f\u5283\nfn coinChangeIIDPComp(comptime coins: []i32, comptime amt: usize) i32 {\n comptime var n = coins.len;\n // \u521d\u59cb\u5316 dp \u8868\n var dp = [_]i32{0} ** (amt + 1);\n dp[0] = 1;\n // \u72c0\u614b\u8f49\u79fb\n for (1..n + 1) |i| {\n for (1..amt + 1) |a| {\n if (coins[i - 1] &gt; @as(i32, @intCast(a))) {\n // \u82e5\u8d85\u904e\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u4e0d\u9078\u786c\u5e63 i\n dp[a] = dp[a];\n } else {\n // \u4e0d\u9078\u548c\u9078\u786c\u5e63 i \u9019\u5169\u7a2e\u65b9\u6848\u7684\u8f03\u5c0f\u503c\n dp[a] = dp[a] + dp[a - @as(usize, @intCast(coins[i - 1]))];\n }\n }\n }\n return dp[amt];\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_graph/","title":"\u7b2c 9 \u7ae0 \u00a0 \u5716","text":"<p>Abstract</p> <p>\u5728\u751f\u547d\u65c5\u9014\u4e2d\uff0c\u6211\u5011\u5c31\u50cf\u662f\u4e00\u500b\u500b\u7bc0\u9ede\uff0c\u88ab\u7121\u6578\u770b\u4e0d\u898b\u7684\u908a\u76f8\u9023\u3002</p> <p>\u6bcf\u4e00\u6b21\u7684\u76f8\u8b58\u8207\u76f8\u96e2\uff0c\u90fd\u5728\u9019\u5f35\u5de8\u5927\u7684\u7db2\u8def\u5716\u4e2d\u7559\u4e0b\u7368\u7279\u7684\u5370\u8a18\u3002</p>"},{"location":"chapter_graph/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>9.1 \u00a0 \u5716</li> <li>9.2 \u00a0 \u5716\u57fa\u790e\u64cd\u4f5c</li> <li>9.3 \u00a0 \u5716\u7684\u8d70\u8a2a</li> <li>9.4 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_graph/graph/","title":"9.1 \u00a0 \u5716","text":"<p>\u5716\uff08graph\uff09\u662f\u4e00\u7a2e\u975e\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\uff0c\u7531\u9802\u9ede\uff08vertex\uff09\u548c\u908a\uff08edge\uff09\u7d44\u6210\u3002\u6211\u5011\u53ef\u4ee5\u5c07\u5716 \\(G\\) \u62bd\u8c61\u5730\u8868\u793a\u70ba\u4e00\u7d44\u9802\u9ede \\(V\\) \u548c\u4e00\u7d44\u908a \\(E\\) \u7684\u96c6\u5408\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u4e00\u500b\u5305\u542b 5 \u500b\u9802\u9ede\u548c 7 \u689d\u908a\u7684\u5716\u3002</p> \\[ \\begin{aligned} V &amp; = \\{ 1, 2, 3, 4, 5 \\} \\newline E &amp; = \\{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \\} \\newline G &amp; = \\{ V, E \\} \\newline \\end{aligned} \\] <p>\u5982\u679c\u5c07\u9802\u9ede\u770b\u4f5c\u7bc0\u9ede\uff0c\u5c07\u908a\u770b\u4f5c\u9023\u7dda\u5404\u500b\u7bc0\u9ede\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u5c07\u5716\u770b\u4f5c\u4e00\u7a2e\u5f9e\u93c8\u7d50\u4e32\u5217\u62d3\u5c55\u800c\u4f86\u7684\u8cc7\u6599\u7d50\u69cb\u3002\u5982\u5716 9-1 \u6240\u793a\uff0c\u76f8\u8f03\u65bc\u7dda\u6027\u95dc\u4fc2\uff08\u93c8\u7d50\u4e32\u5217\uff09\u548c\u5206\u6cbb\u95dc\u4fc2\uff08\u6a39\uff09\uff0c\u7db2\u8def\u95dc\u4fc2\uff08\u5716\uff09\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\uff0c\u56e0\u800c\u66f4\u70ba\u8907\u96dc\u3002</p> <p></p> <p> \u5716 9-1 \u00a0 \u93c8\u7d50\u4e32\u5217\u3001\u6a39\u3001\u5716\u4e4b\u9593\u7684\u95dc\u4fc2 </p>"},{"location":"chapter_graph/graph/#911","title":"9.1.1 \u00a0 \u5716\u7684\u5e38\u898b\u578b\u5225\u8207\u8853\u8a9e","text":"<p>\u6839\u64da\u908a\u662f\u5426\u5177\u6709\u65b9\u5411\uff0c\u53ef\u5206\u70ba\u7121\u5411\u5716\uff08undirected graph\uff09\u548c\u6709\u5411\u5716\uff08directed graph\uff09\uff0c\u5982\u5716 9-2 \u6240\u793a\u3002</p> <ul> <li>\u5728\u7121\u5411\u5716\u4e2d\uff0c\u908a\u8868\u793a\u5169\u9802\u9ede\u4e4b\u9593\u7684\u201c\u96d9\u5411\u201d\u9023\u7dda\u95dc\u4fc2\uff0c\u4f8b\u5982\u5fae\u4fe1\u6216 QQ \u4e2d\u7684\u201c\u597d\u53cb\u95dc\u4fc2\u201d\u3002</li> <li>\u5728\u6709\u5411\u5716\u4e2d\uff0c\u908a\u5177\u6709\u65b9\u5411\u6027\uff0c\u5373 \\(A \\rightarrow B\\) \u548c \\(A \\leftarrow B\\) \u5169\u500b\u65b9\u5411\u7684\u908a\u662f\u76f8\u4e92\u7368\u7acb\u7684\uff0c\u4f8b\u5982\u5fae\u535a\u6216\u6296\u97f3\u4e0a\u7684\u201c\u95dc\u6ce8\u201d\u8207\u201c\u88ab\u95dc\u6ce8\u201d\u95dc\u4fc2\u3002</li> </ul> <p></p> <p> \u5716 9-2 \u00a0 \u6709\u5411\u5716\u8207\u7121\u5411\u5716 </p> <p>\u6839\u64da\u6240\u6709\u9802\u9ede\u662f\u5426\u9023\u901a\uff0c\u53ef\u5206\u70ba\u9023\u901a\u5716\uff08connected graph\uff09\u548c\u975e\u9023\u901a\u5716\uff08disconnected graph\uff09\uff0c\u5982\u5716 9-3 \u6240\u793a\u3002</p> <ul> <li>\u5c0d\u65bc\u9023\u901a\u5716\uff0c\u5f9e\u67d0\u500b\u9802\u9ede\u51fa\u767c\uff0c\u53ef\u4ee5\u5230\u9054\u5176\u9918\u4efb\u610f\u9802\u9ede\u3002</li> <li>\u5c0d\u65bc\u975e\u9023\u901a\u5716\uff0c\u5f9e\u67d0\u500b\u9802\u9ede\u51fa\u767c\uff0c\u81f3\u5c11\u6709\u4e00\u500b\u9802\u9ede\u7121\u6cd5\u5230\u9054\u3002</li> </ul> <p></p> <p> \u5716 9-3 \u00a0 \u9023\u901a\u5716\u8207\u975e\u9023\u901a\u5716 </p> <p>\u6211\u5011\u9084\u53ef\u4ee5\u70ba\u908a\u65b0\u589e\u201c\u6b0a\u91cd\u201d\u8b8a\u6578\uff0c\u5f9e\u800c\u5f97\u5230\u5982\u5716 9-4 \u6240\u793a\u7684\u6709\u6b0a\u5716\uff08weighted graph\uff09\u3002\u4f8b\u5982\u5728\u300a\u738b\u8005\u69ae\u8000\u300b\u7b49\u624b\u904a\u4e2d\uff0c\u7cfb\u7d71\u6703\u6839\u64da\u5171\u540c\u904a\u6232\u6642\u9593\u4f86\u8a08\u7b97\u73a9\u5bb6\u4e4b\u9593\u7684\u201c\u89aa\u5bc6\u5ea6\u201d\uff0c\u9019\u7a2e\u89aa\u5bc6\u5ea6\u7db2\u8def\u5c31\u53ef\u4ee5\u7528\u6709\u6b0a\u5716\u4f86\u8868\u793a\u3002</p> <p></p> <p> \u5716 9-4 \u00a0 \u6709\u6b0a\u5716\u8207\u7121\u6b0a\u5716 </p> <p>\u5716\u8cc7\u6599\u7d50\u69cb\u5305\u542b\u4ee5\u4e0b\u5e38\u7528\u8853\u8a9e\u3002</p> <ul> <li>\u9130\u63a5\uff08adjacency\uff09\uff1a\u7576\u5169\u9802\u9ede\u4e4b\u9593\u5b58\u5728\u908a\u76f8\u9023\u6642\uff0c\u7a31\u9019\u5169\u9802\u9ede\u201c\u9130\u63a5\u201d\u3002\u5728\u5716 9-4 \u4e2d\uff0c\u9802\u9ede 1 \u7684\u9130\u63a5\u9802\u9ede\u70ba\u9802\u9ede 2\u30013\u30015\u3002</li> <li>\u8def\u5f91\uff08path\uff09\uff1a\u5f9e\u9802\u9ede A \u5230\u9802\u9ede B \u7d93\u904e\u7684\u908a\u69cb\u6210\u7684\u5e8f\u5217\u88ab\u7a31\u70ba\u5f9e A \u5230 B \u7684\u201c\u8def\u5f91\u201d\u3002\u5728\u5716 9-4 \u4e2d\uff0c\u908a\u5e8f\u5217 1-5-2-4 \u662f\u9802\u9ede 1 \u5230\u9802\u9ede 4 \u7684\u4e00\u689d\u8def\u5f91\u3002</li> <li>\u5ea6\uff08degree\uff09\uff1a\u4e00\u500b\u9802\u9ede\u64c1\u6709\u7684\u908a\u6578\u3002\u5c0d\u65bc\u6709\u5411\u5716\uff0c\u5165\u5ea6\uff08in-degree\uff09\u8868\u793a\u6709\u591a\u5c11\u689d\u908a\u6307\u5411\u8a72\u9802\u9ede\uff0c\u51fa\u5ea6\uff08out-degree\uff09\u8868\u793a\u6709\u591a\u5c11\u689d\u908a\u5f9e\u8a72\u9802\u9ede\u6307\u51fa\u3002</li> </ul>"},{"location":"chapter_graph/graph/#912","title":"9.1.2 \u00a0 \u5716\u7684\u8868\u793a","text":"<p>\u5716\u7684\u5e38\u7528\u8868\u793a\u65b9\u5f0f\u5305\u62ec\u201c\u9130\u63a5\u77e9\u9663\u201d\u548c\u201c\u9130\u63a5\u8868\u201d\u3002\u4ee5\u4e0b\u4f7f\u7528\u7121\u5411\u5716\u9032\u884c\u8209\u4f8b\u3002</p>"},{"location":"chapter_graph/graph/#1","title":"1. \u00a0 \u9130\u63a5\u77e9\u9663","text":"<p>\u8a2d\u5716\u7684\u9802\u9ede\u6578\u91cf\u70ba \\(n\\) \uff0c\u9130\u63a5\u77e9\u9663\uff08adjacency matrix\uff09\u4f7f\u7528\u4e00\u500b \\(n \\times n\\) \u5927\u5c0f\u7684\u77e9\u9663\u4f86\u8868\u793a\u5716\uff0c\u6bcf\u4e00\u884c\uff08\u5217\uff09\u4ee3\u8868\u4e00\u500b\u9802\u9ede\uff0c\u77e9\u9663\u5143\u7d20\u4ee3\u8868\u908a\uff0c\u7528 \\(1\\) \u6216 \\(0\\) \u8868\u793a\u5169\u500b\u9802\u9ede\u4e4b\u9593\u662f\u5426\u5b58\u5728\u908a\u3002</p> <p>\u5982\u5716 9-5 \u6240\u793a\uff0c\u8a2d\u9130\u63a5\u77e9\u9663\u70ba \\(M\\)\u3001\u9802\u9ede\u4e32\u5217\u70ba \\(V\\) \uff0c\u90a3\u9ebc\u77e9\u9663\u5143\u7d20 \\(M[i, j] = 1\\) \u8868\u793a\u9802\u9ede \\(V[i]\\) \u5230\u9802\u9ede \\(V[j]\\) \u4e4b\u9593\u5b58\u5728\u908a\uff0c\u53cd\u4e4b \\(M[i, j] = 0\\) \u8868\u793a\u5169\u9802\u9ede\u4e4b\u9593\u7121\u908a\u3002</p> <p></p> <p> \u5716 9-5 \u00a0 \u5716\u7684\u9130\u63a5\u77e9\u9663\u8868\u793a </p> <p>\u9130\u63a5\u77e9\u9663\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002</p> <ul> <li>\u9802\u9ede\u4e0d\u80fd\u8207\u81ea\u8eab\u76f8\u9023\uff0c\u56e0\u6b64\u9130\u63a5\u77e9\u9663\u4e3b\u5c0d\u89d2\u7dda\u5143\u7d20\u6c92\u6709\u610f\u7fa9\u3002</li> <li>\u5c0d\u65bc\u7121\u5411\u5716\uff0c\u5169\u500b\u65b9\u5411\u7684\u908a\u7b49\u50f9\uff0c\u6b64\u6642\u9130\u63a5\u77e9\u9663\u95dc\u65bc\u4e3b\u5c0d\u89d2\u7dda\u5c0d\u7a31\u3002</li> <li>\u5c07\u9130\u63a5\u77e9\u9663\u7684\u5143\u7d20\u5f9e \\(1\\) \u548c \\(0\\) \u66ff\u63db\u70ba\u6b0a\u91cd\uff0c\u5247\u53ef\u8868\u793a\u6709\u6b0a\u5716\u3002</li> </ul> <p>\u4f7f\u7528\u9130\u63a5\u77e9\u9663\u8868\u793a\u5716\u6642\uff0c\u6211\u5011\u53ef\u4ee5\u76f4\u63a5\u8a2a\u554f\u77e9\u9663\u5143\u7d20\u4ee5\u7372\u53d6\u908a\uff0c\u56e0\u6b64\u589e\u522a\u67e5\u6539\u64cd\u4f5c\u7684\u6548\u7387\u5f88\u9ad8\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u5747\u70ba \\(O(1)\\) \u3002\u7136\u800c\uff0c\u77e9\u9663\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\) \uff0c\u8a18\u61b6\u9ad4\u4f54\u7528\u8f03\u591a\u3002</p>"},{"location":"chapter_graph/graph/#2","title":"2. \u00a0 \u9130\u63a5\u8868","text":"<p>\u9130\u63a5\u8868\uff08adjacency list\uff09\u4f7f\u7528 \\(n\\) \u500b\u93c8\u7d50\u4e32\u5217\u4f86\u8868\u793a\u5716\uff0c\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u8868\u793a\u9802\u9ede\u3002\u7b2c \\(i\\) \u500b\u93c8\u7d50\u4e32\u5217\u5c0d\u61c9\u9802\u9ede \\(i\\) \uff0c\u5176\u4e2d\u5132\u5b58\u4e86\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\uff08\u8207\u8a72\u9802\u9ede\u76f8\u9023\u7684\u9802\u9ede\uff09\u3002\u5716 9-6 \u5c55\u793a\u4e86\u4e00\u500b\u4f7f\u7528\u9130\u63a5\u8868\u5132\u5b58\u7684\u5716\u7684\u793a\u4f8b\u3002</p> <p></p> <p> \u5716 9-6 \u00a0 \u5716\u7684\u9130\u63a5\u8868\u8868\u793a </p> <p>\u9130\u63a5\u8868\u50c5\u5132\u5b58\u5be6\u969b\u5b58\u5728\u7684\u908a\uff0c\u800c\u908a\u7684\u7e3d\u6578\u901a\u5e38\u9060\u5c0f\u65bc \\(n^2\\) \uff0c\u56e0\u6b64\u5b83\u66f4\u52a0\u7bc0\u7701\u7a7a\u9593\u3002\u7136\u800c\uff0c\u5728\u9130\u63a5\u8868\u4e2d\u9700\u8981\u900f\u904e\u8d70\u8a2a\u93c8\u7d50\u4e32\u5217\u4f86\u67e5\u8a62\u908a\uff0c\u56e0\u6b64\u5176\u6642\u9593\u6548\u7387\u4e0d\u5982\u9130\u63a5\u77e9\u9663\u3002</p> <p>\u89c0\u5bdf\u5716 9-6 \uff0c\u9130\u63a5\u8868\u7d50\u69cb\u8207\u96dc\u6e4a\u8868\u4e2d\u7684\u201c\u93c8\u5f0f\u4f4d\u5740\u201d\u975e\u5e38\u76f8\u4f3c\uff0c\u56e0\u6b64\u6211\u5011\u4e5f\u53ef\u4ee5\u63a1\u7528\u985e\u4f3c\u7684\u65b9\u6cd5\u4f86\u6700\u4f73\u5316\u6548\u7387\u3002\u6bd4\u5982\u7576\u93c8\u7d50\u4e32\u5217\u8f03\u9577\u6642\uff0c\u53ef\u4ee5\u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u5316\u70ba AVL \u6a39\u6216\u7d05\u9ed1\u6a39\uff0c\u5f9e\u800c\u5c07\u6642\u9593\u6548\u7387\u5f9e \\(O(n)\\) \u6700\u4f73\u5316\u81f3 \\(O(\\log n)\\) \uff1b\u9084\u53ef\u4ee5\u628a\u93c8\u7d50\u4e32\u5217\u8f49\u63db\u70ba\u96dc\u6e4a\u8868\uff0c\u5f9e\u800c\u5c07\u6642\u9593\u8907\u96dc\u5ea6\u964d\u81f3 \\(O(1)\\) \u3002</p>"},{"location":"chapter_graph/graph/#913","title":"9.1.3 \u00a0 \u5716\u7684\u5e38\u898b\u61c9\u7528","text":"<p>\u5982\u8868 9-1 \u6240\u793a\uff0c\u8a31\u591a\u73fe\u5be6\u7cfb\u7d71\u53ef\u4ee5\u7528\u5716\u4f86\u5efa\u6a21\uff0c\u76f8\u61c9\u7684\u554f\u984c\u4e5f\u53ef\u4ee5\u7d04\u5316\u70ba\u5716\u8a08\u7b97\u554f\u984c\u3002</p> <p> \u8868 9-1 \u00a0 \u73fe\u5be6\u751f\u6d3b\u4e2d\u5e38\u898b\u7684\u5716 </p> \u9802\u9ede \u908a \u5716\u8a08\u7b97\u554f\u984c \u793e\u4ea4\u7db2\u8def \u4f7f\u7528\u8005 \u597d\u53cb\u95dc\u4fc2 \u6f5b\u5728\u597d\u53cb\u63a8\u85a6 \u5730\u9435\u7dda\u8def \u7ad9\u9ede \u7ad9\u9ede\u9593\u7684\u9023\u901a\u6027 \u6700\u77ed\u8def\u7dda\u63a8\u85a6 \u592a\u967d\u7cfb \u661f\u9ad4 \u661f\u9ad4\u9593\u7684\u842c\u6709\u5f15\u529b\u4f5c\u7528 \u884c\u661f\u8ecc\u9053\u8a08\u7b97"},{"location":"chapter_graph/graph_operations/","title":"9.2 \u00a0 \u5716\u7684\u57fa\u790e\u64cd\u4f5c","text":"<p>\u5716\u7684\u57fa\u790e\u64cd\u4f5c\u53ef\u5206\u70ba\u5c0d\u201c\u908a\u201d\u7684\u64cd\u4f5c\u548c\u5c0d\u201c\u9802\u9ede\u201d\u7684\u64cd\u4f5c\u3002\u5728\u201c\u9130\u63a5\u77e9\u9663\u201d\u548c\u201c\u9130\u63a5\u8868\u201d\u5169\u7a2e\u8868\u793a\u65b9\u6cd5\u4e0b\uff0c\u5be6\u73fe\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\u3002</p>"},{"location":"chapter_graph/graph_operations/#921","title":"9.2.1 \u00a0 \u57fa\u65bc\u9130\u63a5\u77e9\u9663\u7684\u5be6\u73fe","text":"<p>\u7d66\u5b9a\u4e00\u500b\u9802\u9ede\u6578\u91cf\u70ba \\(n\\) \u7684\u7121\u5411\u5716\uff0c\u5247\u5404\u7a2e\u64cd\u4f5c\u7684\u5be6\u73fe\u65b9\u5f0f\u5982\u5716 9-7 \u6240\u793a\u3002</p> <ul> <li>\u65b0\u589e\u6216\u522a\u9664\u908a\uff1a\u76f4\u63a5\u5728\u9130\u63a5\u77e9\u9663\u4e2d\u4fee\u6539\u6307\u5b9a\u7684\u908a\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u6642\u9593\u3002\u800c\u7531\u65bc\u662f\u7121\u5411\u5716\uff0c\u56e0\u6b64\u9700\u8981\u540c\u6642\u66f4\u65b0\u5169\u500b\u65b9\u5411\u7684\u908a\u3002</li> <li>\u65b0\u589e\u9802\u9ede\uff1a\u5728\u9130\u63a5\u77e9\u9663\u7684\u5c3e\u90e8\u65b0\u589e\u4e00\u884c\u4e00\u5217\uff0c\u4e26\u5168\u90e8\u586b \\(0\\) \u5373\u53ef\uff0c\u4f7f\u7528 \\(O(n)\\) \u6642\u9593\u3002</li> <li>\u522a\u9664\u9802\u9ede\uff1a\u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u4e00\u884c\u4e00\u5217\u3002\u7576\u522a\u9664\u9996\u884c\u9996\u5217\u6642\u9054\u5230\u6700\u5dee\u60c5\u6cc1\uff0c\u9700\u8981\u5c07 \\((n-1)^2\\) \u500b\u5143\u7d20\u201c\u5411\u5de6\u4e0a\u79fb\u52d5\u201d\uff0c\u5f9e\u800c\u4f7f\u7528 \\(O(n^2)\\) \u6642\u9593\u3002</li> <li>\u521d\u59cb\u5316\uff1a\u50b3\u5165 \\(n\\) \u500b\u9802\u9ede\uff0c\u521d\u59cb\u5316\u9577\u5ea6\u70ba \\(n\\) \u7684\u9802\u9ede\u4e32\u5217 <code>vertices</code> \uff0c\u4f7f\u7528 \\(O(n)\\) \u6642\u9593\uff1b\u521d\u59cb\u5316 \\(n \\times n\\) \u5927\u5c0f\u7684\u9130\u63a5\u77e9\u9663 <code>adjMat</code> \uff0c\u4f7f\u7528 \\(O(n^2)\\) \u6642\u9593\u3002</li> </ul> \u521d\u59cb\u5316\u9130\u63a5\u77e9\u9663\u65b0\u589e\u908a\u522a\u9664\u908a\u65b0\u589e\u9802\u9ede\u522a\u9664\u9802\u9ede <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 9-7 \u00a0 \u9130\u63a5\u77e9\u9663\u7684\u521d\u59cb\u5316\u3001\u589e\u522a\u908a\u3001\u589e\u522a\u9802\u9ede </p> <p>\u4ee5\u4e0b\u662f\u57fa\u65bc\u9130\u63a5\u77e9\u9663\u8868\u793a\u5716\u7684\u5be6\u73fe\u7a0b\u5f0f\u78bc\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig graph_adjacency_matrix.py<pre><code>class GraphAdjMat:\n \"\"\"\u57fa\u65bc\u9130\u63a5\u77e9\u9663\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225\"\"\"\n\n def __init__(self, vertices: list[int], edges: list[list[int]]):\n \"\"\"\u5efa\u69cb\u5b50\"\"\"\n # \u9802\u9ede\u4e32\u5217\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9802\u9ede\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9802\u9ede\u7d22\u5f15\u201d\n self.vertices: list[int] = []\n # \u9130\u63a5\u77e9\u9663\uff0c\u884c\u5217\u7d22\u5f15\u5c0d\u61c9\u201c\u9802\u9ede\u7d22\u5f15\u201d\n self.adj_mat: list[list[int]] = []\n # \u65b0\u589e\u9802\u9ede\n for val in vertices:\n self.add_vertex(val)\n # \u65b0\u589e\u908a\n # \u8acb\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9802\u9ede\u7d22\u5f15\uff0c\u5373\u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n for e in edges:\n self.add_edge(e[0], e[1])\n\n def size(self) -&gt; int:\n \"\"\"\u7372\u53d6\u9802\u9ede\u6578\u91cf\"\"\"\n return len(self.vertices)\n\n def add_vertex(self, val: int):\n \"\"\"\u65b0\u589e\u9802\u9ede\"\"\"\n n = self.size()\n # \u5411\u9802\u9ede\u4e32\u5217\u4e2d\u65b0\u589e\u65b0\u9802\u9ede\u7684\u503c\n self.vertices.append(val)\n # \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u884c\n new_row = [0] * n\n self.adj_mat.append(new_row)\n # \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u5217\n for row in self.adj_mat:\n row.append(0)\n\n def remove_vertex(self, index: int):\n \"\"\"\u522a\u9664\u9802\u9ede\"\"\"\n if index &gt;= self.size():\n raise IndexError()\n # \u5728\u9802\u9ede\u4e32\u5217\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9802\u9ede\n self.vertices.pop(index)\n # \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u884c\n self.adj_mat.pop(index)\n # \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u5217\n for row in self.adj_mat:\n row.pop(index)\n\n def add_edge(self, i: int, j: int):\n \"\"\"\u65b0\u589e\u908a\"\"\"\n # \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n # \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if i &lt; 0 or j &lt; 0 or i &gt;= self.size() or j &gt;= self.size() or i == j:\n raise IndexError()\n # \u5728\u7121\u5411\u5716\u4e2d\uff0c\u9130\u63a5\u77e9\u9663\u95dc\u65bc\u4e3b\u5c0d\u89d2\u7dda\u5c0d\u7a31\uff0c\u5373\u6eff\u8db3 (i, j) == (j, i)\n self.adj_mat[i][j] = 1\n self.adj_mat[j][i] = 1\n\n def remove_edge(self, i: int, j: int):\n \"\"\"\u522a\u9664\u908a\"\"\"\n # \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n # \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if i &lt; 0 or j &lt; 0 or i &gt;= self.size() or j &gt;= self.size() or i == j:\n raise IndexError()\n self.adj_mat[i][j] = 0\n self.adj_mat[j][i] = 0\n\n def print(self):\n \"\"\"\u5217\u5370\u9130\u63a5\u77e9\u9663\"\"\"\n print(\"\u9802\u9ede\u4e32\u5217 =\", self.vertices)\n print(\"\u9130\u63a5\u77e9\u9663 =\")\n print_matrix(self.adj_mat)\n</code></pre> graph_adjacency_matrix.cpp<pre><code>/* \u57fa\u65bc\u9130\u63a5\u77e9\u9663\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjMat {\n vector&lt;int&gt; vertices; // \u9802\u9ede\u4e32\u5217\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9802\u9ede\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9802\u9ede\u7d22\u5f15\u201d\n vector&lt;vector&lt;int&gt;&gt; adjMat; // \u9130\u63a5\u77e9\u9663\uff0c\u884c\u5217\u7d22\u5f15\u5c0d\u61c9\u201c\u9802\u9ede\u7d22\u5f15\u201d\n\n public:\n /* \u5efa\u69cb\u5b50 */\n GraphAdjMat(const vector&lt;int&gt; &amp;vertices, const vector&lt;vector&lt;int&gt;&gt; &amp;edges) {\n // \u65b0\u589e\u9802\u9ede\n for (int val : vertices) {\n addVertex(val);\n }\n // \u65b0\u589e\u908a\n // \u8acb\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9802\u9ede\u7d22\u5f15\uff0c\u5373\u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n for (const vector&lt;int&gt; &amp;edge : edges) {\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n int size() const {\n return vertices.size();\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n void addVertex(int val) {\n int n = size();\n // \u5411\u9802\u9ede\u4e32\u5217\u4e2d\u65b0\u589e\u65b0\u9802\u9ede\u7684\u503c\n vertices.push_back(val);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u884c\n adjMat.emplace_back(vector&lt;int&gt;(n, 0));\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u5217\n for (vector&lt;int&gt; &amp;row : adjMat) {\n row.push_back(0);\n }\n }\n\n /* \u522a\u9664\u9802\u9ede */\n void removeVertex(int index) {\n if (index &gt;= size()) {\n throw out_of_range(\"\u9802\u9ede\u4e0d\u5b58\u5728\");\n }\n // \u5728\u9802\u9ede\u4e32\u5217\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9802\u9ede\n vertices.erase(vertices.begin() + index);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.erase(adjMat.begin() + index);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u5217\n for (vector&lt;int&gt; &amp;row : adjMat) {\n row.erase(row.begin() + index);\n }\n }\n\n /* \u65b0\u589e\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j) {\n throw out_of_range(\"\u9802\u9ede\u4e0d\u5b58\u5728\");\n }\n // \u5728\u7121\u5411\u5716\u4e2d\uff0c\u9130\u63a5\u77e9\u9663\u95dc\u65bc\u4e3b\u5c0d\u89d2\u7dda\u5c0d\u7a31\uff0c\u5373\u6eff\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u522a\u9664\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j) {\n throw out_of_range(\"\u9802\u9ede\u4e0d\u5b58\u5728\");\n }\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u5217\u5370\u9130\u63a5\u77e9\u9663 */\n void print() {\n cout &lt;&lt; \"\u9802\u9ede\u4e32\u5217 = \";\n printVector(vertices);\n cout &lt;&lt; \"\u9130\u63a5\u77e9\u9663 =\" &lt;&lt; endl;\n printVectorMatrix(adjMat);\n }\n};\n</code></pre> graph_adjacency_matrix.java<pre><code>/* \u57fa\u65bc\u9130\u63a5\u77e9\u9663\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjMat {\n List&lt;Integer&gt; vertices; // \u9802\u9ede\u4e32\u5217\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9802\u9ede\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9802\u9ede\u7d22\u5f15\u201d\n List&lt;List&lt;Integer&gt;&gt; adjMat; // \u9130\u63a5\u77e9\u9663\uff0c\u884c\u5217\u7d22\u5f15\u5c0d\u61c9\u201c\u9802\u9ede\u7d22\u5f15\u201d\n\n /* \u5efa\u69cb\u5b50 */\n public GraphAdjMat(int[] vertices, int[][] edges) {\n this.vertices = new ArrayList&lt;&gt;();\n this.adjMat = new ArrayList&lt;&gt;();\n // \u65b0\u589e\u9802\u9ede\n for (int val : vertices) {\n addVertex(val);\n }\n // \u65b0\u589e\u908a\n // \u8acb\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9802\u9ede\u7d22\u5f15\uff0c\u5373\u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n for (int[] e : edges) {\n addEdge(e[0], e[1]);\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n public int size() {\n return vertices.size();\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n public void addVertex(int val) {\n int n = size();\n // \u5411\u9802\u9ede\u4e32\u5217\u4e2d\u65b0\u589e\u65b0\u9802\u9ede\u7684\u503c\n vertices.add(val);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u884c\n List&lt;Integer&gt; newRow = new ArrayList&lt;&gt;(n);\n for (int j = 0; j &lt; n; j++) {\n newRow.add(0);\n }\n adjMat.add(newRow);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u5217\n for (List&lt;Integer&gt; row : adjMat) {\n row.add(0);\n }\n }\n\n /* \u522a\u9664\u9802\u9ede */\n public void removeVertex(int index) {\n if (index &gt;= size())\n throw new IndexOutOfBoundsException();\n // \u5728\u9802\u9ede\u4e32\u5217\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9802\u9ede\n vertices.remove(index);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.remove(index);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u5217\n for (List&lt;Integer&gt; row : adjMat) {\n row.remove(index);\n }\n }\n\n /* \u65b0\u589e\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n public void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j)\n throw new IndexOutOfBoundsException();\n // \u5728\u7121\u5411\u5716\u4e2d\uff0c\u9130\u63a5\u77e9\u9663\u95dc\u65bc\u4e3b\u5c0d\u89d2\u7dda\u5c0d\u7a31\uff0c\u5373\u6eff\u8db3 (i, j) == (j, i)\n adjMat.get(i).set(j, 1);\n adjMat.get(j).set(i, 1);\n }\n\n /* \u522a\u9664\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n public void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j)\n throw new IndexOutOfBoundsException();\n adjMat.get(i).set(j, 0);\n adjMat.get(j).set(i, 0);\n }\n\n /* \u5217\u5370\u9130\u63a5\u77e9\u9663 */\n public void print() {\n System.out.print(\"\u9802\u9ede\u4e32\u5217 = \");\n System.out.println(vertices);\n System.out.println(\"\u9130\u63a5\u77e9\u9663 =\");\n PrintUtil.printMatrix(adjMat);\n }\n}\n</code></pre> graph_adjacency_matrix.cs<pre><code>/* \u57fa\u65bc\u9130\u63a5\u77e9\u9663\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjMat {\n List&lt;int&gt; vertices; // \u9802\u9ede\u4e32\u5217\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9802\u9ede\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9802\u9ede\u7d22\u5f15\u201d\n List&lt;List&lt;int&gt;&gt; adjMat; // \u9130\u63a5\u77e9\u9663\uff0c\u884c\u5217\u7d22\u5f15\u5c0d\u61c9\u201c\u9802\u9ede\u7d22\u5f15\u201d\n\n /* \u5efa\u69cb\u5b50 */\n public GraphAdjMat(int[] vertices, int[][] edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u65b0\u589e\u9802\u9ede\n foreach (int val in vertices) {\n AddVertex(val);\n }\n // \u65b0\u589e\u908a\n // \u8acb\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9802\u9ede\u7d22\u5f15\uff0c\u5373\u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n foreach (int[] e in edges) {\n AddEdge(e[0], e[1]);\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n int Size() {\n return vertices.Count;\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n public void AddVertex(int val) {\n int n = Size();\n // \u5411\u9802\u9ede\u4e32\u5217\u4e2d\u65b0\u589e\u65b0\u9802\u9ede\u7684\u503c\n vertices.Add(val);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u884c\n List&lt;int&gt; newRow = new(n);\n for (int j = 0; j &lt; n; j++) {\n newRow.Add(0);\n }\n adjMat.Add(newRow);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u5217\n foreach (List&lt;int&gt; row in adjMat) {\n row.Add(0);\n }\n }\n\n /* \u522a\u9664\u9802\u9ede */\n public void RemoveVertex(int index) {\n if (index &gt;= Size())\n throw new IndexOutOfRangeException();\n // \u5728\u9802\u9ede\u4e32\u5217\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9802\u9ede\n vertices.RemoveAt(index);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.RemoveAt(index);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u5217\n foreach (List&lt;int&gt; row in adjMat) {\n row.RemoveAt(index);\n }\n }\n\n /* \u65b0\u589e\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n public void AddEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if (i &lt; 0 || j &lt; 0 || i &gt;= Size() || j &gt;= Size() || i == j)\n throw new IndexOutOfRangeException();\n // \u5728\u7121\u5411\u5716\u4e2d\uff0c\u9130\u63a5\u77e9\u9663\u95dc\u65bc\u4e3b\u5c0d\u89d2\u7dda\u5c0d\u7a31\uff0c\u5373\u6eff\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u522a\u9664\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n public void RemoveEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if (i &lt; 0 || j &lt; 0 || i &gt;= Size() || j &gt;= Size() || i == j)\n throw new IndexOutOfRangeException();\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u5217\u5370\u9130\u63a5\u77e9\u9663 */\n public void Print() {\n Console.Write(\"\u9802\u9ede\u4e32\u5217 = \");\n PrintUtil.PrintList(vertices);\n Console.WriteLine(\"\u9130\u63a5\u77e9\u9663 =\");\n PrintUtil.PrintMatrix(adjMat);\n }\n}\n</code></pre> graph_adjacency_matrix.go<pre><code>/* \u57fa\u65bc\u9130\u63a5\u77e9\u9663\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\ntype graphAdjMat struct {\n // \u9802\u9ede\u4e32\u5217\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9802\u9ede\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9802\u9ede\u7d22\u5f15\u201d\n vertices []int\n // \u9130\u63a5\u77e9\u9663\uff0c\u884c\u5217\u7d22\u5f15\u5c0d\u61c9\u201c\u9802\u9ede\u7d22\u5f15\u201d\n adjMat [][]int\n}\n\n/* \u5efa\u69cb\u5b50 */\nfunc newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {\n // \u65b0\u589e\u9802\u9ede\n n := len(vertices)\n adjMat := make([][]int, n)\n for i := range adjMat {\n adjMat[i] = make([]int, n)\n }\n // \u521d\u59cb\u5316\u5716\n g := &amp;graphAdjMat{\n vertices: vertices,\n adjMat: adjMat,\n }\n // \u65b0\u589e\u908a\n // \u8acb\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9802\u9ede\u7d22\u5f15\uff0c\u5373\u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n for i := range edges {\n g.addEdge(edges[i][0], edges[i][1])\n }\n return g\n}\n\n/* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\nfunc (g *graphAdjMat) size() int {\n return len(g.vertices)\n}\n\n/* \u65b0\u589e\u9802\u9ede */\nfunc (g *graphAdjMat) addVertex(val int) {\n n := g.size()\n // \u5411\u9802\u9ede\u4e32\u5217\u4e2d\u65b0\u589e\u65b0\u9802\u9ede\u7684\u503c\n g.vertices = append(g.vertices, val)\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u884c\n newRow := make([]int, n)\n g.adjMat = append(g.adjMat, newRow)\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u5217\n for i := range g.adjMat {\n g.adjMat[i] = append(g.adjMat[i], 0)\n }\n}\n\n/* \u522a\u9664\u9802\u9ede */\nfunc (g *graphAdjMat) removeVertex(index int) {\n if index &gt;= g.size() {\n return\n }\n // \u5728\u9802\u9ede\u4e32\u5217\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9802\u9ede\n g.vertices = append(g.vertices[:index], g.vertices[index+1:]...)\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u884c\n g.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u5217\n for i := range g.adjMat {\n g.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)\n }\n}\n\n/* \u65b0\u589e\u908a */\n// \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) addEdge(i, j int) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if i &lt; 0 || j &lt; 0 || i &gt;= g.size() || j &gt;= g.size() || i == j {\n fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n }\n // \u5728\u7121\u5411\u5716\u4e2d\uff0c\u9130\u63a5\u77e9\u9663\u95dc\u65bc\u4e3b\u5c0d\u89d2\u7dda\u5c0d\u7a31\uff0c\u5373\u6eff\u8db3 (i, j) == (j, i)\n g.adjMat[i][j] = 1\n g.adjMat[j][i] = 1\n}\n\n/* \u522a\u9664\u908a */\n// \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) removeEdge(i, j int) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if i &lt; 0 || j &lt; 0 || i &gt;= g.size() || j &gt;= g.size() || i == j {\n fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n }\n g.adjMat[i][j] = 0\n g.adjMat[j][i] = 0\n}\n\n/* \u5217\u5370\u9130\u63a5\u77e9\u9663 */\nfunc (g *graphAdjMat) print() {\n fmt.Printf(\"\\t\u9802\u9ede\u4e32\u5217 = %v\\n\", g.vertices)\n fmt.Printf(\"\\t\u9130\u63a5\u77e9\u9663 = \\n\")\n for i := range g.adjMat {\n fmt.Printf(\"\\t\\t\\t%v\\n\", g.adjMat[i])\n }\n}\n</code></pre> graph_adjacency_matrix.swift<pre><code>/* \u57fa\u65bc\u9130\u63a5\u77e9\u9663\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjMat {\n private var vertices: [Int] // \u9802\u9ede\u4e32\u5217\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9802\u9ede\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9802\u9ede\u7d22\u5f15\u201d\n private var adjMat: [[Int]] // \u9130\u63a5\u77e9\u9663\uff0c\u884c\u5217\u7d22\u5f15\u5c0d\u61c9\u201c\u9802\u9ede\u7d22\u5f15\u201d\n\n /* \u5efa\u69cb\u5b50 */\n init(vertices: [Int], edges: [[Int]]) {\n self.vertices = []\n adjMat = []\n // \u65b0\u589e\u9802\u9ede\n for val in vertices {\n addVertex(val: val)\n }\n // \u65b0\u589e\u908a\n // \u8acb\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9802\u9ede\u7d22\u5f15\uff0c\u5373\u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n for e in edges {\n addEdge(i: e[0], j: e[1])\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n func size() -&gt; Int {\n vertices.count\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n func addVertex(val: Int) {\n let n = size()\n // \u5411\u9802\u9ede\u4e32\u5217\u4e2d\u65b0\u589e\u65b0\u9802\u9ede\u7684\u503c\n vertices.append(val)\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u884c\n let newRow = Array(repeating: 0, count: n)\n adjMat.append(newRow)\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u5217\n for i in adjMat.indices {\n adjMat[i].append(0)\n }\n }\n\n /* \u522a\u9664\u9802\u9ede */\n func removeVertex(index: Int) {\n if index &gt;= size() {\n fatalError(\"\u8d8a\u754c\")\n }\n // \u5728\u9802\u9ede\u4e32\u5217\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9802\u9ede\n vertices.remove(at: index)\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.remove(at: index)\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u5217\n for i in adjMat.indices {\n adjMat[i].remove(at: index)\n }\n }\n\n /* \u65b0\u589e\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n func addEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j {\n fatalError(\"\u8d8a\u754c\")\n }\n // \u5728\u7121\u5411\u5716\u4e2d\uff0c\u9130\u63a5\u77e9\u9663\u95dc\u65bc\u4e3b\u5c0d\u89d2\u7dda\u5c0d\u7a31\uff0c\u5373\u6eff\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1\n adjMat[j][i] = 1\n }\n\n /* \u522a\u9664\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n func removeEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j {\n fatalError(\"\u8d8a\u754c\")\n }\n adjMat[i][j] = 0\n adjMat[j][i] = 0\n }\n\n /* \u5217\u5370\u9130\u63a5\u77e9\u9663 */\n func print() {\n Swift.print(\"\u9802\u9ede\u4e32\u5217 = \", terminator: \"\")\n Swift.print(vertices)\n Swift.print(\"\u9130\u63a5\u77e9\u9663 =\")\n PrintUtil.printMatrix(matrix: adjMat)\n }\n}\n</code></pre> graph_adjacency_matrix.js<pre><code>/* \u57fa\u65bc\u9130\u63a5\u77e9\u9663\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjMat {\n vertices; // \u9802\u9ede\u4e32\u5217\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9802\u9ede\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9802\u9ede\u7d22\u5f15\u201d\n adjMat; // \u9130\u63a5\u77e9\u9663\uff0c\u884c\u5217\u7d22\u5f15\u5c0d\u61c9\u201c\u9802\u9ede\u7d22\u5f15\u201d\n\n /* \u5efa\u69cb\u5b50 */\n constructor(vertices, edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u65b0\u589e\u9802\u9ede\n for (const val of vertices) {\n this.addVertex(val);\n }\n // \u65b0\u589e\u908a\n // \u8acb\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9802\u9ede\u7d22\u5f15\uff0c\u5373\u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n for (const e of edges) {\n this.addEdge(e[0], e[1]);\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n size() {\n return this.vertices.length;\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n addVertex(val) {\n const n = this.size();\n // \u5411\u9802\u9ede\u4e32\u5217\u4e2d\u65b0\u589e\u65b0\u9802\u9ede\u7684\u503c\n this.vertices.push(val);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u884c\n const newRow = [];\n for (let j = 0; j &lt; n; j++) {\n newRow.push(0);\n }\n this.adjMat.push(newRow);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u5217\n for (const row of this.adjMat) {\n row.push(0);\n }\n }\n\n /* \u522a\u9664\u9802\u9ede */\n removeVertex(index) {\n if (index &gt;= this.size()) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u9802\u9ede\u4e32\u5217\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9802\u9ede\n this.vertices.splice(index, 1);\n\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u884c\n this.adjMat.splice(index, 1);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u5217\n for (const row of this.adjMat) {\n row.splice(index, 1);\n }\n }\n\n /* \u65b0\u589e\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n addEdge(i, j) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if (i &lt; 0 || j &lt; 0 || i &gt;= this.size() || j &gt;= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u7121\u5411\u5716\u4e2d\uff0c\u9130\u63a5\u77e9\u9663\u95dc\u65bc\u4e3b\u5c0d\u89d2\u7dda\u5c0d\u7a31\uff0c\u5373\u6eff\u8db3 (i, j) === (j, i)\n this.adjMat[i][j] = 1;\n this.adjMat[j][i] = 1;\n }\n\n /* \u522a\u9664\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n removeEdge(i, j) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if (i &lt; 0 || j &lt; 0 || i &gt;= this.size() || j &gt;= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n this.adjMat[i][j] = 0;\n this.adjMat[j][i] = 0;\n }\n\n /* \u5217\u5370\u9130\u63a5\u77e9\u9663 */\n print() {\n console.log('\u9802\u9ede\u4e32\u5217 = ', this.vertices);\n console.log('\u9130\u63a5\u77e9\u9663 =', this.adjMat);\n }\n}\n</code></pre> graph_adjacency_matrix.ts<pre><code>/* \u57fa\u65bc\u9130\u63a5\u77e9\u9663\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjMat {\n vertices: number[]; // \u9802\u9ede\u4e32\u5217\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9802\u9ede\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9802\u9ede\u7d22\u5f15\u201d\n adjMat: number[][]; // \u9130\u63a5\u77e9\u9663\uff0c\u884c\u5217\u7d22\u5f15\u5c0d\u61c9\u201c\u9802\u9ede\u7d22\u5f15\u201d\n\n /* \u5efa\u69cb\u5b50 */\n constructor(vertices: number[], edges: number[][]) {\n this.vertices = [];\n this.adjMat = [];\n // \u65b0\u589e\u9802\u9ede\n for (const val of vertices) {\n this.addVertex(val);\n }\n // \u65b0\u589e\u908a\n // \u8acb\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9802\u9ede\u7d22\u5f15\uff0c\u5373\u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n for (const e of edges) {\n this.addEdge(e[0], e[1]);\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n size(): number {\n return this.vertices.length;\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n addVertex(val: number): void {\n const n: number = this.size();\n // \u5411\u9802\u9ede\u4e32\u5217\u4e2d\u65b0\u589e\u65b0\u9802\u9ede\u7684\u503c\n this.vertices.push(val);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u884c\n const newRow: number[] = [];\n for (let j: number = 0; j &lt; n; j++) {\n newRow.push(0);\n }\n this.adjMat.push(newRow);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u5217\n for (const row of this.adjMat) {\n row.push(0);\n }\n }\n\n /* \u522a\u9664\u9802\u9ede */\n removeVertex(index: number): void {\n if (index &gt;= this.size()) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u9802\u9ede\u4e32\u5217\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9802\u9ede\n this.vertices.splice(index, 1);\n\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u884c\n this.adjMat.splice(index, 1);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u5217\n for (const row of this.adjMat) {\n row.splice(index, 1);\n }\n }\n\n /* \u65b0\u589e\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n addEdge(i: number, j: number): void {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if (i &lt; 0 || j &lt; 0 || i &gt;= this.size() || j &gt;= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u7121\u5411\u5716\u4e2d\uff0c\u9130\u63a5\u77e9\u9663\u95dc\u65bc\u4e3b\u5c0d\u89d2\u7dda\u5c0d\u7a31\uff0c\u5373\u6eff\u8db3 (i, j) === (j, i)\n this.adjMat[i][j] = 1;\n this.adjMat[j][i] = 1;\n }\n\n /* \u522a\u9664\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n removeEdge(i: number, j: number): void {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if (i &lt; 0 || j &lt; 0 || i &gt;= this.size() || j &gt;= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n this.adjMat[i][j] = 0;\n this.adjMat[j][i] = 0;\n }\n\n /* \u5217\u5370\u9130\u63a5\u77e9\u9663 */\n print(): void {\n console.log('\u9802\u9ede\u4e32\u5217 = ', this.vertices);\n console.log('\u9130\u63a5\u77e9\u9663 =', this.adjMat);\n }\n}\n</code></pre> graph_adjacency_matrix.dart<pre><code>/* \u57fa\u65bc\u9130\u63a5\u77e9\u9663\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjMat {\n List&lt;int&gt; vertices = []; // \u9802\u9ede\u5143\u7d20\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9802\u9ede\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9802\u9ede\u7d22\u5f15\u201d\n List&lt;List&lt;int&gt;&gt; adjMat = []; //\u9130\u63a5\u77e9\u9663\uff0c\u884c\u5217\u7d22\u5f15\u5c0d\u61c9\u201c\u9802\u9ede\u7d22\u5f15\u201d\n\n /* \u5efa\u69cb\u5b50 */\n GraphAdjMat(List&lt;int&gt; vertices, List&lt;List&lt;int&gt;&gt; edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u65b0\u589e\u9802\u9ede\n for (int val in vertices) {\n addVertex(val);\n }\n // \u65b0\u589e\u908a\n // \u8acb\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9802\u9ede\u7d22\u5f15\uff0c\u5373\u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n for (List&lt;int&gt; e in edges) {\n addEdge(e[0], e[1]);\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n int size() {\n return vertices.length;\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n void addVertex(int val) {\n int n = size();\n // \u5411\u9802\u9ede\u4e32\u5217\u4e2d\u65b0\u589e\u65b0\u9802\u9ede\u7684\u503c\n vertices.add(val);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u884c\n List&lt;int&gt; newRow = List.filled(n, 0, growable: true);\n adjMat.add(newRow);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u5217\n for (List&lt;int&gt; row in adjMat) {\n row.add(0);\n }\n }\n\n /* \u522a\u9664\u9802\u9ede */\n void removeVertex(int index) {\n if (index &gt;= size()) {\n throw IndexError;\n }\n // \u5728\u9802\u9ede\u4e32\u5217\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9802\u9ede\n vertices.removeAt(index);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.removeAt(index);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u5217\n for (List&lt;int&gt; row in adjMat) {\n row.removeAt(index);\n }\n }\n\n /* \u65b0\u589e\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j) {\n throw IndexError;\n }\n // \u5728\u7121\u5411\u5716\u4e2d\uff0c\u9130\u63a5\u77e9\u9663\u95dc\u65bc\u4e3b\u5c0d\u89d2\u7dda\u5c0d\u7a31\uff0c\u5373\u6eff\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u522a\u9664\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j) {\n throw IndexError;\n }\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u5217\u5370\u9130\u63a5\u77e9\u9663 */\n void printAdjMat() {\n print(\"\u9802\u9ede\u4e32\u5217 = $vertices\");\n print(\"\u9130\u63a5\u77e9\u9663 = \");\n printMatrix(adjMat);\n }\n}\n</code></pre> graph_adjacency_matrix.rs<pre><code>/* \u57fa\u65bc\u9130\u63a5\u77e9\u9663\u5be6\u73fe\u7684\u7121\u5411\u5716\u578b\u5225 */\npub struct GraphAdjMat {\n // \u9802\u9ede\u4e32\u5217\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9802\u9ede\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9802\u9ede\u7d22\u5f15\u201d\n pub vertices: Vec&lt;i32&gt;,\n // \u9130\u63a5\u77e9\u9663\uff0c\u884c\u5217\u7d22\u5f15\u5c0d\u61c9\u201c\u9802\u9ede\u7d22\u5f15\u201d\n pub adj_mat: Vec&lt;Vec&lt;i32&gt;&gt;,\n}\n\nimpl GraphAdjMat {\n /* \u5efa\u69cb\u5b50 */\n pub fn new(vertices: Vec&lt;i32&gt;, edges: Vec&lt;[usize; 2]&gt;) -&gt; Self {\n let mut graph = GraphAdjMat {\n vertices: vec![],\n adj_mat: vec![],\n };\n // \u65b0\u589e\u9802\u9ede\n for val in vertices {\n graph.add_vertex(val);\n }\n // \u65b0\u589e\u908a\n // \u8acb\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9802\u9ede\u7d22\u5f15\uff0c\u5373\u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n for edge in edges {\n graph.add_edge(edge[0], edge[1])\n }\n\n graph\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n pub fn size(&amp;self) -&gt; usize {\n self.vertices.len()\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n pub fn add_vertex(&amp;mut self, val: i32) {\n let n = self.size();\n // \u5411\u9802\u9ede\u4e32\u5217\u4e2d\u65b0\u589e\u65b0\u9802\u9ede\u7684\u503c\n self.vertices.push(val);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u884c\n self.adj_mat.push(vec![0; n]);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u5217\n for row in &amp;mut self.adj_mat {\n row.push(0);\n }\n }\n\n /* \u522a\u9664\u9802\u9ede */\n pub fn remove_vertex(&amp;mut self, index: usize) {\n if index &gt;= self.size() {\n panic!(\"index error\")\n }\n // \u5728\u9802\u9ede\u4e32\u5217\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9802\u9ede\n self.vertices.remove(index);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u884c\n self.adj_mat.remove(index);\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u5217\n for row in &amp;mut self.adj_mat {\n row.remove(index);\n }\n }\n\n /* \u65b0\u589e\u908a */\n pub fn add_edge(&amp;mut self, i: usize, j: usize) {\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if i &gt;= self.size() || j &gt;= self.size() || i == j {\n panic!(\"index error\")\n }\n // \u5728\u7121\u5411\u5716\u4e2d\uff0c\u9130\u63a5\u77e9\u9663\u95dc\u65bc\u4e3b\u5c0d\u89d2\u7dda\u5c0d\u7a31\uff0c\u5373\u6eff\u8db3 (i, j) == (j, i)\n self.adj_mat[i][j] = 1;\n self.adj_mat[j][i] = 1;\n }\n\n /* \u522a\u9664\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n pub fn remove_edge(&amp;mut self, i: usize, j: usize) {\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if i &gt;= self.size() || j &gt;= self.size() || i == j {\n panic!(\"index error\")\n }\n self.adj_mat[i][j] = 0;\n self.adj_mat[j][i] = 0;\n }\n\n /* \u5217\u5370\u9130\u63a5\u77e9\u9663 */\n pub fn print(&amp;self) {\n println!(\"\u9802\u9ede\u4e32\u5217 = {:?}\", self.vertices);\n println!(\"\u9130\u63a5\u77e9\u9663 =\");\n println!(\"[\");\n for row in &amp;self.adj_mat {\n println!(\" {:?},\", row);\n }\n println!(\"]\")\n }\n}\n</code></pre> graph_adjacency_matrix.c<pre><code>/* \u57fa\u65bc\u9130\u63a5\u77e9\u9663\u5be6\u73fe\u7684\u7121\u5411\u5716\u7d50\u69cb\u9ad4 */\ntypedef struct {\n int vertices[MAX_SIZE];\n int adjMat[MAX_SIZE][MAX_SIZE];\n int size;\n} GraphAdjMat;\n\n/* \u5efa\u69cb\u5b50 */\nGraphAdjMat *newGraphAdjMat() {\n GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat));\n graph-&gt;size = 0;\n for (int i = 0; i &lt; MAX_SIZE; i++) {\n for (int j = 0; j &lt; MAX_SIZE; j++) {\n graph-&gt;adjMat[i][j] = 0;\n }\n }\n return graph;\n}\n\n/* \u6790\u69cb\u51fd\u5f0f */\nvoid delGraphAdjMat(GraphAdjMat *graph) {\n free(graph);\n}\n\n/* \u65b0\u589e\u9802\u9ede */\nvoid addVertex(GraphAdjMat *graph, int val) {\n if (graph-&gt;size == MAX_SIZE) {\n fprintf(stderr, \"\u5716\u7684\u9802\u9ede\u6578\u91cf\u5df2\u9054\u6700\u5927\u503c\\n\");\n return;\n }\n // \u65b0\u589e\u7b2c n \u500b\u9802\u9ede\uff0c\u4e26\u5c07\u7b2c n \u884c\u548c\u5217\u7f6e\u96f6\n int n = graph-&gt;size;\n graph-&gt;vertices[n] = val;\n for (int i = 0; i &lt;= n; i++) {\n graph-&gt;adjMat[n][i] = graph-&gt;adjMat[i][n] = 0;\n }\n graph-&gt;size++;\n}\n\n/* \u522a\u9664\u9802\u9ede */\nvoid removeVertex(GraphAdjMat *graph, int index) {\n if (index &lt; 0 || index &gt;= graph-&gt;size) {\n fprintf(stderr, \"\u9802\u9ede\u7d22\u5f15\u8d8a\u754c\\n\");\n return;\n }\n // \u5728\u9802\u9ede\u4e32\u5217\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9802\u9ede\n for (int i = index; i &lt; graph-&gt;size - 1; i++) {\n graph-&gt;vertices[i] = graph-&gt;vertices[i + 1];\n }\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u884c\n for (int i = index; i &lt; graph-&gt;size - 1; i++) {\n for (int j = 0; j &lt; graph-&gt;size; j++) {\n graph-&gt;adjMat[i][j] = graph-&gt;adjMat[i + 1][j];\n }\n }\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u5217\n for (int i = 0; i &lt; graph-&gt;size; i++) {\n for (int j = index; j &lt; graph-&gt;size - 1; j++) {\n graph-&gt;adjMat[i][j] = graph-&gt;adjMat[i][j + 1];\n }\n }\n graph-&gt;size--;\n}\n\n/* \u65b0\u589e\u908a */\n// \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(GraphAdjMat *graph, int i, int j) {\n if (i &lt; 0 || j &lt; 0 || i &gt;= graph-&gt;size || j &gt;= graph-&gt;size || i == j) {\n fprintf(stderr, \"\u908a\u7d22\u5f15\u8d8a\u754c\u6216\u76f8\u7b49\\n\");\n return;\n }\n graph-&gt;adjMat[i][j] = 1;\n graph-&gt;adjMat[j][i] = 1;\n}\n\n/* \u522a\u9664\u908a */\n// \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(GraphAdjMat *graph, int i, int j) {\n if (i &lt; 0 || j &lt; 0 || i &gt;= graph-&gt;size || j &gt;= graph-&gt;size || i == j) {\n fprintf(stderr, \"\u908a\u7d22\u5f15\u8d8a\u754c\u6216\u76f8\u7b49\\n\");\n return;\n }\n graph-&gt;adjMat[i][j] = 0;\n graph-&gt;adjMat[j][i] = 0;\n}\n\n/* \u5217\u5370\u9130\u63a5\u77e9\u9663 */\nvoid printGraphAdjMat(GraphAdjMat *graph) {\n printf(\"\u9802\u9ede\u4e32\u5217 = \");\n printArray(graph-&gt;vertices, graph-&gt;size);\n printf(\"\u9130\u63a5\u77e9\u9663 =\\n\");\n for (int i = 0; i &lt; graph-&gt;size; i++) {\n printArray(graph-&gt;adjMat[i], graph-&gt;size);\n }\n}\n</code></pre> graph_adjacency_matrix.kt<pre><code>/* \u57fa\u65bc\u9130\u63a5\u77e9\u9663\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjMat(vertices: IntArray, edges: Array&lt;IntArray&gt;) {\n val vertices = mutableListOf&lt;Int&gt;() // \u9802\u9ede\u4e32\u5217\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9802\u9ede\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9802\u9ede\u7d22\u5f15\u201d\n val adjMat = mutableListOf&lt;MutableList&lt;Int&gt;&gt;() // \u9130\u63a5\u77e9\u9663\uff0c\u884c\u5217\u7d22\u5f15\u5c0d\u61c9\u201c\u9802\u9ede\u7d22\u5f15\u201d\n\n /* \u5efa\u69cb\u5b50 */\n init {\n // \u65b0\u589e\u9802\u9ede\n for (vertex in vertices) {\n addVertex(vertex)\n }\n // \u65b0\u589e\u908a\n // \u8acb\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9802\u9ede\u7d22\u5f15\uff0c\u5373\u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n for (edge in edges) {\n addEdge(edge[0], edge[1])\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n fun size(): Int {\n return vertices.size\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n fun addVertex(_val: Int) {\n val n = size()\n // \u5411\u9802\u9ede\u4e32\u5217\u4e2d\u65b0\u589e\u65b0\u9802\u9ede\u7684\u503c\n vertices.add(_val)\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u884c\n val newRow = mutableListOf&lt;Int&gt;()\n for (j in 0..&lt;n) {\n newRow.add(0)\n }\n adjMat.add(newRow)\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u5217\n for (row in adjMat) {\n row.add(0)\n }\n }\n\n /* \u522a\u9664\u9802\u9ede */\n fun removeVertex(index: Int) {\n if (index &gt;= size())\n throw IndexOutOfBoundsException()\n // \u5728\u9802\u9ede\u4e32\u5217\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9802\u9ede\n vertices.removeAt(index)\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.removeAt(index)\n // \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u5217\n for (row in adjMat) {\n row.removeAt(index)\n }\n }\n\n /* \u65b0\u589e\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n fun addEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j)\n throw IndexOutOfBoundsException()\n // \u5728\u7121\u5411\u5716\u4e2d\uff0c\u9130\u63a5\u77e9\u9663\u95dc\u65bc\u4e3b\u5c0d\u89d2\u7dda\u5c0d\u7a31\uff0c\u5373\u6eff\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1\n adjMat[j][i] = 1\n }\n\n /* \u522a\u9664\u908a */\n // \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n fun removeEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j)\n throw IndexOutOfBoundsException()\n adjMat[i][j] = 0\n adjMat[j][i] = 0\n }\n\n /* \u5217\u5370\u9130\u63a5\u77e9\u9663 */\n fun print() {\n print(\"\u9802\u9ede\u4e32\u5217 = \")\n println(vertices)\n println(\"\u9130\u63a5\u77e9\u9663 =\")\n printMatrix(adjMat)\n }\n}\n</code></pre> graph_adjacency_matrix.rb<pre><code>### \u57fa\u65bc\u9130\u63a5\u77e9\u9663\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 ###\nclass GraphAdjMat\n def initialize(vertices, edges)\n ### \u5efa\u69cb\u5b50 ###\n # \u9802\u9ede\u4e32\u5217\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9802\u9ede\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9802\u9ede\u7d22\u5f15\u201d\n @vertices = []\n # \u9130\u63a5\u77e9\u9663\uff0c\u884c\u5217\u7d22\u5f15\u5c0d\u61c9\u201c\u9802\u9ede\u7d22\u5f15\u201d\n @adj_mat = []\n # \u65b0\u589e\u9802\u9ede\n vertices.each { |val| add_vertex(val) }\n # \u65b0\u589e\u908a\n # \u8acb\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9802\u9ede\u7d22\u5f15\uff0c\u5373\u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n edges.each { |e| add_edge(e[0], e[1]) }\n end\n\n ### \u7372\u53d6\u9802\u9ede\u6578\u91cf ###\n def size\n @vertices.length\n end\n\n ### \u65b0\u589e\u9802\u9ede ###\n def add_vertex(val)\n n = size\n # \u5411\u9802\u9ede\u4e32\u5217\u4e2d\u65b0\u589e\u65b0\u9802\u9ede\u7684\u503c\n @vertices &lt;&lt; val\n # \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u884c\n new_row = Array.new(n, 0)\n @adj_mat &lt;&lt; new_row\n # \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u65b0\u589e\u4e00\u5217\n @adj_mat.each { |row| row &lt;&lt; 0 }\n end\n\n ### \u522a\u9664\u9802\u9ede ###\n def remove_vertex(index)\n raise IndexError if index &gt;= size\n\n # \u5728\u9802\u9ede\u4e32\u5217\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9802\u9ede\n @vertices.delete_at(index)\n # \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u884c\n @adj_mat.delete_at(index)\n # \u5728\u9130\u63a5\u77e9\u9663\u4e2d\u522a\u9664\u7d22\u5f15 index \u7684\u5217\n @adj_mat.each { |row| row.delete_at(index) }\n end\n\n ### \u65b0\u589e\u908a ###\n def add_edge(i, j)\n # \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n # \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if i &lt; 0 || j &lt; 0 || i &gt;= size || j &gt;= size || i == j\n raise IndexError\n end\n # \u5728\u7121\u5411\u5716\u4e2d\uff0c\u9130\u63a5\u77e9\u9663\u95dc\u65bc\u4e3b\u5c0d\u89d2\u7dda\u5c0d\u7a31\uff0c\u5373\u6eff\u8db3 (i, j) == (j, i)\n @adj_mat[i][j] = 1\n @adj_mat[j][i] = 1\n end\n\n ### \u522a\u9664\u908a ###\n def remove_edge(i, j)\n # \u53c3\u6578 i, j \u5c0d\u61c9 vertices \u5143\u7d20\u7d22\u5f15\n # \u7d22\u5f15\u8d8a\u754c\u8207\u76f8\u7b49\u8655\u7406\n if i &lt; 0 || j &lt; 0 || i &gt;= size || j &gt;= size || i == j\n raise IndexError\n end\n @adj_mat[i][j] = 0\n @adj_mat[j][i] = 0\n end\n\n ### \u5217\u5370\u9130\u63a5\u77e9\u9663 ###\n def __print__\n puts \"\u9802\u9ede\u4e32\u5217 = #{@vertices}\"\n puts '\u9130\u63a5\u77e9\u9663 ='\n print_matrix(@adj_mat)\n end\nend\n</code></pre> graph_adjacency_matrix.zig<pre><code>[class]{GraphAdjMat}-[func]{}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_graph/graph_operations/#922","title":"9.2.2 \u00a0 \u57fa\u65bc\u9130\u63a5\u8868\u7684\u5be6\u73fe","text":"<p>\u8a2d\u7121\u5411\u5716\u7684\u9802\u9ede\u7e3d\u6578\u70ba \\(n\\)\u3001\u908a\u7e3d\u6578\u70ba \\(m\\) \uff0c\u5247\u53ef\u6839\u64da\u5716 9-8 \u6240\u793a\u7684\u65b9\u6cd5\u5be6\u73fe\u5404\u7a2e\u64cd\u4f5c\u3002</p> <ul> <li>\u65b0\u589e\u908a\uff1a\u5728\u9802\u9ede\u5c0d\u61c9\u93c8\u7d50\u4e32\u5217\u7684\u672b\u5c3e\u65b0\u589e\u908a\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u6642\u9593\u3002\u56e0\u70ba\u662f\u7121\u5411\u5716\uff0c\u6240\u4ee5\u9700\u8981\u540c\u6642\u65b0\u589e\u5169\u500b\u65b9\u5411\u7684\u908a\u3002</li> <li>\u522a\u9664\u908a\uff1a\u5728\u9802\u9ede\u5c0d\u61c9\u93c8\u7d50\u4e32\u5217\u4e2d\u67e5\u8a62\u4e26\u522a\u9664\u6307\u5b9a\u908a\uff0c\u4f7f\u7528 \\(O(m)\\) \u6642\u9593\u3002\u5728\u7121\u5411\u5716\u4e2d\uff0c\u9700\u8981\u540c\u6642\u522a\u9664\u5169\u500b\u65b9\u5411\u7684\u908a\u3002</li> <li>\u65b0\u589e\u9802\u9ede\uff1a\u5728\u9130\u63a5\u8868\u4e2d\u65b0\u589e\u4e00\u500b\u93c8\u7d50\u4e32\u5217\uff0c\u4e26\u5c07\u65b0\u589e\u9802\u9ede\u4f5c\u70ba\u93c8\u7d50\u4e32\u5217\u982d\u7bc0\u9ede\uff0c\u4f7f\u7528 \\(O(1)\\) \u6642\u9593\u3002</li> <li>\u522a\u9664\u9802\u9ede\uff1a\u9700\u8d70\u8a2a\u6574\u500b\u9130\u63a5\u8868\uff0c\u522a\u9664\u5305\u542b\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u908a\uff0c\u4f7f\u7528 \\(O(n + m)\\) \u6642\u9593\u3002</li> <li>\u521d\u59cb\u5316\uff1a\u5728\u9130\u63a5\u8868\u4e2d\u5efa\u7acb \\(n\\) \u500b\u9802\u9ede\u548c \\(2m\\) \u689d\u908a\uff0c\u4f7f\u7528 \\(O(n + m)\\) \u6642\u9593\u3002</li> </ul> \u521d\u59cb\u5316\u9130\u63a5\u8868\u65b0\u589e\u908a\u522a\u9664\u908a\u65b0\u589e\u9802\u9ede\u522a\u9664\u9802\u9ede <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 9-8 \u00a0 \u9130\u63a5\u8868\u7684\u521d\u59cb\u5316\u3001\u589e\u522a\u908a\u3001\u589e\u522a\u9802\u9ede </p> <p>\u4ee5\u4e0b\u662f\u9130\u63a5\u8868\u7684\u7a0b\u5f0f\u78bc\u5be6\u73fe\u3002\u5c0d\u6bd4\u5716 9-8 \uff0c\u5be6\u969b\u7a0b\u5f0f\u78bc\u6709\u4ee5\u4e0b\u4e0d\u540c\u3002</p> <ul> <li>\u70ba\u4e86\u65b9\u4fbf\u65b0\u589e\u8207\u522a\u9664\u9802\u9ede\uff0c\u4ee5\u53ca\u7c21\u5316\u7a0b\u5f0f\u78bc\uff0c\u6211\u5011\u4f7f\u7528\u4e32\u5217\uff08\u52d5\u614b\u9663\u5217\uff09\u4f86\u4ee3\u66ff\u93c8\u7d50\u4e32\u5217\u3002</li> <li>\u4f7f\u7528\u96dc\u6e4a\u8868\u4f86\u5132\u5b58\u9130\u63a5\u8868\uff0c<code>key</code> \u70ba\u9802\u9ede\u4f8b\u9805\uff0c<code>value</code> \u70ba\u8a72\u9802\u9ede\u7684\u9130\u63a5\u9802\u9ede\u4e32\u5217\uff08\u93c8\u7d50\u4e32\u5217\uff09\u3002</li> </ul> <p>\u53e6\u5916\uff0c\u6211\u5011\u5728\u9130\u63a5\u8868\u4e2d\u4f7f\u7528 <code>Vertex</code> \u985e\u5225\u4f86\u8868\u793a\u9802\u9ede\uff0c\u9019\u6a23\u505a\u7684\u539f\u56e0\u662f\uff1a\u5982\u679c\u8207\u9130\u63a5\u77e9\u9663\u4e00\u6a23\uff0c\u7528\u4e32\u5217\u7d22\u5f15\u4f86\u5340\u5206\u4e0d\u540c\u9802\u9ede\uff0c\u90a3\u9ebc\u5047\u8a2d\u8981\u522a\u9664\u7d22\u5f15\u70ba \\(i\\) \u7684\u9802\u9ede\uff0c\u5247\u9700\u8d70\u8a2a\u6574\u500b\u9130\u63a5\u8868\uff0c\u5c07\u6240\u6709\u5927\u65bc \\(i\\) \u7684\u7d22\u5f15\u5168\u90e8\u6e1b \\(1\\) \uff0c\u6548\u7387\u5f88\u4f4e\u3002\u800c\u5982\u679c\u6bcf\u500b\u9802\u9ede\u90fd\u662f\u552f\u4e00\u7684 <code>Vertex</code> \u4f8b\u9805\uff0c\u522a\u9664\u67d0\u4e00\u9802\u9ede\u4e4b\u5f8c\u5c31\u7121\u9808\u6539\u52d5\u5176\u4ed6\u9802\u9ede\u4e86\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig graph_adjacency_list.py<pre><code>class GraphAdjList:\n \"\"\"\u57fa\u65bc\u9130\u63a5\u8868\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225\"\"\"\n\n def __init__(self, edges: list[list[Vertex]]):\n \"\"\"\u5efa\u69cb\u5b50\"\"\"\n # \u9130\u63a5\u8868\uff0ckey\uff1a\u9802\u9ede\uff0cvalue\uff1a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n self.adj_list = dict[Vertex, list[Vertex]]()\n # \u65b0\u589e\u6240\u6709\u9802\u9ede\u548c\u908a\n for edge in edges:\n self.add_vertex(edge[0])\n self.add_vertex(edge[1])\n self.add_edge(edge[0], edge[1])\n\n def size(self) -&gt; int:\n \"\"\"\u7372\u53d6\u9802\u9ede\u6578\u91cf\"\"\"\n return len(self.adj_list)\n\n def add_edge(self, vet1: Vertex, vet2: Vertex):\n \"\"\"\u65b0\u589e\u908a\"\"\"\n if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n raise ValueError()\n # \u65b0\u589e\u908a vet1 - vet2\n self.adj_list[vet1].append(vet2)\n self.adj_list[vet2].append(vet1)\n\n def remove_edge(self, vet1: Vertex, vet2: Vertex):\n \"\"\"\u522a\u9664\u908a\"\"\"\n if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n raise ValueError()\n # \u522a\u9664\u908a vet1 - vet2\n self.adj_list[vet1].remove(vet2)\n self.adj_list[vet2].remove(vet1)\n\n def add_vertex(self, vet: Vertex):\n \"\"\"\u65b0\u589e\u9802\u9ede\"\"\"\n if vet in self.adj_list:\n return\n # \u5728\u9130\u63a5\u8868\u4e2d\u65b0\u589e\u4e00\u500b\u65b0\u93c8\u7d50\u4e32\u5217\n self.adj_list[vet] = []\n\n def remove_vertex(self, vet: Vertex):\n \"\"\"\u522a\u9664\u9802\u9ede\"\"\"\n if vet not in self.adj_list:\n raise ValueError()\n # \u5728\u9130\u63a5\u8868\u4e2d\u522a\u9664\u9802\u9ede vet \u5c0d\u61c9\u7684\u93c8\u7d50\u4e32\u5217\n self.adj_list.pop(vet)\n # \u8d70\u8a2a\u5176\u4ed6\u9802\u9ede\u7684\u93c8\u7d50\u4e32\u5217\uff0c\u522a\u9664\u6240\u6709\u5305\u542b vet \u7684\u908a\n for vertex in self.adj_list:\n if vet in self.adj_list[vertex]:\n self.adj_list[vertex].remove(vet)\n\n def print(self):\n \"\"\"\u5217\u5370\u9130\u63a5\u8868\"\"\"\n print(\"\u9130\u63a5\u8868 =\")\n for vertex in self.adj_list:\n tmp = [v.val for v in self.adj_list[vertex]]\n print(f\"{vertex.val}: {tmp},\")\n</code></pre> graph_adjacency_list.cpp<pre><code>/* \u57fa\u65bc\u9130\u63a5\u8868\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjList {\n public:\n // \u9130\u63a5\u8868\uff0ckey\uff1a\u9802\u9ede\uff0cvalue\uff1a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n unordered_map&lt;Vertex *, vector&lt;Vertex *&gt;&gt; adjList;\n\n /* \u5728 vector \u4e2d\u522a\u9664\u6307\u5b9a\u7bc0\u9ede */\n void remove(vector&lt;Vertex *&gt; &amp;vec, Vertex *vet) {\n for (int i = 0; i &lt; vec.size(); i++) {\n if (vec[i] == vet) {\n vec.erase(vec.begin() + i);\n break;\n }\n }\n }\n\n /* \u5efa\u69cb\u5b50 */\n GraphAdjList(const vector&lt;vector&lt;Vertex *&gt;&gt; &amp;edges) {\n // \u65b0\u589e\u6240\u6709\u9802\u9ede\u548c\u908a\n for (const vector&lt;Vertex *&gt; &amp;edge : edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n int size() {\n return adjList.size();\n }\n\n /* \u65b0\u589e\u908a */\n void addEdge(Vertex *vet1, Vertex *vet2) {\n if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9802\u9ede\");\n // \u65b0\u589e\u908a vet1 - vet2\n adjList[vet1].push_back(vet2);\n adjList[vet2].push_back(vet1);\n }\n\n /* \u522a\u9664\u908a */\n void removeEdge(Vertex *vet1, Vertex *vet2) {\n if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9802\u9ede\");\n // \u522a\u9664\u908a vet1 - vet2\n remove(adjList[vet1], vet2);\n remove(adjList[vet2], vet1);\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n void addVertex(Vertex *vet) {\n if (adjList.count(vet))\n return;\n // \u5728\u9130\u63a5\u8868\u4e2d\u65b0\u589e\u4e00\u500b\u65b0\u93c8\u7d50\u4e32\u5217\n adjList[vet] = vector&lt;Vertex *&gt;();\n }\n\n /* \u522a\u9664\u9802\u9ede */\n void removeVertex(Vertex *vet) {\n if (!adjList.count(vet))\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9802\u9ede\");\n // \u5728\u9130\u63a5\u8868\u4e2d\u522a\u9664\u9802\u9ede vet \u5c0d\u61c9\u7684\u93c8\u7d50\u4e32\u5217\n adjList.erase(vet);\n // \u8d70\u8a2a\u5176\u4ed6\u9802\u9ede\u7684\u93c8\u7d50\u4e32\u5217\uff0c\u522a\u9664\u6240\u6709\u5305\u542b vet \u7684\u908a\n for (auto &amp;adj : adjList) {\n remove(adj.second, vet);\n }\n }\n\n /* \u5217\u5370\u9130\u63a5\u8868 */\n void print() {\n cout &lt;&lt; \"\u9130\u63a5\u8868 =\" &lt;&lt; endl;\n for (auto &amp;adj : adjList) {\n const auto &amp;key = adj.first;\n const auto &amp;vec = adj.second;\n cout &lt;&lt; key-&gt;val &lt;&lt; \": \";\n printVector(vetsToVals(vec));\n }\n }\n};\n</code></pre> graph_adjacency_list.java<pre><code>/* \u57fa\u65bc\u9130\u63a5\u8868\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjList {\n // \u9130\u63a5\u8868\uff0ckey\uff1a\u9802\u9ede\uff0cvalue\uff1a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n Map&lt;Vertex, List&lt;Vertex&gt;&gt; adjList;\n\n /* \u5efa\u69cb\u5b50 */\n public GraphAdjList(Vertex[][] edges) {\n this.adjList = new HashMap&lt;&gt;();\n // \u65b0\u589e\u6240\u6709\u9802\u9ede\u548c\u908a\n for (Vertex[] edge : edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n public int size() {\n return adjList.size();\n }\n\n /* \u65b0\u589e\u908a */\n public void addEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw new IllegalArgumentException();\n // \u65b0\u589e\u908a vet1 - vet2\n adjList.get(vet1).add(vet2);\n adjList.get(vet2).add(vet1);\n }\n\n /* \u522a\u9664\u908a */\n public void removeEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw new IllegalArgumentException();\n // \u522a\u9664\u908a vet1 - vet2\n adjList.get(vet1).remove(vet2);\n adjList.get(vet2).remove(vet1);\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n public void addVertex(Vertex vet) {\n if (adjList.containsKey(vet))\n return;\n // \u5728\u9130\u63a5\u8868\u4e2d\u65b0\u589e\u4e00\u500b\u65b0\u93c8\u7d50\u4e32\u5217\n adjList.put(vet, new ArrayList&lt;&gt;());\n }\n\n /* \u522a\u9664\u9802\u9ede */\n public void removeVertex(Vertex vet) {\n if (!adjList.containsKey(vet))\n throw new IllegalArgumentException();\n // \u5728\u9130\u63a5\u8868\u4e2d\u522a\u9664\u9802\u9ede vet \u5c0d\u61c9\u7684\u93c8\u7d50\u4e32\u5217\n adjList.remove(vet);\n // \u8d70\u8a2a\u5176\u4ed6\u9802\u9ede\u7684\u93c8\u7d50\u4e32\u5217\uff0c\u522a\u9664\u6240\u6709\u5305\u542b vet \u7684\u908a\n for (List&lt;Vertex&gt; list : adjList.values()) {\n list.remove(vet);\n }\n }\n\n /* \u5217\u5370\u9130\u63a5\u8868 */\n public void print() {\n System.out.println(\"\u9130\u63a5\u8868 =\");\n for (Map.Entry&lt;Vertex, List&lt;Vertex&gt;&gt; pair : adjList.entrySet()) {\n List&lt;Integer&gt; tmp = new ArrayList&lt;&gt;();\n for (Vertex vertex : pair.getValue())\n tmp.add(vertex.val);\n System.out.println(pair.getKey().val + \": \" + tmp + \",\");\n }\n }\n}\n</code></pre> graph_adjacency_list.cs<pre><code>/* \u57fa\u65bc\u9130\u63a5\u8868\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjList {\n // \u9130\u63a5\u8868\uff0ckey\uff1a\u9802\u9ede\uff0cvalue\uff1a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n public Dictionary&lt;Vertex, List&lt;Vertex&gt;&gt; adjList;\n\n /* \u5efa\u69cb\u5b50 */\n public GraphAdjList(Vertex[][] edges) {\n adjList = [];\n // \u65b0\u589e\u6240\u6709\u9802\u9ede\u548c\u908a\n foreach (Vertex[] edge in edges) {\n AddVertex(edge[0]);\n AddVertex(edge[1]);\n AddEdge(edge[0], edge[1]);\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n int Size() {\n return adjList.Count;\n }\n\n /* \u65b0\u589e\u908a */\n public void AddEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n throw new InvalidOperationException();\n // \u65b0\u589e\u908a vet1 - vet2\n adjList[vet1].Add(vet2);\n adjList[vet2].Add(vet1);\n }\n\n /* \u522a\u9664\u908a */\n public void RemoveEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n throw new InvalidOperationException();\n // \u522a\u9664\u908a vet1 - vet2\n adjList[vet1].Remove(vet2);\n adjList[vet2].Remove(vet1);\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n public void AddVertex(Vertex vet) {\n if (adjList.ContainsKey(vet))\n return;\n // \u5728\u9130\u63a5\u8868\u4e2d\u65b0\u589e\u4e00\u500b\u65b0\u93c8\u7d50\u4e32\u5217\n adjList.Add(vet, []);\n }\n\n /* \u522a\u9664\u9802\u9ede */\n public void RemoveVertex(Vertex vet) {\n if (!adjList.ContainsKey(vet))\n throw new InvalidOperationException();\n // \u5728\u9130\u63a5\u8868\u4e2d\u522a\u9664\u9802\u9ede vet \u5c0d\u61c9\u7684\u93c8\u7d50\u4e32\u5217\n adjList.Remove(vet);\n // \u8d70\u8a2a\u5176\u4ed6\u9802\u9ede\u7684\u93c8\u7d50\u4e32\u5217\uff0c\u522a\u9664\u6240\u6709\u5305\u542b vet \u7684\u908a\n foreach (List&lt;Vertex&gt; list in adjList.Values) {\n list.Remove(vet);\n }\n }\n\n /* \u5217\u5370\u9130\u63a5\u8868 */\n public void Print() {\n Console.WriteLine(\"\u9130\u63a5\u8868 =\");\n foreach (KeyValuePair&lt;Vertex, List&lt;Vertex&gt;&gt; pair in adjList) {\n List&lt;int&gt; tmp = [];\n foreach (Vertex vertex in pair.Value)\n tmp.Add(vertex.val);\n Console.WriteLine(pair.Key.val + \": [\" + string.Join(\", \", tmp) + \"],\");\n }\n }\n}\n</code></pre> graph_adjacency_list.go<pre><code>/* \u57fa\u65bc\u9130\u63a5\u8868\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\ntype graphAdjList struct {\n // \u9130\u63a5\u8868\uff0ckey\uff1a\u9802\u9ede\uff0cvalue\uff1a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n adjList map[Vertex][]Vertex\n}\n\n/* \u5efa\u69cb\u5b50 */\nfunc newGraphAdjList(edges [][]Vertex) *graphAdjList {\n g := &amp;graphAdjList{\n adjList: make(map[Vertex][]Vertex),\n }\n // \u65b0\u589e\u6240\u6709\u9802\u9ede\u548c\u908a\n for _, edge := range edges {\n g.addVertex(edge[0])\n g.addVertex(edge[1])\n g.addEdge(edge[0], edge[1])\n }\n return g\n}\n\n/* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\nfunc (g *graphAdjList) size() int {\n return len(g.adjList)\n}\n\n/* \u65b0\u589e\u908a */\nfunc (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {\n _, ok1 := g.adjList[vet1]\n _, ok2 := g.adjList[vet2]\n if !ok1 || !ok2 || vet1 == vet2 {\n panic(\"error\")\n }\n // \u65b0\u589e\u908a vet1 - vet2, \u65b0\u589e\u533f\u540d struct{},\n g.adjList[vet1] = append(g.adjList[vet1], vet2)\n g.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n\n/* \u522a\u9664\u908a */\nfunc (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {\n _, ok1 := g.adjList[vet1]\n _, ok2 := g.adjList[vet2]\n if !ok1 || !ok2 || vet1 == vet2 {\n panic(\"error\")\n }\n // \u522a\u9664\u908a vet1 - vet2\n g.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)\n g.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)\n}\n\n/* \u65b0\u589e\u9802\u9ede */\nfunc (g *graphAdjList) addVertex(vet Vertex) {\n _, ok := g.adjList[vet]\n if ok {\n return\n }\n // \u5728\u9130\u63a5\u8868\u4e2d\u65b0\u589e\u4e00\u500b\u65b0\u93c8\u7d50\u4e32\u5217\n g.adjList[vet] = make([]Vertex, 0)\n}\n\n/* \u522a\u9664\u9802\u9ede */\nfunc (g *graphAdjList) removeVertex(vet Vertex) {\n _, ok := g.adjList[vet]\n if !ok {\n panic(\"error\")\n }\n // \u5728\u9130\u63a5\u8868\u4e2d\u522a\u9664\u9802\u9ede vet \u5c0d\u61c9\u7684\u93c8\u7d50\u4e32\u5217\n delete(g.adjList, vet)\n // \u8d70\u8a2a\u5176\u4ed6\u9802\u9ede\u7684\u93c8\u7d50\u4e32\u5217\uff0c\u522a\u9664\u6240\u6709\u5305\u542b vet \u7684\u908a\n for v, list := range g.adjList {\n g.adjList[v] = DeleteSliceElms(list, vet)\n }\n}\n\n/* \u5217\u5370\u9130\u63a5\u8868 */\nfunc (g *graphAdjList) print() {\n var builder strings.Builder\n fmt.Printf(\"\u9130\u63a5\u8868 = \\n\")\n for k, v := range g.adjList {\n builder.WriteString(\"\\t\\t\" + strconv.Itoa(k.Val) + \": \")\n for _, vet := range v {\n builder.WriteString(strconv.Itoa(vet.Val) + \" \")\n }\n fmt.Println(builder.String())\n builder.Reset()\n }\n}\n</code></pre> graph_adjacency_list.swift<pre><code>/* \u57fa\u65bc\u9130\u63a5\u8868\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjList {\n // \u9130\u63a5\u8868\uff0ckey\uff1a\u9802\u9ede\uff0cvalue\uff1a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n public private(set) var adjList: [Vertex: [Vertex]]\n\n /* \u5efa\u69cb\u5b50 */\n public init(edges: [[Vertex]]) {\n adjList = [:]\n // \u65b0\u589e\u6240\u6709\u9802\u9ede\u548c\u908a\n for edge in edges {\n addVertex(vet: edge[0])\n addVertex(vet: edge[1])\n addEdge(vet1: edge[0], vet2: edge[1])\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n public func size() -&gt; Int {\n adjList.count\n }\n\n /* \u65b0\u589e\u908a */\n public func addEdge(vet1: Vertex, vet2: Vertex) {\n if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n fatalError(\"\u53c3\u6578\u932f\u8aa4\")\n }\n // \u65b0\u589e\u908a vet1 - vet2\n adjList[vet1]?.append(vet2)\n adjList[vet2]?.append(vet1)\n }\n\n /* \u522a\u9664\u908a */\n public func removeEdge(vet1: Vertex, vet2: Vertex) {\n if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n fatalError(\"\u53c3\u6578\u932f\u8aa4\")\n }\n // \u522a\u9664\u908a vet1 - vet2\n adjList[vet1]?.removeAll { $0 == vet2 }\n adjList[vet2]?.removeAll { $0 == vet1 }\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n public func addVertex(vet: Vertex) {\n if adjList[vet] != nil {\n return\n }\n // \u5728\u9130\u63a5\u8868\u4e2d\u65b0\u589e\u4e00\u500b\u65b0\u93c8\u7d50\u4e32\u5217\n adjList[vet] = []\n }\n\n /* \u522a\u9664\u9802\u9ede */\n public func removeVertex(vet: Vertex) {\n if adjList[vet] == nil {\n fatalError(\"\u53c3\u6578\u932f\u8aa4\")\n }\n // \u5728\u9130\u63a5\u8868\u4e2d\u522a\u9664\u9802\u9ede vet \u5c0d\u61c9\u7684\u93c8\u7d50\u4e32\u5217\n adjList.removeValue(forKey: vet)\n // \u8d70\u8a2a\u5176\u4ed6\u9802\u9ede\u7684\u93c8\u7d50\u4e32\u5217\uff0c\u522a\u9664\u6240\u6709\u5305\u542b vet \u7684\u908a\n for key in adjList.keys {\n adjList[key]?.removeAll { $0 == vet }\n }\n }\n\n /* \u5217\u5370\u9130\u63a5\u8868 */\n public func print() {\n Swift.print(\"\u9130\u63a5\u8868 =\")\n for (vertex, list) in adjList {\n let list = list.map { $0.val }\n Swift.print(\"\\(vertex.val): \\(list),\")\n }\n }\n}\n</code></pre> graph_adjacency_list.js<pre><code>/* \u57fa\u65bc\u9130\u63a5\u8868\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjList {\n // \u9130\u63a5\u8868\uff0ckey\uff1a\u9802\u9ede\uff0cvalue\uff1a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n adjList;\n\n /* \u5efa\u69cb\u5b50 */\n constructor(edges) {\n this.adjList = new Map();\n // \u65b0\u589e\u6240\u6709\u9802\u9ede\u548c\u908a\n for (const edge of edges) {\n this.addVertex(edge[0]);\n this.addVertex(edge[1]);\n this.addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n size() {\n return this.adjList.size;\n }\n\n /* \u65b0\u589e\u908a */\n addEdge(vet1, vet2) {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u65b0\u589e\u908a vet1 - vet2\n this.adjList.get(vet1).push(vet2);\n this.adjList.get(vet2).push(vet1);\n }\n\n /* \u522a\u9664\u908a */\n removeEdge(vet1, vet2) {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u522a\u9664\u908a vet1 - vet2\n this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n addVertex(vet) {\n if (this.adjList.has(vet)) return;\n // \u5728\u9130\u63a5\u8868\u4e2d\u65b0\u589e\u4e00\u500b\u65b0\u93c8\u7d50\u4e32\u5217\n this.adjList.set(vet, []);\n }\n\n /* \u522a\u9664\u9802\u9ede */\n removeVertex(vet) {\n if (!this.adjList.has(vet)) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5728\u9130\u63a5\u8868\u4e2d\u522a\u9664\u9802\u9ede vet \u5c0d\u61c9\u7684\u93c8\u7d50\u4e32\u5217\n this.adjList.delete(vet);\n // \u8d70\u8a2a\u5176\u4ed6\u9802\u9ede\u7684\u93c8\u7d50\u4e32\u5217\uff0c\u522a\u9664\u6240\u6709\u5305\u542b vet \u7684\u908a\n for (const set of this.adjList.values()) {\n const index = set.indexOf(vet);\n if (index &gt; -1) {\n set.splice(index, 1);\n }\n }\n }\n\n /* \u5217\u5370\u9130\u63a5\u8868 */\n print() {\n console.log('\u9130\u63a5\u8868 =');\n for (const [key, value] of this.adjList) {\n const tmp = [];\n for (const vertex of value) {\n tmp.push(vertex.val);\n }\n console.log(key.val + ': ' + tmp.join());\n }\n }\n}\n</code></pre> graph_adjacency_list.ts<pre><code>/* \u57fa\u65bc\u9130\u63a5\u8868\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjList {\n // \u9130\u63a5\u8868\uff0ckey\uff1a\u9802\u9ede\uff0cvalue\uff1a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n adjList: Map&lt;Vertex, Vertex[]&gt;;\n\n /* \u5efa\u69cb\u5b50 */\n constructor(edges: Vertex[][]) {\n this.adjList = new Map();\n // \u65b0\u589e\u6240\u6709\u9802\u9ede\u548c\u908a\n for (const edge of edges) {\n this.addVertex(edge[0]);\n this.addVertex(edge[1]);\n this.addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n size(): number {\n return this.adjList.size;\n }\n\n /* \u65b0\u589e\u908a */\n addEdge(vet1: Vertex, vet2: Vertex): void {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u65b0\u589e\u908a vet1 - vet2\n this.adjList.get(vet1).push(vet2);\n this.adjList.get(vet2).push(vet1);\n }\n\n /* \u522a\u9664\u908a */\n removeEdge(vet1: Vertex, vet2: Vertex): void {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u522a\u9664\u908a vet1 - vet2\n this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n addVertex(vet: Vertex): void {\n if (this.adjList.has(vet)) return;\n // \u5728\u9130\u63a5\u8868\u4e2d\u65b0\u589e\u4e00\u500b\u65b0\u93c8\u7d50\u4e32\u5217\n this.adjList.set(vet, []);\n }\n\n /* \u522a\u9664\u9802\u9ede */\n removeVertex(vet: Vertex): void {\n if (!this.adjList.has(vet)) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5728\u9130\u63a5\u8868\u4e2d\u522a\u9664\u9802\u9ede vet \u5c0d\u61c9\u7684\u93c8\u7d50\u4e32\u5217\n this.adjList.delete(vet);\n // \u8d70\u8a2a\u5176\u4ed6\u9802\u9ede\u7684\u93c8\u7d50\u4e32\u5217\uff0c\u522a\u9664\u6240\u6709\u5305\u542b vet \u7684\u908a\n for (const set of this.adjList.values()) {\n const index: number = set.indexOf(vet);\n if (index &gt; -1) {\n set.splice(index, 1);\n }\n }\n }\n\n /* \u5217\u5370\u9130\u63a5\u8868 */\n print(): void {\n console.log('\u9130\u63a5\u8868 =');\n for (const [key, value] of this.adjList.entries()) {\n const tmp = [];\n for (const vertex of value) {\n tmp.push(vertex.val);\n }\n console.log(key.val + ': ' + tmp.join());\n }\n }\n}\n</code></pre> graph_adjacency_list.dart<pre><code>/* \u57fa\u65bc\u9130\u63a5\u8868\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjList {\n // \u9130\u63a5\u8868\uff0ckey\uff1a\u9802\u9ede\uff0cvalue\uff1a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n Map&lt;Vertex, List&lt;Vertex&gt;&gt; adjList = {};\n\n /* \u5efa\u69cb\u5b50 */\n GraphAdjList(List&lt;List&lt;Vertex&gt;&gt; edges) {\n for (List&lt;Vertex&gt; edge in edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n int size() {\n return adjList.length;\n }\n\n /* \u65b0\u589e\u908a */\n void addEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) ||\n !adjList.containsKey(vet2) ||\n vet1 == vet2) {\n throw ArgumentError;\n }\n // \u65b0\u589e\u908a vet1 - vet2\n adjList[vet1]!.add(vet2);\n adjList[vet2]!.add(vet1);\n }\n\n /* \u522a\u9664\u908a */\n void removeEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) ||\n !adjList.containsKey(vet2) ||\n vet1 == vet2) {\n throw ArgumentError;\n }\n // \u522a\u9664\u908a vet1 - vet2\n adjList[vet1]!.remove(vet2);\n adjList[vet2]!.remove(vet1);\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n void addVertex(Vertex vet) {\n if (adjList.containsKey(vet)) return;\n // \u5728\u9130\u63a5\u8868\u4e2d\u65b0\u589e\u4e00\u500b\u65b0\u93c8\u7d50\u4e32\u5217\n adjList[vet] = [];\n }\n\n /* \u522a\u9664\u9802\u9ede */\n void removeVertex(Vertex vet) {\n if (!adjList.containsKey(vet)) {\n throw ArgumentError;\n }\n // \u5728\u9130\u63a5\u8868\u4e2d\u522a\u9664\u9802\u9ede vet \u5c0d\u61c9\u7684\u93c8\u7d50\u4e32\u5217\n adjList.remove(vet);\n // \u8d70\u8a2a\u5176\u4ed6\u9802\u9ede\u7684\u93c8\u7d50\u4e32\u5217\uff0c\u522a\u9664\u6240\u6709\u5305\u542b vet \u7684\u908a\n adjList.forEach((key, value) {\n value.remove(vet);\n });\n }\n\n /* \u5217\u5370\u9130\u63a5\u8868 */\n void printAdjList() {\n print(\"\u9130\u63a5\u8868 =\");\n adjList.forEach((key, value) {\n List&lt;int&gt; tmp = [];\n for (Vertex vertex in value) {\n tmp.add(vertex.val);\n }\n print(\"${key.val}: $tmp,\");\n });\n }\n}\n</code></pre> graph_adjacency_list.rs<pre><code>/* \u57fa\u65bc\u9130\u63a5\u8868\u5be6\u73fe\u7684\u7121\u5411\u5716\u578b\u5225 */\npub struct GraphAdjList {\n // \u9130\u63a5\u8868\uff0ckey\uff1a\u9802\u9ede\uff0cvalue\uff1a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n pub adj_list: HashMap&lt;Vertex, Vec&lt;Vertex&gt;&gt;,\n}\n\nimpl GraphAdjList {\n /* \u5efa\u69cb\u5b50 */\n pub fn new(edges: Vec&lt;[Vertex; 2]&gt;) -&gt; Self {\n let mut graph = GraphAdjList {\n adj_list: HashMap::new(),\n };\n // \u65b0\u589e\u6240\u6709\u9802\u9ede\u548c\u908a\n for edge in edges {\n graph.add_vertex(edge[0]);\n graph.add_vertex(edge[1]);\n graph.add_edge(edge[0], edge[1]);\n }\n\n graph\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n #[allow(unused)]\n pub fn size(&amp;self) -&gt; usize {\n self.adj_list.len()\n }\n\n /* \u65b0\u589e\u908a */\n pub fn add_edge(&amp;mut self, vet1: Vertex, vet2: Vertex) {\n if !self.adj_list.contains_key(&amp;vet1) || !self.adj_list.contains_key(&amp;vet2) || vet1 == vet2\n {\n panic!(\"value error\");\n }\n // \u65b0\u589e\u908a vet1 - vet2\n self.adj_list.get_mut(&amp;vet1).unwrap().push(vet2);\n self.adj_list.get_mut(&amp;vet2).unwrap().push(vet1);\n }\n\n /* \u522a\u9664\u908a */\n #[allow(unused)]\n pub fn remove_edge(&amp;mut self, vet1: Vertex, vet2: Vertex) {\n if !self.adj_list.contains_key(&amp;vet1) || !self.adj_list.contains_key(&amp;vet2) || vet1 == vet2\n {\n panic!(\"value error\");\n }\n // \u522a\u9664\u908a vet1 - vet2\n self.adj_list\n .get_mut(&amp;vet1)\n .unwrap()\n .retain(|&amp;vet| vet != vet2);\n self.adj_list\n .get_mut(&amp;vet2)\n .unwrap()\n .retain(|&amp;vet| vet != vet1);\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n pub fn add_vertex(&amp;mut self, vet: Vertex) {\n if self.adj_list.contains_key(&amp;vet) {\n return;\n }\n // \u5728\u9130\u63a5\u8868\u4e2d\u65b0\u589e\u4e00\u500b\u65b0\u93c8\u7d50\u4e32\u5217\n self.adj_list.insert(vet, vec![]);\n }\n\n /* \u522a\u9664\u9802\u9ede */\n #[allow(unused)]\n pub fn remove_vertex(&amp;mut self, vet: Vertex) {\n if !self.adj_list.contains_key(&amp;vet) {\n panic!(\"value error\");\n }\n // \u5728\u9130\u63a5\u8868\u4e2d\u522a\u9664\u9802\u9ede vet \u5c0d\u61c9\u7684\u93c8\u7d50\u4e32\u5217\n self.adj_list.remove(&amp;vet);\n // \u8d70\u8a2a\u5176\u4ed6\u9802\u9ede\u7684\u93c8\u7d50\u4e32\u5217\uff0c\u522a\u9664\u6240\u6709\u5305\u542b vet \u7684\u908a\n for list in self.adj_list.values_mut() {\n list.retain(|&amp;v| v != vet);\n }\n }\n\n /* \u5217\u5370\u9130\u63a5\u8868 */\n pub fn print(&amp;self) {\n println!(\"\u9130\u63a5\u8868 =\");\n for (vertex, list) in &amp;self.adj_list {\n let list = list.iter().map(|vertex| vertex.val).collect::&lt;Vec&lt;i32&gt;&gt;();\n println!(\"{}: {:?},\", vertex.val, list);\n }\n }\n}\n</code></pre> graph_adjacency_list.c<pre><code>/* \u7bc0\u9ede\u7d50\u69cb\u9ad4 */\ntypedef struct AdjListNode {\n Vertex *vertex; // \u9802\u9ede\n struct AdjListNode *next; // \u5f8c\u7e7c\u7bc0\u9ede\n} AdjListNode;\n\n/* \u67e5\u8a62\u9802\u9ede\u5c0d\u61c9\u7684\u7bc0\u9ede */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n for (int i = 0; i &lt; graph-&gt;size; i++) {\n if (graph-&gt;heads[i]-&gt;vertex == vet) {\n return graph-&gt;heads[i];\n }\n }\n return NULL;\n}\n\n/* \u65b0\u589e\u908a\u8f14\u52a9\u51fd\u5f0f */\nvoid addEdgeHelper(AdjListNode *head, Vertex *vet) {\n AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode));\n node-&gt;vertex = vet;\n // \u982d\u63d2\u6cd5\n node-&gt;next = head-&gt;next;\n head-&gt;next = node;\n}\n\n/* \u522a\u9664\u908a\u8f14\u52a9\u51fd\u5f0f */\nvoid removeEdgeHelper(AdjListNode *head, Vertex *vet) {\n AdjListNode *pre = head;\n AdjListNode *cur = head-&gt;next;\n // \u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u641c\u7d22 vet \u5c0d\u61c9\u7bc0\u9ede\n while (cur != NULL &amp;&amp; cur-&gt;vertex != vet) {\n pre = cur;\n cur = cur-&gt;next;\n }\n if (cur == NULL)\n return;\n // \u5c07 vet \u5c0d\u61c9\u7bc0\u9ede\u5f9e\u93c8\u7d50\u4e32\u5217\u4e2d\u522a\u9664\n pre-&gt;next = cur-&gt;next;\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(cur);\n}\n\n/* \u57fa\u65bc\u9130\u63a5\u8868\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\ntypedef struct {\n AdjListNode *heads[MAX_SIZE]; // \u7bc0\u9ede\u9663\u5217\n int size; // \u7bc0\u9ede\u6578\u91cf\n} GraphAdjList;\n\n/* \u5efa\u69cb\u5b50 */\nGraphAdjList *newGraphAdjList() {\n GraphAdjList *graph = (GraphAdjList *)malloc(sizeof(GraphAdjList));\n if (!graph) {\n return NULL;\n }\n graph-&gt;size = 0;\n for (int i = 0; i &lt; MAX_SIZE; i++) {\n graph-&gt;heads[i] = NULL;\n }\n return graph;\n}\n\n/* \u6790\u69cb\u51fd\u5f0f */\nvoid delGraphAdjList(GraphAdjList *graph) {\n for (int i = 0; i &lt; graph-&gt;size; i++) {\n AdjListNode *cur = graph-&gt;heads[i];\n while (cur != NULL) {\n AdjListNode *next = cur-&gt;next;\n if (cur != graph-&gt;heads[i]) {\n free(cur);\n }\n cur = next;\n }\n free(graph-&gt;heads[i]-&gt;vertex);\n free(graph-&gt;heads[i]);\n }\n free(graph);\n}\n\n/* \u67e5\u8a62\u9802\u9ede\u5c0d\u61c9\u7684\u7bc0\u9ede */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n for (int i = 0; i &lt; graph-&gt;size; i++) {\n if (graph-&gt;heads[i]-&gt;vertex == vet) {\n return graph-&gt;heads[i];\n }\n }\n return NULL;\n}\n\n/* \u65b0\u589e\u908a */\nvoid addEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n AdjListNode *head1 = findNode(graph, vet1);\n AdjListNode *head2 = findNode(graph, vet2);\n assert(head1 != NULL &amp;&amp; head2 != NULL &amp;&amp; head1 != head2);\n // \u65b0\u589e\u908a vet1 - vet2\n addEdgeHelper(head1, vet2);\n addEdgeHelper(head2, vet1);\n}\n\n/* \u522a\u9664\u908a */\nvoid removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n AdjListNode *head1 = findNode(graph, vet1);\n AdjListNode *head2 = findNode(graph, vet2);\n assert(head1 != NULL &amp;&amp; head2 != NULL);\n // \u522a\u9664\u908a vet1 - vet2\n removeEdgeHelper(head1, head2-&gt;vertex);\n removeEdgeHelper(head2, head1-&gt;vertex);\n}\n\n/* \u65b0\u589e\u9802\u9ede */\nvoid addVertex(GraphAdjList *graph, Vertex *vet) {\n assert(graph != NULL &amp;&amp; graph-&gt;size &lt; MAX_SIZE);\n AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode));\n head-&gt;vertex = vet;\n head-&gt;next = NULL;\n // \u5728\u9130\u63a5\u8868\u4e2d\u65b0\u589e\u4e00\u500b\u65b0\u93c8\u7d50\u4e32\u5217\n graph-&gt;heads[graph-&gt;size++] = head;\n}\n\n/* \u522a\u9664\u9802\u9ede */\nvoid removeVertex(GraphAdjList *graph, Vertex *vet) {\n AdjListNode *node = findNode(graph, vet);\n assert(node != NULL);\n // \u5728\u9130\u63a5\u8868\u4e2d\u522a\u9664\u9802\u9ede vet \u5c0d\u61c9\u7684\u93c8\u7d50\u4e32\u5217\n AdjListNode *cur = node, *pre = NULL;\n while (cur) {\n pre = cur;\n cur = cur-&gt;next;\n free(pre);\n }\n // \u8d70\u8a2a\u5176\u4ed6\u9802\u9ede\u7684\u93c8\u7d50\u4e32\u5217\uff0c\u522a\u9664\u6240\u6709\u5305\u542b vet \u7684\u908a\n for (int i = 0; i &lt; graph-&gt;size; i++) {\n cur = graph-&gt;heads[i];\n pre = NULL;\n while (cur) {\n pre = cur;\n cur = cur-&gt;next;\n if (cur &amp;&amp; cur-&gt;vertex == vet) {\n pre-&gt;next = cur-&gt;next;\n free(cur);\n break;\n }\n }\n }\n // \u5c07\u8a72\u9802\u9ede\u4e4b\u5f8c\u7684\u9802\u9ede\u5411\u524d\u79fb\u52d5\uff0c\u4ee5\u586b\u88dc\u7a7a\u7f3a\n int i;\n for (i = 0; i &lt; graph-&gt;size; i++) {\n if (graph-&gt;heads[i] == node)\n break;\n }\n for (int j = i; j &lt; graph-&gt;size - 1; j++) {\n graph-&gt;heads[j] = graph-&gt;heads[j + 1];\n }\n graph-&gt;size--;\n free(vet);\n}\n</code></pre> graph_adjacency_list.kt<pre><code>/* \u57fa\u65bc\u9130\u63a5\u8868\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 */\nclass GraphAdjList(edges: Array&lt;Array&lt;Vertex?&gt;&gt;) {\n // \u9130\u63a5\u8868\uff0ckey\uff1a\u9802\u9ede\uff0cvalue\uff1a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n val adjList = HashMap&lt;Vertex, MutableList&lt;Vertex&gt;&gt;()\n\n /* \u5efa\u69cb\u5b50 */\n init {\n // \u65b0\u589e\u6240\u6709\u9802\u9ede\u548c\u908a\n for (edge in edges) {\n addVertex(edge[0]!!)\n addVertex(edge[1]!!)\n addEdge(edge[0]!!, edge[1]!!)\n }\n }\n\n /* \u7372\u53d6\u9802\u9ede\u6578\u91cf */\n fun size(): Int {\n return adjList.size\n }\n\n /* \u65b0\u589e\u908a */\n fun addEdge(vet1: Vertex, vet2: Vertex) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw IllegalArgumentException()\n // \u65b0\u589e\u908a vet1 - vet2\n adjList[vet1]?.add(vet2)\n adjList[vet2]?.add(vet1)\n }\n\n /* \u522a\u9664\u908a */\n fun removeEdge(vet1: Vertex, vet2: Vertex) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw IllegalArgumentException()\n // \u522a\u9664\u908a vet1 - vet2\n adjList[vet1]?.remove(vet2)\n adjList[vet2]?.remove(vet1)\n }\n\n /* \u65b0\u589e\u9802\u9ede */\n fun addVertex(vet: Vertex) {\n if (adjList.containsKey(vet))\n return\n // \u5728\u9130\u63a5\u8868\u4e2d\u65b0\u589e\u4e00\u500b\u65b0\u93c8\u7d50\u4e32\u5217\n adjList[vet] = mutableListOf()\n }\n\n /* \u522a\u9664\u9802\u9ede */\n fun removeVertex(vet: Vertex) {\n if (!adjList.containsKey(vet))\n throw IllegalArgumentException()\n // \u5728\u9130\u63a5\u8868\u4e2d\u522a\u9664\u9802\u9ede vet \u5c0d\u61c9\u7684\u93c8\u7d50\u4e32\u5217\n adjList.remove(vet)\n // \u8d70\u8a2a\u5176\u4ed6\u9802\u9ede\u7684\u93c8\u7d50\u4e32\u5217\uff0c\u522a\u9664\u6240\u6709\u5305\u542b vet \u7684\u908a\n for (list in adjList.values) {\n list.remove(vet)\n }\n }\n\n /* \u5217\u5370\u9130\u63a5\u8868 */\n fun print() {\n println(\"\u9130\u63a5\u8868 =\")\n for (pair in adjList.entries) {\n val tmp = mutableListOf&lt;Int&gt;()\n for (vertex in pair.value) {\n tmp.add(vertex._val)\n }\n println(\"${pair.key._val}: $tmp,\")\n }\n }\n}\n</code></pre> graph_adjacency_list.rb<pre><code>### \u57fa\u65bc\u9130\u63a5\u8868\u5be6\u73fe\u7684\u7121\u5411\u5716\u985e\u5225 ###\nclass GraphAdjList\n attr_reader :adj_list\n\n ### \u5efa\u69cb\u5b50 ###\n def initialize(edges)\n # \u9130\u63a5\u8868\uff0ckey\uff1a\u9802\u9ede\uff0cvalue\uff1a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n @adj_list = {}\n # \u65b0\u589e\u6240\u6709\u9802\u9ede\u548c\u908a\n for edge in edges\n add_vertex(edge[0])\n add_vertex(edge[1])\n add_edge(edge[0], edge[1])\n end\n end\n\n ### \u7372\u53d6\u9802\u9ede\u6578\u91cf ###\n def size\n @adj_list.length\n end\n\n ### \u65b0\u589e\u908a ###\n def add_edge(vet1, vet2)\n raise ArgumentError if !@adj_list.include?(vet1) || !@adj_list.include?(vet2)\n\n @adj_list[vet1] &lt;&lt; vet2\n @adj_list[vet2] &lt;&lt; vet1\n end\n\n ### \u522a\u9664\u908a ###\n def remove_edge(vet1, vet2)\n raise ArgumentError if !@adj_list.include?(vet1) || !@adj_list.include?(vet2)\n\n # \u522a\u9664\u908a vet1 - vet2\n @adj_list[vet1].delete(vet2)\n @adj_list[vet2].delete(vet1)\n end\n\n ### \u65b0\u589e\u9802\u9ede ###\n def add_vertex(vet)\n return if @adj_list.include?(vet)\n\n # \u5728\u9130\u63a5\u8868\u4e2d\u65b0\u589e\u4e00\u500b\u65b0\u93c8\u7d50\u4e32\u5217\n @adj_list[vet] = []\n end\n\n ### \u522a\u9664\u9802\u9ede ###\n def remove_vertex(vet)\n raise ArgumentError unless @adj_list.include?(vet)\n\n # \u5728\u9130\u63a5\u8868\u4e2d\u522a\u9664\u9802\u9ede vet \u5c0d\u61c9\u7684\u93c8\u7d50\u4e32\u5217\n @adj_list.delete(vet)\n # \u8d70\u8a2a\u5176\u4ed6\u9802\u9ede\u7684\u93c8\u7d50\u4e32\u5217\uff0c\u522a\u9664\u6240\u6709\u5305\u542b vet \u7684\u908a\n for vertex in @adj_list\n @adj_list[vertex.first].delete(vet) if @adj_list[vertex.first].include?(vet)\n end\n end\n\n ### \u5217\u5370\u9130\u63a5\u8868 ###\n def __print__\n puts '\u9130\u63a5\u8868 ='\n for vertex in @adj_list\n tmp = @adj_list[vertex.first].map { |v| v.val }\n puts \"#{vertex.first.val}: #{tmp},\"\n end\n end\nend\n</code></pre> graph_adjacency_list.zig<pre><code>[class]{GraphAdjList}-[func]{}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_graph/graph_operations/#923","title":"9.2.3 \u00a0 \u6548\u7387\u5c0d\u6bd4","text":"<p>\u8a2d\u5716\u4e2d\u5171\u6709 \\(n\\) \u500b\u9802\u9ede\u548c \\(m\\) \u689d\u908a\uff0c\u8868 9-2 \u5c0d\u6bd4\u4e86\u9130\u63a5\u77e9\u9663\u548c\u9130\u63a5\u8868\u7684\u6642\u9593\u6548\u7387\u548c\u7a7a\u9593\u6548\u7387\u3002</p> <p> \u8868 9-2 \u00a0 \u9130\u63a5\u77e9\u9663\u8207\u9130\u63a5\u8868\u5c0d\u6bd4 </p> \u9130\u63a5\u77e9\u9663 \u9130\u63a5\u8868\uff08\u93c8\u7d50\u4e32\u5217\uff09 \u9130\u63a5\u8868\uff08\u96dc\u6e4a\u8868\uff09 \u5224\u65b7\u662f\u5426\u9130\u63a5 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u65b0\u589e\u908a \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u522a\u9664\u908a \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u65b0\u589e\u9802\u9ede \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) \u522a\u9664\u9802\u9ede \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) \u8a18\u61b6\u9ad4\u7a7a\u9593\u4f54\u7528 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\) <p>\u89c0\u5bdf\u8868 9-2 \uff0c\u4f3c\u4e4e\u9130\u63a5\u8868\uff08\u96dc\u6e4a\u8868\uff09\u7684\u6642\u9593\u6548\u7387\u8207\u7a7a\u9593\u6548\u7387\u6700\u512a\u3002\u4f46\u5be6\u969b\u4e0a\uff0c\u5728\u9130\u63a5\u77e9\u9663\u4e2d\u64cd\u4f5c\u908a\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u53ea\u9700\u4e00\u6b21\u9663\u5217\u8a2a\u554f\u6216\u8ce6\u503c\u64cd\u4f5c\u5373\u53ef\u3002\u7d9c\u5408\u4f86\u770b\uff0c\u9130\u63a5\u77e9\u9663\u9ad4\u73fe\u4e86\u201c\u4ee5\u7a7a\u9593\u63db\u6642\u9593\u201d\u7684\u539f\u5247\uff0c\u800c\u9130\u63a5\u8868\u9ad4\u73fe\u4e86\u201c\u4ee5\u6642\u9593\u63db\u7a7a\u9593\u201d\u7684\u539f\u5247\u3002</p>"},{"location":"chapter_graph/graph_traversal/","title":"9.3 \u00a0 \u5716\u7684\u8d70\u8a2a","text":"<p>\u6a39\u4ee3\u8868\u7684\u662f\u201c\u4e00\u5c0d\u591a\u201d\u7684\u95dc\u4fc2\uff0c\u800c\u5716\u5247\u5177\u6709\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\uff0c\u53ef\u4ee5\u8868\u793a\u4efb\u610f\u7684\u201c\u591a\u5c0d\u591a\u201d\u95dc\u4fc2\u3002\u56e0\u6b64\uff0c\u6211\u5011\u53ef\u4ee5\u628a\u6a39\u770b\u4f5c\u5716\u7684\u4e00\u7a2e\u7279\u4f8b\u3002\u986f\u7136\uff0c\u6a39\u7684\u8d70\u8a2a\u64cd\u4f5c\u4e5f\u662f\u5716\u7684\u8d70\u8a2a\u64cd\u4f5c\u7684\u4e00\u7a2e\u7279\u4f8b\u3002</p> <p>\u5716\u548c\u6a39\u90fd\u9700\u8981\u61c9\u7528\u641c\u7d22\u6f14\u7b97\u6cd5\u4f86\u5be6\u73fe\u8d70\u8a2a\u64cd\u4f5c\u3002\u5716\u7684\u8d70\u8a2a\u65b9\u5f0f\u4e5f\u53ef\u5206\u70ba\u5169\u7a2e\uff1a\u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a\u548c\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u3002</p>"},{"location":"chapter_graph/graph_traversal/#931","title":"9.3.1 \u00a0 \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a","text":"<p>\u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a\u662f\u4e00\u7a2e\u7531\u8fd1\u53ca\u9060\u7684\u8d70\u8a2a\u65b9\u5f0f\uff0c\u5f9e\u67d0\u500b\u7bc0\u9ede\u51fa\u767c\uff0c\u59cb\u7d42\u512a\u5148\u8a2a\u554f\u8ddd\u96e2\u6700\u8fd1\u7684\u9802\u9ede\uff0c\u4e26\u4e00\u5c64\u5c64\u5411\u5916\u64f4\u5f35\u3002\u5982\u5716 9-9 \u6240\u793a\uff0c\u5f9e\u5de6\u4e0a\u89d2\u9802\u9ede\u51fa\u767c\uff0c\u9996\u5148\u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\uff0c\u7136\u5f8c\u8d70\u8a2a\u4e0b\u4e00\u500b\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\uff0c\u4ee5\u6b64\u985e\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9802\u9ede\u8a2a\u554f\u5b8c\u7562\u3002</p> <p></p> <p> \u5716 9-9 \u00a0 \u5716\u7684\u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a </p>"},{"location":"chapter_graph/graph_traversal/#1","title":"1. \u00a0 \u6f14\u7b97\u6cd5\u5be6\u73fe","text":"<p>BFS \u901a\u5e38\u85c9\u52a9\u4f47\u5217\u4f86\u5be6\u73fe\uff0c\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\u3002\u4f47\u5217\u5177\u6709\u201c\u5148\u5165\u5148\u51fa\u201d\u7684\u6027\u8cea\uff0c\u9019\u8207 BFS \u7684\u201c\u7531\u8fd1\u53ca\u9060\u201d\u7684\u601d\u60f3\u7570\u66f2\u540c\u5de5\u3002</p> <ol> <li>\u5c07\u8d70\u8a2a\u8d77\u59cb\u9802\u9ede <code>startVet</code> \u52a0\u5165\u5217\u5217\uff0c\u4e26\u958b\u555f\u8ff4\u5708\u3002</li> <li>\u5728\u8ff4\u5708\u7684\u6bcf\u8f2a\u8fed\u4ee3\u4e2d\uff0c\u5f48\u51fa\u4f47\u5217\u9996\u9802\u9ede\u4e26\u8a18\u9304\u8a2a\u554f\uff0c\u7136\u5f8c\u5c07\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\u52a0\u5165\u5230\u4f47\u5217\u5c3e\u90e8\u3002</li> <li>\u8ff4\u5708\u6b65\u9a5f <code>2.</code> \uff0c\u76f4\u5230\u6240\u6709\u9802\u9ede\u88ab\u8a2a\u554f\u5b8c\u7562\u5f8c\u7d50\u675f\u3002</li> </ol> <p>\u70ba\u4e86\u9632\u6b62\u91cd\u8907\u8d70\u8a2a\u9802\u9ede\uff0c\u6211\u5011\u9700\u8981\u85c9\u52a9\u4e00\u500b\u96dc\u6e4a\u96c6\u5408 <code>visited</code> \u4f86\u8a18\u9304\u54ea\u4e9b\u7bc0\u9ede\u5df2\u88ab\u8a2a\u554f\u3002</p> <p>Tip</p> <p>\u96dc\u6e4a\u96c6\u5408\u53ef\u4ee5\u770b\u4f5c\u4e00\u500b\u53ea\u5132\u5b58 <code>key</code> \u800c\u4e0d\u5132\u5b58 <code>value</code> \u7684\u96dc\u6e4a\u8868\uff0c\u5b83\u53ef\u4ee5\u5728 \\(O(1)\\) \u6642\u9593\u8907\u96dc\u5ea6\u4e0b\u9032\u884c <code>key</code> \u7684\u589e\u522a\u67e5\u6539\u64cd\u4f5c\u3002\u6839\u64da <code>key</code> \u7684\u552f\u4e00\u6027\uff0c\u96dc\u6e4a\u96c6\u5408\u901a\u5e38\u7528\u65bc\u8cc7\u6599\u53bb\u91cd\u7b49\u5834\u666f\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig graph_bfs.py<pre><code>def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -&gt; list[Vertex]:\n \"\"\"\u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a\"\"\"\n # \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n # \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n res = []\n # \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n visited = set[Vertex]([start_vet])\n # \u4f47\u5217\u7528\u65bc\u5be6\u73fe BFS\n que = deque[Vertex]([start_vet])\n # \u4ee5\u9802\u9ede vet \u70ba\u8d77\u9ede\uff0c\u8ff4\u5708\u76f4\u81f3\u8a2a\u554f\u5b8c\u6240\u6709\u9802\u9ede\n while len(que) &gt; 0:\n vet = que.popleft() # \u4f47\u5217\u9996\u9802\u9ede\u51fa\u968a\n res.append(vet) # \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n # \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for adj_vet in graph.adj_list[vet]:\n if adj_vet in visited:\n continue # \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n que.append(adj_vet) # \u53ea\u5165\u5217\u672a\u8a2a\u554f\u7684\u9802\u9ede\n visited.add(adj_vet) # \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n # \u8fd4\u56de\u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n return res\n</code></pre> graph_bfs.cpp<pre><code>/* \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nvector&lt;Vertex *&gt; graphBFS(GraphAdjList &amp;graph, Vertex *startVet) {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n vector&lt;Vertex *&gt; res;\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n unordered_set&lt;Vertex *&gt; visited = {startVet};\n // \u4f47\u5217\u7528\u65bc\u5be6\u73fe BFS\n queue&lt;Vertex *&gt; que;\n que.push(startVet);\n // \u4ee5\u9802\u9ede vet \u70ba\u8d77\u9ede\uff0c\u8ff4\u5708\u76f4\u81f3\u8a2a\u554f\u5b8c\u6240\u6709\u9802\u9ede\n while (!que.empty()) {\n Vertex *vet = que.front();\n que.pop(); // \u4f47\u5217\u9996\u9802\u9ede\u51fa\u968a\n res.push_back(vet); // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for (auto adjVet : graph.adjList[vet]) {\n if (visited.count(adjVet))\n continue; // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n que.push(adjVet); // \u53ea\u5165\u5217\u672a\u8a2a\u554f\u7684\u9802\u9ede\n visited.emplace(adjVet); // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n }\n }\n // \u8fd4\u56de\u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n return res;\n}\n</code></pre> graph_bfs.java<pre><code>/* \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nList&lt;Vertex&gt; graphBFS(GraphAdjList graph, Vertex startVet) {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n List&lt;Vertex&gt; res = new ArrayList&lt;&gt;();\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n Set&lt;Vertex&gt; visited = new HashSet&lt;&gt;();\n visited.add(startVet);\n // \u4f47\u5217\u7528\u65bc\u5be6\u73fe BFS\n Queue&lt;Vertex&gt; que = new LinkedList&lt;&gt;();\n que.offer(startVet);\n // \u4ee5\u9802\u9ede vet \u70ba\u8d77\u9ede\uff0c\u8ff4\u5708\u76f4\u81f3\u8a2a\u554f\u5b8c\u6240\u6709\u9802\u9ede\n while (!que.isEmpty()) {\n Vertex vet = que.poll(); // \u4f47\u5217\u9996\u9802\u9ede\u51fa\u968a\n res.add(vet); // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for (Vertex adjVet : graph.adjList.get(vet)) {\n if (visited.contains(adjVet))\n continue; // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n que.offer(adjVet); // \u53ea\u5165\u5217\u672a\u8a2a\u554f\u7684\u9802\u9ede\n visited.add(adjVet); // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n }\n }\n // \u8fd4\u56de\u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n return res;\n}\n</code></pre> graph_bfs.cs<pre><code>/* \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nList&lt;Vertex&gt; GraphBFS(GraphAdjList graph, Vertex startVet) {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n List&lt;Vertex&gt; res = [];\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n HashSet&lt;Vertex&gt; visited = [startVet];\n // \u4f47\u5217\u7528\u65bc\u5be6\u73fe BFS\n Queue&lt;Vertex&gt; que = new();\n que.Enqueue(startVet);\n // \u4ee5\u9802\u9ede vet \u70ba\u8d77\u9ede\uff0c\u8ff4\u5708\u76f4\u81f3\u8a2a\u554f\u5b8c\u6240\u6709\u9802\u9ede\n while (que.Count &gt; 0) {\n Vertex vet = que.Dequeue(); // \u4f47\u5217\u9996\u9802\u9ede\u51fa\u968a\n res.Add(vet); // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n foreach (Vertex adjVet in graph.adjList[vet]) {\n if (visited.Contains(adjVet)) {\n continue; // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n }\n que.Enqueue(adjVet); // \u53ea\u5165\u5217\u672a\u8a2a\u554f\u7684\u9802\u9ede\n visited.Add(adjVet); // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n }\n }\n\n // \u8fd4\u56de\u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n return res;\n}\n</code></pre> graph_bfs.go<pre><code>/* \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nfunc graphBFS(g *graphAdjList, startVet Vertex) []Vertex {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n res := make([]Vertex, 0)\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n visited := make(map[Vertex]struct{})\n visited[startVet] = struct{}{}\n // \u4f47\u5217\u7528\u65bc\u5be6\u73fe BFS, \u4f7f\u7528\u5207\u7247\u6a21\u64ec\u4f47\u5217\n queue := make([]Vertex, 0)\n queue = append(queue, startVet)\n // \u4ee5\u9802\u9ede vet \u70ba\u8d77\u9ede\uff0c\u8ff4\u5708\u76f4\u81f3\u8a2a\u554f\u5b8c\u6240\u6709\u9802\u9ede\n for len(queue) &gt; 0 {\n // \u4f47\u5217\u9996\u9802\u9ede\u51fa\u968a\n vet := queue[0]\n queue = queue[1:]\n // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n res = append(res, vet)\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for _, adjVet := range g.adjList[vet] {\n _, isExist := visited[adjVet]\n // \u53ea\u5165\u5217\u672a\u8a2a\u554f\u7684\u9802\u9ede\n if !isExist {\n queue = append(queue, adjVet)\n visited[adjVet] = struct{}{}\n }\n }\n }\n // \u8fd4\u56de\u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n return res\n}\n</code></pre> graph_bfs.swift<pre><code>/* \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nfunc graphBFS(graph: GraphAdjList, startVet: Vertex) -&gt; [Vertex] {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n var res: [Vertex] = []\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n var visited: Set&lt;Vertex&gt; = [startVet]\n // \u4f47\u5217\u7528\u65bc\u5be6\u73fe BFS\n var que: [Vertex] = [startVet]\n // \u4ee5\u9802\u9ede vet \u70ba\u8d77\u9ede\uff0c\u8ff4\u5708\u76f4\u81f3\u8a2a\u554f\u5b8c\u6240\u6709\u9802\u9ede\n while !que.isEmpty {\n let vet = que.removeFirst() // \u4f47\u5217\u9996\u9802\u9ede\u51fa\u968a\n res.append(vet) // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for adjVet in graph.adjList[vet] ?? [] {\n if visited.contains(adjVet) {\n continue // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n }\n que.append(adjVet) // \u53ea\u5165\u5217\u672a\u8a2a\u554f\u7684\u9802\u9ede\n visited.insert(adjVet) // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n }\n }\n // \u8fd4\u56de\u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n return res\n}\n</code></pre> graph_bfs.js<pre><code>/* \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nfunction graphBFS(graph, startVet) {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n const res = [];\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n const visited = new Set();\n visited.add(startVet);\n // \u4f47\u5217\u7528\u65bc\u5be6\u73fe BFS\n const que = [startVet];\n // \u4ee5\u9802\u9ede vet \u70ba\u8d77\u9ede\uff0c\u8ff4\u5708\u76f4\u81f3\u8a2a\u554f\u5b8c\u6240\u6709\u9802\u9ede\n while (que.length) {\n const vet = que.shift(); // \u4f47\u5217\u9996\u9802\u9ede\u51fa\u968a\n res.push(vet); // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for (const adjVet of graph.adjList.get(vet) ?? []) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n }\n que.push(adjVet); // \u53ea\u5165\u5217\u672a\u8a2a\u554f\u7684\u9802\u9ede\n visited.add(adjVet); // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n }\n }\n // \u8fd4\u56de\u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n return res;\n}\n</code></pre> graph_bfs.ts<pre><code>/* \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nfunction graphBFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n const res: Vertex[] = [];\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n const visited: Set&lt;Vertex&gt; = new Set();\n visited.add(startVet);\n // \u4f47\u5217\u7528\u65bc\u5be6\u73fe BFS\n const que = [startVet];\n // \u4ee5\u9802\u9ede vet \u70ba\u8d77\u9ede\uff0c\u8ff4\u5708\u76f4\u81f3\u8a2a\u554f\u5b8c\u6240\u6709\u9802\u9ede\n while (que.length) {\n const vet = que.shift(); // \u4f47\u5217\u9996\u9802\u9ede\u51fa\u968a\n res.push(vet); // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for (const adjVet of graph.adjList.get(vet) ?? []) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n }\n que.push(adjVet); // \u53ea\u5165\u5217\u672a\u8a2a\u554f\n visited.add(adjVet); // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n }\n }\n // \u8fd4\u56de\u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n return res;\n}\n</code></pre> graph_bfs.dart<pre><code>/* \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a */\nList&lt;Vertex&gt; graphBFS(GraphAdjList graph, Vertex startVet) {\n // \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n List&lt;Vertex&gt; res = [];\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n Set&lt;Vertex&gt; visited = {};\n visited.add(startVet);\n // \u4f47\u5217\u7528\u65bc\u5be6\u73fe BFS\n Queue&lt;Vertex&gt; que = Queue();\n que.add(startVet);\n // \u4ee5\u9802\u9ede vet \u70ba\u8d77\u9ede\uff0c\u8ff4\u5708\u76f4\u81f3\u8a2a\u554f\u5b8c\u6240\u6709\u9802\u9ede\n while (que.isNotEmpty) {\n Vertex vet = que.removeFirst(); // \u4f47\u5217\u9996\u9802\u9ede\u51fa\u968a\n res.add(vet); // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for (Vertex adjVet in graph.adjList[vet]!) {\n if (visited.contains(adjVet)) {\n continue; // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n }\n que.add(adjVet); // \u53ea\u5165\u5217\u672a\u8a2a\u554f\u7684\u9802\u9ede\n visited.add(adjVet); // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n }\n }\n // \u8fd4\u56de\u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n return res;\n}\n</code></pre> graph_bfs.rs<pre><code>/* \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nfn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -&gt; Vec&lt;Vertex&gt; {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n let mut res = vec![];\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n let mut visited = HashSet::new();\n visited.insert(start_vet);\n // \u4f47\u5217\u7528\u65bc\u5be6\u73fe BFS\n let mut que = VecDeque::new();\n que.push_back(start_vet);\n // \u4ee5\u9802\u9ede vet \u70ba\u8d77\u9ede\uff0c\u8ff4\u5708\u76f4\u81f3\u8a2a\u554f\u5b8c\u6240\u6709\u9802\u9ede\n while !que.is_empty() {\n let vet = que.pop_front().unwrap(); // \u4f47\u5217\u9996\u9802\u9ede\u51fa\u968a\n res.push(vet); // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n if let Some(adj_vets) = graph.adj_list.get(&amp;vet) {\n for &amp;adj_vet in adj_vets {\n if visited.contains(&amp;adj_vet) {\n continue; // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n }\n que.push_back(adj_vet); // \u53ea\u5165\u5217\u672a\u8a2a\u554f\u7684\u9802\u9ede\n visited.insert(adj_vet); // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n }\n }\n }\n // \u8fd4\u56de\u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n res\n}\n</code></pre> graph_bfs.c<pre><code>/* \u7bc0\u9ede\u4f47\u5217\u7d50\u69cb\u9ad4 */\ntypedef struct {\n Vertex *vertices[MAX_SIZE];\n int front, rear, size;\n} Queue;\n\n/* \u5efa\u69cb\u5b50 */\nQueue *newQueue() {\n Queue *q = (Queue *)malloc(sizeof(Queue));\n q-&gt;front = q-&gt;rear = q-&gt;size = 0;\n return q;\n}\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nint isEmpty(Queue *q) {\n return q-&gt;size == 0;\n}\n\n/* \u5165\u5217\u64cd\u4f5c */\nvoid enqueue(Queue *q, Vertex *vet) {\n q-&gt;vertices[q-&gt;rear] = vet;\n q-&gt;rear = (q-&gt;rear + 1) % MAX_SIZE;\n q-&gt;size++;\n}\n\n/* \u51fa\u5217\u64cd\u4f5c */\nVertex *dequeue(Queue *q) {\n Vertex *vet = q-&gt;vertices[q-&gt;front];\n q-&gt;front = (q-&gt;front + 1) % MAX_SIZE;\n q-&gt;size--;\n return vet;\n}\n\n/* \u6aa2\u67e5\u9802\u9ede\u662f\u5426\u5df2\u88ab\u8a2a\u554f */\nint isVisited(Vertex **visited, int size, Vertex *vet) {\n // \u8d70\u8a2a\u67e5\u8a62\u7bc0\u9ede\uff0c\u4f7f\u7528 O(n) \u6642\u9593\n for (int i = 0; i &lt; size; i++) {\n if (visited[i] == vet)\n return 1;\n }\n return 0;\n}\n\n/* \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nvoid graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) {\n // \u4f47\u5217\u7528\u65bc\u5be6\u73fe BFS\n Queue *queue = newQueue();\n enqueue(queue, startVet);\n visited[(*visitedSize)++] = startVet;\n // \u4ee5\u9802\u9ede vet \u70ba\u8d77\u9ede\uff0c\u8ff4\u5708\u76f4\u81f3\u8a2a\u554f\u5b8c\u6240\u6709\u9802\u9ede\n while (!isEmpty(queue)) {\n Vertex *vet = dequeue(queue); // \u4f47\u5217\u9996\u9802\u9ede\u51fa\u968a\n res[(*resSize)++] = vet; // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n AdjListNode *node = findNode(graph, vet);\n while (node != NULL) {\n // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n if (!isVisited(visited, *visitedSize, node-&gt;vertex)) {\n enqueue(queue, node-&gt;vertex); // \u53ea\u5165\u5217\u672a\u8a2a\u554f\u7684\u9802\u9ede\n visited[(*visitedSize)++] = node-&gt;vertex; // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n }\n node = node-&gt;next;\n }\n }\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(queue);\n}\n</code></pre> graph_bfs.kt<pre><code>/* \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nfun graphBFS(graph: GraphAdjList, startVet: Vertex): MutableList&lt;Vertex?&gt; {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n val res = mutableListOf&lt;Vertex?&gt;()\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n val visited = HashSet&lt;Vertex&gt;()\n visited.add(startVet)\n // \u4f47\u5217\u7528\u65bc\u5be6\u73fe BFS\n val que = LinkedList&lt;Vertex&gt;()\n que.offer(startVet)\n // \u4ee5\u9802\u9ede vet \u70ba\u8d77\u9ede\uff0c\u8ff4\u5708\u76f4\u81f3\u8a2a\u554f\u5b8c\u6240\u6709\u9802\u9ede\n while (!que.isEmpty()) {\n val vet = que.poll() // \u4f47\u5217\u9996\u9802\u9ede\u51fa\u968a\n res.add(vet) // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for (adjVet in graph.adjList[vet]!!) {\n if (visited.contains(adjVet))\n continue // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n que.offer(adjVet) // \u53ea\u5165\u5217\u672a\u8a2a\u554f\u7684\u9802\u9ede\n visited.add(adjVet) // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n }\n }\n // \u8fd4\u56de\u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n return res\n}\n</code></pre> graph_bfs.rb<pre><code>### \u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a ###\ndef graph_bfs(graph, start_vet)\n # \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n # \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n res = []\n # \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n visited = Set.new([start_vet])\n # \u4f47\u5217\u7528\u65bc\u5be6\u73fe BFS\n que = [start_vet]\n # \u4ee5\u9802\u9ede vet \u70ba\u8d77\u9ede\uff0c\u8ff4\u5708\u76f4\u81f3\u8a2a\u554f\u5b8c\u6240\u6709\u9802\u9ede\n while que.length &gt; 0\n vet = que.shift # \u4f47\u5217\u9996\u9802\u9ede\u51fa\u968a\n res &lt;&lt; vet # \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n # \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for adj_vet in graph.adj_list[vet]\n next if visited.include?(adj_vet) # \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n que &lt;&lt; adj_vet # \u53ea\u5165\u5217\u672a\u8a2a\u554f\u7684\u9802\u9ede\n visited.add(adj_vet) # \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n end\n end\n # \u8fd4\u56de\u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n res\nend\n</code></pre> graph_bfs.zig<pre><code>[class]{}-[func]{graphBFS}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u7a0b\u5f0f\u78bc\u76f8\u5c0d\u62bd\u8c61\uff0c\u5efa\u8b70\u5c0d\u7167\u5716 9-10 \u4f86\u52a0\u6df1\u7406\u89e3\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt;&lt;10&gt;&lt;11&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 9-10 \u00a0 \u5716\u7684\u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a\u6b65\u9a5f </p> <p>\u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f</p> <p>\u4e0d\u552f\u4e00\u3002\u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a\u53ea\u8981\u6c42\u6309\u201c\u7531\u8fd1\u53ca\u9060\u201d\u7684\u9806\u5e8f\u8d70\u8a2a\uff0c\u800c\u591a\u500b\u76f8\u540c\u8ddd\u96e2\u7684\u9802\u9ede\u7684\u8d70\u8a2a\u9806\u5e8f\u5141\u8a31\u88ab\u4efb\u610f\u6253\u4e82\u3002\u4ee5\u5716 9-10 \u70ba\u4f8b\uff0c\u9802\u9ede \\(1\\)\u3001\\(3\\) \u7684\u8a2a\u554f\u9806\u5e8f\u53ef\u4ee5\u4ea4\u63db\uff0c\u9802\u9ede \\(2\\)\u3001\\(4\\)\u3001\\(6\\) \u7684\u8a2a\u554f\u9806\u5e8f\u4e5f\u53ef\u4ee5\u4efb\u610f\u4ea4\u63db\u3002</p>"},{"location":"chapter_graph/graph_traversal/#2","title":"2. \u00a0 \u8907\u96dc\u5ea6\u5206\u6790","text":"<p>\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u6240\u6709\u9802\u9ede\u90fd\u6703\u5165\u5217\u4e26\u51fa\u968a\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u6642\u9593\uff1b\u5728\u8d70\u8a2a\u9130\u63a5\u9802\u9ede\u7684\u904e\u7a0b\u4e2d\uff0c\u7531\u65bc\u662f\u7121\u5411\u5716\uff0c\u56e0\u6b64\u6240\u6709\u908a\u90fd\u6703\u88ab\u8a2a\u554f \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u6642\u9593\uff1b\u7e3d\u9ad4\u4f7f\u7528 \\(O(|V| + |E|)\\) \u6642\u9593\u3002</p> <p>\u7a7a\u9593\u8907\u96dc\u5ea6\uff1a\u4e32\u5217 <code>res</code> \uff0c\u96dc\u6e4a\u96c6\u5408 <code>visited</code> \uff0c\u4f47\u5217 <code>que</code> \u4e2d\u7684\u9802\u9ede\u6578\u91cf\u6700\u591a\u70ba \\(|V|\\) \uff0c\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u9593\u3002</p>"},{"location":"chapter_graph/graph_traversal/#932","title":"9.3.2 \u00a0 \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a","text":"<p>\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u662f\u4e00\u7a2e\u512a\u5148\u8d70\u5230\u5e95\u3001\u7121\u8def\u53ef\u8d70\u518d\u56de\u982d\u7684\u8d70\u8a2a\u65b9\u5f0f\u3002\u5982\u5716 9-11 \u6240\u793a\uff0c\u5f9e\u5de6\u4e0a\u89d2\u9802\u9ede\u51fa\u767c\uff0c\u8a2a\u554f\u7576\u524d\u9802\u9ede\u7684\u67d0\u500b\u9130\u63a5\u9802\u9ede\uff0c\u76f4\u5230\u8d70\u5230\u76e1\u982d\u6642\u8fd4\u56de\uff0c\u518d\u7e7c\u7e8c\u8d70\u5230\u76e1\u982d\u4e26\u8fd4\u56de\uff0c\u4ee5\u6b64\u985e\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9802\u9ede\u8d70\u8a2a\u5b8c\u6210\u3002</p> <p></p> <p> \u5716 9-11 \u00a0 \u5716\u7684\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a </p>"},{"location":"chapter_graph/graph_traversal/#1_1","title":"1. \u00a0 \u6f14\u7b97\u6cd5\u5be6\u73fe","text":"<p>\u9019\u7a2e\u201c\u8d70\u5230\u76e1\u982d\u518d\u8fd4\u56de\u201d\u7684\u6f14\u7b97\u6cd5\u7bc4\u5f0f\u901a\u5e38\u57fa\u65bc\u905e\u8ff4\u4f86\u5be6\u73fe\u3002\u8207\u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a\u985e\u4f3c\uff0c\u5728\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u4e2d\uff0c\u6211\u5011\u4e5f\u9700\u8981\u85c9\u52a9\u4e00\u500b\u96dc\u6e4a\u96c6\u5408 <code>visited</code> \u4f86\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\uff0c\u4ee5\u907f\u514d\u91cd\u8907\u8a2a\u554f\u9802\u9ede\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig graph_dfs.py<pre><code>def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n \"\"\"\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u8f14\u52a9\u51fd\u5f0f\"\"\"\n res.append(vet) # \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n visited.add(vet) # \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n # \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for adjVet in graph.adj_list[vet]:\n if adjVet in visited:\n continue # \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n # \u905e\u8ff4\u8a2a\u554f\u9130\u63a5\u9802\u9ede\n dfs(graph, visited, res, adjVet)\n\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -&gt; list[Vertex]:\n \"\"\"\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\"\"\"\n # \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n # \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n res = []\n # \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n visited = set[Vertex]()\n dfs(graph, visited, res, start_vet)\n return res\n</code></pre> graph_dfs.cpp<pre><code>/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u8f14\u52a9\u51fd\u5f0f */\nvoid dfs(GraphAdjList &amp;graph, unordered_set&lt;Vertex *&gt; &amp;visited, vector&lt;Vertex *&gt; &amp;res, Vertex *vet) {\n res.push_back(vet); // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n visited.emplace(vet); // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for (Vertex *adjVet : graph.adjList[vet]) {\n if (visited.count(adjVet))\n continue; // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n // \u905e\u8ff4\u8a2a\u554f\u9130\u63a5\u9802\u9ede\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nvector&lt;Vertex *&gt; graphDFS(GraphAdjList &amp;graph, Vertex *startVet) {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n vector&lt;Vertex *&gt; res;\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n unordered_set&lt;Vertex *&gt; visited;\n dfs(graph, visited, res, startVet);\n return res;\n}\n</code></pre> graph_dfs.java<pre><code>/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u8f14\u52a9\u51fd\u5f0f */\nvoid dfs(GraphAdjList graph, Set&lt;Vertex&gt; visited, List&lt;Vertex&gt; res, Vertex vet) {\n res.add(vet); // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n visited.add(vet); // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for (Vertex adjVet : graph.adjList.get(vet)) {\n if (visited.contains(adjVet))\n continue; // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n // \u905e\u8ff4\u8a2a\u554f\u9130\u63a5\u9802\u9ede\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nList&lt;Vertex&gt; graphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n List&lt;Vertex&gt; res = new ArrayList&lt;&gt;();\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n Set&lt;Vertex&gt; visited = new HashSet&lt;&gt;();\n dfs(graph, visited, res, startVet);\n return res;\n}\n</code></pre> graph_dfs.cs<pre><code>/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u8f14\u52a9\u51fd\u5f0f */\nvoid DFS(GraphAdjList graph, HashSet&lt;Vertex&gt; visited, List&lt;Vertex&gt; res, Vertex vet) {\n res.Add(vet); // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n visited.Add(vet); // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n foreach (Vertex adjVet in graph.adjList[vet]) {\n if (visited.Contains(adjVet)) {\n continue; // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede \n }\n // \u905e\u8ff4\u8a2a\u554f\u9130\u63a5\u9802\u9ede\n DFS(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nList&lt;Vertex&gt; GraphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n List&lt;Vertex&gt; res = [];\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n HashSet&lt;Vertex&gt; visited = [];\n DFS(graph, visited, res, startVet);\n return res;\n}\n</code></pre> graph_dfs.go<pre><code>/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u8f14\u52a9\u51fd\u5f0f */\nfunc dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {\n // append \u64cd\u4f5c\u6703\u8fd4\u56de\u65b0\u7684\u7684\u5f15\u7528\uff0c\u5fc5\u9808\u8b93\u539f\u5f15\u7528\u91cd\u65b0\u8ce6\u503c\u70ba\u65b0slice\u7684\u5f15\u7528\n *res = append(*res, vet)\n visited[vet] = struct{}{}\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for _, adjVet := range g.adjList[vet] {\n _, isExist := visited[adjVet]\n // \u905e\u8ff4\u8a2a\u554f\u9130\u63a5\u9802\u9ede\n if !isExist {\n dfs(g, visited, res, adjVet)\n }\n }\n}\n\n/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nfunc graphDFS(g *graphAdjList, startVet Vertex) []Vertex {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n res := make([]Vertex, 0)\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n visited := make(map[Vertex]struct{})\n dfs(g, visited, &amp;res, startVet)\n // \u8fd4\u56de\u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n return res\n}\n</code></pre> graph_dfs.swift<pre><code>/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u8f14\u52a9\u51fd\u5f0f */\nfunc dfs(graph: GraphAdjList, visited: inout Set&lt;Vertex&gt;, res: inout [Vertex], vet: Vertex) {\n res.append(vet) // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n visited.insert(vet) // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for adjVet in graph.adjList[vet] ?? [] {\n if visited.contains(adjVet) {\n continue // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n }\n // \u905e\u8ff4\u8a2a\u554f\u9130\u63a5\u9802\u9ede\n dfs(graph: graph, visited: &amp;visited, res: &amp;res, vet: adjVet)\n }\n}\n\n/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nfunc graphDFS(graph: GraphAdjList, startVet: Vertex) -&gt; [Vertex] {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n var res: [Vertex] = []\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n var visited: Set&lt;Vertex&gt; = []\n dfs(graph: graph, visited: &amp;visited, res: &amp;res, vet: startVet)\n return res\n}\n</code></pre> graph_dfs.js<pre><code>/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nfunction dfs(graph, visited, res, vet) {\n res.push(vet); // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n visited.add(vet); // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for (const adjVet of graph.adjList.get(vet)) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n }\n // \u905e\u8ff4\u8a2a\u554f\u9130\u63a5\u9802\u9ede\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nfunction graphDFS(graph, startVet) {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n const res = [];\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n const visited = new Set();\n dfs(graph, visited, res, startVet);\n return res;\n}\n</code></pre> graph_dfs.ts<pre><code>/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u8f14\u52a9\u51fd\u5f0f */\nfunction dfs(\n graph: GraphAdjList,\n visited: Set&lt;Vertex&gt;,\n res: Vertex[],\n vet: Vertex\n): void {\n res.push(vet); // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n visited.add(vet); // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for (const adjVet of graph.adjList.get(vet)) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n }\n // \u905e\u8ff4\u8a2a\u554f\u9130\u63a5\u9802\u9ede\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nfunction graphDFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n const res: Vertex[] = [];\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n const visited: Set&lt;Vertex&gt; = new Set();\n dfs(graph, visited, res, startVet);\n return res;\n}\n</code></pre> graph_dfs.dart<pre><code>/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u8f14\u52a9\u51fd\u5f0f */\nvoid dfs(\n GraphAdjList graph,\n Set&lt;Vertex&gt; visited,\n List&lt;Vertex&gt; res,\n Vertex vet,\n) {\n res.add(vet); // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n visited.add(vet); // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for (Vertex adjVet in graph.adjList[vet]!) {\n if (visited.contains(adjVet)) {\n continue; // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n }\n // \u905e\u8ff4\u8a2a\u554f\u9130\u63a5\u9802\u9ede\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\nList&lt;Vertex&gt; graphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n List&lt;Vertex&gt; res = [];\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n Set&lt;Vertex&gt; visited = {};\n dfs(graph, visited, res, startVet);\n return res;\n}\n</code></pre> graph_dfs.rs<pre><code>/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u8f14\u52a9\u51fd\u5f0f */\nfn dfs(graph: &amp;GraphAdjList, visited: &amp;mut HashSet&lt;Vertex&gt;, res: &amp;mut Vec&lt;Vertex&gt;, vet: Vertex) {\n res.push(vet); // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n visited.insert(vet); // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n if let Some(adj_vets) = graph.adj_list.get(&amp;vet) {\n for &amp;adj_vet in adj_vets {\n if visited.contains(&amp;adj_vet) {\n continue; // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n }\n // \u905e\u8ff4\u8a2a\u554f\u9130\u63a5\u9802\u9ede\n dfs(graph, visited, res, adj_vet);\n }\n }\n}\n\n/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nfn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -&gt; Vec&lt;Vertex&gt; {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n let mut res = vec![];\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n let mut visited = HashSet::new();\n dfs(&amp;graph, &amp;mut visited, &amp;mut res, start_vet);\n\n res\n}\n</code></pre> graph_dfs.c<pre><code>/* \u6aa2\u67e5\u9802\u9ede\u662f\u5426\u5df2\u88ab\u8a2a\u554f */\nint isVisited(Vertex **res, int size, Vertex *vet) {\n // \u8d70\u8a2a\u67e5\u8a62\u7bc0\u9ede\uff0c\u4f7f\u7528 O(n) \u6642\u9593\n for (int i = 0; i &lt; size; i++) {\n if (res[i] == vet) {\n return 1;\n }\n }\n return 0;\n}\n\n/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u8f14\u52a9\u51fd\u5f0f */\nvoid dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {\n // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n res[(*resSize)++] = vet;\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n AdjListNode *node = findNode(graph, vet);\n while (node != NULL) {\n // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n if (!isVisited(res, *resSize, node-&gt;vertex)) {\n // \u905e\u8ff4\u8a2a\u554f\u9130\u63a5\u9802\u9ede\n dfs(graph, res, resSize, node-&gt;vertex);\n }\n node = node-&gt;next;\n }\n}\n\n/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nvoid graphDFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize) {\n dfs(graph, res, resSize, startVet);\n}\n</code></pre> graph_dfs.kt<pre><code>/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u8f14\u52a9\u51fd\u5f0f */\nfun dfs(\n graph: GraphAdjList,\n visited: MutableSet&lt;Vertex?&gt;,\n res: MutableList&lt;Vertex?&gt;,\n vet: Vertex?\n) {\n res.add(vet) // \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n visited.add(vet) // \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n // \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for (adjVet in graph.adjList[vet]!!) {\n if (visited.contains(adjVet))\n continue // \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n // \u905e\u8ff4\u8a2a\u554f\u9130\u63a5\u9802\u9ede\n dfs(graph, visited, res, adjVet)\n }\n}\n\n/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n// \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\nfun graphDFS(graph: GraphAdjList, startVet: Vertex?): MutableList&lt;Vertex?&gt; {\n // \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n val res = mutableListOf&lt;Vertex?&gt;()\n // \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n val visited = HashSet&lt;Vertex?&gt;()\n dfs(graph, visited, res, startVet)\n return res\n}\n</code></pre> graph_dfs.rb<pre><code>### \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u8f14\u52a9\u51fd\u5f0f ###\ndef dfs(graph, visited, res, vet)\n res &lt;&lt; vet # \u8a18\u9304\u8a2a\u554f\u9802\u9ede\n visited.add(vet) # \u6a19\u8a18\u8a72\u9802\u9ede\u5df2\u88ab\u8a2a\u554f\n # \u8d70\u8a2a\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n for adj_vet in graph.adj_list[vet]\n next if visited.include?(adj_vet) # \u8df3\u904e\u5df2\u88ab\u8a2a\u554f\u7684\u9802\u9ede\n # \u905e\u8ff4\u8a2a\u554f\u9130\u63a5\u9802\u9ede\n dfs(graph, visited, res, adj_vet)\n end\nend\n\n### \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a ###\ndef graph_dfs(graph, start_vet)\n # \u4f7f\u7528\u9130\u63a5\u8868\u4f86\u8868\u793a\u5716\uff0c\u4ee5\u4fbf\u7372\u53d6\u6307\u5b9a\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\n # \u9802\u9ede\u8d70\u8a2a\u5e8f\u5217\n res = []\n # \u96dc\u6e4a\u96c6\u5408\uff0c\u7528\u65bc\u8a18\u9304\u5df2\u88ab\u8a2a\u554f\u904e\u7684\u9802\u9ede\n visited = Set.new\n dfs(graph, visited, res, start_vet)\n res\nend\n</code></pre> graph_dfs.zig<pre><code>[class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u7684\u6f14\u7b97\u6cd5\u6d41\u7a0b\u5982\u5716 9-12 \u6240\u793a\u3002</p> <ul> <li>\u76f4\u865b\u7dda\u4ee3\u8868\u5411\u4e0b\u905e\u63a8\uff0c\u8868\u793a\u958b\u555f\u4e86\u4e00\u500b\u65b0\u7684\u905e\u8ff4\u65b9\u6cd5\u4f86\u8a2a\u554f\u65b0\u9802\u9ede\u3002</li> <li>\u66f2\u865b\u7dda\u4ee3\u8868\u5411\u4e0a\u56de\u6eaf\uff0c\u8868\u793a\u6b64\u905e\u8ff4\u65b9\u6cd5\u5df2\u7d93\u8fd4\u56de\uff0c\u56de\u6eaf\u5230\u4e86\u958b\u555f\u6b64\u65b9\u6cd5\u7684\u4f4d\u7f6e\u3002</li> </ul> <p>\u70ba\u4e86\u52a0\u6df1\u7406\u89e3\uff0c\u5efa\u8b70\u5c07\u5716 9-12 \u8207\u7a0b\u5f0f\u78bc\u7d50\u5408\u8d77\u4f86\uff0c\u5728\u8166\u4e2d\u6a21\u64ec\uff08\u6216\u8005\u7528\u7b46\u756b\u4e0b\u4f86\uff09\u6574\u500b DFS \u904e\u7a0b\uff0c\u5305\u62ec\u6bcf\u500b\u905e\u8ff4\u65b9\u6cd5\u4f55\u6642\u958b\u555f\u3001\u4f55\u6642\u8fd4\u56de\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt;&lt;10&gt;&lt;11&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 9-12 \u00a0 \u5716\u7684\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u6b65\u9a5f </p> <p>\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f</p> <p>\u8207\u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a\u985e\u4f3c\uff0c\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u5e8f\u5217\u7684\u9806\u5e8f\u4e5f\u4e0d\u662f\u552f\u4e00\u7684\u3002\u7d66\u5b9a\u67d0\u9802\u9ede\uff0c\u5148\u5f80\u54ea\u500b\u65b9\u5411\u63a2\u7d22\u90fd\u53ef\u4ee5\uff0c\u5373\u9130\u63a5\u9802\u9ede\u7684\u9806\u5e8f\u53ef\u4ee5\u4efb\u610f\u6253\u4e82\uff0c\u90fd\u662f\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u3002</p> <p>\u4ee5\u6a39\u7684\u8d70\u8a2a\u70ba\u4f8b\uff0c\u201c\u6839 \\(\\rightarrow\\) \u5de6 \\(\\rightarrow\\) \u53f3\u201d\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u201c\u5de6 \\(\\rightarrow\\) \u53f3 \\(\\rightarrow\\) \u6839\u201d\u5206\u5225\u5c0d\u61c9\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u5f8c\u5e8f\u8d70\u8a2a\uff0c\u5b83\u5011\u5c55\u793a\u4e86\u4e09\u7a2e\u8d70\u8a2a\u512a\u5148\u9806\u5e8f\uff0c\u7136\u800c\u9019\u4e09\u8005\u90fd\u5c6c\u65bc\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u3002</p>"},{"location":"chapter_graph/graph_traversal/#2_1","title":"2. \u00a0 \u8907\u96dc\u5ea6\u5206\u6790","text":"<p>\u6642\u9593\u8907\u96dc\u5ea6\uff1a\u6240\u6709\u9802\u9ede\u90fd\u6703\u88ab\u8a2a\u554f \\(1\\) \u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u6642\u9593\uff1b\u6240\u6709\u908a\u90fd\u6703\u88ab\u8a2a\u554f \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u6642\u9593\uff1b\u7e3d\u9ad4\u4f7f\u7528 \\(O(|V| + |E|)\\) \u6642\u9593\u3002</p> <p>\u7a7a\u9593\u8907\u96dc\u5ea6\uff1a\u4e32\u5217 <code>res</code> \uff0c\u96dc\u6e4a\u96c6\u5408 <code>visited</code> \u9802\u9ede\u6578\u91cf\u6700\u591a\u70ba \\(|V|\\) \uff0c\u905e\u8ff4\u6df1\u5ea6\u6700\u5927\u70ba \\(|V|\\) \uff0c\u56e0\u6b64\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u9593\u3002</p>"},{"location":"chapter_graph/summary/","title":"9.4 \u00a0 \u5c0f\u7d50","text":""},{"location":"chapter_graph/summary/#1","title":"1. \u00a0 \u91cd\u9ede\u56de\u9867","text":"<ul> <li>\u5716\u7531\u9802\u9ede\u548c\u908a\u7d44\u6210\uff0c\u53ef\u4ee5\u8868\u793a\u70ba\u4e00\u7d44\u9802\u9ede\u548c\u4e00\u7d44\u908a\u69cb\u6210\u7684\u96c6\u5408\u3002</li> <li>\u76f8\u8f03\u65bc\u7dda\u6027\u95dc\u4fc2\uff08\u93c8\u7d50\u4e32\u5217\uff09\u548c\u5206\u6cbb\u95dc\u4fc2\uff08\u6a39\uff09\uff0c\u7db2\u8def\u95dc\u4fc2\uff08\u5716\uff09\u5177\u6709\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\uff0c\u56e0\u800c\u66f4\u70ba\u8907\u96dc\u3002</li> <li>\u6709\u5411\u5716\u7684\u908a\u5177\u6709\u65b9\u5411\u6027\uff0c\u9023\u901a\u5716\u4e2d\u7684\u4efb\u610f\u9802\u9ede\u5747\u53ef\u9054\uff0c\u6709\u6b0a\u5716\u7684\u6bcf\u689d\u908a\u90fd\u5305\u542b\u6b0a\u91cd\u8b8a\u6578\u3002</li> <li>\u9130\u63a5\u77e9\u9663\u5229\u7528\u77e9\u9663\u4f86\u8868\u793a\u5716\uff0c\u6bcf\u4e00\u884c\uff08\u5217\uff09\u4ee3\u8868\u4e00\u500b\u9802\u9ede\uff0c\u77e9\u9663\u5143\u7d20\u4ee3\u8868\u908a\uff0c\u7528 \\(1\\) \u6216 \\(0\\) \u8868\u793a\u5169\u500b\u9802\u9ede\u4e4b\u9593\u6709\u908a\u6216\u7121\u908a\u3002\u9130\u63a5\u77e9\u9663\u5728\u589e\u522a\u67e5\u6539\u64cd\u4f5c\u4e0a\u6548\u7387\u5f88\u9ad8\uff0c\u4f46\u7a7a\u9593\u4f54\u7528\u8f03\u591a\u3002</li> <li>\u9130\u63a5\u8868\u4f7f\u7528\u591a\u500b\u93c8\u7d50\u4e32\u5217\u4f86\u8868\u793a\u5716\uff0c\u7b2c \\(i\\) \u500b\u93c8\u7d50\u4e32\u5217\u5c0d\u61c9\u9802\u9ede \\(i\\) \uff0c\u5176\u4e2d\u5132\u5b58\u4e86\u8a72\u9802\u9ede\u7684\u6240\u6709\u9130\u63a5\u9802\u9ede\u3002\u9130\u63a5\u8868\u76f8\u5c0d\u65bc\u9130\u63a5\u77e9\u9663\u66f4\u52a0\u7bc0\u7701\u7a7a\u9593\uff0c\u4f46\u7531\u65bc\u9700\u8981\u8d70\u8a2a\u93c8\u7d50\u4e32\u5217\u4f86\u67e5\u8a62\u908a\uff0c\u56e0\u6b64\u6642\u9593\u6548\u7387\u8f03\u4f4e\u3002</li> <li>\u7576\u9130\u63a5\u8868\u4e2d\u7684\u93c8\u7d50\u4e32\u5217\u904e\u9577\u6642\uff0c\u53ef\u4ee5\u5c07\u5176\u8f49\u63db\u70ba\u7d05\u9ed1\u6a39\u6216\u96dc\u6e4a\u8868\uff0c\u5f9e\u800c\u63d0\u5347\u67e5\u8a62\u6548\u7387\u3002</li> <li>\u5f9e\u6f14\u7b97\u6cd5\u601d\u60f3\u7684\u89d2\u5ea6\u5206\u6790\uff0c\u9130\u63a5\u77e9\u9663\u9ad4\u73fe\u4e86\u201c\u4ee5\u7a7a\u9593\u63db\u6642\u9593\u201d\uff0c\u9130\u63a5\u8868\u9ad4\u73fe\u4e86\u201c\u4ee5\u6642\u9593\u63db\u7a7a\u9593\u201d\u3002</li> <li>\u5716\u53ef\u7528\u65bc\u5efa\u6a21\u5404\u985e\u73fe\u5be6\u7cfb\u7d71\uff0c\u5982\u793e\u4ea4\u7db2\u8def\u3001\u5730\u9435\u7dda\u8def\u7b49\u3002</li> <li>\u6a39\u662f\u5716\u7684\u4e00\u7a2e\u7279\u4f8b\uff0c\u6a39\u7684\u8d70\u8a2a\u4e5f\u662f\u5716\u7684\u8d70\u8a2a\u7684\u4e00\u7a2e\u7279\u4f8b\u3002</li> <li>\u5716\u7684\u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a\u662f\u4e00\u7a2e\u7531\u8fd1\u53ca\u9060\u3001\u5c64\u5c64\u64f4\u5f35\u7684\u641c\u5c0b\u65b9\u5f0f\uff0c\u901a\u5e38\u85c9\u52a9\u4f47\u5217\u5be6\u73fe\u3002</li> <li>\u5716\u7684\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u662f\u4e00\u7a2e\u512a\u5148\u8d70\u5230\u5e95\u3001\u7121\u8def\u53ef\u8d70\u6642\u518d\u56de\u6eaf\u7684\u641c\u5c0b\u65b9\u5f0f\uff0c\u5e38\u57fa\u65bc\u905e\u8ff4\u4f86\u5be6\u73fe\u3002</li> </ul>"},{"location":"chapter_graph/summary/#2-q-a","title":"2. \u00a0 Q &amp; A","text":"<p>Q\uff1a\u8def\u5f91\u7684\u5b9a\u7fa9\u662f\u9802\u9ede\u5e8f\u5217\u9084\u662f\u908a\u5e8f\u5217\uff1f</p> <p>\u7dad\u57fa\u767e\u79d1\u4e0a\u4e0d\u540c\u8a9e\u8a00\u7248\u672c\u7684\u5b9a\u7fa9\u4e0d\u4e00\u81f4\uff1a\u82f1\u6587\u7248\u662f\u201c\u8def\u5f91\u662f\u4e00\u500b\u908a\u5e8f\u5217\u201d\uff0c\u800c\u4e2d\u6587\u7248\u662f\u201c\u8def\u5f91\u662f\u4e00\u500b\u9802\u9ede\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.</p> <p>\u5728\u672c\u6587\u4e2d\uff0c\u8def\u5f91\u88ab\u8996\u70ba\u4e00\u500b\u908a\u5e8f\u5217\uff0c\u800c\u4e0d\u662f\u4e00\u500b\u9802\u9ede\u5e8f\u5217\u3002\u9019\u662f\u56e0\u70ba\u5169\u500b\u9802\u9ede\u4e4b\u9593\u53ef\u80fd\u5b58\u5728\u591a\u689d\u908a\u9023\u7dda\uff0c\u6b64\u6642\u6bcf\u689d\u908a\u90fd\u5c0d\u61c9\u4e00\u689d\u8def\u5f91\u3002</p> <p>Q\uff1a\u975e\u9023\u901a\u5716\u4e2d\u662f\u5426\u6703\u6709\u7121\u6cd5\u8d70\u8a2a\u5230\u7684\u9ede\uff1f</p> <p>\u5728\u975e\u9023\u901a\u5716\u4e2d\uff0c\u5f9e\u67d0\u500b\u9802\u9ede\u51fa\u767c\uff0c\u81f3\u5c11\u6709\u4e00\u500b\u9802\u9ede\u7121\u6cd5\u5230\u9054\u3002\u8d70\u8a2a\u975e\u9023\u901a\u5716\u9700\u8981\u8a2d\u5b9a\u591a\u500b\u8d77\u9ede\uff0c\u4ee5\u8d70\u8a2a\u5230\u5716\u7684\u6240\u6709\u9023\u901a\u5206\u91cf\u3002</p> <p>Q\uff1a\u5728\u9130\u63a5\u8868\u4e2d\uff0c\u201c\u8207\u8a72\u9802\u9ede\u76f8\u9023\u7684\u6240\u6709\u9802\u9ede\u201d\u7684\u9802\u9ede\u9806\u5e8f\u662f\u5426\u6709\u8981\u6c42\uff1f</p> <p>\u53ef\u4ee5\u662f\u4efb\u610f\u9806\u5e8f\u3002\u4f46\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6309\u7167\u6307\u5b9a\u898f\u5247\u4f86\u6392\u5e8f\uff0c\u6bd4\u5982\u6309\u7167\u9802\u9ede\u65b0\u589e\u7684\u6b21\u5e8f\uff0c\u6216\u8005\u6309\u7167\u9802\u9ede\u503c\u5927\u5c0f\u7684\u9806\u5e8f\u7b49\uff0c\u9019\u6a23\u6709\u52a9\u65bc\u5feb\u901f\u67e5\u8a62\u201c\u5e36\u6709\u67d0\u7a2e\u6975\u503c\u201d\u7684\u9802\u9ede\u3002</p>"},{"location":"chapter_greedy/","title":"\u7b2c 15 \u7ae0 \u00a0 \u8caa\u5a6a","text":"<p>Abstract</p> <p>\u5411\u65e5\u8475\u671d\u8457\u592a\u967d\u8f49\u52d5\uff0c\u6642\u523b\u8ffd\u6c42\u81ea\u8eab\u6210\u9577\u7684\u6700\u5927\u53ef\u80fd\u3002</p> <p>\u8caa\u5a6a\u7b56\u7565\u5728\u4e00\u8f2a\u8f2a\u7684\u7c21\u55ae\u9078\u64c7\u4e2d\uff0c\u9010\u6b65\u5c0e\u5411\u6700\u4f73\u7b54\u6848\u3002</p>"},{"location":"chapter_greedy/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>15.1 \u00a0 \u8caa\u5a6a\u6f14\u7b97\u6cd5</li> <li>15.2 \u00a0 \u5206\u6578\u80cc\u5305\u554f\u984c</li> <li>15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u554f\u984c</li> <li>15.4 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u7a4d\u554f\u984c</li> <li>15.5 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_greedy/fractional_knapsack_problem/","title":"15.2 \u00a0 \u5206\u6578\u80cc\u5305\u554f\u984c","text":"<p>Question</p> <p>\u7d66\u5b9a \\(n\\) \u500b\u7269\u54c1\uff0c\u7b2c \\(i\\) \u500b\u7269\u54c1\u7684\u91cd\u91cf\u70ba \\(wgt[i-1]\\)\u3001\u50f9\u503c\u70ba \\(val[i-1]\\) \uff0c\u548c\u4e00\u500b\u5bb9\u91cf\u70ba \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u500b\u7269\u54c1\u53ea\u80fd\u9078\u64c7\u4e00\u6b21\uff0c\u4f46\u53ef\u4ee5\u9078\u64c7\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u50f9\u503c\u6839\u64da\u9078\u64c7\u7684\u91cd\u91cf\u6bd4\u4f8b\u8a08\u7b97\uff0c\u554f\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u80cc\u5305\u4e2d\u7269\u54c1\u7684\u6700\u5927\u50f9\u503c\u3002\u793a\u4f8b\u5982\u5716 15-3 \u6240\u793a\u3002</p> <p></p> <p> \u5716 15-3 \u00a0 \u5206\u6578\u80cc\u5305\u554f\u984c\u7684\u793a\u4f8b\u8cc7\u6599 </p> <p>\u5206\u6578\u80cc\u5305\u554f\u984c\u548c 0-1 \u80cc\u5305\u554f\u984c\u6574\u9ad4\u4e0a\u975e\u5e38\u76f8\u4f3c\uff0c\u72c0\u614b\u5305\u542b\u7576\u524d\u7269\u54c1 \\(i\\) \u548c\u5bb9\u91cf \\(c\\) \uff0c\u76ee\u6a19\u662f\u6c42\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u6700\u5927\u50f9\u503c\u3002</p> <p>\u4e0d\u540c\u9ede\u5728\u65bc\uff0c\u672c\u984c\u5141\u8a31\u53ea\u9078\u64c7\u7269\u54c1\u7684\u4e00\u90e8\u5206\u3002\u5982\u5716 15-4 \u6240\u793a\uff0c\u6211\u5011\u53ef\u4ee5\u5c0d\u7269\u54c1\u4efb\u610f\u5730\u9032\u884c\u5207\u5206\uff0c\u4e26\u6309\u7167\u91cd\u91cf\u6bd4\u4f8b\u4f86\u8a08\u7b97\u76f8\u61c9\u50f9\u503c\u3002</p> <ol> <li>\u5c0d\u65bc\u7269\u54c1 \\(i\\) \uff0c\u5b83\u5728\u55ae\u4f4d\u91cd\u91cf\u4e0b\u7684\u50f9\u503c\u70ba \\(val[i-1] / wgt[i-1]\\) \uff0c\u7c21\u7a31\u55ae\u4f4d\u50f9\u503c\u3002</li> <li>\u5047\u8a2d\u653e\u5165\u4e00\u90e8\u5206\u7269\u54c1 \\(i\\) \uff0c\u91cd\u91cf\u70ba \\(w\\) \uff0c\u5247\u80cc\u5305\u589e\u52a0\u7684\u50f9\u503c\u70ba \\(w \\times val[i-1] / wgt[i-1]\\) \u3002</li> </ol> <p></p> <p> \u5716 15-4 \u00a0 \u7269\u54c1\u5728\u55ae\u4f4d\u91cd\u91cf\u4e0b\u7684\u50f9\u503c </p>"},{"location":"chapter_greedy/fractional_knapsack_problem/#1","title":"1. \u00a0 \u8caa\u5a6a\u7b56\u7565\u78ba\u5b9a","text":"<p>\u6700\u5927\u5316\u80cc\u5305\u5167\u7269\u54c1\u7e3d\u50f9\u503c\uff0c\u672c\u8cea\u4e0a\u662f\u6700\u5927\u5316\u55ae\u4f4d\u91cd\u91cf\u4e0b\u7684\u7269\u54c1\u50f9\u503c\u3002\u7531\u6b64\u4fbf\u53ef\u63a8\u7406\u51fa\u5716 15-5 \u6240\u793a\u7684\u8caa\u5a6a\u7b56\u7565\u3002</p> <ol> <li>\u5c07\u7269\u54c1\u6309\u7167\u55ae\u4f4d\u50f9\u503c\u5f9e\u9ad8\u5230\u4f4e\u9032\u884c\u6392\u5e8f\u3002</li> <li>\u8d70\u8a2a\u6240\u6709\u7269\u54c1\uff0c\u6bcf\u8f2a\u8caa\u5a6a\u5730\u9078\u64c7\u55ae\u4f4d\u50f9\u503c\u6700\u9ad8\u7684\u7269\u54c1\u3002</li> <li>\u82e5\u5269\u9918\u80cc\u5305\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5247\u4f7f\u7528\u7576\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u586b\u6eff\u80cc\u5305\u3002</li> </ol> <p></p> <p> \u5716 15-5 \u00a0 \u5206\u6578\u80cc\u5305\u554f\u984c\u7684\u8caa\u5a6a\u7b56\u7565 </p>"},{"location":"chapter_greedy/fractional_knapsack_problem/#2","title":"2. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"<p>\u6211\u5011\u5efa\u7acb\u4e86\u4e00\u500b\u7269\u54c1\u985e\u5225 <code>Item</code> \uff0c\u4ee5\u4fbf\u5c07\u7269\u54c1\u6309\u7167\u55ae\u4f4d\u50f9\u503c\u9032\u884c\u6392\u5e8f\u3002\u8ff4\u5708\u9032\u884c\u8caa\u5a6a\u9078\u64c7\uff0c\u7576\u80cc\u5305\u5df2\u6eff\u6642\u8df3\u51fa\u4e26\u8fd4\u56de\u89e3\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig fractional_knapsack.py<pre><code>class Item:\n \"\"\"\u7269\u54c1\"\"\"\n\n def __init__(self, w: int, v: int):\n self.w = w # \u7269\u54c1\u91cd\u91cf\n self.v = v # \u7269\u54c1\u50f9\u503c\n\ndef fractional_knapsack(wgt: list[int], val: list[int], cap: int) -&gt; int:\n \"\"\"\u5206\u6578\u80cc\u5305\uff1a\u8caa\u5a6a\"\"\"\n # \u5efa\u7acb\u7269\u54c1\u4e32\u5217\uff0c\u5305\u542b\u5169\u500b\u5c6c\u6027\uff1a\u91cd\u91cf\u3001\u50f9\u503c\n items = [Item(w, v) for w, v in zip(wgt, val)]\n # \u6309\u7167\u55ae\u4f4d\u50f9\u503c item.v / item.w \u5f9e\u9ad8\u5230\u4f4e\u9032\u884c\u6392\u5e8f\n items.sort(key=lambda item: item.v / item.w, reverse=True)\n # \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\n res = 0\n for item in items:\n if item.w &lt;= cap:\n # \u82e5\u5269\u9918\u5bb9\u91cf\u5145\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u6574\u500b\u88dd\u9032\u80cc\u5305\n res += item.v\n cap -= item.w\n else:\n # \u82e5\u5269\u9918\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88dd\u9032\u80cc\u5305\n res += (item.v / item.w) * cap\n # \u5df2\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u8ff4\u5708\n break\n return res\n</code></pre> fractional_knapsack.cpp<pre><code>/* \u7269\u54c1 */\nclass Item {\n public:\n int w; // \u7269\u54c1\u91cd\u91cf\n int v; // \u7269\u54c1\u50f9\u503c\n\n Item(int w, int v) : w(w), v(v) {\n }\n};\n\n/* \u5206\u6578\u80cc\u5305\uff1a\u8caa\u5a6a */\ndouble fractionalKnapsack(vector&lt;int&gt; &amp;wgt, vector&lt;int&gt; &amp;val, int cap) {\n // \u5efa\u7acb\u7269\u54c1\u4e32\u5217\uff0c\u5305\u542b\u5169\u500b\u5c6c\u6027\uff1a\u91cd\u91cf\u3001\u50f9\u503c\n vector&lt;Item&gt; items;\n for (int i = 0; i &lt; wgt.size(); i++) {\n items.push_back(Item(wgt[i], val[i]));\n }\n // \u6309\u7167\u55ae\u4f4d\u50f9\u503c item.v / item.w \u5f9e\u9ad8\u5230\u4f4e\u9032\u884c\u6392\u5e8f\n sort(items.begin(), items.end(), [](Item &amp;a, Item &amp;b) { return (double)a.v / a.w &gt; (double)b.v / b.w; });\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\n double res = 0;\n for (auto &amp;item : items) {\n if (item.w &lt;= cap) {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u5145\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u6574\u500b\u88dd\u9032\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88dd\u9032\u80cc\u5305\n res += (double)item.v / item.w * cap;\n // \u5df2\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u8ff4\u5708\n break;\n }\n }\n return res;\n}\n</code></pre> fractional_knapsack.java<pre><code>/* \u7269\u54c1 */\nclass Item {\n int w; // \u7269\u54c1\u91cd\u91cf\n int v; // \u7269\u54c1\u50f9\u503c\n\n public Item(int w, int v) {\n this.w = w;\n this.v = v;\n }\n}\n\n/* \u5206\u6578\u80cc\u5305\uff1a\u8caa\u5a6a */\ndouble fractionalKnapsack(int[] wgt, int[] val, int cap) {\n // \u5efa\u7acb\u7269\u54c1\u4e32\u5217\uff0c\u5305\u542b\u5169\u500b\u5c6c\u6027\uff1a\u91cd\u91cf\u3001\u50f9\u503c\n Item[] items = new Item[wgt.length];\n for (int i = 0; i &lt; wgt.length; i++) {\n items[i] = new Item(wgt[i], val[i]);\n }\n // \u6309\u7167\u55ae\u4f4d\u50f9\u503c item.v / item.w \u5f9e\u9ad8\u5230\u4f4e\u9032\u884c\u6392\u5e8f\n Arrays.sort(items, Comparator.comparingDouble(item -&gt; -((double) item.v / item.w)));\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\n double res = 0;\n for (Item item : items) {\n if (item.w &lt;= cap) {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u5145\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u6574\u500b\u88dd\u9032\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88dd\u9032\u80cc\u5305\n res += (double) item.v / item.w * cap;\n // \u5df2\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u8ff4\u5708\n break;\n }\n }\n return res;\n}\n</code></pre> fractional_knapsack.cs<pre><code>/* \u7269\u54c1 */\nclass Item(int w, int v) {\n public int w = w; // \u7269\u54c1\u91cd\u91cf\n public int v = v; // \u7269\u54c1\u50f9\u503c\n}\n\n/* \u5206\u6578\u80cc\u5305\uff1a\u8caa\u5a6a */\ndouble FractionalKnapsack(int[] wgt, int[] val, int cap) {\n // \u5efa\u7acb\u7269\u54c1\u4e32\u5217\uff0c\u5305\u542b\u5169\u500b\u5c6c\u6027\uff1a\u91cd\u91cf\u3001\u50f9\u503c\n Item[] items = new Item[wgt.Length];\n for (int i = 0; i &lt; wgt.Length; i++) {\n items[i] = new Item(wgt[i], val[i]);\n }\n // \u6309\u7167\u55ae\u4f4d\u50f9\u503c item.v / item.w \u5f9e\u9ad8\u5230\u4f4e\u9032\u884c\u6392\u5e8f\n Array.Sort(items, (x, y) =&gt; (y.v / y.w).CompareTo(x.v / x.w));\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\n double res = 0;\n foreach (Item item in items) {\n if (item.w &lt;= cap) {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u5145\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u6574\u500b\u88dd\u9032\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88dd\u9032\u80cc\u5305\n res += (double)item.v / item.w * cap;\n // \u5df2\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u8ff4\u5708\n break;\n }\n }\n return res;\n}\n</code></pre> fractional_knapsack.go<pre><code>/* \u7269\u54c1 */\ntype Item struct {\n w int // \u7269\u54c1\u91cd\u91cf\n v int // \u7269\u54c1\u50f9\u503c\n}\n\n/* \u5206\u6578\u80cc\u5305\uff1a\u8caa\u5a6a */\nfunc fractionalKnapsack(wgt []int, val []int, cap int) float64 {\n // \u5efa\u7acb\u7269\u54c1\u4e32\u5217\uff0c\u5305\u542b\u5169\u500b\u5c6c\u6027\uff1a\u91cd\u91cf\u3001\u50f9\u503c\n items := make([]Item, len(wgt))\n for i := 0; i &lt; len(wgt); i++ {\n items[i] = Item{wgt[i], val[i]}\n }\n // \u6309\u7167\u55ae\u4f4d\u50f9\u503c item.v / item.w \u5f9e\u9ad8\u5230\u4f4e\u9032\u884c\u6392\u5e8f\n sort.Slice(items, func(i, j int) bool {\n return float64(items[i].v)/float64(items[i].w) &gt; float64(items[j].v)/float64(items[j].w)\n })\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\n res := 0.0\n for _, item := range items {\n if item.w &lt;= cap {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u5145\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u6574\u500b\u88dd\u9032\u80cc\u5305\n res += float64(item.v)\n cap -= item.w\n } else {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88dd\u9032\u80cc\u5305\n res += float64(item.v) / float64(item.w) * float64(cap)\n // \u5df2\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u8ff4\u5708\n break\n }\n }\n return res\n}\n</code></pre> fractional_knapsack.swift<pre><code>/* \u7269\u54c1 */\nclass Item {\n var w: Int // \u7269\u54c1\u91cd\u91cf\n var v: Int // \u7269\u54c1\u50f9\u503c\n\n init(w: Int, v: Int) {\n self.w = w\n self.v = v\n }\n}\n\n/* \u5206\u6578\u80cc\u5305\uff1a\u8caa\u5a6a */\nfunc fractionalKnapsack(wgt: [Int], val: [Int], cap: Int) -&gt; Double {\n // \u5efa\u7acb\u7269\u54c1\u4e32\u5217\uff0c\u5305\u542b\u5169\u500b\u5c6c\u6027\uff1a\u91cd\u91cf\u3001\u50f9\u503c\n var items = zip(wgt, val).map { Item(w: $0, v: $1) }\n // \u6309\u7167\u55ae\u4f4d\u50f9\u503c item.v / item.w \u5f9e\u9ad8\u5230\u4f4e\u9032\u884c\u6392\u5e8f\n items.sort { -(Double($0.v) / Double($0.w)) &lt; -(Double($1.v) / Double($1.w)) }\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\n var res = 0.0\n var cap = cap\n for item in items {\n if item.w &lt;= cap {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u5145\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u6574\u500b\u88dd\u9032\u80cc\u5305\n res += Double(item.v)\n cap -= item.w\n } else {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88dd\u9032\u80cc\u5305\n res += Double(item.v) / Double(item.w) * Double(cap)\n // \u5df2\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u8ff4\u5708\n break\n }\n }\n return res\n}\n</code></pre> fractional_knapsack.js<pre><code>/* \u7269\u54c1 */\nclass Item {\n constructor(w, v) {\n this.w = w; // \u7269\u54c1\u91cd\u91cf\n this.v = v; // \u7269\u54c1\u50f9\u503c\n }\n}\n\n/* \u5206\u6578\u80cc\u5305\uff1a\u8caa\u5a6a */\nfunction fractionalKnapsack(wgt, val, cap) {\n // \u5efa\u7acb\u7269\u54c1\u4e32\u5217\uff0c\u5305\u542b\u5169\u500b\u5c6c\u6027\uff1a\u91cd\u91cf\u3001\u50f9\u503c\n const items = wgt.map((w, i) =&gt; new Item(w, val[i]));\n // \u6309\u7167\u55ae\u4f4d\u50f9\u503c item.v / item.w \u5f9e\u9ad8\u5230\u4f4e\u9032\u884c\u6392\u5e8f\n items.sort((a, b) =&gt; b.v / b.w - a.v / a.w);\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\n let res = 0;\n for (const item of items) {\n if (item.w &lt;= cap) {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u5145\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u6574\u500b\u88dd\u9032\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88dd\u9032\u80cc\u5305\n res += (item.v / item.w) * cap;\n // \u5df2\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u8ff4\u5708\n break;\n }\n }\n return res;\n}\n</code></pre> fractional_knapsack.ts<pre><code>/* \u7269\u54c1 */\nclass Item {\n w: number; // \u7269\u54c1\u91cd\u91cf\n v: number; // \u7269\u54c1\u50f9\u503c\n\n constructor(w: number, v: number) {\n this.w = w;\n this.v = v;\n }\n}\n\n/* \u5206\u6578\u80cc\u5305\uff1a\u8caa\u5a6a */\nfunction fractionalKnapsack(wgt: number[], val: number[], cap: number): number {\n // \u5efa\u7acb\u7269\u54c1\u4e32\u5217\uff0c\u5305\u542b\u5169\u500b\u5c6c\u6027\uff1a\u91cd\u91cf\u3001\u50f9\u503c\n const items: Item[] = wgt.map((w, i) =&gt; new Item(w, val[i]));\n // \u6309\u7167\u55ae\u4f4d\u50f9\u503c item.v / item.w \u5f9e\u9ad8\u5230\u4f4e\u9032\u884c\u6392\u5e8f\n items.sort((a, b) =&gt; b.v / b.w - a.v / a.w);\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\n let res = 0;\n for (const item of items) {\n if (item.w &lt;= cap) {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u5145\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u6574\u500b\u88dd\u9032\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88dd\u9032\u80cc\u5305\n res += (item.v / item.w) * cap;\n // \u5df2\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u8ff4\u5708\n break;\n }\n }\n return res;\n}\n</code></pre> fractional_knapsack.dart<pre><code>/* \u7269\u54c1 */\nclass Item {\n int w; // \u7269\u54c1\u91cd\u91cf\n int v; // \u7269\u54c1\u50f9\u503c\n\n Item(this.w, this.v);\n}\n\n/* \u5206\u6578\u80cc\u5305\uff1a\u8caa\u5a6a */\ndouble fractionalKnapsack(List&lt;int&gt; wgt, List&lt;int&gt; val, int cap) {\n // \u5efa\u7acb\u7269\u54c1\u4e32\u5217\uff0c\u5305\u542b\u5169\u500b\u5c6c\u6027\uff1a\u91cd\u91cf\u3001\u50f9\u503c\n List&lt;Item&gt; items = List.generate(wgt.length, (i) =&gt; Item(wgt[i], val[i]));\n // \u6309\u7167\u55ae\u4f4d\u50f9\u503c item.v / item.w \u5f9e\u9ad8\u5230\u4f4e\u9032\u884c\u6392\u5e8f\n items.sort((a, b) =&gt; (b.v / b.w).compareTo(a.v / a.w));\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\n double res = 0;\n for (Item item in items) {\n if (item.w &lt;= cap) {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u5145\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u6574\u500b\u88dd\u9032\u80cc\u5305\n res += item.v;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88dd\u9032\u80cc\u5305\n res += item.v / item.w * cap;\n // \u5df2\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u8ff4\u5708\n break;\n }\n }\n return res;\n}\n</code></pre> fractional_knapsack.rs<pre><code>/* \u7269\u54c1 */\nstruct Item {\n w: i32, // \u7269\u54c1\u91cd\u91cf\n v: i32, // \u7269\u54c1\u50f9\u503c\n}\n\nimpl Item {\n fn new(w: i32, v: i32) -&gt; Self {\n Self { w, v }\n }\n}\n\n/* \u5206\u6578\u80cc\u5305\uff1a\u8caa\u5a6a */\nfn fractional_knapsack(wgt: &amp;[i32], val: &amp;[i32], mut cap: i32) -&gt; f64 {\n // \u5efa\u7acb\u7269\u54c1\u4e32\u5217\uff0c\u5305\u542b\u5169\u500b\u5c6c\u6027\uff1a\u91cd\u91cf\u3001\u50f9\u503c\n let mut items = wgt\n .iter()\n .zip(val.iter())\n .map(|(&amp;w, &amp;v)| Item::new(w, v))\n .collect::&lt;Vec&lt;Item&gt;&gt;();\n // \u6309\u7167\u55ae\u4f4d\u50f9\u503c item.v / item.w \u5f9e\u9ad8\u5230\u4f4e\u9032\u884c\u6392\u5e8f\n items.sort_by(|a, b| {\n (b.v as f64 / b.w as f64)\n .partial_cmp(&amp;(a.v as f64 / a.w as f64))\n .unwrap()\n });\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\n let mut res = 0.0;\n for item in &amp;items {\n if item.w &lt;= cap {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u5145\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u6574\u500b\u88dd\u9032\u80cc\u5305\n res += item.v as f64;\n cap -= item.w;\n } else {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88dd\u9032\u80cc\u5305\n res += item.v as f64 / item.w as f64 * cap as f64;\n // \u5df2\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u8ff4\u5708\n break;\n }\n }\n res\n}\n</code></pre> fractional_knapsack.c<pre><code>/* \u7269\u54c1 */\ntypedef struct {\n int w; // \u7269\u54c1\u91cd\u91cf\n int v; // \u7269\u54c1\u50f9\u503c\n} Item;\n\n/* \u5206\u6578\u80cc\u5305\uff1a\u8caa\u5a6a */\nfloat fractionalKnapsack(int wgt[], int val[], int itemCount, int cap) {\n // \u5efa\u7acb\u7269\u54c1\u4e32\u5217\uff0c\u5305\u542b\u5169\u500b\u5c6c\u6027\uff1a\u91cd\u91cf\u3001\u50f9\u503c\n Item *items = malloc(sizeof(Item) * itemCount);\n for (int i = 0; i &lt; itemCount; i++) {\n items[i] = (Item){.w = wgt[i], .v = val[i]};\n }\n // \u6309\u7167\u55ae\u4f4d\u50f9\u503c item.v / item.w \u5f9e\u9ad8\u5230\u4f4e\u9032\u884c\u6392\u5e8f\n qsort(items, (size_t)itemCount, sizeof(Item), sortByValueDensity);\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\n float res = 0.0;\n for (int i = 0; i &lt; itemCount; i++) {\n if (items[i].w &lt;= cap) {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u5145\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u6574\u500b\u88dd\u9032\u80cc\u5305\n res += items[i].v;\n cap -= items[i].w;\n } else {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88dd\u9032\u80cc\u5305\n res += (float)cap / items[i].w * items[i].v;\n cap = 0;\n break;\n }\n }\n free(items);\n return res;\n}\n</code></pre> fractional_knapsack.kt<pre><code>/* \u7269\u54c1 */\nclass Item(\n val w: Int, // \u7269\u54c1\n val v: Int // \u7269\u54c1\u50f9\u503c\n)\n\n/* \u5206\u6578\u80cc\u5305\uff1a\u8caa\u5a6a */\nfun fractionalKnapsack(wgt: IntArray, _val: IntArray, c: Int): Double {\n // \u5efa\u7acb\u7269\u54c1\u4e32\u5217\uff0c\u5305\u542b\u5169\u500b\u5c6c\u6027\uff1a\u91cd\u91cf\u3001\u50f9\u503c\n var cap = c\n val items = arrayOfNulls&lt;Item&gt;(wgt.size)\n for (i in wgt.indices) {\n items[i] = Item(wgt[i], _val[i])\n }\n // \u6309\u7167\u55ae\u4f4d\u50f9\u503c item.v / item.w \u5f9e\u9ad8\u5230\u4f4e\u9032\u884c\u6392\u5e8f\n items.sortBy { item: Item? -&gt; -(item!!.v.toDouble() / item.w) }\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\n var res = 0.0\n for (item in items) {\n if (item!!.w &lt;= cap) {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u5145\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u6574\u500b\u88dd\u9032\u80cc\u5305\n res += item.v\n cap -= item.w\n } else {\n // \u82e5\u5269\u9918\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5247\u5c07\u7576\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88dd\u9032\u80cc\u5305\n res += item.v.toDouble() / item.w * cap\n // \u5df2\u7121\u5269\u9918\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u8ff4\u5708\n break\n }\n }\n return res\n}\n</code></pre> fractional_knapsack.rb<pre><code>[class]{Item}-[func]{}\n\n[class]{}-[func]{fractional_knapsack}\n</code></pre> fractional_knapsack.zig<pre><code>[class]{Item}-[func]{}\n\n[class]{}-[func]{fractionalKnapsack}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u9664\u6392\u5e8f\u4e4b\u5916\uff0c\u5728\u6700\u5dee\u60c5\u6cc1\u4e0b\uff0c\u9700\u8981\u8d70\u8a2a\u6574\u500b\u7269\u54c1\u4e32\u5217\uff0c\u56e0\u6b64\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u70ba\u7269\u54c1\u6578\u91cf\u3002</p> <p>\u7531\u65bc\u521d\u59cb\u5316\u4e86\u4e00\u500b <code>Item</code> \u7269\u4ef6\u4e32\u5217\uff0c\u56e0\u6b64\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \u3002</p>"},{"location":"chapter_greedy/fractional_knapsack_problem/#3","title":"3. \u00a0 \u6b63\u78ba\u6027\u8b49\u660e","text":"<p>\u63a1\u7528\u53cd\u8b49\u6cd5\u3002\u5047\u8a2d\u7269\u54c1 \\(x\\) \u662f\u55ae\u4f4d\u50f9\u503c\u6700\u9ad8\u7684\u7269\u54c1\uff0c\u4f7f\u7528\u67d0\u6f14\u7b97\u6cd5\u6c42\u5f97\u6700\u5927\u50f9\u503c\u70ba <code>res</code> \uff0c\u4f46\u8a72\u89e3\u4e2d\u4e0d\u5305\u542b\u7269\u54c1 \\(x\\) \u3002</p> <p>\u73fe\u5728\u5f9e\u80cc\u5305\u4e2d\u62ff\u51fa\u55ae\u4f4d\u91cd\u91cf\u7684\u4efb\u610f\u7269\u54c1\uff0c\u4e26\u66ff\u63db\u70ba\u55ae\u4f4d\u91cd\u91cf\u7684\u7269\u54c1 \\(x\\) \u3002\u7531\u65bc\u7269\u54c1 \\(x\\) \u7684\u55ae\u4f4d\u50f9\u503c\u6700\u9ad8\uff0c\u56e0\u6b64\u66ff\u63db\u5f8c\u7684\u7e3d\u50f9\u503c\u4e00\u5b9a\u5927\u65bc <code>res</code> \u3002\u9019\u8207 <code>res</code> \u662f\u6700\u512a\u89e3\u77db\u76fe\uff0c\u8aaa\u660e\u6700\u512a\u89e3\u4e2d\u5fc5\u9808\u5305\u542b\u7269\u54c1 \\(x\\) \u3002</p> <p>\u5c0d\u65bc\u8a72\u89e3\u4e2d\u7684\u5176\u4ed6\u7269\u54c1\uff0c\u6211\u5011\u4e5f\u53ef\u4ee5\u69cb\u5efa\u51fa\u4e0a\u8ff0\u77db\u76fe\u3002\u7e3d\u800c\u8a00\u4e4b\uff0c\u55ae\u4f4d\u50f9\u503c\u66f4\u5927\u7684\u7269\u54c1\u7e3d\u662f\u66f4\u512a\u9078\u64c7\uff0c\u9019\u8aaa\u660e\u8caa\u5a6a\u7b56\u7565\u662f\u6709\u6548\u7684\u3002</p> <p>\u5982\u5716 15-6 \u6240\u793a\uff0c\u5982\u679c\u5c07\u7269\u54c1\u91cd\u91cf\u548c\u7269\u54c1\u55ae\u4f4d\u50f9\u503c\u5206\u5225\u770b\u4f5c\u4e00\u5f35\u4e8c\u7dad\u5716\u8868\u7684\u6a6b\u8ef8\u548c\u7e31\u8ef8\uff0c\u5247\u5206\u6578\u80cc\u5305\u554f\u984c\u53ef\u8f49\u5316\u70ba\u201c\u6c42\u5728\u6709\u9650\u6a6b\u8ef8\u5340\u9593\u4e0b\u570d\u6210\u7684\u6700\u5927\u9762\u7a4d\u201d\u3002\u9019\u500b\u985e\u6bd4\u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u5f9e\u5e7e\u4f55\u89d2\u5ea6\u7406\u89e3\u8caa\u5a6a\u7b56\u7565\u7684\u6709\u6548\u6027\u3002</p> <p></p> <p> \u5716 15-6 \u00a0 \u5206\u6578\u80cc\u5305\u554f\u984c\u7684\u5e7e\u4f55\u8868\u793a </p>"},{"location":"chapter_greedy/greedy_algorithm/","title":"15.1 \u00a0 \u8caa\u5a6a\u6f14\u7b97\u6cd5","text":"<p>\u8caa\u5a6a\u6f14\u7b97\u6cd5\uff08greedy algorithm\uff09\u662f\u4e00\u7a2e\u5e38\u898b\u7684\u89e3\u6c7a\u6700\u4f73\u5316\u554f\u984c\u7684\u6f14\u7b97\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u60f3\u662f\u5728\u554f\u984c\u7684\u6bcf\u500b\u6c7a\u7b56\u968e\u6bb5\uff0c\u90fd\u9078\u64c7\u7576\u524d\u770b\u8d77\u4f86\u6700\u512a\u7684\u9078\u64c7\uff0c\u5373\u8caa\u5a6a\u5730\u505a\u51fa\u5340\u57df\u6027\u6700\u512a\u7684\u6c7a\u7b56\uff0c\u4ee5\u671f\u7372\u5f97\u5168\u57df\u6027\u6700\u512a\u89e3\u3002\u8caa\u5a6a\u6f14\u7b97\u6cd5\u7c21\u6f54\u4e14\u9ad8\u6548\uff0c\u5728\u8a31\u591a\u5be6\u969b\u554f\u984c\u4e2d\u6709\u8457\u5ee3\u6cdb\u7684\u61c9\u7528\u3002</p> <p>\u8caa\u5a6a\u6f14\u7b97\u6cd5\u548c\u52d5\u614b\u898f\u5283\u90fd\u5e38\u7528\u65bc\u89e3\u6c7a\u6700\u4f73\u5316\u554f\u984c\u3002\u5b83\u5011\u4e4b\u9593\u5b58\u5728\u4e00\u4e9b\u76f8\u4f3c\u4e4b\u8655\uff0c\u6bd4\u5982\u90fd\u4f9d\u8cf4\u6700\u512a\u5b50\u7d50\u69cb\u6027\u8cea\uff0c\u4f46\u5de5\u4f5c\u539f\u7406\u4e0d\u540c\u3002</p> <ul> <li>\u52d5\u614b\u898f\u5283\u6703\u6839\u64da\u4e4b\u524d\u968e\u6bb5\u7684\u6240\u6709\u6c7a\u7b56\u4f86\u8003\u616e\u7576\u524d\u6c7a\u7b56\uff0c\u4e26\u4f7f\u7528\u904e\u53bb\u5b50\u554f\u984c\u7684\u89e3\u4f86\u69cb\u5efa\u7576\u524d\u5b50\u554f\u984c\u7684\u89e3\u3002</li> <li>\u8caa\u5a6a\u6f14\u7b97\u6cd5\u4e0d\u6703\u8003\u616e\u904e\u53bb\u7684\u6c7a\u7b56\uff0c\u800c\u662f\u4e00\u8def\u5411\u524d\u5730\u9032\u884c\u8caa\u5a6a\u9078\u64c7\uff0c\u4e0d\u65b7\u7e2e\u5c0f\u554f\u984c\u7bc4\u570d\uff0c\u76f4\u81f3\u554f\u984c\u88ab\u89e3\u6c7a\u3002</li> </ul> <p>\u6211\u5011\u5148\u900f\u904e\u4f8b\u984c\u201c\u96f6\u9322\u514c\u63db\u201d\u77ad\u89e3\u8caa\u5a6a\u6f14\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002\u9019\u9053\u984c\u5df2\u7d93\u5728\u201c\u5b8c\u5168\u80cc\u5305\u554f\u984c\u201d\u7ae0\u7bc0\u4e2d\u4ecb\u7d39\u904e\uff0c\u76f8\u4fe1\u4f60\u5c0d\u5b83\u4e26\u4e0d\u964c\u751f\u3002</p> <p>Question</p> <p>\u7d66\u5b9a \\(n\\) \u7a2e\u786c\u5e63\uff0c\u7b2c \\(i\\) \u7a2e\u786c\u5e63\u7684\u9762\u503c\u70ba \\(coins[i - 1]\\) \uff0c\u76ee\u6a19\u91d1\u984d\u70ba \\(amt\\) \uff0c\u6bcf\u7a2e\u786c\u5e63\u53ef\u4ee5\u91cd\u8907\u9078\u53d6\uff0c\u554f\u80fd\u5920\u6e4a\u51fa\u76ee\u6a19\u91d1\u984d\u7684\u6700\u5c11\u786c\u5e63\u6578\u91cf\u3002\u5982\u679c\u7121\u6cd5\u6e4a\u51fa\u76ee\u6a19\u91d1\u984d\uff0c\u5247\u8fd4\u56de \\(-1\\) \u3002</p> <p>\u672c\u984c\u63a1\u53d6\u7684\u8caa\u5a6a\u7b56\u7565\u5982\u5716 15-1 \u6240\u793a\u3002\u7d66\u5b9a\u76ee\u6a19\u91d1\u984d\uff0c\u6211\u5011\u8caa\u5a6a\u5730\u9078\u64c7\u4e0d\u5927\u65bc\u4e14\u6700\u63a5\u8fd1\u5b83\u7684\u786c\u5e63\uff0c\u4e0d\u65b7\u8ff4\u5708\u8a72\u6b65\u9a5f\uff0c\u76f4\u81f3\u6e4a\u51fa\u76ee\u6a19\u91d1\u984d\u70ba\u6b62\u3002</p> <p></p> <p> \u5716 15-1 \u00a0 \u96f6\u9322\u514c\u63db\u7684\u8caa\u5a6a\u7b56\u7565 </p> <p>\u5be6\u73fe\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig coin_change_greedy.py<pre><code>def coin_change_greedy(coins: list[int], amt: int) -&gt; int:\n \"\"\"\u96f6\u9322\u514c\u63db\uff1a\u8caa\u5a6a\"\"\"\n # \u5047\u8a2d coins \u4e32\u5217\u6709\u5e8f\n i = len(coins) - 1\n count = 0\n # \u8ff4\u5708\u9032\u884c\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u5230\u7121\u5269\u9918\u91d1\u984d\n while amt &gt; 0:\n # \u627e\u5230\u5c0f\u65bc\u4e14\u6700\u63a5\u8fd1\u5269\u9918\u91d1\u984d\u7684\u786c\u5e63\n while i &gt; 0 and coins[i] &gt; amt:\n i -= 1\n # \u9078\u64c7 coins[i]\n amt -= coins[i]\n count += 1\n # \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5247\u8fd4\u56de -1\n return count if amt == 0 else -1\n</code></pre> coin_change_greedy.cpp<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u8caa\u5a6a */\nint coinChangeGreedy(vector&lt;int&gt; &amp;coins, int amt) {\n // \u5047\u8a2d coins \u4e32\u5217\u6709\u5e8f\n int i = coins.size() - 1;\n int count = 0;\n // \u8ff4\u5708\u9032\u884c\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u5230\u7121\u5269\u9918\u91d1\u984d\n while (amt &gt; 0) {\n // \u627e\u5230\u5c0f\u65bc\u4e14\u6700\u63a5\u8fd1\u5269\u9918\u91d1\u984d\u7684\u786c\u5e63\n while (i &gt; 0 &amp;&amp; coins[i] &gt; amt) {\n i--;\n }\n // \u9078\u64c7 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5247\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n</code></pre> coin_change_greedy.java<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u8caa\u5a6a */\nint coinChangeGreedy(int[] coins, int amt) {\n // \u5047\u8a2d coins \u4e32\u5217\u6709\u5e8f\n int i = coins.length - 1;\n int count = 0;\n // \u8ff4\u5708\u9032\u884c\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u5230\u7121\u5269\u9918\u91d1\u984d\n while (amt &gt; 0) {\n // \u627e\u5230\u5c0f\u65bc\u4e14\u6700\u63a5\u8fd1\u5269\u9918\u91d1\u984d\u7684\u786c\u5e63\n while (i &gt; 0 &amp;&amp; coins[i] &gt; amt) {\n i--;\n }\n // \u9078\u64c7 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5247\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n</code></pre> coin_change_greedy.cs<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u8caa\u5a6a */\nint CoinChangeGreedy(int[] coins, int amt) {\n // \u5047\u8a2d coins \u4e32\u5217\u6709\u5e8f\n int i = coins.Length - 1;\n int count = 0;\n // \u8ff4\u5708\u9032\u884c\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u5230\u7121\u5269\u9918\u91d1\u984d\n while (amt &gt; 0) {\n // \u627e\u5230\u5c0f\u65bc\u4e14\u6700\u63a5\u8fd1\u5269\u9918\u91d1\u984d\u7684\u786c\u5e63\n while (i &gt; 0 &amp;&amp; coins[i] &gt; amt) {\n i--;\n }\n // \u9078\u64c7 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5247\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n</code></pre> coin_change_greedy.go<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u8caa\u5a6a */\nfunc coinChangeGreedy(coins []int, amt int) int {\n // \u5047\u8a2d coins \u4e32\u5217\u6709\u5e8f\n i := len(coins) - 1\n count := 0\n // \u8ff4\u5708\u9032\u884c\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u5230\u7121\u5269\u9918\u91d1\u984d\n for amt &gt; 0 {\n // \u627e\u5230\u5c0f\u65bc\u4e14\u6700\u63a5\u8fd1\u5269\u9918\u91d1\u984d\u7684\u786c\u5e63\n for i &gt; 0 &amp;&amp; coins[i] &gt; amt {\n i--\n }\n // \u9078\u64c7 coins[i]\n amt -= coins[i]\n count++\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5247\u8fd4\u56de -1\n if amt != 0 {\n return -1\n }\n return count\n}\n</code></pre> coin_change_greedy.swift<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u8caa\u5a6a */\nfunc coinChangeGreedy(coins: [Int], amt: Int) -&gt; Int {\n // \u5047\u8a2d coins \u4e32\u5217\u6709\u5e8f\n var i = coins.count - 1\n var count = 0\n var amt = amt\n // \u8ff4\u5708\u9032\u884c\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u5230\u7121\u5269\u9918\u91d1\u984d\n while amt &gt; 0 {\n // \u627e\u5230\u5c0f\u65bc\u4e14\u6700\u63a5\u8fd1\u5269\u9918\u91d1\u984d\u7684\u786c\u5e63\n while i &gt; 0 &amp;&amp; coins[i] &gt; amt {\n i -= 1\n }\n // \u9078\u64c7 coins[i]\n amt -= coins[i]\n count += 1\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5247\u8fd4\u56de -1\n return amt == 0 ? count : -1\n}\n</code></pre> coin_change_greedy.js<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u8caa\u5a6a */\nfunction coinChangeGreedy(coins, amt) {\n // \u5047\u8a2d coins \u9663\u5217\u6709\u5e8f\n let i = coins.length - 1;\n let count = 0;\n // \u8ff4\u5708\u9032\u884c\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u5230\u7121\u5269\u9918\u91d1\u984d\n while (amt &gt; 0) {\n // \u627e\u5230\u5c0f\u65bc\u4e14\u6700\u63a5\u8fd1\u5269\u9918\u91d1\u984d\u7684\u786c\u5e63\n while (i &gt; 0 &amp;&amp; coins[i] &gt; amt) {\n i--;\n }\n // \u9078\u64c7 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5247\u8fd4\u56de -1\n return amt === 0 ? count : -1;\n}\n</code></pre> coin_change_greedy.ts<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u8caa\u5a6a */\nfunction coinChangeGreedy(coins: number[], amt: number): number {\n // \u5047\u8a2d coins \u9663\u5217\u6709\u5e8f\n let i = coins.length - 1;\n let count = 0;\n // \u8ff4\u5708\u9032\u884c\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u5230\u7121\u5269\u9918\u91d1\u984d\n while (amt &gt; 0) {\n // \u627e\u5230\u5c0f\u65bc\u4e14\u6700\u63a5\u8fd1\u5269\u9918\u91d1\u984d\u7684\u786c\u5e63\n while (i &gt; 0 &amp;&amp; coins[i] &gt; amt) {\n i--;\n }\n // \u9078\u64c7 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5247\u8fd4\u56de -1\n return amt === 0 ? count : -1;\n}\n</code></pre> coin_change_greedy.dart<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u8caa\u5a6a */\nint coinChangeGreedy(List&lt;int&gt; coins, int amt) {\n // \u5047\u8a2d coins \u4e32\u5217\u6709\u5e8f\n int i = coins.length - 1;\n int count = 0;\n // \u8ff4\u5708\u9032\u884c\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u5230\u7121\u5269\u9918\u91d1\u984d\n while (amt &gt; 0) {\n // \u627e\u5230\u5c0f\u65bc\u4e14\u6700\u63a5\u8fd1\u5269\u9918\u91d1\u984d\u7684\u786c\u5e63\n while (i &gt; 0 &amp;&amp; coins[i] &gt; amt) {\n i--;\n }\n // \u9078\u64c7 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5247\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n</code></pre> coin_change_greedy.rs<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u8caa\u5a6a */\nfn coin_change_greedy(coins: &amp;[i32], mut amt: i32) -&gt; i32 {\n // \u5047\u8a2d coins \u4e32\u5217\u6709\u5e8f\n let mut i = coins.len() - 1;\n let mut count = 0;\n // \u8ff4\u5708\u9032\u884c\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u5230\u7121\u5269\u9918\u91d1\u984d\n while amt &gt; 0 {\n // \u627e\u5230\u5c0f\u65bc\u4e14\u6700\u63a5\u8fd1\u5269\u9918\u91d1\u984d\u7684\u786c\u5e63\n while i &gt; 0 &amp;&amp; coins[i] &gt; amt {\n i -= 1;\n }\n // \u9078\u64c7 coins[i]\n amt -= coins[i];\n count += 1;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5247\u8fd4\u56de -1\n if amt == 0 {\n count\n } else {\n -1\n }\n}\n</code></pre> coin_change_greedy.c<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u8caa\u5a6a */\nint coinChangeGreedy(int *coins, int size, int amt) {\n // \u5047\u8a2d coins \u4e32\u5217\u6709\u5e8f\n int i = size - 1;\n int count = 0;\n // \u8ff4\u5708\u9032\u884c\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u5230\u7121\u5269\u9918\u91d1\u984d\n while (amt &gt; 0) {\n // \u627e\u5230\u5c0f\u65bc\u4e14\u6700\u63a5\u8fd1\u5269\u9918\u91d1\u984d\u7684\u786c\u5e63\n while (i &gt; 0 &amp;&amp; coins[i] &gt; amt) {\n i--;\n }\n // \u9078\u64c7 coins[i]\n amt -= coins[i];\n count++;\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5247\u8fd4\u56de -1\n return amt == 0 ? count : -1;\n}\n</code></pre> coin_change_greedy.kt<pre><code>/* \u96f6\u9322\u514c\u63db\uff1a\u8caa\u5a6a */\nfun coinChangeGreedy(coins: IntArray, amt: Int): Int {\n // \u5047\u8a2d coins \u4e32\u5217\u6709\u5e8f\n var am = amt\n var i = coins.size - 1\n var count = 0\n // \u8ff4\u5708\u9032\u884c\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u5230\u7121\u5269\u9918\u91d1\u984d\n while (am &gt; 0) {\n // \u627e\u5230\u5c0f\u65bc\u4e14\u6700\u63a5\u8fd1\u5269\u9918\u91d1\u984d\u7684\u786c\u5e63\n while (i &gt; 0 &amp;&amp; coins[i] &gt; am) {\n i--\n }\n // \u9078\u64c7 coins[i]\n am -= coins[i]\n count++\n }\n // \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5247\u8fd4\u56de -1\n return if (am == 0) count else -1\n}\n</code></pre> coin_change_greedy.rb<pre><code>[class]{}-[func]{coin_change_greedy}\n</code></pre> coin_change_greedy.zig<pre><code>[class]{}-[func]{coinChangeGreedy}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u4f60\u53ef\u80fd\u6703\u4e0d\u7531\u5730\u767c\u51fa\u611f\u5606\uff1aSo clean \uff01\u8caa\u5a6a\u6f14\u7b97\u6cd5\u50c5\u7528\u7d04\u5341\u884c\u7a0b\u5f0f\u78bc\u5c31\u89e3\u6c7a\u4e86\u96f6\u9322\u514c\u63db\u554f\u984c\u3002</p>"},{"location":"chapter_greedy/greedy_algorithm/#1511","title":"15.1.1 \u00a0 \u8caa\u5a6a\u6f14\u7b97\u6cd5\u7684\u512a\u9ede\u8207\u4fb7\u9650\u6027","text":"<p>\u8caa\u5a6a\u6f14\u7b97\u6cd5\u4e0d\u50c5\u64cd\u4f5c\u76f4\u63a5\u3001\u5be6\u73fe\u7c21\u55ae\uff0c\u800c\u4e14\u901a\u5e38\u6548\u7387\u4e5f\u5f88\u9ad8\u3002\u5728\u4ee5\u4e0a\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u8a18\u786c\u5e63\u6700\u5c0f\u9762\u503c\u70ba \\(\\min(coins)\\) \uff0c\u5247\u8caa\u5a6a\u9078\u64c7\u6700\u591a\u8ff4\u5708 \\(amt / \\min(coins)\\) \u6b21\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(amt / \\min(coins))\\) \u3002\u9019\u6bd4\u52d5\u614b\u898f\u5283\u89e3\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6 \\(O(n \\times amt)\\) \u5c0f\u4e86\u4e00\u500b\u6578\u91cf\u7d1a\u3002</p> <p>\u7136\u800c\uff0c\u5c0d\u65bc\u67d0\u4e9b\u786c\u5e63\u9762\u503c\u7d44\u5408\uff0c\u8caa\u5a6a\u6f14\u7b97\u6cd5\u4e26\u4e0d\u80fd\u627e\u5230\u6700\u512a\u89e3\u3002\u5716 15-2 \u7d66\u51fa\u4e86\u5169\u500b\u793a\u4f8b\u3002</p> <ul> <li>\u6b63\u4f8b \\(coins = [1, 5, 10, 20, 50, 100]\\)\uff1a\u5728\u8a72\u786c\u5e63\u7d44\u5408\u4e0b\uff0c\u7d66\u5b9a\u4efb\u610f \\(amt\\) \uff0c\u8caa\u5a6a\u6f14\u7b97\u6cd5\u90fd\u53ef\u4ee5\u627e\u5230\u6700\u512a\u89e3\u3002</li> <li>\u53cd\u4f8b \\(coins = [1, 20, 50]\\)\uff1a\u5047\u8a2d \\(amt = 60\\) \uff0c\u8caa\u5a6a\u6f14\u7b97\u6cd5\u53ea\u80fd\u627e\u5230 \\(50 + 1 \\times 10\\) \u7684\u514c\u63db\u7d44\u5408\uff0c\u5171\u8a08 \\(11\\) \u679a\u786c\u5e63\uff0c\u4f46\u52d5\u614b\u898f\u5283\u53ef\u4ee5\u627e\u5230\u6700\u512a\u89e3 \\(20 + 20 + 20\\) \uff0c\u50c5\u9700 \\(3\\) \u679a\u786c\u5e63\u3002</li> <li>\u53cd\u4f8b \\(coins = [1, 49, 50]\\)\uff1a\u5047\u8a2d \\(amt = 98\\) \uff0c\u8caa\u5a6a\u6f14\u7b97\u6cd5\u53ea\u80fd\u627e\u5230 \\(50 + 1 \\times 48\\) \u7684\u514c\u63db\u7d44\u5408\uff0c\u5171\u8a08 \\(49\\) \u679a\u786c\u5e63\uff0c\u4f46\u52d5\u614b\u898f\u5283\u53ef\u4ee5\u627e\u5230\u6700\u512a\u89e3 \\(49 + 49\\) \uff0c\u50c5\u9700 \\(2\\) \u679a\u786c\u5e63\u3002</li> </ul> <p></p> <p> \u5716 15-2 \u00a0 \u8caa\u5a6a\u6f14\u7b97\u6cd5\u7121\u6cd5\u627e\u51fa\u6700\u512a\u89e3\u7684\u793a\u4f8b </p> <p>\u4e5f\u5c31\u662f\u8aaa\uff0c\u5c0d\u65bc\u96f6\u9322\u514c\u63db\u554f\u984c\uff0c\u8caa\u5a6a\u6f14\u7b97\u6cd5\u7121\u6cd5\u4fdd\u8b49\u627e\u5230\u5168\u57df\u6027\u6700\u512a\u89e3\uff0c\u4e26\u4e14\u6709\u53ef\u80fd\u627e\u5230\u975e\u5e38\u5dee\u7684\u89e3\u3002\u5b83\u66f4\u9069\u5408\u7528\u52d5\u614b\u898f\u5283\u89e3\u6c7a\u3002</p> <p>\u4e00\u822c\u60c5\u6cc1\u4e0b\uff0c\u8caa\u5a6a\u6f14\u7b97\u6cd5\u7684\u9069\u7528\u60c5\u6cc1\u5206\u4ee5\u4e0b\u5169\u7a2e\u3002</p> <ol> <li>\u53ef\u4ee5\u4fdd\u8b49\u627e\u5230\u6700\u512a\u89e3\uff1a\u8caa\u5a6a\u6f14\u7b97\u6cd5\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\u5f80\u5f80\u662f\u6700\u512a\u9078\u64c7\uff0c\u56e0\u70ba\u5b83\u5f80\u5f80\u6bd4\u56de\u6eaf\u3001\u52d5\u614b\u898f\u5283\u66f4\u9ad8\u6548\u3002</li> <li>\u53ef\u4ee5\u627e\u5230\u8fd1\u4f3c\u6700\u512a\u89e3\uff1a\u8caa\u5a6a\u6f14\u7b97\u6cd5\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\u4e5f\u662f\u53ef\u7528\u7684\u3002\u5c0d\u65bc\u5f88\u591a\u8907\u96dc\u554f\u984c\u4f86\u8aaa\uff0c\u5c0b\u627e\u5168\u57df\u6027\u6700\u512a\u89e3\u975e\u5e38\u56f0\u96e3\uff0c\u80fd\u4ee5\u8f03\u9ad8\u6548\u7387\u627e\u5230\u6b21\u512a\u89e3\u4e5f\u662f\u975e\u5e38\u4e0d\u932f\u7684\u3002</li> </ol>"},{"location":"chapter_greedy/greedy_algorithm/#1512","title":"15.1.2 \u00a0 \u8caa\u5a6a\u6f14\u7b97\u6cd5\u7279\u6027","text":"<p>\u90a3\u9ebc\u554f\u984c\u4f86\u4e86\uff0c\u4ec0\u9ebc\u6a23\u7684\u554f\u984c\u9069\u5408\u7528\u8caa\u5a6a\u6f14\u7b97\u6cd5\u6c42\u89e3\u5462\uff1f\u6216\u8005\u8aaa\uff0c\u8caa\u5a6a\u6f14\u7b97\u6cd5\u5728\u4ec0\u9ebc\u60c5\u6cc1\u4e0b\u53ef\u4ee5\u4fdd\u8b49\u627e\u5230\u6700\u512a\u89e3\uff1f</p> <p>\u76f8\u8f03\u65bc\u52d5\u614b\u898f\u5283\uff0c\u8caa\u5a6a\u6f14\u7b97\u6cd5\u7684\u4f7f\u7528\u689d\u4ef6\u66f4\u52a0\u82db\u523b\uff0c\u5176\u4e3b\u8981\u95dc\u6ce8\u554f\u984c\u7684\u5169\u500b\u6027\u8cea\u3002</p> <ul> <li>\u8caa\u5a6a\u9078\u64c7\u6027\u8cea\uff1a\u53ea\u6709\u7576\u5c40\u90e8\u6700\u512a\u9078\u64c7\u59cb\u7d42\u53ef\u4ee5\u5c0e\u81f4\u5168\u57df\u6027\u6700\u512a\u89e3\u6642\uff0c\u8caa\u5a6a\u6f14\u7b97\u6cd5\u624d\u80fd\u4fdd\u8b49\u5f97\u5230\u6700\u512a\u89e3\u3002</li> <li>\u6700\u512a\u5b50\u7d50\u69cb\uff1a\u539f\u554f\u984c\u7684\u6700\u512a\u89e3\u5305\u542b\u5b50\u554f\u984c\u7684\u6700\u512a\u89e3\u3002</li> </ul> <p>\u6700\u512a\u5b50\u7d50\u69cb\u5df2\u7d93\u5728\u201c\u52d5\u614b\u898f\u5283\u201d\u7ae0\u7bc0\u4e2d\u4ecb\u7d39\u904e\uff0c\u9019\u88e1\u4e0d\u518d\u8d05\u8ff0\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4e00\u4e9b\u554f\u984c\u7684\u6700\u512a\u5b50\u7d50\u69cb\u4e26\u4e0d\u660e\u986f\uff0c\u4f46\u4ecd\u7136\u53ef\u4f7f\u7528\u8caa\u5a6a\u6f14\u7b97\u6cd5\u89e3\u6c7a\u3002</p> <p>\u6211\u5011\u4e3b\u8981\u63a2\u7a76\u8caa\u5a6a\u9078\u64c7\u6027\u8cea\u7684\u5224\u65b7\u65b9\u6cd5\u3002\u96d6\u7136\u5b83\u7684\u63cf\u8ff0\u770b\u4e0a\u53bb\u6bd4\u8f03\u7c21\u55ae\uff0c\u4f46\u5be6\u969b\u4e0a\u5c0d\u65bc\u8a31\u591a\u554f\u984c\uff0c\u8b49\u660e\u8caa\u5a6a\u9078\u64c7\u6027\u8cea\u4e26\u975e\u6613\u4e8b\u3002</p> <p>\u4f8b\u5982\u96f6\u9322\u514c\u63db\u554f\u984c\uff0c\u6211\u5011\u96d6\u7136\u80fd\u5920\u5bb9\u6613\u5730\u8209\u51fa\u53cd\u4f8b\uff0c\u5c0d\u8caa\u5a6a\u9078\u64c7\u6027\u8cea\u9032\u884c\u8b49\u507d\uff0c\u4f46\u8b49\u5be6\u7684\u96e3\u5ea6\u8f03\u5927\u3002\u5982\u679c\u554f\uff1a\u6eff\u8db3\u4ec0\u9ebc\u689d\u4ef6\u7684\u786c\u5e63\u7d44\u5408\u53ef\u4ee5\u4f7f\u7528\u8caa\u5a6a\u6f14\u7b97\u6cd5\u6c42\u89e3\uff1f\u6211\u5011\u5f80\u5f80\u53ea\u80fd\u6191\u85c9\u76f4\u89ba\u6216\u8209\u4f8b\u5b50\u4f86\u7d66\u51fa\u4e00\u500b\u6a21\u7a1c\u5169\u53ef\u7684\u7b54\u6848\uff0c\u800c\u96e3\u4ee5\u7d66\u51fa\u56b4\u8b39\u7684\u6578\u5b78\u8b49\u660e\u3002</p> <p>Quote</p> <p>\u6709\u4e00\u7bc7\u8ad6\u6587\u7d66\u51fa\u4e86\u4e00\u500b \\(O(n^3)\\) \u6642\u9593\u8907\u96dc\u5ea6\u7684\u6f14\u7b97\u6cd5\uff0c\u7528\u65bc\u5224\u65b7\u4e00\u500b\u786c\u5e63\u7d44\u5408\u80fd\u5426\u4f7f\u7528\u8caa\u5a6a\u6f14\u7b97\u6cd5\u627e\u51fa\u4efb\u610f\u91d1\u984d\u7684\u6700\u512a\u89e3\u3002</p> <p>Pearson, D. A polynomial-time algorithm for the change-making problem[J]. Operations Research Letters, 2005, 33(3): 231-234.</p>"},{"location":"chapter_greedy/greedy_algorithm/#1513","title":"15.1.3 \u00a0 \u8caa\u5a6a\u6f14\u7b97\u6cd5\u89e3\u984c\u6b65\u9a5f","text":"<p>\u8caa\u5a6a\u554f\u984c\u7684\u89e3\u6c7a\u6d41\u7a0b\u5927\u9ad4\u53ef\u5206\u70ba\u4ee5\u4e0b\u4e09\u6b65\u3002</p> <ol> <li>\u554f\u984c\u5206\u6790\uff1a\u68b3\u7406\u8207\u7406\u89e3\u554f\u984c\u7279\u6027\uff0c\u5305\u62ec\u72c0\u614b\u5b9a\u7fa9\u3001\u6700\u4f73\u5316\u76ee\u6a19\u548c\u7d04\u675f\u689d\u4ef6\u7b49\u3002\u9019\u4e00\u6b65\u5728\u56de\u6eaf\u548c\u52d5\u614b\u898f\u5283\u4e2d\u90fd\u6709\u6d89\u53ca\u3002</li> <li>\u78ba\u5b9a\u8caa\u5a6a\u7b56\u7565\uff1a\u78ba\u5b9a\u5982\u4f55\u5728\u6bcf\u4e00\u6b65\u4e2d\u505a\u51fa\u8caa\u5a6a\u9078\u64c7\u3002\u9019\u500b\u7b56\u7565\u80fd\u5920\u5728\u6bcf\u4e00\u6b65\u6e1b\u5c0f\u554f\u984c\u7684\u898f\u6a21\uff0c\u4e26\u6700\u7d42\u89e3\u6c7a\u6574\u500b\u554f\u984c\u3002</li> <li>\u6b63\u78ba\u6027\u8b49\u660e\uff1a\u901a\u5e38\u9700\u8981\u8b49\u660e\u554f\u984c\u5177\u6709\u8caa\u5a6a\u9078\u64c7\u6027\u8cea\u548c\u6700\u512a\u5b50\u7d50\u69cb\u3002\u9019\u500b\u6b65\u9a5f\u53ef\u80fd\u9700\u8981\u7528\u5230\u6578\u5b78\u8b49\u660e\uff0c\u4f8b\u5982\u6b78\u7d0d\u6cd5\u6216\u53cd\u8b49\u6cd5\u7b49\u3002</li> </ol> <p>\u78ba\u5b9a\u8caa\u5a6a\u7b56\u7565\u662f\u6c42\u89e3\u554f\u984c\u7684\u6838\u5fc3\u6b65\u9a5f\uff0c\u4f46\u5be6\u65bd\u8d77\u4f86\u53ef\u80fd\u4e26\u4e0d\u5bb9\u6613\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002</p> <ul> <li>\u4e0d\u540c\u554f\u984c\u7684\u8caa\u5a6a\u7b56\u7565\u7684\u5dee\u7570\u8f03\u5927\u3002\u5c0d\u65bc\u8a31\u591a\u554f\u984c\u4f86\u8aaa\uff0c\u8caa\u5a6a\u7b56\u7565\u6bd4\u8f03\u6dfa\u986f\uff0c\u6211\u5011\u900f\u904e\u4e00\u4e9b\u5927\u6982\u7684\u601d\u8003\u8207\u5617\u8a66\u5c31\u80fd\u5f97\u51fa\u3002\u800c\u5c0d\u65bc\u4e00\u4e9b\u8907\u96dc\u554f\u984c\uff0c\u8caa\u5a6a\u7b56\u7565\u53ef\u80fd\u975e\u5e38\u96b1\u853d\uff0c\u9019\u7a2e\u60c5\u6cc1\u5c31\u975e\u5e38\u8003\u9a57\u500b\u4eba\u7684\u89e3\u984c\u7d93\u9a57\u8207\u6f14\u7b97\u6cd5\u80fd\u529b\u4e86\u3002</li> <li>\u67d0\u4e9b\u8caa\u5a6a\u7b56\u7565\u5177\u6709\u8f03\u5f37\u7684\u8ff7\u60d1\u6027\u3002\u7576\u6211\u5011\u6eff\u61f7\u4fe1\u5fc3\u8a2d\u8a08\u597d\u8caa\u5a6a\u7b56\u7565\uff0c\u5beb\u51fa\u89e3\u984c\u7a0b\u5f0f\u78bc\u4e26\u63d0\u4ea4\u57f7\u884c\uff0c\u5f88\u53ef\u80fd\u767c\u73fe\u90e8\u5206\u6e2c\u8a66\u6a23\u4f8b\u7121\u6cd5\u900f\u904e\u3002\u9019\u662f\u56e0\u70ba\u8a2d\u8a08\u7684\u8caa\u5a6a\u7b56\u7565\u53ea\u662f\u201c\u90e8\u5206\u6b63\u78ba\u201d\u7684\uff0c\u4e0a\u6587\u4ecb\u7d39\u7684\u96f6\u9322\u514c\u63db\u5c31\u662f\u4e00\u500b\u5178\u578b\u6848\u4f8b\u3002</li> </ul> <p>\u70ba\u4e86\u4fdd\u8b49\u6b63\u78ba\u6027\uff0c\u6211\u5011\u61c9\u8a72\u5c0d\u8caa\u5a6a\u7b56\u7565\u9032\u884c\u56b4\u8b39\u7684\u6578\u5b78\u8b49\u660e\uff0c\u901a\u5e38\u9700\u8981\u7528\u5230\u53cd\u8b49\u6cd5\u6216\u6578\u5b78\u6b78\u7d0d\u6cd5\u3002</p> <p>\u7136\u800c\uff0c\u6b63\u78ba\u6027\u8b49\u660e\u4e5f\u5f88\u53ef\u80fd\u4e0d\u662f\u4e00\u4ef6\u6613\u4e8b\u3002\u5982\u82e5\u6c92\u6709\u982d\u7dd2\uff0c\u6211\u5011\u901a\u5e38\u6703\u9078\u64c7\u9762\u5411\u6e2c\u8a66\u7528\u4f8b\u9032\u884c\u7a0b\u5f0f\u78bc\u9664\u932f\uff0c\u4e00\u6b65\u6b65\u4fee\u6539\u8207\u9a57\u8b49\u8caa\u5a6a\u7b56\u7565\u3002</p>"},{"location":"chapter_greedy/greedy_algorithm/#1514","title":"15.1.4 \u00a0 \u8caa\u5a6a\u6f14\u7b97\u6cd5\u5178\u578b\u4f8b\u984c","text":"<p>\u8caa\u5a6a\u6f14\u7b97\u6cd5\u5e38\u5e38\u61c9\u7528\u5728\u6eff\u8db3\u8caa\u5a6a\u9078\u64c7\u6027\u8cea\u548c\u6700\u512a\u5b50\u7d50\u69cb\u7684\u6700\u4f73\u5316\u554f\u984c\u4e2d\uff0c\u4ee5\u4e0b\u5217\u8209\u4e86\u4e00\u4e9b\u5178\u578b\u7684\u8caa\u5a6a\u6f14\u7b97\u6cd5\u554f\u984c\u3002</p> <ul> <li>\u786c\u5e63\u627e\u96f6\u554f\u984c\uff1a\u5728\u67d0\u4e9b\u786c\u5e63\u7d44\u5408\u4e0b\uff0c\u8caa\u5a6a\u6f14\u7b97\u6cd5\u7e3d\u662f\u53ef\u4ee5\u5f97\u5230\u6700\u512a\u89e3\u3002</li> <li>\u5340\u9593\u6392\u7a0b\u554f\u984c\uff1a\u5047\u8a2d\u4f60\u6709\u4e00\u4e9b\u4efb\u52d9\uff0c\u6bcf\u500b\u4efb\u52d9\u5728\u4e00\u6bb5\u6642\u9593\u5167\u9032\u884c\uff0c\u4f60\u7684\u76ee\u6a19\u662f\u5b8c\u6210\u5118\u53ef\u80fd\u591a\u7684\u4efb\u52d9\u3002\u5982\u679c\u6bcf\u6b21\u90fd\u9078\u64c7\u7d50\u675f\u6642\u9593\u6700\u65e9\u7684\u4efb\u52d9\uff0c\u90a3\u9ebc\u8caa\u5a6a\u6f14\u7b97\u6cd5\u5c31\u53ef\u4ee5\u5f97\u5230\u6700\u512a\u89e3\u3002</li> <li>\u5206\u6578\u80cc\u5305\u554f\u984c\uff1a\u7d66\u5b9a\u4e00\u7d44\u7269\u54c1\u548c\u4e00\u500b\u8f09\u91cd\u91cf\uff0c\u4f60\u7684\u76ee\u6a19\u662f\u9078\u64c7\u4e00\u7d44\u7269\u54c1\uff0c\u4f7f\u5f97\u7e3d\u91cd\u91cf\u4e0d\u8d85\u904e\u8f09\u91cd\u91cf\uff0c\u4e14\u7e3d\u50f9\u503c\u6700\u5927\u3002\u5982\u679c\u6bcf\u6b21\u90fd\u9078\u64c7\u50f9\u6548\u6bd4\u6700\u9ad8\uff08\u50f9\u503c / \u91cd\u91cf\uff09\u7684\u7269\u54c1\uff0c\u90a3\u9ebc\u8caa\u5a6a\u6f14\u7b97\u6cd5\u5728\u4e00\u4e9b\u60c5\u6cc1\u4e0b\u53ef\u4ee5\u5f97\u5230\u6700\u512a\u89e3\u3002</li> <li>\u80a1\u7968\u8cb7\u8ce3\u554f\u984c\uff1a\u7d66\u5b9a\u4e00\u7d44\u80a1\u7968\u7684\u6b77\u53f2\u50f9\u683c\uff0c\u4f60\u53ef\u4ee5\u9032\u884c\u591a\u6b21\u8cb7\u8ce3\uff0c\u4f46\u5982\u679c\u4f60\u5df2\u7d93\u6301\u6709\u80a1\u7968\uff0c\u90a3\u9ebc\u5728\u8ce3\u51fa\u4e4b\u524d\u4e0d\u80fd\u518d\u8cb7\uff0c\u76ee\u6a19\u662f\u7372\u53d6\u6700\u5927\u5229\u6f64\u3002</li> <li>\u970d\u592b\u66fc\u7de8\u78bc\uff1a\u970d\u592b\u66fc\u7de8\u78bc\u662f\u4e00\u7a2e\u7528\u65bc\u7121\u640d\u8cc7\u6599\u58d3\u7e2e\u7684\u8caa\u5a6a\u6f14\u7b97\u6cd5\u3002\u900f\u904e\u69cb\u5efa\u970d\u592b\u66fc\u6a39\uff0c\u6bcf\u6b21\u9078\u64c7\u51fa\u73fe\u983b\u7387\u6700\u4f4e\u7684\u5169\u500b\u7bc0\u9ede\u5408\u4f75\uff0c\u6700\u5f8c\u5f97\u5230\u7684\u970d\u592b\u66fc\u6a39\u7684\u5e36\u6b0a\u8def\u5f91\u9577\u5ea6\uff08\u7de8\u78bc\u9577\u5ea6\uff09\u6700\u5c0f\u3002</li> <li>Dijkstra \u6f14\u7b97\u6cd5\uff1a\u5b83\u662f\u4e00\u7a2e\u89e3\u6c7a\u7d66\u5b9a\u6e90\u9802\u9ede\u5230\u5176\u9918\u5404\u9802\u9ede\u7684\u6700\u77ed\u8def\u5f91\u554f\u984c\u7684\u8caa\u5a6a\u6f14\u7b97\u6cd5\u3002</li> </ul>"},{"location":"chapter_greedy/max_capacity_problem/","title":"15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u554f\u984c","text":"<p>Question</p> <p>\u8f38\u5165\u4e00\u500b\u9663\u5217 \\(ht\\) \uff0c\u5176\u4e2d\u7684\u6bcf\u500b\u5143\u7d20\u4ee3\u8868\u4e00\u500b\u5782\u76f4\u9694\u677f\u7684\u9ad8\u5ea6\u3002\u9663\u5217\u4e2d\u7684\u4efb\u610f\u5169\u500b\u9694\u677f\uff0c\u4ee5\u53ca\u5b83\u5011\u4e4b\u9593\u7684\u7a7a\u9593\u53ef\u4ee5\u7d44\u6210\u4e00\u500b\u5bb9\u5668\u3002</p> <p>\u5bb9\u5668\u7684\u5bb9\u91cf\u7b49\u65bc\u9ad8\u5ea6\u548c\u5bec\u5ea6\u7684\u4e58\u7a4d\uff08\u9762\u7a4d\uff09\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u8f03\u77ed\u7684\u9694\u677f\u6c7a\u5b9a\uff0c\u5bec\u5ea6\u662f\u5169\u500b\u9694\u677f\u7684\u9663\u5217\u7d22\u5f15\u4e4b\u5dee\u3002</p> <p>\u8acb\u5728\u9663\u5217\u4e2d\u9078\u64c7\u5169\u500b\u9694\u677f\uff0c\u4f7f\u5f97\u7d44\u6210\u7684\u5bb9\u5668\u7684\u5bb9\u91cf\u6700\u5927\uff0c\u8fd4\u56de\u6700\u5927\u5bb9\u91cf\u3002\u793a\u4f8b\u5982\u5716 15-7 \u6240\u793a\u3002</p> <p></p> <p> \u5716 15-7 \u00a0 \u6700\u5927\u5bb9\u91cf\u554f\u984c\u7684\u793a\u4f8b\u8cc7\u6599 </p> <p>\u5bb9\u5668\u7531\u4efb\u610f\u5169\u500b\u9694\u677f\u570d\u6210\uff0c\u56e0\u6b64\u672c\u984c\u7684\u72c0\u614b\u70ba\u5169\u500b\u9694\u677f\u7684\u7d22\u5f15\uff0c\u8a18\u70ba \\([i, j]\\) \u3002</p> <p>\u6839\u64da\u984c\u610f\uff0c\u5bb9\u91cf\u7b49\u65bc\u9ad8\u5ea6\u4e58\u4ee5\u5bec\u5ea6\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u77ed\u677f\u6c7a\u5b9a\uff0c\u5bec\u5ea6\u662f\u5169\u9694\u677f\u7684\u9663\u5217\u7d22\u5f15\u4e4b\u5dee\u3002\u8a2d\u5bb9\u91cf\u70ba \\(cap[i, j]\\) \uff0c\u5247\u53ef\u5f97\u8a08\u7b97\u516c\u5f0f\uff1a</p> \\[ cap[i, j] = \\min(ht[i], ht[j]) \\times (j - i) \\] <p>\u8a2d\u9663\u5217\u9577\u5ea6\u70ba \\(n\\) \uff0c\u5169\u500b\u9694\u677f\u7684\u7d44\u5408\u6578\u91cf\uff08\u72c0\u614b\u7e3d\u6578\uff09\u70ba \\(C_n^2 = \\frac{n(n - 1)}{2}\\) \u500b\u3002\u6700\u76f4\u63a5\u5730\uff0c\u6211\u5011\u53ef\u4ee5\u7aae\u8209\u6240\u6709\u72c0\u614b\uff0c\u5f9e\u800c\u6c42\u5f97\u6700\u5927\u5bb9\u91cf\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\) \u3002</p>"},{"location":"chapter_greedy/max_capacity_problem/#1","title":"1. \u00a0 \u8caa\u5a6a\u7b56\u7565\u78ba\u5b9a","text":"<p>\u9019\u9053\u984c\u9084\u6709\u66f4\u9ad8\u6548\u7387\u7684\u89e3\u6cd5\u3002\u5982\u5716 15-8 \u6240\u793a\uff0c\u73fe\u9078\u53d6\u4e00\u500b\u72c0\u614b \\([i, j]\\) \uff0c\u5176\u6eff\u8db3\u7d22\u5f15 \\(i &lt; j\\) \u4e14\u9ad8\u5ea6 \\(ht[i] &lt; ht[j]\\) \uff0c\u5373 \\(i\\) \u70ba\u77ed\u677f\u3001\\(j\\) \u70ba\u9577\u677f\u3002</p> <p></p> <p> \u5716 15-8 \u00a0 \u521d\u59cb\u72c0\u614b </p> <p>\u5982\u5716 15-9 \u6240\u793a\uff0c\u82e5\u6b64\u6642\u5c07\u9577\u677f \\(j\\) \u5411\u77ed\u677f \\(i\\) \u9760\u8fd1\uff0c\u5247\u5bb9\u91cf\u4e00\u5b9a\u8b8a\u5c0f\u3002</p> <p>\u9019\u662f\u56e0\u70ba\u5728\u79fb\u52d5\u9577\u677f \\(j\\) \u5f8c\uff0c\u5bec\u5ea6 \\(j-i\\) \u80af\u5b9a\u8b8a\u5c0f\uff1b\u800c\u9ad8\u5ea6\u7531\u77ed\u677f\u6c7a\u5b9a\uff0c\u56e0\u6b64\u9ad8\u5ea6\u53ea\u53ef\u80fd\u4e0d\u8b8a\uff08 \\(i\\) \u4ecd\u70ba\u77ed\u677f\uff09\u6216\u8b8a\u5c0f\uff08\u79fb\u52d5\u5f8c\u7684 \\(j\\) \u6210\u70ba\u77ed\u677f\uff09\u3002</p> <p></p> <p> \u5716 15-9 \u00a0 \u5411\u5167\u79fb\u52d5\u9577\u677f\u5f8c\u7684\u72c0\u614b </p> <p>\u53cd\u5411\u601d\u8003\uff0c\u6211\u5011\u53ea\u6709\u5411\u5167\u6536\u7e2e\u77ed\u677f \\(i\\) \uff0c\u624d\u6709\u53ef\u80fd\u4f7f\u5bb9\u91cf\u8b8a\u5927\u3002\u56e0\u70ba\u96d6\u7136\u5bec\u5ea6\u4e00\u5b9a\u8b8a\u5c0f\uff0c\u4f46\u9ad8\u5ea6\u53ef\u80fd\u6703\u8b8a\u5927\uff08\u79fb\u52d5\u5f8c\u7684\u77ed\u677f \\(i\\) \u53ef\u80fd\u6703\u8b8a\u9577\uff09\u3002\u4f8b\u5982\u5728\u5716 15-10 \u4e2d\uff0c\u79fb\u52d5\u77ed\u677f\u5f8c\u9762\u7a4d\u8b8a\u5927\u3002</p> <p></p> <p> \u5716 15-10 \u00a0 \u5411\u5167\u79fb\u52d5\u77ed\u677f\u5f8c\u7684\u72c0\u614b </p> <p>\u7531\u6b64\u4fbf\u53ef\u63a8\u51fa\u672c\u984c\u7684\u8caa\u5a6a\u7b56\u7565\uff1a\u521d\u59cb\u5316\u5169\u6307\u6a19\uff0c\u4f7f\u5176\u5206\u5217\u5bb9\u5668\u5169\u7aef\uff0c\u6bcf\u8f2a\u5411\u5167\u6536\u7e2e\u77ed\u677f\u5c0d\u61c9\u7684\u6307\u6a19\uff0c\u76f4\u81f3\u5169\u6307\u6a19\u76f8\u9047\u3002</p> <p>\u5716 15-11 \u5c55\u793a\u4e86\u8caa\u5a6a\u7b56\u7565\u7684\u57f7\u884c\u904e\u7a0b\u3002</p> <ol> <li>\u521d\u59cb\u72c0\u614b\u4e0b\uff0c\u6307\u6a19 \\(i\\) \u548c \\(j\\) \u5206\u5217\u9663\u5217\u5169\u7aef\u3002</li> <li>\u8a08\u7b97\u7576\u524d\u72c0\u614b\u7684\u5bb9\u91cf \\(cap[i, j]\\) \uff0c\u4e26\u66f4\u65b0\u6700\u5927\u5bb9\u91cf\u3002</li> <li>\u6bd4\u8f03\u677f \\(i\\) \u548c \u677f \\(j\\) \u7684\u9ad8\u5ea6\uff0c\u4e26\u5c07\u77ed\u677f\u5411\u5167\u79fb\u52d5\u4e00\u683c\u3002</li> <li>\u8ff4\u5708\u57f7\u884c\u7b2c <code>2.</code> \u6b65\u548c\u7b2c <code>3.</code> \u6b65\uff0c\u76f4\u81f3 \\(i\\) \u548c \\(j\\) \u76f8\u9047\u6642\u7d50\u675f\u3002</li> </ol> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 15-11 \u00a0 \u6700\u5927\u5bb9\u91cf\u554f\u984c\u7684\u8caa\u5a6a\u904e\u7a0b </p>"},{"location":"chapter_greedy/max_capacity_problem/#2","title":"2. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"<p>\u7a0b\u5f0f\u78bc\u8ff4\u5708\u6700\u591a \\(n\\) \u8f2a\uff0c\u56e0\u6b64\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \u3002</p> <p>\u8b8a\u6578 \\(i\\)\u3001\\(j\\)\u3001\\(res\\) \u4f7f\u7528\u5e38\u6578\u5927\u5c0f\u7684\u984d\u5916\u7a7a\u9593\uff0c\u56e0\u6b64\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(1)\\) \u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig max_capacity.py<pre><code>def max_capacity(ht: list[int]) -&gt; int:\n \"\"\"\u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u5a6a\"\"\"\n # \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u9663\u5217\u5169\u7aef\n i, j = 0, len(ht) - 1\n # \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u70ba 0\n res = 0\n # \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u81f3\u5169\u677f\u76f8\u9047\n while i &lt; j:\n # \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n cap = min(ht[i], ht[j]) * (j - i)\n res = max(res, cap)\n # \u5411\u5167\u79fb\u52d5\u77ed\u677f\n if ht[i] &lt; ht[j]:\n i += 1\n else:\n j -= 1\n return res\n</code></pre> max_capacity.cpp<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u5a6a */\nint maxCapacity(vector&lt;int&gt; &amp;ht) {\n // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u9663\u5217\u5169\u7aef\n int i = 0, j = ht.size() - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u70ba 0\n int res = 0;\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u81f3\u5169\u677f\u76f8\u9047\n while (i &lt; j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int cap = min(ht[i], ht[j]) * (j - i);\n res = max(res, cap);\n // \u5411\u5167\u79fb\u52d5\u77ed\u677f\n if (ht[i] &lt; ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n</code></pre> max_capacity.java<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u5a6a */\nint maxCapacity(int[] ht) {\n // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u9663\u5217\u5169\u7aef\n int i = 0, j = ht.length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u70ba 0\n int res = 0;\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u81f3\u5169\u677f\u76f8\u9047\n while (i &lt; j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int cap = Math.min(ht[i], ht[j]) * (j - i);\n res = Math.max(res, cap);\n // \u5411\u5167\u79fb\u52d5\u77ed\u677f\n if (ht[i] &lt; ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n</code></pre> max_capacity.cs<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u5a6a */\nint MaxCapacity(int[] ht) {\n // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u9663\u5217\u5169\u7aef\n int i = 0, j = ht.Length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u70ba 0\n int res = 0;\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u81f3\u5169\u677f\u76f8\u9047\n while (i &lt; j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int cap = Math.Min(ht[i], ht[j]) * (j - i);\n res = Math.Max(res, cap);\n // \u5411\u5167\u79fb\u52d5\u77ed\u677f\n if (ht[i] &lt; ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n</code></pre> max_capacity.go<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u5a6a */\nfunc maxCapacity(ht []int) int {\n // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u9663\u5217\u5169\u7aef\n i, j := 0, len(ht)-1\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u70ba 0\n res := 0\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u81f3\u5169\u677f\u76f8\u9047\n for i &lt; j {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n capacity := int(math.Min(float64(ht[i]), float64(ht[j]))) * (j - i)\n res = int(math.Max(float64(res), float64(capacity)))\n // \u5411\u5167\u79fb\u52d5\u77ed\u677f\n if ht[i] &lt; ht[j] {\n i++\n } else {\n j--\n }\n }\n return res\n}\n</code></pre> max_capacity.swift<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u5a6a */\nfunc maxCapacity(ht: [Int]) -&gt; Int {\n // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u9663\u5217\u5169\u7aef\n var i = ht.startIndex, j = ht.endIndex - 1\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u70ba 0\n var res = 0\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u81f3\u5169\u677f\u76f8\u9047\n while i &lt; j {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n let cap = min(ht[i], ht[j]) * (j - i)\n res = max(res, cap)\n // \u5411\u5167\u79fb\u52d5\u77ed\u677f\n if ht[i] &lt; ht[j] {\n i += 1\n } else {\n j -= 1\n }\n }\n return res\n}\n</code></pre> max_capacity.js<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u5a6a */\nfunction maxCapacity(ht) {\n // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u9663\u5217\u5169\u7aef\n let i = 0,\n j = ht.length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u70ba 0\n let res = 0;\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u81f3\u5169\u677f\u76f8\u9047\n while (i &lt; j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n const cap = Math.min(ht[i], ht[j]) * (j - i);\n res = Math.max(res, cap);\n // \u5411\u5167\u79fb\u52d5\u77ed\u677f\n if (ht[i] &lt; ht[j]) {\n i += 1;\n } else {\n j -= 1;\n }\n }\n return res;\n}\n</code></pre> max_capacity.ts<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u5a6a */\nfunction maxCapacity(ht: number[]): number {\n // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u9663\u5217\u5169\u7aef\n let i = 0,\n j = ht.length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u70ba 0\n let res = 0;\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u81f3\u5169\u677f\u76f8\u9047\n while (i &lt; j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n const cap: number = Math.min(ht[i], ht[j]) * (j - i);\n res = Math.max(res, cap);\n // \u5411\u5167\u79fb\u52d5\u77ed\u677f\n if (ht[i] &lt; ht[j]) {\n i += 1;\n } else {\n j -= 1;\n }\n }\n return res;\n}\n</code></pre> max_capacity.dart<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u5a6a */\nint maxCapacity(List&lt;int&gt; ht) {\n // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u9663\u5217\u5169\u7aef\n int i = 0, j = ht.length - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u70ba 0\n int res = 0;\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u81f3\u5169\u677f\u76f8\u9047\n while (i &lt; j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int cap = min(ht[i], ht[j]) * (j - i);\n res = max(res, cap);\n // \u5411\u5167\u79fb\u52d5\u77ed\u677f\n if (ht[i] &lt; ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n</code></pre> max_capacity.rs<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u5a6a */\nfn max_capacity(ht: &amp;[i32]) -&gt; i32 {\n // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u9663\u5217\u5169\u7aef\n let mut i = 0;\n let mut j = ht.len() - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u70ba 0\n let mut res = 0;\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u81f3\u5169\u677f\u76f8\u9047\n while i &lt; j {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n let cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;\n res = std::cmp::max(res, cap);\n // \u5411\u5167\u79fb\u52d5\u77ed\u677f\n if ht[i] &lt; ht[j] {\n i += 1;\n } else {\n j -= 1;\n }\n }\n res\n}\n</code></pre> max_capacity.c<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u5a6a */\nint maxCapacity(int ht[], int htLength) {\n // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u9663\u5217\u5169\u7aef\n int i = 0;\n int j = htLength - 1;\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u70ba 0\n int res = 0;\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u81f3\u5169\u677f\u76f8\u9047\n while (i &lt; j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n int capacity = myMin(ht[i], ht[j]) * (j - i);\n res = myMax(res, capacity);\n // \u5411\u5167\u79fb\u52d5\u77ed\u677f\n if (ht[i] &lt; ht[j]) {\n i++;\n } else {\n j--;\n }\n }\n return res;\n}\n</code></pre> max_capacity.kt<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8caa\u5a6a */\nfun maxCapacity(ht: IntArray): Int {\n // \u521d\u59cb\u5316 i, j\uff0c\u4f7f\u5176\u5206\u5217\u9663\u5217\u5169\u7aef\n var i = 0\n var j = ht.size - 1\n // \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u70ba 0\n var res = 0\n // \u8ff4\u5708\u8caa\u5a6a\u9078\u64c7\uff0c\u76f4\u81f3\u5169\u677f\u76f8\u9047\n while (i &lt; j) {\n // \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\n val cap = min(ht[i], ht[j]) * (j - i)\n res = max(res, cap)\n // \u5411\u5167\u79fb\u52d5\u77ed\u677f\n if (ht[i] &lt; ht[j]) {\n i++\n } else {\n j--\n }\n }\n return res\n}\n</code></pre> max_capacity.rb<pre><code>[class]{}-[func]{max_capacity}\n</code></pre> max_capacity.zig<pre><code>[class]{}-[func]{maxCapacity}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_greedy/max_capacity_problem/#3","title":"3. \u00a0 \u6b63\u78ba\u6027\u8b49\u660e","text":"<p>\u4e4b\u6240\u4ee5\u8caa\u5a6a\u6bd4\u7aae\u8209\u66f4\u5feb\uff0c\u662f\u56e0\u70ba\u6bcf\u8f2a\u7684\u8caa\u5a6a\u9078\u64c7\u90fd\u6703\u201c\u8df3\u904e\u201d\u4e00\u4e9b\u72c0\u614b\u3002</p> <p>\u6bd4\u5982\u5728\u72c0\u614b \\(cap[i, j]\\) \u4e0b\uff0c\\(i\\) \u70ba\u77ed\u677f\u3001\\(j\\) \u70ba\u9577\u677f\u3002\u82e5\u8caa\u5a6a\u5730\u5c07\u77ed\u677f \\(i\\) \u5411\u5167\u79fb\u52d5\u4e00\u683c\uff0c\u6703\u5c0e\u81f4\u5716 15-12 \u6240\u793a\u7684\u72c0\u614b\u88ab\u201c\u8df3\u904e\u201d\u3002\u9019\u610f\u5473\u8457\u4e4b\u5f8c\u7121\u6cd5\u9a57\u8b49\u9019\u4e9b\u72c0\u614b\u7684\u5bb9\u91cf\u5927\u5c0f\u3002</p> \\[ cap[i, i+1], cap[i, i+2], \\dots, cap[i, j-2], cap[i, j-1] \\] <p></p> <p> \u5716 15-12 \u00a0 \u79fb\u52d5\u77ed\u677f\u5c0e\u81f4\u88ab\u8df3\u904e\u7684\u72c0\u614b </p> <p>\u89c0\u5bdf\u767c\u73fe\uff0c\u9019\u4e9b\u88ab\u8df3\u904e\u7684\u72c0\u614b\u5be6\u969b\u4e0a\u5c31\u662f\u5c07\u9577\u677f \\(j\\) \u5411\u5167\u79fb\u52d5\u7684\u6240\u6709\u72c0\u614b\u3002\u524d\u9762\u6211\u5011\u5df2\u7d93\u8b49\u660e\u5167\u79fb\u9577\u677f\u4e00\u5b9a\u6703\u5c0e\u81f4\u5bb9\u91cf\u8b8a\u5c0f\u3002\u4e5f\u5c31\u662f\u8aaa\uff0c\u88ab\u8df3\u904e\u7684\u72c0\u614b\u90fd\u4e0d\u53ef\u80fd\u662f\u6700\u512a\u89e3\uff0c\u8df3\u904e\u5b83\u5011\u4e0d\u6703\u5c0e\u81f4\u932f\u904e\u6700\u512a\u89e3\u3002</p> <p>\u4ee5\u4e0a\u5206\u6790\u8aaa\u660e\uff0c\u79fb\u52d5\u77ed\u677f\u7684\u64cd\u4f5c\u662f\u201c\u5b89\u5168\u201d\u7684\uff0c\u8caa\u5a6a\u7b56\u7565\u662f\u6709\u6548\u7684\u3002</p>"},{"location":"chapter_greedy/max_product_cutting_problem/","title":"15.4 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u7a4d\u554f\u984c","text":"<p>Question</p> <p>\u7d66\u5b9a\u4e00\u500b\u6b63\u6574\u6578 \\(n\\) \uff0c\u5c07\u5176\u5207\u5206\u70ba\u81f3\u5c11\u5169\u500b\u6b63\u6574\u6578\u7684\u548c\uff0c\u6c42\u5207\u5206\u5f8c\u6240\u6709\u6574\u6578\u7684\u4e58\u7a4d\u6700\u5927\u662f\u591a\u5c11\uff0c\u5982\u5716 15-13 \u6240\u793a\u3002</p> <p></p> <p> \u5716 15-13 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u7a4d\u7684\u554f\u984c\u5b9a\u7fa9 </p> <p>\u5047\u8a2d\u6211\u5011\u5c07 \\(n\\) \u5207\u5206\u70ba \\(m\\) \u500b\u6574\u6578\u56e0\u5b50\uff0c\u5176\u4e2d\u7b2c \\(i\\) \u500b\u56e0\u5b50\u8a18\u70ba \\(n_i\\) \uff0c\u5373</p> \\[ n = \\sum_{i=1}^{m}n_i \\] <p>\u672c\u984c\u7684\u76ee\u6a19\u662f\u6c42\u5f97\u6240\u6709\u6574\u6578\u56e0\u5b50\u7684\u6700\u5927\u4e58\u7a4d\uff0c\u5373</p> \\[ \\max(\\prod_{i=1}^{m}n_i) \\] <p>\u6211\u5011\u9700\u8981\u601d\u8003\u7684\u662f\uff1a\u5207\u5206\u6578\u91cf \\(m\\) \u61c9\u8a72\u591a\u5927\uff0c\u6bcf\u500b \\(n_i\\) \u61c9\u8a72\u662f\u591a\u5c11\uff1f</p>"},{"location":"chapter_greedy/max_product_cutting_problem/#1","title":"1. \u00a0 \u8caa\u5a6a\u7b56\u7565\u78ba\u5b9a","text":"<p>\u6839\u64da\u7d93\u9a57\uff0c\u5169\u500b\u6574\u6578\u7684\u4e58\u7a4d\u5f80\u5f80\u6bd4\u5b83\u5011\u7684\u52a0\u548c\u66f4\u5927\u3002\u5047\u8a2d\u5f9e \\(n\\) \u4e2d\u5206\u51fa\u4e00\u500b\u56e0\u5b50 \\(2\\) \uff0c\u5247\u5b83\u5011\u7684\u4e58\u7a4d\u70ba \\(2(n-2)\\) \u3002\u6211\u5011\u5c07\u8a72\u4e58\u7a4d\u8207 \\(n\\) \u4f5c\u6bd4\u8f03\uff1a</p> \\[ \\begin{aligned} 2(n-2) &amp; \\geq n \\newline 2n - n - 4 &amp; \\geq 0 \\newline n &amp; \\geq 4 \\end{aligned} \\] <p>\u5982\u5716 15-14 \u6240\u793a\uff0c\u7576 \\(n \\geq 4\\) \u6642\uff0c\u5207\u5206\u51fa\u4e00\u500b \\(2\\) \u5f8c\u4e58\u7a4d\u6703\u8b8a\u5927\uff0c\u9019\u8aaa\u660e\u5927\u65bc\u7b49\u65bc \\(4\\) \u7684\u6574\u6578\u90fd\u61c9\u8a72\u88ab\u5207\u5206\u3002</p> <p>\u8caa\u5a6a\u7b56\u7565\u4e00\uff1a\u5982\u679c\u5207\u5206\u65b9\u6848\u4e2d\u5305\u542b \\(\\geq 4\\) \u7684\u56e0\u5b50\uff0c\u90a3\u9ebc\u5b83\u5c31\u61c9\u8a72\u88ab\u7e7c\u7e8c\u5207\u5206\u3002\u6700\u7d42\u7684\u5207\u5206\u65b9\u6848\u53ea\u61c9\u51fa\u73fe \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u9019\u4e09\u7a2e\u56e0\u5b50\u3002</p> <p></p> <p> \u5716 15-14 \u00a0 \u5207\u5206\u5c0e\u81f4\u4e58\u7a4d\u8b8a\u5927 </p> <p>\u63a5\u4e0b\u4f86\u601d\u8003\u54ea\u500b\u56e0\u5b50\u662f\u6700\u512a\u7684\u3002\u5728 \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u9019\u4e09\u500b\u56e0\u5b50\u4e2d\uff0c\u986f\u7136 \\(1\\) \u662f\u6700\u5dee\u7684\uff0c\u56e0\u70ba \\(1 \\times (n-1) &lt; n\\) \u6046\u6210\u7acb\uff0c\u5373\u5207\u5206\u51fa \\(1\\) \u53cd\u800c\u6703\u5c0e\u81f4\u4e58\u7a4d\u6e1b\u5c0f\u3002</p> <p>\u5982\u5716 15-15 \u6240\u793a\uff0c\u7576 \\(n = 6\\) \u6642\uff0c\u6709 \\(3 \\times 3 &gt; 2 \\times 2 \\times 2\\) \u3002\u9019\u610f\u5473\u8457\u5207\u5206\u51fa \\(3\\) \u6bd4\u5207\u5206\u51fa \\(2\\) \u66f4\u512a\u3002</p> <p>\u8caa\u5a6a\u7b56\u7565\u4e8c\uff1a\u5728\u5207\u5206\u65b9\u6848\u4e2d\uff0c\u6700\u591a\u96bb\u61c9\u5b58\u5728\u5169\u500b \\(2\\) \u3002\u56e0\u70ba\u4e09\u500b \\(2\\) \u7e3d\u662f\u53ef\u4ee5\u66ff\u63db\u70ba\u5169\u500b \\(3\\) \uff0c\u5f9e\u800c\u7372\u5f97\u66f4\u5927\u7684\u4e58\u7a4d\u3002</p> <p></p> <p> \u5716 15-15 \u00a0 \u6700\u512a\u5207\u5206\u56e0\u5b50 </p> <p>\u7d9c\u4e0a\u6240\u8ff0\uff0c\u53ef\u63a8\u7406\u51fa\u4ee5\u4e0b\u8caa\u5a6a\u7b56\u7565\u3002</p> <ol> <li>\u8f38\u5165\u6574\u6578 \\(n\\) \uff0c\u5f9e\u5176\u4e0d\u65b7\u5730\u5207\u5206\u51fa\u56e0\u5b50 \\(3\\) \uff0c\u76f4\u81f3\u9918\u6578\u70ba \\(0\\)\u3001\\(1\\)\u3001\\(2\\) \u3002</li> <li>\u7576\u9918\u6578\u70ba \\(0\\) \u6642\uff0c\u4ee3\u8868 \\(n\\) \u662f \\(3\\) \u7684\u500d\u6578\uff0c\u56e0\u6b64\u4e0d\u505a\u4efb\u4f55\u8655\u7406\u3002</li> <li>\u7576\u9918\u6578\u70ba \\(2\\) \u6642\uff0c\u4e0d\u7e7c\u7e8c\u5283\u5206\uff0c\u4fdd\u7559\u3002</li> <li>\u7576\u9918\u6578\u70ba \\(1\\) \u6642\uff0c\u7531\u65bc \\(2 \\times 2 &gt; 1 \\times 3\\) \uff0c\u56e0\u6b64\u61c9\u5c07\u6700\u5f8c\u4e00\u500b \\(3\\) \u66ff\u63db\u70ba \\(2\\) \u3002</li> </ol>"},{"location":"chapter_greedy/max_product_cutting_problem/#2","title":"2. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"<p>\u5982\u5716 15-16 \u6240\u793a\uff0c\u6211\u5011\u7121\u9808\u900f\u904e\u8ff4\u5708\u4f86\u5207\u5206\u6574\u6578\uff0c\u800c\u53ef\u4ee5\u5229\u7528\u5411\u4e0b\u6574\u9664\u904b\u7b97\u5f97\u5230 \\(3\\) \u7684\u500b\u6578 \\(a\\) \uff0c\u7528\u53d6\u6a21\u904b\u7b97\u5f97\u5230\u9918\u6578 \\(b\\) \uff0c\u6b64\u6642\u6709\uff1a</p> \\[ n = 3 a + b \\] <p>\u8acb\u6ce8\u610f\uff0c\u5c0d\u65bc \\(n \\leq 3\\) \u7684\u908a\u754c\u60c5\u6cc1\uff0c\u5fc5\u9808\u62c6\u5206\u51fa\u4e00\u500b \\(1\\) \uff0c\u4e58\u7a4d\u70ba \\(1 \\times (n - 1)\\) \u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig max_product_cutting.py<pre><code>def max_product_cutting(n: int) -&gt; int:\n \"\"\"\u6700\u5927\u5207\u5206\u4e58\u7a4d\uff1a\u8caa\u5a6a\"\"\"\n # \u7576 n &lt;= 3 \u6642\uff0c\u5fc5\u9808\u5207\u5206\u51fa\u4e00\u500b 1\n if n &lt;= 3:\n return 1 * (n - 1)\n # \u8caa\u5a6a\u5730\u5207\u5206\u51fa 3 \uff0ca \u70ba 3 \u7684\u500b\u6578\uff0cb \u70ba\u9918\u6578\n a, b = n // 3, n % 3\n if b == 1:\n # \u7576\u9918\u6578\u70ba 1 \u6642\uff0c\u5c07\u4e00\u5c0d 1 * 3 \u8f49\u5316\u70ba 2 * 2\n return int(math.pow(3, a - 1)) * 2 * 2\n if b == 2:\n # \u7576\u9918\u6578\u70ba 2 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return int(math.pow(3, a)) * 2\n # \u7576\u9918\u6578\u70ba 0 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return int(math.pow(3, a))\n</code></pre> max_product_cutting.cpp<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u7a4d\uff1a\u8caa\u5a6a */\nint maxProductCutting(int n) {\n // \u7576 n &lt;= 3 \u6642\uff0c\u5fc5\u9808\u5207\u5206\u51fa\u4e00\u500b 1\n if (n &lt;= 3) {\n return 1 * (n - 1);\n }\n // \u8caa\u5a6a\u5730\u5207\u5206\u51fa 3 \uff0ca \u70ba 3 \u7684\u500b\u6578\uff0cb \u70ba\u9918\u6578\n int a = n / 3;\n int b = n % 3;\n if (b == 1) {\n // \u7576\u9918\u6578\u70ba 1 \u6642\uff0c\u5c07\u4e00\u5c0d 1 * 3 \u8f49\u5316\u70ba 2 * 2\n return (int)pow(3, a - 1) * 2 * 2;\n }\n if (b == 2) {\n // \u7576\u9918\u6578\u70ba 2 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return (int)pow(3, a) * 2;\n }\n // \u7576\u9918\u6578\u70ba 0 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return (int)pow(3, a);\n}\n</code></pre> max_product_cutting.java<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u7a4d\uff1a\u8caa\u5a6a */\nint maxProductCutting(int n) {\n // \u7576 n &lt;= 3 \u6642\uff0c\u5fc5\u9808\u5207\u5206\u51fa\u4e00\u500b 1\n if (n &lt;= 3) {\n return 1 * (n - 1);\n }\n // \u8caa\u5a6a\u5730\u5207\u5206\u51fa 3 \uff0ca \u70ba 3 \u7684\u500b\u6578\uff0cb \u70ba\u9918\u6578\n int a = n / 3;\n int b = n % 3;\n if (b == 1) {\n // \u7576\u9918\u6578\u70ba 1 \u6642\uff0c\u5c07\u4e00\u5c0d 1 * 3 \u8f49\u5316\u70ba 2 * 2\n return (int) Math.pow(3, a - 1) * 2 * 2;\n }\n if (b == 2) {\n // \u7576\u9918\u6578\u70ba 2 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return (int) Math.pow(3, a) * 2;\n }\n // \u7576\u9918\u6578\u70ba 0 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return (int) Math.pow(3, a);\n}\n</code></pre> max_product_cutting.cs<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u7a4d\uff1a\u8caa\u5a6a */\nint MaxProductCutting(int n) {\n // \u7576 n &lt;= 3 \u6642\uff0c\u5fc5\u9808\u5207\u5206\u51fa\u4e00\u500b 1\n if (n &lt;= 3) {\n return 1 * (n - 1);\n }\n // \u8caa\u5a6a\u5730\u5207\u5206\u51fa 3 \uff0ca \u70ba 3 \u7684\u500b\u6578\uff0cb \u70ba\u9918\u6578\n int a = n / 3;\n int b = n % 3;\n if (b == 1) {\n // \u7576\u9918\u6578\u70ba 1 \u6642\uff0c\u5c07\u4e00\u5c0d 1 * 3 \u8f49\u5316\u70ba 2 * 2\n return (int)Math.Pow(3, a - 1) * 2 * 2;\n }\n if (b == 2) {\n // \u7576\u9918\u6578\u70ba 2 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return (int)Math.Pow(3, a) * 2;\n }\n // \u7576\u9918\u6578\u70ba 0 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return (int)Math.Pow(3, a);\n}\n</code></pre> max_product_cutting.go<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u7a4d\uff1a\u8caa\u5a6a */\nfunc maxProductCutting(n int) int {\n // \u7576 n &lt;= 3 \u6642\uff0c\u5fc5\u9808\u5207\u5206\u51fa\u4e00\u500b 1\n if n &lt;= 3 {\n return 1 * (n - 1)\n }\n // \u8caa\u5a6a\u5730\u5207\u5206\u51fa 3 \uff0ca \u70ba 3 \u7684\u500b\u6578\uff0cb \u70ba\u9918\u6578\n a := n / 3\n b := n % 3\n if b == 1 {\n // \u7576\u9918\u6578\u70ba 1 \u6642\uff0c\u5c07\u4e00\u5c0d 1 * 3 \u8f49\u5316\u70ba 2 * 2\n return int(math.Pow(3, float64(a-1))) * 2 * 2\n }\n if b == 2 {\n // \u7576\u9918\u6578\u70ba 2 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return int(math.Pow(3, float64(a))) * 2\n }\n // \u7576\u9918\u6578\u70ba 0 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return int(math.Pow(3, float64(a)))\n}\n</code></pre> max_product_cutting.swift<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u7a4d\uff1a\u8caa\u5a6a */\nfunc maxProductCutting(n: Int) -&gt; Int {\n // \u7576 n &lt;= 3 \u6642\uff0c\u5fc5\u9808\u5207\u5206\u51fa\u4e00\u500b 1\n if n &lt;= 3 {\n return 1 * (n - 1)\n }\n // \u8caa\u5a6a\u5730\u5207\u5206\u51fa 3 \uff0ca \u70ba 3 \u7684\u500b\u6578\uff0cb \u70ba\u9918\u6578\n let a = n / 3\n let b = n % 3\n if b == 1 {\n // \u7576\u9918\u6578\u70ba 1 \u6642\uff0c\u5c07\u4e00\u5c0d 1 * 3 \u8f49\u5316\u70ba 2 * 2\n return pow(3, a - 1) * 2 * 2\n }\n if b == 2 {\n // \u7576\u9918\u6578\u70ba 2 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return pow(3, a) * 2\n }\n // \u7576\u9918\u6578\u70ba 0 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return pow(3, a)\n}\n</code></pre> max_product_cutting.js<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u7a4d\uff1a\u8caa\u5a6a */\nfunction maxProductCutting(n) {\n // \u7576 n &lt;= 3 \u6642\uff0c\u5fc5\u9808\u5207\u5206\u51fa\u4e00\u500b 1\n if (n &lt;= 3) {\n return 1 * (n - 1);\n }\n // \u8caa\u5a6a\u5730\u5207\u5206\u51fa 3 \uff0ca \u70ba 3 \u7684\u500b\u6578\uff0cb \u70ba\u9918\u6578\n let a = Math.floor(n / 3);\n let b = n % 3;\n if (b === 1) {\n // \u7576\u9918\u6578\u70ba 1 \u6642\uff0c\u5c07\u4e00\u5c0d 1 * 3 \u8f49\u5316\u70ba 2 * 2\n return Math.pow(3, a - 1) * 2 * 2;\n }\n if (b === 2) {\n // \u7576\u9918\u6578\u70ba 2 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return Math.pow(3, a) * 2;\n }\n // \u7576\u9918\u6578\u70ba 0 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return Math.pow(3, a);\n}\n</code></pre> max_product_cutting.ts<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u7a4d\uff1a\u8caa\u5a6a */\nfunction maxProductCutting(n: number): number {\n // \u7576 n &lt;= 3 \u6642\uff0c\u5fc5\u9808\u5207\u5206\u51fa\u4e00\u500b 1\n if (n &lt;= 3) {\n return 1 * (n - 1);\n }\n // \u8caa\u5a6a\u5730\u5207\u5206\u51fa 3 \uff0ca \u70ba 3 \u7684\u500b\u6578\uff0cb \u70ba\u9918\u6578\n let a: number = Math.floor(n / 3);\n let b: number = n % 3;\n if (b === 1) {\n // \u7576\u9918\u6578\u70ba 1 \u6642\uff0c\u5c07\u4e00\u5c0d 1 * 3 \u8f49\u5316\u70ba 2 * 2\n return Math.pow(3, a - 1) * 2 * 2;\n }\n if (b === 2) {\n // \u7576\u9918\u6578\u70ba 2 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return Math.pow(3, a) * 2;\n }\n // \u7576\u9918\u6578\u70ba 0 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return Math.pow(3, a);\n}\n</code></pre> max_product_cutting.dart<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u7a4d\uff1a\u8caa\u5a6a */\nint maxProductCutting(int n) {\n // \u7576 n &lt;= 3 \u6642\uff0c\u5fc5\u9808\u5207\u5206\u51fa\u4e00\u500b 1\n if (n &lt;= 3) {\n return 1 * (n - 1);\n }\n // \u8caa\u5a6a\u5730\u5207\u5206\u51fa 3 \uff0ca \u70ba 3 \u7684\u500b\u6578\uff0cb \u70ba\u9918\u6578\n int a = n ~/ 3;\n int b = n % 3;\n if (b == 1) {\n // \u7576\u9918\u6578\u70ba 1 \u6642\uff0c\u5c07\u4e00\u5c0d 1 * 3 \u8f49\u5316\u70ba 2 * 2\n return (pow(3, a - 1) * 2 * 2).toInt();\n }\n if (b == 2) {\n // \u7576\u9918\u6578\u70ba 2 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return (pow(3, a) * 2).toInt();\n }\n // \u7576\u9918\u6578\u70ba 0 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return pow(3, a).toInt();\n}\n</code></pre> max_product_cutting.rs<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u7a4d\uff1a\u8caa\u5a6a */\nfn max_product_cutting(n: i32) -&gt; i32 {\n // \u7576 n &lt;= 3 \u6642\uff0c\u5fc5\u9808\u5207\u5206\u51fa\u4e00\u500b 1\n if n &lt;= 3 {\n return 1 * (n - 1);\n }\n // \u8caa\u5a6a\u5730\u5207\u5206\u51fa 3 \uff0ca \u70ba 3 \u7684\u500b\u6578\uff0cb \u70ba\u9918\u6578\n let a = n / 3;\n let b = n % 3;\n if b == 1 {\n // \u7576\u9918\u6578\u70ba 1 \u6642\uff0c\u5c07\u4e00\u5c0d 1 * 3 \u8f49\u5316\u70ba 2 * 2\n 3_i32.pow(a as u32 - 1) * 2 * 2\n } else if b == 2 {\n // \u7576\u9918\u6578\u70ba 2 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n 3_i32.pow(a as u32) * 2\n } else {\n // \u7576\u9918\u6578\u70ba 0 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n 3_i32.pow(a as u32)\n }\n}\n</code></pre> max_product_cutting.c<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u7a4d\uff1a\u8caa\u5a6a */\nint maxProductCutting(int n) {\n // \u7576 n &lt;= 3 \u6642\uff0c\u5fc5\u9808\u5207\u5206\u51fa\u4e00\u500b 1\n if (n &lt;= 3) {\n return 1 * (n - 1);\n }\n // \u8caa\u5a6a\u5730\u5207\u5206\u51fa 3 \uff0ca \u70ba 3 \u7684\u500b\u6578\uff0cb \u70ba\u9918\u6578\n int a = n / 3;\n int b = n % 3;\n if (b == 1) {\n // \u7576\u9918\u6578\u70ba 1 \u6642\uff0c\u5c07\u4e00\u5c0d 1 * 3 \u8f49\u5316\u70ba 2 * 2\n return pow(3, a - 1) * 2 * 2;\n }\n if (b == 2) {\n // \u7576\u9918\u6578\u70ba 2 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return pow(3, a) * 2;\n }\n // \u7576\u9918\u6578\u70ba 0 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return pow(3, a);\n}\n</code></pre> max_product_cutting.kt<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u7a4d\uff1a\u8caa\u5a6a */\nfun maxProductCutting(n: Int): Int {\n // \u7576 n &lt;= 3 \u6642\uff0c\u5fc5\u9808\u5207\u5206\u51fa\u4e00\u500b 1\n if (n &lt;= 3) {\n return 1 * (n - 1)\n }\n // \u8caa\u5a6a\u5730\u5207\u5206\u51fa 3 \uff0ca \u70ba 3 \u7684\u500b\u6578\uff0cb \u70ba\u9918\u6578\n val a = n / 3\n val b = n % 3\n if (b == 1) {\n // \u7576\u9918\u6578\u70ba 1 \u6642\uff0c\u5c07\u4e00\u5c0d 1 * 3 \u8f49\u5316\u70ba 2 * 2\n return 3.0.pow((a - 1)).toInt() * 2 * 2\n }\n if (b == 2) {\n // \u7576\u9918\u6578\u70ba 2 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return 3.0.pow(a).toInt() * 2 * 2\n }\n // \u7576\u9918\u6578\u70ba 0 \u6642\uff0c\u4e0d\u505a\u8655\u7406\n return 3.0.pow(a).toInt()\n}\n</code></pre> max_product_cutting.rb<pre><code>[class]{}-[func]{max_product_cutting}\n</code></pre> max_product_cutting.zig<pre><code>[class]{}-[func]{maxProductCutting}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p></p> <p> \u5716 15-16 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u7a4d\u7684\u8a08\u7b97\u65b9\u6cd5 </p> <p>\u6642\u9593\u8907\u96dc\u5ea6\u53d6\u6c7a\u65bc\u7a0b\u5f0f\u8a9e\u8a00\u7684\u51aa\u904b\u7b97\u7684\u5be6\u73fe\u65b9\u6cd5\u3002\u4ee5 Python \u70ba\u4f8b\uff0c\u5e38\u7528\u7684\u51aa\u8a08\u7b97\u51fd\u5f0f\u6709\u4e09\u7a2e\u3002</p> <ul> <li>\u904b\u7b97\u5b50 <code>**</code> \u548c\u51fd\u5f0f <code>pow()</code> \u7684\u6642\u9593\u8907\u96dc\u5ea6\u5747\u70ba \\(O(\\log\u2061 a)\\) \u3002</li> <li>\u51fd\u5f0f <code>math.pow()</code> \u5167\u90e8\u547c\u53eb C \u8a9e\u8a00\u5eab\u7684 <code>pow()</code> \u51fd\u5f0f\uff0c\u5176\u57f7\u884c\u6d6e\u9ede\u53d6\u51aa\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(1)\\) \u3002</li> </ul> <p>\u8b8a\u6578 \\(a\\) \u548c \\(b\\) \u4f7f\u7528\u5e38\u6578\u5927\u5c0f\u7684\u984d\u5916\u7a7a\u9593\uff0c\u56e0\u6b64\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(1)\\) \u3002</p>"},{"location":"chapter_greedy/max_product_cutting_problem/#3","title":"3. \u00a0 \u6b63\u78ba\u6027\u8b49\u660e","text":"<p>\u4f7f\u7528\u53cd\u8b49\u6cd5\uff0c\u53ea\u5206\u6790 \\(n \\geq 3\\) \u7684\u60c5\u6cc1\u3002</p> <ol> <li>\u6240\u6709\u56e0\u5b50 \\(\\leq 3\\) \uff1a\u5047\u8a2d\u6700\u512a\u5207\u5206\u65b9\u6848\u4e2d\u5b58\u5728 \\(\\geq 4\\) \u7684\u56e0\u5b50 \\(x\\) \uff0c\u90a3\u9ebc\u4e00\u5b9a\u53ef\u4ee5\u5c07\u5176\u7e7c\u7e8c\u5283\u5206\u70ba \\(2(x-2)\\) \uff0c\u5f9e\u800c\u7372\u5f97\u66f4\u5927\u7684\u4e58\u7a4d\u3002\u9019\u8207\u5047\u8a2d\u77db\u76fe\u3002</li> <li>\u5207\u5206\u65b9\u6848\u4e0d\u5305\u542b \\(1\\) \uff1a\u5047\u8a2d\u6700\u512a\u5207\u5206\u65b9\u6848\u4e2d\u5b58\u5728\u4e00\u500b\u56e0\u5b50 \\(1\\) \uff0c\u90a3\u9ebc\u5b83\u4e00\u5b9a\u53ef\u4ee5\u5408\u4f75\u5165\u53e6\u5916\u4e00\u500b\u56e0\u5b50\u4e2d\uff0c\u4ee5\u7372\u5f97\u66f4\u5927\u7684\u4e58\u7a4d\u3002\u9019\u8207\u5047\u8a2d\u77db\u76fe\u3002</li> <li>\u5207\u5206\u65b9\u6848\u6700\u591a\u5305\u542b\u5169\u500b \\(2\\) \uff1a\u5047\u8a2d\u6700\u512a\u5207\u5206\u65b9\u6848\u4e2d\u5305\u542b\u4e09\u500b \\(2\\) \uff0c\u90a3\u9ebc\u4e00\u5b9a\u53ef\u4ee5\u66ff\u63db\u70ba\u5169\u500b \\(3\\) \uff0c\u4e58\u7a4d\u66f4\u5927\u3002\u9019\u8207\u5047\u8a2d\u77db\u76fe\u3002</li> </ol>"},{"location":"chapter_greedy/summary/","title":"15.5 \u00a0 \u5c0f\u7d50","text":"<ul> <li>\u8caa\u5a6a\u6f14\u7b97\u6cd5\u901a\u5e38\u7528\u65bc\u89e3\u6c7a\u6700\u6700\u4f73\u5316\u554f\u984c\uff0c\u5176\u539f\u7406\u662f\u5728\u6bcf\u500b\u6c7a\u7b56\u968e\u6bb5\u90fd\u505a\u51fa\u5340\u57df\u6027\u6700\u512a\u7684\u6c7a\u7b56\uff0c\u4ee5\u671f\u7372\u5f97\u5168\u57df\u6027\u6700\u512a\u89e3\u3002</li> <li>\u8caa\u5a6a\u6f14\u7b97\u6cd5\u6703\u8fed\u4ee3\u5730\u505a\u51fa\u4e00\u500b\u53c8\u4e00\u500b\u7684\u8caa\u5a6a\u9078\u64c7\uff0c\u6bcf\u8f2a\u90fd\u5c07\u554f\u984c\u8f49\u5316\u6210\u4e00\u500b\u898f\u6a21\u66f4\u5c0f\u7684\u5b50\u554f\u984c\uff0c\u76f4\u5230\u554f\u984c\u88ab\u89e3\u6c7a\u3002</li> <li>\u8caa\u5a6a\u6f14\u7b97\u6cd5\u4e0d\u50c5\u5be6\u73fe\u7c21\u55ae\uff0c\u9084\u5177\u6709\u5f88\u9ad8\u7684\u89e3\u984c\u6548\u7387\u3002\u76f8\u6bd4\u65bc\u52d5\u614b\u898f\u5283\uff0c\u8caa\u5a6a\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u901a\u5e38\u66f4\u4f4e\u3002</li> <li>\u5728\u96f6\u9322\u514c\u63db\u554f\u984c\u4e2d\uff0c\u5c0d\u65bc\u67d0\u4e9b\u786c\u5e63\u7d44\u5408\uff0c\u8caa\u5a6a\u6f14\u7b97\u6cd5\u53ef\u4ee5\u4fdd\u8b49\u627e\u5230\u6700\u512a\u89e3\uff1b\u5c0d\u65bc\u53e6\u5916\u4e00\u4e9b\u786c\u5e63\u7d44\u5408\u5247\u4e0d\u7136\uff0c\u8caa\u5a6a\u6f14\u7b97\u6cd5\u53ef\u80fd\u627e\u5230\u5f88\u5dee\u7684\u89e3\u3002</li> <li>\u9069\u5408\u7528\u8caa\u5a6a\u6f14\u7b97\u6cd5\u6c42\u89e3\u7684\u554f\u984c\u5177\u6709\u5169\u5927\u6027\u8cea\uff1a\u8caa\u5a6a\u9078\u64c7\u6027\u8cea\u548c\u6700\u512a\u5b50\u7d50\u69cb\u3002\u8caa\u5a6a\u9078\u64c7\u6027\u8cea\u4ee3\u8868\u8caa\u5a6a\u7b56\u7565\u7684\u6709\u6548\u6027\u3002</li> <li>\u5c0d\u65bc\u67d0\u4e9b\u8907\u96dc\u554f\u984c\uff0c\u8caa\u5a6a\u9078\u64c7\u6027\u8cea\u7684\u8b49\u660e\u4e26\u4e0d\u7c21\u55ae\u3002\u76f8\u5c0d\u4f86\u8aaa\uff0c\u8b49\u507d\u66f4\u52a0\u5bb9\u6613\uff0c\u4f8b\u5982\u96f6\u9322\u514c\u63db\u554f\u984c\u3002</li> <li>\u6c42\u89e3\u8caa\u5a6a\u554f\u984c\u4e3b\u8981\u5206\u70ba\u4e09\u6b65\uff1a\u554f\u984c\u5206\u6790\u3001\u78ba\u5b9a\u8caa\u5a6a\u7b56\u7565\u3001\u6b63\u78ba\u6027\u8b49\u660e\u3002\u5176\u4e2d\uff0c\u78ba\u5b9a\u8caa\u5a6a\u7b56\u7565\u662f\u6838\u5fc3\u6b65\u9a5f\uff0c\u6b63\u78ba\u6027\u8b49\u660e\u5f80\u5f80\u662f\u96e3\u9ede\u3002</li> <li>\u5206\u6578\u80cc\u5305\u554f\u984c\u5728 0-1 \u80cc\u5305\u7684\u57fa\u790e\u4e0a\uff0c\u5141\u8a31\u9078\u64c7\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u53ef\u4f7f\u7528\u8caa\u5a6a\u6f14\u7b97\u6cd5\u6c42\u89e3\u3002\u8caa\u5a6a\u7b56\u7565\u7684\u6b63\u78ba\u6027\u53ef\u4ee5\u4f7f\u7528\u53cd\u8b49\u6cd5\u4f86\u8b49\u660e\u3002</li> <li>\u6700\u5927\u5bb9\u91cf\u554f\u984c\u53ef\u4f7f\u7528\u7aae\u8209\u6cd5\u6c42\u89e3\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\) \u3002\u900f\u904e\u8a2d\u8a08\u8caa\u5a6a\u7b56\u7565\uff0c\u6bcf\u8f2a\u5411\u5167\u79fb\u52d5\u77ed\u677f\uff0c\u53ef\u5c07\u6642\u9593\u8907\u96dc\u5ea6\u6700\u4f73\u5316\u81f3 \\(O(n)\\) \u3002</li> <li>\u5728\u6700\u5927\u5207\u5206\u4e58\u7a4d\u554f\u984c\u4e2d\uff0c\u6211\u5011\u5148\u5f8c\u63a8\u7406\u51fa\u5169\u500b\u8caa\u5a6a\u7b56\u7565\uff1a\\(\\geq 4\\) \u7684\u6574\u6578\u90fd\u61c9\u8a72\u7e7c\u7e8c\u5207\u5206\uff0c\u6700\u512a\u5207\u5206\u56e0\u5b50\u70ba \\(3\\) \u3002\u7a0b\u5f0f\u78bc\u4e2d\u5305\u542b\u51aa\u904b\u7b97\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u53d6\u6c7a\u65bc\u51aa\u904b\u7b97\u5be6\u73fe\u65b9\u6cd5\uff0c\u901a\u5e38\u70ba \\(O(1)\\) \u6216 \\(O(\\log n)\\) \u3002</li> </ul>"},{"location":"chapter_hashing/","title":"\u7b2c 6 \u7ae0 \u00a0 \u96dc\u6e4a\u8868","text":"<p>Abstract</p> <p>\u5728\u8a08\u7b97\u6a5f\u4e16\u754c\u4e2d\uff0c\u96dc\u6e4a\u8868\u5982\u540c\u4e00\u4f4d\u8070\u6167\u7684\u5716\u66f8\u7ba1\u7406\u54e1\u3002</p> <p>\u4ed6\u77e5\u9053\u5982\u4f55\u8a08\u7b97\u7d22\u66f8\u865f\uff0c\u5f9e\u800c\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u76ee\u6a19\u5716\u66f8\u3002</p>"},{"location":"chapter_hashing/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>6.1 \u00a0 \u96dc\u6e4a\u8868</li> <li>6.2 \u00a0 \u96dc\u6e4a\u885d\u7a81</li> <li>6.3 \u00a0 \u96dc\u6e4a\u6f14\u7b97\u6cd5</li> <li>6.4 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_hashing/hash_algorithm/","title":"6.3 \u00a0 \u96dc\u6e4a\u6f14\u7b97\u6cd5","text":"<p>\u524d\u5169\u7bc0\u4ecb\u7d39\u4e86\u96dc\u6e4a\u8868\u7684\u5de5\u4f5c\u539f\u7406\u548c\u96dc\u6e4a\u885d\u7a81\u7684\u8655\u7406\u65b9\u6cd5\u3002\u7136\u800c\u7121\u8ad6\u662f\u958b\u653e\u5b9a\u5740\u9084\u662f\u93c8\u5f0f\u4f4d\u5740\uff0c\u5b83\u5011\u53ea\u80fd\u4fdd\u8b49\u96dc\u6e4a\u8868\u53ef\u4ee5\u5728\u767c\u751f\u885d\u7a81\u6642\u6b63\u5e38\u5de5\u4f5c\uff0c\u800c\u7121\u6cd5\u6e1b\u5c11\u96dc\u6e4a\u885d\u7a81\u7684\u767c\u751f\u3002</p> <p>\u5982\u679c\u96dc\u6e4a\u885d\u7a81\u904e\u65bc\u983b\u7e41\uff0c\u96dc\u6e4a\u8868\u7684\u6548\u80fd\u5247\u6703\u6025\u5287\u52a3\u5316\u3002\u5982\u5716 6-8 \u6240\u793a\uff0c\u5c0d\u65bc\u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868\uff0c\u7406\u60f3\u60c5\u6cc1\u4e0b\u9375\u503c\u5c0d\u5747\u52fb\u5206\u4f48\u5728\u5404\u500b\u6876\u4e2d\uff0c\u9054\u5230\u6700\u4f73\u67e5\u8a62\u6548\u7387\uff1b\u6700\u5dee\u60c5\u6cc1\u4e0b\u6240\u6709\u9375\u503c\u5c0d\u90fd\u5132\u5b58\u5230\u540c\u4e00\u500b\u6876\u4e2d\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \u3002</p> <p></p> <p> \u5716 6-8 \u00a0 \u96dc\u6e4a\u885d\u7a81\u7684\u6700\u4f73\u60c5\u6cc1\u8207\u6700\u5dee\u60c5\u6cc1 </p> <p>\u9375\u503c\u5c0d\u7684\u5206\u4f48\u60c5\u6cc1\u7531\u96dc\u6e4a\u51fd\u5f0f\u6c7a\u5b9a\u3002\u56de\u61b6\u96dc\u6e4a\u51fd\u5f0f\u7684\u8a08\u7b97\u6b65\u9a5f\uff0c\u5148\u8a08\u7b97\u96dc\u6e4a\u503c\uff0c\u518d\u5c0d\u9663\u5217\u9577\u5ea6\u53d6\u6a21\uff1a</p> <pre><code>index = hash(key) % capacity\n</code></pre> <p>\u89c0\u5bdf\u4ee5\u4e0a\u516c\u5f0f\uff0c\u7576\u96dc\u6e4a\u8868\u5bb9\u91cf <code>capacity</code> \u56fa\u5b9a\u6642\uff0c\u96dc\u6e4a\u6f14\u7b97\u6cd5 <code>hash()</code> \u6c7a\u5b9a\u4e86\u8f38\u51fa\u503c\uff0c\u9032\u800c\u6c7a\u5b9a\u4e86\u9375\u503c\u5c0d\u5728\u96dc\u6e4a\u8868\u4e2d\u7684\u5206\u4f48\u60c5\u6cc1\u3002</p> <p>\u9019\u610f\u5473\u8457\uff0c\u70ba\u4e86\u964d\u4f4e\u96dc\u6e4a\u885d\u7a81\u7684\u767c\u751f\u6a5f\u7387\uff0c\u6211\u5011\u61c9\u7576\u5c07\u6ce8\u610f\u529b\u96c6\u4e2d\u5728\u96dc\u6e4a\u6f14\u7b97\u6cd5 <code>hash()</code> \u7684\u8a2d\u8a08\u4e0a\u3002</p>"},{"location":"chapter_hashing/hash_algorithm/#631","title":"6.3.1 \u00a0 \u96dc\u6e4a\u6f14\u7b97\u6cd5\u7684\u76ee\u6a19","text":"<p>\u70ba\u4e86\u5be6\u73fe\u201c\u65e2\u5feb\u53c8\u7a69\u201d\u7684\u96dc\u6e4a\u8868\u8cc7\u6599\u7d50\u69cb\uff0c\u96dc\u6e4a\u6f14\u7b97\u6cd5\u61c9\u5177\u5099\u4ee5\u4e0b\u7279\u9ede\u3002</p> <ul> <li>\u78ba\u5b9a\u6027\uff1a\u5c0d\u65bc\u76f8\u540c\u7684\u8f38\u5165\uff0c\u96dc\u6e4a\u6f14\u7b97\u6cd5\u61c9\u59cb\u7d42\u7522\u751f\u76f8\u540c\u7684\u8f38\u51fa\u3002\u9019\u6a23\u624d\u80fd\u78ba\u4fdd\u96dc\u6e4a\u8868\u662f\u53ef\u9760\u7684\u3002</li> <li>\u6548\u7387\u9ad8\uff1a\u8a08\u7b97\u96dc\u6e4a\u503c\u7684\u904e\u7a0b\u61c9\u8a72\u8db3\u5920\u5feb\u3002\u8a08\u7b97\u958b\u92b7\u8d8a\u5c0f\uff0c\u96dc\u6e4a\u8868\u7684\u5be6\u7528\u6027\u8d8a\u9ad8\u3002</li> <li>\u5747\u52fb\u5206\u4f48\uff1a\u96dc\u6e4a\u6f14\u7b97\u6cd5\u61c9\u4f7f\u5f97\u9375\u503c\u5c0d\u5747\u52fb\u5206\u4f48\u5728\u96dc\u6e4a\u8868\u4e2d\u3002\u5206\u4f48\u8d8a\u5747\u52fb\uff0c\u96dc\u6e4a\u885d\u7a81\u7684\u6a5f\u7387\u5c31\u8d8a\u4f4e\u3002</li> </ul> <p>\u5be6\u969b\u4e0a\uff0c\u96dc\u6e4a\u6f14\u7b97\u6cd5\u9664\u4e86\u53ef\u4ee5\u7528\u65bc\u5be6\u73fe\u96dc\u6e4a\u8868\uff0c\u9084\u5ee3\u6cdb\u61c9\u7528\u65bc\u5176\u4ed6\u9818\u57df\u4e2d\u3002</p> <ul> <li>\u5bc6\u78bc\u5132\u5b58\uff1a\u70ba\u4e86\u4fdd\u8b77\u4f7f\u7528\u8005\u5bc6\u78bc\u7684\u5b89\u5168\uff0c\u7cfb\u7d71\u901a\u5e38\u4e0d\u6703\u76f4\u63a5\u5132\u5b58\u4f7f\u7528\u8005\u7684\u660e\u6587\u5bc6\u78bc\uff0c\u800c\u662f\u5132\u5b58\u5bc6\u78bc\u7684\u96dc\u6e4a\u503c\u3002\u7576\u7528\u6236\u8f38\u5165\u5bc6\u78bc\u6642\uff0c\u7cfb\u7d71\u6703\u5c0d\u8f38\u5165\u7684\u5bc6\u78bc\u8a08\u7b97\u96dc\u6e4a\u503c\uff0c\u7136\u5f8c\u8207\u5132\u5b58\u7684\u96dc\u6e4a\u503c\u9032\u884c\u6bd4\u8f03\u3002\u5982\u679c\u5169\u8005\u5339\u914d\uff0c\u90a3\u9ebc\u5bc6\u78bc\u5c31\u88ab\u8996\u70ba\u6b63\u78ba\u3002</li> <li>\u8cc7\u6599\u5b8c\u6574\u6027\u6aa2\u67e5\uff1a\u8cc7\u6599\u50b3\u9001\u65b9\u53ef\u4ee5\u8a08\u7b97\u8cc7\u6599\u7684\u96dc\u6e4a\u503c\u4e26\u5c07\u5176\u4e00\u540c\u50b3\u9001\uff1b\u63a5\u6536\u65b9\u53ef\u4ee5\u91cd\u65b0\u8a08\u7b97\u63a5\u6536\u5230\u7684\u8cc7\u6599\u7684\u96dc\u6e4a\u503c\uff0c\u4e26\u8207\u63a5\u6536\u5230\u7684\u96dc\u6e4a\u503c\u9032\u884c\u6bd4\u8f03\u3002\u5982\u679c\u5169\u8005\u5339\u914d\uff0c\u90a3\u9ebc\u8cc7\u6599\u5c31\u88ab\u8996\u70ba\u5b8c\u6574\u3002</li> </ul> <p>\u5c0d\u65bc\u5bc6\u78bc\u5b78\u7684\u76f8\u95dc\u61c9\u7528\uff0c\u70ba\u4e86\u9632\u6b62\u5f9e\u96dc\u6e4a\u503c\u63a8\u5c0e\u51fa\u539f\u59cb\u5bc6\u78bc\u7b49\u9006\u5411\u5de5\u7a0b\uff0c\u96dc\u6e4a\u6f14\u7b97\u6cd5\u9700\u8981\u5177\u5099\u66f4\u9ad8\u7b49\u7d1a\u7684\u5b89\u5168\u7279\u6027\u3002</p> <ul> <li>\u55ae\u5411\u6027\uff1a\u7121\u6cd5\u900f\u904e\u96dc\u6e4a\u503c\u53cd\u63a8\u51fa\u95dc\u65bc\u8f38\u5165\u8cc7\u6599\u7684\u4efb\u4f55\u8cc7\u8a0a\u3002</li> <li>\u6297\u78b0\u649e\u6027\uff1a\u61c9\u7576\u6975\u96e3\u627e\u5230\u5169\u500b\u4e0d\u540c\u7684\u8f38\u5165\uff0c\u4f7f\u5f97\u5b83\u5011\u7684\u96dc\u6e4a\u503c\u76f8\u540c\u3002</li> <li>\u96ea\u5d29\u6548\u61c9\uff1a\u8f38\u5165\u7684\u5fae\u5c0f\u8b8a\u5316\u61c9\u7576\u5c0e\u81f4\u8f38\u51fa\u7684\u986f\u8457\u4e14\u4e0d\u53ef\u9810\u6e2c\u7684\u8b8a\u5316\u3002</li> </ul> <p>\u8acb\u6ce8\u610f\uff0c\u201c\u5747\u52fb\u5206\u4f48\u201d\u8207\u201c\u6297\u78b0\u649e\u6027\u201d\u662f\u5169\u500b\u7368\u7acb\u7684\u6982\u5ff5\uff0c\u6eff\u8db3\u5747\u52fb\u5206\u4f48\u4e0d\u4e00\u5b9a\u6eff\u8db3\u6297\u78b0\u649e\u6027\u3002\u4f8b\u5982\uff0c\u5728\u96a8\u6a5f\u8f38\u5165 <code>key</code> \u4e0b\uff0c\u96dc\u6e4a\u51fd\u5f0f <code>key % 100</code> \u53ef\u4ee5\u7522\u751f\u5747\u52fb\u5206\u4f48\u7684\u8f38\u51fa\u3002\u7136\u800c\u8a72\u96dc\u6e4a\u6f14\u7b97\u6cd5\u904e\u65bc\u7c21\u55ae\uff0c\u6240\u6709\u5f8c\u5169\u4f4d\u76f8\u7b49\u7684 <code>key</code> \u7684\u8f38\u51fa\u90fd\u76f8\u540c\uff0c\u56e0\u6b64\u6211\u5011\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5f9e\u96dc\u6e4a\u503c\u53cd\u63a8\u51fa\u53ef\u7528\u7684 <code>key</code> \uff0c\u5f9e\u800c\u7834\u89e3\u5bc6\u78bc\u3002</p>"},{"location":"chapter_hashing/hash_algorithm/#632","title":"6.3.2 \u00a0 \u96dc\u6e4a\u6f14\u7b97\u6cd5\u7684\u8a2d\u8a08","text":"<p>\u96dc\u6e4a\u6f14\u7b97\u6cd5\u7684\u8a2d\u8a08\u662f\u4e00\u500b\u9700\u8981\u8003\u616e\u8a31\u591a\u56e0\u7d20\u7684\u8907\u96dc\u554f\u984c\u3002\u7136\u800c\u5c0d\u65bc\u67d0\u4e9b\u8981\u6c42\u4e0d\u9ad8\u7684\u5834\u666f\uff0c\u6211\u5011\u4e5f\u80fd\u8a2d\u8a08\u4e00\u4e9b\u7c21\u55ae\u7684\u96dc\u6e4a\u6f14\u7b97\u6cd5\u3002</p> <ul> <li>\u52a0\u6cd5\u96dc\u6e4a\uff1a\u5c0d\u8f38\u5165\u7684\u6bcf\u500b\u5b57\u5143\u7684 ASCII \u78bc\u9032\u884c\u76f8\u52a0\uff0c\u5c07\u5f97\u5230\u7684\u7e3d\u548c\u4f5c\u70ba\u96dc\u6e4a\u503c\u3002</li> <li>\u4e58\u6cd5\u96dc\u6e4a\uff1a\u5229\u7528\u4e58\u6cd5\u7684\u4e0d\u76f8\u95dc\u6027\uff0c\u6bcf\u8f2a\u4e58\u4ee5\u4e00\u500b\u5e38\u6578\uff0c\u5c07\u5404\u500b\u5b57\u5143\u7684 ASCII \u78bc\u7d2f\u7a4d\u5230\u96dc\u6e4a\u503c\u4e2d\u3002</li> <li>\u4e92\u65a5\u6216\u96dc\u6e4a\uff1a\u5c07\u8f38\u5165\u8cc7\u6599\u7684\u6bcf\u500b\u5143\u7d20\u900f\u904e\u4e92\u65a5\u6216\u64cd\u4f5c\u7d2f\u7a4d\u5230\u4e00\u500b\u96dc\u6e4a\u503c\u4e2d\u3002</li> <li>\u65cb\u8f49\u96dc\u6e4a\uff1a\u5c07\u6bcf\u500b\u5b57\u5143\u7684 ASCII \u78bc\u7d2f\u7a4d\u5230\u4e00\u500b\u96dc\u6e4a\u503c\u4e2d\uff0c\u6bcf\u6b21\u7d2f\u7a4d\u4e4b\u524d\u90fd\u6703\u5c0d\u96dc\u6e4a\u503c\u9032\u884c\u65cb\u8f49\u64cd\u4f5c\u3002</li> </ul> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig simple_hash.py<pre><code>def add_hash(key: str) -&gt; int:\n \"\"\"\u52a0\u6cd5\u96dc\u6e4a\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash += ord(c)\n return hash % modulus\n\ndef mul_hash(key: str) -&gt; int:\n \"\"\"\u4e58\u6cd5\u96dc\u6e4a\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash = 31 * hash + ord(c)\n return hash % modulus\n\ndef xor_hash(key: str) -&gt; int:\n \"\"\"\u4e92\u65a5\u6216\u96dc\u6e4a\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash ^= ord(c)\n return hash % modulus\n\ndef rot_hash(key: str) -&gt; int:\n \"\"\"\u65cb\u8f49\u96dc\u6e4a\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash = (hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ ord(c)\n return hash % modulus\n</code></pre> simple_hash.cpp<pre><code>/* \u52a0\u6cd5\u96dc\u6e4a */\nint addHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = (hash + (int)c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u96dc\u6e4a */\nint mulHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = (31 * hash + (int)c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e92\u65a5\u6216\u96dc\u6e4a */\nint xorHash(string key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash ^= (int)c;\n }\n return hash &amp; MODULUS;\n}\n\n/* \u65cb\u8f49\u96dc\u6e4a */\nint rotHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ (int)c) % MODULUS;\n }\n return (int)hash;\n}\n</code></pre> simple_hash.java<pre><code>/* \u52a0\u6cd5\u96dc\u6e4a */\nint addHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = (hash + (int) c) % MODULUS;\n }\n return (int) hash;\n}\n\n/* \u4e58\u6cd5\u96dc\u6e4a */\nint mulHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = (31 * hash + (int) c) % MODULUS;\n }\n return (int) hash;\n}\n\n/* \u4e92\u65a5\u6216\u96dc\u6e4a */\nint xorHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash ^= (int) c;\n }\n return hash &amp; MODULUS;\n}\n\n/* \u65cb\u8f49\u96dc\u6e4a */\nint rotHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ (int) c) % MODULUS;\n }\n return (int) hash;\n}\n</code></pre> simple_hash.cs<pre><code>/* \u52a0\u6cd5\u96dc\u6e4a */\nint AddHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = (hash + c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u96dc\u6e4a */\nint MulHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = (31 * hash + c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e92\u65a5\u6216\u96dc\u6e4a */\nint XorHash(string key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash ^= c;\n }\n return hash &amp; MODULUS;\n}\n\n/* \u65cb\u8f49\u96dc\u6e4a */\nint RotHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ c) % MODULUS;\n }\n return (int)hash;\n}\n</code></pre> simple_hash.go<pre><code>/* \u52a0\u6cd5\u96dc\u6e4a */\nfunc addHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = (hash + int64(b)) % modulus\n }\n return int(hash)\n}\n\n/* \u4e58\u6cd5\u96dc\u6e4a */\nfunc mulHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = (31*hash + int64(b)) % modulus\n }\n return int(hash)\n}\n\n/* \u4e92\u65a5\u6216\u96dc\u6e4a */\nfunc xorHash(key string) int {\n hash := 0\n modulus := 1000000007\n for _, b := range []byte(key) {\n fmt.Println(int(b))\n hash ^= int(b)\n hash = (31*hash + int(b)) % modulus\n }\n return hash &amp; modulus\n}\n\n/* \u65cb\u8f49\u96dc\u6e4a */\nfunc rotHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ int64(b)) % modulus\n }\n return int(hash)\n}\n</code></pre> simple_hash.swift<pre><code>/* \u52a0\u6cd5\u96dc\u6e4a */\nfunc addHash(key: String) -&gt; Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = (hash + Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n\n/* \u4e58\u6cd5\u96dc\u6e4a */\nfunc mulHash(key: String) -&gt; Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = (31 * hash + Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n\n/* \u4e92\u65a5\u6216\u96dc\u6e4a */\nfunc xorHash(key: String) -&gt; Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash ^= Int(scalar.value)\n }\n }\n return hash &amp; MODULUS\n}\n\n/* \u65cb\u8f49\u96dc\u6e4a */\nfunc rotHash(key: String) -&gt; Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n</code></pre> simple_hash.js<pre><code>/* \u52a0\u6cd5\u96dc\u6e4a */\nfunction addHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u96dc\u6e4a */\nfunction mulHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e92\u65a5\u6216\u96dc\u6e4a */\nfunction xorHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash ^= c.charCodeAt(0);\n }\n return hash &amp; MODULUS;\n}\n\n/* \u65cb\u8f49\u96dc\u6e4a */\nfunction rotHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n</code></pre> simple_hash.ts<pre><code>/* \u52a0\u6cd5\u96dc\u6e4a */\nfunction addHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u96dc\u6e4a */\nfunction mulHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e92\u65a5\u6216\u96dc\u6e4a */\nfunction xorHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash ^= c.charCodeAt(0);\n }\n return hash &amp; MODULUS;\n}\n\n/* \u65cb\u8f49\u96dc\u6e4a */\nfunction rotHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n</code></pre> simple_hash.dart<pre><code>/* \u52a0\u6cd5\u96dc\u6e4a */\nint addHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i &lt; key.length; i++) {\n hash = (hash + key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u96dc\u6e4a */\nint mulHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i &lt; key.length; i++) {\n hash = (31 * hash + key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e92\u65a5\u6216\u96dc\u6e4a */\nint xorHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i &lt; key.length; i++) {\n hash ^= key.codeUnitAt(i);\n }\n return hash &amp; MODULUS;\n}\n\n/* \u65cb\u8f49\u96dc\u6e4a */\nint rotHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i &lt; key.length; i++) {\n hash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n</code></pre> simple_hash.rs<pre><code>/* \u52a0\u6cd5\u96dc\u6e4a */\nfn add_hash(key: &amp;str) -&gt; i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = (hash + c as i64) % MODULUS;\n }\n\n hash as i32\n}\n\n/* \u4e58\u6cd5\u96dc\u6e4a */\nfn mul_hash(key: &amp;str) -&gt; i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = (31 * hash + c as i64) % MODULUS;\n }\n\n hash as i32\n}\n\n/* \u4e92\u65a5\u6216\u96dc\u6e4a */\nfn xor_hash(key: &amp;str) -&gt; i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash ^= c as i64;\n }\n\n (hash &amp; MODULUS) as i32\n}\n\n/* \u65cb\u8f49\u96dc\u6e4a */\nfn rot_hash(key: &amp;str) -&gt; i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ c as i64) % MODULUS;\n }\n\n hash as i32\n}\n</code></pre> simple_hash.c<pre><code>/* \u52a0\u6cd5\u96dc\u6e4a */\nint addHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i &lt; strlen(key); i++) {\n hash = (hash + (unsigned char)key[i]) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u96dc\u6e4a */\nint mulHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i &lt; strlen(key); i++) {\n hash = (31 * hash + (unsigned char)key[i]) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e92\u65a5\u6216\u96dc\u6e4a */\nint xorHash(char *key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n\n for (int i = 0; i &lt; strlen(key); i++) {\n hash ^= (unsigned char)key[i];\n }\n return hash &amp; MODULUS;\n}\n\n/* \u65cb\u8f49\u96dc\u6e4a */\nint rotHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i &lt; strlen(key); i++) {\n hash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ (unsigned char)key[i]) % MODULUS;\n }\n\n return (int)hash;\n}\n</code></pre> simple_hash.kt<pre><code>/* \u52a0\u6cd5\u96dc\u6e4a */\nfun addHash(key: String): Int {\n var hash = 0L\n val MODULUS = 1000000007\n for (c in key.toCharArray()) {\n hash = (hash + c.code) % MODULUS\n }\n return hash.toInt()\n}\n\n/* \u4e58\u6cd5\u96dc\u6e4a */\nfun mulHash(key: String): Int {\n var hash = 0L\n val MODULUS = 1000000007\n for (c in key.toCharArray()) {\n hash = (31 * hash + c.code) % MODULUS\n }\n return hash.toInt()\n}\n\n/* \u4e92\u65a5\u6216\u96dc\u6e4a */\nfun xorHash(key: String): Int {\n var hash = 0\n val MODULUS = 1000000007\n for (c in key.toCharArray()) {\n hash = hash xor c.code\n }\n return hash and MODULUS\n}\n\n/* \u65cb\u8f49\u96dc\u6e4a */\nfun rotHash(key: String): Int {\n var hash = 0L\n val MODULUS = 1000000007\n for (c in key.toCharArray()) {\n hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS\n }\n return hash.toInt()\n}\n</code></pre> simple_hash.rb<pre><code>### \u52a0\u6cd5\u96dc\u6e4a ###\ndef add_hash(key)\n hash = 0\n modulus = 1_000_000_007\n\n key.each_char { |c| hash += c.ord }\n\n hash % modulus\nend\n\n### \u4e58\u6cd5\u96dc\u6e4a ###\ndef mul_hash(key)\n hash = 0\n modulus = 1_000_000_007\n\n key.each_char { |c| hash = 31 * hash + c.ord }\n\n hash % modulus\nend\n\n### \u4e92\u65a5\u6216\u96dc\u6e4a ###\ndef xor_hash(key)\n hash = 0\n modulus = 1_000_000_007\n\n key.each_char { |c| hash ^= c.ord }\n\n hash % modulus\nend\n\n### \u65cb\u8f49\u96dc\u6e4a ###\ndef rot_hash(key)\n hash = 0\n modulus = 1_000_000_007\n\n key.each_char { |c| hash = (hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ c.ord }\n\n hash % modulus\nend\n</code></pre> simple_hash.zig<pre><code>[class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u89c0\u5bdf\u767c\u73fe\uff0c\u6bcf\u7a2e\u96dc\u6e4a\u6f14\u7b97\u6cd5\u7684\u6700\u5f8c\u4e00\u6b65\u90fd\u662f\u5c0d\u5927\u8cea\u6578 \\(1000000007\\) \u53d6\u6a21\uff0c\u4ee5\u78ba\u4fdd\u96dc\u6e4a\u503c\u5728\u5408\u9069\u7684\u7bc4\u570d\u5167\u3002\u503c\u5f97\u601d\u8003\u7684\u662f\uff0c\u70ba\u4ec0\u9ebc\u8981\u5f37\u8abf\u5c0d\u8cea\u6578\u53d6\u6a21\uff0c\u6216\u8005\u8aaa\u5c0d\u5408\u6578\u53d6\u6a21\u7684\u5f0a\u7aef\u662f\u4ec0\u9ebc\uff1f\u9019\u662f\u4e00\u500b\u6709\u8da3\u7684\u554f\u984c\u3002</p> <p>\u5148\u4e1f\u64f2\u7d50\u8ad6\uff1a\u4f7f\u7528\u5927\u8cea\u6578\u4f5c\u70ba\u6a21\u6578\uff0c\u53ef\u4ee5\u6700\u5927\u5316\u5730\u4fdd\u8b49\u96dc\u6e4a\u503c\u7684\u5747\u52fb\u5206\u4f48\u3002\u56e0\u70ba\u8cea\u6578\u4e0d\u8207\u5176\u4ed6\u6578\u5b57\u5b58\u5728\u516c\u7d04\u6578\uff0c\u53ef\u4ee5\u6e1b\u5c11\u56e0\u53d6\u6a21\u64cd\u4f5c\u800c\u7522\u751f\u7684\u9031\u671f\u6027\u6a21\u5f0f\uff0c\u5f9e\u800c\u907f\u514d\u96dc\u6e4a\u885d\u7a81\u3002</p> <p>\u8209\u500b\u4f8b\u5b50\uff0c\u5047\u8a2d\u6211\u5011\u9078\u64c7\u5408\u6578 \\(9\\) \u4f5c\u70ba\u6a21\u6578\uff0c\u5b83\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\uff0c\u90a3\u9ebc\u6240\u6709\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\u7684 <code>key</code> \u90fd\u6703\u88ab\u5c0d\u6620\u5230 \\(0\\)\u3001\\(3\\)\u3001\\(6\\) \u9019\u4e09\u500b\u96dc\u6e4a\u503c\u3002</p> \\[ \\begin{aligned} \\text{modulus} &amp; = 9 \\newline \\text{key} &amp; = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} &amp; = \\{ 0, 3, 6, 0, 3, 6, 0, 3, 6, 0, 3, 6,\\dots \\} \\end{aligned} \\] <p>\u5982\u679c\u8f38\u5165 <code>key</code> \u6070\u597d\u6eff\u8db3\u9019\u7a2e\u7b49\u5dee\u6578\u5217\u7684\u8cc7\u6599\u5206\u4f48\uff0c\u90a3\u9ebc\u96dc\u6e4a\u503c\u5c31\u6703\u51fa\u73fe\u805a\u5806\u7a4d\uff0c\u5f9e\u800c\u52a0\u91cd\u96dc\u6e4a\u885d\u7a81\u3002\u73fe\u5728\uff0c\u5047\u8a2d\u5c07 <code>modulus</code> \u66ff\u63db\u70ba\u8cea\u6578 \\(13\\) \uff0c\u7531\u65bc <code>key</code> \u548c <code>modulus</code> \u4e4b\u9593\u4e0d\u5b58\u5728\u516c\u7d04\u6578\uff0c\u56e0\u6b64\u8f38\u51fa\u7684\u96dc\u6e4a\u503c\u7684\u5747\u52fb\u6027\u6703\u660e\u986f\u63d0\u5347\u3002</p> \\[ \\begin{aligned} \\text{modulus} &amp; = 13 \\newline \\text{key} &amp; = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} &amp; = \\{ 0, 3, 6, 9, 12, 2, 5, 8, 11, 1, 4, 7, \\dots \\} \\end{aligned} \\] <p>\u503c\u5f97\u8aaa\u660e\u7684\u662f\uff0c\u5982\u679c\u80fd\u5920\u4fdd\u8b49 <code>key</code> \u662f\u96a8\u6a5f\u5747\u52fb\u5206\u4f48\u7684\uff0c\u90a3\u9ebc\u9078\u64c7\u8cea\u6578\u6216\u8005\u5408\u6578\u4f5c\u70ba\u6a21\u6578\u90fd\u53ef\u4ee5\uff0c\u5b83\u5011\u90fd\u80fd\u8f38\u51fa\u5747\u52fb\u5206\u4f48\u7684\u96dc\u6e4a\u503c\u3002\u800c\u7576 <code>key</code> \u7684\u5206\u4f48\u5b58\u5728\u67d0\u7a2e\u9031\u671f\u6027\u6642\uff0c\u5c0d\u5408\u6578\u53d6\u6a21\u66f4\u5bb9\u6613\u51fa\u73fe\u805a\u96c6\u73fe\u8c61\u3002</p> <p>\u7e3d\u800c\u8a00\u4e4b\uff0c\u6211\u5011\u901a\u5e38\u9078\u53d6\u8cea\u6578\u4f5c\u70ba\u6a21\u6578\uff0c\u4e26\u4e14\u9019\u500b\u8cea\u6578\u6700\u597d\u8db3\u5920\u5927\uff0c\u4ee5\u5118\u53ef\u80fd\u6d88\u9664\u9031\u671f\u6027\u6a21\u5f0f\uff0c\u63d0\u5347\u96dc\u6e4a\u6f14\u7b97\u6cd5\u7684\u7a69\u5065\u6027\u3002</p>"},{"location":"chapter_hashing/hash_algorithm/#633","title":"6.3.3 \u00a0 \u5e38\u898b\u96dc\u6e4a\u6f14\u7b97\u6cd5","text":"<p>\u4e0d\u96e3\u767c\u73fe\uff0c\u4ee5\u4e0a\u4ecb\u7d39\u7684\u7c21\u55ae\u96dc\u6e4a\u6f14\u7b97\u6cd5\u90fd\u6bd4\u8f03\u201c\u8106\u5f31\u201d\uff0c\u9060\u9060\u6c92\u6709\u9054\u5230\u96dc\u6e4a\u6f14\u7b97\u6cd5\u7684\u8a2d\u8a08\u76ee\u6a19\u3002\u4f8b\u5982\uff0c\u7531\u65bc\u52a0\u6cd5\u548c\u4e92\u65a5\u6216\u6eff\u8db3\u4ea4\u63db\u5f8b\uff0c\u56e0\u6b64\u52a0\u6cd5\u96dc\u6e4a\u548c\u4e92\u65a5\u6216\u96dc\u6e4a\u7121\u6cd5\u5340\u5206\u5167\u5bb9\u76f8\u540c\u4f46\u9806\u5e8f\u4e0d\u540c\u7684\u5b57\u4e32\uff0c\u9019\u53ef\u80fd\u6703\u52a0\u5287\u96dc\u6e4a\u885d\u7a81\uff0c\u4e26\u5f15\u8d77\u4e00\u4e9b\u5b89\u5168\u554f\u984c\u3002</p> <p>\u5728\u5be6\u969b\u4e2d\uff0c\u6211\u5011\u901a\u5e38\u6703\u7528\u4e00\u4e9b\u6a19\u6e96\u96dc\u6e4a\u6f14\u7b97\u6cd5\uff0c\u4f8b\u5982 MD5\u3001SHA-1\u3001SHA-2 \u548c SHA-3 \u7b49\u3002\u5b83\u5011\u53ef\u4ee5\u5c07\u4efb\u610f\u9577\u5ea6\u7684\u8f38\u5165\u8cc7\u6599\u5c0d\u6620\u5230\u6046\u5b9a\u9577\u5ea6\u7684\u96dc\u6e4a\u503c\u3002</p> <p>\u8fd1\u4e00\u500b\u4e16\u7d00\u4ee5\u4f86\uff0c\u96dc\u6e4a\u6f14\u7b97\u6cd5\u8655\u5728\u4e0d\u65b7\u5347\u7d1a\u8207\u6700\u4f73\u5316\u7684\u904e\u7a0b\u4e2d\u3002\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u54e1\u52aa\u529b\u63d0\u5347\u96dc\u6e4a\u6f14\u7b97\u6cd5\u7684\u6548\u80fd\uff0c\u53e6\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u54e1\u548c\u99ed\u5ba2\u5247\u81f4\u529b\u65bc\u5c0b\u627e\u96dc\u6e4a\u6f14\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u554f\u984c\u3002\u8868 6-2 \u5c55\u793a\u4e86\u5728\u5be6\u969b\u61c9\u7528\u4e2d\u5e38\u898b\u7684\u96dc\u6e4a\u6f14\u7b97\u6cd5\u3002</p> <ul> <li>MD5 \u548c SHA-1 \u5df2\u591a\u6b21\u88ab\u6210\u529f\u653b\u64ca\uff0c\u56e0\u6b64\u5b83\u5011\u88ab\u5404\u985e\u5b89\u5168\u61c9\u7528\u68c4\u7528\u3002</li> <li>SHA-2 \u7cfb\u5217\u4e2d\u7684 SHA-256 \u662f\u6700\u5b89\u5168\u7684\u96dc\u6e4a\u6f14\u7b97\u6cd5\u4e4b\u4e00\uff0c\u4ecd\u672a\u51fa\u73fe\u6210\u529f\u7684\u653b\u64ca\u6848\u4f8b\uff0c\u56e0\u6b64\u5e38\u7528\u5728\u5404\u985e\u5b89\u5168\u61c9\u7528\u8207\u5354\u8b70\u4e2d\u3002</li> <li>SHA-3 \u76f8\u8f03 SHA-2 \u7684\u5be6\u73fe\u958b\u92b7\u66f4\u4f4e\u3001\u8a08\u7b97\u6548\u7387\u66f4\u9ad8\uff0c\u4f46\u76ee\u524d\u4f7f\u7528\u8986\u84cb\u5ea6\u4e0d\u5982 SHA-2 \u7cfb\u5217\u3002</li> </ul> <p> \u8868 6-2 \u00a0 \u5e38\u898b\u7684\u96dc\u6e4a\u6f14\u7b97\u6cd5 </p> MD5 SHA-1 SHA-2 SHA-3 \u63a8\u51fa\u6642\u9593 1992 1995 2002 2008 \u8f38\u51fa\u9577\u5ea6 128 bit 160 bit 256/512 bit 224/256/384/512 bit \u96dc\u6e4a\u885d\u7a81 \u8f03\u591a \u8f03\u591a \u5f88\u5c11 \u5f88\u5c11 \u5b89\u5168\u7b49\u7d1a \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u64ca \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u64ca \u9ad8 \u9ad8 \u61c9\u7528 \u5df2\u88ab\u68c4\u7528\uff0c\u4ecd\u7528\u65bc\u8cc7\u6599\u5b8c\u6574\u6027\u6aa2\u67e5 \u5df2\u88ab\u68c4\u7528 \u52a0\u5bc6\u8ca8\u5e63\u4ea4\u6613\u9a57\u8b49\u3001\u6578\u5b57\u7c3d\u540d\u7b49 \u53ef\u7528\u65bc\u66ff\u4ee3 SHA-2"},{"location":"chapter_hashing/hash_algorithm/#634","title":"6.3.4 \u00a0 \u8cc7\u6599\u7d50\u69cb\u7684\u96dc\u6e4a\u503c","text":"<p>\u6211\u5011\u77e5\u9053\uff0c\u96dc\u6e4a\u8868\u7684 <code>key</code> \u53ef\u4ee5\u662f\u6574\u6578\u3001\u5c0f\u6578\u6216\u5b57\u4e32\u7b49\u8cc7\u6599\u578b\u5225\u3002\u7a0b\u5f0f\u8a9e\u8a00\u901a\u5e38\u6703\u70ba\u9019\u4e9b\u8cc7\u6599\u578b\u5225\u63d0\u4f9b\u5167\u5efa\u7684\u96dc\u6e4a\u6f14\u7b97\u6cd5\uff0c\u7528\u65bc\u8a08\u7b97\u96dc\u6e4a\u8868\u4e2d\u7684\u6876\u7d22\u5f15\u3002\u4ee5 Python \u70ba\u4f8b\uff0c\u6211\u5011\u53ef\u4ee5\u547c\u53eb <code>hash()</code> \u51fd\u5f0f\u4f86\u8a08\u7b97\u5404\u7a2e\u8cc7\u6599\u578b\u5225\u7684\u96dc\u6e4a\u503c\u3002</p> <ul> <li>\u6574\u6578\u548c\u5e03\u6797\u91cf\u7684\u96dc\u6e4a\u503c\u5c31\u662f\u5176\u672c\u8eab\u3002</li> <li>\u6d6e\u9ede\u6578\u548c\u5b57\u4e32\u7684\u96dc\u6e4a\u503c\u8a08\u7b97\u8f03\u70ba\u8907\u96dc\uff0c\u6709\u8208\u8da3\u7684\u8b80\u8005\u8acb\u81ea\u884c\u5b78\u7fd2\u3002</li> <li>\u5143\u7d44\u7684\u96dc\u6e4a\u503c\u662f\u5c0d\u5176\u4e2d\u6bcf\u4e00\u500b\u5143\u7d20\u9032\u884c\u96dc\u6e4a\uff0c\u7136\u5f8c\u5c07\u9019\u4e9b\u96dc\u6e4a\u503c\u7d44\u5408\u8d77\u4f86\uff0c\u5f97\u5230\u55ae\u4e00\u7684\u96dc\u6e4a\u503c\u3002</li> <li>\u7269\u4ef6\u7684\u96dc\u6e4a\u503c\u57fa\u65bc\u5176\u8a18\u61b6\u9ad4\u4f4d\u5740\u751f\u6210\u3002\u900f\u904e\u91cd\u5beb\u7269\u4ef6\u7684\u96dc\u6e4a\u65b9\u6cd5\uff0c\u53ef\u5be6\u73fe\u57fa\u65bc\u5167\u5bb9\u751f\u6210\u96dc\u6e4a\u503c\u3002</li> </ul> <p>Tip</p> <p>\u8acb\u6ce8\u610f\uff0c\u4e0d\u540c\u7a0b\u5f0f\u8a9e\u8a00\u7684\u5167\u5efa\u96dc\u6e4a\u503c\u8a08\u7b97\u51fd\u5f0f\u7684\u5b9a\u7fa9\u548c\u65b9\u6cd5\u4e0d\u540c\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig built_in_hash.py<pre><code>num = 3\nhash_num = hash(num)\n# \u6574\u6578 3 \u7684\u96dc\u6e4a\u503c\u70ba 3\n\nbol = True\nhash_bol = hash(bol)\n# \u5e03\u6797\u91cf True \u7684\u96dc\u6e4a\u503c\u70ba 1\n\ndec = 3.14159\nhash_dec = hash(dec)\n# \u5c0f\u6578 3.14159 \u7684\u96dc\u6e4a\u503c\u70ba 326484311674566659\n\nstr = \"Hello \u6f14\u7b97\u6cd5\"\nhash_str = hash(str)\n# \u5b57\u4e32\u201cHello \u6f14\u7b97\u6cd5\u201d\u7684\u96dc\u6e4a\u503c\u70ba 4617003410720528961\n\ntup = (12836, \"\u5c0f\u54c8\")\nhash_tup = hash(tup)\n# \u5143\u7d44 (12836, '\u5c0f\u54c8') \u7684\u96dc\u6e4a\u503c\u70ba 1029005403108185979\n\nobj = ListNode(0)\nhash_obj = hash(obj)\n# \u7bc0\u9ede\u7269\u4ef6 &lt;ListNode object at 0x1058fd810&gt; \u7684\u96dc\u6e4a\u503c\u70ba 274267521\n</code></pre> built_in_hash.cpp<pre><code>int num = 3;\nsize_t hashNum = hash&lt;int&gt;()(num);\n// \u6574\u6578 3 \u7684\u96dc\u6e4a\u503c\u70ba 3\n\nbool bol = true;\nsize_t hashBol = hash&lt;bool&gt;()(bol);\n// \u5e03\u6797\u91cf 1 \u7684\u96dc\u6e4a\u503c\u70ba 1\n\ndouble dec = 3.14159;\nsize_t hashDec = hash&lt;double&gt;()(dec);\n// \u5c0f\u6578 3.14159 \u7684\u96dc\u6e4a\u503c\u70ba 4614256650576692846\n\nstring str = \"Hello \u6f14\u7b97\u6cd5\";\nsize_t hashStr = hash&lt;string&gt;()(str);\n// \u5b57\u4e32\u201cHello \u6f14\u7b97\u6cd5\u201d\u7684\u96dc\u6e4a\u503c\u70ba 15466937326284535026\n\n// \u5728 C++ \u4e2d\uff0c\u5167\u5efa std:hash() \u50c5\u63d0\u4f9b\u57fa\u672c\u8cc7\u6599\u578b\u5225\u7684\u96dc\u6e4a\u503c\u8a08\u7b97\n// \u9663\u5217\u3001\u7269\u4ef6\u7684\u96dc\u6e4a\u503c\u8a08\u7b97\u9700\u8981\u81ea\u884c\u5be6\u73fe\n</code></pre> built_in_hash.java<pre><code>int num = 3;\nint hashNum = Integer.hashCode(num);\n// \u6574\u6578 3 \u7684\u96dc\u6e4a\u503c\u70ba 3\n\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// \u5e03\u6797\u91cf true \u7684\u96dc\u6e4a\u503c\u70ba 1231\n\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// \u5c0f\u6578 3.14159 \u7684\u96dc\u6e4a\u503c\u70ba -1340954729\n\nString str = \"Hello \u6f14\u7b97\u6cd5\";\nint hashStr = str.hashCode();\n// \u5b57\u4e32\u201cHello \u6f14\u7b97\u6cd5\u201d\u7684\u96dc\u6e4a\u503c\u70ba -727081396\n\nObject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = Arrays.hashCode(arr);\n// \u9663\u5217 [12836, \u5c0f\u54c8] \u7684\u96dc\u6e4a\u503c\u70ba 1151158\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// \u7bc0\u9ede\u7269\u4ef6 utils.ListNode@7dc5e7b4 \u7684\u96dc\u6e4a\u503c\u70ba 2110121908\n</code></pre> built_in_hash.cs<pre><code>int num = 3;\nint hashNum = num.GetHashCode();\n// \u6574\u6578 3 \u7684\u96dc\u6e4a\u503c\u70ba 3;\n\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// \u5e03\u6797\u91cf true \u7684\u96dc\u6e4a\u503c\u70ba 1;\n\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// \u5c0f\u6578 3.14159 \u7684\u96dc\u6e4a\u503c\u70ba -1340954729;\n\nstring str = \"Hello \u6f14\u7b97\u6cd5\";\nint hashStr = str.GetHashCode();\n// \u5b57\u4e32\u201cHello \u6f14\u7b97\u6cd5\u201d\u7684\u96dc\u6e4a\u503c\u70ba -586107568;\n\nobject[] arr = [12836, \"\u5c0f\u54c8\"];\nint hashTup = arr.GetHashCode();\n// \u9663\u5217 [12836, \u5c0f\u54c8] \u7684\u96dc\u6e4a\u503c\u70ba 42931033;\n\nListNode obj = new(0);\nint hashObj = obj.GetHashCode();\n// \u7bc0\u9ede\u7269\u4ef6 0 \u7684\u96dc\u6e4a\u503c\u70ba 39053774;\n</code></pre> built_in_hash.go<pre><code>// Go \u672a\u63d0\u4f9b\u5167\u5efa hash code \u51fd\u5f0f\n</code></pre> built_in_hash.swift<pre><code>let num = 3\nlet hashNum = num.hashValue\n// \u6574\u6578 3 \u7684\u96dc\u6e4a\u503c\u70ba 9047044699613009734\n\nlet bol = true\nlet hashBol = bol.hashValue\n// \u5e03\u6797\u91cf true \u7684\u96dc\u6e4a\u503c\u70ba -4431640247352757451\n\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// \u5c0f\u6578 3.14159 \u7684\u96dc\u6e4a\u503c\u70ba -2465384235396674631\n\nlet str = \"Hello \u6f14\u7b97\u6cd5\"\nlet hashStr = str.hashValue\n// \u5b57\u4e32\u201cHello \u6f14\u7b97\u6cd5\u201d\u7684\u96dc\u6e4a\u503c\u70ba -7850626797806988787\n\nlet arr = [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\nlet hashTup = arr.hashValue\n// \u9663\u5217 [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")] \u7684\u96dc\u6e4a\u503c\u70ba -2308633508154532996\n\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// \u7bc0\u9ede\u7269\u4ef6 utils.ListNode \u7684\u96dc\u6e4a\u503c\u70ba -2434780518035996159\n</code></pre> built_in_hash.js<pre><code>// JavaScript \u672a\u63d0\u4f9b\u5167\u5efa hash code \u51fd\u5f0f\n</code></pre> built_in_hash.ts<pre><code>// TypeScript \u672a\u63d0\u4f9b\u5167\u5efa hash code \u51fd\u5f0f\n</code></pre> built_in_hash.dart<pre><code>int num = 3;\nint hashNum = num.hashCode;\n// \u6574\u6578 3 \u7684\u96dc\u6e4a\u503c\u70ba 34803\n\nbool bol = true;\nint hashBol = bol.hashCode;\n// \u5e03\u6797\u503c true \u7684\u96dc\u6e4a\u503c\u70ba 1231\n\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// \u5c0f\u6578 3.14159 \u7684\u96dc\u6e4a\u503c\u70ba 2570631074981783\n\nString str = \"Hello \u6f14\u7b97\u6cd5\";\nint hashStr = str.hashCode;\n// \u5b57\u4e32\u201cHello \u6f14\u7b97\u6cd5\u201d\u7684\u96dc\u6e4a\u503c\u70ba 468167534\n\nList arr = [12836, \"\u5c0f\u54c8\"];\nint hashArr = arr.hashCode;\n// \u9663\u5217 [12836, \u5c0f\u54c8] \u7684\u96dc\u6e4a\u503c\u70ba 976512528\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// \u7bc0\u9ede\u7269\u4ef6 Instance of 'ListNode' \u7684\u96dc\u6e4a\u503c\u70ba 1033450432\n</code></pre> built_in_hash.rs<pre><code>use std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\n\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&amp;mut num_hasher);\nlet hash_num = num_hasher.finish();\n// \u6574\u6578 3 \u7684\u96dc\u6e4a\u503c\u70ba 568126464209439262\n\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&amp;mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// \u5e03\u6797\u91cf true \u7684\u96dc\u6e4a\u503c\u70ba 4952851536318644461\n\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&amp;mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\n// \u5c0f\u6578 3.14159 \u7684\u96dc\u6e4a\u503c\u70ba 2566941990314602357\n\nlet str = \"Hello \u6f14\u7b97\u6cd5\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&amp;mut str_hasher);\nlet hash_str = str_hasher.finish();\n// \u5b57\u4e32\u201cHello \u6f14\u7b97\u6cd5\u201d\u7684\u96dc\u6e4a\u503c\u70ba 16092673739211250988\n\nlet arr = (&amp;12836, &amp;\"\u5c0f\u54c8\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&amp;mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// \u5143\u7d44 (12836, \"\u5c0f\u54c8\") \u7684\u96dc\u6e4a\u503c\u70ba 1885128010422702749\n\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&amp;mut hasher);\nlet hash = hasher.finish();\n// \u7bc0\u9ede\u7269\u4ef6 RefCell { value: ListNode { val: 42, next: None } } \u7684\u96dc\u6e4a\u503c\u70ba15387811073369036852\n</code></pre> built_in_hash.c<pre><code>// C \u672a\u63d0\u4f9b\u5167\u5efa hash code \u51fd\u5f0f\n</code></pre> built_in_hash.kt<pre><code>val num = 3\nval hashNum = num.hashCode()\n// \u6574\u6578 3 \u7684\u96dc\u6e4a\u503c\u70ba 3\n\nval bol = true\nval hashBol = bol.hashCode()\n// \u5e03\u6797\u91cf true \u7684\u96dc\u6e4a\u503c\u70ba 1231\n\nval dec = 3.14159\nval hashDec = dec.hashCode()\n// \u5c0f\u6578 3.14159 \u7684\u96dc\u6e4a\u503c\u70ba -1340954729\n\nval str = \"Hello \u6f14\u7b97\u6cd5\"\nval hashStr = str.hashCode()\n// \u5b57\u4e32\u201cHello \u6f14\u7b97\u6cd5\u201d\u7684\u96dc\u6e4a\u503c\u70ba -727081396\n\nval arr = arrayOf&lt;Any&gt;(12836, \"\u5c0f\u54c8\")\nval hashTup = arr.hashCode()\n// \u9663\u5217 [12836, \u5c0f\u54c8] \u7684\u96dc\u6e4a\u503c\u70ba 189568618\n\nval obj = ListNode(0)\nval hashObj = obj.hashCode()\n// \u7bc0\u9ede\u7269\u4ef6 utils.ListNode@1d81eb93 \u7684\u96dc\u6e4a\u503c\u70ba 495053715\n</code></pre> built_in_hash.rb<pre><code>num = 3\nhash_num = num.hash\n# \u6574\u6578 3 \u7684\u96dc\u6e4a\u503c\u70ba -4385856518450339636\n\nbol = true\nhash_bol = bol.hash\n# \u5e03\u6797\u91cf true \u7684\u96dc\u6e4a\u503c\u70ba -1617938112149317027\n\ndec = 3.14159\nhash_dec = dec.hash\n# \u5c0f\u6578 3.14159 \u7684\u96dc\u6e4a\u503c\u70ba -1479186995943067893\n\nstr = \"Hello \u6f14\u7b97\u6cd5\"\nhash_str = str.hash\n# \u5b57\u4e32\u201cHello \u6f14\u7b97\u6cd5\u201d\u7684\u96dc\u6e4a\u503c\u70ba -4075943250025831763\n\ntup = [12836, '\u5c0f\u54c8']\nhash_tup = tup.hash\n# \u5143\u7d44 (12836, '\u5c0f\u54c8') \u7684\u96dc\u6e4a\u503c\u70ba 1999544809202288822\n\nobj = ListNode.new(0)\nhash_obj = obj.hash\n# \u7bc0\u9ede\u7269\u4ef6 #&lt;ListNode:0x000078133140ab70&gt; \u7684\u96dc\u6e4a\u503c\u70ba 4302940560806366381\n</code></pre> built_in_hash.zig<pre><code>\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5728\u8a31\u591a\u7a0b\u5f0f\u8a9e\u8a00\u4e2d\uff0c\u53ea\u6709\u4e0d\u53ef\u8b8a\u7269\u4ef6\u624d\u53ef\u4f5c\u70ba\u96dc\u6e4a\u8868\u7684 <code>key</code> \u3002\u5047\u5982\u6211\u5011\u5c07\u4e32\u5217\uff08\u52d5\u614b\u9663\u5217\uff09\u4f5c\u70ba <code>key</code> \uff0c\u7576\u4e32\u5217\u7684\u5167\u5bb9\u767c\u751f\u8b8a\u5316\u6642\uff0c\u5b83\u7684\u96dc\u6e4a\u503c\u4e5f\u96a8\u4e4b\u6539\u8b8a\uff0c\u6211\u5011\u5c31\u7121\u6cd5\u5728\u96dc\u6e4a\u8868\u4e2d\u67e5\u8a62\u5230\u539f\u5148\u7684 <code>value</code> \u4e86\u3002</p> <p>\u96d6\u7136\u81ea\u5b9a\u7fa9\u7269\u4ef6\uff08\u6bd4\u5982\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\uff09\u7684\u6210\u54e1\u8b8a\u6578\u662f\u53ef\u8b8a\u7684\uff0c\u4f46\u5b83\u662f\u53ef\u96dc\u6e4a\u7684\u3002\u9019\u662f\u56e0\u70ba\u7269\u4ef6\u7684\u96dc\u6e4a\u503c\u901a\u5e38\u662f\u57fa\u65bc\u8a18\u61b6\u9ad4\u4f4d\u5740\u751f\u6210\u7684\uff0c\u5373\u4f7f\u7269\u4ef6\u7684\u5167\u5bb9\u767c\u751f\u4e86\u8b8a\u5316\uff0c\u4f46\u5b83\u7684\u8a18\u61b6\u9ad4\u4f4d\u5740\u4e0d\u8b8a\uff0c\u96dc\u6e4a\u503c\u4ecd\u7136\u662f\u4e0d\u8b8a\u7684\u3002</p> <p>\u7d30\u5fc3\u7684\u4f60\u53ef\u80fd\u767c\u73fe\u5728\u4e0d\u540c\u63a7\u5236\u6aaf\u4e2d\u57f7\u884c\u7a0b\u5f0f\u6642\uff0c\u8f38\u51fa\u7684\u96dc\u6e4a\u503c\u662f\u4e0d\u540c\u7684\u3002\u9019\u662f\u56e0\u70ba Python \u76f4\u8b6f\u5668\u5728\u6bcf\u6b21\u555f\u52d5\u6642\uff0c\u90fd\u6703\u70ba\u5b57\u4e32\u96dc\u6e4a\u51fd\u5f0f\u52a0\u5165\u4e00\u500b\u96a8\u6a5f\u7684\u9e7d\uff08salt\uff09\u503c\u3002\u9019\u7a2e\u505a\u6cd5\u53ef\u4ee5\u6709\u6548\u9632\u6b62 HashDoS \u653b\u64ca\uff0c\u63d0\u5347\u96dc\u6e4a\u6f14\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u3002</p>"},{"location":"chapter_hashing/hash_collision/","title":"6.2 \u00a0 \u96dc\u6e4a\u885d\u7a81","text":"<p>\u4e0a\u4e00\u7bc0\u63d0\u5230\uff0c\u901a\u5e38\u60c5\u6cc1\u4e0b\u96dc\u6e4a\u51fd\u5f0f\u7684\u8f38\u5165\u7a7a\u9593\u9060\u5927\u65bc\u8f38\u51fa\u7a7a\u9593\uff0c\u56e0\u6b64\u7406\u8ad6\u4e0a\u96dc\u6e4a\u885d\u7a81\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u6bd4\u5982\uff0c\u8f38\u5165\u7a7a\u9593\u70ba\u5168\u9ad4\u6574\u6578\uff0c\u8f38\u51fa\u7a7a\u9593\u70ba\u9663\u5217\u5bb9\u91cf\u5927\u5c0f\uff0c\u5247\u5fc5\u7136\u6709\u591a\u500b\u6574\u6578\u5c0d\u6620\u81f3\u540c\u4e00\u6876\u7d22\u5f15\u3002</p> <p>\u96dc\u6e4a\u885d\u7a81\u6703\u5c0e\u81f4\u67e5\u8a62\u7d50\u679c\u932f\u8aa4\uff0c\u56b4\u91cd\u5f71\u97ff\u96dc\u6e4a\u8868\u7684\u53ef\u7528\u6027\u3002\u70ba\u4e86\u89e3\u6c7a\u8a72\u554f\u984c\uff0c\u6bcf\u7576\u9047\u5230\u96dc\u6e4a\u885d\u7a81\u6642\uff0c\u6211\u5011\u5c31\u9032\u884c\u96dc\u6e4a\u8868\u64f4\u5bb9\uff0c\u76f4\u81f3\u885d\u7a81\u6d88\u5931\u70ba\u6b62\u3002\u6b64\u65b9\u6cd5\u7c21\u55ae\u7c97\u66b4\u4e14\u6709\u6548\uff0c\u4f46\u6548\u7387\u592a\u4f4e\uff0c\u56e0\u70ba\u96dc\u6e4a\u8868\u64f4\u5bb9\u9700\u8981\u9032\u884c\u5927\u91cf\u7684\u8cc7\u6599\u642c\u904b\u8207\u96dc\u6e4a\u503c\u8a08\u7b97\u3002\u70ba\u4e86\u63d0\u5347\u6548\u7387\uff0c\u6211\u5011\u53ef\u4ee5\u63a1\u7528\u4ee5\u4e0b\u7b56\u7565\u3002</p> <ol> <li>\u6539\u826f\u96dc\u6e4a\u8868\u8cc7\u6599\u7d50\u69cb\uff0c\u4f7f\u5f97\u96dc\u6e4a\u8868\u53ef\u4ee5\u5728\u51fa\u73fe\u96dc\u6e4a\u885d\u7a81\u6642\u6b63\u5e38\u5de5\u4f5c\u3002</li> <li>\u50c5\u5728\u5fc5\u8981\u6642\uff0c\u5373\u7576\u96dc\u6e4a\u885d\u7a81\u6bd4\u8f03\u56b4\u91cd\u6642\uff0c\u624d\u57f7\u884c\u64f4\u5bb9\u64cd\u4f5c\u3002</li> </ol> <p>\u96dc\u6e4a\u8868\u7684\u7d50\u69cb\u6539\u826f\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u201c\u93c8\u5f0f\u4f4d\u5740\u201d\u548c\u201c\u958b\u653e\u5b9a\u5740\u201d\u3002</p>"},{"location":"chapter_hashing/hash_collision/#621","title":"6.2.1 \u00a0 \u93c8\u5f0f\u4f4d\u5740","text":"<p>\u5728\u539f\u59cb\u96dc\u6e4a\u8868\u4e2d\uff0c\u6bcf\u500b\u6876\u50c5\u80fd\u5132\u5b58\u4e00\u500b\u9375\u503c\u5c0d\u3002\u93c8\u5f0f\u4f4d\u5740\uff08separate chaining\uff09\u5c07\u55ae\u500b\u5143\u7d20\u8f49\u63db\u70ba\u93c8\u7d50\u4e32\u5217\uff0c\u5c07\u9375\u503c\u5c0d\u4f5c\u70ba\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\uff0c\u5c07\u6240\u6709\u767c\u751f\u885d\u7a81\u7684\u9375\u503c\u5c0d\u90fd\u5132\u5b58\u5728\u540c\u4e00\u93c8\u7d50\u4e32\u5217\u4e2d\u3002\u5716 6-5 \u5c55\u793a\u4e86\u4e00\u500b\u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868\u7684\u4f8b\u5b50\u3002</p> <p></p> <p> \u5716 6-5 \u00a0 \u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868 </p> <p>\u57fa\u65bc\u93c8\u5f0f\u4f4d\u5740\u5be6\u73fe\u7684\u96dc\u6e4a\u8868\u7684\u64cd\u4f5c\u65b9\u6cd5\u767c\u751f\u4e86\u4ee5\u4e0b\u8b8a\u5316\u3002</p> <ul> <li>\u67e5\u8a62\u5143\u7d20\uff1a\u8f38\u5165 <code>key</code> \uff0c\u7d93\u904e\u96dc\u6e4a\u51fd\u5f0f\u5f97\u5230\u6876\u7d22\u5f15\uff0c\u5373\u53ef\u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u982d\u7bc0\u9ede\uff0c\u7136\u5f8c\u8d70\u8a2a\u93c8\u7d50\u4e32\u5217\u4e26\u5c0d\u6bd4 <code>key</code> \u4ee5\u67e5\u8a62\u76ee\u6a19\u9375\u503c\u5c0d\u3002</li> <li>\u65b0\u589e\u5143\u7d20\uff1a\u9996\u5148\u900f\u904e\u96dc\u6e4a\u51fd\u5f0f\u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u982d\u7bc0\u9ede\uff0c\u7136\u5f8c\u5c07\u7bc0\u9ede\uff08\u9375\u503c\u5c0d\uff09\u65b0\u589e\u5230\u93c8\u7d50\u4e32\u5217\u4e2d\u3002</li> <li>\u522a\u9664\u5143\u7d20\uff1a\u6839\u64da\u96dc\u6e4a\u51fd\u5f0f\u7684\u7d50\u679c\u8a2a\u554f\u93c8\u7d50\u4e32\u5217\u982d\u90e8\uff0c\u63a5\u8457\u8d70\u8a2a\u93c8\u7d50\u4e32\u5217\u4ee5\u67e5\u8a62\u76ee\u6a19\u7bc0\u9ede\u4e26\u5c07\u5176\u522a\u9664\u3002</li> </ul> <p>\u93c8\u5f0f\u4f4d\u5740\u5b58\u5728\u4ee5\u4e0b\u4fb7\u9650\u6027\u3002</p> <ul> <li>\u4f54\u7528\u7a7a\u9593\u589e\u5927\uff1a\u93c8\u7d50\u4e32\u5217\u5305\u542b\u7bc0\u9ede\u6307\u6a19\uff0c\u5b83\u76f8\u6bd4\u9663\u5217\u66f4\u52a0\u8017\u8cbb\u8a18\u61b6\u9ad4\u7a7a\u9593\u3002</li> <li>\u67e5\u8a62\u6548\u7387\u964d\u4f4e\uff1a\u56e0\u70ba\u9700\u8981\u7dda\u6027\u8d70\u8a2a\u93c8\u7d50\u4e32\u5217\u4f86\u67e5\u8a62\u5c0d\u61c9\u5143\u7d20\u3002</li> </ul> <p>\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u7d66\u51fa\u4e86\u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868\u7684\u7c21\u55ae\u5be6\u73fe\uff0c\u9700\u8981\u6ce8\u610f\u5169\u9ede\u3002</p> <ul> <li>\u4f7f\u7528\u4e32\u5217\uff08\u52d5\u614b\u9663\u5217\uff09\u4ee3\u66ff\u93c8\u7d50\u4e32\u5217\uff0c\u5f9e\u800c\u7c21\u5316\u7a0b\u5f0f\u78bc\u3002\u5728\u9019\u7a2e\u8a2d\u5b9a\u4e0b\uff0c\u96dc\u6e4a\u8868\uff08\u9663\u5217\uff09\u5305\u542b\u591a\u500b\u6876\uff0c\u6bcf\u500b\u6876\u90fd\u662f\u4e00\u500b\u4e32\u5217\u3002</li> <li>\u4ee5\u4e0b\u5be6\u73fe\u5305\u542b\u96dc\u6e4a\u8868\u64f4\u5bb9\u65b9\u6cd5\u3002\u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e \\(\\frac{2}{3}\\) \u6642\uff0c\u6211\u5011\u5c07\u96dc\u6e4a\u8868\u64f4\u5bb9\u81f3\u539f\u5148\u7684 \\(2\\) \u500d\u3002</li> </ul> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig hash_map_chaining.py<pre><code>class HashMapChaining:\n \"\"\"\u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u5efa\u69cb\u5b50\"\"\"\n self.size = 0 # \u9375\u503c\u5c0d\u6578\u91cf\n self.capacity = 4 # \u96dc\u6e4a\u8868\u5bb9\u91cf\n self.load_thres = 2.0 / 3.0 # \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n self.extend_ratio = 2 # \u64f4\u5bb9\u500d\u6578\n self.buckets = [[] for _ in range(self.capacity)] # \u6876\u9663\u5217\n\n def hash_func(self, key: int) -&gt; int:\n \"\"\"\u96dc\u6e4a\u51fd\u5f0f\"\"\"\n return key % self.capacity\n\n def load_factor(self) -&gt; float:\n \"\"\"\u8ca0\u8f09\u56e0\u5b50\"\"\"\n return self.size / self.capacity\n\n def get(self, key: int) -&gt; str | None:\n \"\"\"\u67e5\u8a62\u64cd\u4f5c\"\"\"\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u8d70\u8a2a\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n for pair in bucket:\n if pair.key == key:\n return pair.val\n # \u82e5\u672a\u627e\u5230 key \uff0c\u5247\u8fd4\u56de None\n return None\n\n def put(self, key: int, val: str):\n \"\"\"\u65b0\u589e\u64cd\u4f5c\"\"\"\n # \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if self.load_factor() &gt; self.load_thres:\n self.extend()\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u8d70\u8a2a\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5247\u66f4\u65b0\u5c0d\u61c9 val \u4e26\u8fd4\u56de\n for pair in bucket:\n if pair.key == key:\n pair.val = val\n return\n # \u82e5\u7121\u8a72 key \uff0c\u5247\u5c07\u9375\u503c\u5c0d\u65b0\u589e\u81f3\u5c3e\u90e8\n pair = Pair(key, val)\n bucket.append(pair)\n self.size += 1\n\n def remove(self, key: int):\n \"\"\"\u522a\u9664\u64cd\u4f5c\"\"\"\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u8d70\u8a2a\u6876\uff0c\u5f9e\u4e2d\u522a\u9664\u9375\u503c\u5c0d\n for pair in bucket:\n if pair.key == key:\n bucket.remove(pair)\n self.size -= 1\n break\n\n def extend(self):\n \"\"\"\u64f4\u5bb9\u96dc\u6e4a\u8868\"\"\"\n # \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n buckets = self.buckets\n # \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n self.capacity *= self.extend_ratio\n self.buckets = [[] for _ in range(self.capacity)]\n self.size = 0\n # \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for bucket in buckets:\n for pair in bucket:\n self.put(pair.key, pair.val)\n\n def print(self):\n \"\"\"\u5217\u5370\u96dc\u6e4a\u8868\"\"\"\n for bucket in self.buckets:\n res = []\n for pair in bucket:\n res.append(str(pair.key) + \" -&gt; \" + pair.val)\n print(res)\n</code></pre> hash_map_chaining.cpp<pre><code>/* \u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868 */\nclass HashMapChaining {\n private:\n int size; // \u9375\u503c\u5c0d\u6578\u91cf\n int capacity; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n double loadThres; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n int extendRatio; // \u64f4\u5bb9\u500d\u6578\n vector&lt;vector&lt;Pair *&gt;&gt; buckets; // \u6876\u9663\u5217\n\n public:\n /* \u5efa\u69cb\u5b50 */\n HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3.0), extendRatio(2) {\n buckets.resize(capacity);\n }\n\n /* \u6790\u69cb\u65b9\u6cd5 */\n ~HashMapChaining() {\n for (auto &amp;bucket : buckets) {\n for (Pair *pair : bucket) {\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n delete pair;\n }\n }\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n double loadFactor() {\n return (double)size / (double)capacity;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n string get(int key) {\n int index = hashFunc(key);\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n for (Pair *pair : buckets[index]) {\n if (pair-&gt;key == key) {\n return pair-&gt;val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u7a7a\u5b57\u4e32\n return \"\";\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n void put(int key, string val) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (loadFactor() &gt; loadThres) {\n extend();\n }\n int index = hashFunc(key);\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5247\u66f4\u65b0\u5c0d\u61c9 val \u4e26\u8fd4\u56de\n for (Pair *pair : buckets[index]) {\n if (pair-&gt;key == key) {\n pair-&gt;val = val;\n return;\n }\n }\n // \u82e5\u7121\u8a72 key \uff0c\u5247\u5c07\u9375\u503c\u5c0d\u65b0\u589e\u81f3\u5c3e\u90e8\n buckets[index].push_back(new Pair(key, val));\n size++;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n auto &amp;bucket = buckets[index];\n // \u8d70\u8a2a\u6876\uff0c\u5f9e\u4e2d\u522a\u9664\u9375\u503c\u5c0d\n for (int i = 0; i &lt; bucket.size(); i++) {\n if (bucket[i]-&gt;key == key) {\n Pair *tmp = bucket[i];\n bucket.erase(bucket.begin() + i); // \u5f9e\u4e2d\u522a\u9664\u9375\u503c\u5c0d\n delete tmp; // \u91cb\u653e\u8a18\u61b6\u9ad4\n size--;\n return;\n }\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n void extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n vector&lt;vector&lt;Pair *&gt;&gt; bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n capacity *= extendRatio;\n buckets.clear();\n buckets.resize(capacity);\n size = 0;\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for (auto &amp;bucket : bucketsTmp) {\n for (Pair *pair : bucket) {\n put(pair-&gt;key, pair-&gt;val);\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n delete pair;\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n void print() {\n for (auto &amp;bucket : buckets) {\n cout &lt;&lt; \"[\";\n for (Pair *pair : bucket) {\n cout &lt;&lt; pair-&gt;key &lt;&lt; \" -&gt; \" &lt;&lt; pair-&gt;val &lt;&lt; \", \";\n }\n cout &lt;&lt; \"]\\n\";\n }\n }\n};\n</code></pre> hash_map_chaining.java<pre><code>/* \u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868 */\nclass HashMapChaining {\n int size; // \u9375\u503c\u5c0d\u6578\u91cf\n int capacity; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n double loadThres; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n int extendRatio; // \u64f4\u5bb9\u500d\u6578\n List&lt;List&lt;Pair&gt;&gt; buckets; // \u6876\u9663\u5217\n\n /* \u5efa\u69cb\u5b50 */\n public HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = new ArrayList&lt;&gt;(capacity);\n for (int i = 0; i &lt; capacity; i++) {\n buckets.add(new ArrayList&lt;&gt;());\n }\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n double loadFactor() {\n return (double) size / capacity;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n String get(int key) {\n int index = hashFunc(key);\n List&lt;Pair&gt; bucket = buckets.get(index);\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n for (Pair pair : bucket) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5247\u8fd4\u56de null\n return null;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n void put(int key, String val) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (loadFactor() &gt; loadThres) {\n extend();\n }\n int index = hashFunc(key);\n List&lt;Pair&gt; bucket = buckets.get(index);\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5247\u66f4\u65b0\u5c0d\u61c9 val \u4e26\u8fd4\u56de\n for (Pair pair : bucket) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u7121\u8a72 key \uff0c\u5247\u5c07\u9375\u503c\u5c0d\u65b0\u589e\u81f3\u5c3e\u90e8\n Pair pair = new Pair(key, val);\n bucket.add(pair);\n size++;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n List&lt;Pair&gt; bucket = buckets.get(index);\n // \u8d70\u8a2a\u6876\uff0c\u5f9e\u4e2d\u522a\u9664\u9375\u503c\u5c0d\n for (Pair pair : bucket) {\n if (pair.key == key) {\n bucket.remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n void extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n List&lt;List&lt;Pair&gt;&gt; bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n capacity *= extendRatio;\n buckets = new ArrayList&lt;&gt;(capacity);\n for (int i = 0; i &lt; capacity; i++) {\n buckets.add(new ArrayList&lt;&gt;());\n }\n size = 0;\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for (List&lt;Pair&gt; bucket : bucketsTmp) {\n for (Pair pair : bucket) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n void print() {\n for (List&lt;Pair&gt; bucket : buckets) {\n List&lt;String&gt; res = new ArrayList&lt;&gt;();\n for (Pair pair : bucket) {\n res.add(pair.key + \" -&gt; \" + pair.val);\n }\n System.out.println(res);\n }\n }\n}\n</code></pre> hash_map_chaining.cs<pre><code>/* \u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868 */\nclass HashMapChaining {\n int size; // \u9375\u503c\u5c0d\u6578\u91cf\n int capacity; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n double loadThres; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n int extendRatio; // \u64f4\u5bb9\u500d\u6578\n List&lt;List&lt;Pair&gt;&gt; buckets; // \u6876\u9663\u5217\n\n /* \u5efa\u69cb\u5b50 */\n public HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = new List&lt;List&lt;Pair&gt;&gt;(capacity);\n for (int i = 0; i &lt; capacity; i++) {\n buckets.Add([]);\n }\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n int HashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n double LoadFactor() {\n return (double)size / capacity;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n public string? Get(int key) {\n int index = HashFunc(key);\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n foreach (Pair pair in buckets[index]) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5247\u8fd4\u56de null\n return null;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n public void Put(int key, string val) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (LoadFactor() &gt; loadThres) {\n Extend();\n }\n int index = HashFunc(key);\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5247\u66f4\u65b0\u5c0d\u61c9 val \u4e26\u8fd4\u56de\n foreach (Pair pair in buckets[index]) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u7121\u8a72 key \uff0c\u5247\u5c07\u9375\u503c\u5c0d\u65b0\u589e\u81f3\u5c3e\u90e8\n buckets[index].Add(new Pair(key, val));\n size++;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n int index = HashFunc(key);\n // \u8d70\u8a2a\u6876\uff0c\u5f9e\u4e2d\u522a\u9664\u9375\u503c\u5c0d\n foreach (Pair pair in buckets[index].ToList()) {\n if (pair.key == key) {\n buckets[index].Remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n void Extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n List&lt;List&lt;Pair&gt;&gt; bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n capacity *= extendRatio;\n buckets = new List&lt;List&lt;Pair&gt;&gt;(capacity);\n for (int i = 0; i &lt; capacity; i++) {\n buckets.Add([]);\n }\n size = 0;\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n foreach (List&lt;Pair&gt; bucket in bucketsTmp) {\n foreach (Pair pair in bucket) {\n Put(pair.key, pair.val);\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n public void Print() {\n foreach (List&lt;Pair&gt; bucket in buckets) {\n List&lt;string&gt; res = [];\n foreach (Pair pair in bucket) {\n res.Add(pair.key + \" -&gt; \" + pair.val);\n }\n foreach (string kv in res) {\n Console.WriteLine(kv);\n }\n }\n }\n}\n</code></pre> hash_map_chaining.go<pre><code>/* \u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868 */\ntype hashMapChaining struct {\n size int // \u9375\u503c\u5c0d\u6578\u91cf\n capacity int // \u96dc\u6e4a\u8868\u5bb9\u91cf\n loadThres float64 // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n extendRatio int // \u64f4\u5bb9\u500d\u6578\n buckets [][]pair // \u6876\u9663\u5217\n}\n\n/* \u5efa\u69cb\u5b50 */\nfunc newHashMapChaining() *hashMapChaining {\n buckets := make([][]pair, 4)\n for i := 0; i &lt; 4; i++ {\n buckets[i] = make([]pair, 0)\n }\n return &amp;hashMapChaining{\n size: 0,\n capacity: 4,\n loadThres: 2.0 / 3.0,\n extendRatio: 2,\n buckets: buckets,\n }\n}\n\n/* \u96dc\u6e4a\u51fd\u5f0f */\nfunc (m *hashMapChaining) hashFunc(key int) int {\n return key % m.capacity\n}\n\n/* \u8ca0\u8f09\u56e0\u5b50 */\nfunc (m *hashMapChaining) loadFactor() float64 {\n return float64(m.size) / float64(m.capacity)\n}\n\n/* \u67e5\u8a62\u64cd\u4f5c */\nfunc (m *hashMapChaining) get(key int) string {\n idx := m.hashFunc(key)\n bucket := m.buckets[idx]\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n for _, p := range bucket {\n if p.key == key {\n return p.val\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u7a7a\u5b57\u4e32\n return \"\"\n}\n\n/* \u65b0\u589e\u64cd\u4f5c */\nfunc (m *hashMapChaining) put(key int, val string) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if m.loadFactor() &gt; m.loadThres {\n m.extend()\n }\n idx := m.hashFunc(key)\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5247\u66f4\u65b0\u5c0d\u61c9 val \u4e26\u8fd4\u56de\n for i := range m.buckets[idx] {\n if m.buckets[idx][i].key == key {\n m.buckets[idx][i].val = val\n return\n }\n }\n // \u82e5\u7121\u8a72 key \uff0c\u5247\u5c07\u9375\u503c\u5c0d\u65b0\u589e\u81f3\u5c3e\u90e8\n p := pair{\n key: key,\n val: val,\n }\n m.buckets[idx] = append(m.buckets[idx], p)\n m.size += 1\n}\n\n/* \u522a\u9664\u64cd\u4f5c */\nfunc (m *hashMapChaining) remove(key int) {\n idx := m.hashFunc(key)\n // \u8d70\u8a2a\u6876\uff0c\u5f9e\u4e2d\u522a\u9664\u9375\u503c\u5c0d\n for i, p := range m.buckets[idx] {\n if p.key == key {\n // \u5207\u7247\u522a\u9664\n m.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)\n m.size -= 1\n break\n }\n }\n}\n\n/* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\nfunc (m *hashMapChaining) extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n tmpBuckets := make([][]pair, len(m.buckets))\n for i := 0; i &lt; len(m.buckets); i++ {\n tmpBuckets[i] = make([]pair, len(m.buckets[i]))\n copy(tmpBuckets[i], m.buckets[i])\n }\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n m.capacity *= m.extendRatio\n m.buckets = make([][]pair, m.capacity)\n for i := 0; i &lt; m.capacity; i++ {\n m.buckets[i] = make([]pair, 0)\n }\n m.size = 0\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for _, bucket := range tmpBuckets {\n for _, p := range bucket {\n m.put(p.key, p.val)\n }\n }\n}\n\n/* \u5217\u5370\u96dc\u6e4a\u8868 */\nfunc (m *hashMapChaining) print() {\n var builder strings.Builder\n\n for _, bucket := range m.buckets {\n builder.WriteString(\"[\")\n for _, p := range bucket {\n builder.WriteString(strconv.Itoa(p.key) + \" -&gt; \" + p.val + \" \")\n }\n builder.WriteString(\"]\")\n fmt.Println(builder.String())\n builder.Reset()\n }\n}\n</code></pre> hash_map_chaining.swift<pre><code>/* \u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868 */\nclass HashMapChaining {\n var size: Int // \u9375\u503c\u5c0d\u6578\u91cf\n var capacity: Int // \u96dc\u6e4a\u8868\u5bb9\u91cf\n var loadThres: Double // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n var extendRatio: Int // \u64f4\u5bb9\u500d\u6578\n var buckets: [[Pair]] // \u6876\u9663\u5217\n\n /* \u5efa\u69cb\u5b50 */\n init() {\n size = 0\n capacity = 4\n loadThres = 2.0 / 3.0\n extendRatio = 2\n buckets = Array(repeating: [], count: capacity)\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n func hashFunc(key: Int) -&gt; Int {\n key % capacity\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n func loadFactor() -&gt; Double {\n Double(size) / Double(capacity)\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n func get(key: Int) -&gt; String? {\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n for pair in bucket {\n if pair.key == key {\n return pair.val\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5247\u8fd4\u56de nil\n return nil\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n func put(key: Int, val: String) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if loadFactor() &gt; loadThres {\n extend()\n }\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5247\u66f4\u65b0\u5c0d\u61c9 val \u4e26\u8fd4\u56de\n for pair in bucket {\n if pair.key == key {\n pair.val = val\n return\n }\n }\n // \u82e5\u7121\u8a72 key \uff0c\u5247\u5c07\u9375\u503c\u5c0d\u65b0\u589e\u81f3\u5c3e\u90e8\n let pair = Pair(key: key, val: val)\n buckets[index].append(pair)\n size += 1\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u8d70\u8a2a\u6876\uff0c\u5f9e\u4e2d\u522a\u9664\u9375\u503c\u5c0d\n for (pairIndex, pair) in bucket.enumerated() {\n if pair.key == key {\n buckets[index].remove(at: pairIndex)\n size -= 1\n break\n }\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n func extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n let bucketsTmp = buckets\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n capacity *= extendRatio\n buckets = Array(repeating: [], count: capacity)\n size = 0\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for bucket in bucketsTmp {\n for pair in bucket {\n put(key: pair.key, val: pair.val)\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n func print() {\n for bucket in buckets {\n let res = bucket.map { \"\\($0.key) -&gt; \\($0.val)\" }\n Swift.print(res)\n }\n }\n}\n</code></pre> hash_map_chaining.js<pre><code>/* \u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868 */\nclass HashMapChaining {\n #size; // \u9375\u503c\u5c0d\u6578\u91cf\n #capacity; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n #loadThres; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n #extendRatio; // \u64f4\u5bb9\u500d\u6578\n #buckets; // \u6876\u9663\u5217\n\n /* \u5efa\u69cb\u5b50 */\n constructor() {\n this.#size = 0;\n this.#capacity = 4;\n this.#loadThres = 2.0 / 3.0;\n this.#extendRatio = 2;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) =&gt; []);\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n #hashFunc(key) {\n return key % this.#capacity;\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n #loadFactor() {\n return this.#size / this.#capacity;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n get(key) {\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n for (const pair of bucket) {\n if (pair.key === key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5247\u8fd4\u56de null\n return null;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n put(key, val) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (this.#loadFactor() &gt; this.#loadThres) {\n this.#extend();\n }\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5247\u66f4\u65b0\u5c0d\u61c9 val \u4e26\u8fd4\u56de\n for (const pair of bucket) {\n if (pair.key === key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u7121\u8a72 key \uff0c\u5247\u5c07\u9375\u503c\u5c0d\u65b0\u589e\u81f3\u5c3e\u90e8\n const pair = new Pair(key, val);\n bucket.push(pair);\n this.#size++;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n remove(key) {\n const index = this.#hashFunc(key);\n let bucket = this.#buckets[index];\n // \u8d70\u8a2a\u6876\uff0c\u5f9e\u4e2d\u522a\u9664\u9375\u503c\u5c0d\n for (let i = 0; i &lt; bucket.length; i++) {\n if (bucket[i].key === key) {\n bucket.splice(i, 1);\n this.#size--;\n break;\n }\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n #extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) =&gt; []);\n this.#size = 0;\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for (const bucket of bucketsTmp) {\n for (const pair of bucket) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n print() {\n for (const bucket of this.#buckets) {\n let res = [];\n for (const pair of bucket) {\n res.push(pair.key + ' -&gt; ' + pair.val);\n }\n console.log(res);\n }\n }\n}\n</code></pre> hash_map_chaining.ts<pre><code>/* \u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868 */\nclass HashMapChaining {\n #size: number; // \u9375\u503c\u5c0d\u6578\u91cf\n #capacity: number; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n #loadThres: number; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n #extendRatio: number; // \u64f4\u5bb9\u500d\u6578\n #buckets: Pair[][]; // \u6876\u9663\u5217\n\n /* \u5efa\u69cb\u5b50 */\n constructor() {\n this.#size = 0;\n this.#capacity = 4;\n this.#loadThres = 2.0 / 3.0;\n this.#extendRatio = 2;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) =&gt; []);\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n #hashFunc(key: number): number {\n return key % this.#capacity;\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n #loadFactor(): number {\n return this.#size / this.#capacity;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n get(key: number): string | null {\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n for (const pair of bucket) {\n if (pair.key === key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5247\u8fd4\u56de null\n return null;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n put(key: number, val: string): void {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (this.#loadFactor() &gt; this.#loadThres) {\n this.#extend();\n }\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5247\u66f4\u65b0\u5c0d\u61c9 val \u4e26\u8fd4\u56de\n for (const pair of bucket) {\n if (pair.key === key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u7121\u8a72 key \uff0c\u5247\u5c07\u9375\u503c\u5c0d\u65b0\u589e\u81f3\u5c3e\u90e8\n const pair = new Pair(key, val);\n bucket.push(pair);\n this.#size++;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n remove(key: number): void {\n const index = this.#hashFunc(key);\n let bucket = this.#buckets[index];\n // \u8d70\u8a2a\u6876\uff0c\u5f9e\u4e2d\u522a\u9664\u9375\u503c\u5c0d\n for (let i = 0; i &lt; bucket.length; i++) {\n if (bucket[i].key === key) {\n bucket.splice(i, 1);\n this.#size--;\n break;\n }\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n #extend(): void {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) =&gt; []);\n this.#size = 0;\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for (const bucket of bucketsTmp) {\n for (const pair of bucket) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n print(): void {\n for (const bucket of this.#buckets) {\n let res = [];\n for (const pair of bucket) {\n res.push(pair.key + ' -&gt; ' + pair.val);\n }\n console.log(res);\n }\n }\n}\n</code></pre> hash_map_chaining.dart<pre><code>/* \u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868 */\nclass HashMapChaining {\n late int size; // \u9375\u503c\u5c0d\u6578\u91cf\n late int capacity; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n late double loadThres; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n late int extendRatio; // \u64f4\u5bb9\u500d\u6578\n late List&lt;List&lt;Pair&gt;&gt; buckets; // \u6876\u9663\u5217\n\n /* \u5efa\u69cb\u5b50 */\n HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = List.generate(capacity, (_) =&gt; []);\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n double loadFactor() {\n return size / capacity;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n String? get(int key) {\n int index = hashFunc(key);\n List&lt;Pair&gt; bucket = buckets[index];\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n for (Pair pair in bucket) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5247\u8fd4\u56de null\n return null;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n void put(int key, String val) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (loadFactor() &gt; loadThres) {\n extend();\n }\n int index = hashFunc(key);\n List&lt;Pair&gt; bucket = buckets[index];\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5247\u66f4\u65b0\u5c0d\u61c9 val \u4e26\u8fd4\u56de\n for (Pair pair in bucket) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u7121\u8a72 key \uff0c\u5247\u5c07\u9375\u503c\u5c0d\u65b0\u589e\u81f3\u5c3e\u90e8\n Pair pair = Pair(key, val);\n bucket.add(pair);\n size++;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n List&lt;Pair&gt; bucket = buckets[index];\n // \u8d70\u8a2a\u6876\uff0c\u5f9e\u4e2d\u522a\u9664\u9375\u503c\u5c0d\n for (Pair pair in bucket) {\n if (pair.key == key) {\n bucket.remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n void extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n List&lt;List&lt;Pair&gt;&gt; bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n capacity *= extendRatio;\n buckets = List.generate(capacity, (_) =&gt; []);\n size = 0;\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for (List&lt;Pair&gt; bucket in bucketsTmp) {\n for (Pair pair in bucket) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n void printHashMap() {\n for (List&lt;Pair&gt; bucket in buckets) {\n List&lt;String&gt; res = [];\n for (Pair pair in bucket) {\n res.add(\"${pair.key} -&gt; ${pair.val}\");\n }\n print(res);\n }\n }\n}\n</code></pre> hash_map_chaining.rs<pre><code>/* \u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868 */\nstruct HashMapChaining {\n size: i32,\n capacity: i32,\n load_thres: f32,\n extend_ratio: i32,\n buckets: Vec&lt;Vec&lt;Pair&gt;&gt;,\n}\n\nimpl HashMapChaining {\n /* \u5efa\u69cb\u5b50 */\n fn new() -&gt; Self {\n Self {\n size: 0,\n capacity: 4,\n load_thres: 2.0 / 3.0,\n extend_ratio: 2,\n buckets: vec![vec![]; 4],\n }\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n fn hash_func(&amp;self, key: i32) -&gt; usize {\n key as usize % self.capacity as usize\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n fn load_factor(&amp;self) -&gt; f32 {\n self.size as f32 / self.capacity as f32\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n fn remove(&amp;mut self, key: i32) -&gt; Option&lt;String&gt; {\n let index = self.hash_func(key);\n let bucket = &amp;mut self.buckets[index];\n\n // \u8d70\u8a2a\u6876\uff0c\u5f9e\u4e2d\u522a\u9664\u9375\u503c\u5c0d\n for i in 0..bucket.len() {\n if bucket[i].key == key {\n let pair = bucket.remove(i);\n self.size -= 1;\n return Some(pair.val);\n }\n }\n\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5247\u8fd4\u56de None\n None\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n fn extend(&amp;mut self) {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n let buckets_tmp = std::mem::replace(&amp;mut self.buckets, vec![]);\n\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n self.capacity *= self.extend_ratio;\n self.buckets = vec![Vec::new(); self.capacity as usize];\n self.size = 0;\n\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for bucket in buckets_tmp {\n for pair in bucket {\n self.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n fn print(&amp;self) {\n for bucket in &amp;self.buckets {\n let mut res = Vec::new();\n for pair in bucket {\n res.push(format!(\"{} -&gt; {}\", pair.key, pair.val));\n }\n println!(\"{:?}\", res);\n }\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n fn put(&amp;mut self, key: i32, val: String) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if self.load_factor() &gt; self.load_thres {\n self.extend();\n }\n\n let index = self.hash_func(key);\n let bucket = &amp;mut self.buckets[index];\n\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5247\u66f4\u65b0\u5c0d\u61c9 val \u4e26\u8fd4\u56de\n for pair in bucket {\n if pair.key == key {\n pair.val = val;\n return;\n }\n }\n let bucket = &amp;mut self.buckets[index];\n\n // \u82e5\u7121\u8a72 key \uff0c\u5247\u5c07\u9375\u503c\u5c0d\u65b0\u589e\u81f3\u5c3e\u90e8\n let pair = Pair { key, val };\n bucket.push(pair);\n self.size += 1;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n fn get(&amp;self, key: i32) -&gt; Option&lt;&amp;str&gt; {\n let index = self.hash_func(key);\n let bucket = &amp;self.buckets[index];\n\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n for pair in bucket {\n if pair.key == key {\n return Some(&amp;pair.val);\n }\n }\n\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5247\u8fd4\u56de None\n None\n }\n}\n</code></pre> hash_map_chaining.c<pre><code>/* \u93c8\u7d50\u4e32\u5217\u7bc0\u9ede */\ntypedef struct Node {\n Pair *pair;\n struct Node *next;\n} Node;\n\n/* \u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868 */\ntypedef struct {\n int size; // \u9375\u503c\u5c0d\u6578\u91cf\n int capacity; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n double loadThres; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n int extendRatio; // \u64f4\u5bb9\u500d\u6578\n Node **buckets; // \u6876\u9663\u5217\n} HashMapChaining;\n\n/* \u5efa\u69cb\u5b50 */\nHashMapChaining *newHashMapChaining() {\n HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));\n hashMap-&gt;size = 0;\n hashMap-&gt;capacity = 4;\n hashMap-&gt;loadThres = 2.0 / 3.0;\n hashMap-&gt;extendRatio = 2;\n hashMap-&gt;buckets = (Node **)malloc(hashMap-&gt;capacity * sizeof(Node *));\n for (int i = 0; i &lt; hashMap-&gt;capacity; i++) {\n hashMap-&gt;buckets[i] = NULL;\n }\n return hashMap;\n}\n\n/* \u6790\u69cb\u51fd\u5f0f */\nvoid delHashMapChaining(HashMapChaining *hashMap) {\n for (int i = 0; i &lt; hashMap-&gt;capacity; i++) {\n Node *cur = hashMap-&gt;buckets[i];\n while (cur) {\n Node *tmp = cur;\n cur = cur-&gt;next;\n free(tmp-&gt;pair);\n free(tmp);\n }\n }\n free(hashMap-&gt;buckets);\n free(hashMap);\n}\n\n/* \u96dc\u6e4a\u51fd\u5f0f */\nint hashFunc(HashMapChaining *hashMap, int key) {\n return key % hashMap-&gt;capacity;\n}\n\n/* \u8ca0\u8f09\u56e0\u5b50 */\ndouble loadFactor(HashMapChaining *hashMap) {\n return (double)hashMap-&gt;size / (double)hashMap-&gt;capacity;\n}\n\n/* \u67e5\u8a62\u64cd\u4f5c */\nchar *get(HashMapChaining *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n Node *cur = hashMap-&gt;buckets[index];\n while (cur) {\n if (cur-&gt;pair-&gt;key == key) {\n return cur-&gt;pair-&gt;val;\n }\n cur = cur-&gt;next;\n }\n return \"\"; // \u82e5\u672a\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u7a7a\u5b57\u4e32\n}\n\n/* \u65b0\u589e\u64cd\u4f5c */\nvoid put(HashMapChaining *hashMap, int key, const char *val) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (loadFactor(hashMap) &gt; hashMap-&gt;loadThres) {\n extend(hashMap);\n }\n int index = hashFunc(hashMap, key);\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5247\u66f4\u65b0\u5c0d\u61c9 val \u4e26\u8fd4\u56de\n Node *cur = hashMap-&gt;buckets[index];\n while (cur) {\n if (cur-&gt;pair-&gt;key == key) {\n strcpy(cur-&gt;pair-&gt;val, val); // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5247\u66f4\u65b0\u5c0d\u61c9 val \u4e26\u8fd4\u56de\n return;\n }\n cur = cur-&gt;next;\n }\n // \u82e5\u7121\u8a72 key \uff0c\u5247\u5c07\u9375\u503c\u5c0d\u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u982d\u90e8\n Pair *newPair = (Pair *)malloc(sizeof(Pair));\n newPair-&gt;key = key;\n strcpy(newPair-&gt;val, val);\n Node *newNode = (Node *)malloc(sizeof(Node));\n newNode-&gt;pair = newPair;\n newNode-&gt;next = hashMap-&gt;buckets[index];\n hashMap-&gt;buckets[index] = newNode;\n hashMap-&gt;size++;\n}\n\n/* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\nvoid extend(HashMapChaining *hashMap) {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n int oldCapacity = hashMap-&gt;capacity;\n Node **oldBuckets = hashMap-&gt;buckets;\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n hashMap-&gt;capacity *= hashMap-&gt;extendRatio;\n hashMap-&gt;buckets = (Node **)malloc(hashMap-&gt;capacity * sizeof(Node *));\n for (int i = 0; i &lt; hashMap-&gt;capacity; i++) {\n hashMap-&gt;buckets[i] = NULL;\n }\n hashMap-&gt;size = 0;\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for (int i = 0; i &lt; oldCapacity; i++) {\n Node *cur = oldBuckets[i];\n while (cur) {\n put(hashMap, cur-&gt;pair-&gt;key, cur-&gt;pair-&gt;val);\n Node *temp = cur;\n cur = cur-&gt;next;\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(temp-&gt;pair);\n free(temp);\n }\n }\n\n free(oldBuckets);\n}\n\n/* \u522a\u9664\u64cd\u4f5c */\nvoid removeItem(HashMapChaining *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n Node *cur = hashMap-&gt;buckets[index];\n Node *pre = NULL;\n while (cur) {\n if (cur-&gt;pair-&gt;key == key) {\n // \u5f9e\u4e2d\u522a\u9664\u9375\u503c\u5c0d\n if (pre) {\n pre-&gt;next = cur-&gt;next;\n } else {\n hashMap-&gt;buckets[index] = cur-&gt;next;\n }\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(cur-&gt;pair);\n free(cur);\n hashMap-&gt;size--;\n return;\n }\n pre = cur;\n cur = cur-&gt;next;\n }\n}\n\n/* \u5217\u5370\u96dc\u6e4a\u8868 */\nvoid print(HashMapChaining *hashMap) {\n for (int i = 0; i &lt; hashMap-&gt;capacity; i++) {\n Node *cur = hashMap-&gt;buckets[i];\n printf(\"[\");\n while (cur) {\n printf(\"%d -&gt; %s, \", cur-&gt;pair-&gt;key, cur-&gt;pair-&gt;val);\n cur = cur-&gt;next;\n }\n printf(\"]\\n\");\n }\n}\n</code></pre> hash_map_chaining.kt<pre><code>/* \u93c8\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868 */\nclass HashMapChaining {\n var size: Int // \u9375\u503c\u5c0d\u6578\u91cf\n var capacity: Int // \u96dc\u6e4a\u8868\u5bb9\u91cf\n val loadThres: Double // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n val extendRatio: Int // \u64f4\u5bb9\u500d\u6578\n var buckets: MutableList&lt;MutableList&lt;Pair&gt;&gt; // \u6876\u9663\u5217\n\n /* \u5efa\u69cb\u5b50 */\n init {\n size = 0\n capacity = 4\n loadThres = 2.0 / 3.0\n extendRatio = 2\n buckets = mutableListOf()\n for (i in 0..&lt;capacity) {\n buckets.add(mutableListOf())\n }\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n fun hashFunc(key: Int): Int {\n return key % capacity\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n fun loadFactor(): Double {\n return (size / capacity).toDouble()\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n fun get(key: Int): String? {\n val index = hashFunc(key)\n val bucket = buckets[index]\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n for (pair in bucket) {\n if (pair.key == key) return pair._val\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5247\u8fd4\u56de null\n return null\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n fun put(key: Int, _val: String) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (loadFactor() &gt; loadThres) {\n extend()\n }\n val index = hashFunc(key)\n val bucket = buckets[index]\n // \u8d70\u8a2a\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5247\u66f4\u65b0\u5c0d\u61c9 val \u4e26\u8fd4\u56de\n for (pair in bucket) {\n if (pair.key == key) {\n pair._val = _val\n return\n }\n }\n // \u82e5\u7121\u8a72 key \uff0c\u5247\u5c07\u9375\u503c\u5c0d\u65b0\u589e\u81f3\u5c3e\u90e8\n val pair = Pair(key, _val)\n bucket.add(pair)\n size++\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n fun remove(key: Int) {\n val index = hashFunc(key)\n val bucket = buckets[index]\n // \u8d70\u8a2a\u6876\uff0c\u5f9e\u4e2d\u522a\u9664\u9375\u503c\u5c0d\n for (pair in bucket) {\n if (pair.key == key) {\n bucket.remove(pair)\n size--\n break\n }\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n fun extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n val bucketsTmp = buckets\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n capacity *= extendRatio\n // mutablelist \u7121\u56fa\u5b9a\u5927\u5c0f\n buckets = mutableListOf()\n for (i in 0..&lt;capacity) {\n buckets.add(mutableListOf())\n }\n size = 0\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for (bucket in bucketsTmp) {\n for (pair in bucket) {\n put(pair.key, pair._val)\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n fun print() {\n for (bucket in buckets) {\n val res = mutableListOf&lt;String&gt;()\n for (pair in bucket) {\n val k = pair.key\n val v = pair._val\n res.add(\"$k -&gt; $v\")\n }\n println(res)\n }\n }\n}\n</code></pre> hash_map_chaining.rb<pre><code>### \u9375\u5f0f\u4f4d\u5740\u96dc\u6e4a\u8868 ###\nclass HashMapChaining\n ### \u5efa\u69cb\u5b50 ###\n def initialize\n @size = 0 # \u9375\u503c\u5c0d\u6578\u91cf\n @capacity = 4 # \u96dc\u6e4a\u8868\u5bb9\u91cf\n @load_thres = 2.0 / 3.0 # \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n @extend_ratio = 2 # \u64f4\u5bb9\u500d\u6578\n @buckets = Array.new(@capacity) { [] } # \u6876\u9663\u5217\n end\n\n ### \u96dc\u6e4a\u51fd\u5f0f ###\n def hash_func(key)\n key % @capacity\n end\n\n ### \u8ca0\u8f09\u56e0\u5b50 ###\n def load_factor\n @size / @capacity\n end\n\n ### \u67e5\u8a62\u64cd\u4f5c ###\n def get(key)\n index = hash_func(key)\n bucket = @buckets[index]\n # \u8d70\u8a2a\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n for pair in bucket\n return pair.val if pair.key == key\n end\n # \u82e5\u672a\u627e\u5230 key , \u5247\u8fd4\u56de nil\n nil\n end\n\n ### \u65b0\u589e\u64cd\u4f5c ###\n def put(key, val)\n # \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n extend if load_factor &gt; @load_thres\n index = hash_func(key)\n bucket = @buckets[index]\n # \u8d70\u8a2a\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5247\u66f4\u65b0\u5c0d\u61c9 val \u4e26\u8fd4\u56de\n for pair in bucket\n if pair.key == key\n pair.val = val\n return\n end\n end\n # \u82e5\u7121\u8a72 key \uff0c\u5247\u5c07\u9375\u503c\u5c0d\u65b0\u589e\u81f3\u5c3e\u90e8\n pair = Pair.new(key, val)\n bucket &lt;&lt; pair\n @size += 1\n end\n\n ### \u522a\u9664\u64cd\u4f5c ###\n def remove(key)\n index = hash_func(key)\n bucket = @buckets[index]\n # \u8d70\u8a2a\u6876\uff0c\u5f9e\u4e2d\u522a\u9664\u9375\u503c\u5c0d\n for pair in bucket\n if pair.key == key\n bucket.delete(pair)\n @size -= 1\n break\n end\n end\n end\n\n ### \u64f4\u5bb9\u96dc\u6e4a\u8868 ###\n def extend\n # \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n buckets = @buckets\n # \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n @capacity *= @extend_ratio\n @buckets = Array.new(@capacity) { [] }\n @size = 0\n # \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for bucket in buckets\n for pair in bucket\n put(pair.key, pair.val)\n end\n end\n end\n\n ### \u5217\u5370\u96dc\u6e4a\u8868 ###\n def print\n for bucket in @buckets\n res = []\n for pair in bucket\n res &lt;&lt; \"#{pair.key} -&gt; #{pair.val}\"\n end\n pp res\n end\n end\nend\n</code></pre> hash_map_chaining.zig<pre><code>[class]{HashMapChaining}-[func]{}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7576\u93c8\u7d50\u4e32\u5217\u5f88\u9577\u6642\uff0c\u67e5\u8a62\u6548\u7387 \\(O(n)\\) \u5f88\u5dee\u3002\u6b64\u6642\u53ef\u4ee5\u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u63db\u70ba\u201cAVL \u6a39\u201d\u6216\u201c\u7d05\u9ed1\u6a39\u201d\uff0c\u5f9e\u800c\u5c07\u67e5\u8a62\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u6700\u4f73\u5316\u81f3 \\(O(\\log n)\\) \u3002</p>"},{"location":"chapter_hashing/hash_collision/#622","title":"6.2.2 \u00a0 \u958b\u653e\u5b9a\u5740","text":"<p>\u958b\u653e\u5b9a\u5740\uff08open addressing\uff09\u4e0d\u5f15\u5165\u984d\u5916\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u800c\u662f\u900f\u904e\u201c\u591a\u6b21\u63a2\u6e2c\u201d\u4f86\u8655\u7406\u96dc\u6e4a\u885d\u7a81\uff0c\u63a2\u6e2c\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\u7dda\u6027\u63a2\u67e5\u3001\u5e73\u65b9\u63a2\u6e2c\u548c\u591a\u6b21\u96dc\u6e4a\u7b49\u3002</p> <p>\u4e0b\u9762\u4ee5\u7dda\u6027\u63a2\u67e5\u70ba\u4f8b\uff0c\u4ecb\u7d39\u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868\u7684\u5de5\u4f5c\u6a5f\u5236\u3002</p>"},{"location":"chapter_hashing/hash_collision/#1","title":"1. \u00a0 \u7dda\u6027\u63a2\u67e5","text":"<p>\u7dda\u6027\u63a2\u67e5\u63a1\u7528\u56fa\u5b9a\u6b65\u9577\u7684\u7dda\u6027\u641c\u5c0b\u4f86\u9032\u884c\u63a2\u6e2c\uff0c\u5176\u64cd\u4f5c\u65b9\u6cd5\u8207\u666e\u901a\u96dc\u6e4a\u8868\u6709\u6240\u4e0d\u540c\u3002</p> <ul> <li>\u63d2\u5165\u5143\u7d20\uff1a\u900f\u904e\u96dc\u6e4a\u51fd\u5f0f\u8a08\u7b97\u6876\u7d22\u5f15\uff0c\u82e5\u767c\u73fe\u6876\u5167\u5df2\u6709\u5143\u7d20\uff0c\u5247\u5f9e\u885d\u7a81\u4f4d\u7f6e\u5411\u5f8c\u7dda\u6027\u8d70\u8a2a\uff08\u6b65\u9577\u901a\u5e38\u70ba \\(1\\) \uff09\uff0c\u76f4\u81f3\u627e\u5230\u7a7a\u6876\uff0c\u5c07\u5143\u7d20\u63d2\u5165\u5176\u4e2d\u3002</li> <li>\u67e5\u8a62\u5143\u7d20\uff1a\u82e5\u767c\u73fe\u96dc\u6e4a\u885d\u7a81\uff0c\u5247\u4f7f\u7528\u76f8\u540c\u6b65\u9577\u5411\u5f8c\u9032\u884c\u7dda\u6027\u8d70\u8a2a\uff0c\u76f4\u5230\u627e\u5230\u5c0d\u61c9\u5143\u7d20\uff0c\u8fd4\u56de <code>value</code> \u5373\u53ef\uff1b\u5982\u679c\u9047\u5230\u7a7a\u6876\uff0c\u8aaa\u660e\u76ee\u6a19\u5143\u7d20\u4e0d\u5728\u96dc\u6e4a\u8868\u4e2d\uff0c\u8fd4\u56de <code>None</code> \u3002</li> </ul> <p>\u5716 6-6 \u5c55\u793a\u4e86\u958b\u653e\u5b9a\u5740\uff08\u7dda\u6027\u63a2\u67e5\uff09\u96dc\u6e4a\u8868\u7684\u9375\u503c\u5c0d\u5206\u4f48\u3002\u6839\u64da\u6b64\u96dc\u6e4a\u51fd\u5f0f\uff0c\u6700\u5f8c\u5169\u4f4d\u76f8\u540c\u7684 <code>key</code> \u90fd\u6703\u88ab\u5c0d\u6620\u5230\u76f8\u540c\u7684\u6876\u3002\u800c\u900f\u904e\u7dda\u6027\u63a2\u67e5\uff0c\u5b83\u5011\u88ab\u4f9d\u6b21\u5132\u5b58\u5728\u8a72\u6876\u4ee5\u53ca\u4e4b\u4e0b\u7684\u6876\u4e2d\u3002</p> <p></p> <p> \u5716 6-6 \u00a0 \u958b\u653e\u5b9a\u5740\uff08\u7dda\u6027\u63a2\u67e5\uff09\u96dc\u6e4a\u8868\u7684\u9375\u503c\u5c0d\u5206\u4f48 </p> <p>\u7136\u800c\uff0c\u7dda\u6027\u63a2\u67e5\u5bb9\u6613\u7522\u751f\u201c\u805a\u96c6\u73fe\u8c61\u201d\u3002\u5177\u9ad4\u4f86\u8aaa\uff0c\u9663\u5217\u4e2d\u9023\u7e8c\u88ab\u4f54\u7528\u7684\u4f4d\u7f6e\u8d8a\u9577\uff0c\u9019\u4e9b\u9023\u7e8c\u4f4d\u7f6e\u767c\u751f\u96dc\u6e4a\u885d\u7a81\u7684\u53ef\u80fd\u6027\u8d8a\u5927\uff0c\u5f9e\u800c\u9032\u4e00\u6b65\u4fc3\u4f7f\u8a72\u4f4d\u7f6e\u7684\u805a\u5806\u7a4d\u751f\u9577\uff0c\u5f62\u6210\u60e1\u6027\u8ff4\u5708\uff0c\u6700\u7d42\u5c0e\u81f4\u589e\u522a\u67e5\u6539\u64cd\u4f5c\u6548\u7387\u52a3\u5316\u3002</p> <p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6211\u5011\u4e0d\u80fd\u5728\u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868\u4e2d\u76f4\u63a5\u522a\u9664\u5143\u7d20\u3002\u9019\u662f\u56e0\u70ba\u522a\u9664\u5143\u7d20\u6703\u5728\u9663\u5217\u5167\u7522\u751f\u4e00\u500b\u7a7a\u6876 <code>None</code> \uff0c\u800c\u7576\u67e5\u8a62\u5143\u7d20\u6642\uff0c\u7dda\u6027\u63a2\u67e5\u5230\u8a72\u7a7a\u6876\u5c31\u6703\u8fd4\u56de\uff0c\u56e0\u6b64\u5728\u8a72\u7a7a\u6876\u4e4b\u4e0b\u7684\u5143\u7d20\u90fd\u7121\u6cd5\u518d\u88ab\u8a2a\u554f\u5230\uff0c\u7a0b\u5f0f\u53ef\u80fd\u8aa4\u5224\u9019\u4e9b\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5982\u5716 6-7 \u6240\u793a\u3002</p> <p></p> <p> \u5716 6-7 \u00a0 \u5728\u958b\u653e\u5b9a\u5740\u4e2d\u522a\u9664\u5143\u7d20\u5c0e\u81f4\u7684\u67e5\u8a62\u554f\u984c </p> <p>\u70ba\u4e86\u89e3\u6c7a\u8a72\u554f\u984c\uff0c\u6211\u5011\u53ef\u4ee5\u63a1\u7528\u61f6\u522a\u9664\uff08lazy deletion\uff09\u6a5f\u5236\uff1a\u5b83\u4e0d\u76f4\u63a5\u5f9e\u96dc\u6e4a\u8868\u4e2d\u79fb\u9664\u5143\u7d20\uff0c\u800c\u662f\u5229\u7528\u4e00\u500b\u5e38\u6578 <code>TOMBSTONE</code> \u4f86\u6a19\u8a18\u9019\u500b\u6876\u3002\u5728\u8a72\u6a5f\u5236\u4e0b\uff0c<code>None</code> \u548c <code>TOMBSTONE</code> \u90fd\u4ee3\u8868\u7a7a\u6876\uff0c\u90fd\u53ef\u4ee5\u653e\u7f6e\u9375\u503c\u5c0d\u3002\u4f46\u4e0d\u540c\u7684\u662f\uff0c\u7dda\u6027\u63a2\u67e5\u5230 <code>TOMBSTONE</code> \u6642\u61c9\u8a72\u7e7c\u7e8c\u8d70\u8a2a\uff0c\u56e0\u70ba\u5176\u4e4b\u4e0b\u53ef\u80fd\u9084\u5b58\u5728\u9375\u503c\u5c0d\u3002</p> <p>\u7136\u800c\uff0c\u61f6\u522a\u9664\u53ef\u80fd\u6703\u52a0\u901f\u96dc\u6e4a\u8868\u7684\u6548\u80fd\u9000\u5316\u3002\u9019\u662f\u56e0\u70ba\u6bcf\u6b21\u522a\u9664\u64cd\u4f5c\u90fd\u6703\u7522\u751f\u4e00\u500b\u522a\u9664\u6a19\u8a18\uff0c\u96a8\u8457 <code>TOMBSTONE</code> \u7684\u589e\u52a0\uff0c\u641c\u5c0b\u6642\u9593\u4e5f\u6703\u589e\u52a0\uff0c\u56e0\u70ba\u7dda\u6027\u63a2\u67e5\u53ef\u80fd\u9700\u8981\u8df3\u904e\u591a\u500b <code>TOMBSTONE</code> \u624d\u80fd\u627e\u5230\u76ee\u6a19\u5143\u7d20\u3002</p> <p>\u70ba\u6b64\uff0c\u8003\u616e\u7dda\u4e0a\u6027\u63a2\u67e5\u4e2d\u8a18\u9304\u9047\u5230\u7684\u9996\u500b <code>TOMBSTONE</code> \u7684\u7d22\u5f15\uff0c\u4e26\u5c07\u641c\u5c0b\u5230\u7684\u76ee\u6a19\u5143\u7d20\u8207\u8a72 <code>TOMBSTONE</code> \u4ea4\u63db\u4f4d\u7f6e\u3002\u9019\u6a23\u505a\u7684\u597d\u8655\u662f\u7576\u6bcf\u6b21\u67e5\u8a62\u6216\u65b0\u589e\u5143\u7d20\u6642\uff0c\u5143\u7d20\u6703\u88ab\u79fb\u52d5\u81f3\u8ddd\u96e2\u7406\u60f3\u4f4d\u7f6e\uff08\u63a2\u6e2c\u8d77\u59cb\u9ede\uff09\u66f4\u8fd1\u7684\u6876\uff0c\u5f9e\u800c\u6700\u4f73\u5316\u67e5\u8a62\u6548\u7387\u3002</p> <p>\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u5be6\u73fe\u4e86\u4e00\u500b\u5305\u542b\u61f6\u522a\u9664\u7684\u958b\u653e\u5b9a\u5740\uff08\u7dda\u6027\u63a2\u67e5\uff09\u96dc\u6e4a\u8868\u3002\u70ba\u4e86\u66f4\u52a0\u5145\u5206\u5730\u4f7f\u7528\u96dc\u6e4a\u8868\u7684\u7a7a\u9593\uff0c\u6211\u5011\u5c07\u96dc\u6e4a\u8868\u770b\u4f5c\u4e00\u500b\u201c\u74b0\u5f62\u9663\u5217\u201d\uff0c\u7576\u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u56de\u5230\u982d\u90e8\u7e7c\u7e8c\u8d70\u8a2a\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig hash_map_open_addressing.py<pre><code>class HashMapOpenAddressing:\n \"\"\"\u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u5efa\u69cb\u5b50\"\"\"\n self.size = 0 # \u9375\u503c\u5c0d\u6578\u91cf\n self.capacity = 4 # \u96dc\u6e4a\u8868\u5bb9\u91cf\n self.load_thres = 2.0 / 3.0 # \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n self.extend_ratio = 2 # \u64f4\u5bb9\u500d\u6578\n self.buckets: list[Pair | None] = [None] * self.capacity # \u6876\u9663\u5217\n self.TOMBSTONE = Pair(-1, \"-1\") # \u522a\u9664\u6a19\u8a18\n\n def hash_func(self, key: int) -&gt; int:\n \"\"\"\u96dc\u6e4a\u51fd\u5f0f\"\"\"\n return key % self.capacity\n\n def load_factor(self) -&gt; float:\n \"\"\"\u8ca0\u8f09\u56e0\u5b50\"\"\"\n return self.size / self.capacity\n\n def find_bucket(self, key: int) -&gt; int:\n \"\"\"\u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\"\"\"\n index = self.hash_func(key)\n first_tombstone = -1\n # \u7dda\u6027\u63a2\u67e5\uff0c\u7576\u9047\u5230\u7a7a\u6876\u6642\u8df3\u51fa\n while self.buckets[index] is not None:\n # \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if self.buckets[index].key == key:\n # \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u522a\u9664\u6a19\u8a18\uff0c\u5247\u5c07\u9375\u503c\u5c0d\u79fb\u52d5\u81f3\u8a72\u7d22\u5f15\u8655\n if first_tombstone != -1:\n self.buckets[first_tombstone] = self.buckets[index]\n self.buckets[index] = self.TOMBSTONE\n return first_tombstone # \u8fd4\u56de\u79fb\u52d5\u5f8c\u7684\u6876\u7d22\u5f15\n return index # \u8fd4\u56de\u6876\u7d22\u5f15\n # \u8a18\u9304\u9047\u5230\u7684\u9996\u500b\u522a\u9664\u6a19\u8a18\n if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:\n first_tombstone = index\n # \u8a08\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u904e\u5c3e\u90e8\u5247\u8fd4\u56de\u982d\u90e8\n index = (index + 1) % self.capacity\n # \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u65b0\u589e\u9ede\u7684\u7d22\u5f15\n return index if first_tombstone == -1 else first_tombstone\n\n def get(self, key: int) -&gt; str:\n \"\"\"\u67e5\u8a62\u64cd\u4f5c\"\"\"\n # \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n return self.buckets[index].val\n # \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de None\n return None\n\n def put(self, key: int, val: str):\n \"\"\"\u65b0\u589e\u64cd\u4f5c\"\"\"\n # \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if self.load_factor() &gt; self.load_thres:\n self.extend()\n # \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8986\u84cb val \u4e26\u8fd4\u56de\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n self.buckets[index].val = val\n return\n # \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u65b0\u589e\u8a72\u9375\u503c\u5c0d\n self.buckets[index] = Pair(key, val)\n self.size += 1\n\n def remove(self, key: int):\n \"\"\"\u522a\u9664\u64cd\u4f5c\"\"\"\n # \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u7528\u522a\u9664\u6a19\u8a18\u8986\u84cb\u5b83\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n self.buckets[index] = self.TOMBSTONE\n self.size -= 1\n\n def extend(self):\n \"\"\"\u64f4\u5bb9\u96dc\u6e4a\u8868\"\"\"\n # \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n buckets_tmp = self.buckets\n # \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n self.capacity *= self.extend_ratio\n self.buckets = [None] * self.capacity\n self.size = 0\n # \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for pair in buckets_tmp:\n if pair not in [None, self.TOMBSTONE]:\n self.put(pair.key, pair.val)\n\n def print(self):\n \"\"\"\u5217\u5370\u96dc\u6e4a\u8868\"\"\"\n for pair in self.buckets:\n if pair is None:\n print(\"None\")\n elif pair is self.TOMBSTONE:\n print(\"TOMBSTONE\")\n else:\n print(pair.key, \"-&gt;\", pair.val)\n</code></pre> hash_map_open_addressing.cpp<pre><code>/* \u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868 */\nclass HashMapOpenAddressing {\n private:\n int size; // \u9375\u503c\u5c0d\u6578\u91cf\n int capacity = 4; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n const double loadThres = 2.0 / 3.0; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n const int extendRatio = 2; // \u64f4\u5bb9\u500d\u6578\n vector&lt;Pair *&gt; buckets; // \u6876\u9663\u5217\n Pair *TOMBSTONE = new Pair(-1, \"-1\"); // \u522a\u9664\u6a19\u8a18\n\n public:\n /* \u5efa\u69cb\u5b50 */\n HashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {\n }\n\n /* \u6790\u69cb\u65b9\u6cd5 */\n ~HashMapOpenAddressing() {\n for (Pair *pair : buckets) {\n if (pair != nullptr &amp;&amp; pair != TOMBSTONE) {\n delete pair;\n }\n }\n delete TOMBSTONE;\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n double loadFactor() {\n return (double)size / capacity;\n }\n\n /* \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15 */\n int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7dda\u6027\u63a2\u67e5\uff0c\u7576\u9047\u5230\u7a7a\u6876\u6642\u8df3\u51fa\n while (buckets[index] != nullptr) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if (buckets[index]-&gt;key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u522a\u9664\u6a19\u8a18\uff0c\u5247\u5c07\u9375\u503c\u5c0d\u79fb\u52d5\u81f3\u8a72\u7d22\u5f15\u8655\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52d5\u5f8c\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8a18\u9304\u9047\u5230\u7684\u9996\u500b\u522a\u9664\u6a19\u8a18\n if (firstTombstone == -1 &amp;&amp; buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8a08\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u904e\u5c3e\u90e8\u5247\u8fd4\u56de\u982d\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u65b0\u589e\u9ede\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n string get(int key) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n if (buckets[index] != nullptr &amp;&amp; buckets[index] != TOMBSTONE) {\n return buckets[index]-&gt;val;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u7a7a\u5b57\u4e32\n return \"\";\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n void put(int key, string val) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (loadFactor() &gt; loadThres) {\n extend();\n }\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8986\u84cb val \u4e26\u8fd4\u56de\n if (buckets[index] != nullptr &amp;&amp; buckets[index] != TOMBSTONE) {\n buckets[index]-&gt;val = val;\n return;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u65b0\u589e\u8a72\u9375\u503c\u5c0d\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n void remove(int key) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u7528\u522a\u9664\u6a19\u8a18\u8986\u84cb\u5b83\n if (buckets[index] != nullptr &amp;&amp; buckets[index] != TOMBSTONE) {\n delete buckets[index];\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n void extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n vector&lt;Pair *&gt; bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n capacity *= extendRatio;\n buckets = vector&lt;Pair *&gt;(capacity, nullptr);\n size = 0;\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for (Pair *pair : bucketsTmp) {\n if (pair != nullptr &amp;&amp; pair != TOMBSTONE) {\n put(pair-&gt;key, pair-&gt;val);\n delete pair;\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n void print() {\n for (Pair *pair : buckets) {\n if (pair == nullptr) {\n cout &lt;&lt; \"nullptr\" &lt;&lt; endl;\n } else if (pair == TOMBSTONE) {\n cout &lt;&lt; \"TOMBSTONE\" &lt;&lt; endl;\n } else {\n cout &lt;&lt; pair-&gt;key &lt;&lt; \" -&gt; \" &lt;&lt; pair-&gt;val &lt;&lt; endl;\n }\n }\n }\n};\n</code></pre> hash_map_open_addressing.java<pre><code>/* \u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868 */\nclass HashMapOpenAddressing {\n private int size; // \u9375\u503c\u5c0d\u6578\u91cf\n private int capacity = 4; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n private final double loadThres = 2.0 / 3.0; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n private final int extendRatio = 2; // \u64f4\u5bb9\u500d\u6578\n private Pair[] buckets; // \u6876\u9663\u5217\n private final Pair TOMBSTONE = new Pair(-1, \"-1\"); // \u522a\u9664\u6a19\u8a18\n\n /* \u5efa\u69cb\u5b50 */\n public HashMapOpenAddressing() {\n size = 0;\n buckets = new Pair[capacity];\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n private int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n private double loadFactor() {\n return (double) size / capacity;\n }\n\n /* \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15 */\n private int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7dda\u6027\u63a2\u67e5\uff0c\u7576\u9047\u5230\u7a7a\u6876\u6642\u8df3\u51fa\n while (buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if (buckets[index].key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u522a\u9664\u6a19\u8a18\uff0c\u5247\u5c07\u9375\u503c\u5c0d\u79fb\u52d5\u81f3\u8a72\u7d22\u5f15\u8655\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52d5\u5f8c\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8a18\u9304\u9047\u5230\u7684\u9996\u500b\u522a\u9664\u6a19\u8a18\n if (firstTombstone == -1 &amp;&amp; buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8a08\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u904e\u5c3e\u90e8\u5247\u8fd4\u56de\u982d\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u65b0\u589e\u9ede\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n public String get(int key) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n if (buckets[index] != null &amp;&amp; buckets[index] != TOMBSTONE) {\n return buckets[index].val;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de null\n return null;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n public void put(int key, String val) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (loadFactor() &gt; loadThres) {\n extend();\n }\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8986\u84cb val \u4e26\u8fd4\u56de\n if (buckets[index] != null &amp;&amp; buckets[index] != TOMBSTONE) {\n buckets[index].val = val;\n return;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u65b0\u589e\u8a72\u9375\u503c\u5c0d\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n public void remove(int key) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u7528\u522a\u9664\u6a19\u8a18\u8986\u84cb\u5b83\n if (buckets[index] != null &amp;&amp; buckets[index] != TOMBSTONE) {\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n private void extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n Pair[] bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n capacity *= extendRatio;\n buckets = new Pair[capacity];\n size = 0;\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for (Pair pair : bucketsTmp) {\n if (pair != null &amp;&amp; pair != TOMBSTONE) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n public void print() {\n for (Pair pair : buckets) {\n if (pair == null) {\n System.out.println(\"null\");\n } else if (pair == TOMBSTONE) {\n System.out.println(\"TOMBSTONE\");\n } else {\n System.out.println(pair.key + \" -&gt; \" + pair.val);\n }\n }\n }\n}\n</code></pre> hash_map_open_addressing.cs<pre><code>/* \u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868 */\nclass HashMapOpenAddressing {\n int size; // \u9375\u503c\u5c0d\u6578\u91cf\n int capacity = 4; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n double loadThres = 2.0 / 3.0; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n int extendRatio = 2; // \u64f4\u5bb9\u500d\u6578\n Pair[] buckets; // \u6876\u9663\u5217\n Pair TOMBSTONE = new(-1, \"-1\"); // \u522a\u9664\u6a19\u8a18\n\n /* \u5efa\u69cb\u5b50 */\n public HashMapOpenAddressing() {\n size = 0;\n buckets = new Pair[capacity];\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n int HashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n double LoadFactor() {\n return (double)size / capacity;\n }\n\n /* \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15 */\n int FindBucket(int key) {\n int index = HashFunc(key);\n int firstTombstone = -1;\n // \u7dda\u6027\u63a2\u67e5\uff0c\u7576\u9047\u5230\u7a7a\u6876\u6642\u8df3\u51fa\n while (buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if (buckets[index].key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u522a\u9664\u6a19\u8a18\uff0c\u5247\u5c07\u9375\u503c\u5c0d\u79fb\u52d5\u81f3\u8a72\u7d22\u5f15\u8655\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52d5\u5f8c\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8a18\u9304\u9047\u5230\u7684\u9996\u500b\u522a\u9664\u6a19\u8a18\n if (firstTombstone == -1 &amp;&amp; buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8a08\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u904e\u5c3e\u90e8\u5247\u8fd4\u56de\u982d\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u65b0\u589e\u9ede\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n public string? Get(int key) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n if (buckets[index] != null &amp;&amp; buckets[index] != TOMBSTONE) {\n return buckets[index].val;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de null\n return null;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n public void Put(int key, string val) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (LoadFactor() &gt; loadThres) {\n Extend();\n }\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8986\u84cb val \u4e26\u8fd4\u56de\n if (buckets[index] != null &amp;&amp; buckets[index] != TOMBSTONE) {\n buckets[index].val = val;\n return;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u65b0\u589e\u8a72\u9375\u503c\u5c0d\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u7528\u522a\u9664\u6a19\u8a18\u8986\u84cb\u5b83\n if (buckets[index] != null &amp;&amp; buckets[index] != TOMBSTONE) {\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n void Extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n Pair[] bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n capacity *= extendRatio;\n buckets = new Pair[capacity];\n size = 0;\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n foreach (Pair pair in bucketsTmp) {\n if (pair != null &amp;&amp; pair != TOMBSTONE) {\n Put(pair.key, pair.val);\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n public void Print() {\n foreach (Pair pair in buckets) {\n if (pair == null) {\n Console.WriteLine(\"null\");\n } else if (pair == TOMBSTONE) {\n Console.WriteLine(\"TOMBSTONE\");\n } else {\n Console.WriteLine(pair.key + \" -&gt; \" + pair.val);\n }\n }\n }\n}\n</code></pre> hash_map_open_addressing.go<pre><code>/* \u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868 */\ntype hashMapOpenAddressing struct {\n size int // \u9375\u503c\u5c0d\u6578\u91cf\n capacity int // \u96dc\u6e4a\u8868\u5bb9\u91cf\n loadThres float64 // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n extendRatio int // \u64f4\u5bb9\u500d\u6578\n buckets []*pair // \u6876\u9663\u5217\n TOMBSTONE *pair // \u522a\u9664\u6a19\u8a18\n}\n\n/* \u5efa\u69cb\u5b50 */\nfunc newHashMapOpenAddressing() *hashMapOpenAddressing {\n return &amp;hashMapOpenAddressing{\n size: 0,\n capacity: 4,\n loadThres: 2.0 / 3.0,\n extendRatio: 2,\n buckets: make([]*pair, 4),\n TOMBSTONE: &amp;pair{-1, \"-1\"},\n }\n}\n\n/* \u96dc\u6e4a\u51fd\u5f0f */\nfunc (h *hashMapOpenAddressing) hashFunc(key int) int {\n return key % h.capacity // \u6839\u64da\u9375\u8a08\u7b97\u96dc\u6e4a\u503c\n}\n\n/* \u8ca0\u8f09\u56e0\u5b50 */\nfunc (h *hashMapOpenAddressing) loadFactor() float64 {\n return float64(h.size) / float64(h.capacity) // \u8a08\u7b97\u7576\u524d\u8ca0\u8f09\u56e0\u5b50\n}\n\n/* \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15 */\nfunc (h *hashMapOpenAddressing) findBucket(key int) int {\n index := h.hashFunc(key) // \u7372\u53d6\u521d\u59cb\u7d22\u5f15\n firstTombstone := -1 // \u8a18\u9304\u9047\u5230\u7684\u7b2c\u4e00\u500bTOMBSTONE\u7684\u4f4d\u7f6e\n for h.buckets[index] != nil {\n if h.buckets[index].key == key {\n if firstTombstone != -1 {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u522a\u9664\u6a19\u8a18\uff0c\u5247\u5c07\u9375\u503c\u5c0d\u79fb\u52d5\u81f3\u8a72\u7d22\u5f15\u8655\n h.buckets[firstTombstone] = h.buckets[index]\n h.buckets[index] = h.TOMBSTONE\n return firstTombstone // \u8fd4\u56de\u79fb\u52d5\u5f8c\u7684\u6876\u7d22\u5f15\n }\n return index // \u8fd4\u56de\u627e\u5230\u7684\u7d22\u5f15\n }\n if firstTombstone == -1 &amp;&amp; h.buckets[index] == h.TOMBSTONE {\n firstTombstone = index // \u8a18\u9304\u9047\u5230\u7684\u9996\u500b\u522a\u9664\u6a19\u8a18\u7684\u4f4d\u7f6e\n }\n index = (index + 1) % h.capacity // \u7dda\u6027\u63a2\u67e5\uff0c\u8d8a\u904e\u5c3e\u90e8\u5247\u8fd4\u56de\u982d\u90e8\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u65b0\u589e\u9ede\u7684\u7d22\u5f15\n if firstTombstone != -1 {\n return firstTombstone\n }\n return index\n}\n\n/* \u67e5\u8a62\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) get(key int) string {\n index := h.findBucket(key) // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if h.buckets[index] != nil &amp;&amp; h.buckets[index] != h.TOMBSTONE {\n return h.buckets[index].val // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n }\n return \"\" // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de \"\"\n}\n\n/* \u65b0\u589e\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) put(key int, val string) {\n if h.loadFactor() &gt; h.loadThres {\n h.extend() // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n }\n index := h.findBucket(key) // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if h.buckets[index] == nil || h.buckets[index] == h.TOMBSTONE {\n h.buckets[index] = &amp;pair{key, val} // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u65b0\u589e\u8a72\u9375\u503c\u5c0d\n h.size++\n } else {\n h.buckets[index].val = val // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8986\u84cb val\n }\n}\n\n/* \u522a\u9664\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) remove(key int) {\n index := h.findBucket(key) // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if h.buckets[index] != nil &amp;&amp; h.buckets[index] != h.TOMBSTONE {\n h.buckets[index] = h.TOMBSTONE // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u7528\u522a\u9664\u6a19\u8a18\u8986\u84cb\u5b83\n h.size--\n }\n}\n\n/* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\nfunc (h *hashMapOpenAddressing) extend() {\n oldBuckets := h.buckets // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n h.capacity *= h.extendRatio // \u66f4\u65b0\u5bb9\u91cf\n h.buckets = make([]*pair, h.capacity) // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n h.size = 0 // \u91cd\u7f6e\u5927\u5c0f\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for _, pair := range oldBuckets {\n if pair != nil &amp;&amp; pair != h.TOMBSTONE {\n h.put(pair.key, pair.val)\n }\n }\n}\n\n/* \u5217\u5370\u96dc\u6e4a\u8868 */\nfunc (h *hashMapOpenAddressing) print() {\n for _, pair := range h.buckets {\n if pair == nil {\n fmt.Println(\"nil\")\n } else if pair == h.TOMBSTONE {\n fmt.Println(\"TOMBSTONE\")\n } else {\n fmt.Printf(\"%d -&gt; %s\\n\", pair.key, pair.val)\n }\n }\n}\n</code></pre> hash_map_open_addressing.swift<pre><code>/* \u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868 */\nclass HashMapOpenAddressing {\n var size: Int // \u9375\u503c\u5c0d\u6578\u91cf\n var capacity: Int // \u96dc\u6e4a\u8868\u5bb9\u91cf\n var loadThres: Double // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n var extendRatio: Int // \u64f4\u5bb9\u500d\u6578\n var buckets: [Pair?] // \u6876\u9663\u5217\n var TOMBSTONE: Pair // \u522a\u9664\u6a19\u8a18\n\n /* \u5efa\u69cb\u5b50 */\n init() {\n size = 0\n capacity = 4\n loadThres = 2.0 / 3.0\n extendRatio = 2\n buckets = Array(repeating: nil, count: capacity)\n TOMBSTONE = Pair(key: -1, val: \"-1\")\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n func hashFunc(key: Int) -&gt; Int {\n key % capacity\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n func loadFactor() -&gt; Double {\n Double(size) / Double(capacity)\n }\n\n /* \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15 */\n func findBucket(key: Int) -&gt; Int {\n var index = hashFunc(key: key)\n var firstTombstone = -1\n // \u7dda\u6027\u63a2\u67e5\uff0c\u7576\u9047\u5230\u7a7a\u6876\u6642\u8df3\u51fa\n while buckets[index] != nil {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if buckets[index]!.key == key {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u522a\u9664\u6a19\u8a18\uff0c\u5247\u5c07\u9375\u503c\u5c0d\u79fb\u52d5\u81f3\u8a72\u7d22\u5f15\u8655\n if firstTombstone != -1 {\n buckets[firstTombstone] = buckets[index]\n buckets[index] = TOMBSTONE\n return firstTombstone // \u8fd4\u56de\u79fb\u52d5\u5f8c\u7684\u6876\u7d22\u5f15\n }\n return index // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8a18\u9304\u9047\u5230\u7684\u9996\u500b\u522a\u9664\u6a19\u8a18\n if firstTombstone == -1 &amp;&amp; buckets[index] == TOMBSTONE {\n firstTombstone = index\n }\n // \u8a08\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u904e\u5c3e\u90e8\u5247\u8fd4\u56de\u982d\u90e8\n index = (index + 1) % capacity\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u65b0\u589e\u9ede\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n func get(key: Int) -&gt; String? {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n return buckets[index]!.val\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de null\n return nil\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n func put(key: Int, val: String) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if loadFactor() &gt; loadThres {\n extend()\n }\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8986\u84cb val \u4e26\u8fd4\u56de\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n buckets[index]!.val = val\n return\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u65b0\u589e\u8a72\u9375\u503c\u5c0d\n buckets[index] = Pair(key: key, val: val)\n size += 1\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u7528\u522a\u9664\u6a19\u8a18\u8986\u84cb\u5b83\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n buckets[index] = TOMBSTONE\n size -= 1\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n func extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n let bucketsTmp = buckets\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n capacity *= extendRatio\n buckets = Array(repeating: nil, count: capacity)\n size = 0\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for pair in bucketsTmp {\n if let pair, pair != TOMBSTONE {\n put(key: pair.key, val: pair.val)\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n func print() {\n for pair in buckets {\n if pair == nil {\n Swift.print(\"null\")\n } else if pair == TOMBSTONE {\n Swift.print(\"TOMBSTONE\")\n } else {\n Swift.print(\"\\(pair!.key) -&gt; \\(pair!.val)\")\n }\n }\n }\n}\n</code></pre> hash_map_open_addressing.js<pre><code>/* \u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868 */\nclass HashMapOpenAddressing {\n #size; // \u9375\u503c\u5c0d\u6578\u91cf\n #capacity; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n #loadThres; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n #extendRatio; // \u64f4\u5bb9\u500d\u6578\n #buckets; // \u6876\u9663\u5217\n #TOMBSTONE; // \u522a\u9664\u6a19\u8a18\n\n /* \u5efa\u69cb\u5b50 */\n constructor() {\n this.#size = 0; // \u9375\u503c\u5c0d\u6578\u91cf\n this.#capacity = 4; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n this.#loadThres = 2.0 / 3.0; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n this.#extendRatio = 2; // \u64f4\u5bb9\u500d\u6578\n this.#buckets = Array(this.#capacity).fill(null); // \u6876\u9663\u5217\n this.#TOMBSTONE = new Pair(-1, '-1'); // \u522a\u9664\u6a19\u8a18\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n #hashFunc(key) {\n return key % this.#capacity;\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n #loadFactor() {\n return this.#size / this.#capacity;\n }\n\n /* \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15 */\n #findBucket(key) {\n let index = this.#hashFunc(key);\n let firstTombstone = -1;\n // \u7dda\u6027\u63a2\u67e5\uff0c\u7576\u9047\u5230\u7a7a\u6876\u6642\u8df3\u51fa\n while (this.#buckets[index] !== null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if (this.#buckets[index].key === key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u522a\u9664\u6a19\u8a18\uff0c\u5247\u5c07\u9375\u503c\u5c0d\u79fb\u52d5\u81f3\u8a72\u7d22\u5f15\u8655\n if (firstTombstone !== -1) {\n this.#buckets[firstTombstone] = this.#buckets[index];\n this.#buckets[index] = this.#TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52d5\u5f8c\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8a18\u9304\u9047\u5230\u7684\u9996\u500b\u522a\u9664\u6a19\u8a18\n if (\n firstTombstone === -1 &amp;&amp;\n this.#buckets[index] === this.#TOMBSTONE\n ) {\n firstTombstone = index;\n }\n // \u8a08\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u904e\u5c3e\u90e8\u5247\u8fd4\u56de\u982d\u90e8\n index = (index + 1) % this.#capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u65b0\u589e\u9ede\u7684\u7d22\u5f15\n return firstTombstone === -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n get(key) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n if (\n this.#buckets[index] !== null &amp;&amp;\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n return this.#buckets[index].val;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de null\n return null;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n put(key, val) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (this.#loadFactor() &gt; this.#loadThres) {\n this.#extend();\n }\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8986\u84cb val \u4e26\u8fd4\u56de\n if (\n this.#buckets[index] !== null &amp;&amp;\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n this.#buckets[index].val = val;\n return;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u65b0\u589e\u8a72\u9375\u503c\u5c0d\n this.#buckets[index] = new Pair(key, val);\n this.#size++;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n remove(key) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u7528\u522a\u9664\u6a19\u8a18\u8986\u84cb\u5b83\n if (\n this.#buckets[index] !== null &amp;&amp;\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n this.#buckets[index] = this.#TOMBSTONE;\n this.#size--;\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n #extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = Array(this.#capacity).fill(null);\n this.#size = 0;\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for (const pair of bucketsTmp) {\n if (pair !== null &amp;&amp; pair !== this.#TOMBSTONE) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n print() {\n for (const pair of this.#buckets) {\n if (pair === null) {\n console.log('null');\n } else if (pair === this.#TOMBSTONE) {\n console.log('TOMBSTONE');\n } else {\n console.log(pair.key + ' -&gt; ' + pair.val);\n }\n }\n }\n}\n</code></pre> hash_map_open_addressing.ts<pre><code>/* \u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868 */\nclass HashMapOpenAddressing {\n private size: number; // \u9375\u503c\u5c0d\u6578\u91cf\n private capacity: number; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n private loadThres: number; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n private extendRatio: number; // \u64f4\u5bb9\u500d\u6578\n private buckets: Array&lt;Pair | null&gt;; // \u6876\u9663\u5217\n private TOMBSTONE: Pair; // \u522a\u9664\u6a19\u8a18\n\n /* \u5efa\u69cb\u5b50 */\n constructor() {\n this.size = 0; // \u9375\u503c\u5c0d\u6578\u91cf\n this.capacity = 4; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n this.loadThres = 2.0 / 3.0; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n this.extendRatio = 2; // \u64f4\u5bb9\u500d\u6578\n this.buckets = Array(this.capacity).fill(null); // \u6876\u9663\u5217\n this.TOMBSTONE = new Pair(-1, '-1'); // \u522a\u9664\u6a19\u8a18\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n private hashFunc(key: number): number {\n return key % this.capacity;\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n private loadFactor(): number {\n return this.size / this.capacity;\n }\n\n /* \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15 */\n private findBucket(key: number): number {\n let index = this.hashFunc(key);\n let firstTombstone = -1;\n // \u7dda\u6027\u63a2\u67e5\uff0c\u7576\u9047\u5230\u7a7a\u6876\u6642\u8df3\u51fa\n while (this.buckets[index] !== null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if (this.buckets[index]!.key === key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u522a\u9664\u6a19\u8a18\uff0c\u5247\u5c07\u9375\u503c\u5c0d\u79fb\u52d5\u81f3\u8a72\u7d22\u5f15\u8655\n if (firstTombstone !== -1) {\n this.buckets[firstTombstone] = this.buckets[index];\n this.buckets[index] = this.TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52d5\u5f8c\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8a18\u9304\u9047\u5230\u7684\u9996\u500b\u522a\u9664\u6a19\u8a18\n if (\n firstTombstone === -1 &amp;&amp;\n this.buckets[index] === this.TOMBSTONE\n ) {\n firstTombstone = index;\n }\n // \u8a08\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u904e\u5c3e\u90e8\u5247\u8fd4\u56de\u982d\u90e8\n index = (index + 1) % this.capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u65b0\u589e\u9ede\u7684\u7d22\u5f15\n return firstTombstone === -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n get(key: number): string | null {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n if (\n this.buckets[index] !== null &amp;&amp;\n this.buckets[index] !== this.TOMBSTONE\n ) {\n return this.buckets[index]!.val;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de null\n return null;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n put(key: number, val: string): void {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (this.loadFactor() &gt; this.loadThres) {\n this.extend();\n }\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8986\u84cb val \u4e26\u8fd4\u56de\n if (\n this.buckets[index] !== null &amp;&amp;\n this.buckets[index] !== this.TOMBSTONE\n ) {\n this.buckets[index]!.val = val;\n return;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u65b0\u589e\u8a72\u9375\u503c\u5c0d\n this.buckets[index] = new Pair(key, val);\n this.size++;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n remove(key: number): void {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u7528\u522a\u9664\u6a19\u8a18\u8986\u84cb\u5b83\n if (\n this.buckets[index] !== null &amp;&amp;\n this.buckets[index] !== this.TOMBSTONE\n ) {\n this.buckets[index] = this.TOMBSTONE;\n this.size--;\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n private extend(): void {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n const bucketsTmp = this.buckets;\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n this.capacity *= this.extendRatio;\n this.buckets = Array(this.capacity).fill(null);\n this.size = 0;\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for (const pair of bucketsTmp) {\n if (pair !== null &amp;&amp; pair !== this.TOMBSTONE) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n print(): void {\n for (const pair of this.buckets) {\n if (pair === null) {\n console.log('null');\n } else if (pair === this.TOMBSTONE) {\n console.log('TOMBSTONE');\n } else {\n console.log(pair.key + ' -&gt; ' + pair.val);\n }\n }\n }\n}\n</code></pre> hash_map_open_addressing.dart<pre><code>/* \u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868 */\nclass HashMapOpenAddressing {\n late int _size; // \u9375\u503c\u5c0d\u6578\u91cf\n int _capacity = 4; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n double _loadThres = 2.0 / 3.0; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n int _extendRatio = 2; // \u64f4\u5bb9\u500d\u6578\n late List&lt;Pair?&gt; _buckets; // \u6876\u9663\u5217\n Pair _TOMBSTONE = Pair(-1, \"-1\"); // \u522a\u9664\u6a19\u8a18\n\n /* \u5efa\u69cb\u5b50 */\n HashMapOpenAddressing() {\n _size = 0;\n _buckets = List.generate(_capacity, (index) =&gt; null);\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n int hashFunc(int key) {\n return key % _capacity;\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n double loadFactor() {\n return _size / _capacity;\n }\n\n /* \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15 */\n int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7dda\u6027\u63a2\u67e5\uff0c\u7576\u9047\u5230\u7a7a\u6876\u6642\u8df3\u51fa\n while (_buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if (_buckets[index]!.key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u522a\u9664\u6a19\u8a18\uff0c\u5247\u5c07\u9375\u503c\u5c0d\u79fb\u52d5\u81f3\u8a72\u7d22\u5f15\u8655\n if (firstTombstone != -1) {\n _buckets[firstTombstone] = _buckets[index];\n _buckets[index] = _TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52d5\u5f8c\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8a18\u9304\u9047\u5230\u7684\u9996\u500b\u522a\u9664\u6a19\u8a18\n if (firstTombstone == -1 &amp;&amp; _buckets[index] == _TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8a08\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u904e\u5c3e\u90e8\u5247\u8fd4\u56de\u982d\u90e8\n index = (index + 1) % _capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u65b0\u589e\u9ede\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n String? get(int key) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n if (_buckets[index] != null &amp;&amp; _buckets[index] != _TOMBSTONE) {\n return _buckets[index]!.val;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de null\n return null;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n void put(int key, String val) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (loadFactor() &gt; _loadThres) {\n extend();\n }\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8986\u84cb val \u4e26\u8fd4\u56de\n if (_buckets[index] != null &amp;&amp; _buckets[index] != _TOMBSTONE) {\n _buckets[index]!.val = val;\n return;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u65b0\u589e\u8a72\u9375\u503c\u5c0d\n _buckets[index] = new Pair(key, val);\n _size++;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n void remove(int key) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u7528\u522a\u9664\u6a19\u8a18\u8986\u84cb\u5b83\n if (_buckets[index] != null &amp;&amp; _buckets[index] != _TOMBSTONE) {\n _buckets[index] = _TOMBSTONE;\n _size--;\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n void extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n List&lt;Pair?&gt; bucketsTmp = _buckets;\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n _capacity *= _extendRatio;\n _buckets = List.generate(_capacity, (index) =&gt; null);\n _size = 0;\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for (Pair? pair in bucketsTmp) {\n if (pair != null &amp;&amp; pair != _TOMBSTONE) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n void printHashMap() {\n for (Pair? pair in _buckets) {\n if (pair == null) {\n print(\"null\");\n } else if (pair == _TOMBSTONE) {\n print(\"TOMBSTONE\");\n } else {\n print(\"${pair.key} -&gt; ${pair.val}\");\n }\n }\n }\n}\n</code></pre> hash_map_open_addressing.rs<pre><code>/* \u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868 */\nstruct HashMapOpenAddressing {\n size: usize, // \u9375\u503c\u5c0d\u6578\u91cf\n capacity: usize, // \u96dc\u6e4a\u8868\u5bb9\u91cf\n load_thres: f64, // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n extend_ratio: usize, // \u64f4\u5bb9\u500d\u6578\n buckets: Vec&lt;Option&lt;Pair&gt;&gt;, // \u6876\u9663\u5217\n TOMBSTONE: Option&lt;Pair&gt;, // \u522a\u9664\u6a19\u8a18\n}\n\nimpl HashMapOpenAddressing {\n /* \u5efa\u69cb\u5b50 */\n fn new() -&gt; Self {\n Self {\n size: 0,\n capacity: 4,\n load_thres: 2.0 / 3.0,\n extend_ratio: 2,\n buckets: vec![None; 4],\n TOMBSTONE: Some(Pair {\n key: -1,\n val: \"-1\".to_string(),\n }),\n }\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n fn hash_func(&amp;self, key: i32) -&gt; usize {\n (key % self.capacity as i32) as usize\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n fn load_factor(&amp;self) -&gt; f64 {\n self.size as f64 / self.capacity as f64\n }\n\n /* \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15 */\n fn find_bucket(&amp;mut self, key: i32) -&gt; usize {\n let mut index = self.hash_func(key);\n let mut first_tombstone = -1;\n // \u7dda\u6027\u63a2\u67e5\uff0c\u7576\u9047\u5230\u7a7a\u6876\u6642\u8df3\u51fa\n while self.buckets[index].is_some() {\n // \u82e5\u9047\u5230 key\uff0c\u8fd4\u56de\u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if self.buckets[index].as_ref().unwrap().key == key {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u522a\u9664\u6a19\u8a18\uff0c\u5247\u5c07\u5efa\u503c\u5c0d\u79fb\u52d5\u81f3\u8a72\u7d22\u5f15\n if first_tombstone != -1 {\n self.buckets[first_tombstone as usize] = self.buckets[index].take();\n self.buckets[index] = self.TOMBSTONE.clone();\n return first_tombstone as usize; // \u8fd4\u56de\u79fb\u52d5\u5f8c\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8a18\u9304\u9047\u5230\u7684\u9996\u500b\u522a\u9664\u6a19\u8a18\n if first_tombstone == -1 &amp;&amp; self.buckets[index] == self.TOMBSTONE {\n first_tombstone = index as i32;\n }\n // \u8a08\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u904e\u5c3e\u90e8\u5247\u8fd4\u56de\u982d\u90e8\n index = (index + 1) % self.capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u65b0\u589e\u9ede\u7684\u7d22\u5f15\n if first_tombstone == -1 {\n index\n } else {\n first_tombstone as usize\n }\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n fn get(&amp;mut self, key: i32) -&gt; Option&lt;&amp;str&gt; {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n if self.buckets[index].is_some() &amp;&amp; self.buckets[index] != self.TOMBSTONE {\n return self.buckets[index].as_ref().map(|pair| &amp;pair.val as &amp;str);\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de null\n None\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n fn put(&amp;mut self, key: i32, val: String) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if self.load_factor() &gt; self.load_thres {\n self.extend();\n }\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8986\u84cb val \u4e26\u8fd4\u56de\n if self.buckets[index].is_some() &amp;&amp; self.buckets[index] != self.TOMBSTONE {\n self.buckets[index].as_mut().unwrap().val = val;\n return;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u65b0\u589e\u8a72\u9375\u503c\u5c0d\n self.buckets[index] = Some(Pair { key, val });\n self.size += 1;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n fn remove(&amp;mut self, key: i32) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u7528\u522a\u9664\u6a19\u8a18\u8986\u84cb\u5b83\n if self.buckets[index].is_some() &amp;&amp; self.buckets[index] != self.TOMBSTONE {\n self.buckets[index] = self.TOMBSTONE.clone();\n self.size -= 1;\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n fn extend(&amp;mut self) {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n let buckets_tmp = self.buckets.clone();\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n self.capacity *= self.extend_ratio;\n self.buckets = vec![None; self.capacity];\n self.size = 0;\n\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for pair in buckets_tmp {\n if pair.is_none() || pair == self.TOMBSTONE {\n continue;\n }\n let pair = pair.unwrap();\n\n self.put(pair.key, pair.val);\n }\n }\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n fn print(&amp;self) {\n for pair in &amp;self.buckets {\n if pair.is_none() {\n println!(\"null\");\n } else if pair == &amp;self.TOMBSTONE {\n println!(\"TOMBSTONE\");\n } else {\n let pair = pair.as_ref().unwrap();\n println!(\"{} -&gt; {}\", pair.key, pair.val);\n }\n }\n }\n}\n</code></pre> hash_map_open_addressing.c<pre><code>/* \u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868 */\ntypedef struct {\n int size; // \u9375\u503c\u5c0d\u6578\u91cf\n int capacity; // \u96dc\u6e4a\u8868\u5bb9\u91cf\n double loadThres; // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n int extendRatio; // \u64f4\u5bb9\u500d\u6578\n Pair **buckets; // \u6876\u9663\u5217\n Pair *TOMBSTONE; // \u522a\u9664\u6a19\u8a18\n} HashMapOpenAddressing;\n\n/* \u5efa\u69cb\u5b50 */\nHashMapOpenAddressing *newHashMapOpenAddressing() {\n HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing));\n hashMap-&gt;size = 0;\n hashMap-&gt;capacity = 4;\n hashMap-&gt;loadThres = 2.0 / 3.0;\n hashMap-&gt;extendRatio = 2;\n hashMap-&gt;buckets = (Pair **)malloc(sizeof(Pair *) * hashMap-&gt;capacity);\n hashMap-&gt;TOMBSTONE = (Pair *)malloc(sizeof(Pair));\n hashMap-&gt;TOMBSTONE-&gt;key = -1;\n hashMap-&gt;TOMBSTONE-&gt;val = \"-1\";\n\n return hashMap;\n}\n\n/* \u6790\u69cb\u51fd\u5f0f */\nvoid delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {\n for (int i = 0; i &lt; hashMap-&gt;capacity; i++) {\n Pair *pair = hashMap-&gt;buckets[i];\n if (pair != NULL &amp;&amp; pair != hashMap-&gt;TOMBSTONE) {\n free(pair-&gt;val);\n free(pair);\n }\n }\n free(hashMap-&gt;buckets);\n free(hashMap-&gt;TOMBSTONE);\n free(hashMap);\n}\n\n/* \u96dc\u6e4a\u51fd\u5f0f */\nint hashFunc(HashMapOpenAddressing *hashMap, int key) {\n return key % hashMap-&gt;capacity;\n}\n\n/* \u8ca0\u8f09\u56e0\u5b50 */\ndouble loadFactor(HashMapOpenAddressing *hashMap) {\n return (double)hashMap-&gt;size / (double)hashMap-&gt;capacity;\n}\n\n/* \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15 */\nint findBucket(HashMapOpenAddressing *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n int firstTombstone = -1;\n // \u7dda\u6027\u63a2\u67e5\uff0c\u7576\u9047\u5230\u7a7a\u6876\u6642\u8df3\u51fa\n while (hashMap-&gt;buckets[index] != NULL) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if (hashMap-&gt;buckets[index]-&gt;key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u522a\u9664\u6a19\u8a18\uff0c\u5247\u5c07\u9375\u503c\u5c0d\u79fb\u52d5\u81f3\u8a72\u7d22\u5f15\u8655\n if (firstTombstone != -1) {\n hashMap-&gt;buckets[firstTombstone] = hashMap-&gt;buckets[index];\n hashMap-&gt;buckets[index] = hashMap-&gt;TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52d5\u5f8c\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8a18\u9304\u9047\u5230\u7684\u9996\u500b\u522a\u9664\u6a19\u8a18\n if (firstTombstone == -1 &amp;&amp; hashMap-&gt;buckets[index] == hashMap-&gt;TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8a08\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u904e\u5c3e\u90e8\u5247\u8fd4\u56de\u982d\u90e8\n index = (index + 1) % hashMap-&gt;capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u65b0\u589e\u9ede\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n}\n\n/* \u67e5\u8a62\u64cd\u4f5c */\nchar *get(HashMapOpenAddressing *hashMap, int key) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n if (hashMap-&gt;buckets[index] != NULL &amp;&amp; hashMap-&gt;buckets[index] != hashMap-&gt;TOMBSTONE) {\n return hashMap-&gt;buckets[index]-&gt;val;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u7a7a\u5b57\u4e32\n return \"\";\n}\n\n/* \u65b0\u589e\u64cd\u4f5c */\nvoid put(HashMapOpenAddressing *hashMap, int key, char *val) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (loadFactor(hashMap) &gt; hashMap-&gt;loadThres) {\n extend(hashMap);\n }\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8986\u84cb val \u4e26\u8fd4\u56de\n if (hashMap-&gt;buckets[index] != NULL &amp;&amp; hashMap-&gt;buckets[index] != hashMap-&gt;TOMBSTONE) {\n free(hashMap-&gt;buckets[index]-&gt;val);\n hashMap-&gt;buckets[index]-&gt;val = (char *)malloc(sizeof(strlen(val) + 1));\n strcpy(hashMap-&gt;buckets[index]-&gt;val, val);\n hashMap-&gt;buckets[index]-&gt;val[strlen(val)] = '\\0';\n return;\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u65b0\u589e\u8a72\u9375\u503c\u5c0d\n Pair *pair = (Pair *)malloc(sizeof(Pair));\n pair-&gt;key = key;\n pair-&gt;val = (char *)malloc(sizeof(strlen(val) + 1));\n strcpy(pair-&gt;val, val);\n pair-&gt;val[strlen(val)] = '\\0';\n\n hashMap-&gt;buckets[index] = pair;\n hashMap-&gt;size++;\n}\n\n/* \u522a\u9664\u64cd\u4f5c */\nvoid removeItem(HashMapOpenAddressing *hashMap, int key) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u7528\u522a\u9664\u6a19\u8a18\u8986\u84cb\u5b83\n if (hashMap-&gt;buckets[index] != NULL &amp;&amp; hashMap-&gt;buckets[index] != hashMap-&gt;TOMBSTONE) {\n Pair *pair = hashMap-&gt;buckets[index];\n free(pair-&gt;val);\n free(pair);\n hashMap-&gt;buckets[index] = hashMap-&gt;TOMBSTONE;\n hashMap-&gt;size--;\n }\n}\n\n/* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\nvoid extend(HashMapOpenAddressing *hashMap) {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n Pair **bucketsTmp = hashMap-&gt;buckets;\n int oldCapacity = hashMap-&gt;capacity;\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n hashMap-&gt;capacity *= hashMap-&gt;extendRatio;\n hashMap-&gt;buckets = (Pair **)malloc(sizeof(Pair *) * hashMap-&gt;capacity);\n hashMap-&gt;size = 0;\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for (int i = 0; i &lt; oldCapacity; i++) {\n Pair *pair = bucketsTmp[i];\n if (pair != NULL &amp;&amp; pair != hashMap-&gt;TOMBSTONE) {\n put(hashMap, pair-&gt;key, pair-&gt;val);\n free(pair-&gt;val);\n free(pair);\n }\n }\n free(bucketsTmp);\n}\n\n/* \u5217\u5370\u96dc\u6e4a\u8868 */\nvoid print(HashMapOpenAddressing *hashMap) {\n for (int i = 0; i &lt; hashMap-&gt;capacity; i++) {\n Pair *pair = hashMap-&gt;buckets[i];\n if (pair == NULL) {\n printf(\"NULL\\n\");\n } else if (pair == hashMap-&gt;TOMBSTONE) {\n printf(\"TOMBSTONE\\n\");\n } else {\n printf(\"%d -&gt; %s\\n\", pair-&gt;key, pair-&gt;val);\n }\n }\n}\n</code></pre> hash_map_open_addressing.kt<pre><code>/* \u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868 */\nclass HashMapOpenAddressing {\n private var size: Int // \u9375\u503c\u5c0d\u6578\u91cf\n private var capacity: Int // \u96dc\u6e4a\u8868\u5bb9\u91cf\n private val loadThres: Double // \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n private val extendRatio: Int // \u64f4\u5bb9\u500d\u6578\n private var buckets: Array&lt;Pair?&gt; // \u6876\u9663\u5217\n private val TOMBSTONE: Pair // \u522a\u9664\u6a19\u8a18\n\n /* \u5efa\u69cb\u5b50 */\n init {\n size = 0\n capacity = 4\n loadThres = 2.0 / 3.0\n extendRatio = 2\n buckets = arrayOfNulls(capacity)\n TOMBSTONE = Pair(-1, \"-1\")\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n fun hashFunc(key: Int): Int {\n return key % capacity\n }\n\n /* \u8ca0\u8f09\u56e0\u5b50 */\n fun loadFactor(): Double {\n return (size / capacity).toDouble()\n }\n\n /* \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15 */\n fun findBucket(key: Int): Int {\n var index = hashFunc(key)\n var firstTombstone = -1\n // \u7dda\u6027\u63a2\u67e5\uff0c\u7576\u9047\u5230\u7a7a\u6876\u6642\u8df3\u51fa\n while (buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if (buckets[index]?.key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u522a\u9664\u6a19\u8a18\uff0c\u5247\u5c07\u9375\u503c\u5c0d\u79fb\u52d5\u81f3\u8a72\u7d22\u5f15\u8655\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index]\n buckets[index] = TOMBSTONE\n return firstTombstone // \u8fd4\u56de\u79fb\u52d5\u5f8c\u7684\u6876\u7d22\u5f15\n }\n return index // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8a18\u9304\u9047\u5230\u7684\u9996\u500b\u522a\u9664\u6a19\u8a18\n if (firstTombstone == -1 &amp;&amp; buckets[index] == TOMBSTONE) {\n firstTombstone = index\n }\n // \u8a08\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u904e\u5c3e\u90e8\u5247\u8fd4\u56de\u982d\u90e8\n index = (index + 1) % capacity\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u65b0\u589e\u9ede\u7684\u7d22\u5f15\n return if (firstTombstone == -1) index else firstTombstone\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n fun get(key: Int): String? {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n val index = findBucket(key)\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n if (buckets[index] != null &amp;&amp; buckets[index] != TOMBSTONE) {\n return buckets[index]?._val\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de null\n return null\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n fun put(key: Int, _val: String) {\n // \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n if (loadFactor() &gt; loadThres) {\n extend()\n }\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n val index = findBucket(key)\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8986\u84cb val \u4e26\u8fd4\u56de\n if (buckets[index] != null &amp;&amp; buckets[index] != TOMBSTONE) {\n buckets[index]!!._val = _val\n return\n }\n // \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u65b0\u589e\u8a72\u9375\u503c\u5c0d\n buckets[index] = Pair(key, _val)\n size++\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n fun remove(key: Int) {\n // \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n val index = findBucket(key)\n // \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u7528\u522a\u9664\u6a19\u8a18\u8986\u84cb\u5b83\n if (buckets[index] != null &amp;&amp; buckets[index] != TOMBSTONE) {\n buckets[index] = TOMBSTONE\n size--\n }\n }\n\n /* \u64f4\u5bb9\u96dc\u6e4a\u8868 */\n fun extend() {\n // \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n val bucketsTmp = buckets\n // \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n capacity *= extendRatio\n buckets = arrayOfNulls(capacity)\n size = 0\n // \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for (pair in bucketsTmp) {\n if (pair != null &amp;&amp; pair != TOMBSTONE) {\n put(pair.key, pair._val)\n }\n }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n fun print() {\n for (pair in buckets) {\n if (pair == null) {\n println(\"null\")\n } else if (pair == TOMBSTONE) {\n println(\"TOMESTOME\")\n } else {\n println(\"${pair.key} -&gt; ${pair._val}\")\n }\n }\n }\n}\n</code></pre> hash_map_open_addressing.rb<pre><code>### \u958b\u653e\u5b9a\u5740\u96dc\u6e4a\u8868 ###\nclass HashMapOpenAddressing\n TOMBSTONE = Pair.new(-1, '-1') # \u522a\u9664\u6a19\u8a18\n\n ### \u5efa\u69cb\u5b50 ###\n def initialize\n @size = 0 # \u9375\u503c\u5c0d\u6578\u91cf\n @capacity = 4 # \u96dc\u6e4a\u8868\u5bb9\u91cf\n @load_thres = 2.0 / 3.0 # \u89f8\u767c\u64f4\u5bb9\u7684\u8ca0\u8f09\u56e0\u5b50\u95be\u503c\n @extend_ratio = 2 # \u64f4\u5bb9\u500d\u6578\n @buckets = Array.new(@capacity) # \u6876\u9663\u5217\n end\n\n ### \u96dc\u6e4a\u51fd\u5f0f ###\n def hash_func(key)\n key % @capacity\n end\n\n ### \u8ca0\u8f09\u56e0\u5b50 ###\n def load_factor\n @size / @capacity\n end\n\n ### \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15 ###\n def find_bucket(key)\n index = hash_func(key)\n first_tombstone = -1\n # \u7dda\u6027\u63a2\u67e5\uff0c\u7576\u9047\u5230\u7a7a\u6876\u6642\u8df3\u51fa\n while !@buckets[index].nil?\n # \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n if @buckets[index].key == key\n # \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u522a\u9664\u6a19\u8a18\uff0c\u5247\u5c07\u9375\u503c\u5c0d\u79fb\u52d5\u81f3\u8a72\u7d22\u5f15\u8655\n if first_tombstone != -1\n @buckets[first_tombstone] = @buckets[index]\n @buckets[index] = TOMBSTONE\n return first_tombstone # \u8fd4\u56de\u79fb\u52d5\u5f8c\u7684\u6876\u7d22\u5f15\n end\n return index # \u8fd4\u56de\u6876\u7d22\u5f15\n end\n # \u8a18\u9304\u9047\u5230\u7684\u9996\u500b\u522a\u9664\u6a19\u8a18\n first_tombstone = index if first_tombstone == -1 &amp;&amp; @buckets[index] == TOMBSTONE\n # \u8a08\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u904e\u5c3e\u90e8\u5247\u8fd4\u56de\u982d\u90e8\n index = (index + 1) % @capacity\n end\n # \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de\u65b0\u589e\u9ede\u7684\u7d22\u5f15\n first_tombstone == -1 ? index : first_tombstone\n end\n\n ### \u67e5\u8a62\u64cd\u4f5c ###\n def get(key)\n # \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n index = find_bucket(key)\n # \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8fd4\u56de\u5c0d\u61c9 val\n return @buckets[index].val unless [nil, TOMBSTONE].include?(@buckets[index])\n # \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de nil\n nil\n end\n\n ### \u65b0\u589e\u64cd\u4f5c ###\n def put(key, val)\n # \u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e\u95be\u503c\u6642\uff0c\u57f7\u884c\u64f4\u5bb9\n extend if load_factor &gt; @load_thres\n # \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n index = find_bucket(key)\n # \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u8986\u84cb val \u958b\u8fd4\u56de\n unless [nil, TOMBSTONE].include?(@buckets[index])\n @buckets[index].val = val\n return\n end\n # \u82e5\u9375\u503c\u5c0d\u4e0d\u5b58\u5728\uff0c\u5247\u65b0\u589e\u8a72\u9375\u503c\u5c0d\n @buckets[index] = Pair.new(key, val)\n @size += 1\n end\n\n ### \u522a\u9664\u64cd\u4f5c ###\n def remove(key)\n # \u641c\u5c0b key \u5c0d\u61c9\u7684\u6876\u7d22\u5f15\n index = find_bucket(key)\n # \u82e5\u627e\u5230\u9375\u503c\u5c0d\uff0c\u5247\u7528\u522a\u9664\u6a19\u8a18\u8986\u84cb\u5b83\n unless [nil, TOMBSTONE].include?(@buckets[index])\n @buckets[index] = TOMBSTONE\n @size -= 1\n end\n end\n\n ### \u64f4\u5bb9\u96dc\u6e4a\u8868 ###\n def extend\n # \u66ab\u5b58\u539f\u96dc\u6e4a\u8868\n buckets_tmp = @buckets\n # \u521d\u59cb\u5316\u64f4\u5bb9\u5f8c\u7684\u65b0\u96dc\u6e4a\u8868\n @capacity *= @extend_ratio\n @buckets = Array.new(@capacity)\n @size = 0\n # \u5c07\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u642c\u904b\u81f3\u65b0\u96dc\u6e4a\u8868\n for pair in buckets_tmp\n put(pair.key, pair.val) unless [nil, TOMBSTONE].include?(pair)\n end\n end\n\n ### \u5217\u5370\u96dc\u6e4a\u8868 ###\n def print\n for pair in @buckets\n if pair.nil?\n puts \"Nil\"\n elsif pair == TOMBSTONE\n puts \"TOMBSTONE\"\n else\n puts \"#{pair.key} -&gt; #{pair.val}\"\n end\n end\n end\nend\n</code></pre> hash_map_open_addressing.zig<pre><code>[class]{HashMapOpenAddressing}-[func]{}\n</code></pre>"},{"location":"chapter_hashing/hash_collision/#2","title":"2. \u00a0 \u5e73\u65b9\u63a2\u6e2c","text":"<p>\u5e73\u65b9\u63a2\u6e2c\u8207\u7dda\u6027\u63a2\u67e5\u985e\u4f3c\uff0c\u90fd\u662f\u958b\u653e\u5b9a\u5740\u7684\u5e38\u898b\u7b56\u7565\u4e4b\u4e00\u3002\u7576\u767c\u751f\u885d\u7a81\u6642\uff0c\u5e73\u65b9\u63a2\u6e2c\u4e0d\u662f\u7c21\u55ae\u5730\u8df3\u904e\u4e00\u500b\u56fa\u5b9a\u7684\u6b65\u6578\uff0c\u800c\u662f\u8df3\u904e\u201c\u63a2\u6e2c\u6b21\u6578\u7684\u5e73\u65b9\u201d\u7684\u6b65\u6578\uff0c\u5373 \\(1, 4, 9, \\dots\\) \u6b65\u3002</p> <p>\u5e73\u65b9\u63a2\u6e2c\u4e3b\u8981\u5177\u6709\u4ee5\u4e0b\u512a\u52e2\u3002</p> <ul> <li>\u5e73\u65b9\u63a2\u6e2c\u900f\u904e\u8df3\u904e\u63a2\u6e2c\u6b21\u6578\u5e73\u65b9\u7684\u8ddd\u96e2\uff0c\u8a66\u5716\u7de9\u89e3\u7dda\u6027\u63a2\u67e5\u7684\u805a\u96c6\u6548\u61c9\u3002</li> <li>\u5e73\u65b9\u63a2\u6e2c\u6703\u8df3\u904e\u66f4\u5927\u7684\u8ddd\u96e2\u4f86\u5c0b\u627e\u7a7a\u4f4d\u7f6e\uff0c\u6709\u52a9\u65bc\u8cc7\u6599\u5206\u4f48\u5f97\u66f4\u52a0\u5747\u52fb\u3002</li> </ul> <p>\u7136\u800c\uff0c\u5e73\u65b9\u63a2\u6e2c\u4e26\u4e0d\u662f\u5b8c\u7f8e\u7684\u3002</p> <ul> <li>\u4ecd\u7136\u5b58\u5728\u805a\u96c6\u73fe\u8c61\uff0c\u5373\u67d0\u4e9b\u4f4d\u7f6e\u6bd4\u5176\u4ed6\u4f4d\u7f6e\u66f4\u5bb9\u6613\u88ab\u4f54\u7528\u3002</li> <li>\u7531\u65bc\u5e73\u65b9\u7684\u589e\u9577\uff0c\u5e73\u65b9\u63a2\u6e2c\u53ef\u80fd\u4e0d\u6703\u63a2\u6e2c\u6574\u500b\u96dc\u6e4a\u8868\uff0c\u9019\u610f\u5473\u8457\u5373\u4f7f\u96dc\u6e4a\u8868\u4e2d\u6709\u7a7a\u6876\uff0c\u5e73\u65b9\u63a2\u6e2c\u4e5f\u53ef\u80fd\u7121\u6cd5\u8a2a\u554f\u5230\u5b83\u3002</li> </ul>"},{"location":"chapter_hashing/hash_collision/#3","title":"3. \u00a0 \u591a\u6b21\u96dc\u6e4a","text":"<p>\u9867\u540d\u601d\u7fa9\uff0c\u591a\u6b21\u96dc\u6e4a\u65b9\u6cd5\u4f7f\u7528\u591a\u500b\u96dc\u6e4a\u51fd\u5f0f \\(f_1(x)\\)\u3001\\(f_2(x)\\)\u3001\\(f_3(x)\\)\u3001\\(\\dots\\) \u9032\u884c\u63a2\u6e2c\u3002</p> <ul> <li>\u63d2\u5165\u5143\u7d20\uff1a\u82e5\u96dc\u6e4a\u51fd\u5f0f \\(f_1(x)\\) \u51fa\u73fe\u885d\u7a81\uff0c\u5247\u5617\u8a66 \\(f_2(x)\\) \uff0c\u4ee5\u6b64\u985e\u63a8\uff0c\u76f4\u5230\u627e\u5230\u7a7a\u4f4d\u5f8c\u63d2\u5165\u5143\u7d20\u3002</li> <li>\u67e5\u8a62\u5143\u7d20\uff1a\u5728\u76f8\u540c\u7684\u96dc\u6e4a\u51fd\u5f0f\u9806\u5e8f\u4e0b\u9032\u884c\u67e5\u8a62\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6a19\u5143\u7d20\u6642\u8fd4\u56de\uff1b\u82e5\u9047\u5230\u7a7a\u4f4d\u6216\u5df2\u5617\u8a66\u6240\u6709\u96dc\u6e4a\u51fd\u5f0f\uff0c\u8aaa\u660e\u96dc\u6e4a\u8868\u4e2d\u4e0d\u5b58\u5728\u8a72\u5143\u7d20\uff0c\u5247\u8fd4\u56de <code>None</code> \u3002</li> </ul> <p>\u8207\u7dda\u6027\u63a2\u67e5\u76f8\u6bd4\uff0c\u591a\u6b21\u96dc\u6e4a\u65b9\u6cd5\u4e0d\u6613\u7522\u751f\u805a\u96c6\uff0c\u4f46\u591a\u500b\u96dc\u6e4a\u51fd\u5f0f\u6703\u5e36\u4f86\u984d\u5916\u7684\u8a08\u7b97\u91cf\u3002</p> <p>Tip</p> <p>\u8acb\u6ce8\u610f\uff0c\u958b\u653e\u5b9a\u5740\uff08\u7dda\u6027\u63a2\u67e5\u3001\u5e73\u65b9\u63a2\u6e2c\u548c\u591a\u6b21\u96dc\u6e4a\uff09\u96dc\u6e4a\u8868\u90fd\u5b58\u5728\u201c\u4e0d\u80fd\u76f4\u63a5\u522a\u9664\u5143\u7d20\u201d\u7684\u554f\u984c\u3002</p>"},{"location":"chapter_hashing/hash_collision/#623","title":"6.2.3 \u00a0 \u7a0b\u5f0f\u8a9e\u8a00\u7684\u9078\u64c7","text":"<p>\u5404\u7a2e\u7a0b\u5f0f\u8a9e\u8a00\u63a1\u53d6\u4e86\u4e0d\u540c\u7684\u96dc\u6e4a\u8868\u5be6\u73fe\u7b56\u7565\uff0c\u4e0b\u9762\u8209\u5e7e\u500b\u4f8b\u5b50\u3002</p> <ul> <li>Python \u63a1\u7528\u958b\u653e\u5b9a\u5740\u3002\u5b57\u5178 <code>dict</code> \u4f7f\u7528\u507d\u96a8\u6a5f\u6578\u9032\u884c\u63a2\u6e2c\u3002</li> <li>Java \u63a1\u7528\u93c8\u5f0f\u4f4d\u5740\u3002\u81ea JDK 1.8 \u4ee5\u4f86\uff0c\u7576 <code>HashMap</code> \u5167\u9663\u5217\u9577\u5ea6\u9054\u5230 64 \u4e14\u93c8\u7d50\u4e32\u5217\u9577\u5ea6\u9054\u5230 8 \u6642\uff0c\u93c8\u7d50\u4e32\u5217\u6703\u8f49\u63db\u70ba\u7d05\u9ed1\u6a39\u4ee5\u63d0\u5347\u67e5\u8a62\u6548\u80fd\u3002</li> <li>Go \u63a1\u7528\u93c8\u5f0f\u4f4d\u5740\u3002Go \u898f\u5b9a\u6bcf\u500b\u6876\u6700\u591a\u5132\u5b58 8 \u500b\u9375\u503c\u5c0d\uff0c\u8d85\u51fa\u5bb9\u91cf\u5247\u9023\u7dda\u4e00\u500b\u6ea2\u4f4d\u6876\uff1b\u7576\u6ea2\u4f4d\u6876\u904e\u591a\u6642\uff0c\u6703\u57f7\u884c\u4e00\u6b21\u7279\u6b8a\u7684\u7b49\u91cf\u64f4\u5bb9\u64cd\u4f5c\uff0c\u4ee5\u78ba\u4fdd\u6548\u80fd\u3002</li> </ul>"},{"location":"chapter_hashing/hash_map/","title":"6.1 \u00a0 \u96dc\u6e4a\u8868","text":"<p>\u96dc\u6e4a\u8868\uff08hash table\uff09\uff0c\u53c8\u7a31\u6563\u5217\u8868\uff0c\u5b83\u900f\u904e\u5efa\u7acb\u9375 <code>key</code> \u8207\u503c <code>value</code> \u4e4b\u9593\u7684\u5c0d\u6620\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u8a62\u3002\u5177\u9ad4\u800c\u8a00\uff0c\u6211\u5011\u5411\u96dc\u6e4a\u8868\u4e2d\u8f38\u5165\u4e00\u500b\u9375 <code>key</code> \uff0c\u5247\u53ef\u4ee5\u5728 \\(O(1)\\) \u6642\u9593\u5167\u7372\u53d6\u5c0d\u61c9\u7684\u503c <code>value</code> \u3002</p> <p>\u5982\u5716 6-1 \u6240\u793a\uff0c\u7d66\u5b9a \\(n\\) \u500b\u5b78\u751f\uff0c\u6bcf\u500b\u5b78\u751f\u90fd\u6709\u201c\u59d3\u540d\u201d\u548c\u201c\u5b78\u865f\u201d\u5169\u9805\u8cc7\u6599\u3002\u5047\u5982\u6211\u5011\u5e0c\u671b\u5be6\u73fe\u201c\u8f38\u5165\u4e00\u500b\u5b78\u865f\uff0c\u8fd4\u56de\u5c0d\u61c9\u7684\u59d3\u540d\u201d\u7684\u67e5\u8a62\u529f\u80fd\uff0c\u5247\u53ef\u4ee5\u63a1\u7528\u5716 6-1 \u6240\u793a\u7684\u96dc\u6e4a\u8868\u4f86\u5be6\u73fe\u3002</p> <p></p> <p> \u5716 6-1 \u00a0 \u96dc\u6e4a\u8868\u7684\u62bd\u8c61\u8868\u793a </p> <p>\u9664\u96dc\u6e4a\u8868\u5916\uff0c\u9663\u5217\u548c\u93c8\u7d50\u4e32\u5217\u4e5f\u53ef\u4ee5\u5be6\u73fe\u67e5\u8a62\u529f\u80fd\uff0c\u5b83\u5011\u7684\u6548\u7387\u5c0d\u6bd4\u5982\u8868 6-1 \u6240\u793a\u3002</p> <ul> <li>\u65b0\u589e\u5143\u7d20\uff1a\u50c5\u9700\u5c07\u5143\u7d20\u65b0\u589e\u81f3\u9663\u5217\uff08\u93c8\u7d50\u4e32\u5217\uff09\u7684\u5c3e\u90e8\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u6642\u9593\u3002</li> <li>\u67e5\u8a62\u5143\u7d20\uff1a\u7531\u65bc\u9663\u5217\uff08\u93c8\u7d50\u4e32\u5217\uff09\u662f\u4e82\u5e8f\u7684\uff0c\u56e0\u6b64\u9700\u8981\u8d70\u8a2a\u5176\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u4f7f\u7528 \\(O(n)\\) \u6642\u9593\u3002</li> <li>\u522a\u9664\u5143\u7d20\uff1a\u9700\u8981\u5148\u67e5\u8a62\u5230\u5143\u7d20\uff0c\u518d\u5f9e\u9663\u5217\uff08\u93c8\u7d50\u4e32\u5217\uff09\u4e2d\u522a\u9664\uff0c\u4f7f\u7528 \\(O(n)\\) \u6642\u9593\u3002</li> </ul> <p> \u8868 6-1 \u00a0 \u5143\u7d20\u67e5\u8a62\u6548\u7387\u5c0d\u6bd4 </p> \u9663\u5217 \u93c8\u7d50\u4e32\u5217 \u96dc\u6e4a\u8868 \u67e5\u8a62\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) \u65b0\u589e\u5143\u7d20 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u522a\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) <p>\u89c0\u5bdf\u767c\u73fe\uff0c\u5728\u96dc\u6e4a\u8868\u4e2d\u9032\u884c\u589e\u522a\u67e5\u6539\u7684\u6642\u9593\u8907\u96dc\u5ea6\u90fd\u662f \\(O(1)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002</p>"},{"location":"chapter_hashing/hash_map/#611","title":"6.1.1 \u00a0 \u96dc\u6e4a\u8868\u5e38\u7528\u64cd\u4f5c","text":"<p>\u96dc\u6e4a\u8868\u7684\u5e38\u898b\u64cd\u4f5c\u5305\u62ec\uff1a\u521d\u59cb\u5316\u3001\u67e5\u8a62\u64cd\u4f5c\u3001\u65b0\u589e\u9375\u503c\u5c0d\u548c\u522a\u9664\u9375\u503c\u5c0d\u7b49\uff0c\u793a\u4f8b\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig hash_map.py<pre><code># \u521d\u59cb\u5316\u96dc\u6e4a\u8868\nhmap: dict = {}\n\n# \u65b0\u589e\u64cd\u4f5c\n# \u5728\u96dc\u6e4a\u8868\u4e2d\u65b0\u589e\u9375\u503c\u5c0d (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u56c9\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9d28\"\n\n# \u67e5\u8a62\u64cd\u4f5c\n# \u5411\u96dc\u6e4a\u8868\u4e2d\u8f38\u5165\u9375 key \uff0c\u5f97\u5230\u503c value\nname: str = hmap[15937]\n\n# \u522a\u9664\u64cd\u4f5c\n# \u5728\u96dc\u6e4a\u8868\u4e2d\u522a\u9664\u9375\u503c\u5c0d (key, value)\nhmap.pop(10583)\n</code></pre> hash_map.cpp<pre><code>/* \u521d\u59cb\u5316\u96dc\u6e4a\u8868 */\nunordered_map&lt;int, string&gt; map;\n\n/* \u65b0\u589e\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u65b0\u589e\u9375\u503c\u5c0d (key, value)\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u56c9\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9d28\";\n\n/* \u67e5\u8a62\u64cd\u4f5c */\n// \u5411\u96dc\u6e4a\u8868\u4e2d\u8f38\u5165\u9375 key \uff0c\u5f97\u5230\u503c value\nstring name = map[15937];\n\n/* \u522a\u9664\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u522a\u9664\u9375\u503c\u5c0d (key, value)\nmap.erase(10583);\n</code></pre> hash_map.java<pre><code>/* \u521d\u59cb\u5316\u96dc\u6e4a\u8868 */\nMap&lt;Integer, String&gt; map = new HashMap&lt;&gt;();\n\n/* \u65b0\u589e\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u65b0\u589e\u9375\u503c\u5c0d (key, value)\nmap.put(12836, \"\u5c0f\u54c8\");\nmap.put(15937, \"\u5c0f\u56c9\");\nmap.put(16750, \"\u5c0f\u7b97\");\nmap.put(13276, \"\u5c0f\u6cd5\");\nmap.put(10583, \"\u5c0f\u9d28\");\n\n/* \u67e5\u8a62\u64cd\u4f5c */\n// \u5411\u96dc\u6e4a\u8868\u4e2d\u8f38\u5165\u9375 key \uff0c\u5f97\u5230\u503c value\nString name = map.get(15937);\n\n/* \u522a\u9664\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u522a\u9664\u9375\u503c\u5c0d (key, value)\nmap.remove(10583);\n</code></pre> hash_map.cs<pre><code>/* \u521d\u59cb\u5316\u96dc\u6e4a\u8868 */\nDictionary&lt;int, string&gt; map = new() {\n /* \u65b0\u589e\u64cd\u4f5c */\n // \u5728\u96dc\u6e4a\u8868\u4e2d\u65b0\u589e\u9375\u503c\u5c0d (key, value)\n { 12836, \"\u5c0f\u54c8\" },\n { 15937, \"\u5c0f\u56c9\" },\n { 16750, \"\u5c0f\u7b97\" },\n { 13276, \"\u5c0f\u6cd5\" },\n { 10583, \"\u5c0f\u9d28\" }\n};\n\n/* \u67e5\u8a62\u64cd\u4f5c */\n// \u5411\u96dc\u6e4a\u8868\u4e2d\u8f38\u5165\u9375 key \uff0c\u5f97\u5230\u503c value\nstring name = map[15937];\n\n/* \u522a\u9664\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u522a\u9664\u9375\u503c\u5c0d (key, value)\nmap.Remove(10583);\n</code></pre> hash_map_test.go<pre><code>/* \u521d\u59cb\u5316\u96dc\u6e4a\u8868 */\nhmap := make(map[int]string)\n\n/* \u65b0\u589e\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u65b0\u589e\u9375\u503c\u5c0d (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u56c9\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9d28\"\n\n/* \u67e5\u8a62\u64cd\u4f5c */\n// \u5411\u96dc\u6e4a\u8868\u4e2d\u8f38\u5165\u9375 key \uff0c\u5f97\u5230\u503c value\nname := hmap[15937]\n\n/* \u522a\u9664\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u522a\u9664\u9375\u503c\u5c0d (key, value)\ndelete(hmap, 10583)\n</code></pre> hash_map.swift<pre><code>/* \u521d\u59cb\u5316\u96dc\u6e4a\u8868 */\nvar map: [Int: String] = [:]\n\n/* \u65b0\u589e\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u65b0\u589e\u9375\u503c\u5c0d (key, value)\nmap[12836] = \"\u5c0f\u54c8\"\nmap[15937] = \"\u5c0f\u56c9\"\nmap[16750] = \"\u5c0f\u7b97\"\nmap[13276] = \"\u5c0f\u6cd5\"\nmap[10583] = \"\u5c0f\u9d28\"\n\n/* \u67e5\u8a62\u64cd\u4f5c */\n// \u5411\u96dc\u6e4a\u8868\u4e2d\u8f38\u5165\u9375 key \uff0c\u5f97\u5230\u503c value\nlet name = map[15937]!\n\n/* \u522a\u9664\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u522a\u9664\u9375\u503c\u5c0d (key, value)\nmap.removeValue(forKey: 10583)\n</code></pre> hash_map.js<pre><code>/* \u521d\u59cb\u5316\u96dc\u6e4a\u8868 */\nconst map = new Map();\n/* \u65b0\u589e\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u65b0\u589e\u9375\u503c\u5c0d (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u56c9');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9d28');\n\n/* \u67e5\u8a62\u64cd\u4f5c */\n// \u5411\u96dc\u6e4a\u8868\u4e2d\u8f38\u5165\u9375 key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\n\n/* \u522a\u9664\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u522a\u9664\u9375\u503c\u5c0d (key, value)\nmap.delete(10583);\n</code></pre> hash_map.ts<pre><code>/* \u521d\u59cb\u5316\u96dc\u6e4a\u8868 */\nconst map = new Map&lt;number, string&gt;();\n/* \u65b0\u589e\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u65b0\u589e\u9375\u503c\u5c0d (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u56c9');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9d28');\nconsole.info('\\n\u65b0\u589e\u5b8c\u6210\u5f8c\uff0c\u96dc\u6e4a\u8868\u70ba\\nKey -&gt; Value');\nconsole.info(map);\n\n/* \u67e5\u8a62\u64cd\u4f5c */\n// \u5411\u96dc\u6e4a\u8868\u4e2d\u8f38\u5165\u9375 key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\nconsole.info('\\n\u8f38\u5165\u5b78\u865f 15937 \uff0c\u67e5\u8a62\u5230\u59d3\u540d ' + name);\n\n/* \u522a\u9664\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u522a\u9664\u9375\u503c\u5c0d (key, value)\nmap.delete(10583);\nconsole.info('\\n\u522a\u9664 10583 \u5f8c\uff0c\u96dc\u6e4a\u8868\u70ba\\nKey -&gt; Value');\nconsole.info(map);\n</code></pre> hash_map.dart<pre><code>/* \u521d\u59cb\u5316\u96dc\u6e4a\u8868 */\nMap&lt;int, String&gt; map = {};\n\n/* \u65b0\u589e\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u65b0\u589e\u9375\u503c\u5c0d (key, value)\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u56c9\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9d28\";\n\n/* \u67e5\u8a62\u64cd\u4f5c */\n// \u5411\u96dc\u6e4a\u8868\u4e2d\u8f38\u5165\u9375 key \uff0c\u5f97\u5230\u503c value\nString name = map[15937];\n\n/* \u522a\u9664\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u522a\u9664\u9375\u503c\u5c0d (key, value)\nmap.remove(10583);\n</code></pre> hash_map.rs<pre><code>use std::collections::HashMap;\n\n/* \u521d\u59cb\u5316\u96dc\u6e4a\u8868 */\nlet mut map: HashMap&lt;i32, String&gt; = HashMap::new();\n\n/* \u65b0\u589e\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u65b0\u589e\u9375\u503c\u5c0d (key, value)\nmap.insert(12836, \"\u5c0f\u54c8\".to_string());\nmap.insert(15937, \"\u5c0f\u56c9\".to_string());\nmap.insert(16750, \"\u5c0f\u7b97\".to_string());\nmap.insert(13279, \"\u5c0f\u6cd5\".to_string());\nmap.insert(10583, \"\u5c0f\u9d28\".to_string());\n\n/* \u67e5\u8a62\u64cd\u4f5c */\n// \u5411\u96dc\u6e4a\u8868\u4e2d\u8f38\u5165\u9375 key \uff0c\u5f97\u5230\u503c value\nlet _name: Option&lt;&amp;String&gt; = map.get(&amp;15937);\n\n/* \u522a\u9664\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u522a\u9664\u9375\u503c\u5c0d (key, value)\nlet _removed_value: Option&lt;String&gt; = map.remove(&amp;10583);\n</code></pre> hash_map.c<pre><code>// C \u672a\u63d0\u4f9b\u5167\u5efa\u96dc\u6e4a\u8868\n</code></pre> hash_map.kt<pre><code>/* \u521d\u59cb\u5316\u96dc\u6e4a\u8868 */\nval map = HashMap&lt;Int,String&gt;()\n\n/* \u65b0\u589e\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u65b0\u589e\u9375\u503c\u5c0d (key, value)\nmap[12836] = \"\u5c0f\u54c8\"\nmap[15937] = \"\u5c0f\u56c9\"\nmap[16750] = \"\u5c0f\u7b97\"\nmap[13276] = \"\u5c0f\u6cd5\"\nmap[10583] = \"\u5c0f\u9d28\"\n\n/* \u67e5\u8a62\u64cd\u4f5c */\n// \u5411\u96dc\u6e4a\u8868\u4e2d\u8f38\u5165\u9375 key \uff0c\u5f97\u5230\u503c value\nval name = map[15937]\n\n/* \u522a\u9664\u64cd\u4f5c */\n// \u5728\u96dc\u6e4a\u8868\u4e2d\u522a\u9664\u9375\u503c\u5c0d (key, value)\nmap.remove(10583)\n</code></pre> hash_map.rb<pre><code># \u521d\u59cb\u5316\u96dc\u6e4a\u8868\nhmap = {}\n\n# \u65b0\u589e\u64cd\u4f5c\n# \u5728\u96dc\u6e4a\u8868\u4e2d\u65b0\u589e\u9375\u503c\u5c0d (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u56c9\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9d28\"\n\n# \u67e5\u8a62\u64cd\u4f5c\n# \u5411\u96dc\u6e4a\u8868\u4e2d\u8f38\u5165\u9375 key \uff0c\u5f97\u5230\u503c value\nname = hmap[15937]\n\n# \u522a\u9664\u64cd\u4f5c\n# \u5728\u96dc\u6e4a\u8868\u4e2d\u522a\u9664\u9375\u503c\u5c0d (key, value)\nhmap.delete(10583)\n</code></pre> hash_map.zig<pre><code>\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u96dc\u6e4a\u8868\u6709\u4e09\u7a2e\u5e38\u7528\u7684\u8d70\u8a2a\u65b9\u5f0f\uff1a\u8d70\u8a2a\u9375\u503c\u5c0d\u3001\u8d70\u8a2a\u9375\u548c\u8d70\u8a2a\u503c\u3002\u793a\u4f8b\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig hash_map.py<pre><code># \u8d70\u8a2a\u96dc\u6e4a\u8868\n# \u8d70\u8a2a\u9375\u503c\u5c0d key-&gt;value\nfor key, value in hmap.items():\n print(key, \"-&gt;\", value)\n# \u55ae\u7368\u8d70\u8a2a\u9375 key\nfor key in hmap.keys():\n print(key)\n# \u55ae\u7368\u8d70\u8a2a\u503c value\nfor value in hmap.values():\n print(value)\n</code></pre> hash_map.cpp<pre><code>/* \u8d70\u8a2a\u96dc\u6e4a\u8868 */\n// \u8d70\u8a2a\u9375\u503c\u5c0d key-&gt;value\nfor (auto kv: map) {\n cout &lt;&lt; kv.first &lt;&lt; \" -&gt; \" &lt;&lt; kv.second &lt;&lt; endl;\n}\n// \u4f7f\u7528\u8fed\u4ee3\u5668\u8d70\u8a2a key-&gt;value\nfor (auto iter = map.begin(); iter != map.end(); iter++) {\n cout &lt;&lt; iter-&gt;first &lt;&lt; \"-&gt;\" &lt;&lt; iter-&gt;second &lt;&lt; endl;\n}\n</code></pre> hash_map.java<pre><code>/* \u8d70\u8a2a\u96dc\u6e4a\u8868 */\n// \u8d70\u8a2a\u9375\u503c\u5c0d key-&gt;value\nfor (Map.Entry &lt;Integer, String&gt; kv: map.entrySet()) {\n System.out.println(kv.getKey() + \" -&gt; \" + kv.getValue());\n}\n// \u55ae\u7368\u8d70\u8a2a\u9375 key\nfor (int key: map.keySet()) {\n System.out.println(key);\n}\n// \u55ae\u7368\u8d70\u8a2a\u503c value\nfor (String val: map.values()) {\n System.out.println(val);\n}\n</code></pre> hash_map.cs<pre><code>/* \u8d70\u8a2a\u96dc\u6e4a\u8868 */\n// \u8d70\u8a2a\u9375\u503c\u5c0d Key-&gt;Value\nforeach (var kv in map) {\n Console.WriteLine(kv.Key + \" -&gt; \" + kv.Value);\n}\n// \u55ae\u7368\u8d70\u8a2a\u9375 key\nforeach (int key in map.Keys) {\n Console.WriteLine(key);\n}\n// \u55ae\u7368\u8d70\u8a2a\u503c value\nforeach (string val in map.Values) {\n Console.WriteLine(val);\n}\n</code></pre> hash_map_test.go<pre><code>/* \u8d70\u8a2a\u96dc\u6e4a\u8868 */\n// \u8d70\u8a2a\u9375\u503c\u5c0d key-&gt;value\nfor key, value := range hmap {\n fmt.Println(key, \"-&gt;\", value)\n}\n// \u55ae\u7368\u8d70\u8a2a\u9375 key\nfor key := range hmap {\n fmt.Println(key)\n}\n// \u55ae\u7368\u8d70\u8a2a\u503c value\nfor _, value := range hmap {\n fmt.Println(value)\n}\n</code></pre> hash_map.swift<pre><code>/* \u8d70\u8a2a\u96dc\u6e4a\u8868 */\n// \u8d70\u8a2a\u9375\u503c\u5c0d Key-&gt;Value\nfor (key, value) in map {\n print(\"\\(key) -&gt; \\(value)\")\n}\n// \u55ae\u7368\u8d70\u8a2a\u9375 Key\nfor key in map.keys {\n print(key)\n}\n// \u55ae\u7368\u8d70\u8a2a\u503c Value\nfor value in map.values {\n print(value)\n}\n</code></pre> hash_map.js<pre><code>/* \u8d70\u8a2a\u96dc\u6e4a\u8868 */\nconsole.info('\\n\u8d70\u8a2a\u9375\u503c\u5c0d Key-&gt;Value');\nfor (const [k, v] of map.entries()) {\n console.info(k + ' -&gt; ' + v);\n}\nconsole.info('\\n\u55ae\u7368\u8d70\u8a2a\u9375 Key');\nfor (const k of map.keys()) {\n console.info(k);\n}\nconsole.info('\\n\u55ae\u7368\u8d70\u8a2a\u503c Value');\nfor (const v of map.values()) {\n console.info(v);\n}\n</code></pre> hash_map.ts<pre><code>/* \u8d70\u8a2a\u96dc\u6e4a\u8868 */\nconsole.info('\\n\u8d70\u8a2a\u9375\u503c\u5c0d Key-&gt;Value');\nfor (const [k, v] of map.entries()) {\n console.info(k + ' -&gt; ' + v);\n}\nconsole.info('\\n\u55ae\u7368\u8d70\u8a2a\u9375 Key');\nfor (const k of map.keys()) {\n console.info(k);\n}\nconsole.info('\\n\u55ae\u7368\u8d70\u8a2a\u503c Value');\nfor (const v of map.values()) {\n console.info(v);\n}\n</code></pre> hash_map.dart<pre><code>/* \u8d70\u8a2a\u96dc\u6e4a\u8868 */\n// \u8d70\u8a2a\u9375\u503c\u5c0d Key-&gt;Value\nmap.forEach((key, value) {\n print('$key -&gt; $value');\n});\n\n// \u55ae\u7368\u8d70\u8a2a\u9375 Key\nmap.keys.forEach((key) {\n print(key);\n});\n\n// \u55ae\u7368\u8d70\u8a2a\u503c Value\nmap.values.forEach((value) {\n print(value);\n});\n</code></pre> hash_map.rs<pre><code>/* \u8d70\u8a2a\u96dc\u6e4a\u8868 */\n// \u8d70\u8a2a\u9375\u503c\u5c0d Key-&gt;Value\nfor (key, value) in &amp;map {\n println!(\"{key} -&gt; {value}\");\n}\n\n// \u55ae\u7368\u8d70\u8a2a\u9375 Key\nfor key in map.keys() {\n println!(\"{key}\");\n}\n\n// \u55ae\u7368\u8d70\u8a2a\u503c Value\nfor value in map.values() {\n println!(\"{value}\");\n}\n</code></pre> hash_map.c<pre><code>// C \u672a\u63d0\u4f9b\u5167\u5efa\u96dc\u6e4a\u8868\n</code></pre> hash_map.kt<pre><code>/* \u8d70\u8a2a\u96dc\u6e4a\u8868 */\n// \u8d70\u8a2a\u9375\u503c\u5c0d key-&gt;value\nfor ((key, value) in map) {\n println(\"$key -&gt; $value\")\n}\n// \u55ae\u7368\u8d70\u8a2a\u9375 key\nfor (key in map.keys) {\n println(key)\n}\n// \u55ae\u7368\u8d70\u8a2a\u503c value\nfor (_val in map.values) {\n println(_val)\n}\n</code></pre> hash_map.rb<pre><code># \u8d70\u8a2a\u96dc\u6e4a\u8868\n# \u8d70\u8a2a\u9375\u503c\u5c0d key-&gt;value\nhmap.entries.each { |key, value| puts \"#{key} -&gt; #{value}\" }\n\n# \u55ae\u7368\u8d70\u8a2a\u9375 key\nhmap.keys.each { |key| puts key }\n\n# \u55ae\u7368\u8d70\u8a2a\u503c value\nhmap.values.each { |val| puts val }\n</code></pre> hash_map.zig<pre><code>\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_hashing/hash_map/#612","title":"6.1.2 \u00a0 \u96dc\u6e4a\u8868\u7c21\u55ae\u5be6\u73fe","text":"<p>\u6211\u5011\u5148\u8003\u616e\u6700\u7c21\u55ae\u7684\u60c5\u6cc1\uff0c\u50c5\u7528\u4e00\u500b\u9663\u5217\u4f86\u5be6\u73fe\u96dc\u6e4a\u8868\u3002\u5728\u96dc\u6e4a\u8868\u4e2d\uff0c\u6211\u5011\u5c07\u9663\u5217\u4e2d\u7684\u6bcf\u500b\u7a7a\u4f4d\u7a31\u70ba\u6876\uff08bucket\uff09\uff0c\u6bcf\u500b\u6876\u53ef\u5132\u5b58\u4e00\u500b\u9375\u503c\u5c0d\u3002\u56e0\u6b64\uff0c\u67e5\u8a62\u64cd\u4f5c\u5c31\u662f\u627e\u5230 <code>key</code> \u5c0d\u61c9\u7684\u6876\uff0c\u4e26\u5728\u6876\u4e2d\u7372\u53d6 <code>value</code> \u3002</p> <p>\u90a3\u9ebc\uff0c\u5982\u4f55\u57fa\u65bc <code>key</code> \u5b9a\u4f4d\u5c0d\u61c9\u7684\u6876\u5462\uff1f\u9019\u662f\u900f\u904e\u96dc\u6e4a\u51fd\u5f0f\uff08hash function\uff09\u5be6\u73fe\u7684\u3002\u96dc\u6e4a\u51fd\u5f0f\u7684\u4f5c\u7528\u662f\u5c07\u4e00\u500b\u8f03\u5927\u7684\u8f38\u5165\u7a7a\u9593\u5c0d\u6620\u5230\u4e00\u500b\u8f03\u5c0f\u7684\u8f38\u51fa\u7a7a\u9593\u3002\u5728\u96dc\u6e4a\u8868\u4e2d\uff0c\u8f38\u5165\u7a7a\u9593\u662f\u6240\u6709 <code>key</code> \uff0c\u8f38\u51fa\u7a7a\u9593\u662f\u6240\u6709\u6876\uff08\u9663\u5217\u7d22\u5f15\uff09\u3002\u63db\u53e5\u8a71\u8aaa\uff0c\u8f38\u5165\u4e00\u500b <code>key</code> \uff0c\u6211\u5011\u53ef\u4ee5\u900f\u904e\u96dc\u6e4a\u51fd\u5f0f\u5f97\u5230\u8a72 <code>key</code> \u5c0d\u61c9\u7684\u9375\u503c\u5c0d\u5728\u9663\u5217\u4e2d\u7684\u5132\u5b58\u4f4d\u7f6e\u3002</p> <p>\u8f38\u5165\u4e00\u500b <code>key</code> \uff0c\u96dc\u6e4a\u51fd\u5f0f\u7684\u8a08\u7b97\u904e\u7a0b\u5206\u70ba\u4ee5\u4e0b\u5169\u6b65\u3002</p> <ol> <li>\u900f\u904e\u67d0\u7a2e\u96dc\u6e4a\u6f14\u7b97\u6cd5 <code>hash()</code> \u8a08\u7b97\u5f97\u5230\u96dc\u6e4a\u503c\u3002</li> <li>\u5c07\u96dc\u6e4a\u503c\u5c0d\u6876\u6578\u91cf\uff08\u9663\u5217\u9577\u5ea6\uff09<code>capacity</code> \u53d6\u6a21\uff0c\u5f9e\u800c\u7372\u53d6\u8a72 <code>key</code> \u5c0d\u61c9\u7684\u9663\u5217\u7d22\u5f15 <code>index</code> \u3002</li> </ol> <pre><code>index = hash(key) % capacity\n</code></pre> <p>\u96a8\u5f8c\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u5229\u7528 <code>index</code> \u5728\u96dc\u6e4a\u8868\u4e2d\u8a2a\u554f\u5c0d\u61c9\u7684\u6876\uff0c\u5f9e\u800c\u7372\u53d6 <code>value</code> \u3002</p> <p>\u8a2d\u9663\u5217\u9577\u5ea6 <code>capacity = 100</code>\u3001\u96dc\u6e4a\u6f14\u7b97\u6cd5 <code>hash(key) = key</code> \uff0c\u6613\u5f97\u96dc\u6e4a\u51fd\u5f0f\u70ba <code>key % 100</code> \u3002\u5716 6-2 \u4ee5 <code>key</code> \u5b78\u865f\u548c <code>value</code> \u59d3\u540d\u70ba\u4f8b\uff0c\u5c55\u793a\u4e86\u96dc\u6e4a\u51fd\u5f0f\u7684\u5de5\u4f5c\u539f\u7406\u3002</p> <p></p> <p> \u5716 6-2 \u00a0 \u96dc\u6e4a\u51fd\u5f0f\u5de5\u4f5c\u539f\u7406 </p> <p>\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u5be6\u73fe\u4e86\u4e00\u500b\u7c21\u55ae\u96dc\u6e4a\u8868\u3002\u5176\u4e2d\uff0c\u6211\u5011\u5c07 <code>key</code> \u548c <code>value</code> \u5c01\u88dd\u6210\u4e00\u500b\u985e\u5225 <code>Pair</code> \uff0c\u4ee5\u8868\u793a\u9375\u503c\u5c0d\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array_hash_map.py<pre><code>class Pair:\n \"\"\"\u9375\u503c\u5c0d\"\"\"\n\n def __init__(self, key: int, val: str):\n self.key = key\n self.val = val\n\nclass ArrayHashMap:\n \"\"\"\u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u96dc\u6e4a\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u5efa\u69cb\u5b50\"\"\"\n # \u521d\u59cb\u5316\u9663\u5217\uff0c\u5305\u542b 100 \u500b\u6876\n self.buckets: list[Pair | None] = [None] * 100\n\n def hash_func(self, key: int) -&gt; int:\n \"\"\"\u96dc\u6e4a\u51fd\u5f0f\"\"\"\n index = key % 100\n return index\n\n def get(self, key: int) -&gt; str:\n \"\"\"\u67e5\u8a62\u64cd\u4f5c\"\"\"\n index: int = self.hash_func(key)\n pair: Pair = self.buckets[index]\n if pair is None:\n return None\n return pair.val\n\n def put(self, key: int, val: str):\n \"\"\"\u65b0\u589e\u64cd\u4f5c\"\"\"\n pair = Pair(key, val)\n index: int = self.hash_func(key)\n self.buckets[index] = pair\n\n def remove(self, key: int):\n \"\"\"\u522a\u9664\u64cd\u4f5c\"\"\"\n index: int = self.hash_func(key)\n # \u7f6e\u70ba None \uff0c\u4ee3\u8868\u522a\u9664\n self.buckets[index] = None\n\n def entry_set(self) -&gt; list[Pair]:\n \"\"\"\u7372\u53d6\u6240\u6709\u9375\u503c\u5c0d\"\"\"\n result: list[Pair] = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair)\n return result\n\n def key_set(self) -&gt; list[int]:\n \"\"\"\u7372\u53d6\u6240\u6709\u9375\"\"\"\n result = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair.key)\n return result\n\n def value_set(self) -&gt; list[str]:\n \"\"\"\u7372\u53d6\u6240\u6709\u503c\"\"\"\n result = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair.val)\n return result\n\n def print(self):\n \"\"\"\u5217\u5370\u96dc\u6e4a\u8868\"\"\"\n for pair in self.buckets:\n if pair is not None:\n print(pair.key, \"-&gt;\", pair.val)\n</code></pre> array_hash_map.cpp<pre><code>/* \u9375\u503c\u5c0d */\nstruct Pair {\n public:\n int key;\n string val;\n Pair(int key, string val) {\n this-&gt;key = key;\n this-&gt;val = val;\n }\n};\n\n/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u96dc\u6e4a\u8868 */\nclass ArrayHashMap {\n private:\n vector&lt;Pair *&gt; buckets;\n\n public:\n ArrayHashMap() {\n // \u521d\u59cb\u5316\u9663\u5217\uff0c\u5305\u542b 100 \u500b\u6876\n buckets = vector&lt;Pair *&gt;(100);\n }\n\n ~ArrayHashMap() {\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n for (const auto &amp;bucket : buckets) {\n delete bucket;\n }\n buckets.clear();\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n int hashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n string get(int key) {\n int index = hashFunc(key);\n Pair *pair = buckets[index];\n if (pair == nullptr)\n return \"\";\n return pair-&gt;val;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n void put(int key, string val) {\n Pair *pair = new Pair(key, val);\n int index = hashFunc(key);\n buckets[index] = pair;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n // \u91cb\u653e\u8a18\u61b6\u9ad4\u4e26\u7f6e\u70ba nullptr\n delete buckets[index];\n buckets[index] = nullptr;\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375\u503c\u5c0d */\n vector&lt;Pair *&gt; pairSet() {\n vector&lt;Pair *&gt; pairSet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n pairSet.push_back(pair);\n }\n }\n return pairSet;\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375 */\n vector&lt;int&gt; keySet() {\n vector&lt;int&gt; keySet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n keySet.push_back(pair-&gt;key);\n }\n }\n return keySet;\n }\n\n /* \u7372\u53d6\u6240\u6709\u503c */\n vector&lt;string&gt; valueSet() {\n vector&lt;string&gt; valueSet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n valueSet.push_back(pair-&gt;val);\n }\n }\n return valueSet;\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n void print() {\n for (Pair *kv : pairSet()) {\n cout &lt;&lt; kv-&gt;key &lt;&lt; \" -&gt; \" &lt;&lt; kv-&gt;val &lt;&lt; endl;\n }\n }\n};\n</code></pre> array_hash_map.java<pre><code>/* \u9375\u503c\u5c0d */\nclass Pair {\n public int key;\n public String val;\n\n public Pair(int key, String val) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u96dc\u6e4a\u8868 */\nclass ArrayHashMap {\n private List&lt;Pair&gt; buckets;\n\n public ArrayHashMap() {\n // \u521d\u59cb\u5316\u9663\u5217\uff0c\u5305\u542b 100 \u500b\u6876\n buckets = new ArrayList&lt;&gt;();\n for (int i = 0; i &lt; 100; i++) {\n buckets.add(null);\n }\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n private int hashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n public String get(int key) {\n int index = hashFunc(key);\n Pair pair = buckets.get(index);\n if (pair == null)\n return null;\n return pair.val;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n public void put(int key, String val) {\n Pair pair = new Pair(key, val);\n int index = hashFunc(key);\n buckets.set(index, pair);\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n public void remove(int key) {\n int index = hashFunc(key);\n // \u7f6e\u70ba null \uff0c\u4ee3\u8868\u522a\u9664\n buckets.set(index, null);\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375\u503c\u5c0d */\n public List&lt;Pair&gt; pairSet() {\n List&lt;Pair&gt; pairSet = new ArrayList&lt;&gt;();\n for (Pair pair : buckets) {\n if (pair != null)\n pairSet.add(pair);\n }\n return pairSet;\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375 */\n public List&lt;Integer&gt; keySet() {\n List&lt;Integer&gt; keySet = new ArrayList&lt;&gt;();\n for (Pair pair : buckets) {\n if (pair != null)\n keySet.add(pair.key);\n }\n return keySet;\n }\n\n /* \u7372\u53d6\u6240\u6709\u503c */\n public List&lt;String&gt; valueSet() {\n List&lt;String&gt; valueSet = new ArrayList&lt;&gt;();\n for (Pair pair : buckets) {\n if (pair != null)\n valueSet.add(pair.val);\n }\n return valueSet;\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n public void print() {\n for (Pair kv : pairSet()) {\n System.out.println(kv.key + \" -&gt; \" + kv.val);\n }\n }\n}\n</code></pre> array_hash_map.cs<pre><code>/* \u9375\u503c\u5c0d int-&gt;string */\nclass Pair(int key, string val) {\n public int key = key;\n public string val = val;\n}\n\n/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u96dc\u6e4a\u8868 */\nclass ArrayHashMap {\n List&lt;Pair?&gt; buckets;\n public ArrayHashMap() {\n // \u521d\u59cb\u5316\u9663\u5217\uff0c\u5305\u542b 100 \u500b\u6876\n buckets = [];\n for (int i = 0; i &lt; 100; i++) {\n buckets.Add(null);\n }\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n int HashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n public string? Get(int key) {\n int index = HashFunc(key);\n Pair? pair = buckets[index];\n if (pair == null) return null;\n return pair.val;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n public void Put(int key, string val) {\n Pair pair = new(key, val);\n int index = HashFunc(key);\n buckets[index] = pair;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n int index = HashFunc(key);\n // \u7f6e\u70ba null \uff0c\u4ee3\u8868\u522a\u9664\n buckets[index] = null;\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375\u503c\u5c0d */\n public List&lt;Pair&gt; PairSet() {\n List&lt;Pair&gt; pairSet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n pairSet.Add(pair);\n }\n return pairSet;\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375 */\n public List&lt;int&gt; KeySet() {\n List&lt;int&gt; keySet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n keySet.Add(pair.key);\n }\n return keySet;\n }\n\n /* \u7372\u53d6\u6240\u6709\u503c */\n public List&lt;string&gt; ValueSet() {\n List&lt;string&gt; valueSet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n valueSet.Add(pair.val);\n }\n return valueSet;\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n public void Print() {\n foreach (Pair kv in PairSet()) {\n Console.WriteLine(kv.key + \" -&gt; \" + kv.val);\n }\n }\n}\n</code></pre> array_hash_map.go<pre><code>/* \u9375\u503c\u5c0d */\ntype pair struct {\n key int\n val string\n}\n\n/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u96dc\u6e4a\u8868 */\ntype arrayHashMap struct {\n buckets []*pair\n}\n\n/* \u521d\u59cb\u5316\u96dc\u6e4a\u8868 */\nfunc newArrayHashMap() *arrayHashMap {\n // \u521d\u59cb\u5316\u9663\u5217\uff0c\u5305\u542b 100 \u500b\u6876\n buckets := make([]*pair, 100)\n return &amp;arrayHashMap{buckets: buckets}\n}\n\n/* \u96dc\u6e4a\u51fd\u5f0f */\nfunc (a *arrayHashMap) hashFunc(key int) int {\n index := key % 100\n return index\n}\n\n/* \u67e5\u8a62\u64cd\u4f5c */\nfunc (a *arrayHashMap) get(key int) string {\n index := a.hashFunc(key)\n pair := a.buckets[index]\n if pair == nil {\n return \"Not Found\"\n }\n return pair.val\n}\n\n/* \u65b0\u589e\u64cd\u4f5c */\nfunc (a *arrayHashMap) put(key int, val string) {\n pair := &amp;pair{key: key, val: val}\n index := a.hashFunc(key)\n a.buckets[index] = pair\n}\n\n/* \u522a\u9664\u64cd\u4f5c */\nfunc (a *arrayHashMap) remove(key int) {\n index := a.hashFunc(key)\n // \u7f6e\u70ba nil \uff0c\u4ee3\u8868\u522a\u9664\n a.buckets[index] = nil\n}\n\n/* \u7372\u53d6\u6240\u6709\u9375\u5c0d */\nfunc (a *arrayHashMap) pairSet() []*pair {\n var pairs []*pair\n for _, pair := range a.buckets {\n if pair != nil {\n pairs = append(pairs, pair)\n }\n }\n return pairs\n}\n\n/* \u7372\u53d6\u6240\u6709\u9375 */\nfunc (a *arrayHashMap) keySet() []int {\n var keys []int\n for _, pair := range a.buckets {\n if pair != nil {\n keys = append(keys, pair.key)\n }\n }\n return keys\n}\n\n/* \u7372\u53d6\u6240\u6709\u503c */\nfunc (a *arrayHashMap) valueSet() []string {\n var values []string\n for _, pair := range a.buckets {\n if pair != nil {\n values = append(values, pair.val)\n }\n }\n return values\n}\n\n/* \u5217\u5370\u96dc\u6e4a\u8868 */\nfunc (a *arrayHashMap) print() {\n for _, pair := range a.buckets {\n if pair != nil {\n fmt.Println(pair.key, \"-&gt;\", pair.val)\n }\n }\n}\n</code></pre> array_hash_map.swift<pre><code>/* \u9375\u503c\u5c0d */\nclass Pair: Equatable {\n public var key: Int\n public var val: String\n\n public init(key: Int, val: String) {\n self.key = key\n self.val = val\n }\n\n public static func == (lhs: Pair, rhs: Pair) -&gt; Bool {\n lhs.key == rhs.key &amp;&amp; lhs.val == rhs.val\n }\n}\n\n/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u96dc\u6e4a\u8868 */\nclass ArrayHashMap {\n private var buckets: [Pair?]\n\n init() {\n // \u521d\u59cb\u5316\u9663\u5217\uff0c\u5305\u542b 100 \u500b\u6876\n buckets = Array(repeating: nil, count: 100)\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n private func hashFunc(key: Int) -&gt; Int {\n let index = key % 100\n return index\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n func get(key: Int) -&gt; String? {\n let index = hashFunc(key: key)\n let pair = buckets[index]\n return pair?.val\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n func put(key: Int, val: String) {\n let pair = Pair(key: key, val: val)\n let index = hashFunc(key: key)\n buckets[index] = pair\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n let index = hashFunc(key: key)\n // \u7f6e\u70ba nil \uff0c\u4ee3\u8868\u522a\u9664\n buckets[index] = nil\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375\u503c\u5c0d */\n func pairSet() -&gt; [Pair] {\n buckets.compactMap { $0 }\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375 */\n func keySet() -&gt; [Int] {\n buckets.compactMap { $0?.key }\n }\n\n /* \u7372\u53d6\u6240\u6709\u503c */\n func valueSet() -&gt; [String] {\n buckets.compactMap { $0?.val }\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n func print() {\n for pair in pairSet() {\n Swift.print(\"\\(pair.key) -&gt; \\(pair.val)\")\n }\n }\n}\n</code></pre> array_hash_map.js<pre><code>/* \u9375\u503c\u5c0d Number -&gt; String */\nclass Pair {\n constructor(key, val) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u96dc\u6e4a\u8868 */\nclass ArrayHashMap {\n #buckets;\n constructor() {\n // \u521d\u59cb\u5316\u9663\u5217\uff0c\u5305\u542b 100 \u500b\u6876\n this.#buckets = new Array(100).fill(null);\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n #hashFunc(key) {\n return key % 100;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n get(key) {\n let index = this.#hashFunc(key);\n let pair = this.#buckets[index];\n if (pair === null) return null;\n return pair.val;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n set(key, val) {\n let index = this.#hashFunc(key);\n this.#buckets[index] = new Pair(key, val);\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n delete(key) {\n let index = this.#hashFunc(key);\n // \u7f6e\u70ba null \uff0c\u4ee3\u8868\u522a\u9664\n this.#buckets[index] = null;\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375\u503c\u5c0d */\n entries() {\n let arr = [];\n for (let i = 0; i &lt; this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i]);\n }\n }\n return arr;\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375 */\n keys() {\n let arr = [];\n for (let i = 0; i &lt; this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i].key);\n }\n }\n return arr;\n }\n\n /* \u7372\u53d6\u6240\u6709\u503c */\n values() {\n let arr = [];\n for (let i = 0; i &lt; this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i].val);\n }\n }\n return arr;\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n print() {\n let pairSet = this.entries();\n for (const pair of pairSet) {\n console.info(`${pair.key} -&gt; ${pair.val}`);\n }\n }\n}\n</code></pre> array_hash_map.ts<pre><code>/* \u9375\u503c\u5c0d Number -&gt; String */\nclass Pair {\n public key: number;\n public val: string;\n\n constructor(key: number, val: string) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u96dc\u6e4a\u8868 */\nclass ArrayHashMap {\n private readonly buckets: (Pair | null)[];\n\n constructor() {\n // \u521d\u59cb\u5316\u9663\u5217\uff0c\u5305\u542b 100 \u500b\u6876\n this.buckets = new Array(100).fill(null);\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n private hashFunc(key: number): number {\n return key % 100;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n public get(key: number): string | null {\n let index = this.hashFunc(key);\n let pair = this.buckets[index];\n if (pair === null) return null;\n return pair.val;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n public set(key: number, val: string) {\n let index = this.hashFunc(key);\n this.buckets[index] = new Pair(key, val);\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n public delete(key: number) {\n let index = this.hashFunc(key);\n // \u7f6e\u70ba null \uff0c\u4ee3\u8868\u522a\u9664\n this.buckets[index] = null;\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375\u503c\u5c0d */\n public entries(): (Pair | null)[] {\n let arr: (Pair | null)[] = [];\n for (let i = 0; i &lt; this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i]);\n }\n }\n return arr;\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375 */\n public keys(): (number | undefined)[] {\n let arr: (number | undefined)[] = [];\n for (let i = 0; i &lt; this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i].key);\n }\n }\n return arr;\n }\n\n /* \u7372\u53d6\u6240\u6709\u503c */\n public values(): (string | undefined)[] {\n let arr: (string | undefined)[] = [];\n for (let i = 0; i &lt; this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i].val);\n }\n }\n return arr;\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n public print() {\n let pairSet = this.entries();\n for (const pair of pairSet) {\n console.info(`${pair.key} -&gt; ${pair.val}`);\n }\n }\n}\n</code></pre> array_hash_map.dart<pre><code>/* \u9375\u503c\u5c0d */\nclass Pair {\n int key;\n String val;\n Pair(this.key, this.val);\n}\n\n/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u96dc\u6e4a\u8868 */\nclass ArrayHashMap {\n late List&lt;Pair?&gt; _buckets;\n\n ArrayHashMap() {\n // \u521d\u59cb\u5316\u9663\u5217\uff0c\u5305\u542b 100 \u500b\u6876\n _buckets = List.filled(100, null);\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n int _hashFunc(int key) {\n final int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n String? get(int key) {\n final int index = _hashFunc(key);\n final Pair? pair = _buckets[index];\n if (pair == null) {\n return null;\n }\n return pair.val;\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n void put(int key, String val) {\n final Pair pair = Pair(key, val);\n final int index = _hashFunc(key);\n _buckets[index] = pair;\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n void remove(int key) {\n final int index = _hashFunc(key);\n _buckets[index] = null;\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375\u503c\u5c0d */\n List&lt;Pair&gt; pairSet() {\n List&lt;Pair&gt; pairSet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n pairSet.add(pair);\n }\n }\n return pairSet;\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375 */\n List&lt;int&gt; keySet() {\n List&lt;int&gt; keySet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n keySet.add(pair.key);\n }\n }\n return keySet;\n }\n\n /* \u7372\u53d6\u6240\u6709\u503c */\n List&lt;String&gt; values() {\n List&lt;String&gt; valueSet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n valueSet.add(pair.val);\n }\n }\n return valueSet;\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n void printHashMap() {\n for (final Pair kv in pairSet()) {\n print(\"${kv.key} -&gt; ${kv.val}\");\n }\n }\n}\n</code></pre> array_hash_map.rs<pre><code>/* \u9375\u503c\u5c0d */\n#[derive(Debug, Clone, PartialEq)]\npub struct Pair {\n pub key: i32,\n pub val: String,\n}\n\n/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u96dc\u6e4a\u8868 */\npub struct ArrayHashMap {\n buckets: Vec&lt;Option&lt;Pair&gt;&gt;,\n}\n\nimpl ArrayHashMap {\n pub fn new() -&gt; ArrayHashMap {\n // \u521d\u59cb\u5316\u9663\u5217\uff0c\u5305\u542b 100 \u500b\u6876\n Self {\n buckets: vec![None; 100],\n }\n }\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n fn hash_func(&amp;self, key: i32) -&gt; usize {\n key as usize % 100\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n pub fn get(&amp;self, key: i32) -&gt; Option&lt;&amp;String&gt; {\n let index = self.hash_func(key);\n self.buckets[index].as_ref().map(|pair| &amp;pair.val)\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n pub fn put(&amp;mut self, key: i32, val: &amp;str) {\n let index = self.hash_func(key);\n self.buckets[index] = Some(Pair {\n key,\n val: val.to_string(),\n });\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n pub fn remove(&amp;mut self, key: i32) {\n let index = self.hash_func(key);\n // \u7f6e\u70ba None \uff0c\u4ee3\u8868\u522a\u9664\n self.buckets[index] = None;\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375\u503c\u5c0d */\n pub fn entry_set(&amp;self) -&gt; Vec&lt;&amp;Pair&gt; {\n self.buckets\n .iter()\n .filter_map(|pair| pair.as_ref())\n .collect()\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375 */\n pub fn key_set(&amp;self) -&gt; Vec&lt;&amp;i32&gt; {\n self.buckets\n .iter()\n .filter_map(|pair| pair.as_ref().map(|pair| &amp;pair.key))\n .collect()\n }\n\n /* \u7372\u53d6\u6240\u6709\u503c */\n pub fn value_set(&amp;self) -&gt; Vec&lt;&amp;String&gt; {\n self.buckets\n .iter()\n .filter_map(|pair| pair.as_ref().map(|pair| &amp;pair.val))\n .collect()\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n pub fn print(&amp;self) {\n for pair in self.entry_set() {\n println!(\"{} -&gt; {}\", pair.key, pair.val);\n }\n }\n}\n</code></pre> array_hash_map.c<pre><code>/* \u9375\u503c\u5c0d int-&gt;string */\ntypedef struct {\n int key;\n char *val;\n} Pair;\n\n/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u96dc\u6e4a\u8868 */\ntypedef struct {\n Pair *buckets[MAX_SIZE];\n} ArrayHashMap;\n\n/* \u5efa\u69cb\u5b50 */\nArrayHashMap *newArrayHashMap() {\n ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));\n for (int i=0; i &lt; MAX_SIZE; i++) {\n hmap-&gt;buckets[i] = NULL;\n }\n return hmap;\n}\n\n/* \u6790\u69cb\u51fd\u5f0f */\nvoid delArrayHashMap(ArrayHashMap *hmap) {\n for (int i = 0; i &lt; MAX_SIZE; i++) {\n if (hmap-&gt;buckets[i] != NULL) {\n free(hmap-&gt;buckets[i]-&gt;val);\n free(hmap-&gt;buckets[i]);\n }\n }\n free(hmap);\n}\n\n/* \u65b0\u589e\u64cd\u4f5c */\nvoid put(ArrayHashMap *hmap, const int key, const char *val) {\n Pair *Pair = malloc(sizeof(Pair));\n Pair-&gt;key = key;\n Pair-&gt;val = malloc(strlen(val) + 1);\n strcpy(Pair-&gt;val, val);\n\n int index = hashFunc(key);\n hmap-&gt;buckets[index] = Pair;\n}\n\n/* \u522a\u9664\u64cd\u4f5c */\nvoid removeItem(ArrayHashMap *hmap, const int key) {\n int index = hashFunc(key);\n free(hmap-&gt;buckets[index]-&gt;val);\n free(hmap-&gt;buckets[index]);\n hmap-&gt;buckets[index] = NULL;\n}\n\n/* \u7372\u53d6\u6240\u6709\u9375\u503c\u5c0d */\nvoid pairSet(ArrayHashMap *hmap, MapSet *set) {\n Pair *entries;\n int i = 0, index = 0;\n int total = 0;\n /* \u7d71\u8a08\u6709\u6548\u9375\u503c\u5c0d\u6578\u91cf */\n for (i = 0; i &lt; MAX_SIZE; i++) {\n if (hmap-&gt;buckets[i] != NULL) {\n total++;\n }\n }\n entries = malloc(sizeof(Pair) * total);\n for (i = 0; i &lt; MAX_SIZE; i++) {\n if (hmap-&gt;buckets[i] != NULL) {\n entries[index].key = hmap-&gt;buckets[i]-&gt;key;\n entries[index].val = malloc(strlen(hmap-&gt;buckets[i]-&gt;val) + 1);\n strcpy(entries[index].val, hmap-&gt;buckets[i]-&gt;val);\n index++;\n }\n }\n set-&gt;set = entries;\n set-&gt;len = total;\n}\n\n/* \u7372\u53d6\u6240\u6709\u9375 */\nvoid keySet(ArrayHashMap *hmap, MapSet *set) {\n int *keys;\n int i = 0, index = 0;\n int total = 0;\n /* \u7d71\u8a08\u6709\u6548\u9375\u503c\u5c0d\u6578\u91cf */\n for (i = 0; i &lt; MAX_SIZE; i++) {\n if (hmap-&gt;buckets[i] != NULL) {\n total++;\n }\n }\n keys = malloc(total * sizeof(int));\n for (i = 0; i &lt; MAX_SIZE; i++) {\n if (hmap-&gt;buckets[i] != NULL) {\n keys[index] = hmap-&gt;buckets[i]-&gt;key;\n index++;\n }\n }\n set-&gt;set = keys;\n set-&gt;len = total;\n}\n\n/* \u7372\u53d6\u6240\u6709\u503c */\nvoid valueSet(ArrayHashMap *hmap, MapSet *set) {\n char **vals;\n int i = 0, index = 0;\n int total = 0;\n /* \u7d71\u8a08\u6709\u6548\u9375\u503c\u5c0d\u6578\u91cf */\n for (i = 0; i &lt; MAX_SIZE; i++) {\n if (hmap-&gt;buckets[i] != NULL) {\n total++;\n }\n }\n vals = malloc(total * sizeof(char *));\n for (i = 0; i &lt; MAX_SIZE; i++) {\n if (hmap-&gt;buckets[i] != NULL) {\n vals[index] = hmap-&gt;buckets[i]-&gt;val;\n index++;\n }\n }\n set-&gt;set = vals;\n set-&gt;len = total;\n}\n\n/* \u5217\u5370\u96dc\u6e4a\u8868 */\nvoid print(ArrayHashMap *hmap) {\n int i;\n MapSet set;\n pairSet(hmap, &amp;set);\n Pair *entries = (Pair *)set.set;\n for (i = 0; i &lt; set.len; i++) {\n printf(\"%d -&gt; %s\\n\", entries[i].key, entries[i].val);\n }\n free(set.set);\n}\n</code></pre> array_hash_map.kt<pre><code>/* \u9375\u503c\u5c0d */\nclass Pair(\n var key: Int,\n var _val: String\n)\n\n/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u96dc\u6e4a\u8868 */\nclass ArrayHashMap {\n // \u521d\u59cb\u5316\u9663\u5217\uff0c\u5305\u542b 100 \u500b\u6876\n private val buckets = arrayOfNulls&lt;Pair&gt;(100)\n\n /* \u96dc\u6e4a\u51fd\u5f0f */\n fun hashFunc(key: Int): Int {\n val index = key % 100\n return index\n }\n\n /* \u67e5\u8a62\u64cd\u4f5c */\n fun get(key: Int): String? {\n val index = hashFunc(key)\n val pair = buckets[index] ?: return null\n return pair._val\n }\n\n /* \u65b0\u589e\u64cd\u4f5c */\n fun put(key: Int, _val: String) {\n val pair = Pair(key, _val)\n val index = hashFunc(key)\n buckets[index] = pair\n }\n\n /* \u522a\u9664\u64cd\u4f5c */\n fun remove(key: Int) {\n val index = hashFunc(key)\n // \u7f6e\u70ba null \uff0c\u4ee3\u8868\u522a\u9664\n buckets[index] = null\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375\u503c\u5c0d */\n fun pairSet(): MutableList&lt;Pair&gt; {\n val pairSet = mutableListOf&lt;Pair&gt;()\n for (pair in buckets) {\n if (pair != null)\n pairSet.add(pair)\n }\n return pairSet\n }\n\n /* \u7372\u53d6\u6240\u6709\u9375 */\n fun keySet(): MutableList&lt;Int&gt; {\n val keySet = mutableListOf&lt;Int&gt;()\n for (pair in buckets) {\n if (pair != null)\n keySet.add(pair.key)\n }\n return keySet\n }\n\n /* \u7372\u53d6\u6240\u6709\u503c */\n fun valueSet(): MutableList&lt;String&gt; {\n val valueSet = mutableListOf&lt;String&gt;()\n for (pair in buckets) {\n if (pair != null)\n valueSet.add(pair._val)\n }\n return valueSet\n }\n\n /* \u5217\u5370\u96dc\u6e4a\u8868 */\n fun print() {\n for (kv in pairSet()) {\n val key = kv.key\n val _val = kv._val\n println(\"$key -&gt; $_val\")\n }\n }\n}\n</code></pre> array_hash_map.rb<pre><code>### \u9375\u503c\u5c0d ###\nclass Pair\n attr_accessor :key, :val\n\n def initialize(key, val)\n @key = key\n @val = val\n end\nend\n\n### \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u96dc\u6e4a\u8868 ###\nclass ArrayHashMap\n ### \u5efa\u69cb\u5b50 ###\n def initialize\n # \u521d\u59cb\u5316\u9663\u5217\uff0c\u5305\u542b 100 \u500b\u6876\n @buckets = Array.new(100)\n end\n\n ### \u96dc\u6e4a\u51fd\u5f0f ###\n def hash_func(key)\n index = key % 100\n end\n\n ### \u67e5\u8a62\u64cd\u4f5c ###\n def get(key)\n index = hash_func(key)\n pair = @buckets[index]\n\n return if pair.nil?\n pair.val\n end\n\n ### \u65b0\u589e\u64cd\u4f5c ###\n def put(key, val)\n pair = Pair.new(key, val)\n index = hash_func(key)\n @buckets[index] = pair\n end\n\n ### \u522a\u9664\u64cd\u4f5c ###\n def remove(key)\n index = hash_func(key)\n # \u7f6e\u70ba nil \uff0c\u4ee3\u8868\u522a\u9664\n @buckets[index] = nil\n end\n\n ### \u7372\u53d6\u6240\u6709\u9375\u503c\u5c0d ###\n def entry_set\n result = []\n @buckets.each { |pair| result &lt;&lt; pair unless pair.nil? }\n result\n end\n\n ### \u7372\u53d6\u6240\u6709\u9375 ###\n def key_set\n result = []\n @buckets.each { |pair| result &lt;&lt; pair.key unless pair.nil? }\n result\n end\n\n ### \u7372\u53d6\u6240\u6709\u503c ###\n def value_set\n result = []\n @buckets.each { |pair| result &lt;&lt; pair.val unless pair.nil? }\n result\n end\n\n ### \u5217\u5370\u96dc\u6e4a\u8868 ###\n def print\n @buckets.each { |pair| puts \"#{pair.key} -&gt; #{pair.val}\" unless pair.nil? }\n end\nend\n</code></pre> array_hash_map.zig<pre><code>// \u9375\u503c\u5c0d\nconst Pair = struct {\n key: usize = undefined,\n val: []const u8 = undefined,\n\n pub fn init(key: usize, val: []const u8) Pair {\n return Pair {\n .key = key,\n .val = val,\n };\n }\n};\n\n// \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u96dc\u6e4a\u8868\nfn ArrayHashMap(comptime T: type) type {\n return struct {\n bucket: ?std.ArrayList(?T) = null,\n mem_allocator: std.mem.Allocator = undefined,\n\n const Self = @This();\n\n // \u5efa\u69cb\u5b50\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n self.mem_allocator = allocator;\n // \u521d\u59cb\u5316\u4e00\u500b\u9577\u5ea6\u70ba 100 \u7684\u6876\uff08\u9663\u5217\uff09\n self.bucket = std.ArrayList(?T).init(self.mem_allocator);\n var i: i32 = 0;\n while (i &lt; 100) : (i += 1) {\n try self.bucket.?.append(null);\n }\n }\n\n // \u6790\u69cb\u51fd\u5f0f\n pub fn deinit(self: *Self) void {\n if (self.bucket != null) self.bucket.?.deinit();\n }\n\n // \u96dc\u6e4a\u51fd\u5f0f\n fn hashFunc(key: usize) usize {\n var index = key % 100;\n return index;\n }\n\n // \u67e5\u8a62\u64cd\u4f5c\n pub fn get(self: *Self, key: usize) []const u8 {\n var index = hashFunc(key);\n var pair = self.bucket.?.items[index];\n return pair.?.val;\n }\n\n // \u65b0\u589e\u64cd\u4f5c\n pub fn put(self: *Self, key: usize, val: []const u8) !void {\n var pair = Pair.init(key, val);\n var index = hashFunc(key);\n self.bucket.?.items[index] = pair;\n }\n\n // \u522a\u9664\u64cd\u4f5c\n pub fn remove(self: *Self, key: usize) !void {\n var index = hashFunc(key);\n // \u7f6e\u70ba null \uff0c\u4ee3\u8868\u522a\u9664\n self.bucket.?.items[index] = null;\n } \n\n // \u7372\u53d6\u6240\u6709\u9375\u503c\u5c0d\n pub fn pairSet(self: *Self) !std.ArrayList(T) {\n var entry_set = std.ArrayList(T).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try entry_set.append(item.?);\n }\n return entry_set;\n } \n\n // \u7372\u53d6\u6240\u6709\u9375\n pub fn keySet(self: *Self) !std.ArrayList(usize) {\n var key_set = std.ArrayList(usize).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try key_set.append(item.?.key);\n }\n return key_set;\n } \n\n // \u7372\u53d6\u6240\u6709\u503c\n pub fn valueSet(self: *Self) !std.ArrayList([]const u8) {\n var value_set = std.ArrayList([]const u8).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try value_set.append(item.?.val);\n }\n return value_set;\n }\n\n // \u5217\u5370\u96dc\u6e4a\u8868\n pub fn print(self: *Self) !void {\n var entry_set = try self.pairSet();\n defer entry_set.deinit();\n for (entry_set.items) |item| {\n std.debug.print(\"{} -&gt; {s}\\n\", .{item.key, item.val});\n }\n }\n };\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_hashing/hash_map/#613","title":"6.1.3 \u00a0 \u96dc\u6e4a\u885d\u7a81\u8207\u64f4\u5bb9","text":"<p>\u5f9e\u672c\u8cea\u4e0a\u770b\uff0c\u96dc\u6e4a\u51fd\u5f0f\u7684\u4f5c\u7528\u662f\u5c07\u6240\u6709 <code>key</code> \u69cb\u6210\u7684\u8f38\u5165\u7a7a\u9593\u5c0d\u6620\u5230\u9663\u5217\u6240\u6709\u7d22\u5f15\u69cb\u6210\u7684\u8f38\u51fa\u7a7a\u9593\uff0c\u800c\u8f38\u5165\u7a7a\u9593\u5f80\u5f80\u9060\u5927\u65bc\u8f38\u51fa\u7a7a\u9593\u3002\u56e0\u6b64\uff0c\u7406\u8ad6\u4e0a\u4e00\u5b9a\u5b58\u5728\u201c\u591a\u500b\u8f38\u5165\u5c0d\u61c9\u76f8\u540c\u8f38\u51fa\u201d\u7684\u60c5\u6cc1\u3002</p> <p>\u5c0d\u65bc\u4e0a\u8ff0\u793a\u4f8b\u4e2d\u7684\u96dc\u6e4a\u51fd\u5f0f\uff0c\u7576\u8f38\u5165\u7684 <code>key</code> \u5f8c\u5169\u4f4d\u76f8\u540c\u6642\uff0c\u96dc\u6e4a\u51fd\u5f0f\u7684\u8f38\u51fa\u7d50\u679c\u4e5f\u76f8\u540c\u3002\u4f8b\u5982\uff0c\u67e5\u8a62\u5b78\u865f\u70ba 12836 \u548c 20336 \u7684\u5169\u500b\u5b78\u751f\u6642\uff0c\u6211\u5011\u5f97\u5230\uff1a</p> <pre><code>12836 % 100 = 36\n20336 % 100 = 36\n</code></pre> <p>\u5982\u5716 6-3 \u6240\u793a\uff0c\u5169\u500b\u5b78\u865f\u6307\u5411\u4e86\u540c\u4e00\u500b\u59d3\u540d\uff0c\u9019\u986f\u7136\u662f\u4e0d\u5c0d\u7684\u3002\u6211\u5011\u5c07\u9019\u7a2e\u591a\u500b\u8f38\u5165\u5c0d\u61c9\u540c\u4e00\u8f38\u51fa\u7684\u60c5\u6cc1\u7a31\u70ba\u96dc\u6e4a\u885d\u7a81\uff08hash collision\uff09\u3002</p> <p></p> <p> \u5716 6-3 \u00a0 \u96dc\u6e4a\u885d\u7a81\u793a\u4f8b </p> <p>\u5bb9\u6613\u60f3\u5230\uff0c\u96dc\u6e4a\u8868\u5bb9\u91cf \\(n\\) \u8d8a\u5927\uff0c\u591a\u500b <code>key</code> \u88ab\u5206\u914d\u5230\u540c\u4e00\u500b\u6876\u4e2d\u7684\u6a5f\u7387\u5c31\u8d8a\u4f4e\uff0c\u885d\u7a81\u5c31\u8d8a\u5c11\u3002\u56e0\u6b64\uff0c\u6211\u5011\u53ef\u4ee5\u900f\u904e\u64f4\u5bb9\u96dc\u6e4a\u8868\u4f86\u6e1b\u5c11\u96dc\u6e4a\u885d\u7a81\u3002</p> <p>\u5982\u5716 6-4 \u6240\u793a\uff0c\u64f4\u5bb9\u524d\u9375\u503c\u5c0d <code>(136, A)</code> \u548c <code>(236, D)</code> \u767c\u751f\u885d\u7a81\uff0c\u64f4\u5bb9\u5f8c\u885d\u7a81\u6d88\u5931\u3002</p> <p></p> <p> \u5716 6-4 \u00a0 \u96dc\u6e4a\u8868\u64f4\u5bb9 </p> <p>\u985e\u4f3c\u65bc\u9663\u5217\u64f4\u5bb9\uff0c\u96dc\u6e4a\u8868\u64f4\u5bb9\u9700\u5c07\u6240\u6709\u9375\u503c\u5c0d\u5f9e\u539f\u96dc\u6e4a\u8868\u9077\u79fb\u81f3\u65b0\u96dc\u6e4a\u8868\uff0c\u975e\u5e38\u8017\u6642\uff1b\u4e26\u4e14\u7531\u65bc\u96dc\u6e4a\u8868\u5bb9\u91cf <code>capacity</code> \u6539\u8b8a\uff0c\u6211\u5011\u9700\u8981\u900f\u904e\u96dc\u6e4a\u51fd\u5f0f\u4f86\u91cd\u65b0\u8a08\u7b97\u6240\u6709\u9375\u503c\u5c0d\u7684\u5132\u5b58\u4f4d\u7f6e\uff0c\u9019\u9032\u4e00\u6b65\u589e\u52a0\u4e86\u64f4\u5bb9\u904e\u7a0b\u7684\u8a08\u7b97\u958b\u92b7\u3002\u70ba\u6b64\uff0c\u7a0b\u5f0f\u8a9e\u8a00\u901a\u5e38\u6703\u9810\u7559\u8db3\u5920\u5927\u7684\u96dc\u6e4a\u8868\u5bb9\u91cf\uff0c\u9632\u6b62\u983b\u7e41\u64f4\u5bb9\u3002</p> <p>\u8ca0\u8f09\u56e0\u5b50\uff08load factor\uff09\u662f\u96dc\u6e4a\u8868\u7684\u4e00\u500b\u91cd\u8981\u6982\u5ff5\uff0c\u5176\u5b9a\u7fa9\u70ba\u96dc\u6e4a\u8868\u7684\u5143\u7d20\u6578\u91cf\u9664\u4ee5\u6876\u6578\u91cf\uff0c\u7528\u65bc\u8861\u91cf\u96dc\u6e4a\u885d\u7a81\u7684\u56b4\u91cd\u7a0b\u5ea6\uff0c\u4e5f\u5e38\u4f5c\u70ba\u96dc\u6e4a\u8868\u64f4\u5bb9\u7684\u89f8\u767c\u689d\u4ef6\u3002\u4f8b\u5982\u5728 Java \u4e2d\uff0c\u7576\u8ca0\u8f09\u56e0\u5b50\u8d85\u904e \\(0.75\\) \u6642\uff0c\u7cfb\u7d71\u6703\u5c07\u96dc\u6e4a\u8868\u64f4\u5bb9\u81f3\u539f\u5148\u7684 \\(2\\) \u500d\u3002</p>"},{"location":"chapter_hashing/summary/","title":"6.4 \u00a0 \u5c0f\u7d50","text":""},{"location":"chapter_hashing/summary/#1","title":"1. \u00a0 \u91cd\u9ede\u56de\u9867","text":"<ul> <li>\u8f38\u5165 <code>key</code> \uff0c\u96dc\u6e4a\u8868\u80fd\u5920\u5728 \\(O(1)\\) \u6642\u9593\u5167\u67e5\u8a62\u5230 <code>value</code> \uff0c\u6548\u7387\u975e\u5e38\u9ad8\u3002</li> <li>\u5e38\u898b\u7684\u96dc\u6e4a\u8868\u64cd\u4f5c\u5305\u62ec\u67e5\u8a62\u3001\u65b0\u589e\u9375\u503c\u5c0d\u3001\u522a\u9664\u9375\u503c\u5c0d\u548c\u8d70\u8a2a\u96dc\u6e4a\u8868\u7b49\u3002</li> <li>\u96dc\u6e4a\u51fd\u5f0f\u5c07 <code>key</code> \u5c0d\u6620\u70ba\u9663\u5217\u7d22\u5f15\uff0c\u5f9e\u800c\u8a2a\u554f\u5c0d\u61c9\u6876\u4e26\u7372\u53d6 <code>value</code> \u3002</li> <li>\u5169\u500b\u4e0d\u540c\u7684 <code>key</code> \u53ef\u80fd\u5728\u7d93\u904e\u96dc\u6e4a\u51fd\u5f0f\u5f8c\u5f97\u5230\u76f8\u540c\u7684\u9663\u5217\u7d22\u5f15\uff0c\u5c0e\u81f4\u67e5\u8a62\u7d50\u679c\u51fa\u932f\uff0c\u9019\u7a2e\u73fe\u8c61\u88ab\u7a31\u70ba\u96dc\u6e4a\u885d\u7a81\u3002</li> <li>\u96dc\u6e4a\u8868\u5bb9\u91cf\u8d8a\u5927\uff0c\u96dc\u6e4a\u885d\u7a81\u7684\u6a5f\u7387\u5c31\u8d8a\u4f4e\u3002\u56e0\u6b64\u53ef\u4ee5\u900f\u904e\u64f4\u5bb9\u96dc\u6e4a\u8868\u4f86\u7de9\u89e3\u96dc\u6e4a\u885d\u7a81\u3002\u8207\u9663\u5217\u64f4\u5bb9\u985e\u4f3c\uff0c\u96dc\u6e4a\u8868\u64f4\u5bb9\u64cd\u4f5c\u7684\u958b\u92b7\u5f88\u5927\u3002</li> <li>\u8ca0\u8f09\u56e0\u5b50\u5b9a\u7fa9\u70ba\u96dc\u6e4a\u8868\u4e2d\u5143\u7d20\u6578\u91cf\u9664\u4ee5\u6876\u6578\u91cf\uff0c\u53cd\u6620\u4e86\u96dc\u6e4a\u885d\u7a81\u7684\u56b4\u91cd\u7a0b\u5ea6\uff0c\u5e38\u7528\u4f5c\u89f8\u767c\u96dc\u6e4a\u8868\u64f4\u5bb9\u7684\u689d\u4ef6\u3002</li> <li>\u93c8\u5f0f\u4f4d\u5740\u900f\u904e\u5c07\u55ae\u500b\u5143\u7d20\u8f49\u5316\u70ba\u93c8\u7d50\u4e32\u5217\uff0c\u5c07\u6240\u6709\u885d\u7a81\u5143\u7d20\u5132\u5b58\u5728\u540c\u4e00\u500b\u93c8\u7d50\u4e32\u5217\u4e2d\u3002\u7136\u800c\uff0c\u93c8\u7d50\u4e32\u5217\u904e\u9577\u6703\u964d\u4f4e\u67e5\u8a62\u6548\u7387\uff0c\u53ef\u4ee5\u900f\u904e\u9032\u4e00\u6b65\u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u63db\u70ba\u7d05\u9ed1\u6a39\u4f86\u63d0\u9ad8\u6548\u7387\u3002</li> <li>\u958b\u653e\u5b9a\u5740\u900f\u904e\u591a\u6b21\u63a2\u6e2c\u4f86\u8655\u7406\u96dc\u6e4a\u885d\u7a81\u3002\u7dda\u6027\u63a2\u67e5\u4f7f\u7528\u56fa\u5b9a\u6b65\u9577\uff0c\u7f3a\u9ede\u662f\u4e0d\u80fd\u522a\u9664\u5143\u7d20\uff0c\u4e14\u5bb9\u6613\u7522\u751f\u805a\u96c6\u3002\u591a\u6b21\u96dc\u6e4a\u4f7f\u7528\u591a\u500b\u96dc\u6e4a\u51fd\u5f0f\u9032\u884c\u63a2\u6e2c\uff0c\u76f8\u8f03\u7dda\u6027\u63a2\u67e5\u66f4\u4e0d\u6613\u7522\u751f\u805a\u96c6\uff0c\u4f46\u591a\u500b\u96dc\u6e4a\u51fd\u5f0f\u589e\u52a0\u4e86\u8a08\u7b97\u91cf\u3002</li> <li>\u4e0d\u540c\u7a0b\u5f0f\u8a9e\u8a00\u63a1\u53d6\u4e86\u4e0d\u540c\u7684\u96dc\u6e4a\u8868\u5be6\u73fe\u3002\u4f8b\u5982\uff0cJava \u7684 <code>HashMap</code> \u4f7f\u7528\u93c8\u5f0f\u4f4d\u5740\uff0c\u800c Python \u7684 <code>Dict</code> \u63a1\u7528\u958b\u653e\u5b9a\u5740\u3002</li> <li>\u5728\u96dc\u6e4a\u8868\u4e2d\uff0c\u6211\u5011\u5e0c\u671b\u96dc\u6e4a\u6f14\u7b97\u6cd5\u5177\u6709\u78ba\u5b9a\u6027\u3001\u9ad8\u6548\u7387\u548c\u5747\u52fb\u5206\u4f48\u7684\u7279\u9ede\u3002\u5728\u5bc6\u78bc\u5b78\u4e2d\uff0c\u96dc\u6e4a\u6f14\u7b97\u6cd5\u9084\u61c9\u8a72\u5177\u5099\u6297\u78b0\u649e\u6027\u548c\u96ea\u5d29\u6548\u61c9\u3002</li> <li>\u96dc\u6e4a\u6f14\u7b97\u6cd5\u901a\u5e38\u63a1\u7528\u5927\u8cea\u6578\u4f5c\u70ba\u6a21\u6578\uff0c\u4ee5\u6700\u5927\u5316\u5730\u4fdd\u8b49\u96dc\u6e4a\u503c\u5747\u52fb\u5206\u4f48\uff0c\u6e1b\u5c11\u96dc\u6e4a\u885d\u7a81\u3002</li> <li>\u5e38\u898b\u7684\u96dc\u6e4a\u6f14\u7b97\u6cd5\u5305\u62ec MD5\u3001SHA-1\u3001SHA-2 \u548c SHA-3 \u7b49\u3002MD5 \u5e38\u7528\u65bc\u6821\u9a57\u6a94\u6848\u5b8c\u6574\u6027\uff0cSHA-2 \u5e38\u7528\u65bc\u5b89\u5168\u61c9\u7528\u8207\u5354\u8b70\u3002</li> <li>\u7a0b\u5f0f\u8a9e\u8a00\u901a\u5e38\u6703\u70ba\u8cc7\u6599\u578b\u5225\u63d0\u4f9b\u5167\u5efa\u96dc\u6e4a\u6f14\u7b97\u6cd5\uff0c\u7528\u65bc\u8a08\u7b97\u96dc\u6e4a\u8868\u4e2d\u7684\u6876\u7d22\u5f15\u3002\u901a\u5e38\u60c5\u6cc1\u4e0b\uff0c\u53ea\u6709\u4e0d\u53ef\u8b8a\u7269\u4ef6\u662f\u53ef\u96dc\u6e4a\u7684\u3002</li> </ul>"},{"location":"chapter_hashing/summary/#2-q-a","title":"2. \u00a0 Q &amp; A","text":"<p>Q\uff1a\u96dc\u6e4a\u8868\u7684\u6642\u9593\u8907\u96dc\u5ea6\u5728\u4ec0\u9ebc\u60c5\u6cc1\u4e0b\u662f \\(O(n)\\) \uff1f</p> <p>\u7576\u96dc\u6e4a\u885d\u7a81\u6bd4\u8f03\u56b4\u91cd\u6642\uff0c\u96dc\u6e4a\u8868\u7684\u6642\u9593\u8907\u96dc\u5ea6\u6703\u9000\u5316\u81f3 \\(O(n)\\) \u3002\u7576\u96dc\u6e4a\u51fd\u5f0f\u8a2d\u8a08\u5f97\u6bd4\u8f03\u597d\u3001\u5bb9\u91cf\u8a2d\u5b9a\u6bd4\u8f03\u5408\u7406\u3001\u885d\u7a81\u6bd4\u8f03\u5e73\u5747\u6642\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(O(1)\\) \u3002\u6211\u5011\u4f7f\u7528\u7a0b\u5f0f\u8a9e\u8a00\u5167\u5efa\u7684\u96dc\u6e4a\u8868\u6642\uff0c\u901a\u5e38\u8a8d\u70ba\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(O(1)\\) \u3002</p> <p>Q\uff1a\u70ba\u4ec0\u9ebc\u4e0d\u4f7f\u7528\u96dc\u6e4a\u51fd\u5f0f \\(f(x) = x\\) \u5462\uff1f\u9019\u6a23\u5c31\u4e0d\u6703\u6709\u885d\u7a81\u4e86\u3002</p> <p>\u5728 \\(f(x) = x\\) \u96dc\u6e4a\u51fd\u5f0f\u4e0b\uff0c\u6bcf\u500b\u5143\u7d20\u5c0d\u61c9\u552f\u4e00\u7684\u6876\u7d22\u5f15\uff0c\u9019\u8207\u9663\u5217\u7b49\u50f9\u3002\u7136\u800c\uff0c\u8f38\u5165\u7a7a\u9593\u901a\u5e38\u9060\u5927\u65bc\u8f38\u51fa\u7a7a\u9593\uff08\u9663\u5217\u9577\u5ea6\uff09\uff0c\u56e0\u6b64\u96dc\u6e4a\u51fd\u5f0f\u7684\u6700\u5f8c\u4e00\u6b65\u5f80\u5f80\u662f\u5c0d\u9663\u5217\u9577\u5ea6\u53d6\u6a21\u3002\u63db\u53e5\u8a71\u8aaa\uff0c\u96dc\u6e4a\u8868\u7684\u76ee\u6a19\u662f\u5c07\u4e00\u500b\u8f03\u5927\u7684\u72c0\u614b\u7a7a\u9593\u5c0d\u6620\u5230\u4e00\u500b\u8f03\u5c0f\u7684\u7a7a\u9593\uff0c\u4e26\u63d0\u4f9b \\(O(1)\\) \u7684\u67e5\u8a62\u6548\u7387\u3002</p> <p>Q\uff1a\u96dc\u6e4a\u8868\u5e95\u5c64\u5be6\u73fe\u662f\u9663\u5217\u3001\u93c8\u7d50\u4e32\u5217\u3001\u4e8c\u5143\u6a39\uff0c\u4f46\u70ba\u4ec0\u9ebc\u6548\u7387\u53ef\u4ee5\u6bd4\u5b83\u5011\u66f4\u9ad8\u5462\uff1f</p> <p>\u9996\u5148\uff0c\u96dc\u6e4a\u8868\u7684\u6642\u9593\u6548\u7387\u8b8a\u9ad8\uff0c\u4f46\u7a7a\u9593\u6548\u7387\u8b8a\u4f4e\u4e86\u3002\u96dc\u6e4a\u8868\u6709\u76f8\u7576\u4e00\u90e8\u5206\u8a18\u61b6\u9ad4\u672a\u4f7f\u7528\u3002</p> <p>\u5176\u6b21\uff0c\u53ea\u662f\u5728\u7279\u5b9a\u4f7f\u7528\u5834\u666f\u4e0b\u6642\u9593\u6548\u7387\u8b8a\u9ad8\u4e86\u3002\u5982\u679c\u4e00\u500b\u529f\u80fd\u80fd\u5920\u5728\u76f8\u540c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u4e0b\u4f7f\u7528\u9663\u5217\u6216\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\uff0c\u90a3\u9ebc\u901a\u5e38\u6bd4\u96dc\u6e4a\u8868\u66f4\u5feb\u3002\u9019\u662f\u56e0\u70ba\u96dc\u6e4a\u51fd\u5f0f\u8a08\u7b97\u9700\u8981\u958b\u92b7\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u7684\u5e38\u6578\u9805\u66f4\u5927\u3002</p> <p>\u6700\u5f8c\uff0c\u96dc\u6e4a\u8868\u7684\u6642\u9593\u8907\u96dc\u5ea6\u53ef\u80fd\u767c\u751f\u52a3\u5316\u3002\u4f8b\u5982\u5728\u93c8\u5f0f\u4f4d\u5740\u4e2d\uff0c\u6211\u5011\u63a1\u53d6\u5728\u93c8\u7d50\u4e32\u5217\u6216\u7d05\u9ed1\u6a39\u4e2d\u57f7\u884c\u67e5\u8a62\u64cd\u4f5c\uff0c\u4ecd\u7136\u6709\u9000\u5316\u81f3 \\(O(n)\\) \u6642\u9593\u7684\u98a8\u96aa\u3002</p> <p>Q\uff1a\u591a\u6b21\u96dc\u6e4a\u6709\u4e0d\u80fd\u76f4\u63a5\u522a\u9664\u5143\u7d20\u7684\u7f3a\u9677\u55ce\uff1f\u6a19\u8a18\u70ba\u5df2\u522a\u9664\u7684\u7a7a\u9593\u9084\u80fd\u518d\u6b21\u4f7f\u7528\u55ce\uff1f</p> <p>\u591a\u6b21\u96dc\u6e4a\u662f\u958b\u653e\u5b9a\u5740\u7684\u4e00\u7a2e\uff0c\u958b\u653e\u5b9a\u5740\u6cd5\u90fd\u6709\u4e0d\u80fd\u76f4\u63a5\u522a\u9664\u5143\u7d20\u7684\u7f3a\u9677\uff0c\u9700\u8981\u900f\u904e\u6a19\u8a18\u522a\u9664\u3002\u6a19\u8a18\u70ba\u5df2\u522a\u9664\u7684\u7a7a\u9593\u53ef\u4ee5\u518d\u6b21\u4f7f\u7528\u3002\u7576\u5c07\u65b0\u5143\u7d20\u63d2\u5165\u96dc\u6e4a\u8868\uff0c\u4e26\u4e14\u900f\u904e\u96dc\u6e4a\u51fd\u5f0f\u627e\u5230\u6a19\u8a18\u70ba\u5df2\u522a\u9664\u7684\u4f4d\u7f6e\u6642\uff0c\u8a72\u4f4d\u7f6e\u53ef\u4ee5\u88ab\u65b0\u5143\u7d20\u4f7f\u7528\u3002\u9019\u6a23\u505a\u65e2\u80fd\u4fdd\u6301\u96dc\u6e4a\u8868\u7684\u63a2\u6e2c\u5e8f\u5217\u4e0d\u8b8a\uff0c\u53c8\u80fd\u4fdd\u8b49\u96dc\u6e4a\u8868\u7684\u7a7a\u9593\u4f7f\u7528\u7387\u3002</p> <p>Q\uff1a\u70ba\u4ec0\u9ebc\u7dda\u4e0a\u6027\u63a2\u67e5\u4e2d\uff0c\u67e5\u8a62\u5143\u7d20\u7684\u6642\u5019\u6703\u51fa\u73fe\u96dc\u6e4a\u885d\u7a81\u5462\uff1f</p> <p>\u67e5\u8a62\u7684\u6642\u5019\u900f\u904e\u96dc\u6e4a\u51fd\u5f0f\u627e\u5230\u5c0d\u61c9\u7684\u6876\u548c\u9375\u503c\u5c0d\uff0c\u767c\u73fe <code>key</code> \u4e0d\u5339\u914d\uff0c\u9019\u5c31\u4ee3\u8868\u6709\u96dc\u6e4a\u885d\u7a81\u3002\u56e0\u6b64\uff0c\u7dda\u6027\u63a2\u67e5\u6cd5\u6703\u6839\u64da\u9810\u5148\u8a2d\u5b9a\u7684\u6b65\u9577\u4f9d\u6b21\u5411\u4e0b\u67e5\u8a62\uff0c\u76f4\u81f3\u627e\u5230\u6b63\u78ba\u7684\u9375\u503c\u5c0d\u6216\u7121\u6cd5\u627e\u5230\u8df3\u51fa\u70ba\u6b62\u3002</p> <p>Q\uff1a\u70ba\u4ec0\u9ebc\u96dc\u6e4a\u8868\u64f4\u5bb9\u80fd\u5920\u7de9\u89e3\u96dc\u6e4a\u885d\u7a81\uff1f</p> <p>\u96dc\u6e4a\u51fd\u5f0f\u7684\u6700\u5f8c\u4e00\u6b65\u5f80\u5f80\u662f\u5c0d\u9663\u5217\u9577\u5ea6 \\(n\\) \u53d6\u6a21\uff08\u53d6\u9918\uff09\uff0c\u8b93\u8f38\u51fa\u503c\u843d\u5728\u9663\u5217\u7d22\u5f15\u7bc4\u570d\u5167\uff1b\u5728\u64f4\u5bb9\u5f8c\uff0c\u9663\u5217\u9577\u5ea6 \\(n\\) \u767c\u751f\u8b8a\u5316\uff0c\u800c <code>key</code> \u5c0d\u61c9\u7684\u7d22\u5f15\u4e5f\u53ef\u80fd\u767c\u751f\u8b8a\u5316\u3002\u539f\u5148\u843d\u5728\u540c\u4e00\u500b\u6876\u7684\u591a\u500b <code>key</code> \uff0c\u5728\u64f4\u5bb9\u5f8c\u53ef\u80fd\u6703\u88ab\u5206\u914d\u5230\u591a\u500b\u6876\u4e2d\uff0c\u5f9e\u800c\u5be6\u73fe\u96dc\u6e4a\u885d\u7a81\u7684\u7de9\u89e3\u3002</p>"},{"location":"chapter_heap/","title":"\u7b2c 8 \u7ae0 \u00a0 \u5806\u7a4d","text":"<p>Abstract</p> <p>\u5806\u7a4d\u5c31\u50cf\u662f\u5c71\u5dbd\u5cf0\u5dd2\uff0c\u5c64\u758a\u8d77\u4f0f\u3001\u5f62\u614b\u5404\u7570\u3002</p> <p>\u5ea7\u5ea7\u5c71\u5cf0\u9ad8\u4f4e\u932f\u843d\uff0c\u800c\u6700\u9ad8\u7684\u5c71\u5cf0\u7e3d\u662f\u6700\u5148\u6620\u5165\u773c\u7c3e\u3002</p>"},{"location":"chapter_heap/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>8.1 \u00a0 \u5806\u7a4d</li> <li>8.2 \u00a0 \u5efa\u5806\u7a4d\u64cd\u4f5c</li> <li>8.3 \u00a0 Top-k \u554f\u984c</li> <li>8.4 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_heap/build_heap/","title":"8.2 \u00a0 \u5efa\u5806\u7a4d\u64cd\u4f5c","text":"<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u6211\u5011\u5e0c\u671b\u4f7f\u7528\u4e00\u500b\u4e32\u5217\u7684\u6240\u6709\u5143\u7d20\u4f86\u69cb\u5efa\u4e00\u500b\u5806\u7a4d\uff0c\u9019\u500b\u904e\u7a0b\u88ab\u7a31\u70ba\u201c\u5efa\u5806\u7a4d\u64cd\u4f5c\u201d\u3002</p>"},{"location":"chapter_heap/build_heap/#821","title":"8.2.1 \u00a0 \u85c9\u52a9\u5165\u5806\u7a4d\u64cd\u4f5c\u5be6\u73fe","text":"<p>\u6211\u5011\u9996\u5148\u5efa\u7acb\u4e00\u500b\u7a7a\u5806\u7a4d\uff0c\u7136\u5f8c\u8d70\u8a2a\u4e32\u5217\uff0c\u4f9d\u6b21\u5c0d\u6bcf\u500b\u5143\u7d20\u57f7\u884c\u201c\u5165\u5806\u7a4d\u64cd\u4f5c\u201d\uff0c\u5373\u5148\u5c07\u5143\u7d20\u65b0\u589e\u81f3\u5806\u7a4d\u7684\u5c3e\u90e8\uff0c\u518d\u5c0d\u8a72\u5143\u7d20\u57f7\u884c\u201c\u5f9e\u5e95\u81f3\u9802\u201d\u5806\u7a4d\u5316\u3002</p> <p>\u6bcf\u7576\u4e00\u500b\u5143\u7d20\u5165\u5806\u7a4d\uff0c\u5806\u7a4d\u7684\u9577\u5ea6\u5c31\u52a0\u4e00\u3002\u7531\u65bc\u7bc0\u9ede\u662f\u5f9e\u9802\u5230\u5e95\u4f9d\u6b21\u88ab\u65b0\u589e\u9032\u4e8c\u5143\u6a39\u7684\uff0c\u56e0\u6b64\u5806\u7a4d\u662f\u201c\u81ea\u4e0a\u800c\u4e0b\u201d\u69cb\u5efa\u7684\u3002</p> <p>\u8a2d\u5143\u7d20\u6578\u91cf\u70ba \\(n\\) \uff0c\u6bcf\u500b\u5143\u7d20\u7684\u5165\u5806\u7a4d\u64cd\u4f5c\u4f7f\u7528 \\(O(\\log{n})\\) \u6642\u9593\uff0c\u56e0\u6b64\u8a72\u5efa\u5806\u7a4d\u65b9\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n \\log n)\\) \u3002</p>"},{"location":"chapter_heap/build_heap/#822","title":"8.2.2 \u00a0 \u900f\u904e\u8d70\u8a2a\u5806\u7a4d\u5316\u5be6\u73fe","text":"<p>\u5be6\u969b\u4e0a\uff0c\u6211\u5011\u53ef\u4ee5\u5be6\u73fe\u4e00\u7a2e\u66f4\u70ba\u9ad8\u6548\u7684\u5efa\u5806\u7a4d\u65b9\u6cd5\uff0c\u5171\u5206\u70ba\u5169\u6b65\u3002</p> <ol> <li>\u5c07\u4e32\u5217\u6240\u6709\u5143\u7d20\u539f\u5c01\u4e0d\u52d5\u5730\u65b0\u589e\u5230\u5806\u7a4d\u4e2d\uff0c\u6b64\u6642\u5806\u7a4d\u7684\u6027\u8cea\u5c1a\u672a\u5f97\u5230\u6eff\u8db3\u3002</li> <li>\u5012\u5e8f\u8d70\u8a2a\u5806\u7a4d\uff08\u5c64\u5e8f\u8d70\u8a2a\u7684\u5012\u5e8f\uff09\uff0c\u4f9d\u6b21\u5c0d\u6bcf\u500b\u975e\u8449\u7bc0\u9ede\u57f7\u884c\u201c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\u201d\u3002</li> </ol> <p>\u6bcf\u7576\u5806\u7a4d\u5316\u4e00\u500b\u7bc0\u9ede\u5f8c\uff0c\u4ee5\u8a72\u7bc0\u9ede\u70ba\u6839\u7bc0\u9ede\u7684\u5b50\u6a39\u5c31\u5f62\u6210\u4e00\u500b\u5408\u6cd5\u7684\u5b50\u5806\u7a4d\u3002\u800c\u7531\u65bc\u662f\u5012\u5e8f\u8d70\u8a2a\uff0c\u56e0\u6b64\u5806\u7a4d\u662f\u201c\u81ea\u4e0b\u800c\u4e0a\u201d\u69cb\u5efa\u7684\u3002</p> <p>\u4e4b\u6240\u4ee5\u9078\u64c7\u5012\u5e8f\u8d70\u8a2a\uff0c\u662f\u56e0\u70ba\u9019\u6a23\u80fd\u5920\u4fdd\u8b49\u7576\u524d\u7bc0\u9ede\u4e4b\u4e0b\u7684\u5b50\u6a39\u5df2\u7d93\u662f\u5408\u6cd5\u7684\u5b50\u5806\u7a4d\uff0c\u9019\u6a23\u5806\u7a4d\u5316\u7576\u524d\u7bc0\u9ede\u624d\u662f\u6709\u6548\u7684\u3002</p> <p>\u503c\u5f97\u8aaa\u660e\u7684\u662f\uff0c\u7531\u65bc\u8449\u7bc0\u9ede\u6c92\u6709\u5b50\u7bc0\u9ede\uff0c\u56e0\u6b64\u5b83\u5011\u5929\u7136\u5c31\u662f\u5408\u6cd5\u7684\u5b50\u5806\u7a4d\uff0c\u7121\u9808\u5806\u7a4d\u5316\u3002\u5982\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u6240\u793a\uff0c\u6700\u5f8c\u4e00\u500b\u975e\u8449\u7bc0\u9ede\u662f\u6700\u5f8c\u4e00\u500b\u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\uff0c\u6211\u5011\u5f9e\u5b83\u958b\u59cb\u5012\u5e8f\u8d70\u8a2a\u4e26\u57f7\u884c\u5806\u7a4d\u5316\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.py<pre><code>def __init__(self, nums: list[int]):\n \"\"\"\u5efa\u69cb\u5b50\uff0c\u6839\u64da\u8f38\u5165\u4e32\u5217\u5efa\u5806\u7a4d\"\"\"\n # \u5c07\u4e32\u5217\u5143\u7d20\u539f\u5c01\u4e0d\u52d5\u65b0\u589e\u9032\u5806\u7a4d\n self.max_heap = nums\n # \u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for i in range(self.parent(self.size() - 1), -1, -1):\n self.sift_down(i)\n</code></pre> my_heap.cpp<pre><code>/* \u5efa\u69cb\u5b50\uff0c\u6839\u64da\u8f38\u5165\u4e32\u5217\u5efa\u5806\u7a4d */\nMaxHeap(vector&lt;int&gt; nums) {\n // \u5c07\u4e32\u5217\u5143\u7d20\u539f\u5c01\u4e0d\u52d5\u65b0\u589e\u9032\u5806\u7a4d\n maxHeap = nums;\n // \u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for (int i = parent(size() - 1); i &gt;= 0; i--) {\n siftDown(i);\n }\n}\n</code></pre> my_heap.java<pre><code>/* \u5efa\u69cb\u5b50\uff0c\u6839\u64da\u8f38\u5165\u4e32\u5217\u5efa\u5806\u7a4d */\nMaxHeap(List&lt;Integer&gt; nums) {\n // \u5c07\u4e32\u5217\u5143\u7d20\u539f\u5c01\u4e0d\u52d5\u65b0\u589e\u9032\u5806\u7a4d\n maxHeap = new ArrayList&lt;&gt;(nums);\n // \u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for (int i = parent(size() - 1); i &gt;= 0; i--) {\n siftDown(i);\n }\n}\n</code></pre> my_heap.cs<pre><code>/* \u5efa\u69cb\u5b50\uff0c\u6839\u64da\u8f38\u5165\u4e32\u5217\u5efa\u5806\u7a4d */\nMaxHeap(IEnumerable&lt;int&gt; nums) {\n // \u5c07\u4e32\u5217\u5143\u7d20\u539f\u5c01\u4e0d\u52d5\u65b0\u589e\u9032\u5806\u7a4d\n maxHeap = new List&lt;int&gt;(nums);\n // \u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n var size = Parent(this.Size() - 1);\n for (int i = size; i &gt;= 0; i--) {\n SiftDown(i);\n }\n}\n</code></pre> my_heap.go<pre><code>/* \u5efa\u69cb\u5b50\uff0c\u6839\u64da\u5207\u7247\u5efa\u5806\u7a4d */\nfunc newMaxHeap(nums []any) *maxHeap {\n // \u5c07\u4e32\u5217\u5143\u7d20\u539f\u5c01\u4e0d\u52d5\u65b0\u589e\u9032\u5806\u7a4d\n h := &amp;maxHeap{data: nums}\n for i := h.parent(len(h.data) - 1); i &gt;= 0; i-- {\n // \u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n h.siftDown(i)\n }\n return h\n}\n</code></pre> my_heap.swift<pre><code>/* \u5efa\u69cb\u5b50\uff0c\u6839\u64da\u8f38\u5165\u4e32\u5217\u5efa\u5806\u7a4d */\ninit(nums: [Int]) {\n // \u5c07\u4e32\u5217\u5143\u7d20\u539f\u5c01\u4e0d\u52d5\u65b0\u589e\u9032\u5806\u7a4d\n maxHeap = nums\n // \u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for i in (0 ... parent(i: size() - 1)).reversed() {\n siftDown(i: i)\n }\n}\n</code></pre> my_heap.js<pre><code>/* \u5efa\u69cb\u5b50\uff0c\u5efa\u7acb\u7a7a\u5806\u7a4d\u6216\u6839\u64da\u8f38\u5165\u4e32\u5217\u5efa\u5806\u7a4d */\nconstructor(nums) {\n // \u5c07\u4e32\u5217\u5143\u7d20\u539f\u5c01\u4e0d\u52d5\u65b0\u589e\u9032\u5806\u7a4d\n this.#maxHeap = nums === undefined ? [] : [...nums];\n // \u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for (let i = this.#parent(this.size() - 1); i &gt;= 0; i--) {\n this.#siftDown(i);\n }\n}\n</code></pre> my_heap.ts<pre><code>/* \u5efa\u69cb\u5b50\uff0c\u5efa\u7acb\u7a7a\u5806\u7a4d\u6216\u6839\u64da\u8f38\u5165\u4e32\u5217\u5efa\u5806\u7a4d */\nconstructor(nums?: number[]) {\n // \u5c07\u4e32\u5217\u5143\u7d20\u539f\u5c01\u4e0d\u52d5\u65b0\u589e\u9032\u5806\u7a4d\n this.maxHeap = nums === undefined ? [] : [...nums];\n // \u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for (let i = this.parent(this.size() - 1); i &gt;= 0; i--) {\n this.siftDown(i);\n }\n}\n</code></pre> my_heap.dart<pre><code>/* \u5efa\u69cb\u5b50\uff0c\u6839\u64da\u8f38\u5165\u4e32\u5217\u5efa\u5806\u7a4d */\nMaxHeap(List&lt;int&gt; nums) {\n // \u5c07\u4e32\u5217\u5143\u7d20\u539f\u5c01\u4e0d\u52d5\u65b0\u589e\u9032\u5806\u7a4d\n _maxHeap = nums;\n // \u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for (int i = _parent(size() - 1); i &gt;= 0; i--) {\n siftDown(i);\n }\n}\n</code></pre> my_heap.rs<pre><code>/* \u5efa\u69cb\u5b50\uff0c\u6839\u64da\u8f38\u5165\u4e32\u5217\u5efa\u5806\u7a4d */\nfn new(nums: Vec&lt;i32&gt;) -&gt; Self {\n // \u5c07\u4e32\u5217\u5143\u7d20\u539f\u5c01\u4e0d\u52d5\u65b0\u589e\u9032\u5806\u7a4d\n let mut heap = MaxHeap { max_heap: nums };\n // \u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for i in (0..=Self::parent(heap.size() - 1)).rev() {\n heap.sift_down(i);\n }\n heap\n}\n</code></pre> my_heap.c<pre><code>/* \u5efa\u69cb\u5b50\uff0c\u6839\u64da\u5207\u7247\u5efa\u5806\u7a4d */\nMaxHeap *newMaxHeap(int nums[], int size) {\n // \u6240\u6709\u5143\u7d20\u5165\u5806\u7a4d\n MaxHeap *maxHeap = (MaxHeap *)malloc(sizeof(MaxHeap));\n maxHeap-&gt;size = size;\n memcpy(maxHeap-&gt;data, nums, size * sizeof(int));\n for (int i = parent(maxHeap, size - 1); i &gt;= 0; i--) {\n // \u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n siftDown(maxHeap, i);\n }\n return maxHeap;\n}\n</code></pre> my_heap.kt<pre><code>/* \u5927\u9802\u5806\u7a4d */\nclass MaxHeap(nums: MutableList&lt;Int&gt;?) {\n // \u4f7f\u7528\u4e32\u5217\u800c\u975e\u9663\u5217\uff0c\u9019\u6a23\u7121\u9808\u8003\u616e\u64f4\u5bb9\u554f\u984c\n private val maxHeap = mutableListOf&lt;Int&gt;()\n\n /* \u5efa\u69cb\u5b50\uff0c\u6839\u64da\u8f38\u5165\u4e32\u5217\u5efa\u5806\u7a4d */\n init {\n // \u5c07\u4e32\u5217\u5143\u7d20\u539f\u5c01\u4e0d\u52d5\u65b0\u589e\u9032\u5806\u7a4d\n maxHeap.addAll(nums!!)\n // \u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for (i in parent(size() - 1) downTo 0) {\n siftDown(i)\n }\n }\n\n /* \u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n private fun left(i: Int): Int {\n return 2 * i + 1\n }\n\n /* \u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n private fun right(i: Int): Int {\n return 2 * i + 2\n }\n\n /* \u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\n private fun parent(i: Int): Int {\n return (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n }\n\n /* \u4ea4\u63db\u5143\u7d20 */\n private fun swap(i: Int, j: Int) {\n val temp = maxHeap[i]\n maxHeap[i] = maxHeap[j]\n maxHeap[j] = temp\n }\n\n /* \u7372\u53d6\u5806\u7a4d\u5927\u5c0f */\n fun size(): Int {\n return maxHeap.size\n }\n\n /* \u5224\u65b7\u5806\u7a4d\u662f\u5426\u70ba\u7a7a */\n fun isEmpty(): Boolean {\n /* \u5224\u65b7\u5806\u7a4d\u662f\u5426\u70ba\u7a7a */\n return size() == 0\n }\n\n /* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\n fun peek(): Int {\n return maxHeap[0]\n }\n\n /* \u5143\u7d20\u5165\u5806\u7a4d */\n fun push(_val: Int) {\n // \u65b0\u589e\u7bc0\u9ede\n maxHeap.add(_val)\n // \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n siftUp(size() - 1)\n }\n\n /* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316 */\n private fun siftUp(it: Int) {\n // Kotlin\u7684\u51fd\u5f0f\u53c3\u6578\u4e0d\u53ef\u8b8a\uff0c\u56e0\u6b64\u5efa\u7acb\u81e8\u6642\u8b8a\u6578\n var i = it\n while (true) {\n // \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n val p = parent(i)\n // \u7576\u201c\u8d8a\u904e\u6839\u7bc0\u9ede\u201d\u6216\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\u6642\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n if (p &lt; 0 || maxHeap[i] &lt;= maxHeap[p]) break\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n swap(i, p)\n // \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p\n }\n }\n\n /* \u5143\u7d20\u51fa\u5806\u7a4d */\n fun pop(): Int {\n // \u5224\u7a7a\u8655\u7406\n if (isEmpty()) throw IndexOutOfBoundsException()\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n swap(0, size() - 1)\n // \u522a\u9664\u7bc0\u9ede\n val _val = maxHeap.removeAt(size() - 1)\n // \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n siftDown(0)\n // \u8fd4\u56de\u5806\u7a4d\u9802\u5143\u7d20\n return _val\n }\n\n /* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\n private fun siftDown(it: Int) {\n // Kotlin\u7684\u51fd\u5f0f\u53c3\u6578\u4e0d\u53ef\u8b8a\uff0c\u56e0\u6b64\u5efa\u7acb\u81e8\u6642\u8b8a\u6578\n var i = it\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n val l = left(i)\n val r = right(i)\n var ma = i\n if (l &lt; size() &amp;&amp; maxHeap[l] &gt; maxHeap[ma]) ma = l\n if (r &lt; size() &amp;&amp; maxHeap[r] &gt; maxHeap[ma]) ma = r\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma == i) break\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n swap(i, ma)\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma\n }\n }\n\n /* \u5217\u5370\u5806\u7a4d\uff08\u4e8c\u5143\u6a39\uff09 */\n fun print() {\n val queue = PriorityQueue { a: Int, b: Int -&gt; b - a }\n queue.addAll(maxHeap)\n printHeap(queue)\n }\n}\n</code></pre> my_heap.rb<pre><code>[class]{MaxHeap}-[func]{__init__}\n</code></pre> my_heap.zig<pre><code>// \u5efa\u69cb\u5b50\uff0c\u6839\u64da\u8f38\u5165\u4e32\u5217\u5efa\u5806\u7a4d\nfn init(self: *Self, allocator: std.mem.Allocator, nums: []const T) !void {\n if (self.max_heap != null) return;\n self.max_heap = std.ArrayList(T).init(allocator);\n // \u5c07\u4e32\u5217\u5143\u7d20\u539f\u5c01\u4e0d\u52d5\u65b0\u589e\u9032\u5806\u7a4d\n try self.max_heap.?.appendSlice(nums);\n // \u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n var i: usize = parent(self.size() - 1) + 1;\n while (i &gt; 0) : (i -= 1) {\n try self.siftDown(i - 1);\n }\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_heap/build_heap/#823","title":"8.2.3 \u00a0 \u8907\u96dc\u5ea6\u5206\u6790","text":"<p>\u4e0b\u9762\uff0c\u6211\u5011\u4f86\u5617\u8a66\u63a8\u7b97\u7b2c\u4e8c\u7a2e\u5efa\u5806\u7a4d\u65b9\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u3002</p> <ul> <li>\u5047\u8a2d\u5b8c\u5168\u4e8c\u5143\u6a39\u7684\u7bc0\u9ede\u6578\u91cf\u70ba \\(n\\) \uff0c\u5247\u8449\u7bc0\u9ede\u6578\u91cf\u70ba \\((n + 1) / 2\\) \uff0c\u5176\u4e2d \\(/\\) \u70ba\u5411\u4e0b\u6574\u9664\u3002\u56e0\u6b64\u9700\u8981\u5806\u7a4d\u5316\u7684\u7bc0\u9ede\u6578\u91cf\u70ba \\((n - 1) / 2\\) \u3002</li> <li>\u5728\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\u7684\u904e\u7a0b\u4e2d\uff0c\u6bcf\u500b\u7bc0\u9ede\u6700\u591a\u5806\u7a4d\u5316\u5230\u8449\u7bc0\u9ede\uff0c\u56e0\u6b64\u6700\u5927\u8fed\u4ee3\u6b21\u6578\u70ba\u4e8c\u5143\u6a39\u9ad8\u5ea6 \\(\\log n\\) \u3002</li> </ul> <p>\u5c07\u4e0a\u8ff0\u5169\u8005\u76f8\u4e58\uff0c\u53ef\u5f97\u5230\u5efa\u5806\u7a4d\u904e\u7a0b\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n \\log n)\\) \u3002\u4f46\u9019\u500b\u4f30\u7b97\u7d50\u679c\u4e26\u4e0d\u6e96\u78ba\uff0c\u56e0\u70ba\u6211\u5011\u6c92\u6709\u8003\u616e\u5230\u4e8c\u5143\u6a39\u5e95\u5c64\u7bc0\u9ede\u6578\u91cf\u9060\u591a\u65bc\u9802\u5c64\u7bc0\u9ede\u7684\u6027\u8cea\u3002</p> <p>\u63a5\u4e0b\u4f86\u6211\u5011\u4f86\u9032\u884c\u66f4\u70ba\u6e96\u78ba\u7684\u8a08\u7b97\u3002\u70ba\u4e86\u964d\u4f4e\u8a08\u7b97\u96e3\u5ea6\uff0c\u5047\u8a2d\u7d66\u5b9a\u4e00\u500b\u7bc0\u9ede\u6578\u91cf\u70ba \\(n\\) \u3001\u9ad8\u5ea6\u70ba \\(h\\) \u7684\u201c\u5b8c\u7f8e\u4e8c\u5143\u6a39\u201d\uff0c\u8a72\u5047\u8a2d\u4e0d\u6703\u5f71\u97ff\u8a08\u7b97\u7d50\u679c\u7684\u6b63\u78ba\u6027\u3002</p> <p></p> <p> \u5716 8-5 \u00a0 \u5b8c\u7f8e\u4e8c\u5143\u6a39\u7684\u5404\u5c64\u7bc0\u9ede\u6578\u91cf </p> <p>\u5982\u5716 8-5 \u6240\u793a\uff0c\u7bc0\u9ede\u201c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\u201d\u7684\u6700\u5927\u8fed\u4ee3\u6b21\u6578\u7b49\u65bc\u8a72\u7bc0\u9ede\u5230\u8449\u7bc0\u9ede\u7684\u8ddd\u96e2\uff0c\u800c\u8a72\u8ddd\u96e2\u6b63\u662f\u201c\u7bc0\u9ede\u9ad8\u5ea6\u201d\u3002\u56e0\u6b64\uff0c\u6211\u5011\u53ef\u4ee5\u5c0d\u5404\u5c64\u7684\u201c\u7bc0\u9ede\u6578\u91cf \\(\\times\\) \u7bc0\u9ede\u9ad8\u5ea6\u201d\u6c42\u548c\uff0c\u5f97\u5230\u6240\u6709\u7bc0\u9ede\u7684\u5806\u7a4d\u5316\u8fed\u4ee3\u6b21\u6578\u7684\u7e3d\u548c\u3002</p> \\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{(h-1)}\\times1 \\] <p>\u5316\u7c21\u4e0a\u5f0f\u9700\u8981\u85c9\u52a9\u4e2d\u5b78\u7684\u6578\u5217\u77e5\u8b58\uff0c\u5148\u5c07 \\(T(h)\\) \u4e58\u4ee5 \\(2\\) \uff0c\u5f97\u5230\uff1a</p> \\[ \\begin{aligned} T(h) &amp; = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{h-1}\\times1 \\newline 2 T(h) &amp; = 2^1h + 2^2(h-1) + 2^3(h-2) + \\dots + 2^{h}\\times1 \\newline \\end{aligned} \\] <p>\u4f7f\u7528\u932f\u4f4d\u76f8\u6e1b\u6cd5\uff0c\u7528\u4e0b\u5f0f \\(2 T(h)\\) \u6e1b\u53bb\u4e0a\u5f0f \\(T(h)\\) \uff0c\u53ef\u5f97\uff1a</p> \\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\] <p>\u89c0\u5bdf\u4e0a\u5f0f\uff0c\u767c\u73fe \\(T(h)\\) \u662f\u4e00\u500b\u7b49\u6bd4\u6578\u5217\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528\u6c42\u548c\u516c\u5f0f\uff0c\u5f97\u5230\u6642\u9593\u8907\u96dc\u5ea6\u70ba\uff1a</p> \\[ \\begin{aligned} T(h) &amp; = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline &amp; = 2^{h+1} - h - 2 \\newline &amp; = O(2^h) \\end{aligned} \\] <p>\u9032\u4e00\u6b65\uff0c\u9ad8\u5ea6\u70ba \\(h\\) \u7684\u5b8c\u7f8e\u4e8c\u5143\u6a39\u7684\u7bc0\u9ede\u6578\u91cf\u70ba \\(n = 2^{h+1} - 1\\) \uff0c\u6613\u5f97\u8907\u96dc\u5ea6\u70ba \\(O(2^h) = O(n)\\) \u3002\u4ee5\u4e0a\u63a8\u7b97\u8868\u660e\uff0c\u8f38\u5165\u4e32\u5217\u4e26\u5efa\u5806\u7a4d\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002</p>"},{"location":"chapter_heap/heap/","title":"8.1 \u00a0 \u5806\u7a4d","text":"<p>\u5806\u7a4d\uff08heap\uff09\u662f\u4e00\u7a2e\u6eff\u8db3\u7279\u5b9a\u689d\u4ef6\u7684\u5b8c\u5168\u4e8c\u5143\u6a39\uff0c\u4e3b\u8981\u53ef\u5206\u70ba\u5169\u7a2e\u578b\u5225\uff0c\u5982\u5716 8-1 \u6240\u793a\u3002</p> <ul> <li>\u5c0f\u9802\u5806\u7a4d\uff08min heap\uff09\uff1a\u4efb\u610f\u7bc0\u9ede\u7684\u503c \\(\\leq\\) \u5176\u5b50\u7bc0\u9ede\u7684\u503c\u3002</li> <li>\u5927\u9802\u5806\u7a4d\uff08max heap\uff09\uff1a\u4efb\u610f\u7bc0\u9ede\u7684\u503c \\(\\geq\\) \u5176\u5b50\u7bc0\u9ede\u7684\u503c\u3002</li> </ul> <p></p> <p> \u5716 8-1 \u00a0 \u5c0f\u9802\u5806\u7a4d\u8207\u5927\u9802\u5806\u7a4d </p> <p>\u5806\u7a4d\u4f5c\u70ba\u5b8c\u5168\u4e8c\u5143\u6a39\u7684\u4e00\u500b\u7279\u4f8b\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002</p> <ul> <li>\u6700\u5e95\u5c64\u7bc0\u9ede\u9760\u5de6\u586b\u5145\uff0c\u5176\u4ed6\u5c64\u7684\u7bc0\u9ede\u90fd\u88ab\u586b\u6eff\u3002</li> <li>\u6211\u5011\u5c07\u4e8c\u5143\u6a39\u7684\u6839\u7bc0\u9ede\u7a31\u70ba\u201c\u5806\u7a4d\u9802\u201d\uff0c\u5c07\u5e95\u5c64\u6700\u9760\u53f3\u7684\u7bc0\u9ede\u7a31\u70ba\u201c\u5806\u7a4d\u5e95\u201d\u3002</li> <li>\u5c0d\u65bc\u5927\u9802\u5806\u7a4d\uff08\u5c0f\u9802\u5806\u7a4d\uff09\uff0c\u5806\u7a4d\u9802\u5143\u7d20\uff08\u6839\u7bc0\u9ede\uff09\u7684\u503c\u662f\u6700\u5927\uff08\u6700\u5c0f\uff09\u7684\u3002</li> </ul>"},{"location":"chapter_heap/heap/#811","title":"8.1.1 \u00a0 \u5806\u7a4d\u7684\u5e38\u7528\u64cd\u4f5c","text":"<p>\u9700\u8981\u6307\u51fa\u7684\u662f\uff0c\u8a31\u591a\u7a0b\u5f0f\u8a9e\u8a00\u63d0\u4f9b\u7684\u662f\u512a\u5148\u4f47\u5217\uff08priority queue\uff09\uff0c\u9019\u662f\u4e00\u7a2e\u62bd\u8c61\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u5b9a\u7fa9\u70ba\u5177\u6709\u512a\u5148\u9806\u5e8f\u6392\u5e8f\u7684\u4f47\u5217\u3002</p> <p>\u5be6\u969b\u4e0a\uff0c\u5806\u7a4d\u901a\u5e38\u7528\u65bc\u5be6\u73fe\u512a\u5148\u4f47\u5217\uff0c\u5927\u9802\u5806\u7a4d\u76f8\u7576\u65bc\u5143\u7d20\u6309\u5f9e\u5927\u5230\u5c0f\u7684\u9806\u5e8f\u51fa\u5217\u7684\u512a\u5148\u4f47\u5217\u3002\u5f9e\u4f7f\u7528\u89d2\u5ea6\u4f86\u770b\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u201c\u512a\u5148\u4f47\u5217\u201d\u548c\u201c\u5806\u7a4d\u201d\u770b\u4f5c\u7b49\u50f9\u7684\u8cc7\u6599\u7d50\u69cb\u3002\u56e0\u6b64\uff0c\u672c\u66f8\u5c0d\u5169\u8005\u4e0d\u505a\u7279\u5225\u5340\u5206\uff0c\u7d71\u4e00\u7a31\u4f5c\u201c\u5806\u7a4d\u201d\u3002</p> <p>\u5806\u7a4d\u7684\u5e38\u7528\u64cd\u4f5c\u898b\u8868 8-1 \uff0c\u65b9\u6cd5\u540d\u9700\u8981\u6839\u64da\u7a0b\u5f0f\u8a9e\u8a00\u4f86\u78ba\u5b9a\u3002</p> <p> \u8868 8-1 \u00a0 \u5806\u7a4d\u7684\u64cd\u4f5c\u6548\u7387 </p> \u65b9\u6cd5\u540d \u63cf\u8ff0 \u6642\u9593\u8907\u96dc\u5ea6 <code>push()</code> \u5143\u7d20\u5165\u5806\u7a4d \\(O(\\log n)\\) <code>pop()</code> \u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d \\(O(\\log n)\\) <code>peek()</code> \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20\uff08\u5c0d\u65bc\u5927 / \u5c0f\u9802\u5806\u7a4d\u5206\u5225\u70ba\u6700\u5927 / \u5c0f\u503c\uff09 \\(O(1)\\) <code>size()</code> \u7372\u53d6\u5806\u7a4d\u7684\u5143\u7d20\u6578\u91cf \\(O(1)\\) <code>isEmpty()</code> \u5224\u65b7\u5806\u7a4d\u662f\u5426\u70ba\u7a7a \\(O(1)\\) <p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7a0b\u5f0f\u8a9e\u8a00\u63d0\u4f9b\u7684\u5806\u7a4d\u985e\u5225\uff08\u6216\u512a\u5148\u4f47\u5217\u985e\u5225\uff09\u3002</p> <p>\u985e\u4f3c\u65bc\u6392\u5e8f\u6f14\u7b97\u6cd5\u4e2d\u7684\u201c\u5f9e\u5c0f\u5230\u5927\u6392\u5217\u201d\u548c\u201c\u5f9e\u5927\u5230\u5c0f\u6392\u5217\u201d\uff0c\u6211\u5011\u53ef\u4ee5\u900f\u904e\u8a2d\u5b9a\u4e00\u500b <code>flag</code> \u6216\u4fee\u6539 <code>Comparator</code> \u5be6\u73fe\u201c\u5c0f\u9802\u5806\u7a4d\u201d\u8207\u201c\u5927\u9802\u5806\u7a4d\u201d\u4e4b\u9593\u7684\u8f49\u63db\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig heap.py<pre><code># \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\nmin_heap, flag = [], 1\n# \u521d\u59cb\u5316\u5927\u9802\u5806\u7a4d\nmax_heap, flag = [], -1\n\n# Python \u7684 heapq \u6a21\u7d44\u9810\u8a2d\u5be6\u73fe\u5c0f\u9802\u5806\u7a4d\n# \u8003\u616e\u5c07\u201c\u5143\u7d20\u53d6\u8ca0\u201d\u5f8c\u518d\u5165\u5806\u7a4d\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u5c07\u5927\u5c0f\u95dc\u4fc2\u985b\u5012\uff0c\u5f9e\u800c\u5be6\u73fe\u5927\u9802\u5806\u7a4d\n# \u5728\u672c\u793a\u4f8b\u4e2d\uff0cflag = 1 \u6642\u5c0d\u61c9\u5c0f\u9802\u5806\u7a4d\uff0cflag = -1 \u6642\u5c0d\u61c9\u5927\u9802\u5806\u7a4d\n\n# \u5143\u7d20\u5165\u5806\u7a4d\nheapq.heappush(max_heap, flag * 1)\nheapq.heappush(max_heap, flag * 3)\nheapq.heappush(max_heap, flag * 2)\nheapq.heappush(max_heap, flag * 5)\nheapq.heappush(max_heap, flag * 4)\n\n# \u7372\u53d6\u5806\u7a4d\u9802\u5143\u7d20\npeek: int = flag * max_heap[0] # 5\n\n# \u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\n# \u51fa\u5806\u7a4d\u5143\u7d20\u6703\u5f62\u6210\u4e00\u500b\u5f9e\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nval = flag * heapq.heappop(max_heap) # 5\nval = flag * heapq.heappop(max_heap) # 4\nval = flag * heapq.heappop(max_heap) # 3\nval = flag * heapq.heappop(max_heap) # 2\nval = flag * heapq.heappop(max_heap) # 1\n\n# \u7372\u53d6\u5806\u7a4d\u5927\u5c0f\nsize: int = len(max_heap)\n\n# \u5224\u65b7\u5806\u7a4d\u662f\u5426\u70ba\u7a7a\nis_empty: bool = not max_heap\n\n# \u8f38\u5165\u4e32\u5217\u4e26\u5efa\u5806\u7a4d\nmin_heap: list[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\n</code></pre> heap.cpp<pre><code>/* \u521d\u59cb\u5316\u5806\u7a4d */\n// \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\npriority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; minHeap;\n// \u521d\u59cb\u5316\u5927\u9802\u5806\u7a4d\npriority_queue&lt;int, vector&lt;int&gt;, less&lt;int&gt;&gt; maxHeap;\n\n/* \u5143\u7d20\u5165\u5806\u7a4d */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n\n/* \u7372\u53d6\u5806\u7a4d\u9802\u5143\u7d20 */\nint peek = maxHeap.top(); // 5\n\n/* \u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d */\n// \u51fa\u5806\u7a4d\u5143\u7d20\u6703\u5f62\u6210\u4e00\u500b\u5f9e\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nmaxHeap.pop(); // 5\nmaxHeap.pop(); // 4\nmaxHeap.pop(); // 3\nmaxHeap.pop(); // 2\nmaxHeap.pop(); // 1\n\n/* \u7372\u53d6\u5806\u7a4d\u5927\u5c0f */\nint size = maxHeap.size();\n\n/* \u5224\u65b7\u5806\u7a4d\u662f\u5426\u70ba\u7a7a */\nbool isEmpty = maxHeap.empty();\n\n/* \u8f38\u5165\u4e32\u5217\u4e26\u5efa\u5806\u7a4d */\nvector&lt;int&gt; input{1, 3, 2, 5, 4};\npriority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; minHeap(input.begin(), input.end());\n</code></pre> heap.java<pre><code>/* \u521d\u59cb\u5316\u5806\u7a4d */\n// \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\nQueue&lt;Integer&gt; minHeap = new PriorityQueue&lt;&gt;();\n// \u521d\u59cb\u5316\u5927\u9802\u5806\u7a4d\uff08\u4f7f\u7528 lambda \u8868\u793a\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nQueue&lt;Integer&gt; maxHeap = new PriorityQueue&lt;&gt;((a, b) -&gt; b - a);\n\n/* \u5143\u7d20\u5165\u5806\u7a4d */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n\n/* \u7372\u53d6\u5806\u7a4d\u9802\u5143\u7d20 */\nint peek = maxHeap.peek(); // 5\n\n/* \u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d */\n// \u51fa\u5806\u7a4d\u5143\u7d20\u6703\u5f62\u6210\u4e00\u500b\u5f9e\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.poll(); // 1\n\n/* \u7372\u53d6\u5806\u7a4d\u5927\u5c0f */\nint size = maxHeap.size();\n\n/* \u5224\u65b7\u5806\u7a4d\u662f\u5426\u70ba\u7a7a */\nboolean isEmpty = maxHeap.isEmpty();\n\n/* \u8f38\u5165\u4e32\u5217\u4e26\u5efa\u5806\u7a4d */\nminHeap = new PriorityQueue&lt;&gt;(Arrays.asList(1, 3, 2, 5, 4));\n</code></pre> heap.cs<pre><code>/* \u521d\u59cb\u5316\u5806\u7a4d */\n// \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\nPriorityQueue&lt;int, int&gt; minHeap = new();\n// \u521d\u59cb\u5316\u5927\u9802\u5806\u7a4d\uff08\u4f7f\u7528 lambda \u8868\u793a\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nPriorityQueue&lt;int, int&gt; maxHeap = new(Comparer&lt;int&gt;.Create((x, y) =&gt; y - x));\n\n/* \u5143\u7d20\u5165\u5806\u7a4d */\nmaxHeap.Enqueue(1, 1);\nmaxHeap.Enqueue(3, 3);\nmaxHeap.Enqueue(2, 2);\nmaxHeap.Enqueue(5, 5);\nmaxHeap.Enqueue(4, 4);\n\n/* \u7372\u53d6\u5806\u7a4d\u9802\u5143\u7d20 */\nint peek = maxHeap.Peek();//5\n\n/* \u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d */\n// \u51fa\u5806\u7a4d\u5143\u7d20\u6703\u5f62\u6210\u4e00\u500b\u5f9e\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.Dequeue(); // 5\npeek = maxHeap.Dequeue(); // 4\npeek = maxHeap.Dequeue(); // 3\npeek = maxHeap.Dequeue(); // 2\npeek = maxHeap.Dequeue(); // 1\n\n/* \u7372\u53d6\u5806\u7a4d\u5927\u5c0f */\nint size = maxHeap.Count;\n\n/* \u5224\u65b7\u5806\u7a4d\u662f\u5426\u70ba\u7a7a */\nbool isEmpty = maxHeap.Count == 0;\n\n/* \u8f38\u5165\u4e32\u5217\u4e26\u5efa\u5806\u7a4d */\nminHeap = new PriorityQueue&lt;int, int&gt;([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);\n</code></pre> heap.go<pre><code>// Go \u8a9e\u8a00\u4e2d\u53ef\u4ee5\u900f\u904e\u5be6\u73fe heap.Interface \u4f86\u69cb\u5efa\u6574\u6578\u5927\u9802\u5806\u7a4d\n// \u5be6\u73fe heap.Interface \u9700\u8981\u540c\u6642\u5be6\u73fe sort.Interface\ntype intHeap []any\n\n// Push heap.Interface \u7684\u65b9\u6cd5\uff0c\u5be6\u73fe\u63a8\u5165\u5143\u7d20\u5230\u5806\u7a4d\nfunc (h *intHeap) Push(x any) {\n // Push \u548c Pop \u4f7f\u7528 pointer receiver \u4f5c\u70ba\u53c3\u6578\n // \u56e0\u70ba\u5b83\u5011\u4e0d\u50c5\u6703\u5c0d\u5207\u7247\u7684\u5167\u5bb9\u9032\u884c\u8abf\u6574\uff0c\u9084\u6703\u4fee\u6539\u5207\u7247\u7684\u9577\u5ea6\u3002\n *h = append(*h, x.(int))\n}\n\n// Pop heap.Interface \u7684\u65b9\u6cd5\uff0c\u5be6\u73fe\u5f48\u51fa\u5806\u7a4d\u9802\u5143\u7d20\nfunc (h *intHeap) Pop() any {\n // \u5f85\u51fa\u5806\u7a4d\u5143\u7d20\u5b58\u653e\u5728\u6700\u5f8c\n last := (*h)[len(*h)-1]\n *h = (*h)[:len(*h)-1]\n return last\n}\n\n// Len sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Len() int {\n return len(*h)\n}\n\n// Less sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Less(i, j int) bool {\n // \u5982\u679c\u5be6\u73fe\u5c0f\u9802\u5806\u7a4d\uff0c\u5247\u9700\u8981\u8abf\u6574\u70ba\u5c0f\u65bc\u865f\n return (*h)[i].(int) &gt; (*h)[j].(int)\n}\n\n// Swap sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Swap(i, j int) {\n (*h)[i], (*h)[j] = (*h)[j], (*h)[i]\n}\n\n// Top \u7372\u53d6\u5806\u7a4d\u9802\u5143\u7d20\nfunc (h *intHeap) Top() any {\n return (*h)[0]\n}\n\n/* Driver Code */\nfunc TestHeap(t *testing.T) {\n /* \u521d\u59cb\u5316\u5806\u7a4d */\n // \u521d\u59cb\u5316\u5927\u9802\u5806\u7a4d\n maxHeap := &amp;intHeap{}\n heap.Init(maxHeap)\n /* \u5143\u7d20\u5165\u5806\u7a4d */\n // \u547c\u53eb heap.Interface \u7684\u65b9\u6cd5\uff0c\u4f86\u65b0\u589e\u5143\u7d20\n heap.Push(maxHeap, 1)\n heap.Push(maxHeap, 3)\n heap.Push(maxHeap, 2)\n heap.Push(maxHeap, 4)\n heap.Push(maxHeap, 5)\n\n /* \u7372\u53d6\u5806\u7a4d\u9802\u5143\u7d20 */\n top := maxHeap.Top()\n fmt.Printf(\"\u5806\u7a4d\u9802\u5143\u7d20\u70ba %d\\n\", top)\n\n /* \u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d */\n // \u547c\u53eb heap.Interface \u7684\u65b9\u6cd5\uff0c\u4f86\u79fb\u9664\u5143\u7d20\n heap.Pop(maxHeap) // 5\n heap.Pop(maxHeap) // 4\n heap.Pop(maxHeap) // 3\n heap.Pop(maxHeap) // 2\n heap.Pop(maxHeap) // 1\n\n /* \u7372\u53d6\u5806\u7a4d\u5927\u5c0f */\n size := len(*maxHeap)\n fmt.Printf(\"\u5806\u7a4d\u5143\u7d20\u6578\u91cf\u70ba %d\\n\", size)\n\n /* \u5224\u65b7\u5806\u7a4d\u662f\u5426\u70ba\u7a7a */\n isEmpty := len(*maxHeap) == 0\n fmt.Printf(\"\u5806\u7a4d\u662f\u5426\u70ba\u7a7a %t\\n\", isEmpty)\n}\n</code></pre> heap.swift<pre><code>/* \u521d\u59cb\u5316\u5806\u7a4d */\n// Swift \u7684 Heap \u578b\u5225\u540c\u6642\u652f\u6301\u6700\u5927\u5806\u7a4d\u548c\u6700\u5c0f\u5806\u7a4d\uff0c\u4e14\u9700\u8981\u5f15\u5165 swift-collections\nvar heap = Heap&lt;Int&gt;()\n\n/* \u5143\u7d20\u5165\u5806\u7a4d */\nheap.insert(1)\nheap.insert(3)\nheap.insert(2)\nheap.insert(5)\nheap.insert(4)\n\n/* \u7372\u53d6\u5806\u7a4d\u9802\u5143\u7d20 */\nvar peek = heap.max()!\n\n/* \u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d */\npeek = heap.removeMax() // 5\npeek = heap.removeMax() // 4\npeek = heap.removeMax() // 3\npeek = heap.removeMax() // 2\npeek = heap.removeMax() // 1\n\n/* \u7372\u53d6\u5806\u7a4d\u5927\u5c0f */\nlet size = heap.count\n\n/* \u5224\u65b7\u5806\u7a4d\u662f\u5426\u70ba\u7a7a */\nlet isEmpty = heap.isEmpty\n\n/* \u8f38\u5165\u4e32\u5217\u4e26\u5efa\u5806\u7a4d */\nlet heap2 = Heap([1, 3, 2, 5, 4])\n</code></pre> heap.js<pre><code>// JavaScript \u672a\u63d0\u4f9b\u5167\u5efa Heap \u985e\u5225\n</code></pre> heap.ts<pre><code>// TypeScript \u672a\u63d0\u4f9b\u5167\u5efa Heap \u985e\u5225\n</code></pre> heap.dart<pre><code>// Dart \u672a\u63d0\u4f9b\u5167\u5efa Heap \u985e\u5225\n</code></pre> heap.rs<pre><code>use std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n\n/* \u521d\u59cb\u5316\u5806\u7a4d */\n// \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\nlet mut min_heap = BinaryHeap::&lt;Reverse&lt;i32&gt;&gt;::new();\n// \u521d\u59cb\u5316\u5927\u9802\u5806\u7a4d\nlet mut max_heap = BinaryHeap::new();\n\n/* \u5143\u7d20\u5165\u5806\u7a4d */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n\n/* \u7372\u53d6\u5806\u7a4d\u9802\u5143\u7d20 */\nlet peek = max_heap.peek().unwrap(); // 5\n\n/* \u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d */\n// \u51fa\u5806\u7a4d\u5143\u7d20\u6703\u5f62\u6210\u4e00\u500b\u5f9e\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nlet peek = max_heap.pop().unwrap(); // 5\nlet peek = max_heap.pop().unwrap(); // 4\nlet peek = max_heap.pop().unwrap(); // 3\nlet peek = max_heap.pop().unwrap(); // 2\nlet peek = max_heap.pop().unwrap(); // 1\n\n/* \u7372\u53d6\u5806\u7a4d\u5927\u5c0f */\nlet size = max_heap.len();\n\n/* \u5224\u65b7\u5806\u7a4d\u662f\u5426\u70ba\u7a7a */\nlet is_empty = max_heap.is_empty();\n\n/* \u8f38\u5165\u4e32\u5217\u4e26\u5efa\u5806\u7a4d */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n</code></pre> heap.c<pre><code>// C \u672a\u63d0\u4f9b\u5167\u5efa Heap \u985e\u5225\n</code></pre> heap.kt<pre><code>/* \u521d\u59cb\u5316\u5806\u7a4d */\n// \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\nvar minHeap = PriorityQueue&lt;Int&gt;()\n// \u521d\u59cb\u5316\u5927\u9802\u5806\u7a4d\uff08\u4f7f\u7528 lambda \u8868\u793a\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nval maxHeap = PriorityQueue { a: Int, b: Int -&gt; b - a }\n\n/* \u5143\u7d20\u5165\u5806\u7a4d */\nmaxHeap.offer(1)\nmaxHeap.offer(3)\nmaxHeap.offer(2)\nmaxHeap.offer(5)\nmaxHeap.offer(4)\n\n/* \u7372\u53d6\u5806\u7a4d\u9802\u5143\u7d20 */\nvar peek = maxHeap.peek() // 5\n\n/* \u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d */\n// \u51fa\u5806\u7a4d\u5143\u7d20\u6703\u5f62\u6210\u4e00\u500b\u5f9e\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.poll() // 5\npeek = maxHeap.poll() // 4\npeek = maxHeap.poll() // 3\npeek = maxHeap.poll() // 2\npeek = maxHeap.poll() // 1\n\n/* \u7372\u53d6\u5806\u7a4d\u5927\u5c0f */\nval size = maxHeap.size\n\n/* \u5224\u65b7\u5806\u7a4d\u662f\u5426\u70ba\u7a7a */\nval isEmpty = maxHeap.isEmpty()\n\n/* \u8f38\u5165\u4e32\u5217\u4e26\u5efa\u5806\u7a4d */\nminHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))\n</code></pre> heap.rb<pre><code>\n</code></pre> heap.zig<pre><code>\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_heap/heap/#812","title":"8.1.2 \u00a0 \u5806\u7a4d\u7684\u5be6\u73fe","text":"<p>\u4e0b\u6587\u5be6\u73fe\u7684\u662f\u5927\u9802\u5806\u7a4d\u3002\u82e5\u8981\u5c07\u5176\u8f49\u63db\u70ba\u5c0f\u9802\u5806\u7a4d\uff0c\u53ea\u9700\u5c07\u6240\u6709\u5927\u5c0f\u908f\u8f2f\u5224\u65b7\u9032\u884c\u9006\u8f49\uff08\u4f8b\u5982\uff0c\u5c07 \\(\\geq\\) \u66ff\u63db\u70ba \\(\\leq\\) \uff09\u3002\u611f\u8208\u8da3\u7684\u8b80\u8005\u53ef\u4ee5\u81ea\u884c\u5be6\u73fe\u3002</p>"},{"location":"chapter_heap/heap/#1","title":"1. \u00a0 \u5806\u7a4d\u7684\u5132\u5b58\u8207\u8868\u793a","text":"<p>\u201c\u4e8c\u5143\u6a39\u201d\u7ae0\u7bc0\u8b1b\u904e\uff0c\u5b8c\u5168\u4e8c\u5143\u6a39\u975e\u5e38\u9069\u5408\u7528\u9663\u5217\u4f86\u8868\u793a\u3002\u7531\u65bc\u5806\u7a4d\u6b63\u662f\u4e00\u7a2e\u5b8c\u5168\u4e8c\u5143\u6a39\uff0c\u56e0\u6b64\u6211\u5011\u5c07\u63a1\u7528\u9663\u5217\u4f86\u5132\u5b58\u5806\u7a4d\u3002</p> <p>\u7576\u4f7f\u7528\u9663\u5217\u8868\u793a\u4e8c\u5143\u6a39\u6642\uff0c\u5143\u7d20\u4ee3\u8868\u7bc0\u9ede\u503c\uff0c\u7d22\u5f15\u4ee3\u8868\u7bc0\u9ede\u5728\u4e8c\u5143\u6a39\u4e2d\u7684\u4f4d\u7f6e\u3002\u7bc0\u9ede\u6307\u6a19\u900f\u904e\u7d22\u5f15\u5c0d\u6620\u516c\u5f0f\u4f86\u5be6\u73fe\u3002</p> <p>\u5982\u5716 8-2 \u6240\u793a\uff0c\u7d66\u5b9a\u7d22\u5f15 \\(i\\) \uff0c\u5176\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15\u70ba \\(2i + 1\\) \uff0c\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15\u70ba \\(2i + 2\\) \uff0c\u7236\u7bc0\u9ede\u7684\u7d22\u5f15\u70ba \\((i - 1) / 2\\)\uff08\u5411\u4e0b\u6574\u9664\uff09\u3002\u7576\u7d22\u5f15\u8d8a\u754c\u6642\uff0c\u8868\u793a\u7a7a\u7bc0\u9ede\u6216\u7bc0\u9ede\u4e0d\u5b58\u5728\u3002</p> <p></p> <p> \u5716 8-2 \u00a0 \u5806\u7a4d\u7684\u8868\u793a\u8207\u5132\u5b58 </p> <p>\u6211\u5011\u53ef\u4ee5\u5c07\u7d22\u5f15\u5c0d\u6620\u516c\u5f0f\u5c01\u88dd\u6210\u51fd\u5f0f\uff0c\u65b9\u4fbf\u5f8c\u7e8c\u4f7f\u7528\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.py<pre><code>def left(self, i: int) -&gt; int:\n \"\"\"\u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 1\n\ndef right(self, i: int) -&gt; int:\n \"\"\"\u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 2\n\ndef parent(self, i: int) -&gt; int:\n \"\"\"\u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15\"\"\"\n return (i - 1) // 2 # \u5411\u4e0b\u6574\u9664\n</code></pre> my_heap.cpp<pre><code>/* \u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint left(int i) {\n return 2 * i + 1;\n}\n\n/* \u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint right(int i) {\n return 2 * i + 2;\n}\n\n/* \u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.java<pre><code>/* \u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint left(int i) {\n return 2 * i + 1;\n}\n\n/* \u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint right(int i) {\n return 2 * i + 2;\n}\n\n/* \u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.cs<pre><code>/* \u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint Left(int i) {\n return 2 * i + 1;\n}\n\n/* \u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint Right(int i) {\n return 2 * i + 2;\n}\n\n/* \u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint Parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.go<pre><code>/* \u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) left(i int) int {\n return 2*i + 1\n}\n\n/* \u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) right(i int) int {\n return 2*i + 2\n}\n\n/* \u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) parent(i int) int {\n // \u5411\u4e0b\u6574\u9664\n return (i - 1) / 2\n}\n</code></pre> my_heap.swift<pre><code>/* \u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfunc left(i: Int) -&gt; Int {\n 2 * i + 1\n}\n\n/* \u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfunc right(i: Int) -&gt; Int {\n 2 * i + 2\n}\n\n/* \u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfunc parent(i: Int) -&gt; Int {\n (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.js<pre><code>/* \u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n#left(i) {\n return 2 * i + 1;\n}\n\n/* \u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n#right(i) {\n return 2 * i + 2;\n}\n\n/* \u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\n#parent(i) {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.ts<pre><code>/* \u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nleft(i: number): number {\n return 2 * i + 1;\n}\n\n/* \u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nright(i: number): number {\n return 2 * i + 2;\n}\n\n/* \u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\nparent(i: number): number {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.dart<pre><code>/* \u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint _left(int i) {\n return 2 * i + 1;\n}\n\n/* \u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint _right(int i) {\n return 2 * i + 2;\n}\n\n/* \u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint _parent(int i) {\n return (i - 1) ~/ 2; // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.rs<pre><code>/* \u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfn left(i: usize) -&gt; usize {\n 2 * i + 1\n}\n\n/* \u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfn right(i: usize) -&gt; usize {\n 2 * i + 2\n}\n\n/* \u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfn parent(i: usize) -&gt; usize {\n (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.c<pre><code>/* \u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint left(MaxHeap *maxHeap, int i) {\n return 2 * i + 1;\n}\n\n/* \u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint right(MaxHeap *maxHeap, int i) {\n return 2 * i + 2;\n}\n\n/* \u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\nint parent(MaxHeap *maxHeap, int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u53d6\u6574\n}\n</code></pre> my_heap.kt<pre><code>/* \u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfun left(i: Int): Int {\n return 2 * i + 1\n}\n\n/* \u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfun right(i: Int): Int {\n return 2 * i + 2\n}\n\n/* \u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfun parent(i: Int): Int {\n return (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.rb<pre><code>### \u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 ###\ndef left(i)\n 2 * i + 1\nend\n\n### \u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 ###\ndef right(i)\n 2 * i + 2\nend\n\n### \u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 ###\ndef parent(i)\n (i - 1) / 2 # \u5411\u4e0b\u6574\u9664\nend\n</code></pre> my_heap.zig<pre><code>// \u7372\u53d6\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15\nfn left(i: usize) usize {\n return 2 * i + 1;\n}\n\n// \u7372\u53d6\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15\nfn right(i: usize) usize {\n return 2 * i + 2;\n}\n\n// \u7372\u53d6\u7236\u7bc0\u9ede\u7684\u7d22\u5f15\nfn parent(i: usize) usize {\n // return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n return @divFloor(i - 1, 2);\n}\n</code></pre>"},{"location":"chapter_heap/heap/#2","title":"2. \u00a0 \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20","text":"<p>\u5806\u7a4d\u9802\u5143\u7d20\u5373\u70ba\u4e8c\u5143\u6a39\u7684\u6839\u7bc0\u9ede\uff0c\u4e5f\u5c31\u662f\u4e32\u5217\u7684\u9996\u500b\u5143\u7d20\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.py<pre><code>def peek(self) -&gt; int:\n \"\"\"\u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20\"\"\"\n return self.max_heap[0]\n</code></pre> my_heap.cpp<pre><code>/* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\nint peek() {\n return maxHeap[0];\n}\n</code></pre> my_heap.java<pre><code>/* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\nint peek() {\n return maxHeap.get(0);\n}\n</code></pre> my_heap.cs<pre><code>/* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\nint Peek() {\n return maxHeap[0];\n}\n</code></pre> my_heap.go<pre><code>/* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\nfunc (h *maxHeap) peek() any {\n return h.data[0]\n}\n</code></pre> my_heap.swift<pre><code>/* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\nfunc peek() -&gt; Int {\n maxHeap[0]\n}\n</code></pre> my_heap.js<pre><code>/* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\npeek() {\n return this.#maxHeap[0];\n}\n</code></pre> my_heap.ts<pre><code>/* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\npeek(): number {\n return this.maxHeap[0];\n}\n</code></pre> my_heap.dart<pre><code>/* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\nint peek() {\n return _maxHeap[0];\n}\n</code></pre> my_heap.rs<pre><code>/* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\nfn peek(&amp;self) -&gt; Option&lt;i32&gt; {\n self.max_heap.first().copied()\n}\n</code></pre> my_heap.c<pre><code>/* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\nint peek(MaxHeap *maxHeap) {\n return maxHeap-&gt;data[0];\n}\n</code></pre> my_heap.kt<pre><code>/* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\nfun peek(): Int {\n return maxHeap[0]\n}\n</code></pre> my_heap.rb<pre><code>### \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 ###\ndef peek\n @max_heap[0]\nend\n</code></pre> my_heap.zig<pre><code>// \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20\nfn peek(self: *Self) T {\n return self.max_heap.?.items[0];\n} \n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_heap/heap/#3","title":"3. \u00a0 \u5143\u7d20\u5165\u5806\u7a4d","text":"<p>\u7d66\u5b9a\u5143\u7d20 <code>val</code> \uff0c\u6211\u5011\u9996\u5148\u5c07\u5176\u65b0\u589e\u5230\u5806\u7a4d\u5e95\u3002\u65b0\u589e\u4e4b\u5f8c\uff0c\u7531\u65bc <code>val</code> \u53ef\u80fd\u5927\u65bc\u5806\u7a4d\u4e2d\u5176\u4ed6\u5143\u7d20\uff0c\u5806\u7a4d\u7684\u6210\u7acb\u689d\u4ef6\u53ef\u80fd\u5df2\u88ab\u7834\u58de\uff0c\u56e0\u6b64\u9700\u8981\u4fee\u5fa9\u5f9e\u63d2\u5165\u7bc0\u9ede\u5230\u6839\u7bc0\u9ede\u7684\u8def\u5f91\u4e0a\u7684\u5404\u500b\u7bc0\u9ede\uff0c\u9019\u500b\u64cd\u4f5c\u88ab\u7a31\u70ba\u5806\u7a4d\u5316\uff08heapify\uff09\u3002</p> <p>\u8003\u616e\u5f9e\u5165\u5806\u7a4d\u7bc0\u9ede\u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u57f7\u884c\u5806\u7a4d\u5316\u3002\u5982\u5716 8-3 \u6240\u793a\uff0c\u6211\u5011\u6bd4\u8f03\u63d2\u5165\u7bc0\u9ede\u8207\u5176\u7236\u7bc0\u9ede\u7684\u503c\uff0c\u5982\u679c\u63d2\u5165\u7bc0\u9ede\u66f4\u5927\uff0c\u5247\u5c07\u5b83\u5011\u4ea4\u63db\u3002\u7136\u5f8c\u7e7c\u7e8c\u57f7\u884c\u6b64\u64cd\u4f5c\uff0c\u5f9e\u5e95\u81f3\u9802\u4fee\u5fa9\u5806\u7a4d\u4e2d\u7684\u5404\u500b\u7bc0\u9ede\uff0c\u76f4\u81f3\u8d8a\u904e\u6839\u7bc0\u9ede\u6216\u9047\u5230\u7121\u9808\u4ea4\u63db\u7684\u7bc0\u9ede\u6642\u7d50\u675f\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 8-3 \u00a0 \u5143\u7d20\u5165\u5806\u7a4d\u6b65\u9a5f </p> <p>\u8a2d\u7bc0\u9ede\u7e3d\u6578\u70ba \\(n\\) \uff0c\u5247\u6a39\u7684\u9ad8\u5ea6\u70ba \\(O(\\log n)\\) \u3002\u7531\u6b64\u53ef\u77e5\uff0c\u5806\u7a4d\u5316\u64cd\u4f5c\u7684\u8ff4\u5708\u8f2a\u6578\u6700\u591a\u70ba \\(O(\\log n)\\) \uff0c\u5143\u7d20\u5165\u5806\u7a4d\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(\\log n)\\) \u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.py<pre><code>def push(self, val: int):\n \"\"\"\u5143\u7d20\u5165\u5806\u7a4d\"\"\"\n # \u65b0\u589e\u7bc0\u9ede\n self.max_heap.append(val)\n # \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n self.sift_up(self.size() - 1)\n\ndef sift_up(self, i: int):\n \"\"\"\u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\"\"\"\n while True:\n # \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n p = self.parent(i)\n # \u7576\u201c\u8d8a\u904e\u6839\u7bc0\u9ede\u201d\u6216\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\u6642\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n if p &lt; 0 or self.max_heap[i] &lt;= self.max_heap[p]:\n break\n # \u4ea4\u63db\u5169\u7bc0\u9ede\n self.swap(i, p)\n # \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p\n</code></pre> my_heap.cpp<pre><code>/* \u5143\u7d20\u5165\u5806\u7a4d */\nvoid push(int val) {\n // \u65b0\u589e\u7bc0\u9ede\n maxHeap.push_back(val);\n // \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n siftUp(size() - 1);\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n int p = parent(i);\n // \u7576\u201c\u8d8a\u904e\u6839\u7bc0\u9ede\u201d\u6216\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\u6642\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n if (p &lt; 0 || maxHeap[i] &lt;= maxHeap[p])\n break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n swap(maxHeap[i], maxHeap[p]);\n // \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p;\n }\n}\n</code></pre> my_heap.java<pre><code>/* \u5143\u7d20\u5165\u5806\u7a4d */\nvoid push(int val) {\n // \u65b0\u589e\u7bc0\u9ede\n maxHeap.add(val);\n // \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n siftUp(size() - 1);\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n int p = parent(i);\n // \u7576\u201c\u8d8a\u904e\u6839\u7bc0\u9ede\u201d\u6216\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\u6642\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n if (p &lt; 0 || maxHeap.get(i) &lt;= maxHeap.get(p))\n break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n swap(i, p);\n // \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p;\n }\n}\n</code></pre> my_heap.cs<pre><code>/* \u5143\u7d20\u5165\u5806\u7a4d */\nvoid Push(int val) {\n // \u65b0\u589e\u7bc0\u9ede\n maxHeap.Add(val);\n // \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n SiftUp(Size() - 1);\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316 */\nvoid SiftUp(int i) {\n while (true) {\n // \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n int p = Parent(i);\n // \u82e5\u201c\u8d8a\u904e\u6839\u7bc0\u9ede\u201d\u6216\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\uff0c\u5247\u7d50\u675f\u5806\u7a4d\u5316\n if (p &lt; 0 || maxHeap[i] &lt;= maxHeap[p])\n break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n Swap(i, p);\n // \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p;\n }\n}\n</code></pre> my_heap.go<pre><code>/* \u5143\u7d20\u5165\u5806\u7a4d */\nfunc (h *maxHeap) push(val any) {\n // \u65b0\u589e\u7bc0\u9ede\n h.data = append(h.data, val)\n // \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n h.siftUp(len(h.data) - 1)\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316 */\nfunc (h *maxHeap) siftUp(i int) {\n for true {\n // \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n p := h.parent(i)\n // \u7576\u201c\u8d8a\u904e\u6839\u7bc0\u9ede\u201d\u6216\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\u6642\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n if p &lt; 0 || h.data[i].(int) &lt;= h.data[p].(int) {\n break\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n h.swap(i, p)\n // \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p\n }\n}\n</code></pre> my_heap.swift<pre><code>/* \u5143\u7d20\u5165\u5806\u7a4d */\nfunc push(val: Int) {\n // \u65b0\u589e\u7bc0\u9ede\n maxHeap.append(val)\n // \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n siftUp(i: size() - 1)\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316 */\nfunc siftUp(i: Int) {\n var i = i\n while true {\n // \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n let p = parent(i: i)\n // \u7576\u201c\u8d8a\u904e\u6839\u7bc0\u9ede\u201d\u6216\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\u6642\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n if p &lt; 0 || maxHeap[i] &lt;= maxHeap[p] {\n break\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n swap(i: i, j: p)\n // \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p\n }\n}\n</code></pre> my_heap.js<pre><code>/* \u5143\u7d20\u5165\u5806\u7a4d */\npush(val) {\n // \u65b0\u589e\u7bc0\u9ede\n this.#maxHeap.push(val);\n // \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n this.#siftUp(this.size() - 1);\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316 */\n#siftUp(i) {\n while (true) {\n // \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n const p = this.#parent(i);\n // \u7576\u201c\u8d8a\u904e\u6839\u7bc0\u9ede\u201d\u6216\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\u6642\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n if (p &lt; 0 || this.#maxHeap[i] &lt;= this.#maxHeap[p]) break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n this.#swap(i, p);\n // \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p;\n }\n}\n</code></pre> my_heap.ts<pre><code>/* \u5143\u7d20\u5165\u5806\u7a4d */\npush(val: number): void {\n // \u65b0\u589e\u7bc0\u9ede\n this.maxHeap.push(val);\n // \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n this.siftUp(this.size() - 1);\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316 */\nsiftUp(i: number): void {\n while (true) {\n // \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n const p = this.parent(i);\n // \u7576\u201c\u8d8a\u904e\u6839\u7bc0\u9ede\u201d\u6216\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\u6642\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n if (p &lt; 0 || this.maxHeap[i] &lt;= this.maxHeap[p]) break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n this.swap(i, p);\n // \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p;\n }\n}\n</code></pre> my_heap.dart<pre><code>/* \u5143\u7d20\u5165\u5806\u7a4d */\nvoid push(int val) {\n // \u65b0\u589e\u7bc0\u9ede\n _maxHeap.add(val);\n // \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n siftUp(size() - 1);\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n int p = _parent(i);\n // \u7576\u201c\u8d8a\u904e\u6839\u7bc0\u9ede\u201d\u6216\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\u6642\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n if (p &lt; 0 || _maxHeap[i] &lt;= _maxHeap[p]) {\n break;\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n _swap(i, p);\n // \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p;\n }\n}\n</code></pre> my_heap.rs<pre><code>/* \u5143\u7d20\u5165\u5806\u7a4d */\nfn push(&amp;mut self, val: i32) {\n // \u65b0\u589e\u7bc0\u9ede\n self.max_heap.push(val);\n // \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n self.sift_up(self.size() - 1);\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316 */\nfn sift_up(&amp;mut self, mut i: usize) {\n loop {\n // \u7bc0\u9ede i \u5df2\u7d93\u662f\u5806\u7a4d\u9802\u7bc0\u9ede\u4e86\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n if i == 0 {\n break;\n }\n // \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n let p = Self::parent(i);\n // \u7576\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\u6642\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n if self.max_heap[i] &lt;= self.max_heap[p] {\n break;\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n self.swap(i, p);\n // \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p;\n }\n}\n</code></pre> my_heap.c<pre><code>/* \u5143\u7d20\u5165\u5806\u7a4d */\nvoid push(MaxHeap *maxHeap, int val) {\n // \u9810\u8a2d\u60c5\u6cc1\u4e0b\uff0c\u4e0d\u61c9\u8a72\u65b0\u589e\u9019\u9ebc\u591a\u7bc0\u9ede\n if (maxHeap-&gt;size == MAX_SIZE) {\n printf(\"heap is full!\");\n return;\n }\n // \u65b0\u589e\u7bc0\u9ede\n maxHeap-&gt;data[maxHeap-&gt;size] = val;\n maxHeap-&gt;size++;\n\n // \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n siftUp(maxHeap, maxHeap-&gt;size - 1);\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316 */\nvoid siftUp(MaxHeap *maxHeap, int i) {\n while (true) {\n // \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n int p = parent(maxHeap, i);\n // \u7576\u201c\u8d8a\u904e\u6839\u7bc0\u9ede\u201d\u6216\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\u6642\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n if (p &lt; 0 || maxHeap-&gt;data[i] &lt;= maxHeap-&gt;data[p]) {\n break;\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n swap(maxHeap, i, p);\n // \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p;\n }\n}\n</code></pre> my_heap.kt<pre><code>/* \u5143\u7d20\u5165\u5806\u7a4d */\nfun push(_val: Int) {\n // \u65b0\u589e\u7bc0\u9ede\n maxHeap.add(_val)\n // \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n siftUp(size() - 1)\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316 */\nfun siftUp(it: Int) {\n // Kotlin\u7684\u51fd\u5f0f\u53c3\u6578\u4e0d\u53ef\u8b8a\uff0c\u56e0\u6b64\u5efa\u7acb\u81e8\u6642\u8b8a\u6578\n var i = it\n while (true) {\n // \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n val p = parent(i)\n // \u7576\u201c\u8d8a\u904e\u6839\u7bc0\u9ede\u201d\u6216\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\u6642\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n if (p &lt; 0 || maxHeap[i] &lt;= maxHeap[p]) break\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n swap(i, p)\n // \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p\n }\n}\n</code></pre> my_heap.rb<pre><code>### \u5143\u7d20\u5165\u5806\u7a4d ###\ndef push(val)\n # \u65b0\u589e\u7bc0\u9ede\n @max_heap &lt;&lt; val\n # \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n sift_up(size - 1)\nend\n\n### \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316 ###\ndef sift_up(i)\n loop do\n # \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n p = parent(i)\n # \u7576\u201c\u8d8a\u904e\u6839\u7bc0\u9ede\u201d\u6216\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\u6642\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n break if p &lt; 0 || @max_heap[i] &lt;= @max_heap[p]\n # \u4ea4\u63db\u5169\u7bc0\u9ede\n swap(i, p)\n # \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p\n end\nend\n</code></pre> my_heap.zig<pre><code>// \u5143\u7d20\u5165\u5806\u7a4d\nfn push(self: *Self, val: T) !void {\n // \u65b0\u589e\u7bc0\u9ede\n try self.max_heap.?.append(val);\n // \u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\n try self.siftUp(self.size() - 1);\n} \n\n// \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\nfn siftUp(self: *Self, i_: usize) !void {\n var i = i_;\n while (true) {\n // \u7372\u53d6\u7bc0\u9ede i \u7684\u7236\u7bc0\u9ede\n var p = parent(i);\n // \u7576\u201c\u8d8a\u904e\u6839\u7bc0\u9ede\u201d\u6216\u201c\u7bc0\u9ede\u7121\u9808\u4fee\u5fa9\u201d\u6642\uff0c\u7d50\u675f\u5806\u7a4d\u5316\n if (p &lt; 0 or self.max_heap.?.items[i] &lt;= self.max_heap.?.items[p]) break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n try self.swap(i, p);\n // \u8ff4\u5708\u5411\u4e0a\u5806\u7a4d\u5316\n i = p;\n }\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_heap/heap/#4","title":"4. \u00a0 \u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d","text":"<p>\u5806\u7a4d\u9802\u5143\u7d20\u662f\u4e8c\u5143\u6a39\u7684\u6839\u7bc0\u9ede\uff0c\u5373\u4e32\u5217\u9996\u5143\u7d20\u3002\u5982\u679c\u6211\u5011\u76f4\u63a5\u5f9e\u4e32\u5217\u4e2d\u522a\u9664\u9996\u5143\u7d20\uff0c\u90a3\u9ebc\u4e8c\u5143\u6a39\u4e2d\u6240\u6709\u7bc0\u9ede\u7684\u7d22\u5f15\u90fd\u6703\u767c\u751f\u8b8a\u5316\uff0c\u9019\u5c07\u4f7f\u5f97\u5f8c\u7e8c\u4f7f\u7528\u5806\u7a4d\u5316\u9032\u884c\u4fee\u5fa9\u8b8a\u5f97\u56f0\u96e3\u3002\u70ba\u4e86\u5118\u91cf\u6e1b\u5c11\u5143\u7d20\u7d22\u5f15\u7684\u8b8a\u52d5\uff0c\u6211\u5011\u63a1\u7528\u4ee5\u4e0b\u64cd\u4f5c\u6b65\u9a5f\u3002</p> <ol> <li>\u4ea4\u63db\u5806\u7a4d\u9802\u5143\u7d20\u8207\u5806\u7a4d\u5e95\u5143\u7d20\uff08\u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff09\u3002</li> <li>\u4ea4\u63db\u5b8c\u6210\u5f8c\uff0c\u5c07\u5806\u7a4d\u5e95\u5f9e\u4e32\u5217\u4e2d\u522a\u9664\uff08\u6ce8\u610f\uff0c\u7531\u65bc\u5df2\u7d93\u4ea4\u63db\uff0c\u56e0\u6b64\u5be6\u969b\u4e0a\u522a\u9664\u7684\u662f\u539f\u4f86\u7684\u5806\u7a4d\u9802\u5143\u7d20\uff09\u3002</li> <li>\u5f9e\u6839\u7bc0\u9ede\u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u57f7\u884c\u5806\u7a4d\u5316\u3002</li> </ol> <p>\u5982\u5716 8-4 \u6240\u793a\uff0c\u201c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\u201d\u7684\u64cd\u4f5c\u65b9\u5411\u8207\u201c\u5f9e\u5e95\u81f3\u9802\u5806\u7a4d\u5316\u201d\u76f8\u53cd\uff0c\u6211\u5011\u5c07\u6839\u7bc0\u9ede\u7684\u503c\u8207\u5176\u5169\u500b\u5b50\u7bc0\u9ede\u7684\u503c\u9032\u884c\u6bd4\u8f03\uff0c\u5c07\u6700\u5927\u7684\u5b50\u7bc0\u9ede\u8207\u6839\u7bc0\u9ede\u4ea4\u63db\u3002\u7136\u5f8c\u8ff4\u5708\u57f7\u884c\u6b64\u64cd\u4f5c\uff0c\u76f4\u5230\u8d8a\u904e\u8449\u7bc0\u9ede\u6216\u9047\u5230\u7121\u9808\u4ea4\u63db\u7684\u7bc0\u9ede\u6642\u7d50\u675f\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt;&lt;10&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 8-4 \u00a0 \u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u6b65\u9a5f </p> <p>\u8207\u5143\u7d20\u5165\u5806\u7a4d\u64cd\u4f5c\u76f8\u4f3c\uff0c\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u4e5f\u70ba \\(O(\\log n)\\) \u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.py<pre><code>def pop(self) -&gt; int:\n \"\"\"\u5143\u7d20\u51fa\u5806\u7a4d\"\"\"\n # \u5224\u7a7a\u8655\u7406\n if self.is_empty():\n raise IndexError(\"\u5806\u7a4d\u70ba\u7a7a\")\n # \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n self.swap(0, self.size() - 1)\n # \u522a\u9664\u7bc0\u9ede\n val = self.max_heap.pop()\n # \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n self.sift_down(0)\n # \u8fd4\u56de\u5806\u7a4d\u9802\u5143\u7d20\n return val\n\ndef sift_down(self, i: int):\n \"\"\"\u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\"\"\"\n while True:\n # \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n l, r, ma = self.left(i), self.right(i), i\n if l &lt; self.size() and self.max_heap[l] &gt; self.max_heap[ma]:\n ma = l\n if r &lt; self.size() and self.max_heap[r] &gt; self.max_heap[ma]:\n ma = r\n # \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if ma == i:\n break\n # \u4ea4\u63db\u5169\u7bc0\u9ede\n self.swap(i, ma)\n # \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma\n</code></pre> my_heap.cpp<pre><code>/* \u5143\u7d20\u51fa\u5806\u7a4d */\nvoid pop() {\n // \u5224\u7a7a\u8655\u7406\n if (isEmpty()) {\n throw out_of_range(\"\u5806\u7a4d\u70ba\u7a7a\");\n }\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n swap(maxHeap[0], maxHeap[size() - 1]);\n // \u522a\u9664\u7bc0\u9ede\n maxHeap.pop_back();\n // \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n siftDown(0);\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n int l = left(i), r = right(i), ma = i;\n if (l &lt; size() &amp;&amp; maxHeap[l] &gt; maxHeap[ma])\n ma = l;\n if (r &lt; size() &amp;&amp; maxHeap[r] &gt; maxHeap[ma])\n ma = r;\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n swap(maxHeap[i], maxHeap[ma]);\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n</code></pre> my_heap.java<pre><code>/* \u5143\u7d20\u51fa\u5806\u7a4d */\nint pop() {\n // \u5224\u7a7a\u8655\u7406\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n swap(0, size() - 1);\n // \u522a\u9664\u7bc0\u9ede\n int val = maxHeap.remove(size() - 1);\n // \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n siftDown(0);\n // \u8fd4\u56de\u5806\u7a4d\u9802\u5143\u7d20\n return val;\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n int l = left(i), r = right(i), ma = i;\n if (l &lt; size() &amp;&amp; maxHeap.get(l) &gt; maxHeap.get(ma))\n ma = l;\n if (r &lt; size() &amp;&amp; maxHeap.get(r) &gt; maxHeap.get(ma))\n ma = r;\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n swap(i, ma);\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n</code></pre> my_heap.cs<pre><code>/* \u5143\u7d20\u51fa\u5806\u7a4d */\nint Pop() {\n // \u5224\u7a7a\u8655\u7406\n if (IsEmpty())\n throw new IndexOutOfRangeException();\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n Swap(0, Size() - 1);\n // \u522a\u9664\u7bc0\u9ede\n int val = maxHeap.Last();\n maxHeap.RemoveAt(Size() - 1);\n // \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n SiftDown(0);\n // \u8fd4\u56de\u5806\u7a4d\u9802\u5143\u7d20\n return val;\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nvoid SiftDown(int i) {\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n int l = Left(i), r = Right(i), ma = i;\n if (l &lt; Size() &amp;&amp; maxHeap[l] &gt; maxHeap[ma])\n ma = l;\n if (r &lt; Size() &amp;&amp; maxHeap[r] &gt; maxHeap[ma])\n ma = r;\n // \u82e5\u201c\u7bc0\u9ede i \u6700\u5927\u201d\u6216\u201c\u8d8a\u904e\u8449\u7bc0\u9ede\u201d\uff0c\u5247\u7d50\u675f\u5806\u7a4d\u5316\n if (ma == i) break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n Swap(i, ma);\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n</code></pre> my_heap.go<pre><code>/* \u5143\u7d20\u51fa\u5806\u7a4d */\nfunc (h *maxHeap) pop() any {\n // \u5224\u7a7a\u8655\u7406\n if h.isEmpty() {\n fmt.Println(\"error\")\n return nil\n }\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n h.swap(0, h.size()-1)\n // \u522a\u9664\u7bc0\u9ede\n val := h.data[len(h.data)-1]\n h.data = h.data[:len(h.data)-1]\n // \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n h.siftDown(0)\n\n // \u8fd4\u56de\u5806\u7a4d\u9802\u5143\u7d20\n return val\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nfunc (h *maxHeap) siftDown(i int) {\n for true {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba max\n l, r, max := h.left(i), h.right(i), i\n if l &lt; h.size() &amp;&amp; h.data[l].(int) &gt; h.data[max].(int) {\n max = l\n }\n if r &lt; h.size() &amp;&amp; h.data[r].(int) &gt; h.data[max].(int) {\n max = r\n }\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if max == i {\n break\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n h.swap(i, max)\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = max\n }\n}\n</code></pre> my_heap.swift<pre><code>/* \u5143\u7d20\u51fa\u5806\u7a4d */\nfunc pop() -&gt; Int {\n // \u5224\u7a7a\u8655\u7406\n if isEmpty() {\n fatalError(\"\u5806\u7a4d\u70ba\u7a7a\")\n }\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n swap(i: 0, j: size() - 1)\n // \u522a\u9664\u7bc0\u9ede\n let val = maxHeap.remove(at: size() - 1)\n // \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n siftDown(i: 0)\n // \u8fd4\u56de\u5806\u7a4d\u9802\u5143\u7d20\n return val\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nfunc siftDown(i: Int) {\n var i = i\n while true {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n let l = left(i: i)\n let r = right(i: i)\n var ma = i\n if l &lt; size(), maxHeap[l] &gt; maxHeap[ma] {\n ma = l\n }\n if r &lt; size(), maxHeap[r] &gt; maxHeap[ma] {\n ma = r\n }\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n swap(i: i, j: ma)\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma\n }\n}\n</code></pre> my_heap.js<pre><code>/* \u5143\u7d20\u51fa\u5806\u7a4d */\npop() {\n // \u5224\u7a7a\u8655\u7406\n if (this.isEmpty()) throw new Error('\u5806\u7a4d\u70ba\u7a7a');\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n this.#swap(0, this.size() - 1);\n // \u522a\u9664\u7bc0\u9ede\n const val = this.#maxHeap.pop();\n // \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n this.#siftDown(0);\n // \u8fd4\u56de\u5806\u7a4d\u9802\u5143\u7d20\n return val;\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\n#siftDown(i) {\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n const l = this.#left(i),\n r = this.#right(i);\n let ma = i;\n if (l &lt; this.size() &amp;&amp; this.#maxHeap[l] &gt; this.#maxHeap[ma]) ma = l;\n if (r &lt; this.size() &amp;&amp; this.#maxHeap[r] &gt; this.#maxHeap[ma]) ma = r;\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma === i) break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n this.#swap(i, ma);\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n</code></pre> my_heap.ts<pre><code>/* \u5143\u7d20\u51fa\u5806\u7a4d */\npop(): number {\n // \u5224\u7a7a\u8655\u7406\n if (this.isEmpty()) throw new RangeError('Heap is empty.');\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n this.swap(0, this.size() - 1);\n // \u522a\u9664\u7bc0\u9ede\n const val = this.maxHeap.pop();\n // \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n this.siftDown(0);\n // \u8fd4\u56de\u5806\u7a4d\u9802\u5143\u7d20\n return val;\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nsiftDown(i: number): void {\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n const l = this.left(i),\n r = this.right(i);\n let ma = i;\n if (l &lt; this.size() &amp;&amp; this.maxHeap[l] &gt; this.maxHeap[ma]) ma = l;\n if (r &lt; this.size() &amp;&amp; this.maxHeap[r] &gt; this.maxHeap[ma]) ma = r;\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma === i) break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n this.swap(i, ma);\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n</code></pre> my_heap.dart<pre><code>/* \u5143\u7d20\u51fa\u5806\u7a4d */\nint pop() {\n // \u5224\u7a7a\u8655\u7406\n if (isEmpty()) throw Exception('\u5806\u7a4d\u70ba\u7a7a');\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n _swap(0, size() - 1);\n // \u522a\u9664\u7bc0\u9ede\n int val = _maxHeap.removeLast();\n // \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n siftDown(0);\n // \u8fd4\u56de\u5806\u7a4d\u9802\u5143\u7d20\n return val;\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n int l = _left(i);\n int r = _right(i);\n int ma = i;\n if (l &lt; size() &amp;&amp; _maxHeap[l] &gt; _maxHeap[ma]) ma = l;\n if (r &lt; size() &amp;&amp; _maxHeap[r] &gt; _maxHeap[ma]) ma = r;\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma == i) break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n _swap(i, ma);\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n</code></pre> my_heap.rs<pre><code>/* \u5143\u7d20\u51fa\u5806\u7a4d */\nfn pop(&amp;mut self) -&gt; i32 {\n // \u5224\u7a7a\u8655\u7406\n if self.is_empty() {\n panic!(\"index out of bounds\");\n }\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n self.swap(0, self.size() - 1);\n // \u522a\u9664\u7bc0\u9ede\n let val = self.max_heap.pop().unwrap();\n // \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n self.sift_down(0);\n // \u8fd4\u56de\u5806\u7a4d\u9802\u5143\u7d20\n val\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nfn sift_down(&amp;mut self, mut i: usize) {\n loop {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n let (l, r, mut ma) = (Self::left(i), Self::right(i), i);\n if l &lt; self.size() &amp;&amp; self.max_heap[l] &gt; self.max_heap[ma] {\n ma = l;\n }\n if r &lt; self.size() &amp;&amp; self.max_heap[r] &gt; self.max_heap[ma] {\n ma = r;\n }\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break;\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n self.swap(i, ma);\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n</code></pre> my_heap.c<pre><code>/* \u5143\u7d20\u51fa\u5806\u7a4d */\nint pop(MaxHeap *maxHeap) {\n // \u5224\u7a7a\u8655\u7406\n if (isEmpty(maxHeap)) {\n printf(\"heap is empty!\");\n return INT_MAX;\n }\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n swap(maxHeap, 0, size(maxHeap) - 1);\n // \u522a\u9664\u7bc0\u9ede\n int val = maxHeap-&gt;data[maxHeap-&gt;size - 1];\n maxHeap-&gt;size--;\n // \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n siftDown(maxHeap, 0);\n\n // \u8fd4\u56de\u5806\u7a4d\u9802\u5143\u7d20\n return val;\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nvoid siftDown(MaxHeap *maxHeap, int i) {\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba max\n int l = left(maxHeap, i);\n int r = right(maxHeap, i);\n int max = i;\n if (l &lt; size(maxHeap) &amp;&amp; maxHeap-&gt;data[l] &gt; maxHeap-&gt;data[max]) {\n max = l;\n }\n if (r &lt; size(maxHeap) &amp;&amp; maxHeap-&gt;data[r] &gt; maxHeap-&gt;data[max]) {\n max = r;\n }\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (max == i) {\n break;\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n swap(maxHeap, i, max);\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = max;\n }\n}\n</code></pre> my_heap.kt<pre><code>/* \u5143\u7d20\u51fa\u5806\u7a4d */\nfun pop(): Int {\n // \u5224\u7a7a\u8655\u7406\n if (isEmpty()) throw IndexOutOfBoundsException()\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n swap(0, size() - 1)\n // \u522a\u9664\u7bc0\u9ede\n val _val = maxHeap.removeAt(size() - 1)\n // \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n siftDown(0)\n // \u8fd4\u56de\u5806\u7a4d\u9802\u5143\u7d20\n return _val\n}\n\n/* \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nfun siftDown(it: Int) {\n // Kotlin\u7684\u51fd\u5f0f\u53c3\u6578\u4e0d\u53ef\u8b8a\uff0c\u56e0\u6b64\u5efa\u7acb\u81e8\u6642\u8b8a\u6578\n var i = it\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n val l = left(i)\n val r = right(i)\n var ma = i\n if (l &lt; size() &amp;&amp; maxHeap[l] &gt; maxHeap[ma]) ma = l\n if (r &lt; size() &amp;&amp; maxHeap[r] &gt; maxHeap[ma]) ma = r\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma == i) break\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n swap(i, ma)\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma\n }\n}\n</code></pre> my_heap.rb<pre><code>### \u5143\u7d20\u51fa\u5806\u7a4d ###\ndef pop\n # \u5224\u7a7a\u8655\u7406\n raise IndexError, \"\u5806\u7a4d\u70ba\u7a7a\" if is_empty?\n # \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n swap(0, size - 1)\n # \u522a\u9664\u7bc0\u9ede\n val = @max_heap.pop\n # \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n sift_down(0)\n # \u8fd4\u56de\u5806\u7a4d\u9802\u5143\u7d20\n val\nend\n\n### \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 ###\ndef sift_down(i)\n loop do\n # \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n l, r, ma = left(i), right(i), i\n ma = l if l &lt; size &amp;&amp; @max_heap[l] &gt; @max_heap[ma]\n ma = r if r &lt; size &amp;&amp; @max_heap[r] &gt; @max_heap[ma]\n\n # \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n break if ma == i\n\n # \u4ea4\u63db\u5169\u7bc0\u9ede\n swap(i, ma)\n # \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma\n end\nend\n</code></pre> my_heap.zig<pre><code>// \u5143\u7d20\u51fa\u5806\u7a4d\nfn pop(self: *Self) !T {\n // \u5224\u65b7\u8655\u7406\n if (self.isEmpty()) unreachable;\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n try self.swap(0, self.size() - 1);\n // \u522a\u9664\u7bc0\u9ede\n var val = self.max_heap.?.pop();\n // \u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\n try self.siftDown(0);\n // \u8fd4\u56de\u5806\u7a4d\u9802\u5143\u7d20\n return val;\n} \n\n// \u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\nfn siftDown(self: *Self, i_: usize) !void {\n var i = i_;\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n var l = left(i);\n var r = right(i);\n var ma = i;\n if (l &lt; self.size() and self.max_heap.?.items[l] &gt; self.max_heap.?.items[ma]) ma = l;\n if (r &lt; self.size() and self.max_heap.?.items[r] &gt; self.max_heap.?.items[ma]) ma = r;\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma == i) break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n try self.swap(i, ma);\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_heap/heap/#813","title":"8.1.3 \u00a0 \u5806\u7a4d\u7684\u5e38\u898b\u61c9\u7528","text":"<ul> <li>\u512a\u5148\u4f47\u5217\uff1a\u5806\u7a4d\u901a\u5e38\u4f5c\u70ba\u5be6\u73fe\u512a\u5148\u4f47\u5217\u7684\u9996\u9078\u8cc7\u6599\u7d50\u69cb\uff0c\u5176\u5165\u5217\u548c\u51fa\u5217\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u5747\u70ba \\(O(\\log n)\\) \uff0c\u800c\u5efa\u968a\u64cd\u4f5c\u70ba \\(O(n)\\) \uff0c\u9019\u4e9b\u64cd\u4f5c\u90fd\u975e\u5e38\u9ad8\u6548\u3002</li> <li>\u5806\u7a4d\u6392\u5e8f\uff1a\u7d66\u5b9a\u4e00\u7d44\u8cc7\u6599\uff0c\u6211\u5011\u53ef\u4ee5\u7528\u5b83\u5011\u5efa\u7acb\u4e00\u500b\u5806\u7a4d\uff0c\u7136\u5f8c\u4e0d\u65b7\u5730\u57f7\u884c\u5143\u7d20\u51fa\u5806\u7a4d\u64cd\u4f5c\uff0c\u5f9e\u800c\u5f97\u5230\u6709\u5e8f\u8cc7\u6599\u3002\u7136\u800c\uff0c\u6211\u5011\u901a\u5e38\u6703\u4f7f\u7528\u4e00\u7a2e\u66f4\u512a\u96c5\u7684\u65b9\u5f0f\u5be6\u73fe\u5806\u7a4d\u6392\u5e8f\uff0c\u8a73\u898b\u201c\u5806\u7a4d\u6392\u5e8f\u201d\u7ae0\u7bc0\u3002</li> <li>\u7372\u53d6\u6700\u5927\u7684 \\(k\\) \u500b\u5143\u7d20\uff1a\u9019\u662f\u4e00\u500b\u7d93\u5178\u7684\u6f14\u7b97\u6cd5\u554f\u984c\uff0c\u540c\u6642\u4e5f\u662f\u4e00\u7a2e\u5178\u578b\u61c9\u7528\uff0c\u4f8b\u5982\u9078\u64c7\u71b1\u5ea6\u524d 10 \u7684\u65b0\u805e\u4f5c\u70ba\u5fae\u535a\u71b1\u641c\uff0c\u9078\u53d6\u92b7\u91cf\u524d 10 \u7684\u5546\u54c1\u7b49\u3002</li> </ul>"},{"location":"chapter_heap/summary/","title":"8.4 \u00a0 \u5c0f\u7d50","text":""},{"location":"chapter_heap/summary/#1","title":"1. \u00a0 \u91cd\u9ede\u56de\u9867","text":"<ul> <li>\u5806\u7a4d\u662f\u4e00\u68f5\u5b8c\u5168\u4e8c\u5143\u6a39\uff0c\u6839\u64da\u6210\u7acb\u689d\u4ef6\u53ef\u5206\u70ba\u5927\u9802\u5806\u7a4d\u548c\u5c0f\u9802\u5806\u7a4d\u3002\u5927\uff08\u5c0f\uff09\u9802\u5806\u7a4d\u7684\u5806\u7a4d\u9802\u5143\u7d20\u662f\u6700\u5927\uff08\u5c0f\uff09\u7684\u3002</li> <li>\u512a\u5148\u4f47\u5217\u7684\u5b9a\u7fa9\u662f\u5177\u6709\u51fa\u5217\u512a\u5148\u9806\u5e8f\u7684\u4f47\u5217\uff0c\u901a\u5e38\u4f7f\u7528\u5806\u7a4d\u4f86\u5be6\u73fe\u3002</li> <li>\u5806\u7a4d\u7684\u5e38\u7528\u64cd\u4f5c\u53ca\u5176\u5c0d\u61c9\u7684\u6642\u9593\u8907\u96dc\u5ea6\u5305\u62ec\uff1a\u5143\u7d20\u5165\u5806\u7a4d \\(O(\\log n)\\)\u3001\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d \\(O(\\log n)\\) \u548c\u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 \\(O(1)\\) \u7b49\u3002</li> <li>\u5b8c\u5168\u4e8c\u5143\u6a39\u975e\u5e38\u9069\u5408\u7528\u9663\u5217\u8868\u793a\uff0c\u56e0\u6b64\u6211\u5011\u901a\u5e38\u4f7f\u7528\u9663\u5217\u4f86\u5132\u5b58\u5806\u7a4d\u3002</li> <li>\u5806\u7a4d\u5316\u64cd\u4f5c\u7528\u65bc\u7dad\u8b77\u5806\u7a4d\u7684\u6027\u8cea\uff0c\u5728\u5165\u5806\u7a4d\u548c\u51fa\u5806\u7a4d\u64cd\u4f5c\u4e2d\u90fd\u6703\u7528\u5230\u3002</li> <li>\u8f38\u5165 \\(n\\) \u500b\u5143\u7d20\u4e26\u5efa\u5806\u7a4d\u7684\u6642\u9593\u8907\u96dc\u5ea6\u53ef\u4ee5\u6700\u4f73\u5316\u81f3 \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002</li> <li>Top-k \u662f\u4e00\u500b\u7d93\u5178\u6f14\u7b97\u6cd5\u554f\u984c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5806\u7a4d\u8cc7\u6599\u7d50\u69cb\u9ad8\u6548\u89e3\u6c7a\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n \\log k)\\) \u3002</li> </ul>"},{"location":"chapter_heap/summary/#2-q-a","title":"2. \u00a0 Q &amp; A","text":"<p>Q\uff1a\u8cc7\u6599\u7d50\u69cb\u7684\u201c\u5806\u7a4d\u201d\u8207\u8a18\u61b6\u9ad4\u7ba1\u7406\u7684\u201c\u5806\u7a4d\u201d\u662f\u540c\u4e00\u500b\u6982\u5ff5\u55ce\uff1f</p> <p>\u5169\u8005\u4e0d\u662f\u540c\u4e00\u500b\u6982\u5ff5\uff0c\u53ea\u662f\u78b0\u5de7\u90fd\u53eb\u201c\u5806\u7a4d\u201d\u3002\u8a08\u7b97\u6a5f\u7cfb\u7d71\u8a18\u61b6\u9ad4\u4e2d\u7684\u5806\u7a4d\u662f\u52d5\u614b\u8a18\u61b6\u9ad4\u5206\u914d\u7684\u4e00\u90e8\u5206\uff0c\u7a0b\u5f0f\u5728\u57f7\u884c\u6642\u53ef\u4ee5\u4f7f\u7528\u5b83\u4f86\u5132\u5b58\u8cc7\u6599\u3002\u7a0b\u5f0f\u53ef\u4ee5\u8acb\u6c42\u4e00\u5b9a\u91cf\u7684\u5806\u7a4d\u8a18\u61b6\u9ad4\uff0c\u7528\u65bc\u5132\u5b58\u5982\u7269\u4ef6\u548c\u9663\u5217\u7b49\u8907\u96dc\u7d50\u69cb\u3002\u7576\u9019\u4e9b\u8cc7\u6599\u4e0d\u518d\u9700\u8981\u6642\uff0c\u7a0b\u5f0f\u9700\u8981\u91cb\u653e\u9019\u4e9b\u8a18\u61b6\u9ad4\uff0c\u4ee5\u9632\u6b62\u8a18\u61b6\u9ad4\u6d41\u5931\u3002\u76f8\u8f03\u65bc\u5806\u758a\u8a18\u61b6\u9ad4\uff0c\u5806\u7a4d\u8a18\u61b6\u9ad4\u7684\u7ba1\u7406\u548c\u4f7f\u7528\u9700\u8981\u66f4\u8b39\u614e\uff0c\u4f7f\u7528\u4e0d\u7576\u53ef\u80fd\u6703\u5c0e\u81f4\u8a18\u61b6\u9ad4\u6d41\u5931\u548c\u91ce\u6307\u6a19\u7b49\u554f\u984c\u3002</p>"},{"location":"chapter_heap/top_k/","title":"8.3 \u00a0 Top-k \u554f\u984c","text":"<p>Question</p> <p>\u7d66\u5b9a\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u7121\u5e8f\u9663\u5217 <code>nums</code> \uff0c\u8acb\u8fd4\u56de\u9663\u5217\u4e2d\u6700\u5927\u7684 \\(k\\) \u500b\u5143\u7d20\u3002</p> <p>\u5c0d\u65bc\u8a72\u554f\u984c\uff0c\u6211\u5011\u5148\u4ecb\u7d39\u5169\u7a2e\u601d\u8def\u6bd4\u8f03\u76f4\u63a5\u7684\u89e3\u6cd5\uff0c\u518d\u4ecb\u7d39\u6548\u7387\u66f4\u9ad8\u7684\u5806\u7a4d\u89e3\u6cd5\u3002</p>"},{"location":"chapter_heap/top_k/#831","title":"8.3.1 \u00a0 \u65b9\u6cd5\u4e00\uff1a\u8d70\u8a2a\u9078\u64c7","text":"<p>\u6211\u5011\u53ef\u4ee5\u9032\u884c\u5716 8-6 \u6240\u793a\u7684 \\(k\\) \u8f2a\u8d70\u8a2a\uff0c\u5206\u5225\u5728\u6bcf\u8f2a\u4e2d\u63d0\u53d6\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(\\dots\\)\u3001\\(k\\) \u5927\u7684\u5143\u7d20\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(nk)\\) \u3002</p> <p>\u6b64\u65b9\u6cd5\u53ea\u9069\u7528\u65bc \\(k \\ll n\\) \u7684\u60c5\u6cc1\uff0c\u56e0\u70ba\u7576 \\(k\\) \u8207 \\(n\\) \u6bd4\u8f03\u63a5\u8fd1\u6642\uff0c\u5176\u6642\u9593\u8907\u96dc\u5ea6\u8da8\u5411\u65bc \\(O(n^2)\\) \uff0c\u975e\u5e38\u8017\u6642\u3002</p> <p></p> <p> \u5716 8-6 \u00a0 \u8d70\u8a2a\u5c0b\u627e\u6700\u5927\u7684 k \u500b\u5143\u7d20 </p> <p>Tip</p> <p>\u7576 \\(k = n\\) \u6642\uff0c\u6211\u5011\u53ef\u4ee5\u5f97\u5230\u5b8c\u6574\u7684\u6709\u5e8f\u5e8f\u5217\uff0c\u6b64\u6642\u7b49\u50f9\u65bc\u201c\u9078\u64c7\u6392\u5e8f\u201d\u6f14\u7b97\u6cd5\u3002</p>"},{"location":"chapter_heap/top_k/#832","title":"8.3.2 \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u6392\u5e8f","text":"<p>\u5982\u5716 8-7 \u6240\u793a\uff0c\u6211\u5011\u53ef\u4ee5\u5148\u5c0d\u9663\u5217 <code>nums</code> \u9032\u884c\u6392\u5e8f\uff0c\u518d\u8fd4\u56de\u6700\u53f3\u908a\u7684 \\(k\\) \u500b\u5143\u7d20\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n \\log n)\\) \u3002</p> <p>\u986f\u7136\uff0c\u8a72\u65b9\u6cd5\u201c\u8d85\u984d\u201d\u5b8c\u6210\u4efb\u52d9\u4e86\uff0c\u56e0\u70ba\u6211\u5011\u53ea\u9700\u627e\u51fa\u6700\u5927\u7684 \\(k\\) \u500b\u5143\u7d20\u5373\u53ef\uff0c\u800c\u4e0d\u9700\u8981\u6392\u5e8f\u5176\u4ed6\u5143\u7d20\u3002</p> <p></p> <p> \u5716 8-7 \u00a0 \u6392\u5e8f\u5c0b\u627e\u6700\u5927\u7684 k \u500b\u5143\u7d20 </p>"},{"location":"chapter_heap/top_k/#833","title":"8.3.3 \u00a0 \u65b9\u6cd5\u4e09\uff1a\u5806\u7a4d","text":"<p>\u6211\u5011\u53ef\u4ee5\u57fa\u65bc\u5806\u7a4d\u66f4\u52a0\u9ad8\u6548\u5730\u89e3\u6c7a Top-k \u554f\u984c\uff0c\u6d41\u7a0b\u5982\u5716 8-8 \u6240\u793a\u3002</p> <ol> <li>\u521d\u59cb\u5316\u4e00\u500b\u5c0f\u9802\u5806\u7a4d\uff0c\u5176\u5806\u7a4d\u9802\u5143\u7d20\u6700\u5c0f\u3002</li> <li>\u5148\u5c07\u9663\u5217\u7684\u524d \\(k\\) \u500b\u5143\u7d20\u4f9d\u6b21\u5165\u5806\u7a4d\u3002</li> <li>\u5f9e\u7b2c \\(k + 1\\) \u500b\u5143\u7d20\u958b\u59cb\uff0c\u82e5\u7576\u524d\u5143\u7d20\u5927\u65bc\u5806\u7a4d\u9802\u5143\u7d20\uff0c\u5247\u5c07\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\uff0c\u4e26\u5c07\u7576\u524d\u5143\u7d20\u5165\u5806\u7a4d\u3002</li> <li>\u8d70\u8a2a\u5b8c\u6210\u5f8c\uff0c\u5806\u7a4d\u4e2d\u5132\u5b58\u7684\u5c31\u662f\u6700\u5927\u7684 \\(k\\) \u500b\u5143\u7d20\u3002</li> </ol> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 8-8 \u00a0 \u57fa\u65bc\u5806\u7a4d\u5c0b\u627e\u6700\u5927\u7684 k \u500b\u5143\u7d20 </p> <p>\u793a\u4f8b\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig top_k.py<pre><code>def top_k_heap(nums: list[int], k: int) -&gt; list[int]:\n \"\"\"\u57fa\u65bc\u5806\u7a4d\u67e5\u8a62\u9663\u5217\u4e2d\u6700\u5927\u7684 k \u500b\u5143\u7d20\"\"\"\n # \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\n heap = []\n # \u5c07\u9663\u5217\u7684\u524d k \u500b\u5143\u7d20\u5165\u5806\u7a4d\n for i in range(k):\n heapq.heappush(heap, nums[i])\n # \u5f9e\u7b2c k+1 \u500b\u5143\u7d20\u958b\u59cb\uff0c\u4fdd\u6301\u5806\u7a4d\u7684\u9577\u5ea6\u70ba k\n for i in range(k, len(nums)):\n # \u82e5\u7576\u524d\u5143\u7d20\u5927\u65bc\u5806\u7a4d\u9802\u5143\u7d20\uff0c\u5247\u5c07\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u3001\u7576\u524d\u5143\u7d20\u5165\u5806\u7a4d\n if nums[i] &gt; heap[0]:\n heapq.heappop(heap)\n heapq.heappush(heap, nums[i])\n return heap\n</code></pre> top_k.cpp<pre><code>/* \u57fa\u65bc\u5806\u7a4d\u67e5\u8a62\u9663\u5217\u4e2d\u6700\u5927\u7684 k \u500b\u5143\u7d20 */\npriority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; topKHeap(vector&lt;int&gt; &amp;nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\n priority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; heap;\n // \u5c07\u9663\u5217\u7684\u524d k \u500b\u5143\u7d20\u5165\u5806\u7a4d\n for (int i = 0; i &lt; k; i++) {\n heap.push(nums[i]);\n }\n // \u5f9e\u7b2c k+1 \u500b\u5143\u7d20\u958b\u59cb\uff0c\u4fdd\u6301\u5806\u7a4d\u7684\u9577\u5ea6\u70ba k\n for (int i = k; i &lt; nums.size(); i++) {\n // \u82e5\u7576\u524d\u5143\u7d20\u5927\u65bc\u5806\u7a4d\u9802\u5143\u7d20\uff0c\u5247\u5c07\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u3001\u7576\u524d\u5143\u7d20\u5165\u5806\u7a4d\n if (nums[i] &gt; heap.top()) {\n heap.pop();\n heap.push(nums[i]);\n }\n }\n return heap;\n}\n</code></pre> top_k.java<pre><code>/* \u57fa\u65bc\u5806\u7a4d\u67e5\u8a62\u9663\u5217\u4e2d\u6700\u5927\u7684 k \u500b\u5143\u7d20 */\nQueue&lt;Integer&gt; topKHeap(int[] nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\n Queue&lt;Integer&gt; heap = new PriorityQueue&lt;Integer&gt;();\n // \u5c07\u9663\u5217\u7684\u524d k \u500b\u5143\u7d20\u5165\u5806\u7a4d\n for (int i = 0; i &lt; k; i++) {\n heap.offer(nums[i]);\n }\n // \u5f9e\u7b2c k+1 \u500b\u5143\u7d20\u958b\u59cb\uff0c\u4fdd\u6301\u5806\u7a4d\u7684\u9577\u5ea6\u70ba k\n for (int i = k; i &lt; nums.length; i++) {\n // \u82e5\u7576\u524d\u5143\u7d20\u5927\u65bc\u5806\u7a4d\u9802\u5143\u7d20\uff0c\u5247\u5c07\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u3001\u7576\u524d\u5143\u7d20\u5165\u5806\u7a4d\n if (nums[i] &gt; heap.peek()) {\n heap.poll();\n heap.offer(nums[i]);\n }\n }\n return heap;\n}\n</code></pre> top_k.cs<pre><code>/* \u57fa\u65bc\u5806\u7a4d\u67e5\u8a62\u9663\u5217\u4e2d\u6700\u5927\u7684 k \u500b\u5143\u7d20 */\nPriorityQueue&lt;int, int&gt; TopKHeap(int[] nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\n PriorityQueue&lt;int, int&gt; heap = new();\n // \u5c07\u9663\u5217\u7684\u524d k \u500b\u5143\u7d20\u5165\u5806\u7a4d\n for (int i = 0; i &lt; k; i++) {\n heap.Enqueue(nums[i], nums[i]);\n }\n // \u5f9e\u7b2c k+1 \u500b\u5143\u7d20\u958b\u59cb\uff0c\u4fdd\u6301\u5806\u7a4d\u7684\u9577\u5ea6\u70ba k\n for (int i = k; i &lt; nums.Length; i++) {\n // \u82e5\u7576\u524d\u5143\u7d20\u5927\u65bc\u5806\u7a4d\u9802\u5143\u7d20\uff0c\u5247\u5c07\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u3001\u7576\u524d\u5143\u7d20\u5165\u5806\u7a4d\n if (nums[i] &gt; heap.Peek()) {\n heap.Dequeue();\n heap.Enqueue(nums[i], nums[i]);\n }\n }\n return heap;\n}\n</code></pre> top_k.go<pre><code>/* \u57fa\u65bc\u5806\u7a4d\u67e5\u8a62\u9663\u5217\u4e2d\u6700\u5927\u7684 k \u500b\u5143\u7d20 */\nfunc topKHeap(nums []int, k int) *minHeap {\n // \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\n h := &amp;minHeap{}\n heap.Init(h)\n // \u5c07\u9663\u5217\u7684\u524d k \u500b\u5143\u7d20\u5165\u5806\u7a4d\n for i := 0; i &lt; k; i++ {\n heap.Push(h, nums[i])\n }\n // \u5f9e\u7b2c k+1 \u500b\u5143\u7d20\u958b\u59cb\uff0c\u4fdd\u6301\u5806\u7a4d\u7684\u9577\u5ea6\u70ba k\n for i := k; i &lt; len(nums); i++ {\n // \u82e5\u7576\u524d\u5143\u7d20\u5927\u65bc\u5806\u7a4d\u9802\u5143\u7d20\uff0c\u5247\u5c07\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u3001\u7576\u524d\u5143\u7d20\u5165\u5806\u7a4d\n if nums[i] &gt; h.Top().(int) {\n heap.Pop(h)\n heap.Push(h, nums[i])\n }\n }\n return h\n}\n</code></pre> top_k.swift<pre><code>/* \u57fa\u65bc\u5806\u7a4d\u67e5\u8a62\u9663\u5217\u4e2d\u6700\u5927\u7684 k \u500b\u5143\u7d20 */\nfunc topKHeap(nums: [Int], k: Int) -&gt; [Int] {\n // \u521d\u59cb\u5316\u4e00\u500b\u5c0f\u9802\u5806\u7a4d\uff0c\u4e26\u5c07\u524d k \u500b\u5143\u7d20\u5efa\u5806\u7a4d\n var heap = Heap(nums.prefix(k))\n // \u5f9e\u7b2c k+1 \u500b\u5143\u7d20\u958b\u59cb\uff0c\u4fdd\u6301\u5806\u7a4d\u7684\u9577\u5ea6\u70ba k\n for i in nums.indices.dropFirst(k) {\n // \u82e5\u7576\u524d\u5143\u7d20\u5927\u65bc\u5806\u7a4d\u9802\u5143\u7d20\uff0c\u5247\u5c07\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u3001\u7576\u524d\u5143\u7d20\u5165\u5806\u7a4d\n if nums[i] &gt; heap.min()! {\n _ = heap.removeMin()\n heap.insert(nums[i])\n }\n }\n return heap.unordered\n}\n</code></pre> top_k.js<pre><code>/* \u5143\u7d20\u5165\u5806\u7a4d */\nfunction pushMinHeap(maxHeap, val) {\n // \u5143\u7d20\u53d6\u53cd\n maxHeap.push(-val);\n}\n\n/* \u5143\u7d20\u51fa\u5806\u7a4d */\nfunction popMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.pop();\n}\n\n/* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\nfunction peekMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.peek();\n}\n\n/* \u53d6\u51fa\u5806\u7a4d\u4e2d\u5143\u7d20 */\nfunction getMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return maxHeap.getMaxHeap().map((num) =&gt; -num);\n}\n\n/* \u57fa\u65bc\u5806\u7a4d\u67e5\u8a62\u9663\u5217\u4e2d\u6700\u5927\u7684 k \u500b\u5143\u7d20 */\nfunction topKHeap(nums, k) {\n // \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\n // \u8acb\u6ce8\u610f\uff1a\u6211\u5011\u5c07\u5806\u7a4d\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u5f9e\u800c\u7528\u5927\u9802\u5806\u7a4d\u4f86\u6a21\u64ec\u5c0f\u9802\u5806\u7a4d\n const maxHeap = new MaxHeap([]);\n // \u5c07\u9663\u5217\u7684\u524d k \u500b\u5143\u7d20\u5165\u5806\u7a4d\n for (let i = 0; i &lt; k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u5f9e\u7b2c k+1 \u500b\u5143\u7d20\u958b\u59cb\uff0c\u4fdd\u6301\u5806\u7a4d\u7684\u9577\u5ea6\u70ba k\n for (let i = k; i &lt; nums.length; i++) {\n // \u82e5\u7576\u524d\u5143\u7d20\u5927\u65bc\u5806\u7a4d\u9802\u5143\u7d20\uff0c\u5247\u5c07\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u3001\u7576\u524d\u5143\u7d20\u5165\u5806\u7a4d\n if (nums[i] &gt; peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n // \u8fd4\u56de\u5806\u7a4d\u4e2d\u5143\u7d20\n return getMinHeap(maxHeap);\n}\n</code></pre> top_k.ts<pre><code>/* \u5143\u7d20\u5165\u5806\u7a4d */\nfunction pushMinHeap(maxHeap: MaxHeap, val: number): void {\n // \u5143\u7d20\u53d6\u53cd\n maxHeap.push(-val);\n}\n\n/* \u5143\u7d20\u51fa\u5806\u7a4d */\nfunction popMinHeap(maxHeap: MaxHeap): number {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.pop();\n}\n\n/* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\nfunction peekMinHeap(maxHeap: MaxHeap): number {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.peek();\n}\n\n/* \u53d6\u51fa\u5806\u7a4d\u4e2d\u5143\u7d20 */\nfunction getMinHeap(maxHeap: MaxHeap): number[] {\n // \u5143\u7d20\u53d6\u53cd\n return maxHeap.getMaxHeap().map((num: number) =&gt; -num);\n}\n\n/* \u57fa\u65bc\u5806\u7a4d\u67e5\u8a62\u9663\u5217\u4e2d\u6700\u5927\u7684 k \u500b\u5143\u7d20 */\nfunction topKHeap(nums: number[], k: number): number[] {\n // \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\n // \u8acb\u6ce8\u610f\uff1a\u6211\u5011\u5c07\u5806\u7a4d\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u5f9e\u800c\u7528\u5927\u9802\u5806\u7a4d\u4f86\u6a21\u64ec\u5c0f\u9802\u5806\u7a4d\n const maxHeap = new MaxHeap([]);\n // \u5c07\u9663\u5217\u7684\u524d k \u500b\u5143\u7d20\u5165\u5806\u7a4d\n for (let i = 0; i &lt; k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u5f9e\u7b2c k+1 \u500b\u5143\u7d20\u958b\u59cb\uff0c\u4fdd\u6301\u5806\u7a4d\u7684\u9577\u5ea6\u70ba k\n for (let i = k; i &lt; nums.length; i++) {\n // \u82e5\u7576\u524d\u5143\u7d20\u5927\u65bc\u5806\u7a4d\u9802\u5143\u7d20\uff0c\u5247\u5c07\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u3001\u7576\u524d\u5143\u7d20\u5165\u5806\u7a4d\n if (nums[i] &gt; peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n // \u8fd4\u56de\u5806\u7a4d\u4e2d\u5143\u7d20\n return getMinHeap(maxHeap);\n}\n</code></pre> top_k.dart<pre><code>/* \u57fa\u65bc\u5806\u7a4d\u67e5\u8a62\u9663\u5217\u4e2d\u6700\u5927\u7684 k \u500b\u5143\u7d20 */\nMinHeap topKHeap(List&lt;int&gt; nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\uff0c\u5c07\u9663\u5217\u7684\u524d k \u500b\u5143\u7d20\u5165\u5806\u7a4d\n MinHeap heap = MinHeap(nums.sublist(0, k));\n // \u5f9e\u7b2c k+1 \u500b\u5143\u7d20\u958b\u59cb\uff0c\u4fdd\u6301\u5806\u7a4d\u7684\u9577\u5ea6\u70ba k\n for (int i = k; i &lt; nums.length; i++) {\n // \u82e5\u7576\u524d\u5143\u7d20\u5927\u65bc\u5806\u7a4d\u9802\u5143\u7d20\uff0c\u5247\u5c07\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u3001\u7576\u524d\u5143\u7d20\u5165\u5806\u7a4d\n if (nums[i] &gt; heap.peek()) {\n heap.pop();\n heap.push(nums[i]);\n }\n }\n return heap;\n}\n</code></pre> top_k.rs<pre><code>/* \u57fa\u65bc\u5806\u7a4d\u67e5\u8a62\u9663\u5217\u4e2d\u6700\u5927\u7684 k \u500b\u5143\u7d20 */\nfn top_k_heap(nums: Vec&lt;i32&gt;, k: usize) -&gt; BinaryHeap&lt;Reverse&lt;i32&gt;&gt; {\n // BinaryHeap \u662f\u5927\u9802\u5806\u7a4d\uff0c\u4f7f\u7528 Reverse \u5c07\u5143\u7d20\u53d6\u53cd\uff0c\u5f9e\u800c\u5be6\u73fe\u5c0f\u9802\u5806\u7a4d\n let mut heap = BinaryHeap::&lt;Reverse&lt;i32&gt;&gt;::new();\n // \u5c07\u9663\u5217\u7684\u524d k \u500b\u5143\u7d20\u5165\u5806\u7a4d\n for &amp;num in nums.iter().take(k) {\n heap.push(Reverse(num));\n }\n // \u5f9e\u7b2c k+1 \u500b\u5143\u7d20\u958b\u59cb\uff0c\u4fdd\u6301\u5806\u7a4d\u7684\u9577\u5ea6\u70ba k\n for &amp;num in nums.iter().skip(k) {\n // \u82e5\u7576\u524d\u5143\u7d20\u5927\u65bc\u5806\u7a4d\u9802\u5143\u7d20\uff0c\u5247\u5c07\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u3001\u7576\u524d\u5143\u7d20\u5165\u5806\u7a4d\n if num &gt; heap.peek().unwrap().0 {\n heap.pop();\n heap.push(Reverse(num));\n }\n }\n heap\n}\n</code></pre> top_k.c<pre><code>/* \u5143\u7d20\u5165\u5806\u7a4d */\nvoid pushMinHeap(MaxHeap *maxHeap, int val) {\n // \u5143\u7d20\u53d6\u53cd\n push(maxHeap, -val);\n}\n\n/* \u5143\u7d20\u51fa\u5806\u7a4d */\nint popMinHeap(MaxHeap *maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -pop(maxHeap);\n}\n\n/* \u8a2a\u554f\u5806\u7a4d\u9802\u5143\u7d20 */\nint peekMinHeap(MaxHeap *maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -peek(maxHeap);\n}\n\n/* \u53d6\u51fa\u5806\u7a4d\u4e2d\u5143\u7d20 */\nint *getMinHeap(MaxHeap *maxHeap) {\n // \u5c07\u5806\u7a4d\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\u4e26\u5b58\u5165 res \u9663\u5217\n int *res = (int *)malloc(maxHeap-&gt;size * sizeof(int));\n for (int i = 0; i &lt; maxHeap-&gt;size; i++) {\n res[i] = -maxHeap-&gt;data[i];\n }\n return res;\n}\n\n/* \u53d6\u51fa\u5806\u7a4d\u4e2d\u5143\u7d20 */\nint *getMinHeap(MaxHeap *maxHeap) {\n // \u5c07\u5806\u7a4d\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\u4e26\u5b58\u5165 res \u9663\u5217\n int *res = (int *)malloc(maxHeap-&gt;size * sizeof(int));\n for (int i = 0; i &lt; maxHeap-&gt;size; i++) {\n res[i] = -maxHeap-&gt;data[i];\n }\n return res;\n}\n\n// \u57fa\u65bc\u5806\u7a4d\u67e5\u8a62\u9663\u5217\u4e2d\u6700\u5927\u7684 k \u500b\u5143\u7d20\u7684\u51fd\u5f0f\nint *topKHeap(int *nums, int sizeNums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\n // \u8acb\u6ce8\u610f\uff1a\u6211\u5011\u5c07\u5806\u7a4d\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u5f9e\u800c\u7528\u5927\u9802\u5806\u7a4d\u4f86\u6a21\u64ec\u5c0f\u9802\u5806\u7a4d\n int *empty = (int *)malloc(0);\n MaxHeap *maxHeap = newMaxHeap(empty, 0);\n // \u5c07\u9663\u5217\u7684\u524d k \u500b\u5143\u7d20\u5165\u5806\u7a4d\n for (int i = 0; i &lt; k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u5f9e\u7b2c k+1 \u500b\u5143\u7d20\u958b\u59cb\uff0c\u4fdd\u6301\u5806\u7a4d\u7684\u9577\u5ea6\u70ba k\n for (int i = k; i &lt; sizeNums; i++) {\n // \u82e5\u7576\u524d\u5143\u7d20\u5927\u65bc\u5806\u7a4d\u9802\u5143\u7d20\uff0c\u5247\u5c07\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u3001\u7576\u524d\u5143\u7d20\u5165\u5806\u7a4d\n if (nums[i] &gt; peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n int *res = getMinHeap(maxHeap);\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n delMaxHeap(maxHeap);\n return res;\n}\n</code></pre> top_k.kt<pre><code>/* \u57fa\u65bc\u5806\u7a4d\u67e5\u8a62\u9663\u5217\u4e2d\u6700\u5927\u7684 k \u500b\u5143\u7d20 */\nfun topKHeap(nums: IntArray, k: Int): Queue&lt;Int&gt; {\n // \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\n val heap = PriorityQueue&lt;Int&gt;()\n // \u5c07\u9663\u5217\u7684\u524d k \u500b\u5143\u7d20\u5165\u5806\u7a4d\n for (i in 0..&lt;k) {\n heap.offer(nums[i])\n }\n // \u5f9e\u7b2c k+1 \u500b\u5143\u7d20\u958b\u59cb\uff0c\u4fdd\u6301\u5806\u7a4d\u7684\u9577\u5ea6\u70ba k\n for (i in k..&lt;nums.size) {\n // \u82e5\u7576\u524d\u5143\u7d20\u5927\u65bc\u5806\u7a4d\u9802\u5143\u7d20\uff0c\u5247\u5c07\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u3001\u7576\u524d\u5143\u7d20\u5165\u5806\u7a4d\n if (nums[i] &gt; heap.peek()) {\n heap.poll()\n heap.offer(nums[i])\n }\n }\n return heap\n}\n</code></pre> top_k.rb<pre><code>### \u57fa\u65bc\u5806\u7a4d\u67e5\u8a62\u9663\u5217\u4e2d\u6700\u5927\u7684 k \u500b\u5143\u7d20 ###\ndef top_k_heap(nums, k)\n # \u521d\u59cb\u5316\u5c0f\u9802\u5806\u7a4d\n # \u8acb\u6ce8\u610f\uff1a\u6211\u5011\u5c07\u5806\u7a4d\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u5f9e\u800c\u7528\u5927\u9802\u5806\u7a4d\u4f86\u6a21\u64ec\u5c0f\u9802\u5806\u7a4d\n max_heap = MaxHeap.new([])\n\n # \u5c07\u9663\u5217\u7684\u524d k \u500b\u5143\u7d20\u5165\u5806\u7a4d\n for i in 0...k\n push_min_heap(max_heap, nums[i])\n end\n\n # \u5f9e\u7b2c k+1 \u500b\u5143\u7d20\u958b\u59cb\uff0c\u4fdd\u6301\u5806\u7a4d\u7684\u9577\u5ea6\u70ba k\n for i in k...nums.length\n # \u82e5\u7576\u524d\u5143\u7d20\u5927\u65bc\u5806\u7a4d\u9802\u5143\u7d20\uff0c\u5247\u5c07\u5806\u7a4d\u9802\u5143\u7d20\u51fa\u5806\u7a4d\u3001\u7576\u524d\u5143\u7d20\u5165\u5806\u7a4d\n if nums[i] &gt; peek_min_heap(max_heap)\n pop_min_heap(max_heap)\n push_min_heap(max_heap, nums[i])\n end\n end\n\n get_min_heap(max_heap)\nend\n</code></pre> top_k.zig<pre><code>[class]{}-[func]{topKHeap}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u7e3d\u5171\u57f7\u884c\u4e86 \\(n\\) \u8f2a\u5165\u5806\u7a4d\u548c\u51fa\u5806\u7a4d\uff0c\u5806\u7a4d\u7684\u6700\u5927\u9577\u5ea6\u70ba \\(k\\) \uff0c\u56e0\u6b64\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n \\log k)\\) \u3002\u8a72\u65b9\u6cd5\u7684\u6548\u7387\u5f88\u9ad8\uff0c\u7576 \\(k\\) \u8f03\u5c0f\u6642\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u8da8\u5411 \\(O(n)\\) \uff1b\u7576 \\(k\\) \u8f03\u5927\u6642\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u4e0d\u6703\u8d85\u904e \\(O(n \\log n)\\) \u3002</p> <p>\u53e6\u5916\uff0c\u8a72\u65b9\u6cd5\u9069\u7528\u65bc\u52d5\u614b\u8cc7\u6599\u6d41\u7684\u4f7f\u7528\u5834\u666f\u3002\u5728\u4e0d\u65b7\u52a0\u5165\u8cc7\u6599\u6642\uff0c\u6211\u5011\u53ef\u4ee5\u6301\u7e8c\u7dad\u8b77\u5806\u7a4d\u5167\u7684\u5143\u7d20\uff0c\u5f9e\u800c\u5be6\u73fe\u6700\u5927\u7684 \\(k\\) \u500b\u5143\u7d20\u7684\u52d5\u614b\u66f4\u65b0\u3002</p>"},{"location":"chapter_hello_algo/","title":"\u5e8f","text":"<p>\u5e7e\u5e74\u524d\uff0c\u6211\u5728\u529b\u6263\u4e0a\u5206\u4eab\u4e86\u201c\u528d\u6307 Offer\u201d\u7cfb\u5217\u984c\u89e3\uff0c\u53d7\u5230\u4e86\u8a31\u591a\u8b80\u8005\u7684\u9f13\u52f5\u548c\u652f\u6301\u3002\u5728\u8207\u8b80\u8005\u4ea4\u6d41\u671f\u9593\uff0c\u6211\u6700\u5e38\u88ab\u554f\u7684\u4e00\u500b\u554f\u984c\u662f\u201c\u5982\u4f55\u5165\u9580\u6f14\u7b97\u6cd5\u201d\u3002\u9010\u6f38\u5730\uff0c\u6211\u5c0d\u9019\u500b\u554f\u984c\u7522\u751f\u4e86\u6fc3\u539a\u7684\u8208\u8da3\u3002</p> <p>\u5169\u773c\u4e00\u62b9\u9ed1\u5730\u5237\u984c\u4f3c\u4e4e\u662f\u6700\u53d7\u6b61\u8fce\u7684\u65b9\u6cd5\uff0c\u7c21\u55ae\u3001\u76f4\u63a5\u4e14\u6709\u6548\u3002\u7136\u800c\u5237\u984c\u5c31\u5982\u540c\u73a9\u201c\u6383\u96f7\u201d\u904a\u6232\uff0c\u81ea\u5b78\u80fd\u529b\u5f37\u7684\u4eba\u80fd\u5920\u9806\u5229\u5c07\u5730\u96f7\u9010\u500b\u6392\u6389\uff0c\u800c\u57fa\u790e\u4e0d\u8db3\u7684\u4eba\u5f88\u53ef\u80fd\u88ab\u70b8\u5f97\u6eff\u982d\u662f\u5305\uff0c\u4e26\u5728\u632b\u6298\u4e2d\u6b65\u6b65\u9000\u7e2e\u3002\u901a\u8b80\u6559\u6750\u4e5f\u662f\u4e00\u7a2e\u5e38\u898b\u505a\u6cd5\uff0c\u4f46\u5c0d\u65bc\u9762\u5411\u6c42\u8077\u7684\u4eba\u4f86\u8aaa\uff0c\u7562\u696d\u8ad6\u6587\u3001\u6295\u905e\u7c21\u6b77\u3001\u6e96\u5099\u7b46\u8a66\u548c\u9762\u8a66\u5df2\u7d93\u6d88\u8017\u4e86\u5927\u90e8\u5206\u7cbe\u529b\uff0c\u5543\u539a\u91cd\u7684\u66f8\u5f80\u5f80\u8b8a\u6210\u4e86\u4e00\u9805\u8271\u9245\u7684\u6311\u6230\u3002</p> <p>\u5982\u679c\u4f60\u4e5f\u9762\u81e8\u985e\u4f3c\u7684\u56f0\u64fe\uff0c\u90a3\u9ebc\u5f88\u5e78\u904b\u9019\u672c\u66f8\u201c\u627e\u201d\u5230\u4e86\u4f60\u3002\u672c\u66f8\u662f\u6211\u5c0d\u9019\u500b\u554f\u984c\u7d66\u51fa\u7684\u7b54\u6848\uff0c\u5373\u4f7f\u4e0d\u662f\u6700\u512a\u89e3\uff0c\u4e5f\u81f3\u5c11\u662f\u4e00\u6b21\u7a4d\u6975\u7684\u5617\u8a66\u3002\u672c\u66f8\u96d6\u7136\u4e0d\u8db3\u4ee5\u8b93\u4f60\u76f4\u63a5\u62ff\u5230 Offer\uff0c\u4f46\u6703\u5f15\u5c0e\u4f60\u63a2\u7d22\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u7684\u201c\u77e5\u8b58\u5730\u5716\u201d\uff0c\u5e36\u4f60\u77ad\u89e3\u4e0d\u540c\u201c\u5730\u96f7\u201d\u7684\u5f62\u72c0\u3001\u5927\u5c0f\u548c\u5206\u4f48\u4f4d\u7f6e\uff0c\u8b93\u4f60\u638c\u63e1\u5404\u7a2e\u201c\u6392\u96f7\u65b9\u6cd5\u201d\u3002\u6709\u4e86\u9019\u4e9b\u672c\u9818\uff0c\u76f8\u4fe1\u4f60\u53ef\u4ee5\u66f4\u52a0\u81ea\u5982\u5730\u5237\u984c\u548c\u95b1\u8b80\u6587\u737b\uff0c\u9010\u6b65\u69cb\u5efa\u8d77\u5b8c\u6574\u7684\u77e5\u8b58\u9ad4\u7cfb\u3002</p> <p>\u6211\u6df1\u6df1\u8d0a\u540c\u8cbb\u66fc\u6559\u6388\u6240\u8a00\uff1a\u201cKnowledge isn't free. You have to pay attention.\u201d\u5f9e\u9019\u500b\u610f\u7fa9\u4e0a\u770b\uff0c\u9019\u672c\u66f8\u4e26\u975e\u5b8c\u5168\u201c\u514d\u8cbb\u201d\u3002\u70ba\u4e86\u4e0d\u8f9c\u8ca0\u4f60\u70ba\u672c\u66f8\u6240\u4ed8\u51fa\u7684\u5bf6\u8cb4\u201c\u6ce8\u610f\u529b\u201d\uff0c\u6211\u6703\u7aed\u76e1\u6240\u80fd\uff0c\u6295\u5165\u6700\u5927\u7684\u201c\u6ce8\u610f\u529b\u201d\u4f86\u5b8c\u6210\u672c\u66f8\u7684\u5275\u4f5c\u3002</p> <p>\u672c\u4eba\u81ea\u77e5\u5b78\u758f\u624d\u6dfa\uff0c\u66f8\u4e2d\u5167\u5bb9\u96d6\u7136\u5df2\u7d93\u904e\u4e00\u6bb5\u6642\u9593\u7684\u6253\u78e8\uff0c\u4f46\u4e00\u5b9a\u4ecd\u6709\u8a31\u591a\u932f\u8aa4\uff0c\u61c7\u8acb\u5404\u4f4d\u8001\u5e2b\u548c\u540c\u5b78\u6279\u8a55\u6307\u6b63\u3002</p> <p></p> Hello\uff0c\u6f14\u7b97\u6cd5\uff01 <p>\u8a08\u7b97\u6a5f\u7684\u51fa\u73fe\u7d66\u4e16\u754c\u5e36\u4f86\u4e86\u5de8\u5927\u8b8a\u9769\uff0c\u5b83\u6191\u85c9\u9ad8\u901f\u7684\u8a08\u7b97\u80fd\u529b\u548c\u51fa\u8272\u7684\u53ef\u7a0b\u5f0f\u8a2d\u8a08\u6027\uff0c\u6210\u70ba\u4e86\u57f7\u884c\u6f14\u7b97\u6cd5\u8207\u8655\u7406\u8cc7\u6599\u7684\u7406\u60f3\u5a92\u4ecb\u3002\u7121\u8ad6\u662f\u96fb\u5b50\u904a\u6232\u7684\u903c\u771f\u756b\u9762\u3001\u81ea\u52d5\u99d5\u99db\u7684\u667a\u6167\u6c7a\u7b56\uff0c\u9084\u662f AlphaGo \u7684\u7cbe\u5f69\u68cb\u5c40\u3001ChatGPT \u7684\u81ea\u7136\u4e92\u52d5\uff0c\u9019\u4e9b\u61c9\u7528\u90fd\u662f\u6f14\u7b97\u6cd5\u5728\u8a08\u7b97\u6a5f\u4e0a\u7684\u7cbe\u5999\u6f14\u7e79\u3002</p> <p>\u4e8b\u5be6\u4e0a\uff0c\u5728\u8a08\u7b97\u6a5f\u554f\u4e16\u4e4b\u524d\uff0c\u6f14\u7b97\u6cd5\u548c\u8cc7\u6599\u7d50\u69cb\u5c31\u5df2\u7d93\u5b58\u5728\u65bc\u4e16\u754c\u7684\u5404\u500b\u89d2\u843d\u3002\u65e9\u671f\u7684\u6f14\u7b97\u6cd5\u76f8\u5c0d\u7c21\u55ae\uff0c\u4f8b\u5982\u53e4\u4ee3\u7684\u8a08\u6578\u65b9\u6cd5\u548c\u5de5\u5177\u88fd\u4f5c\u6b65\u9a5f\u7b49\u3002\u96a8\u8457\u6587\u660e\u7684\u9032\u6b65\uff0c\u6f14\u7b97\u6cd5\u9010\u6f38\u8b8a\u5f97\u66f4\u52a0\u7cbe\u7d30\u548c\u8907\u96dc\u3002\u5f9e\u5de7\u596a\u5929\u5de5\u7684\u5320\u4eba\u6280\u85dd\u3001\u5230\u89e3\u653e\u751f\u7522\u529b\u7684\u5de5\u696d\u7522\u54c1\u3001\u518d\u5230\u5b87\u5b99\u57f7\u884c\u7684\u79d1\u5b78\u898f\u5f8b\uff0c\u5e7e\u4e4e\u6bcf\u4e00\u4ef6\u5e73\u51e1\u6216\u4ee4\u4eba\u9a5a\u6b4e\u7684\u4e8b\u7269\u80cc\u5f8c\uff0c\u90fd\u96b1\u85cf\u8457\u7cbe\u5999\u7684\u6f14\u7b97\u6cd5\u601d\u60f3\u3002</p> <p>\u540c\u6a23\uff0c\u8cc7\u6599\u7d50\u69cb\u7121\u8655\u4e0d\u5728\uff1a\u5927\u5230\u793e\u6703\u7db2\u8def\uff0c\u5c0f\u5230\u5730\u9435\u7dda\u8def\uff0c\u8a31\u591a\u7cfb\u7d71\u90fd\u53ef\u4ee5\u5efa\u6a21\u70ba\u201c\u5716\u201d\uff1b\u5927\u5230\u4e00\u500b\u570b\u5bb6\uff0c\u5c0f\u5230\u4e00\u500b\u5bb6\u5ead\uff0c\u793e\u6703\u7684\u4e3b\u8981\u7d44\u7e54\u5f62\u5f0f\u5448\u73fe\u51fa\u201c\u6a39\u201d\u7684\u7279\u5fb5\uff1b\u51ac\u5929\u7684\u8863\u670d\u5c31\u50cf\u201c\u5806\u758a\u201d\uff0c\u6700\u5148\u7a7f\u4e0a\u7684\u6700\u5f8c\u624d\u80fd\u812b\u4e0b\uff1b\u7fbd\u6bdb\u7403\u7b52\u5247\u5982\u540c\u201c\u4f47\u5217\u201d\uff0c\u4e00\u7aef\u653e\u5165\u3001\u53e6\u4e00\u7aef\u53d6\u51fa\uff1b\u5b57\u5178\u5c31\u50cf\u4e00\u500b\u201c\u96dc\u6e4a\u8868\u201d\uff0c\u80fd\u5920\u5feb\u901f\u67e5\u8a62\u76ee\u6a19\u8a5e\u689d\u3002</p> <p>\u672c\u66f8\u65e8\u5728\u900f\u904e\u6e05\u6670\u6613\u61c2\u7684\u52d5\u756b\u5716\u89e3\u548c\u53ef\u57f7\u884c\u7684\u7a0b\u5f0f\u78bc\u793a\u4f8b\uff0c\u4f7f\u8b80\u8005\u7406\u89e3\u6f14\u7b97\u6cd5\u548c\u8cc7\u6599\u7d50\u69cb\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u4e26\u80fd\u5920\u900f\u904e\u7a0b\u5f0f\u8a2d\u8a08\u4f86\u5be6\u73fe\u5b83\u5011\u3002\u5728\u6b64\u57fa\u790e\u4e0a\uff0c\u672c\u66f8\u81f4\u529b\u65bc\u63ed\u793a\u6f14\u7b97\u6cd5\u5728\u8907\u96dc\u4e16\u754c\u4e2d\u7684\u751f\u52d5\u9ad4\u73fe\uff0c\u5c55\u73fe\u6f14\u7b97\u6cd5\u4e4b\u7f8e\u3002\u5e0c\u671b\u672c\u66f8\u80fd\u5920\u5e6b\u52a9\u5230\u4f60\uff01</p>"},{"location":"chapter_introduction/","title":"\u7b2c 1 \u7ae0 \u00a0 \u521d\u8b58\u6f14\u7b97\u6cd5","text":"<p>Abstract</p> <p>\u4e00\u4f4d\u5c11\u5973\u7fe9\u7fe9\u8d77\u821e\uff0c\u8207\u8cc7\u6599\u4ea4\u7e54\u5728\u4e00\u8d77\uff0c\u88d9\u896c\u4e0a\u98c4\u63da\u8457\u6f14\u7b97\u6cd5\u7684\u65cb\u5f8b\u3002</p> <p>\u5979\u9080\u8acb\u4f60\u5171\u821e\uff0c\u8acb\u7dca\u8ddf\u5979\u7684\u6b65\u4f10\uff0c\u8e0f\u5165\u5145\u6eff\u908f\u8f2f\u8207\u7f8e\u611f\u7684\u6f14\u7b97\u6cd5\u4e16\u754c\u3002</p>"},{"location":"chapter_introduction/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>1.1 \u00a0 \u6f14\u7b97\u6cd5\u7121\u8655\u4e0d\u5728</li> <li>1.2 \u00a0 \u6f14\u7b97\u6cd5\u662f\u4ec0\u9ebc</li> <li>1.3 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_introduction/algorithms_are_everywhere/","title":"1.1 \u00a0 \u6f14\u7b97\u6cd5\u7121\u8655\u4e0d\u5728","text":"<p>\u7576\u6211\u5011\u807d\u5230\u201c\u6f14\u7b97\u6cd5\u201d\u9019\u500b\u8a5e\u6642\uff0c\u5f88\u81ea\u7136\u5730\u6703\u60f3\u5230\u6578\u5b78\u3002\u7136\u800c\u5be6\u969b\u4e0a\uff0c\u8a31\u591a\u6f14\u7b97\u6cd5\u4e26\u4e0d\u6d89\u53ca\u8907\u96dc\u6578\u5b78\uff0c\u800c\u662f\u66f4\u591a\u5730\u4f9d\u8cf4\u57fa\u672c\u908f\u8f2f\uff0c\u9019\u4e9b\u908f\u8f2f\u5728\u6211\u5011\u7684\u65e5\u5e38\u751f\u6d3b\u4e2d\u8655\u8655\u53ef\u898b\u3002</p> <p>\u5728\u6b63\u5f0f\u63a2\u8a0e\u6f14\u7b97\u6cd5\u4e4b\u524d\uff0c\u6709\u4e00\u500b\u6709\u8da3\u7684\u4e8b\u5be6\u503c\u5f97\u5206\u4eab\uff1a\u4f60\u5df2\u7d93\u5728\u4e0d\u77e5\u4e0d\u89ba\u4e2d\u5b78\u6703\u4e86\u8a31\u591a\u6f14\u7b97\u6cd5\uff0c\u4e26\u7fd2\u6163\u5c07\u5b83\u5011\u61c9\u7528\u5230\u65e5\u5e38\u751f\u6d3b\u4e2d\u4e86\u3002\u4e0b\u9762\u6211\u5c07\u8209\u5e7e\u500b\u5177\u9ad4\u7684\u4f8b\u5b50\u4f86\u8b49\u5be6\u9019\u4e00\u9ede\u3002</p> <p>\u4f8b\u4e00\uff1a\u67e5\u5b57\u5178\u3002\u5728\u5b57\u5178\u88e1\uff0c\u6bcf\u500b\u6f22\u5b57\u90fd\u5c0d\u61c9\u4e00\u500b\u62fc\u97f3\uff0c\u800c\u5b57\u5178\u662f\u6309\u7167\u62fc\u97f3\u5b57\u6bcd\u9806\u5e8f\u6392\u5217\u7684\u3002\u5047\u8a2d\u6211\u5011\u9700\u8981\u67e5\u8a62\u4e00\u500b\u62fc\u97f3\u9996\u5b57\u6bcd\u70ba \\(r\\) \u7684\u5b57\uff0c\u901a\u5e38\u6703\u6309\u7167\u5716 1-1 \u6240\u793a\u7684\u65b9\u5f0f\u5be6\u73fe\u3002</p> <ol> <li>\u7ffb\u958b\u5b57\u5178\u7d04\u4e00\u534a\u7684\u9801\u6578\uff0c\u6aa2\u8996\u8a72\u9801\u7684\u9996\u5b57\u6bcd\u662f\u4ec0\u9ebc\uff0c\u5047\u8a2d\u9996\u5b57\u6bcd\u70ba \\(m\\) \u3002</li> <li>\u7531\u65bc\u5728\u62fc\u97f3\u5b57\u6bcd\u8868\u4e2d \\(r\\) \u4f4d\u65bc \\(m\\) \u4e4b\u5f8c\uff0c\u6240\u4ee5\u6392\u9664\u5b57\u5178\u524d\u534a\u90e8\u5206\uff0c\u67e5\u8a62\u7bc4\u570d\u7e2e\u5c0f\u5230\u5f8c\u534a\u90e8\u5206\u3002</li> <li>\u4e0d\u65b7\u91cd\u8907\u6b65\u9a5f <code>1.</code> \u548c \u6b65\u9a5f <code>2.</code> \uff0c\u76f4\u81f3\u627e\u5230\u62fc\u97f3\u9996\u5b57\u6bcd\u70ba \\(r\\) \u7684\u9801\u78bc\u70ba\u6b62\u3002</li> </ol> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 1-1 \u00a0 \u67e5\u5b57\u5178\u6b65\u9a5f </p> <p>\u67e5\u5b57\u5178\u9019\u500b\u5c0f\u5b78\u751f\u5fc5\u5099\u6280\u80fd\uff0c\u5be6\u969b\u4e0a\u5c31\u662f\u8457\u540d\u7684\u201c\u4e8c\u5206\u641c\u5c0b\u201d\u6f14\u7b97\u6cd5\u3002\u5f9e\u8cc7\u6599\u7d50\u69cb\u7684\u89d2\u5ea6\uff0c\u6211\u5011\u53ef\u4ee5\u628a\u5b57\u5178\u8996\u70ba\u4e00\u500b\u5df2\u6392\u5e8f\u7684\u201c\u9663\u5217\u201d\uff1b\u5f9e\u6f14\u7b97\u6cd5\u7684\u89d2\u5ea6\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u4e0a\u8ff0\u67e5\u5b57\u5178\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u770b\u4f5c\u201c\u4e8c\u5206\u641c\u5c0b\u201d\u3002</p> <p>\u4f8b\u4e8c\uff1a\u6574\u7406\u64b2\u514b\u3002\u6211\u5011\u5728\u6253\u724c\u6642\uff0c\u6bcf\u5c40\u90fd\u9700\u8981\u6574\u7406\u624b\u4e2d\u7684\u64b2\u514b\u724c\uff0c\u4f7f\u5176\u5f9e\u5c0f\u5230\u5927\u6392\u5217\uff0c\u5be6\u73fe\u6d41\u7a0b\u5982\u5716 1-2 \u6240\u793a\u3002</p> <ol> <li>\u5c07\u64b2\u514b\u724c\u5283\u5206\u70ba\u201c\u6709\u5e8f\u201d\u548c\u201c\u7121\u5e8f\u201d\u5169\u90e8\u5206\uff0c\u4e26\u5047\u8a2d\u521d\u59cb\u72c0\u614b\u4e0b\u6700\u5de6 1 \u5f35\u64b2\u514b\u724c\u5df2\u7d93\u6709\u5e8f\u3002</li> <li>\u5728\u7121\u5e8f\u90e8\u5206\u62bd\u51fa\u4e00\u5f35\u64b2\u514b\u724c\uff0c\u63d2\u5165\u81f3\u6709\u5e8f\u90e8\u5206\u7684\u6b63\u78ba\u4f4d\u7f6e\uff1b\u5b8c\u6210\u5f8c\u6700\u5de6 2 \u5f35\u64b2\u514b\u5df2\u7d93\u6709\u5e8f\u3002</li> <li>\u4e0d\u65b7\u8ff4\u5708\u6b65\u9a5f <code>2.</code> \uff0c\u6bcf\u4e00\u8f2a\u5c07\u4e00\u5f35\u64b2\u514b\u724c\u5f9e\u7121\u5e8f\u90e8\u5206\u63d2\u5165\u81f3\u6709\u5e8f\u90e8\u5206\uff0c\u76f4\u81f3\u6240\u6709\u64b2\u514b\u724c\u90fd\u6709\u5e8f\u3002</li> </ol> <p></p> <p> \u5716 1-2 \u00a0 \u64b2\u514b\u6392\u5e8f\u6b65\u9a5f </p> <p>\u4e0a\u8ff0\u6574\u7406\u64b2\u514b\u724c\u7684\u65b9\u6cd5\u672c\u8cea\u4e0a\u662f\u201c\u63d2\u5165\u6392\u5e8f\u201d\u6f14\u7b97\u6cd5\uff0c\u5b83\u5728\u8655\u7406\u5c0f\u578b\u8cc7\u6599\u96c6\u6642\u975e\u5e38\u9ad8\u6548\u3002\u8a31\u591a\u7a0b\u5f0f\u8a9e\u8a00\u7684\u6392\u5e8f\u5eab\u51fd\u5f0f\u4e2d\u90fd\u6709\u63d2\u5165\u6392\u5e8f\u7684\u8eab\u5f71\u3002</p> <p>\u4f8b\u4e09\uff1a\u8ca8\u5e63\u627e\u96f6\u3002\u5047\u8a2d\u6211\u5011\u5728\u8d85\u5e02\u8cfc\u8cb7\u4e86 \\(69\\) \u5143\u7684\u5546\u54c1\uff0c\u7d66\u4e86\u6536\u9280\u54e1 \\(100\\) \u5143\uff0c\u5247\u6536\u9280\u54e1\u9700\u8981\u627e\u6211\u5011 \\(31\\) \u5143\u3002\u4ed6\u6703\u5f88\u81ea\u7136\u5730\u5b8c\u6210\u5982\u5716 1-3 \u6240\u793a\u7684\u601d\u8003\u3002</p> <ol> <li>\u53ef\u9078\u9805\u662f\u6bd4 \\(31\\) \u5143\u9762\u503c\u66f4\u5c0f\u7684\u8ca8\u5e63\uff0c\u5305\u62ec \\(1\\) \u5143\u3001\\(5\\) \u5143\u3001\\(10\\) \u5143\u3001\\(20\\) \u5143\u3002</li> <li>\u5f9e\u53ef\u9078\u9805\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(20\\) \u5143\uff0c\u5269\u9918 \\(31 - 20 = 11\\) \u5143\u3002</li> <li>\u5f9e\u5269\u9918\u53ef\u9078\u9805\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(10\\) \u5143\uff0c\u5269\u9918 \\(11 - 10 = 1\\) \u5143\u3002</li> <li>\u5f9e\u5269\u9918\u53ef\u9078\u9805\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(1\\) \u5143\uff0c\u5269\u9918 \\(1 - 1 = 0\\) \u5143\u3002</li> <li>\u5b8c\u6210\u627e\u96f6\uff0c\u65b9\u6848\u70ba \\(20 + 10 + 1 = 31\\) \u5143\u3002</li> </ol> <p></p> <p> \u5716 1-3 \u00a0 \u8ca8\u5e63\u627e\u96f6\u904e\u7a0b </p> <p>\u5728\u4ee5\u4e0a\u6b65\u9a5f\u4e2d\uff0c\u6211\u5011\u6bcf\u4e00\u6b65\u90fd\u63a1\u53d6\u7576\u524d\u770b\u4f86\u6700\u597d\u7684\u9078\u64c7\uff08\u5118\u53ef\u80fd\u7528\u5927\u9762\u984d\u7684\u8ca8\u5e63\uff09\uff0c\u6700\u7d42\u5f97\u5230\u4e86\u53ef\u884c\u7684\u627e\u96f6\u65b9\u6848\u3002\u5f9e\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u9019\u7a2e\u65b9\u6cd5\u672c\u8cea\u4e0a\u662f\u201c\u8caa\u5a6a\u201d\u6f14\u7b97\u6cd5\u3002</p> <p>\u5c0f\u5230\u70f9\u98ea\u4e00\u9053\u83dc\uff0c\u5927\u5230\u661f\u969b\u822a\u884c\uff0c\u5e7e\u4e4e\u6240\u6709\u554f\u984c\u7684\u89e3\u6c7a\u90fd\u96e2\u4e0d\u958b\u6f14\u7b97\u6cd5\u3002\u8a08\u7b97\u6a5f\u7684\u51fa\u73fe\u4f7f\u5f97\u6211\u5011\u80fd\u5920\u900f\u904e\u7a0b\u5f0f\u8a2d\u8a08\u5c07\u8cc7\u6599\u7d50\u69cb\u5132\u5b58\u5728\u8a18\u61b6\u9ad4\u4e2d\uff0c\u540c\u6642\u7de8\u5beb\u7a0b\u5f0f\u78bc\u547c\u53eb CPU \u548c GPU \u57f7\u884c\u6f14\u7b97\u6cd5\u3002\u9019\u6a23\u4e00\u4f86\uff0c\u6211\u5011\u5c31\u80fd\u628a\u751f\u6d3b\u4e2d\u7684\u554f\u984c\u8f49\u79fb\u5230\u8a08\u7b97\u6a5f\u4e0a\uff0c\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u89e3\u6c7a\u5404\u7a2e\u8907\u96dc\u554f\u984c\u3002</p> <p>Tip</p> <p>\u5982\u679c\u4f60\u5c0d\u8cc7\u6599\u7d50\u69cb\u3001\u6f14\u7b97\u6cd5\u3001\u9663\u5217\u548c\u4e8c\u5206\u641c\u5c0b\u7b49\u6982\u5ff5\u4ecd\u611f\u5230\u4e00\u77e5\u534a\u89e3\uff0c\u8acb\u7e7c\u7e8c\u5f80\u4e0b\u95b1\u8b80\uff0c\u672c\u66f8\u5c07\u5f15\u5c0e\u4f60\u9081\u5165\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u7684\u77e5\u8b58\u6bbf\u5802\u3002</p>"},{"location":"chapter_introduction/summary/","title":"1.3 \u00a0 \u5c0f\u7d50","text":"<ul> <li>\u6f14\u7b97\u6cd5\u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\u7121\u8655\u4e0d\u5728\uff0c\u4e26\u4e0d\u662f\u9059\u4e0d\u53ef\u53ca\u7684\u9ad8\u6df1\u77e5\u8b58\u3002\u5be6\u969b\u4e0a\uff0c\u6211\u5011\u5df2\u7d93\u5728\u4e0d\u77e5\u4e0d\u89ba\u4e2d\u5b78\u6703\u4e86\u8a31\u591a\u6f14\u7b97\u6cd5\uff0c\u7528\u4ee5\u89e3\u6c7a\u751f\u6d3b\u4e2d\u7684\u5927\u5c0f\u554f\u984c\u3002</li> <li>\u67e5\u5b57\u5178\u7684\u539f\u7406\u8207\u4e8c\u5206\u641c\u5c0b\u6f14\u7b97\u6cd5\u76f8\u4e00\u81f4\u3002\u4e8c\u5206\u641c\u5c0b\u6f14\u7b97\u6cd5\u9ad4\u73fe\u4e86\u5206\u800c\u6cbb\u4e4b\u7684\u91cd\u8981\u6f14\u7b97\u6cd5\u601d\u60f3\u3002</li> <li>\u6574\u7406\u64b2\u514b\u7684\u904e\u7a0b\u8207\u63d2\u5165\u6392\u5e8f\u6f14\u7b97\u6cd5\u975e\u5e38\u985e\u4f3c\u3002\u63d2\u5165\u6392\u5e8f\u6f14\u7b97\u6cd5\u9069\u5408\u6392\u5e8f\u5c0f\u578b\u8cc7\u6599\u96c6\u3002</li> <li>\u8ca8\u5e63\u627e\u96f6\u7684\u6b65\u9a5f\u672c\u8cea\u4e0a\u662f\u8caa\u5a6a\u6f14\u7b97\u6cd5\uff0c\u6bcf\u4e00\u6b65\u90fd\u63a1\u53d6\u7576\u524d\u770b\u4f86\u6700\u597d\u7684\u9078\u64c7\u3002</li> <li>\u6f14\u7b97\u6cd5\u662f\u5728\u6709\u9650\u6642\u9593\u5167\u89e3\u6c7a\u7279\u5b9a\u554f\u984c\u7684\u4e00\u7d44\u6307\u4ee4\u6216\u64cd\u4f5c\u6b65\u9a5f\uff0c\u800c\u8cc7\u6599\u7d50\u69cb\u662f\u8a08\u7b97\u6a5f\u4e2d\u7d44\u7e54\u548c\u5132\u5b58\u8cc7\u6599\u7684\u65b9\u5f0f\u3002</li> <li>\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u7dca\u5bc6\u76f8\u9023\u3002\u8cc7\u6599\u7d50\u69cb\u662f\u6f14\u7b97\u6cd5\u7684\u57fa\u77f3\uff0c\u800c\u6f14\u7b97\u6cd5\u662f\u8cc7\u6599\u7d50\u69cb\u767c\u63ee\u4f5c\u7528\u7684\u821e\u81fa\u3002</li> <li>\u6211\u5011\u53ef\u4ee5\u5c07\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u985e\u6bd4\u70ba\u62fc\u88dd\u7a4d\u6728\uff0c\u7a4d\u6728\u4ee3\u8868\u8cc7\u6599\uff0c\u7a4d\u6728\u7684\u5f62\u72c0\u548c\u9023\u7dda\u65b9\u5f0f\u7b49\u4ee3\u8868\u8cc7\u6599\u7d50\u69cb\uff0c\u62fc\u88dd\u7a4d\u6728\u7684\u6b65\u9a5f\u5247\u5c0d\u61c9\u6f14\u7b97\u6cd5\u3002</li> </ul>"},{"location":"chapter_introduction/summary/#1-q-a","title":"1. \u00a0 Q &amp; A","text":"<p>Q\uff1a\u4f5c\u70ba\u4e00\u540d\u7a0b\u5f0f\u8a2d\u8a08\u5e2b\uff0c\u6211\u5728\u65e5\u5e38\u5de5\u4f5c\u4e2d\u5f9e\u672a\u7528\u6f14\u7b97\u6cd5\u89e3\u6c7a\u904e\u554f\u984c\uff0c\u5e38\u7528\u6f14\u7b97\u6cd5\u90fd\u88ab\u7a0b\u5f0f\u8a9e\u8a00\u5c01\u88dd\u597d\u4e86\uff0c\u76f4\u63a5\u7528\u5c31\u53ef\u4ee5\u4e86\uff1b\u9019\u662f\u5426\u610f\u5473\u8457\u6211\u5011\u5de5\u4f5c\u4e2d\u7684\u554f\u984c\u9084\u6c92\u6709\u5230\u9054\u9700\u8981\u6f14\u7b97\u6cd5\u7684\u7a0b\u5ea6\uff1f</p> <p>\u5982\u679c\u628a\u5177\u9ad4\u7684\u5de5\u4f5c\u6280\u80fd\u6bd4\u4f5c\u662f\u6b66\u529f\u7684\u201c\u62db\u5f0f\u201d\u7684\u8a71\uff0c\u90a3\u9ebc\u57fa\u790e\u79d1\u76ee\u61c9\u8a72\u66f4\u50cf\u662f\u201c\u5167\u529f\u201d\u3002</p> <p>\u6211\u8a8d\u70ba\u5b78\u6f14\u7b97\u6cd5\uff08\u4ee5\u53ca\u5176\u4ed6\u57fa\u790e\u79d1\u76ee\uff09\u7684\u610f\u7fa9\u4e0d\u662f\u5728\u65bc\u5728\u5de5\u4f5c\u4e2d\u5f9e\u96f6\u5be6\u73fe\u5b83\uff0c\u800c\u662f\u57fa\u65bc\u5b78\u5230\u7684\u77e5\u8b58\uff0c\u5728\u89e3\u6c7a\u554f\u984c\u6642\u80fd\u5920\u4f5c\u51fa\u5c08\u696d\u7684\u53cd\u61c9\u548c\u5224\u65b7\uff0c\u5f9e\u800c\u63d0\u5347\u5de5\u4f5c\u7684\u6574\u9ad4\u8cea\u91cf\u3002\u8209\u4e00\u500b\u7c21\u55ae\u4f8b\u5b50\uff0c\u6bcf\u7a2e\u7a0b\u5f0f\u8a9e\u8a00\u90fd\u5167\u5efa\u4e86\u6392\u5e8f\u51fd\u5f0f\uff1a</p> <ul> <li>\u5982\u679c\u6211\u5011\u6c92\u6709\u5b78\u904e\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\uff0c\u90a3\u9ebc\u7d66\u5b9a\u4efb\u4f55\u8cc7\u6599\uff0c\u6211\u5011\u53ef\u80fd\u90fd\u585e\u7d66\u9019\u500b\u6392\u5e8f\u51fd\u5f0f\u53bb\u505a\u4e86\u3002\u57f7\u884c\u9806\u66a2\u3001\u6548\u80fd\u4e0d\u932f\uff0c\u770b\u4e0a\u53bb\u4e26\u6c92\u6709\u4ec0\u9ebc\u554f\u984c\u3002</li> <li>\u4f46\u5982\u679c\u5b78\u904e\u6f14\u7b97\u6cd5\uff0c\u6211\u5011\u5c31\u6703\u77e5\u9053\u5167\u5efa\u6392\u5e8f\u51fd\u5f0f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(O(n \\log n)\\) \uff1b\u800c\u5982\u679c\u7d66\u5b9a\u7684\u8cc7\u6599\u662f\u56fa\u5b9a\u4f4d\u6578\u7684\u6574\u6578\uff08\u4f8b\u5982\u5b78\u865f\uff09\uff0c\u90a3\u9ebc\u6211\u5011\u5c31\u53ef\u4ee5\u7528\u6548\u7387\u66f4\u9ad8\u7684\u201c\u57fa\u6578\u6392\u5e8f\u201d\u4f86\u505a\uff0c\u5c07\u6642\u9593\u8907\u96dc\u5ea6\u964d\u70ba \\(O(nk)\\) \uff0c\u5176\u4e2d \\(k\\) \u70ba\u4f4d\u6578\u3002\u7576\u8cc7\u6599\u9ad4\u91cf\u5f88\u5927\u6642\uff0c\u7bc0\u7701\u51fa\u4f86\u7684\u57f7\u884c\u6642\u9593\u5c31\u80fd\u5275\u9020\u8f03\u5927\u50f9\u503c\uff08\u6210\u672c\u964d\u4f4e\u3001\u9ad4\u9a57\u8b8a\u597d\u7b49\uff09\u3002</li> </ul> <p>\u5728\u5de5\u7a0b\u9818\u57df\u4e2d\uff0c\u5927\u91cf\u554f\u984c\u662f\u96e3\u4ee5\u9054\u5230\u6700\u512a\u89e3\u7684\uff0c\u8a31\u591a\u554f\u984c\u53ea\u662f\u88ab\u201c\u5dee\u4e0d\u591a\u201d\u5730\u89e3\u6c7a\u4e86\u3002\u554f\u984c\u7684\u96e3\u6613\u7a0b\u5ea6\u4e00\u65b9\u9762\u53d6\u6c7a\u65bc\u554f\u984c\u672c\u8eab\u7684\u6027\u8cea\uff0c\u53e6\u4e00\u65b9\u9762\u4e5f\u53d6\u6c7a\u65bc\u89c0\u6e2c\u554f\u984c\u7684\u4eba\u7684\u77e5\u8b58\u5132\u5099\u3002\u4eba\u7684\u77e5\u8b58\u8d8a\u5b8c\u5099\u3001\u7d93\u9a57\u8d8a\u591a\uff0c\u5206\u6790\u554f\u984c\u5c31\u6703\u8d8a\u6df1\u5165\uff0c\u554f\u984c\u5c31\u80fd\u88ab\u89e3\u6c7a\u5f97\u66f4\u512a\u96c5\u3002</p>"},{"location":"chapter_introduction/what_is_dsa/","title":"1.2 \u00a0 \u6f14\u7b97\u6cd5\u662f\u4ec0\u9ebc","text":""},{"location":"chapter_introduction/what_is_dsa/#121","title":"1.2.1 \u00a0 \u6f14\u7b97\u6cd5\u5b9a\u7fa9","text":"<p>\u6f14\u7b97\u6cd5\uff08algorithm\uff09\u662f\u5728\u6709\u9650\u6642\u9593\u5167\u89e3\u6c7a\u7279\u5b9a\u554f\u984c\u7684\u4e00\u7d44\u6307\u4ee4\u6216\u64cd\u4f5c\u6b65\u9a5f\uff0c\u5b83\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002</p> <ul> <li>\u554f\u984c\u662f\u660e\u78ba\u7684\uff0c\u5305\u542b\u6e05\u6670\u7684\u8f38\u5165\u548c\u8f38\u51fa\u5b9a\u7fa9\u3002</li> <li>\u5177\u6709\u53ef\u884c\u6027\uff0c\u80fd\u5920\u5728\u6709\u9650\u6b65\u9a5f\u3001\u6642\u9593\u548c\u8a18\u61b6\u9ad4\u7a7a\u9593\u4e0b\u5b8c\u6210\u3002</li> <li>\u5404\u6b65\u9a5f\u90fd\u6709\u78ba\u5b9a\u7684\u542b\u7fa9\uff0c\u5728\u76f8\u540c\u7684\u8f38\u5165\u548c\u57f7\u884c\u689d\u4ef6\u4e0b\uff0c\u8f38\u51fa\u59cb\u7d42\u76f8\u540c\u3002</li> </ul>"},{"location":"chapter_introduction/what_is_dsa/#122","title":"1.2.2 \u00a0 \u8cc7\u6599\u7d50\u69cb\u5b9a\u7fa9","text":"<p>\u8cc7\u6599\u7d50\u69cb\uff08data structure\uff09\u662f\u8a08\u7b97\u6a5f\u4e2d\u7d44\u7e54\u548c\u5132\u5b58\u8cc7\u6599\u7684\u65b9\u5f0f\uff0c\u5177\u6709\u4ee5\u4e0b\u8a2d\u8a08\u76ee\u6a19\u3002</p> <ul> <li>\u7a7a\u9593\u4f54\u7528\u5118\u91cf\u5c11\uff0c\u4ee5\u7bc0\u7701\u8a08\u7b97\u6a5f\u8a18\u61b6\u9ad4\u3002</li> <li>\u8cc7\u6599\u64cd\u4f5c\u5118\u53ef\u80fd\u5feb\u901f\uff0c\u6db5\u84cb\u8cc7\u6599\u8a2a\u554f\u3001\u65b0\u589e\u3001\u522a\u9664\u3001\u66f4\u65b0\u7b49\u3002</li> <li>\u63d0\u4f9b\u7c21\u6f54\u7684\u8cc7\u6599\u8868\u793a\u548c\u908f\u8f2f\u8cc7\u8a0a\uff0c\u4ee5\u4fbf\u6f14\u7b97\u6cd5\u9ad8\u6548\u57f7\u884c\u3002</li> </ul> <p>\u8cc7\u6599\u7d50\u69cb\u8a2d\u8a08\u662f\u4e00\u500b\u5145\u6eff\u6b0a\u8861\u7684\u904e\u7a0b\u3002\u5982\u679c\u60f3\u5728\u67d0\u65b9\u9762\u53d6\u5f97\u63d0\u5347\uff0c\u5f80\u5f80\u9700\u8981\u5728\u53e6\u4e00\u65b9\u9762\u4f5c\u51fa\u59a5\u5354\u3002\u4e0b\u9762\u8209\u5169\u500b\u4f8b\u5b50\u3002</p> <ul> <li>\u93c8\u7d50\u4e32\u5217\u76f8\u8f03\u65bc\u9663\u5217\uff0c\u5728\u8cc7\u6599\u65b0\u589e\u548c\u522a\u9664\u64cd\u4f5c\u4e0a\u66f4\u52a0\u4fbf\u6377\uff0c\u4f46\u72a7\u7272\u4e86\u8cc7\u6599\u8a2a\u554f\u901f\u5ea6\u3002</li> <li>\u5716\u76f8\u8f03\u65bc\u93c8\u7d50\u4e32\u5217\uff0c\u63d0\u4f9b\u4e86\u66f4\u8c50\u5bcc\u7684\u908f\u8f2f\u8cc7\u8a0a\uff0c\u4f46\u9700\u8981\u4f54\u7528\u66f4\u5927\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u3002</li> </ul>"},{"location":"chapter_introduction/what_is_dsa/#123","title":"1.2.3 \u00a0 \u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u7684\u95dc\u4fc2","text":"<p>\u5982\u5716 1-4 \u6240\u793a\uff0c\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u9ad8\u5ea6\u76f8\u95dc\u3001\u7dca\u5bc6\u7d50\u5408\uff0c\u5177\u9ad4\u8868\u73fe\u5728\u4ee5\u4e0b\u4e09\u500b\u65b9\u9762\u3002</p> <ul> <li>\u8cc7\u6599\u7d50\u69cb\u662f\u6f14\u7b97\u6cd5\u7684\u57fa\u77f3\u3002\u8cc7\u6599\u7d50\u69cb\u70ba\u6f14\u7b97\u6cd5\u63d0\u4f9b\u4e86\u7d50\u69cb\u5316\u5132\u5b58\u7684\u8cc7\u6599\uff0c\u4ee5\u53ca\u64cd\u4f5c\u8cc7\u6599\u7684\u65b9\u6cd5\u3002</li> <li>\u6f14\u7b97\u6cd5\u662f\u8cc7\u6599\u7d50\u69cb\u767c\u63ee\u4f5c\u7528\u7684\u821e\u81fa\u3002\u8cc7\u6599\u7d50\u69cb\u672c\u8eab\u50c5\u5132\u5b58\u8cc7\u6599\u8cc7\u8a0a\uff0c\u7d50\u5408\u6f14\u7b97\u6cd5\u624d\u80fd\u89e3\u6c7a\u7279\u5b9a\u554f\u984c\u3002</li> <li>\u6f14\u7b97\u6cd5\u901a\u5e38\u53ef\u4ee5\u57fa\u65bc\u4e0d\u540c\u7684\u8cc7\u6599\u7d50\u69cb\u5be6\u73fe\uff0c\u4f46\u57f7\u884c\u6548\u7387\u53ef\u80fd\u76f8\u5dee\u5f88\u5927\uff0c\u9078\u64c7\u5408\u9069\u7684\u8cc7\u6599\u7d50\u69cb\u662f\u95dc\u9375\u3002</li> </ul> <p></p> <p> \u5716 1-4 \u00a0 \u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u7684\u95dc\u4fc2 </p> <p>\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u7336\u5982\u5716 1-5 \u6240\u793a\u7684\u62fc\u88dd\u7a4d\u6728\u3002\u4e00\u5957\u7a4d\u6728\uff0c\u9664\u4e86\u5305\u542b\u8a31\u591a\u96f6\u4ef6\u4e4b\u5916\uff0c\u9084\u9644\u6709\u8a73\u7d30\u7684\u7d44\u88dd\u8aaa\u660e\u66f8\u3002\u6211\u5011\u6309\u7167\u8aaa\u660e\u66f8\u4e00\u6b65\u6b65\u64cd\u4f5c\uff0c\u5c31\u80fd\u7d44\u88dd\u51fa\u7cbe\u7f8e\u7684\u7a4d\u6728\u6a21\u578b\u3002</p> <p></p> <p> \u5716 1-5 \u00a0 \u62fc\u88dd\u7a4d\u6728 </p> <p>\u5169\u8005\u7684\u8a73\u7d30\u5c0d\u61c9\u95dc\u4fc2\u5982\u8868 1-1 \u6240\u793a\u3002</p> <p> \u8868 1-1 \u00a0 \u5c07\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u985e\u6bd4\u70ba\u62fc\u88dd\u7a4d\u6728 </p> \u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5 \u62fc\u88dd\u7a4d\u6728 \u8f38\u5165\u8cc7\u6599 \u672a\u62fc\u88dd\u7684\u7a4d\u6728 \u8cc7\u6599\u7d50\u69cb \u7a4d\u6728\u7d44\u7e54\u5f62\u5f0f\uff0c\u5305\u62ec\u5f62\u72c0\u3001\u5927\u5c0f\u3001\u9023\u7dda\u65b9\u5f0f\u7b49 \u6f14\u7b97\u6cd5 \u628a\u7a4d\u6728\u62fc\u6210\u76ee\u6a19\u5f62\u614b\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u6b65\u9a5f \u8f38\u51fa\u8cc7\u6599 \u7a4d\u6728\u6a21\u578b <p>\u503c\u5f97\u8aaa\u660e\u7684\u662f\uff0c\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u662f\u7368\u7acb\u65bc\u7a0b\u5f0f\u8a9e\u8a00\u7684\u3002\u6b63\u56e0\u5982\u6b64\uff0c\u672c\u66f8\u5f97\u4ee5\u63d0\u4f9b\u57fa\u65bc\u591a\u7a2e\u7a0b\u5f0f\u8a9e\u8a00\u7684\u5be6\u73fe\u3002</p> <p>\u7d04\u5b9a\u4fd7\u6210\u7684\u7c21\u7a31</p> <p>\u5728\u5be6\u969b\u8a0e\u8ad6\u6642\uff0c\u6211\u5011\u901a\u5e38\u6703\u5c07\u201c\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u201d\u7c21\u7a31\u70ba\u201c\u6f14\u7b97\u6cd5\u201d\u3002\u6bd4\u5982\u773e\u6240\u5468\u77e5\u7684 LeetCode \u6f14\u7b97\u6cd5\u984c\u76ee\uff0c\u5be6\u969b\u4e0a\u540c\u6642\u8003\u67e5\u8cc7\u6599\u7d50\u69cb\u548c\u6f14\u7b97\u6cd5\u5169\u65b9\u9762\u7684\u77e5\u8b58\u3002</p>"},{"location":"chapter_preface/","title":"\u7b2c 0 \u7ae0 \u00a0 \u524d\u8a00","text":"<p>Abstract</p> <p>\u6f14\u7b97\u6cd5\u7336\u5982\u7f8e\u5999\u7684\u4ea4\u97ff\u6a02\uff0c\u6bcf\u4e00\u884c\u7a0b\u5f0f\u78bc\u90fd\u50cf\u97fb\u5f8b\u822c\u6d41\u6dcc\u3002</p> <p>\u9858\u9019\u672c\u66f8\u5728\u4f60\u7684\u8166\u6d77\u4e2d\u8f15\u8f15\u97ff\u8d77\uff0c\u7559\u4e0b\u7368\u7279\u800c\u6df1\u523b\u7684\u65cb\u5f8b\u3002</p>"},{"location":"chapter_preface/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>0.1 \u00a0 \u95dc\u65bc\u672c\u66f8</li> <li>0.2 \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u66f8</li> <li>0.3 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_preface/about_the_book/","title":"0.1 \u00a0 \u95dc\u65bc\u672c\u66f8","text":"<p>\u672c\u5c08\u6848\u65e8\u5728\u5efa\u7acb\u4e00\u672c\u958b\u6e90\u3001\u514d\u8cbb\u3001\u5c0d\u65b0\u624b\u53cb\u597d\u7684\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u5165\u9580\u6559\u7a0b\u3002</p> <ul> <li>\u5168\u66f8\u63a1\u7528\u52d5\u756b\u5716\u89e3\uff0c\u7d50\u69cb\u5316\u5730\u8b1b\u89e3\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u77e5\u8b58\uff0c\u5167\u5bb9\u6e05\u6670\u6613\u61c2\uff0c\u5b78\u7fd2\u66f2\u7dda\u5e73\u6ed1\u3002</li> <li>\u6f14\u7b97\u6cd5\u6e90\u7a0b\u5f0f\u78bc\u7686\u53ef\u4e00\u9375\u57f7\u884c\uff0c\u652f\u6301 Python\u3001C++\u3001Java\u3001C#\u3001Go\u3001Swift\u3001JavaScript\u3001TypeScript\u3001Dart\u3001Rust\u3001C \u548c Zig \u7b49\u8a9e\u8a00\u3002</li> <li>\u9f13\u52f5\u8b80\u8005\u5728\u7dda\u4e0a\u7ae0\u7bc0\u8a55\u8ad6\u5340\u4e92\u5e6b\u4e92\u52a9\u3001\u5171\u540c\u9032\u6b65\uff0c\u63d0\u554f\u8207\u8a55\u8ad6\u901a\u5e38\u53ef\u5728\u5169\u65e5\u5167\u5f97\u5230\u56de\u8986\u3002</li> </ul>"},{"location":"chapter_preface/about_the_book/#011","title":"0.1.1 \u00a0 \u8b80\u8005\u7269\u4ef6","text":"<p>\u82e5\u4f60\u662f\u6f14\u7b97\u6cd5\u521d\u5b78\u8005\uff0c\u5f9e\u672a\u63a5\u89f8\u904e\u6f14\u7b97\u6cd5\uff0c\u6216\u8005\u5df2\u7d93\u6709\u4e00\u4e9b\u5237\u984c\u7d93\u9a57\uff0c\u5c0d\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u6709\u6a21\u7cca\u7684\u8a8d\u8b58\uff0c\u5728\u6703\u8207\u4e0d\u6703\u4e4b\u9593\u53cd\u8986\u6a6b\u8df3\uff0c\u90a3\u9ebc\u672c\u66f8\u6b63\u662f\u70ba\u4f60\u91cf\u8eab\u5b9a\u88fd\u7684\uff01</p> <p>\u5982\u679c\u4f60\u5df2\u7d93\u7a4d\u7d2f\u4e00\u5b9a\u7684\u5237\u984c\u91cf\uff0c\u719f\u6089\u5927\u90e8\u5206\u984c\u578b\uff0c\u90a3\u9ebc\u672c\u66f8\u53ef\u52a9\u4f60\u56de\u9867\u8207\u68b3\u7406\u6f14\u7b97\u6cd5\u77e5\u8b58\u9ad4\u7cfb\uff0c\u5009\u5eab\u6e90\u7a0b\u5f0f\u78bc\u53ef\u4ee5\u7576\u4f5c\u201c\u5237\u984c\u5de5\u5177\u5eab\u201d\u6216\u201c\u6f14\u7b97\u6cd5\u5b57\u5178\u201d\u4f86\u4f7f\u7528\u3002</p> <p>\u82e5\u4f60\u662f\u6f14\u7b97\u6cd5\u201c\u5927\u795e\u201d\uff0c\u6211\u5011\u671f\u5f85\u6536\u5230\u4f60\u7684\u5bf6\u8cb4\u5efa\u8b70\uff0c\u6216\u8005\u4e00\u8d77\u53c3\u8207\u5275\u4f5c\u3002</p> <p>\u524d\u7f6e\u689d\u4ef6</p> <p>\u4f60\u9700\u8981\u81f3\u5c11\u5177\u5099\u4efb\u4e00\u8a9e\u8a00\u7684\u7a0b\u5f0f\u8a2d\u8a08\u57fa\u790e\uff0c\u80fd\u5920\u95b1\u8b80\u548c\u7de8\u5beb\u7c21\u55ae\u7a0b\u5f0f\u78bc\u3002</p>"},{"location":"chapter_preface/about_the_book/#012","title":"0.1.2 \u00a0 \u5167\u5bb9\u7d50\u69cb","text":"<p>\u672c\u66f8\u7684\u4e3b\u8981\u5167\u5bb9\u5982\u5716 0-1 \u6240\u793a\u3002</p> <ul> <li>\u8907\u96dc\u5ea6\u5206\u6790\uff1a\u8cc7\u6599\u7d50\u69cb\u548c\u6f14\u7b97\u6cd5\u7684\u8a55\u50f9\u7dad\u5ea6\u8207\u65b9\u6cd5\u3002\u6642\u9593\u8907\u96dc\u5ea6\u548c\u7a7a\u9593\u8907\u96dc\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u3001\u5e38\u898b\u578b\u5225\u3001\u793a\u4f8b\u7b49\u3002</li> <li>\u8cc7\u6599\u7d50\u69cb\uff1a\u57fa\u672c\u8cc7\u6599\u578b\u5225\u548c\u8cc7\u6599\u7d50\u69cb\u7684\u5206\u985e\u65b9\u6cd5\u3002\u9663\u5217\u3001\u93c8\u7d50\u4e32\u5217\u3001\u5806\u758a\u3001\u4f47\u5217\u3001\u96dc\u6e4a\u8868\u3001\u6a39\u3001\u5806\u7a4d\u3001\u5716\u7b49\u8cc7\u6599\u7d50\u69cb\u7684\u5b9a\u7fa9\u3001\u512a\u7f3a\u9ede\u3001\u5e38\u7528\u64cd\u4f5c\u3001\u5e38\u898b\u578b\u5225\u3001\u5178\u578b\u61c9\u7528\u3001\u5be6\u73fe\u65b9\u6cd5\u7b49\u3002</li> <li>\u6f14\u7b97\u6cd5\uff1a\u641c\u5c0b\u3001\u6392\u5e8f\u3001\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52d5\u614b\u898f\u5283\u3001\u8caa\u5a6a\u7b49\u6f14\u7b97\u6cd5\u7684\u5b9a\u7fa9\u3001\u512a\u7f3a\u9ede\u3001\u6548\u7387\u3001\u61c9\u7528\u5834\u666f\u3001\u89e3\u984c\u6b65\u9a5f\u548c\u793a\u4f8b\u554f\u984c\u7b49\u3002</li> </ul> <p></p> <p> \u5716 0-1 \u00a0 \u672c\u66f8\u4e3b\u8981\u5167\u5bb9 </p>"},{"location":"chapter_preface/about_the_book/#013","title":"0.1.3 \u00a0 \u81f4\u8b1d","text":"<p>\u672c\u66f8\u5728\u958b\u6e90\u793e\u7fa4\u773e\u591a\u8ca2\u737b\u8005\u7684\u5171\u540c\u52aa\u529b\u4e0b\u4e0d\u65b7\u5b8c\u5584\u3002\u611f\u8b1d\u6bcf\u4e00\u4f4d\u6295\u5165\u6642\u9593\u8207\u7cbe\u529b\u7684\u64b0\u7a3f\u4eba\uff0c\u4ed6\u5011\u662f\uff08\u6309\u7167 GitHub \u81ea\u52d5\u751f\u6210\u7684\u9806\u5e8f\uff09\uff1akrahets\u3001Gonglja\u3001nuomi1\u3001codingonion\u3001Reanon\u3001justin-tse\u3001hpstory\u3001danielsss\u3001curtishd\u3001night-cruise\u3001S-N-O-R-L-A-X\u3001msk397\u3001gvenusleo\u3001RiverTwilight\u3001gyt95\u3001zhuoqinyue\u3001Zuoxun\u3001mingXta\u3001hello-ikun\u3001khoaxuantu\u3001FangYuan33\u3001GN-Yu\u3001longsizhuo\u3001mgisr\u3001Cathay-Chen\u3001guowei-gong\u3001xBLACKICEx\u3001K3v123\u3001IsChristina\u3001JoseHung\u3001qualifier1024\u3001pengchzn\u3001Guanngxu\u3001QiLOL\u3001L-Super\u3001WSL0809\u3001Slone123c\u3001lhxsm\u3001yuan0221\u3001what-is-me\u3001rongyi\u3001JeffersonHuang\u3001longranger2\u3001theNefelibatas\u3001yuelinxin\u3001xiongsp\u3001nanlei\u3001a16su\u3001cy-by-side\u3001gaofer\u3001malone6\u3001Wonderdch\u3001hongyun-robot\u3001XiaChuerwu\u3001yd-j\u3001bluebean-cloud\u3001iron-irax\u3001he-weilai\u3001Nigh\u3001MolDuM\u3001Phoenix0415\u3001XC-Zero\u3001SamJin98\u3001reeswell\u3001NI-SW\u3001Horbin-Magician\u3001xjr7670\u3001YangXuanyi\u3001DullSword\u3001iStig\u3001qq909244296\u3001jiaxianhua\u3001wenjianmin\u3001keshida\u3001kilikilikid\u3001lclc6\u3001lwbaptx\u3001luluxia\u3001boloboloda\u3001hts0000\u3001gledfish\u3001fbigm\u3001echo1937\u3001szu17dmy\u3001dshlstarr\u3001coderlef\u3001czruby\u3001beintentional\u3001KeiichiKasai\u3001xb534\u3001ElaBosak233\u3001baagod\u3001zhouLion\u3001yishangzhang\u3001yi427\u3001yabo083\u3001weibk\u3001wangwang105\u3001th1nk3r-ing\u3001tao363\u30014yDX3906\u3001syd168\u3001siqyka\u3001selear\u3001sdshaoda\u3001noobcodemaker\u3001chadyi\u3001lyl625760\u3001lucaswangdev\u3001liuxjerry\u30010130w\u3001shanghai-Jerry\u3001JackYang-hellobobo\u3001Javesun99\u3001lipusheng\u3001ShiMaRing\u3001FreddieLi\u3001FloranceYeh\u3001Transmigration-zhou\u3001fanchenggang\u3001gltianwen\u3001Dr-XYZ\u3001curly210102\u3001CuB3y0nd\u3001youshaoXG\u3001bubble9um\u3001fanenr\u300152coder\u3001foursevenlove\u3001KorsChen\u3001ZongYangL\u3001hezhizhen\u3001linzeyan\u3001ZJKung\u3001GaochaoZhu\u3001yang-le\u3001Evilrabbit520\u3001Turing-1024-Lee\u3001Suremotoo\u3001Allen-Scai\u3001Richard-Zhang1019\u3001qingpeng9802\u3001primexiao\u3001nidhoggfgg\u30011ch0\u3001MwumLi\u3001ZnYang2018\u3001hugtyftg\u3001logan-qiu\u3001psychelzh \u548c Keynman \u3002</p> <p>\u672c\u66f8\u7684\u7a0b\u5f0f\u78bc\u5be9\u95b1\u5de5\u4f5c\u7531 codingonion\u3001curtishd\u3001Gonglja\u3001gvenusleo\u3001hpstory\u3001justin-tse\u3001krahets\u3001night-cruise\u3001nuomi1 \u548c Reanon \u5b8c\u6210\uff08\u6309\u7167\u9996\u5b57\u6bcd\u9806\u5e8f\u6392\u5217\uff09\u3002\u611f\u8b1d\u4ed6\u5011\u4ed8\u51fa\u7684\u6642\u9593\u8207\u7cbe\u529b\uff0c\u6b63\u662f\u4ed6\u5011\u78ba\u4fdd\u4e86\u5404\u8a9e\u8a00\u7a0b\u5f0f\u78bc\u7684\u898f\u7bc4\u8207\u7d71\u4e00\u3002</p> <p>\u5728\u672c\u66f8\u7684\u5275\u4f5c\u904e\u7a0b\u4e2d\uff0c\u6211\u5f97\u5230\u4e86\u8a31\u591a\u4eba\u7684\u5e6b\u52a9\u3002</p> <ul> <li>\u611f\u8b1d\u6211\u5728\u516c\u53f8\u7684\u5c0e\u5e2b\u674e\u6c50\u535a\u58eb\uff0c\u5728\u4e00\u6b21\u66a2\u8ac7\u4e2d\u4f60\u9f13\u52f5\u6211\u201c\u5feb\u884c\u52d5\u8d77\u4f86\u201d\uff0c\u5805\u5b9a\u4e86\u6211\u5beb\u9019\u672c\u66f8\u7684\u6c7a\u5fc3\uff1b</li> <li>\u611f\u8b1d\u6211\u7684\u5973\u670b\u53cb\u6ce1\u6ce1\u4f5c\u70ba\u672c\u66f8\u7684\u9996\u4f4d\u8b80\u8005\uff0c\u5f9e\u6f14\u7b97\u6cd5\u5c0f\u767d\u7684\u89d2\u5ea6\u63d0\u51fa\u8a31\u591a\u5bf6\u8cb4\u5efa\u8b70\uff0c\u4f7f\u5f97\u672c\u66f8\u66f4\u9069\u5408\u65b0\u624b\u95b1\u8b80\uff1b</li> <li>\u611f\u8b1d\u9a30\u5bf6\u3001\u7426\u5bf6\u3001\u98db\u5bf6\u70ba\u672c\u66f8\u8d77\u4e86\u4e00\u500b\u5bcc\u6709\u5275\u610f\u7684\u540d\u5b57\uff0c\u559a\u8d77\u5927\u5bb6\u5beb\u4e0b\u7b2c\u4e00\u884c\u7a0b\u5f0f\u78bc\u201cHello World!\u201d\u7684\u7f8e\u597d\u56de\u61b6\uff1b</li> <li>\u611f\u8b1d\u6821\u9293\u5728\u667a\u6167\u8ca1\u7522\u6b0a\u65b9\u9762\u63d0\u4f9b\u7684\u5c08\u696d\u5e6b\u52a9\uff0c\u9019\u5c0d\u672c\u958b\u6e90\u66f8\u7684\u5b8c\u5584\u8d77\u5230\u4e86\u91cd\u8981\u4f5c\u7528\uff1b</li> <li>\u611f\u8b1d\u8607\u6f7c\u70ba\u672c\u66f8\u8a2d\u8a08\u4e86\u7cbe\u7f8e\u7684\u5c01\u9762\u548c logo \uff0c\u4e26\u5728\u6211\u7684\u5f37\u8feb\u75c7\u7684\u9a45\u4f7f\u4e0b\u591a\u6b21\u8010\u5fc3\u4fee\u6539\uff1b</li> <li>\u611f\u8b1d @squidfunk \u63d0\u4f9b\u7684\u6392\u7248\u5efa\u8b70\uff0c\u4ee5\u53ca\u4ed6\u958b\u767c\u7684\u958b\u6e90\u6587\u4ef6\u4e3b\u984c Material-for-MkDocs \u3002</li> </ul> <p>\u5728\u5beb\u4f5c\u904e\u7a0b\u4e2d\uff0c\u6211\u95b1\u8b80\u4e86\u8a31\u591a\u95dc\u65bc\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u7684\u6559\u6750\u548c\u6587\u7ae0\u3002\u9019\u4e9b\u4f5c\u54c1\u70ba\u672c\u66f8\u63d0\u4f9b\u4e86\u512a\u79c0\u7684\u7bc4\u672c\uff0c\u78ba\u4fdd\u4e86\u672c\u66f8\u5167\u5bb9\u7684\u6e96\u78ba\u6027\u8207\u54c1\u8cea\u3002\u5728\u6b64\u611f\u8b1d\u6240\u6709\u8001\u5e2b\u548c\u524d\u8f29\u7684\u5091\u51fa\u8ca2\u737b\uff01</p> <p>\u672c\u66f8\u5021\u5c0e\u624b\u8166\u4e26\u7528\u7684\u5b78\u7fd2\u65b9\u5f0f\uff0c\u5728\u9019\u4e00\u9ede\u4e0a\u6211\u6df1\u53d7\u300a\u52d5\u624b\u5b78\u6df1\u5ea6\u5b78\u7fd2\u300b\u7684\u555f\u767c\u3002\u5728\u6b64\u5411\u5404\u4f4d\u8b80\u8005\u5f37\u70c8\u63a8\u85a6\u9019\u672c\u512a\u79c0\u7684\u8457\u4f5c\u3002</p> <p>\u8877\u5fc3\u611f\u8b1d\u6211\u7684\u7236\u6bcd\uff0c\u6b63\u662f\u4f60\u5011\u4e00\u76f4\u4ee5\u4f86\u7684\u652f\u6301\u8207\u9f13\u52f5\uff0c\u8b93\u6211\u6709\u6a5f\u6703\u505a\u9019\u4ef6\u5bcc\u6709\u8da3\u5473\u7684\u4e8b\u3002</p>"},{"location":"chapter_preface/suggestions/","title":"0.2 \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u66f8","text":"<p>Tip</p> <p>\u70ba\u4e86\u7372\u5f97\u6700\u4f73\u7684\u95b1\u8b80\u9ad4\u9a57\uff0c\u5efa\u8b70\u4f60\u901a\u8b80\u672c\u7bc0\u5167\u5bb9\u3002</p>"},{"location":"chapter_preface/suggestions/#021","title":"0.2.1 \u00a0 \u884c\u6587\u98a8\u683c\u7d04\u5b9a","text":"<ul> <li>\u6a19\u984c\u5f8c\u6a19\u8a3b <code>*</code> \u7684\u662f\u9078\u8b80\u7ae0\u7bc0\uff0c\u5167\u5bb9\u76f8\u5c0d\u56f0\u96e3\u3002\u5982\u679c\u4f60\u7684\u6642\u9593\u6709\u9650\uff0c\u53ef\u4ee5\u5148\u8df3\u904e\u3002</li> <li>\u5c08\u696d\u8853\u8a9e\u6703\u4f7f\u7528\u9ed1\u9ad4\uff08\u7d19\u8cea\u7248\u548c PDF \u7248\uff09\u6216\u65b0\u589e\u4e0b\u5283\u7dda\uff08\u7db2\u9801\u7248\uff09\uff0c\u4f8b\u5982\u9663\u5217\uff08array\uff09\u3002\u5efa\u8b70\u8a18\u4f4f\u5b83\u5011\uff0c\u4ee5\u4fbf\u95b1\u8b80\u6587\u737b\u3002</li> <li>\u91cd\u9ede\u5167\u5bb9\u548c\u7e3d\u7d50\u6027\u8a9e\u53e5\u6703 \u52a0\u7c97\uff0c\u9019\u985e\u6587\u5b57\u503c\u5f97\u7279\u5225\u95dc\u6ce8\u3002</li> <li>\u6709\u7279\u6307\u542b\u7fa9\u7684\u8a5e\u53e5\u6703\u4f7f\u7528\u201c\u5f15\u865f\u201d\u6a19\u8a3b\uff0c\u4ee5\u907f\u514d\u6b67\u7fa9\u3002</li> <li>\u7576\u6d89\u53ca\u7a0b\u5f0f\u8a9e\u8a00\u4e4b\u9593\u4e0d\u4e00\u81f4\u7684\u540d\u8a5e\u6642\uff0c\u672c\u66f8\u5747\u4ee5 Python \u70ba\u6e96\uff0c\u4f8b\u5982\u4f7f\u7528 <code>None</code> \u4f86\u8868\u793a\u201c\u7a7a\u201d\u3002</li> <li>\u672c\u66f8\u90e8\u5206\u653e\u68c4\u4e86\u7a0b\u5f0f\u8a9e\u8a00\u7684\u8a3b\u91cb\u898f\u7bc4\uff0c\u4ee5\u63db\u53d6\u66f4\u52a0\u7dca\u6e4a\u7684\u5167\u5bb9\u6392\u7248\u3002\u8a3b\u91cb\u4e3b\u8981\u5206\u70ba\u4e09\u7a2e\u985e\u578b\uff1a\u6a19\u984c\u8a3b\u91cb\u3001\u5167\u5bb9\u8a3b\u91cb\u3001\u591a\u884c\u8a3b\u91cb\u3002</li> </ul> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code>\"\"\"\u6a19\u984c\u8a3b\u91cb\uff0c\u7528\u65bc\u6a19\u8a3b\u51fd\u5f0f\u3001\u985e\u5225\u3001\u6e2c\u8a66\u6a23\u4f8b\u7b49\"\"\"\n\n# \u5167\u5bb9\u8a3b\u91cb\uff0c\u7528\u65bc\u8a73\u89e3\u7a0b\u5f0f\u78bc\n\n\"\"\"\n\u591a\u884c\n\u8a3b\u91cb\n\"\"\"\n</code></pre> <pre><code>/* \u6a19\u984c\u8a3b\u91cb\uff0c\u7528\u65bc\u6a19\u8a3b\u51fd\u5f0f\u3001\u985e\u5225\u3001\u6e2c\u8a66\u6a23\u4f8b\u7b49 */\n\n// \u5167\u5bb9\u8a3b\u91cb\uff0c\u7528\u65bc\u8a73\u89e3\u7a0b\u5f0f\u78bc\n\n/**\n * \u591a\u884c\n * \u8a3b\u91cb\n */\n</code></pre> <pre><code>/* \u6a19\u984c\u8a3b\u91cb\uff0c\u7528\u65bc\u6a19\u8a3b\u51fd\u5f0f\u3001\u985e\u5225\u3001\u6e2c\u8a66\u6a23\u4f8b\u7b49 */\n\n// \u5167\u5bb9\u8a3b\u91cb\uff0c\u7528\u65bc\u8a73\u89e3\u7a0b\u5f0f\u78bc\n\n/**\n * \u591a\u884c\n * \u8a3b\u91cb\n */\n</code></pre> <pre><code>/* \u6a19\u984c\u8a3b\u91cb\uff0c\u7528\u65bc\u6a19\u8a3b\u51fd\u5f0f\u3001\u985e\u5225\u3001\u6e2c\u8a66\u6a23\u4f8b\u7b49 */\n\n// \u5167\u5bb9\u8a3b\u91cb\uff0c\u7528\u65bc\u8a73\u89e3\u7a0b\u5f0f\u78bc\n\n/**\n * \u591a\u884c\n * \u8a3b\u91cb\n */\n</code></pre> <pre><code>/* \u6a19\u984c\u8a3b\u91cb\uff0c\u7528\u65bc\u6a19\u8a3b\u51fd\u5f0f\u3001\u985e\u5225\u3001\u6e2c\u8a66\u6a23\u4f8b\u7b49 */\n\n// \u5167\u5bb9\u8a3b\u91cb\uff0c\u7528\u65bc\u8a73\u89e3\u7a0b\u5f0f\u78bc\n\n/**\n * \u591a\u884c\n * \u8a3b\u91cb\n */\n</code></pre> <pre><code>/* \u6a19\u984c\u8a3b\u91cb\uff0c\u7528\u65bc\u6a19\u8a3b\u51fd\u5f0f\u3001\u985e\u5225\u3001\u6e2c\u8a66\u6a23\u4f8b\u7b49 */\n\n// \u5167\u5bb9\u8a3b\u91cb\uff0c\u7528\u65bc\u8a73\u89e3\u7a0b\u5f0f\u78bc\n\n/**\n * \u591a\u884c\n * \u8a3b\u91cb\n */\n</code></pre> <pre><code>/* \u6a19\u984c\u8a3b\u91cb\uff0c\u7528\u65bc\u6a19\u8a3b\u51fd\u5f0f\u3001\u985e\u5225\u3001\u6e2c\u8a66\u6a23\u4f8b\u7b49 */\n\n// \u5167\u5bb9\u8a3b\u91cb\uff0c\u7528\u65bc\u8a73\u89e3\u7a0b\u5f0f\u78bc\n\n/**\n * \u591a\u884c\n * \u8a3b\u91cb\n */\n</code></pre> <pre><code>/* \u6a19\u984c\u8a3b\u91cb\uff0c\u7528\u65bc\u6a19\u8a3b\u51fd\u5f0f\u3001\u985e\u5225\u3001\u6e2c\u8a66\u6a23\u4f8b\u7b49 */\n\n// \u5167\u5bb9\u8a3b\u91cb\uff0c\u7528\u65bc\u8a73\u89e3\u7a0b\u5f0f\u78bc\n\n/**\n * \u591a\u884c\n * \u8a3b\u91cb\n */\n</code></pre> <pre><code>/* \u6a19\u984c\u8a3b\u91cb\uff0c\u7528\u65bc\u6a19\u8a3b\u51fd\u5f0f\u3001\u985e\u5225\u3001\u6e2c\u8a66\u6a23\u4f8b\u7b49 */\n\n// \u5167\u5bb9\u8a3b\u91cb\uff0c\u7528\u65bc\u8a73\u89e3\u7a0b\u5f0f\u78bc\n\n/**\n * \u591a\u884c\n * \u8a3b\u91cb\n */\n</code></pre> <pre><code>/* \u6a19\u984c\u8a3b\u91cb\uff0c\u7528\u65bc\u6a19\u8a3b\u51fd\u5f0f\u3001\u985e\u5225\u3001\u6e2c\u8a66\u6a23\u4f8b\u7b49 */\n\n// \u5167\u5bb9\u8a3b\u91cb\uff0c\u7528\u65bc\u8a73\u89e3\u7a0b\u5f0f\u78bc\n\n/**\n * \u591a\u884c\n * \u8a3b\u91cb\n */\n</code></pre> <pre><code>/* \u6a19\u984c\u8a3b\u91cb\uff0c\u7528\u65bc\u6a19\u8a3b\u51fd\u5f0f\u3001\u985e\u5225\u3001\u6e2c\u8a66\u6a23\u4f8b\u7b49 */\n\n// \u5167\u5bb9\u8a3b\u91cb\uff0c\u7528\u65bc\u8a73\u89e3\u7a0b\u5f0f\u78bc\n\n/**\n * \u591a\u884c\n * \u8a3b\u91cb\n */\n</code></pre> <pre><code>/* \u6a19\u984c\u8a3b\u91cb\uff0c\u7528\u65bc\u6a19\u8a3b\u51fd\u5f0f\u3001\u985e\u5225\u3001\u6e2c\u8a66\u6a23\u4f8b\u7b49 */\n\n// \u5167\u5bb9\u8a3b\u91cb\uff0c\u7528\u65bc\u8a73\u89e3\u7a0b\u5f0f\u78bc\n\n/**\n * \u591a\u884c\n * \u8a3b\u91cb\n */\n</code></pre> <pre><code>### \u6a19\u984c\u8a3b\u91cb\uff0c\u7528\u65bc\u6a19\u8a3b\u51fd\u5f0f\u3001\u985e\u5225\u3001\u6e2c\u8a66\u6a23\u4f8b\u7b49 ###\n\n# \u5167\u5bb9\u8a3b\u91cb\uff0c\u7528\u65bc\u8a73\u89e3\u7a0b\u5f0f\u78bc\n\n# \u591a\u884c\n# \u8a3b\u91cb\n</code></pre> <pre><code>// \u6a19\u984c\u8a3b\u91cb\uff0c\u7528\u65bc\u6a19\u8a3b\u51fd\u5f0f\u3001\u985e\u5225\u3001\u6e2c\u8a66\u6a23\u4f8b\u7b49\n\n// \u5167\u5bb9\u8a3b\u91cb\uff0c\u7528\u65bc\u8a73\u89e3\u7a0b\u5f0f\u78bc\n\n// \u591a\u884c\n// \u8a3b\u91cb\n</code></pre>"},{"location":"chapter_preface/suggestions/#022","title":"0.2.2 \u00a0 \u5728\u52d5\u756b\u5716\u89e3\u4e2d\u9ad8\u6548\u5b78\u7fd2","text":"<p>\u76f8\u8f03\u65bc\u6587\u5b57\uff0c\u5f71\u7247\u548c\u5716\u7247\u5177\u6709\u66f4\u9ad8\u7684\u8cc7\u8a0a\u5bc6\u5ea6\u548c\u7d50\u69cb\u5316\u7a0b\u5ea6\uff0c\u66f4\u6613\u65bc\u7406\u89e3\u3002\u5728\u672c\u66f8\u4e2d\uff0c\u91cd\u9ede\u548c\u96e3\u9ede\u77e5\u8b58\u5c07\u4e3b\u8981\u900f\u904e\u52d5\u756b\u4ee5\u5716\u89e3\u5f62\u5f0f\u5c55\u793a\uff0c\u800c\u6587\u5b57\u5247\u4f5c\u70ba\u89e3\u91cb\u8207\u88dc\u5145\u3002</p> <p>\u5982\u679c\u4f60\u5728\u95b1\u8b80\u672c\u66f8\u6642\uff0c\u767c\u73fe\u67d0\u6bb5\u5167\u5bb9\u63d0\u4f9b\u77ad\u5982\u5716 0-2 \u6240\u793a\u7684\u52d5\u756b\u5716\u89e3\uff0c\u8acb\u4ee5\u5716\u70ba\u4e3b\u3001\u4ee5\u6587\u5b57\u70ba\u8f14\uff0c\u7d9c\u5408\u5169\u8005\u4f86\u7406\u89e3\u5167\u5bb9\u3002</p> <p></p> <p> \u5716 0-2 \u00a0 \u52d5\u756b\u5716\u89e3\u793a\u4f8b </p>"},{"location":"chapter_preface/suggestions/#023","title":"0.2.3 \u00a0 \u5728\u7a0b\u5f0f\u78bc\u5be6\u8e10\u4e2d\u52a0\u6df1\u7406\u89e3","text":"<p>\u672c\u66f8\u7684\u914d\u5957\u7a0b\u5f0f\u78bc\u8a17\u7ba1\u5728 GitHub \u5009\u5eab\u3002\u5982\u5716 0-3 \u6240\u793a\uff0c\u6e90\u7a0b\u5f0f\u78bc\u9644\u6709\u6e2c\u8a66\u6a23\u4f8b\uff0c\u53ef\u4e00\u9375\u57f7\u884c\u3002</p> <p>\u5982\u679c\u6642\u9593\u5141\u8a31\uff0c\u5efa\u8b70\u4f60\u53c3\u7167\u7a0b\u5f0f\u78bc\u81ea\u884c\u6572\u4e00\u904d\u3002\u5982\u679c\u5b78\u7fd2\u6642\u9593\u6709\u9650\uff0c\u8acb\u81f3\u5c11\u901a\u8b80\u4e26\u57f7\u884c\u6240\u6709\u7a0b\u5f0f\u78bc\u3002</p> <p>\u8207\u95b1\u8b80\u7a0b\u5f0f\u78bc\u76f8\u6bd4\uff0c\u7de8\u5beb\u7a0b\u5f0f\u78bc\u7684\u904e\u7a0b\u5f80\u5f80\u80fd\u5e36\u4f86\u66f4\u591a\u6536\u7a6b\u3002\u52d5\u624b\u5b78\uff0c\u624d\u662f\u771f\u7684\u5b78\u3002</p> <p></p> <p> \u5716 0-3 \u00a0 \u57f7\u884c\u7a0b\u5f0f\u78bc\u793a\u4f8b </p> <p>\u57f7\u884c\u7a0b\u5f0f\u78bc\u7684\u524d\u7f6e\u5de5\u4f5c\u4e3b\u8981\u5206\u70ba\u4e09\u6b65\u3002</p> <p>\u7b2c\u4e00\u6b65\uff1a\u5b89\u88dd\u672c\u5730\u7a0b\u5f0f\u8a2d\u8a08\u74b0\u5883\u3002\u8acb\u53c3\u7167\u9644\u9304\u6240\u793a\u7684\u6559\u7a0b\u9032\u884c\u5b89\u88dd\uff0c\u5982\u679c\u5df2\u5b89\u88dd\uff0c\u5247\u53ef\u8df3\u904e\u6b64\u6b65\u9a5f\u3002</p> <p>\u7b2c\u4e8c\u6b65\uff1a\u514b\u9686\u6216\u4e0b\u8f09\u7a0b\u5f0f\u78bc\u5009\u5eab\u3002\u524d\u5f80 GitHub \u5009\u5eab\u3002\u5982\u679c\u5df2\u7d93\u5b89\u88dd Git \uff0c\u53ef\u4ee5\u900f\u904e\u4ee5\u4e0b\u547d\u4ee4\u514b\u9686\u672c\u5009\u5eab\uff1a</p> <pre><code>git clone https://github.com/krahets/hello-algo.git\n</code></pre> <p>\u7576\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728\u5716 0-4 \u6240\u793a\u7684\u4f4d\u7f6e\uff0c\u9ede\u9078\u201cDownload ZIP\u201d\u6309\u9215\u76f4\u63a5\u4e0b\u8f09\u7a0b\u5f0f\u78bc\u58d3\u7e2e\u5305\uff0c\u7136\u5f8c\u5728\u672c\u5730\u89e3\u58d3\u5373\u53ef\u3002</p> <p></p> <p> \u5716 0-4 \u00a0 \u514b\u9686\u5009\u5eab\u8207\u4e0b\u8f09\u7a0b\u5f0f\u78bc </p> <p>\u7b2c\u4e09\u6b65\uff1a\u57f7\u884c\u6e90\u7a0b\u5f0f\u78bc\u3002\u5982\u5716 0-5 \u6240\u793a\uff0c\u5c0d\u65bc\u9802\u90e8\u6a19\u6709\u6a94\u6848\u540d\u7a31\u7684\u7a0b\u5f0f\u78bc\u584a\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u5009\u5eab\u7684 <code>codes</code> \u6a94\u6848\u593e\u5167\u627e\u5230\u5c0d\u61c9\u7684\u6e90\u7a0b\u5f0f\u78bc\u6a94\u6848\u3002\u6e90\u7a0b\u5f0f\u78bc\u6a94\u6848\u53ef\u4e00\u9375\u57f7\u884c\uff0c\u5c07\u5e6b\u52a9\u4f60\u7bc0\u7701\u4e0d\u5fc5\u8981\u7684\u9664\u932f\u6642\u9593\uff0c\u8b93\u4f60\u80fd\u5920\u5c08\u6ce8\u65bc\u5b78\u7fd2\u5167\u5bb9\u3002</p> <p></p> <p> \u5716 0-5 \u00a0 \u7a0b\u5f0f\u78bc\u584a\u8207\u5c0d\u61c9\u7684\u6e90\u7a0b\u5f0f\u78bc\u6a94\u6848 </p> <p>\u9664\u4e86\u672c\u5730\u57f7\u884c\u7a0b\u5f0f\u78bc\uff0c\u7db2\u9801\u7248\u9084\u652f\u6301 Python \u7a0b\u5f0f\u78bc\u7684\u8996\u89ba\u5316\u57f7\u884c\uff08\u57fa\u65bc pythontutor \u5be6\u73fe\uff09\u3002\u5982\u5716 0-6 \u6240\u793a\uff0c\u4f60\u53ef\u4ee5\u9ede\u9078\u7a0b\u5f0f\u78bc\u584a\u4e0b\u65b9\u7684\u201c\u8996\u89ba\u5316\u57f7\u884c\u201d\u4f86\u5c55\u958b\u6aa2\u8996\uff0c\u89c0\u5bdf\u6f14\u7b97\u6cd5\u7a0b\u5f0f\u78bc\u7684\u57f7\u884c\u904e\u7a0b\uff1b\u4e5f\u53ef\u4ee5\u9ede\u9078\u201c\u5168\u5c4f\u89c0\u770b\u201d\uff0c\u4ee5\u7372\u5f97\u66f4\u597d\u7684\u95b1\u89bd\u9ad4\u9a57\u3002</p> <p></p> <p> \u5716 0-6 \u00a0 Python \u7a0b\u5f0f\u78bc\u7684\u8996\u89ba\u5316\u57f7\u884c </p>"},{"location":"chapter_preface/suggestions/#024","title":"0.2.4 \u00a0 \u5728\u63d0\u554f\u8a0e\u8ad6\u4e2d\u5171\u540c\u6210\u9577","text":"<p>\u5728\u95b1\u8b80\u672c\u66f8\u6642\uff0c\u8acb\u4e0d\u8981\u8f15\u6613\u8df3\u904e\u90a3\u4e9b\u6c92\u5b78\u660e\u767d\u7684\u77e5\u8b58\u9ede\u3002\u6b61\u8fce\u5728\u8a55\u8ad6\u5340\u63d0\u51fa\u4f60\u7684\u554f\u984c\uff0c\u6211\u548c\u5c0f\u5925\u4f34\u5011\u5c07\u7aed\u8aa0\u70ba\u4f60\u89e3\u7b54\uff0c\u4e00\u822c\u60c5\u6cc1\u4e0b\u53ef\u5728\u5169\u5929\u5167\u56de\u8986\u3002</p> <p>\u5982\u5716 0-7 \u6240\u793a\uff0c\u7db2\u9801\u7248\u6bcf\u500b\u7ae0\u7bc0\u7684\u5e95\u90e8\u90fd\u914d\u6709\u8a55\u8ad6\u5340\u3002\u5e0c\u671b\u4f60\u80fd\u591a\u95dc\u6ce8\u8a55\u8ad6\u5340\u7684\u5167\u5bb9\u3002\u4e00\u65b9\u9762\uff0c\u4f60\u53ef\u4ee5\u77ad\u89e3\u5927\u5bb6\u9047\u5230\u7684\u554f\u984c\uff0c\u5f9e\u800c\u67e5\u6f0f\u88dc\u7f3a\uff0c\u6fc0\u767c\u66f4\u6df1\u5165\u7684\u601d\u8003\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u671f\u5f85\u4f60\u80fd\u6177\u6168\u5730\u56de\u7b54\u5176\u4ed6\u5c0f\u5925\u4f34\u7684\u554f\u984c\uff0c\u5206\u4eab\u4f60\u7684\u898b\u89e3\uff0c\u5e6b\u52a9\u4ed6\u4eba\u9032\u6b65\u3002</p> <p></p> <p> \u5716 0-7 \u00a0 \u8a55\u8ad6\u5340\u793a\u4f8b </p>"},{"location":"chapter_preface/suggestions/#025","title":"0.2.5 \u00a0 \u6f14\u7b97\u6cd5\u5b78\u7fd2\u8def\u7dda","text":"<p>\u5f9e\u7e3d\u9ad4\u4e0a\u770b\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u5b78\u7fd2\u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u7684\u904e\u7a0b\u5283\u5206\u70ba\u4e09\u500b\u968e\u6bb5\u3002</p> <ol> <li>\u968e\u6bb5\u4e00\uff1a\u6f14\u7b97\u6cd5\u5165\u9580\u3002\u6211\u5011\u9700\u8981\u719f\u6089\u5404\u7a2e\u8cc7\u6599\u7d50\u69cb\u7684\u7279\u9ede\u548c\u7528\u6cd5\uff0c\u5b78\u7fd2\u4e0d\u540c\u6f14\u7b97\u6cd5\u7684\u539f\u7406\u3001\u6d41\u7a0b\u3001\u7528\u9014\u548c\u6548\u7387\u7b49\u65b9\u9762\u7684\u5167\u5bb9\u3002</li> <li>\u968e\u6bb5\u4e8c\uff1a\u5237\u6f14\u7b97\u6cd5\u984c\u3002\u5efa\u8b70\u5f9e\u71b1\u9580\u984c\u76ee\u958b\u5237\uff0c\u5148\u7a4d\u7d2f\u81f3\u5c11 100 \u9053\u984c\u76ee\uff0c\u719f\u6089\u4e3b\u6d41\u7684\u6f14\u7b97\u6cd5\u554f\u984c\u3002\u521d\u6b21\u5237\u984c\u6642\uff0c\u201c\u77e5\u8b58\u907a\u5fd8\u201d\u53ef\u80fd\u662f\u4e00\u500b\u6311\u6230\uff0c\u4f46\u8acb\u653e\u5fc3\uff0c\u9019\u662f\u5f88\u6b63\u5e38\u7684\u3002\u6211\u5011\u53ef\u4ee5\u6309\u7167\u201c\u827e\u8cd3\u6d69\u65af\u907a\u5fd8\u66f2\u7dda\u201d\u4f86\u8907\u7fd2\u984c\u76ee\uff0c\u901a\u5e38\u5728\u9032\u884c 3\uff5e5 \u8f2a\u7684\u91cd\u8907\u5f8c\uff0c\u5c31\u80fd\u5c07\u5176\u7262\u8a18\u5728\u5fc3\u3002\u63a8\u85a6\u7684\u984c\u55ae\u548c\u5237\u984c\u8a08\u5283\u8acb\u898b\u6b64 GitHub \u5009\u5eab\u3002</li> <li>\u968e\u6bb5\u4e09\uff1a\u642d\u5efa\u77e5\u8b58\u9ad4\u7cfb\u3002\u5728\u5b78\u7fd2\u65b9\u9762\uff0c\u6211\u5011\u53ef\u4ee5\u95b1\u8b80\u6f14\u7b97\u6cd5\u5c08\u6b04\u6587\u7ae0\u3001\u89e3\u984c\u6846\u67b6\u548c\u6f14\u7b97\u6cd5\u6559\u6750\uff0c\u4ee5\u4e0d\u65b7\u8c50\u5bcc\u77e5\u8b58\u9ad4\u7cfb\u3002\u5728\u5237\u984c\u65b9\u9762\uff0c\u53ef\u4ee5\u5617\u8a66\u63a1\u7528\u9032\u968e\u5237\u984c\u7b56\u7565\uff0c\u5982\u6309\u5c08\u984c\u5206\u985e\u3001\u4e00\u984c\u591a\u89e3\u3001\u4e00\u89e3\u591a\u984c\u7b49\uff0c\u76f8\u95dc\u7684\u5237\u984c\u5fc3\u5f97\u53ef\u4ee5\u5728\u5404\u500b\u793e\u7fa4\u627e\u5230\u3002</li> </ol> <p>\u5982\u5716 0-8 \u6240\u793a\uff0c\u672c\u66f8\u5167\u5bb9\u4e3b\u8981\u6db5\u84cb\u201c\u968e\u6bb5\u4e00\u201d\uff0c\u65e8\u5728\u5e6b\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5c55\u958b\u968e\u6bb5\u4e8c\u548c\u968e\u6bb5\u4e09\u7684\u5b78\u7fd2\u3002</p> <p></p> <p> \u5716 0-8 \u00a0 \u6f14\u7b97\u6cd5\u5b78\u7fd2\u8def\u7dda </p>"},{"location":"chapter_preface/summary/","title":"0.3 \u00a0 \u5c0f\u7d50","text":"<ul> <li>\u672c\u66f8\u7684\u4e3b\u8981\u53d7\u773e\u662f\u6f14\u7b97\u6cd5\u521d\u5b78\u8005\u3002\u5982\u679c\u4f60\u5df2\u6709\u4e00\u5b9a\u57fa\u790e\uff0c\u672c\u66f8\u80fd\u5e6b\u52a9\u4f60\u7cfb\u7d71\u56de\u9867\u6f14\u7b97\u6cd5\u77e5\u8b58\uff0c\u66f8\u4e2d\u6e90\u7a0b\u5f0f\u78bc\u4e5f\u53ef\u4f5c\u70ba\u201c\u5237\u984c\u5de5\u5177\u5eab\u201d\u4f7f\u7528\u3002</li> <li>\u66f8\u4e2d\u5167\u5bb9\u4e3b\u8981\u5305\u62ec\u8907\u96dc\u5ea6\u5206\u6790\u3001\u8cc7\u6599\u7d50\u69cb\u548c\u6f14\u7b97\u6cd5\u4e09\u90e8\u5206\uff0c\u6db5\u84cb\u4e86\u8a72\u9818\u57df\u7684\u5927\u90e8\u5206\u4e3b\u984c\u3002</li> <li>\u5c0d\u65bc\u6f14\u7b97\u6cd5\u65b0\u624b\uff0c\u5728\u521d\u5b78\u968e\u6bb5\u95b1\u8b80\u4e00\u672c\u5165\u9580\u66f8\u81f3\u95dc\u91cd\u8981\uff0c\u53ef\u4ee5\u5c11\u8d70\u8a31\u591a\u5f4e\u8def\u3002</li> <li>\u66f8\u4e2d\u7684\u52d5\u756b\u5716\u89e3\u901a\u5e38\u7528\u65bc\u4ecb\u7d39\u91cd\u9ede\u548c\u96e3\u9ede\u77e5\u8b58\u3002\u95b1\u8b80\u672c\u66f8\u6642\uff0c\u61c9\u7d66\u4e88\u9019\u4e9b\u5167\u5bb9\u66f4\u591a\u95dc\u6ce8\u3002</li> <li>\u5be6\u8e10\u4e43\u5b78\u7fd2\u7a0b\u5f0f\u8a2d\u8a08\u4e4b\u6700\u4f73\u9014\u5f91\u3002\u5f37\u70c8\u5efa\u8b70\u57f7\u884c\u6e90\u7a0b\u5f0f\u78bc\u4e26\u89aa\u81ea\u6572\u7a0b\u5f0f\u78bc\u3002</li> <li>\u672c\u66f8\u7db2\u9801\u7248\u7684\u6bcf\u500b\u7ae0\u7bc0\u90fd\u8a2d\u6709\u8a55\u8ad6\u5340\uff0c\u6b61\u8fce\u96a8\u6642\u5206\u4eab\u4f60\u7684\u7591\u60d1\u8207\u898b\u89e3\u3002</li> </ul>"},{"location":"chapter_reference/","title":"\u53c3\u8003\u6587\u737b","text":"<p>[1] Thomas H. Cormen, et al. Introduction to Algorithms (3<sup>rd</sup> Edition).</p> <p>[2] Aditya Bhargava. Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People (1<sup>st</sup> Edition).</p> <p>[3] Robert Sedgewick, et al. Algorithms (4<sup>th</sup> Edition).</p> <p>[4] \u56b4\u851a\u654f. \u8cc7\u6599\u7d50\u69cb\uff08C \u8a9e\u8a00\u7248\uff09.</p> <p>[5] \u9127\u4fca\u8f1d. \u8cc7\u6599\u7d50\u69cb\uff08C++ \u8a9e\u8a00\u7248\uff0c\u7b2c\u4e09\u7248\uff09.</p> <p>[6] \u99ac\u514b \u827e\u502b \u7dad\u65af\u8457\uff0c\u9673\u8d8a\u8b6f. \u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u5206\u6790\uff1aJava\u8a9e\u8a00\u63cf\u8ff0\uff08\u7b2c\u4e09\u7248\uff09.</p> <p>[7] \u7a0b\u5091. \u5927\u8a71\u8cc7\u6599\u7d50\u69cb.</p> <p>[8] \u738b\u722d. \u8cc7\u6599\u7d50\u69cb\u8207\u6f14\u7b97\u6cd5\u4e4b\u7f8e.</p> <p>[9] Gayle Laakmann McDowell. Cracking the Coding Interview: 189 Programming Questions and Solutions (6<sup>th</sup> Edition).</p> <p>[10] Aston Zhang, et al. Dive into Deep Learning.</p>"},{"location":"chapter_searching/","title":"\u7b2c 10 \u7ae0 \u00a0 \u641c\u5c0b","text":"<p>Abstract</p> <p>\u641c\u5c0b\u662f\u4e00\u5834\u672a\u77e5\u7684\u5192\u96aa\uff0c\u6211\u5011\u6216\u8a31\u9700\u8981\u8d70\u904d\u795e\u79d8\u7a7a\u9593\u7684\u6bcf\u500b\u89d2\u843d\uff0c\u53c8\u6216\u8a31\u53ef\u4ee5\u5feb\u901f\u9396\u5b9a\u76ee\u6a19\u3002</p> <p>\u5728\u9019\u5834\u5c0b\u8993\u4e4b\u65c5\u4e2d\uff0c\u6bcf\u4e00\u6b21\u63a2\u7d22\u90fd\u53ef\u80fd\u5f97\u5230\u4e00\u500b\u672a\u66fe\u6599\u60f3\u7684\u7b54\u6848\u3002</p>"},{"location":"chapter_searching/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>10.1 \u00a0 \u4e8c\u5206\u641c\u5c0b</li> <li>10.2 \u00a0 \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede</li> <li>10.3 \u00a0 \u4e8c\u5206\u641c\u5c0b\u908a\u754c</li> <li>10.4 \u00a0 \u96dc\u6e4a\u6700\u4f73\u5316\u7b56\u7565</li> <li>10.5 \u00a0 \u91cd\u8b58\u641c\u5c0b\u6f14\u7b97\u6cd5</li> <li>10.6 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_searching/binary_search/","title":"10.1 \u00a0 \u4e8c\u5206\u641c\u5c0b","text":"<p>\u4e8c\u5206\u641c\u5c0b\uff08binary search\uff09\u662f\u4e00\u7a2e\u57fa\u65bc\u5206\u6cbb\u7b56\u7565\u7684\u9ad8\u6548\u641c\u5c0b\u6f14\u7b97\u6cd5\u3002\u5b83\u5229\u7528\u8cc7\u6599\u7684\u6709\u5e8f\u6027\uff0c\u6bcf\u8f2a\u7e2e\u5c0f\u4e00\u534a\u641c\u5c0b\u7bc4\u570d\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6a19\u5143\u7d20\u6216\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u70ba\u6b62\u3002</p> <p>Question</p> <p>\u7d66\u5b9a\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u9663\u5217 <code>nums</code> \uff0c\u5143\u7d20\u6309\u5f9e\u5c0f\u5230\u5927\u7684\u9806\u5e8f\u6392\u5217\u4e14\u4e0d\u91cd\u8907\u3002\u8acb\u67e5\u8a62\u4e26\u8fd4\u56de\u5143\u7d20 <code>target</code> \u5728\u8a72\u9663\u5217\u4e2d\u7684\u7d22\u5f15\u3002\u82e5\u9663\u5217\u4e0d\u5305\u542b\u8a72\u5143\u7d20\uff0c\u5247\u8fd4\u56de \\(-1\\) \u3002\u793a\u4f8b\u5982\u5716 10-1 \u6240\u793a\u3002</p> <p></p> <p> \u5716 10-1 \u00a0 \u4e8c\u5206\u641c\u5c0b\u793a\u4f8b\u8cc7\u6599 </p> <p>\u5982\u5716 10-2 \u6240\u793a\uff0c\u6211\u5011\u5148\u521d\u59cb\u5316\u6307\u6a19 \\(i = 0\\) \u548c \\(j = n - 1\\) \uff0c\u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u548c\u5c3e\u5143\u7d20\uff0c\u4ee3\u8868\u641c\u5c0b\u5340\u9593 \\([0, n - 1]\\) \u3002\u8acb\u6ce8\u610f\uff0c\u4e2d\u62ec\u865f\u8868\u793a\u9589\u5340\u9593\uff0c\u5176\u5305\u542b\u908a\u754c\u503c\u672c\u8eab\u3002</p> <p>\u63a5\u4e0b\u4f86\uff0c\u8ff4\u5708\u57f7\u884c\u4ee5\u4e0b\u5169\u6b65\u3002</p> <ol> <li>\u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 \\(m = \\lfloor {(i + j) / 2} \\rfloor\\) \uff0c\u5176\u4e2d \\(\\lfloor \\: \\rfloor\\) \u8868\u793a\u5411\u4e0b\u53d6\u6574\u64cd\u4f5c\u3002</li> <li>\u5224\u65b7 <code>nums[m]</code> \u548c <code>target</code> \u7684\u5927\u5c0f\u95dc\u4fc2\uff0c\u5206\u70ba\u4ee5\u4e0b\u4e09\u7a2e\u60c5\u6cc1\u3002<ol> <li>\u7576 <code>nums[m] &lt; target</code> \u6642\uff0c\u8aaa\u660e <code>target</code> \u5728\u5340\u9593 \\([m + 1, j]\\) \u4e2d\uff0c\u56e0\u6b64\u57f7\u884c \\(i = m + 1\\) \u3002</li> <li>\u7576 <code>nums[m] &gt; target</code> \u6642\uff0c\u8aaa\u660e <code>target</code> \u5728\u5340\u9593 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u57f7\u884c \\(j = m - 1\\) \u3002</li> <li>\u7576 <code>nums[m] = target</code> \u6642\uff0c\u8aaa\u660e\u627e\u5230 <code>target</code> \uff0c\u56e0\u6b64\u8fd4\u56de\u7d22\u5f15 \\(m\\) \u3002</li> </ol> </li> </ol> <p>\u82e5\u9663\u5217\u4e0d\u5305\u542b\u76ee\u6a19\u5143\u7d20\uff0c\u641c\u5c0b\u5340\u9593\u6700\u7d42\u6703\u7e2e\u5c0f\u70ba\u7a7a\u3002\u6b64\u6642\u8fd4\u56de \\(-1\\) \u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 10-2 \u00a0 \u4e8c\u5206\u641c\u5c0b\u6d41\u7a0b </p> <p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u65bc \\(i\\) \u548c \\(j\\) \u90fd\u662f <code>int</code> \u578b\u5225\uff0c\u56e0\u6b64 \\(i + j\\) \u53ef\u80fd\u6703\u8d85\u51fa <code>int</code> \u578b\u5225\u7684\u53d6\u503c\u7bc4\u570d\u3002\u70ba\u4e86\u907f\u514d\u5927\u6578\u8d8a\u754c\uff0c\u6211\u5011\u901a\u5e38\u63a1\u7528\u516c\u5f0f \\(m = \\lfloor {i + (j - i) / 2} \\rfloor\\) \u4f86\u8a08\u7b97\u4e2d\u9ede\u3002</p> <p>\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_search.py<pre><code>def binary_search(nums: list[int], target: int) -&gt; int:\n \"\"\"\u4e8c\u5206\u641c\u5c0b\uff08\u96d9\u9589\u5340\u9593\uff09\"\"\"\n # \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1] \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n i, j = 0, len(nums) - 1\n # \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i &gt; j \u6642\u70ba\u7a7a\uff09\n while i &lt;= j:\n # \u7406\u8ad6\u4e0a Python \u7684\u6578\u5b57\u53ef\u4ee5\u7121\u9650\u5927\uff08\u53d6\u6c7a\u65bc\u8a18\u61b6\u9ad4\u5927\u5c0f\uff09\uff0c\u7121\u9808\u8003\u616e\u5927\u6578\u8d8a\u754c\u554f\u984c\n m = (i + j) // 2 # \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if nums[m] &lt; target:\n i = m + 1 # \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j] \u4e2d\n elif nums[m] &gt; target:\n j = m - 1 # \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m-1] \u4e2d\n else:\n return m # \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return -1 # \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n</code></pre> binary_search.cpp<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u96d9\u9589\u5340\u9593\uff09 */\nint binarySearch(vector&lt;int&gt; &amp;nums, int target) {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1] \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = nums.size() - 1;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i &gt; j \u6642\u70ba\u7a7a\uff09\n while (i &lt;= j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] &gt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1;\n else // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> binary_search.java<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u96d9\u9589\u5340\u9593\uff09 */\nint binarySearch(int[] nums, int target) {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1] \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = nums.length - 1;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i &gt; j \u6642\u70ba\u7a7a\uff09\n while (i &lt;= j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] &gt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1;\n else // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> binary_search.cs<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u96d9\u9589\u5340\u9593\uff09 */\nint BinarySearch(int[] nums, int target) {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1] \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = nums.Length - 1;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i &gt; j \u6642\u70ba\u7a7a\uff09\n while (i &lt;= j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] &gt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1;\n else // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> binary_search.go<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u96d9\u9589\u5340\u9593\uff09 */\nfunc binarySearch(nums []int, target int) int {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1] \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n i, j := 0, len(nums)-1\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i &gt; j \u6642\u70ba\u7a7a\uff09\n for i &lt;= j {\n m := i + (j-i)/2 // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if nums[m] &lt; target { // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j] \u4e2d\n i = m + 1\n } else if nums[m] &gt; target { // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1\n } else { // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1\n}\n</code></pre> binary_search.swift<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u96d9\u9589\u5340\u9593\uff09 */\nfunc binarySearch(nums: [Int], target: Int) -&gt; Int {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1] \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n var i = nums.startIndex\n var j = nums.endIndex - 1\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i &gt; j \u6642\u70ba\u7a7a\uff09\n while i &lt;= j {\n let m = i + (j - i) / 2 // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if nums[m] &lt; target { // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j] \u4e2d\n i = m + 1\n } else if nums[m] &gt; target { // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1\n } else { // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1\n}\n</code></pre> binary_search.js<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u96d9\u9589\u5340\u9593\uff09 */\nfunction binarySearch(nums, target) {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1] \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n let i = 0,\n j = nums.length - 1;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i &gt; j \u6642\u70ba\u7a7a\uff09\n while (i &lt;= j) {\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\n const m = parseInt(i + (j - i) / 2);\n if (nums[m] &lt; target)\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] &gt; target)\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1;\n else return m; // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> binary_search.ts<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u96d9\u9589\u5340\u9593\uff09 */\nfunction binarySearch(nums: number[], target: number): number {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1] \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n let i = 0,\n j = nums.length - 1;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i &gt; j \u6642\u70ba\u7a7a\uff09\n while (i &lt;= j) {\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n const m = Math.floor(i + (j - i) / 2);\n if (nums[m] &lt; target) {\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j] \u4e2d\n i = m + 1;\n } else if (nums[m] &gt; target) {\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1;\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n }\n return -1; // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n</code></pre> binary_search.dart<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u96d9\u9589\u5340\u9593\uff09 */\nint binarySearch(List&lt;int&gt; nums, int target) {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1] \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = nums.length - 1;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i &gt; j \u6642\u70ba\u7a7a\uff09\n while (i &lt;= j) {\n int m = i + (j - i) ~/ 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) {\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j] \u4e2d\n i = m + 1;\n } else if (nums[m] &gt; target) {\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1;\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> binary_search.rs<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u96d9\u9589\u5340\u9593\uff09 */\nfn binary_search(nums: &amp;[i32], target: i32) -&gt; i32 {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1] \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n let mut i = 0;\n let mut j = nums.len() as i32 - 1;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i &gt; j \u6642\u70ba\u7a7a\uff09\n while i &lt;= j {\n let m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if nums[m as usize] &lt; target {\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j] \u4e2d\n i = m + 1;\n } else if nums[m as usize] &gt; target {\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1;\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> binary_search.c<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u96d9\u9589\u5340\u9593\uff09 */\nint binarySearch(int *nums, int len, int target) {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1] \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n int i = 0, j = len - 1;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i &gt; j \u6642\u70ba\u7a7a\uff09\n while (i &lt;= j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j] \u4e2d\n i = m + 1;\n else if (nums[m] &gt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1;\n else // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> binary_search.kt<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u96d9\u9589\u5340\u9593\uff09 */\nfun binarySearch(nums: IntArray, target: Int): Int {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1] \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n var i = 0\n var j = nums.size - 1\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i &gt; j \u6642\u70ba\u7a7a\uff09\n while (i &lt;= j) {\n val m = i + (j - i) / 2 // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j] \u4e2d\n i = m + 1\n else if (nums[m] &gt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1\n else // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1\n}\n</code></pre> binary_search.rb<pre><code>### \u4e8c\u5206\u641c\u5c0b\uff08\u96d9\u9589\u5340\u9593\uff09 ###\ndef binary_search(nums, target)\n # \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1] \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n i, j = 0, nums.length - 1\n\n # \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i &gt; j \u6642\u70ba\u7a7a\uff09\n while i &lt;= j\n # \u7406\u8ad6\u4e0a Ruby \u7684\u6578\u5b57\u53ef\u4ee5\u7121\u9650\u5927\uff08\u53d6\u6c7a\u65bc\u8a18\u61b6\u9ad4\u5927\u5c0f\uff09\uff0c\u7121\u9808\u8003\u616e\u5927\u6578\u8d8a\u754c\u554f\u984c\n m = (i + j) / 2 # \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n\n if nums[m] &lt; target\n i = m + 1 # \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j] \u4e2d\n elsif nums[m] &gt; target\n j = m - 1 # \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m-1] \u4e2d\n else\n return m # \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n end\n end\n\n -1 # \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\nend\n</code></pre> binary_search.zig<pre><code>// \u4e8c\u5206\u641c\u5c0b\uff08\u96d9\u9589\u5340\u9593\uff09\nfn binarySearch(comptime T: type, nums: std.ArrayList(T), target: T) T {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1] \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\n var i: usize = 0;\n var j: usize = nums.items.len - 1;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i &gt; j \u6642\u70ba\u7a7a\uff09\n while (i &lt;= j) {\n var m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums.items[m] &lt; target) { // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j] \u4e2d\n i = m + 1;\n } else if (nums.items[m] &gt; target) { // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1;\n } else { // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return @intCast(m);\n }\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(\\log n)\\) \uff1a\u5728\u4e8c\u5206\u8ff4\u5708\u4e2d\uff0c\u5340\u9593\u6bcf\u8f2a\u7e2e\u5c0f\u4e00\u534a\uff0c\u56e0\u6b64\u8ff4\u5708\u6b21\u6578\u70ba \\(\\log_2 n\\) \u3002</p> <p>\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(1)\\) \uff1a\u6307\u6a19 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6578\u5927\u5c0f\u7a7a\u9593\u3002</p>"},{"location":"chapter_searching/binary_search/#1011","title":"10.1.1 \u00a0 \u5340\u9593\u8868\u793a\u65b9\u6cd5","text":"<p>\u9664\u4e86\u4e0a\u8ff0\u96d9\u9589\u5340\u9593\u5916\uff0c\u5e38\u898b\u7684\u5340\u9593\u8868\u793a\u9084\u6709\u201c\u5de6\u9589\u53f3\u958b\u201d\u5340\u9593\uff0c\u5b9a\u7fa9\u70ba \\([0, n)\\) \uff0c\u5373\u5de6\u908a\u754c\u5305\u542b\u81ea\u8eab\uff0c\u53f3\u908a\u754c\u4e0d\u5305\u542b\u81ea\u8eab\u3002\u5728\u8a72\u8868\u793a\u4e0b\uff0c\u5340\u9593 \\([i, j)\\) \u5728 \\(i = j\\) \u6642\u70ba\u7a7a\u3002</p> <p>\u6211\u5011\u53ef\u4ee5\u57fa\u65bc\u8a72\u8868\u793a\u5be6\u73fe\u5177\u6709\u76f8\u540c\u529f\u80fd\u7684\u4e8c\u5206\u641c\u5c0b\u6f14\u7b97\u6cd5\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_search.py<pre><code>def binary_search_lcro(nums: list[int], target: int) -&gt; int:\n \"\"\"\u4e8c\u5206\u641c\u5c0b\uff08\u5de6\u9589\u53f3\u958b\u5340\u9593\uff09\"\"\"\n # \u521d\u59cb\u5316\u5de6\u9589\u53f3\u958b\u5340\u9593 [0, n) \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n i, j = 0, len(nums)\n # \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i = j \u6642\u70ba\u7a7a\uff09\n while i &lt; j:\n m = (i + j) // 2 # \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if nums[m] &lt; target:\n i = m + 1 # \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j) \u4e2d\n elif nums[m] &gt; target:\n j = m # \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m) \u4e2d\n else:\n return m # \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return -1 # \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n</code></pre> binary_search.cpp<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u5de6\u9589\u53f3\u958b\u5340\u9593\uff09 */\nint binarySearchLCRO(vector&lt;int&gt; &amp;nums, int target) {\n // \u521d\u59cb\u5316\u5de6\u9589\u53f3\u958b\u5340\u9593 [0, n) \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = nums.size();\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i = j \u6642\u70ba\u7a7a\uff09\n while (i &lt; j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] &gt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m) \u4e2d\n j = m;\n else // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> binary_search.java<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u5de6\u9589\u53f3\u958b\u5340\u9593\uff09 */\nint binarySearchLCRO(int[] nums, int target) {\n // \u521d\u59cb\u5316\u5de6\u9589\u53f3\u958b\u5340\u9593 [0, n) \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = nums.length;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i = j \u6642\u70ba\u7a7a\uff09\n while (i &lt; j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] &gt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m) \u4e2d\n j = m;\n else // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> binary_search.cs<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u5de6\u9589\u53f3\u958b\u5340\u9593\uff09 */\nint BinarySearchLCRO(int[] nums, int target) {\n // \u521d\u59cb\u5316\u5de6\u9589\u53f3\u958b\u5340\u9593 [0, n) \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = nums.Length;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i = j \u6642\u70ba\u7a7a\uff09\n while (i &lt; j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] &gt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m) \u4e2d\n j = m;\n else // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> binary_search.go<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u5de6\u9589\u53f3\u958b\u5340\u9593\uff09 */\nfunc binarySearchLCRO(nums []int, target int) int {\n // \u521d\u59cb\u5316\u5de6\u9589\u53f3\u958b\u5340\u9593 [0, n) \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n i, j := 0, len(nums)\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i = j \u6642\u70ba\u7a7a\uff09\n for i &lt; j {\n m := i + (j-i)/2 // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if nums[m] &lt; target { // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j) \u4e2d\n i = m + 1\n } else if nums[m] &gt; target { // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m) \u4e2d\n j = m\n } else { // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1\n}\n</code></pre> binary_search.swift<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u5de6\u9589\u53f3\u958b\u5340\u9593\uff09 */\nfunc binarySearchLCRO(nums: [Int], target: Int) -&gt; Int {\n // \u521d\u59cb\u5316\u5de6\u9589\u53f3\u958b\u5340\u9593 [0, n) \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n var i = nums.startIndex\n var j = nums.endIndex\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i = j \u6642\u70ba\u7a7a\uff09\n while i &lt; j {\n let m = i + (j - i) / 2 // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if nums[m] &lt; target { // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j) \u4e2d\n i = m + 1\n } else if nums[m] &gt; target { // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m) \u4e2d\n j = m\n } else { // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1\n}\n</code></pre> binary_search.js<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u5de6\u9589\u53f3\u958b\u5340\u9593\uff09 */\nfunction binarySearchLCRO(nums, target) {\n // \u521d\u59cb\u5316\u5de6\u9589\u53f3\u958b\u5340\u9593 [0, n) \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n let i = 0,\n j = nums.length;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i = j \u6642\u70ba\u7a7a\uff09\n while (i &lt; j) {\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\n const m = parseInt(i + (j - i) / 2);\n if (nums[m] &lt; target)\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] &gt; target)\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m) \u4e2d\n j = m;\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n else return m;\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> binary_search.ts<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u5de6\u9589\u53f3\u958b\u5340\u9593\uff09 */\nfunction binarySearchLCRO(nums: number[], target: number): number {\n // \u521d\u59cb\u5316\u5de6\u9589\u53f3\u958b\u5340\u9593 [0, n) \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n let i = 0,\n j = nums.length;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i = j \u6642\u70ba\u7a7a\uff09\n while (i &lt; j) {\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n const m = Math.floor(i + (j - i) / 2);\n if (nums[m] &lt; target) {\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j) \u4e2d\n i = m + 1;\n } else if (nums[m] &gt; target) {\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m) \u4e2d\n j = m;\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n }\n return -1; // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n</code></pre> binary_search.dart<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u5de6\u9589\u53f3\u958b\u5340\u9593\uff09 */\nint binarySearchLCRO(List&lt;int&gt; nums, int target) {\n // \u521d\u59cb\u5316\u5de6\u9589\u53f3\u958b\u5340\u9593 [0, n) \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = nums.length;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i = j \u6642\u70ba\u7a7a\uff09\n while (i &lt; j) {\n int m = i + (j - i) ~/ 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) {\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j) \u4e2d\n i = m + 1;\n } else if (nums[m] &gt; target) {\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m) \u4e2d\n j = m;\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> binary_search.rs<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u5de6\u9589\u53f3\u958b\u5340\u9593\uff09 */\nfn binary_search_lcro(nums: &amp;[i32], target: i32) -&gt; i32 {\n // \u521d\u59cb\u5316\u5de6\u9589\u53f3\u958b\u5340\u9593 [0, n) \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n let mut i = 0;\n let mut j = nums.len() as i32;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i = j \u6642\u70ba\u7a7a\uff09\n while i &lt; j {\n let m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if nums[m as usize] &lt; target {\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j) \u4e2d\n i = m + 1;\n } else if nums[m as usize] &gt; target {\n // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m) \u4e2d\n j = m;\n } else {\n // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> binary_search.c<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u5de6\u9589\u53f3\u958b\u5340\u9593\uff09 */\nint binarySearchLCRO(int *nums, int len, int target) {\n // \u521d\u59cb\u5316\u5de6\u9589\u53f3\u958b\u5340\u9593 [0, n) \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n int i = 0, j = len;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i = j \u6642\u70ba\u7a7a\uff09\n while (i &lt; j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j) \u4e2d\n i = m + 1;\n else if (nums[m] &gt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m) \u4e2d\n j = m;\n else // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m;\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> binary_search.kt<pre><code>/* \u4e8c\u5206\u641c\u5c0b\uff08\u5de6\u9589\u53f3\u958b\u5340\u9593\uff09 */\nfun binarySearchLCRO(nums: IntArray, target: Int): Int {\n // \u521d\u59cb\u5316\u5de6\u9589\u53f3\u958b\u5340\u9593 [0, n) \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n var i = 0\n var j = nums.size\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i = j \u6642\u70ba\u7a7a\uff09\n while (i &lt; j) {\n val m = i + (j - i) / 2 // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j) \u4e2d\n i = m + 1\n else if (nums[m] &gt; target) // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m) \u4e2d\n j = m\n else // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return m\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1\n}\n</code></pre> binary_search.rb<pre><code>### \u4e8c\u5206\u641c\u5c0b\uff08\u5de6\u9589\u53f3\u958b\u5340\u9593\uff09 ###\ndef binary_search_lcro(nums, target)\n # \u521d\u59cb\u5316\u5de6\u9589\u53f3\u958b\u5340\u9593 [0, n) \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n i, j = 0, nums.length\n\n # \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i = j \u6642\u70ba\u7a7a\uff09\n while i &lt; j\n # \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n m = (i + j) / 2\n\n if nums[m] &lt; target\n i = m + 1 # \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j) \u4e2d\n elsif nums[m] &gt; target\n j = m - 1 # \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m) \u4e2d\n else\n return m # \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n end\n end\n\n -1 # \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\nend\n</code></pre> binary_search.zig<pre><code>// \u4e8c\u5206\u641c\u5c0b\uff08\u5de6\u9589\u53f3\u958b\u5340\u9593\uff09\nfn binarySearchLCRO(comptime T: type, nums: std.ArrayList(T), target: T) T {\n // \u521d\u59cb\u5316\u5de6\u9589\u53f3\u958b\u5340\u9593 [0, n) \uff0c\u5373 i, j \u5206\u5225\u6307\u5411\u9663\u5217\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\n var i: usize = 0;\n var j: usize = nums.items.len;\n // \u8ff4\u5708\uff0c\u7576\u641c\u5c0b\u5340\u9593\u70ba\u7a7a\u6642\u8df3\u51fa\uff08\u7576 i = j \u6642\u70ba\u7a7a\uff09\n while (i &lt;= j) {\n var m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums.items[m] &lt; target) { // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [m+1, j) \u4e2d\n i = m + 1;\n } else if (nums.items[m] &gt; target) { // \u6b64\u60c5\u6cc1\u8aaa\u660e target \u5728\u5340\u9593 [i, m) \u4e2d\n j = m;\n } else { // \u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n return @intCast(m);\n }\n }\n // \u672a\u627e\u5230\u76ee\u6a19\u5143\u7d20\uff0c\u8fd4\u56de -1\n return -1;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u5982\u5716 10-3 \u6240\u793a\uff0c\u5728\u5169\u7a2e\u5340\u9593\u8868\u793a\u4e0b\uff0c\u4e8c\u5206\u641c\u5c0b\u6f14\u7b97\u6cd5\u7684\u521d\u59cb\u5316\u3001\u8ff4\u5708\u689d\u4ef6\u548c\u7e2e\u5c0f\u5340\u9593\u64cd\u4f5c\u7686\u6709\u6240\u4e0d\u540c\u3002</p> <p>\u7531\u65bc\u201c\u96d9\u9589\u5340\u9593\u201d\u8868\u793a\u4e2d\u7684\u5de6\u53f3\u908a\u754c\u90fd\u88ab\u5b9a\u7fa9\u70ba\u9589\u5340\u9593\uff0c\u56e0\u6b64\u900f\u904e\u6307\u6a19 \\(i\\) \u548c\u6307\u6a19 \\(j\\) \u7e2e\u5c0f\u5340\u9593\u7684\u64cd\u4f5c\u4e5f\u662f\u5c0d\u7a31\u7684\u3002\u9019\u6a23\u66f4\u4e0d\u5bb9\u6613\u51fa\u932f\uff0c\u56e0\u6b64\u4e00\u822c\u5efa\u8b70\u63a1\u7528\u201c\u96d9\u9589\u5340\u9593\u201d\u7684\u5beb\u6cd5\u3002</p> <p></p> <p> \u5716 10-3 \u00a0 \u5169\u7a2e\u5340\u9593\u5b9a\u7fa9 </p>"},{"location":"chapter_searching/binary_search/#1012","title":"10.1.2 \u00a0 \u512a\u9ede\u8207\u4fb7\u9650\u6027","text":"<p>\u4e8c\u5206\u641c\u5c0b\u5728\u6642\u9593\u548c\u7a7a\u9593\u65b9\u9762\u90fd\u6709\u8f03\u597d\u7684\u6548\u80fd\u3002</p> <ul> <li>\u4e8c\u5206\u641c\u5c0b\u7684\u6642\u9593\u6548\u7387\u9ad8\u3002\u5728\u5927\u8cc7\u6599\u91cf\u4e0b\uff0c\u5c0d\u6578\u968e\u7684\u6642\u9593\u8907\u96dc\u5ea6\u5177\u6709\u986f\u8457\u512a\u52e2\u3002\u4f8b\u5982\uff0c\u7576\u8cc7\u6599\u5927\u5c0f \\(n = 2^{20}\\) \u6642\uff0c\u7dda\u6027\u67e5\u8a62\u9700\u8981 \\(2^{20} = 1048576\\) \u8f2a\u8ff4\u5708\uff0c\u800c\u4e8c\u5206\u641c\u5c0b\u50c5\u9700 \\(\\log_2 2^{20} = 20\\) \u8f2a\u8ff4\u5708\u3002</li> <li>\u4e8c\u5206\u641c\u5c0b\u7121\u9808\u984d\u5916\u7a7a\u9593\u3002\u76f8\u8f03\u65bc\u9700\u8981\u85c9\u52a9\u984d\u5916\u7a7a\u9593\u7684\u641c\u5c0b\u6f14\u7b97\u6cd5\uff08\u4f8b\u5982\u96dc\u6e4a\u67e5\u8a62\uff09\uff0c\u4e8c\u5206\u641c\u5c0b\u66f4\u52a0\u7bc0\u7701\u7a7a\u9593\u3002</li> </ul> <p>\u7136\u800c\uff0c\u4e8c\u5206\u641c\u5c0b\u4e26\u975e\u9069\u7528\u65bc\u6240\u6709\u60c5\u6cc1\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002</p> <ul> <li>\u4e8c\u5206\u641c\u5c0b\u50c5\u9069\u7528\u65bc\u6709\u5e8f\u8cc7\u6599\u3002\u82e5\u8f38\u5165\u8cc7\u6599\u7121\u5e8f\uff0c\u70ba\u4e86\u4f7f\u7528\u4e8c\u5206\u641c\u5c0b\u800c\u5c08\u9580\u9032\u884c\u6392\u5e8f\uff0c\u5f97\u4e0d\u511f\u5931\u3002\u56e0\u70ba\u6392\u5e8f\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u901a\u5e38\u70ba \\(O(n \\log n)\\) \uff0c\u6bd4\u7dda\u6027\u67e5\u8a62\u548c\u4e8c\u5206\u641c\u5c0b\u90fd\u66f4\u9ad8\u3002\u5c0d\u65bc\u983b\u7e41\u63d2\u5165\u5143\u7d20\u7684\u5834\u666f\uff0c\u70ba\u4fdd\u6301\u9663\u5217\u6709\u5e8f\u6027\uff0c\u9700\u8981\u5c07\u5143\u7d20\u63d2\u5165\u5230\u7279\u5b9a\u4f4d\u7f6e\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \uff0c\u4e5f\u662f\u975e\u5e38\u6602\u8cb4\u7684\u3002</li> <li>\u4e8c\u5206\u641c\u5c0b\u50c5\u9069\u7528\u65bc\u9663\u5217\u3002\u4e8c\u5206\u641c\u5c0b\u9700\u8981\u8df3\u8e8d\u5f0f\uff08\u975e\u9023\u7e8c\u5730\uff09\u8a2a\u554f\u5143\u7d20\uff0c\u800c\u5728\u93c8\u7d50\u4e32\u5217\u4e2d\u57f7\u884c\u8df3\u8e8d\u5f0f\u8a2a\u554f\u7684\u6548\u7387\u8f03\u4f4e\uff0c\u56e0\u6b64\u4e0d\u9069\u5408\u61c9\u7528\u5728\u93c8\u7d50\u4e32\u5217\u6216\u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u8cc7\u6599\u7d50\u69cb\u3002</li> <li>\u5c0f\u8cc7\u6599\u91cf\u4e0b\uff0c\u7dda\u6027\u67e5\u8a62\u6548\u80fd\u66f4\u4f73\u3002\u7dda\u4e0a\u6027\u67e5\u8a62\u4e2d\uff0c\u6bcf\u8f2a\u53ea\u9700 1 \u6b21\u5224\u65b7\u64cd\u4f5c\uff1b\u800c\u5728\u4e8c\u5206\u641c\u5c0b\u4e2d\uff0c\u9700\u8981 1 \u6b21\u52a0\u6cd5\u30011 \u6b21\u9664\u6cd5\u30011 ~ 3 \u6b21\u5224\u65b7\u64cd\u4f5c\u30011 \u6b21\u52a0\u6cd5\uff08\u6e1b\u6cd5\uff09\uff0c\u5171 4 ~ 6 \u500b\u55ae\u5143\u64cd\u4f5c\uff1b\u56e0\u6b64\uff0c\u7576\u8cc7\u6599\u91cf \\(n\\) \u8f03\u5c0f\u6642\uff0c\u7dda\u6027\u67e5\u8a62\u53cd\u800c\u6bd4\u4e8c\u5206\u641c\u5c0b\u66f4\u5feb\u3002</li> </ul>"},{"location":"chapter_searching/binary_search_edge/","title":"10.3 \u00a0 \u4e8c\u5206\u641c\u5c0b\u908a\u754c","text":""},{"location":"chapter_searching/binary_search_edge/#1031","title":"10.3.1 \u00a0 \u67e5\u8a62\u5de6\u908a\u754c","text":"<p>Question</p> <p>\u7d66\u5b9a\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u6709\u5e8f\u9663\u5217 <code>nums</code> \uff0c\u5176\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u8907\u5143\u7d20\u3002\u8acb\u8fd4\u56de\u9663\u5217\u4e2d\u6700\u5de6\u4e00\u500b\u5143\u7d20 <code>target</code> \u7684\u7d22\u5f15\u3002\u82e5\u9663\u5217\u4e2d\u4e0d\u5305\u542b\u8a72\u5143\u7d20\uff0c\u5247\u8fd4\u56de \\(-1\\) \u3002</p> <p>\u56de\u61b6\u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\u7684\u65b9\u6cd5\uff0c\u641c\u5c0b\u5b8c\u6210\u5f8c \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u500b <code>target</code> \uff0c\u56e0\u6b64\u67e5\u8a62\u63d2\u5165\u9ede\u672c\u8cea\u4e0a\u662f\u5728\u67e5\u8a62\u6700\u5de6\u4e00\u500b <code>target</code> \u7684\u7d22\u5f15\u3002</p> <p>\u8003\u616e\u900f\u904e\u67e5\u8a62\u63d2\u5165\u9ede\u7684\u51fd\u5f0f\u5be6\u73fe\u67e5\u8a62\u5de6\u908a\u754c\u3002\u8acb\u6ce8\u610f\uff0c\u9663\u5217\u4e2d\u53ef\u80fd\u4e0d\u5305\u542b <code>target</code> \uff0c\u9019\u7a2e\u60c5\u6cc1\u53ef\u80fd\u5c0e\u81f4\u4ee5\u4e0b\u5169\u7a2e\u7d50\u679c\u3002</p> <ul> <li>\u63d2\u5165\u9ede\u7684\u7d22\u5f15 \\(i\\) \u8d8a\u754c\u3002</li> <li>\u5143\u7d20 <code>nums[i]</code> \u8207 <code>target</code> \u4e0d\u76f8\u7b49\u3002</li> </ul> <p>\u7576\u9047\u5230\u4ee5\u4e0a\u5169\u7a2e\u60c5\u6cc1\u6642\uff0c\u76f4\u63a5\u8fd4\u56de \\(-1\\) \u5373\u53ef\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_search_edge.py<pre><code>def binary_search_left_edge(nums: list[int], target: int) -&gt; int:\n \"\"\"\u4e8c\u5206\u641c\u5c0b\u6700\u5de6\u4e00\u500b target\"\"\"\n # \u7b49\u50f9\u65bc\u67e5\u8a62 target \u7684\u63d2\u5165\u9ede\n i = binary_search_insertion(nums, target)\n # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if i == len(nums) or nums[i] != target:\n return -1\n # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i\n</code></pre> binary_search_edge.cpp<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u5de6\u4e00\u500b target */\nint binarySearchLeftEdge(vector&lt;int&gt; &amp;nums, int target) {\n // \u7b49\u50f9\u65bc\u67e5\u8a62 target \u7684\u63d2\u5165\u9ede\n int i = binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == nums.size() || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n</code></pre> binary_search_edge.java<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u5de6\u4e00\u500b target */\nint binarySearchLeftEdge(int[] nums, int target) {\n // \u7b49\u50f9\u65bc\u67e5\u8a62 target \u7684\u63d2\u5165\u9ede\n int i = binary_search_insertion.binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == nums.length || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n</code></pre> binary_search_edge.cs<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u5de6\u4e00\u500b target */\nint BinarySearchLeftEdge(int[] nums, int target) {\n // \u7b49\u50f9\u65bc\u67e5\u8a62 target \u7684\u63d2\u5165\u9ede\n int i = binary_search_insertion.BinarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == nums.Length || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n</code></pre> binary_search_edge.go<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u5de6\u4e00\u500b target */\nfunc binarySearchLeftEdge(nums []int, target int) int {\n // \u7b49\u50f9\u65bc\u67e5\u8a62 target \u7684\u63d2\u5165\u9ede\n i := binarySearchInsertion(nums, target)\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if i == len(nums) || nums[i] != target {\n return -1\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i\n}\n</code></pre> binary_search_edge.swift<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u5de6\u4e00\u500b target */\nfunc binarySearchLeftEdge(nums: [Int], target: Int) -&gt; Int {\n // \u7b49\u50f9\u65bc\u67e5\u8a62 target \u7684\u63d2\u5165\u9ede\n let i = binarySearchInsertion(nums: nums, target: target)\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if i == nums.endIndex || nums[i] != target {\n return -1\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i\n}\n</code></pre> binary_search_edge.js<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u5de6\u4e00\u500b target */\nfunction binarySearchLeftEdge(nums, target) {\n // \u7b49\u50f9\u65bc\u67e5\u8a62 target \u7684\u63d2\u5165\u9ede\n const i = binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i === nums.length || nums[i] !== target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n</code></pre> binary_search_edge.ts<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u5de6\u4e00\u500b target */\nfunction binarySearchLeftEdge(nums: Array&lt;number&gt;, target: number): number {\n // \u7b49\u50f9\u65bc\u67e5\u8a62 target \u7684\u63d2\u5165\u9ede\n const i = binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i === nums.length || nums[i] !== target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n</code></pre> binary_search_edge.dart<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u5de6\u4e00\u500b target */\nint binarySearchLeftEdge(List&lt;int&gt; nums, int target) {\n // \u7b49\u50f9\u65bc\u67e5\u8a62 target \u7684\u63d2\u5165\u9ede\n int i = binarySearchInsertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == nums.length || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n</code></pre> binary_search_edge.rs<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u5de6\u4e00\u500b target */\nfn binary_search_left_edge(nums: &amp;[i32], target: i32) -&gt; i32 {\n // \u7b49\u50f9\u65bc\u67e5\u8a62 target \u7684\u63d2\u5165\u9ede\n let i = binary_search_insertion(nums, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if i == nums.len() as i32 || nums[i as usize] != target {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n i\n}\n</code></pre> binary_search_edge.c<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u5de6\u4e00\u500b target */\nint binarySearchLeftEdge(int *nums, int numSize, int target) {\n // \u7b49\u50f9\u65bc\u67e5\u8a62 target \u7684\u63d2\u5165\u9ede\n int i = binarySearchInsertion(nums, numSize, target);\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == numSize || nums[i] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i;\n}\n</code></pre> binary_search_edge.kt<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u5de6\u4e00\u500b target */\nfun binarySearchLeftEdge(nums: IntArray, target: Int): Int {\n // \u7b49\u50f9\u65bc\u67e5\u8a62 target \u7684\u63d2\u5165\u9ede\n val i = binarySearchInsertion(nums, target)\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (i == nums.size || nums[i] != target) {\n return -1\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\n return i\n}\n</code></pre> binary_search_edge.rb<pre><code>### \u4e8c\u5206\u641c\u5c0b\u6700\u5de6\u4e00\u500b target ###\ndef binary_search_left_edge(nums, target)\n # \u7b49\u50f9\u65bc\u67e5\u8a62 target \u7684\u63d2\u5165\u9ede\n i = binary_search_insertion(nums, target)\n\n # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n return -1 if i == nums.length || nums[i] != target\n\n i # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nend\n</code></pre> binary_search_edge.zig<pre><code>[class]{}-[func]{binarySearchLeftEdge}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_searching/binary_search_edge/#1032","title":"10.3.2 \u00a0 \u67e5\u8a62\u53f3\u908a\u754c","text":"<p>\u90a3\u9ebc\u5982\u4f55\u67e5\u8a62\u6700\u53f3\u4e00\u500b <code>target</code> \u5462\uff1f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u4fee\u6539\u7a0b\u5f0f\u78bc\uff0c\u66ff\u63db\u5728 <code>nums[m] == target</code> \u60c5\u6cc1\u4e0b\u7684\u6307\u6a19\u6536\u7e2e\u64cd\u4f5c\u3002\u7a0b\u5f0f\u78bc\u5728\u6b64\u7701\u7565\uff0c\u6709\u8208\u8da3\u7684\u8b80\u8005\u53ef\u4ee5\u81ea\u884c\u5be6\u73fe\u3002</p> <p>\u4e0b\u9762\u6211\u5011\u4ecb\u7d39\u5169\u7a2e\u66f4\u52a0\u53d6\u5de7\u7684\u65b9\u6cd5\u3002</p>"},{"location":"chapter_searching/binary_search_edge/#1","title":"1. \u00a0 \u8907\u7528\u67e5\u8a62\u5de6\u908a\u754c","text":"<p>\u5be6\u969b\u4e0a\uff0c\u6211\u5011\u53ef\u4ee5\u5229\u7528\u67e5\u8a62\u6700\u5de6\u5143\u7d20\u7684\u51fd\u5f0f\u4f86\u67e5\u8a62\u6700\u53f3\u5143\u7d20\uff0c\u5177\u9ad4\u65b9\u6cd5\u70ba\uff1a\u5c07\u67e5\u8a62\u6700\u53f3\u4e00\u500b <code>target</code> \u8f49\u5316\u70ba\u67e5\u8a62\u6700\u5de6\u4e00\u500b <code>target + 1</code>\u3002</p> <p>\u5982\u5716 10-7 \u6240\u793a\uff0c\u67e5\u8a62\u5b8c\u6210\u5f8c\uff0c\u6307\u6a19 \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u500b <code>target + 1</code>\uff08\u5982\u679c\u5b58\u5728\uff09\uff0c\u800c \\(j\\) \u6307\u5411\u6700\u53f3\u4e00\u500b <code>target</code> \uff0c\u56e0\u6b64\u8fd4\u56de \\(j\\) \u5373\u53ef\u3002</p> <p></p> <p> \u5716 10-7 \u00a0 \u5c07\u67e5\u8a62\u53f3\u908a\u754c\u8f49\u5316\u70ba\u67e5\u8a62\u5de6\u908a\u754c </p> <p>\u8acb\u6ce8\u610f\uff0c\u8fd4\u56de\u7684\u63d2\u5165\u9ede\u662f \\(i\\) \uff0c\u56e0\u6b64\u9700\u8981\u5c07\u5176\u6e1b \\(1\\) \uff0c\u5f9e\u800c\u7372\u5f97 \\(j\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_search_edge.py<pre><code>def binary_search_right_edge(nums: list[int], target: int) -&gt; int:\n \"\"\"\u4e8c\u5206\u641c\u5c0b\u6700\u53f3\u4e00\u500b target\"\"\"\n # \u8f49\u5316\u70ba\u67e5\u8a62\u6700\u5de6\u4e00\u500b target + 1\n i = binary_search_insertion(nums, target + 1)\n # j \u6307\u5411\u6700\u53f3\u4e00\u500b target \uff0ci \u6307\u5411\u9996\u500b\u5927\u65bc target \u7684\u5143\u7d20\n j = i - 1\n # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if j == -1 or nums[j] != target:\n return -1\n # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j\n</code></pre> binary_search_edge.cpp<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u53f3\u4e00\u500b target */\nint binarySearchRightEdge(vector&lt;int&gt; &amp;nums, int target) {\n // \u8f49\u5316\u70ba\u67e5\u8a62\u6700\u5de6\u4e00\u500b target + 1\n int i = binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u500b target \uff0ci \u6307\u5411\u9996\u500b\u5927\u65bc target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n</code></pre> binary_search_edge.java<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u53f3\u4e00\u500b target */\nint binarySearchRightEdge(int[] nums, int target) {\n // \u8f49\u5316\u70ba\u67e5\u8a62\u6700\u5de6\u4e00\u500b target + 1\n int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u500b target \uff0ci \u6307\u5411\u9996\u500b\u5927\u65bc target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n</code></pre> binary_search_edge.cs<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u53f3\u4e00\u500b target */\nint BinarySearchRightEdge(int[] nums, int target) {\n // \u8f49\u5316\u70ba\u67e5\u8a62\u6700\u5de6\u4e00\u500b target + 1\n int i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u500b target \uff0ci \u6307\u5411\u9996\u500b\u5927\u65bc target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n</code></pre> binary_search_edge.go<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u53f3\u4e00\u500b target */\nfunc binarySearchRightEdge(nums []int, target int) int {\n // \u8f49\u5316\u70ba\u67e5\u8a62\u6700\u5de6\u4e00\u500b target + 1\n i := binarySearchInsertion(nums, target+1)\n // j \u6307\u5411\u6700\u53f3\u4e00\u500b target \uff0ci \u6307\u5411\u9996\u500b\u5927\u65bc target \u7684\u5143\u7d20\n j := i - 1\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if j == -1 || nums[j] != target {\n return -1\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j\n}\n</code></pre> binary_search_edge.swift<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u53f3\u4e00\u500b target */\nfunc binarySearchRightEdge(nums: [Int], target: Int) -&gt; Int {\n // \u8f49\u5316\u70ba\u67e5\u8a62\u6700\u5de6\u4e00\u500b target + 1\n let i = binarySearchInsertion(nums: nums, target: target + 1)\n // j \u6307\u5411\u6700\u53f3\u4e00\u500b target \uff0ci \u6307\u5411\u9996\u500b\u5927\u65bc target \u7684\u5143\u7d20\n let j = i - 1\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if j == -1 || nums[j] != target {\n return -1\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j\n}\n</code></pre> binary_search_edge.js<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u53f3\u4e00\u500b target */\nfunction binarySearchRightEdge(nums, target) {\n // \u8f49\u5316\u70ba\u67e5\u8a62\u6700\u5de6\u4e00\u500b target + 1\n const i = binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u500b target \uff0ci \u6307\u5411\u9996\u500b\u5927\u65bc target \u7684\u5143\u7d20\n const j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j === -1 || nums[j] !== target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n</code></pre> binary_search_edge.ts<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u53f3\u4e00\u500b target */\nfunction binarySearchRightEdge(nums: Array&lt;number&gt;, target: number): number {\n // \u8f49\u5316\u70ba\u67e5\u8a62\u6700\u5de6\u4e00\u500b target + 1\n const i = binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u500b target \uff0ci \u6307\u5411\u9996\u500b\u5927\u65bc target \u7684\u5143\u7d20\n const j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j === -1 || nums[j] !== target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n</code></pre> binary_search_edge.dart<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u53f3\u4e00\u500b target */\nint binarySearchRightEdge(List&lt;int&gt; nums, int target) {\n // \u8f49\u5316\u70ba\u67e5\u8a62\u6700\u5de6\u4e00\u500b target + 1\n int i = binarySearchInsertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u500b target \uff0ci \u6307\u5411\u9996\u500b\u5927\u65bc target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n</code></pre> binary_search_edge.rs<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u53f3\u4e00\u500b target */\nfn binary_search_right_edge(nums: &amp;[i32], target: i32) -&gt; i32 {\n // \u8f49\u5316\u70ba\u67e5\u8a62\u6700\u5de6\u4e00\u500b target + 1\n let i = binary_search_insertion(nums, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u500b target \uff0ci \u6307\u5411\u9996\u500b\u5927\u65bc target \u7684\u5143\u7d20\n let j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if j == -1 || nums[j as usize] != target {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n j\n}\n</code></pre> binary_search_edge.c<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u53f3\u4e00\u500b target */\nint binarySearchRightEdge(int *nums, int numSize, int target) {\n // \u8f49\u5316\u70ba\u67e5\u8a62\u6700\u5de6\u4e00\u500b target + 1\n int i = binarySearchInsertion(nums, numSize, target + 1);\n // j \u6307\u5411\u6700\u53f3\u4e00\u500b target \uff0ci \u6307\u5411\u9996\u500b\u5927\u65bc target \u7684\u5143\u7d20\n int j = i - 1;\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1;\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j;\n}\n</code></pre> binary_search_edge.kt<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u6700\u53f3\u4e00\u500b target */\nfun binarySearchRightEdge(nums: IntArray, target: Int): Int {\n // \u8f49\u5316\u70ba\u67e5\u8a62\u6700\u5de6\u4e00\u500b target + 1\n val i = binarySearchInsertion(nums, target + 1)\n // j \u6307\u5411\u6700\u53f3\u4e00\u500b target \uff0ci \u6307\u5411\u9996\u500b\u5927\u65bc target \u7684\u5143\u7d20\n val j = i - 1\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n if (j == -1 || nums[j] != target) {\n return -1\n }\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\n return j\n}\n</code></pre> binary_search_edge.rb<pre><code>### \u4e8c\u5206\u641c\u5c0b\u6700\u53f3\u4e00\u500b target ###\ndef binary_search_right_edge(nums, target)\n # \u8f49\u5316\u70ba\u67e5\u8a62\u6700\u5de6\u4e00\u500b target + 1\n i = binary_search_insertion(nums, target + 1)\n\n # j \u6307\u5411\u6700\u53f3\u4e00\u500b target \uff0ci \u6307\u5411\u9996\u500b\u5927\u65bc target \u7684\u5143\u7d20\n j = i - 1\n\n # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\n return -1 if j == -1 || nums[j] != target\n\n j # \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nend\n</code></pre> binary_search_edge.zig<pre><code>[class]{}-[func]{binarySearchRightEdge}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_searching/binary_search_edge/#2","title":"2. \u00a0 \u8f49\u5316\u70ba\u67e5\u8a62\u5143\u7d20","text":"<p>\u6211\u5011\u77e5\u9053\uff0c\u7576\u9663\u5217\u4e0d\u5305\u542b <code>target</code> \u6642\uff0c\u6700\u7d42 \\(i\\) \u548c \\(j\\) \u6703\u5206\u5225\u6307\u5411\u9996\u500b\u5927\u65bc\u3001\u5c0f\u65bc <code>target</code> \u7684\u5143\u7d20\u3002</p> <p>\u56e0\u6b64\uff0c\u5982\u5716 10-8 \u6240\u793a\uff0c\u6211\u5011\u53ef\u4ee5\u69cb\u9020\u4e00\u500b\u9663\u5217\u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\uff0c\u7528\u65bc\u67e5\u8a62\u5de6\u53f3\u908a\u754c\u3002</p> <ul> <li>\u67e5\u8a62\u6700\u5de6\u4e00\u500b <code>target</code> \uff1a\u53ef\u4ee5\u8f49\u5316\u70ba\u67e5\u8a62 <code>target - 0.5</code> \uff0c\u4e26\u8fd4\u56de\u6307\u6a19 \\(i\\) \u3002</li> <li>\u67e5\u8a62\u6700\u53f3\u4e00\u500b <code>target</code> \uff1a\u53ef\u4ee5\u8f49\u5316\u70ba\u67e5\u8a62 <code>target + 0.5</code> \uff0c\u4e26\u8fd4\u56de\u6307\u6a19 \\(j\\) \u3002</li> </ul> <p></p> <p> \u5716 10-8 \u00a0 \u5c07\u67e5\u8a62\u908a\u754c\u8f49\u5316\u70ba\u67e5\u8a62\u5143\u7d20 </p> <p>\u7a0b\u5f0f\u78bc\u5728\u6b64\u7701\u7565\uff0c\u4ee5\u4e0b\u5169\u9ede\u503c\u5f97\u6ce8\u610f\u3002</p> <ul> <li>\u7d66\u5b9a\u9663\u5217\u4e0d\u5305\u542b\u5c0f\u6578\uff0c\u9019\u610f\u5473\u8457\u6211\u5011\u7121\u9808\u95dc\u5fc3\u5982\u4f55\u8655\u7406\u76f8\u7b49\u7684\u60c5\u6cc1\u3002</li> <li>\u56e0\u70ba\u8a72\u65b9\u6cd5\u5f15\u5165\u4e86\u5c0f\u6578\uff0c\u6240\u4ee5\u9700\u8981\u5c07\u51fd\u5f0f\u4e2d\u7684\u8b8a\u6578 <code>target</code> \u6539\u70ba\u6d6e\u9ede\u6578\u578b\u5225\uff08Python \u7121\u9808\u6539\u52d5\uff09\u3002</li> </ul>"},{"location":"chapter_searching/binary_search_insertion/","title":"10.2 \u00a0 \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede","text":"<p>\u4e8c\u5206\u641c\u5c0b\u4e0d\u50c5\u53ef\u7528\u65bc\u641c\u5c0b\u76ee\u6a19\u5143\u7d20\uff0c\u9084\u53ef\u7528\u65bc\u89e3\u6c7a\u8a31\u591a\u8b8a\u7a2e\u554f\u984c\uff0c\u6bd4\u5982\u641c\u5c0b\u76ee\u6a19\u5143\u7d20\u7684\u63d2\u5165\u4f4d\u7f6e\u3002</p>"},{"location":"chapter_searching/binary_search_insertion/#1021","title":"10.2.1 \u00a0 \u7121\u91cd\u8907\u5143\u7d20\u7684\u60c5\u6cc1","text":"<p>Question</p> <p>\u7d66\u5b9a\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u6709\u5e8f\u9663\u5217 <code>nums</code> \u548c\u4e00\u500b\u5143\u7d20 <code>target</code> \uff0c\u9663\u5217\u4e0d\u5b58\u5728\u91cd\u8907\u5143\u7d20\u3002\u73fe\u5c07 <code>target</code> \u63d2\u5165\u9663\u5217 <code>nums</code> \u4e2d\uff0c\u4e26\u4fdd\u6301\u5176\u6709\u5e8f\u6027\u3002\u82e5\u9663\u5217\u4e2d\u5df2\u5b58\u5728\u5143\u7d20 <code>target</code> \uff0c\u5247\u63d2\u5165\u5230\u5176\u5de6\u65b9\u3002\u8acb\u8fd4\u56de\u63d2\u5165\u5f8c <code>target</code> \u5728\u9663\u5217\u4e2d\u7684\u7d22\u5f15\u3002\u793a\u4f8b\u5982\u5716 10-4 \u6240\u793a\u3002</p> <p></p> <p> \u5716 10-4 \u00a0 \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\u793a\u4f8b\u8cc7\u6599 </p> <p>\u5982\u679c\u60f3\u8907\u7528\u4e0a\u4e00\u7bc0\u7684\u4e8c\u5206\u641c\u5c0b\u7a0b\u5f0f\u78bc\uff0c\u5247\u9700\u8981\u56de\u7b54\u4ee5\u4e0b\u5169\u500b\u554f\u984c\u3002</p> <p>\u554f\u984c\u4e00\uff1a\u7576\u9663\u5217\u4e2d\u5305\u542b <code>target</code> \u6642\uff0c\u63d2\u5165\u9ede\u7684\u7d22\u5f15\u662f\u5426\u662f\u8a72\u5143\u7d20\u7684\u7d22\u5f15\uff1f</p> <p>\u984c\u76ee\u8981\u6c42\u5c07 <code>target</code> \u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u5de6\u908a\uff0c\u9019\u610f\u5473\u8457\u65b0\u63d2\u5165\u7684 <code>target</code> \u66ff\u63db\u4e86\u539f\u4f86 <code>target</code> \u7684\u4f4d\u7f6e\u3002\u4e5f\u5c31\u662f\u8aaa\uff0c\u7576\u9663\u5217\u5305\u542b <code>target</code> \u6642\uff0c\u63d2\u5165\u9ede\u7684\u7d22\u5f15\u5c31\u662f\u8a72 <code>target</code> \u7684\u7d22\u5f15\u3002</p> <p>\u554f\u984c\u4e8c\uff1a\u7576\u9663\u5217\u4e2d\u4e0d\u5b58\u5728 <code>target</code> \u6642\uff0c\u63d2\u5165\u9ede\u662f\u54ea\u500b\u5143\u7d20\u7684\u7d22\u5f15\uff1f</p> <p>\u9032\u4e00\u6b65\u601d\u8003\u4e8c\u5206\u641c\u5c0b\u904e\u7a0b\uff1a\u7576 <code>nums[m] &lt; target</code> \u6642 \\(i\\) \u79fb\u52d5\uff0c\u9019\u610f\u5473\u8457\u6307\u6a19 \\(i\\) \u5728\u5411\u5927\u65bc\u7b49\u65bc <code>target</code> \u7684\u5143\u7d20\u9760\u8fd1\u3002\u540c\u7406\uff0c\u6307\u6a19 \\(j\\) \u59cb\u7d42\u5728\u5411\u5c0f\u65bc\u7b49\u65bc <code>target</code> \u7684\u5143\u7d20\u9760\u8fd1\u3002</p> <p>\u56e0\u6b64\u4e8c\u5206\u7d50\u675f\u6642\u4e00\u5b9a\u6709\uff1a\\(i\\) \u6307\u5411\u9996\u500b\u5927\u65bc <code>target</code> \u7684\u5143\u7d20\uff0c\\(j\\) \u6307\u5411\u9996\u500b\u5c0f\u65bc <code>target</code> \u7684\u5143\u7d20\u3002\u6613\u5f97\u7576\u9663\u5217\u4e0d\u5305\u542b <code>target</code> \u6642\uff0c\u63d2\u5165\u7d22\u5f15\u70ba \\(i\\) \u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_search_insertion.py<pre><code>def binary_search_insertion_simple(nums: list[int], target: int) -&gt; int:\n \"\"\"\u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u7121\u91cd\u8907\u5143\u7d20\uff09\"\"\"\n i, j = 0, len(nums) - 1 # \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while i &lt;= j:\n m = (i + j) // 2 # \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if nums[m] &lt; target:\n i = m + 1 # target \u5728\u5340\u9593 [m+1, j] \u4e2d\n elif nums[m] &gt; target:\n j = m - 1 # target \u5728\u5340\u9593 [i, m-1] \u4e2d\n else:\n return m # \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede m\n # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede i\n return i\n</code></pre> binary_search_insertion.cpp<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u7121\u91cd\u8907\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(vector&lt;int&gt; &amp;nums, int target) {\n int i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede i\n return i;\n}\n</code></pre> binary_search_insertion.java<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u7121\u91cd\u8907\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int[] nums, int target) {\n int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede i\n return i;\n}\n</code></pre> binary_search_insertion.cs<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u7121\u91cd\u8907\u5143\u7d20\uff09 */\nint BinarySearchInsertionSimple(int[] nums, int target) {\n int i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede i\n return i;\n}\n</code></pre> binary_search_insertion.go<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u7121\u91cd\u8907\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums []int, target int) int {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n i, j := 0, len(nums)-1\n for i &lt;= j {\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n m := i + (j-i)/2\n if nums[m] &lt; target {\n // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n i = m + 1\n } else if nums[m] &gt; target {\n // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1\n } else {\n // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede m\n return m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede i\n return i\n}\n</code></pre> binary_search_insertion.swift<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u7121\u91cd\u8907\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums: [Int], target: Int) -&gt; Int {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n var i = nums.startIndex\n var j = nums.endIndex - 1\n while i &lt;= j {\n let m = i + (j - i) / 2 // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if nums[m] &lt; target {\n i = m + 1 // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if nums[m] &gt; target {\n j = m - 1 // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n return m // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede i\n return i\n}\n</code></pre> binary_search_insertion.js<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u7121\u91cd\u8907\u5143\u7d20\uff09 */\nfunction binarySearchInsertionSimple(nums, target) {\n let i = 0,\n j = nums.length - 1; // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n const m = Math.floor(i + (j - i) / 2); // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n if (nums[m] &lt; target) {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede i\n return i;\n}\n</code></pre> binary_search_insertion.ts<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u7121\u91cd\u8907\u5143\u7d20\uff09 */\nfunction binarySearchInsertionSimple(\n nums: Array&lt;number&gt;,\n target: number\n): number {\n let i = 0,\n j = nums.length - 1; // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n const m = Math.floor(i + (j - i) / 2); // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n if (nums[m] &lt; target) {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede i\n return i;\n}\n</code></pre> binary_search_insertion.dart<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u7121\u91cd\u8907\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(List&lt;int&gt; nums, int target) {\n int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n int m = i + (j - i) ~/ 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede i\n return i;\n}\n</code></pre> binary_search_insertion.rs<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u7121\u91cd\u8907\u5143\u7d20\uff09 */\nfn binary_search_insertion_simple(nums: &amp;[i32], target: i32) -&gt; i32 {\n let (mut i, mut j) = (0, nums.len() as i32 - 1); // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while i &lt;= j {\n let m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if nums[m as usize] &lt; target {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if nums[m as usize] &gt; target {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n return m;\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede i\n i\n}\n</code></pre> binary_search_insertion.c<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u7121\u91cd\u8907\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int *nums, int numSize, int target) {\n int i = 0, j = numSize - 1; // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n return m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede i\n return i;\n}\n</code></pre> binary_search_insertion.kt<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u7121\u91cd\u8907\u5143\u7d20\uff09 */\nfun binarySearchInsertionSimple(nums: IntArray, target: Int): Int {\n var i = 0\n var j = nums.size - 1 // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n val m = i + (j - i) / 2 // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) {\n i = m + 1 // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1 // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n return m // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede m\n }\n }\n // \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede i\n return i\n}\n</code></pre> binary_search_insertion.rb<pre><code>### \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u7121\u91cd\u8907\u5143\u7d20\uff09 ###\ndef binary_search_insertion_simple(nums, target)\n # \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n i, j = 0, nums.length - 1\n\n while i &lt;= j\n # \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n m = (i + j) / 2\n\n if nums[m] &lt; target\n i = m + 1 # target \u5728\u5340\u9593 [m+1, j] \u4e2d\n elsif nums[m] &gt; target\n j = m - 1 # target \u5728\u5340\u9593 [i, m-1] \u4e2d\n else\n return m # \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede m\n end\n end\n\n i # \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u9ede i\nend\n</code></pre> binary_search_insertion.zig<pre><code>[class]{}-[func]{binarySearchInsertionSimple}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_searching/binary_search_insertion/#1022","title":"10.2.2 \u00a0 \u5b58\u5728\u91cd\u8907\u5143\u7d20\u7684\u60c5\u6cc1","text":"<p>Question</p> <p>\u5728\u4e0a\u4e00\u984c\u7684\u57fa\u790e\u4e0a\uff0c\u898f\u5b9a\u9663\u5217\u53ef\u80fd\u5305\u542b\u91cd\u8907\u5143\u7d20\uff0c\u5176\u9918\u4e0d\u8b8a\u3002</p> <p>\u5047\u8a2d\u9663\u5217\u4e2d\u5b58\u5728\u591a\u500b <code>target</code> \uff0c\u5247\u666e\u901a\u4e8c\u5206\u641c\u5c0b\u53ea\u80fd\u8fd4\u56de\u5176\u4e2d\u4e00\u500b <code>target</code> \u7684\u7d22\u5f15\uff0c\u800c\u7121\u6cd5\u78ba\u5b9a\u8a72\u5143\u7d20\u7684\u5de6\u908a\u548c\u53f3\u908a\u9084\u6709\u591a\u5c11 <code>target</code>\u3002</p> <p>\u984c\u76ee\u8981\u6c42\u5c07\u76ee\u6a19\u5143\u7d20\u63d2\u5165\u5230\u6700\u5de6\u908a\uff0c\u6240\u4ee5\u6211\u5011\u9700\u8981\u67e5\u8a62\u9663\u5217\u4e2d\u6700\u5de6\u4e00\u500b <code>target</code> \u7684\u7d22\u5f15\u3002\u521d\u6b65\u8003\u616e\u900f\u904e\u5716 10-5 \u6240\u793a\u7684\u6b65\u9a5f\u5be6\u73fe\u3002</p> <ol> <li>\u57f7\u884c\u4e8c\u5206\u641c\u5c0b\uff0c\u5f97\u5230\u4efb\u610f\u4e00\u500b <code>target</code> \u7684\u7d22\u5f15\uff0c\u8a18\u70ba \\(k\\) \u3002</li> <li>\u5f9e\u7d22\u5f15 \\(k\\) \u958b\u59cb\uff0c\u5411\u5de6\u9032\u884c\u7dda\u6027\u8d70\u8a2a\uff0c\u7576\u627e\u5230\u6700\u5de6\u908a\u7684 <code>target</code> \u6642\u8fd4\u56de\u3002</li> </ol> <p></p> <p> \u5716 10-5 \u00a0 \u7dda\u6027\u67e5\u8a62\u91cd\u8907\u5143\u7d20\u7684\u63d2\u5165\u9ede </p> <p>\u6b64\u65b9\u6cd5\u96d6\u7136\u53ef\u7528\uff0c\u4f46\u5176\u5305\u542b\u7dda\u6027\u67e5\u8a62\uff0c\u56e0\u6b64\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \u3002\u7576\u9663\u5217\u4e2d\u5b58\u5728\u5f88\u591a\u91cd\u8907\u7684 <code>target</code> \u6642\uff0c\u8a72\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\u3002</p> <p>\u73fe\u8003\u616e\u62d3\u5c55\u4e8c\u5206\u641c\u5c0b\u7a0b\u5f0f\u78bc\u3002\u5982\u5716 10-6 \u6240\u793a\uff0c\u6574\u9ad4\u6d41\u7a0b\u4fdd\u6301\u4e0d\u8b8a\uff0c\u6bcf\u8f2a\u5148\u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 \\(m\\) \uff0c\u518d\u5224\u65b7 <code>target</code> \u548c <code>nums[m]</code> \u7684\u5927\u5c0f\u95dc\u4fc2\uff0c\u5206\u70ba\u4ee5\u4e0b\u5e7e\u7a2e\u60c5\u6cc1\u3002</p> <ul> <li>\u7576 <code>nums[m] &lt; target</code> \u6216 <code>nums[m] &gt; target</code> \u6642\uff0c\u8aaa\u660e\u9084\u6c92\u6709\u627e\u5230 <code>target</code> \uff0c\u56e0\u6b64\u63a1\u7528\u666e\u901a\u4e8c\u5206\u641c\u5c0b\u7684\u7e2e\u5c0f\u5340\u9593\u64cd\u4f5c\uff0c\u5f9e\u800c\u4f7f\u6307\u6a19 \\(i\\) \u548c \\(j\\) \u5411 <code>target</code> \u9760\u8fd1\u3002</li> <li>\u7576 <code>nums[m] == target</code> \u6642\uff0c\u8aaa\u660e\u5c0f\u65bc <code>target</code> \u7684\u5143\u7d20\u5728\u5340\u9593 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u63a1\u7528 \\(j = m - 1\\) \u4f86\u7e2e\u5c0f\u5340\u9593\uff0c\u5f9e\u800c\u4f7f\u6307\u6a19 \\(j\\) \u5411\u5c0f\u65bc <code>target</code> \u7684\u5143\u7d20\u9760\u8fd1\u3002</li> </ul> <p>\u8ff4\u5708\u5b8c\u6210\u5f8c\uff0c\\(i\\) \u6307\u5411\u6700\u5de6\u908a\u7684 <code>target</code> \uff0c\\(j\\) \u6307\u5411\u9996\u500b\u5c0f\u65bc <code>target</code> \u7684\u5143\u7d20\uff0c\u56e0\u6b64\u7d22\u5f15 \\(i\\) \u5c31\u662f\u63d2\u5165\u9ede\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 10-6 \u00a0 \u4e8c\u5206\u641c\u5c0b\u91cd\u8907\u5143\u7d20\u7684\u63d2\u5165\u9ede\u7684\u6b65\u9a5f </p> <p>\u89c0\u5bdf\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff0c\u5224\u65b7\u5206\u652f <code>nums[m] &gt; target</code> \u548c <code>nums[m] == target</code> \u7684\u64cd\u4f5c\u76f8\u540c\uff0c\u56e0\u6b64\u5169\u8005\u53ef\u4ee5\u5408\u4f75\u3002</p> <p>\u5373\u4fbf\u5982\u6b64\uff0c\u6211\u5011\u4ecd\u7136\u53ef\u4ee5\u5c07\u5224\u65b7\u689d\u4ef6\u4fdd\u6301\u5c55\u958b\uff0c\u56e0\u70ba\u5176\u908f\u8f2f\u66f4\u52a0\u6e05\u6670\u3001\u53ef\u8b80\u6027\u66f4\u597d\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_search_insertion.py<pre><code>def binary_search_insertion(nums: list[int], target: int) -&gt; int:\n \"\"\"\u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u5b58\u5728\u91cd\u8907\u5143\u7d20\uff09\"\"\"\n i, j = 0, len(nums) - 1 # \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while i &lt;= j:\n m = (i + j) // 2 # \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if nums[m] &lt; target:\n i = m + 1 # target \u5728\u5340\u9593 [m+1, j] \u4e2d\n elif nums[m] &gt; target:\n j = m - 1 # target \u5728\u5340\u9593 [i, m-1] \u4e2d\n else:\n j = m - 1 # \u9996\u500b\u5c0f\u65bc target \u7684\u5143\u7d20\u5728\u5340\u9593 [i, m-1] \u4e2d\n # \u8fd4\u56de\u63d2\u5165\u9ede i\n return i\n</code></pre> binary_search_insertion.cpp<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u5b58\u5728\u91cd\u8907\u5143\u7d20\uff09 */\nint binarySearchInsertion(vector&lt;int&gt; &amp;nums, int target) {\n int i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u500b\u5c0f\u65bc target \u7684\u5143\u7d20\u5728\u5340\u9593 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u9ede i\n return i;\n}\n</code></pre> binary_search_insertion.java<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u5b58\u5728\u91cd\u8907\u5143\u7d20\uff09 */\nint binarySearchInsertion(int[] nums, int target) {\n int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u500b\u5c0f\u65bc target \u7684\u5143\u7d20\u5728\u5340\u9593 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u9ede i\n return i;\n}\n</code></pre> binary_search_insertion.cs<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u5b58\u5728\u91cd\u8907\u5143\u7d20\uff09 */\nint BinarySearchInsertion(int[] nums, int target) {\n int i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u500b\u5c0f\u65bc target \u7684\u5143\u7d20\u5728\u5340\u9593 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u9ede i\n return i;\n}\n</code></pre> binary_search_insertion.go<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u5b58\u5728\u91cd\u8907\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums []int, target int) int {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n i, j := 0, len(nums)-1\n for i &lt;= j {\n // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n m := i + (j-i)/2\n if nums[m] &lt; target {\n // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n i = m + 1\n } else if nums[m] &gt; target {\n // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1\n } else {\n // \u9996\u500b\u5c0f\u65bc target \u7684\u5143\u7d20\u5728\u5340\u9593 [i, m-1] \u4e2d\n j = m - 1\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u9ede i\n return i\n}\n</code></pre> binary_search_insertion.swift<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u5b58\u5728\u91cd\u8907\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums: [Int], target: Int) -&gt; Int {\n // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n var i = nums.startIndex\n var j = nums.endIndex - 1\n while i &lt;= j {\n let m = i + (j - i) / 2 // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if nums[m] &lt; target {\n i = m + 1 // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if nums[m] &gt; target {\n j = m - 1 // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n j = m - 1 // \u9996\u500b\u5c0f\u65bc target \u7684\u5143\u7d20\u5728\u5340\u9593 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u9ede i\n return i\n}\n</code></pre> binary_search_insertion.js<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u5b58\u5728\u91cd\u8907\u5143\u7d20\uff09 */\nfunction binarySearchInsertion(nums, target) {\n let i = 0,\n j = nums.length - 1; // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n const m = Math.floor(i + (j - i) / 2); // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n if (nums[m] &lt; target) {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u500b\u5c0f\u65bc target \u7684\u5143\u7d20\u5728\u5340\u9593 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u9ede i\n return i;\n}\n</code></pre> binary_search_insertion.ts<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u5b58\u5728\u91cd\u8907\u5143\u7d20\uff09 */\nfunction binarySearchInsertion(nums: Array&lt;number&gt;, target: number): number {\n let i = 0,\n j = nums.length - 1; // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n const m = Math.floor(i + (j - i) / 2); // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m, \u4f7f\u7528 Math.floor() \u5411\u4e0b\u53d6\u6574\n if (nums[m] &lt; target) {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u500b\u5c0f\u65bc target \u7684\u5143\u7d20\u5728\u5340\u9593 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u9ede i\n return i;\n}\n</code></pre> binary_search_insertion.dart<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u5b58\u5728\u91cd\u8907\u5143\u7d20\uff09 */\nint binarySearchInsertion(List&lt;int&gt; nums, int target) {\n int i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n int m = i + (j - i) ~/ 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u500b\u5c0f\u65bc target \u7684\u5143\u7d20\u5728\u5340\u9593 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u9ede i\n return i;\n}\n</code></pre> binary_search_insertion.rs<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u5b58\u5728\u91cd\u8907\u5143\u7d20\uff09 */\npub fn binary_search_insertion(nums: &amp;[i32], target: i32) -&gt; i32 {\n let (mut i, mut j) = (0, nums.len() as i32 - 1); // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while i &lt;= j {\n let m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if nums[m as usize] &lt; target {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if nums[m as usize] &gt; target {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u500b\u5c0f\u65bc target \u7684\u5143\u7d20\u5728\u5340\u9593 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u9ede i\n i\n}\n</code></pre> binary_search_insertion.c<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u5b58\u5728\u91cd\u8907\u5143\u7d20\uff09 */\nint binarySearchInsertion(int *nums, int numSize, int target) {\n int i = 0, j = numSize - 1; // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n int m = i + (j - i) / 2; // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) {\n i = m + 1; // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1; // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n j = m - 1; // \u9996\u500b\u5c0f\u65bc target \u7684\u5143\u7d20\u5728\u5340\u9593 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u9ede i\n return i;\n}\n</code></pre> binary_search_insertion.kt<pre><code>/* \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u5b58\u5728\u91cd\u8907\u5143\u7d20\uff09 */\nfun binarySearchInsertion(nums: IntArray, target: Int): Int {\n var i = 0\n var j = nums.size - 1 // \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n while (i &lt;= j) {\n val m = i + (j - i) / 2 // \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n if (nums[m] &lt; target) {\n i = m + 1 // target \u5728\u5340\u9593 [m+1, j] \u4e2d\n } else if (nums[m] &gt; target) {\n j = m - 1 // target \u5728\u5340\u9593 [i, m-1] \u4e2d\n } else {\n j = m - 1 // \u9996\u500b\u5c0f\u65bc target \u7684\u5143\u7d20\u5728\u5340\u9593 [i, m-1] \u4e2d\n }\n }\n // \u8fd4\u56de\u63d2\u5165\u9ede i\n return i\n}\n</code></pre> binary_search_insertion.rb<pre><code>### \u4e8c\u5206\u641c\u5c0b\u63d2\u5165\u9ede\uff08\u5b58\u5728\u91cd\u8907\u5143\u7d20\uff09 ###\ndef binary_search_insertion(nums, target)\n # \u521d\u59cb\u5316\u96d9\u9589\u5340\u9593 [0, n-1]\n i, j = 0, nums.length - 1\n\n while i &lt;= j\n # \u8a08\u7b97\u4e2d\u9ede\u7d22\u5f15 m\n m = (i + j) / 2\n\n if nums[m] &lt; target\n i = m + 1 # target \u5728\u5340\u9593 [m+1, j] \u4e2d\n elsif nums[m] &gt; target\n j = m - 1 # target \u5728\u5340\u9593 [i, m-1] \u4e2d\n else\n j = m - 1 # \u9996\u500b\u5c0f\u65bc target \u7684\u5143\u7d20\u5728\u5340\u9593 [i, m-1] \u4e2d\n end\n end\n\n i # \u8fd4\u56de\u63d2\u5165\u9ede i\nend\n</code></pre> binary_search_insertion.zig<pre><code>[class]{}-[func]{binarySearchInsertion}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>Tip</p> <p>\u672c\u7bc0\u7684\u7a0b\u5f0f\u78bc\u90fd\u662f\u201c\u96d9\u9589\u5340\u9593\u201d\u5beb\u6cd5\u3002\u6709\u8208\u8da3\u7684\u8b80\u8005\u53ef\u4ee5\u81ea\u884c\u5be6\u73fe\u201c\u5de6\u9589\u53f3\u958b\u201d\u5beb\u6cd5\u3002</p> <p>\u7e3d\u7684\u4f86\u770b\uff0c\u4e8c\u5206\u641c\u5c0b\u7121\u975e\u5c31\u662f\u7d66\u6307\u6a19 \\(i\\) \u548c \\(j\\) \u5206\u5225\u8a2d\u5b9a\u641c\u5c0b\u76ee\u6a19\uff0c\u76ee\u6a19\u53ef\u80fd\u662f\u4e00\u500b\u5177\u9ad4\u7684\u5143\u7d20\uff08\u4f8b\u5982 <code>target</code> \uff09\uff0c\u4e5f\u53ef\u80fd\u662f\u4e00\u500b\u5143\u7d20\u7bc4\u570d\uff08\u4f8b\u5982\u5c0f\u65bc <code>target</code> \u7684\u5143\u7d20\uff09\u3002</p> <p>\u5728\u4e0d\u65b7\u7684\u8ff4\u5708\u4e8c\u5206\u4e2d\uff0c\u6307\u6a19 \\(i\\) \u548c \\(j\\) \u90fd\u9010\u6f38\u903c\u8fd1\u9810\u5148\u8a2d\u5b9a\u7684\u76ee\u6a19\u3002\u6700\u7d42\uff0c\u5b83\u5011\u6216\u662f\u6210\u529f\u627e\u5230\u7b54\u6848\uff0c\u6216\u662f\u8d8a\u904e\u908a\u754c\u5f8c\u505c\u6b62\u3002</p>"},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.4 \u00a0 \u96dc\u6e4a\u6700\u4f73\u5316\u7b56\u7565","text":"<p>\u5728\u6f14\u7b97\u6cd5\u984c\u4e2d\uff0c\u6211\u5011\u5e38\u900f\u904e\u5c07\u7dda\u6027\u67e5\u8a62\u66ff\u63db\u70ba\u96dc\u6e4a\u67e5\u8a62\u4f86\u964d\u4f4e\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u3002\u6211\u5011\u85c9\u52a9\u4e00\u500b\u6f14\u7b97\u6cd5\u984c\u4f86\u52a0\u6df1\u7406\u89e3\u3002</p> <p>Question</p> <p>\u7d66\u5b9a\u4e00\u500b\u6574\u6578\u9663\u5217 <code>nums</code> \u548c\u4e00\u500b\u76ee\u6a19\u5143\u7d20 <code>target</code> \uff0c\u8acb\u5728\u9663\u5217\u4e2d\u641c\u7d22\u201c\u548c\u201d\u70ba <code>target</code> \u7684\u5169\u500b\u5143\u7d20\uff0c\u4e26\u8fd4\u56de\u5b83\u5011\u7684\u9663\u5217\u7d22\u5f15\u3002\u8fd4\u56de\u4efb\u610f\u4e00\u500b\u89e3\u5373\u53ef\u3002</p>"},{"location":"chapter_searching/replace_linear_by_hashing/#1041","title":"10.4.1 \u00a0 \u7dda\u6027\u67e5\u8a62\uff1a\u4ee5\u6642\u9593\u63db\u7a7a\u9593","text":"<p>\u8003\u616e\u76f4\u63a5\u8d70\u8a2a\u6240\u6709\u53ef\u80fd\u7684\u7d44\u5408\u3002\u5982\u5716 10-9 \u6240\u793a\uff0c\u6211\u5011\u958b\u555f\u4e00\u500b\u5169\u5c64\u8ff4\u5708\uff0c\u5728\u6bcf\u8f2a\u4e2d\u5224\u65b7\u5169\u500b\u6574\u6578\u7684\u548c\u662f\u5426\u70ba <code>target</code> \uff0c\u82e5\u662f\uff0c\u5247\u8fd4\u56de\u5b83\u5011\u7684\u7d22\u5f15\u3002</p> <p></p> <p> \u5716 10-9 \u00a0 \u7dda\u6027\u67e5\u8a62\u6c42\u89e3\u5169\u6578\u4e4b\u548c </p> <p>\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig two_sum.py<pre><code>def two_sum_brute_force(nums: list[int], target: int) -&gt; list[int]:\n \"\"\"\u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u5217\u8209\"\"\"\n # \u5169\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n^2)\n for i in range(len(nums) - 1):\n for j in range(i + 1, len(nums)):\n if nums[i] + nums[j] == target:\n return [i, j]\n return []\n</code></pre> two_sum.cpp<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u5217\u8209 */\nvector&lt;int&gt; twoSumBruteForce(vector&lt;int&gt; &amp;nums, int target) {\n int size = nums.size();\n // \u5169\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n^2)\n for (int i = 0; i &lt; size - 1; i++) {\n for (int j = i + 1; j &lt; size; j++) {\n if (nums[i] + nums[j] == target)\n return {i, j};\n }\n }\n return {};\n}\n</code></pre> two_sum.java<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u5217\u8209 */\nint[] twoSumBruteForce(int[] nums, int target) {\n int size = nums.length;\n // \u5169\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n^2)\n for (int i = 0; i &lt; size - 1; i++) {\n for (int j = i + 1; j &lt; size; j++) {\n if (nums[i] + nums[j] == target)\n return new int[] { i, j };\n }\n }\n return new int[0];\n}\n</code></pre> two_sum.cs<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u5217\u8209 */\nint[] TwoSumBruteForce(int[] nums, int target) {\n int size = nums.Length;\n // \u5169\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n^2)\n for (int i = 0; i &lt; size - 1; i++) {\n for (int j = i + 1; j &lt; size; j++) {\n if (nums[i] + nums[j] == target)\n return [i, j];\n }\n }\n return [];\n}\n</code></pre> two_sum.go<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u5217\u8209 */\nfunc twoSumBruteForce(nums []int, target int) []int {\n size := len(nums)\n // \u5169\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n^2)\n for i := 0; i &lt; size-1; i++ {\n for j := i + 1; j &lt; size; j++ {\n if nums[i]+nums[j] == target {\n return []int{i, j}\n }\n }\n }\n return nil\n}\n</code></pre> two_sum.swift<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u5217\u8209 */\nfunc twoSumBruteForce(nums: [Int], target: Int) -&gt; [Int] {\n // \u5169\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n^2)\n for i in nums.indices.dropLast() {\n for j in nums.indices.dropFirst(i + 1) {\n if nums[i] + nums[j] == target {\n return [i, j]\n }\n }\n }\n return [0]\n}\n</code></pre> two_sum.js<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u5217\u8209 */\nfunction twoSumBruteForce(nums, target) {\n const n = nums.length;\n // \u5169\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n^2)\n for (let i = 0; i &lt; n; i++) {\n for (let j = i + 1; j &lt; n; j++) {\n if (nums[i] + nums[j] === target) {\n return [i, j];\n }\n }\n }\n return [];\n}\n</code></pre> two_sum.ts<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u5217\u8209 */\nfunction twoSumBruteForce(nums: number[], target: number): number[] {\n const n = nums.length;\n // \u5169\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n^2)\n for (let i = 0; i &lt; n; i++) {\n for (let j = i + 1; j &lt; n; j++) {\n if (nums[i] + nums[j] === target) {\n return [i, j];\n }\n }\n }\n return [];\n}\n</code></pre> two_sum.dart<pre><code>/* \u65b9\u6cd5\u4e00\uff1a \u66b4\u529b\u5217\u8209 */\nList&lt;int&gt; twoSumBruteForce(List&lt;int&gt; nums, int target) {\n int size = nums.length;\n // \u5169\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n^2)\n for (var i = 0; i &lt; size - 1; i++) {\n for (var j = i + 1; j &lt; size; j++) {\n if (nums[i] + nums[j] == target) return [i, j];\n }\n }\n return [0];\n}\n</code></pre> two_sum.rs<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u5217\u8209 */\npub fn two_sum_brute_force(nums: &amp;Vec&lt;i32&gt;, target: i32) -&gt; Option&lt;Vec&lt;i32&gt;&gt; {\n let size = nums.len();\n // \u5169\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n^2)\n for i in 0..size - 1 {\n for j in i + 1..size {\n if nums[i] + nums[j] == target {\n return Some(vec![i as i32, j as i32]);\n }\n }\n }\n None\n}\n</code></pre> two_sum.c<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u5217\u8209 */\nint *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {\n for (int i = 0; i &lt; numsSize; ++i) {\n for (int j = i + 1; j &lt; numsSize; ++j) {\n if (nums[i] + nums[j] == target) {\n int *res = malloc(sizeof(int) * 2);\n res[0] = i, res[1] = j;\n *returnSize = 2;\n return res;\n }\n }\n }\n *returnSize = 0;\n return NULL;\n}\n</code></pre> two_sum.kt<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u5217\u8209 */\nfun twoSumBruteForce(nums: IntArray, target: Int): IntArray {\n val size = nums.size\n // \u5169\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n^2)\n for (i in 0..&lt;size - 1) {\n for (j in i + 1..&lt;size) {\n if (nums[i] + nums[j] == target) return intArrayOf(i, j)\n }\n }\n return IntArray(0)\n}\n</code></pre> two_sum.rb<pre><code>### \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u5217\u8209 ###\ndef two_sum_brute_force(nums, target)\n # \u5169\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n^2)\n for i in 0...(nums.length - 1)\n for j in (i + 1)...nums.length\n return [i, j] if nums[i] + nums[j] == target\n end\n end\n\n []\nend\n</code></pre> two_sum.zig<pre><code>// \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u5217\u8209\nfn twoSumBruteForce(nums: []i32, target: i32) ?[2]i32 {\n var size: usize = nums.len;\n var i: usize = 0;\n // \u5169\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n^2)\n while (i &lt; size - 1) : (i += 1) {\n var j = i + 1;\n while (j &lt; size) : (j += 1) {\n if (nums[i] + nums[j] == target) {\n return [_]i32{@intCast(i), @intCast(j)};\n }\n }\n }\n return null;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u6b64\u65b9\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\) \uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(1)\\) \uff0c\u5728\u5927\u8cc7\u6599\u91cf\u4e0b\u975e\u5e38\u8017\u6642\u3002</p>"},{"location":"chapter_searching/replace_linear_by_hashing/#1042","title":"10.4.2 \u00a0 \u96dc\u6e4a\u67e5\u8a62\uff1a\u4ee5\u7a7a\u9593\u63db\u6642\u9593","text":"<p>\u8003\u616e\u85c9\u52a9\u4e00\u500b\u96dc\u6e4a\u8868\uff0c\u9375\u503c\u5c0d\u5206\u5225\u70ba\u9663\u5217\u5143\u7d20\u548c\u5143\u7d20\u7d22\u5f15\u3002\u8ff4\u5708\u8d70\u8a2a\u9663\u5217\uff0c\u6bcf\u8f2a\u57f7\u884c\u5716 10-10 \u6240\u793a\u7684\u6b65\u9a5f\u3002</p> <ol> <li>\u5224\u65b7\u6578\u5b57 <code>target - nums[i]</code> \u662f\u5426\u5728\u96dc\u6e4a\u8868\u4e2d\uff0c\u82e5\u662f\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u9019\u5169\u500b\u5143\u7d20\u7684\u7d22\u5f15\u3002</li> <li>\u5c07\u9375\u503c\u5c0d <code>nums[i]</code> \u548c\u7d22\u5f15 <code>i</code> \u65b0\u589e\u9032\u96dc\u6e4a\u8868\u3002</li> </ol> &lt;1&gt;&lt;2&gt;&lt;3&gt; <p></p> <p></p> <p></p> <p> \u5716 10-10 \u00a0 \u8f14\u52a9\u96dc\u6e4a\u8868\u6c42\u89e3\u5169\u6578\u4e4b\u548c </p> <p>\u5be6\u73fe\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff0c\u50c5\u9700\u55ae\u5c64\u8ff4\u5708\u5373\u53ef\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig two_sum.py<pre><code>def two_sum_hash_table(nums: list[int], target: int) -&gt; list[int]:\n \"\"\"\u65b9\u6cd5\u4e8c\uff1a\u8f14\u52a9\u96dc\u6e4a\u8868\"\"\"\n # \u8f14\u52a9\u96dc\u6e4a\u8868\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n dic = {}\n # \u55ae\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n for i in range(len(nums)):\n if target - nums[i] in dic:\n return [dic[target - nums[i]], i]\n dic[nums[i]] = i\n return []\n</code></pre> two_sum.cpp<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f14\u52a9\u96dc\u6e4a\u8868 */\nvector&lt;int&gt; twoSumHashTable(vector&lt;int&gt; &amp;nums, int target) {\n int size = nums.size();\n // \u8f14\u52a9\u96dc\u6e4a\u8868\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n unordered_map&lt;int, int&gt; dic;\n // \u55ae\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n for (int i = 0; i &lt; size; i++) {\n if (dic.find(target - nums[i]) != dic.end()) {\n return {dic[target - nums[i]], i};\n }\n dic.emplace(nums[i], i);\n }\n return {};\n}\n</code></pre> two_sum.java<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f14\u52a9\u96dc\u6e4a\u8868 */\nint[] twoSumHashTable(int[] nums, int target) {\n int size = nums.length;\n // \u8f14\u52a9\u96dc\u6e4a\u8868\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n Map&lt;Integer, Integer&gt; dic = new HashMap&lt;&gt;();\n // \u55ae\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n for (int i = 0; i &lt; size; i++) {\n if (dic.containsKey(target - nums[i])) {\n return new int[] { dic.get(target - nums[i]), i };\n }\n dic.put(nums[i], i);\n }\n return new int[0];\n}\n</code></pre> two_sum.cs<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f14\u52a9\u96dc\u6e4a\u8868 */\nint[] TwoSumHashTable(int[] nums, int target) {\n int size = nums.Length;\n // \u8f14\u52a9\u96dc\u6e4a\u8868\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n Dictionary&lt;int, int&gt; dic = [];\n // \u55ae\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n for (int i = 0; i &lt; size; i++) {\n if (dic.ContainsKey(target - nums[i])) {\n return [dic[target - nums[i]], i];\n }\n dic.Add(nums[i], i);\n }\n return [];\n}\n</code></pre> two_sum.go<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f14\u52a9\u96dc\u6e4a\u8868 */\nfunc twoSumHashTable(nums []int, target int) []int {\n // \u8f14\u52a9\u96dc\u6e4a\u8868\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n hashTable := map[int]int{}\n // \u55ae\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n for idx, val := range nums {\n if preIdx, ok := hashTable[target-val]; ok {\n return []int{preIdx, idx}\n }\n hashTable[val] = idx\n }\n return nil\n}\n</code></pre> two_sum.swift<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f14\u52a9\u96dc\u6e4a\u8868 */\nfunc twoSumHashTable(nums: [Int], target: Int) -&gt; [Int] {\n // \u8f14\u52a9\u96dc\u6e4a\u8868\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n var dic: [Int: Int] = [:]\n // \u55ae\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n for i in nums.indices {\n if let j = dic[target - nums[i]] {\n return [j, i]\n }\n dic[nums[i]] = i\n }\n return [0]\n}\n</code></pre> two_sum.js<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f14\u52a9\u96dc\u6e4a\u8868 */\nfunction twoSumHashTable(nums, target) {\n // \u8f14\u52a9\u96dc\u6e4a\u8868\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n let m = {};\n // \u55ae\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n for (let i = 0; i &lt; nums.length; i++) {\n if (m[target - nums[i]] !== undefined) {\n return [m[target - nums[i]], i];\n } else {\n m[nums[i]] = i;\n }\n }\n return [];\n}\n</code></pre> two_sum.ts<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f14\u52a9\u96dc\u6e4a\u8868 */\nfunction twoSumHashTable(nums: number[], target: number): number[] {\n // \u8f14\u52a9\u96dc\u6e4a\u8868\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n let m: Map&lt;number, number&gt; = new Map();\n // \u55ae\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n for (let i = 0; i &lt; nums.length; i++) {\n let index = m.get(target - nums[i]);\n if (index !== undefined) {\n return [index, i];\n } else {\n m.set(nums[i], i);\n }\n }\n return [];\n}\n</code></pre> two_sum.dart<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a \u8f14\u52a9\u96dc\u6e4a\u8868 */\nList&lt;int&gt; twoSumHashTable(List&lt;int&gt; nums, int target) {\n int size = nums.length;\n // \u8f14\u52a9\u96dc\u6e4a\u8868\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n Map&lt;int, int&gt; dic = HashMap();\n // \u55ae\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n for (var i = 0; i &lt; size; i++) {\n if (dic.containsKey(target - nums[i])) {\n return [dic[target - nums[i]]!, i];\n }\n dic.putIfAbsent(nums[i], () =&gt; i);\n }\n return [0];\n}\n</code></pre> two_sum.rs<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f14\u52a9\u96dc\u6e4a\u8868 */\npub fn two_sum_hash_table(nums: &amp;Vec&lt;i32&gt;, target: i32) -&gt; Option&lt;Vec&lt;i32&gt;&gt; {\n // \u8f14\u52a9\u96dc\u6e4a\u8868\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n let mut dic = HashMap::new();\n // \u55ae\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n for (i, num) in nums.iter().enumerate() {\n match dic.get(&amp;(target - num)) {\n Some(v) =&gt; return Some(vec![*v as i32, i as i32]),\n None =&gt; dic.insert(num, i as i32),\n };\n }\n None\n}\n</code></pre> two_sum.c<pre><code>/* \u96dc\u6e4a\u8868 */\ntypedef struct {\n int key;\n int val;\n UT_hash_handle hh; // \u57fa\u65bc uthash.h \u5be6\u73fe\n} HashTable;\n\n/* \u96dc\u6e4a\u8868\u67e5\u8a62 */\nHashTable *find(HashTable *h, int key) {\n HashTable *tmp;\n HASH_FIND_INT(h, &amp;key, tmp);\n return tmp;\n}\n\n/* \u96dc\u6e4a\u8868\u5143\u7d20\u63d2\u5165 */\nvoid insert(HashTable *h, int key, int val) {\n HashTable *t = find(h, key);\n if (t == NULL) {\n HashTable *tmp = malloc(sizeof(HashTable));\n tmp-&gt;key = key, tmp-&gt;val = val;\n HASH_ADD_INT(h, key, tmp);\n } else {\n t-&gt;val = val;\n }\n}\n\n/* \u65b9\u6cd5\u4e8c\uff1a\u8f14\u52a9\u96dc\u6e4a\u8868 */\nint *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {\n HashTable *hashtable = NULL;\n for (int i = 0; i &lt; numsSize; i++) {\n HashTable *t = find(hashtable, target - nums[i]);\n if (t != NULL) {\n int *res = malloc(sizeof(int) * 2);\n res[0] = t-&gt;val, res[1] = i;\n *returnSize = 2;\n return res;\n }\n insert(hashtable, nums[i], i);\n }\n *returnSize = 0;\n return NULL;\n}\n</code></pre> two_sum.kt<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f14\u52a9\u96dc\u6e4a\u8868 */\nfun twoSumHashTable(nums: IntArray, target: Int): IntArray {\n val size = nums.size\n // \u8f14\u52a9\u96dc\u6e4a\u8868\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n val dic = HashMap&lt;Int, Int&gt;()\n // \u55ae\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n for (i in 0..&lt;size) {\n if (dic.containsKey(target - nums[i])) {\n return intArrayOf(dic[target - nums[i]]!!, i)\n }\n dic[nums[i]] = i\n }\n return IntArray(0)\n}\n</code></pre> two_sum.rb<pre><code>### \u65b9\u6cd5\u4e8c\uff1a\u8f14\u52a9\u96dc\u6e4a\u8868 ###\ndef two_sum_hash_table(nums, target)\n # \u8f14\u52a9\u96dc\u6e4a\u8868\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n dic = {}\n # \u55ae\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n for i in 0...nums.length\n return [dic[target - nums[i]], i] if dic.has_key?(target - nums[i])\n\n dic[nums[i]] = i\n end\n\n []\nend\n</code></pre> two_sum.zig<pre><code>// \u65b9\u6cd5\u4e8c\uff1a\u8f14\u52a9\u96dc\u6e4a\u8868\nfn twoSumHashTable(nums: []i32, target: i32) !?[2]i32 {\n var size: usize = nums.len;\n // \u8f14\u52a9\u96dc\u6e4a\u8868\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n var dic = std.AutoHashMap(i32, i32).init(std.heap.page_allocator);\n defer dic.deinit();\n var i: usize = 0;\n // \u55ae\u5c64\u8ff4\u5708\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\n while (i &lt; size) : (i += 1) {\n if (dic.contains(target - nums[i])) {\n return [_]i32{dic.get(target - nums[i]).?, @intCast(i)};\n }\n try dic.put(nums[i], @intCast(i));\n }\n return null;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u6b64\u65b9\u6cd5\u900f\u904e\u96dc\u6e4a\u67e5\u8a62\u5c07\u6642\u9593\u8907\u96dc\u5ea6\u5f9e \\(O(n^2)\\) \u964d\u81f3 \\(O(n)\\) \uff0c\u5927\u5e45\u63d0\u5347\u57f7\u884c\u6548\u7387\u3002</p> <p>\u7531\u65bc\u9700\u8981\u7dad\u8b77\u4e00\u500b\u984d\u5916\u7684\u96dc\u6e4a\u8868\uff0c\u56e0\u6b64\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \u3002\u5118\u7ba1\u5982\u6b64\uff0c\u8a72\u65b9\u6cd5\u7684\u6574\u9ad4\u6642\u7a7a\u6548\u7387\u66f4\u70ba\u5747\u8861\uff0c\u56e0\u6b64\u5b83\u662f\u672c\u984c\u7684\u6700\u512a\u89e3\u6cd5\u3002</p>"},{"location":"chapter_searching/searching_algorithm_revisited/","title":"10.5 \u00a0 \u91cd\u8b58\u641c\u5c0b\u6f14\u7b97\u6cd5","text":"<p>\u641c\u5c0b\u6f14\u7b97\u6cd5\uff08searching algorithm\uff09\u7528\u65bc\u5728\u8cc7\u6599\u7d50\u69cb\uff08\u4f8b\u5982\u9663\u5217\u3001\u93c8\u7d50\u4e32\u5217\u3001\u6a39\u6216\u5716\uff09\u4e2d\u641c\u7d22\u4e00\u500b\u6216\u4e00\u7d44\u6eff\u8db3\u7279\u5b9a\u689d\u4ef6\u7684\u5143\u7d20\u3002</p> <p>\u641c\u5c0b\u6f14\u7b97\u6cd5\u53ef\u6839\u64da\u5be6\u73fe\u601d\u8def\u5206\u70ba\u4ee5\u4e0b\u5169\u985e\u3002</p> <ul> <li>\u900f\u904e\u8d70\u8a2a\u8cc7\u6599\u7d50\u69cb\u4f86\u5b9a\u4f4d\u76ee\u6a19\u5143\u7d20\uff0c\u4f8b\u5982\u9663\u5217\u3001\u93c8\u7d50\u4e32\u5217\u3001\u6a39\u548c\u5716\u7684\u8d70\u8a2a\u7b49\u3002</li> <li>\u5229\u7528\u8cc7\u6599\u7d44\u7e54\u7d50\u69cb\u6216\u8cc7\u6599\u5305\u542b\u7684\u5148\u9a57\u8cc7\u8a0a\uff0c\u5be6\u73fe\u9ad8\u6548\u5143\u7d20\u67e5\u8a62\uff0c\u4f8b\u5982\u4e8c\u5206\u641c\u5c0b\u3001\u96dc\u6e4a\u67e5\u8a62\u548c\u4e8c\u5143\u641c\u5c0b\u6a39\u67e5\u8a62\u7b49\u3002</li> </ul> <p>\u4e0d\u96e3\u767c\u73fe\uff0c\u9019\u4e9b\u77e5\u8b58\u9ede\u90fd\u5df2\u5728\u524d\u9762\u7684\u7ae0\u7bc0\u4e2d\u4ecb\u7d39\u904e\uff0c\u56e0\u6b64\u641c\u5c0b\u6f14\u7b97\u6cd5\u5c0d\u65bc\u6211\u5011\u4f86\u8aaa\u4e26\u4e0d\u964c\u751f\u3002\u5728\u672c\u7bc0\u4e2d\uff0c\u6211\u5011\u5c07\u5f9e\u66f4\u52a0\u7cfb\u7d71\u7684\u8996\u89d2\u5207\u5165\uff0c\u91cd\u65b0\u5be9\u8996\u641c\u5c0b\u6f14\u7b97\u6cd5\u3002</p>"},{"location":"chapter_searching/searching_algorithm_revisited/#1051","title":"10.5.1 \u00a0 \u66b4\u529b\u641c\u5c0b","text":"<p>\u66b4\u529b\u641c\u5c0b\u900f\u904e\u8d70\u8a2a\u8cc7\u6599\u7d50\u69cb\u7684\u6bcf\u500b\u5143\u7d20\u4f86\u5b9a\u4f4d\u76ee\u6a19\u5143\u7d20\u3002</p> <ul> <li>\u201c\u7dda\u6027\u641c\u5c0b\u201d\u9069\u7528\u65bc\u9663\u5217\u548c\u93c8\u7d50\u4e32\u5217\u7b49\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\u3002\u5b83\u5f9e\u8cc7\u6599\u7d50\u69cb\u7684\u4e00\u7aef\u958b\u59cb\uff0c\u9010\u500b\u8a2a\u554f\u5143\u7d20\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6a19\u5143\u7d20\u6216\u5230\u9054\u53e6\u4e00\u7aef\u4ecd\u6c92\u6709\u627e\u5230\u76ee\u6a19\u5143\u7d20\u70ba\u6b62\u3002</li> <li>\u201c\u5ee3\u5ea6\u512a\u5148\u641c\u5c0b\u201d\u548c\u201c\u6df1\u5ea6\u512a\u5148\u641c\u5c0b\u201d\u662f\u5716\u548c\u6a39\u7684\u5169\u7a2e\u8d70\u8a2a\u7b56\u7565\u3002\u5ee3\u5ea6\u512a\u5148\u641c\u5c0b\u5f9e\u521d\u59cb\u7bc0\u9ede\u958b\u59cb\u9010\u5c64\u641c\u5c0b\uff0c\u7531\u8fd1\u53ca\u9060\u5730\u8a2a\u554f\u5404\u500b\u7bc0\u9ede\u3002\u6df1\u5ea6\u512a\u5148\u641c\u5c0b\u5f9e\u521d\u59cb\u7bc0\u9ede\u958b\u59cb\uff0c\u6cbf\u8457\u4e00\u689d\u8def\u5f91\u8d70\u5230\u982d\uff0c\u518d\u56de\u6eaf\u4e26\u5617\u8a66\u5176\u4ed6\u8def\u5f91\uff0c\u76f4\u5230\u8d70\u8a2a\u5b8c\u6574\u500b\u8cc7\u6599\u7d50\u69cb\u3002</li> </ul> <p>\u66b4\u529b\u641c\u5c0b\u7684\u512a\u9ede\u662f\u7c21\u55ae\u4e14\u901a\u7528\u6027\u597d\uff0c\u7121\u9808\u5c0d\u8cc7\u6599\u505a\u9810\u8655\u7406\u548c\u85c9\u52a9\u984d\u5916\u7684\u8cc7\u6599\u7d50\u69cb\u3002</p> <p>\u7136\u800c\uff0c\u6b64\u985e\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u70ba\u5143\u7d20\u6578\u91cf\uff0c\u56e0\u6b64\u5728\u8cc7\u6599\u91cf\u8f03\u5927\u7684\u60c5\u6cc1\u4e0b\u6548\u80fd\u8f03\u5dee\u3002</p>"},{"location":"chapter_searching/searching_algorithm_revisited/#1052","title":"10.5.2 \u00a0 \u81ea\u9069\u61c9\u641c\u5c0b","text":"<p>\u81ea\u9069\u61c9\u641c\u5c0b\u5229\u7528\u8cc7\u6599\u7684\u7279\u6709\u5c6c\u6027\uff08\u4f8b\u5982\u6709\u5e8f\u6027\uff09\u4f86\u6700\u4f73\u5316\u641c\u5c0b\u904e\u7a0b\uff0c\u5f9e\u800c\u66f4\u9ad8\u6548\u5730\u5b9a\u4f4d\u76ee\u6a19\u5143\u7d20\u3002</p> <ul> <li>\u201c\u4e8c\u5206\u641c\u5c0b\u201d\u5229\u7528\u8cc7\u6599\u7684\u6709\u5e8f\u6027\u5be6\u73fe\u9ad8\u6548\u67e5\u8a62\uff0c\u50c5\u9069\u7528\u65bc\u9663\u5217\u3002</li> <li>\u201c\u96dc\u6e4a\u67e5\u8a62\u201d\u5229\u7528\u96dc\u6e4a\u8868\u5c07\u641c\u5c0b\u8cc7\u6599\u548c\u76ee\u6a19\u8cc7\u6599\u5efa\u7acb\u70ba\u9375\u503c\u5c0d\u5c0d\u6620\uff0c\u5f9e\u800c\u5be6\u73fe\u67e5\u8a62\u64cd\u4f5c\u3002</li> <li>\u201c\u6a39\u67e5\u8a62\u201d\u5728\u7279\u5b9a\u7684\u6a39\u7d50\u69cb\uff08\u4f8b\u5982\u4e8c\u5143\u641c\u5c0b\u6a39\uff09\u4e2d\uff0c\u57fa\u65bc\u6bd4\u8f03\u7bc0\u9ede\u503c\u4f86\u5feb\u901f\u6392\u9664\u7bc0\u9ede\uff0c\u5f9e\u800c\u5b9a\u4f4d\u76ee\u6a19\u5143\u7d20\u3002</li> </ul> <p>\u6b64\u985e\u6f14\u7b97\u6cd5\u7684\u512a\u9ede\u662f\u6548\u7387\u9ad8\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u53ef\u9054\u5230 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \u3002</p> <p>\u7136\u800c\uff0c\u4f7f\u7528\u9019\u4e9b\u6f14\u7b97\u6cd5\u5f80\u5f80\u9700\u8981\u5c0d\u8cc7\u6599\u9032\u884c\u9810\u8655\u7406\u3002\u4f8b\u5982\uff0c\u4e8c\u5206\u641c\u5c0b\u9700\u8981\u9810\u5148\u5c0d\u9663\u5217\u9032\u884c\u6392\u5e8f\uff0c\u96dc\u6e4a\u67e5\u8a62\u548c\u6a39\u67e5\u8a62\u90fd\u9700\u8981\u85c9\u52a9\u984d\u5916\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u7dad\u8b77\u9019\u4e9b\u8cc7\u6599\u7d50\u69cb\u4e5f\u9700\u8981\u984d\u5916\u7684\u6642\u9593\u548c\u7a7a\u9593\u958b\u92b7\u3002</p> <p>Tip</p> <p>\u81ea\u9069\u61c9\u641c\u5c0b\u6f14\u7b97\u6cd5\u5e38\u88ab\u7a31\u70ba\u67e5\u8a62\u6f14\u7b97\u6cd5\uff0c\u4e3b\u8981\u7528\u65bc\u5728\u7279\u5b9a\u8cc7\u6599\u7d50\u69cb\u4e2d\u5feb\u901f\u6aa2\u7d22\u76ee\u6a19\u5143\u7d20\u3002</p>"},{"location":"chapter_searching/searching_algorithm_revisited/#1053","title":"10.5.3 \u00a0 \u641c\u5c0b\u65b9\u6cd5\u9078\u53d6","text":"<p>\u7d66\u5b9a\u5927\u5c0f\u70ba \\(n\\) \u7684\u4e00\u7d44\u8cc7\u6599\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u7dda\u6027\u641c\u5c0b\u3001\u4e8c\u5206\u641c\u5c0b\u3001\u6a39\u67e5\u8a62\u3001\u96dc\u6e4a\u67e5\u8a62\u7b49\u591a\u7a2e\u65b9\u6cd5\u5f9e\u4e2d\u641c\u7d22\u76ee\u6a19\u5143\u7d20\u3002\u5404\u500b\u65b9\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u5982\u5716 10-11 \u6240\u793a\u3002</p> <p></p> <p> \u5716 10-11 \u00a0 \u591a\u7a2e\u641c\u5c0b\u7b56\u7565 </p> <p>\u4e0a\u8ff0\u5e7e\u7a2e\u65b9\u6cd5\u7684\u64cd\u4f5c\u6548\u7387\u8207\u7279\u6027\u5982\u8868 10-1 \u6240\u793a\u3002</p> <p> \u8868 10-1 \u00a0 \u67e5\u8a62\u6f14\u7b97\u6cd5\u6548\u7387\u5c0d\u6bd4 </p> \u7dda\u6027\u641c\u5c0b \u4e8c\u5206\u641c\u5c0b \u6a39\u67e5\u8a62 \u96dc\u6e4a\u67e5\u8a62 \u67e5\u8a62\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)\\) \u522a\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u984d\u5916\u7a7a\u9593 \\(O(1)\\) \\(O(1)\\) \\(O(n)\\) \\(O(n)\\) \u8cc7\u6599\u9810\u8655\u7406 / \u6392\u5e8f \\(O(n \\log n)\\) \u5efa\u6a39 \\(O(n \\log n)\\) \u5efa\u96dc\u6e4a\u8868 \\(O(n)\\) \u8cc7\u6599\u662f\u5426\u6709\u5e8f \u7121\u5e8f \u6709\u5e8f \u6709\u5e8f \u7121\u5e8f <p>\u641c\u5c0b\u6f14\u7b97\u6cd5\u7684\u9078\u64c7\u9084\u53d6\u6c7a\u65bc\u8cc7\u6599\u9ad4\u91cf\u3001\u641c\u5c0b\u6548\u80fd\u8981\u6c42\u3001\u8cc7\u6599\u67e5\u8a62\u8207\u66f4\u65b0\u983b\u7387\u7b49\u3002</p> <p>\u7dda\u6027\u641c\u5c0b</p> <ul> <li>\u901a\u7528\u6027\u8f03\u597d\uff0c\u7121\u9808\u4efb\u4f55\u8cc7\u6599\u9810\u8655\u7406\u64cd\u4f5c\u3002\u5047\u5982\u6211\u5011\u50c5\u9700\u67e5\u8a62\u4e00\u6b21\u8cc7\u6599\uff0c\u90a3\u9ebc\u5176\u4ed6\u4e09\u7a2e\u65b9\u6cd5\u7684\u8cc7\u6599\u9810\u8655\u7406\u7684\u6642\u9593\u6bd4\u7dda\u6027\u641c\u5c0b\u7684\u6642\u9593\u9084\u8981\u66f4\u9577\u3002</li> <li>\u9069\u7528\u65bc\u9ad4\u91cf\u8f03\u5c0f\u7684\u8cc7\u6599\uff0c\u6b64\u60c5\u6cc1\u4e0b\u6642\u9593\u8907\u96dc\u5ea6\u5c0d\u6548\u7387\u5f71\u97ff\u8f03\u5c0f\u3002</li> <li>\u9069\u7528\u65bc\u8cc7\u6599\u66f4\u65b0\u983b\u7387\u8f03\u9ad8\u7684\u5834\u666f\uff0c\u56e0\u70ba\u8a72\u65b9\u6cd5\u4e0d\u9700\u8981\u5c0d\u8cc7\u6599\u9032\u884c\u4efb\u4f55\u984d\u5916\u7dad\u8b77\u3002</li> </ul> <p>\u4e8c\u5206\u641c\u5c0b</p> <ul> <li>\u9069\u7528\u65bc\u5927\u8cc7\u6599\u91cf\u7684\u60c5\u6cc1\uff0c\u6548\u7387\u8868\u73fe\u7a69\u5b9a\uff0c\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(\\log n)\\) \u3002</li> <li>\u8cc7\u6599\u91cf\u4e0d\u80fd\u904e\u5927\uff0c\u56e0\u70ba\u5132\u5b58\u9663\u5217\u9700\u8981\u9023\u7e8c\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u3002</li> <li>\u4e0d\u9069\u7528\u65bc\u9ad8\u983b\u589e\u522a\u8cc7\u6599\u7684\u5834\u666f\uff0c\u56e0\u70ba\u7dad\u8b77\u6709\u5e8f\u9663\u5217\u7684\u958b\u92b7\u8f03\u5927\u3002</li> </ul> <p>\u96dc\u6e4a\u67e5\u8a62</p> <ul> <li>\u9069\u5408\u5c0d\u67e5\u8a62\u6548\u80fd\u8981\u6c42\u5f88\u9ad8\u7684\u5834\u666f\uff0c\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(1)\\) \u3002</li> <li>\u4e0d\u9069\u5408\u9700\u8981\u6709\u5e8f\u8cc7\u6599\u6216\u7bc4\u570d\u67e5\u8a62\u7684\u5834\u666f\uff0c\u56e0\u70ba\u96dc\u6e4a\u8868\u7121\u6cd5\u7dad\u8b77\u8cc7\u6599\u7684\u6709\u5e8f\u6027\u3002</li> <li>\u5c0d\u96dc\u6e4a\u51fd\u5f0f\u548c\u96dc\u6e4a\u885d\u7a81\u8655\u7406\u7b56\u7565\u7684\u4f9d\u8cf4\u6027\u8f03\u9ad8\uff0c\u5177\u6709\u8f03\u5927\u7684\u6548\u80fd\u52a3\u5316\u98a8\u96aa\u3002</li> <li>\u4e0d\u9069\u5408\u8cc7\u6599\u91cf\u904e\u5927\u7684\u60c5\u6cc1\uff0c\u56e0\u70ba\u96dc\u6e4a\u8868\u9700\u8981\u984d\u5916\u7a7a\u9593\u4f86\u6700\u5927\u7a0b\u5ea6\u5730\u6e1b\u5c11\u885d\u7a81\uff0c\u5f9e\u800c\u63d0\u4f9b\u826f\u597d\u7684\u67e5\u8a62\u6548\u80fd\u3002</li> </ul> <p>\u6a39\u67e5\u8a62</p> <ul> <li>\u9069\u7528\u65bc\u6d77\u91cf\u8cc7\u6599\uff0c\u56e0\u70ba\u6a39\u7bc0\u9ede\u5728\u8a18\u61b6\u9ad4\u4e2d\u662f\u5206\u6563\u5132\u5b58\u7684\u3002</li> <li>\u9069\u5408\u9700\u8981\u7dad\u8b77\u6709\u5e8f\u8cc7\u6599\u6216\u7bc4\u570d\u67e5\u8a62\u7684\u5834\u666f\u3002</li> <li>\u5728\u6301\u7e8c\u589e\u522a\u7bc0\u9ede\u7684\u904e\u7a0b\u4e2d\uff0c\u4e8c\u5143\u641c\u5c0b\u6a39\u53ef\u80fd\u7522\u751f\u50be\u659c\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u52a3\u5316\u81f3 \\(O(n)\\) \u3002</li> <li>\u82e5\u4f7f\u7528 AVL \u6a39\u6216\u7d05\u9ed1\u6a39\uff0c\u5247\u5404\u9805\u64cd\u4f5c\u53ef\u5728 \\(O(\\log n)\\) \u6548\u7387\u4e0b\u7a69\u5b9a\u57f7\u884c\uff0c\u4f46\u7dad\u8b77\u6a39\u5e73\u8861\u7684\u64cd\u4f5c\u6703\u589e\u52a0\u984d\u5916\u7684\u958b\u92b7\u3002</li> </ul>"},{"location":"chapter_searching/summary/","title":"10.6 \u00a0 \u5c0f\u7d50","text":"<ul> <li>\u4e8c\u5206\u641c\u5c0b\u4f9d\u8cf4\u8cc7\u6599\u7684\u6709\u5e8f\u6027\uff0c\u900f\u904e\u8ff4\u5708\u9010\u6b65\u7e2e\u6e1b\u4e00\u534a\u641c\u5c0b\u5340\u9593\u4f86\u9032\u884c\u67e5\u8a62\u3002\u5b83\u8981\u6c42\u8f38\u5165\u8cc7\u6599\u6709\u5e8f\uff0c\u4e14\u50c5\u9069\u7528\u65bc\u9663\u5217\u6216\u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u8cc7\u6599\u7d50\u69cb\u3002</li> <li>\u66b4\u529b\u641c\u5c0b\u900f\u904e\u8d70\u8a2a\u8cc7\u6599\u7d50\u69cb\u4f86\u5b9a\u4f4d\u8cc7\u6599\u3002\u7dda\u6027\u641c\u5c0b\u9069\u7528\u65bc\u9663\u5217\u548c\u93c8\u7d50\u4e32\u5217\uff0c\u5ee3\u5ea6\u512a\u5148\u641c\u5c0b\u548c\u6df1\u5ea6\u512a\u5148\u641c\u5c0b\u9069\u7528\u65bc\u5716\u548c\u6a39\u3002\u6b64\u985e\u6f14\u7b97\u6cd5\u901a\u7528\u6027\u597d\uff0c\u7121\u9808\u5c0d\u8cc7\u6599\u9032\u884c\u9810\u8655\u7406\uff0c\u4f46\u6642\u9593\u8907\u96dc\u5ea6 \\(O(n)\\) \u8f03\u9ad8\u3002</li> <li>\u96dc\u6e4a\u67e5\u8a62\u3001\u6a39\u67e5\u8a62\u548c\u4e8c\u5206\u641c\u5c0b\u5c6c\u65bc\u9ad8\u6548\u641c\u5c0b\u65b9\u6cd5\uff0c\u53ef\u5728\u7279\u5b9a\u8cc7\u6599\u7d50\u69cb\u4e2d\u5feb\u901f\u5b9a\u4f4d\u76ee\u6a19\u5143\u7d20\u3002\u6b64\u985e\u6f14\u7b97\u6cd5\u6548\u7387\u9ad8\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u53ef\u9054 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \uff0c\u4f46\u901a\u5e38\u9700\u8981\u85c9\u52a9\u984d\u5916\u8cc7\u6599\u7d50\u69cb\u3002</li> <li>\u5be6\u969b\u4e2d\uff0c\u6211\u5011\u9700\u8981\u5c0d\u8cc7\u6599\u9ad4\u91cf\u3001\u641c\u5c0b\u6548\u80fd\u8981\u6c42\u3001\u8cc7\u6599\u67e5\u8a62\u548c\u66f4\u65b0\u983b\u7387\u7b49\u56e0\u7d20\u9032\u884c\u5177\u9ad4\u5206\u6790\uff0c\u5f9e\u800c\u9078\u64c7\u5408\u9069\u7684\u641c\u5c0b\u65b9\u6cd5\u3002</li> <li>\u7dda\u6027\u641c\u5c0b\u9069\u7528\u65bc\u5c0f\u578b\u6216\u983b\u7e41\u66f4\u65b0\u7684\u8cc7\u6599\uff1b\u4e8c\u5206\u641c\u5c0b\u9069\u7528\u65bc\u5927\u578b\u3001\u6392\u5e8f\u7684\u8cc7\u6599\uff1b\u96dc\u6e4a\u67e5\u8a62\u9069\u7528\u65bc\u5c0d\u67e5\u8a62\u6548\u7387\u8981\u6c42\u8f03\u9ad8\u4e14\u7121\u9808\u7bc4\u570d\u67e5\u8a62\u7684\u8cc7\u6599\uff1b\u6a39\u67e5\u8a62\u9069\u7528\u65bc\u9700\u8981\u7dad\u8b77\u9806\u5e8f\u548c\u652f\u6301\u7bc4\u570d\u67e5\u8a62\u7684\u5927\u578b\u52d5\u614b\u8cc7\u6599\u3002</li> <li>\u7528\u96dc\u6e4a\u67e5\u8a62\u66ff\u63db\u7dda\u6027\u67e5\u8a62\u662f\u4e00\u7a2e\u5e38\u7528\u7684\u6700\u4f73\u5316\u57f7\u884c\u6642\u9593\u7684\u7b56\u7565\uff0c\u53ef\u5c07\u6642\u9593\u8907\u96dc\u5ea6\u5f9e \\(O(n)\\) \u964d\u81f3 \\(O(1)\\) \u3002</li> </ul>"},{"location":"chapter_sorting/","title":"\u7b2c 11 \u7ae0 \u00a0 \u6392\u5e8f","text":"<p>Abstract</p> <p>\u6392\u5e8f\u7336\u5982\u4e00\u628a\u5c07\u6df7\u4e82\u8b8a\u70ba\u79e9\u5e8f\u7684\u9b54\u6cd5\u9470\u5319\uff0c\u4f7f\u6211\u5011\u80fd\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u7406\u89e3\u8207\u8655\u7406\u8cc7\u6599\u3002</p> <p>\u7121\u8ad6\u662f\u7c21\u55ae\u7684\u5347\u5e8f\uff0c\u9084\u662f\u8907\u96dc\u7684\u5206\u985e\u6392\u5217\uff0c\u6392\u5e8f\u90fd\u5411\u6211\u5011\u5c55\u793a\u4e86\u8cc7\u6599\u7684\u548c\u8ae7\u7f8e\u611f\u3002</p>"},{"location":"chapter_sorting/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>11.1 \u00a0 \u6392\u5e8f\u6f14\u7b97\u6cd5</li> <li>11.2 \u00a0 \u9078\u64c7\u6392\u5e8f</li> <li>11.3 \u00a0 \u6ce1\u6cab\u6392\u5e8f</li> <li>11.4 \u00a0 \u63d2\u5165\u6392\u5e8f</li> <li>11.5 \u00a0 \u5feb\u901f\u6392\u5e8f</li> <li>11.6 \u00a0 \u5408\u4f75\u6392\u5e8f</li> <li>11.7 \u00a0 \u5806\u7a4d\u6392\u5e8f</li> <li>11.8 \u00a0 \u6876\u6392\u5e8f</li> <li>11.9 \u00a0 \u8a08\u6578\u6392\u5e8f</li> <li>11.10 \u00a0 \u57fa\u6578\u6392\u5e8f</li> <li>11.11 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_sorting/bubble_sort/","title":"11.3 \u00a0 \u6ce1\u6cab\u6392\u5e8f","text":"<p>\u6ce1\u6cab\u6392\u5e8f\uff08bubble sort\uff09\u900f\u904e\u9023\u7e8c\u5730\u6bd4\u8f03\u8207\u4ea4\u63db\u76f8\u9130\u5143\u7d20\u5be6\u73fe\u6392\u5e8f\u3002\u9019\u500b\u904e\u7a0b\u5c31\u50cf\u6c23\u6ce1\u5f9e\u5e95\u90e8\u5347\u5230\u9802\u90e8\u4e00\u6a23\uff0c\u56e0\u6b64\u5f97\u540d\u6ce1\u6cab\u6392\u5e8f\u3002</p> <p>\u5982\u5716 11-4 \u6240\u793a\uff0c\u5192\u6ce1\u904e\u7a0b\u53ef\u4ee5\u5229\u7528\u5143\u7d20\u4ea4\u63db\u64cd\u4f5c\u4f86\u6a21\u64ec\uff1a\u5f9e\u9663\u5217\u6700\u5de6\u7aef\u958b\u59cb\u5411\u53f3\u8d70\u8a2a\uff0c\u4f9d\u6b21\u6bd4\u8f03\u76f8\u9130\u5143\u7d20\u5927\u5c0f\uff0c\u5982\u679c\u201c\u5de6\u5143\u7d20 &gt; \u53f3\u5143\u7d20\u201d\u5c31\u4ea4\u63db\u4e8c\u8005\u3002\u8d70\u8a2a\u5b8c\u6210\u5f8c\uff0c\u6700\u5927\u7684\u5143\u7d20\u6703\u88ab\u79fb\u52d5\u5230\u9663\u5217\u7684\u6700\u53f3\u7aef\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 11-4 \u00a0 \u5229\u7528\u5143\u7d20\u4ea4\u63db\u64cd\u4f5c\u6a21\u64ec\u5192\u6ce1 </p>"},{"location":"chapter_sorting/bubble_sort/#1131","title":"11.3.1 \u00a0 \u6f14\u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u8a2d\u9663\u5217\u7684\u9577\u5ea6\u70ba \\(n\\) \uff0c\u6ce1\u6cab\u6392\u5e8f\u7684\u6b65\u9a5f\u5982\u5716 11-5 \u6240\u793a\u3002</p> <ol> <li>\u9996\u5148\uff0c\u5c0d \\(n\\) \u500b\u5143\u7d20\u57f7\u884c\u201c\u5192\u6ce1\u201d\uff0c\u5c07\u9663\u5217\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u6b63\u78ba\u4f4d\u7f6e\u3002</li> <li>\u63a5\u4e0b\u4f86\uff0c\u5c0d\u5269\u9918 \\(n - 1\\) \u500b\u5143\u7d20\u57f7\u884c\u201c\u5192\u6ce1\u201d\uff0c\u5c07\u7b2c\u4e8c\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u6b63\u78ba\u4f4d\u7f6e\u3002</li> <li>\u4ee5\u6b64\u985e\u63a8\uff0c\u7d93\u904e \\(n - 1\\) \u8f2a\u201c\u5192\u6ce1\u201d\u5f8c\uff0c\u524d \\(n - 1\\) \u5927\u7684\u5143\u7d20\u90fd\u88ab\u4ea4\u63db\u81f3\u6b63\u78ba\u4f4d\u7f6e\u3002</li> <li>\u50c5\u5269\u7684\u4e00\u500b\u5143\u7d20\u5fc5\u5b9a\u662f\u6700\u5c0f\u5143\u7d20\uff0c\u7121\u9808\u6392\u5e8f\uff0c\u56e0\u6b64\u9663\u5217\u6392\u5e8f\u5b8c\u6210\u3002</li> </ol> <p></p> <p> \u5716 11-5 \u00a0 \u6ce1\u6cab\u6392\u5e8f\u6d41\u7a0b </p> <p>\u793a\u4f8b\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig bubble_sort.py<pre><code>def bubble_sort(nums: list[int]):\n \"\"\"\u6ce1\u6cab\u6392\u5e8f\"\"\"\n n = len(nums)\n # \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for i in range(n - 1, 0, -1):\n # \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for j in range(i):\n if nums[j] &gt; nums[j + 1]:\n # \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n nums[j], nums[j + 1] = nums[j + 1], nums[j]\n</code></pre> bubble_sort.cpp<pre><code>/* \u6ce1\u6cab\u6392\u5e8f */\nvoid bubbleSort(vector&lt;int&gt; &amp;nums) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (int i = nums.size() - 1; i &gt; 0; i--) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (int j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n // \u9019\u88e1\u4f7f\u7528\u4e86 std::swap() \u51fd\u5f0f\n swap(nums[j], nums[j + 1]);\n }\n }\n }\n}\n</code></pre> bubble_sort.java<pre><code>/* \u6ce1\u6cab\u6392\u5e8f */\nvoid bubbleSort(int[] nums) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (int i = nums.length - 1; i &gt; 0; i--) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (int j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n</code></pre> bubble_sort.cs<pre><code>/* \u6ce1\u6cab\u6392\u5e8f */\nvoid BubbleSort(int[] nums) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (int i = nums.Length - 1; i &gt; 0; i--) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (int j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n }\n }\n }\n}\n</code></pre> bubble_sort.go<pre><code>/* \u6ce1\u6cab\u6392\u5e8f */\nfunc bubbleSort(nums []int) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for i := len(nums) - 1; i &gt; 0; i-- {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for j := 0; j &lt; i; j++ {\n if nums[j] &gt; nums[j+1] {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n nums[j], nums[j+1] = nums[j+1], nums[j]\n }\n }\n }\n}\n</code></pre> bubble_sort.swift<pre><code>/* \u6ce1\u6cab\u6392\u5e8f */\nfunc bubbleSort(nums: inout [Int]) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for i in nums.indices.dropFirst().reversed() {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for j in 0 ..&lt; i {\n if nums[j] &gt; nums[j + 1] {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n nums.swapAt(j, j + 1)\n }\n }\n }\n}\n</code></pre> bubble_sort.js<pre><code>/* \u6ce1\u6cab\u6392\u5e8f */\nfunction bubbleSort(nums) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (let i = nums.length - 1; i &gt; 0; i--) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (let j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n</code></pre> bubble_sort.ts<pre><code>/* \u6ce1\u6cab\u6392\u5e8f */\nfunction bubbleSort(nums: number[]): void {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (let i = nums.length - 1; i &gt; 0; i--) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (let j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n</code></pre> bubble_sort.dart<pre><code>/* \u6ce1\u6cab\u6392\u5e8f */\nvoid bubbleSort(List&lt;int&gt; nums) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (int i = nums.length - 1; i &gt; 0; i--) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (int j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n</code></pre> bubble_sort.rs<pre><code>/* \u6ce1\u6cab\u6392\u5e8f */\nfn bubble_sort(nums: &amp;mut [i32]) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for i in (1..nums.len()).rev() {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for j in 0..i {\n if nums[j] &gt; nums[j + 1] {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n</code></pre> bubble_sort.c<pre><code>/* \u6ce1\u6cab\u6392\u5e8f */\nvoid bubbleSort(int nums[], int size) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (int i = size - 1; i &gt; 0; i--) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (int j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n int temp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = temp;\n }\n }\n }\n}\n</code></pre> bubble_sort.kt<pre><code>/* \u6ce1\u6cab\u6392\u5e8f */\nfun bubbleSort(nums: IntArray) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (i in nums.size - 1 downTo 1) {\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (j in 0..&lt;i) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n val temp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = temp\n }\n }\n }\n}\n</code></pre> bubble_sort.rb<pre><code>[class]{}-[func]{bubble_sort}\n</code></pre> bubble_sort.zig<pre><code>// \u6ce1\u6cab\u6392\u5e8f\nfn bubbleSort(nums: []i32) void {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n var i: usize = nums.len - 1;\n while (i &gt; 0) : (i -= 1) {\n var j: usize = 0;\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n while (j &lt; i) : (j += 1) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n var tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n }\n }\n }\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_sorting/bubble_sort/#1132","title":"11.3.2 \u00a0 \u6548\u7387\u6700\u4f73\u5316","text":"<p>\u6211\u5011\u767c\u73fe\uff0c\u5982\u679c\u67d0\u8f2a\u201c\u5192\u6ce1\u201d\u4e2d\u6c92\u6709\u57f7\u884c\u4efb\u4f55\u4ea4\u63db\u64cd\u4f5c\uff0c\u8aaa\u660e\u9663\u5217\u5df2\u7d93\u5b8c\u6210\u6392\u5e8f\uff0c\u53ef\u76f4\u63a5\u8fd4\u56de\u7d50\u679c\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u589e\u52a0\u4e00\u500b\u6a19\u8a8c\u4f4d <code>flag</code> \u4f86\u76e3\u6e2c\u9019\u7a2e\u60c5\u6cc1\uff0c\u4e00\u65e6\u51fa\u73fe\u5c31\u7acb\u5373\u8fd4\u56de\u3002</p> <p>\u7d93\u904e\u6700\u4f73\u5316\uff0c\u6ce1\u6cab\u6392\u5e8f\u7684\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6\u548c\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u4ecd\u70ba \\(O(n^2)\\) \uff1b\u4f46\u7576\u8f38\u5165\u9663\u5217\u5b8c\u5168\u6709\u5e8f\u6642\uff0c\u53ef\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 \\(O(n)\\) \u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig bubble_sort.py<pre><code>def bubble_sort_with_flag(nums: list[int]):\n \"\"\"\u6ce1\u6cab\u6392\u5e8f\uff08\u6a19\u8a8c\u6700\u4f73\u5316\uff09\"\"\"\n n = len(nums)\n # \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for i in range(n - 1, 0, -1):\n flag = False # \u521d\u59cb\u5316\u6a19\u8a8c\u4f4d\n # \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for j in range(i):\n if nums[j] &gt; nums[j + 1]:\n # \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n nums[j], nums[j + 1] = nums[j + 1], nums[j]\n flag = True # \u8a18\u9304\u4ea4\u63db\u5143\u7d20\n if not flag:\n break # \u6b64\u8f2a\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u63db\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n</code></pre> bubble_sort.cpp<pre><code>/* \u6ce1\u6cab\u6392\u5e8f\uff08\u6a19\u8a8c\u6700\u4f73\u5316\uff09*/\nvoid bubbleSortWithFlag(vector&lt;int&gt; &amp;nums) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (int i = nums.size() - 1; i &gt; 0; i--) {\n bool flag = false; // \u521d\u59cb\u5316\u6a19\u8a8c\u4f4d\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (int j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n // \u9019\u88e1\u4f7f\u7528\u4e86 std::swap() \u51fd\u5f0f\n swap(nums[j], nums[j + 1]);\n flag = true; // \u8a18\u9304\u4ea4\u63db\u5143\u7d20\n }\n }\n if (!flag)\n break; // \u6b64\u8f2a\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u63db\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n</code></pre> bubble_sort.java<pre><code>/* \u6ce1\u6cab\u6392\u5e8f\uff08\u6a19\u8a8c\u6700\u4f73\u5316\uff09 */\nvoid bubbleSortWithFlag(int[] nums) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (int i = nums.length - 1; i &gt; 0; i--) {\n boolean flag = false; // \u521d\u59cb\u5316\u6a19\u8a8c\u4f4d\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (int j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8a18\u9304\u4ea4\u63db\u5143\u7d20\n }\n }\n if (!flag)\n break; // \u6b64\u8f2a\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u63db\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n</code></pre> bubble_sort.cs<pre><code>/* \u6ce1\u6cab\u6392\u5e8f\uff08\u6a19\u8a8c\u6700\u4f73\u5316\uff09*/\nvoid BubbleSortWithFlag(int[] nums) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (int i = nums.Length - 1; i &gt; 0; i--) {\n bool flag = false; // \u521d\u59cb\u5316\u6a19\u8a8c\u4f4d\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (int j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n flag = true; // \u8a18\u9304\u4ea4\u63db\u5143\u7d20\n }\n }\n if (!flag) break; // \u6b64\u8f2a\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u63db\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n</code></pre> bubble_sort.go<pre><code>/* \u6ce1\u6cab\u6392\u5e8f\uff08\u6a19\u8a8c\u6700\u4f73\u5316\uff09*/\nfunc bubbleSortWithFlag(nums []int) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for i := len(nums) - 1; i &gt; 0; i-- {\n flag := false // \u521d\u59cb\u5316\u6a19\u8a8c\u4f4d\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for j := 0; j &lt; i; j++ {\n if nums[j] &gt; nums[j+1] {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n nums[j], nums[j+1] = nums[j+1], nums[j]\n flag = true // \u8a18\u9304\u4ea4\u63db\u5143\u7d20\n }\n }\n if flag == false { // \u6b64\u8f2a\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u63db\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n break\n }\n }\n}\n</code></pre> bubble_sort.swift<pre><code>/* \u6ce1\u6cab\u6392\u5e8f\uff08\u6a19\u8a8c\u6700\u4f73\u5316\uff09*/\nfunc bubbleSortWithFlag(nums: inout [Int]) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for i in nums.indices.dropFirst().reversed() {\n var flag = false // \u521d\u59cb\u5316\u6a19\u8a8c\u4f4d\n for j in 0 ..&lt; i {\n if nums[j] &gt; nums[j + 1] {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n nums.swapAt(j, j + 1)\n flag = true // \u8a18\u9304\u4ea4\u63db\u5143\u7d20\n }\n }\n if !flag { // \u6b64\u8f2a\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u63db\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n break\n }\n }\n}\n</code></pre> bubble_sort.js<pre><code>/* \u6ce1\u6cab\u6392\u5e8f\uff08\u6a19\u8a8c\u6700\u4f73\u5316\uff09*/\nfunction bubbleSortWithFlag(nums) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (let i = nums.length - 1; i &gt; 0; i--) {\n let flag = false; // \u521d\u59cb\u5316\u6a19\u8a8c\u4f4d\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (let j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8a18\u9304\u4ea4\u63db\u5143\u7d20\n }\n }\n if (!flag) break; // \u6b64\u8f2a\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u63db\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n</code></pre> bubble_sort.ts<pre><code>/* \u6ce1\u6cab\u6392\u5e8f\uff08\u6a19\u8a8c\u6700\u4f73\u5316\uff09*/\nfunction bubbleSortWithFlag(nums: number[]): void {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (let i = nums.length - 1; i &gt; 0; i--) {\n let flag = false; // \u521d\u59cb\u5316\u6a19\u8a8c\u4f4d\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (let j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8a18\u9304\u4ea4\u63db\u5143\u7d20\n }\n }\n if (!flag) break; // \u6b64\u8f2a\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u63db\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n</code></pre> bubble_sort.dart<pre><code>/* \u6ce1\u6cab\u6392\u5e8f\uff08\u6a19\u8a8c\u6700\u4f73\u5316\uff09*/\nvoid bubbleSortWithFlag(List&lt;int&gt; nums) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (int i = nums.length - 1; i &gt; 0; i--) {\n bool flag = false; // \u521d\u59cb\u5316\u6a19\u8a8c\u4f4d\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (int j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8a18\u9304\u4ea4\u63db\u5143\u7d20\n }\n }\n if (!flag) break; // \u6b64\u8f2a\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u63db\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n</code></pre> bubble_sort.rs<pre><code>/* \u6ce1\u6cab\u6392\u5e8f\uff08\u6a19\u8a8c\u6700\u4f73\u5316\uff09 */\nfn bubble_sort_with_flag(nums: &amp;mut [i32]) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for i in (1..nums.len()).rev() {\n let mut flag = false; // \u521d\u59cb\u5316\u6a19\u8a8c\u4f4d\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for j in 0..i {\n if nums[j] &gt; nums[j + 1] {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true; // \u8a18\u9304\u4ea4\u63db\u5143\u7d20\n }\n }\n if !flag {\n break; // \u6b64\u8f2a\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u63db\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n };\n }\n}\n</code></pre> bubble_sort.c<pre><code>/* \u6ce1\u6cab\u6392\u5e8f\uff08\u6a19\u8a8c\u6700\u4f73\u5316\uff09*/\nvoid bubbleSortWithFlag(int nums[], int size) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (int i = size - 1; i &gt; 0; i--) {\n bool flag = false;\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (int j = 0; j &lt; i; j++) {\n if (nums[j] &gt; nums[j + 1]) {\n int temp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = temp;\n flag = true;\n }\n }\n if (!flag)\n break;\n }\n}\n</code></pre> bubble_sort.kt<pre><code>/* \u6ce1\u6cab\u6392\u5e8f\uff08\u6a19\u8a8c\u6700\u4f73\u5316\uff09 */\nfun bubbleSortWithFlag(nums: IntArray) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n for (i in nums.size - 1 downTo 1) {\n var flag = false // \u521d\u59cb\u5316\u6a19\u8a8c\u4f4d\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n for (j in 0..&lt;i) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n val temp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = temp\n flag = true // \u8a18\u9304\u4ea4\u63db\u5143\u7d20\n }\n }\n if (!flag) break // \u6b64\u8f2a\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u63db\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n</code></pre> bubble_sort.rb<pre><code>[class]{}-[func]{bubble_sort_with_flag}\n</code></pre> bubble_sort.zig<pre><code>// \u6ce1\u6cab\u6392\u5e8f\uff08\u6a19\u8a8c\u6700\u4f73\u5316\uff09\nfn bubbleSortWithFlag(nums: []i32) void {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [0, i]\n var i: usize = nums.len - 1;\n while (i &gt; 0) : (i -= 1) {\n var flag = false; // \u521d\u59cb\u5316\u6a19\u8a8c\u4f4d\n var j: usize = 0;\n // \u5167\u8ff4\u5708\uff1a\u5c07\u672a\u6392\u5e8f\u5340\u9593 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u63db\u81f3\u8a72\u5340\u9593\u7684\u6700\u53f3\u7aef\n while (j &lt; i) : (j += 1) {\n if (nums[j] &gt; nums[j + 1]) {\n // \u4ea4\u63db nums[j] \u8207 nums[j + 1]\n var tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n flag = true;\n }\n }\n if (!flag) break; // \u6b64\u8f2a\u201c\u5192\u6ce1\u201d\u672a\u4ea4\u63db\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n }\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_sorting/bubble_sort/#1133","title":"11.3.3 \u00a0 \u6f14\u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\)\u3001\u81ea\u9069\u61c9\u6392\u5e8f\uff1a\u5404\u8f2a\u201c\u5192\u6ce1\u201d\u8d70\u8a2a\u7684\u9663\u5217\u9577\u5ea6\u4f9d\u6b21\u70ba \\(n - 1\\)\u3001\\(n - 2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u7e3d\u548c\u70ba \\((n - 1) n / 2\\) \u3002\u5728\u5f15\u5165 <code>flag</code> \u6700\u4f73\u5316\u5f8c\uff0c\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6\u53ef\u9054\u5230 \\(O(n)\\) \u3002</li> <li>\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(1)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u6a19 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6578\u5927\u5c0f\u7684\u984d\u5916\u7a7a\u9593\u3002</li> <li>\u7a69\u5b9a\u6392\u5e8f\uff1a\u7531\u65bc\u5728\u201c\u5192\u6ce1\u201d\u4e2d\u9047\u5230\u76f8\u7b49\u5143\u7d20\u4e0d\u4ea4\u63db\u3002</li> </ul>"},{"location":"chapter_sorting/bucket_sort/","title":"11.8 \u00a0 \u6876\u6392\u5e8f","text":"<p>\u524d\u8ff0\u5e7e\u7a2e\u6392\u5e8f\u6f14\u7b97\u6cd5\u90fd\u5c6c\u65bc\u201c\u57fa\u65bc\u6bd4\u8f03\u7684\u6392\u5e8f\u6f14\u7b97\u6cd5\u201d\uff0c\u5b83\u5011\u900f\u904e\u6bd4\u8f03\u5143\u7d20\u9593\u7684\u5927\u5c0f\u4f86\u5be6\u73fe\u6392\u5e8f\u3002\u6b64\u985e\u6392\u5e8f\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u7121\u6cd5\u8d85\u8d8a \\(O(n \\log n)\\) \u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u5e7e\u7a2e\u201c\u975e\u6bd4\u8f03\u6392\u5e8f\u6f14\u7b97\u6cd5\u201d\uff0c\u5b83\u5011\u7684\u6642\u9593\u8907\u96dc\u5ea6\u53ef\u4ee5\u9054\u5230\u7dda\u6027\u968e\u3002</p> <p>\u6876\u6392\u5e8f\uff08bucket sort\uff09\u662f\u5206\u6cbb\u7b56\u7565\u7684\u4e00\u500b\u5178\u578b\u61c9\u7528\u3002\u5b83\u900f\u904e\u8a2d\u5b9a\u4e00\u4e9b\u5177\u6709\u5927\u5c0f\u9806\u5e8f\u7684\u6876\uff0c\u6bcf\u500b\u6876\u5c0d\u61c9\u4e00\u500b\u6578\u64da\u7bc4\u570d\uff0c\u5c07\u8cc7\u6599\u5e73\u5747\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\uff1b\u7136\u5f8c\uff0c\u5728\u6bcf\u500b\u6876\u5167\u90e8\u5206\u5225\u57f7\u884c\u6392\u5e8f\uff1b\u6700\u7d42\u6309\u7167\u6876\u7684\u9806\u5e8f\u5c07\u6240\u6709\u8cc7\u6599\u5408\u4f75\u3002</p>"},{"location":"chapter_sorting/bucket_sort/#1181","title":"11.8.1 \u00a0 \u6f14\u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u8003\u616e\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u9663\u5217\uff0c\u5176\u5143\u7d20\u662f\u7bc4\u570d \\([0, 1)\\) \u5167\u7684\u6d6e\u9ede\u6578\u3002\u6876\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u5716 11-13 \u6240\u793a\u3002</p> <ol> <li>\u521d\u59cb\u5316 \\(k\\) \u500b\u6876\uff0c\u5c07 \\(n\\) \u500b\u5143\u7d20\u5206\u914d\u5230 \\(k\\) \u500b\u6876\u4e2d\u3002</li> <li>\u5c0d\u6bcf\u500b\u6876\u5206\u5225\u57f7\u884c\u6392\u5e8f\uff08\u9019\u88e1\u63a1\u7528\u7a0b\u5f0f\u8a9e\u8a00\u7684\u5167\u5efa\u6392\u5e8f\u51fd\u5f0f\uff09\u3002</li> <li>\u6309\u7167\u6876\u5f9e\u5c0f\u5230\u5927\u7684\u9806\u5e8f\u5408\u4f75\u7d50\u679c\u3002</li> </ol> <p></p> <p> \u5716 11-13 \u00a0 \u6876\u6392\u5e8f\u6f14\u7b97\u6cd5\u6d41\u7a0b </p> <p>\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig bucket_sort.py<pre><code>def bucket_sort(nums: list[float]):\n \"\"\"\u6876\u6392\u5e8f\"\"\"\n # \u521d\u59cb\u5316 k = n/2 \u500b\u6876\uff0c\u9810\u671f\u5411\u6bcf\u500b\u6876\u5206\u914d 2 \u500b\u5143\u7d20\n k = len(nums) // 2\n buckets = [[] for _ in range(k)]\n # 1. \u5c07\u9663\u5217\u5143\u7d20\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\n for num in nums:\n # \u8f38\u5165\u8cc7\u6599\u7bc4\u570d\u70ba [0, 1)\uff0c\u4f7f\u7528 num * k \u5c0d\u6620\u5230\u7d22\u5f15\u7bc4\u570d [0, k-1]\n i = int(num * k)\n # \u5c07 num \u65b0\u589e\u9032\u6876 i\n buckets[i].append(num)\n # 2. \u5c0d\u5404\u500b\u6876\u57f7\u884c\u6392\u5e8f\n for bucket in buckets:\n # \u4f7f\u7528\u5167\u5efa\u6392\u5e8f\u51fd\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u66ff\u63db\u6210\u5176\u4ed6\u6392\u5e8f\u6f14\u7b97\u6cd5\n bucket.sort()\n # 3. \u8d70\u8a2a\u6876\u5408\u4f75\u7d50\u679c\n i = 0\n for bucket in buckets:\n for num in bucket:\n nums[i] = num\n i += 1\n</code></pre> bucket_sort.cpp<pre><code>/* \u6876\u6392\u5e8f */\nvoid bucketSort(vector&lt;float&gt; &amp;nums) {\n // \u521d\u59cb\u5316 k = n/2 \u500b\u6876\uff0c\u9810\u671f\u5411\u6bcf\u500b\u6876\u5206\u914d 2 \u500b\u5143\u7d20\n int k = nums.size() / 2;\n vector&lt;vector&lt;float&gt;&gt; buckets(k);\n // 1. \u5c07\u9663\u5217\u5143\u7d20\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\n for (float num : nums) {\n // \u8f38\u5165\u8cc7\u6599\u7bc4\u570d\u70ba [0, 1)\uff0c\u4f7f\u7528 num * k \u5c0d\u6620\u5230\u7d22\u5f15\u7bc4\u570d [0, k-1]\n int i = num * k;\n // \u5c07 num \u65b0\u589e\u9032\u6876 bucket_idx\n buckets[i].push_back(num);\n }\n // 2. \u5c0d\u5404\u500b\u6876\u57f7\u884c\u6392\u5e8f\n for (vector&lt;float&gt; &amp;bucket : buckets) {\n // \u4f7f\u7528\u5167\u5efa\u6392\u5e8f\u51fd\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u66ff\u63db\u6210\u5176\u4ed6\u6392\u5e8f\u6f14\u7b97\u6cd5\n sort(bucket.begin(), bucket.end());\n }\n // 3. \u8d70\u8a2a\u6876\u5408\u4f75\u7d50\u679c\n int i = 0;\n for (vector&lt;float&gt; &amp;bucket : buckets) {\n for (float num : bucket) {\n nums[i++] = num;\n }\n }\n}\n</code></pre> bucket_sort.java<pre><code>/* \u6876\u6392\u5e8f */\nvoid bucketSort(float[] nums) {\n // \u521d\u59cb\u5316 k = n/2 \u500b\u6876\uff0c\u9810\u671f\u5411\u6bcf\u500b\u6876\u5206\u914d 2 \u500b\u5143\u7d20\n int k = nums.length / 2;\n List&lt;List&lt;Float&gt;&gt; buckets = new ArrayList&lt;&gt;();\n for (int i = 0; i &lt; k; i++) {\n buckets.add(new ArrayList&lt;&gt;());\n }\n // 1. \u5c07\u9663\u5217\u5143\u7d20\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\n for (float num : nums) {\n // \u8f38\u5165\u8cc7\u6599\u7bc4\u570d\u70ba [0, 1)\uff0c\u4f7f\u7528 num * k \u5c0d\u6620\u5230\u7d22\u5f15\u7bc4\u570d [0, k-1]\n int i = (int) (num * k);\n // \u5c07 num \u65b0\u589e\u9032\u6876 i\n buckets.get(i).add(num);\n }\n // 2. \u5c0d\u5404\u500b\u6876\u57f7\u884c\u6392\u5e8f\n for (List&lt;Float&gt; bucket : buckets) {\n // \u4f7f\u7528\u5167\u5efa\u6392\u5e8f\u51fd\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u66ff\u63db\u6210\u5176\u4ed6\u6392\u5e8f\u6f14\u7b97\u6cd5\n Collections.sort(bucket);\n }\n // 3. \u8d70\u8a2a\u6876\u5408\u4f75\u7d50\u679c\n int i = 0;\n for (List&lt;Float&gt; bucket : buckets) {\n for (float num : bucket) {\n nums[i++] = num;\n }\n }\n}\n</code></pre> bucket_sort.cs<pre><code>/* \u6876\u6392\u5e8f */\nvoid BucketSort(float[] nums) {\n // \u521d\u59cb\u5316 k = n/2 \u500b\u6876\uff0c\u9810\u671f\u5411\u6bcf\u500b\u6876\u5206\u914d 2 \u500b\u5143\u7d20\n int k = nums.Length / 2;\n List&lt;List&lt;float&gt;&gt; buckets = [];\n for (int i = 0; i &lt; k; i++) {\n buckets.Add([]);\n }\n // 1. \u5c07\u9663\u5217\u5143\u7d20\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\n foreach (float num in nums) {\n // \u8f38\u5165\u8cc7\u6599\u7bc4\u570d\u70ba [0, 1)\uff0c\u4f7f\u7528 num * k \u5c0d\u6620\u5230\u7d22\u5f15\u7bc4\u570d [0, k-1]\n int i = (int)(num * k);\n // \u5c07 num \u65b0\u589e\u9032\u6876 i\n buckets[i].Add(num);\n }\n // 2. \u5c0d\u5404\u500b\u6876\u57f7\u884c\u6392\u5e8f\n foreach (List&lt;float&gt; bucket in buckets) {\n // \u4f7f\u7528\u5167\u5efa\u6392\u5e8f\u51fd\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u66ff\u63db\u6210\u5176\u4ed6\u6392\u5e8f\u6f14\u7b97\u6cd5\n bucket.Sort();\n }\n // 3. \u8d70\u8a2a\u6876\u5408\u4f75\u7d50\u679c\n int j = 0;\n foreach (List&lt;float&gt; bucket in buckets) {\n foreach (float num in bucket) {\n nums[j++] = num;\n }\n }\n}\n</code></pre> bucket_sort.go<pre><code>/* \u6876\u6392\u5e8f */\nfunc bucketSort(nums []float64) {\n // \u521d\u59cb\u5316 k = n/2 \u500b\u6876\uff0c\u9810\u671f\u5411\u6bcf\u500b\u6876\u5206\u914d 2 \u500b\u5143\u7d20\n k := len(nums) / 2\n buckets := make([][]float64, k)\n for i := 0; i &lt; k; i++ {\n buckets[i] = make([]float64, 0)\n }\n // 1. \u5c07\u9663\u5217\u5143\u7d20\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\n for _, num := range nums {\n // \u8f38\u5165\u8cc7\u6599\u7bc4\u570d\u70ba [0, 1)\uff0c\u4f7f\u7528 num * k \u5c0d\u6620\u5230\u7d22\u5f15\u7bc4\u570d [0, k-1]\n i := int(num * float64(k))\n // \u5c07 num \u65b0\u589e\u9032\u6876 i\n buckets[i] = append(buckets[i], num)\n }\n // 2. \u5c0d\u5404\u500b\u6876\u57f7\u884c\u6392\u5e8f\n for i := 0; i &lt; k; i++ {\n // \u4f7f\u7528\u5167\u5efa\u5207\u7247\u6392\u5e8f\u51fd\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u66ff\u63db\u6210\u5176\u4ed6\u6392\u5e8f\u6f14\u7b97\u6cd5\n sort.Float64s(buckets[i])\n }\n // 3. \u8d70\u8a2a\u6876\u5408\u4f75\u7d50\u679c\n i := 0\n for _, bucket := range buckets {\n for _, num := range bucket {\n nums[i] = num\n i++\n }\n }\n}\n</code></pre> bucket_sort.swift<pre><code>/* \u6876\u6392\u5e8f */\nfunc bucketSort(nums: inout [Double]) {\n // \u521d\u59cb\u5316 k = n/2 \u500b\u6876\uff0c\u9810\u671f\u5411\u6bcf\u500b\u6876\u5206\u914d 2 \u500b\u5143\u7d20\n let k = nums.count / 2\n var buckets = (0 ..&lt; k).map { _ in [Double]() }\n // 1. \u5c07\u9663\u5217\u5143\u7d20\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\n for num in nums {\n // \u8f38\u5165\u8cc7\u6599\u7bc4\u570d\u70ba [0, 1)\uff0c\u4f7f\u7528 num * k \u5c0d\u6620\u5230\u7d22\u5f15\u7bc4\u570d [0, k-1]\n let i = Int(num * Double(k))\n // \u5c07 num \u65b0\u589e\u9032\u6876 i\n buckets[i].append(num)\n }\n // 2. \u5c0d\u5404\u500b\u6876\u57f7\u884c\u6392\u5e8f\n for i in buckets.indices {\n // \u4f7f\u7528\u5167\u5efa\u6392\u5e8f\u51fd\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u66ff\u63db\u6210\u5176\u4ed6\u6392\u5e8f\u6f14\u7b97\u6cd5\n buckets[i].sort()\n }\n // 3. \u8d70\u8a2a\u6876\u5408\u4f75\u7d50\u679c\n var i = nums.startIndex\n for bucket in buckets {\n for num in bucket {\n nums[i] = num\n i += 1\n }\n }\n}\n</code></pre> bucket_sort.js<pre><code>/* \u6876\u6392\u5e8f */\nfunction bucketSort(nums) {\n // \u521d\u59cb\u5316 k = n/2 \u500b\u6876\uff0c\u9810\u671f\u5411\u6bcf\u500b\u6876\u5206\u914d 2 \u500b\u5143\u7d20\n const k = nums.length / 2;\n const buckets = [];\n for (let i = 0; i &lt; k; i++) {\n buckets.push([]);\n }\n // 1. \u5c07\u9663\u5217\u5143\u7d20\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\n for (const num of nums) {\n // \u8f38\u5165\u8cc7\u6599\u7bc4\u570d\u70ba [0, 1)\uff0c\u4f7f\u7528 num * k \u5c0d\u6620\u5230\u7d22\u5f15\u7bc4\u570d [0, k-1]\n const i = Math.floor(num * k);\n // \u5c07 num \u65b0\u589e\u9032\u6876 i\n buckets[i].push(num);\n }\n // 2. \u5c0d\u5404\u500b\u6876\u57f7\u884c\u6392\u5e8f\n for (const bucket of buckets) {\n // \u4f7f\u7528\u5167\u5efa\u6392\u5e8f\u51fd\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u66ff\u63db\u6210\u5176\u4ed6\u6392\u5e8f\u6f14\u7b97\u6cd5\n bucket.sort((a, b) =&gt; a - b);\n }\n // 3. \u8d70\u8a2a\u6876\u5408\u4f75\u7d50\u679c\n let i = 0;\n for (const bucket of buckets) {\n for (const num of bucket) {\n nums[i++] = num;\n }\n }\n}\n</code></pre> bucket_sort.ts<pre><code>/* \u6876\u6392\u5e8f */\nfunction bucketSort(nums: number[]): void {\n // \u521d\u59cb\u5316 k = n/2 \u500b\u6876\uff0c\u9810\u671f\u5411\u6bcf\u500b\u6876\u5206\u914d 2 \u500b\u5143\u7d20\n const k = nums.length / 2;\n const buckets: number[][] = [];\n for (let i = 0; i &lt; k; i++) {\n buckets.push([]);\n }\n // 1. \u5c07\u9663\u5217\u5143\u7d20\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\n for (const num of nums) {\n // \u8f38\u5165\u8cc7\u6599\u7bc4\u570d\u70ba [0, 1)\uff0c\u4f7f\u7528 num * k \u5c0d\u6620\u5230\u7d22\u5f15\u7bc4\u570d [0, k-1]\n const i = Math.floor(num * k);\n // \u5c07 num \u65b0\u589e\u9032\u6876 i\n buckets[i].push(num);\n }\n // 2. \u5c0d\u5404\u500b\u6876\u57f7\u884c\u6392\u5e8f\n for (const bucket of buckets) {\n // \u4f7f\u7528\u5167\u5efa\u6392\u5e8f\u51fd\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u66ff\u63db\u6210\u5176\u4ed6\u6392\u5e8f\u6f14\u7b97\u6cd5\n bucket.sort((a, b) =&gt; a - b);\n }\n // 3. \u8d70\u8a2a\u6876\u5408\u4f75\u7d50\u679c\n let i = 0;\n for (const bucket of buckets) {\n for (const num of bucket) {\n nums[i++] = num;\n }\n }\n}\n</code></pre> bucket_sort.dart<pre><code>/* \u6876\u6392\u5e8f */\nvoid bucketSort(List&lt;double&gt; nums) {\n // \u521d\u59cb\u5316 k = n/2 \u500b\u6876\uff0c\u9810\u671f\u5411\u6bcf\u500b\u6876\u5206\u914d 2 \u500b\u5143\u7d20\n int k = nums.length ~/ 2;\n List&lt;List&lt;double&gt;&gt; buckets = List.generate(k, (index) =&gt; []);\n\n // 1. \u5c07\u9663\u5217\u5143\u7d20\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\n for (double _num in nums) {\n // \u8f38\u5165\u8cc7\u6599\u7bc4\u570d\u70ba [0, 1)\uff0c\u4f7f\u7528 _num * k \u5c0d\u6620\u5230\u7d22\u5f15\u7bc4\u570d [0, k-1]\n int i = (_num * k).toInt();\n // \u5c07 _num \u65b0\u589e\u9032\u6876 bucket_idx\n buckets[i].add(_num);\n }\n // 2. \u5c0d\u5404\u500b\u6876\u57f7\u884c\u6392\u5e8f\n for (List&lt;double&gt; bucket in buckets) {\n bucket.sort();\n }\n // 3. \u8d70\u8a2a\u6876\u5408\u4f75\u7d50\u679c\n int i = 0;\n for (List&lt;double&gt; bucket in buckets) {\n for (double _num in bucket) {\n nums[i++] = _num;\n }\n }\n}\n</code></pre> bucket_sort.rs<pre><code>/* \u6876\u6392\u5e8f */\nfn bucket_sort(nums: &amp;mut [f64]) {\n // \u521d\u59cb\u5316 k = n/2 \u500b\u6876\uff0c\u9810\u671f\u5411\u6bcf\u500b\u6876\u5206\u914d 2 \u500b\u5143\u7d20\n let k = nums.len() / 2;\n let mut buckets = vec![vec![]; k];\n // 1. \u5c07\u9663\u5217\u5143\u7d20\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\n for &amp;mut num in &amp;mut *nums {\n // \u8f38\u5165\u8cc7\u6599\u7bc4\u570d\u70ba [0, 1)\uff0c\u4f7f\u7528 num * k \u5c0d\u6620\u5230\u7d22\u5f15\u7bc4\u570d [0, k-1]\n let i = (num * k as f64) as usize;\n // \u5c07 num \u65b0\u589e\u9032\u6876 i\n buckets[i].push(num);\n }\n // 2. \u5c0d\u5404\u500b\u6876\u57f7\u884c\u6392\u5e8f\n for bucket in &amp;mut buckets {\n // \u4f7f\u7528\u5167\u5efa\u6392\u5e8f\u51fd\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u66ff\u63db\u6210\u5176\u4ed6\u6392\u5e8f\u6f14\u7b97\u6cd5\n bucket.sort_by(|a, b| a.partial_cmp(b).unwrap());\n }\n // 3. \u8d70\u8a2a\u6876\u5408\u4f75\u7d50\u679c\n let mut i = 0;\n for bucket in &amp;mut buckets {\n for &amp;mut num in bucket {\n nums[i] = num;\n i += 1;\n }\n }\n}\n</code></pre> bucket_sort.c<pre><code>/* \u6876\u6392\u5e8f */\nvoid bucketSort(float nums[], int n) {\n int k = n / 2; // \u521d\u59cb\u5316 k = n/2 \u500b\u6876\n int *sizes = malloc(k * sizeof(int)); // \u8a18\u9304\u6bcf\u500b\u6876\u7684\u5927\u5c0f\n float **buckets = malloc(k * sizeof(float *)); // \u52d5\u614b\u9663\u5217\u7684\u9663\u5217\uff08\u6876\uff09\n // \u70ba\u6bcf\u500b\u6876\u9810\u5206\u914d\u8db3\u5920\u7684\u7a7a\u9593\n for (int i = 0; i &lt; k; ++i) {\n buckets[i] = (float *)malloc(n * sizeof(float));\n sizes[i] = 0;\n }\n // 1. \u5c07\u9663\u5217\u5143\u7d20\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\n for (int i = 0; i &lt; n; ++i) {\n int idx = (int)(nums[i] * k);\n buckets[idx][sizes[idx]++] = nums[i];\n }\n // 2. \u5c0d\u5404\u500b\u6876\u57f7\u884c\u6392\u5e8f\n for (int i = 0; i &lt; k; ++i) {\n qsort(buckets[i], sizes[i], sizeof(float), compare);\n }\n // 3. \u5408\u4f75\u6392\u5e8f\u5f8c\u7684\u6876\n int idx = 0;\n for (int i = 0; i &lt; k; ++i) {\n for (int j = 0; j &lt; sizes[i]; ++j) {\n nums[idx++] = buckets[i][j];\n }\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(buckets[i]);\n }\n}\n</code></pre> bucket_sort.kt<pre><code>/* \u6876\u6392\u5e8f */\nfun bucketSort(nums: FloatArray) {\n // \u521d\u59cb\u5316 k = n/2 \u500b\u6876\uff0c\u9810\u671f\u5411\u6bcf\u500b\u6876\u5206\u914d 2 \u500b\u5143\u7d20\n val k = nums.size / 2\n val buckets = mutableListOf&lt;MutableList&lt;Float&gt;&gt;()\n for (i in 0..&lt;k) {\n buckets.add(mutableListOf())\n }\n // 1. \u5c07\u9663\u5217\u5143\u7d20\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\n for (num in nums) {\n // \u8f38\u5165\u8cc7\u6599\u7bc4\u570d\u70ba [0, 1)\uff0c\u4f7f\u7528 num * k \u5c0d\u6620\u5230\u7d22\u5f15\u7bc4\u570d [0, k-1]\n val i = (num * k).toInt()\n // \u5c07 num \u65b0\u589e\u9032\u6876 i\n buckets[i].add(num)\n }\n // 2. \u5c0d\u5404\u500b\u6876\u57f7\u884c\u6392\u5e8f\n for (bucket in buckets) {\n // \u4f7f\u7528\u5167\u5efa\u6392\u5e8f\u51fd\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u66ff\u63db\u6210\u5176\u4ed6\u6392\u5e8f\u6f14\u7b97\u6cd5\n bucket.sort()\n }\n // 3. \u8d70\u8a2a\u6876\u5408\u4f75\u7d50\u679c\n var i = 0\n for (bucket in buckets) {\n for (num in bucket) {\n nums[i++] = num\n }\n }\n}\n</code></pre> bucket_sort.rb<pre><code>[class]{}-[func]{bucket_sort}\n</code></pre> bucket_sort.zig<pre><code>[class]{}-[func]{bucketSort}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_sorting/bucket_sort/#1182","title":"11.8.2 \u00a0 \u6f14\u7b97\u6cd5\u7279\u6027","text":"<p>\u6876\u6392\u5e8f\u9069\u7528\u65bc\u8655\u7406\u9ad4\u91cf\u5f88\u5927\u7684\u8cc7\u6599\u3002\u4f8b\u5982\uff0c\u8f38\u5165\u8cc7\u6599\u5305\u542b 100 \u842c\u500b\u5143\u7d20\uff0c\u7531\u65bc\u7a7a\u9593\u9650\u5236\uff0c\u7cfb\u7d71\u8a18\u61b6\u9ad4\u7121\u6cd5\u4e00\u6b21\u6027\u8f09\u5165\u6240\u6709\u8cc7\u6599\u3002\u6b64\u6642\uff0c\u53ef\u4ee5\u5c07\u8cc7\u6599\u5206\u6210 1000 \u500b\u6876\uff0c\u7136\u5f8c\u5206\u5225\u5c0d\u6bcf\u500b\u6876\u9032\u884c\u6392\u5e8f\uff0c\u6700\u5f8c\u5c07\u7d50\u679c\u5408\u4f75\u3002</p> <ul> <li>\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n + k)\\) \uff1a\u5047\u8a2d\u5143\u7d20\u5728\u5404\u500b\u6876\u5167\u5e73\u5747\u5206\u4f48\uff0c\u90a3\u9ebc\u6bcf\u500b\u6876\u5167\u7684\u5143\u7d20\u6578\u91cf\u70ba \\(\\frac{n}{k}\\) \u3002\u5047\u8a2d\u6392\u5e8f\u55ae\u500b\u6876\u4f7f\u7528 \\(O(\\frac{n}{k} \\log\\frac{n}{k})\\) \u6642\u9593\uff0c\u5247\u6392\u5e8f\u6240\u6709\u6876\u4f7f\u7528 \\(O(n \\log\\frac{n}{k})\\) \u6642\u9593\u3002\u7576\u6876\u6578\u91cf \\(k\\) \u6bd4\u8f03\u5927\u6642\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u5247\u8da8\u5411\u65bc \\(O(n)\\) \u3002\u5408\u4f75\u7d50\u679c\u6642\u9700\u8981\u8d70\u8a2a\u6240\u6709\u6876\u548c\u5143\u7d20\uff0c\u82b1\u8cbb \\(O(n + k)\\) \u6642\u9593\u3002</li> <li>\u81ea\u9069\u61c9\u6392\u5e8f\uff1a\u5728\u6700\u5dee\u60c5\u6cc1\u4e0b\uff0c\u6240\u6709\u8cc7\u6599\u88ab\u5206\u914d\u5230\u4e00\u500b\u6876\u4e2d\uff0c\u4e14\u6392\u5e8f\u8a72\u6876\u4f7f\u7528 \\(O(n^2)\\) \u6642\u9593\u3002</li> <li>\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n + k)\\)\u3001\u975e\u539f\u5730\u6392\u5e8f\uff1a\u9700\u8981\u85c9\u52a9 \\(k\\) \u500b\u6876\u548c\u7e3d\u5171 \\(n\\) \u500b\u5143\u7d20\u7684\u984d\u5916\u7a7a\u9593\u3002</li> <li>\u6876\u6392\u5e8f\u662f\u5426\u7a69\u5b9a\u53d6\u6c7a\u65bc\u6392\u5e8f\u6876\u5167\u5143\u7d20\u7684\u6f14\u7b97\u6cd5\u662f\u5426\u7a69\u5b9a\u3002</li> </ul>"},{"location":"chapter_sorting/bucket_sort/#1183","title":"11.8.3 \u00a0 \u5982\u4f55\u5be6\u73fe\u5e73\u5747\u5206\u914d","text":"<p>\u6876\u6392\u5e8f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u7406\u8ad6\u4e0a\u53ef\u4ee5\u9054\u5230 \\(O(n)\\) \uff0c\u95dc\u9375\u5728\u65bc\u5c07\u5143\u7d20\u5747\u52fb\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\uff0c\u56e0\u70ba\u5be6\u969b\u8cc7\u6599\u5f80\u5f80\u4e0d\u662f\u5747\u52fb\u5206\u4f48\u7684\u3002\u4f8b\u5982\uff0c\u6211\u5011\u60f3\u8981\u5c07\u6dd8\u5bf6\u4e0a\u7684\u6240\u6709\u5546\u54c1\u6309\u50f9\u683c\u7bc4\u570d\u5e73\u5747\u5206\u914d\u5230 10 \u500b\u6876\u4e2d\uff0c\u4f46\u5546\u54c1\u50f9\u683c\u5206\u4f48\u4e0d\u5747\uff0c\u4f4e\u65bc 100 \u5143\u7684\u975e\u5e38\u591a\uff0c\u9ad8\u65bc 1000 \u5143\u7684\u975e\u5e38\u5c11\u3002\u82e5\u5c07\u50f9\u683c\u5340\u9593\u5e73\u5747\u5283\u5206\u70ba 10 \u500b\uff0c\u5404\u500b\u6876\u4e2d\u7684\u5546\u54c1\u6578\u91cf\u5dee\u8ddd\u6703\u975e\u5e38\u5927\u3002</p> <p>\u70ba\u5be6\u73fe\u5e73\u5747\u5206\u914d\uff0c\u6211\u5011\u53ef\u4ee5\u5148\u8a2d\u5b9a\u4e00\u689d\u5927\u81f4\u7684\u5206\u754c\u7dda\uff0c\u5c07\u8cc7\u6599\u7c97\u7565\u5730\u5206\u5230 3 \u500b\u6876\u4e2d\u3002\u5206\u914d\u5b8c\u7562\u5f8c\uff0c\u518d\u5c07\u5546\u54c1\u8f03\u591a\u7684\u6876\u7e7c\u7e8c\u5283\u5206\u70ba 3 \u500b\u6876\uff0c\u76f4\u81f3\u6240\u6709\u6876\u4e2d\u7684\u5143\u7d20\u6578\u91cf\u5927\u81f4\u76f8\u7b49\u3002</p> <p>\u5982\u5716 11-14 \u6240\u793a\uff0c\u9019\u7a2e\u65b9\u6cd5\u672c\u8cea\u4e0a\u662f\u5efa\u7acb\u4e00\u68f5\u905e\u8ff4\u6a39\uff0c\u76ee\u6a19\u662f\u8b93\u8449\u7bc0\u9ede\u7684\u503c\u5118\u53ef\u80fd\u5e73\u5747\u3002\u7576\u7136\uff0c\u4e0d\u4e00\u5b9a\u8981\u6bcf\u8f2a\u5c07\u8cc7\u6599\u5283\u5206\u70ba 3 \u500b\u6876\uff0c\u5177\u9ad4\u5283\u5206\u65b9\u5f0f\u53ef\u6839\u64da\u8cc7\u6599\u7279\u9ede\u9748\u6d3b\u9078\u64c7\u3002</p> <p></p> <p> \u5716 11-14 \u00a0 \u905e\u8ff4\u5283\u5206\u6876 </p> <p>\u5982\u679c\u6211\u5011\u63d0\u524d\u77e5\u9053\u5546\u54c1\u50f9\u683c\u7684\u6a5f\u7387\u5206\u4f48\uff0c\u5247\u53ef\u4ee5\u6839\u64da\u8cc7\u6599\u6a5f\u7387\u5206\u4f48\u8a2d\u5b9a\u6bcf\u500b\u6876\u7684\u50f9\u683c\u5206\u754c\u7dda\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8cc7\u6599\u5206\u4f48\u4e26\u4e0d\u4e00\u5b9a\u9700\u8981\u7279\u610f\u7d71\u8a08\uff0c\u4e5f\u53ef\u4ee5\u6839\u64da\u8cc7\u6599\u7279\u9ede\u63a1\u7528\u67d0\u7a2e\u6a5f\u7387\u6a21\u578b\u9032\u884c\u8fd1\u4f3c\u3002</p> <p>\u5982\u5716 11-15 \u6240\u793a\uff0c\u6211\u5011\u5047\u8a2d\u5546\u54c1\u50f9\u683c\u670d\u5f9e\u6b63\u614b\u5206\u4f48\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u5408\u7406\u5730\u8a2d\u5b9a\u50f9\u683c\u5340\u9593\uff0c\u5f9e\u800c\u5c07\u5546\u54c1\u5e73\u5747\u5206\u914d\u5230\u5404\u500b\u6876\u4e2d\u3002</p> <p></p> <p> \u5716 11-15 \u00a0 \u6839\u64da\u6a5f\u7387\u5206\u4f48\u5283\u5206\u6876 </p>"},{"location":"chapter_sorting/counting_sort/","title":"11.9 \u00a0 \u8a08\u6578\u6392\u5e8f","text":"<p>\u8a08\u6578\u6392\u5e8f\uff08counting sort\uff09\u900f\u904e\u7d71\u8a08\u5143\u7d20\u6578\u91cf\u4f86\u5be6\u73fe\u6392\u5e8f\uff0c\u901a\u5e38\u61c9\u7528\u65bc\u6574\u6578\u9663\u5217\u3002</p>"},{"location":"chapter_sorting/counting_sort/#1191","title":"11.9.1 \u00a0 \u7c21\u55ae\u5be6\u73fe","text":"<p>\u5148\u4f86\u770b\u4e00\u500b\u7c21\u55ae\u7684\u4f8b\u5b50\u3002\u7d66\u5b9a\u4e00\u500b\u9577\u5ea6\u70ba \\(n\\) \u7684\u9663\u5217 <code>nums</code> \uff0c\u5176\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u201c\u975e\u8ca0\u6574\u6578\u201d\uff0c\u8a08\u6578\u6392\u5e8f\u7684\u6574\u9ad4\u6d41\u7a0b\u5982\u5716 11-16 \u6240\u793a\u3002</p> <ol> <li>\u8d70\u8a2a\u9663\u5217\uff0c\u627e\u51fa\u5176\u4e2d\u7684\u6700\u5927\u6578\u5b57\uff0c\u8a18\u70ba \\(m\\) \uff0c\u7136\u5f8c\u5efa\u7acb\u4e00\u500b\u9577\u5ea6\u70ba \\(m + 1\\) \u7684\u8f14\u52a9\u9663\u5217 <code>counter</code> \u3002</li> <li>\u85c9\u52a9 <code>counter</code> \u7d71\u8a08 <code>nums</code> \u4e2d\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\uff0c\u5176\u4e2d <code>counter[num]</code> \u5c0d\u61c9\u6578\u5b57 <code>num</code> \u7684\u51fa\u73fe\u6b21\u6578\u3002\u7d71\u8a08\u65b9\u6cd5\u5f88\u7c21\u55ae\uff0c\u53ea\u9700\u8d70\u8a2a <code>nums</code>\uff08\u8a2d\u7576\u524d\u6578\u5b57\u70ba <code>num</code>\uff09\uff0c\u6bcf\u8f2a\u5c07 <code>counter[num]</code> \u589e\u52a0 \\(1\\) \u5373\u53ef\u3002</li> <li>\u7531\u65bc <code>counter</code> \u7684\u5404\u500b\u7d22\u5f15\u5929\u7136\u6709\u5e8f\uff0c\u56e0\u6b64\u76f8\u7576\u65bc\u6240\u6709\u6578\u5b57\u5df2\u7d93\u6392\u5e8f\u597d\u4e86\u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u8d70\u8a2a <code>counter</code> \uff0c\u6839\u64da\u5404\u6578\u5b57\u51fa\u73fe\u6b21\u6578\u5f9e\u5c0f\u5230\u5927\u7684\u9806\u5e8f\u586b\u5165 <code>nums</code> \u5373\u53ef\u3002</li> </ol> <p></p> <p> \u5716 11-16 \u00a0 \u8a08\u6578\u6392\u5e8f\u6d41\u7a0b </p> <p>\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig counting_sort.py<pre><code>def counting_sort_naive(nums: list[int]):\n \"\"\"\u8a08\u6578\u6392\u5e8f\"\"\"\n # \u7c21\u55ae\u5be6\u73fe\uff0c\u7121\u6cd5\u7528\u65bc\u6392\u5e8f\u7269\u4ef6\n # 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n m = 0\n for num in nums:\n m = max(m, num)\n # 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n # counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n counter = [0] * (m + 1)\n for num in nums:\n counter[num] += 1\n # 3. \u8d70\u8a2a counter \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u539f\u9663\u5217 nums\n i = 0\n for num in range(m + 1):\n for _ in range(counter[num]):\n nums[i] = num\n i += 1\n</code></pre> counting_sort.cpp<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u7c21\u55ae\u5be6\u73fe\uff0c\u7121\u6cd5\u7528\u65bc\u6392\u5e8f\u7269\u4ef6\nvoid countingSortNaive(vector&lt;int&gt; &amp;nums) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int num : nums) {\n m = max(m, num);\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n vector&lt;int&gt; counter(m + 1, 0);\n for (int num : nums) {\n counter[num]++;\n }\n // 3. \u8d70\u8a2a counter \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u539f\u9663\u5217 nums\n int i = 0;\n for (int num = 0; num &lt; m + 1; num++) {\n for (int j = 0; j &lt; counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n</code></pre> counting_sort.java<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u7c21\u55ae\u5be6\u73fe\uff0c\u7121\u6cd5\u7528\u65bc\u6392\u5e8f\u7269\u4ef6\nvoid countingSortNaive(int[] nums) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int num : nums) {\n m = Math.max(m, num);\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n int[] counter = new int[m + 1];\n for (int num : nums) {\n counter[num]++;\n }\n // 3. \u8d70\u8a2a counter \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u539f\u9663\u5217 nums\n int i = 0;\n for (int num = 0; num &lt; m + 1; num++) {\n for (int j = 0; j &lt; counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n</code></pre> counting_sort.cs<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u7c21\u55ae\u5be6\u73fe\uff0c\u7121\u6cd5\u7528\u65bc\u6392\u5e8f\u7269\u4ef6\nvoid CountingSortNaive(int[] nums) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n int m = 0;\n foreach (int num in nums) {\n m = Math.Max(m, num);\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n int[] counter = new int[m + 1];\n foreach (int num in nums) {\n counter[num]++;\n }\n // 3. \u8d70\u8a2a counter \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u539f\u9663\u5217 nums\n int i = 0;\n for (int num = 0; num &lt; m + 1; num++) {\n for (int j = 0; j &lt; counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n</code></pre> counting_sort.go<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u7c21\u55ae\u5be6\u73fe\uff0c\u7121\u6cd5\u7528\u65bc\u6392\u5e8f\u7269\u4ef6\nfunc countingSortNaive(nums []int) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n m := 0\n for _, num := range nums {\n if num &gt; m {\n m = num\n }\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n counter := make([]int, m+1)\n for _, num := range nums {\n counter[num]++\n }\n // 3. \u8d70\u8a2a counter \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u539f\u9663\u5217 nums\n for i, num := 0, 0; num &lt; m+1; num++ {\n for j := 0; j &lt; counter[num]; j++ {\n nums[i] = num\n i++\n }\n }\n}\n</code></pre> counting_sort.swift<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u7c21\u55ae\u5be6\u73fe\uff0c\u7121\u6cd5\u7528\u65bc\u6392\u5e8f\u7269\u4ef6\nfunc countingSortNaive(nums: inout [Int]) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n let m = nums.max()!\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n var counter = Array(repeating: 0, count: m + 1)\n for num in nums {\n counter[num] += 1\n }\n // 3. \u8d70\u8a2a counter \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u539f\u9663\u5217 nums\n var i = 0\n for num in 0 ..&lt; m + 1 {\n for _ in 0 ..&lt; counter[num] {\n nums[i] = num\n i += 1\n }\n }\n}\n</code></pre> counting_sort.js<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u7c21\u55ae\u5be6\u73fe\uff0c\u7121\u6cd5\u7528\u65bc\u6392\u5e8f\u7269\u4ef6\nfunction countingSortNaive(nums) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n let m = 0;\n for (const num of nums) {\n m = Math.max(m, num);\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n const counter = new Array(m + 1).fill(0);\n for (const num of nums) {\n counter[num]++;\n }\n // 3. \u8d70\u8a2a counter \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u539f\u9663\u5217 nums\n let i = 0;\n for (let num = 0; num &lt; m + 1; num++) {\n for (let j = 0; j &lt; counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n</code></pre> counting_sort.ts<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u7c21\u55ae\u5be6\u73fe\uff0c\u7121\u6cd5\u7528\u65bc\u6392\u5e8f\u7269\u4ef6\nfunction countingSortNaive(nums: number[]): void {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n let m = 0;\n for (const num of nums) {\n m = Math.max(m, num);\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n const counter: number[] = new Array&lt;number&gt;(m + 1).fill(0);\n for (const num of nums) {\n counter[num]++;\n }\n // 3. \u8d70\u8a2a counter \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u539f\u9663\u5217 nums\n let i = 0;\n for (let num = 0; num &lt; m + 1; num++) {\n for (let j = 0; j &lt; counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n}\n</code></pre> counting_sort.dart<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u7c21\u55ae\u5be6\u73fe\uff0c\u7121\u6cd5\u7528\u65bc\u6392\u5e8f\u7269\u4ef6\nvoid countingSortNaive(List&lt;int&gt; nums) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int _num in nums) {\n m = max(m, _num);\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[_num] \u4ee3\u8868 _num \u7684\u51fa\u73fe\u6b21\u6578\n List&lt;int&gt; counter = List.filled(m + 1, 0);\n for (int _num in nums) {\n counter[_num]++;\n }\n // 3. \u8d70\u8a2a counter \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u539f\u9663\u5217 nums\n int i = 0;\n for (int _num = 0; _num &lt; m + 1; _num++) {\n for (int j = 0; j &lt; counter[_num]; j++, i++) {\n nums[i] = _num;\n }\n }\n}\n</code></pre> counting_sort.rs<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u7c21\u55ae\u5be6\u73fe\uff0c\u7121\u6cd5\u7528\u65bc\u6392\u5e8f\u7269\u4ef6\nfn counting_sort_naive(nums: &amp;mut [i32]) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n let m = *nums.into_iter().max().unwrap();\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n let mut counter = vec![0; m as usize + 1];\n for &amp;num in &amp;*nums {\n counter[num as usize] += 1;\n }\n // 3. \u8d70\u8a2a counter \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u539f\u9663\u5217 nums\n let mut i = 0;\n for num in 0..m + 1 {\n for _ in 0..counter[num as usize] {\n nums[i] = num;\n i += 1;\n }\n }\n}\n</code></pre> counting_sort.c<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u7c21\u55ae\u5be6\u73fe\uff0c\u7121\u6cd5\u7528\u65bc\u6392\u5e8f\u7269\u4ef6\nvoid countingSortNaive(int nums[], int size) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int i = 0; i &lt; size; i++) {\n if (nums[i] &gt; m) {\n m = nums[i];\n }\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n int *counter = calloc(m + 1, sizeof(int));\n for (int i = 0; i &lt; size; i++) {\n counter[nums[i]]++;\n }\n // 3. \u8d70\u8a2a counter \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u539f\u9663\u5217 nums\n int i = 0;\n for (int num = 0; num &lt; m + 1; num++) {\n for (int j = 0; j &lt; counter[num]; j++, i++) {\n nums[i] = num;\n }\n }\n // 4. \u91cb\u653e\u8a18\u61b6\u9ad4\n free(counter);\n}\n</code></pre> counting_sort.kt<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u7c21\u55ae\u5be6\u73fe\uff0c\u7121\u6cd5\u7528\u65bc\u6392\u5e8f\u7269\u4ef6\nfun countingSortNaive(nums: IntArray) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n var m = 0\n for (num in nums) {\n m = max(m, num)\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n val counter = IntArray(m + 1)\n for (num in nums) {\n counter[num]++\n }\n // 3. \u8d70\u8a2a counter \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u539f\u9663\u5217 nums\n var i = 0\n for (num in 0..&lt;m + 1) {\n var j = 0\n while (j &lt; counter[num]) {\n nums[i] = num\n j++\n i++\n }\n }\n}\n</code></pre> counting_sort.rb<pre><code>[class]{}-[func]{counting_sort_naive}\n</code></pre> counting_sort.zig<pre><code>[class]{}-[func]{countingSortNaive}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u8a08\u6578\u6392\u5e8f\u8207\u6876\u6392\u5e8f\u7684\u95dc\u806f</p> <p>\u5f9e\u6876\u6392\u5e8f\u7684\u89d2\u5ea6\u770b\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u8a08\u6578\u6392\u5e8f\u4e2d\u7684\u8a08\u6578\u9663\u5217 <code>counter</code> \u7684\u6bcf\u500b\u7d22\u5f15\u8996\u70ba\u4e00\u500b\u6876\uff0c\u5c07\u7d71\u8a08\u6578\u91cf\u7684\u904e\u7a0b\u770b\u4f5c\u5c07\u5404\u500b\u5143\u7d20\u5206\u914d\u5230\u5c0d\u61c9\u7684\u6876\u4e2d\u3002\u672c\u8cea\u4e0a\uff0c\u8a08\u6578\u6392\u5e8f\u662f\u6876\u6392\u5e8f\u5728\u6574\u578b\u8cc7\u6599\u4e0b\u7684\u4e00\u500b\u7279\u4f8b\u3002</p>"},{"location":"chapter_sorting/counting_sort/#1192","title":"11.9.2 \u00a0 \u5b8c\u6574\u5be6\u73fe","text":"<p>\u7d30\u5fc3\u7684\u8b80\u8005\u53ef\u80fd\u767c\u73fe\u4e86\uff0c\u5982\u679c\u8f38\u5165\u8cc7\u6599\u662f\u7269\u4ef6\uff0c\u4e0a\u8ff0\u6b65\u9a5f <code>3.</code> \u5c31\u5931\u6548\u4e86\u3002\u5047\u8a2d\u8f38\u5165\u8cc7\u6599\u662f\u5546\u54c1\u7269\u4ef6\uff0c\u6211\u5011\u60f3\u6309\u7167\u5546\u54c1\u50f9\u683c\uff08\u985e\u5225\u7684\u6210\u54e1\u8b8a\u6578\uff09\u5c0d\u5546\u54c1\u9032\u884c\u6392\u5e8f\uff0c\u800c\u4e0a\u8ff0\u6f14\u7b97\u6cd5\u53ea\u80fd\u7d66\u51fa\u50f9\u683c\u7684\u6392\u5e8f\u7d50\u679c\u3002</p> <p>\u90a3\u9ebc\u5982\u4f55\u624d\u80fd\u5f97\u5230\u539f\u8cc7\u6599\u7684\u6392\u5e8f\u7d50\u679c\u5462\uff1f\u6211\u5011\u9996\u5148\u8a08\u7b97 <code>counter</code> \u7684\u201c\u524d\u7db4\u548c\u201d\u3002\u9867\u540d\u601d\u7fa9\uff0c\u7d22\u5f15 <code>i</code> \u8655\u7684\u524d\u7db4\u548c <code>prefix[i]</code> \u7b49\u65bc\u9663\u5217\u524d <code>i</code> \u500b\u5143\u7d20\u4e4b\u548c\uff1a</p> \\[ \\text{prefix}[i] = \\sum_{j=0}^i \\text{counter[j]} \\] <p>\u524d\u7db4\u548c\u5177\u6709\u660e\u78ba\u7684\u610f\u7fa9\uff0c<code>prefix[num] - 1</code> \u4ee3\u8868\u5143\u7d20 <code>num</code> \u5728\u7d50\u679c\u9663\u5217 <code>res</code> \u4e2d\u6700\u5f8c\u4e00\u6b21\u51fa\u73fe\u7684\u7d22\u5f15\u3002\u9019\u500b\u8cc7\u8a0a\u975e\u5e38\u95dc\u9375\uff0c\u56e0\u70ba\u5b83\u544a\u8a34\u6211\u5011\u5404\u500b\u5143\u7d20\u61c9\u8a72\u51fa\u73fe\u5728\u7d50\u679c\u9663\u5217\u7684\u54ea\u500b\u4f4d\u7f6e\u3002\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5012\u5e8f\u8d70\u8a2a\u539f\u9663\u5217 <code>nums</code> \u7684\u6bcf\u500b\u5143\u7d20 <code>num</code> \uff0c\u5728\u6bcf\u8f2a\u8fed\u4ee3\u4e2d\u57f7\u884c\u4ee5\u4e0b\u5169\u6b65\u3002</p> <ol> <li>\u5c07 <code>num</code> \u586b\u5165\u9663\u5217 <code>res</code> \u7684\u7d22\u5f15 <code>prefix[num] - 1</code> \u8655\u3002</li> <li>\u4ee4\u524d\u7db4\u548c <code>prefix[num]</code> \u6e1b\u5c0f \\(1\\) \uff0c\u5f9e\u800c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e <code>num</code> \u7684\u7d22\u5f15\u3002</li> </ol> <p>\u8d70\u8a2a\u5b8c\u6210\u5f8c\uff0c\u9663\u5217 <code>res</code> \u4e2d\u5c31\u662f\u6392\u5e8f\u597d\u7684\u7d50\u679c\uff0c\u6700\u5f8c\u4f7f\u7528 <code>res</code> \u8986\u84cb\u539f\u9663\u5217 <code>nums</code> \u5373\u53ef\u3002\u5716 11-17 \u5c55\u793a\u4e86\u5b8c\u6574\u7684\u8a08\u6578\u6392\u5e8f\u6d41\u7a0b\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 11-17 \u00a0 \u8a08\u6578\u6392\u5e8f\u6b65\u9a5f </p> <p>\u8a08\u6578\u6392\u5e8f\u7684\u5be6\u73fe\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig counting_sort.py<pre><code>def counting_sort(nums: list[int]):\n \"\"\"\u8a08\u6578\u6392\u5e8f\"\"\"\n # \u5b8c\u6574\u5be6\u73fe\uff0c\u53ef\u6392\u5e8f\u7269\u4ef6\uff0c\u4e26\u4e14\u662f\u7a69\u5b9a\u6392\u5e8f\n # 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n m = max(nums)\n # 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n # counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n counter = [0] * (m + 1)\n for num in nums:\n counter[num] += 1\n # 3. \u6c42 counter \u7684\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u6b21\u6578\u201d\u8f49\u63db\u70ba\u201c\u5c3e\u7d22\u5f15\u201d\n # \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u5f8c\u4e00\u6b21\u51fa\u73fe\u7684\u7d22\u5f15\n for i in range(m):\n counter[i + 1] += counter[i]\n # 4. \u5012\u5e8f\u8d70\u8a2a nums \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u7d50\u679c\u9663\u5217 res\n # \u521d\u59cb\u5316\u9663\u5217 res \u7528\u65bc\u8a18\u9304\u7d50\u679c\n n = len(nums)\n res = [0] * n\n for i in range(n - 1, -1, -1):\n num = nums[i]\n res[counter[num] - 1] = num # \u5c07 num \u653e\u7f6e\u5230\u5c0d\u61c9\u7d22\u5f15\u8655\n counter[num] -= 1 # \u4ee4\u524d\u7db4\u548c\u81ea\u6e1b 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n # \u4f7f\u7528\u7d50\u679c\u9663\u5217 res \u8986\u84cb\u539f\u9663\u5217 nums\n for i in range(n):\n nums[i] = res[i]\n</code></pre> counting_sort.cpp<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u5b8c\u6574\u5be6\u73fe\uff0c\u53ef\u6392\u5e8f\u7269\u4ef6\uff0c\u4e26\u4e14\u662f\u7a69\u5b9a\u6392\u5e8f\nvoid countingSort(vector&lt;int&gt; &amp;nums) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int num : nums) {\n m = max(m, num);\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n vector&lt;int&gt; counter(m + 1, 0);\n for (int num : nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u6b21\u6578\u201d\u8f49\u63db\u70ba\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u5f8c\u4e00\u6b21\u51fa\u73fe\u7684\u7d22\u5f15\n for (int i = 0; i &lt; m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u8d70\u8a2a nums \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u7d50\u679c\u9663\u5217 res\n // \u521d\u59cb\u5316\u9663\u5217 res \u7528\u65bc\u8a18\u9304\u7d50\u679c\n int n = nums.size();\n vector&lt;int&gt; res(n);\n for (int i = n - 1; i &gt;= 0; i--) {\n int num = nums[i];\n res[counter[num] - 1] = num; // \u5c07 num \u653e\u7f6e\u5230\u5c0d\u61c9\u7d22\u5f15\u8655\n counter[num]--; // \u4ee4\u524d\u7db4\u548c\u81ea\u6e1b 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7d50\u679c\u9663\u5217 res \u8986\u84cb\u539f\u9663\u5217 nums\n nums = res;\n}\n</code></pre> counting_sort.java<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u5b8c\u6574\u5be6\u73fe\uff0c\u53ef\u6392\u5e8f\u7269\u4ef6\uff0c\u4e26\u4e14\u662f\u7a69\u5b9a\u6392\u5e8f\nvoid countingSort(int[] nums) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int num : nums) {\n m = Math.max(m, num);\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n int[] counter = new int[m + 1];\n for (int num : nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u6b21\u6578\u201d\u8f49\u63db\u70ba\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u5f8c\u4e00\u6b21\u51fa\u73fe\u7684\u7d22\u5f15\n for (int i = 0; i &lt; m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u8d70\u8a2a nums \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u7d50\u679c\u9663\u5217 res\n // \u521d\u59cb\u5316\u9663\u5217 res \u7528\u65bc\u8a18\u9304\u7d50\u679c\n int n = nums.length;\n int[] res = new int[n];\n for (int i = n - 1; i &gt;= 0; i--) {\n int num = nums[i];\n res[counter[num] - 1] = num; // \u5c07 num \u653e\u7f6e\u5230\u5c0d\u61c9\u7d22\u5f15\u8655\n counter[num]--; // \u4ee4\u524d\u7db4\u548c\u81ea\u6e1b 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7d50\u679c\u9663\u5217 res \u8986\u84cb\u539f\u9663\u5217 nums\n for (int i = 0; i &lt; n; i++) {\n nums[i] = res[i];\n }\n}\n</code></pre> counting_sort.cs<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u5b8c\u6574\u5be6\u73fe\uff0c\u53ef\u6392\u5e8f\u7269\u4ef6\uff0c\u4e26\u4e14\u662f\u7a69\u5b9a\u6392\u5e8f\nvoid CountingSort(int[] nums) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n int m = 0;\n foreach (int num in nums) {\n m = Math.Max(m, num);\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n int[] counter = new int[m + 1];\n foreach (int num in nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u6b21\u6578\u201d\u8f49\u63db\u70ba\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u5f8c\u4e00\u6b21\u51fa\u73fe\u7684\u7d22\u5f15\n for (int i = 0; i &lt; m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u8d70\u8a2a nums \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u7d50\u679c\u9663\u5217 res\n // \u521d\u59cb\u5316\u9663\u5217 res \u7528\u65bc\u8a18\u9304\u7d50\u679c\n int n = nums.Length;\n int[] res = new int[n];\n for (int i = n - 1; i &gt;= 0; i--) {\n int num = nums[i];\n res[counter[num] - 1] = num; // \u5c07 num \u653e\u7f6e\u5230\u5c0d\u61c9\u7d22\u5f15\u8655\n counter[num]--; // \u4ee4\u524d\u7db4\u548c\u81ea\u6e1b 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7d50\u679c\u9663\u5217 res \u8986\u84cb\u539f\u9663\u5217 nums\n for (int i = 0; i &lt; n; i++) {\n nums[i] = res[i];\n }\n}\n</code></pre> counting_sort.go<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u5b8c\u6574\u5be6\u73fe\uff0c\u53ef\u6392\u5e8f\u7269\u4ef6\uff0c\u4e26\u4e14\u662f\u7a69\u5b9a\u6392\u5e8f\nfunc countingSort(nums []int) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n m := 0\n for _, num := range nums {\n if num &gt; m {\n m = num\n }\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n counter := make([]int, m+1)\n for _, num := range nums {\n counter[num]++\n }\n // 3. \u6c42 counter \u7684\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u6b21\u6578\u201d\u8f49\u63db\u70ba\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u5f8c\u4e00\u6b21\u51fa\u73fe\u7684\u7d22\u5f15\n for i := 0; i &lt; m; i++ {\n counter[i+1] += counter[i]\n }\n // 4. \u5012\u5e8f\u8d70\u8a2a nums \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u7d50\u679c\u9663\u5217 res\n // \u521d\u59cb\u5316\u9663\u5217 res \u7528\u65bc\u8a18\u9304\u7d50\u679c\n n := len(nums)\n res := make([]int, n)\n for i := n - 1; i &gt;= 0; i-- {\n num := nums[i]\n // \u5c07 num \u653e\u7f6e\u5230\u5c0d\u61c9\u7d22\u5f15\u8655\n res[counter[num]-1] = num\n // \u4ee4\u524d\u7db4\u548c\u81ea\u6e1b 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n counter[num]--\n }\n // \u4f7f\u7528\u7d50\u679c\u9663\u5217 res \u8986\u84cb\u539f\u9663\u5217 nums\n copy(nums, res)\n}\n</code></pre> counting_sort.swift<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u5b8c\u6574\u5be6\u73fe\uff0c\u53ef\u6392\u5e8f\u7269\u4ef6\uff0c\u4e26\u4e14\u662f\u7a69\u5b9a\u6392\u5e8f\nfunc countingSort(nums: inout [Int]) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n let m = nums.max()!\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n var counter = Array(repeating: 0, count: m + 1)\n for num in nums {\n counter[num] += 1\n }\n // 3. \u6c42 counter \u7684\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u6b21\u6578\u201d\u8f49\u63db\u70ba\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u5f8c\u4e00\u6b21\u51fa\u73fe\u7684\u7d22\u5f15\n for i in 0 ..&lt; m {\n counter[i + 1] += counter[i]\n }\n // 4. \u5012\u5e8f\u8d70\u8a2a nums \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u7d50\u679c\u9663\u5217 res\n // \u521d\u59cb\u5316\u9663\u5217 res \u7528\u65bc\u8a18\u9304\u7d50\u679c\n var res = Array(repeating: 0, count: nums.count)\n for i in nums.indices.reversed() {\n let num = nums[i]\n res[counter[num] - 1] = num // \u5c07 num \u653e\u7f6e\u5230\u5c0d\u61c9\u7d22\u5f15\u8655\n counter[num] -= 1 // \u4ee4\u524d\u7db4\u548c\u81ea\u6e1b 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7d50\u679c\u9663\u5217 res \u8986\u84cb\u539f\u9663\u5217 nums\n for i in nums.indices {\n nums[i] = res[i]\n }\n}\n</code></pre> counting_sort.js<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u5b8c\u6574\u5be6\u73fe\uff0c\u53ef\u6392\u5e8f\u7269\u4ef6\uff0c\u4e26\u4e14\u662f\u7a69\u5b9a\u6392\u5e8f\nfunction countingSort(nums) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n let m = 0;\n for (const num of nums) {\n m = Math.max(m, num);\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n const counter = new Array(m + 1).fill(0);\n for (const num of nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u6b21\u6578\u201d\u8f49\u63db\u70ba\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u5f8c\u4e00\u6b21\u51fa\u73fe\u7684\u7d22\u5f15\n for (let i = 0; i &lt; m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u8d70\u8a2a nums \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u7d50\u679c\u9663\u5217 res\n // \u521d\u59cb\u5316\u9663\u5217 res \u7528\u65bc\u8a18\u9304\u7d50\u679c\n const n = nums.length;\n const res = new Array(n);\n for (let i = n - 1; i &gt;= 0; i--) {\n const num = nums[i];\n res[counter[num] - 1] = num; // \u5c07 num \u653e\u7f6e\u5230\u5c0d\u61c9\u7d22\u5f15\u8655\n counter[num]--; // \u4ee4\u524d\u7db4\u548c\u81ea\u6e1b 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7d50\u679c\u9663\u5217 res \u8986\u84cb\u539f\u9663\u5217 nums\n for (let i = 0; i &lt; n; i++) {\n nums[i] = res[i];\n }\n}\n</code></pre> counting_sort.ts<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u5b8c\u6574\u5be6\u73fe\uff0c\u53ef\u6392\u5e8f\u7269\u4ef6\uff0c\u4e26\u4e14\u662f\u7a69\u5b9a\u6392\u5e8f\nfunction countingSort(nums: number[]): void {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n let m = 0;\n for (const num of nums) {\n m = Math.max(m, num);\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n const counter: number[] = new Array&lt;number&gt;(m + 1).fill(0);\n for (const num of nums) {\n counter[num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u6b21\u6578\u201d\u8f49\u63db\u70ba\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u5f8c\u4e00\u6b21\u51fa\u73fe\u7684\u7d22\u5f15\n for (let i = 0; i &lt; m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u8d70\u8a2a nums \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u7d50\u679c\u9663\u5217 res\n // \u521d\u59cb\u5316\u9663\u5217 res \u7528\u65bc\u8a18\u9304\u7d50\u679c\n const n = nums.length;\n const res: number[] = new Array&lt;number&gt;(n);\n for (let i = n - 1; i &gt;= 0; i--) {\n const num = nums[i];\n res[counter[num] - 1] = num; // \u5c07 num \u653e\u7f6e\u5230\u5c0d\u61c9\u7d22\u5f15\u8655\n counter[num]--; // \u4ee4\u524d\u7db4\u548c\u81ea\u6e1b 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7d50\u679c\u9663\u5217 res \u8986\u84cb\u539f\u9663\u5217 nums\n for (let i = 0; i &lt; n; i++) {\n nums[i] = res[i];\n }\n}\n</code></pre> counting_sort.dart<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u5b8c\u6574\u5be6\u73fe\uff0c\u53ef\u6392\u5e8f\u7269\u4ef6\uff0c\u4e26\u4e14\u662f\u7a69\u5b9a\u6392\u5e8f\nvoid countingSort(List&lt;int&gt; nums) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int _num in nums) {\n m = max(m, _num);\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[_num] \u4ee3\u8868 _num \u7684\u51fa\u73fe\u6b21\u6578\n List&lt;int&gt; counter = List.filled(m + 1, 0);\n for (int _num in nums) {\n counter[_num]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u6b21\u6578\u201d\u8f49\u63db\u70ba\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[_num]-1 \u662f _num \u5728 res \u4e2d\u6700\u5f8c\u4e00\u6b21\u51fa\u73fe\u7684\u7d22\u5f15\n for (int i = 0; i &lt; m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u8d70\u8a2a nums \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u7d50\u679c\u9663\u5217 res\n // \u521d\u59cb\u5316\u9663\u5217 res \u7528\u65bc\u8a18\u9304\u7d50\u679c\n int n = nums.length;\n List&lt;int&gt; res = List.filled(n, 0);\n for (int i = n - 1; i &gt;= 0; i--) {\n int _num = nums[i];\n res[counter[_num] - 1] = _num; // \u5c07 _num \u653e\u7f6e\u5230\u5c0d\u61c9\u7d22\u5f15\u8655\n counter[_num]--; // \u4ee4\u524d\u7db4\u548c\u81ea\u6e1b 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e _num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7d50\u679c\u9663\u5217 res \u8986\u84cb\u539f\u9663\u5217 nums\n nums.setAll(0, res);\n}\n</code></pre> counting_sort.rs<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u5b8c\u6574\u5be6\u73fe\uff0c\u53ef\u6392\u5e8f\u7269\u4ef6\uff0c\u4e26\u4e14\u662f\u7a69\u5b9a\u6392\u5e8f\nfn counting_sort(nums: &amp;mut [i32]) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n let m = *nums.into_iter().max().unwrap();\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n let mut counter = vec![0; m as usize + 1];\n for &amp;num in &amp;*nums {\n counter[num as usize] += 1;\n }\n // 3. \u6c42 counter \u7684\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u6b21\u6578\u201d\u8f49\u63db\u70ba\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u5f8c\u4e00\u6b21\u51fa\u73fe\u7684\u7d22\u5f15\n for i in 0..m as usize {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u8d70\u8a2a nums \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u7d50\u679c\u9663\u5217 res\n // \u521d\u59cb\u5316\u9663\u5217 res \u7528\u65bc\u8a18\u9304\u7d50\u679c\n let n = nums.len();\n let mut res = vec![0; n];\n for i in (0..n).rev() {\n let num = nums[i];\n res[counter[num as usize] - 1] = num; // \u5c07 num \u653e\u7f6e\u5230\u5c0d\u61c9\u7d22\u5f15\u8655\n counter[num as usize] -= 1; // \u4ee4\u524d\u7db4\u548c\u81ea\u6e1b 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7d50\u679c\u9663\u5217 res \u8986\u84cb\u539f\u9663\u5217 nums\n for i in 0..n {\n nums[i] = res[i];\n }\n}\n</code></pre> counting_sort.c<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u5b8c\u6574\u5be6\u73fe\uff0c\u53ef\u6392\u5e8f\u7269\u4ef6\uff0c\u4e26\u4e14\u662f\u7a69\u5b9a\u6392\u5e8f\nvoid countingSort(int nums[], int size) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n int m = 0;\n for (int i = 0; i &lt; size; i++) {\n if (nums[i] &gt; m) {\n m = nums[i];\n }\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n int *counter = calloc(m, sizeof(int));\n for (int i = 0; i &lt; size; i++) {\n counter[nums[i]]++;\n }\n // 3. \u6c42 counter \u7684\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u6b21\u6578\u201d\u8f49\u63db\u70ba\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u5f8c\u4e00\u6b21\u51fa\u73fe\u7684\u7d22\u5f15\n for (int i = 0; i &lt; m; i++) {\n counter[i + 1] += counter[i];\n }\n // 4. \u5012\u5e8f\u8d70\u8a2a nums \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u7d50\u679c\u9663\u5217 res\n // \u521d\u59cb\u5316\u9663\u5217 res \u7528\u65bc\u8a18\u9304\u7d50\u679c\n int *res = malloc(sizeof(int) * size);\n for (int i = size - 1; i &gt;= 0; i--) {\n int num = nums[i];\n res[counter[num] - 1] = num; // \u5c07 num \u653e\u7f6e\u5230\u5c0d\u61c9\u7d22\u5f15\u8655\n counter[num]--; // \u4ee4\u524d\u7db4\u548c\u81ea\u6e1b 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7d50\u679c\u9663\u5217 res \u8986\u84cb\u539f\u9663\u5217 nums\n memcpy(nums, res, size * sizeof(int));\n // 5. \u91cb\u653e\u8a18\u61b6\u9ad4\n free(counter);\n}\n</code></pre> counting_sort.kt<pre><code>/* \u8a08\u6578\u6392\u5e8f */\n// \u5b8c\u6574\u5be6\u73fe\uff0c\u53ef\u6392\u5e8f\u7269\u4ef6\uff0c\u4e26\u4e14\u662f\u7a69\u5b9a\u6392\u5e8f\nfun countingSort(nums: IntArray) {\n // 1. \u7d71\u8a08\u9663\u5217\u6700\u5927\u5143\u7d20 m\n var m = 0\n for (num in nums) {\n m = max(m, num)\n }\n // 2. \u7d71\u8a08\u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n // counter[num] \u4ee3\u8868 num \u7684\u51fa\u73fe\u6b21\u6578\n val counter = IntArray(m + 1)\n for (num in nums) {\n counter[num]++\n }\n // 3. \u6c42 counter \u7684\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u6b21\u6578\u201d\u8f49\u63db\u70ba\u201c\u5c3e\u7d22\u5f15\u201d\n // \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u5f8c\u4e00\u6b21\u51fa\u73fe\u7684\u7d22\u5f15\n for (i in 0..&lt;m) {\n counter[i + 1] += counter[i]\n }\n // 4. \u5012\u5e8f\u8d70\u8a2a nums \uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165\u7d50\u679c\u9663\u5217 res\n // \u521d\u59cb\u5316\u9663\u5217 res \u7528\u65bc\u8a18\u9304\u7d50\u679c\n val n = nums.size\n val res = IntArray(n)\n for (i in n - 1 downTo 0) {\n val num = nums[i]\n res[counter[num] - 1] = num // \u5c07 num \u653e\u7f6e\u5230\u5c0d\u61c9\u7d22\u5f15\u8655\n counter[num]-- // \u4ee4\u524d\u7db4\u548c\u81ea\u6e1b 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n }\n // \u4f7f\u7528\u7d50\u679c\u9663\u5217 res \u8986\u84cb\u539f\u9663\u5217 nums\n for (i in 0..&lt;n) {\n nums[i] = res[i]\n }\n}\n</code></pre> counting_sort.rb<pre><code>[class]{}-[func]{counting_sort}\n</code></pre> counting_sort.zig<pre><code>[class]{}-[func]{countingSort}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_sorting/counting_sort/#1193","title":"11.9.3 \u00a0 \u6f14\u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n + m)\\)\u3001\u975e\u81ea\u9069\u61c9\u6392\u5e8f \uff1a\u6d89\u53ca\u8d70\u8a2a <code>nums</code> \u548c\u8d70\u8a2a <code>counter</code> \uff0c\u90fd\u4f7f\u7528\u7dda\u6027\u6642\u9593\u3002\u4e00\u822c\u60c5\u6cc1\u4e0b \\(n \\gg m\\) \uff0c\u6642\u9593\u8907\u96dc\u5ea6\u8da8\u65bc \\(O(n)\\) \u3002</li> <li>\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n + m)\\)\u3001\u975e\u539f\u5730\u6392\u5e8f\uff1a\u85c9\u52a9\u4e86\u9577\u5ea6\u5206\u5225\u70ba \\(n\\) \u548c \\(m\\) \u7684\u9663\u5217 <code>res</code> \u548c <code>counter</code> \u3002</li> <li>\u7a69\u5b9a\u6392\u5e8f\uff1a\u7531\u65bc\u5411 <code>res</code> \u4e2d\u586b\u5145\u5143\u7d20\u7684\u9806\u5e8f\u662f\u201c\u5f9e\u53f3\u5411\u5de6\u201d\u7684\uff0c\u56e0\u6b64\u5012\u5e8f\u8d70\u8a2a <code>nums</code> \u53ef\u4ee5\u907f\u514d\u6539\u8b8a\u76f8\u7b49\u5143\u7d20\u4e4b\u9593\u7684\u76f8\u5c0d\u4f4d\u7f6e\uff0c\u5f9e\u800c\u5be6\u73fe\u7a69\u5b9a\u6392\u5e8f\u3002\u5be6\u969b\u4e0a\uff0c\u6b63\u5e8f\u8d70\u8a2a <code>nums</code> \u4e5f\u53ef\u4ee5\u5f97\u5230\u6b63\u78ba\u7684\u6392\u5e8f\u7d50\u679c\uff0c\u4f46\u7d50\u679c\u662f\u975e\u7a69\u5b9a\u7684\u3002</li> </ul>"},{"location":"chapter_sorting/counting_sort/#1194","title":"11.9.4 \u00a0 \u4fb7\u9650\u6027","text":"<p>\u770b\u5230\u9019\u88e1\uff0c\u4f60\u4e5f\u8a31\u6703\u89ba\u5f97\u8a08\u6578\u6392\u5e8f\u975e\u5e38\u5de7\u5999\uff0c\u50c5\u900f\u904e\u7d71\u8a08\u6578\u91cf\u5c31\u53ef\u4ee5\u5be6\u73fe\u9ad8\u6548\u7684\u6392\u5e8f\u3002\u7136\u800c\uff0c\u4f7f\u7528\u8a08\u6578\u6392\u5e8f\u7684\u524d\u7f6e\u689d\u4ef6\u76f8\u5c0d\u8f03\u70ba\u56b4\u683c\u3002</p> <p>\u8a08\u6578\u6392\u5e8f\u53ea\u9069\u7528\u65bc\u975e\u8ca0\u6574\u6578\u3002\u82e5\u60f3\u5c07\u5176\u7528\u65bc\u5176\u4ed6\u578b\u5225\u7684\u8cc7\u6599\uff0c\u9700\u8981\u78ba\u4fdd\u9019\u4e9b\u8cc7\u6599\u53ef\u4ee5\u8f49\u63db\u70ba\u975e\u8ca0\u6574\u6578\uff0c\u4e26\u4e14\u5728\u8f49\u63db\u904e\u7a0b\u4e2d\u4e0d\u80fd\u6539\u8b8a\u5404\u500b\u5143\u7d20\u4e4b\u9593\u7684\u76f8\u5c0d\u5927\u5c0f\u95dc\u4fc2\u3002\u4f8b\u5982\uff0c\u5c0d\u65bc\u5305\u542b\u8ca0\u6578\u7684\u6574\u6578\u9663\u5217\uff0c\u53ef\u4ee5\u5148\u7d66\u6240\u6709\u6578\u5b57\u52a0\u4e0a\u4e00\u500b\u5e38\u6578\uff0c\u5c07\u5168\u90e8\u6578\u5b57\u8f49\u5316\u70ba\u6b63\u6578\uff0c\u6392\u5e8f\u5b8c\u6210\u5f8c\u518d\u8f49\u63db\u56de\u53bb\u3002</p> <p>\u8a08\u6578\u6392\u5e8f\u9069\u7528\u65bc\u8cc7\u6599\u91cf\u5927\u4f46\u8cc7\u6599\u7bc4\u570d\u8f03\u5c0f\u7684\u60c5\u6cc1\u3002\u6bd4\u5982\uff0c\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d \\(m\\) \u4e0d\u80fd\u592a\u5927\uff0c\u5426\u5247\u6703\u4f54\u7528\u904e\u591a\u7a7a\u9593\u3002\u800c\u7576 \\(n \\ll m\\) \u6642\uff0c\u8a08\u6578\u6392\u5e8f\u4f7f\u7528 \\(O(m)\\) \u6642\u9593\uff0c\u53ef\u80fd\u6bd4 \\(O(n \\log n)\\) \u7684\u6392\u5e8f\u6f14\u7b97\u6cd5\u9084\u8981\u6162\u3002</p>"},{"location":"chapter_sorting/heap_sort/","title":"11.7 \u00a0 \u5806\u7a4d\u6392\u5e8f","text":"<p>Tip</p> <p>\u95b1\u8b80\u672c\u7bc0\u524d\uff0c\u8acb\u78ba\u4fdd\u5df2\u5b78\u5b8c\u201c\u5806\u7a4d\u201c\u7ae0\u7bc0\u3002</p> <p>\u5806\u7a4d\u6392\u5e8f\uff08heap sort\uff09\u662f\u4e00\u7a2e\u57fa\u65bc\u5806\u7a4d\u8cc7\u6599\u7d50\u69cb\u5be6\u73fe\u7684\u9ad8\u6548\u6392\u5e8f\u6f14\u7b97\u6cd5\u3002\u6211\u5011\u53ef\u4ee5\u5229\u7528\u5df2\u7d93\u5b78\u904e\u7684\u201c\u5efa\u5806\u7a4d\u64cd\u4f5c\u201d\u548c\u201c\u5143\u7d20\u51fa\u5806\u7a4d\u64cd\u4f5c\u201d\u5be6\u73fe\u5806\u7a4d\u6392\u5e8f\u3002</p> <ol> <li>\u8f38\u5165\u9663\u5217\u4e26\u5efa\u7acb\u5c0f\u9802\u5806\u7a4d\uff0c\u6b64\u6642\u6700\u5c0f\u5143\u7d20\u4f4d\u65bc\u5806\u7a4d\u9802\u3002</li> <li>\u4e0d\u65b7\u57f7\u884c\u51fa\u5806\u7a4d\u64cd\u4f5c\uff0c\u4f9d\u6b21\u8a18\u9304\u51fa\u5806\u7a4d\u5143\u7d20\uff0c\u5373\u53ef\u5f97\u5230\u5f9e\u5c0f\u5230\u5927\u6392\u5e8f\u7684\u5e8f\u5217\u3002</li> </ol> <p>\u4ee5\u4e0a\u65b9\u6cd5\u96d6\u7136\u53ef\u884c\uff0c\u4f46\u9700\u8981\u85c9\u52a9\u4e00\u500b\u984d\u5916\u9663\u5217\u4f86\u5132\u5b58\u5f48\u51fa\u7684\u5143\u7d20\uff0c\u6bd4\u8f03\u6d6a\u8cbb\u7a7a\u9593\u3002\u5728\u5be6\u969b\u4e2d\uff0c\u6211\u5011\u901a\u5e38\u4f7f\u7528\u4e00\u7a2e\u66f4\u52a0\u512a\u96c5\u7684\u5be6\u73fe\u65b9\u5f0f\u3002</p>"},{"location":"chapter_sorting/heap_sort/#1171","title":"11.7.1 \u00a0 \u6f14\u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u8a2d\u9663\u5217\u7684\u9577\u5ea6\u70ba \\(n\\) \uff0c\u5806\u7a4d\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u5716 11-12 \u6240\u793a\u3002</p> <ol> <li>\u8f38\u5165\u9663\u5217\u4e26\u5efa\u7acb\u5927\u9802\u5806\u7a4d\u3002\u5b8c\u6210\u5f8c\uff0c\u6700\u5927\u5143\u7d20\u4f4d\u65bc\u5806\u7a4d\u9802\u3002</li> <li>\u5c07\u5806\u7a4d\u9802\u5143\u7d20\uff08\u7b2c\u4e00\u500b\u5143\u7d20\uff09\u8207\u5806\u7a4d\u5e95\u5143\u7d20\uff08\u6700\u5f8c\u4e00\u500b\u5143\u7d20\uff09\u4ea4\u63db\u3002\u5b8c\u6210\u4ea4\u63db\u5f8c\uff0c\u5806\u7a4d\u7684\u9577\u5ea6\u6e1b \\(1\\) \uff0c\u5df2\u6392\u5e8f\u5143\u7d20\u6578\u91cf\u52a0 \\(1\\) \u3002</li> <li>\u5f9e\u5806\u7a4d\u9802\u5143\u7d20\u958b\u59cb\uff0c\u5f9e\u9802\u5230\u5e95\u57f7\u884c\u5806\u7a4d\u5316\u64cd\u4f5c\uff08sift down\uff09\u3002\u5b8c\u6210\u5806\u7a4d\u5316\u5f8c\uff0c\u5806\u7a4d\u7684\u6027\u8cea\u5f97\u5230\u4fee\u5fa9\u3002</li> <li>\u8ff4\u5708\u57f7\u884c\u7b2c <code>2.</code> \u6b65\u548c\u7b2c <code>3.</code> \u6b65\u3002\u8ff4\u5708 \\(n - 1\\) \u8f2a\u5f8c\uff0c\u5373\u53ef\u5b8c\u6210\u9663\u5217\u6392\u5e8f\u3002</li> </ol> <p>Tip</p> <p>\u5be6\u969b\u4e0a\uff0c\u5143\u7d20\u51fa\u5806\u7a4d\u64cd\u4f5c\u4e2d\u4e5f\u5305\u542b\u7b2c <code>2.</code> \u6b65\u548c\u7b2c <code>3.</code> \u6b65\uff0c\u53ea\u662f\u591a\u4e86\u4e00\u500b\u5f48\u51fa\u5143\u7d20\u7684\u6b65\u9a5f\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt;&lt;10&gt;&lt;11&gt;&lt;12&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 11-12 \u00a0 \u5806\u7a4d\u6392\u5e8f\u6b65\u9a5f </p> <p>\u5728\u7a0b\u5f0f\u78bc\u5be6\u73fe\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86\u8207\u201c\u5806\u7a4d\u201d\u7ae0\u7bc0\u76f8\u540c\u7684\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 <code>sift_down()</code> \u51fd\u5f0f\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u65bc\u5806\u7a4d\u7684\u9577\u5ea6\u6703\u96a8\u8457\u63d0\u53d6\u6700\u5927\u5143\u7d20\u800c\u6e1b\u5c0f\uff0c\u56e0\u6b64\u6211\u5011\u9700\u8981\u7d66 <code>sift_down()</code> \u51fd\u5f0f\u65b0\u589e\u4e00\u500b\u9577\u5ea6\u53c3\u6578 \\(n\\) \uff0c\u7528\u65bc\u6307\u5b9a\u5806\u7a4d\u7684\u7576\u524d\u6709\u6548\u9577\u5ea6\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig heap_sort.py<pre><code>def sift_down(nums: list[int], n: int, i: int):\n \"\"\"\u5806\u7a4d\u7684\u9577\u5ea6\u70ba n \uff0c\u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316\"\"\"\n while True:\n # \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n l = 2 * i + 1\n r = 2 * i + 2\n ma = i\n if l &lt; n and nums[l] &gt; nums[ma]:\n ma = l\n if r &lt; n and nums[r] &gt; nums[ma]:\n ma = r\n # \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if ma == i:\n break\n # \u4ea4\u63db\u5169\u7bc0\u9ede\n nums[i], nums[ma] = nums[ma], nums[i]\n # \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma\n\ndef heap_sort(nums: list[int]):\n \"\"\"\u5806\u7a4d\u6392\u5e8f\"\"\"\n # \u5efa\u5806\u7a4d\u64cd\u4f5c\uff1a\u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for i in range(len(nums) // 2 - 1, -1, -1):\n sift_down(nums, len(nums), i)\n # \u5f9e\u5806\u7a4d\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u8ff4\u5708 n-1 \u8f2a\n for i in range(len(nums) - 1, 0, -1):\n # \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n nums[0], nums[i] = nums[i], nums[0]\n # \u4ee5\u6839\u7bc0\u9ede\u70ba\u8d77\u9ede\uff0c\u5f9e\u9802\u81f3\u5e95\u9032\u884c\u5806\u7a4d\u5316\n sift_down(nums, i, 0)\n</code></pre> heap_sort.cpp<pre><code>/* \u5806\u7a4d\u7684\u9577\u5ea6\u70ba n \uff0c\u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nvoid siftDown(vector&lt;int&gt; &amp;nums, int n, int i) {\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l &lt; n &amp;&amp; nums[l] &gt; nums[ma])\n ma = l;\n if (r &lt; n &amp;&amp; nums[r] &gt; nums[ma])\n ma = r;\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma == i) {\n break;\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n swap(nums[i], nums[ma]);\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n\n/* \u5806\u7a4d\u6392\u5e8f */\nvoid heapSort(vector&lt;int&gt; &amp;nums) {\n // \u5efa\u5806\u7a4d\u64cd\u4f5c\uff1a\u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for (int i = nums.size() / 2 - 1; i &gt;= 0; --i) {\n siftDown(nums, nums.size(), i);\n }\n // \u5f9e\u5806\u7a4d\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u8ff4\u5708 n-1 \u8f2a\n for (int i = nums.size() - 1; i &gt; 0; --i) {\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n swap(nums[0], nums[i]);\n // \u4ee5\u6839\u7bc0\u9ede\u70ba\u8d77\u9ede\uff0c\u5f9e\u9802\u81f3\u5e95\u9032\u884c\u5806\u7a4d\u5316\n siftDown(nums, i, 0);\n }\n}\n</code></pre> heap_sort.java<pre><code>/* \u5806\u7a4d\u7684\u9577\u5ea6\u70ba n \uff0c\u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nvoid siftDown(int[] nums, int n, int i) {\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l &lt; n &amp;&amp; nums[l] &gt; nums[ma])\n ma = l;\n if (r &lt; n &amp;&amp; nums[r] &gt; nums[ma])\n ma = r;\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n int temp = nums[i];\n nums[i] = nums[ma];\n nums[ma] = temp;\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n\n/* \u5806\u7a4d\u6392\u5e8f */\nvoid heapSort(int[] nums) {\n // \u5efa\u5806\u7a4d\u64cd\u4f5c\uff1a\u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for (int i = nums.length / 2 - 1; i &gt;= 0; i--) {\n siftDown(nums, nums.length, i);\n }\n // \u5f9e\u5806\u7a4d\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u8ff4\u5708 n-1 \u8f2a\n for (int i = nums.length - 1; i &gt; 0; i--) {\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n int tmp = nums[0];\n nums[0] = nums[i];\n nums[i] = tmp;\n // \u4ee5\u6839\u7bc0\u9ede\u70ba\u8d77\u9ede\uff0c\u5f9e\u9802\u81f3\u5e95\u9032\u884c\u5806\u7a4d\u5316\n siftDown(nums, i, 0);\n }\n}\n</code></pre> heap_sort.cs<pre><code>/* \u5806\u7a4d\u7684\u9577\u5ea6\u70ba n \uff0c\u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nvoid SiftDown(int[] nums, int n, int i) {\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l &lt; n &amp;&amp; nums[l] &gt; nums[ma])\n ma = l;\n if (r &lt; n &amp;&amp; nums[r] &gt; nums[ma])\n ma = r;\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n (nums[ma], nums[i]) = (nums[i], nums[ma]);\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n\n/* \u5806\u7a4d\u6392\u5e8f */\nvoid HeapSort(int[] nums) {\n // \u5efa\u5806\u7a4d\u64cd\u4f5c\uff1a\u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for (int i = nums.Length / 2 - 1; i &gt;= 0; i--) {\n SiftDown(nums, nums.Length, i);\n }\n // \u5f9e\u5806\u7a4d\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u8ff4\u5708 n-1 \u8f2a\n for (int i = nums.Length - 1; i &gt; 0; i--) {\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n (nums[i], nums[0]) = (nums[0], nums[i]);\n // \u4ee5\u6839\u7bc0\u9ede\u70ba\u8d77\u9ede\uff0c\u5f9e\u9802\u81f3\u5e95\u9032\u884c\u5806\u7a4d\u5316\n SiftDown(nums, i, 0);\n }\n}\n</code></pre> heap_sort.go<pre><code>/* \u5806\u7a4d\u7684\u9577\u5ea6\u70ba n \uff0c\u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nfunc siftDown(nums *[]int, n, i int) {\n for true {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n l := 2*i + 1\n r := 2*i + 2\n ma := i\n if l &lt; n &amp;&amp; (*nums)[l] &gt; (*nums)[ma] {\n ma = l\n }\n if r &lt; n &amp;&amp; (*nums)[r] &gt; (*nums)[ma] {\n ma = r\n }\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n (*nums)[i], (*nums)[ma] = (*nums)[ma], (*nums)[i]\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma\n }\n}\n\n/* \u5806\u7a4d\u6392\u5e8f */\nfunc heapSort(nums *[]int) {\n // \u5efa\u5806\u7a4d\u64cd\u4f5c\uff1a\u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for i := len(*nums)/2 - 1; i &gt;= 0; i-- {\n siftDown(nums, len(*nums), i)\n }\n // \u5f9e\u5806\u7a4d\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u8ff4\u5708 n-1 \u8f2a\n for i := len(*nums) - 1; i &gt; 0; i-- {\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n (*nums)[0], (*nums)[i] = (*nums)[i], (*nums)[0]\n // \u4ee5\u6839\u7bc0\u9ede\u70ba\u8d77\u9ede\uff0c\u5f9e\u9802\u81f3\u5e95\u9032\u884c\u5806\u7a4d\u5316\n siftDown(nums, i, 0)\n }\n}\n</code></pre> heap_sort.swift<pre><code>/* \u5806\u7a4d\u7684\u9577\u5ea6\u70ba n \uff0c\u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nfunc siftDown(nums: inout [Int], n: Int, i: Int) {\n var i = i\n while true {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n let l = 2 * i + 1\n let r = 2 * i + 2\n var ma = i\n if l &lt; n, nums[l] &gt; nums[ma] {\n ma = l\n }\n if r &lt; n, nums[r] &gt; nums[ma] {\n ma = r\n }\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n nums.swapAt(i, ma)\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma\n }\n}\n\n/* \u5806\u7a4d\u6392\u5e8f */\nfunc heapSort(nums: inout [Int]) {\n // \u5efa\u5806\u7a4d\u64cd\u4f5c\uff1a\u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for i in stride(from: nums.count / 2 - 1, through: 0, by: -1) {\n siftDown(nums: &amp;nums, n: nums.count, i: i)\n }\n // \u5f9e\u5806\u7a4d\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u8ff4\u5708 n-1 \u8f2a\n for i in nums.indices.dropFirst().reversed() {\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n nums.swapAt(0, i)\n // \u4ee5\u6839\u7bc0\u9ede\u70ba\u8d77\u9ede\uff0c\u5f9e\u9802\u81f3\u5e95\u9032\u884c\u5806\u7a4d\u5316\n siftDown(nums: &amp;nums, n: i, i: 0)\n }\n}\n</code></pre> heap_sort.js<pre><code>/* \u5806\u7a4d\u7684\u9577\u5ea6\u70ba n \uff0c\u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nfunction siftDown(nums, n, i) {\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n let l = 2 * i + 1;\n let r = 2 * i + 2;\n let ma = i;\n if (l &lt; n &amp;&amp; nums[l] &gt; nums[ma]) {\n ma = l;\n }\n if (r &lt; n &amp;&amp; nums[r] &gt; nums[ma]) {\n ma = r;\n }\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma === i) {\n break;\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n [nums[i], nums[ma]] = [nums[ma], nums[i]];\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n\n/* \u5806\u7a4d\u6392\u5e8f */\nfunction heapSort(nums) {\n // \u5efa\u5806\u7a4d\u64cd\u4f5c\uff1a\u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for (let i = Math.floor(nums.length / 2) - 1; i &gt;= 0; i--) {\n siftDown(nums, nums.length, i);\n }\n // \u5f9e\u5806\u7a4d\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u8ff4\u5708 n-1 \u8f2a\n for (let i = nums.length - 1; i &gt; 0; i--) {\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n [nums[0], nums[i]] = [nums[i], nums[0]];\n // \u4ee5\u6839\u7bc0\u9ede\u70ba\u8d77\u9ede\uff0c\u5f9e\u9802\u81f3\u5e95\u9032\u884c\u5806\u7a4d\u5316\n siftDown(nums, i, 0);\n }\n}\n</code></pre> heap_sort.ts<pre><code>/* \u5806\u7a4d\u7684\u9577\u5ea6\u70ba n \uff0c\u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nfunction siftDown(nums: number[], n: number, i: number): void {\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n let l = 2 * i + 1;\n let r = 2 * i + 2;\n let ma = i;\n if (l &lt; n &amp;&amp; nums[l] &gt; nums[ma]) {\n ma = l;\n }\n if (r &lt; n &amp;&amp; nums[r] &gt; nums[ma]) {\n ma = r;\n }\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma === i) {\n break;\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n [nums[i], nums[ma]] = [nums[ma], nums[i]];\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n\n/* \u5806\u7a4d\u6392\u5e8f */\nfunction heapSort(nums: number[]): void {\n // \u5efa\u5806\u7a4d\u64cd\u4f5c\uff1a\u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for (let i = Math.floor(nums.length / 2) - 1; i &gt;= 0; i--) {\n siftDown(nums, nums.length, i);\n }\n // \u5f9e\u5806\u7a4d\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u8ff4\u5708 n-1 \u8f2a\n for (let i = nums.length - 1; i &gt; 0; i--) {\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n [nums[0], nums[i]] = [nums[i], nums[0]];\n // \u4ee5\u6839\u7bc0\u9ede\u70ba\u8d77\u9ede\uff0c\u5f9e\u9802\u81f3\u5e95\u9032\u884c\u5806\u7a4d\u5316\n siftDown(nums, i, 0);\n }\n}\n</code></pre> heap_sort.dart<pre><code>/* \u5806\u7a4d\u7684\u9577\u5ea6\u70ba n \uff0c\u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nvoid siftDown(List&lt;int&gt; nums, int n, int i) {\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l &lt; n &amp;&amp; nums[l] &gt; nums[ma]) ma = l;\n if (r &lt; n &amp;&amp; nums[r] &gt; nums[ma]) ma = r;\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma == i) break;\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n int temp = nums[i];\n nums[i] = nums[ma];\n nums[ma] = temp;\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n\n/* \u5806\u7a4d\u6392\u5e8f */\nvoid heapSort(List&lt;int&gt; nums) {\n // \u5efa\u5806\u7a4d\u64cd\u4f5c\uff1a\u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for (int i = nums.length ~/ 2 - 1; i &gt;= 0; i--) {\n siftDown(nums, nums.length, i);\n }\n // \u5f9e\u5806\u7a4d\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u8ff4\u5708 n-1 \u8f2a\n for (int i = nums.length - 1; i &gt; 0; i--) {\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n int tmp = nums[0];\n nums[0] = nums[i];\n nums[i] = tmp;\n // \u4ee5\u6839\u7bc0\u9ede\u70ba\u8d77\u9ede\uff0c\u5f9e\u9802\u81f3\u5e95\u9032\u884c\u5806\u7a4d\u5316\n siftDown(nums, i, 0);\n }\n}\n</code></pre> heap_sort.rs<pre><code>/* \u5806\u7a4d\u7684\u9577\u5ea6\u70ba n \uff0c\u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nfn sift_down(nums: &amp;mut [i32], n: usize, mut i: usize) {\n loop {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n let l = 2 * i + 1;\n let r = 2 * i + 2;\n let mut ma = i;\n if l &lt; n &amp;&amp; nums[l] &gt; nums[ma] {\n ma = l;\n }\n if r &lt; n &amp;&amp; nums[r] &gt; nums[ma] {\n ma = r;\n }\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break;\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n let temp = nums[i];\n nums[i] = nums[ma];\n nums[ma] = temp;\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n\n/* \u5806\u7a4d\u6392\u5e8f */\nfn heap_sort(nums: &amp;mut [i32]) {\n // \u5efa\u5806\u7a4d\u64cd\u4f5c\uff1a\u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for i in (0..=nums.len() / 2 - 1).rev() {\n sift_down(nums, nums.len(), i);\n }\n // \u5f9e\u5806\u7a4d\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u8ff4\u5708 n-1 \u8f2a\n for i in (1..=nums.len() - 1).rev() {\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n let tmp = nums[0];\n nums[0] = nums[i];\n nums[i] = tmp;\n // \u4ee5\u6839\u7bc0\u9ede\u70ba\u8d77\u9ede\uff0c\u5f9e\u9802\u81f3\u5e95\u9032\u884c\u5806\u7a4d\u5316\n sift_down(nums, i, 0);\n }\n}\n</code></pre> heap_sort.c<pre><code>/* \u5806\u7a4d\u7684\u9577\u5ea6\u70ba n \uff0c\u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nvoid siftDown(int nums[], int n, int i) {\n while (1) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n int l = 2 * i + 1;\n int r = 2 * i + 2;\n int ma = i;\n if (l &lt; n &amp;&amp; nums[l] &gt; nums[ma])\n ma = l;\n if (r &lt; n &amp;&amp; nums[r] &gt; nums[ma])\n ma = r;\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma == i) {\n break;\n }\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n int temp = nums[i];\n nums[i] = nums[ma];\n nums[ma] = temp;\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma;\n }\n}\n\n/* \u5806\u7a4d\u6392\u5e8f */\nvoid heapSort(int nums[], int n) {\n // \u5efa\u5806\u7a4d\u64cd\u4f5c\uff1a\u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for (int i = n / 2 - 1; i &gt;= 0; --i) {\n siftDown(nums, n, i);\n }\n // \u5f9e\u5806\u7a4d\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u8ff4\u5708 n-1 \u8f2a\n for (int i = n - 1; i &gt; 0; --i) {\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n int tmp = nums[0];\n nums[0] = nums[i];\n nums[i] = tmp;\n // \u4ee5\u6839\u7bc0\u9ede\u70ba\u8d77\u9ede\uff0c\u5f9e\u9802\u81f3\u5e95\u9032\u884c\u5806\u7a4d\u5316\n siftDown(nums, i, 0);\n }\n}\n</code></pre> heap_sort.kt<pre><code>/* \u5806\u7a4d\u7684\u9577\u5ea6\u70ba n \uff0c\u5f9e\u7bc0\u9ede i \u958b\u59cb\uff0c\u5f9e\u9802\u81f3\u5e95\u5806\u7a4d\u5316 */\nfun siftDown(nums: IntArray, n: Int, li: Int) {\n var i = li\n while (true) {\n // \u5224\u65b7\u7bc0\u9ede i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7bc0\u9ede\uff0c\u8a18\u70ba ma\n val l = 2 * i + 1\n val r = 2 * i + 2\n var ma = i\n if (l &lt; n &amp;&amp; nums[l] &gt; nums[ma]) \n ma = l\n if (r &lt; n &amp;&amp; nums[r] &gt; nums[ma]) \n ma = r\n // \u82e5\u7bc0\u9ede i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5247\u7121\u9808\u7e7c\u7e8c\u5806\u7a4d\u5316\uff0c\u8df3\u51fa\n if (ma == i) \n break\n // \u4ea4\u63db\u5169\u7bc0\u9ede\n val temp = nums[i]\n nums[i] = nums[ma]\n nums[ma] = temp\n // \u8ff4\u5708\u5411\u4e0b\u5806\u7a4d\u5316\n i = ma\n }\n}\n\n/* \u5806\u7a4d\u6392\u5e8f */\nfun heapSort(nums: IntArray) {\n // \u5efa\u5806\u7a4d\u64cd\u4f5c\uff1a\u5806\u7a4d\u5316\u9664\u8449\u7bc0\u9ede\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\n for (i in nums.size / 2 - 1 downTo 0) {\n siftDown(nums, nums.size, i)\n }\n // \u5f9e\u5806\u7a4d\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u8ff4\u5708 n-1 \u8f2a\n for (i in nums.size - 1 downTo 1) {\n // \u4ea4\u63db\u6839\u7bc0\u9ede\u8207\u6700\u53f3\u8449\u7bc0\u9ede\uff08\u4ea4\u63db\u9996\u5143\u7d20\u8207\u5c3e\u5143\u7d20\uff09\n val temp = nums[0]\n nums[0] = nums[i]\n nums[i] = temp\n // \u4ee5\u6839\u7bc0\u9ede\u70ba\u8d77\u9ede\uff0c\u5f9e\u9802\u81f3\u5e95\u9032\u884c\u5806\u7a4d\u5316\n siftDown(nums, i, 0)\n }\n}\n</code></pre> heap_sort.rb<pre><code>[class]{}-[func]{sift_down}\n\n[class]{}-[func]{heap_sort}\n</code></pre> heap_sort.zig<pre><code>[class]{}-[func]{siftDown}\n\n[class]{}-[func]{heapSort}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_sorting/heap_sort/#1172","title":"11.7.2 \u00a0 \u6f14\u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n \\log n)\\)\u3001\u975e\u81ea\u9069\u61c9\u6392\u5e8f\uff1a\u5efa\u5806\u7a4d\u64cd\u4f5c\u4f7f\u7528 \\(O(n)\\) \u6642\u9593\u3002\u5f9e\u5806\u7a4d\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(\\log n)\\) \uff0c\u5171\u8ff4\u5708 \\(n - 1\\) \u8f2a\u3002</li> <li>\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(1)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u5e7e\u500b\u6307\u6a19\u8b8a\u6578\u4f7f\u7528 \\(O(1)\\) \u7a7a\u9593\u3002\u5143\u7d20\u4ea4\u63db\u548c\u5806\u7a4d\u5316\u64cd\u4f5c\u90fd\u662f\u5728\u539f\u9663\u5217\u4e0a\u9032\u884c\u7684\u3002</li> <li>\u975e\u7a69\u5b9a\u6392\u5e8f\uff1a\u5728\u4ea4\u63db\u5806\u7a4d\u9802\u5143\u7d20\u548c\u5806\u7a4d\u5e95\u5143\u7d20\u6642\uff0c\u76f8\u7b49\u5143\u7d20\u7684\u76f8\u5c0d\u4f4d\u7f6e\u53ef\u80fd\u767c\u751f\u8b8a\u5316\u3002</li> </ul>"},{"location":"chapter_sorting/insertion_sort/","title":"11.4 \u00a0 \u63d2\u5165\u6392\u5e8f","text":"<p>\u63d2\u5165\u6392\u5e8f\uff08insertion sort\uff09\u662f\u4e00\u7a2e\u7c21\u55ae\u7684\u6392\u5e8f\u6f14\u7b97\u6cd5\uff0c\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u8207\u624b\u52d5\u6574\u7406\u4e00\u526f\u724c\u7684\u904e\u7a0b\u975e\u5e38\u76f8\u4f3c\u3002</p> <p>\u5177\u9ad4\u4f86\u8aaa\uff0c\u6211\u5011\u5728\u672a\u6392\u5e8f\u5340\u9593\u9078\u64c7\u4e00\u500b\u57fa\u6e96\u5143\u7d20\uff0c\u5c07\u8a72\u5143\u7d20\u8207\u5176\u5de6\u5074\u5df2\u6392\u5e8f\u5340\u9593\u7684\u5143\u7d20\u9010\u4e00\u6bd4\u8f03\u5927\u5c0f\uff0c\u4e26\u5c07\u8a72\u5143\u7d20\u63d2\u5165\u5230\u6b63\u78ba\u7684\u4f4d\u7f6e\u3002</p> <p>\u5716 11-6 \u5c55\u793a\u4e86\u9663\u5217\u63d2\u5165\u5143\u7d20\u7684\u64cd\u4f5c\u6d41\u7a0b\u3002\u8a2d\u57fa\u6e96\u5143\u7d20\u70ba <code>base</code> \uff0c\u6211\u5011\u9700\u8981\u5c07\u5f9e\u76ee\u6a19\u7d22\u5f15\u5230 <code>base</code> \u4e4b\u9593\u7684\u6240\u6709\u5143\u7d20\u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\uff0c\u7136\u5f8c\u5c07 <code>base</code> \u8ce6\u503c\u7d66\u76ee\u6a19\u7d22\u5f15\u3002</p> <p></p> <p> \u5716 11-6 \u00a0 \u55ae\u6b21\u63d2\u5165\u64cd\u4f5c </p>"},{"location":"chapter_sorting/insertion_sort/#1141","title":"11.4.1 \u00a0 \u6f14\u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u63d2\u5165\u6392\u5e8f\u7684\u6574\u9ad4\u6d41\u7a0b\u5982\u5716 11-7 \u6240\u793a\u3002</p> <ol> <li>\u521d\u59cb\u72c0\u614b\u4e0b\uff0c\u9663\u5217\u7684\u7b2c 1 \u500b\u5143\u7d20\u5df2\u5b8c\u6210\u6392\u5e8f\u3002</li> <li>\u9078\u53d6\u9663\u5217\u7684\u7b2c 2 \u500b\u5143\u7d20\u4f5c\u70ba <code>base</code> \uff0c\u5c07\u5176\u63d2\u5165\u5230\u6b63\u78ba\u4f4d\u7f6e\u5f8c\uff0c\u9663\u5217\u7684\u524d 2 \u500b\u5143\u7d20\u5df2\u6392\u5e8f\u3002</li> <li>\u9078\u53d6\u7b2c 3 \u500b\u5143\u7d20\u4f5c\u70ba <code>base</code> \uff0c\u5c07\u5176\u63d2\u5165\u5230\u6b63\u78ba\u4f4d\u7f6e\u5f8c\uff0c\u9663\u5217\u7684\u524d 3 \u500b\u5143\u7d20\u5df2\u6392\u5e8f\u3002</li> <li>\u4ee5\u6b64\u985e\u63a8\uff0c\u5728\u6700\u5f8c\u4e00\u8f2a\u4e2d\uff0c\u9078\u53d6\u6700\u5f8c\u4e00\u500b\u5143\u7d20\u4f5c\u70ba <code>base</code> \uff0c\u5c07\u5176\u63d2\u5165\u5230\u6b63\u78ba\u4f4d\u7f6e\u5f8c\uff0c\u6240\u6709\u5143\u7d20\u5747\u5df2\u6392\u5e8f\u3002</li> </ol> <p></p> <p> \u5716 11-7 \u00a0 \u63d2\u5165\u6392\u5e8f\u6d41\u7a0b </p> <p>\u793a\u4f8b\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig insertion_sort.py<pre><code>def insertion_sort(nums: list[int]):\n \"\"\"\u63d2\u5165\u6392\u5e8f\"\"\"\n # \u5916\u8ff4\u5708\uff1a\u5df2\u6392\u5e8f\u5340\u9593\u70ba [0, i-1]\n for i in range(1, len(nums)):\n base = nums[i]\n j = i - 1\n # \u5167\u8ff4\u5708\uff1a\u5c07 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593 [0, i-1] \u4e2d\u7684\u6b63\u78ba\u4f4d\u7f6e\n while j &gt;= 0 and nums[j] &gt; base:\n nums[j + 1] = nums[j] # \u5c07 nums[j] \u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n j -= 1\n nums[j + 1] = base # \u5c07 base \u8ce6\u503c\u5230\u6b63\u78ba\u4f4d\u7f6e\n</code></pre> insertion_sort.cpp<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(vector&lt;int&gt; &amp;nums) {\n // \u5916\u8ff4\u5708\uff1a\u5df2\u6392\u5e8f\u5340\u9593\u70ba [0, i-1]\n for (int i = 1; i &lt; nums.size(); i++) {\n int base = nums[i], j = i - 1;\n // \u5167\u8ff4\u5708\uff1a\u5c07 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593 [0, i-1] \u4e2d\u7684\u6b63\u78ba\u4f4d\u7f6e\n while (j &gt;= 0 &amp;&amp; nums[j] &gt; base) {\n nums[j + 1] = nums[j]; // \u5c07 nums[j] \u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c07 base \u8ce6\u503c\u5230\u6b63\u78ba\u4f4d\u7f6e\n }\n}\n</code></pre> insertion_sort.java<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int[] nums) {\n // \u5916\u8ff4\u5708\uff1a\u5df2\u6392\u5e8f\u5340\u9593\u70ba [0, i-1]\n for (int i = 1; i &lt; nums.length; i++) {\n int base = nums[i], j = i - 1;\n // \u5167\u8ff4\u5708\uff1a\u5c07 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593 [0, i-1] \u4e2d\u7684\u6b63\u78ba\u4f4d\u7f6e\n while (j &gt;= 0 &amp;&amp; nums[j] &gt; base) {\n nums[j + 1] = nums[j]; // \u5c07 nums[j] \u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c07 base \u8ce6\u503c\u5230\u6b63\u78ba\u4f4d\u7f6e\n }\n}\n</code></pre> insertion_sort.cs<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nvoid InsertionSort(int[] nums) {\n // \u5916\u8ff4\u5708\uff1a\u5df2\u6392\u5e8f\u5340\u9593\u70ba [0, i-1]\n for (int i = 1; i &lt; nums.Length; i++) {\n int bas = nums[i], j = i - 1;\n // \u5167\u8ff4\u5708\uff1a\u5c07 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593 [0, i-1] \u4e2d\u7684\u6b63\u78ba\u4f4d\u7f6e\n while (j &gt;= 0 &amp;&amp; nums[j] &gt; bas) {\n nums[j + 1] = nums[j]; // \u5c07 nums[j] \u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = bas; // \u5c07 base \u8ce6\u503c\u5230\u6b63\u78ba\u4f4d\u7f6e\n }\n}\n</code></pre> insertion_sort.go<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfunc insertionSort(nums []int) {\n // \u5916\u8ff4\u5708\uff1a\u5df2\u6392\u5e8f\u5340\u9593\u70ba [0, i-1]\n for i := 1; i &lt; len(nums); i++ {\n base := nums[i]\n j := i - 1\n // \u5167\u8ff4\u5708\uff1a\u5c07 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593 [0, i-1] \u4e2d\u7684\u6b63\u78ba\u4f4d\u7f6e\n for j &gt;= 0 &amp;&amp; nums[j] &gt; base {\n nums[j+1] = nums[j] // \u5c07 nums[j] \u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n j--\n }\n nums[j+1] = base // \u5c07 base \u8ce6\u503c\u5230\u6b63\u78ba\u4f4d\u7f6e\n }\n}\n</code></pre> insertion_sort.swift<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfunc insertionSort(nums: inout [Int]) {\n // \u5916\u8ff4\u5708\uff1a\u5df2\u6392\u5e8f\u5340\u9593\u70ba [0, i-1]\n for i in nums.indices.dropFirst() {\n let base = nums[i]\n var j = i - 1\n // \u5167\u8ff4\u5708\uff1a\u5c07 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593 [0, i-1] \u4e2d\u7684\u6b63\u78ba\u4f4d\u7f6e\n while j &gt;= 0, nums[j] &gt; base {\n nums[j + 1] = nums[j] // \u5c07 nums[j] \u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n j -= 1\n }\n nums[j + 1] = base // \u5c07 base \u8ce6\u503c\u5230\u6b63\u78ba\u4f4d\u7f6e\n }\n}\n</code></pre> insertion_sort.js<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfunction insertionSort(nums) {\n // \u5916\u8ff4\u5708\uff1a\u5df2\u6392\u5e8f\u5340\u9593\u70ba [0, i-1]\n for (let i = 1; i &lt; nums.length; i++) {\n let base = nums[i],\n j = i - 1;\n // \u5167\u8ff4\u5708\uff1a\u5c07 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593 [0, i-1] \u4e2d\u7684\u6b63\u78ba\u4f4d\u7f6e\n while (j &gt;= 0 &amp;&amp; nums[j] &gt; base) {\n nums[j + 1] = nums[j]; // \u5c07 nums[j] \u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c07 base \u8ce6\u503c\u5230\u6b63\u78ba\u4f4d\u7f6e\n }\n}\n</code></pre> insertion_sort.ts<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfunction insertionSort(nums: number[]): void {\n // \u5916\u8ff4\u5708\uff1a\u5df2\u6392\u5e8f\u5340\u9593\u70ba [0, i-1]\n for (let i = 1; i &lt; nums.length; i++) {\n const base = nums[i];\n let j = i - 1;\n // \u5167\u8ff4\u5708\uff1a\u5c07 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593 [0, i-1] \u4e2d\u7684\u6b63\u78ba\u4f4d\u7f6e\n while (j &gt;= 0 &amp;&amp; nums[j] &gt; base) {\n nums[j + 1] = nums[j]; // \u5c07 nums[j] \u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c07 base \u8ce6\u503c\u5230\u6b63\u78ba\u4f4d\u7f6e\n }\n}\n</code></pre> insertion_sort.dart<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(List&lt;int&gt; nums) {\n // \u5916\u8ff4\u5708\uff1a\u5df2\u6392\u5e8f\u5340\u9593\u70ba [0, i-1]\n for (int i = 1; i &lt; nums.length; i++) {\n int base = nums[i], j = i - 1;\n // \u5167\u8ff4\u5708\uff1a\u5c07 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593 [0, i-1] \u4e2d\u7684\u6b63\u78ba\u4f4d\u7f6e\n while (j &gt;= 0 &amp;&amp; nums[j] &gt; base) {\n nums[j + 1] = nums[j]; // \u5c07 nums[j] \u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n j--;\n }\n nums[j + 1] = base; // \u5c07 base \u8ce6\u503c\u5230\u6b63\u78ba\u4f4d\u7f6e\n }\n}\n</code></pre> insertion_sort.rs<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfn insertion_sort(nums: &amp;mut [i32]) {\n // \u5916\u8ff4\u5708\uff1a\u5df2\u6392\u5e8f\u5340\u9593\u70ba [0, i-1]\n for i in 1..nums.len() {\n let (base, mut j) = (nums[i], (i - 1) as i32);\n // \u5167\u8ff4\u5708\uff1a\u5c07 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593 [0, i-1] \u4e2d\u7684\u6b63\u78ba\u4f4d\u7f6e\n while j &gt;= 0 &amp;&amp; nums[j as usize] &gt; base {\n nums[(j + 1) as usize] = nums[j as usize]; // \u5c07 nums[j] \u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n j -= 1;\n }\n nums[(j + 1) as usize] = base; // \u5c07 base \u8ce6\u503c\u5230\u6b63\u78ba\u4f4d\u7f6e\n }\n}\n</code></pre> insertion_sort.c<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int nums[], int size) {\n // \u5916\u8ff4\u5708\uff1a\u5df2\u6392\u5e8f\u5340\u9593\u70ba [0, i-1]\n for (int i = 1; i &lt; size; i++) {\n int base = nums[i], j = i - 1;\n // \u5167\u8ff4\u5708\uff1a\u5c07 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593 [0, i-1] \u4e2d\u7684\u6b63\u78ba\u4f4d\u7f6e\n while (j &gt;= 0 &amp;&amp; nums[j] &gt; base) {\n // \u5c07 nums[j] \u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n nums[j + 1] = nums[j];\n j--;\n }\n // \u5c07 base \u8ce6\u503c\u5230\u6b63\u78ba\u4f4d\u7f6e\n nums[j + 1] = base;\n }\n}\n</code></pre> insertion_sort.kt<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfun insertionSort(nums: IntArray) {\n //\u5916\u8ff4\u5708: \u5df2\u6392\u5e8f\u5143\u7d20\u70ba 1, 2, ..., n\n for (i in nums.indices) {\n val base = nums[i]\n var j = i - 1\n // \u5167\u8ff4\u5708\uff1a\u5c07 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593 [0, i-1] \u4e2d\u7684\u6b63\u78ba\u4f4d\u7f6e\n while (j &gt;= 0 &amp;&amp; nums[j] &gt; base) {\n nums[j + 1] = nums[j] // \u5c07 nums[j] \u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n j--\n }\n nums[j + 1] = base // \u5c07 base \u8ce6\u503c\u5230\u6b63\u78ba\u4f4d\u7f6e\n }\n}\n</code></pre> insertion_sort.rb<pre><code>### \u63d2\u5165\u6392\u5e8f ###\ndef insertion_sort(nums)\n n = nums.length\n # \u5916\u8ff4\u5708\uff1a\u5df2\u6392\u5e8f\u5340\u9593\u70ba [0, i-1]\n for i in 1...n\n base = nums[i]\n j = i - 1\n # \u5167\u8ff4\u5708\uff1a\u5c07 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593 [0, i-1] \u4e2d\u7684\u6b63\u78ba\u4f4d\u7f6e\n while j &gt;= 0 &amp;&amp; nums[j] &gt; base\n nums[j + 1] = nums[j] # \u5c07 nums[j] \u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n j -= 1\n end\n nums[j + 1] = base # \u5c07 base \u8ce6\u503c\u5230\u6b63\u78ba\u4f4d\u7f6e\n end\nend\n</code></pre> insertion_sort.zig<pre><code>// \u63d2\u5165\u6392\u5e8f\nfn insertionSort(nums: []i32) void {\n // \u5916\u8ff4\u5708\uff1a\u5df2\u6392\u5e8f\u5340\u9593\u70ba [0, i-1]\n var i: usize = 1;\n while (i &lt; nums.len) : (i += 1) {\n var base = nums[i];\n var j: usize = i;\n // \u5167\u8ff4\u5708\uff1a\u5c07 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593 [0, i-1] \u4e2d\u7684\u6b63\u78ba\u4f4d\u7f6e\n while (j &gt;= 1 and nums[j - 1] &gt; base) : (j -= 1) {\n nums[j] = nums[j - 1]; // \u5c07 nums[j] \u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n }\n nums[j] = base; // \u5c07 base \u8ce6\u503c\u5230\u6b63\u78ba\u4f4d\u7f6e\n }\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_sorting/insertion_sort/#1142","title":"11.4.2 \u00a0 \u6f14\u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\)\u3001\u81ea\u9069\u61c9\u6392\u5e8f\uff1a\u5728\u6700\u5dee\u60c5\u6cc1\u4e0b\uff0c\u6bcf\u6b21\u63d2\u5165\u64cd\u4f5c\u5206\u5225\u9700\u8981\u8ff4\u5708 \\(n - 1\\)\u3001\\(n-2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u6b21\uff0c\u6c42\u548c\u5f97\u5230 \\((n - 1) n / 2\\) \uff0c\u56e0\u6b64\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\) \u3002\u5728\u9047\u5230\u6709\u5e8f\u8cc7\u6599\u6642\uff0c\u63d2\u5165\u64cd\u4f5c\u6703\u63d0\u524d\u7d42\u6b62\u3002\u7576\u8f38\u5165\u9663\u5217\u5b8c\u5168\u6709\u5e8f\u6642\uff0c\u63d2\u5165\u6392\u5e8f\u9054\u5230\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6 \\(O(n)\\) \u3002</li> <li>\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(1)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u6a19 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6578\u5927\u5c0f\u7684\u984d\u5916\u7a7a\u9593\u3002</li> <li>\u7a69\u5b9a\u6392\u5e8f\uff1a\u5728\u63d2\u5165\u64cd\u4f5c\u904e\u7a0b\u4e2d\uff0c\u6211\u5011\u6703\u5c07\u5143\u7d20\u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u5074\uff0c\u4e0d\u6703\u6539\u8b8a\u5b83\u5011\u7684\u9806\u5e8f\u3002</li> </ul>"},{"location":"chapter_sorting/insertion_sort/#1143","title":"11.4.3 \u00a0 \u63d2\u5165\u6392\u5e8f\u7684\u512a\u52e2","text":"<p>\u63d2\u5165\u6392\u5e8f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\) \uff0c\u800c\u6211\u5011\u5373\u5c07\u5b78\u7fd2\u7684\u5feb\u901f\u6392\u5e8f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n \\log n)\\) \u3002\u5118\u7ba1\u63d2\u5165\u6392\u5e8f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u66f4\u9ad8\uff0c\u4f46\u5728\u8cc7\u6599\u91cf\u8f03\u5c0f\u7684\u60c5\u6cc1\u4e0b\uff0c\u63d2\u5165\u6392\u5e8f\u901a\u5e38\u66f4\u5feb\u3002</p> <p>\u9019\u500b\u7d50\u8ad6\u8207\u7dda\u6027\u67e5\u8a62\u548c\u4e8c\u5206\u641c\u5c0b\u7684\u9069\u7528\u60c5\u6cc1\u7684\u7d50\u8ad6\u985e\u4f3c\u3002\u5feb\u901f\u6392\u5e8f\u9019\u985e \\(O(n \\log n)\\) \u7684\u6f14\u7b97\u6cd5\u5c6c\u65bc\u57fa\u65bc\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u6f14\u7b97\u6cd5\uff0c\u5f80\u5f80\u5305\u542b\u66f4\u591a\u55ae\u5143\u8a08\u7b97\u64cd\u4f5c\u3002\u800c\u5728\u8cc7\u6599\u91cf\u8f03\u5c0f\u6642\uff0c\\(n^2\\) \u548c \\(n \\log n\\) \u7684\u6578\u503c\u6bd4\u8f03\u63a5\u8fd1\uff0c\u8907\u96dc\u5ea6\u4e0d\u4f54\u4e3b\u5c0e\u5730\u4f4d\uff0c\u6bcf\u8f2a\u4e2d\u7684\u55ae\u5143\u64cd\u4f5c\u6578\u91cf\u8d77\u5230\u6c7a\u5b9a\u6027\u4f5c\u7528\u3002</p> <p>\u5be6\u969b\u4e0a\uff0c\u8a31\u591a\u7a0b\u5f0f\u8a9e\u8a00\uff08\u4f8b\u5982 Java\uff09\u7684\u5167\u5efa\u6392\u5e8f\u51fd\u5f0f\u63a1\u7528\u4e86\u63d2\u5165\u6392\u5e8f\uff0c\u5927\u81f4\u601d\u8def\u70ba\uff1a\u5c0d\u65bc\u9577\u9663\u5217\uff0c\u63a1\u7528\u57fa\u65bc\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u6f14\u7b97\u6cd5\uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\uff1b\u5c0d\u65bc\u77ed\u9663\u5217\uff0c\u76f4\u63a5\u4f7f\u7528\u63d2\u5165\u6392\u5e8f\u3002</p> <p>\u96d6\u7136\u6ce1\u6cab\u6392\u5e8f\u3001\u9078\u64c7\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u90fd\u70ba \\(O(n^2)\\) \uff0c\u4f46\u5728\u5be6\u969b\u60c5\u6cc1\u4e2d\uff0c\u63d2\u5165\u6392\u5e8f\u7684\u4f7f\u7528\u983b\u7387\u986f\u8457\u9ad8\u65bc\u6ce1\u6cab\u6392\u5e8f\u548c\u9078\u64c7\u6392\u5e8f\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002</p> <ul> <li>\u6ce1\u6cab\u6392\u5e8f\u57fa\u65bc\u5143\u7d20\u4ea4\u63db\u5be6\u73fe\uff0c\u9700\u8981\u85c9\u52a9\u4e00\u500b\u81e8\u6642\u8b8a\u6578\uff0c\u5171\u6d89\u53ca 3 \u500b\u55ae\u5143\u64cd\u4f5c\uff1b\u63d2\u5165\u6392\u5e8f\u57fa\u65bc\u5143\u7d20\u8ce6\u503c\u5be6\u73fe\uff0c\u50c5\u9700 1 \u500b\u55ae\u5143\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u6ce1\u6cab\u6392\u5e8f\u7684\u8a08\u7b97\u958b\u92b7\u901a\u5e38\u6bd4\u63d2\u5165\u6392\u5e8f\u66f4\u9ad8\u3002</li> <li>\u9078\u64c7\u6392\u5e8f\u5728\u4efb\u4f55\u60c5\u6cc1\u4e0b\u7684\u6642\u9593\u8907\u96dc\u5ea6\u90fd\u70ba \\(O(n^2)\\) \u3002\u5982\u679c\u7d66\u5b9a\u4e00\u7d44\u90e8\u5206\u6709\u5e8f\u7684\u8cc7\u6599\uff0c\u63d2\u5165\u6392\u5e8f\u901a\u5e38\u6bd4\u9078\u64c7\u6392\u5e8f\u6548\u7387\u66f4\u9ad8\u3002</li> <li>\u9078\u64c7\u6392\u5e8f\u4e0d\u7a69\u5b9a\uff0c\u7121\u6cd5\u61c9\u7528\u65bc\u591a\u7d1a\u6392\u5e8f\u3002</li> </ul>"},{"location":"chapter_sorting/merge_sort/","title":"11.6 \u00a0 \u5408\u4f75\u6392\u5e8f","text":"<p>\u5408\u4f75\u6392\u5e8f\uff08merge sort\uff09\u662f\u4e00\u7a2e\u57fa\u65bc\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u6f14\u7b97\u6cd5\uff0c\u5305\u542b\u5716 11-10 \u6240\u793a\u7684\u201c\u5283\u5206\u201d\u548c\u201c\u5408\u4f75\u201d\u968e\u6bb5\u3002</p> <ol> <li>\u5283\u5206\u968e\u6bb5\uff1a\u900f\u904e\u905e\u8ff4\u4e0d\u65b7\u5730\u5c07\u9663\u5217\u5f9e\u4e2d\u9ede\u8655\u5206\u958b\uff0c\u5c07\u9577\u9663\u5217\u7684\u6392\u5e8f\u554f\u984c\u8f49\u63db\u70ba\u77ed\u9663\u5217\u7684\u6392\u5e8f\u554f\u984c\u3002</li> <li>\u5408\u4f75\u968e\u6bb5\uff1a\u7576\u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u5283\u5206\uff0c\u958b\u59cb\u5408\u4f75\uff0c\u6301\u7e8c\u5730\u5c07\u5de6\u53f3\u5169\u500b\u8f03\u77ed\u7684\u6709\u5e8f\u9663\u5217\u5408\u4f75\u70ba\u4e00\u500b\u8f03\u9577\u7684\u6709\u5e8f\u9663\u5217\uff0c\u76f4\u81f3\u7d50\u675f\u3002</li> </ol> <p></p> <p> \u5716 11-10 \u00a0 \u5408\u4f75\u6392\u5e8f\u7684\u5283\u5206\u8207\u5408\u4f75\u968e\u6bb5 </p>"},{"location":"chapter_sorting/merge_sort/#1161","title":"11.6.1 \u00a0 \u6f14\u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u5982\u5716 11-11 \u6240\u793a\uff0c\u201c\u5283\u5206\u968e\u6bb5\u201d\u5f9e\u9802\u81f3\u5e95\u905e\u8ff4\u5730\u5c07\u9663\u5217\u5f9e\u4e2d\u9ede\u5207\u5206\u70ba\u5169\u500b\u5b50\u9663\u5217\u3002</p> <ol> <li>\u8a08\u7b97\u9663\u5217\u4e2d\u9ede <code>mid</code> \uff0c\u905e\u8ff4\u5283\u5206\u5de6\u5b50\u9663\u5217\uff08\u5340\u9593 <code>[left, mid]</code> \uff09\u548c\u53f3\u5b50\u9663\u5217\uff08\u5340\u9593 <code>[mid + 1, right]</code> \uff09\u3002</li> <li>\u905e\u8ff4\u57f7\u884c\u6b65\u9a5f <code>1.</code> \uff0c\u76f4\u81f3\u5b50\u9663\u5217\u5340\u9593\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u3002</li> </ol> <p>\u201c\u5408\u4f75\u968e\u6bb5\u201d\u5f9e\u5e95\u81f3\u9802\u5730\u5c07\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u5408\u4f75\u70ba\u4e00\u500b\u6709\u5e8f\u9663\u5217\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f9e\u9577\u5ea6\u70ba 1 \u7684\u5b50\u9663\u5217\u958b\u59cb\u5408\u4f75\uff0c\u5408\u4f75\u968e\u6bb5\u4e2d\u7684\u6bcf\u500b\u5b50\u9663\u5217\u90fd\u662f\u6709\u5e8f\u7684\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt;&lt;10&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 11-11 \u00a0 \u5408\u4f75\u6392\u5e8f\u6b65\u9a5f </p> <p>\u89c0\u5bdf\u767c\u73fe\uff0c\u5408\u4f75\u6392\u5e8f\u8207\u4e8c\u5143\u6a39\u5f8c\u5e8f\u8d70\u8a2a\u7684\u905e\u8ff4\u9806\u5e8f\u662f\u4e00\u81f4\u7684\u3002</p> <ul> <li>\u5f8c\u5e8f\u8d70\u8a2a\uff1a\u5148\u905e\u8ff4\u5de6\u5b50\u6a39\uff0c\u518d\u905e\u8ff4\u53f3\u5b50\u6a39\uff0c\u6700\u5f8c\u8655\u7406\u6839\u7bc0\u9ede\u3002</li> <li>\u5408\u4f75\u6392\u5e8f\uff1a\u5148\u905e\u8ff4\u5de6\u5b50\u9663\u5217\uff0c\u518d\u905e\u8ff4\u53f3\u5b50\u9663\u5217\uff0c\u6700\u5f8c\u8655\u7406\u5408\u4f75\u3002</li> </ul> <p>\u5408\u4f75\u6392\u5e8f\u7684\u5be6\u73fe\u5982\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u6240\u793a\u3002\u8acb\u6ce8\u610f\uff0c<code>nums</code> \u7684\u5f85\u5408\u4f75\u5340\u9593\u70ba <code>[left, right]</code> \uff0c\u800c <code>tmp</code> \u7684\u5c0d\u61c9\u5340\u9593\u70ba <code>[0, right - left]</code> \u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig merge_sort.py<pre><code>def merge(nums: list[int], left: int, mid: int, right: int):\n \"\"\"\u5408\u4f75\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\"\"\"\n # \u5de6\u5b50\u9663\u5217\u5340\u9593\u70ba [left, mid], \u53f3\u5b50\u9663\u5217\u5340\u9593\u70ba [mid+1, right]\n # \u5efa\u7acb\u4e00\u500b\u81e8\u6642\u9663\u5217 tmp \uff0c\u7528\u65bc\u5b58\u653e\u5408\u4f75\u5f8c\u7684\u7d50\u679c\n tmp = [0] * (right - left + 1)\n # \u521d\u59cb\u5316\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u8d77\u59cb\u7d22\u5f15\n i, j, k = left, mid + 1, 0\n # \u7576\u5de6\u53f3\u5b50\u9663\u5217\u90fd\u9084\u6709\u5143\u7d20\u6642\uff0c\u9032\u884c\u6bd4\u8f03\u4e26\u5c07\u8f03\u5c0f\u7684\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while i &lt;= mid and j &lt;= right:\n if nums[i] &lt;= nums[j]:\n tmp[k] = nums[i]\n i += 1\n else:\n tmp[k] = nums[j]\n j += 1\n k += 1\n # \u5c07\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u5269\u9918\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while i &lt;= mid:\n tmp[k] = nums[i]\n i += 1\n k += 1\n while j &lt;= right:\n tmp[k] = nums[j]\n j += 1\n k += 1\n # \u5c07\u81e8\u6642\u9663\u5217 tmp \u4e2d\u7684\u5143\u7d20\u8907\u88fd\u56de\u539f\u9663\u5217 nums \u7684\u5c0d\u61c9\u5340\u9593\n for k in range(0, len(tmp)):\n nums[left + k] = tmp[k]\n\ndef merge_sort(nums: list[int], left: int, right: int):\n \"\"\"\u5408\u4f75\u6392\u5e8f\"\"\"\n # \u7d42\u6b62\u689d\u4ef6\n if left &gt;= right:\n return # \u7576\u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n # \u5283\u5206\u968e\u6bb5\n mid = (left + right) // 2 # \u8a08\u7b97\u4e2d\u9ede\n merge_sort(nums, left, mid) # \u905e\u8ff4\u5de6\u5b50\u9663\u5217\n merge_sort(nums, mid + 1, right) # \u905e\u8ff4\u53f3\u5b50\u9663\u5217\n # \u5408\u4f75\u968e\u6bb5\n merge(nums, left, mid, right)\n</code></pre> merge_sort.cpp<pre><code>/* \u5408\u4f75\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217 */\nvoid merge(vector&lt;int&gt; &amp;nums, int left, int mid, int right) {\n // \u5de6\u5b50\u9663\u5217\u5340\u9593\u70ba [left, mid], \u53f3\u5b50\u9663\u5217\u5340\u9593\u70ba [mid+1, right]\n // \u5efa\u7acb\u4e00\u500b\u81e8\u6642\u9663\u5217 tmp \uff0c\u7528\u65bc\u5b58\u653e\u5408\u4f75\u5f8c\u7684\u7d50\u679c\n vector&lt;int&gt; tmp(right - left + 1);\n // \u521d\u59cb\u5316\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u7576\u5de6\u53f3\u5b50\u9663\u5217\u90fd\u9084\u6709\u5143\u7d20\u6642\uff0c\u9032\u884c\u6bd4\u8f03\u4e26\u5c07\u8f03\u5c0f\u7684\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid &amp;&amp; j &lt;= right) {\n if (nums[i] &lt;= nums[j])\n tmp[k++] = nums[i++];\n else\n tmp[k++] = nums[j++];\n }\n // \u5c07\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u5269\u9918\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid) {\n tmp[k++] = nums[i++];\n }\n while (j &lt;= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c07\u81e8\u6642\u9663\u5217 tmp \u4e2d\u7684\u5143\u7d20\u8907\u88fd\u56de\u539f\u9663\u5217 nums \u7684\u5c0d\u61c9\u5340\u9593\n for (k = 0; k &lt; tmp.size(); k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5408\u4f75\u6392\u5e8f */\nvoid mergeSort(vector&lt;int&gt; &amp;nums, int left, int right) {\n // \u7d42\u6b62\u689d\u4ef6\n if (left &gt;= right)\n return; // \u7576\u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n // \u5283\u5206\u968e\u6bb5\n int mid = (left + right) / 2; // \u8a08\u7b97\u4e2d\u9ede\n mergeSort(nums, left, mid); // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\n mergeSort(nums, mid + 1, right); // \u905e\u8ff4\u53f3\u5b50\u9663\u5217\n // \u5408\u4f75\u968e\u6bb5\n merge(nums, left, mid, right);\n}\n</code></pre> merge_sort.java<pre><code>/* \u5408\u4f75\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217 */\nvoid merge(int[] nums, int left, int mid, int right) {\n // \u5de6\u5b50\u9663\u5217\u5340\u9593\u70ba [left, mid], \u53f3\u5b50\u9663\u5217\u5340\u9593\u70ba [mid+1, right]\n // \u5efa\u7acb\u4e00\u500b\u81e8\u6642\u9663\u5217 tmp \uff0c\u7528\u65bc\u5b58\u653e\u5408\u4f75\u5f8c\u7684\u7d50\u679c\n int[] tmp = new int[right - left + 1];\n // \u521d\u59cb\u5316\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u7576\u5de6\u53f3\u5b50\u9663\u5217\u90fd\u9084\u6709\u5143\u7d20\u6642\uff0c\u9032\u884c\u6bd4\u8f03\u4e26\u5c07\u8f03\u5c0f\u7684\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid &amp;&amp; j &lt;= right) {\n if (nums[i] &lt;= nums[j])\n tmp[k++] = nums[i++];\n else\n tmp[k++] = nums[j++];\n }\n // \u5c07\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u5269\u9918\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid) {\n tmp[k++] = nums[i++];\n }\n while (j &lt;= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c07\u81e8\u6642\u9663\u5217 tmp \u4e2d\u7684\u5143\u7d20\u8907\u88fd\u56de\u539f\u9663\u5217 nums \u7684\u5c0d\u61c9\u5340\u9593\n for (k = 0; k &lt; tmp.length; k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5408\u4f75\u6392\u5e8f */\nvoid mergeSort(int[] nums, int left, int right) {\n // \u7d42\u6b62\u689d\u4ef6\n if (left &gt;= right)\n return; // \u7576\u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n // \u5283\u5206\u968e\u6bb5\n int mid = (left + right) / 2; // \u8a08\u7b97\u4e2d\u9ede\n mergeSort(nums, left, mid); // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\n mergeSort(nums, mid + 1, right); // \u905e\u8ff4\u53f3\u5b50\u9663\u5217\n // \u5408\u4f75\u968e\u6bb5\n merge(nums, left, mid, right);\n}\n</code></pre> merge_sort.cs<pre><code>/* \u5408\u4f75\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217 */\nvoid Merge(int[] nums, int left, int mid, int right) {\n // \u5de6\u5b50\u9663\u5217\u5340\u9593\u70ba [left, mid], \u53f3\u5b50\u9663\u5217\u5340\u9593\u70ba [mid+1, right]\n // \u5efa\u7acb\u4e00\u500b\u81e8\u6642\u9663\u5217 tmp \uff0c\u7528\u65bc\u5b58\u653e\u5408\u4f75\u5f8c\u7684\u7d50\u679c\n int[] tmp = new int[right - left + 1];\n // \u521d\u59cb\u5316\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u7576\u5de6\u53f3\u5b50\u9663\u5217\u90fd\u9084\u6709\u5143\u7d20\u6642\uff0c\u9032\u884c\u6bd4\u8f03\u4e26\u5c07\u8f03\u5c0f\u7684\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid &amp;&amp; j &lt;= right) {\n if (nums[i] &lt;= nums[j])\n tmp[k++] = nums[i++];\n else\n tmp[k++] = nums[j++];\n }\n // \u5c07\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u5269\u9918\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid) {\n tmp[k++] = nums[i++];\n }\n while (j &lt;= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c07\u81e8\u6642\u9663\u5217 tmp \u4e2d\u7684\u5143\u7d20\u8907\u88fd\u56de\u539f\u9663\u5217 nums \u7684\u5c0d\u61c9\u5340\u9593\n for (k = 0; k &lt; tmp.Length; ++k) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5408\u4f75\u6392\u5e8f */\nvoid MergeSort(int[] nums, int left, int right) {\n // \u7d42\u6b62\u689d\u4ef6\n if (left &gt;= right) return; // \u7576\u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n // \u5283\u5206\u968e\u6bb5\n int mid = (left + right) / 2; // \u8a08\u7b97\u4e2d\u9ede\n MergeSort(nums, left, mid); // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\n MergeSort(nums, mid + 1, right); // \u905e\u8ff4\u53f3\u5b50\u9663\u5217\n // \u5408\u4f75\u968e\u6bb5\n Merge(nums, left, mid, right);\n}\n</code></pre> merge_sort.go<pre><code>/* \u5408\u4f75\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217 */\nfunc merge(nums []int, left, mid, right int) {\n // \u5de6\u5b50\u9663\u5217\u5340\u9593\u70ba [left, mid], \u53f3\u5b50\u9663\u5217\u5340\u9593\u70ba [mid+1, right]\n // \u5efa\u7acb\u4e00\u500b\u81e8\u6642\u9663\u5217 tmp \uff0c\u7528\u65bc\u5b58\u653e\u5408\u4f75\u5f8c\u7684\u7d50\u679c\n tmp := make([]int, right-left+1)\n // \u521d\u59cb\u5316\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u8d77\u59cb\u7d22\u5f15\n i, j, k := left, mid+1, 0\n // \u7576\u5de6\u53f3\u5b50\u9663\u5217\u90fd\u9084\u6709\u5143\u7d20\u6642\uff0c\u9032\u884c\u6bd4\u8f03\u4e26\u5c07\u8f03\u5c0f\u7684\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n for i &lt;= mid &amp;&amp; j &lt;= right {\n if nums[i] &lt;= nums[j] {\n tmp[k] = nums[i]\n i++\n } else {\n tmp[k] = nums[j]\n j++\n }\n k++\n }\n // \u5c07\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u5269\u9918\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n for i &lt;= mid {\n tmp[k] = nums[i]\n i++\n k++\n }\n for j &lt;= right {\n tmp[k] = nums[j]\n j++\n k++\n }\n // \u5c07\u81e8\u6642\u9663\u5217 tmp \u4e2d\u7684\u5143\u7d20\u8907\u88fd\u56de\u539f\u9663\u5217 nums \u7684\u5c0d\u61c9\u5340\u9593\n for k := 0; k &lt; len(tmp); k++ {\n nums[left+k] = tmp[k]\n }\n}\n\n/* \u5408\u4f75\u6392\u5e8f */\nfunc mergeSort(nums []int, left, right int) {\n // \u7d42\u6b62\u689d\u4ef6\n if left &gt;= right {\n return\n }\n // \u5283\u5206\u968e\u6bb5\n mid := (left + right) / 2\n mergeSort(nums, left, mid)\n mergeSort(nums, mid+1, right)\n // \u5408\u4f75\u968e\u6bb5\n merge(nums, left, mid, right)\n}\n</code></pre> merge_sort.swift<pre><code>/* \u5408\u4f75\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217 */\nfunc merge(nums: inout [Int], left: Int, mid: Int, right: Int) {\n // \u5de6\u5b50\u9663\u5217\u5340\u9593\u70ba [left, mid], \u53f3\u5b50\u9663\u5217\u5340\u9593\u70ba [mid+1, right]\n // \u5efa\u7acb\u4e00\u500b\u81e8\u6642\u9663\u5217 tmp \uff0c\u7528\u65bc\u5b58\u653e\u5408\u4f75\u5f8c\u7684\u7d50\u679c\n var tmp = Array(repeating: 0, count: right - left + 1)\n // \u521d\u59cb\u5316\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u8d77\u59cb\u7d22\u5f15\n var i = left, j = mid + 1, k = 0\n // \u7576\u5de6\u53f3\u5b50\u9663\u5217\u90fd\u9084\u6709\u5143\u7d20\u6642\uff0c\u9032\u884c\u6bd4\u8f03\u4e26\u5c07\u8f03\u5c0f\u7684\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while i &lt;= mid, j &lt;= right {\n if nums[i] &lt;= nums[j] {\n tmp[k] = nums[i]\n i += 1\n } else {\n tmp[k] = nums[j]\n j += 1\n }\n k += 1\n }\n // \u5c07\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u5269\u9918\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while i &lt;= mid {\n tmp[k] = nums[i]\n i += 1\n k += 1\n }\n while j &lt;= right {\n tmp[k] = nums[j]\n j += 1\n k += 1\n }\n // \u5c07\u81e8\u6642\u9663\u5217 tmp \u4e2d\u7684\u5143\u7d20\u8907\u88fd\u56de\u539f\u9663\u5217 nums \u7684\u5c0d\u61c9\u5340\u9593\n for k in tmp.indices {\n nums[left + k] = tmp[k]\n }\n}\n\n/* \u5408\u4f75\u6392\u5e8f */\nfunc mergeSort(nums: inout [Int], left: Int, right: Int) {\n // \u7d42\u6b62\u689d\u4ef6\n if left &gt;= right { // \u7576\u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n return\n }\n // \u5283\u5206\u968e\u6bb5\n let mid = (left + right) / 2 // \u8a08\u7b97\u4e2d\u9ede\n mergeSort(nums: &amp;nums, left: left, right: mid) // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\n mergeSort(nums: &amp;nums, left: mid + 1, right: right) // \u905e\u8ff4\u53f3\u5b50\u9663\u5217\n // \u5408\u4f75\u968e\u6bb5\n merge(nums: &amp;nums, left: left, mid: mid, right: right)\n}\n</code></pre> merge_sort.js<pre><code>/* \u5408\u4f75\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217 */\nfunction merge(nums, left, mid, right) {\n // \u5de6\u5b50\u9663\u5217\u5340\u9593\u70ba [left, mid], \u53f3\u5b50\u9663\u5217\u5340\u9593\u70ba [mid+1, right]\n // \u5efa\u7acb\u4e00\u500b\u81e8\u6642\u9663\u5217 tmp \uff0c\u7528\u65bc\u5b58\u653e\u5408\u4f75\u5f8c\u7684\u7d50\u679c\n const tmp = new Array(right - left + 1);\n // \u521d\u59cb\u5316\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u8d77\u59cb\u7d22\u5f15\n let i = left,\n j = mid + 1,\n k = 0;\n // \u7576\u5de6\u53f3\u5b50\u9663\u5217\u90fd\u9084\u6709\u5143\u7d20\u6642\uff0c\u9032\u884c\u6bd4\u8f03\u4e26\u5c07\u8f03\u5c0f\u7684\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid &amp;&amp; j &lt;= right) {\n if (nums[i] &lt;= nums[j]) {\n tmp[k++] = nums[i++];\n } else {\n tmp[k++] = nums[j++];\n }\n }\n // \u5c07\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u5269\u9918\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid) {\n tmp[k++] = nums[i++];\n }\n while (j &lt;= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c07\u81e8\u6642\u9663\u5217 tmp \u4e2d\u7684\u5143\u7d20\u8907\u88fd\u56de\u539f\u9663\u5217 nums \u7684\u5c0d\u61c9\u5340\u9593\n for (k = 0; k &lt; tmp.length; k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5408\u4f75\u6392\u5e8f */\nfunction mergeSort(nums, left, right) {\n // \u7d42\u6b62\u689d\u4ef6\n if (left &gt;= right) return; // \u7576\u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n // \u5283\u5206\u968e\u6bb5\n let mid = Math.floor((left + right) / 2); // \u8a08\u7b97\u4e2d\u9ede\n mergeSort(nums, left, mid); // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\n mergeSort(nums, mid + 1, right); // \u905e\u8ff4\u53f3\u5b50\u9663\u5217\n // \u5408\u4f75\u968e\u6bb5\n merge(nums, left, mid, right);\n}\n</code></pre> merge_sort.ts<pre><code>/* \u5408\u4f75\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217 */\nfunction merge(nums: number[], left: number, mid: number, right: number): void {\n // \u5de6\u5b50\u9663\u5217\u5340\u9593\u70ba [left, mid], \u53f3\u5b50\u9663\u5217\u5340\u9593\u70ba [mid+1, right]\n // \u5efa\u7acb\u4e00\u500b\u81e8\u6642\u9663\u5217 tmp \uff0c\u7528\u65bc\u5b58\u653e\u5408\u4f75\u5f8c\u7684\u7d50\u679c\n const tmp = new Array(right - left + 1);\n // \u521d\u59cb\u5316\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u8d77\u59cb\u7d22\u5f15\n let i = left,\n j = mid + 1,\n k = 0;\n // \u7576\u5de6\u53f3\u5b50\u9663\u5217\u90fd\u9084\u6709\u5143\u7d20\u6642\uff0c\u9032\u884c\u6bd4\u8f03\u4e26\u5c07\u8f03\u5c0f\u7684\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid &amp;&amp; j &lt;= right) {\n if (nums[i] &lt;= nums[j]) {\n tmp[k++] = nums[i++];\n } else {\n tmp[k++] = nums[j++];\n }\n }\n // \u5c07\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u5269\u9918\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid) {\n tmp[k++] = nums[i++];\n }\n while (j &lt;= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c07\u81e8\u6642\u9663\u5217 tmp \u4e2d\u7684\u5143\u7d20\u8907\u88fd\u56de\u539f\u9663\u5217 nums \u7684\u5c0d\u61c9\u5340\u9593\n for (k = 0; k &lt; tmp.length; k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5408\u4f75\u6392\u5e8f */\nfunction mergeSort(nums: number[], left: number, right: number): void {\n // \u7d42\u6b62\u689d\u4ef6\n if (left &gt;= right) return; // \u7576\u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n // \u5283\u5206\u968e\u6bb5\n let mid = Math.floor((left + right) / 2); // \u8a08\u7b97\u4e2d\u9ede\n mergeSort(nums, left, mid); // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\n mergeSort(nums, mid + 1, right); // \u905e\u8ff4\u53f3\u5b50\u9663\u5217\n // \u5408\u4f75\u968e\u6bb5\n merge(nums, left, mid, right);\n}\n</code></pre> merge_sort.dart<pre><code>/* \u5408\u4f75\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217 */\nvoid merge(List&lt;int&gt; nums, int left, int mid, int right) {\n // \u5de6\u5b50\u9663\u5217\u5340\u9593\u70ba [left, mid], \u53f3\u5b50\u9663\u5217\u5340\u9593\u70ba [mid+1, right]\n // \u5efa\u7acb\u4e00\u500b\u81e8\u6642\u9663\u5217 tmp \uff0c\u7528\u65bc\u5b58\u653e\u5408\u4f75\u5f8c\u7684\u7d50\u679c\n List&lt;int&gt; tmp = List.filled(right - left + 1, 0);\n // \u521d\u59cb\u5316\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u7576\u5de6\u53f3\u5b50\u9663\u5217\u90fd\u9084\u6709\u5143\u7d20\u6642\uff0c\u9032\u884c\u6bd4\u8f03\u4e26\u5c07\u8f03\u5c0f\u7684\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid &amp;&amp; j &lt;= right) {\n if (nums[i] &lt;= nums[j])\n tmp[k++] = nums[i++];\n else\n tmp[k++] = nums[j++];\n }\n // \u5c07\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u5269\u9918\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid) {\n tmp[k++] = nums[i++];\n }\n while (j &lt;= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c07\u81e8\u6642\u9663\u5217 tmp \u4e2d\u7684\u5143\u7d20\u8907\u88fd\u56de\u539f\u9663\u5217 nums \u7684\u5c0d\u61c9\u5340\u9593\n for (k = 0; k &lt; tmp.length; k++) {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5408\u4f75\u6392\u5e8f */\nvoid mergeSort(List&lt;int&gt; nums, int left, int right) {\n // \u7d42\u6b62\u689d\u4ef6\n if (left &gt;= right) return; // \u7576\u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n // \u5283\u5206\u968e\u6bb5\n int mid = (left + right) ~/ 2; // \u8a08\u7b97\u4e2d\u9ede\n mergeSort(nums, left, mid); // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\n mergeSort(nums, mid + 1, right); // \u905e\u8ff4\u53f3\u5b50\u9663\u5217\n // \u5408\u4f75\u968e\u6bb5\n merge(nums, left, mid, right);\n}\n</code></pre> merge_sort.rs<pre><code>/* \u5408\u4f75\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217 */\nfn merge(nums: &amp;mut [i32], left: usize, mid: usize, right: usize) {\n // \u5de6\u5b50\u9663\u5217\u5340\u9593\u70ba [left, mid], \u53f3\u5b50\u9663\u5217\u5340\u9593\u70ba [mid+1, right]\n // \u5efa\u7acb\u4e00\u500b\u81e8\u6642\u9663\u5217 tmp \uff0c\u7528\u65bc\u5b58\u653e\u5408\u4f75\u5f8c\u7684\u7d50\u679c\n let tmp_size = right - left + 1;\n let mut tmp = vec![0; tmp_size];\n // \u521d\u59cb\u5316\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u8d77\u59cb\u7d22\u5f15\n let (mut i, mut j, mut k) = (left, mid + 1, 0);\n // \u7576\u5de6\u53f3\u5b50\u9663\u5217\u90fd\u9084\u6709\u5143\u7d20\u6642\uff0c\u9032\u884c\u6bd4\u8f03\u4e26\u5c07\u8f03\u5c0f\u7684\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while i &lt;= mid &amp;&amp; j &lt;= right {\n if nums[i] &lt;= nums[j] {\n tmp[k] = nums[i];\n i += 1;\n } else {\n tmp[k] = nums[j];\n j += 1;\n }\n k += 1;\n }\n // \u5c07\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u5269\u9918\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while i &lt;= mid {\n tmp[k] = nums[i];\n k += 1;\n i += 1;\n }\n while j &lt;= right {\n tmp[k] = nums[j];\n k += 1;\n j += 1;\n }\n // \u5c07\u81e8\u6642\u9663\u5217 tmp \u4e2d\u7684\u5143\u7d20\u8907\u88fd\u56de\u539f\u9663\u5217 nums \u7684\u5c0d\u61c9\u5340\u9593\n for k in 0..tmp_size {\n nums[left + k] = tmp[k];\n }\n}\n\n/* \u5408\u4f75\u6392\u5e8f */\nfn merge_sort(nums: &amp;mut [i32], left: usize, right: usize) {\n // \u7d42\u6b62\u689d\u4ef6\n if left &gt;= right {\n return; // \u7576\u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n }\n\n // \u5283\u5206\u968e\u6bb5\n let mid = (left + right) / 2; // \u8a08\u7b97\u4e2d\u9ede\n merge_sort(nums, left, mid); // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\n merge_sort(nums, mid + 1, right); // \u905e\u8ff4\u53f3\u5b50\u9663\u5217\n\n // \u5408\u4f75\u968e\u6bb5\n merge(nums, left, mid, right);\n}\n</code></pre> merge_sort.c<pre><code>/* \u5408\u4f75\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217 */\nvoid merge(int *nums, int left, int mid, int right) {\n // \u5de6\u5b50\u9663\u5217\u5340\u9593\u70ba [left, mid], \u53f3\u5b50\u9663\u5217\u5340\u9593\u70ba [mid+1, right]\n // \u5efa\u7acb\u4e00\u500b\u81e8\u6642\u9663\u5217 tmp \uff0c\u7528\u65bc\u5b58\u653e\u5408\u4f75\u5f8c\u7684\u7d50\u679c\n int tmpSize = right - left + 1;\n int *tmp = (int *)malloc(tmpSize * sizeof(int));\n // \u521d\u59cb\u5316\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u8d77\u59cb\u7d22\u5f15\n int i = left, j = mid + 1, k = 0;\n // \u7576\u5de6\u53f3\u5b50\u9663\u5217\u90fd\u9084\u6709\u5143\u7d20\u6642\uff0c\u9032\u884c\u6bd4\u8f03\u4e26\u5c07\u8f03\u5c0f\u7684\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid &amp;&amp; j &lt;= right) {\n if (nums[i] &lt;= nums[j]) {\n tmp[k++] = nums[i++];\n } else {\n tmp[k++] = nums[j++];\n }\n }\n // \u5c07\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u5269\u9918\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid) {\n tmp[k++] = nums[i++];\n }\n while (j &lt;= right) {\n tmp[k++] = nums[j++];\n }\n // \u5c07\u81e8\u6642\u9663\u5217 tmp \u4e2d\u7684\u5143\u7d20\u8907\u88fd\u56de\u539f\u9663\u5217 nums \u7684\u5c0d\u61c9\u5340\u9593\n for (k = 0; k &lt; tmpSize; ++k) {\n nums[left + k] = tmp[k];\n }\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(tmp);\n}\n\n/* \u5408\u4f75\u6392\u5e8f */\nvoid mergeSort(int *nums, int left, int right) {\n // \u7d42\u6b62\u689d\u4ef6\n if (left &gt;= right)\n return; // \u7576\u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n // \u5283\u5206\u968e\u6bb5\n int mid = (left + right) / 2; // \u8a08\u7b97\u4e2d\u9ede\n mergeSort(nums, left, mid); // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\n mergeSort(nums, mid + 1, right); // \u905e\u8ff4\u53f3\u5b50\u9663\u5217\n // \u5408\u4f75\u968e\u6bb5\n merge(nums, left, mid, right);\n}\n</code></pre> merge_sort.kt<pre><code>/* \u5408\u4f75\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217 */\nfun merge(nums: IntArray, left: Int, mid: Int, right: Int) {\n // \u5de6\u5b50\u9663\u5217\u5340\u9593\u70ba [left, mid], \u53f3\u5b50\u9663\u5217\u5340\u9593\u70ba [mid+1, right]\n // \u5efa\u7acb\u4e00\u500b\u81e8\u6642\u9663\u5217 tmp \uff0c\u7528\u65bc\u5b58\u653e\u5408\u4f75\u5f8c\u7684\u7d50\u679c\n val tmp = IntArray(right - left + 1)\n // \u521d\u59cb\u5316\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u8d77\u59cb\u7d22\u5f15\n var i = left\n var j = mid + 1\n var k = 0\n // \u7576\u5de6\u53f3\u5b50\u9663\u5217\u90fd\u9084\u6709\u5143\u7d20\u6642\uff0c\u9032\u884c\u6bd4\u8f03\u4e26\u5c07\u8f03\u5c0f\u7684\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid &amp;&amp; j &lt;= right) {\n if (nums[i] &lt;= nums[j])\n tmp[k++] = nums[i++]\n else \n tmp[k++] = nums[j++]\n }\n // \u5c07\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u7684\u5269\u9918\u5143\u7d20\u8907\u88fd\u5230\u81e8\u6642\u9663\u5217\u4e2d\n while (i &lt;= mid) {\n tmp[k++] = nums[i++]\n }\n while (j &lt;= right) {\n tmp[k++] = nums[j++]\n }\n // \u5c07\u81e8\u6642\u9663\u5217 tmp \u4e2d\u7684\u5143\u7d20\u8907\u88fd\u56de\u539f\u9663\u5217 nums \u7684\u5c0d\u61c9\u5340\u9593\n for (l in tmp.indices) {\n nums[left + l] = tmp[l]\n }\n}\n\n/* \u5408\u4f75\u6392\u5e8f */\nfun mergeSort(nums: IntArray, left: Int, right: Int) {\n // \u7d42\u6b62\u689d\u4ef6\n if (left &gt;= right) return // \u7576\u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n // \u5283\u5206\u968e\u6bb5\n val mid = (left + right) / 2 // \u8a08\u7b97\u4e2d\u9ede\n mergeSort(nums, left, mid) // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\n mergeSort(nums, mid + 1, right) // \u905e\u8ff4\u53f3\u5b50\u9663\u5217\n // \u5408\u4f75\u968e\u6bb5\n merge(nums, left, mid, right)\n}\n</code></pre> merge_sort.rb<pre><code>[class]{}-[func]{merge}\n\n[class]{}-[func]{merge_sort}\n</code></pre> merge_sort.zig<pre><code>// \u5408\u4f75\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\n// \u5de6\u5b50\u9663\u5217\u5340\u9593 [left, mid]\n// \u53f3\u5b50\u9663\u5217\u5340\u9593 [mid + 1, right]\nfn merge(nums: []i32, left: usize, mid: usize, right: usize) !void {\n // \u521d\u59cb\u5316\u8f14\u52a9\u9663\u5217\n var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);\n defer mem_arena.deinit();\n const mem_allocator = mem_arena.allocator();\n var tmp = try mem_allocator.alloc(i32, right + 1 - left);\n std.mem.copy(i32, tmp, nums[left..right+1]);\n // \u5de6\u5b50\u9663\u5217\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7d50\u675f\u7d22\u5f15 \n var leftStart = left - left;\n var leftEnd = mid - left;\n // \u53f3\u5b50\u9663\u5217\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7d50\u675f\u7d22\u5f15 \n var rightStart = mid + 1 - left;\n var rightEnd = right - left;\n // i, j \u5206\u5225\u6307\u5411\u5de6\u5b50\u9663\u5217\u3001\u53f3\u5b50\u9663\u5217\u7684\u9996\u5143\u7d20\n var i = leftStart;\n var j = rightStart;\n // \u900f\u904e\u8986\u84cb\u539f\u9663\u5217 nums \u4f86\u5408\u4f75\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\n var k = left;\n while (k &lt;= right) : (k += 1) {\n // \u82e5\u201c\u5de6\u5b50\u9663\u5217\u5df2\u5168\u90e8\u5408\u4f75\u5b8c\u201d\uff0c\u5247\u9078\u53d6\u53f3\u5b50\u9663\u5217\u5143\u7d20\uff0c\u4e26\u4e14 j++\n if (i &gt; leftEnd) {\n nums[k] = tmp[j];\n j += 1;\n // \u5426\u5247\uff0c\u82e5\u201c\u53f3\u5b50\u9663\u5217\u5df2\u5168\u90e8\u5408\u4f75\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u9663\u5217\u5143\u7d20 &lt;= \u53f3\u5b50\u9663\u5217\u5143\u7d20\u201d\uff0c\u5247\u9078\u53d6\u5de6\u5b50\u9663\u5217\u5143\u7d20\uff0c\u4e26\u4e14 i++\n } else if (j &gt; rightEnd or tmp[i] &lt;= tmp[j]) {\n nums[k] = tmp[i];\n i += 1;\n // \u5426\u5247\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u9663\u5217\u90fd\u672a\u5168\u90e8\u5408\u4f75\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u9663\u5217\u5143\u7d20 &gt; \u53f3\u5b50\u9663\u5217\u5143\u7d20\u201d\uff0c\u5247\u9078\u53d6\u53f3\u5b50\u9663\u5217\u5143\u7d20\uff0c\u4e26\u4e14 j++\n } else {\n nums[k] = tmp[j];\n j += 1;\n }\n }\n}\n\n// \u5408\u4f75\u6392\u5e8f\nfn mergeSort(nums: []i32, left: usize, right: usize) !void {\n // \u7d42\u6b62\u689d\u4ef6\n if (left &gt;= right) return; // \u7576\u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n // \u5283\u5206\u968e\u6bb5\n var mid = (left + right) / 2; // \u8a08\u7b97\u4e2d\u9ede\n try mergeSort(nums, left, mid); // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\n try mergeSort(nums, mid + 1, right); // \u905e\u8ff4\u53f3\u5b50\u9663\u5217\n // \u5408\u4f75\u968e\u6bb5\n try merge(nums, left, mid, right);\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_sorting/merge_sort/#1162","title":"11.6.2 \u00a0 \u6f14\u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n \\log n)\\)\u3001\u975e\u81ea\u9069\u61c9\u6392\u5e8f\uff1a\u5283\u5206\u7522\u751f\u9ad8\u5ea6\u70ba \\(\\log n\\) \u7684\u905e\u8ff4\u6a39\uff0c\u6bcf\u5c64\u5408\u4f75\u7684\u7e3d\u64cd\u4f5c\u6578\u91cf\u70ba \\(n\\) \uff0c\u56e0\u6b64\u7e3d\u9ad4\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n \\log n)\\) \u3002</li> <li>\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\)\u3001\u975e\u539f\u5730\u6392\u5e8f\uff1a\u905e\u8ff4\u6df1\u5ea6\u70ba \\(\\log n\\) \uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u5927\u5c0f\u7684\u5806\u758a\u5e40\u7a7a\u9593\u3002\u5408\u4f75\u64cd\u4f5c\u9700\u8981\u85c9\u52a9\u8f14\u52a9\u9663\u5217\u5be6\u73fe\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u984d\u5916\u7a7a\u9593\u3002</li> <li>\u7a69\u5b9a\u6392\u5e8f\uff1a\u5728\u5408\u4f75\u904e\u7a0b\u4e2d\uff0c\u76f8\u7b49\u5143\u7d20\u7684\u6b21\u5e8f\u4fdd\u6301\u4e0d\u8b8a\u3002</li> </ul>"},{"location":"chapter_sorting/merge_sort/#1163","title":"11.6.3 \u00a0 \u93c8\u7d50\u4e32\u5217\u6392\u5e8f","text":"<p>\u5c0d\u65bc\u93c8\u7d50\u4e32\u5217\uff0c\u5408\u4f75\u6392\u5e8f\u76f8\u8f03\u65bc\u5176\u4ed6\u6392\u5e8f\u6f14\u7b97\u6cd5\u5177\u6709\u986f\u8457\u512a\u52e2\uff0c\u53ef\u4ee5\u5c07\u93c8\u7d50\u4e32\u5217\u6392\u5e8f\u4efb\u52d9\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u6700\u4f73\u5316\u81f3 \\(O(1)\\) \u3002</p> <ul> <li>\u5283\u5206\u968e\u6bb5\uff1a\u53ef\u4ee5\u4f7f\u7528\u201c\u8fed\u4ee3\u201d\u66ff\u4ee3\u201c\u905e\u8ff4\u201d\u4f86\u5be6\u73fe\u93c8\u7d50\u4e32\u5217\u5283\u5206\u5de5\u4f5c\uff0c\u5f9e\u800c\u7701\u53bb\u905e\u8ff4\u4f7f\u7528\u7684\u5806\u758a\u5e40\u7a7a\u9593\u3002</li> <li>\u5408\u4f75\u968e\u6bb5\uff1a\u5728\u93c8\u7d50\u4e32\u5217\u4e2d\uff0c\u7bc0\u9ede\u589e\u522a\u64cd\u4f5c\u50c5\u9700\u6539\u8b8a\u5f15\u7528\uff08\u6307\u6a19\uff09\u5373\u53ef\u5be6\u73fe\uff0c\u56e0\u6b64\u5408\u4f75\u968e\u6bb5\uff08\u5c07\u5169\u500b\u77ed\u6709\u5e8f\u93c8\u7d50\u4e32\u5217\u5408\u4f75\u70ba\u4e00\u500b\u9577\u6709\u5e8f\u93c8\u7d50\u4e32\u5217\uff09\u7121\u9808\u5efa\u7acb\u984d\u5916\u93c8\u7d50\u4e32\u5217\u3002</li> </ul> <p>\u5177\u9ad4\u5be6\u73fe\u7d30\u7bc0\u6bd4\u8f03\u8907\u96dc\uff0c\u6709\u8208\u8da3\u7684\u8b80\u8005\u53ef\u4ee5\u67e5\u95b1\u76f8\u95dc\u8cc7\u6599\u9032\u884c\u5b78\u7fd2\u3002</p>"},{"location":"chapter_sorting/quick_sort/","title":"11.5 \u00a0 \u5feb\u901f\u6392\u5e8f","text":"<p>\u5feb\u901f\u6392\u5e8f\uff08quick sort\uff09\u662f\u4e00\u7a2e\u57fa\u65bc\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u6f14\u7b97\u6cd5\uff0c\u57f7\u884c\u9ad8\u6548\uff0c\u61c9\u7528\u5ee3\u6cdb\u3002</p> <p>\u5feb\u901f\u6392\u5e8f\u7684\u6838\u5fc3\u64cd\u4f5c\u662f\u201c\u54e8\u5175\u5283\u5206\u201d\uff0c\u5176\u76ee\u6a19\u662f\uff1a\u9078\u64c7\u9663\u5217\u4e2d\u7684\u67d0\u500b\u5143\u7d20\u4f5c\u70ba\u201c\u57fa\u6e96\u6578\u201d\uff0c\u5c07\u6240\u6709\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\u79fb\u5230\u5176\u5de6\u5074\uff0c\u800c\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\u79fb\u5230\u5176\u53f3\u5074\u3002\u5177\u9ad4\u4f86\u8aaa\uff0c\u54e8\u5175\u5283\u5206\u7684\u6d41\u7a0b\u5982\u5716 11-8 \u6240\u793a\u3002</p> <ol> <li>\u9078\u53d6\u9663\u5217\u6700\u5de6\u7aef\u5143\u7d20\u4f5c\u70ba\u57fa\u6e96\u6578\uff0c\u521d\u59cb\u5316\u5169\u500b\u6307\u6a19 <code>i</code> \u548c <code>j</code> \u5206\u5225\u6307\u5411\u9663\u5217\u7684\u5169\u7aef\u3002</li> <li>\u8a2d\u5b9a\u4e00\u500b\u8ff4\u5708\uff0c\u5728\u6bcf\u8f2a\u4e2d\u4f7f\u7528 <code>i</code>\uff08<code>j</code>\uff09\u5206\u5225\u5c0b\u627e\u7b2c\u4e00\u500b\u6bd4\u57fa\u6e96\u6578\u5927\uff08\u5c0f\uff09\u7684\u5143\u7d20\uff0c\u7136\u5f8c\u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\u3002</li> <li>\u8ff4\u5708\u57f7\u884c\u6b65\u9a5f <code>2.</code> \uff0c\u76f4\u5230 <code>i</code> \u548c <code>j</code> \u76f8\u9047\u6642\u505c\u6b62\uff0c\u6700\u5f8c\u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u500b\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\u3002</li> </ol> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 11-8 \u00a0 \u54e8\u5175\u5283\u5206\u6b65\u9a5f </p> <p>\u54e8\u5175\u5283\u5206\u5b8c\u6210\u5f8c\uff0c\u539f\u9663\u5217\u88ab\u5283\u5206\u6210\u4e09\u90e8\u5206\uff1a\u5de6\u5b50\u9663\u5217\u3001\u57fa\u6e96\u6578\u3001\u53f3\u5b50\u9663\u5217\uff0c\u4e14\u6eff\u8db3\u201c\u5de6\u5b50\u9663\u5217\u4efb\u610f\u5143\u7d20 \\(\\leq\\) \u57fa\u6e96\u6578 \\(\\leq\\) \u53f3\u5b50\u9663\u5217\u4efb\u610f\u5143\u7d20\u201d\u3002\u56e0\u6b64\uff0c\u6211\u5011\u63a5\u4e0b\u4f86\u53ea\u9700\u5c0d\u9019\u5169\u500b\u5b50\u9663\u5217\u9032\u884c\u6392\u5e8f\u3002</p> <p>\u5feb\u901f\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565</p> <p>\u54e8\u5175\u5283\u5206\u7684\u5be6\u8cea\u662f\u5c07\u4e00\u500b\u8f03\u9577\u9663\u5217\u7684\u6392\u5e8f\u554f\u984c\u7c21\u5316\u70ba\u5169\u500b\u8f03\u77ed\u9663\u5217\u7684\u6392\u5e8f\u554f\u984c\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig quick_sort.py<pre><code>def partition(self, nums: list[int], left: int, right: int) -&gt; int:\n \"\"\"\u54e8\u5175\u5283\u5206\"\"\"\n # \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n i, j = left, right\n while i &lt; j:\n while i &lt; j and nums[j] &gt;= nums[left]:\n j -= 1 # \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while i &lt; j and nums[i] &lt;= nums[left]:\n i += 1 # \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n # \u5143\u7d20\u4ea4\u63db\n nums[i], nums[j] = nums[j], nums[i]\n # \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n nums[i], nums[left] = nums[left], nums[i]\n return i # \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n</code></pre> quick_sort.cpp<pre><code>/* \u5143\u7d20\u4ea4\u63db */\nvoid swap(vector&lt;int&gt; &amp;nums, int i, int j) {\n int tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5283\u5206 */\nint partition(vector&lt;int&gt; &amp;nums, int left, int right) {\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n int i = left, j = right;\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\n j--; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\n i++; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.java<pre><code>/* \u5143\u7d20\u4ea4\u63db */\nvoid swap(int[] nums, int i, int j) {\n int tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5283\u5206 */\nint partition(int[] nums, int left, int right) {\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n int i = left, j = right;\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\n j--; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\n i++; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.cs<pre><code>/* \u5143\u7d20\u4ea4\u63db */\nvoid Swap(int[] nums, int i, int j) {\n (nums[j], nums[i]) = (nums[i], nums[j]);\n}\n\n/* \u54e8\u5175\u5283\u5206 */\nint Partition(int[] nums, int left, int right) {\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n int i = left, j = right;\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\n j--; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\n i++; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n Swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n Swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.go<pre><code>/* \u54e8\u5175\u5283\u5206 */\nfunc (q *quickSort) partition(nums []int, left, right int) int {\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n i, j := left, right\n for i &lt; j {\n for i &lt; j &amp;&amp; nums[j] &gt;= nums[left] {\n j-- // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n for i &lt; j &amp;&amp; nums[i] &lt;= nums[left] {\n i++ // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n // \u5143\u7d20\u4ea4\u63db\n nums[i], nums[j] = nums[j], nums[i]\n }\n // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n nums[i], nums[left] = nums[left], nums[i]\n return i // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.swift<pre><code>/* \u54e8\u5175\u5283\u5206 */\nfunc partition(nums: inout [Int], left: Int, right: Int) -&gt; Int {\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n var i = left\n var j = right\n while i &lt; j {\n while i &lt; j, nums[j] &gt;= nums[left] {\n j -= 1 // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n while i &lt; j, nums[i] &lt;= nums[left] {\n i += 1 // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n nums.swapAt(i, j) // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n nums.swapAt(i, left) // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.js<pre><code>/* \u5143\u7d20\u4ea4\u63db */\nswap(nums, i, j) {\n let tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5283\u5206 */\npartition(nums, left, right) {\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n let i = left,\n j = right;\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) {\n j -= 1; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) {\n i += 1; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n // \u5143\u7d20\u4ea4\u63db\n this.swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n this.swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.ts<pre><code>/* \u5143\u7d20\u4ea4\u63db */\nswap(nums: number[], i: number, j: number): void {\n let tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5283\u5206 */\npartition(nums: number[], left: number, right: number): number {\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n let i = left,\n j = right;\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) {\n j -= 1; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) {\n i += 1; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n // \u5143\u7d20\u4ea4\u63db\n this.swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n this.swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.dart<pre><code>/* \u5143\u7d20\u4ea4\u63db */\nvoid _swap(List&lt;int&gt; nums, int i, int j) {\n int tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5283\u5206 */\nint _partition(List&lt;int&gt; nums, int left, int right) {\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n int i = left, j = right;\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) j--; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) i++; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n _swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n _swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.rs<pre><code>/* \u54e8\u5175\u5283\u5206 */\nfn partition(nums: &amp;mut [i32], left: usize, right: usize) -&gt; usize {\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n let (mut i, mut j) = (left, right);\n while i &lt; j {\n while i &lt; j &amp;&amp; nums[j] &gt;= nums[left] {\n j -= 1; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n while i &lt; j &amp;&amp; nums[i] &lt;= nums[left] {\n i += 1; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n nums.swap(i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n nums.swap(i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n i // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.c<pre><code>/* \u5143\u7d20\u4ea4\u63db */\nvoid swap(int nums[], int i, int j) {\n int tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n/* \u54e8\u5175\u5283\u5206 */\nint partition(int nums[], int left, int right) {\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n int i = left, j = right;\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) {\n j--; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) {\n i++; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n swap(nums, i, j);\n }\n // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n swap(nums, i, left);\n // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n return i;\n}\n</code></pre> quick_sort.kt<pre><code>/* \u5143\u7d20\u4ea4\u63db */\nfun swap(nums: IntArray, i: Int, j: Int) {\n val temp = nums[i]\n nums[i] = nums[j]\n nums[j] = temp\n}\n\n/* \u54e8\u5175\u5283\u5206 */\nfun partition(nums: IntArray, left: Int, right: Int): Int {\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n var i = left\n var j = right\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\n j-- // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\n i++ // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n swap(nums, i, j) // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n swap(nums, i, left) // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.rb<pre><code>[class]{QuickSort}-[func]{partition}\n</code></pre> quick_sort.zig<pre><code>// \u5143\u7d20\u4ea4\u63db\nfn swap(nums: []i32, i: usize, j: usize) void {\n var tmp = nums[i];\n nums[i] = nums[j];\n nums[j] = tmp;\n}\n\n// \u54e8\u5175\u5283\u5206\nfn partition(nums: []i32, left: usize, right: usize) usize {\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n var i = left;\n var j = right;\n while (i &lt; j) {\n while (i &lt; j and nums[j] &gt;= nums[left]) j -= 1; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while (i &lt; j and nums[i] &lt;= nums[left]) i += 1; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_sorting/quick_sort/#1151","title":"11.5.1 \u00a0 \u6f14\u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u5feb\u901f\u6392\u5e8f\u7684\u6574\u9ad4\u6d41\u7a0b\u5982\u5716 11-9 \u6240\u793a\u3002</p> <ol> <li>\u9996\u5148\uff0c\u5c0d\u539f\u9663\u5217\u57f7\u884c\u4e00\u6b21\u201c\u54e8\u5175\u5283\u5206\u201d\uff0c\u5f97\u5230\u672a\u6392\u5e8f\u7684\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u3002</li> <li>\u7136\u5f8c\uff0c\u5c0d\u5de6\u5b50\u9663\u5217\u548c\u53f3\u5b50\u9663\u5217\u5206\u5225\u905e\u8ff4\u57f7\u884c\u201c\u54e8\u5175\u5283\u5206\u201d\u3002</li> <li>\u6301\u7e8c\u905e\u8ff4\uff0c\u76f4\u81f3\u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\uff0c\u5f9e\u800c\u5b8c\u6210\u6574\u500b\u9663\u5217\u7684\u6392\u5e8f\u3002</li> </ol> <p></p> <p> \u5716 11-9 \u00a0 \u5feb\u901f\u6392\u5e8f\u6d41\u7a0b </p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig quick_sort.py<pre><code>def quick_sort(self, nums: list[int], left: int, right: int):\n \"\"\"\u5feb\u901f\u6392\u5e8f\"\"\"\n # \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n if left &gt;= right:\n return\n # \u54e8\u5175\u5283\u5206\n pivot = self.partition(nums, left, right)\n # \u905e\u8ff4\u5de6\u5b50\u9663\u5217\u3001\u53f3\u5b50\u9663\u5217\n self.quick_sort(nums, left, pivot - 1)\n self.quick_sort(nums, pivot + 1, right)\n</code></pre> quick_sort.cpp<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(vector&lt;int&gt; &amp;nums, int left, int right) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n if (left &gt;= right)\n return;\n // \u54e8\u5175\u5283\u5206\n int pivot = partition(nums, left, right);\n // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\u3001\u53f3\u5b50\u9663\u5217\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.java<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(int[] nums, int left, int right) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n if (left &gt;= right)\n return;\n // \u54e8\u5175\u5283\u5206\n int pivot = partition(nums, left, right);\n // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\u3001\u53f3\u5b50\u9663\u5217\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.cs<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nvoid QuickSort(int[] nums, int left, int right) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n if (left &gt;= right)\n return;\n // \u54e8\u5175\u5283\u5206\n int pivot = Partition(nums, left, right);\n // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\u3001\u53f3\u5b50\u9663\u5217\n QuickSort(nums, left, pivot - 1);\n QuickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.go<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nfunc (q *quickSort) quickSort(nums []int, left, right int) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n if left &gt;= right {\n return\n }\n // \u54e8\u5175\u5283\u5206\n pivot := q.partition(nums, left, right)\n // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\u3001\u53f3\u5b50\u9663\u5217\n q.quickSort(nums, left, pivot-1)\n q.quickSort(nums, pivot+1, right)\n}\n</code></pre> quick_sort.swift<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nfunc quickSort(nums: inout [Int], left: Int, right: Int) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n if left &gt;= right {\n return\n }\n // \u54e8\u5175\u5283\u5206\n let pivot = partition(nums: &amp;nums, left: left, right: right)\n // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\u3001\u53f3\u5b50\u9663\u5217\n quickSort(nums: &amp;nums, left: left, right: pivot - 1)\n quickSort(nums: &amp;nums, left: pivot + 1, right: right)\n}\n</code></pre> quick_sort.js<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nquickSort(nums, left, right) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n if (left &gt;= right) return;\n // \u54e8\u5175\u5283\u5206\n const pivot = this.partition(nums, left, right);\n // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\u3001\u53f3\u5b50\u9663\u5217\n this.quickSort(nums, left, pivot - 1);\n this.quickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.ts<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nquickSort(nums: number[], left: number, right: number): void {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n if (left &gt;= right) {\n return;\n }\n // \u54e8\u5175\u5283\u5206\n const pivot = this.partition(nums, left, right);\n // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\u3001\u53f3\u5b50\u9663\u5217\n this.quickSort(nums, left, pivot - 1);\n this.quickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.dart<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(List&lt;int&gt; nums, int left, int right) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n if (left &gt;= right) return;\n // \u54e8\u5175\u5283\u5206\n int pivot = _partition(nums, left, right);\n // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\u3001\u53f3\u5b50\u9663\u5217\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.rs<pre><code>/* \u5feb\u901f\u6392\u5e8f */\npub fn quick_sort(left: i32, right: i32, nums: &amp;mut [i32]) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n if left &gt;= right {\n return;\n }\n // \u54e8\u5175\u5283\u5206\n let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\u3001\u53f3\u5b50\u9663\u5217\n Self::quick_sort(left, pivot - 1, nums);\n Self::quick_sort(pivot + 1, right, nums);\n}\n</code></pre> quick_sort.c<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(int nums[], int left, int right) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n if (left &gt;= right) {\n return;\n }\n // \u54e8\u5175\u5283\u5206\n int pivot = partition(nums, left, right);\n // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\u3001\u53f3\u5b50\u9663\u5217\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.kt<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nfun quickSort(nums: IntArray, left: Int, right: Int) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n if (left &gt;= right) return\n // \u54e8\u5175\u5283\u5206\n val pivot = partition(nums, left, right)\n // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\u3001\u53f3\u5b50\u9663\u5217\n quickSort(nums, left, pivot - 1)\n quickSort(nums, pivot + 1, right)\n}\n</code></pre> quick_sort.rb<pre><code>[class]{QuickSort}-[func]{quick_sort}\n</code></pre> quick_sort.zig<pre><code>// \u5feb\u901f\u6392\u5e8f\nfn quickSort(nums: []i32, left: usize, right: usize) void {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n if (left &gt;= right) return;\n // \u54e8\u5175\u5283\u5206\n var pivot = partition(nums, left, right);\n // \u905e\u8ff4\u5de6\u5b50\u9663\u5217\u3001\u53f3\u5b50\u9663\u5217\n quickSort(nums, left, pivot - 1);\n quickSort(nums, pivot + 1, right);\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_sorting/quick_sort/#1152","title":"11.5.2 \u00a0 \u6f14\u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n \\log n)\\)\u3001\u81ea\u9069\u61c9\u6392\u5e8f\uff1a\u5728\u5e73\u5747\u60c5\u6cc1\u4e0b\uff0c\u54e8\u5175\u5283\u5206\u7684\u905e\u8ff4\u5c64\u6578\u70ba \\(\\log n\\) \uff0c\u6bcf\u5c64\u4e2d\u7684\u7e3d\u8ff4\u5708\u6578\u70ba \\(n\\) \uff0c\u7e3d\u9ad4\u4f7f\u7528 \\(O(n \\log n)\\) \u6642\u9593\u3002\u5728\u6700\u5dee\u60c5\u6cc1\u4e0b\uff0c\u6bcf\u8f2a\u54e8\u5175\u5283\u5206\u64cd\u4f5c\u90fd\u5c07\u9577\u5ea6\u70ba \\(n\\) \u7684\u9663\u5217\u5283\u5206\u70ba\u9577\u5ea6\u70ba \\(0\\) \u548c \\(n - 1\\) \u7684\u5169\u500b\u5b50\u9663\u5217\uff0c\u6b64\u6642\u905e\u8ff4\u5c64\u6578\u9054\u5230 \\(n\\) \uff0c\u6bcf\u5c64\u4e2d\u7684\u8ff4\u5708\u6578\u70ba \\(n\\) \uff0c\u7e3d\u9ad4\u4f7f\u7528 \\(O(n^2)\\) \u6642\u9593\u3002</li> <li>\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u5728\u8f38\u5165\u9663\u5217\u5b8c\u5168\u5012\u5e8f\u7684\u60c5\u6cc1\u4e0b\uff0c\u9054\u5230\u6700\u5dee\u905e\u8ff4\u6df1\u5ea6 \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u5806\u758a\u5e40\u7a7a\u9593\u3002\u6392\u5e8f\u64cd\u4f5c\u662f\u5728\u539f\u9663\u5217\u4e0a\u9032\u884c\u7684\uff0c\u672a\u85c9\u52a9\u984d\u5916\u9663\u5217\u3002</li> <li>\u975e\u7a69\u5b9a\u6392\u5e8f\uff1a\u5728\u54e8\u5175\u5283\u5206\u7684\u6700\u5f8c\u4e00\u6b65\uff0c\u57fa\u6e96\u6578\u53ef\u80fd\u6703\u88ab\u4ea4\u63db\u81f3\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u5074\u3002</li> </ul>"},{"location":"chapter_sorting/quick_sort/#1153","title":"11.5.3 \u00a0 \u5feb\u901f\u6392\u5e8f\u70ba\u4ec0\u9ebc\u5feb","text":"<p>\u5f9e\u540d\u7a31\u4e0a\u5c31\u80fd\u770b\u51fa\uff0c\u5feb\u901f\u6392\u5e8f\u5728\u6548\u7387\u65b9\u9762\u61c9\u8a72\u5177\u6709\u4e00\u5b9a\u7684\u512a\u52e2\u3002\u5118\u7ba1\u5feb\u901f\u6392\u5e8f\u7684\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u8207\u201c\u5408\u4f75\u6392\u5e8f\u201d\u548c\u201c\u5806\u7a4d\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</p> <ul> <li>\u51fa\u73fe\u6700\u5dee\u60c5\u6cc1\u7684\u6a5f\u7387\u5f88\u4f4e\uff1a\u96d6\u7136\u5feb\u901f\u6392\u5e8f\u7684\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\) \uff0c\u6c92\u6709\u5408\u4f75\u6392\u5e8f\u7a69\u5b9a\uff0c\u4f46\u5728\u7d55\u5927\u591a\u6578\u60c5\u6cc1\u4e0b\uff0c\u5feb\u901f\u6392\u5e8f\u80fd\u5728 \\(O(n \\log n)\\) \u7684\u6642\u9593\u8907\u96dc\u5ea6\u4e0b\u57f7\u884c\u3002</li> <li>\u5feb\u53d6\u4f7f\u7528\u6548\u7387\u9ad8\uff1a\u5728\u57f7\u884c\u54e8\u5175\u5283\u5206\u64cd\u4f5c\u6642\uff0c\u7cfb\u7d71\u53ef\u5c07\u6574\u500b\u5b50\u9663\u5217\u8f09\u5165\u5230\u5feb\u53d6\uff0c\u56e0\u6b64\u8a2a\u554f\u5143\u7d20\u7684\u6548\u7387\u8f03\u9ad8\u3002\u800c\u50cf\u201c\u5806\u7a4d\u6392\u5e8f\u201d\u9019\u985e\u6f14\u7b97\u6cd5\u9700\u8981\u8df3\u8e8d\u5f0f\u8a2a\u554f\u5143\u7d20\uff0c\u5f9e\u800c\u7f3a\u4e4f\u9019\u4e00\u7279\u6027\u3002</li> <li>\u8907\u96dc\u5ea6\u7684\u5e38\u6578\u4fc2\u6578\u5c0f\uff1a\u5728\u4e0a\u8ff0\u4e09\u7a2e\u6f14\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u6bd4\u8f03\u3001\u8ce6\u503c\u3001\u4ea4\u63db\u7b49\u64cd\u4f5c\u7684\u7e3d\u6578\u91cf\u6700\u5c11\u3002\u9019\u8207\u201c\u63d2\u5165\u6392\u5e8f\u201d\u6bd4\u201c\u6ce1\u6cab\u6392\u5e8f\u201d\u66f4\u5feb\u7684\u539f\u56e0\u985e\u4f3c\u3002</li> </ul>"},{"location":"chapter_sorting/quick_sort/#1154","title":"11.5.4 \u00a0 \u57fa\u6e96\u6578\u6700\u4f73\u5316","text":"<p>\u5feb\u901f\u6392\u5e8f\u5728\u67d0\u4e9b\u8f38\u5165\u4e0b\u7684\u6642\u9593\u6548\u7387\u53ef\u80fd\u964d\u4f4e\u3002\u8209\u4e00\u500b\u6975\u7aef\u4f8b\u5b50\uff0c\u5047\u8a2d\u8f38\u5165\u9663\u5217\u662f\u5b8c\u5168\u5012\u5e8f\u7684\uff0c\u7531\u65bc\u6211\u5011\u9078\u64c7\u6700\u5de6\u7aef\u5143\u7d20\u4f5c\u70ba\u57fa\u6e96\u6578\uff0c\u90a3\u9ebc\u5728\u54e8\u5175\u5283\u5206\u5b8c\u6210\u5f8c\uff0c\u57fa\u6e96\u6578\u88ab\u4ea4\u63db\u81f3\u9663\u5217\u6700\u53f3\u7aef\uff0c\u5c0e\u81f4\u5de6\u5b50\u9663\u5217\u9577\u5ea6\u70ba \\(n - 1\\)\u3001\u53f3\u5b50\u9663\u5217\u9577\u5ea6\u70ba \\(0\\) \u3002\u5982\u6b64\u905e\u8ff4\u4e0b\u53bb\uff0c\u6bcf\u8f2a\u54e8\u5175\u5283\u5206\u5f8c\u90fd\u6709\u4e00\u500b\u5b50\u9663\u5217\u7684\u9577\u5ea6\u70ba \\(0\\) \uff0c\u5206\u6cbb\u7b56\u7565\u5931\u6548\uff0c\u5feb\u901f\u6392\u5e8f\u9000\u5316\u70ba\u201c\u6ce1\u6cab\u6392\u5e8f\u201d\u7684\u8fd1\u4f3c\u5f62\u5f0f\u3002</p> <p>\u70ba\u4e86\u5118\u91cf\u907f\u514d\u9019\u7a2e\u60c5\u6cc1\u767c\u751f\uff0c\u6211\u5011\u53ef\u4ee5\u6700\u4f73\u5316\u54e8\u5175\u5283\u5206\u4e2d\u7684\u57fa\u6e96\u6578\u7684\u9078\u53d6\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u6211\u5011\u53ef\u4ee5\u96a8\u6a5f\u9078\u53d6\u4e00\u500b\u5143\u7d20\u4f5c\u70ba\u57fa\u6e96\u6578\u3002\u7136\u800c\uff0c\u5982\u679c\u904b\u6c23\u4e0d\u4f73\uff0c\u6bcf\u6b21\u90fd\u9078\u5230\u4e0d\u7406\u60f3\u7684\u57fa\u6e96\u6578\uff0c\u6548\u7387\u4ecd\u7136\u4e0d\u76e1\u5982\u4eba\u610f\u3002</p> <p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7a0b\u5f0f\u8a9e\u8a00\u901a\u5e38\u751f\u6210\u7684\u662f\u201c\u507d\u96a8\u6a5f\u6578\u201d\u3002\u5982\u679c\u6211\u5011\u91dd\u5c0d\u507d\u96a8\u6a5f\u6578\u5e8f\u5217\u69cb\u5efa\u4e00\u500b\u7279\u5b9a\u7684\u6e2c\u8a66\u6a23\u4f8b\uff0c\u90a3\u9ebc\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u4ecd\u7136\u53ef\u80fd\u52a3\u5316\u3002</p> <p>\u70ba\u4e86\u9032\u4e00\u6b65\u6539\u9032\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u9663\u5217\u4e2d\u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\uff08\u901a\u5e38\u70ba\u9663\u5217\u7684\u9996\u3001\u5c3e\u3001\u4e2d\u9ede\u5143\u7d20\uff09\uff0c\u4e26\u5c07\u9019\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578\u4f5c\u70ba\u57fa\u6e96\u6578\u3002\u9019\u6a23\u4e00\u4f86\uff0c\u57fa\u6e96\u6578\u201c\u65e2\u4e0d\u592a\u5c0f\u4e5f\u4e0d\u592a\u5927\u201d\u7684\u6a5f\u7387\u5c07\u5927\u5e45\u63d0\u5347\u3002\u7576\u7136\uff0c\u6211\u5011\u9084\u53ef\u4ee5\u9078\u53d6\u66f4\u591a\u5019\u9078\u5143\u7d20\uff0c\u4ee5\u9032\u4e00\u6b65\u63d0\u9ad8\u6f14\u7b97\u6cd5\u7684\u7a69\u5065\u6027\u3002\u63a1\u7528\u9019\u7a2e\u65b9\u6cd5\u5f8c\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u7684\u6a5f\u7387\u5927\u5927\u964d\u4f4e\u3002</p> <p>\u793a\u4f8b\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig quick_sort.py<pre><code>def median_three(self, nums: list[int], left: int, mid: int, right: int) -&gt; int:\n \"\"\"\u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578\"\"\"\n l, m, r = nums[left], nums[mid], nums[right]\n if (l &lt;= m &lt;= r) or (r &lt;= m &lt;= l):\n return mid # m \u5728 l \u548c r \u4e4b\u9593\n if (m &lt;= l &lt;= r) or (r &lt;= l &lt;= m):\n return left # l \u5728 m \u548c r \u4e4b\u9593\n return right\n\ndef partition(self, nums: list[int], left: int, right: int) -&gt; int:\n \"\"\"\u54e8\u5175\u5283\u5206\uff08\u4e09\u6578\u53d6\u4e2d\u503c\uff09\"\"\"\n # \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n med = self.median_three(nums, left, (left + right) // 2, right)\n # \u5c07\u4e2d\u4f4d\u6578\u4ea4\u63db\u81f3\u9663\u5217\u6700\u5de6\u7aef\n nums[left], nums[med] = nums[med], nums[left]\n # \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n i, j = left, right\n while i &lt; j:\n while i &lt; j and nums[j] &gt;= nums[left]:\n j -= 1 # \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while i &lt; j and nums[i] &lt;= nums[left]:\n i += 1 # \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n # \u5143\u7d20\u4ea4\u63db\n nums[i], nums[j] = nums[j], nums[i]\n # \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n nums[i], nums[left] = nums[left], nums[i]\n return i # \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n</code></pre> quick_sort.cpp<pre><code>/* \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578 */\nint medianThree(vector&lt;int&gt; &amp;nums, int left, int mid, int right) {\n int l = nums[left], m = nums[mid], r = nums[right];\n if ((l &lt;= m &amp;&amp; m &lt;= r) || (r &lt;= m &amp;&amp; m &lt;= l))\n return mid; // m \u5728 l \u548c r \u4e4b\u9593\n if ((m &lt;= l &amp;&amp; l &lt;= r) || (r &lt;= l &amp;&amp; l &lt;= m))\n return left; // l \u5728 m \u548c r \u4e4b\u9593\n return right;\n}\n\n/* \u54e8\u5175\u5283\u5206\uff08\u4e09\u6578\u53d6\u4e2d\u503c\uff09 */\nint partition(vector&lt;int&gt; &amp;nums, int left, int right) {\n // \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578\n int med = medianThree(nums, left, (left + right) / 2, right);\n // \u5c07\u4e2d\u4f4d\u6578\u4ea4\u63db\u81f3\u9663\u5217\u6700\u5de6\u7aef\n swap(nums, left, med);\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n int i = left, j = right;\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\n j--; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\n i++; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.java<pre><code>/* \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578 */\nint medianThree(int[] nums, int left, int mid, int right) {\n int l = nums[left], m = nums[mid], r = nums[right];\n if ((l &lt;= m &amp;&amp; m &lt;= r) || (r &lt;= m &amp;&amp; m &lt;= l))\n return mid; // m \u5728 l \u548c r \u4e4b\u9593\n if ((m &lt;= l &amp;&amp; l &lt;= r) || (r &lt;= l &amp;&amp; l &lt;= m))\n return left; // l \u5728 m \u548c r \u4e4b\u9593\n return right;\n}\n\n/* \u54e8\u5175\u5283\u5206\uff08\u4e09\u6578\u53d6\u4e2d\u503c\uff09 */\nint partition(int[] nums, int left, int right) {\n // \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578\n int med = medianThree(nums, left, (left + right) / 2, right);\n // \u5c07\u4e2d\u4f4d\u6578\u4ea4\u63db\u81f3\u9663\u5217\u6700\u5de6\u7aef\n swap(nums, left, med);\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n int i = left, j = right;\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\n j--; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\n i++; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.cs<pre><code>/* \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578 */\nint MedianThree(int[] nums, int left, int mid, int right) {\n int l = nums[left], m = nums[mid], r = nums[right];\n if ((l &lt;= m &amp;&amp; m &lt;= r) || (r &lt;= m &amp;&amp; m &lt;= l))\n return mid; // m \u5728 l \u548c r \u4e4b\u9593\n if ((m &lt;= l &amp;&amp; l &lt;= r) || (r &lt;= l &amp;&amp; l &lt;= m))\n return left; // l \u5728 m \u548c r \u4e4b\u9593\n return right;\n}\n\n/* \u54e8\u5175\u5283\u5206\uff08\u4e09\u6578\u53d6\u4e2d\u503c\uff09 */\nint Partition(int[] nums, int left, int right) {\n // \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578\n int med = MedianThree(nums, left, (left + right) / 2, right);\n // \u5c07\u4e2d\u4f4d\u6578\u4ea4\u63db\u81f3\u9663\u5217\u6700\u5de6\u7aef\n Swap(nums, left, med);\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n int i = left, j = right;\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\n j--; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\n i++; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n Swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n Swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.go<pre><code>/* \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578 */\nfunc (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int {\n l, m, r := nums[left], nums[mid], nums[right]\n if (l &lt;= m &amp;&amp; m &lt;= r) || (r &lt;= m &amp;&amp; m &lt;= l) {\n return mid // m \u5728 l \u548c r \u4e4b\u9593\n }\n if (m &lt;= l &amp;&amp; l &lt;= r) || (r &lt;= l &amp;&amp; l &lt;= m) {\n return left // l \u5728 m \u548c r \u4e4b\u9593\n }\n return right\n}\n\n/* \u54e8\u5175\u5283\u5206\uff08\u4e09\u6578\u53d6\u4e2d\u503c\uff09*/\nfunc (q *quickSortMedian) partition(nums []int, left, right int) int {\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n med := q.medianThree(nums, left, (left+right)/2, right)\n // \u5c07\u4e2d\u4f4d\u6578\u4ea4\u63db\u81f3\u9663\u5217\u6700\u5de6\u7aef\n nums[left], nums[med] = nums[med], nums[left]\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n i, j := left, right\n for i &lt; j {\n for i &lt; j &amp;&amp; nums[j] &gt;= nums[left] {\n j-- //\u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n for i &lt; j &amp;&amp; nums[i] &lt;= nums[left] {\n i++ //\u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n //\u5143\u7d20\u4ea4\u63db\n nums[i], nums[j] = nums[j], nums[i]\n }\n //\u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n nums[i], nums[left] = nums[left], nums[i]\n return i //\u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.swift<pre><code>/* \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578 */\nfunc medianThree(nums: [Int], left: Int, mid: Int, right: Int) -&gt; Int {\n let l = nums[left]\n let m = nums[mid]\n let r = nums[right]\n if (l &lt;= m &amp;&amp; m &lt;= r) || (r &lt;= m &amp;&amp; m &lt;= l) {\n return mid // m \u5728 l \u548c r \u4e4b\u9593\n }\n if (m &lt;= l &amp;&amp; l &lt;= r) || (r &lt;= l &amp;&amp; l &lt;= m) {\n return left // l \u5728 m \u548c r \u4e4b\u9593\n }\n return right\n}\n\n/* \u54e8\u5175\u5283\u5206\uff08\u4e09\u6578\u53d6\u4e2d\u503c\uff09 */\nfunc partitionMedian(nums: inout [Int], left: Int, right: Int) -&gt; Int {\n // \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578\n let med = medianThree(nums: nums, left: left, mid: (left + right) / 2, right: right)\n // \u5c07\u4e2d\u4f4d\u6578\u4ea4\u63db\u81f3\u9663\u5217\u6700\u5de6\u7aef\n nums.swapAt(left, med)\n return partition(nums: &amp;nums, left: left, right: right)\n}\n</code></pre> quick_sort.js<pre><code>/* \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578 */\nmedianThree(nums, left, mid, right) {\n let l = nums[left],\n m = nums[mid],\n r = nums[right];\n // m \u5728 l \u548c r \u4e4b\u9593\n if ((l &lt;= m &amp;&amp; m &lt;= r) || (r &lt;= m &amp;&amp; m &lt;= l)) return mid;\n // l \u5728 m \u548c r \u4e4b\u9593\n if ((m &lt;= l &amp;&amp; l &lt;= r) || (r &lt;= l &amp;&amp; l &lt;= m)) return left;\n return right;\n}\n\n/* \u54e8\u5175\u5283\u5206\uff08\u4e09\u6578\u53d6\u4e2d\u503c\uff09 */\npartition(nums, left, right) {\n // \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578\n let med = this.medianThree(\n nums,\n left,\n Math.floor((left + right) / 2),\n right\n );\n // \u5c07\u4e2d\u4f4d\u6578\u4ea4\u63db\u81f3\u9663\u5217\u6700\u5de6\u7aef\n this.swap(nums, left, med);\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n let i = left,\n j = right;\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) j--; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) i++; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n this.swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n this.swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.ts<pre><code>/* \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578 */\nmedianThree(\n nums: number[],\n left: number,\n mid: number,\n right: number\n): number {\n let l = nums[left],\n m = nums[mid],\n r = nums[right];\n // m \u5728 l \u548c r \u4e4b\u9593\n if ((l &lt;= m &amp;&amp; m &lt;= r) || (r &lt;= m &amp;&amp; m &lt;= l)) return mid;\n // l \u5728 m \u548c r \u4e4b\u9593\n if ((m &lt;= l &amp;&amp; l &lt;= r) || (r &lt;= l &amp;&amp; l &lt;= m)) return left;\n return right;\n}\n\n/* \u54e8\u5175\u5283\u5206\uff08\u4e09\u6578\u53d6\u4e2d\u503c\uff09 */\npartition(nums: number[], left: number, right: number): number {\n // \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578\n let med = this.medianThree(\n nums,\n left,\n Math.floor((left + right) / 2),\n right\n );\n // \u5c07\u4e2d\u4f4d\u6578\u4ea4\u63db\u81f3\u9663\u5217\u6700\u5de6\u7aef\n this.swap(nums, left, med);\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n let i = left,\n j = right;\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) {\n j--; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) {\n i++; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n this.swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n this.swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.dart<pre><code>/* \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578 */\nint _medianThree(List&lt;int&gt; nums, int left, int mid, int right) {\n int l = nums[left], m = nums[mid], r = nums[right];\n if ((l &lt;= m &amp;&amp; m &lt;= r) || (r &lt;= m &amp;&amp; m &lt;= l))\n return mid; // m \u5728 l \u548c r \u4e4b\u9593\n if ((m &lt;= l &amp;&amp; l &lt;= r) || (r &lt;= l &amp;&amp; l &lt;= m))\n return left; // l \u5728 m \u548c r \u4e4b\u9593\n return right;\n}\n\n/* \u54e8\u5175\u5283\u5206\uff08\u4e09\u6578\u53d6\u4e2d\u503c\uff09 */\nint _partition(List&lt;int&gt; nums, int left, int right) {\n // \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578\n int med = _medianThree(nums, left, (left + right) ~/ 2, right);\n // \u5c07\u4e2d\u4f4d\u6578\u4ea4\u63db\u81f3\u9663\u5217\u6700\u5de6\u7aef\n _swap(nums, left, med);\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n int i = left, j = right;\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) j--; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) i++; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n _swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n _swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.rs<pre><code>/* \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578 */\nfn median_three(nums: &amp;mut [i32], left: usize, mid: usize, right: usize) -&gt; usize {\n let (l, m, r) = (nums[left], nums[mid], nums[right]);\n if (l &lt;= m &amp;&amp; m &lt;= r) || (r &lt;= m &amp;&amp; m &lt;= l) {\n return mid; // m \u5728 l \u548c r \u4e4b\u9593\n }\n if (m &lt;= l &amp;&amp; l &lt;= r) || (r &lt;= l &amp;&amp; l &lt;= m) {\n return left; // l \u5728 m \u548c r \u4e4b\u9593\n }\n right\n}\n\n/* \u54e8\u5175\u5283\u5206\uff08\u4e09\u6578\u53d6\u4e2d\u503c\uff09 */\nfn partition(nums: &amp;mut [i32], left: usize, right: usize) -&gt; usize {\n // \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578\n let med = Self::median_three(nums, left, (left + right) / 2, right);\n // \u5c07\u4e2d\u4f4d\u6578\u4ea4\u63db\u81f3\u9663\u5217\u6700\u5de6\u7aef\n nums.swap(left, med);\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n let (mut i, mut j) = (left, right);\n while i &lt; j {\n while i &lt; j &amp;&amp; nums[j] &gt;= nums[left] {\n j -= 1; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n while i &lt; j &amp;&amp; nums[i] &lt;= nums[left] {\n i += 1; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n }\n nums.swap(i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n nums.swap(i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n i // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.c<pre><code>/* \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578 */\nint medianThree(int nums[], int left, int mid, int right) {\n int l = nums[left], m = nums[mid], r = nums[right];\n if ((l &lt;= m &amp;&amp; m &lt;= r) || (r &lt;= m &amp;&amp; m &lt;= l))\n return mid; // m \u5728 l \u548c r \u4e4b\u9593\n if ((m &lt;= l &amp;&amp; l &lt;= r) || (r &lt;= l &amp;&amp; l &lt;= m))\n return left; // l \u5728 m \u548c r \u4e4b\u9593\n return right;\n}\n\n/* \u54e8\u5175\u5283\u5206\uff08\u4e09\u6578\u53d6\u4e2d\u503c\uff09 */\nint partitionMedian(int nums[], int left, int right) {\n // \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578\n int med = medianThree(nums, left, (left + right) / 2, right);\n // \u5c07\u4e2d\u4f4d\u6578\u4ea4\u63db\u81f3\u9663\u5217\u6700\u5de6\u7aef\n swap(nums, left, med);\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n int i = left, j = right;\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\n j--; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\n i++; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.kt<pre><code>/* \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578 */\nfun medianThree(nums: IntArray, left: Int, mid: Int, right: Int): Int {\n val l = nums[left]\n val m = nums[mid]\n val r = nums[right]\n if ((m in l..r) || (m in r..l))\n return mid // m \u5728 l \u548c r \u4e4b\u9593\n if ((l in m..r) || (l in r..m))\n return left // l \u5728 m \u548c r \u4e4b\u9593\n return right\n}\n\n/* \u54e8\u5175\u5283\u5206\uff08\u4e09\u6578\u53d6\u4e2d\u503c\uff09 */\nfun partitionMedian(nums: IntArray, left: Int, right: Int): Int {\n // \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578\n val med = medianThree(nums, left, (left + right) / 2, right)\n // \u5c07\u4e2d\u4f4d\u6578\u4ea4\u63db\u81f3\u9663\u5217\u6700\u5de6\u7aef\n swap(nums, left, med)\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n var i = left\n var j = right\n while (i &lt; j) {\n while (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\n j-- // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\n i++ // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n swap(nums, i, j) // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n swap(nums, i, left) // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.rb<pre><code>[class]{QuickSortMedian}-[func]{median_three}\n\n[class]{QuickSortMedian}-[func]{partition}\n</code></pre> quick_sort.zig<pre><code>// \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578\nfn medianThree(nums: []i32, left: usize, mid: usize, right: usize) usize {\n var l = nums[left];\n var m = nums[mid];\n var r = nums[right];\n if ((l &lt;= m &amp;&amp; m &lt;= r) || (r &lt;= m &amp;&amp; m &lt;= l))\n return mid; // m \u5728 l \u548c r \u4e4b\u9593\n if ((m &lt;= l &amp;&amp; l &lt;= r) || (r &lt;= l &amp;&amp; l &lt;= m))\n return left; // l \u5728 m \u548c r \u4e4b\u9593\n return right;\n}\n\n// \u54e8\u5175\u5283\u5206\uff08\u4e09\u6578\u53d6\u4e2d\u503c\uff09\nfn partition(nums: []i32, left: usize, right: usize) usize {\n // \u9078\u53d6\u4e09\u500b\u5019\u9078\u5143\u7d20\u7684\u4e2d\u4f4d\u6578\n var med = medianThree(nums, left, (left + right) / 2, right);\n // \u5c07\u4e2d\u4f4d\u6578\u4ea4\u63db\u81f3\u9663\u5217\u6700\u5de6\u7aef\n swap(nums, left, med);\n // \u4ee5 nums[left] \u70ba\u57fa\u6e96\u6578\n var i = left;\n var j = right;\n while (i &lt; j) {\n while (i &lt; j and nums[j] &gt;= nums[left]) j -= 1; // \u5f9e\u53f3\u5411\u5de6\u627e\u9996\u500b\u5c0f\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n while (i &lt; j and nums[i] &lt;= nums[left]) i += 1; // \u5f9e\u5de6\u5411\u53f3\u627e\u9996\u500b\u5927\u65bc\u57fa\u6e96\u6578\u7684\u5143\u7d20\n swap(nums, i, j); // \u4ea4\u63db\u9019\u5169\u500b\u5143\u7d20\n }\n swap(nums, i, left); // \u5c07\u57fa\u6e96\u6578\u4ea4\u63db\u81f3\u5169\u5b50\u9663\u5217\u7684\u5206\u754c\u7dda\n return i; // \u8fd4\u56de\u57fa\u6e96\u6578\u7684\u7d22\u5f15\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_sorting/quick_sort/#1155","title":"11.5.5 \u00a0 \u5c3e\u905e\u8ff4\u6700\u4f73\u5316","text":"<p>\u5728\u67d0\u4e9b\u8f38\u5165\u4e0b\uff0c\u5feb\u901f\u6392\u5e8f\u53ef\u80fd\u4f54\u7528\u7a7a\u9593\u8f03\u591a\u3002\u4ee5\u5b8c\u5168\u6709\u5e8f\u7684\u8f38\u5165\u9663\u5217\u70ba\u4f8b\uff0c\u8a2d\u905e\u8ff4\u4e2d\u7684\u5b50\u9663\u5217\u9577\u5ea6\u70ba \\(m\\) \uff0c\u6bcf\u8f2a\u54e8\u5175\u5283\u5206\u64cd\u4f5c\u90fd\u5c07\u7522\u751f\u9577\u5ea6\u70ba \\(0\\) \u7684\u5de6\u5b50\u9663\u5217\u548c\u9577\u5ea6\u70ba \\(m - 1\\) \u7684\u53f3\u5b50\u9663\u5217\uff0c\u9019\u610f\u5473\u8457\u6bcf\u4e00\u5c64\u905e\u8ff4\u547c\u53eb\u6e1b\u5c11\u7684\u554f\u984c\u898f\u6a21\u975e\u5e38\u5c0f\uff08\u53ea\u6e1b\u5c11\u4e00\u500b\u5143\u7d20\uff09\uff0c\u905e\u8ff4\u6a39\u7684\u9ad8\u5ea6\u6703\u9054\u5230 \\(n - 1\\) \uff0c\u6b64\u6642\u9700\u8981\u4f54\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u5806\u758a\u5e40\u7a7a\u9593\u3002</p> <p>\u70ba\u4e86\u9632\u6b62\u5806\u758a\u5e40\u7a7a\u9593\u7684\u7d2f\u7a4d\uff0c\u6211\u5011\u53ef\u4ee5\u5728\u6bcf\u8f2a\u54e8\u5175\u6392\u5e8f\u5b8c\u6210\u5f8c\uff0c\u6bd4\u8f03\u5169\u500b\u5b50\u9663\u5217\u7684\u9577\u5ea6\uff0c\u50c5\u5c0d\u8f03\u77ed\u7684\u5b50\u9663\u5217\u9032\u884c\u905e\u8ff4\u3002\u7531\u65bc\u8f03\u77ed\u5b50\u9663\u5217\u7684\u9577\u5ea6\u4e0d\u6703\u8d85\u904e \\(n / 2\\) \uff0c\u56e0\u6b64\u9019\u7a2e\u65b9\u6cd5\u80fd\u78ba\u4fdd\u905e\u8ff4\u6df1\u5ea6\u4e0d\u8d85\u904e \\(\\log n\\) \uff0c\u5f9e\u800c\u5c07\u6700\u5dee\u7a7a\u9593\u8907\u96dc\u5ea6\u6700\u4f73\u5316\u81f3 \\(O(\\log n)\\) \u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig quick_sort.py<pre><code>def quick_sort(self, nums: list[int], left: int, right: int):\n \"\"\"\u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff09\"\"\"\n # \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\n while left &lt; right:\n # \u54e8\u5175\u5283\u5206\u64cd\u4f5c\n pivot = self.partition(nums, left, right)\n # \u5c0d\u5169\u500b\u5b50\u9663\u5217\u4e2d\u8f03\u77ed\u7684\u90a3\u500b\u57f7\u884c\u5feb\u901f\u6392\u5e8f\n if pivot - left &lt; right - pivot:\n self.quick_sort(nums, left, pivot - 1) # \u905e\u8ff4\u6392\u5e8f\u5de6\u5b50\u9663\u5217\n left = pivot + 1 # \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [pivot + 1, right]\n else:\n self.quick_sort(nums, pivot + 1, right) # \u905e\u8ff4\u6392\u5e8f\u53f3\u5b50\u9663\u5217\n right = pivot - 1 # \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [left, pivot - 1]\n</code></pre> quick_sort.cpp<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff09 */\nvoid quickSort(vector&lt;int&gt; &amp;nums, int left, int right) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\n while (left &lt; right) {\n // \u54e8\u5175\u5283\u5206\u64cd\u4f5c\n int pivot = partition(nums, left, right);\n // \u5c0d\u5169\u500b\u5b50\u9663\u5217\u4e2d\u8f03\u77ed\u7684\u90a3\u500b\u57f7\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left &lt; right - pivot) {\n quickSort(nums, left, pivot - 1); // \u905e\u8ff4\u6392\u5e8f\u5de6\u5b50\u9663\u5217\n left = pivot + 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [pivot + 1, right]\n } else {\n quickSort(nums, pivot + 1, right); // \u905e\u8ff4\u6392\u5e8f\u53f3\u5b50\u9663\u5217\n right = pivot - 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [left, pivot - 1]\n }\n }\n}\n</code></pre> quick_sort.java<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff09 */\nvoid quickSort(int[] nums, int left, int right) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\n while (left &lt; right) {\n // \u54e8\u5175\u5283\u5206\u64cd\u4f5c\n int pivot = partition(nums, left, right);\n // \u5c0d\u5169\u500b\u5b50\u9663\u5217\u4e2d\u8f03\u77ed\u7684\u90a3\u500b\u57f7\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left &lt; right - pivot) {\n quickSort(nums, left, pivot - 1); // \u905e\u8ff4\u6392\u5e8f\u5de6\u5b50\u9663\u5217\n left = pivot + 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [pivot + 1, right]\n } else {\n quickSort(nums, pivot + 1, right); // \u905e\u8ff4\u6392\u5e8f\u53f3\u5b50\u9663\u5217\n right = pivot - 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [left, pivot - 1]\n }\n }\n}\n</code></pre> quick_sort.cs<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff09 */\nvoid QuickSort(int[] nums, int left, int right) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\n while (left &lt; right) {\n // \u54e8\u5175\u5283\u5206\u64cd\u4f5c\n int pivot = Partition(nums, left, right);\n // \u5c0d\u5169\u500b\u5b50\u9663\u5217\u4e2d\u8f03\u77ed\u7684\u90a3\u500b\u57f7\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left &lt; right - pivot) {\n QuickSort(nums, left, pivot - 1); // \u905e\u8ff4\u6392\u5e8f\u5de6\u5b50\u9663\u5217\n left = pivot + 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [pivot + 1, right]\n } else {\n QuickSort(nums, pivot + 1, right); // \u905e\u8ff4\u6392\u5e8f\u53f3\u5b50\u9663\u5217\n right = pivot - 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [left, pivot - 1]\n }\n }\n}\n</code></pre> quick_sort.go<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff09*/\nfunc (q *quickSortTailCall) quickSort(nums []int, left, right int) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\n for left &lt; right {\n // \u54e8\u5175\u5283\u5206\u64cd\u4f5c\n pivot := q.partition(nums, left, right)\n // \u5c0d\u5169\u500b\u5b50\u9663\u5217\u4e2d\u8f03\u77ed\u7684\u90a3\u500b\u57f7\u884c\u5feb\u901f\u6392\u5e8f\n if pivot-left &lt; right-pivot {\n q.quickSort(nums, left, pivot-1) // \u905e\u8ff4\u6392\u5e8f\u5de6\u5b50\u9663\u5217\n left = pivot + 1 // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [pivot + 1, right]\n } else {\n q.quickSort(nums, pivot+1, right) // \u905e\u8ff4\u6392\u5e8f\u53f3\u5b50\u9663\u5217\n right = pivot - 1 // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [left, pivot - 1]\n }\n }\n}\n</code></pre> quick_sort.swift<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff09 */\nfunc quickSortTailCall(nums: inout [Int], left: Int, right: Int) {\n var left = left\n var right = right\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\n while left &lt; right {\n // \u54e8\u5175\u5283\u5206\u64cd\u4f5c\n let pivot = partition(nums: &amp;nums, left: left, right: right)\n // \u5c0d\u5169\u500b\u5b50\u9663\u5217\u4e2d\u8f03\u77ed\u7684\u90a3\u500b\u57f7\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left) &lt; (right - pivot) {\n quickSortTailCall(nums: &amp;nums, left: left, right: pivot - 1) // \u905e\u8ff4\u6392\u5e8f\u5de6\u5b50\u9663\u5217\n left = pivot + 1 // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [pivot + 1, right]\n } else {\n quickSortTailCall(nums: &amp;nums, left: pivot + 1, right: right) // \u905e\u8ff4\u6392\u5e8f\u53f3\u5b50\u9663\u5217\n right = pivot - 1 // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [left, pivot - 1]\n }\n }\n}\n</code></pre> quick_sort.js<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff09 */\nquickSort(nums, left, right) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\n while (left &lt; right) {\n // \u54e8\u5175\u5283\u5206\u64cd\u4f5c\n let pivot = this.partition(nums, left, right);\n // \u5c0d\u5169\u500b\u5b50\u9663\u5217\u4e2d\u8f03\u77ed\u7684\u90a3\u500b\u57f7\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left &lt; right - pivot) {\n this.quickSort(nums, left, pivot - 1); // \u905e\u8ff4\u6392\u5e8f\u5de6\u5b50\u9663\u5217\n left = pivot + 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [pivot + 1, right]\n } else {\n this.quickSort(nums, pivot + 1, right); // \u905e\u8ff4\u6392\u5e8f\u53f3\u5b50\u9663\u5217\n right = pivot - 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [left, pivot - 1]\n }\n }\n}\n</code></pre> quick_sort.ts<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff09 */\nquickSort(nums: number[], left: number, right: number): void {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\n while (left &lt; right) {\n // \u54e8\u5175\u5283\u5206\u64cd\u4f5c\n let pivot = this.partition(nums, left, right);\n // \u5c0d\u5169\u500b\u5b50\u9663\u5217\u4e2d\u8f03\u77ed\u7684\u90a3\u500b\u57f7\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left &lt; right - pivot) {\n this.quickSort(nums, left, pivot - 1); // \u905e\u8ff4\u6392\u5e8f\u5de6\u5b50\u9663\u5217\n left = pivot + 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [pivot + 1, right]\n } else {\n this.quickSort(nums, pivot + 1, right); // \u905e\u8ff4\u6392\u5e8f\u53f3\u5b50\u9663\u5217\n right = pivot - 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [left, pivot - 1]\n }\n }\n}\n</code></pre> quick_sort.dart<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff09 */\nvoid quickSort(List&lt;int&gt; nums, int left, int right) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\n while (left &lt; right) {\n // \u54e8\u5175\u5283\u5206\u64cd\u4f5c\n int pivot = _partition(nums, left, right);\n // \u5c0d\u5169\u500b\u5b50\u9663\u5217\u4e2d\u8f03\u77ed\u7684\u90a3\u500b\u57f7\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left &lt; right - pivot) {\n quickSort(nums, left, pivot - 1); // \u905e\u8ff4\u6392\u5e8f\u5de6\u5b50\u9663\u5217\n left = pivot + 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [pivot + 1, right]\n } else {\n quickSort(nums, pivot + 1, right); // \u905e\u8ff4\u6392\u5e8f\u53f3\u5b50\u9663\u5217\n right = pivot - 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [left, pivot - 1]\n }\n }\n}\n</code></pre> quick_sort.rs<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff09 */\npub fn quick_sort(mut left: i32, mut right: i32, nums: &amp;mut [i32]) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\n while left &lt; right {\n // \u54e8\u5175\u5283\u5206\u64cd\u4f5c\n let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n // \u5c0d\u5169\u500b\u5b50\u9663\u5217\u4e2d\u8f03\u77ed\u7684\u90a3\u500b\u57f7\u884c\u5feb\u901f\u6392\u5e8f\n if pivot - left &lt; right - pivot {\n Self::quick_sort(left, pivot - 1, nums); // \u905e\u8ff4\u6392\u5e8f\u5de6\u5b50\u9663\u5217\n left = pivot + 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [pivot + 1, right]\n } else {\n Self::quick_sort(pivot + 1, right, nums); // \u905e\u8ff4\u6392\u5e8f\u53f3\u5b50\u9663\u5217\n right = pivot - 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [left, pivot - 1]\n }\n }\n}\n</code></pre> quick_sort.c<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff09 */\nvoid quickSortTailCall(int nums[], int left, int right) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\n while (left &lt; right) {\n // \u54e8\u5175\u5283\u5206\u64cd\u4f5c\n int pivot = partition(nums, left, right);\n // \u5c0d\u5169\u500b\u5b50\u9663\u5217\u4e2d\u8f03\u77ed\u7684\u90a3\u500b\u57f7\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left &lt; right - pivot) {\n // \u905e\u8ff4\u6392\u5e8f\u5de6\u5b50\u9663\u5217\n quickSortTailCall(nums, left, pivot - 1);\n // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [pivot + 1, right]\n left = pivot + 1;\n } else {\n // \u905e\u8ff4\u6392\u5e8f\u53f3\u5b50\u9663\u5217\n quickSortTailCall(nums, pivot + 1, right);\n // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [left, pivot - 1]\n right = pivot - 1;\n }\n }\n}\n</code></pre> quick_sort.kt<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff09 */\nfun quickSortTailCall(nums: IntArray, left: Int, right: Int) {\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\n var l = left\n var r = right\n while (l &lt; r) {\n // \u54e8\u5175\u5283\u5206\u64cd\u4f5c\n val pivot = partition(nums, l, r)\n // \u5c0d\u5169\u500b\u5b50\u9663\u5217\u4e2d\u8f03\u77ed\u7684\u90a3\u500b\u57f7\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - l &lt; r - pivot) {\n quickSort(nums, l, pivot - 1) // \u905e\u8ff4\u6392\u5e8f\u5de6\u5b50\u9663\u5217\n l = pivot + 1 // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [pivot + 1, right]\n } else {\n quickSort(nums, pivot + 1, r) // \u905e\u8ff4\u6392\u5e8f\u53f3\u5b50\u9663\u5217\n r = pivot - 1 // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [left, pivot - 1]\n }\n }\n}\n</code></pre> quick_sort.rb<pre><code>[class]{QuickSortTailCall}-[func]{quick_sort}\n</code></pre> quick_sort.zig<pre><code>// \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff09\nfn quickSort(nums: []i32, left_: usize, right_: usize) void {\n var left = left_;\n var right = right_;\n // \u5b50\u9663\u5217\u9577\u5ea6\u70ba 1 \u6642\u7d42\u6b62\u905e\u8ff4\n while (left &lt; right) {\n // \u54e8\u5175\u5283\u5206\u64cd\u4f5c\n var pivot = partition(nums, left, right);\n // \u5c0d\u5169\u500b\u5b50\u9663\u5217\u4e2d\u8f03\u77ed\u7684\u90a3\u500b\u57f7\u884c\u5feb\u901f\u6392\u5e8f\n if (pivot - left &lt; right - pivot) {\n quickSort(nums, left, pivot - 1); // \u905e\u8ff4\u6392\u5e8f\u5de6\u5b50\u9663\u5217\n left = pivot + 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [pivot + 1, right]\n } else {\n quickSort(nums, pivot + 1, right); // \u905e\u8ff4\u6392\u5e8f\u53f3\u5b50\u9663\u5217\n right = pivot - 1; // \u5269\u9918\u672a\u6392\u5e8f\u5340\u9593\u70ba [left, pivot - 1]\n }\n }\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_sorting/radix_sort/","title":"11.10 \u00a0 \u57fa\u6578\u6392\u5e8f","text":"<p>\u4e0a\u4e00\u7bc0\u4ecb\u7d39\u4e86\u8a08\u6578\u6392\u5e8f\uff0c\u5b83\u9069\u7528\u65bc\u8cc7\u6599\u91cf \\(n\\) \u8f03\u5927\u4f46\u8cc7\u6599\u7bc4\u570d \\(m\\) \u8f03\u5c0f\u7684\u60c5\u6cc1\u3002\u5047\u8a2d\u6211\u5011\u9700\u8981\u5c0d \\(n = 10^6\\) \u500b\u5b78\u865f\u9032\u884c\u6392\u5e8f\uff0c\u800c\u5b78\u865f\u662f\u4e00\u500b \\(8\\) \u4f4d\u6578\u5b57\uff0c\u9019\u610f\u5473\u8457\u8cc7\u6599\u7bc4\u570d \\(m = 10^8\\) \u975e\u5e38\u5927\uff0c\u4f7f\u7528\u8a08\u6578\u6392\u5e8f\u9700\u8981\u5206\u914d\u5927\u91cf\u8a18\u61b6\u9ad4\u7a7a\u9593\uff0c\u800c\u57fa\u6578\u6392\u5e8f\u53ef\u4ee5\u907f\u514d\u9019\u7a2e\u60c5\u6cc1\u3002</p> <p>\u57fa\u6578\u6392\u5e8f\uff08radix sort\uff09\u7684\u6838\u5fc3\u601d\u60f3\u8207\u8a08\u6578\u6392\u5e8f\u4e00\u81f4\uff0c\u4e5f\u900f\u904e\u7d71\u8a08\u500b\u6578\u4f86\u5be6\u73fe\u6392\u5e8f\u3002\u5728\u6b64\u57fa\u790e\u4e0a\uff0c\u57fa\u6578\u6392\u5e8f\u5229\u7528\u6578\u5b57\u5404\u4f4d\u4e4b\u9593\u7684\u905e\u9032\u95dc\u4fc2\uff0c\u4f9d\u6b21\u5c0d\u6bcf\u4e00\u4f4d\u9032\u884c\u6392\u5e8f\uff0c\u5f9e\u800c\u5f97\u5230\u6700\u7d42\u7684\u6392\u5e8f\u7d50\u679c\u3002</p>"},{"location":"chapter_sorting/radix_sort/#11101","title":"11.10.1 \u00a0 \u6f14\u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u4ee5\u5b78\u865f\u8cc7\u6599\u70ba\u4f8b\uff0c\u5047\u8a2d\u6578\u5b57\u7684\u6700\u4f4e\u4f4d\u662f\u7b2c \\(1\\) \u4f4d\uff0c\u6700\u9ad8\u4f4d\u662f\u7b2c \\(8\\) \u4f4d\uff0c\u57fa\u6578\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u5716 11-18 \u6240\u793a\u3002</p> <ol> <li>\u521d\u59cb\u5316\u4f4d\u6578 \\(k = 1\\) \u3002</li> <li>\u5c0d\u5b78\u865f\u7684\u7b2c \\(k\\) \u4f4d\u57f7\u884c\u201c\u8a08\u6578\u6392\u5e8f\u201d\u3002\u5b8c\u6210\u5f8c\uff0c\u8cc7\u6599\u6703\u6839\u64da\u7b2c \\(k\\) \u4f4d\u5f9e\u5c0f\u5230\u5927\u6392\u5e8f\u3002</li> <li>\u5c07 \\(k\\) \u589e\u52a0 \\(1\\) \uff0c\u7136\u5f8c\u8fd4\u56de\u6b65\u9a5f <code>2.</code> \u7e7c\u7e8c\u8fed\u4ee3\uff0c\u76f4\u5230\u6240\u6709\u4f4d\u90fd\u6392\u5e8f\u5b8c\u6210\u5f8c\u7d50\u675f\u3002</li> </ol> <p></p> <p> \u5716 11-18 \u00a0 \u57fa\u6578\u6392\u5e8f\u6f14\u7b97\u6cd5\u6d41\u7a0b </p> <p>\u4e0b\u9762\u5256\u6790\u7a0b\u5f0f\u78bc\u5be6\u73fe\u3002\u5c0d\u65bc\u4e00\u500b \\(d\\) \u9032\u4f4d\u5236\u7684\u6578\u5b57 \\(x\\) \uff0c\u8981\u7372\u53d6\u5176\u7b2c \\(k\\) \u4f4d \\(x_k\\) \uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8a08\u7b97\u516c\u5f0f\uff1a</p> \\[ x_k = \\lfloor\\frac{x}{d^{k-1}}\\rfloor \\bmod d \\] <p>\u5176\u4e2d \\(\\lfloor a \\rfloor\\) \u8868\u793a\u5c0d\u6d6e\u9ede\u6578 \\(a\\) \u5411\u4e0b\u53d6\u6574\uff0c\u800c \\(\\bmod \\: d\\) \u8868\u793a\u5c0d \\(d\\) \u53d6\u6a21\uff08\u53d6\u9918\uff09\u3002\u5c0d\u65bc\u5b78\u865f\u8cc7\u6599\uff0c\\(d = 10\\) \u4e14 \\(k \\in [1, 8]\\) \u3002</p> <p>\u6b64\u5916\uff0c\u6211\u5011\u9700\u8981\u5c0f\u5e45\u6539\u52d5\u8a08\u6578\u6392\u5e8f\u7a0b\u5f0f\u78bc\uff0c\u4f7f\u4e4b\u53ef\u4ee5\u6839\u64da\u6578\u5b57\u7684\u7b2c \\(k\\) \u4f4d\u9032\u884c\u6392\u5e8f\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig radix_sort.py<pre><code>def digit(num: int, exp: int) -&gt; int:\n \"\"\"\u7372\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1)\"\"\"\n # \u50b3\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u8907\u57f7\u884c\u6602\u8cb4\u7684\u6b21\u65b9\u8a08\u7b97\n return (num // exp) % 10\n\ndef counting_sort_digit(nums: list[int], exp: int):\n \"\"\"\u8a08\u6578\u6392\u5e8f\uff08\u6839\u64da nums \u7b2c k \u4f4d\u6392\u5e8f\uff09\"\"\"\n # \u5341\u9032\u4f4d\u5236\u7684\u4f4d\u7bc4\u570d\u70ba 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u9577\u5ea6\u70ba 10 \u7684\u6876\u9663\u5217\n counter = [0] * 10\n n = len(nums)\n # \u7d71\u8a08 0~9 \u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n for i in range(n):\n d = digit(nums[i], exp) # \u7372\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8a18\u70ba d\n counter[d] += 1 # \u7d71\u8a08\u6578\u5b57 d \u7684\u51fa\u73fe\u6b21\u6578\n # \u6c42\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u500b\u6578\u201d\u8f49\u63db\u70ba\u201c\u9663\u5217\u7d22\u5f15\u201d\n for i in range(1, 10):\n counter[i] += counter[i - 1]\n # \u5012\u5e8f\u8d70\u8a2a\uff0c\u6839\u64da\u6876\u5167\u7d71\u8a08\u7d50\u679c\uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165 res\n res = [0] * n\n for i in range(n - 1, -1, -1):\n d = digit(nums[i], exp)\n j = counter[d] - 1 # \u7372\u53d6 d \u5728\u9663\u5217\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i] # \u5c07\u7576\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d] -= 1 # \u5c07 d \u7684\u6578\u91cf\u6e1b 1\n # \u4f7f\u7528\u7d50\u679c\u8986\u84cb\u539f\u9663\u5217 nums\n for i in range(n):\n nums[i] = res[i]\n\ndef radix_sort(nums: list[int]):\n \"\"\"\u57fa\u6578\u6392\u5e8f\"\"\"\n # \u7372\u53d6\u9663\u5217\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u65bc\u5224\u65b7\u6700\u5927\u4f4d\u6578\n m = max(nums)\n # \u6309\u7167\u5f9e\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u9806\u5e8f\u8d70\u8a2a\n exp = 1\n while exp &lt;= m:\n # \u5c0d\u9663\u5217\u5143\u7d20\u7684\u7b2c k \u4f4d\u57f7\u884c\u8a08\u6578\u6392\u5e8f\n # k = 1 -&gt; exp = 1\n # k = 2 -&gt; exp = 10\n # \u5373 exp = 10^(k-1)\n counting_sort_digit(nums, exp)\n exp *= 10\n</code></pre> radix_sort.cpp<pre><code>/* \u7372\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n // \u50b3\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u8907\u57f7\u884c\u6602\u8cb4\u7684\u6b21\u65b9\u8a08\u7b97\n return (num / exp) % 10;\n}\n\n/* \u8a08\u6578\u6392\u5e8f\uff08\u6839\u64da nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(vector&lt;int&gt; &amp;nums, int exp) {\n // \u5341\u9032\u4f4d\u5236\u7684\u4f4d\u7bc4\u570d\u70ba 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u9577\u5ea6\u70ba 10 \u7684\u6876\u9663\u5217\n vector&lt;int&gt; counter(10, 0);\n int n = nums.size();\n // \u7d71\u8a08 0~9 \u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n for (int i = 0; i &lt; n; i++) {\n int d = digit(nums[i], exp); // \u7372\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8a18\u70ba d\n counter[d]++; // \u7d71\u8a08\u6578\u5b57 d \u7684\u51fa\u73fe\u6b21\u6578\n }\n // \u6c42\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u500b\u6578\u201d\u8f49\u63db\u70ba\u201c\u9663\u5217\u7d22\u5f15\u201d\n for (int i = 1; i &lt; 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u8d70\u8a2a\uff0c\u6839\u64da\u6876\u5167\u7d71\u8a08\u7d50\u679c\uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165 res\n vector&lt;int&gt; res(n, 0);\n for (int i = n - 1; i &gt;= 0; i--) {\n int d = digit(nums[i], exp);\n int j = counter[d] - 1; // \u7372\u53d6 d \u5728\u9663\u5217\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c07\u7576\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c07 d \u7684\u6578\u91cf\u6e1b 1\n }\n // \u4f7f\u7528\u7d50\u679c\u8986\u84cb\u539f\u9663\u5217 nums\n for (int i = 0; i &lt; n; i++)\n nums[i] = res[i];\n}\n\n/* \u57fa\u6578\u6392\u5e8f */\nvoid radixSort(vector&lt;int&gt; &amp;nums) {\n // \u7372\u53d6\u9663\u5217\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u65bc\u5224\u65b7\u6700\u5927\u4f4d\u6578\n int m = *max_element(nums.begin(), nums.end());\n // \u6309\u7167\u5f9e\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u9806\u5e8f\u8d70\u8a2a\n for (int exp = 1; exp &lt;= m; exp *= 10)\n // \u5c0d\u9663\u5217\u5143\u7d20\u7684\u7b2c k \u4f4d\u57f7\u884c\u8a08\u6578\u6392\u5e8f\n // k = 1 -&gt; exp = 1\n // k = 2 -&gt; exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n}\n</code></pre> radix_sort.java<pre><code>/* \u7372\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n // \u50b3\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u8907\u57f7\u884c\u6602\u8cb4\u7684\u6b21\u65b9\u8a08\u7b97\n return (num / exp) % 10;\n}\n\n/* \u8a08\u6578\u6392\u5e8f\uff08\u6839\u64da nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(int[] nums, int exp) {\n // \u5341\u9032\u4f4d\u5236\u7684\u4f4d\u7bc4\u570d\u70ba 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u9577\u5ea6\u70ba 10 \u7684\u6876\u9663\u5217\n int[] counter = new int[10];\n int n = nums.length;\n // \u7d71\u8a08 0~9 \u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n for (int i = 0; i &lt; n; i++) {\n int d = digit(nums[i], exp); // \u7372\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8a18\u70ba d\n counter[d]++; // \u7d71\u8a08\u6578\u5b57 d \u7684\u51fa\u73fe\u6b21\u6578\n }\n // \u6c42\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u500b\u6578\u201d\u8f49\u63db\u70ba\u201c\u9663\u5217\u7d22\u5f15\u201d\n for (int i = 1; i &lt; 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u8d70\u8a2a\uff0c\u6839\u64da\u6876\u5167\u7d71\u8a08\u7d50\u679c\uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165 res\n int[] res = new int[n];\n for (int i = n - 1; i &gt;= 0; i--) {\n int d = digit(nums[i], exp);\n int j = counter[d] - 1; // \u7372\u53d6 d \u5728\u9663\u5217\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c07\u7576\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c07 d \u7684\u6578\u91cf\u6e1b 1\n }\n // \u4f7f\u7528\u7d50\u679c\u8986\u84cb\u539f\u9663\u5217 nums\n for (int i = 0; i &lt; n; i++)\n nums[i] = res[i];\n}\n\n/* \u57fa\u6578\u6392\u5e8f */\nvoid radixSort(int[] nums) {\n // \u7372\u53d6\u9663\u5217\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u65bc\u5224\u65b7\u6700\u5927\u4f4d\u6578\n int m = Integer.MIN_VALUE;\n for (int num : nums)\n if (num &gt; m)\n m = num;\n // \u6309\u7167\u5f9e\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u9806\u5e8f\u8d70\u8a2a\n for (int exp = 1; exp &lt;= m; exp *= 10) {\n // \u5c0d\u9663\u5217\u5143\u7d20\u7684\u7b2c k \u4f4d\u57f7\u884c\u8a08\u6578\u6392\u5e8f\n // k = 1 -&gt; exp = 1\n // k = 2 -&gt; exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n }\n}\n</code></pre> radix_sort.cs<pre><code>/* \u7372\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint Digit(int num, int exp) {\n // \u50b3\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u8907\u57f7\u884c\u6602\u8cb4\u7684\u6b21\u65b9\u8a08\u7b97\n return (num / exp) % 10;\n}\n\n/* \u8a08\u6578\u6392\u5e8f\uff08\u6839\u64da nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid CountingSortDigit(int[] nums, int exp) {\n // \u5341\u9032\u4f4d\u5236\u7684\u4f4d\u7bc4\u570d\u70ba 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u9577\u5ea6\u70ba 10 \u7684\u6876\u9663\u5217\n int[] counter = new int[10];\n int n = nums.Length;\n // \u7d71\u8a08 0~9 \u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n for (int i = 0; i &lt; n; i++) {\n int d = Digit(nums[i], exp); // \u7372\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8a18\u70ba d\n counter[d]++; // \u7d71\u8a08\u6578\u5b57 d \u7684\u51fa\u73fe\u6b21\u6578\n }\n // \u6c42\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u500b\u6578\u201d\u8f49\u63db\u70ba\u201c\u9663\u5217\u7d22\u5f15\u201d\n for (int i = 1; i &lt; 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u8d70\u8a2a\uff0c\u6839\u64da\u6876\u5167\u7d71\u8a08\u7d50\u679c\uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165 res\n int[] res = new int[n];\n for (int i = n - 1; i &gt;= 0; i--) {\n int d = Digit(nums[i], exp);\n int j = counter[d] - 1; // \u7372\u53d6 d \u5728\u9663\u5217\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c07\u7576\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c07 d \u7684\u6578\u91cf\u6e1b 1\n }\n // \u4f7f\u7528\u7d50\u679c\u8986\u84cb\u539f\u9663\u5217 nums\n for (int i = 0; i &lt; n; i++) {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6578\u6392\u5e8f */\nvoid RadixSort(int[] nums) {\n // \u7372\u53d6\u9663\u5217\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u65bc\u5224\u65b7\u6700\u5927\u4f4d\u6578\n int m = int.MinValue;\n foreach (int num in nums) {\n if (num &gt; m) m = num;\n }\n // \u6309\u7167\u5f9e\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u9806\u5e8f\u8d70\u8a2a\n for (int exp = 1; exp &lt;= m; exp *= 10) {\n // \u5c0d\u9663\u5217\u5143\u7d20\u7684\u7b2c k \u4f4d\u57f7\u884c\u8a08\u6578\u6392\u5e8f\n // k = 1 -&gt; exp = 1\n // k = 2 -&gt; exp = 10\n // \u5373 exp = 10^(k-1)\n CountingSortDigit(nums, exp);\n }\n}\n</code></pre> radix_sort.go<pre><code>/* \u7372\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunc digit(num, exp int) int {\n // \u50b3\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u8907\u57f7\u884c\u6602\u8cb4\u7684\u6b21\u65b9\u8a08\u7b97\n return (num / exp) % 10\n}\n\n/* \u8a08\u6578\u6392\u5e8f\uff08\u6839\u64da nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunc countingSortDigit(nums []int, exp int) {\n // \u5341\u9032\u4f4d\u5236\u7684\u4f4d\u7bc4\u570d\u70ba 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u9577\u5ea6\u70ba 10 \u7684\u6876\u9663\u5217\n counter := make([]int, 10)\n n := len(nums)\n // \u7d71\u8a08 0~9 \u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n for i := 0; i &lt; n; i++ {\n d := digit(nums[i], exp) // \u7372\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8a18\u70ba d\n counter[d]++ // \u7d71\u8a08\u6578\u5b57 d \u7684\u51fa\u73fe\u6b21\u6578\n }\n // \u6c42\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u500b\u6578\u201d\u8f49\u63db\u70ba\u201c\u9663\u5217\u7d22\u5f15\u201d\n for i := 1; i &lt; 10; i++ {\n counter[i] += counter[i-1]\n }\n // \u5012\u5e8f\u8d70\u8a2a\uff0c\u6839\u64da\u6876\u5167\u7d71\u8a08\u7d50\u679c\uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165 res\n res := make([]int, n)\n for i := n - 1; i &gt;= 0; i-- {\n d := digit(nums[i], exp)\n j := counter[d] - 1 // \u7372\u53d6 d \u5728\u9663\u5217\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i] // \u5c07\u7576\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]-- // \u5c07 d \u7684\u6578\u91cf\u6e1b 1\n }\n // \u4f7f\u7528\u7d50\u679c\u8986\u84cb\u539f\u9663\u5217 nums\n for i := 0; i &lt; n; i++ {\n nums[i] = res[i]\n }\n}\n\n/* \u57fa\u6578\u6392\u5e8f */\nfunc radixSort(nums []int) {\n // \u7372\u53d6\u9663\u5217\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u65bc\u5224\u65b7\u6700\u5927\u4f4d\u6578\n max := math.MinInt\n for _, num := range nums {\n if num &gt; max {\n max = num\n }\n }\n // \u6309\u7167\u5f9e\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u9806\u5e8f\u8d70\u8a2a\n for exp := 1; max &gt;= exp; exp *= 10 {\n // \u5c0d\u9663\u5217\u5143\u7d20\u7684\u7b2c k \u4f4d\u57f7\u884c\u8a08\u6578\u6392\u5e8f\n // k = 1 -&gt; exp = 1\n // k = 2 -&gt; exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp)\n }\n}\n</code></pre> radix_sort.swift<pre><code>/* \u7372\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunc digit(num: Int, exp: Int) -&gt; Int {\n // \u50b3\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u8907\u57f7\u884c\u6602\u8cb4\u7684\u6b21\u65b9\u8a08\u7b97\n (num / exp) % 10\n}\n\n/* \u8a08\u6578\u6392\u5e8f\uff08\u6839\u64da nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunc countingSortDigit(nums: inout [Int], exp: Int) {\n // \u5341\u9032\u4f4d\u5236\u7684\u4f4d\u7bc4\u570d\u70ba 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u9577\u5ea6\u70ba 10 \u7684\u6876\u9663\u5217\n var counter = Array(repeating: 0, count: 10)\n // \u7d71\u8a08 0~9 \u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n for i in nums.indices {\n let d = digit(num: nums[i], exp: exp) // \u7372\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8a18\u70ba d\n counter[d] += 1 // \u7d71\u8a08\u6578\u5b57 d \u7684\u51fa\u73fe\u6b21\u6578\n }\n // \u6c42\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u500b\u6578\u201d\u8f49\u63db\u70ba\u201c\u9663\u5217\u7d22\u5f15\u201d\n for i in 1 ..&lt; 10 {\n counter[i] += counter[i - 1]\n }\n // \u5012\u5e8f\u8d70\u8a2a\uff0c\u6839\u64da\u6876\u5167\u7d71\u8a08\u7d50\u679c\uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165 res\n var res = Array(repeating: 0, count: nums.count)\n for i in nums.indices.reversed() {\n let d = digit(num: nums[i], exp: exp)\n let j = counter[d] - 1 // \u7372\u53d6 d \u5728\u9663\u5217\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i] // \u5c07\u7576\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d] -= 1 // \u5c07 d \u7684\u6578\u91cf\u6e1b 1\n }\n // \u4f7f\u7528\u7d50\u679c\u8986\u84cb\u539f\u9663\u5217 nums\n for i in nums.indices {\n nums[i] = res[i]\n }\n}\n\n/* \u57fa\u6578\u6392\u5e8f */\nfunc radixSort(nums: inout [Int]) {\n // \u7372\u53d6\u9663\u5217\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u65bc\u5224\u65b7\u6700\u5927\u4f4d\u6578\n var m = Int.min\n for num in nums {\n if num &gt; m {\n m = num\n }\n }\n // \u6309\u7167\u5f9e\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u9806\u5e8f\u8d70\u8a2a\n for exp in sequence(first: 1, next: { m &gt;= ($0 * 10) ? $0 * 10 : nil }) {\n // \u5c0d\u9663\u5217\u5143\u7d20\u7684\u7b2c k \u4f4d\u57f7\u884c\u8a08\u6578\u6392\u5e8f\n // k = 1 -&gt; exp = 1\n // k = 2 -&gt; exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums: &amp;nums, exp: exp)\n }\n}\n</code></pre> radix_sort.js<pre><code>/* \u7372\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunction digit(num, exp) {\n // \u50b3\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u8907\u57f7\u884c\u6602\u8cb4\u7684\u6b21\u65b9\u8a08\u7b97\n return Math.floor(num / exp) % 10;\n}\n\n/* \u8a08\u6578\u6392\u5e8f\uff08\u6839\u64da nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunction countingSortDigit(nums, exp) {\n // \u5341\u9032\u4f4d\u5236\u7684\u4f4d\u7bc4\u570d\u70ba 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u9577\u5ea6\u70ba 10 \u7684\u6876\u9663\u5217\n const counter = new Array(10).fill(0);\n const n = nums.length;\n // \u7d71\u8a08 0~9 \u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n for (let i = 0; i &lt; n; i++) {\n const d = digit(nums[i], exp); // \u7372\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8a18\u70ba d\n counter[d]++; // \u7d71\u8a08\u6578\u5b57 d \u7684\u51fa\u73fe\u6b21\u6578\n }\n // \u6c42\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u500b\u6578\u201d\u8f49\u63db\u70ba\u201c\u9663\u5217\u7d22\u5f15\u201d\n for (let i = 1; i &lt; 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u8d70\u8a2a\uff0c\u6839\u64da\u6876\u5167\u7d71\u8a08\u7d50\u679c\uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165 res\n const res = new Array(n).fill(0);\n for (let i = n - 1; i &gt;= 0; i--) {\n const d = digit(nums[i], exp);\n const j = counter[d] - 1; // \u7372\u53d6 d \u5728\u9663\u5217\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c07\u7576\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c07 d \u7684\u6578\u91cf\u6e1b 1\n }\n // \u4f7f\u7528\u7d50\u679c\u8986\u84cb\u539f\u9663\u5217 nums\n for (let i = 0; i &lt; n; i++) {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6578\u6392\u5e8f */\nfunction radixSort(nums) {\n // \u7372\u53d6\u9663\u5217\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u65bc\u5224\u65b7\u6700\u5927\u4f4d\u6578\n let m = Number.MIN_VALUE;\n for (const num of nums) {\n if (num &gt; m) {\n m = num;\n }\n }\n // \u6309\u7167\u5f9e\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u9806\u5e8f\u8d70\u8a2a\n for (let exp = 1; exp &lt;= m; exp *= 10) {\n // \u5c0d\u9663\u5217\u5143\u7d20\u7684\u7b2c k \u4f4d\u57f7\u884c\u8a08\u6578\u6392\u5e8f\n // k = 1 -&gt; exp = 1\n // k = 2 -&gt; exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n }\n}\n</code></pre> radix_sort.ts<pre><code>/* \u7372\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunction digit(num: number, exp: number): number {\n // \u50b3\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u8907\u57f7\u884c\u6602\u8cb4\u7684\u6b21\u65b9\u8a08\u7b97\n return Math.floor(num / exp) % 10;\n}\n\n/* \u8a08\u6578\u6392\u5e8f\uff08\u6839\u64da nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunction countingSortDigit(nums: number[], exp: number): void {\n // \u5341\u9032\u4f4d\u5236\u7684\u4f4d\u7bc4\u570d\u70ba 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u9577\u5ea6\u70ba 10 \u7684\u6876\u9663\u5217\n const counter = new Array(10).fill(0);\n const n = nums.length;\n // \u7d71\u8a08 0~9 \u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n for (let i = 0; i &lt; n; i++) {\n const d = digit(nums[i], exp); // \u7372\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8a18\u70ba d\n counter[d]++; // \u7d71\u8a08\u6578\u5b57 d \u7684\u51fa\u73fe\u6b21\u6578\n }\n // \u6c42\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u500b\u6578\u201d\u8f49\u63db\u70ba\u201c\u9663\u5217\u7d22\u5f15\u201d\n for (let i = 1; i &lt; 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u8d70\u8a2a\uff0c\u6839\u64da\u6876\u5167\u7d71\u8a08\u7d50\u679c\uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165 res\n const res = new Array(n).fill(0);\n for (let i = n - 1; i &gt;= 0; i--) {\n const d = digit(nums[i], exp);\n const j = counter[d] - 1; // \u7372\u53d6 d \u5728\u9663\u5217\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c07\u7576\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c07 d \u7684\u6578\u91cf\u6e1b 1\n }\n // \u4f7f\u7528\u7d50\u679c\u8986\u84cb\u539f\u9663\u5217 nums\n for (let i = 0; i &lt; n; i++) {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6578\u6392\u5e8f */\nfunction radixSort(nums: number[]): void {\n // \u7372\u53d6\u9663\u5217\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u65bc\u5224\u65b7\u6700\u5927\u4f4d\u6578\n let m = Number.MIN_VALUE;\n for (const num of nums) {\n if (num &gt; m) {\n m = num;\n }\n }\n // \u6309\u7167\u5f9e\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u9806\u5e8f\u8d70\u8a2a\n for (let exp = 1; exp &lt;= m; exp *= 10) {\n // \u5c0d\u9663\u5217\u5143\u7d20\u7684\u7b2c k \u4f4d\u57f7\u884c\u8a08\u6578\u6392\u5e8f\n // k = 1 -&gt; exp = 1\n // k = 2 -&gt; exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n }\n}\n</code></pre> radix_sort.dart<pre><code>/* \u7372\u53d6\u5143\u7d20 _num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int _num, int exp) {\n // \u50b3\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u8907\u57f7\u884c\u6602\u8cb4\u7684\u6b21\u65b9\u8a08\u7b97\n return (_num ~/ exp) % 10;\n}\n\n/* \u8a08\u6578\u6392\u5e8f\uff08\u6839\u64da nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(List&lt;int&gt; nums, int exp) {\n // \u5341\u9032\u4f4d\u5236\u7684\u4f4d\u7bc4\u570d\u70ba 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u9577\u5ea6\u70ba 10 \u7684\u6876\u9663\u5217\n List&lt;int&gt; counter = List&lt;int&gt;.filled(10, 0);\n int n = nums.length;\n // \u7d71\u8a08 0~9 \u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n for (int i = 0; i &lt; n; i++) {\n int d = digit(nums[i], exp); // \u7372\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8a18\u70ba d\n counter[d]++; // \u7d71\u8a08\u6578\u5b57 d \u7684\u51fa\u73fe\u6b21\u6578\n }\n // \u6c42\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u500b\u6578\u201d\u8f49\u63db\u70ba\u201c\u9663\u5217\u7d22\u5f15\u201d\n for (int i = 1; i &lt; 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u8d70\u8a2a\uff0c\u6839\u64da\u6876\u5167\u7d71\u8a08\u7d50\u679c\uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165 res\n List&lt;int&gt; res = List&lt;int&gt;.filled(n, 0);\n for (int i = n - 1; i &gt;= 0; i--) {\n int d = digit(nums[i], exp);\n int j = counter[d] - 1; // \u7372\u53d6 d \u5728\u9663\u5217\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c07\u7576\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c07 d \u7684\u6578\u91cf\u6e1b 1\n }\n // \u4f7f\u7528\u7d50\u679c\u8986\u84cb\u539f\u9663\u5217 nums\n for (int i = 0; i &lt; n; i++) nums[i] = res[i];\n}\n\n/* \u57fa\u6578\u6392\u5e8f */\nvoid radixSort(List&lt;int&gt; nums) {\n // \u7372\u53d6\u9663\u5217\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u65bc\u5224\u65b7\u6700\u5927\u4f4d\u6578\n // dart \u4e2d int \u7684\u9577\u5ea6\u662f 64 \u4f4d\u7684\n int m = -1 &lt;&lt; 63;\n for (int _num in nums) if (_num &gt; m) m = _num;\n // \u6309\u7167\u5f9e\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u9806\u5e8f\u8d70\u8a2a\n for (int exp = 1; exp &lt;= m; exp *= 10)\n // \u5c0d\u9663\u5217\u5143\u7d20\u7684\u7b2c k \u4f4d\u57f7\u884c\u8a08\u6578\u6392\u5e8f\n // k = 1 -&gt; exp = 1\n // k = 2 -&gt; exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp);\n}\n</code></pre> radix_sort.rs<pre><code>/* \u7372\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfn digit(num: i32, exp: i32) -&gt; usize {\n // \u50b3\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u8907\u57f7\u884c\u6602\u8cb4\u7684\u6b21\u65b9\u8a08\u7b97\n return ((num / exp) % 10) as usize;\n}\n\n/* \u8a08\u6578\u6392\u5e8f\uff08\u6839\u64da nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfn counting_sort_digit(nums: &amp;mut [i32], exp: i32) {\n // \u5341\u9032\u4f4d\u5236\u7684\u4f4d\u7bc4\u570d\u70ba 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u9577\u5ea6\u70ba 10 \u7684\u6876\u9663\u5217\n let mut counter = [0; 10];\n let n = nums.len();\n // \u7d71\u8a08 0~9 \u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n for i in 0..n {\n let d = digit(nums[i], exp); // \u7372\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8a18\u70ba d\n counter[d] += 1; // \u7d71\u8a08\u6578\u5b57 d \u7684\u51fa\u73fe\u6b21\u6578\n }\n // \u6c42\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u500b\u6578\u201d\u8f49\u63db\u70ba\u201c\u9663\u5217\u7d22\u5f15\u201d\n for i in 1..10 {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u8d70\u8a2a\uff0c\u6839\u64da\u6876\u5167\u7d71\u8a08\u7d50\u679c\uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165 res\n let mut res = vec![0; n];\n for i in (0..n).rev() {\n let d = digit(nums[i], exp);\n let j = counter[d] - 1; // \u7372\u53d6 d \u5728\u9663\u5217\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c07\u7576\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d] -= 1; // \u5c07 d \u7684\u6578\u91cf\u6e1b 1\n }\n // \u4f7f\u7528\u7d50\u679c\u8986\u84cb\u539f\u9663\u5217 nums\n for i in 0..n {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6578\u6392\u5e8f */\nfn radix_sort(nums: &amp;mut [i32]) {\n // \u7372\u53d6\u9663\u5217\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u65bc\u5224\u65b7\u6700\u5927\u4f4d\u6578\n let m = *nums.into_iter().max().unwrap();\n // \u6309\u7167\u5f9e\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u9806\u5e8f\u8d70\u8a2a\n let mut exp = 1;\n while exp &lt;= m {\n counting_sort_digit(nums, exp);\n exp *= 10;\n }\n}\n</code></pre> radix_sort.c<pre><code>/* \u7372\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n // \u50b3\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u8907\u57f7\u884c\u6602\u8cb4\u7684\u6b21\u65b9\u8a08\u7b97\n return (num / exp) % 10;\n}\n\n/* \u8a08\u6578\u6392\u5e8f\uff08\u6839\u64da nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(int nums[], int size, int exp) {\n // \u5341\u9032\u4f4d\u5236\u7684\u4f4d\u7bc4\u570d\u70ba 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u9577\u5ea6\u70ba 10 \u7684\u6876\u9663\u5217\n int *counter = (int *)malloc((sizeof(int) * 10));\n // \u7d71\u8a08 0~9 \u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n for (int i = 0; i &lt; size; i++) {\n // \u7372\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8a18\u70ba d\n int d = digit(nums[i], exp);\n // \u7d71\u8a08\u6578\u5b57 d \u7684\u51fa\u73fe\u6b21\u6578\n counter[d]++;\n }\n // \u6c42\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u500b\u6578\u201d\u8f49\u63db\u70ba\u201c\u9663\u5217\u7d22\u5f15\u201d\n for (int i = 1; i &lt; 10; i++) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u8d70\u8a2a\uff0c\u6839\u64da\u6876\u5167\u7d71\u8a08\u7d50\u679c\uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165 res\n int *res = (int *)malloc(sizeof(int) * size);\n for (int i = size - 1; i &gt;= 0; i--) {\n int d = digit(nums[i], exp);\n int j = counter[d] - 1; // \u7372\u53d6 d \u5728\u9663\u5217\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c07\u7576\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]--; // \u5c07 d \u7684\u6578\u91cf\u6e1b 1\n }\n // \u4f7f\u7528\u7d50\u679c\u8986\u84cb\u539f\u9663\u5217 nums\n for (int i = 0; i &lt; size; i++) {\n nums[i] = res[i];\n }\n}\n\n/* \u57fa\u6578\u6392\u5e8f */\nvoid radixSort(int nums[], int size) {\n // \u7372\u53d6\u9663\u5217\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u65bc\u5224\u65b7\u6700\u5927\u4f4d\u6578\n int max = INT32_MIN;\n for (size_t i = 0; i &lt; size - 1; i++) {\n if (nums[i] &gt; max) {\n max = nums[i];\n }\n }\n // \u6309\u7167\u5f9e\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u9806\u5e8f\u8d70\u8a2a\n for (int exp = 1; max &gt;= exp; exp *= 10)\n // \u5c0d\u9663\u5217\u5143\u7d20\u7684\u7b2c k \u4f4d\u57f7\u884c\u8a08\u6578\u6392\u5e8f\n // k = 1 -&gt; exp = 1\n // k = 2 -&gt; exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, size, exp);\n}\n</code></pre> radix_sort.kt<pre><code>/* \u7372\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfun digit(num: Int, exp: Int): Int {\n // \u50b3\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u8907\u57f7\u884c\u6602\u8cb4\u7684\u6b21\u65b9\u8a08\u7b97\n return (num / exp) % 10\n}\n\n/* \u8a08\u6578\u6392\u5e8f\uff08\u6839\u64da nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfun countingSortDigit(nums: IntArray, exp: Int) {\n // \u5341\u9032\u4f4d\u5236\u7684\u4f4d\u7bc4\u570d\u70ba 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u9577\u5ea6\u70ba 10 \u7684\u6876\u9663\u5217\n val counter = IntArray(10)\n val n = nums.size\n // \u7d71\u8a08 0~9 \u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n for (i in 0..&lt;n) {\n val d = digit(nums[i], exp) // \u7372\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8a18\u70ba d\n counter[d]++ // \u7d71\u8a08\u6578\u5b57 d \u7684\u51fa\u73fe\u6b21\u6578\n }\n // \u6c42\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u500b\u6578\u201d\u8f49\u63db\u70ba\u201c\u9663\u5217\u7d22\u5f15\u201d\n for (i in 1..9) {\n counter[i] += counter[i - 1]\n }\n // \u5012\u5e8f\u8d70\u8a2a\uff0c\u6839\u64da\u6876\u5167\u7d71\u8a08\u7d50\u679c\uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165 res\n val res = IntArray(n)\n for (i in n - 1 downTo 0) {\n val d = digit(nums[i], exp)\n val j = counter[d] - 1 // \u7372\u53d6 d \u5728\u9663\u5217\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i] // \u5c07\u7576\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d]-- // \u5c07 d \u7684\u6578\u91cf\u6e1b 1\n }\n // \u4f7f\u7528\u7d50\u679c\u8986\u84cb\u539f\u9663\u5217 nums\n for (i in 0..&lt;n)\n nums[i] = res[i]\n}\n\n/* \u57fa\u6578\u6392\u5e8f */\nfun radixSort(nums: IntArray) {\n // \u7372\u53d6\u9663\u5217\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u65bc\u5224\u65b7\u6700\u5927\u4f4d\u6578\n var m = Int.MIN_VALUE\n for (num in nums) if (num &gt; m) m = num\n var exp = 1\n // \u6309\u7167\u5f9e\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u9806\u5e8f\u8d70\u8a2a\n while (exp &lt;= m) {\n // \u5c0d\u9663\u5217\u5143\u7d20\u7684\u7b2c k \u4f4d\u57f7\u884c\u8a08\u6578\u6392\u5e8f\n // k = 1 -&gt; exp = 1\n // k = 2 -&gt; exp = 10\n // \u5373 exp = 10^(k-1)\n countingSortDigit(nums, exp)\n exp *= 10\n }\n}\n</code></pre> radix_sort.rb<pre><code>[class]{}-[func]{digit}\n\n[class]{}-[func]{counting_sort_digit}\n\n[class]{}-[func]{radix_sort}\n</code></pre> radix_sort.zig<pre><code>// \u7372\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1)\nfn digit(num: i32, exp: i32) i32 {\n // \u50b3\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u8907\u57f7\u884c\u6602\u8cb4\u7684\u6b21\u65b9\u8a08\u7b97\n return @mod(@divFloor(num, exp), 10);\n}\n\n// \u8a08\u6578\u6392\u5e8f\uff08\u6839\u64da nums \u7b2c k \u4f4d\u6392\u5e8f\uff09\nfn countingSortDigit(nums: []i32, exp: i32) !void {\n // \u5341\u9032\u4f4d\u5236\u7684\u4f4d\u7bc4\u570d\u70ba 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u9577\u5ea6\u70ba 10 \u7684\u6876\u9663\u5217\n var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);\n // defer mem_arena.deinit();\n const mem_allocator = mem_arena.allocator();\n var counter = try mem_allocator.alloc(usize, 10);\n @memset(counter, 0);\n var n = nums.len;\n // \u7d71\u8a08 0~9 \u5404\u6578\u5b57\u7684\u51fa\u73fe\u6b21\u6578\n for (nums) |num| {\n var d: u32 = @bitCast(digit(num, exp)); // \u7372\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8a18\u70ba d\n counter[d] += 1; // \u7d71\u8a08\u6578\u5b57 d \u7684\u51fa\u73fe\u6b21\u6578\n }\n // \u6c42\u524d\u7db4\u548c\uff0c\u5c07\u201c\u51fa\u73fe\u500b\u6578\u201d\u8f49\u63db\u70ba\u201c\u9663\u5217\u7d22\u5f15\u201d\n var i: usize = 1;\n while (i &lt; 10) : (i += 1) {\n counter[i] += counter[i - 1];\n }\n // \u5012\u5e8f\u8d70\u8a2a\uff0c\u6839\u64da\u6876\u5167\u7d71\u8a08\u7d50\u679c\uff0c\u5c07\u5404\u5143\u7d20\u586b\u5165 res\n var res = try mem_allocator.alloc(i32, n);\n i = n - 1;\n while (i &gt;= 0) : (i -= 1) {\n var d: u32 = @bitCast(digit(nums[i], exp));\n var j = counter[d] - 1; // \u7372\u53d6 d \u5728\u9663\u5217\u4e2d\u7684\u7d22\u5f15 j\n res[j] = nums[i]; // \u5c07\u7576\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\n counter[d] -= 1; // \u5c07 d \u7684\u6578\u91cf\u6e1b 1\n if (i == 0) break;\n }\n // \u4f7f\u7528\u7d50\u679c\u8986\u84cb\u539f\u9663\u5217 nums\n i = 0;\n while (i &lt; n) : (i += 1) {\n nums[i] = res[i];\n }\n}\n\n// \u57fa\u6578\u6392\u5e8f\nfn radixSort(nums: []i32) !void {\n // \u7372\u53d6\u9663\u5217\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u65bc\u5224\u65b7\u6700\u5927\u4f4d\u6578\n var m: i32 = std.math.minInt(i32);\n for (nums) |num| {\n if (num &gt; m) m = num;\n }\n // \u6309\u7167\u5f9e\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u9806\u5e8f\u8d70\u8a2a\n var exp: i32 = 1;\n while (exp &lt;= m) : (exp *= 10) {\n // \u5c0d\u9663\u5217\u5143\u7d20\u7684\u7b2c k \u4f4d\u57f7\u884c\u8a08\u6578\u6392\u5e8f\n // k = 1 -&gt; exp = 1\n // k = 2 -&gt; exp = 10\n // \u5373 exp = 10^(k-1)\n try countingSortDigit(nums, exp); \n }\n} \n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u70ba\u4ec0\u9ebc\u5f9e\u6700\u4f4e\u4f4d\u958b\u59cb\u6392\u5e8f\uff1f</p> <p>\u5728\u9023\u7e8c\u7684\u6392\u5e8f\u8f2a\u6b21\u4e2d\uff0c\u5f8c\u4e00\u8f2a\u6392\u5e8f\u6703\u8986\u84cb\u524d\u4e00\u8f2a\u6392\u5e8f\u7684\u7d50\u679c\u3002\u8209\u4f8b\u4f86\u8aaa\uff0c\u5982\u679c\u7b2c\u4e00\u8f2a\u6392\u5e8f\u7d50\u679c \\(a &lt; b\\) \uff0c\u800c\u7b2c\u4e8c\u8f2a\u6392\u5e8f\u7d50\u679c \\(a &gt; b\\) \uff0c\u90a3\u9ebc\u7b2c\u4e8c\u8f2a\u7684\u7d50\u679c\u5c07\u53d6\u4ee3\u7b2c\u4e00\u8f2a\u7684\u7d50\u679c\u3002\u7531\u65bc\u6578\u5b57\u7684\u9ad8\u4f4d\u512a\u5148\u9806\u5e8f\u9ad8\u65bc\u4f4e\u4f4d\uff0c\u56e0\u6b64\u61c9\u8a72\u5148\u6392\u5e8f\u4f4e\u4f4d\u518d\u6392\u5e8f\u9ad8\u4f4d\u3002</p>"},{"location":"chapter_sorting/radix_sort/#11102","title":"11.10.2 \u00a0 \u6f14\u7b97\u6cd5\u7279\u6027","text":"<p>\u76f8\u8f03\u65bc\u8a08\u6578\u6392\u5e8f\uff0c\u57fa\u6578\u6392\u5e8f\u9069\u7528\u65bc\u6578\u503c\u7bc4\u570d\u8f03\u5927\u7684\u60c5\u6cc1\uff0c\u4f46\u524d\u63d0\u662f\u8cc7\u6599\u5fc5\u9808\u53ef\u4ee5\u8868\u793a\u70ba\u56fa\u5b9a\u4f4d\u6578\u7684\u683c\u5f0f\uff0c\u4e14\u4f4d\u6578\u4e0d\u80fd\u904e\u5927\u3002\u4f8b\u5982\uff0c\u6d6e\u9ede\u6578\u4e0d\u9069\u5408\u4f7f\u7528\u57fa\u6578\u6392\u5e8f\uff0c\u56e0\u70ba\u5176\u4f4d\u6578 \\(k\\) \u904e\u5927\uff0c\u53ef\u80fd\u5c0e\u81f4\u6642\u9593\u8907\u96dc\u5ea6 \\(O(nk) \\gg O(n^2)\\) \u3002</p> <ul> <li>\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(nk)\\)\u3001\u975e\u81ea\u9069\u61c9\u6392\u5e8f\uff1a\u8a2d\u8cc7\u6599\u91cf\u70ba \\(n\\)\u3001\u8cc7\u6599\u70ba \\(d\\) \u9032\u4f4d\u5236\u3001\u6700\u5927\u4f4d\u6578\u70ba \\(k\\) \uff0c\u5247\u5c0d\u67d0\u4e00\u4f4d\u57f7\u884c\u8a08\u6578\u6392\u5e8f\u4f7f\u7528 \\(O(n + d)\\) \u6642\u9593\uff0c\u6392\u5e8f\u6240\u6709 \\(k\\) \u4f4d\u4f7f\u7528 \\(O((n + d)k)\\) \u6642\u9593\u3002\u901a\u5e38\u60c5\u6cc1\u4e0b\uff0c\\(d\\) \u548c \\(k\\) \u90fd\u76f8\u5c0d\u8f03\u5c0f\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u8da8\u5411 \\(O(n)\\) \u3002</li> <li>\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n + d)\\)\u3001\u975e\u539f\u5730\u6392\u5e8f\uff1a\u8207\u8a08\u6578\u6392\u5e8f\u76f8\u540c\uff0c\u57fa\u6578\u6392\u5e8f\u9700\u8981\u85c9\u52a9\u9577\u5ea6\u70ba \\(n\\) \u548c \\(d\\) \u7684\u9663\u5217 <code>res</code> \u548c <code>counter</code> \u3002</li> <li>\u7a69\u5b9a\u6392\u5e8f\uff1a\u7576\u8a08\u6578\u6392\u5e8f\u7a69\u5b9a\u6642\uff0c\u57fa\u6578\u6392\u5e8f\u4e5f\u7a69\u5b9a\uff1b\u7576\u8a08\u6578\u6392\u5e8f\u4e0d\u7a69\u5b9a\u6642\uff0c\u57fa\u6578\u6392\u5e8f\u7121\u6cd5\u4fdd\u8b49\u5f97\u5230\u6b63\u78ba\u7684\u6392\u5e8f\u7d50\u679c\u3002</li> </ul>"},{"location":"chapter_sorting/selection_sort/","title":"11.2 \u00a0 \u9078\u64c7\u6392\u5e8f","text":"<p>\u9078\u64c7\u6392\u5e8f\uff08selection sort\uff09\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u7c21\u55ae\uff1a\u958b\u555f\u4e00\u500b\u8ff4\u5708\uff0c\u6bcf\u8f2a\u5f9e\u672a\u6392\u5e8f\u5340\u9593\u9078\u64c7\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u5c07\u5176\u653e\u5230\u5df2\u6392\u5e8f\u5340\u9593\u7684\u672b\u5c3e\u3002</p> <p>\u8a2d\u9663\u5217\u7684\u9577\u5ea6\u70ba \\(n\\) \uff0c\u9078\u64c7\u6392\u5e8f\u7684\u6f14\u7b97\u6cd5\u6d41\u7a0b\u5982\u5716 11-2 \u6240\u793a\u3002</p> <ol> <li>\u521d\u59cb\u72c0\u614b\u4e0b\uff0c\u6240\u6709\u5143\u7d20\u672a\u6392\u5e8f\uff0c\u5373\u672a\u6392\u5e8f\uff08\u7d22\u5f15\uff09\u5340\u9593\u70ba \\([0, n-1]\\) \u3002</li> <li>\u9078\u53d6\u5340\u9593 \\([0, n-1]\\) \u4e2d\u7684\u6700\u5c0f\u5143\u7d20\uff0c\u5c07\u5176\u8207\u7d22\u5f15 \\(0\\) \u8655\u7684\u5143\u7d20\u4ea4\u63db\u3002\u5b8c\u6210\u5f8c\uff0c\u9663\u5217\u524d 1 \u500b\u5143\u7d20\u5df2\u6392\u5e8f\u3002</li> <li>\u9078\u53d6\u5340\u9593 \\([1, n-1]\\) \u4e2d\u7684\u6700\u5c0f\u5143\u7d20\uff0c\u5c07\u5176\u8207\u7d22\u5f15 \\(1\\) \u8655\u7684\u5143\u7d20\u4ea4\u63db\u3002\u5b8c\u6210\u5f8c\uff0c\u9663\u5217\u524d 2 \u500b\u5143\u7d20\u5df2\u6392\u5e8f\u3002</li> <li>\u4ee5\u6b64\u985e\u63a8\u3002\u7d93\u904e \\(n - 1\\) \u8f2a\u9078\u64c7\u8207\u4ea4\u63db\u5f8c\uff0c\u9663\u5217\u524d \\(n - 1\\) \u500b\u5143\u7d20\u5df2\u6392\u5e8f\u3002</li> <li>\u50c5\u5269\u7684\u4e00\u500b\u5143\u7d20\u5fc5\u5b9a\u662f\u6700\u5927\u5143\u7d20\uff0c\u7121\u9808\u6392\u5e8f\uff0c\u56e0\u6b64\u9663\u5217\u6392\u5e8f\u5b8c\u6210\u3002</li> </ol> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt;&lt;10&gt;&lt;11&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 11-2 \u00a0 \u9078\u64c7\u6392\u5e8f\u6b65\u9a5f </p> <p>\u5728\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u7528 \\(k\\) \u4f86\u8a18\u9304\u672a\u6392\u5e8f\u5340\u9593\u5167\u7684\u6700\u5c0f\u5143\u7d20\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig selection_sort.py<pre><code>def selection_sort(nums: list[int]):\n \"\"\"\u9078\u64c7\u6392\u5e8f\"\"\"\n n = len(nums)\n # \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [i, n-1]\n for i in range(n - 1):\n # \u5167\u8ff4\u5708\uff1a\u627e\u5230\u672a\u6392\u5e8f\u5340\u9593\u5167\u7684\u6700\u5c0f\u5143\u7d20\n k = i\n for j in range(i + 1, n):\n if nums[j] &lt; nums[k]:\n k = j # \u8a18\u9304\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n # \u5c07\u8a72\u6700\u5c0f\u5143\u7d20\u8207\u672a\u6392\u5e8f\u5340\u9593\u7684\u9996\u500b\u5143\u7d20\u4ea4\u63db\n nums[i], nums[k] = nums[k], nums[i]\n</code></pre> selection_sort.cpp<pre><code>/* \u9078\u64c7\u6392\u5e8f */\nvoid selectionSort(vector&lt;int&gt; &amp;nums) {\n int n = nums.size();\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [i, n-1]\n for (int i = 0; i &lt; n - 1; i++) {\n // \u5167\u8ff4\u5708\uff1a\u627e\u5230\u672a\u6392\u5e8f\u5340\u9593\u5167\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j &lt; n; j++) {\n if (nums[j] &lt; nums[k])\n k = j; // \u8a18\u9304\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c07\u8a72\u6700\u5c0f\u5143\u7d20\u8207\u672a\u6392\u5e8f\u5340\u9593\u7684\u9996\u500b\u5143\u7d20\u4ea4\u63db\n swap(nums[i], nums[k]);\n }\n}\n</code></pre> selection_sort.java<pre><code>/* \u9078\u64c7\u6392\u5e8f */\nvoid selectionSort(int[] nums) {\n int n = nums.length;\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [i, n-1]\n for (int i = 0; i &lt; n - 1; i++) {\n // \u5167\u8ff4\u5708\uff1a\u627e\u5230\u672a\u6392\u5e8f\u5340\u9593\u5167\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j &lt; n; j++) {\n if (nums[j] &lt; nums[k])\n k = j; // \u8a18\u9304\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c07\u8a72\u6700\u5c0f\u5143\u7d20\u8207\u672a\u6392\u5e8f\u5340\u9593\u7684\u9996\u500b\u5143\u7d20\u4ea4\u63db\n int temp = nums[i];\n nums[i] = nums[k];\n nums[k] = temp;\n }\n}\n</code></pre> selection_sort.cs<pre><code>/* \u9078\u64c7\u6392\u5e8f */\nvoid SelectionSort(int[] nums) {\n int n = nums.Length;\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [i, n-1]\n for (int i = 0; i &lt; n - 1; i++) {\n // \u5167\u8ff4\u5708\uff1a\u627e\u5230\u672a\u6392\u5e8f\u5340\u9593\u5167\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j &lt; n; j++) {\n if (nums[j] &lt; nums[k])\n k = j; // \u8a18\u9304\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c07\u8a72\u6700\u5c0f\u5143\u7d20\u8207\u672a\u6392\u5e8f\u5340\u9593\u7684\u9996\u500b\u5143\u7d20\u4ea4\u63db\n (nums[k], nums[i]) = (nums[i], nums[k]);\n }\n}\n</code></pre> selection_sort.go<pre><code>/* \u9078\u64c7\u6392\u5e8f */\nfunc selectionSort(nums []int) {\n n := len(nums)\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [i, n-1]\n for i := 0; i &lt; n-1; i++ {\n // \u5167\u8ff4\u5708\uff1a\u627e\u5230\u672a\u6392\u5e8f\u5340\u9593\u5167\u7684\u6700\u5c0f\u5143\u7d20\n k := i\n for j := i + 1; j &lt; n; j++ {\n if nums[j] &lt; nums[k] {\n // \u8a18\u9304\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n k = j\n }\n }\n // \u5c07\u8a72\u6700\u5c0f\u5143\u7d20\u8207\u672a\u6392\u5e8f\u5340\u9593\u7684\u9996\u500b\u5143\u7d20\u4ea4\u63db\n nums[i], nums[k] = nums[k], nums[i]\n\n }\n}\n</code></pre> selection_sort.swift<pre><code>/* \u9078\u64c7\u6392\u5e8f */\nfunc selectionSort(nums: inout [Int]) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [i, n-1]\n for i in nums.indices.dropLast() {\n // \u5167\u8ff4\u5708\uff1a\u627e\u5230\u672a\u6392\u5e8f\u5340\u9593\u5167\u7684\u6700\u5c0f\u5143\u7d20\n var k = i\n for j in nums.indices.dropFirst(i + 1) {\n if nums[j] &lt; nums[k] {\n k = j // \u8a18\u9304\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n }\n // \u5c07\u8a72\u6700\u5c0f\u5143\u7d20\u8207\u672a\u6392\u5e8f\u5340\u9593\u7684\u9996\u500b\u5143\u7d20\u4ea4\u63db\n nums.swapAt(i, k)\n }\n}\n</code></pre> selection_sort.js<pre><code>/* \u9078\u64c7\u6392\u5e8f */\nfunction selectionSort(nums) {\n let n = nums.length;\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [i, n-1]\n for (let i = 0; i &lt; n - 1; i++) {\n // \u5167\u8ff4\u5708\uff1a\u627e\u5230\u672a\u6392\u5e8f\u5340\u9593\u5167\u7684\u6700\u5c0f\u5143\u7d20\n let k = i;\n for (let j = i + 1; j &lt; n; j++) {\n if (nums[j] &lt; nums[k]) {\n k = j; // \u8a18\u9304\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n }\n // \u5c07\u8a72\u6700\u5c0f\u5143\u7d20\u8207\u672a\u6392\u5e8f\u5340\u9593\u7684\u9996\u500b\u5143\u7d20\u4ea4\u63db\n [nums[i], nums[k]] = [nums[k], nums[i]];\n }\n}\n</code></pre> selection_sort.ts<pre><code>/* \u9078\u64c7\u6392\u5e8f */\nfunction selectionSort(nums: number[]): void {\n let n = nums.length;\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [i, n-1]\n for (let i = 0; i &lt; n - 1; i++) {\n // \u5167\u8ff4\u5708\uff1a\u627e\u5230\u672a\u6392\u5e8f\u5340\u9593\u5167\u7684\u6700\u5c0f\u5143\u7d20\n let k = i;\n for (let j = i + 1; j &lt; n; j++) {\n if (nums[j] &lt; nums[k]) {\n k = j; // \u8a18\u9304\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n }\n // \u5c07\u8a72\u6700\u5c0f\u5143\u7d20\u8207\u672a\u6392\u5e8f\u5340\u9593\u7684\u9996\u500b\u5143\u7d20\u4ea4\u63db\n [nums[i], nums[k]] = [nums[k], nums[i]];\n }\n}\n</code></pre> selection_sort.dart<pre><code>/* \u9078\u64c7\u6392\u5e8f */\nvoid selectionSort(List&lt;int&gt; nums) {\n int n = nums.length;\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [i, n-1]\n for (int i = 0; i &lt; n - 1; i++) {\n // \u5167\u8ff4\u5708\uff1a\u627e\u5230\u672a\u6392\u5e8f\u5340\u9593\u5167\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j &lt; n; j++) {\n if (nums[j] &lt; nums[k]) k = j; // \u8a18\u9304\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c07\u8a72\u6700\u5c0f\u5143\u7d20\u8207\u672a\u6392\u5e8f\u5340\u9593\u7684\u9996\u500b\u5143\u7d20\u4ea4\u63db\n int temp = nums[i];\n nums[i] = nums[k];\n nums[k] = temp;\n }\n}\n</code></pre> selection_sort.rs<pre><code>/* \u9078\u64c7\u6392\u5e8f */\nfn selection_sort(nums: &amp;mut [i32]) {\n if nums.is_empty() {\n return;\n }\n let n = nums.len();\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [i, n-1]\n for i in 0..n - 1 {\n // \u5167\u8ff4\u5708\uff1a\u627e\u5230\u672a\u6392\u5e8f\u5340\u9593\u5167\u7684\u6700\u5c0f\u5143\u7d20\n let mut k = i;\n for j in i + 1..n {\n if nums[j] &lt; nums[k] {\n k = j; // \u8a18\u9304\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n }\n // \u5c07\u8a72\u6700\u5c0f\u5143\u7d20\u8207\u672a\u6392\u5e8f\u5340\u9593\u7684\u9996\u500b\u5143\u7d20\u4ea4\u63db\n nums.swap(i, k);\n }\n}\n</code></pre> selection_sort.c<pre><code>/* \u9078\u64c7\u6392\u5e8f */\nvoid selectionSort(int nums[], int n) {\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [i, n-1]\n for (int i = 0; i &lt; n - 1; i++) {\n // \u5167\u8ff4\u5708\uff1a\u627e\u5230\u672a\u6392\u5e8f\u5340\u9593\u5167\u7684\u6700\u5c0f\u5143\u7d20\n int k = i;\n for (int j = i + 1; j &lt; n; j++) {\n if (nums[j] &lt; nums[k])\n k = j; // \u8a18\u9304\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c07\u8a72\u6700\u5c0f\u5143\u7d20\u8207\u672a\u6392\u5e8f\u5340\u9593\u7684\u9996\u500b\u5143\u7d20\u4ea4\u63db\n int temp = nums[i];\n nums[i] = nums[k];\n nums[k] = temp;\n }\n}\n</code></pre> selection_sort.kt<pre><code>/* \u9078\u64c7\u6392\u5e8f */\nfun selectionSort(nums: IntArray) {\n val n = nums.size\n // \u5916\u8ff4\u5708\uff1a\u672a\u6392\u5e8f\u5340\u9593\u70ba [i, n-1]\n for (i in 0..&lt;n - 1) {\n var k = i\n // \u5167\u8ff4\u5708\uff1a\u627e\u5230\u672a\u6392\u5e8f\u5340\u9593\u5167\u7684\u6700\u5c0f\u5143\u7d20\n for (j in i + 1..&lt;n) {\n if (nums[j] &lt; nums[k])\n k = j // \u8a18\u9304\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n }\n // \u5c07\u8a72\u6700\u5c0f\u5143\u7d20\u8207\u672a\u6392\u5e8f\u5340\u9593\u7684\u9996\u500b\u5143\u7d20\u4ea4\u63db\n val temp = nums[i]\n nums[i] = nums[k]\n nums[k] = temp\n }\n}\n</code></pre> selection_sort.rb<pre><code>[class]{}-[func]{selection_sort}\n</code></pre> selection_sort.zig<pre><code>[class]{}-[func]{selectionSort}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_sorting/selection_sort/#1121","title":"11.2.1 \u00a0 \u6f14\u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\)\u3001\u975e\u81ea\u9069\u61c9\u6392\u5e8f\uff1a\u5916\u8ff4\u5708\u5171 \\(n - 1\\) \u8f2a\uff0c\u7b2c\u4e00\u8f2a\u7684\u672a\u6392\u5e8f\u5340\u9593\u9577\u5ea6\u70ba \\(n\\) \uff0c\u6700\u5f8c\u4e00\u8f2a\u7684\u672a\u6392\u5e8f\u5340\u9593\u9577\u5ea6\u70ba \\(2\\) \uff0c\u5373\u5404\u8f2a\u5916\u8ff4\u5708\u5206\u5225\u5305\u542b \\(n\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(3\\)\u3001\\(2\\) \u8f2a\u5167\u8ff4\u5708\uff0c\u6c42\u548c\u70ba \\(\\frac{(n - 1)(n + 2)}{2}\\) \u3002</li> <li>\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(1)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u6a19 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6578\u5927\u5c0f\u7684\u984d\u5916\u7a7a\u9593\u3002</li> <li>\u975e\u7a69\u5b9a\u6392\u5e8f\uff1a\u5982\u5716 11-3 \u6240\u793a\uff0c\u5143\u7d20 <code>nums[i]</code> \u6709\u53ef\u80fd\u88ab\u4ea4\u63db\u81f3\u8207\u5176\u76f8\u7b49\u7684\u5143\u7d20\u7684\u53f3\u908a\uff0c\u5c0e\u81f4\u5169\u8005\u7684\u76f8\u5c0d\u9806\u5e8f\u767c\u751f\u6539\u8b8a\u3002</li> </ul> <p> \u5716 11-3 \u00a0 \u9078\u64c7\u6392\u5e8f\u975e\u7a69\u5b9a\u793a\u4f8b </p>"},{"location":"chapter_sorting/sorting_algorithm/","title":"11.1 \u00a0 \u6392\u5e8f\u6f14\u7b97\u6cd5","text":"<p>\u6392\u5e8f\u6f14\u7b97\u6cd5\uff08sorting algorithm\uff09\u7528\u65bc\u5c0d\u4e00\u7d44\u8cc7\u6599\u6309\u7167\u7279\u5b9a\u9806\u5e8f\u9032\u884c\u6392\u5217\u3002\u6392\u5e8f\u6f14\u7b97\u6cd5\u6709\u8457\u5ee3\u6cdb\u7684\u61c9\u7528\uff0c\u56e0\u70ba\u6709\u5e8f\u8cc7\u6599\u901a\u5e38\u80fd\u5920\u88ab\u66f4\u9ad8\u6548\u5730\u67e5\u8a62\u3001\u5206\u6790\u548c\u8655\u7406\u3002</p> <p>\u5982\u5716 11-1 \u6240\u793a\uff0c\u6392\u5e8f\u6f14\u7b97\u6cd5\u4e2d\u7684\u8cc7\u6599\u578b\u5225\u53ef\u4ee5\u662f\u6574\u6578\u3001\u6d6e\u9ede\u6578\u3001\u5b57\u5143\u6216\u5b57\u4e32\u7b49\u3002\u6392\u5e8f\u7684\u5224\u65b7\u898f\u5247\u53ef\u6839\u64da\u9700\u6c42\u8a2d\u5b9a\uff0c\u5982\u6578\u5b57\u5927\u5c0f\u3001\u5b57\u5143 ASCII \u78bc\u9806\u5e8f\u6216\u81ea\u5b9a\u7fa9\u898f\u5247\u3002</p> <p></p> <p> \u5716 11-1 \u00a0 \u8cc7\u6599\u578b\u5225\u548c\u5224\u65b7\u898f\u5247\u793a\u4f8b </p>"},{"location":"chapter_sorting/sorting_algorithm/#1111","title":"11.1.1 \u00a0 \u8a55\u50f9\u7dad\u5ea6","text":"<p>\u57f7\u884c\u6548\u7387\uff1a\u6211\u5011\u671f\u671b\u6392\u5e8f\u6f14\u7b97\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u5118\u91cf\u4f4e\uff0c\u4e14\u7e3d\u9ad4\u64cd\u4f5c\u6578\u91cf\u8f03\u5c11\uff08\u6642\u9593\u8907\u96dc\u5ea6\u4e2d\u7684\u5e38\u6578\u9805\u8b8a\u5c0f\uff09\u3002\u5c0d\u65bc\u5927\u8cc7\u6599\u91cf\u7684\u60c5\u6cc1\uff0c\u57f7\u884c\u6548\u7387\u986f\u5f97\u5c24\u70ba\u91cd\u8981\u3002</p> <p>\u5c31\u5730\u6027\uff1a\u9867\u540d\u601d\u7fa9\uff0c\u539f\u5730\u6392\u5e8f\u900f\u904e\u5728\u539f\u9663\u5217\u4e0a\u76f4\u63a5\u64cd\u4f5c\u5be6\u73fe\u6392\u5e8f\uff0c\u7121\u9808\u85c9\u52a9\u984d\u5916\u7684\u8f14\u52a9\u9663\u5217\uff0c\u5f9e\u800c\u7bc0\u7701\u8a18\u61b6\u9ad4\u3002\u901a\u5e38\u60c5\u6cc1\u4e0b\uff0c\u539f\u5730\u6392\u5e8f\u7684\u8cc7\u6599\u642c\u904b\u64cd\u4f5c\u8f03\u5c11\uff0c\u57f7\u884c\u901f\u5ea6\u4e5f\u66f4\u5feb\u3002</p> <p>\u7a69\u5b9a\u6027\uff1a\u7a69\u5b9a\u6392\u5e8f\u5728\u5b8c\u6210\u6392\u5e8f\u5f8c\uff0c\u76f8\u7b49\u5143\u7d20\u5728\u9663\u5217\u4e2d\u7684\u76f8\u5c0d\u9806\u5e8f\u4e0d\u767c\u751f\u6539\u8b8a\u3002</p> <p>\u7a69\u5b9a\u6392\u5e8f\u662f\u591a\u7d1a\u6392\u5e8f\u5834\u666f\u7684\u5fc5\u8981\u689d\u4ef6\u3002\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u5132\u5b58\u5b78\u751f\u8cc7\u8a0a\u7684\u8868\u683c\uff0c\u7b2c 1 \u5217\u548c\u7b2c 2 \u5217\u5206\u5225\u662f\u59d3\u540d\u548c\u5e74\u9f61\u3002\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u975e\u7a69\u5b9a\u6392\u5e8f\u53ef\u80fd\u5c0e\u81f4\u8f38\u5165\u8cc7\u6599\u7684\u6709\u5e8f\u6027\u55aa\u5931\uff1a</p> <pre><code># \u8f38\u5165\u8cc7\u6599\u662f\u6309\u7167\u59d3\u540d\u6392\u5e8f\u597d\u7684\n# (name, age)\n ('A', 19)\n ('B', 18)\n ('C', 21)\n ('D', 19)\n ('E', 23)\n\n# \u5047\u8a2d\u4f7f\u7528\u975e\u7a69\u5b9a\u6392\u5e8f\u6f14\u7b97\u6cd5\u6309\u5e74\u9f61\u6392\u5e8f\u4e32\u5217\uff0c\n# \u7d50\u679c\u4e2d ('D', 19) \u548c ('A', 19) \u7684\u76f8\u5c0d\u4f4d\u7f6e\u6539\u8b8a\uff0c\n# \u8f38\u5165\u8cc7\u6599\u6309\u59d3\u540d\u6392\u5e8f\u7684\u6027\u8cea\u4e1f\u5931\n ('B', 18)\n ('D', 19)\n ('A', 19)\n ('C', 21)\n ('E', 23)\n</code></pre> <p>\u81ea\u9069\u61c9\u6027\uff1a\u81ea\u9069\u61c9\u6392\u5e8f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u6703\u53d7\u8f38\u5165\u8cc7\u6599\u7684\u5f71\u97ff\uff0c\u5373\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6\u3001\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6\u3001\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\u4e26\u4e0d\u5b8c\u5168\u76f8\u7b49\u3002</p> <p>\u81ea\u9069\u61c9\u6027\u9700\u8981\u6839\u64da\u5177\u9ad4\u60c5\u6cc1\u4f86\u8a55\u4f30\u3002\u5982\u679c\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6\u5dee\u65bc\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\uff0c\u8aaa\u660e\u6392\u5e8f\u6f14\u7b97\u6cd5\u5728\u67d0\u4e9b\u8cc7\u6599\u4e0b\u6548\u80fd\u53ef\u80fd\u52a3\u5316\uff0c\u56e0\u6b64\u88ab\u8996\u70ba\u8ca0\u9762\u5c6c\u6027\uff1b\u800c\u5982\u679c\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6\u512a\u65bc\u5e73\u5747\u6642\u9593\u8907\u96dc\u5ea6\uff0c\u5247\u88ab\u8996\u70ba\u6b63\u9762\u5c6c\u6027\u3002</p> <p>\u662f\u5426\u57fa\u65bc\u6bd4\u8f03\uff1a\u57fa\u65bc\u6bd4\u8f03\u7684\u6392\u5e8f\u4f9d\u8cf4\u6bd4\u8f03\u904b\u7b97\u5b50\uff08\\(&lt;\\)\u3001\\(=\\)\u3001\\(&gt;\\)\uff09\u4f86\u5224\u65b7\u5143\u7d20\u7684\u76f8\u5c0d\u9806\u5e8f\uff0c\u5f9e\u800c\u6392\u5e8f\u6574\u500b\u9663\u5217\uff0c\u7406\u8ad6\u6700\u512a\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n \\log n)\\) \u3002\u800c\u975e\u6bd4\u8f03\u6392\u5e8f\u4e0d\u4f7f\u7528\u6bd4\u8f03\u904b\u7b97\u5b50\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u53ef\u9054 \\(O(n)\\) \uff0c\u4f46\u5176\u901a\u7528\u6027\u76f8\u5c0d\u8f03\u5dee\u3002</p>"},{"location":"chapter_sorting/sorting_algorithm/#1112","title":"11.1.2 \u00a0 \u7406\u60f3\u6392\u5e8f\u6f14\u7b97\u6cd5","text":"<p>\u57f7\u884c\u5feb\u3001\u539f\u5730\u3001\u7a69\u5b9a\u3001\u6b63\u5411\u81ea\u9069\u61c9\u3001\u901a\u7528\u6027\u597d\u3002\u986f\u7136\uff0c\u8fc4\u4eca\u70ba\u6b62\u5c1a\u672a\u767c\u73fe\u517c\u5177\u4ee5\u4e0a\u6240\u6709\u7279\u6027\u7684\u6392\u5e8f\u6f14\u7b97\u6cd5\u3002\u56e0\u6b64\uff0c\u5728\u9078\u64c7\u6392\u5e8f\u6f14\u7b97\u6cd5\u6642\uff0c\u9700\u8981\u6839\u64da\u5177\u9ad4\u7684\u8cc7\u6599\u7279\u9ede\u548c\u554f\u984c\u9700\u6c42\u4f86\u6c7a\u5b9a\u3002</p> <p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u5171\u540c\u5b78\u7fd2\u5404\u7a2e\u6392\u5e8f\u6f14\u7b97\u6cd5\uff0c\u4e26\u57fa\u65bc\u4e0a\u8ff0\u8a55\u50f9\u7dad\u5ea6\u5c0d\u5404\u500b\u6392\u5e8f\u6f14\u7b97\u6cd5\u7684\u512a\u7f3a\u9ede\u9032\u884c\u5206\u6790\u3002</p>"},{"location":"chapter_sorting/summary/","title":"11.11 \u00a0 \u5c0f\u7d50","text":""},{"location":"chapter_sorting/summary/#1","title":"1. \u00a0 \u91cd\u9ede\u56de\u9867","text":"<ul> <li>\u6ce1\u6cab\u6392\u5e8f\u900f\u904e\u4ea4\u63db\u76f8\u9130\u5143\u7d20\u4f86\u5be6\u73fe\u6392\u5e8f\u3002\u900f\u904e\u65b0\u589e\u4e00\u500b\u6a19\u8a8c\u4f4d\u4f86\u5be6\u73fe\u63d0\u524d\u8fd4\u56de\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u6ce1\u6cab\u6392\u5e8f\u7684\u6700\u4f73\u6642\u9593\u8907\u96dc\u5ea6\u6700\u4f73\u5316\u5230 \\(O(n)\\) \u3002</li> <li>\u63d2\u5165\u6392\u5e8f\u6bcf\u8f2a\u5c07\u672a\u6392\u5e8f\u5340\u9593\u5167\u7684\u5143\u7d20\u63d2\u5165\u5230\u5df2\u6392\u5e8f\u5340\u9593\u7684\u6b63\u78ba\u4f4d\u7f6e\uff0c\u5f9e\u800c\u5b8c\u6210\u6392\u5e8f\u3002\u96d6\u7136\u63d2\u5165\u6392\u5e8f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\) \uff0c\u4f46\u7531\u65bc\u55ae\u5143\u64cd\u4f5c\u76f8\u5c0d\u8f03\u5c11\uff0c\u56e0\u6b64\u5728\u5c0f\u8cc7\u6599\u91cf\u7684\u6392\u5e8f\u4efb\u52d9\u4e2d\u975e\u5e38\u53d7\u6b61\u8fce\u3002</li> <li>\u5feb\u901f\u6392\u5e8f\u57fa\u65bc\u54e8\u5175\u5283\u5206\u64cd\u4f5c\u5be6\u73fe\u6392\u5e8f\u3002\u5728\u54e8\u5175\u5283\u5206\u4e2d\uff0c\u6709\u53ef\u80fd\u6bcf\u6b21\u90fd\u9078\u53d6\u5230\u6700\u5dee\u7684\u57fa\u6e96\u6578\uff0c\u5c0e\u81f4\u6642\u9593\u8907\u96dc\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u3002\u5f15\u5165\u4e2d\u4f4d\u6578\u57fa\u6e96\u6578\u6216\u96a8\u6a5f\u57fa\u6e96\u6578\u53ef\u4ee5\u964d\u4f4e\u9019\u7a2e\u52a3\u5316\u7684\u6a5f\u7387\u3002\u5c3e\u905e\u8ff4\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u6e1b\u5c11\u905e\u8ff4\u6df1\u5ea6\uff0c\u5c07\u7a7a\u9593\u8907\u96dc\u5ea6\u6700\u4f73\u5316\u5230 \\(O(\\log n)\\) \u3002</li> <li>\u5408\u4f75\u6392\u5e8f\u5305\u62ec\u5283\u5206\u548c\u5408\u4e26\u5169\u500b\u968e\u6bb5\uff0c\u5178\u578b\u5730\u9ad4\u73fe\u4e86\u5206\u6cbb\u7b56\u7565\u3002\u5728\u5408\u4f75\u6392\u5e8f\u4e2d\uff0c\u6392\u5e8f\u9663\u5217\u9700\u8981\u5efa\u7acb\u8f14\u52a9\u9663\u5217\uff0c\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \uff1b\u7136\u800c\u6392\u5e8f\u93c8\u7d50\u4e32\u5217\u7684\u7a7a\u9593\u8907\u96dc\u5ea6\u53ef\u4ee5\u6700\u4f73\u5316\u81f3 \\(O(1)\\) \u3002</li> <li>\u6876\u6392\u5e8f\u5305\u542b\u4e09\u500b\u6b65\u9a5f\uff1a\u8cc7\u6599\u5206\u6876\u3001\u6876\u5167\u6392\u5e8f\u548c\u5408\u4e26\u7d50\u679c\u3002\u5b83\u540c\u6a23\u9ad4\u73fe\u4e86\u5206\u6cbb\u7b56\u7565\uff0c\u9069\u7528\u65bc\u8cc7\u6599\u9ad4\u91cf\u5f88\u5927\u7684\u60c5\u6cc1\u3002\u6876\u6392\u5e8f\u7684\u95dc\u9375\u5728\u65bc\u5c0d\u8cc7\u6599\u9032\u884c\u5e73\u5747\u5206\u914d\u3002</li> <li>\u8a08\u6578\u6392\u5e8f\u662f\u6876\u6392\u5e8f\u7684\u4e00\u500b\u7279\u4f8b\uff0c\u5b83\u900f\u904e\u7d71\u8a08\u8cc7\u6599\u51fa\u73fe\u7684\u6b21\u6578\u4f86\u5be6\u73fe\u6392\u5e8f\u3002\u8a08\u6578\u6392\u5e8f\u9069\u7528\u65bc\u8cc7\u6599\u91cf\u5927\u4f46\u8cc7\u6599\u7bc4\u570d\u6709\u9650\u7684\u60c5\u6cc1\uff0c\u4e26\u4e14\u8981\u6c42\u8cc7\u6599\u80fd\u5920\u8f49\u63db\u70ba\u6b63\u6574\u6578\u3002</li> <li>\u57fa\u6578\u6392\u5e8f\u900f\u904e\u9010\u4f4d\u6392\u5e8f\u4f86\u5be6\u73fe\u8cc7\u6599\u6392\u5e8f\uff0c\u8981\u6c42\u8cc7\u6599\u80fd\u5920\u8868\u793a\u70ba\u56fa\u5b9a\u4f4d\u6578\u7684\u6578\u5b57\u3002</li> <li>\u7e3d\u7684\u4f86\u8aaa\uff0c\u6211\u5011\u5e0c\u671b\u627e\u5230\u4e00\u7a2e\u6392\u5e8f\u6f14\u7b97\u6cd5\uff0c\u5177\u6709\u9ad8\u6548\u7387\u3001\u7a69\u5b9a\u3001\u539f\u5730\u4ee5\u53ca\u6b63\u5411\u81ea\u9069\u61c9\u6027\u7b49\u512a\u9ede\u3002\u7136\u800c\uff0c\u6b63\u5982\u5176\u4ed6\u8cc7\u6599\u7d50\u69cb\u548c\u6f14\u7b97\u6cd5\u4e00\u6a23\uff0c\u6c92\u6709\u4e00\u7a2e\u6392\u5e8f\u6f14\u7b97\u6cd5\u80fd\u5920\u540c\u6642\u6eff\u8db3\u6240\u6709\u9019\u4e9b\u689d\u4ef6\u3002\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u6211\u5011\u9700\u8981\u6839\u64da\u8cc7\u6599\u7684\u7279\u6027\u4f86\u9078\u64c7\u5408\u9069\u7684\u6392\u5e8f\u6f14\u7b97\u6cd5\u3002</li> <li>\u5716 11-19 \u5c0d\u6bd4\u4e86\u4e3b\u6d41\u6392\u5e8f\u6f14\u7b97\u6cd5\u7684\u6548\u7387\u3001\u7a69\u5b9a\u6027\u3001\u5c31\u5730\u6027\u548c\u81ea\u9069\u61c9\u6027\u7b49\u3002</li> </ul> <p> \u5716 11-19 \u00a0 \u6392\u5e8f\u6f14\u7b97\u6cd5\u5c0d\u6bd4 </p>"},{"location":"chapter_sorting/summary/#2-q-a","title":"2. \u00a0 Q &amp; A","text":"<p>Q\uff1a\u6392\u5e8f\u6f14\u7b97\u6cd5\u7a69\u5b9a\u6027\u5728\u4ec0\u9ebc\u60c5\u6cc1\u4e0b\u662f\u5fc5\u9700\u7684\uff1f</p> <p>\u5728\u73fe\u5be6\u4e2d\uff0c\u6211\u5011\u6709\u53ef\u80fd\u57fa\u65bc\u7269\u4ef6\u7684\u67d0\u500b\u5c6c\u6027\u9032\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5b78\u751f\u6709\u59d3\u540d\u548c\u8eab\u9ad8\u5169\u500b\u5c6c\u6027\uff0c\u6211\u5011\u5e0c\u671b\u5be6\u73fe\u4e00\u500b\u591a\u7d1a\u6392\u5e8f\uff1a\u5148\u6309\u7167\u59d3\u540d\u9032\u884c\u6392\u5e8f\uff0c\u5f97\u5230 <code>(A, 180) (B, 185) (C, 170) (D, 170)</code> \uff1b\u518d\u5c0d\u8eab\u9ad8\u9032\u884c\u6392\u5e8f\u3002\u7531\u65bc\u6392\u5e8f\u6f14\u7b97\u6cd5\u4e0d\u7a69\u5b9a\uff0c\u56e0\u6b64\u53ef\u80fd\u5f97\u5230 <code>(D, 170) (C, 170) (A, 180) (B, 185)</code> \u3002</p> <p>\u53ef\u4ee5\u767c\u73fe\uff0c\u5b78\u751f D \u548c C \u7684\u4f4d\u7f6e\u767c\u751f\u4e86\u4ea4\u63db\uff0c\u59d3\u540d\u7684\u6709\u5e8f\u6027\u88ab\u7834\u58de\u4e86\uff0c\u800c\u9019\u662f\u6211\u5011\u4e0d\u5e0c\u671b\u770b\u5230\u7684\u3002</p> <p>Q\uff1a\u54e8\u5175\u5283\u5206\u4e2d\u201c\u5f9e\u53f3\u5f80\u5de6\u67e5\u8a62\u201d\u8207\u201c\u5f9e\u5de6\u5f80\u53f3\u67e5\u8a62\u201d\u7684\u9806\u5e8f\u53ef\u4ee5\u4ea4\u63db\u55ce\uff1f</p> <p>\u4e0d\u884c\uff0c\u7576\u6211\u5011\u4ee5\u6700\u5de6\u7aef\u5143\u7d20\u70ba\u57fa\u6e96\u6578\u6642\uff0c\u5fc5\u9808\u5148\u201c\u5f9e\u53f3\u5f80\u5de6\u67e5\u8a62\u201d\u518d\u201c\u5f9e\u5de6\u5f80\u53f3\u67e5\u8a62\u201d\u3002\u9019\u500b\u7d50\u8ad6\u6709\u4e9b\u53cd\u76f4\u89ba\uff0c\u6211\u5011\u4f86\u5256\u6790\u4e00\u4e0b\u539f\u56e0\u3002</p> <p>\u54e8\u5175\u5283\u5206 <code>partition()</code> \u7684\u6700\u5f8c\u4e00\u6b65\u662f\u4ea4\u63db <code>nums[left]</code> \u548c <code>nums[i]</code> \u3002\u5b8c\u6210\u4ea4\u63db\u5f8c\uff0c\u57fa\u6e96\u6578\u5de6\u908a\u7684\u5143\u7d20\u90fd <code>&lt;=</code> \u57fa\u6e96\u6578\uff0c\u9019\u5c31\u8981\u6c42\u6700\u5f8c\u4e00\u6b65\u4ea4\u63db\u524d <code>nums[left] &gt;= nums[i]</code> \u5fc5\u9808\u6210\u7acb\u3002\u5047\u8a2d\u6211\u5011\u5148\u201c\u5f9e\u5de6\u5f80\u53f3\u67e5\u8a62\u201d\uff0c\u90a3\u9ebc\u5982\u679c\u627e\u4e0d\u5230\u6bd4\u57fa\u6e96\u6578\u66f4\u5927\u7684\u5143\u7d20\uff0c\u5247\u6703\u5728 <code>i == j</code> \u6642\u8df3\u51fa\u8ff4\u5708\uff0c\u6b64\u6642\u53ef\u80fd <code>nums[j] == nums[i] &gt; nums[left]</code>\u3002\u4e5f\u5c31\u662f\u8aaa\uff0c\u6b64\u6642\u6700\u5f8c\u4e00\u6b65\u4ea4\u63db\u64cd\u4f5c\u6703\u628a\u4e00\u500b\u6bd4\u57fa\u6e96\u6578\u66f4\u5927\u7684\u5143\u7d20\u4ea4\u63db\u81f3\u9663\u5217\u6700\u5de6\u7aef\uff0c\u5c0e\u81f4\u54e8\u5175\u5283\u5206\u5931\u6557\u3002</p> <p>\u8209\u500b\u4f8b\u5b50\uff0c\u7d66\u5b9a\u9663\u5217 <code>[0, 0, 0, 0, 1]</code> \uff0c\u5982\u679c\u5148\u201c\u5f9e\u5de6\u5411\u53f3\u67e5\u8a62\u201d\uff0c\u54e8\u5175\u5283\u5206\u5f8c\u9663\u5217\u70ba <code>[1, 0, 0, 0, 0]</code> \uff0c\u9019\u500b\u7d50\u679c\u662f\u4e0d\u6b63\u78ba\u7684\u3002</p> <p>\u518d\u6df1\u5165\u601d\u8003\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u5011\u9078\u64c7 <code>nums[right]</code> \u70ba\u57fa\u6e96\u6578\uff0c\u90a3\u9ebc\u6b63\u597d\u53cd\u904e\u4f86\uff0c\u5fc5\u9808\u5148\u201c\u5f9e\u5de6\u5f80\u53f3\u67e5\u8a62\u201d\u3002</p> <p>Q\uff1a\u95dc\u65bc\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\uff0c\u70ba\u4ec0\u9ebc\u9078\u77ed\u7684\u9663\u5217\u80fd\u4fdd\u8b49\u905e\u8ff4\u6df1\u5ea6\u4e0d\u8d85\u904e \\(\\log n\\) \uff1f</p> <p>\u905e\u8ff4\u6df1\u5ea6\u5c31\u662f\u7576\u524d\u672a\u8fd4\u56de\u7684\u905e\u8ff4\u65b9\u6cd5\u7684\u6578\u91cf\u3002\u6bcf\u8f2a\u54e8\u5175\u5283\u5206\u6211\u5011\u5c07\u539f\u9663\u5217\u5283\u5206\u70ba\u5169\u500b\u5b50\u9663\u5217\u3002\u5728\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\u5f8c\uff0c\u5411\u4e0b\u905e\u8ff4\u7684\u5b50\u9663\u5217\u9577\u5ea6\u6700\u5927\u70ba\u539f\u9663\u5217\u9577\u5ea6\u7684\u4e00\u534a\u3002\u5047\u8a2d\u6700\u5dee\u60c5\u6cc1\uff0c\u4e00\u76f4\u70ba\u4e00\u534a\u9577\u5ea6\uff0c\u90a3\u9ebc\u6700\u7d42\u7684\u905e\u8ff4\u6df1\u5ea6\u5c31\u662f \\(\\log n\\) \u3002</p> <p>\u56de\u9867\u539f\u59cb\u7684\u5feb\u901f\u6392\u5e8f\uff0c\u6211\u5011\u6709\u53ef\u80fd\u6703\u9023\u7e8c\u5730\u905e\u8ff4\u9577\u5ea6\u8f03\u5927\u7684\u9663\u5217\uff0c\u6700\u5dee\u60c5\u6cc1\u4e0b\u70ba \\(n\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u905e\u8ff4\u6df1\u5ea6\u70ba \\(n\\) \u3002\u5c3e\u905e\u8ff4\u6700\u4f73\u5316\u53ef\u4ee5\u907f\u514d\u9019\u7a2e\u60c5\u6cc1\u51fa\u73fe\u3002</p> <p>Q\uff1a\u7576\u9663\u5217\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u76f8\u7b49\u6642\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u6642\u9593\u8907\u96dc\u5ea6\u662f \\(O(n^2)\\) \u55ce\uff1f\u8a72\u5982\u4f55\u8655\u7406\u9019\u7a2e\u9000\u5316\u60c5\u6cc1\uff1f</p> <p>\u662f\u7684\u3002\u5c0d\u65bc\u9019\u7a2e\u60c5\u6cc1\uff0c\u53ef\u4ee5\u8003\u616e\u900f\u904e\u54e8\u5175\u5283\u5206\u5c07\u9663\u5217\u5283\u5206\u70ba\u4e09\u500b\u90e8\u5206\uff1a\u5c0f\u65bc\u3001\u7b49\u65bc\u3001\u5927\u65bc\u57fa\u6e96\u6578\u3002\u50c5\u5411\u4e0b\u905e\u8ff4\u5c0f\u65bc\u548c\u5927\u65bc\u7684\u5169\u90e8\u5206\u3002\u5728\u8a72\u65b9\u6cd5\u4e0b\uff0c\u8f38\u5165\u5143\u7d20\u5168\u90e8\u76f8\u7b49\u7684\u9663\u5217\uff0c\u50c5\u4e00\u8f2a\u54e8\u5175\u5283\u5206\u5373\u53ef\u5b8c\u6210\u6392\u5e8f\u3002</p> <p>Q\uff1a\u6876\u6392\u5e8f\u7684\u6700\u5dee\u6642\u9593\u8907\u96dc\u5ea6\u70ba\u4ec0\u9ebc\u662f \\(O(n^2)\\) \uff1f</p> <p>\u6700\u5dee\u60c5\u6cc1\u4e0b\uff0c\u6240\u6709\u5143\u7d20\u88ab\u5206\u81f3\u540c\u4e00\u500b\u6876\u4e2d\u3002\u5982\u679c\u6211\u5011\u63a1\u7528\u4e00\u500b \\(O(n^2)\\) \u6f14\u7b97\u6cd5\u4f86\u6392\u5e8f\u9019\u4e9b\u5143\u7d20\uff0c\u5247\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n^2)\\) \u3002</p>"},{"location":"chapter_stack_and_queue/","title":"\u7b2c 5 \u7ae0 \u00a0 \u5806\u758a\u8207\u4f47\u5217","text":"<p>Abstract</p> <p>\u5806\u758a\u5982\u540c\u758a\u8c93\u8c93\uff0c\u800c\u4f47\u5217\u5c31\u50cf\u8c93\u8c93\u6392\u968a\u3002</p> <p>\u5169\u8005\u5206\u5225\u4ee3\u8868\u5148\u5165\u5f8c\u51fa\u548c\u5148\u5165\u5148\u51fa\u7684\u908f\u8f2f\u95dc\u4fc2\u3002</p>"},{"location":"chapter_stack_and_queue/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>5.1 \u00a0 \u5806\u758a</li> <li>5.2 \u00a0 \u4f47\u5217</li> <li>5.3 \u00a0 \u96d9\u5411\u4f47\u5217</li> <li>5.4 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_stack_and_queue/deque/","title":"5.3 \u00a0 \u96d9\u5411\u4f47\u5217","text":"<p>\u5728\u4f47\u5217\u4e2d\uff0c\u6211\u5011\u50c5\u80fd\u522a\u9664\u982d\u90e8\u5143\u7d20\u6216\u5728\u5c3e\u90e8\u65b0\u589e\u5143\u7d20\u3002\u5982\u5716 5-7 \u6240\u793a\uff0c\u96d9\u5411\u4f47\u5217\uff08double-ended queue\uff09\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u9748\u6d3b\u6027\uff0c\u5141\u8a31\u5728\u982d\u90e8\u548c\u5c3e\u90e8\u57f7\u884c\u5143\u7d20\u7684\u65b0\u589e\u6216\u522a\u9664\u64cd\u4f5c\u3002</p> <p></p> <p> \u5716 5-7 \u00a0 \u96d9\u5411\u4f47\u5217\u7684\u64cd\u4f5c </p>"},{"location":"chapter_stack_and_queue/deque/#531","title":"5.3.1 \u00a0 \u96d9\u5411\u4f47\u5217\u5e38\u7528\u64cd\u4f5c","text":"<p>\u96d9\u5411\u4f47\u5217\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u8868 5-3 \u6240\u793a\uff0c\u5177\u9ad4\u7684\u65b9\u6cd5\u540d\u7a31\u9700\u8981\u6839\u64da\u6240\u4f7f\u7528\u7684\u7a0b\u5f0f\u8a9e\u8a00\u4f86\u78ba\u5b9a\u3002</p> <p> \u8868 5-3 \u00a0 \u96d9\u5411\u4f47\u5217\u64cd\u4f5c\u6548\u7387 </p> \u65b9\u6cd5\u540d \u63cf\u8ff0 \u6642\u9593\u8907\u96dc\u5ea6 <code>push_first()</code> \u5c07\u5143\u7d20\u65b0\u589e\u81f3\u4f47\u5217\u9996 \\(O(1)\\) <code>push_last()</code> \u5c07\u5143\u7d20\u65b0\u589e\u81f3\u4f47\u5217\u5c3e \\(O(1)\\) <code>pop_first()</code> \u522a\u9664\u4f47\u5217\u9996\u5143\u7d20 \\(O(1)\\) <code>pop_last()</code> \u522a\u9664\u4f47\u5217\u5c3e\u5143\u7d20 \\(O(1)\\) <code>peek_first()</code> \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 \\(O(1)\\) <code>peek_last()</code> \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 \\(O(1)\\) <p>\u540c\u6a23\u5730\uff0c\u6211\u5011\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7a0b\u5f0f\u8a9e\u8a00\u4e2d\u5df2\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217\u985e\u5225\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig deque.py<pre><code>from collections import deque\n\n# \u521d\u59cb\u5316\u96d9\u5411\u4f47\u5217\ndeq: deque[int] = deque()\n\n# \u5143\u7d20\u5165\u5217\ndeq.append(2) # \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\ndeq.append(5)\ndeq.append(4)\ndeq.appendleft(3) # \u65b0\u589e\u81f3\u4f47\u5217\u9996\ndeq.appendleft(1)\n\n# \u8a2a\u554f\u5143\u7d20\nfront: int = deq[0] # \u4f47\u5217\u9996\u5143\u7d20\nrear: int = deq[-1] # \u4f47\u5217\u5c3e\u5143\u7d20\n\n# \u5143\u7d20\u51fa\u5217\npop_front: int = deq.popleft() # \u4f47\u5217\u9996\u5143\u7d20\u51fa\u5217\npop_rear: int = deq.pop() # \u4f47\u5217\u5c3e\u5143\u7d20\u51fa\u5217\n\n# \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\nsize: int = len(deq)\n\n# \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a\nis_empty: bool = len(deq) == 0\n</code></pre> deque.cpp<pre><code>/* \u521d\u59cb\u5316\u96d9\u5411\u4f47\u5217 */\ndeque&lt;int&gt; deque;\n\n/* \u5143\u7d20\u5165\u5217 */\ndeque.push_back(2); // \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // \u65b0\u589e\u81f3\u4f47\u5217\u9996\ndeque.push_front(1);\n\n/* \u8a2a\u554f\u5143\u7d20 */\nint front = deque.front(); // \u4f47\u5217\u9996\u5143\u7d20\nint back = deque.back(); // \u4f47\u5217\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u5217 */\ndeque.pop_front(); // \u4f47\u5217\u9996\u5143\u7d20\u51fa\u5217\ndeque.pop_back(); // \u4f47\u5217\u5c3e\u5143\u7d20\u51fa\u5217\n\n/* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\nint size = deque.size();\n\n/* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nbool empty = deque.empty();\n</code></pre> deque.java<pre><code>/* \u521d\u59cb\u5316\u96d9\u5411\u4f47\u5217 */\nDeque&lt;Integer&gt; deque = new LinkedList&lt;&gt;();\n\n/* \u5143\u7d20\u5165\u5217 */\ndeque.offerLast(2); // \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\ndeque.offerLast(5);\ndeque.offerLast(4);\ndeque.offerFirst(3); // \u65b0\u589e\u81f3\u4f47\u5217\u9996\ndeque.offerFirst(1);\n\n/* \u8a2a\u554f\u5143\u7d20 */\nint peekFirst = deque.peekFirst(); // \u4f47\u5217\u9996\u5143\u7d20\nint peekLast = deque.peekLast(); // \u4f47\u5217\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u5217 */\nint popFirst = deque.pollFirst(); // \u4f47\u5217\u9996\u5143\u7d20\u51fa\u5217\nint popLast = deque.pollLast(); // \u4f47\u5217\u5c3e\u5143\u7d20\u51fa\u5217\n\n/* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\nint size = deque.size();\n\n/* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nboolean isEmpty = deque.isEmpty();\n</code></pre> deque.cs<pre><code>/* \u521d\u59cb\u5316\u96d9\u5411\u4f47\u5217 */\n// \u5728 C# \u4e2d\uff0c\u5c07\u93c8\u7d50\u4e32\u5217 LinkedList \u770b\u4f5c\u96d9\u5411\u4f47\u5217\u4f86\u4f7f\u7528\nLinkedList&lt;int&gt; deque = new();\n\n/* \u5143\u7d20\u5165\u5217 */\ndeque.AddLast(2); // \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\ndeque.AddLast(5);\ndeque.AddLast(4);\ndeque.AddFirst(3); // \u65b0\u589e\u81f3\u4f47\u5217\u9996\ndeque.AddFirst(1);\n\n/* \u8a2a\u554f\u5143\u7d20 */\nint peekFirst = deque.First.Value; // \u4f47\u5217\u9996\u5143\u7d20\nint peekLast = deque.Last.Value; // \u4f47\u5217\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u5217 */\ndeque.RemoveFirst(); // \u4f47\u5217\u9996\u5143\u7d20\u51fa\u5217\ndeque.RemoveLast(); // \u4f47\u5217\u5c3e\u5143\u7d20\u51fa\u5217\n\n/* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\nint size = deque.Count;\n\n/* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nbool isEmpty = deque.Count == 0;\n</code></pre> deque_test.go<pre><code>/* \u521d\u59cb\u5316\u96d9\u5411\u4f47\u5217 */\n// \u5728 Go \u4e2d\uff0c\u5c07 list \u4f5c\u70ba\u96d9\u5411\u4f47\u5217\u4f7f\u7528\ndeque := list.New()\n\n/* \u5143\u7d20\u5165\u5217 */\ndeque.PushBack(2) // \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\ndeque.PushBack(5)\ndeque.PushBack(4)\ndeque.PushFront(3) // \u65b0\u589e\u81f3\u4f47\u5217\u9996\ndeque.PushFront(1)\n\n/* \u8a2a\u554f\u5143\u7d20 */\nfront := deque.Front() // \u4f47\u5217\u9996\u5143\u7d20\nrear := deque.Back() // \u4f47\u5217\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u5217 */\ndeque.Remove(front) // \u4f47\u5217\u9996\u5143\u7d20\u51fa\u5217\ndeque.Remove(rear) // \u4f47\u5217\u5c3e\u5143\u7d20\u51fa\u5217\n\n/* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\nsize := deque.Len()\n\n/* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nisEmpty := deque.Len() == 0\n</code></pre> deque.swift<pre><code>/* \u521d\u59cb\u5316\u96d9\u5411\u4f47\u5217 */\n// Swift \u6c92\u6709\u5167\u5efa\u7684\u96d9\u5411\u4f47\u5217\u985e\u5225\uff0c\u53ef\u4ee5\u628a Array \u7576\u4f5c\u96d9\u5411\u4f47\u5217\u4f86\u4f7f\u7528\nvar deque: [Int] = []\n\n/* \u5143\u7d20\u5165\u5217 */\ndeque.append(2) // \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\ndeque.append(5)\ndeque.append(4)\ndeque.insert(3, at: 0) // \u65b0\u589e\u81f3\u4f47\u5217\u9996\ndeque.insert(1, at: 0)\n\n/* \u8a2a\u554f\u5143\u7d20 */\nlet peekFirst = deque.first! // \u4f47\u5217\u9996\u5143\u7d20\nlet peekLast = deque.last! // \u4f47\u5217\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u5217 */\n// \u4f7f\u7528 Array \u6a21\u64ec\u6642 popFirst \u7684\u8907\u96dc\u5ea6\u70ba O(n)\nlet popFirst = deque.removeFirst() // \u4f47\u5217\u9996\u5143\u7d20\u51fa\u5217\nlet popLast = deque.removeLast() // \u4f47\u5217\u5c3e\u5143\u7d20\u51fa\u5217\n\n/* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\nlet size = deque.count\n\n/* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nlet isEmpty = deque.isEmpty\n</code></pre> deque.js<pre><code>/* \u521d\u59cb\u5316\u96d9\u5411\u4f47\u5217 */\n// JavaScript \u6c92\u6709\u5167\u5efa\u7684\u96d9\u7aef\u4f47\u5217\uff0c\u53ea\u80fd\u628a Array \u7576\u4f5c\u96d9\u7aef\u4f47\u5217\u4f86\u4f7f\u7528\nconst deque = [];\n\n/* \u5143\u7d20\u5165\u5217 */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u8acb\u6ce8\u610f\uff0c\u7531\u65bc\u662f\u9663\u5217\uff0cunshift() \u65b9\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* \u8a2a\u554f\u5143\u7d20 */\nconst peekFirst = deque[0];\nconst peekLast = deque[deque.length - 1];\n\n/* \u5143\u7d20\u51fa\u5217 */\n// \u8acb\u6ce8\u610f\uff0c\u7531\u65bc\u662f\u9663\u5217\uff0cshift() \u65b9\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\nconst popFront = deque.shift();\nconst popBack = deque.pop();\n\n/* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\nconst size = deque.length;\n\n/* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nconst isEmpty = size === 0;\n</code></pre> deque.ts<pre><code>/* \u521d\u59cb\u5316\u96d9\u5411\u4f47\u5217 */\n// TypeScript \u6c92\u6709\u5167\u5efa\u7684\u96d9\u7aef\u4f47\u5217\uff0c\u53ea\u80fd\u628a Array \u7576\u4f5c\u96d9\u7aef\u4f47\u5217\u4f86\u4f7f\u7528\nconst deque: number[] = [];\n\n/* \u5143\u7d20\u5165\u5217 */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u8acb\u6ce8\u610f\uff0c\u7531\u65bc\u662f\u9663\u5217\uff0cunshift() \u65b9\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* \u8a2a\u554f\u5143\u7d20 */\nconst peekFirst: number = deque[0];\nconst peekLast: number = deque[deque.length - 1];\n\n/* \u5143\u7d20\u51fa\u5217 */\n// \u8acb\u6ce8\u610f\uff0c\u7531\u65bc\u662f\u9663\u5217\uff0cshift() \u65b9\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\nconst popFront: number = deque.shift() as number;\nconst popBack: number = deque.pop() as number;\n\n/* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\nconst size: number = deque.length;\n\n/* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nconst isEmpty: boolean = size === 0;\n</code></pre> deque.dart<pre><code>/* \u521d\u59cb\u5316\u96d9\u5411\u4f47\u5217 */\n// \u5728 Dart \u4e2d\uff0cQueue \u88ab\u5b9a\u7fa9\u70ba\u96d9\u5411\u4f47\u5217\nQueue&lt;int&gt; deque = Queue&lt;int&gt;();\n\n/* \u5143\u7d20\u5165\u5217 */\ndeque.addLast(2); // \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\ndeque.addLast(5);\ndeque.addLast(4);\ndeque.addFirst(3); // \u65b0\u589e\u81f3\u4f47\u5217\u9996\ndeque.addFirst(1);\n\n/* \u8a2a\u554f\u5143\u7d20 */\nint peekFirst = deque.first; // \u4f47\u5217\u9996\u5143\u7d20\nint peekLast = deque.last; // \u4f47\u5217\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u5217 */\nint popFirst = deque.removeFirst(); // \u4f47\u5217\u9996\u5143\u7d20\u51fa\u5217\nint popLast = deque.removeLast(); // \u4f47\u5217\u5c3e\u5143\u7d20\u51fa\u5217\n\n/* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\nint size = deque.length;\n\n/* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nbool isEmpty = deque.isEmpty;\n</code></pre> deque.rs<pre><code>/* \u521d\u59cb\u5316\u96d9\u5411\u4f47\u5217 */\nlet mut deque: VecDeque&lt;u32&gt; = VecDeque::new();\n\n/* \u5143\u7d20\u5165\u5217 */\ndeque.push_back(2); // \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // \u65b0\u589e\u81f3\u4f47\u5217\u9996\ndeque.push_front(1);\n\n/* \u8a2a\u554f\u5143\u7d20 */\nif let Some(front) = deque.front() { // \u4f47\u5217\u9996\u5143\u7d20\n}\nif let Some(rear) = deque.back() { // \u4f47\u5217\u5c3e\u5143\u7d20\n}\n\n/* \u5143\u7d20\u51fa\u5217 */\nif let Some(pop_front) = deque.pop_front() { // \u4f47\u5217\u9996\u5143\u7d20\u51fa\u5217\n}\nif let Some(pop_rear) = deque.pop_back() { // \u4f47\u5217\u5c3e\u5143\u7d20\u51fa\u5217\n}\n\n/* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\nlet size = deque.len();\n\n/* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nlet is_empty = deque.is_empty();\n</code></pre> deque.c<pre><code>// C \u672a\u63d0\u4f9b\u5167\u5efa\u96d9\u5411\u4f47\u5217\n</code></pre> deque.kt<pre><code>/* \u521d\u59cb\u5316\u96d9\u5411\u4f47\u5217 */\nval deque = LinkedList&lt;Int&gt;()\n\n/* \u5143\u7d20\u5165\u5217 */\ndeque.offerLast(2) // \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\ndeque.offerLast(5)\ndeque.offerLast(4)\ndeque.offerFirst(3) // \u65b0\u589e\u81f3\u4f47\u5217\u9996\ndeque.offerFirst(1)\n\n/* \u8a2a\u554f\u5143\u7d20 */\nval peekFirst = deque.peekFirst() // \u4f47\u5217\u9996\u5143\u7d20\nval peekLast = deque.peekLast() // \u4f47\u5217\u5c3e\u5143\u7d20\n\n/* \u5143\u7d20\u51fa\u5217 */\nval popFirst = deque.pollFirst() // \u4f47\u5217\u9996\u5143\u7d20\u51fa\u5217\nval popLast = deque.pollLast() // \u4f47\u5217\u5c3e\u5143\u7d20\u51fa\u5217\n\n/* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\nval size = deque.size\n\n/* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nval isEmpty = deque.isEmpty()\n</code></pre> deque.rb<pre><code># \u521d\u59cb\u5316\u96d9\u5411\u4f47\u5217\n# Ruby \u6c92\u6709\u5167\u76f4\u7684\u96d9\u7aef\u4f47\u5217\uff0c\u53ea\u80fd\u628a Array \u7576\u4f5c\u96d9\u7aef\u4f47\u5217\u4f86\u4f7f\u7528\ndeque = []\n\n# \u5143\u7d20\u5982\u968a\ndeque &lt;&lt; 2\ndeque &lt;&lt; 5\ndeque &lt;&lt; 4\n# \u8acb\u6ce8\u610f\uff0c\u7531\u65bc\u662f\u9663\u5217\uff0cArray#unshift \u65b9\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\ndeque.unshift(3)\ndeque.unshift(1)\n\n# \u8a2a\u554f\u5143\u7d20\npeek_first = deque.first\npeek_last = deque.last\n\n# \u5143\u7d20\u51fa\u5217\n# \u8acb\u6ce8\u610f\uff0c\u7531\u65bc\u662f\u9663\u5217\uff0c Array#shift \u65b9\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\npop_front = deque.shift\npop_back = deque.pop\n\n# \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\nsize = deque.length\n\n# \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a\nis_empty = size.zero?\n</code></pre> deque.zig<pre><code>\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_stack_and_queue/deque/#532","title":"5.3.2 \u00a0 \u96d9\u5411\u4f47\u5217\u5be6\u73fe *","text":"<p>\u96d9\u5411\u4f47\u5217\u7684\u5be6\u73fe\u8207\u4f47\u5217\u985e\u4f3c\uff0c\u53ef\u4ee5\u9078\u64c7\u93c8\u7d50\u4e32\u5217\u6216\u9663\u5217\u4f5c\u70ba\u5e95\u5c64\u8cc7\u6599\u7d50\u69cb\u3002</p>"},{"location":"chapter_stack_and_queue/deque/#1","title":"1. \u00a0 \u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7684\u5be6\u73fe","text":"<p>\u56de\u9867\u4e0a\u4e00\u7bc0\u5167\u5bb9\uff0c\u6211\u5011\u4f7f\u7528\u666e\u901a\u55ae\u5411\u93c8\u7d50\u4e32\u5217\u4f86\u5be6\u73fe\u4f47\u5217\uff0c\u56e0\u70ba\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u522a\u9664\u982d\u7bc0\u9ede\uff08\u5c0d\u61c9\u51fa\u5217\u64cd\u4f5c\uff09\u548c\u5728\u5c3e\u7bc0\u9ede\u5f8c\u65b0\u589e\u65b0\u7bc0\u9ede\uff08\u5c0d\u61c9\u5165\u5217\u64cd\u4f5c\uff09\u3002</p> <p>\u5c0d\u65bc\u96d9\u5411\u4f47\u5217\u800c\u8a00\uff0c\u982d\u90e8\u548c\u5c3e\u90e8\u90fd\u53ef\u4ee5\u57f7\u884c\u5165\u5217\u548c\u51fa\u5217\u64cd\u4f5c\u3002\u63db\u53e5\u8a71\u8aaa\uff0c\u96d9\u5411\u4f47\u5217\u9700\u8981\u5be6\u73fe\u53e6\u4e00\u500b\u5c0d\u7a31\u65b9\u5411\u7684\u64cd\u4f5c\u3002\u70ba\u6b64\uff0c\u6211\u5011\u63a1\u7528\u201c\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u201d\u4f5c\u70ba\u96d9\u5411\u4f47\u5217\u7684\u5e95\u5c64\u8cc7\u6599\u7d50\u69cb\u3002</p> <p>\u5982\u5716 5-8 \u6240\u793a\uff0c\u6211\u5011\u5c07\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7684\u982d\u7bc0\u9ede\u548c\u5c3e\u7bc0\u9ede\u8996\u70ba\u96d9\u5411\u4f47\u5217\u7684\u4f47\u5217\u9996\u548c\u4f47\u5217\u5c3e\uff0c\u540c\u6642\u5be6\u73fe\u5728\u5169\u7aef\u65b0\u589e\u548c\u522a\u9664\u7bc0\u9ede\u7684\u529f\u80fd\u3002</p> LinkedListDequepush_last()push_first()pop_last()pop_first() <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 5-8 \u00a0 \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u96d9\u5411\u4f47\u5217\u7684\u5165\u5217\u51fa\u5217\u64cd\u4f5c </p> <p>\u5be6\u73fe\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linkedlist_deque.py<pre><code>class ListNode:\n \"\"\"\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\"\"\"\n\n def __init__(self, val: int):\n \"\"\"\u5efa\u69cb\u5b50\"\"\"\n self.val: int = val\n self.next: ListNode | None = None # \u5f8c\u7e7c\u7bc0\u9ede\u5f15\u7528\n self.prev: ListNode | None = None # \u524d\u9a45\u7bc0\u9ede\u5f15\u7528\n\nclass LinkedListDeque:\n \"\"\"\u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217\"\"\"\n\n def __init__(self):\n \"\"\"\u5efa\u69cb\u5b50\"\"\"\n self._front: ListNode | None = None # \u982d\u7bc0\u9ede front\n self._rear: ListNode | None = None # \u5c3e\u7bc0\u9ede rear\n self._size: int = 0 # \u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\n\n def size(self) -&gt; int:\n \"\"\"\u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -&gt; bool:\n \"\"\"\u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a\"\"\"\n return self._size == 0\n\n def push(self, num: int, is_front: bool):\n \"\"\"\u5165\u5217\u64cd\u4f5c\"\"\"\n node = ListNode(num)\n # \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if self.is_empty():\n self._front = self._rear = node\n # \u4f47\u5217\u9996\u5165\u5217\u64cd\u4f5c\n elif is_front:\n # \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u982d\u90e8\n self._front.prev = node\n node.next = self._front\n self._front = node # \u66f4\u65b0\u982d\u7bc0\u9ede\n # \u4f47\u5217\u5c3e\u5165\u5217\u64cd\u4f5c\n else:\n # \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u5c3e\u90e8\n self._rear.next = node\n node.prev = self._rear\n self._rear = node # \u66f4\u65b0\u5c3e\u7bc0\u9ede\n self._size += 1 # \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n\n def push_first(self, num: int):\n \"\"\"\u4f47\u5217\u9996\u5165\u5217\"\"\"\n self.push(num, True)\n\n def push_last(self, num: int):\n \"\"\"\u4f47\u5217\u5c3e\u5165\u5217\"\"\"\n self.push(num, False)\n\n def pop(self, is_front: bool) -&gt; int:\n \"\"\"\u51fa\u5217\u64cd\u4f5c\"\"\"\n if self.is_empty():\n raise IndexError(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\")\n # \u4f47\u5217\u9996\u51fa\u5217\u64cd\u4f5c\n if is_front:\n val: int = self._front.val # \u66ab\u5b58\u982d\u7bc0\u9ede\u503c\n # \u522a\u9664\u982d\u7bc0\u9ede\n fnext: ListNode | None = self._front.next\n if fnext != None:\n fnext.prev = None\n self._front.next = None\n self._front = fnext # \u66f4\u65b0\u982d\u7bc0\u9ede\n # \u4f47\u5217\u5c3e\u51fa\u5217\u64cd\u4f5c\n else:\n val: int = self._rear.val # \u66ab\u5b58\u5c3e\u7bc0\u9ede\u503c\n # \u522a\u9664\u5c3e\u7bc0\u9ede\n rprev: ListNode | None = self._rear.prev\n if rprev != None:\n rprev.next = None\n self._rear.prev = None\n self._rear = rprev # \u66f4\u65b0\u5c3e\u7bc0\u9ede\n self._size -= 1 # \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n return val\n\n def pop_first(self) -&gt; int:\n \"\"\"\u4f47\u5217\u9996\u51fa\u5217\"\"\"\n return self.pop(True)\n\n def pop_last(self) -&gt; int:\n \"\"\"\u4f47\u5217\u5c3e\u51fa\u5217\"\"\"\n return self.pop(False)\n\n def peek_first(self) -&gt; int:\n \"\"\"\u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\")\n return self._front.val\n\n def peek_last(self) -&gt; int:\n \"\"\"\u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\")\n return self._rear.val\n\n def to_array(self) -&gt; list[int]:\n \"\"\"\u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370\"\"\"\n node = self._front\n res = [0] * self.size()\n for i in range(self.size()):\n res[i] = node.val\n node = node.next\n return res\n</code></pre> linkedlist_deque.cpp<pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede */\nstruct DoublyListNode {\n int val; // \u7bc0\u9ede\u503c\n DoublyListNode *next; // \u5f8c\u7e7c\u7bc0\u9ede\u6307\u6a19\n DoublyListNode *prev; // \u524d\u9a45\u7bc0\u9ede\u6307\u6a19\n DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n }\n};\n\n/* \u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nclass LinkedListDeque {\n private:\n DoublyListNode *front, *rear; // \u982d\u7bc0\u9ede front \uff0c\u5c3e\u7bc0\u9ede rear\n int queSize = 0; // \u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\n\n public:\n /* \u5efa\u69cb\u5b50 */\n LinkedListDeque() : front(nullptr), rear(nullptr) {\n }\n\n /* \u6790\u69cb\u65b9\u6cd5 */\n ~LinkedListDeque() {\n // \u8d70\u8a2a\u93c8\u7d50\u4e32\u5217\u522a\u9664\u7bc0\u9ede\uff0c\u91cb\u653e\u8a18\u61b6\u9ad4\n DoublyListNode *pre, *cur = front;\n while (cur != nullptr) {\n pre = cur;\n cur = cur-&gt;next;\n delete pre;\n }\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u5217\u64cd\u4f5c */\n void push(int num, bool isFront) {\n DoublyListNode *node = new DoublyListNode(num);\n // \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (isEmpty())\n front = rear = node;\n // \u4f47\u5217\u9996\u5165\u5217\u64cd\u4f5c\n else if (isFront) {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u982d\u90e8\n front-&gt;prev = node;\n node-&gt;next = front;\n front = node; // \u66f4\u65b0\u982d\u7bc0\u9ede\n // \u4f47\u5217\u5c3e\u5165\u5217\u64cd\u4f5c\n } else {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u5c3e\u90e8\n rear-&gt;next = node;\n node-&gt;prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n queSize++; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n void pushFirst(int num) {\n push(num, true);\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n void pushLast(int num) {\n push(num, false);\n }\n\n /* \u51fa\u5217\u64cd\u4f5c */\n int pop(bool isFront) {\n if (isEmpty())\n throw out_of_range(\"\u4f47\u5217\u70ba\u7a7a\");\n int val;\n // \u4f47\u5217\u9996\u51fa\u5217\u64cd\u4f5c\n if (isFront) {\n val = front-&gt;val; // \u66ab\u5b58\u982d\u7bc0\u9ede\u503c\n // \u522a\u9664\u982d\u7bc0\u9ede\n DoublyListNode *fNext = front-&gt;next;\n if (fNext != nullptr) {\n fNext-&gt;prev = nullptr;\n front-&gt;next = nullptr;\n }\n delete front;\n front = fNext; // \u66f4\u65b0\u982d\u7bc0\u9ede\n // \u4f47\u5217\u5c3e\u51fa\u5217\u64cd\u4f5c\n } else {\n val = rear-&gt;val; // \u66ab\u5b58\u5c3e\u7bc0\u9ede\u503c\n // \u522a\u9664\u5c3e\u7bc0\u9ede\n DoublyListNode *rPrev = rear-&gt;prev;\n if (rPrev != nullptr) {\n rPrev-&gt;next = nullptr;\n rear-&gt;prev = nullptr;\n }\n delete rear;\n rear = rPrev; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n queSize--; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n return val;\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n int popFirst() {\n return pop(true);\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n int popLast() {\n return pop(false);\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty())\n throw out_of_range(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\");\n return front-&gt;val;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty())\n throw out_of_range(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\");\n return rear-&gt;val;\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n vector&lt;int&gt; toVector() {\n DoublyListNode *node = front;\n vector&lt;int&gt; res(size());\n for (int i = 0; i &lt; res.size(); i++) {\n res[i] = node-&gt;val;\n node = node-&gt;next;\n }\n return res;\n }\n};\n</code></pre> linkedlist_deque.java<pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede */\nclass ListNode {\n int val; // \u7bc0\u9ede\u503c\n ListNode next; // \u5f8c\u7e7c\u7bc0\u9ede\u5f15\u7528\n ListNode prev; // \u524d\u9a45\u7bc0\u9ede\u5f15\u7528\n\n ListNode(int val) {\n this.val = val;\n prev = next = null;\n }\n}\n\n/* \u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nclass LinkedListDeque {\n private ListNode front, rear; // \u982d\u7bc0\u9ede front \uff0c\u5c3e\u7bc0\u9ede rear\n private int queSize = 0; // \u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\n\n public LinkedListDeque() {\n front = rear = null;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u5217\u64cd\u4f5c */\n private void push(int num, boolean isFront) {\n ListNode node = new ListNode(num);\n // \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (isEmpty())\n front = rear = node;\n // \u4f47\u5217\u9996\u5165\u5217\u64cd\u4f5c\n else if (isFront) {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u982d\u90e8\n front.prev = node;\n node.next = front;\n front = node; // \u66f4\u65b0\u982d\u7bc0\u9ede\n // \u4f47\u5217\u5c3e\u5165\u5217\u64cd\u4f5c\n } else {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u5c3e\u90e8\n rear.next = node;\n node.prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n queSize++; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n public void pushFirst(int num) {\n push(num, true);\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n public void pushLast(int num) {\n push(num, false);\n }\n\n /* \u51fa\u5217\u64cd\u4f5c */\n private int pop(boolean isFront) {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n int val;\n // \u4f47\u5217\u9996\u51fa\u5217\u64cd\u4f5c\n if (isFront) {\n val = front.val; // \u66ab\u5b58\u982d\u7bc0\u9ede\u503c\n // \u522a\u9664\u982d\u7bc0\u9ede\n ListNode fNext = front.next;\n if (fNext != null) {\n fNext.prev = null;\n front.next = null;\n }\n front = fNext; // \u66f4\u65b0\u982d\u7bc0\u9ede\n // \u4f47\u5217\u5c3e\u51fa\u5217\u64cd\u4f5c\n } else {\n val = rear.val; // \u66ab\u5b58\u5c3e\u7bc0\u9ede\u503c\n // \u522a\u9664\u5c3e\u7bc0\u9ede\n ListNode rPrev = rear.prev;\n if (rPrev != null) {\n rPrev.next = null;\n rear.prev = null;\n }\n rear = rPrev; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n queSize--; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n return val;\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n public int popFirst() {\n return pop(true);\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n public int popLast() {\n return pop(false);\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n public int peekFirst() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return front.val;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n public int peekLast() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return rear.val;\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n public int[] toArray() {\n ListNode node = front;\n int[] res = new int[size()];\n for (int i = 0; i &lt; res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n</code></pre> linkedlist_deque.cs<pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede */\nclass ListNode(int val) {\n public int val = val; // \u7bc0\u9ede\u503c\n public ListNode? next = null; // \u5f8c\u7e7c\u7bc0\u9ede\u5f15\u7528\n public ListNode? prev = null; // \u524d\u9a45\u7bc0\u9ede\u5f15\u7528\n}\n\n/* \u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nclass LinkedListDeque {\n ListNode? front, rear; // \u982d\u7bc0\u9ede front, \u5c3e\u7bc0\u9ede rear\n int queSize = 0; // \u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\n\n public LinkedListDeque() {\n front = null;\n rear = null;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u5217\u64cd\u4f5c */\n void Push(int num, bool isFront) {\n ListNode node = new(num);\n // \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (IsEmpty()) {\n front = node;\n rear = node;\n }\n // \u4f47\u5217\u9996\u5165\u5217\u64cd\u4f5c\n else if (isFront) {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u982d\u90e8\n front!.prev = node;\n node.next = front;\n front = node; // \u66f4\u65b0\u982d\u7bc0\u9ede \n }\n // \u4f47\u5217\u5c3e\u5165\u5217\u64cd\u4f5c\n else {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u5c3e\u90e8\n rear!.next = node;\n node.prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n\n queSize++; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n public void PushFirst(int num) {\n Push(num, true);\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n public void PushLast(int num) {\n Push(num, false);\n }\n\n /* \u51fa\u5217\u64cd\u4f5c */\n int? Pop(bool isFront) {\n if (IsEmpty())\n throw new Exception();\n int? val;\n // \u4f47\u5217\u9996\u51fa\u5217\u64cd\u4f5c\n if (isFront) {\n val = front?.val; // \u66ab\u5b58\u982d\u7bc0\u9ede\u503c\n // \u522a\u9664\u982d\u7bc0\u9ede\n ListNode? fNext = front?.next;\n if (fNext != null) {\n fNext.prev = null;\n front!.next = null;\n }\n front = fNext; // \u66f4\u65b0\u982d\u7bc0\u9ede\n }\n // \u4f47\u5217\u5c3e\u51fa\u5217\u64cd\u4f5c\n else {\n val = rear?.val; // \u66ab\u5b58\u5c3e\u7bc0\u9ede\u503c\n // \u522a\u9664\u5c3e\u7bc0\u9ede\n ListNode? rPrev = rear?.prev;\n if (rPrev != null) {\n rPrev.next = null;\n rear!.prev = null;\n }\n rear = rPrev; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n\n queSize--; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n return val;\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n public int? PopFirst() {\n return Pop(true);\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n public int? PopLast() {\n return Pop(false);\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n public int? PeekFirst() {\n if (IsEmpty())\n throw new Exception();\n return front?.val;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n public int? PeekLast() {\n if (IsEmpty())\n throw new Exception();\n return rear?.val;\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n public int?[] ToArray() {\n ListNode? node = front;\n int?[] res = new int?[Size()];\n for (int i = 0; i &lt; res.Length; i++) {\n res[i] = node?.val;\n node = node?.next;\n }\n\n return res;\n }\n}\n</code></pre> linkedlist_deque.go<pre><code>/* \u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\ntype linkedListDeque struct {\n // \u4f7f\u7528\u5167\u5efa\u5305 list\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u96d9\u7aef\u4f47\u5217 */\nfunc newLinkedListDeque() *linkedListDeque {\n return &amp;linkedListDeque{\n data: list.New(),\n }\n}\n\n/* \u4f47\u5217\u9996\u5143\u7d20\u5165\u5217 */\nfunc (s *linkedListDeque) pushFirst(value any) {\n s.data.PushFront(value)\n}\n\n/* \u4f47\u5217\u5c3e\u5143\u7d20\u5165\u5217 */\nfunc (s *linkedListDeque) pushLast(value any) {\n s.data.PushBack(value)\n}\n\n/* \u4f47\u5217\u9996\u5143\u7d20\u51fa\u5217 */\nfunc (s *linkedListDeque) popFirst() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u4f47\u5217\u5c3e\u5143\u7d20\u51fa\u5217 */\nfunc (s *linkedListDeque) popLast() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nfunc (s *linkedListDeque) peekFirst() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n return e.Value\n}\n\n/* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\nfunc (s *linkedListDeque) peekLast() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n return e.Value\n}\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nfunc (s *linkedListDeque) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nfunc (s *linkedListDeque) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u7372\u53d6 List \u7528\u65bc\u5217\u5370 */\nfunc (s *linkedListDeque) toList() *list.List {\n return s.data\n}\n</code></pre> linkedlist_deque.swift<pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede */\nclass ListNode {\n var val: Int // \u7bc0\u9ede\u503c\n var next: ListNode? // \u5f8c\u7e7c\u7bc0\u9ede\u5f15\u7528\n weak var prev: ListNode? // \u524d\u9a45\u7bc0\u9ede\u5f15\u7528\n\n init(val: Int) {\n self.val = val\n }\n}\n\n/* \u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nclass LinkedListDeque {\n private var front: ListNode? // \u982d\u7bc0\u9ede front\n private var rear: ListNode? // \u5c3e\u7bc0\u9ede rear\n private var _size: Int // \u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\n\n init() {\n _size = 0\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n func size() -&gt; Int {\n _size\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n func isEmpty() -&gt; Bool {\n size() == 0\n }\n\n /* \u5165\u5217\u64cd\u4f5c */\n private func push(num: Int, isFront: Bool) {\n let node = ListNode(val: num)\n // \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if isEmpty() {\n front = node\n rear = node\n }\n // \u4f47\u5217\u9996\u5165\u5217\u64cd\u4f5c\n else if isFront {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u982d\u90e8\n front?.prev = node\n node.next = front\n front = node // \u66f4\u65b0\u982d\u7bc0\u9ede\n }\n // \u4f47\u5217\u5c3e\u5165\u5217\u64cd\u4f5c\n else {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u5c3e\u90e8\n rear?.next = node\n node.prev = rear\n rear = node // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n _size += 1 // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n func pushFirst(num: Int) {\n push(num: num, isFront: true)\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n func pushLast(num: Int) {\n push(num: num, isFront: false)\n }\n\n /* \u51fa\u5217\u64cd\u4f5c */\n private func pop(isFront: Bool) -&gt; Int {\n if isEmpty() {\n fatalError(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\")\n }\n let val: Int\n // \u4f47\u5217\u9996\u51fa\u5217\u64cd\u4f5c\n if isFront {\n val = front!.val // \u66ab\u5b58\u982d\u7bc0\u9ede\u503c\n // \u522a\u9664\u982d\u7bc0\u9ede\n let fNext = front?.next\n if fNext != nil {\n fNext?.prev = nil\n front?.next = nil\n }\n front = fNext // \u66f4\u65b0\u982d\u7bc0\u9ede\n }\n // \u4f47\u5217\u5c3e\u51fa\u5217\u64cd\u4f5c\n else {\n val = rear!.val // \u66ab\u5b58\u5c3e\u7bc0\u9ede\u503c\n // \u522a\u9664\u5c3e\u7bc0\u9ede\n let rPrev = rear?.prev\n if rPrev != nil {\n rPrev?.next = nil\n rear?.prev = nil\n }\n rear = rPrev // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n _size -= 1 // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n return val\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n func popFirst() -&gt; Int {\n pop(isFront: true)\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n func popLast() -&gt; Int {\n pop(isFront: false)\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n func peekFirst() -&gt; Int {\n if isEmpty() {\n fatalError(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\")\n }\n return front!.val\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n func peekLast() -&gt; Int {\n if isEmpty() {\n fatalError(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\")\n }\n return rear!.val\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n func toArray() -&gt; [Int] {\n var node = front\n var res = Array(repeating: 0, count: size())\n for i in res.indices {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n</code></pre> linkedlist_deque.js<pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede */\nclass ListNode {\n prev; // \u524d\u9a45\u7bc0\u9ede\u5f15\u7528 (\u6307\u6a19)\n next; // \u5f8c\u7e7c\u7bc0\u9ede\u5f15\u7528 (\u6307\u6a19)\n val; // \u7bc0\u9ede\u503c\n\n constructor(val) {\n this.val = val;\n this.next = null;\n this.prev = null;\n }\n}\n\n/* \u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nclass LinkedListDeque {\n #front; // \u982d\u7bc0\u9ede front\n #rear; // \u5c3e\u7bc0\u9ede rear\n #queSize; // \u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\n\n constructor() {\n this.#front = null;\n this.#rear = null;\n this.#queSize = 0;\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217\u64cd\u4f5c */\n pushLast(val) {\n const node = new ListNode(val);\n // \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.#queSize === 0) {\n this.#front = node;\n this.#rear = node;\n } else {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u5c3e\u90e8\n this.#rear.next = node;\n node.prev = this.#rear;\n this.#rear = node; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n this.#queSize++;\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217\u64cd\u4f5c */\n pushFirst(val) {\n const node = new ListNode(val);\n // \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.#queSize === 0) {\n this.#front = node;\n this.#rear = node;\n } else {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u982d\u90e8\n this.#front.prev = node;\n node.next = this.#front;\n this.#front = node; // \u66f4\u65b0\u982d\u7bc0\u9ede\n }\n this.#queSize++;\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217\u64cd\u4f5c */\n popLast() {\n if (this.#queSize === 0) {\n return null;\n }\n const value = this.#rear.val; // \u5132\u5b58\u5c3e\u7bc0\u9ede\u503c\n // \u522a\u9664\u5c3e\u7bc0\u9ede\n let temp = this.#rear.prev;\n if (temp !== null) {\n temp.next = null;\n this.#rear.prev = null;\n }\n this.#rear = temp; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n this.#queSize--;\n return value;\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217\u64cd\u4f5c */\n popFirst() {\n if (this.#queSize === 0) {\n return null;\n }\n const value = this.#front.val; // \u5132\u5b58\u5c3e\u7bc0\u9ede\u503c\n // \u522a\u9664\u982d\u7bc0\u9ede\n let temp = this.#front.next;\n if (temp !== null) {\n temp.prev = null;\n this.#front.next = null;\n }\n this.#front = temp; // \u66f4\u65b0\u982d\u7bc0\u9ede\n this.#queSize--;\n return value;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n peekLast() {\n return this.#queSize === 0 ? null : this.#rear.val;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n peekFirst() {\n return this.#queSize === 0 ? null : this.#front.val;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n size() {\n return this.#queSize;\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u5217\u5370\u96d9\u5411\u4f47\u5217 */\n print() {\n const arr = [];\n let temp = this.#front;\n while (temp !== null) {\n arr.push(temp.val);\n temp = temp.next;\n }\n console.log('[' + arr.join(', ') + ']');\n }\n}\n</code></pre> linkedlist_deque.ts<pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede */\nclass ListNode {\n prev: ListNode; // \u524d\u9a45\u7bc0\u9ede\u5f15\u7528 (\u6307\u6a19)\n next: ListNode; // \u5f8c\u7e7c\u7bc0\u9ede\u5f15\u7528 (\u6307\u6a19)\n val: number; // \u7bc0\u9ede\u503c\n\n constructor(val: number) {\n this.val = val;\n this.next = null;\n this.prev = null;\n }\n}\n\n/* \u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nclass LinkedListDeque {\n private front: ListNode; // \u982d\u7bc0\u9ede front\n private rear: ListNode; // \u5c3e\u7bc0\u9ede rear\n private queSize: number; // \u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\n\n constructor() {\n this.front = null;\n this.rear = null;\n this.queSize = 0;\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217\u64cd\u4f5c */\n pushLast(val: number): void {\n const node: ListNode = new ListNode(val);\n // \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.queSize === 0) {\n this.front = node;\n this.rear = node;\n } else {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u5c3e\u90e8\n this.rear.next = node;\n node.prev = this.rear;\n this.rear = node; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n this.queSize++;\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217\u64cd\u4f5c */\n pushFirst(val: number): void {\n const node: ListNode = new ListNode(val);\n // \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.queSize === 0) {\n this.front = node;\n this.rear = node;\n } else {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u982d\u90e8\n this.front.prev = node;\n node.next = this.front;\n this.front = node; // \u66f4\u65b0\u982d\u7bc0\u9ede\n }\n this.queSize++;\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217\u64cd\u4f5c */\n popLast(): number {\n if (this.queSize === 0) {\n return null;\n }\n const value: number = this.rear.val; // \u5132\u5b58\u5c3e\u7bc0\u9ede\u503c\n // \u522a\u9664\u5c3e\u7bc0\u9ede\n let temp: ListNode = this.rear.prev;\n if (temp !== null) {\n temp.next = null;\n this.rear.prev = null;\n }\n this.rear = temp; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n this.queSize--;\n return value;\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217\u64cd\u4f5c */\n popFirst(): number {\n if (this.queSize === 0) {\n return null;\n }\n const value: number = this.front.val; // \u5132\u5b58\u5c3e\u7bc0\u9ede\u503c\n // \u522a\u9664\u982d\u7bc0\u9ede\n let temp: ListNode = this.front.next;\n if (temp !== null) {\n temp.prev = null;\n this.front.next = null;\n }\n this.front = temp; // \u66f4\u65b0\u982d\u7bc0\u9ede\n this.queSize--;\n return value;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n peekLast(): number {\n return this.queSize === 0 ? null : this.rear.val;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n peekFirst(): number {\n return this.queSize === 0 ? null : this.front.val;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u5217\u5370\u96d9\u5411\u4f47\u5217 */\n print(): void {\n const arr: number[] = [];\n let temp: ListNode = this.front;\n while (temp !== null) {\n arr.push(temp.val);\n temp = temp.next;\n }\n console.log('[' + arr.join(', ') + ']');\n }\n}\n</code></pre> linkedlist_deque.dart<pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede */\nclass ListNode {\n int val; // \u7bc0\u9ede\u503c\n ListNode? next; // \u5f8c\u7e7c\u7bc0\u9ede\u5f15\u7528\n ListNode? prev; // \u524d\u9a45\u7bc0\u9ede\u5f15\u7528\n\n ListNode(this.val, {this.next, this.prev});\n}\n\n/* \u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u96d9\u5411\u5c0d\u5217 */\nclass LinkedListDeque {\n late ListNode? _front; // \u982d\u7bc0\u9ede _front\n late ListNode? _rear; // \u5c3e\u7bc0\u9ede _rear\n int _queSize = 0; // \u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\n\n LinkedListDeque() {\n this._front = null;\n this._rear = null;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u9577\u5ea6 */\n int size() {\n return this._queSize;\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u5217\u64cd\u4f5c */\n void push(int _num, bool isFront) {\n final ListNode node = ListNode(_num);\n if (isEmpty()) {\n // \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 _front \u548c _rear \u90fd\u6307\u5411 node\n _front = _rear = node;\n } else if (isFront) {\n // \u4f47\u5217\u9996\u5165\u5217\u64cd\u4f5c\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u982d\u90e8\n _front!.prev = node;\n node.next = _front;\n _front = node; // \u66f4\u65b0\u982d\u7bc0\u9ede\n } else {\n // \u4f47\u5217\u5c3e\u5165\u5217\u64cd\u4f5c\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u5c3e\u90e8\n _rear!.next = node;\n node.prev = _rear;\n _rear = node; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n _queSize++; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n void pushFirst(int _num) {\n push(_num, true);\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n void pushLast(int _num) {\n push(_num, false);\n }\n\n /* \u51fa\u5217\u64cd\u4f5c */\n int? pop(bool isFront) {\n // \u82e5\u4f47\u5217\u70ba\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\n if (isEmpty()) {\n return null;\n }\n final int val;\n if (isFront) {\n // \u4f47\u5217\u9996\u51fa\u5217\u64cd\u4f5c\n val = _front!.val; // \u66ab\u5b58\u982d\u7bc0\u9ede\u503c\n // \u522a\u9664\u982d\u7bc0\u9ede\n ListNode? fNext = _front!.next;\n if (fNext != null) {\n fNext.prev = null;\n _front!.next = null;\n }\n _front = fNext; // \u66f4\u65b0\u982d\u7bc0\u9ede\n } else {\n // \u4f47\u5217\u5c3e\u51fa\u5217\u64cd\u4f5c\n val = _rear!.val; // \u66ab\u5b58\u5c3e\u7bc0\u9ede\u503c\n // \u522a\u9664\u5c3e\u7bc0\u9ede\n ListNode? rPrev = _rear!.prev;\n if (rPrev != null) {\n rPrev.next = null;\n _rear!.prev = null;\n }\n _rear = rPrev; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n _queSize--; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n return val;\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n int? popFirst() {\n return pop(true);\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n int? popLast() {\n return pop(false);\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n int? peekFirst() {\n return _front?.val;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n int? peekLast() {\n return _rear?.val;\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n List&lt;int&gt; toArray() {\n ListNode? node = _front;\n final List&lt;int&gt; res = [];\n for (int i = 0; i &lt; _queSize; i++) {\n res.add(node!.val);\n node = node.next;\n }\n return res;\n }\n}\n</code></pre> linkedlist_deque.rs<pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede */\npub struct ListNode&lt;T&gt; {\n pub val: T, // \u7bc0\u9ede\u503c\n pub next: Option&lt;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;, // \u5f8c\u7e7c\u7bc0\u9ede\u6307\u6a19\n pub prev: Option&lt;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;, // \u524d\u9a45\u7bc0\u9ede\u6307\u6a19\n}\n\nimpl&lt;T&gt; ListNode&lt;T&gt; {\n pub fn new(val: T) -&gt; Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt; {\n Rc::new(RefCell::new(ListNode {\n val,\n next: None,\n prev: None,\n }))\n }\n}\n\n/* \u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\n#[allow(dead_code)]\npub struct LinkedListDeque&lt;T&gt; {\n front: Option&lt;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;, // \u982d\u7bc0\u9ede front\n rear: Option&lt;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;, // \u5c3e\u7bc0\u9ede rear\n que_size: usize, // \u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\n}\n\nimpl&lt;T: Copy&gt; LinkedListDeque&lt;T&gt; {\n pub fn new() -&gt; Self {\n Self {\n front: None,\n rear: None,\n que_size: 0,\n }\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n pub fn size(&amp;self) -&gt; usize {\n return self.que_size;\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n pub fn is_empty(&amp;self) -&gt; bool {\n return self.size() == 0;\n }\n\n /* \u5165\u5217\u64cd\u4f5c */\n pub fn push(&amp;mut self, num: T, is_front: bool) {\n let node = ListNode::new(num);\n // \u4f47\u5217\u9996\u5165\u5217\u64cd\u4f5c\n if is_front {\n match self.front.take() {\n // \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n None =&gt; {\n self.rear = Some(node.clone());\n self.front = Some(node);\n }\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u982d\u90e8\n Some(old_front) =&gt; {\n old_front.borrow_mut().prev = Some(node.clone());\n node.borrow_mut().next = Some(old_front);\n self.front = Some(node); // \u66f4\u65b0\u982d\u7bc0\u9ede\n }\n }\n }\n // \u4f47\u5217\u5c3e\u5165\u5217\u64cd\u4f5c\n else {\n match self.rear.take() {\n // \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n None =&gt; {\n self.front = Some(node.clone());\n self.rear = Some(node);\n }\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u5c3e\u90e8\n Some(old_rear) =&gt; {\n old_rear.borrow_mut().next = Some(node.clone());\n node.borrow_mut().prev = Some(old_rear);\n self.rear = Some(node); // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n }\n }\n self.que_size += 1; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n pub fn push_first(&amp;mut self, num: T) {\n self.push(num, true);\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n pub fn push_last(&amp;mut self, num: T) {\n self.push(num, false);\n }\n\n /* \u51fa\u5217\u64cd\u4f5c */\n pub fn pop(&amp;mut self, is_front: bool) -&gt; Option&lt;T&gt; {\n // \u82e5\u4f47\u5217\u70ba\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de None\n if self.is_empty() {\n return None;\n };\n // \u4f47\u5217\u9996\u51fa\u5217\u64cd\u4f5c\n if is_front {\n self.front.take().map(|old_front| {\n match old_front.borrow_mut().next.take() {\n Some(new_front) =&gt; {\n new_front.borrow_mut().prev.take();\n self.front = Some(new_front); // \u66f4\u65b0\u982d\u7bc0\u9ede\n }\n None =&gt; {\n self.rear.take();\n }\n }\n self.que_size -= 1; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n Rc::try_unwrap(old_front).ok().unwrap().into_inner().val\n })\n }\n // \u4f47\u5217\u5c3e\u51fa\u5217\u64cd\u4f5c\n else {\n self.rear.take().map(|old_rear| {\n match old_rear.borrow_mut().prev.take() {\n Some(new_rear) =&gt; {\n new_rear.borrow_mut().next.take();\n self.rear = Some(new_rear); // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n None =&gt; {\n self.front.take();\n }\n }\n self.que_size -= 1; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n Rc::try_unwrap(old_rear).ok().unwrap().into_inner().val\n })\n }\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n pub fn pop_first(&amp;mut self) -&gt; Option&lt;T&gt; {\n return self.pop(true);\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n pub fn pop_last(&amp;mut self) -&gt; Option&lt;T&gt; {\n return self.pop(false);\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n pub fn peek_first(&amp;self) -&gt; Option&lt;&amp;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt; {\n self.front.as_ref()\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n pub fn peek_last(&amp;self) -&gt; Option&lt;&amp;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt; {\n self.rear.as_ref()\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n pub fn to_array(&amp;self, head: Option&lt;&amp;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;) -&gt; Vec&lt;T&gt; {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.insert(0, node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n</code></pre> linkedlist_deque.c<pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede */\ntypedef struct DoublyListNode {\n int val; // \u7bc0\u9ede\u503c\n struct DoublyListNode *next; // \u5f8c\u7e7c\u7bc0\u9ede\n struct DoublyListNode *prev; // \u524d\u9a45\u7bc0\u9ede\n} DoublyListNode;\n\n/* \u5efa\u69cb\u5b50 */\nDoublyListNode *newDoublyListNode(int num) {\n DoublyListNode *new = (DoublyListNode *)malloc(sizeof(DoublyListNode));\n new-&gt;val = num;\n new-&gt;next = NULL;\n new-&gt;prev = NULL;\n return new;\n}\n\n/* \u6790\u69cb\u51fd\u5f0f */\nvoid delDoublyListNode(DoublyListNode *node) {\n free(node);\n}\n\n/* \u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\ntypedef struct {\n DoublyListNode *front, *rear; // \u982d\u7bc0\u9ede front \uff0c\u5c3e\u7bc0\u9ede rear\n int queSize; // \u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\n} LinkedListDeque;\n\n/* \u5efa\u69cb\u5b50 */\nLinkedListDeque *newLinkedListDeque() {\n LinkedListDeque *deque = (LinkedListDeque *)malloc(sizeof(LinkedListDeque));\n deque-&gt;front = NULL;\n deque-&gt;rear = NULL;\n deque-&gt;queSize = 0;\n return deque;\n}\n\n/* \u6790\u69cb\u51fd\u5f0f */\nvoid delLinkedListdeque(LinkedListDeque *deque) {\n // \u91cb\u653e\u6240\u6709\u7bc0\u9ede\n for (int i = 0; i &lt; deque-&gt;queSize &amp;&amp; deque-&gt;front != NULL; i++) {\n DoublyListNode *tmp = deque-&gt;front;\n deque-&gt;front = deque-&gt;front-&gt;next;\n free(tmp);\n }\n // \u91cb\u653e deque \u7d50\u69cb\u9ad4\n free(deque);\n}\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nint size(LinkedListDeque *deque) {\n return deque-&gt;queSize;\n}\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nbool empty(LinkedListDeque *deque) {\n return (size(deque) == 0);\n}\n\n/* \u5165\u5217 */\nvoid push(LinkedListDeque *deque, int num, bool isFront) {\n DoublyListNode *node = newDoublyListNode(num);\n // \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411node\n if (empty(deque)) {\n deque-&gt;front = deque-&gt;rear = node;\n }\n // \u4f47\u5217\u9996\u5165\u5217\u64cd\u4f5c\n else if (isFront) {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u982d\u90e8\n deque-&gt;front-&gt;prev = node;\n node-&gt;next = deque-&gt;front;\n deque-&gt;front = node; // \u66f4\u65b0\u982d\u7bc0\u9ede\n }\n // \u4f47\u5217\u5c3e\u5165\u5217\u64cd\u4f5c\n else {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u5c3e\u90e8\n deque-&gt;rear-&gt;next = node;\n node-&gt;prev = deque-&gt;rear;\n deque-&gt;rear = node;\n }\n deque-&gt;queSize++; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n}\n\n/* \u4f47\u5217\u9996\u5165\u5217 */\nvoid pushFirst(LinkedListDeque *deque, int num) {\n push(deque, num, true);\n}\n\n/* \u4f47\u5217\u5c3e\u5165\u5217 */\nvoid pushLast(LinkedListDeque *deque, int num) {\n push(deque, num, false);\n}\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nint peekFirst(LinkedListDeque *deque) {\n assert(size(deque) &amp;&amp; deque-&gt;front);\n return deque-&gt;front-&gt;val;\n}\n\n/* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\nint peekLast(LinkedListDeque *deque) {\n assert(size(deque) &amp;&amp; deque-&gt;rear);\n return deque-&gt;rear-&gt;val;\n}\n\n/* \u51fa\u5217 */\nint pop(LinkedListDeque *deque, bool isFront) {\n if (empty(deque))\n return -1;\n int val;\n // \u4f47\u5217\u9996\u51fa\u5217\u64cd\u4f5c\n if (isFront) {\n val = peekFirst(deque); // \u66ab\u5b58\u982d\u7bc0\u9ede\u503c\n DoublyListNode *fNext = deque-&gt;front-&gt;next;\n if (fNext) {\n fNext-&gt;prev = NULL;\n deque-&gt;front-&gt;next = NULL;\n }\n delDoublyListNode(deque-&gt;front);\n deque-&gt;front = fNext; // \u66f4\u65b0\u982d\u7bc0\u9ede\n }\n // \u4f47\u5217\u5c3e\u51fa\u5217\u64cd\u4f5c\n else {\n val = peekLast(deque); // \u66ab\u5b58\u5c3e\u7bc0\u9ede\u503c\n DoublyListNode *rPrev = deque-&gt;rear-&gt;prev;\n if (rPrev) {\n rPrev-&gt;next = NULL;\n deque-&gt;rear-&gt;prev = NULL;\n }\n delDoublyListNode(deque-&gt;rear);\n deque-&gt;rear = rPrev; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n deque-&gt;queSize--; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n return val;\n}\n\n/* \u4f47\u5217\u9996\u51fa\u5217 */\nint popFirst(LinkedListDeque *deque) {\n return pop(deque, true);\n}\n\n/* \u4f47\u5217\u5c3e\u51fa\u5217 */\nint popLast(LinkedListDeque *deque) {\n return pop(deque, false);\n}\n\n/* \u5217\u5370\u4f47\u5217 */\nvoid printLinkedListDeque(LinkedListDeque *deque) {\n int *arr = malloc(sizeof(int) * deque-&gt;queSize);\n // \u8907\u88fd\u93c8\u7d50\u4e32\u5217\u4e2d\u7684\u8cc7\u6599\u5230\u9663\u5217\n int i;\n DoublyListNode *node;\n for (i = 0, node = deque-&gt;front; i &lt; deque-&gt;queSize; i++) {\n arr[i] = node-&gt;val;\n node = node-&gt;next;\n }\n printArray(arr, deque-&gt;queSize);\n free(arr);\n}\n</code></pre> linkedlist_deque.kt<pre><code>/* \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede */\nclass ListNode(var _val: Int) {\n // \u7bc0\u9ede\u503c\n var next: ListNode? = null // \u5f8c\u7e7c\u7bc0\u9ede\u5f15\u7528\n var prev: ListNode? = null // \u524d\u9a45\u7bc0\u9ede\u5f15\u7528\n}\n\n/* \u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nclass LinkedListDeque {\n private var front: ListNode? = null // \u982d\u7bc0\u9ede front\n private var rear: ListNode? = null // \u5c3e\u7bc0\u9ede rear\n private var queSize: Int = 0 // \u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n fun size(): Int {\n return queSize\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n fun isEmpty(): Boolean {\n return size() == 0\n }\n\n /* \u5165\u5217\u64cd\u4f5c */\n fun push(num: Int, isFront: Boolean) {\n val node = ListNode(num)\n // \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (isEmpty()) {\n rear = node\n front = rear\n // \u4f47\u5217\u9996\u5165\u5217\u64cd\u4f5c\n } else if (isFront) {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u982d\u90e8\n front?.prev = node\n node.next = front\n front = node // \u66f4\u65b0\u982d\u7bc0\u9ede\n // \u4f47\u5217\u5c3e\u5165\u5217\u64cd\u4f5c\n } else {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u5c3e\u90e8\n rear?.next = node\n node.prev = rear\n rear = node // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n queSize++ // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n fun pushFirst(num: Int) {\n push(num, true)\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n fun pushLast(num: Int) {\n push(num, false)\n }\n\n /* \u51fa\u5217\u64cd\u4f5c */\n fun pop(isFront: Boolean): Int {\n if (isEmpty()) \n throw IndexOutOfBoundsException()\n val _val: Int\n // \u4f47\u5217\u9996\u51fa\u5217\u64cd\u4f5c\n if (isFront) {\n _val = front!!._val // \u66ab\u5b58\u982d\u7bc0\u9ede\u503c\n // \u522a\u9664\u982d\u7bc0\u9ede\n val fNext = front!!.next\n if (fNext != null) {\n fNext.prev = null\n front!!.next = null\n }\n front = fNext // \u66f4\u65b0\u982d\u7bc0\u9ede\n // \u4f47\u5217\u5c3e\u51fa\u5217\u64cd\u4f5c\n } else {\n _val = rear!!._val // \u66ab\u5b58\u5c3e\u7bc0\u9ede\u503c\n // \u522a\u9664\u5c3e\u7bc0\u9ede\n val rPrev = rear!!.prev\n if (rPrev != null) {\n rPrev.next = null\n rear!!.prev = null\n }\n rear = rPrev // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n queSize-- // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n return _val\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n fun popFirst(): Int {\n return pop(true)\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n fun popLast(): Int {\n return pop(false)\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n fun peekFirst(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return front!!._val\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n fun peekLast(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return rear!!._val\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n fun toArray(): IntArray {\n var node = front\n val res = IntArray(size())\n for (i in res.indices) {\n res[i] = node!!._val\n node = node.next\n }\n return res\n }\n}\n</code></pre> linkedlist_deque.rb<pre><code>=begin\nFile: linkedlist_deque.rb\nCreated Time: 2024-04-06\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n### \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\nclass ListNode\n attr_accessor :val\n attr_accessor :next # \u5f8c\u7e7c\u7bc0\u9ede\u5f15\u7528\n attr_accessor :prev # \u524d\u8ec0\u7bc0\u9ede\u5f15\u7528\n\n ### \u5efa\u69cb\u5b50 ###\n def initialize(val)\n @val = val\n end\nend\n\n### \u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 ###\nclass LinkedListDeque\n ### \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 ###\n attr_reader :size\n\n ### \u5efa\u69cb\u5b50 ###\n def initialize\n @front = nil # \u982d\u7bc0\u9ede front\n @rear = nil # \u5c3e\u7bc0\u9ede rear\n @size = 0 # \u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\n end\n\n ### \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a ###\n def is_empty?\n size.zero?\n end\n\n ### \u5165\u5217\u64cd\u4f5c ###\n def push(num, is_front)\n node = ListNode.new(num)\n # \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c \u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if is_empty?\n @front = @rear = node\n # \u4f47\u5217\u9996\u5165\u5217\u64cd\u4f5c\n elsif is_front\n # \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u982d\u90e8\n @front.prev = node\n node.next = @front\n @front = node # \u66f4\u65b0\u982d\u7bc0\u9ede\n # \u4f47\u5217\u5c3e\u5165\u5217\u64cd\u4f5c\n else\n # \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u5c3e\u90e8\n @rear.next = node\n node.prev = @rear\n @rear = node # \u66f4\u65b0\u5c3e\u7bc0\u9ede\n end\n @size += 1 # \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n end\n\n ### \u4f47\u5217\u9996\u5165\u5217 ###\n def push_first(num)\n push(num, true)\n end\n\n ### \u4f47\u5217\u5c3e\u5165\u5217 ###\n def push_last(num)\n push(num, false)\n end\n\n ### \u51fa\u5217\u64cd\u4f5c ###\n def pop(is_front)\n raise IndexError, '\u96d9\u5411\u4f47\u5217\u70ba\u7a7a' if is_empty?\n\n # \u4f47\u5217\u9996\u51fa\u5217\u64cd\u4f5c\n if is_front\n val = @front.val # \u66ab\u5b58\u982d\u7bc0\u9ede\u503c\n # \u522a\u9664\u982d\u7bc0\u9ede\n fnext = @front.next\n unless fnext.nil?\n fnext.prev = nil\n @front.next = nil\n end\n @front = fnext # \u66f4\u65b0\u982d\u7bc0\u9ede\n # \u4f47\u5217\u5c3e\u51fa\u5217\u64cd\u4f5c\n else\n val = @rear.val # \u66ab\u5b58\u5c3e\u7bc0\u9ede\u503c\n # \u522a\u9664\u5c3e\u7bc0\u9ede\n rprev = @rear.prev\n unless rprev.nil?\n rprev.next = nil\n @rear.prev = nil\n end\n @rear = rprev # \u66f4\u65b0\u5c3e\u7bc0\u9ede\n end\n @size -= 1 # \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n\n val\n end\n\n ### \u4f47\u5217\u9996\u51fa\u5217 ###\n def pop_first\n pop(true)\n end\n\n ### \u4f47\u5217\u9996\u51fa\u5217 ###\n def pop_last\n pop(false)\n end\n\n ### \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 ###\n def peek_first\n raise IndexError, '\u96d9\u5411\u4f47\u5217\u70ba\u7a7a' if is_empty?\n\n @front.val\n end\n\n ### \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 ###\n def peek_last\n raise IndexError, '\u96d9\u5411\u4f47\u5217\u70ba\u7a7a' if is_empty?\n\n @rear.val\n end\n\n ### \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 ###\n def to_array\n node = @front\n res = Array.new(size, 0)\n for i in 0...size\n res[i] = node.val\n node = node.next\n end\n res\n end\nend\n</code></pre> linkedlist_deque.zig<pre><code>// \u96d9\u5411\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\nfn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = undefined, // \u7bc0\u9ede\u503c\n next: ?*Self = null, // \u5f8c\u7e7c\u7bc0\u9ede\u6307\u6a19\n prev: ?*Self = null, // \u524d\u9a45\u7bc0\u9ede\u6307\u6a19\n\n // Initialize a list node with specific value\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n self.prev = null;\n }\n };\n}\n\n// \u57fa\u65bc\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217\nfn LinkedListDeque(comptime T: type) type {\n return struct {\n const Self = @This();\n\n front: ?*ListNode(T) = null, // \u982d\u7bc0\u9ede front\n rear: ?*ListNode(T) = null, // \u5c3e\u7bc0\u9ede rear\n que_size: usize = 0, // \u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u8a18\u61b6\u9ad4\u5206\u914d\u5668\n\n // \u5efa\u69cb\u5b50\uff08\u5206\u914d\u8a18\u61b6\u9ad4+\u521d\u59cb\u5316\u4f47\u5217\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.front = null;\n self.rear = null;\n self.que_size = 0;\n }\n\n // \u6790\u69cb\u51fd\u5f0f\uff08\u91cb\u653e\u8a18\u61b6\u9ad4\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\n pub fn size(self: *Self) usize {\n return self.que_size;\n }\n\n // \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u5165\u5217\u64cd\u4f5c\n pub fn push(self: *Self, num: T, is_front: bool) !void {\n var node = try self.mem_allocator.create(ListNode(T));\n node.init(num);\n // \u82e5\u93c8\u7d50\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (self.isEmpty()) {\n self.front = node;\n self.rear = node;\n // \u4f47\u5217\u9996\u5165\u5217\u64cd\u4f5c\n } else if (is_front) {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u982d\u90e8\n self.front.?.prev = node;\n node.next = self.front;\n self.front = node; // \u66f4\u65b0\u982d\u7bc0\u9ede\n // \u4f47\u5217\u5c3e\u5165\u5217\u64cd\u4f5c\n } else {\n // \u5c07 node \u65b0\u589e\u81f3\u93c8\u7d50\u4e32\u5217\u5c3e\u90e8\n self.rear.?.next = node;\n node.prev = self.rear;\n self.rear = node; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n self.que_size += 1; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n } \n\n // \u4f47\u5217\u9996\u5165\u5217\n pub fn pushFirst(self: *Self, num: T) !void {\n try self.push(num, true);\n } \n\n // \u4f47\u5217\u5c3e\u5165\u5217\n pub fn pushLast(self: *Self, num: T) !void {\n try self.push(num, false);\n } \n\n // \u51fa\u5217\u64cd\u4f5c\n pub fn pop(self: *Self, is_front: bool) T {\n if (self.isEmpty()) @panic(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\");\n var val: T = undefined;\n // \u4f47\u5217\u9996\u51fa\u5217\u64cd\u4f5c\n if (is_front) {\n val = self.front.?.val; // \u66ab\u5b58\u982d\u7bc0\u9ede\u503c\n // \u522a\u9664\u982d\u7bc0\u9ede\n var fNext = self.front.?.next;\n if (fNext != null) {\n fNext.?.prev = null;\n self.front.?.next = null;\n }\n self.front = fNext; // \u66f4\u65b0\u982d\u7bc0\u9ede\n // \u4f47\u5217\u5c3e\u51fa\u5217\u64cd\u4f5c\n } else {\n val = self.rear.?.val; // \u66ab\u5b58\u5c3e\u7bc0\u9ede\u503c\n // \u522a\u9664\u5c3e\u7bc0\u9ede\n var rPrev = self.rear.?.prev;\n if (rPrev != null) {\n rPrev.?.next = null;\n self.rear.?.prev = null;\n }\n self.rear = rPrev; // \u66f4\u65b0\u5c3e\u7bc0\u9ede\n }\n self.que_size -= 1; // \u66f4\u65b0\u4f47\u5217\u9577\u5ea6\n return val;\n } \n\n // \u4f47\u5217\u9996\u51fa\u5217\n pub fn popFirst(self: *Self) T {\n return self.pop(true);\n } \n\n // \u4f47\u5217\u5c3e\u51fa\u5217\n pub fn popLast(self: *Self) T {\n return self.pop(false);\n } \n\n // \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20\n pub fn peekFirst(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\");\n return self.front.?.val;\n } \n\n // \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20\n pub fn peekLast(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\");\n return self.rear.?.val;\n }\n\n // \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370\n pub fn toArray(self: *Self) ![]T {\n var node = self.front;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i &lt; res.len) : (i += 1) {\n res[i] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n</code></pre>"},{"location":"chapter_stack_and_queue/deque/#2","title":"2. \u00a0 \u57fa\u65bc\u9663\u5217\u7684\u5be6\u73fe","text":"<p>\u5982\u5716 5-9 \u6240\u793a\uff0c\u8207\u57fa\u65bc\u9663\u5217\u5be6\u73fe\u4f47\u5217\u985e\u4f3c\uff0c\u6211\u5011\u4e5f\u53ef\u4ee5\u4f7f\u7528\u74b0\u5f62\u9663\u5217\u4f86\u5be6\u73fe\u96d9\u5411\u4f47\u5217\u3002</p> ArrayDequepush_last()push_first()pop_last()pop_first() <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 5-9 \u00a0 \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u96d9\u5411\u4f47\u5217\u7684\u5165\u5217\u51fa\u5217\u64cd\u4f5c </p> <p>\u5728\u4f47\u5217\u7684\u5be6\u73fe\u57fa\u790e\u4e0a\uff0c\u50c5\u9700\u589e\u52a0\u201c\u4f47\u5217\u9996\u5165\u5217\u201d\u548c\u201c\u4f47\u5217\u5c3e\u51fa\u5217\u201d\u7684\u65b9\u6cd5\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array_deque.py<pre><code>class ArrayDeque:\n \"\"\"\u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217\"\"\"\n\n def __init__(self, capacity: int):\n \"\"\"\u5efa\u69cb\u5b50\"\"\"\n self._nums: list[int] = [0] * capacity\n self._front: int = 0\n self._size: int = 0\n\n def capacity(self) -&gt; int:\n \"\"\"\u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u5bb9\u91cf\"\"\"\n return len(self._nums)\n\n def size(self) -&gt; int:\n \"\"\"\u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -&gt; bool:\n \"\"\"\u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a\"\"\"\n return self._size == 0\n\n def index(self, i: int) -&gt; int:\n \"\"\"\u8a08\u7b97\u74b0\u5f62\u9663\u5217\u7d22\u5f15\"\"\"\n # \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe\u9663\u5217\u9996\u5c3e\u76f8\u9023\n # \u7576 i \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\uff0c\u56de\u5230\u982d\u90e8\n # \u7576 i \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + self.capacity()) % self.capacity()\n\n def push_first(self, num: int):\n \"\"\"\u4f47\u5217\u9996\u5165\u5217\"\"\"\n if self._size == self.capacity():\n print(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\")\n return\n # \u4f47\u5217\u9996\u6307\u6a19\u5411\u5de6\u79fb\u52d5\u4e00\u4f4d\n # \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe front \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\u56de\u5230\u5c3e\u90e8\n self._front = self.index(self._front - 1)\n # \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u9996\n self._nums[self._front] = num\n self._size += 1\n\n def push_last(self, num: int):\n \"\"\"\u4f47\u5217\u5c3e\u5165\u5217\"\"\"\n if self._size == self.capacity():\n print(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\")\n return\n # \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n rear = self.index(self._front + self._size)\n # \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n self._nums[rear] = num\n self._size += 1\n\n def pop_first(self) -&gt; int:\n \"\"\"\u4f47\u5217\u9996\u51fa\u5217\"\"\"\n num = self.peek_first()\n # \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n self._front = self.index(self._front + 1)\n self._size -= 1\n return num\n\n def pop_last(self) -&gt; int:\n \"\"\"\u4f47\u5217\u5c3e\u51fa\u5217\"\"\"\n num = self.peek_last()\n self._size -= 1\n return num\n\n def peek_first(self) -&gt; int:\n \"\"\"\u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\")\n return self._nums[self._front]\n\n def peek_last(self) -&gt; int:\n \"\"\"\u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\")\n # \u8a08\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n last = self.index(self._front + self._size - 1)\n return self._nums[last]\n\n def to_array(self) -&gt; list[int]:\n \"\"\"\u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370\"\"\"\n # \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n res = []\n for i in range(self._size):\n res.append(self._nums[self.index(self._front + i)])\n return res\n</code></pre> array_deque.cpp<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nclass ArrayDeque {\n private:\n vector&lt;int&gt; nums; // \u7528\u65bc\u5132\u5b58\u96d9\u5411\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n int front; // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n int queSize; // \u96d9\u5411\u4f47\u5217\u9577\u5ea6\n\n public:\n /* \u5efa\u69cb\u5b50 */\n ArrayDeque(int capacity) {\n nums.resize(capacity);\n front = queSize = 0;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return nums.size();\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n bool isEmpty() {\n return queSize == 0;\n }\n\n /* \u8a08\u7b97\u74b0\u5f62\u9663\u5217\u7d22\u5f15 */\n int index(int i) {\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe\u9663\u5217\u9996\u5c3e\u76f8\u9023\n // \u7576 i \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\uff0c\u56de\u5230\u982d\u90e8\n // \u7576 i \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n void pushFirst(int num) {\n if (queSize == capacity()) {\n cout &lt;&lt; \"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\" &lt;&lt; endl;\n return;\n }\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5de6\u79fb\u52d5\u4e00\u4f4d\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe front \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\u56de\u5230\u5c3e\u90e8\n front = index(front - 1);\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n void pushLast(int num) {\n if (queSize == capacity()) {\n cout &lt;&lt; \"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\" &lt;&lt; endl;\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n int rear = index(front + queSize);\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n int popFirst() {\n int num = peekFirst();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n front = index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n int popLast() {\n int num = peekLast();\n queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty())\n throw out_of_range(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\");\n return nums[front];\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty())\n throw out_of_range(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\");\n // \u8a08\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n vector&lt;int&gt; toVector() {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n vector&lt;int&gt; res(queSize);\n for (int i = 0, j = front; i &lt; queSize; i++, j++) {\n res[i] = nums[index(j)];\n }\n return res;\n }\n};\n</code></pre> array_deque.java<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nclass ArrayDeque {\n private int[] nums; // \u7528\u65bc\u5132\u5b58\u96d9\u5411\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n private int front; // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n private int queSize; // \u96d9\u5411\u4f47\u5217\u9577\u5ea6\n\n /* \u5efa\u69cb\u5b50 */\n public ArrayDeque(int capacity) {\n this.nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u5bb9\u91cf */\n public int capacity() {\n return nums.length;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n public boolean isEmpty() {\n return queSize == 0;\n }\n\n /* \u8a08\u7b97\u74b0\u5f62\u9663\u5217\u7d22\u5f15 */\n private int index(int i) {\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe\u9663\u5217\u9996\u5c3e\u76f8\u9023\n // \u7576 i \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\uff0c\u56de\u5230\u982d\u90e8\n // \u7576 i \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n public void pushFirst(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\");\n return;\n }\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5de6\u79fb\u52d5\u4e00\u4f4d\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe front \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\u56de\u5230\u5c3e\u90e8\n front = index(front - 1);\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n public void pushLast(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\");\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n int rear = index(front + queSize);\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n public int popFirst() {\n int num = peekFirst();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n front = index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n public int popLast() {\n int num = peekLast();\n queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n public int peekFirst() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return nums[front];\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n public int peekLast() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n // \u8a08\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n public int[] toArray() {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i &lt; queSize; i++, j++) {\n res[i] = nums[index(j)];\n }\n return res;\n }\n}\n</code></pre> array_deque.cs<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nclass ArrayDeque {\n int[] nums; // \u7528\u65bc\u5132\u5b58\u96d9\u5411\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n int front; // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n int queSize; // \u96d9\u5411\u4f47\u5217\u9577\u5ea6\n\n /* \u5efa\u69cb\u5b50 */\n public ArrayDeque(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u5bb9\u91cf */\n int Capacity() {\n return nums.Length;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n public bool IsEmpty() {\n return queSize == 0;\n }\n\n /* \u8a08\u7b97\u74b0\u5f62\u9663\u5217\u7d22\u5f15 */\n int Index(int i) {\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe\u9663\u5217\u9996\u5c3e\u76f8\u9023\n // \u7576 i \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\uff0c\u56de\u5230\u982d\u90e8\n // \u7576 i \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + Capacity()) % Capacity();\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n public void PushFirst(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\");\n return;\n }\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5de6\u79fb\u52d5\u4e00\u4f4d\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe front \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\u56de\u5230\u5c3e\u90e8\n front = Index(front - 1);\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n public void PushLast(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\");\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n int rear = Index(front + queSize);\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n public int PopFirst() {\n int num = PeekFirst();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n front = Index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n public int PopLast() {\n int num = PeekLast();\n queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n public int PeekFirst() {\n if (IsEmpty()) {\n throw new InvalidOperationException();\n }\n return nums[front];\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n public int PeekLast() {\n if (IsEmpty()) {\n throw new InvalidOperationException();\n }\n // \u8a08\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = Index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n public int[] ToArray() {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i &lt; queSize; i++, j++) {\n res[i] = nums[Index(j)];\n }\n return res;\n }\n}\n</code></pre> array_deque.go<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\ntype arrayDeque struct {\n nums []int // \u7528\u65bc\u5132\u5b58\u96d9\u5411\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n front int // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n queSize int // \u96d9\u5411\u4f47\u5217\u9577\u5ea6\n queCapacity int // \u4f47\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7d0d\u5143\u7d20\u6578\u91cf\uff09\n}\n\n/* \u521d\u59cb\u5316\u4f47\u5217 */\nfunc newArrayDeque(queCapacity int) *arrayDeque {\n return &amp;arrayDeque{\n nums: make([]int, queCapacity),\n queCapacity: queCapacity,\n front: 0,\n queSize: 0,\n }\n}\n\n/* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\nfunc (q *arrayDeque) size() int {\n return q.queSize\n}\n\n/* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nfunc (q *arrayDeque) isEmpty() bool {\n return q.queSize == 0\n}\n\n/* \u8a08\u7b97\u74b0\u5f62\u9663\u5217\u7d22\u5f15 */\nfunc (q *arrayDeque) index(i int) int {\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe\u9663\u5217\u9996\u5c3e\u76f8\u9023\n // \u7576 i \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\uff0c\u56de\u5230\u982d\u90e8\n // \u7576 i \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + q.queCapacity) % q.queCapacity\n}\n\n/* \u4f47\u5217\u9996\u5165\u5217 */\nfunc (q *arrayDeque) pushFirst(num int) {\n if q.queSize == q.queCapacity {\n fmt.Println(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\")\n return\n }\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5de6\u79fb\u52d5\u4e00\u4f4d\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe front \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\u56de\u5230\u5c3e\u90e8\n q.front = q.index(q.front - 1)\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u9996\n q.nums[q.front] = num\n q.queSize++\n}\n\n/* \u4f47\u5217\u5c3e\u5165\u5217 */\nfunc (q *arrayDeque) pushLast(num int) {\n if q.queSize == q.queCapacity {\n fmt.Println(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\")\n return\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n rear := q.index(q.front + q.queSize)\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n q.nums[rear] = num\n q.queSize++\n}\n\n/* \u4f47\u5217\u9996\u51fa\u5217 */\nfunc (q *arrayDeque) popFirst() any {\n num := q.peekFirst()\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n q.front = q.index(q.front + 1)\n q.queSize--\n return num\n}\n\n/* \u4f47\u5217\u5c3e\u51fa\u5217 */\nfunc (q *arrayDeque) popLast() any {\n num := q.peekLast()\n q.queSize--\n return num\n}\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nfunc (q *arrayDeque) peekFirst() any {\n if q.isEmpty() {\n return nil\n }\n return q.nums[q.front]\n}\n\n/* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\nfunc (q *arrayDeque) peekLast() any {\n if q.isEmpty() {\n return nil\n }\n // \u8a08\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n last := q.index(q.front + q.queSize - 1)\n return q.nums[last]\n}\n\n/* \u7372\u53d6 Slice \u7528\u65bc\u5217\u5370 */\nfunc (q *arrayDeque) toSlice() []int {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n res := make([]int, q.queSize)\n for i, j := 0, q.front; i &lt; q.queSize; i++ {\n res[i] = q.nums[q.index(j)]\n j++\n }\n return res\n}\n</code></pre> array_deque.swift<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nclass ArrayDeque {\n private var nums: [Int] // \u7528\u65bc\u5132\u5b58\u96d9\u5411\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n private var front: Int // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n private var _size: Int // \u96d9\u5411\u4f47\u5217\u9577\u5ea6\n\n /* \u5efa\u69cb\u5b50 */\n init(capacity: Int) {\n nums = Array(repeating: 0, count: capacity)\n front = 0\n _size = 0\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u5bb9\u91cf */\n func capacity() -&gt; Int {\n nums.count\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n func size() -&gt; Int {\n _size\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n func isEmpty() -&gt; Bool {\n size() == 0\n }\n\n /* \u8a08\u7b97\u74b0\u5f62\u9663\u5217\u7d22\u5f15 */\n private func index(i: Int) -&gt; Int {\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe\u9663\u5217\u9996\u5c3e\u76f8\u9023\n // \u7576 i \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\uff0c\u56de\u5230\u982d\u90e8\n // \u7576 i \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\uff0c\u56de\u5230\u5c3e\u90e8\n (i + capacity()) % capacity()\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n func pushFirst(num: Int) {\n if size() == capacity() {\n print(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\")\n return\n }\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5de6\u79fb\u52d5\u4e00\u4f4d\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe front \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\u56de\u5230\u5c3e\u90e8\n front = index(i: front - 1)\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u9996\n nums[front] = num\n _size += 1\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n func pushLast(num: Int) {\n if size() == capacity() {\n print(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\")\n return\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n let rear = index(i: front + size())\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n nums[rear] = num\n _size += 1\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n func popFirst() -&gt; Int {\n let num = peekFirst()\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n front = index(i: front + 1)\n _size -= 1\n return num\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n func popLast() -&gt; Int {\n let num = peekLast()\n _size -= 1\n return num\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n func peekFirst() -&gt; Int {\n if isEmpty() {\n fatalError(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\")\n }\n return nums[front]\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n func peekLast() -&gt; Int {\n if isEmpty() {\n fatalError(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\")\n }\n // \u8a08\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n let last = index(i: front + size() - 1)\n return nums[last]\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n func toArray() -&gt; [Int] {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n (front ..&lt; front + size()).map { nums[index(i: $0)] }\n }\n}\n</code></pre> array_deque.js<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nclass ArrayDeque {\n #nums; // \u7528\u65bc\u5132\u5b58\u96d9\u5411\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n #front; // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n #queSize; // \u96d9\u5411\u4f47\u5217\u9577\u5ea6\n\n /* \u5efa\u69cb\u5b50 */\n constructor(capacity) {\n this.#nums = new Array(capacity);\n this.#front = 0;\n this.#queSize = 0;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u5bb9\u91cf */\n capacity() {\n return this.#nums.length;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n size() {\n return this.#queSize;\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u8a08\u7b97\u74b0\u5f62\u9663\u5217\u7d22\u5f15 */\n index(i) {\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe\u9663\u5217\u9996\u5c3e\u76f8\u9023\n // \u7576 i \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\uff0c\u56de\u5230\u982d\u90e8\n // \u7576 i \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + this.capacity()) % this.capacity();\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n pushFirst(num) {\n if (this.#queSize === this.capacity()) {\n console.log('\u96d9\u5411\u4f47\u5217\u5df2\u6eff');\n return;\n }\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5de6\u79fb\u52d5\u4e00\u4f4d\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe front \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\u56de\u5230\u5c3e\u90e8\n this.#front = this.index(this.#front - 1);\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u9996\n this.#nums[this.#front] = num;\n this.#queSize++;\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n pushLast(num) {\n if (this.#queSize === this.capacity()) {\n console.log('\u96d9\u5411\u4f47\u5217\u5df2\u6eff');\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n const rear = this.index(this.#front + this.#queSize);\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n this.#nums[rear] = num;\n this.#queSize++;\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n popFirst() {\n const num = this.peekFirst();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n this.#front = this.index(this.#front + 1);\n this.#queSize--;\n return num;\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n popLast() {\n const num = this.peekLast();\n this.#queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n peekFirst() {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n return this.#nums[this.#front];\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n peekLast() {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n // \u8a08\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n const last = this.index(this.#front + this.#queSize - 1);\n return this.#nums[last];\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n toArray() {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n const res = [];\n for (let i = 0, j = this.#front; i &lt; this.#queSize; i++, j++) {\n res[i] = this.#nums[this.index(j)];\n }\n return res;\n }\n}\n</code></pre> array_deque.ts<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nclass ArrayDeque {\n private nums: number[]; // \u7528\u65bc\u5132\u5b58\u96d9\u5411\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n private front: number; // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n private queSize: number; // \u96d9\u5411\u4f47\u5217\u9577\u5ea6\n\n /* \u5efa\u69cb\u5b50 */\n constructor(capacity: number) {\n this.nums = new Array(capacity);\n this.front = 0;\n this.queSize = 0;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u5bb9\u91cf */\n capacity(): number {\n return this.nums.length;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u8a08\u7b97\u74b0\u5f62\u9663\u5217\u7d22\u5f15 */\n index(i: number): number {\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe\u9663\u5217\u9996\u5c3e\u76f8\u9023\n // \u7576 i \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\uff0c\u56de\u5230\u982d\u90e8\n // \u7576 i \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + this.capacity()) % this.capacity();\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n pushFirst(num: number): void {\n if (this.queSize === this.capacity()) {\n console.log('\u96d9\u5411\u4f47\u5217\u5df2\u6eff');\n return;\n }\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5de6\u79fb\u52d5\u4e00\u4f4d\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe front \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\u56de\u5230\u5c3e\u90e8\n this.front = this.index(this.front - 1);\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u9996\n this.nums[this.front] = num;\n this.queSize++;\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n pushLast(num: number): void {\n if (this.queSize === this.capacity()) {\n console.log('\u96d9\u5411\u4f47\u5217\u5df2\u6eff');\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n const rear: number = this.index(this.front + this.queSize);\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n this.nums[rear] = num;\n this.queSize++;\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n popFirst(): number {\n const num: number = this.peekFirst();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n this.front = this.index(this.front + 1);\n this.queSize--;\n return num;\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n popLast(): number {\n const num: number = this.peekLast();\n this.queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n peekFirst(): number {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n return this.nums[this.front];\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n peekLast(): number {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n // \u8a08\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n const last = this.index(this.front + this.queSize - 1);\n return this.nums[last];\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n toArray(): number[] {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n const res: number[] = [];\n for (let i = 0, j = this.front; i &lt; this.queSize; i++, j++) {\n res[i] = this.nums[this.index(j)];\n }\n return res;\n }\n}\n</code></pre> array_deque.dart<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nclass ArrayDeque {\n late List&lt;int&gt; _nums; // \u7528\u65bc\u5132\u5b58\u96d9\u5411\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n late int _front; // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n late int _queSize; // \u96d9\u5411\u4f47\u5217\u9577\u5ea6\n\n /* \u5efa\u69cb\u5b50 */\n ArrayDeque(int capacity) {\n this._nums = List.filled(capacity, 0);\n this._front = this._queSize = 0;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return _nums.length;\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u8a08\u7b97\u74b0\u5f62\u9663\u5217\u7d22\u5f15 */\n int index(int i) {\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe\u9663\u5217\u9996\u5c3e\u76f8\u9023\n // \u7576 i \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\uff0c\u56de\u5230\u982d\u90e8\n // \u7576 i \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n void pushFirst(int _num) {\n if (_queSize == capacity()) {\n throw Exception(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\");\n }\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5de6\u79fb\u52d5\u4e00\u4f4d\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe _front \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\u56de\u5230\u5c3e\u90e8\n _front = index(_front - 1);\n // \u5c07 _num \u65b0\u589e\u81f3\u4f47\u5217\u9996\n _nums[_front] = _num;\n _queSize++;\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n void pushLast(int _num) {\n if (_queSize == capacity()) {\n throw Exception(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\");\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n int rear = index(_front + _queSize);\n // \u5c07 _num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n _nums[rear] = _num;\n _queSize++;\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n int popFirst() {\n int _num = peekFirst();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u53f3\u79fb\u52d5\u4e00\u4f4d\n _front = index(_front + 1);\n _queSize--;\n return _num;\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n int popLast() {\n int _num = peekLast();\n _queSize--;\n return _num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty()) {\n throw Exception(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\");\n }\n return _nums[_front];\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty()) {\n throw Exception(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\");\n }\n // \u8a08\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(_front + _queSize - 1);\n return _nums[last];\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n List&lt;int&gt; toArray() {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n List&lt;int&gt; res = List.filled(_queSize, 0);\n for (int i = 0, j = _front; i &lt; _queSize; i++, j++) {\n res[i] = _nums[index(j)];\n }\n return res;\n }\n}\n</code></pre> array_deque.rs<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\nstruct ArrayDeque {\n nums: Vec&lt;i32&gt;, // \u7528\u65bc\u5132\u5b58\u96d9\u5411\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n front: usize, // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n que_size: usize, // \u96d9\u5411\u4f47\u5217\u9577\u5ea6\n}\n\nimpl ArrayDeque {\n /* \u5efa\u69cb\u5b50 */\n pub fn new(capacity: usize) -&gt; Self {\n Self {\n nums: vec![0; capacity],\n front: 0,\n que_size: 0,\n }\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u5bb9\u91cf */\n pub fn capacity(&amp;self) -&gt; usize {\n self.nums.len()\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n pub fn size(&amp;self) -&gt; usize {\n self.que_size\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n pub fn is_empty(&amp;self) -&gt; bool {\n self.que_size == 0\n }\n\n /* \u8a08\u7b97\u74b0\u5f62\u9663\u5217\u7d22\u5f15 */\n fn index(&amp;self, i: i32) -&gt; usize {\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe\u9663\u5217\u9996\u5c3e\u76f8\u9023\n // \u7576 i \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\uff0c\u56de\u5230\u982d\u90e8\n // \u7576 i \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\uff0c\u56de\u5230\u5c3e\u90e8\n return ((i + self.capacity() as i32) % self.capacity() as i32) as usize;\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n pub fn push_first(&amp;mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\");\n return;\n }\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5de6\u79fb\u52d5\u4e00\u4f4d\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe front \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\u56de\u5230\u5c3e\u90e8\n self.front = self.index(self.front as i32 - 1);\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u9996\n self.nums[self.front] = num;\n self.que_size += 1;\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n pub fn push_last(&amp;mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\");\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n let rear = self.index(self.front as i32 + self.que_size as i32);\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n self.nums[rear] = num;\n self.que_size += 1;\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n fn pop_first(&amp;mut self) -&gt; i32 {\n let num = self.peek_first();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n self.front = self.index(self.front as i32 + 1);\n self.que_size -= 1;\n num\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n fn pop_last(&amp;mut self) -&gt; i32 {\n let num = self.peek_last();\n self.que_size -= 1;\n num\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n fn peek_first(&amp;self) -&gt; i32 {\n if self.is_empty() {\n panic!(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\")\n };\n self.nums[self.front]\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n fn peek_last(&amp;self) -&gt; i32 {\n if self.is_empty() {\n panic!(\"\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\")\n };\n // \u8a08\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n let last = self.index(self.front as i32 + self.que_size as i32 - 1);\n self.nums[last]\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n fn to_array(&amp;self) -&gt; Vec&lt;i32&gt; {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n let mut res = vec![0; self.que_size];\n let mut j = self.front;\n for i in 0..self.que_size {\n res[i] = self.nums[self.index(j as i32)];\n j += 1;\n }\n res\n }\n}\n</code></pre> array_deque.c<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 */\ntypedef struct {\n int *nums; // \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n int front; // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n int queSize; // \u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e + 1\n int queCapacity; // \u4f47\u5217\u5bb9\u91cf\n} ArrayDeque;\n\n/* \u5efa\u69cb\u5b50 */\nArrayDeque *newArrayDeque(int capacity) {\n ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));\n // \u521d\u59cb\u5316\u9663\u5217\n deque-&gt;queCapacity = capacity;\n deque-&gt;nums = (int *)malloc(sizeof(int) * deque-&gt;queCapacity);\n deque-&gt;front = deque-&gt;queSize = 0;\n return deque;\n}\n\n/* \u6790\u69cb\u51fd\u5f0f */\nvoid delArrayDeque(ArrayDeque *deque) {\n free(deque-&gt;nums);\n free(deque);\n}\n\n/* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u5bb9\u91cf */\nint capacity(ArrayDeque *deque) {\n return deque-&gt;queCapacity;\n}\n\n/* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\nint size(ArrayDeque *deque) {\n return deque-&gt;queSize;\n}\n\n/* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nbool empty(ArrayDeque *deque) {\n return deque-&gt;queSize == 0;\n}\n\n/* \u8a08\u7b97\u74b0\u5f62\u9663\u5217\u7d22\u5f15 */\nint dequeIndex(ArrayDeque *deque, int i) {\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe\u9663\u5217\u9996\u5c3e\u76f8\u9023\n // \u7576 i \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u56de\u5230\u982d\u90e8\n // \u7576 i \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\uff0c\u56de\u5230\u5c3e\u90e8\n return ((i + capacity(deque)) % capacity(deque));\n}\n\n/* \u4f47\u5217\u9996\u5165\u5217 */\nvoid pushFirst(ArrayDeque *deque, int num) {\n if (deque-&gt;queSize == capacity(deque)) {\n printf(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\\r\\n\");\n return;\n }\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5de6\u79fb\u52d5\u4e00\u4f4d\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe front \u8d8a\u904e\u9663\u5217\u982d\u90e8\u56de\u5230\u5c3e\u90e8\n deque-&gt;front = dequeIndex(deque, deque-&gt;front - 1);\n // \u5c07 num \u65b0\u589e\u5230\u4f47\u5217\u9996\n deque-&gt;nums[deque-&gt;front] = num;\n deque-&gt;queSize++;\n}\n\n/* \u4f47\u5217\u5c3e\u5165\u5217 */\nvoid pushLast(ArrayDeque *deque, int num) {\n if (deque-&gt;queSize == capacity(deque)) {\n printf(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\\r\\n\");\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n int rear = dequeIndex(deque, deque-&gt;front + deque-&gt;queSize);\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n deque-&gt;nums[rear] = num;\n deque-&gt;queSize++;\n}\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nint peekFirst(ArrayDeque *deque) {\n // \u8a2a\u554f\u7570\u5e38\uff1a\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\n assert(empty(deque) == 0);\n return deque-&gt;nums[deque-&gt;front];\n}\n\n/* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\nint peekLast(ArrayDeque *deque) {\n // \u8a2a\u554f\u7570\u5e38\uff1a\u96d9\u5411\u4f47\u5217\u70ba\u7a7a\n assert(empty(deque) == 0);\n int last = dequeIndex(deque, deque-&gt;front + deque-&gt;queSize - 1);\n return deque-&gt;nums[last];\n}\n\n/* \u4f47\u5217\u9996\u51fa\u5217 */\nint popFirst(ArrayDeque *deque) {\n int num = peekFirst(deque);\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n deque-&gt;front = dequeIndex(deque, deque-&gt;front + 1);\n deque-&gt;queSize--;\n return num;\n}\n\n/* \u4f47\u5217\u5c3e\u51fa\u5217 */\nint popLast(ArrayDeque *deque) {\n int num = peekLast(deque);\n deque-&gt;queSize--;\n return num;\n}\n</code></pre> array_deque.kt<pre><code>/* \u5efa\u69cb\u5b50 */\nclass ArrayDeque(capacity: Int) {\n private var nums: IntArray = IntArray(capacity) // \u7528\u65bc\u5132\u5b58\u96d9\u5411\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n private var front: Int = 0 // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n private var queSize: Int = 0 // \u96d9\u5411\u4f47\u5217\u9577\u5ea6\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u5bb9\u91cf */\n fun capacity(): Int {\n return nums.size\n }\n\n /* \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 */\n fun size(): Int {\n return queSize\n }\n\n /* \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n fun isEmpty(): Boolean {\n return queSize == 0\n }\n\n /* \u8a08\u7b97\u74b0\u5f62\u9663\u5217\u7d22\u5f15 */\n private fun index(i: Int): Int {\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe\u9663\u5217\u9996\u5c3e\u76f8\u9023\n // \u7576 i \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\uff0c\u56de\u5230\u982d\u90e8\n // \u7576 i \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity()\n }\n\n /* \u4f47\u5217\u9996\u5165\u5217 */\n fun pushFirst(num: Int) {\n if (queSize == capacity()) {\n println(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\")\n return\n }\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5de6\u79fb\u52d5\u4e00\u4f4d\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe front \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\u56de\u5230\u5c3e\u90e8\n front = index(front - 1)\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u9996\n nums[front] = num\n queSize++\n }\n\n /* \u4f47\u5217\u5c3e\u5165\u5217 */\n fun pushLast(num: Int) {\n if (queSize == capacity()) {\n println(\"\u96d9\u5411\u4f47\u5217\u5df2\u6eff\")\n return\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n val rear = index(front + queSize)\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n nums[rear] = num\n queSize++\n }\n\n /* \u4f47\u5217\u9996\u51fa\u5217 */\n fun popFirst(): Int {\n val num = peekFirst()\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n front = index(front + 1)\n queSize--\n return num\n }\n\n /* \u4f47\u5217\u5c3e\u51fa\u5217 */\n fun popLast(): Int {\n val num = peekLast()\n queSize--\n return num\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n fun peekFirst(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return nums[front]\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 */\n fun peekLast(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n // \u8a08\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n val last = index(front + queSize - 1)\n return nums[last]\n }\n\n /* \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 */\n fun toArray(): IntArray {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n val res = IntArray(queSize)\n var i = 0\n var j = front\n while (i &lt; queSize) {\n res[i] = nums[index(j)]\n i++\n j++\n }\n return res\n }\n}\n</code></pre> array_deque.rb<pre><code>### \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u96d9\u5411\u4f47\u5217 ###\nclass ArrayDeque\n ### \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u9577\u5ea6 ###\n attr_reader :size\n\n ### \u5efa\u69cb\u5b50 ###\n def initialize(capacity)\n @nums = Array.new(capacity, 0)\n @front = 0\n @size = 0\n end\n\n ### \u7372\u53d6\u96d9\u5411\u4f47\u5217\u7684\u5bb9\u91cf ###\n def capacity\n @nums.length\n end\n\n ### \u5224\u65b7\u96d9\u5411\u4f47\u5217\u662f\u5426\u70ba\u7a7a ###\n def is_empty?\n size.zero?\n end\n\n ### \u4f47\u5217\u9996\u5165\u5217 ###\n def push_first(num)\n if size == capacity\n puts '\u96d9\u5411\u4f47\u5217\u5df2\u6eff'\n return\n end\n\n # \u4f47\u5217\u9996\u6307\u6a19\u5411\u5de6\u79fb\u52d5\u4e00\u4f4d\n # \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe front \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\u56de\u5230\u5c3e\u90e8\n @front = index(@front - 1)\n # \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u9996\n @nums[@front] = num\n @size += 1\n end\n\n ### \u4f47\u5217\u5c3e\u5165\u5217 ###\n def push_last(num)\n if size == capacity\n puts '\u96d9\u5411\u4f47\u5217\u5df2\u6eff'\n return\n end\n\n # \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n rear = index(@front + size)\n # \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n @nums[rear] = num\n @size += 1\n end\n\n ### \u4f47\u5217\u9996\u51fa\u5217 ###\n def pop_first\n num = peek_first\n # \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\n @front = index(@front + 1)\n @size -= 1\n num\n end\n\n ### \u4f47\u5217\u5c3e\u51fa\u5217 ###\n def pop_last\n num = peek_last\n @size -= 1\n num\n end\n\n ### \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 ###\n def peek_first\n raise IndexError, '\u96d9\u5411\u4f47\u5217\u70ba\u7a7a' if is_empty?\n\n @nums[@front]\n end\n\n ### \u8a2a\u554f\u4f47\u5217\u5c3e\u5143\u7d20 ###\n def peek_last\n raise IndexError, '\u96d9\u5411\u4f47\u5217\u70ba\u7a7a' if is_empty?\n\n # \u8a08\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n last = index(@front + size - 1)\n @nums[last]\n end\n\n ### \u8fd4\u56de\u9663\u5217\u7528\u65bc\u5217\u5370 ###\n def to_array\n # \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n res = []\n for i in 0...size\n res &lt;&lt; @nums[index(@front + i)]\n end\n res\n end\n\n private\n\n ### \u8a08\u7b97\u74b0\u5f62\u9663\u5217\u7d22\u5f15 ###\n def index(i)\n # \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe\u9663\u5217\u9996\u5c3e\u76f8\u9023\n # \u7576 i \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\uff0c\u56de\u5230\u982d\u90e8\n # \u7576 i \u8d8a\u904e\u9663\u5217\u982d\u90e8\u5f8c\uff0c\u56de\u5230\u5c3e\u90e8\n (i + capacity) % capacity\n end\nend\n</code></pre> array_deque.zig<pre><code>[class]{ArrayDeque}-[func]{}\n</code></pre>"},{"location":"chapter_stack_and_queue/deque/#533","title":"5.3.3 \u00a0 \u96d9\u5411\u4f47\u5217\u61c9\u7528","text":"<p>\u96d9\u5411\u4f47\u5217\u517c\u5177\u5806\u758a\u8207\u4f47\u5217\u7684\u908f\u8f2f\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5be6\u73fe\u9019\u5169\u8005\u7684\u6240\u6709\u61c9\u7528\u5834\u666f\uff0c\u540c\u6642\u63d0\u4f9b\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\u3002</p> <p>\u6211\u5011\u77e5\u9053\uff0c\u8edf\u9ad4\u7684\u201c\u64a4\u92b7\u201d\u529f\u80fd\u901a\u5e38\u4f7f\u7528\u5806\u758a\u4f86\u5be6\u73fe\uff1a\u7cfb\u7d71\u5c07\u6bcf\u6b21\u66f4\u6539\u64cd\u4f5c <code>push</code> \u5230\u5806\u758a\u4e2d\uff0c\u7136\u5f8c\u900f\u904e <code>pop</code> \u5be6\u73fe\u64a4\u92b7\u3002\u7136\u800c\uff0c\u8003\u616e\u5230\u7cfb\u7d71\u8cc7\u6e90\u7684\u9650\u5236\uff0c\u8edf\u9ad4\u901a\u5e38\u6703\u9650\u5236\u64a4\u92b7\u7684\u6b65\u6578\uff08\u4f8b\u5982\u50c5\u5141\u8a31\u5132\u5b58 \\(50\\) \u6b65\uff09\u3002\u7576\u5806\u758a\u7684\u9577\u5ea6\u8d85\u904e \\(50\\) \u6642\uff0c\u8edf\u9ad4\u9700\u8981\u5728\u5806\u758a\u5e95\uff08\u4f47\u5217\u9996\uff09\u57f7\u884c\u522a\u9664\u64cd\u4f5c\u3002\u4f46\u5806\u758a\u7121\u6cd5\u5be6\u73fe\u8a72\u529f\u80fd\uff0c\u6b64\u6642\u5c31\u9700\u8981\u4f7f\u7528\u96d9\u5411\u4f47\u5217\u4f86\u66ff\u4ee3\u5806\u758a\u3002\u8acb\u6ce8\u610f\uff0c\u201c\u64a4\u92b7\u201d\u7684\u6838\u5fc3\u908f\u8f2f\u4ecd\u7136\u9075\u5faa\u5806\u758a\u7684\u5148\u5165\u5f8c\u51fa\u539f\u5247\uff0c\u53ea\u662f\u96d9\u5411\u4f47\u5217\u80fd\u5920\u66f4\u52a0\u9748\u6d3b\u5730\u5be6\u73fe\u4e00\u4e9b\u984d\u5916\u908f\u8f2f\u3002</p>"},{"location":"chapter_stack_and_queue/queue/","title":"5.2 \u00a0 \u4f47\u5217","text":"<p>\u4f47\u5217\uff08queue\uff09\u662f\u4e00\u7a2e\u9075\u5faa\u5148\u5165\u5148\u51fa\u898f\u5247\u7684\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\u3002\u9867\u540d\u601d\u7fa9\uff0c\u4f47\u5217\u6a21\u64ec\u4e86\u6392\u968a\u73fe\u8c61\uff0c\u5373\u65b0\u4f86\u7684\u4eba\u4e0d\u65b7\u52a0\u5165\u4f47\u5217\u5c3e\u90e8\uff0c\u800c\u4f4d\u65bc\u4f47\u5217\u982d\u90e8\u7684\u4eba\u9010\u500b\u96e2\u958b\u3002</p> <p>\u5982\u5716 5-4 \u6240\u793a\uff0c\u6211\u5011\u5c07\u4f47\u5217\u982d\u90e8\u7a31\u70ba\u201c\u4f47\u5217\u9996\u201d\uff0c\u5c3e\u90e8\u7a31\u70ba\u201c\u4f47\u5217\u5c3e\u201d\uff0c\u5c07\u628a\u5143\u7d20\u52a0\u5165\u5217\u5c3e\u7684\u64cd\u4f5c\u7a31\u70ba\u201c\u5165\u5217\u201d\uff0c\u522a\u9664\u4f47\u5217\u9996\u5143\u7d20\u7684\u64cd\u4f5c\u7a31\u70ba\u201c\u51fa\u5217\u201d\u3002</p> <p></p> <p> \u5716 5-4 \u00a0 \u4f47\u5217\u7684\u5148\u5165\u5148\u51fa\u898f\u5247 </p>"},{"location":"chapter_stack_and_queue/queue/#521","title":"5.2.1 \u00a0 \u4f47\u5217\u5e38\u7528\u64cd\u4f5c","text":"<p>\u4f47\u5217\u7684\u5e38\u898b\u64cd\u4f5c\u5982\u8868 5-2 \u6240\u793a\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7a0b\u5f0f\u8a9e\u8a00\u7684\u65b9\u6cd5\u540d\u7a31\u53ef\u80fd\u6703\u6709\u6240\u4e0d\u540c\u3002\u6211\u5011\u5728\u6b64\u63a1\u7528\u8207\u5806\u758a\u76f8\u540c\u7684\u65b9\u6cd5\u547d\u540d\u3002</p> <p> \u8868 5-2 \u00a0 \u4f47\u5217\u64cd\u4f5c\u6548\u7387 </p> \u65b9\u6cd5\u540d \u63cf\u8ff0 \u6642\u9593\u8907\u96dc\u5ea6 <code>push()</code> \u5143\u7d20\u5165\u5217\uff0c\u5373\u5c07\u5143\u7d20\u65b0\u589e\u81f3\u4f47\u5217\u5c3e \\(O(1)\\) <code>pop()</code> \u4f47\u5217\u9996\u5143\u7d20\u51fa\u5217 \\(O(1)\\) <code>peek()</code> \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 \\(O(1)\\) <p>\u6211\u5011\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7a0b\u5f0f\u8a9e\u8a00\u4e2d\u73fe\u6210\u7684\u4f47\u5217\u985e\u5225\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig queue.py<pre><code>from collections import deque\n\n# \u521d\u59cb\u5316\u4f47\u5217\n# \u5728 Python \u4e2d\uff0c\u6211\u5011\u4e00\u822c\u5c07\u96d9\u5411\u4f47\u5217\u985e\u5225 deque \u7576\u4f5c\u4f47\u5217\u4f7f\u7528\n# \u96d6\u7136 queue.Queue() \u662f\u7d14\u6b63\u7684\u4f47\u5217\u985e\u5225\uff0c\u4f46\u4e0d\u592a\u597d\u7528\uff0c\u56e0\u6b64\u4e0d\u63a8\u85a6\nque: deque[int] = deque()\n\n# \u5143\u7d20\u5165\u5217\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n\n# \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20\nfront: int = que[0]\n\n# \u5143\u7d20\u51fa\u5217\npop: int = que.popleft()\n\n# \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6\nsize: int = len(que)\n\n# \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a\nis_empty: bool = len(que) == 0\n</code></pre> queue.cpp<pre><code>/* \u521d\u59cb\u5316\u4f47\u5217 */\nqueue&lt;int&gt; queue;\n\n/* \u5143\u7d20\u5165\u5217 */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nint front = queue.front();\n\n/* \u5143\u7d20\u51fa\u5217 */\nqueue.pop();\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nint size = queue.size();\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nbool empty = queue.empty();\n</code></pre> queue.java<pre><code>/* \u521d\u59cb\u5316\u4f47\u5217 */\nQueue&lt;Integer&gt; queue = new LinkedList&lt;&gt;();\n\n/* \u5143\u7d20\u5165\u5217 */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nint peek = queue.peek();\n\n/* \u5143\u7d20\u51fa\u5217 */\nint pop = queue.poll();\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nint size = queue.size();\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nboolean isEmpty = queue.isEmpty();\n</code></pre> queue.cs<pre><code>/* \u521d\u59cb\u5316\u4f47\u5217 */\nQueue&lt;int&gt; queue = new();\n\n/* \u5143\u7d20\u5165\u5217 */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nint peek = queue.Peek();\n\n/* \u5143\u7d20\u51fa\u5217 */\nint pop = queue.Dequeue();\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nint size = queue.Count;\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nbool isEmpty = queue.Count == 0;\n</code></pre> queue_test.go<pre><code>/* \u521d\u59cb\u5316\u4f47\u5217 */\n// \u5728 Go \u4e2d\uff0c\u5c07 list \u4f5c\u70ba\u4f47\u5217\u4f86\u4f7f\u7528\nqueue := list.New()\n\n/* \u5143\u7d20\u5165\u5217 */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\npeek := queue.Front()\n\n/* \u5143\u7d20\u51fa\u5217 */\npop := queue.Front()\nqueue.Remove(pop)\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nsize := queue.Len()\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nisEmpty := queue.Len() == 0\n</code></pre> queue.swift<pre><code>/* \u521d\u59cb\u5316\u4f47\u5217 */\n// Swift \u6c92\u6709\u5167\u5efa\u7684\u4f47\u5217\u985e\u5225\uff0c\u53ef\u4ee5\u628a Array \u7576\u4f5c\u4f47\u5217\u4f86\u4f7f\u7528\nvar queue: [Int] = []\n\n/* \u5143\u7d20\u5165\u5217 */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nlet peek = queue.first!\n\n/* \u5143\u7d20\u51fa\u5217 */\n// \u7531\u65bc\u662f\u9663\u5217\uff0c\u56e0\u6b64 removeFirst \u7684\u8907\u96dc\u5ea6\u70ba O(n)\nlet pool = queue.removeFirst()\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nlet size = queue.count\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nlet isEmpty = queue.isEmpty\n</code></pre> queue.js<pre><code>/* \u521d\u59cb\u5316\u4f47\u5217 */\n// JavaScript \u6c92\u6709\u5167\u5efa\u7684\u4f47\u5217\uff0c\u53ef\u4ee5\u628a Array \u7576\u4f5c\u4f47\u5217\u4f86\u4f7f\u7528\nconst queue = [];\n\n/* \u5143\u7d20\u5165\u5217 */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nconst peek = queue[0];\n\n/* \u5143\u7d20\u51fa\u5217 */\n// \u5e95\u5c64\u662f\u9663\u5217\uff0c\u56e0\u6b64 shift() \u65b9\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\nconst pop = queue.shift();\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nconst size = queue.length;\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nconst empty = queue.length === 0;\n</code></pre> queue.ts<pre><code>/* \u521d\u59cb\u5316\u4f47\u5217 */\n// TypeScript \u6c92\u6709\u5167\u5efa\u7684\u4f47\u5217\uff0c\u53ef\u4ee5\u628a Array \u7576\u4f5c\u4f47\u5217\u4f86\u4f7f\u7528\nconst queue: number[] = [];\n\n/* \u5143\u7d20\u5165\u5217 */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nconst peek = queue[0];\n\n/* \u5143\u7d20\u51fa\u5217 */\n// \u5e95\u5c64\u662f\u9663\u5217\uff0c\u56e0\u6b64 shift() \u65b9\u6cd5\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\nconst pop = queue.shift();\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nconst size = queue.length;\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nconst empty = queue.length === 0;\n</code></pre> queue.dart<pre><code>/* \u521d\u59cb\u5316\u4f47\u5217 */\n// \u5728 Dart \u4e2d\uff0c\u4f47\u5217\u985e\u5225 Qeque \u662f\u96d9\u5411\u4f47\u5217\uff0c\u4e5f\u53ef\u4f5c\u70ba\u4f47\u5217\u4f7f\u7528\nQueue&lt;int&gt; queue = Queue();\n\n/* \u5143\u7d20\u5165\u5217 */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nint peek = queue.first;\n\n/* \u5143\u7d20\u51fa\u5217 */\nint pop = queue.removeFirst();\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nint size = queue.length;\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nbool isEmpty = queue.isEmpty;\n</code></pre> queue.rs<pre><code>/* \u521d\u59cb\u5316\u96d9\u5411\u4f47\u5217 */\n// \u5728 Rust \u4e2d\u4f7f\u7528\u96d9\u5411\u4f47\u5217\u4f5c\u70ba\u666e\u901a\u4f47\u5217\u4f86\u4f7f\u7528\nlet mut deque: VecDeque&lt;u32&gt; = VecDeque::new();\n\n/* \u5143\u7d20\u5165\u5217 */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nif let Some(front) = deque.front() {\n}\n\n/* \u5143\u7d20\u51fa\u5217 */\nif let Some(pop) = deque.pop_front() {\n}\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nlet size = deque.len();\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nlet is_empty = deque.is_empty();\n</code></pre> queue.c<pre><code>// C \u672a\u63d0\u4f9b\u5167\u5efa\u4f47\u5217\n</code></pre> queue.kt<pre><code>/* \u521d\u59cb\u5316\u4f47\u5217 */\nval queue = LinkedList&lt;Int&gt;()\n\n/* \u5143\u7d20\u5165\u5217 */\nqueue.offer(1)\nqueue.offer(3)\nqueue.offer(2)\nqueue.offer(5)\nqueue.offer(4)\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nval peek = queue.peek()\n\n/* \u5143\u7d20\u51fa\u5217 */\nval pop = queue.poll()\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nval size = queue.size\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nval isEmpty = queue.isEmpty()\n</code></pre> queue.rb<pre><code># \u521d\u59cb\u5316\u4f47\u5217\n# Ruby \u5167\u5efa\u7684\u4f47\u5217\uff08Thread::Queue) \u6c92\u6709 peek \u548c\u8d70\u8a2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u628a Array \u7576\u4f5c\u4f47\u5217\u4f86\u4f7f\u7528\nqueue = []\n\n# \u5143\u7d20\u5165\u5217\nqueue.push(1)\nqueue.push(3)\nqueue.push(2)\nqueue.push(5)\nqueue.push(4)\n\n# \u8a2a\u554f\u4f47\u5217\u5143\u7d20\npeek = queue.first\n\n# \u5143\u7d20\u51fa\u5217\n# \u6e05\u6ce8\u610f\uff0c\u7531\u65bc\u662f\u9663\u5217\uff0cArray#shift \u65b9\u6cd5\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\npop = queue.shift\n\n# \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6\nsize = queue.length\n\n# \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a\nis_empty = queue.empty?\n</code></pre> queue.zig<pre><code>\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_stack_and_queue/queue/#522","title":"5.2.2 \u00a0 \u4f47\u5217\u5be6\u73fe","text":"<p>\u70ba\u4e86\u5be6\u73fe\u4f47\u5217\uff0c\u6211\u5011\u9700\u8981\u4e00\u7a2e\u8cc7\u6599\u7d50\u69cb\uff0c\u53ef\u4ee5\u5728\u4e00\u7aef\u65b0\u589e\u5143\u7d20\uff0c\u4e26\u5728\u53e6\u4e00\u7aef\u522a\u9664\u5143\u7d20\uff0c\u93c8\u7d50\u4e32\u5217\u548c\u9663\u5217\u90fd\u7b26\u5408\u8981\u6c42\u3002</p>"},{"location":"chapter_stack_and_queue/queue/#1","title":"1. \u00a0 \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u7684\u5be6\u73fe","text":"<p>\u5982\u5716 5-5 \u6240\u793a\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u93c8\u7d50\u4e32\u5217\u7684\u201c\u982d\u7bc0\u9ede\u201d\u548c\u201c\u5c3e\u7bc0\u9ede\u201d\u5206\u5225\u8996\u70ba\u201c\u4f47\u5217\u9996\u201d\u548c\u201c\u4f47\u5217\u5c3e\u201d\uff0c\u898f\u5b9a\u4f47\u5217\u5c3e\u50c5\u53ef\u65b0\u589e\u7bc0\u9ede\uff0c\u4f47\u5217\u9996\u50c5\u53ef\u522a\u9664\u7bc0\u9ede\u3002</p> LinkedListQueuepush()pop() <p></p> <p></p> <p></p> <p> \u5716 5-5 \u00a0 \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u4f47\u5217\u7684\u5165\u5217\u51fa\u5217\u64cd\u4f5c </p> <p>\u4ee5\u4e0b\u662f\u7528\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u4f47\u5217\u7684\u7a0b\u5f0f\u78bc\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linkedlist_queue.py<pre><code>class LinkedListQueue:\n \"\"\"\u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u4f47\u5217\"\"\"\n\n def __init__(self):\n \"\"\"\u5efa\u69cb\u5b50\"\"\"\n self._front: ListNode | None = None # \u982d\u7bc0\u9ede front\n self._rear: ListNode | None = None # \u5c3e\u7bc0\u9ede rear\n self._size: int = 0\n\n def size(self) -&gt; int:\n \"\"\"\u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -&gt; bool:\n \"\"\"\u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a\"\"\"\n return self._size == 0\n\n def push(self, num: int):\n \"\"\"\u5165\u5217\"\"\"\n # \u5728\u5c3e\u7bc0\u9ede\u5f8c\u65b0\u589e num\n node = ListNode(num)\n # \u5982\u679c\u4f47\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4\u982d\u3001\u5c3e\u7bc0\u9ede\u90fd\u6307\u5411\u8a72\u7bc0\u9ede\n if self._front is None:\n self._front = node\n self._rear = node\n # \u5982\u679c\u4f47\u5217\u4e0d\u70ba\u7a7a\uff0c\u5247\u5c07\u8a72\u7bc0\u9ede\u65b0\u589e\u5230\u5c3e\u7bc0\u9ede\u5f8c\n else:\n self._rear.next = node\n self._rear = node\n self._size += 1\n\n def pop(self) -&gt; int:\n \"\"\"\u51fa\u5217\"\"\"\n num = self.peek()\n # \u522a\u9664\u982d\u7bc0\u9ede\n self._front = self._front.next\n self._size -= 1\n return num\n\n def peek(self) -&gt; int:\n \"\"\"\u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u4f47\u5217\u70ba\u7a7a\")\n return self._front.val\n\n def to_list(self) -&gt; list[int]:\n \"\"\"\u8f49\u5316\u70ba\u4e32\u5217\u7528\u65bc\u5217\u5370\"\"\"\n queue = []\n temp = self._front\n while temp:\n queue.append(temp.val)\n temp = temp.next\n return queue\n</code></pre> linkedlist_queue.cpp<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass LinkedListQueue {\n private:\n ListNode *front, *rear; // \u982d\u7bc0\u9ede front \uff0c\u5c3e\u7bc0\u9ede rear\n int queSize;\n\n public:\n LinkedListQueue() {\n front = nullptr;\n rear = nullptr;\n queSize = 0;\n }\n\n ~LinkedListQueue() {\n // \u8d70\u8a2a\u93c8\u7d50\u4e32\u5217\u522a\u9664\u7bc0\u9ede\uff0c\u91cb\u653e\u8a18\u61b6\u9ad4\n freeMemoryLinkedList(front);\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n bool isEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u5217 */\n void push(int num) {\n // \u5728\u5c3e\u7bc0\u9ede\u5f8c\u65b0\u589e num\n ListNode *node = new ListNode(num);\n // \u5982\u679c\u4f47\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4\u982d\u3001\u5c3e\u7bc0\u9ede\u90fd\u6307\u5411\u8a72\u7bc0\u9ede\n if (front == nullptr) {\n front = node;\n rear = node;\n }\n // \u5982\u679c\u4f47\u5217\u4e0d\u70ba\u7a7a\uff0c\u5247\u5c07\u8a72\u7bc0\u9ede\u65b0\u589e\u5230\u5c3e\u7bc0\u9ede\u5f8c\n else {\n rear-&gt;next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u5217 */\n int pop() {\n int num = peek();\n // \u522a\u9664\u982d\u7bc0\u9ede\n ListNode *tmp = front;\n front = front-&gt;next;\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n delete tmp;\n queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n int peek() {\n if (size() == 0)\n throw out_of_range(\"\u4f47\u5217\u70ba\u7a7a\");\n return front-&gt;val;\n }\n\n /* \u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u5316\u70ba Vector \u4e26\u8fd4\u56de */\n vector&lt;int&gt; toVector() {\n ListNode *node = front;\n vector&lt;int&gt; res(size());\n for (int i = 0; i &lt; res.size(); i++) {\n res[i] = node-&gt;val;\n node = node-&gt;next;\n }\n return res;\n }\n};\n</code></pre> linkedlist_queue.java<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass LinkedListQueue {\n private ListNode front, rear; // \u982d\u7bc0\u9ede front \uff0c\u5c3e\u7bc0\u9ede rear\n private int queSize = 0;\n\n public LinkedListQueue() {\n front = null;\n rear = null;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u5217 */\n public void push(int num) {\n // \u5728\u5c3e\u7bc0\u9ede\u5f8c\u65b0\u589e num\n ListNode node = new ListNode(num);\n // \u5982\u679c\u4f47\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4\u982d\u3001\u5c3e\u7bc0\u9ede\u90fd\u6307\u5411\u8a72\u7bc0\u9ede\n if (front == null) {\n front = node;\n rear = node;\n // \u5982\u679c\u4f47\u5217\u4e0d\u70ba\u7a7a\uff0c\u5247\u5c07\u8a72\u7bc0\u9ede\u65b0\u589e\u5230\u5c3e\u7bc0\u9ede\u5f8c\n } else {\n rear.next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u5217 */\n public int pop() {\n int num = peek();\n // \u522a\u9664\u982d\u7bc0\u9ede\n front = front.next;\n queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return front.val;\n }\n\n /* \u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n public int[] toArray() {\n ListNode node = front;\n int[] res = new int[size()];\n for (int i = 0; i &lt; res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n</code></pre> linkedlist_queue.cs<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass LinkedListQueue {\n ListNode? front, rear; // \u982d\u7bc0\u9ede front \uff0c\u5c3e\u7bc0\u9ede rear \n int queSize = 0;\n\n public LinkedListQueue() {\n front = null;\n rear = null;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u5217 */\n public void Push(int num) {\n // \u5728\u5c3e\u7bc0\u9ede\u5f8c\u65b0\u589e num\n ListNode node = new(num);\n // \u5982\u679c\u4f47\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4\u982d\u3001\u5c3e\u7bc0\u9ede\u90fd\u6307\u5411\u8a72\u7bc0\u9ede\n if (front == null) {\n front = node;\n rear = node;\n // \u5982\u679c\u4f47\u5217\u4e0d\u70ba\u7a7a\uff0c\u5247\u5c07\u8a72\u7bc0\u9ede\u65b0\u589e\u5230\u5c3e\u7bc0\u9ede\u5f8c\n } else if (rear != null) {\n rear.next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u5217 */\n public int Pop() {\n int num = Peek();\n // \u522a\u9664\u982d\u7bc0\u9ede\n front = front?.next;\n queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return front!.val;\n }\n\n /* \u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n public int[] ToArray() {\n if (front == null)\n return [];\n\n ListNode? node = front;\n int[] res = new int[Size()];\n for (int i = 0; i &lt; res.Length; i++) {\n res[i] = node!.val;\n node = node.next;\n }\n return res;\n }\n}\n</code></pre> linkedlist_queue.go<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\ntype linkedListQueue struct {\n // \u4f7f\u7528\u5167\u5efa\u5305 list \u4f86\u5be6\u73fe\u4f47\u5217\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u4f47\u5217 */\nfunc newLinkedListQueue() *linkedListQueue {\n return &amp;linkedListQueue{\n data: list.New(),\n }\n}\n\n/* \u5165\u5217 */\nfunc (s *linkedListQueue) push(value any) {\n s.data.PushBack(value)\n}\n\n/* \u51fa\u5217 */\nfunc (s *linkedListQueue) pop() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nfunc (s *linkedListQueue) peek() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n return e.Value\n}\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nfunc (s *linkedListQueue) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nfunc (s *linkedListQueue) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u7372\u53d6 List \u7528\u65bc\u5217\u5370 */\nfunc (s *linkedListQueue) toList() *list.List {\n return s.data\n}\n</code></pre> linkedlist_queue.swift<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass LinkedListQueue {\n private var front: ListNode? // \u982d\u7bc0\u9ede\n private var rear: ListNode? // \u5c3e\u7bc0\u9ede\n private var _size: Int\n\n init() {\n _size = 0\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n func size() -&gt; Int {\n _size\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n func isEmpty() -&gt; Bool {\n size() == 0\n }\n\n /* \u5165\u5217 */\n func push(num: Int) {\n // \u5728\u5c3e\u7bc0\u9ede\u5f8c\u65b0\u589e num\n let node = ListNode(x: num)\n // \u5982\u679c\u4f47\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4\u982d\u3001\u5c3e\u7bc0\u9ede\u90fd\u6307\u5411\u8a72\u7bc0\u9ede\n if front == nil {\n front = node\n rear = node\n }\n // \u5982\u679c\u4f47\u5217\u4e0d\u70ba\u7a7a\uff0c\u5247\u5c07\u8a72\u7bc0\u9ede\u65b0\u589e\u5230\u5c3e\u7bc0\u9ede\u5f8c\n else {\n rear?.next = node\n rear = node\n }\n _size += 1\n }\n\n /* \u51fa\u5217 */\n @discardableResult\n func pop() -&gt; Int {\n let num = peek()\n // \u522a\u9664\u982d\u7bc0\u9ede\n front = front?.next\n _size -= 1\n return num\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n func peek() -&gt; Int {\n if isEmpty() {\n fatalError(\"\u4f47\u5217\u70ba\u7a7a\")\n }\n return front!.val\n }\n\n /* \u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n func toArray() -&gt; [Int] {\n var node = front\n var res = Array(repeating: 0, count: size())\n for i in res.indices {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n</code></pre> linkedlist_queue.js<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass LinkedListQueue {\n #front; // \u982d\u7bc0\u9ede #front\n #rear; // \u5c3e\u7bc0\u9ede #rear\n #queSize = 0;\n\n constructor() {\n this.#front = null;\n this.#rear = null;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n get size() {\n return this.#queSize;\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n isEmpty() {\n return this.size === 0;\n }\n\n /* \u5165\u5217 */\n push(num) {\n // \u5728\u5c3e\u7bc0\u9ede\u5f8c\u65b0\u589e num\n const node = new ListNode(num);\n // \u5982\u679c\u4f47\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4\u982d\u3001\u5c3e\u7bc0\u9ede\u90fd\u6307\u5411\u8a72\u7bc0\u9ede\n if (!this.#front) {\n this.#front = node;\n this.#rear = node;\n // \u5982\u679c\u4f47\u5217\u4e0d\u70ba\u7a7a\uff0c\u5247\u5c07\u8a72\u7bc0\u9ede\u65b0\u589e\u5230\u5c3e\u7bc0\u9ede\u5f8c\n } else {\n this.#rear.next = node;\n this.#rear = node;\n }\n this.#queSize++;\n }\n\n /* \u51fa\u5217 */\n pop() {\n const num = this.peek();\n // \u522a\u9664\u982d\u7bc0\u9ede\n this.#front = this.#front.next;\n this.#queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n peek() {\n if (this.size === 0) throw new Error('\u4f47\u5217\u70ba\u7a7a');\n return this.#front.val;\n }\n\n /* \u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n toArray() {\n let node = this.#front;\n const res = new Array(this.size);\n for (let i = 0; i &lt; res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n</code></pre> linkedlist_queue.ts<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass LinkedListQueue {\n private front: ListNode | null; // \u982d\u7bc0\u9ede front\n private rear: ListNode | null; // \u5c3e\u7bc0\u9ede rear\n private queSize: number = 0;\n\n constructor() {\n this.front = null;\n this.rear = null;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n get size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n isEmpty(): boolean {\n return this.size === 0;\n }\n\n /* \u5165\u5217 */\n push(num: number): void {\n // \u5728\u5c3e\u7bc0\u9ede\u5f8c\u65b0\u589e num\n const node = new ListNode(num);\n // \u5982\u679c\u4f47\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4\u982d\u3001\u5c3e\u7bc0\u9ede\u90fd\u6307\u5411\u8a72\u7bc0\u9ede\n if (!this.front) {\n this.front = node;\n this.rear = node;\n // \u5982\u679c\u4f47\u5217\u4e0d\u70ba\u7a7a\uff0c\u5247\u5c07\u8a72\u7bc0\u9ede\u65b0\u589e\u5230\u5c3e\u7bc0\u9ede\u5f8c\n } else {\n this.rear!.next = node;\n this.rear = node;\n }\n this.queSize++;\n }\n\n /* \u51fa\u5217 */\n pop(): number {\n const num = this.peek();\n if (!this.front) throw new Error('\u4f47\u5217\u70ba\u7a7a');\n // \u522a\u9664\u982d\u7bc0\u9ede\n this.front = this.front.next;\n this.queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n peek(): number {\n if (this.size === 0) throw new Error('\u4f47\u5217\u70ba\u7a7a');\n return this.front!.val;\n }\n\n /* \u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n toArray(): number[] {\n let node = this.front;\n const res = new Array&lt;number&gt;(this.size);\n for (let i = 0; i &lt; res.length; i++) {\n res[i] = node!.val;\n node = node!.next;\n }\n return res;\n }\n}\n</code></pre> linkedlist_queue.dart<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass LinkedListQueue {\n ListNode? _front; // \u982d\u7bc0\u9ede _front\n ListNode? _rear; // \u5c3e\u7bc0\u9ede _rear\n int _queSize = 0; // \u4f47\u5217\u9577\u5ea6\n\n LinkedListQueue() {\n _front = null;\n _rear = null;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u5165\u5217 */\n void push(int _num) {\n // \u5728\u5c3e\u7bc0\u9ede\u5f8c\u65b0\u589e _num\n final node = ListNode(_num);\n // \u5982\u679c\u4f47\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4\u982d\u3001\u5c3e\u7bc0\u9ede\u90fd\u6307\u5411\u8a72\u7bc0\u9ede\n if (_front == null) {\n _front = node;\n _rear = node;\n } else {\n // \u5982\u679c\u4f47\u5217\u4e0d\u70ba\u7a7a\uff0c\u5247\u5c07\u8a72\u7bc0\u9ede\u65b0\u589e\u5230\u5c3e\u7bc0\u9ede\u5f8c\n _rear!.next = node;\n _rear = node;\n }\n _queSize++;\n }\n\n /* \u51fa\u5217 */\n int pop() {\n final int _num = peek();\n // \u522a\u9664\u982d\u7bc0\u9ede\n _front = _front!.next;\n _queSize--;\n return _num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n int peek() {\n if (_queSize == 0) {\n throw Exception('\u4f47\u5217\u70ba\u7a7a');\n }\n return _front!.val;\n }\n\n /* \u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n List&lt;int&gt; toArray() {\n ListNode? node = _front;\n final List&lt;int&gt; queue = [];\n while (node != null) {\n queue.add(node.val);\n node = node.next;\n }\n return queue;\n }\n}\n</code></pre> linkedlist_queue.rs<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\n#[allow(dead_code)]\npub struct LinkedListQueue&lt;T&gt; {\n front: Option&lt;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;, // \u982d\u7bc0\u9ede front\n rear: Option&lt;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;, // \u5c3e\u7bc0\u9ede rear\n que_size: usize, // \u4f47\u5217\u7684\u9577\u5ea6\n}\n\nimpl&lt;T: Copy&gt; LinkedListQueue&lt;T&gt; {\n pub fn new() -&gt; Self {\n Self {\n front: None,\n rear: None,\n que_size: 0,\n }\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n pub fn size(&amp;self) -&gt; usize {\n return self.que_size;\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n pub fn is_empty(&amp;self) -&gt; bool {\n return self.size() == 0;\n }\n\n /* \u5165\u5217 */\n pub fn push(&amp;mut self, num: T) {\n // \u5728\u5c3e\u7bc0\u9ede\u5f8c\u65b0\u589e num\n let new_rear = ListNode::new(num);\n match self.rear.take() {\n // \u5982\u679c\u4f47\u5217\u4e0d\u70ba\u7a7a\uff0c\u5247\u5c07\u8a72\u7bc0\u9ede\u65b0\u589e\u5230\u5c3e\u7bc0\u9ede\u5f8c\n Some(old_rear) =&gt; {\n old_rear.borrow_mut().next = Some(new_rear.clone());\n self.rear = Some(new_rear);\n }\n // \u5982\u679c\u4f47\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4\u982d\u3001\u5c3e\u7bc0\u9ede\u90fd\u6307\u5411\u8a72\u7bc0\u9ede\n None =&gt; {\n self.front = Some(new_rear.clone());\n self.rear = Some(new_rear);\n }\n }\n self.que_size += 1;\n }\n\n /* \u51fa\u5217 */\n pub fn pop(&amp;mut self) -&gt; Option&lt;T&gt; {\n self.front.take().map(|old_front| {\n match old_front.borrow_mut().next.take() {\n Some(new_front) =&gt; {\n self.front = Some(new_front);\n }\n None =&gt; {\n self.rear.take();\n }\n }\n self.que_size -= 1;\n Rc::try_unwrap(old_front).ok().unwrap().into_inner().val\n })\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n pub fn peek(&amp;self) -&gt; Option&lt;&amp;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt; {\n self.front.as_ref()\n }\n\n /* \u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n pub fn to_array(&amp;self, head: Option&lt;&amp;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;) -&gt; Vec&lt;T&gt; {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.insert(0, node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n</code></pre> linkedlist_queue.c<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\ntypedef struct {\n ListNode *front, *rear;\n int queSize;\n} LinkedListQueue;\n\n/* \u5efa\u69cb\u5b50 */\nLinkedListQueue *newLinkedListQueue() {\n LinkedListQueue *queue = (LinkedListQueue *)malloc(sizeof(LinkedListQueue));\n queue-&gt;front = NULL;\n queue-&gt;rear = NULL;\n queue-&gt;queSize = 0;\n return queue;\n}\n\n/* \u6790\u69cb\u51fd\u5f0f */\nvoid delLinkedListQueue(LinkedListQueue *queue) {\n // \u91cb\u653e\u6240\u6709\u7bc0\u9ede\n while (queue-&gt;front != NULL) {\n ListNode *tmp = queue-&gt;front;\n queue-&gt;front = queue-&gt;front-&gt;next;\n free(tmp);\n }\n // \u91cb\u653e queue \u7d50\u69cb\u9ad4\n free(queue);\n}\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nint size(LinkedListQueue *queue) {\n return queue-&gt;queSize;\n}\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nbool empty(LinkedListQueue *queue) {\n return (size(queue) == 0);\n}\n\n/* \u5165\u5217 */\nvoid push(LinkedListQueue *queue, int num) {\n // \u5c3e\u7bc0\u9ede\u8655\u65b0\u589e node\n ListNode *node = newListNode(num);\n // \u5982\u679c\u4f47\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4\u982d\u3001\u5c3e\u7bc0\u9ede\u90fd\u6307\u5411\u8a72\u7bc0\u9ede\n if (queue-&gt;front == NULL) {\n queue-&gt;front = node;\n queue-&gt;rear = node;\n }\n // \u5982\u679c\u4f47\u5217\u4e0d\u70ba\u7a7a\uff0c\u5247\u5c07\u8a72\u7bc0\u9ede\u65b0\u589e\u5230\u5c3e\u7bc0\u9ede\u5f8c\n else {\n queue-&gt;rear-&gt;next = node;\n queue-&gt;rear = node;\n }\n queue-&gt;queSize++;\n}\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nint peek(LinkedListQueue *queue) {\n assert(size(queue) &amp;&amp; queue-&gt;front);\n return queue-&gt;front-&gt;val;\n}\n\n/* \u51fa\u5217 */\nint pop(LinkedListQueue *queue) {\n int num = peek(queue);\n ListNode *tmp = queue-&gt;front;\n queue-&gt;front = queue-&gt;front-&gt;next;\n free(tmp);\n queue-&gt;queSize--;\n return num;\n}\n\n/* \u5217\u5370\u4f47\u5217 */\nvoid printLinkedListQueue(LinkedListQueue *queue) {\n int *arr = malloc(sizeof(int) * queue-&gt;queSize);\n // \u8907\u88fd\u93c8\u7d50\u4e32\u5217\u4e2d\u7684\u8cc7\u6599\u5230\u9663\u5217\n int i;\n ListNode *node;\n for (i = 0, node = queue-&gt;front; i &lt; queue-&gt;queSize; i++) {\n arr[i] = node-&gt;val;\n node = node-&gt;next;\n }\n printArray(arr, queue-&gt;queSize);\n free(arr);\n}\n</code></pre> linkedlist_queue.kt<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass LinkedListQueue(\n // \u982d\u7bc0\u9ede front \uff0c\u5c3e\u7bc0\u9ede rear\n private var front: ListNode? = null,\n private var rear: ListNode? = null,\n private var queSize: Int = 0\n) {\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n fun size(): Int {\n return queSize\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n fun isEmpty(): Boolean {\n return size() == 0\n }\n\n /* \u5165\u5217 */\n fun push(num: Int) {\n // \u5728\u5c3e\u7bc0\u9ede\u5f8c\u65b0\u589e num\n val node = ListNode(num)\n // \u5982\u679c\u4f47\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4\u982d\u3001\u5c3e\u7bc0\u9ede\u90fd\u6307\u5411\u8a72\u7bc0\u9ede\n if (front == null) {\n front = node\n rear = node\n // \u5982\u679c\u4f47\u5217\u4e0d\u70ba\u7a7a\uff0c\u5247\u5c07\u8a72\u7bc0\u9ede\u65b0\u589e\u5230\u5c3e\u7bc0\u9ede\u5f8c\n } else {\n rear?.next = node\n rear = node\n }\n queSize++\n }\n\n /* \u51fa\u5217 */\n fun pop(): Int {\n val num = peek()\n // \u522a\u9664\u982d\u7bc0\u9ede\n front = front?.next\n queSize--\n return num\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n fun peek(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return front!!._val\n }\n\n /* \u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n fun toArray(): IntArray {\n var node = front\n val res = IntArray(size())\n for (i in res.indices) {\n res[i] = node!!._val\n node = node.next\n }\n return res\n }\n}\n</code></pre> linkedlist_queue.rb<pre><code>### \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u982d\u73fe\u7684\u4f47\u5217 ###\nclass LinkedListQueue\n ### \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 ###\n attr_reader :size\n\n ### \u5efa\u69cb\u5b50 ###\n def initialize\n @front = nil # \u982d\u7bc0\u9ede front\n @rear = nil # \u5c3e\u7bc0\u9ede rear\n @size = 0\n end\n\n ### \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a ###\n def is_empty?\n @front.nil?\n end\n\n ### \u5165\u5217 ###\n def push(num)\n # \u5728\u5c3e\u7bc0\u9ede\u5f8c\u65b0\u589e num\n node = ListNode.new(num)\n\n # \u5982\u679c\u4f47\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4\u982d\uff0c\u5c3e\u7bc0\u9ede\u90fd\u6307\u5411\u8a72\u7bc0\u9ede\n if @front.nil?\n @front = node\n @rear = node\n # \u5982\u679c\u4f47\u5217\u4e0d\u70ba\u7a7a\uff0c\u5247\u4ee4\u8a72\u7bc0\u9ede\u65b0\u589e\u5230\u5c3e\u7bc0\u9ede\u5f8c\n else\n @rear.next = node\n @rear = node\n end\n\n @size += 1\n end\n\n ### \u51fa\u5217 ###\n def pop\n num = peek\n # \u522a\u9664\u982d\u7bc0\u9ede\n @front = @front.next\n @size -= 1\n num\n end\n\n ### \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 ###\n def peek\n raise IndexError, '\u4f47\u5217\u70ba\u7a7a' if is_empty?\n\n @front.val\n end\n\n ### \u5c07\u93c8\u7d50\u4e32\u5217\u70ba Array \u4e26\u8fd4\u56de ###\n def to_array\n queue = []\n temp = @front\n while temp\n queue &lt;&lt; temp.val\n temp = temp.next\n end\n queue\n end\nend\n</code></pre> linkedlist_queue.zig<pre><code>// \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u4f47\u5217\nfn LinkedListQueue(comptime T: type) type {\n return struct {\n const Self = @This();\n\n front: ?*inc.ListNode(T) = null, // \u982d\u7bc0\u9ede front\n rear: ?*inc.ListNode(T) = null, // \u5c3e\u7bc0\u9ede rear\n que_size: usize = 0, // \u4f47\u5217\u7684\u9577\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u8a18\u61b6\u9ad4\u5206\u914d\u5668\n\n // \u5efa\u69cb\u5b50\uff08\u5206\u914d\u8a18\u61b6\u9ad4+\u521d\u59cb\u5316\u4f47\u5217\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.front = null;\n self.rear = null;\n self.que_size = 0;\n }\n\n // \u6790\u69cb\u51fd\u5f0f\uff08\u91cb\u653e\u8a18\u61b6\u9ad4\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6\n pub fn size(self: *Self) usize {\n return self.que_size;\n }\n\n // \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.size() == 0) @panic(\"\u4f47\u5217\u70ba\u7a7a\");\n return self.front.?.val;\n } \n\n // \u5165\u5217\n pub fn push(self: *Self, num: T) !void {\n // \u5728\u5c3e\u7bc0\u9ede\u5f8c\u65b0\u589e num\n var node = try self.mem_allocator.create(inc.ListNode(T));\n node.init(num);\n // \u5982\u679c\u4f47\u5217\u70ba\u7a7a\uff0c\u5247\u4ee4\u982d\u3001\u5c3e\u7bc0\u9ede\u90fd\u6307\u5411\u8a72\u7bc0\u9ede\n if (self.front == null) {\n self.front = node;\n self.rear = node;\n // \u5982\u679c\u4f47\u5217\u4e0d\u70ba\u7a7a\uff0c\u5247\u5c07\u8a72\u7bc0\u9ede\u65b0\u589e\u5230\u5c3e\u7bc0\u9ede\u5f8c\n } else {\n self.rear.?.next = node;\n self.rear = node;\n }\n self.que_size += 1;\n } \n\n // \u51fa\u5217\n pub fn pop(self: *Self) T {\n var num = self.peek();\n // \u522a\u9664\u982d\u7bc0\u9ede\n self.front = self.front.?.next;\n self.que_size -= 1;\n return num;\n } \n\n // \u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u63db\u70ba\u9663\u5217\n pub fn toArray(self: *Self) ![]T {\n var node = self.front;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i &lt; res.len) : (i += 1) {\n res[i] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_stack_and_queue/queue/#2","title":"2. \u00a0 \u57fa\u65bc\u9663\u5217\u7684\u5be6\u73fe","text":"<p>\u5728\u9663\u5217\u4e2d\u522a\u9664\u9996\u5143\u7d20\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \uff0c\u9019\u6703\u5c0e\u81f4\u51fa\u5217\u64cd\u4f5c\u6548\u7387\u8f03\u4f4e\u3002\u7136\u800c\uff0c\u6211\u5011\u53ef\u4ee5\u63a1\u7528\u4ee5\u4e0b\u5de7\u5999\u65b9\u6cd5\u4f86\u907f\u514d\u9019\u500b\u554f\u984c\u3002</p> <p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4e00\u500b\u8b8a\u6578 <code>front</code> \u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u4e26\u7dad\u8b77\u4e00\u500b\u8b8a\u6578 <code>size</code> \u7528\u65bc\u8a18\u9304\u4f47\u5217\u9577\u5ea6\u3002\u5b9a\u7fa9 <code>rear = front + size</code> \uff0c\u9019\u500b\u516c\u5f0f\u8a08\u7b97\u51fa\u7684 <code>rear</code> \u6307\u5411\u4f47\u5217\u5c3e\u5143\u7d20\u4e4b\u5f8c\u7684\u4e0b\u4e00\u500b\u4f4d\u7f6e\u3002</p> <p>\u57fa\u65bc\u6b64\u8a2d\u8a08\uff0c\u9663\u5217\u4e2d\u5305\u542b\u5143\u7d20\u7684\u6709\u6548\u5340\u9593\u70ba <code>[front, rear - 1]</code>\uff0c\u5404\u7a2e\u64cd\u4f5c\u7684\u5be6\u73fe\u65b9\u6cd5\u5982\u5716 5-6 \u6240\u793a\u3002</p> <ul> <li>\u5165\u5217\u64cd\u4f5c\uff1a\u5c07\u8f38\u5165\u5143\u7d20\u8ce6\u503c\u7d66 <code>rear</code> \u7d22\u5f15\u8655\uff0c\u4e26\u5c07 <code>size</code> \u589e\u52a0 1 \u3002</li> <li>\u51fa\u5217\u64cd\u4f5c\uff1a\u53ea\u9700\u5c07 <code>front</code> \u589e\u52a0 1 \uff0c\u4e26\u5c07 <code>size</code> \u6e1b\u5c11 1 \u3002</li> </ul> <p>\u53ef\u4ee5\u770b\u5230\uff0c\u5165\u5217\u548c\u51fa\u5217\u64cd\u4f5c\u90fd\u53ea\u9700\u9032\u884c\u4e00\u6b21\u64cd\u4f5c\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u5747\u70ba \\(O(1)\\) \u3002</p> ArrayQueuepush()pop() <p></p> <p></p> <p></p> <p> \u5716 5-6 \u00a0 \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u4f47\u5217\u7684\u5165\u5217\u51fa\u5217\u64cd\u4f5c </p> <p>\u4f60\u53ef\u80fd\u6703\u767c\u73fe\u4e00\u500b\u554f\u984c\uff1a\u5728\u4e0d\u65b7\u9032\u884c\u5165\u5217\u548c\u51fa\u5217\u7684\u904e\u7a0b\u4e2d\uff0c<code>front</code> \u548c <code>rear</code> \u90fd\u5728\u5411\u53f3\u79fb\u52d5\uff0c\u7576\u5b83\u5011\u5230\u9054\u9663\u5217\u5c3e\u90e8\u6642\u5c31\u7121\u6cd5\u7e7c\u7e8c\u79fb\u52d5\u4e86\u3002\u70ba\u4e86\u89e3\u6c7a\u6b64\u554f\u984c\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u9663\u5217\u8996\u70ba\u9996\u5c3e\u76f8\u63a5\u7684\u201c\u74b0\u5f62\u9663\u5217\u201d\u3002</p> <p>\u5c0d\u65bc\u74b0\u5f62\u9663\u5217\uff0c\u6211\u5011\u9700\u8981\u8b93 <code>front</code> \u6216 <code>rear</code> \u5728\u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u6642\uff0c\u76f4\u63a5\u56de\u5230\u9663\u5217\u982d\u90e8\u7e7c\u7e8c\u8d70\u8a2a\u3002\u9019\u7a2e\u9031\u671f\u6027\u898f\u5f8b\u53ef\u4ee5\u900f\u904e\u201c\u53d6\u9918\u64cd\u4f5c\u201d\u4f86\u5be6\u73fe\uff0c\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array_queue.py<pre><code>class ArrayQueue:\n \"\"\"\u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u4f47\u5217\"\"\"\n\n def __init__(self, size: int):\n \"\"\"\u5efa\u69cb\u5b50\"\"\"\n self._nums: list[int] = [0] * size # \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n self._front: int = 0 # \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n self._size: int = 0 # \u4f47\u5217\u9577\u5ea6\n\n def capacity(self) -&gt; int:\n \"\"\"\u7372\u53d6\u4f47\u5217\u7684\u5bb9\u91cf\"\"\"\n return len(self._nums)\n\n def size(self) -&gt; int:\n \"\"\"\u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -&gt; bool:\n \"\"\"\u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a\"\"\"\n return self._size == 0\n\n def push(self, num: int):\n \"\"\"\u5165\u5217\"\"\"\n if self._size == self.capacity():\n raise IndexError(\"\u4f47\u5217\u5df2\u6eff\")\n # \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n # \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe rear \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\u56de\u5230\u982d\u90e8\n rear: int = (self._front + self._size) % self.capacity()\n # \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n self._nums[rear] = num\n self._size += 1\n\n def pop(self) -&gt; int:\n \"\"\"\u51fa\u5217\"\"\"\n num: int = self.peek()\n # \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u82e5\u8d8a\u904e\u5c3e\u90e8\uff0c\u5247\u8fd4\u56de\u5230\u9663\u5217\u982d\u90e8\n self._front = (self._front + 1) % self.capacity()\n self._size -= 1\n return num\n\n def peek(self) -&gt; int:\n \"\"\"\u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u4f47\u5217\u70ba\u7a7a\")\n return self._nums[self._front]\n\n def to_list(self) -&gt; list[int]:\n \"\"\"\u8fd4\u56de\u4e32\u5217\u7528\u65bc\u5217\u5370\"\"\"\n res = [0] * self.size()\n j: int = self._front\n for i in range(self.size()):\n res[i] = self._nums[(j % self.capacity())]\n j += 1\n return res\n</code></pre> array_queue.cpp<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass ArrayQueue {\n private:\n int *nums; // \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n int front; // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n int queSize; // \u4f47\u5217\u9577\u5ea6\n int queCapacity; // \u4f47\u5217\u5bb9\u91cf\n\n public:\n ArrayQueue(int capacity) {\n // \u521d\u59cb\u5316\u9663\u5217\n nums = new int[capacity];\n queCapacity = capacity;\n front = queSize = 0;\n }\n\n ~ArrayQueue() {\n delete[] nums;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return queCapacity;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u5217 */\n void push(int num) {\n if (queSize == queCapacity) {\n cout &lt;&lt; \"\u4f47\u5217\u5df2\u6eff\" &lt;&lt; endl;\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe rear \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\u56de\u5230\u982d\u90e8\n int rear = (front + queSize) % queCapacity;\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u5217 */\n int pop() {\n int num = peek();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u82e5\u8d8a\u904e\u5c3e\u90e8\uff0c\u5247\u8fd4\u56de\u5230\u9663\u5217\u982d\u90e8\n front = (front + 1) % queCapacity;\n queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n int peek() {\n if (isEmpty())\n throw out_of_range(\"\u4f47\u5217\u70ba\u7a7a\");\n return nums[front];\n }\n\n /* \u5c07\u9663\u5217\u8f49\u5316\u70ba Vector \u4e26\u8fd4\u56de */\n vector&lt;int&gt; toVector() {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n vector&lt;int&gt; arr(queSize);\n for (int i = 0, j = front; i &lt; queSize; i++, j++) {\n arr[i] = nums[j % queCapacity];\n }\n return arr;\n }\n};\n</code></pre> array_queue.java<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass ArrayQueue {\n private int[] nums; // \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n private int front; // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n private int queSize; // \u4f47\u5217\u9577\u5ea6\n\n public ArrayQueue(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u5bb9\u91cf */\n public int capacity() {\n return nums.length;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n public boolean isEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u5217 */\n public void push(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u4f47\u5217\u5df2\u6eff\");\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe rear \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\u56de\u5230\u982d\u90e8\n int rear = (front + queSize) % capacity();\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u5217 */\n public int pop() {\n int num = peek();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u82e5\u8d8a\u904e\u5c3e\u90e8\uff0c\u5247\u8fd4\u56de\u5230\u9663\u5217\u982d\u90e8\n front = (front + 1) % capacity();\n queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return nums[front];\n }\n\n /* \u8fd4\u56de\u9663\u5217 */\n public int[] toArray() {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i &lt; queSize; i++, j++) {\n res[i] = nums[j % capacity()];\n }\n return res;\n }\n}\n</code></pre> array_queue.cs<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass ArrayQueue {\n int[] nums; // \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n int front; // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n int queSize; // \u4f47\u5217\u9577\u5ea6\n\n public ArrayQueue(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u5bb9\u91cf */\n int Capacity() {\n return nums.Length;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n public bool IsEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u5217 */\n public void Push(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u4f47\u5217\u5df2\u6eff\");\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe rear \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\u56de\u5230\u982d\u90e8\n int rear = (front + queSize) % Capacity();\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u5217 */\n public int Pop() {\n int num = Peek();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u82e5\u8d8a\u904e\u5c3e\u90e8\uff0c\u5247\u8fd4\u56de\u5230\u9663\u5217\u982d\u90e8\n front = (front + 1) % Capacity();\n queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return nums[front];\n }\n\n /* \u8fd4\u56de\u9663\u5217 */\n public int[] ToArray() {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i &lt; queSize; i++, j++) {\n res[i] = nums[j % this.Capacity()];\n }\n return res;\n }\n}\n</code></pre> array_queue.go<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\ntype arrayQueue struct {\n nums []int // \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n front int // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n queSize int // \u4f47\u5217\u9577\u5ea6\n queCapacity int // \u4f47\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7d0d\u5143\u7d20\u6578\u91cf\uff09\n}\n\n/* \u521d\u59cb\u5316\u4f47\u5217 */\nfunc newArrayQueue(queCapacity int) *arrayQueue {\n return &amp;arrayQueue{\n nums: make([]int, queCapacity),\n queCapacity: queCapacity,\n front: 0,\n queSize: 0,\n }\n}\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nfunc (q *arrayQueue) size() int {\n return q.queSize\n}\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nfunc (q *arrayQueue) isEmpty() bool {\n return q.queSize == 0\n}\n\n/* \u5165\u5217 */\nfunc (q *arrayQueue) push(num int) {\n // \u7576 rear == queCapacity \u8868\u793a\u4f47\u5217\u5df2\u6eff\n if q.queSize == q.queCapacity {\n return\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe rear \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\u56de\u5230\u982d\u90e8\n rear := (q.front + q.queSize) % q.queCapacity\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n q.nums[rear] = num\n q.queSize++\n}\n\n/* \u51fa\u5217 */\nfunc (q *arrayQueue) pop() any {\n num := q.peek()\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u82e5\u8d8a\u904e\u5c3e\u90e8\uff0c\u5247\u8fd4\u56de\u5230\u9663\u5217\u982d\u90e8\n q.front = (q.front + 1) % q.queCapacity\n q.queSize--\n return num\n}\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nfunc (q *arrayQueue) peek() any {\n if q.isEmpty() {\n return nil\n }\n return q.nums[q.front]\n}\n\n/* \u7372\u53d6 Slice \u7528\u65bc\u5217\u5370 */\nfunc (q *arrayQueue) toSlice() []int {\n rear := (q.front + q.queSize)\n if rear &gt;= q.queCapacity {\n rear %= q.queCapacity\n return append(q.nums[q.front:], q.nums[:rear]...)\n }\n return q.nums[q.front:rear]\n}\n</code></pre> array_queue.swift<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass ArrayQueue {\n private var nums: [Int] // \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n private var front: Int // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n private var _size: Int // \u4f47\u5217\u9577\u5ea6\n\n init(capacity: Int) {\n // \u521d\u59cb\u5316\u9663\u5217\n nums = Array(repeating: 0, count: capacity)\n front = 0\n _size = 0\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u5bb9\u91cf */\n func capacity() -&gt; Int {\n nums.count\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n func size() -&gt; Int {\n _size\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n func isEmpty() -&gt; Bool {\n size() == 0\n }\n\n /* \u5165\u5217 */\n func push(num: Int) {\n if size() == capacity() {\n print(\"\u4f47\u5217\u5df2\u6eff\")\n return\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe rear \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\u56de\u5230\u982d\u90e8\n let rear = (front + size()) % capacity()\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n nums[rear] = num\n _size += 1\n }\n\n /* \u51fa\u5217 */\n @discardableResult\n func pop() -&gt; Int {\n let num = peek()\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u82e5\u8d8a\u904e\u5c3e\u90e8\uff0c\u5247\u8fd4\u56de\u5230\u9663\u5217\u982d\u90e8\n front = (front + 1) % capacity()\n _size -= 1\n return num\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n func peek() -&gt; Int {\n if isEmpty() {\n fatalError(\"\u4f47\u5217\u70ba\u7a7a\")\n }\n return nums[front]\n }\n\n /* \u8fd4\u56de\u9663\u5217 */\n func toArray() -&gt; [Int] {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n (front ..&lt; front + size()).map { nums[$0 % capacity()] }\n }\n}\n</code></pre> array_queue.js<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass ArrayQueue {\n #nums; // \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n #front = 0; // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n #queSize = 0; // \u4f47\u5217\u9577\u5ea6\n\n constructor(capacity) {\n this.#nums = new Array(capacity);\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u5bb9\u91cf */\n get capacity() {\n return this.#nums.length;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n get size() {\n return this.#queSize;\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u5165\u5217 */\n push(num) {\n if (this.size === this.capacity) {\n console.log('\u4f47\u5217\u5df2\u6eff');\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe rear \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\u56de\u5230\u982d\u90e8\n const rear = (this.#front + this.size) % this.capacity;\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n this.#nums[rear] = num;\n this.#queSize++;\n }\n\n /* \u51fa\u5217 */\n pop() {\n const num = this.peek();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u82e5\u8d8a\u904e\u5c3e\u90e8\uff0c\u5247\u8fd4\u56de\u5230\u9663\u5217\u982d\u90e8\n this.#front = (this.#front + 1) % this.capacity;\n this.#queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n peek() {\n if (this.isEmpty()) throw new Error('\u4f47\u5217\u70ba\u7a7a');\n return this.#nums[this.#front];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n const arr = new Array(this.size);\n for (let i = 0, j = this.#front; i &lt; this.size; i++, j++) {\n arr[i] = this.#nums[j % this.capacity];\n }\n return arr;\n }\n}\n</code></pre> array_queue.ts<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass ArrayQueue {\n private nums: number[]; // \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n private front: number; // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n private queSize: number; // \u4f47\u5217\u9577\u5ea6\n\n constructor(capacity: number) {\n this.nums = new Array(capacity);\n this.front = this.queSize = 0;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u5bb9\u91cf */\n get capacity(): number {\n return this.nums.length;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n get size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u5165\u5217 */\n push(num: number): void {\n if (this.size === this.capacity) {\n console.log('\u4f47\u5217\u5df2\u6eff');\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe rear \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\u56de\u5230\u982d\u90e8\n const rear = (this.front + this.queSize) % this.capacity;\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n this.nums[rear] = num;\n this.queSize++;\n }\n\n /* \u51fa\u5217 */\n pop(): number {\n const num = this.peek();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u82e5\u8d8a\u904e\u5c3e\u90e8\uff0c\u5247\u8fd4\u56de\u5230\u9663\u5217\u982d\u90e8\n this.front = (this.front + 1) % this.capacity;\n this.queSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n peek(): number {\n if (this.isEmpty()) throw new Error('\u4f47\u5217\u70ba\u7a7a');\n return this.nums[this.front];\n }\n\n /* \u8fd4\u56de Array */\n toArray(): number[] {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n const arr = new Array(this.size);\n for (let i = 0, j = this.front; i &lt; this.size; i++, j++) {\n arr[i] = this.nums[j % this.capacity];\n }\n return arr;\n }\n}\n</code></pre> array_queue.dart<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass ArrayQueue {\n late List&lt;int&gt; _nums; // \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n late int _front; // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n late int _queSize; // \u4f47\u5217\u9577\u5ea6\n\n ArrayQueue(int capacity) {\n _nums = List.filled(capacity, 0);\n _front = _queSize = 0;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u5bb9\u91cf */\n int capaCity() {\n return _nums.length;\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u5165\u5217 */\n void push(int _num) {\n if (_queSize == capaCity()) {\n throw Exception(\"\u4f47\u5217\u5df2\u6eff\");\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe rear \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\u56de\u5230\u982d\u90e8\n int rear = (_front + _queSize) % capaCity();\n // \u5c07 _num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n _nums[rear] = _num;\n _queSize++;\n }\n\n /* \u51fa\u5217 */\n int pop() {\n int _num = peek();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u82e5\u8d8a\u904e\u5c3e\u90e8\uff0c\u5247\u8fd4\u56de\u5230\u9663\u5217\u982d\u90e8\n _front = (_front + 1) % capaCity();\n _queSize--;\n return _num;\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n int peek() {\n if (isEmpty()) {\n throw Exception(\"\u4f47\u5217\u70ba\u7a7a\");\n }\n return _nums[_front];\n }\n\n /* \u8fd4\u56de Array */\n List&lt;int&gt; toArray() {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n final List&lt;int&gt; res = List.filled(_queSize, 0);\n for (int i = 0, j = _front; i &lt; _queSize; i++, j++) {\n res[i] = _nums[j % capaCity()];\n }\n return res;\n }\n}\n</code></pre> array_queue.rs<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nstruct ArrayQueue {\n nums: Vec&lt;i32&gt;, // \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n front: i32, // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n que_size: i32, // \u4f47\u5217\u9577\u5ea6\n que_capacity: i32, // \u4f47\u5217\u5bb9\u91cf\n}\n\nimpl ArrayQueue {\n /* \u5efa\u69cb\u5b50 */\n fn new(capacity: i32) -&gt; ArrayQueue {\n ArrayQueue {\n nums: vec![0; capacity as usize],\n front: 0,\n que_size: 0,\n que_capacity: capacity,\n }\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u5bb9\u91cf */\n fn capacity(&amp;self) -&gt; i32 {\n self.que_capacity\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n fn size(&amp;self) -&gt; i32 {\n self.que_size\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n fn is_empty(&amp;self) -&gt; bool {\n self.que_size == 0\n }\n\n /* \u5165\u5217 */\n fn push(&amp;mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u4f47\u5217\u5df2\u6eff\");\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe rear \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\u56de\u5230\u982d\u90e8\n let rear = (self.front + self.que_size) % self.que_capacity;\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n self.nums[rear as usize] = num;\n self.que_size += 1;\n }\n\n /* \u51fa\u5217 */\n fn pop(&amp;mut self) -&gt; i32 {\n let num = self.peek();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u82e5\u8d8a\u904e\u5c3e\u90e8\uff0c\u5247\u8fd4\u56de\u5230\u9663\u5217\u982d\u90e8\n self.front = (self.front + 1) % self.que_capacity;\n self.que_size -= 1;\n num\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n fn peek(&amp;self) -&gt; i32 {\n if self.is_empty() {\n panic!(\"index out of bounds\");\n }\n self.nums[self.front as usize]\n }\n\n /* \u8fd4\u56de\u9663\u5217 */\n fn to_vector(&amp;self) -&gt; Vec&lt;i32&gt; {\n let cap = self.que_capacity;\n let mut j = self.front;\n let mut arr = vec![0; self.que_size as usize];\n for i in 0..self.que_size {\n arr[i as usize] = self.nums[(j % cap) as usize];\n j += 1;\n }\n arr\n }\n}\n</code></pre> array_queue.c<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\ntypedef struct {\n int *nums; // \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n int front; // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n int queSize; // \u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e + 1\n int queCapacity; // \u4f47\u5217\u5bb9\u91cf\n} ArrayQueue;\n\n/* \u5efa\u69cb\u5b50 */\nArrayQueue *newArrayQueue(int capacity) {\n ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue));\n // \u521d\u59cb\u5316\u9663\u5217\n queue-&gt;queCapacity = capacity;\n queue-&gt;nums = (int *)malloc(sizeof(int) * queue-&gt;queCapacity);\n queue-&gt;front = queue-&gt;queSize = 0;\n return queue;\n}\n\n/* \u6790\u69cb\u51fd\u5f0f */\nvoid delArrayQueue(ArrayQueue *queue) {\n free(queue-&gt;nums);\n free(queue);\n}\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u5bb9\u91cf */\nint capacity(ArrayQueue *queue) {\n return queue-&gt;queCapacity;\n}\n\n/* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\nint size(ArrayQueue *queue) {\n return queue-&gt;queSize;\n}\n\n/* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\nbool empty(ArrayQueue *queue) {\n return queue-&gt;queSize == 0;\n}\n\n/* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\nint peek(ArrayQueue *queue) {\n assert(size(queue) != 0);\n return queue-&gt;nums[queue-&gt;front];\n}\n\n/* \u5165\u5217 */\nvoid push(ArrayQueue *queue, int num) {\n if (size(queue) == capacity(queue)) {\n printf(\"\u4f47\u5217\u5df2\u6eff\\r\\n\");\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe rear \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\u56de\u5230\u982d\u90e8\n int rear = (queue-&gt;front + queue-&gt;queSize) % queue-&gt;queCapacity;\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n queue-&gt;nums[rear] = num;\n queue-&gt;queSize++;\n}\n\n/* \u51fa\u5217 */\nint pop(ArrayQueue *queue) {\n int num = peek(queue);\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u82e5\u8d8a\u904e\u5c3e\u90e8\uff0c\u5247\u8fd4\u56de\u5230\u9663\u5217\u982d\u90e8\n queue-&gt;front = (queue-&gt;front + 1) % queue-&gt;queCapacity;\n queue-&gt;queSize--;\n return num;\n}\n</code></pre> array_queue.kt<pre><code>/* \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u4f47\u5217 */\nclass ArrayQueue(capacity: Int) {\n private val nums: IntArray = IntArray(capacity) // \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n private var front: Int = 0 // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n private var queSize: Int = 0 // \u4f47\u5217\u9577\u5ea6\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u5bb9\u91cf */\n fun capacity(): Int {\n return nums.size\n }\n\n /* \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 */\n fun size(): Int {\n return queSize\n }\n\n /* \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a */\n fun isEmpty(): Boolean {\n return queSize == 0\n }\n\n /* \u5165\u5217 */\n fun push(num: Int) {\n if (queSize == capacity()) {\n println(\"\u4f47\u5217\u5df2\u6eff\")\n return\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe rear \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\u56de\u5230\u982d\u90e8\n val rear = (front + queSize) % capacity()\n // \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n nums[rear] = num\n queSize++\n }\n\n /* \u51fa\u5217 */\n fun pop(): Int {\n val num = peek()\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u82e5\u8d8a\u904e\u5c3e\u90e8\uff0c\u5247\u8fd4\u56de\u5230\u9663\u5217\u982d\u90e8\n front = (front + 1) % capacity()\n queSize--\n return num\n }\n\n /* \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 */\n fun peek(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return nums[front]\n }\n\n /* \u8fd4\u56de\u9663\u5217 */\n fun toArray(): IntArray {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n val res = IntArray(queSize)\n var i = 0\n var j = front\n while (i &lt; queSize) {\n res[i] = nums[j % capacity()]\n i++\n j++\n }\n return res\n }\n}\n</code></pre> array_queue.rb<pre><code>### \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u4f47\u5217 ###\nclass ArrayQueue\n ### \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6 ###\n attr_reader :size\n\n ### \u5efa\u69cb\u5b50 ###\n def initialize(size)\n @nums = Array.new(size, 0) # \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217\n @front = 0 # \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n @size = 0 # \u4f47\u5217\u9577\u5ea6\n end\n\n ### \u7372\u53d6\u4f47\u5217\u7684\u5bb9\u91cf ###\n def capacity\n @nums.length\n end\n\n ### \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a ###\n def is_empty?\n size.zero?\n end\n\n ### \u5165\u5217 ###\n def push(num)\n raise IndexError, '\u4f47\u5217\u5df2\u6eff' if size == capacity\n\n # \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n # \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe rear \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\u56de\u5230\u982d\u90e8\n rear = (@front + size) % capacity\n # \u5c07 num \u65b0\u589e\u81f3\u4f47\u5217\u5c3e\n @nums[rear] = num\n @size += 1\n end\n\n ### \u51fa\u5217 ###\n def pop\n num = peek\n # \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u82e5\u8d8a\u904e\u5c3e\u90e8\uff0c\u5247\u8fd4\u56de\u5230\u9663\u5217\u982d\u90e8\n @front = (@front + 1) % capacity\n @size -= 1\n num\n end\n\n ### \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20 ###\n def peek\n raise IndexError, '\u4f47\u5217\u70ba\u7a7a' if is_empty?\n\n @nums[@front]\n end\n\n ### \u8fd4\u56de\u4e32\u5217\u7528\u65bc\u5217\u5370 ###\n def to_array\n res = Array.new(size, 0)\n j = @front\n\n for i in 0...size\n res[i] = @nums[j % capacity]\n j += 1\n end\n\n res\n end\nend\n</code></pre> array_queue.zig<pre><code>// \u57fa\u65bc\u74b0\u5f62\u9663\u5217\u5be6\u73fe\u7684\u4f47\u5217\nfn ArrayQueue(comptime T: type) type {\n return struct {\n const Self = @This();\n\n nums: []T = undefined, // \u7528\u65bc\u5132\u5b58\u4f47\u5217\u5143\u7d20\u7684\u9663\u5217 \n cap: usize = 0, // \u4f47\u5217\u5bb9\u91cf\n front: usize = 0, // \u4f47\u5217\u9996\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u9996\u5143\u7d20\n queSize: usize = 0, // \u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e + 1\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u8a18\u61b6\u9ad4\u5206\u914d\u5668\n\n // \u5efa\u69cb\u5b50\uff08\u5206\u914d\u8a18\u61b6\u9ad4+\u521d\u59cb\u5316\u9663\u5217\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.cap = cap;\n self.nums = try self.mem_allocator.alloc(T, self.cap);\n @memset(self.nums, @as(T, 0));\n }\n\n // \u6790\u69cb\u51fd\u5f0f\uff08\u91cb\u653e\u8a18\u61b6\u9ad4\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u7372\u53d6\u4f47\u5217\u7684\u5bb9\u91cf\n pub fn capacity(self: *Self) usize {\n return self.cap;\n }\n\n // \u7372\u53d6\u4f47\u5217\u7684\u9577\u5ea6\n pub fn size(self: *Self) usize {\n return self.queSize;\n }\n\n // \u5224\u65b7\u4f47\u5217\u662f\u5426\u70ba\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.queSize == 0;\n }\n\n // \u5165\u5217\n pub fn push(self: *Self, num: T) !void {\n if (self.size() == self.capacity()) {\n std.debug.print(\"\u4f47\u5217\u5df2\u6eff\\n\", .{});\n return;\n }\n // \u8a08\u7b97\u4f47\u5217\u5c3e\u6307\u6a19\uff0c\u6307\u5411\u4f47\u5217\u5c3e\u7d22\u5f15 + 1\n // \u900f\u904e\u53d6\u9918\u64cd\u4f5c\u5be6\u73fe rear \u8d8a\u904e\u9663\u5217\u5c3e\u90e8\u5f8c\u56de\u5230\u982d\u90e8\n var rear = (self.front + self.queSize) % self.capacity();\n // \u5728\u5c3e\u7bc0\u9ede\u5f8c\u65b0\u589e num\n self.nums[rear] = num;\n self.queSize += 1;\n } \n\n // \u51fa\u5217\n pub fn pop(self: *Self) T {\n var num = self.peek();\n // \u4f47\u5217\u9996\u6307\u6a19\u5411\u5f8c\u79fb\u52d5\u4e00\u4f4d\uff0c\u82e5\u8d8a\u904e\u5c3e\u90e8\uff0c\u5247\u8fd4\u56de\u5230\u9663\u5217\u982d\u90e8\n self.front = (self.front + 1) % self.capacity();\n self.queSize -= 1;\n return num;\n } \n\n // \u8a2a\u554f\u4f47\u5217\u9996\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u4f47\u5217\u70ba\u7a7a\");\n return self.nums[self.front];\n } \n\n // \u8fd4\u56de\u9663\u5217\n pub fn toArray(self: *Self) ![]T {\n // \u50c5\u8f49\u63db\u6709\u6548\u9577\u5ea6\u7bc4\u570d\u5167\u7684\u4e32\u5217\u5143\u7d20\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n var j: usize = self.front;\n while (i &lt; self.size()) : ({ i += 1; j += 1; }) {\n res[i] = self.nums[j % self.capacity()];\n }\n return res;\n }\n };\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u4ee5\u4e0a\u5be6\u73fe\u7684\u4f47\u5217\u4ecd\u7136\u5177\u6709\u4fb7\u9650\u6027\uff1a\u5176\u9577\u5ea6\u4e0d\u53ef\u8b8a\u3002\u7136\u800c\uff0c\u9019\u500b\u554f\u984c\u4e0d\u96e3\u89e3\u6c7a\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u9663\u5217\u66ff\u63db\u70ba\u52d5\u614b\u9663\u5217\uff0c\u5f9e\u800c\u5f15\u5165\u64f4\u5bb9\u6a5f\u5236\u3002\u6709\u8208\u8da3\u7684\u8b80\u8005\u53ef\u4ee5\u5617\u8a66\u81ea\u884c\u5be6\u73fe\u3002</p> <p>\u5169\u7a2e\u5be6\u73fe\u7684\u5c0d\u6bd4\u7d50\u8ad6\u8207\u5806\u758a\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d05\u8ff0\u3002</p>"},{"location":"chapter_stack_and_queue/queue/#523","title":"5.2.3 \u00a0 \u4f47\u5217\u5178\u578b\u61c9\u7528","text":"<ul> <li>\u6dd8\u5bf6\u8a02\u55ae\u3002\u8cfc\u7269\u8005\u4e0b\u55ae\u5f8c\uff0c\u8a02\u55ae\u5c07\u52a0\u5165\u5217\u5217\u4e2d\uff0c\u7cfb\u7d71\u96a8\u5f8c\u6703\u6839\u64da\u9806\u5e8f\u8655\u7406\u4f47\u5217\u4e2d\u7684\u8a02\u55ae\u3002\u5728\u96d9\u5341\u4e00\u671f\u9593\uff0c\u77ed\u6642\u9593\u5167\u6703\u7522\u751f\u6d77\u91cf\u8a02\u55ae\uff0c\u9ad8\u4f75\u767c\u6210\u70ba\u5de5\u7a0b\u5e2b\u5011\u9700\u8981\u91cd\u9ede\u653b\u514b\u7684\u554f\u984c\u3002</li> <li>\u5404\u985e\u5f85\u8fa6\u4e8b\u9805\u3002\u4efb\u4f55\u9700\u8981\u5be6\u73fe\u201c\u5148\u4f86\u5f8c\u5230\u201d\u529f\u80fd\u7684\u5834\u666f\uff0c\u4f8b\u5982\u5370\u8868\u6a5f\u7684\u4efb\u52d9\u4f47\u5217\u3001\u9910\u5ef3\u7684\u51fa\u9910\u4f47\u5217\u7b49\uff0c\u4f47\u5217\u5728\u9019\u4e9b\u5834\u666f\u4e2d\u53ef\u4ee5\u6709\u6548\u5730\u7dad\u8b77\u8655\u7406\u9806\u5e8f\u3002</li> </ul>"},{"location":"chapter_stack_and_queue/stack/","title":"5.1 \u00a0 \u5806\u758a","text":"<p>\u5806\u758a\uff08stack\uff09\u662f\u4e00\u7a2e\u9075\u5faa\u5148\u5165\u5f8c\u51fa\u908f\u8f2f\u7684\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\u3002</p> <p>\u6211\u5011\u53ef\u4ee5\u5c07\u5806\u758a\u985e\u6bd4\u70ba\u684c\u9762\u4e0a\u7684\u4e00\u758a\u76e4\u5b50\uff0c\u5982\u679c\u60f3\u53d6\u51fa\u5e95\u90e8\u7684\u76e4\u5b50\uff0c\u5247\u9700\u8981\u5148\u5c07\u4e0a\u9762\u7684\u76e4\u5b50\u4f9d\u6b21\u79fb\u8d70\u3002\u6211\u5011\u5c07\u76e4\u5b50\u66ff\u63db\u70ba\u5404\u7a2e\u578b\u5225\u7684\u5143\u7d20\uff08\u5982\u6574\u6578\u3001\u5b57\u5143\u3001\u7269\u4ef6\u7b49\uff09\uff0c\u5c31\u5f97\u5230\u4e86\u5806\u758a\u9019\u7a2e\u8cc7\u6599\u7d50\u69cb\u3002</p> <p>\u5982\u5716 5-1 \u6240\u793a\uff0c\u6211\u5011\u628a\u5806\u7a4d\u758a\u5143\u7d20\u7684\u9802\u90e8\u7a31\u70ba\u201c\u5806\u758a\u9802\u201d\uff0c\u5e95\u90e8\u7a31\u70ba\u201c\u5806\u758a\u5e95\u201d\u3002\u5c07\u628a\u5143\u7d20\u65b0\u589e\u5230\u5806\u758a\u9802\u7684\u64cd\u4f5c\u53eb\u4f5c\u201c\u5165\u5806\u758a\u201d\uff0c\u522a\u9664\u5806\u758a\u9802\u5143\u7d20\u7684\u64cd\u4f5c\u53eb\u4f5c\u201c\u51fa\u5806\u758a\u201d\u3002</p> <p></p> <p> \u5716 5-1 \u00a0 \u5806\u758a\u7684\u5148\u5165\u5f8c\u51fa\u898f\u5247 </p>"},{"location":"chapter_stack_and_queue/stack/#511","title":"5.1.1 \u00a0 \u5806\u758a\u7684\u5e38\u7528\u64cd\u4f5c","text":"<p>\u5806\u758a\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u8868 5-1 \u6240\u793a\uff0c\u5177\u9ad4\u7684\u65b9\u6cd5\u540d\u9700\u8981\u6839\u64da\u6240\u4f7f\u7528\u7684\u7a0b\u5f0f\u8a9e\u8a00\u4f86\u78ba\u5b9a\u3002\u5728\u6b64\uff0c\u6211\u5011\u4ee5\u5e38\u898b\u7684 <code>push()</code>\u3001<code>pop()</code>\u3001<code>peek()</code> \u547d\u540d\u70ba\u4f8b\u3002</p> <p> \u8868 5-1 \u00a0 \u5806\u758a\u7684\u64cd\u4f5c\u6548\u7387 </p> \u65b9\u6cd5 \u63cf\u8ff0 \u6642\u9593\u8907\u96dc\u5ea6 <code>push()</code> \u5143\u7d20\u5165\u5806\u758a\uff08\u65b0\u589e\u81f3\u5806\u758a\u9802\uff09 \\(O(1)\\) <code>pop()</code> \u5806\u758a\u9802\u5143\u7d20\u51fa\u5806\u758a \\(O(1)\\) <code>peek()</code> \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 \\(O(1)\\) <p>\u901a\u5e38\u60c5\u6cc1\u4e0b\uff0c\u6211\u5011\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7a0b\u5f0f\u8a9e\u8a00\u5167\u5efa\u7684\u5806\u758a\u985e\u5225\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u8a9e\u8a00\u53ef\u80fd\u6c92\u6709\u5c08\u9580\u63d0\u4f9b\u5806\u758a\u985e\u5225\uff0c\u9019\u6642\u6211\u5011\u53ef\u4ee5\u5c07\u8a72\u8a9e\u8a00\u7684\u201c\u9663\u5217\u201d\u6216\u201c\u93c8\u7d50\u4e32\u5217\u201d\u7576\u4f5c\u5806\u758a\u4f86\u4f7f\u7528\uff0c\u4e26\u5728\u7a0b\u5f0f\u908f\u8f2f\u4e0a\u5ffd\u7565\u8207\u5806\u758a\u7121\u95dc\u7684\u64cd\u4f5c\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig stack.py<pre><code># \u521d\u59cb\u5316\u5806\u758a\n# Python \u6c92\u6709\u5167\u5efa\u7684\u5806\u758a\u985e\u5225\uff0c\u53ef\u4ee5\u628a list \u7576\u4f5c\u5806\u758a\u4f86\u4f7f\u7528\nstack: list[int] = []\n\n# \u5143\u7d20\u5165\u5806\u758a\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n# \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20\npeek: int = stack[-1]\n\n# \u5143\u7d20\u51fa\u5806\u758a\npop: int = stack.pop()\n\n# \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6\nsize: int = len(stack)\n\n# \u5224\u65b7\u662f\u5426\u70ba\u7a7a\nis_empty: bool = len(stack) == 0\n</code></pre> stack.cpp<pre><code>/* \u521d\u59cb\u5316\u5806\u758a */\nstack&lt;int&gt; stack;\n\n/* \u5143\u7d20\u5165\u5806\u758a */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\nint top = stack.top();\n\n/* \u5143\u7d20\u51fa\u5806\u758a */\nstack.pop(); // \u7121\u8fd4\u56de\u503c\n\n/* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\nint size = stack.size();\n\n/* \u5224\u65b7\u662f\u5426\u70ba\u7a7a */\nbool empty = stack.empty();\n</code></pre> stack.java<pre><code>/* \u521d\u59cb\u5316\u5806\u758a */\nStack&lt;Integer&gt; stack = new Stack&lt;&gt;();\n\n/* \u5143\u7d20\u5165\u5806\u758a */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\nint peek = stack.peek();\n\n/* \u5143\u7d20\u51fa\u5806\u758a */\nint pop = stack.pop();\n\n/* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\nint size = stack.size();\n\n/* \u5224\u65b7\u662f\u5426\u70ba\u7a7a */\nboolean isEmpty = stack.isEmpty();\n</code></pre> stack.cs<pre><code>/* \u521d\u59cb\u5316\u5806\u758a */\nStack&lt;int&gt; stack = new();\n\n/* \u5143\u7d20\u5165\u5806\u758a */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n\n/* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\nint peek = stack.Peek();\n\n/* \u5143\u7d20\u51fa\u5806\u758a */\nint pop = stack.Pop();\n\n/* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\nint size = stack.Count;\n\n/* \u5224\u65b7\u662f\u5426\u70ba\u7a7a */\nbool isEmpty = stack.Count == 0;\n</code></pre> stack_test.go<pre><code>/* \u521d\u59cb\u5316\u5806\u758a */\n// \u5728 Go \u4e2d\uff0c\u63a8\u85a6\u5c07 Slice \u7576\u4f5c\u5806\u758a\u4f86\u4f7f\u7528\nvar stack []int\n\n/* \u5143\u7d20\u5165\u5806\u758a */\nstack = append(stack, 1)\nstack = append(stack, 3)\nstack = append(stack, 2)\nstack = append(stack, 5)\nstack = append(stack, 4)\n\n/* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\npeek := stack[len(stack)-1]\n\n/* \u5143\u7d20\u51fa\u5806\u758a */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n\n/* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\nsize := len(stack)\n\n/* \u5224\u65b7\u662f\u5426\u70ba\u7a7a */\nisEmpty := len(stack) == 0\n</code></pre> stack.swift<pre><code>/* \u521d\u59cb\u5316\u5806\u758a */\n// Swift \u6c92\u6709\u5167\u5efa\u7684\u5806\u758a\u985e\u5225\uff0c\u53ef\u4ee5\u628a Array \u7576\u4f5c\u5806\u758a\u4f86\u4f7f\u7528\nvar stack: [Int] = []\n\n/* \u5143\u7d20\u5165\u5806\u758a */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n/* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\nlet peek = stack.last!\n\n/* \u5143\u7d20\u51fa\u5806\u758a */\nlet pop = stack.removeLast()\n\n/* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\nlet size = stack.count\n\n/* \u5224\u65b7\u662f\u5426\u70ba\u7a7a */\nlet isEmpty = stack.isEmpty\n</code></pre> stack.js<pre><code>/* \u521d\u59cb\u5316\u5806\u758a */\n// JavaScript \u6c92\u6709\u5167\u5efa\u7684\u5806\u758a\u985e\u5225\uff0c\u53ef\u4ee5\u628a Array \u7576\u4f5c\u5806\u758a\u4f86\u4f7f\u7528\nconst stack = [];\n\n/* \u5143\u7d20\u5165\u5806\u758a */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\nconst peek = stack[stack.length-1];\n\n/* \u5143\u7d20\u51fa\u5806\u758a */\nconst pop = stack.pop();\n\n/* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\nconst size = stack.length;\n\n/* \u5224\u65b7\u662f\u5426\u70ba\u7a7a */\nconst is_empty = stack.length === 0;\n</code></pre> stack.ts<pre><code>/* \u521d\u59cb\u5316\u5806\u758a */\n// TypeScript \u6c92\u6709\u5167\u5efa\u7684\u5806\u758a\u985e\u5225\uff0c\u53ef\u4ee5\u628a Array \u7576\u4f5c\u5806\u758a\u4f86\u4f7f\u7528\nconst stack: number[] = [];\n\n/* \u5143\u7d20\u5165\u5806\u758a */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\nconst peek = stack[stack.length - 1];\n\n/* \u5143\u7d20\u51fa\u5806\u758a */\nconst pop = stack.pop();\n\n/* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\nconst size = stack.length;\n\n/* \u5224\u65b7\u662f\u5426\u70ba\u7a7a */\nconst is_empty = stack.length === 0;\n</code></pre> stack.dart<pre><code>/* \u521d\u59cb\u5316\u5806\u758a */\n// Dart \u6c92\u6709\u5167\u5efa\u7684\u5806\u758a\u985e\u5225\uff0c\u53ef\u4ee5\u628a List \u7576\u4f5c\u5806\u758a\u4f86\u4f7f\u7528\nList&lt;int&gt; stack = [];\n\n/* \u5143\u7d20\u5165\u5806\u758a */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n\n/* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\nint peek = stack.last;\n\n/* \u5143\u7d20\u51fa\u5806\u758a */\nint pop = stack.removeLast();\n\n/* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\nint size = stack.length;\n\n/* \u5224\u65b7\u662f\u5426\u70ba\u7a7a */\nbool isEmpty = stack.isEmpty;\n</code></pre> stack.rs<pre><code>/* \u521d\u59cb\u5316\u5806\u758a */\n// \u628a Vec \u7576\u4f5c\u5806\u758a\u4f86\u4f7f\u7528\nlet mut stack: Vec&lt;i32&gt; = Vec::new();\n\n/* \u5143\u7d20\u5165\u5806\u758a */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\nlet top = stack.last().unwrap();\n\n/* \u5143\u7d20\u51fa\u5806\u758a */\nlet pop = stack.pop().unwrap();\n\n/* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\nlet size = stack.len();\n\n/* \u5224\u65b7\u662f\u5426\u70ba\u7a7a */\nlet is_empty = stack.is_empty();\n</code></pre> stack.c<pre><code>// C \u672a\u63d0\u4f9b\u5167\u5efa\u5806\u758a\n</code></pre> stack.kt<pre><code>/* \u521d\u59cb\u5316\u5806\u758a */\nval stack = Stack&lt;Int&gt;()\n\n/* \u5143\u7d20\u5165\u5806\u758a */\nstack.push(1)\nstack.push(3)\nstack.push(2)\nstack.push(5)\nstack.push(4)\n\n/* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\nval peek = stack.peek()\n\n/* \u5143\u7d20\u51fa\u5806\u758a */\nval pop = stack.pop()\n\n/* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\nval size = stack.size\n\n/* \u5224\u65b7\u662f\u5426\u70ba\u7a7a */\nval isEmpty = stack.isEmpty()\n</code></pre> stack.rb<pre><code># \u521d\u59cb\u5316\u5806\u758a\n# Ruby \u6c92\u6709\u5167\u5efa\u7684\u5806\u758a\u985e\u5225\uff0c\u53ef\u4ee5\u628a Array \u7576\u4f5c\u5806\u758a\u4f86\u4f7f\u7528\nstack = []\n\n# \u5143\u7d20\u5165\u5806\u758a\nstack &lt;&lt; 1\nstack &lt;&lt; 3\nstack &lt;&lt; 2\nstack &lt;&lt; 5\nstack &lt;&lt; 4\n\n# \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20\npeek = stack.last\n\n# \u5143\u7d20\u51fa\u5806\u758a\npop = stack.pop\n\n# \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6\nsize = stack.length\n\n# \u5224\u65b7\u662f\u5426\u70ba\u7a7a\nis_empty = stack.empty?\n</code></pre> stack.zig<pre><code>\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_stack_and_queue/stack/#512","title":"5.1.2 \u00a0 \u5806\u758a\u7684\u5be6\u73fe","text":"<p>\u70ba\u4e86\u6df1\u5165\u77ad\u89e3\u5806\u758a\u7684\u57f7\u884c\u6a5f\u5236\uff0c\u6211\u5011\u4f86\u5617\u8a66\u81ea\u5df1\u5be6\u73fe\u4e00\u500b\u5806\u758a\u985e\u5225\u3002</p> <p>\u5806\u758a\u9075\u5faa\u5148\u5165\u5f8c\u51fa\u7684\u539f\u5247\uff0c\u56e0\u6b64\u6211\u5011\u53ea\u80fd\u5728\u5806\u758a\u9802\u65b0\u589e\u6216\u522a\u9664\u5143\u7d20\u3002\u7136\u800c\uff0c\u9663\u5217\u548c\u93c8\u7d50\u4e32\u5217\u90fd\u53ef\u4ee5\u5728\u4efb\u610f\u4f4d\u7f6e\u65b0\u589e\u548c\u522a\u9664\u5143\u7d20\uff0c\u56e0\u6b64\u5806\u758a\u53ef\u4ee5\u8996\u70ba\u4e00\u7a2e\u53d7\u9650\u5236\u7684\u9663\u5217\u6216\u93c8\u7d50\u4e32\u5217\u3002\u63db\u53e5\u8a71\u8aaa\uff0c\u6211\u5011\u53ef\u4ee5\u201c\u906e\u853d\u201d\u9663\u5217\u6216\u93c8\u7d50\u4e32\u5217\u7684\u90e8\u5206\u7121\u95dc\u64cd\u4f5c\uff0c\u4f7f\u5176\u5c0d\u5916\u8868\u73fe\u7684\u908f\u8f2f\u7b26\u5408\u5806\u758a\u7684\u7279\u6027\u3002</p>"},{"location":"chapter_stack_and_queue/stack/#1","title":"1. \u00a0 \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u7684\u5be6\u73fe","text":"<p>\u4f7f\u7528\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u5806\u758a\u6642\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u93c8\u7d50\u4e32\u5217\u7684\u982d\u7bc0\u9ede\u8996\u70ba\u5806\u758a\u9802\uff0c\u5c3e\u7bc0\u9ede\u8996\u70ba\u5806\u758a\u5e95\u3002</p> <p>\u5982\u5716 5-2 \u6240\u793a\uff0c\u5c0d\u65bc\u5165\u5806\u758a\u64cd\u4f5c\uff0c\u6211\u5011\u53ea\u9700\u5c07\u5143\u7d20\u63d2\u5165\u93c8\u7d50\u4e32\u5217\u982d\u90e8\uff0c\u9019\u7a2e\u7bc0\u9ede\u63d2\u5165\u65b9\u6cd5\u88ab\u7a31\u70ba\u201c\u982d\u63d2\u6cd5\u201d\u3002\u800c\u5c0d\u65bc\u51fa\u5806\u758a\u64cd\u4f5c\uff0c\u53ea\u9700\u5c07\u982d\u7bc0\u9ede\u5f9e\u93c8\u7d50\u4e32\u5217\u4e2d\u522a\u9664\u5373\u53ef\u3002</p> LinkedListStackpush()pop() <p></p> <p></p> <p></p> <p> \u5716 5-2 \u00a0 \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u5806\u758a\u7684\u5165\u5806\u758a\u51fa\u5806\u758a\u64cd\u4f5c </p> <p>\u4ee5\u4e0b\u662f\u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u5806\u758a\u7684\u793a\u4f8b\u7a0b\u5f0f\u78bc\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linkedlist_stack.py<pre><code>class LinkedListStack:\n \"\"\"\u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a\"\"\"\n\n def __init__(self):\n \"\"\"\u5efa\u69cb\u5b50\"\"\"\n self._peek: ListNode | None = None\n self._size: int = 0\n\n def size(self) -&gt; int:\n \"\"\"\u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -&gt; bool:\n \"\"\"\u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a\"\"\"\n return self._size == 0\n\n def push(self, val: int):\n \"\"\"\u5165\u5806\u758a\"\"\"\n node = ListNode(val)\n node.next = self._peek\n self._peek = node\n self._size += 1\n\n def pop(self) -&gt; int:\n \"\"\"\u51fa\u5806\u758a\"\"\"\n num = self.peek()\n self._peek = self._peek.next\n self._size -= 1\n return num\n\n def peek(self) -&gt; int:\n \"\"\"\u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u5806\u758a\u70ba\u7a7a\")\n return self._peek.val\n\n def to_list(self) -&gt; list[int]:\n \"\"\"\u8f49\u5316\u70ba\u4e32\u5217\u7528\u65bc\u5217\u5370\"\"\"\n arr = []\n node = self._peek\n while node:\n arr.append(node.val)\n node = node.next\n arr.reverse()\n return arr\n</code></pre> linkedlist_stack.cpp<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass LinkedListStack {\n private:\n ListNode *stackTop; // \u5c07\u982d\u7bc0\u9ede\u4f5c\u70ba\u5806\u758a\u9802\n int stkSize; // \u5806\u758a\u7684\u9577\u5ea6\n\n public:\n LinkedListStack() {\n stackTop = nullptr;\n stkSize = 0;\n }\n\n ~LinkedListStack() {\n // \u8d70\u8a2a\u93c8\u7d50\u4e32\u5217\u522a\u9664\u7bc0\u9ede\uff0c\u91cb\u653e\u8a18\u61b6\u9ad4\n freeMemoryLinkedList(stackTop);\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n int size() {\n return stkSize;\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u5806\u758a */\n void push(int num) {\n ListNode *node = new ListNode(num);\n node-&gt;next = stackTop;\n stackTop = node;\n stkSize++;\n }\n\n /* \u51fa\u5806\u758a */\n int pop() {\n int num = top();\n ListNode *tmp = stackTop;\n stackTop = stackTop-&gt;next;\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n delete tmp;\n stkSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n int top() {\n if (isEmpty())\n throw out_of_range(\"\u5806\u758a\u70ba\u7a7a\");\n return stackTop-&gt;val;\n }\n\n /* \u5c07 List \u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n vector&lt;int&gt; toVector() {\n ListNode *node = stackTop;\n vector&lt;int&gt; res(size());\n for (int i = res.size() - 1; i &gt;= 0; i--) {\n res[i] = node-&gt;val;\n node = node-&gt;next;\n }\n return res;\n }\n};\n</code></pre> linkedlist_stack.java<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass LinkedListStack {\n private ListNode stackPeek; // \u5c07\u982d\u7bc0\u9ede\u4f5c\u70ba\u5806\u758a\u9802\n private int stkSize = 0; // \u5806\u758a\u7684\u9577\u5ea6\n\n public LinkedListStack() {\n stackPeek = null;\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n public int size() {\n return stkSize;\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u5806\u758a */\n public void push(int num) {\n ListNode node = new ListNode(num);\n node.next = stackPeek;\n stackPeek = node;\n stkSize++;\n }\n\n /* \u51fa\u5806\u758a */\n public int pop() {\n int num = peek();\n stackPeek = stackPeek.next;\n stkSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stackPeek.val;\n }\n\n /* \u5c07 List \u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n public int[] toArray() {\n ListNode node = stackPeek;\n int[] res = new int[size()];\n for (int i = res.length - 1; i &gt;= 0; i--) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n</code></pre> linkedlist_stack.cs<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass LinkedListStack {\n ListNode? stackPeek; // \u5c07\u982d\u7bc0\u9ede\u4f5c\u70ba\u5806\u758a\u9802\n int stkSize = 0; // \u5806\u758a\u7684\u9577\u5ea6\n\n public LinkedListStack() {\n stackPeek = null;\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n public int Size() {\n return stkSize;\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u5806\u758a */\n public void Push(int num) {\n ListNode node = new(num) {\n next = stackPeek\n };\n stackPeek = node;\n stkSize++;\n }\n\n /* \u51fa\u5806\u758a */\n public int Pop() {\n int num = Peek();\n stackPeek = stackPeek!.next;\n stkSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return stackPeek!.val;\n }\n\n /* \u5c07 List \u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n public int[] ToArray() {\n if (stackPeek == null)\n return [];\n\n ListNode? node = stackPeek;\n int[] res = new int[Size()];\n for (int i = res.Length - 1; i &gt;= 0; i--) {\n res[i] = node!.val;\n node = node.next;\n }\n return res;\n }\n}\n</code></pre> linkedlist_stack.go<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a */\ntype linkedListStack struct {\n // \u4f7f\u7528\u5167\u5efa\u5305 list \u4f86\u5be6\u73fe\u5806\u758a\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u5806\u758a */\nfunc newLinkedListStack() *linkedListStack {\n return &amp;linkedListStack{\n data: list.New(),\n }\n}\n\n/* \u5165\u5806\u758a */\nfunc (s *linkedListStack) push(value int) {\n s.data.PushBack(value)\n}\n\n/* \u51fa\u5806\u758a */\nfunc (s *linkedListStack) pop() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\nfunc (s *linkedListStack) peek() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n return e.Value\n}\n\n/* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\nfunc (s *linkedListStack) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\nfunc (s *linkedListStack) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u7372\u53d6 List \u7528\u65bc\u5217\u5370 */\nfunc (s *linkedListStack) toList() *list.List {\n return s.data\n}\n</code></pre> linkedlist_stack.swift<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass LinkedListStack {\n private var _peek: ListNode? // \u5c07\u982d\u7bc0\u9ede\u4f5c\u70ba\u5806\u758a\u9802\n private var _size: Int // \u5806\u758a\u7684\u9577\u5ea6\n\n init() {\n _size = 0\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n func size() -&gt; Int {\n _size\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n func isEmpty() -&gt; Bool {\n size() == 0\n }\n\n /* \u5165\u5806\u758a */\n func push(num: Int) {\n let node = ListNode(x: num)\n node.next = _peek\n _peek = node\n _size += 1\n }\n\n /* \u51fa\u5806\u758a */\n @discardableResult\n func pop() -&gt; Int {\n let num = peek()\n _peek = _peek?.next\n _size -= 1\n return num\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n func peek() -&gt; Int {\n if isEmpty() {\n fatalError(\"\u5806\u758a\u70ba\u7a7a\")\n }\n return _peek!.val\n }\n\n /* \u5c07 List \u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n func toArray() -&gt; [Int] {\n var node = _peek\n var res = Array(repeating: 0, count: size())\n for i in res.indices.reversed() {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n</code></pre> linkedlist_stack.js<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass LinkedListStack {\n #stackPeek; // \u5c07\u982d\u7bc0\u9ede\u4f5c\u70ba\u5806\u758a\u9802\n #stkSize = 0; // \u5806\u758a\u7684\u9577\u5ea6\n\n constructor() {\n this.#stackPeek = null;\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n get size() {\n return this.#stkSize;\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n isEmpty() {\n return this.size === 0;\n }\n\n /* \u5165\u5806\u758a */\n push(num) {\n const node = new ListNode(num);\n node.next = this.#stackPeek;\n this.#stackPeek = node;\n this.#stkSize++;\n }\n\n /* \u51fa\u5806\u758a */\n pop() {\n const num = this.peek();\n this.#stackPeek = this.#stackPeek.next;\n this.#stkSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n peek() {\n if (!this.#stackPeek) throw new Error('\u5806\u758a\u70ba\u7a7a');\n return this.#stackPeek.val;\n }\n\n /* \u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n toArray() {\n let node = this.#stackPeek;\n const res = new Array(this.size);\n for (let i = res.length - 1; i &gt;= 0; i--) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n</code></pre> linkedlist_stack.ts<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass LinkedListStack {\n private stackPeek: ListNode | null; // \u5c07\u982d\u7bc0\u9ede\u4f5c\u70ba\u5806\u758a\u9802\n private stkSize: number = 0; // \u5806\u758a\u7684\u9577\u5ea6\n\n constructor() {\n this.stackPeek = null;\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n get size(): number {\n return this.stkSize;\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n isEmpty(): boolean {\n return this.size === 0;\n }\n\n /* \u5165\u5806\u758a */\n push(num: number): void {\n const node = new ListNode(num);\n node.next = this.stackPeek;\n this.stackPeek = node;\n this.stkSize++;\n }\n\n /* \u51fa\u5806\u758a */\n pop(): number {\n const num = this.peek();\n if (!this.stackPeek) throw new Error('\u5806\u758a\u70ba\u7a7a');\n this.stackPeek = this.stackPeek.next;\n this.stkSize--;\n return num;\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n peek(): number {\n if (!this.stackPeek) throw new Error('\u5806\u758a\u70ba\u7a7a');\n return this.stackPeek.val;\n }\n\n /* \u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n toArray(): number[] {\n let node = this.stackPeek;\n const res = new Array&lt;number&gt;(this.size);\n for (let i = res.length - 1; i &gt;= 0; i--) {\n res[i] = node!.val;\n node = node!.next;\n }\n return res;\n }\n}\n</code></pre> linkedlist_stack.dart<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u985e\u5225\u5be6\u73fe\u7684\u5806\u758a */\nclass LinkedListStack {\n ListNode? _stackPeek; // \u5c07\u982d\u7bc0\u9ede\u4f5c\u70ba\u5806\u758a\u9802\n int _stkSize = 0; // \u5806\u758a\u7684\u9577\u5ea6\n\n LinkedListStack() {\n _stackPeek = null;\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n int size() {\n return _stkSize;\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n bool isEmpty() {\n return _stkSize == 0;\n }\n\n /* \u5165\u5806\u758a */\n void push(int _num) {\n final ListNode node = ListNode(_num);\n node.next = _stackPeek;\n _stackPeek = node;\n _stkSize++;\n }\n\n /* \u51fa\u5806\u758a */\n int pop() {\n final int _num = peek();\n _stackPeek = _stackPeek!.next;\n _stkSize--;\n return _num;\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n int peek() {\n if (_stackPeek == null) {\n throw Exception(\"\u5806\u758a\u70ba\u7a7a\");\n }\n return _stackPeek!.val;\n }\n\n /* \u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u5316\u70ba List \u4e26\u8fd4\u56de */\n List&lt;int&gt; toList() {\n ListNode? node = _stackPeek;\n List&lt;int&gt; list = [];\n while (node != null) {\n list.add(node.val);\n node = node.next;\n }\n list = list.reversed.toList();\n return list;\n }\n}\n</code></pre> linkedlist_stack.rs<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a */\n#[allow(dead_code)]\npub struct LinkedListStack&lt;T&gt; {\n stack_peek: Option&lt;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;, // \u5c07\u982d\u7bc0\u9ede\u4f5c\u70ba\u5806\u758a\u9802\n stk_size: usize, // \u5806\u758a\u7684\u9577\u5ea6\n}\n\nimpl&lt;T: Copy&gt; LinkedListStack&lt;T&gt; {\n pub fn new() -&gt; Self {\n Self {\n stack_peek: None,\n stk_size: 0,\n }\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n pub fn size(&amp;self) -&gt; usize {\n return self.stk_size;\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n pub fn is_empty(&amp;self) -&gt; bool {\n return self.size() == 0;\n }\n\n /* \u5165\u5806\u758a */\n pub fn push(&amp;mut self, num: T) {\n let node = ListNode::new(num);\n node.borrow_mut().next = self.stack_peek.take();\n self.stack_peek = Some(node);\n self.stk_size += 1;\n }\n\n /* \u51fa\u5806\u758a */\n pub fn pop(&amp;mut self) -&gt; Option&lt;T&gt; {\n self.stack_peek.take().map(|old_head| {\n match old_head.borrow_mut().next.take() {\n Some(new_head) =&gt; {\n self.stack_peek = Some(new_head);\n }\n None =&gt; {\n self.stack_peek = None;\n }\n }\n self.stk_size -= 1;\n Rc::try_unwrap(old_head).ok().unwrap().into_inner().val\n })\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n pub fn peek(&amp;self) -&gt; Option&lt;&amp;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt; {\n self.stack_peek.as_ref()\n }\n\n /* \u5c07 List \u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n pub fn to_array(&amp;self, head: Option&lt;&amp;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;) -&gt; Vec&lt;T&gt; {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.push(node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n</code></pre> linkedlist_stack.c<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a */\ntypedef struct {\n ListNode *top; // \u5c07\u982d\u7bc0\u9ede\u4f5c\u70ba\u5806\u758a\u9802\n int size; // \u5806\u758a\u7684\u9577\u5ea6\n} LinkedListStack;\n\n/* \u5efa\u69cb\u5b50 */\nLinkedListStack *newLinkedListStack() {\n LinkedListStack *s = malloc(sizeof(LinkedListStack));\n s-&gt;top = NULL;\n s-&gt;size = 0;\n return s;\n}\n\n/* \u6790\u69cb\u51fd\u5f0f */\nvoid delLinkedListStack(LinkedListStack *s) {\n while (s-&gt;top) {\n ListNode *n = s-&gt;top-&gt;next;\n free(s-&gt;top);\n s-&gt;top = n;\n }\n free(s);\n}\n\n/* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\nint size(LinkedListStack *s) {\n return s-&gt;size;\n}\n\n/* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\nbool isEmpty(LinkedListStack *s) {\n return size(s) == 0;\n}\n\n/* \u5165\u5806\u758a */\nvoid push(LinkedListStack *s, int num) {\n ListNode *node = (ListNode *)malloc(sizeof(ListNode));\n node-&gt;next = s-&gt;top; // \u66f4\u65b0\u65b0\u52a0\u7bc0\u9ede\u6307\u6a19\u57df\n node-&gt;val = num; // \u66f4\u65b0\u65b0\u52a0\u7bc0\u9ede\u8cc7\u6599\u57df\n s-&gt;top = node; // \u66f4\u65b0\u5806\u758a\u9802\n s-&gt;size++; // \u66f4\u65b0\u5806\u758a\u5927\u5c0f\n}\n\n/* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\nint peek(LinkedListStack *s) {\n if (s-&gt;size == 0) {\n printf(\"\u5806\u758a\u70ba\u7a7a\\n\");\n return INT_MAX;\n }\n return s-&gt;top-&gt;val;\n}\n\n/* \u51fa\u5806\u758a */\nint pop(LinkedListStack *s) {\n int val = peek(s);\n ListNode *tmp = s-&gt;top;\n s-&gt;top = s-&gt;top-&gt;next;\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(tmp);\n s-&gt;size--;\n return val;\n}\n</code></pre> linkedlist_stack.kt<pre><code>/* \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass LinkedListStack(\n private var stackPeek: ListNode? = null, // \u5c07\u982d\u7bc0\u9ede\u4f5c\u70ba\u5806\u758a\u9802\n private var stkSize: Int = 0 // \u5806\u758a\u7684\u9577\u5ea6\n) {\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n fun size(): Int {\n return stkSize\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n fun isEmpty(): Boolean {\n return size() == 0\n }\n\n /* \u5165\u5806\u758a */\n fun push(num: Int) {\n val node = ListNode(num)\n node.next = stackPeek\n stackPeek = node\n stkSize++\n }\n\n /* \u51fa\u5806\u758a */\n fun pop(): Int? {\n val num = peek()\n stackPeek = stackPeek?.next\n stkSize--\n return num\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n fun peek(): Int? {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return stackPeek?._val\n }\n\n /* \u5c07 List \u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n fun toArray(): IntArray {\n var node = stackPeek\n val res = IntArray(size())\n for (i in res.size - 1 downTo 0) {\n res[i] = node?._val!!\n node = node.next\n }\n return res\n }\n}\n</code></pre> linkedlist_stack.rb<pre><code>### \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a ###\nclass LinkedListStack\n attr_reader :size\n\n ### \u5efa\u69cb\u5b50 ###\n def initialize\n @size = 0\n end\n\n ### \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a ###\n def is_empty?\n @peek.nil?\n end\n\n ### \u5165\u5806\u758a ###\n def push(val)\n node = ListNode.new(val)\n node.next = @peek\n @peek = node\n @size += 1\n end\n\n ### \u51fa\u5806\u758a ###\n def pop\n num = peek\n @peek = @peek.next\n @size -= 1\n num\n end\n\n ### \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 ###\n def peek\n raise IndexError, '\u5806\u758a\u70ba\u7a7a' if is_empty?\n\n @peek.val\n end\n\n ### \u5c07\u93c8\u7d50\u4e32\u5217\u8f49\u5316\u70ba Array \u4e26\u53cd\u56de ###\n def to_array\n arr = []\n node = @peek\n while node\n arr &lt;&lt; node.val\n node = node.next\n end\n arr.reverse\n end\nend\n</code></pre> linkedlist_stack.zig<pre><code>// \u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a\nfn LinkedListStack(comptime T: type) type {\n return struct {\n const Self = @This();\n\n stack_top: ?*inc.ListNode(T) = null, // \u5c07\u982d\u7bc0\u9ede\u4f5c\u70ba\u5806\u758a\u9802\n stk_size: usize = 0, // \u5806\u758a\u7684\u9577\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u8a18\u61b6\u9ad4\u5206\u914d\u5668\n\n // \u5efa\u69cb\u5b50\uff08\u5206\u914d\u8a18\u61b6\u9ad4+\u521d\u59cb\u5316\u5806\u758a\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.stack_top = null;\n self.stk_size = 0;\n }\n\n // \u6790\u69cb\u51fd\u5f0f\uff08\u91cb\u653e\u8a18\u61b6\u9ad4\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6\n pub fn size(self: *Self) usize {\n return self.stk_size;\n }\n\n // \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.size() == 0) @panic(\"\u5806\u758a\u70ba\u7a7a\");\n return self.stack_top.?.val;\n } \n\n // \u5165\u5806\u758a\n pub fn push(self: *Self, num: T) !void {\n var node = try self.mem_allocator.create(inc.ListNode(T));\n node.init(num);\n node.next = self.stack_top;\n self.stack_top = node;\n self.stk_size += 1;\n } \n\n // \u51fa\u5806\u758a\n pub fn pop(self: *Self) T {\n var num = self.peek();\n self.stack_top = self.stack_top.?.next;\n self.stk_size -= 1;\n return num;\n } \n\n // \u5c07\u5806\u758a\u8f49\u63db\u70ba\u9663\u5217\n pub fn toArray(self: *Self) ![]T {\n var node = self.stack_top;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i &lt; res.len) : (i += 1) {\n res[res.len - i - 1] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_stack_and_queue/stack/#2","title":"2. \u00a0 \u57fa\u65bc\u9663\u5217\u7684\u5be6\u73fe","text":"<p>\u4f7f\u7528\u9663\u5217\u5be6\u73fe\u5806\u758a\u6642\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u9663\u5217\u7684\u5c3e\u90e8\u4f5c\u70ba\u5806\u758a\u9802\u3002\u5982\u5716 5-3 \u6240\u793a\uff0c\u5165\u5806\u758a\u8207\u51fa\u5806\u758a\u64cd\u4f5c\u5206\u5225\u5c0d\u61c9\u5728\u9663\u5217\u5c3e\u90e8\u65b0\u589e\u5143\u7d20\u8207\u522a\u9664\u5143\u7d20\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u90fd\u70ba \\(O(1)\\) \u3002</p> ArrayStackpush()pop() <p></p> <p></p> <p></p> <p> \u5716 5-3 \u00a0 \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u5806\u758a\u7684\u5165\u5806\u758a\u51fa\u5806\u758a\u64cd\u4f5c </p> <p>\u7531\u65bc\u5165\u5806\u758a\u7684\u5143\u7d20\u53ef\u80fd\u6703\u6e90\u6e90\u4e0d\u65b7\u5730\u589e\u52a0\uff0c\u56e0\u6b64\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u52d5\u614b\u9663\u5217\uff0c\u9019\u6a23\u5c31\u7121\u9808\u81ea\u884c\u8655\u7406\u9663\u5217\u64f4\u5bb9\u554f\u984c\u3002\u4ee5\u4e0b\u70ba\u793a\u4f8b\u7a0b\u5f0f\u78bc\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array_stack.py<pre><code>class ArrayStack:\n \"\"\"\u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a\"\"\"\n\n def __init__(self):\n \"\"\"\u5efa\u69cb\u5b50\"\"\"\n self._stack: list[int] = []\n\n def size(self) -&gt; int:\n \"\"\"\u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6\"\"\"\n return len(self._stack)\n\n def is_empty(self) -&gt; bool:\n \"\"\"\u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a\"\"\"\n return self.size() == 0\n\n def push(self, item: int):\n \"\"\"\u5165\u5806\u758a\"\"\"\n self._stack.append(item)\n\n def pop(self) -&gt; int:\n \"\"\"\u51fa\u5806\u758a\"\"\"\n if self.is_empty():\n raise IndexError(\"\u5806\u758a\u70ba\u7a7a\")\n return self._stack.pop()\n\n def peek(self) -&gt; int:\n \"\"\"\u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u5806\u758a\u70ba\u7a7a\")\n return self._stack[-1]\n\n def to_list(self) -&gt; list[int]:\n \"\"\"\u8fd4\u56de\u4e32\u5217\u7528\u65bc\u5217\u5370\"\"\"\n return self._stack\n</code></pre> array_stack.cpp<pre><code>/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass ArrayStack {\n private:\n vector&lt;int&gt; stack;\n\n public:\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n int size() {\n return stack.size();\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n bool isEmpty() {\n return stack.size() == 0;\n }\n\n /* \u5165\u5806\u758a */\n void push(int num) {\n stack.push_back(num);\n }\n\n /* \u51fa\u5806\u758a */\n int pop() {\n int num = top();\n stack.pop_back();\n return num;\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n int top() {\n if (isEmpty())\n throw out_of_range(\"\u5806\u758a\u70ba\u7a7a\");\n return stack.back();\n }\n\n /* \u8fd4\u56de Vector */\n vector&lt;int&gt; toVector() {\n return stack;\n }\n};\n</code></pre> array_stack.java<pre><code>/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass ArrayStack {\n private ArrayList&lt;Integer&gt; stack;\n\n public ArrayStack() {\n // \u521d\u59cb\u5316\u4e32\u5217\uff08\u52d5\u614b\u9663\u5217\uff09\n stack = new ArrayList&lt;&gt;();\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n public int size() {\n return stack.size();\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u5806\u758a */\n public void push(int num) {\n stack.add(num);\n }\n\n /* \u51fa\u5806\u758a */\n public int pop() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stack.remove(size() - 1);\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stack.get(size() - 1);\n }\n\n /* \u5c07 List \u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n public Object[] toArray() {\n return stack.toArray();\n }\n}\n</code></pre> array_stack.cs<pre><code>/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass ArrayStack {\n List&lt;int&gt; stack;\n public ArrayStack() {\n // \u521d\u59cb\u5316\u4e32\u5217\uff08\u52d5\u614b\u9663\u5217\uff09\n stack = [];\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n public int Size() {\n return stack.Count;\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u5806\u758a */\n public void Push(int num) {\n stack.Add(num);\n }\n\n /* \u51fa\u5806\u758a */\n public int Pop() {\n if (IsEmpty())\n throw new Exception();\n var val = Peek();\n stack.RemoveAt(Size() - 1);\n return val;\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return stack[Size() - 1];\n }\n\n /* \u5c07 List \u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n public int[] ToArray() {\n return [.. stack];\n }\n}\n</code></pre> array_stack.go<pre><code>/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a */\ntype arrayStack struct {\n data []int // \u8cc7\u6599\n}\n\n/* \u521d\u59cb\u5316\u5806\u758a */\nfunc newArrayStack() *arrayStack {\n return &amp;arrayStack{\n // \u8a2d\u5b9a\u5806\u758a\u7684\u9577\u5ea6\u70ba 0\uff0c\u5bb9\u91cf\u70ba 16\n data: make([]int, 0, 16),\n }\n}\n\n/* \u5806\u758a\u7684\u9577\u5ea6 */\nfunc (s *arrayStack) size() int {\n return len(s.data)\n}\n\n/* \u5806\u758a\u662f\u5426\u70ba\u7a7a */\nfunc (s *arrayStack) isEmpty() bool {\n return s.size() == 0\n}\n\n/* \u5165\u5806\u758a */\nfunc (s *arrayStack) push(v int) {\n // \u5207\u7247\u6703\u81ea\u52d5\u64f4\u5bb9\n s.data = append(s.data, v)\n}\n\n/* \u51fa\u5806\u758a */\nfunc (s *arrayStack) pop() any {\n val := s.peek()\n s.data = s.data[:len(s.data)-1]\n return val\n}\n\n/* \u7372\u53d6\u5806\u758a\u9802\u5143\u7d20 */\nfunc (s *arrayStack) peek() any {\n if s.isEmpty() {\n return nil\n }\n val := s.data[len(s.data)-1]\n return val\n}\n\n/* \u7372\u53d6 Slice \u7528\u65bc\u5217\u5370 */\nfunc (s *arrayStack) toSlice() []int {\n return s.data\n}\n</code></pre> array_stack.swift<pre><code>/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass ArrayStack {\n private var stack: [Int]\n\n init() {\n // \u521d\u59cb\u5316\u4e32\u5217\uff08\u52d5\u614b\u9663\u5217\uff09\n stack = []\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n func size() -&gt; Int {\n stack.count\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n func isEmpty() -&gt; Bool {\n stack.isEmpty\n }\n\n /* \u5165\u5806\u758a */\n func push(num: Int) {\n stack.append(num)\n }\n\n /* \u51fa\u5806\u758a */\n @discardableResult\n func pop() -&gt; Int {\n if isEmpty() {\n fatalError(\"\u5806\u758a\u70ba\u7a7a\")\n }\n return stack.removeLast()\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n func peek() -&gt; Int {\n if isEmpty() {\n fatalError(\"\u5806\u758a\u70ba\u7a7a\")\n }\n return stack.last!\n }\n\n /* \u5c07 List \u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n func toArray() -&gt; [Int] {\n stack\n }\n}\n</code></pre> array_stack.js<pre><code>/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass ArrayStack {\n #stack;\n constructor() {\n this.#stack = [];\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n get size() {\n return this.#stack.length;\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n isEmpty() {\n return this.#stack.length === 0;\n }\n\n /* \u5165\u5806\u758a */\n push(num) {\n this.#stack.push(num);\n }\n\n /* \u51fa\u5806\u758a */\n pop() {\n if (this.isEmpty()) throw new Error('\u5806\u758a\u70ba\u7a7a');\n return this.#stack.pop();\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n top() {\n if (this.isEmpty()) throw new Error('\u5806\u758a\u70ba\u7a7a');\n return this.#stack[this.#stack.length - 1];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n return this.#stack;\n }\n}\n</code></pre> array_stack.ts<pre><code>/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass ArrayStack {\n private stack: number[];\n constructor() {\n this.stack = [];\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n get size(): number {\n return this.stack.length;\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n isEmpty(): boolean {\n return this.stack.length === 0;\n }\n\n /* \u5165\u5806\u758a */\n push(num: number): void {\n this.stack.push(num);\n }\n\n /* \u51fa\u5806\u758a */\n pop(): number | undefined {\n if (this.isEmpty()) throw new Error('\u5806\u758a\u70ba\u7a7a');\n return this.stack.pop();\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n top(): number | undefined {\n if (this.isEmpty()) throw new Error('\u5806\u758a\u70ba\u7a7a');\n return this.stack[this.stack.length - 1];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n return this.stack;\n }\n}\n</code></pre> array_stack.dart<pre><code>/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass ArrayStack {\n late List&lt;int&gt; _stack;\n ArrayStack() {\n _stack = [];\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n int size() {\n return _stack.length;\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n bool isEmpty() {\n return _stack.isEmpty;\n }\n\n /* \u5165\u5806\u758a */\n void push(int _num) {\n _stack.add(_num);\n }\n\n /* \u51fa\u5806\u758a */\n int pop() {\n if (isEmpty()) {\n throw Exception(\"\u5806\u758a\u70ba\u7a7a\");\n }\n return _stack.removeLast();\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n int peek() {\n if (isEmpty()) {\n throw Exception(\"\u5806\u758a\u70ba\u7a7a\");\n }\n return _stack.last;\n }\n\n /* \u5c07\u5806\u758a\u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n List&lt;int&gt; toArray() =&gt; _stack;\n}\n</code></pre> array_stack.rs<pre><code>/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a */\nstruct ArrayStack&lt;T&gt; {\n stack: Vec&lt;T&gt;,\n}\n\nimpl&lt;T&gt; ArrayStack&lt;T&gt; {\n /* \u521d\u59cb\u5316\u5806\u758a */\n fn new() -&gt; ArrayStack&lt;T&gt; {\n ArrayStack::&lt;T&gt; {\n stack: Vec::&lt;T&gt;::new(),\n }\n }\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n fn size(&amp;self) -&gt; usize {\n self.stack.len()\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n fn is_empty(&amp;self) -&gt; bool {\n self.size() == 0\n }\n\n /* \u5165\u5806\u758a */\n fn push(&amp;mut self, num: T) {\n self.stack.push(num);\n }\n\n /* \u51fa\u5806\u758a */\n fn pop(&amp;mut self) -&gt; Option&lt;T&gt; {\n self.stack.pop()\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n fn peek(&amp;self) -&gt; Option&lt;&amp;T&gt; {\n if self.is_empty() {\n panic!(\"\u5806\u758a\u70ba\u7a7a\")\n };\n self.stack.last()\n }\n\n /* \u8fd4\u56de &amp;Vec */\n fn to_array(&amp;self) -&gt; &amp;Vec&lt;T&gt; {\n &amp;self.stack\n }\n}\n</code></pre> array_stack.c<pre><code>/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a */\ntypedef struct {\n int *data;\n int size;\n} ArrayStack;\n\n/* \u5efa\u69cb\u5b50 */\nArrayStack *newArrayStack() {\n ArrayStack *stack = malloc(sizeof(ArrayStack));\n // \u521d\u59cb\u5316\u4e00\u500b\u5927\u5bb9\u91cf\uff0c\u907f\u514d\u64f4\u5bb9\n stack-&gt;data = malloc(sizeof(int) * MAX_SIZE);\n stack-&gt;size = 0;\n return stack;\n}\n\n/* \u6790\u69cb\u51fd\u5f0f */\nvoid delArrayStack(ArrayStack *stack) {\n free(stack-&gt;data);\n free(stack);\n}\n\n/* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\nint size(ArrayStack *stack) {\n return stack-&gt;size;\n}\n\n/* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\nbool isEmpty(ArrayStack *stack) {\n return stack-&gt;size == 0;\n}\n\n/* \u5165\u5806\u758a */\nvoid push(ArrayStack *stack, int num) {\n if (stack-&gt;size == MAX_SIZE) {\n printf(\"\u5806\u758a\u5df2\u6eff\\n\");\n return;\n }\n stack-&gt;data[stack-&gt;size] = num;\n stack-&gt;size++;\n}\n\n/* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\nint peek(ArrayStack *stack) {\n if (stack-&gt;size == 0) {\n printf(\"\u5806\u758a\u70ba\u7a7a\\n\");\n return INT_MAX;\n }\n return stack-&gt;data[stack-&gt;size - 1];\n}\n\n/* \u51fa\u5806\u758a */\nint pop(ArrayStack *stack) {\n int val = peek(stack);\n stack-&gt;size--;\n return val;\n}\n</code></pre> array_stack.kt<pre><code>/* \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a */\nclass ArrayStack {\n // \u521d\u59cb\u5316\u4e32\u5217\uff08\u52d5\u614b\u9663\u5217\uff09\n private val stack = mutableListOf&lt;Int&gt;()\n\n /* \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 */\n fun size(): Int {\n return stack.size\n }\n\n /* \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a */\n fun isEmpty(): Boolean {\n return size() == 0\n }\n\n /* \u5165\u5806\u758a */\n fun push(num: Int) {\n stack.add(num)\n }\n\n /* \u51fa\u5806\u758a */\n fun pop(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return stack.removeAt(size() - 1)\n }\n\n /* \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 */\n fun peek(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return stack[size() - 1]\n }\n\n /* \u5c07 List \u8f49\u5316\u70ba Array \u4e26\u8fd4\u56de */\n fun toArray(): Array&lt;Any&gt; {\n return stack.toTypedArray()\n }\n}\n</code></pre> array_stack.rb<pre><code>### \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a ###\nclass ArrayStack\n ### \u5efa\u69cb\u5b50 ###\n def initialize\n @stack = []\n end\n\n ### \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6 ###\n def size\n @stack.length\n end\n\n ### \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a ###\n def is_empty?\n @stack.empty?\n end\n\n ### \u5165\u5806\u758a ###\n def push(item)\n @stack &lt;&lt; item\n end\n\n ### \u51fa\u5806\u758a ###\n def pop\n raise IndexError, '\u5806\u758a\u70ba\u7a7a' if is_empty?\n\n @stack.pop\n end\n\n ### \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20 ###\n def peek\n raise IndexError, '\u5806\u758a\u70ba\u7a7a' if is_empty?\n\n @stack.last\n end\n\n ### \u8fd4\u56de\u4e32\u5217\u7528\u65bc\u5217\u5370 ###\n def to_array\n @stack\n end\nend\n</code></pre> array_stack.zig<pre><code>// \u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a\nfn ArrayStack(comptime T: type) type {\n return struct {\n const Self = @This();\n\n stack: ?std.ArrayList(T) = null, \n\n // \u5efa\u69cb\u5b50\uff08\u5206\u914d\u8a18\u61b6\u9ad4+\u521d\u59cb\u5316\u5806\u758a\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) void {\n if (self.stack == null) {\n self.stack = std.ArrayList(T).init(allocator);\n }\n }\n\n // \u6790\u69cb\u65b9\u6cd5\uff08\u91cb\u653e\u8a18\u61b6\u9ad4\uff09\n pub fn deinit(self: *Self) void {\n if (self.stack == null) return;\n self.stack.?.deinit();\n }\n\n // \u7372\u53d6\u5806\u758a\u7684\u9577\u5ea6\n pub fn size(self: *Self) usize {\n return self.stack.?.items.len;\n }\n\n // \u5224\u65b7\u5806\u758a\u662f\u5426\u70ba\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8a2a\u554f\u5806\u758a\u9802\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u5806\u758a\u70ba\u7a7a\");\n return self.stack.?.items[self.size() - 1];\n } \n\n // \u5165\u5806\u758a\n pub fn push(self: *Self, num: T) !void {\n try self.stack.?.append(num);\n } \n\n // \u51fa\u5806\u758a\n pub fn pop(self: *Self) T {\n var num = self.stack.?.pop();\n return num;\n } \n\n // \u8fd4\u56de ArrayList\n pub fn toList(self: *Self) std.ArrayList(T) {\n return self.stack.?;\n }\n };\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_stack_and_queue/stack/#513","title":"5.1.3 \u00a0 \u5169\u7a2e\u5be6\u73fe\u5c0d\u6bd4","text":"<p>\u652f\u6301\u64cd\u4f5c</p> <p>\u5169\u7a2e\u5be6\u73fe\u90fd\u652f\u6301\u5806\u758a\u5b9a\u7fa9\u4e2d\u7684\u5404\u9805\u64cd\u4f5c\u3002\u9663\u5217\u5be6\u73fe\u984d\u5916\u652f\u6301\u96a8\u6a5f\u8a2a\u554f\uff0c\u4f46\u9019\u5df2\u8d85\u51fa\u4e86\u5806\u758a\u7684\u5b9a\u7fa9\u7bc4\u7587\uff0c\u56e0\u6b64\u4e00\u822c\u4e0d\u6703\u7528\u5230\u3002</p> <p>\u6642\u9593\u6548\u7387</p> <p>\u5728\u57fa\u65bc\u9663\u5217\u7684\u5be6\u73fe\u4e2d\uff0c\u5165\u5806\u758a\u548c\u51fa\u5806\u758a\u64cd\u4f5c\u90fd\u5728\u9810\u5148\u5206\u914d\u597d\u7684\u9023\u7e8c\u8a18\u61b6\u9ad4\u4e2d\u9032\u884c\uff0c\u5177\u6709\u5f88\u597d\u7684\u5feb\u53d6\u672c\u5730\u6027\uff0c\u56e0\u6b64\u6548\u7387\u8f03\u9ad8\u3002\u7136\u800c\uff0c\u5982\u679c\u5165\u5806\u758a\u6642\u8d85\u51fa\u9663\u5217\u5bb9\u91cf\uff0c\u6703\u89f8\u767c\u64f4\u5bb9\u6a5f\u5236\uff0c\u5c0e\u81f4\u8a72\u6b21\u5165\u5806\u758a\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u8b8a\u70ba \\(O(n)\\) \u3002</p> <p>\u5728\u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u7684\u5be6\u73fe\u4e2d\uff0c\u93c8\u7d50\u4e32\u5217\u7684\u64f4\u5bb9\u975e\u5e38\u9748\u6d3b\uff0c\u4e0d\u5b58\u5728\u4e0a\u8ff0\u9663\u5217\u64f4\u5bb9\u6642\u6548\u7387\u964d\u4f4e\u7684\u554f\u984c\u3002\u4f46\u662f\uff0c\u5165\u5806\u758a\u64cd\u4f5c\u9700\u8981\u521d\u59cb\u5316\u7bc0\u9ede\u7269\u4ef6\u4e26\u4fee\u6539\u6307\u6a19\uff0c\u56e0\u6b64\u6548\u7387\u76f8\u5c0d\u8f03\u4f4e\u3002\u4e0d\u904e\uff0c\u5982\u679c\u5165\u5806\u758a\u5143\u7d20\u672c\u8eab\u5c31\u662f\u7bc0\u9ede\u7269\u4ef6\uff0c\u90a3\u9ebc\u53ef\u4ee5\u7701\u53bb\u521d\u59cb\u5316\u6b65\u9a5f\uff0c\u5f9e\u800c\u63d0\u9ad8\u6548\u7387\u3002</p> <p>\u7d9c\u4e0a\u6240\u8ff0\uff0c\u7576\u5165\u5806\u758a\u8207\u51fa\u5806\u758a\u64cd\u4f5c\u7684\u5143\u7d20\u662f\u57fa\u672c\u8cc7\u6599\u578b\u5225\u6642\uff0c\u4f8b\u5982 <code>int</code> \u6216 <code>double</code> \uff0c\u6211\u5011\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7d50\u8ad6\u3002</p> <ul> <li>\u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a\u5728\u89f8\u767c\u64f4\u5bb9\u6642\u6548\u7387\u6703\u964d\u4f4e\uff0c\u4f46\u7531\u65bc\u64f4\u5bb9\u662f\u4f4e\u983b\u64cd\u4f5c\uff0c\u56e0\u6b64\u5e73\u5747\u6548\u7387\u66f4\u9ad8\u3002</li> <li>\u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u7684\u5806\u758a\u53ef\u4ee5\u63d0\u4f9b\u66f4\u52a0\u7a69\u5b9a\u7684\u6548\u7387\u8868\u73fe\u3002</li> </ul> <p>\u7a7a\u9593\u6548\u7387</p> <p>\u5728\u521d\u59cb\u5316\u4e32\u5217\u6642\uff0c\u7cfb\u7d71\u6703\u70ba\u4e32\u5217\u5206\u914d\u201c\u521d\u59cb\u5bb9\u91cf\u201d\uff0c\u8a72\u5bb9\u91cf\u53ef\u80fd\u8d85\u51fa\u5be6\u969b\u9700\u6c42\uff1b\u4e26\u4e14\uff0c\u64f4\u5bb9\u6a5f\u5236\u901a\u5e38\u662f\u6309\u7167\u7279\u5b9a\u500d\u7387\uff08\u4f8b\u5982 2 \u500d\uff09\u9032\u884c\u64f4\u5bb9\u7684\uff0c\u64f4\u5bb9\u5f8c\u7684\u5bb9\u91cf\u4e5f\u53ef\u80fd\u8d85\u51fa\u5be6\u969b\u9700\u6c42\u3002\u56e0\u6b64\uff0c\u57fa\u65bc\u9663\u5217\u5be6\u73fe\u7684\u5806\u758a\u53ef\u80fd\u9020\u6210\u4e00\u5b9a\u7684\u7a7a\u9593\u6d6a\u8cbb\u3002</p> <p>\u7136\u800c\uff0c\u7531\u65bc\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u9700\u8981\u984d\u5916\u5132\u5b58\u6307\u6a19\uff0c\u56e0\u6b64\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u4f54\u7528\u7684\u7a7a\u9593\u76f8\u5c0d\u8f03\u5927\u3002</p> <p>\u7d9c\u4e0a\uff0c\u6211\u5011\u4e0d\u80fd\u7c21\u55ae\u5730\u78ba\u5b9a\u54ea\u7a2e\u5be6\u73fe\u66f4\u52a0\u7bc0\u7701\u8a18\u61b6\u9ad4\uff0c\u9700\u8981\u91dd\u5c0d\u5177\u9ad4\u60c5\u6cc1\u9032\u884c\u5206\u6790\u3002</p>"},{"location":"chapter_stack_and_queue/stack/#514","title":"5.1.4 \u00a0 \u5806\u758a\u7684\u5178\u578b\u61c9\u7528","text":"<ul> <li>\u700f\u89bd\u5668\u4e2d\u7684\u5f8c\u9000\u8207\u524d\u9032\u3001\u8edf\u9ad4\u4e2d\u7684\u64a4\u92b7\u8207\u53cd\u64a4\u92b7\u3002\u6bcf\u7576\u6211\u5011\u958b\u555f\u65b0\u7684\u7db2\u9801\uff0c\u700f\u89bd\u5668\u5c31\u6703\u5c0d\u4e0a\u4e00\u500b\u7db2\u9801\u57f7\u884c\u5165\u5806\u758a\uff0c\u9019\u6a23\u6211\u5011\u5c31\u53ef\u4ee5\u901a\u904e\u5f8c\u9000\u64cd\u4f5c\u56de\u5230\u4e0a\u4e00\u500b\u7db2\u9801\u3002\u5f8c\u9000\u64cd\u4f5c\u5be6\u969b\u4e0a\u662f\u5728\u57f7\u884c\u51fa\u5806\u758a\u3002\u5982\u679c\u8981\u540c\u6642\u652f\u6301\u5f8c\u9000\u548c\u524d\u9032\uff0c\u90a3\u9ebc\u9700\u8981\u5169\u500b\u5806\u758a\u4f86\u914d\u5408\u5be6\u73fe\u3002</li> <li>\u7a0b\u5f0f\u8a18\u61b6\u9ad4\u7ba1\u7406\u3002\u6bcf\u6b21\u547c\u53eb\u51fd\u5f0f\u6642\uff0c\u7cfb\u7d71\u90fd\u6703\u5728\u5806\u758a\u9802\u65b0\u589e\u4e00\u500b\u5806\u758a\u5e40\uff0c\u7528\u65bc\u8a18\u9304\u51fd\u5f0f\u7684\u4e0a\u4e0b\u6587\u8cc7\u8a0a\u3002\u5728\u905e\u8ff4\u51fd\u5f0f\u4e2d\uff0c\u5411\u4e0b\u905e\u63a8\u968e\u6bb5\u6703\u4e0d\u65b7\u57f7\u884c\u5165\u5806\u758a\u64cd\u4f5c\uff0c\u800c\u5411\u4e0a\u56de\u6eaf\u968e\u6bb5\u5247\u6703\u4e0d\u65b7\u57f7\u884c\u51fa\u5806\u758a\u64cd\u4f5c\u3002</li> </ul>"},{"location":"chapter_stack_and_queue/summary/","title":"5.4 \u00a0 \u5c0f\u7d50","text":""},{"location":"chapter_stack_and_queue/summary/#1","title":"1. \u00a0 \u91cd\u9ede\u56de\u9867","text":"<ul> <li>\u5806\u758a\u662f\u4e00\u7a2e\u9075\u5faa\u5148\u5165\u5f8c\u51fa\u539f\u5247\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u53ef\u900f\u904e\u9663\u5217\u6216\u93c8\u7d50\u4e32\u5217\u4f86\u5be6\u73fe\u3002</li> <li>\u5728\u6642\u9593\u6548\u7387\u65b9\u9762\uff0c\u5806\u758a\u7684\u9663\u5217\u5be6\u73fe\u5177\u6709\u8f03\u9ad8\u7684\u5e73\u5747\u6548\u7387\uff0c\u4f46\u5728\u64f4\u5bb9\u904e\u7a0b\u4e2d\uff0c\u55ae\u6b21\u5165\u5806\u758a\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u6703\u52a3\u5316\u81f3 \\(O(n)\\) \u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5806\u758a\u7684\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\u5177\u6709\u66f4\u70ba\u7a69\u5b9a\u7684\u6548\u7387\u8868\u73fe\u3002</li> <li>\u5728\u7a7a\u9593\u6548\u7387\u65b9\u9762\uff0c\u5806\u758a\u7684\u9663\u5217\u5be6\u73fe\u53ef\u80fd\u5c0e\u81f4\u4e00\u5b9a\u7a0b\u5ea6\u7684\u7a7a\u9593\u6d6a\u8cbb\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u93c8\u7d50\u4e32\u5217\u7bc0\u9ede\u6240\u4f54\u7528\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u6bd4\u9663\u5217\u5143\u7d20\u66f4\u5927\u3002</li> <li>\u4f47\u5217\u662f\u4e00\u7a2e\u9075\u5faa\u5148\u5165\u5148\u51fa\u539f\u5247\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u540c\u6a23\u53ef\u4ee5\u900f\u904e\u9663\u5217\u6216\u93c8\u7d50\u4e32\u5217\u4f86\u5be6\u73fe\u3002\u5728\u6642\u9593\u6548\u7387\u548c\u7a7a\u9593\u6548\u7387\u7684\u5c0d\u6bd4\u4e0a\uff0c\u4f47\u5217\u7684\u7d50\u8ad6\u8207\u524d\u8ff0\u5806\u758a\u7684\u7d50\u8ad6\u76f8\u4f3c\u3002</li> <li>\u96d9\u5411\u4f47\u5217\u662f\u4e00\u7a2e\u5177\u6709\u66f4\u9ad8\u81ea\u7531\u5ea6\u7684\u4f47\u5217\uff0c\u5b83\u5141\u8a31\u5728\u5169\u7aef\u9032\u884c\u5143\u7d20\u7684\u65b0\u589e\u548c\u522a\u9664\u64cd\u4f5c\u3002</li> </ul>"},{"location":"chapter_stack_and_queue/summary/#2-q-a","title":"2. \u00a0 Q &amp; A","text":"<p>Q\uff1a\u700f\u89bd\u5668\u7684\u524d\u9032\u5f8c\u9000\u662f\u5426\u662f\u96d9\u5411\u93c8\u7d50\u4e32\u5217\u5be6\u73fe\uff1f</p> <p>\u700f\u89bd\u5668\u7684\u524d\u9032\u5f8c\u9000\u529f\u80fd\u672c\u8cea\u4e0a\u662f\u201c\u5806\u758a\u201d\u7684\u9ad4\u73fe\u3002\u7576\u7528\u6236\u8a2a\u554f\u4e00\u500b\u65b0\u9801\u9762\u6642\uff0c\u8a72\u9801\u9762\u6703\u88ab\u65b0\u589e\u5230\u5806\u758a\u9802\uff1b\u7576\u7528\u6236\u9ede\u9078\u5f8c\u9000\u6309\u9215\u6642\uff0c\u8a72\u9801\u9762\u6703\u5f9e\u5806\u758a\u9802\u5f48\u51fa\u3002\u4f7f\u7528\u96d9\u5411\u4f47\u5217\u53ef\u4ee5\u65b9\u4fbf\u5730\u5be6\u73fe\u4e00\u4e9b\u984d\u5916\u64cd\u4f5c\uff0c\u9019\u500b\u5728\u201c\u96d9\u5411\u4f47\u5217\u201d\u7ae0\u7bc0\u6709\u63d0\u5230\u3002</p> <p>Q\uff1a\u5728\u51fa\u5806\u758a\u5f8c\uff0c\u662f\u5426\u9700\u8981\u91cb\u653e\u51fa\u5806\u758a\u7bc0\u9ede\u7684\u8a18\u61b6\u9ad4\uff1f</p> <p>\u5982\u679c\u5f8c\u7e8c\u4ecd\u9700\u8981\u4f7f\u7528\u5f48\u51fa\u7bc0\u9ede\uff0c\u5247\u4e0d\u9700\u8981\u91cb\u653e\u8a18\u61b6\u9ad4\u3002\u82e5\u4e4b\u5f8c\u4e0d\u9700\u8981\u7528\u5230\uff0c<code>Java</code> \u548c <code>Python</code> \u7b49\u8a9e\u8a00\u64c1\u6709\u81ea\u52d5\u5783\u573e\u56de\u6536\u6a5f\u5236\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u624b\u52d5\u91cb\u653e\u8a18\u61b6\u9ad4\uff1b\u5728 <code>C</code> \u548c <code>C++</code> \u4e2d\u9700\u8981\u624b\u52d5\u91cb\u653e\u8a18\u61b6\u9ad4\u3002</p> <p>Q\uff1a\u96d9\u5411\u4f47\u5217\u50cf\u662f\u5169\u500b\u5806\u758a\u62fc\u63a5\u5728\u4e86\u4e00\u8d77\uff0c\u5b83\u7684\u7528\u9014\u662f\u4ec0\u9ebc\uff1f</p> <p>\u96d9\u5411\u4f47\u5217\u5c31\u50cf\u662f\u5806\u758a\u548c\u4f47\u5217\u7684\u7d44\u5408\u6216\u5169\u500b\u5806\u758a\u62fc\u5728\u4e86\u4e00\u8d77\u3002\u5b83\u8868\u73fe\u7684\u662f\u5806\u758a + \u4f47\u5217\u7684\u908f\u8f2f\uff0c\u56e0\u6b64\u53ef\u4ee5\u5be6\u73fe\u5806\u758a\u8207\u4f47\u5217\u7684\u6240\u6709\u61c9\u7528\uff0c\u4e26\u4e14\u66f4\u52a0\u9748\u6d3b\u3002</p> <p>Q\uff1a\u64a4\u92b7\uff08undo\uff09\u548c\u53cd\u64a4\u92b7\uff08redo\uff09\u5177\u9ad4\u662f\u5982\u4f55\u5be6\u73fe\u7684\uff1f</p> <p>\u4f7f\u7528\u5169\u500b\u5806\u758a\uff0c\u5806\u758a <code>A</code> \u7528\u65bc\u64a4\u92b7\uff0c\u5806\u758a <code>B</code> \u7528\u65bc\u53cd\u64a4\u92b7\u3002</p> <ol> <li>\u6bcf\u7576\u4f7f\u7528\u8005\u57f7\u884c\u4e00\u500b\u64cd\u4f5c\uff0c\u5c07\u9019\u500b\u64cd\u4f5c\u58d3\u5165\u5806\u758a <code>A</code> \uff0c\u4e26\u6e05\u7a7a\u5806\u758a <code>B</code> \u3002</li> <li>\u7576\u7528\u6236\u57f7\u884c\u201c\u64a4\u92b7\u201d\u6642\uff0c\u5f9e\u5806\u758a <code>A</code> \u4e2d\u5f48\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u4e26\u5c07\u5176\u58d3\u5165\u5806\u758a <code>B</code> \u3002</li> <li>\u7576\u7528\u6236\u57f7\u884c\u201c\u53cd\u64a4\u92b7\u201d\u6642\uff0c\u5f9e\u5806\u758a <code>B</code> \u4e2d\u5f48\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u4e26\u5c07\u5176\u58d3\u5165\u5806\u758a <code>A</code> \u3002</li> </ol>"},{"location":"chapter_tree/","title":"\u7b2c 7 \u7ae0 \u00a0 \u6a39","text":"<p>Abstract</p> <p>\u53c3\u5929\u5927\u6a39\u5145\u6eff\u751f\u547d\u529b\uff0c\u6839\u6df1\u8449\u8302\uff0c\u5206\u679d\u6276\u758f\u3002</p> <p>\u5b83\u70ba\u6211\u5011\u5c55\u73fe\u4e86\u8cc7\u6599\u5206\u6cbb\u7684\u751f\u52d5\u5f62\u614b\u3002</p>"},{"location":"chapter_tree/#_1","title":"\u672c\u7ae0\u5167\u5bb9","text":"<ul> <li>7.1 \u00a0 \u4e8c\u5143\u6a39</li> <li>7.2 \u00a0 \u4e8c\u5143\u6a39\u8d70\u8a2a</li> <li>7.3 \u00a0 \u4e8c\u5143\u6a39\u9663\u5217\u8868\u793a</li> <li>7.4 \u00a0 \u4e8c\u5143\u641c\u5c0b\u6a39</li> <li>7.5 \u00a0 AVL *</li> <li>7.6 \u00a0 \u5c0f\u7d50</li> </ul>"},{"location":"chapter_tree/array_representation_of_tree/","title":"7.3 \u00a0 \u4e8c\u5143\u6a39\u9663\u5217\u8868\u793a","text":"<p>\u5728\u93c8\u7d50\u4e32\u5217\u8868\u793a\u4e0b\uff0c\u4e8c\u5143\u6a39\u7684\u5132\u5b58\u55ae\u5143\u70ba\u7bc0\u9ede <code>TreeNode</code> \uff0c\u7bc0\u9ede\u4e4b\u9593\u900f\u904e\u6307\u6a19\u76f8\u9023\u7dda\u3002\u4e0a\u4e00\u7bc0\u4ecb\u7d39\u4e86\u93c8\u7d50\u4e32\u5217\u8868\u793a\u4e0b\u7684\u4e8c\u5143\u6a39\u7684\u5404\u9805\u57fa\u672c\u64cd\u4f5c\u3002</p> <p>\u90a3\u9ebc\uff0c\u6211\u5011\u80fd\u5426\u7528\u9663\u5217\u4f86\u8868\u793a\u4e8c\u5143\u6a39\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002</p>"},{"location":"chapter_tree/array_representation_of_tree/#731","title":"7.3.1 \u00a0 \u8868\u793a\u5b8c\u7f8e\u4e8c\u5143\u6a39","text":"<p>\u5148\u5206\u6790\u4e00\u500b\u7c21\u55ae\u6848\u4f8b\u3002\u7d66\u5b9a\u4e00\u68f5\u5b8c\u7f8e\u4e8c\u5143\u6a39\uff0c\u6211\u5011\u5c07\u6240\u6709\u7bc0\u9ede\u6309\u7167\u5c64\u5e8f\u8d70\u8a2a\u7684\u9806\u5e8f\u5132\u5b58\u5728\u4e00\u500b\u9663\u5217\u4e2d\uff0c\u5247\u6bcf\u500b\u7bc0\u9ede\u90fd\u5c0d\u61c9\u552f\u4e00\u7684\u9663\u5217\u7d22\u5f15\u3002</p> <p>\u6839\u64da\u5c64\u5e8f\u8d70\u8a2a\u7684\u7279\u6027\uff0c\u6211\u5011\u53ef\u4ee5\u63a8\u5c0e\u51fa\u7236\u7bc0\u9ede\u7d22\u5f15\u8207\u5b50\u7bc0\u9ede\u7d22\u5f15\u4e4b\u9593\u7684\u201c\u5c0d\u6620\u516c\u5f0f\u201d\uff1a\u82e5\u67d0\u7bc0\u9ede\u7684\u7d22\u5f15\u70ba \\(i\\) \uff0c\u5247\u8a72\u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u7d22\u5f15\u70ba \\(2i + 1\\) \uff0c\u53f3\u5b50\u7bc0\u9ede\u7d22\u5f15\u70ba \\(2i + 2\\) \u3002\u5716 7-12 \u5c55\u793a\u4e86\u5404\u500b\u7bc0\u9ede\u7d22\u5f15\u4e4b\u9593\u7684\u5c0d\u6620\u95dc\u4fc2\u3002</p> <p></p> <p> \u5716 7-12 \u00a0 \u5b8c\u7f8e\u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a </p> <p>\u5c0d\u6620\u516c\u5f0f\u7684\u89d2\u8272\u76f8\u7576\u65bc\u93c8\u7d50\u4e32\u5217\u4e2d\u7684\u7bc0\u9ede\u5f15\u7528\uff08\u6307\u6a19\uff09\u3002\u7d66\u5b9a\u9663\u5217\u4e2d\u7684\u4efb\u610f\u4e00\u500b\u7bc0\u9ede\uff0c\u6211\u5011\u90fd\u53ef\u4ee5\u900f\u904e\u5c0d\u6620\u516c\u5f0f\u4f86\u8a2a\u554f\u5b83\u7684\u5de6\uff08\u53f3\uff09\u5b50\u7bc0\u9ede\u3002</p>"},{"location":"chapter_tree/array_representation_of_tree/#732","title":"7.3.2 \u00a0 \u8868\u793a\u4efb\u610f\u4e8c\u5143\u6a39","text":"<p>\u5b8c\u7f8e\u4e8c\u5143\u6a39\u662f\u4e00\u500b\u7279\u4f8b\uff0c\u5728\u4e8c\u5143\u6a39\u7684\u4e2d\u9593\u5c64\u901a\u5e38\u5b58\u5728\u8a31\u591a <code>None</code> \u3002\u7531\u65bc\u5c64\u5e8f\u8d70\u8a2a\u5e8f\u5217\u4e26\u4e0d\u5305\u542b\u9019\u4e9b <code>None</code> \uff0c\u56e0\u6b64\u6211\u5011\u7121\u6cd5\u50c5\u6191\u8a72\u5e8f\u5217\u4f86\u63a8\u6e2c <code>None</code> \u7684\u6578\u91cf\u548c\u5206\u4f48\u4f4d\u7f6e\u3002\u9019\u610f\u5473\u8457\u5b58\u5728\u591a\u7a2e\u4e8c\u5143\u6a39\u7d50\u69cb\u90fd\u7b26\u5408\u8a72\u5c64\u5e8f\u8d70\u8a2a\u5e8f\u5217\u3002</p> <p>\u5982\u5716 7-13 \u6240\u793a\uff0c\u7d66\u5b9a\u4e00\u68f5\u975e\u5b8c\u7f8e\u4e8c\u5143\u6a39\uff0c\u4e0a\u8ff0\u9663\u5217\u8868\u793a\u65b9\u6cd5\u5df2\u7d93\u5931\u6548\u3002</p> <p></p> <p> \u5716 7-13 \u00a0 \u5c64\u5e8f\u8d70\u8a2a\u5e8f\u5217\u5c0d\u61c9\u591a\u7a2e\u4e8c\u5143\u6a39\u53ef\u80fd\u6027 </p> <p>\u70ba\u4e86\u89e3\u6c7a\u6b64\u554f\u984c\uff0c\u6211\u5011\u53ef\u4ee5\u8003\u616e\u5728\u5c64\u5e8f\u8d70\u8a2a\u5e8f\u5217\u4e2d\u986f\u5f0f\u5730\u5beb\u51fa\u6240\u6709 <code>None</code> \u3002\u5982\u5716 7-14 \u6240\u793a\uff0c\u9019\u6a23\u8655\u7406\u5f8c\uff0c\u5c64\u5e8f\u8d70\u8a2a\u5e8f\u5217\u5c31\u53ef\u4ee5\u552f\u4e00\u8868\u793a\u4e8c\u5143\u6a39\u4e86\u3002\u793a\u4f8b\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code># \u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a\n# \u4f7f\u7528 None \u4f86\u8868\u793a\u7a7a\u4f4d\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c INT_MAX \u6a19\u8a18\u7a7a\u4f4d\nvector&lt;int&gt; tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a */\n// \u4f7f\u7528 int \u7684\u5305\u88dd\u985e\u5225 Integer \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u4f86\u6a19\u8a18\u7a7a\u4f4d\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u578b\u5225 \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u4f86\u6a19\u8a18\u7a7a\u4f4d\nint?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a */\n// \u4f7f\u7528 any \u578b\u5225\u7684\u5207\u7247, \u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u4f86\u6a19\u8a18\u7a7a\u4f4d\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a */\n// \u4f7f\u7528 Int? \u53ef\u7a7a\u578b\u5225 \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u4f86\u6a19\u8a18\u7a7a\u4f4d\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a */\n// \u4f7f\u7528 null \u4f86\u8868\u793a\u7a7a\u4f4d\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a */\n// \u4f7f\u7528 null \u4f86\u8868\u793a\u7a7a\u4f4d\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u578b\u5225 \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u4f86\u6a19\u8a18\u7a7a\u4f4d\nList&lt;int?&gt; tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a */\n// \u4f7f\u7528 None \u4f86\u6a19\u8a18\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</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c\u6a19\u8a18\u7a7a\u4f4d\uff0c\u56e0\u6b64\u8981\u6c42\u7bc0\u9ede\u503c\u4e0d\u80fd\u70ba 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</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a */\n// \u4f7f\u7528 null \u4f86\u8868\u793a\u7a7a\u4f4d\nval tree = arrayOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )\n</code></pre> <pre><code>### \u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a ###\n# \u4f7f\u7528 nil \u4f86\u8868\u793a\u7a7a\u4f4d\ntree = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n</code></pre> <pre><code>\n</code></pre> <p></p> <p> \u5716 7-14 \u00a0 \u4efb\u610f\u578b\u5225\u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a </p> <p>\u503c\u5f97\u8aaa\u660e\u7684\u662f\uff0c\u5b8c\u5168\u4e8c\u5143\u6a39\u975e\u5e38\u9069\u5408\u4f7f\u7528\u9663\u5217\u4f86\u8868\u793a\u3002\u56de\u9867\u5b8c\u5168\u4e8c\u5143\u6a39\u7684\u5b9a\u7fa9\uff0c<code>None</code> \u53ea\u51fa\u73fe\u5728\u6700\u5e95\u5c64\u4e14\u9760\u53f3\u7684\u4f4d\u7f6e\uff0c\u56e0\u6b64\u6240\u6709 <code>None</code> \u4e00\u5b9a\u51fa\u73fe\u5728\u5c64\u5e8f\u8d70\u8a2a\u5e8f\u5217\u7684\u672b\u5c3e\u3002</p> <p>\u9019\u610f\u5473\u8457\u4f7f\u7528\u9663\u5217\u8868\u793a\u5b8c\u5168\u4e8c\u5143\u6a39\u6642\uff0c\u53ef\u4ee5\u7701\u7565\u5132\u5b58\u6240\u6709 <code>None</code> \uff0c\u975e\u5e38\u65b9\u4fbf\u3002\u5716 7-15 \u7d66\u51fa\u4e86\u4e00\u500b\u4f8b\u5b50\u3002</p> <p></p> <p> \u5716 7-15 \u00a0 \u5b8c\u5168\u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a </p> <p>\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u5be6\u73fe\u4e86\u4e00\u68f5\u57fa\u65bc\u9663\u5217\u8868\u793a\u7684\u4e8c\u5143\u6a39\uff0c\u5305\u62ec\u4ee5\u4e0b\u5e7e\u7a2e\u64cd\u4f5c\u3002</p> <ul> <li>\u7d66\u5b9a\u67d0\u7bc0\u9ede\uff0c\u7372\u53d6\u5b83\u7684\u503c\u3001\u5de6\uff08\u53f3\uff09\u5b50\u7bc0\u9ede\u3001\u7236\u7bc0\u9ede\u3002</li> <li>\u7372\u53d6\u524d\u5e8f\u8d70\u8a2a\u3001\u4e2d\u5e8f\u8d70\u8a2a\u3001\u5f8c\u5e8f\u8d70\u8a2a\u3001\u5c64\u5e8f\u8d70\u8a2a\u5e8f\u5217\u3002</li> </ul> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array_binary_tree.py<pre><code>class ArrayBinaryTree:\n \"\"\"\u9663\u5217\u8868\u793a\u4e0b\u7684\u4e8c\u5143\u6a39\u985e\u5225\"\"\"\n\n def __init__(self, arr: list[int | None]):\n \"\"\"\u5efa\u69cb\u5b50\"\"\"\n self._tree = list(arr)\n\n def size(self):\n \"\"\"\u4e32\u5217\u5bb9\u91cf\"\"\"\n return len(self._tree)\n\n def val(self, i: int) -&gt; int | None:\n \"\"\"\u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u503c\"\"\"\n # \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i &lt; 0 or i &gt;= self.size():\n return None\n return self._tree[i]\n\n def left(self, i: int) -&gt; int | None:\n \"\"\"\u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 1\n\n def right(self, i: int) -&gt; int | None:\n \"\"\"\u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 2\n\n def parent(self, i: int) -&gt; int | None:\n \"\"\"\u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\u7684\u7d22\u5f15\"\"\"\n return (i - 1) // 2\n\n def level_order(self) -&gt; list[int]:\n \"\"\"\u5c64\u5e8f\u8d70\u8a2a\"\"\"\n self.res = []\n # \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\n for i in range(self.size()):\n if self.val(i) is not None:\n self.res.append(self.val(i))\n return self.res\n\n def dfs(self, i: int, order: str):\n \"\"\"\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\"\"\"\n if self.val(i) is None:\n return\n # \u524d\u5e8f\u8d70\u8a2a\n if order == \"pre\":\n self.res.append(self.val(i))\n self.dfs(self.left(i), order)\n # \u4e2d\u5e8f\u8d70\u8a2a\n if order == \"in\":\n self.res.append(self.val(i))\n self.dfs(self.right(i), order)\n # \u5f8c\u5e8f\u8d70\u8a2a\n if order == \"post\":\n self.res.append(self.val(i))\n\n def pre_order(self) -&gt; list[int]:\n \"\"\"\u524d\u5e8f\u8d70\u8a2a\"\"\"\n self.res = []\n self.dfs(0, order=\"pre\")\n return self.res\n\n def in_order(self) -&gt; list[int]:\n \"\"\"\u4e2d\u5e8f\u8d70\u8a2a\"\"\"\n self.res = []\n self.dfs(0, order=\"in\")\n return self.res\n\n def post_order(self) -&gt; list[int]:\n \"\"\"\u5f8c\u5e8f\u8d70\u8a2a\"\"\"\n self.res = []\n self.dfs(0, order=\"post\")\n return self.res\n</code></pre> array_binary_tree.cpp<pre><code>/* \u9663\u5217\u8868\u793a\u4e0b\u7684\u4e8c\u5143\u6a39\u985e\u5225 */\nclass ArrayBinaryTree {\n public:\n /* \u5efa\u69cb\u5b50 */\n ArrayBinaryTree(vector&lt;int&gt; arr) {\n tree = arr;\n }\n\n /* \u4e32\u5217\u5bb9\u91cf */\n int size() {\n return tree.size();\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u503c */\n int val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i &lt; 0 || i &gt;= size())\n return INT_MAX;\n return tree[i];\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n int left(int i) {\n return 2 * i + 1;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n int right(int i) {\n return 2 * i + 2;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\n int parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c64\u5e8f\u8d70\u8a2a */\n vector&lt;int&gt; levelOrder() {\n vector&lt;int&gt; res;\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\n for (int i = 0; i &lt; size(); i++) {\n if (val(i) != INT_MAX)\n res.push_back(val(i));\n }\n return res;\n }\n\n /* \u524d\u5e8f\u8d70\u8a2a */\n vector&lt;int&gt; preOrder() {\n vector&lt;int&gt; res;\n dfs(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u8d70\u8a2a */\n vector&lt;int&gt; inOrder() {\n vector&lt;int&gt; res;\n dfs(0, \"in\", res);\n return res;\n }\n\n /* \u5f8c\u5e8f\u8d70\u8a2a */\n vector&lt;int&gt; postOrder() {\n vector&lt;int&gt; res;\n dfs(0, \"post\", res);\n return res;\n }\n\n private:\n vector&lt;int&gt; tree;\n\n /* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n void dfs(int i, string order, vector&lt;int&gt; &amp;res) {\n // \u82e5\u70ba\u7a7a\u4f4d\uff0c\u5247\u8fd4\u56de\n if (val(i) == INT_MAX)\n return;\n // \u524d\u5e8f\u8d70\u8a2a\n if (order == \"pre\")\n res.push_back(val(i));\n dfs(left(i), order, res);\n // \u4e2d\u5e8f\u8d70\u8a2a\n if (order == \"in\")\n res.push_back(val(i));\n dfs(right(i), order, res);\n // \u5f8c\u5e8f\u8d70\u8a2a\n if (order == \"post\")\n res.push_back(val(i));\n }\n};\n</code></pre> array_binary_tree.java<pre><code>/* \u9663\u5217\u8868\u793a\u4e0b\u7684\u4e8c\u5143\u6a39\u985e\u5225 */\nclass ArrayBinaryTree {\n private List&lt;Integer&gt; tree;\n\n /* \u5efa\u69cb\u5b50 */\n public ArrayBinaryTree(List&lt;Integer&gt; arr) {\n tree = new ArrayList&lt;&gt;(arr);\n }\n\n /* \u4e32\u5217\u5bb9\u91cf */\n public int size() {\n return tree.size();\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u503c */\n public Integer val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i &lt; 0 || i &gt;= size())\n return null;\n return tree.get(i);\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n public Integer left(int i) {\n return 2 * i + 1;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n public Integer right(int i) {\n return 2 * i + 2;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\n public Integer parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c64\u5e8f\u8d70\u8a2a */\n public List&lt;Integer&gt; levelOrder() {\n List&lt;Integer&gt; res = new ArrayList&lt;&gt;();\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\n for (int i = 0; i &lt; size(); i++) {\n if (val(i) != null)\n res.add(val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n private void dfs(Integer i, String order, List&lt;Integer&gt; res) {\n // \u82e5\u70ba\u7a7a\u4f4d\uff0c\u5247\u8fd4\u56de\n if (val(i) == null)\n return;\n // \u524d\u5e8f\u8d70\u8a2a\n if (\"pre\".equals(order))\n res.add(val(i));\n dfs(left(i), order, res);\n // \u4e2d\u5e8f\u8d70\u8a2a\n if (\"in\".equals(order))\n res.add(val(i));\n dfs(right(i), order, res);\n // \u5f8c\u5e8f\u8d70\u8a2a\n if (\"post\".equals(order))\n res.add(val(i));\n }\n\n /* \u524d\u5e8f\u8d70\u8a2a */\n public List&lt;Integer&gt; preOrder() {\n List&lt;Integer&gt; res = new ArrayList&lt;&gt;();\n dfs(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u8d70\u8a2a */\n public List&lt;Integer&gt; inOrder() {\n List&lt;Integer&gt; res = new ArrayList&lt;&gt;();\n dfs(0, \"in\", res);\n return res;\n }\n\n /* \u5f8c\u5e8f\u8d70\u8a2a */\n public List&lt;Integer&gt; postOrder() {\n List&lt;Integer&gt; res = new ArrayList&lt;&gt;();\n dfs(0, \"post\", res);\n return res;\n }\n}\n</code></pre> array_binary_tree.cs<pre><code>/* \u9663\u5217\u8868\u793a\u4e0b\u7684\u4e8c\u5143\u6a39\u985e\u5225 */\nclass ArrayBinaryTree(List&lt;int?&gt; arr) {\n List&lt;int?&gt; tree = new(arr);\n\n /* \u4e32\u5217\u5bb9\u91cf */\n public int Size() {\n return tree.Count;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u503c */\n public int? Val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i &lt; 0 || i &gt;= Size())\n return null;\n return tree[i];\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n public int Left(int i) {\n return 2 * i + 1;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n public int Right(int i) {\n return 2 * i + 2;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\n public int Parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c64\u5e8f\u8d70\u8a2a */\n public List&lt;int&gt; LevelOrder() {\n List&lt;int&gt; res = [];\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\n for (int i = 0; i &lt; Size(); i++) {\n if (Val(i).HasValue)\n res.Add(Val(i)!.Value);\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n void DFS(int i, string order, List&lt;int&gt; res) {\n // \u82e5\u70ba\u7a7a\u4f4d\uff0c\u5247\u8fd4\u56de\n if (!Val(i).HasValue)\n return;\n // \u524d\u5e8f\u8d70\u8a2a\n if (order == \"pre\")\n res.Add(Val(i)!.Value);\n DFS(Left(i), order, res);\n // \u4e2d\u5e8f\u8d70\u8a2a\n if (order == \"in\")\n res.Add(Val(i)!.Value);\n DFS(Right(i), order, res);\n // \u5f8c\u5e8f\u8d70\u8a2a\n if (order == \"post\")\n res.Add(Val(i)!.Value);\n }\n\n /* \u524d\u5e8f\u8d70\u8a2a */\n public List&lt;int&gt; PreOrder() {\n List&lt;int&gt; res = [];\n DFS(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u8d70\u8a2a */\n public List&lt;int&gt; InOrder() {\n List&lt;int&gt; res = [];\n DFS(0, \"in\", res);\n return res;\n }\n\n /* \u5f8c\u5e8f\u8d70\u8a2a */\n public List&lt;int&gt; PostOrder() {\n List&lt;int&gt; res = [];\n DFS(0, \"post\", res);\n return res;\n }\n}\n</code></pre> array_binary_tree.go<pre><code>/* \u9663\u5217\u8868\u793a\u4e0b\u7684\u4e8c\u5143\u6a39\u985e\u5225 */\ntype arrayBinaryTree struct {\n tree []any\n}\n\n/* \u5efa\u69cb\u5b50 */\nfunc newArrayBinaryTree(arr []any) *arrayBinaryTree {\n return &amp;arrayBinaryTree{\n tree: arr,\n }\n}\n\n/* \u4e32\u5217\u5bb9\u91cf */\nfunc (abt *arrayBinaryTree) size() int {\n return len(abt.tree)\n}\n\n/* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u503c */\nfunc (abt *arrayBinaryTree) val(i int) any {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i &lt; 0 || i &gt;= abt.size() {\n return nil\n }\n return abt.tree[i]\n}\n\n/* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) left(i int) int {\n return 2*i + 1\n}\n\n/* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) right(i int) int {\n return 2*i + 2\n}\n\n/* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) parent(i int) int {\n return (i - 1) / 2\n}\n\n/* \u5c64\u5e8f\u8d70\u8a2a */\nfunc (abt *arrayBinaryTree) levelOrder() []any {\n var res []any\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\n for i := 0; i &lt; abt.size(); i++ {\n if abt.val(i) != nil {\n res = append(res, abt.val(i))\n }\n }\n return res\n}\n\n/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\nfunc (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {\n // \u82e5\u70ba\u7a7a\u4f4d\uff0c\u5247\u8fd4\u56de\n if abt.val(i) == nil {\n return\n }\n // \u524d\u5e8f\u8d70\u8a2a\n if order == \"pre\" {\n *res = append(*res, abt.val(i))\n }\n abt.dfs(abt.left(i), order, res)\n // \u4e2d\u5e8f\u8d70\u8a2a\n if order == \"in\" {\n *res = append(*res, abt.val(i))\n }\n abt.dfs(abt.right(i), order, res)\n // \u5f8c\u5e8f\u8d70\u8a2a\n if order == \"post\" {\n *res = append(*res, abt.val(i))\n }\n}\n\n/* \u524d\u5e8f\u8d70\u8a2a */\nfunc (abt *arrayBinaryTree) preOrder() []any {\n var res []any\n abt.dfs(0, \"pre\", &amp;res)\n return res\n}\n\n/* \u4e2d\u5e8f\u8d70\u8a2a */\nfunc (abt *arrayBinaryTree) inOrder() []any {\n var res []any\n abt.dfs(0, \"in\", &amp;res)\n return res\n}\n\n/* \u5f8c\u5e8f\u8d70\u8a2a */\nfunc (abt *arrayBinaryTree) postOrder() []any {\n var res []any\n abt.dfs(0, \"post\", &amp;res)\n return res\n}\n</code></pre> array_binary_tree.swift<pre><code>/* \u9663\u5217\u8868\u793a\u4e0b\u7684\u4e8c\u5143\u6a39\u985e\u5225 */\nclass ArrayBinaryTree {\n private var tree: [Int?]\n\n /* \u5efa\u69cb\u5b50 */\n init(arr: [Int?]) {\n tree = arr\n }\n\n /* \u4e32\u5217\u5bb9\u91cf */\n func size() -&gt; Int {\n tree.count\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u503c */\n func val(i: Int) -&gt; Int? {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i &lt; 0 || i &gt;= size() {\n return nil\n }\n return tree[i]\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n func left(i: Int) -&gt; Int {\n 2 * i + 1\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n func right(i: Int) -&gt; Int {\n 2 * i + 2\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\n func parent(i: Int) -&gt; Int {\n (i - 1) / 2\n }\n\n /* \u5c64\u5e8f\u8d70\u8a2a */\n func levelOrder() -&gt; [Int] {\n var res: [Int] = []\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\n for i in 0 ..&lt; size() {\n if let val = val(i: i) {\n res.append(val)\n }\n }\n return res\n }\n\n /* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n private func dfs(i: Int, order: String, res: inout [Int]) {\n // \u82e5\u70ba\u7a7a\u4f4d\uff0c\u5247\u8fd4\u56de\n guard let val = val(i: i) else {\n return\n }\n // \u524d\u5e8f\u8d70\u8a2a\n if order == \"pre\" {\n res.append(val)\n }\n dfs(i: left(i: i), order: order, res: &amp;res)\n // \u4e2d\u5e8f\u8d70\u8a2a\n if order == \"in\" {\n res.append(val)\n }\n dfs(i: right(i: i), order: order, res: &amp;res)\n // \u5f8c\u5e8f\u8d70\u8a2a\n if order == \"post\" {\n res.append(val)\n }\n }\n\n /* \u524d\u5e8f\u8d70\u8a2a */\n func preOrder() -&gt; [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"pre\", res: &amp;res)\n return res\n }\n\n /* \u4e2d\u5e8f\u8d70\u8a2a */\n func inOrder() -&gt; [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"in\", res: &amp;res)\n return res\n }\n\n /* \u5f8c\u5e8f\u8d70\u8a2a */\n func postOrder() -&gt; [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"post\", res: &amp;res)\n return res\n }\n}\n</code></pre> array_binary_tree.js<pre><code>/* \u9663\u5217\u8868\u793a\u4e0b\u7684\u4e8c\u5143\u6a39\u985e\u5225 */\nclass ArrayBinaryTree {\n #tree;\n\n /* \u5efa\u69cb\u5b50 */\n constructor(arr) {\n this.#tree = arr;\n }\n\n /* \u4e32\u5217\u5bb9\u91cf */\n size() {\n return this.#tree.length;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u503c */\n val(i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i &lt; 0 || i &gt;= this.size()) return null;\n return this.#tree[i];\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n left(i) {\n return 2 * i + 1;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n right(i) {\n return 2 * i + 2;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\n parent(i) {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n }\n\n /* \u5c64\u5e8f\u8d70\u8a2a */\n levelOrder() {\n let res = [];\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\n for (let i = 0; i &lt; this.size(); i++) {\n if (this.val(i) !== null) res.push(this.val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n #dfs(i, order, res) {\n // \u82e5\u70ba\u7a7a\u4f4d\uff0c\u5247\u8fd4\u56de\n if (this.val(i) === null) return;\n // \u524d\u5e8f\u8d70\u8a2a\n if (order === 'pre') res.push(this.val(i));\n this.#dfs(this.left(i), order, res);\n // \u4e2d\u5e8f\u8d70\u8a2a\n if (order === 'in') res.push(this.val(i));\n this.#dfs(this.right(i), order, res);\n // \u5f8c\u5e8f\u8d70\u8a2a\n if (order === 'post') res.push(this.val(i));\n }\n\n /* \u524d\u5e8f\u8d70\u8a2a */\n preOrder() {\n const res = [];\n this.#dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u8d70\u8a2a */\n inOrder() {\n const res = [];\n this.#dfs(0, 'in', res);\n return res;\n }\n\n /* \u5f8c\u5e8f\u8d70\u8a2a */\n postOrder() {\n const res = [];\n this.#dfs(0, 'post', res);\n return res;\n }\n}\n</code></pre> array_binary_tree.ts<pre><code>/* \u9663\u5217\u8868\u793a\u4e0b\u7684\u4e8c\u5143\u6a39\u985e\u5225 */\nclass ArrayBinaryTree {\n #tree: (number | null)[];\n\n /* \u5efa\u69cb\u5b50 */\n constructor(arr: (number | null)[]) {\n this.#tree = arr;\n }\n\n /* \u4e32\u5217\u5bb9\u91cf */\n size(): number {\n return this.#tree.length;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u503c */\n val(i: number): number | null {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i &lt; 0 || i &gt;= this.size()) return null;\n return this.#tree[i];\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n left(i: number): number {\n return 2 * i + 1;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n right(i: number): number {\n return 2 * i + 2;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\n parent(i: number): number {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n }\n\n /* \u5c64\u5e8f\u8d70\u8a2a */\n levelOrder(): number[] {\n let res = [];\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\n for (let i = 0; i &lt; this.size(); i++) {\n if (this.val(i) !== null) res.push(this.val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n #dfs(i: number, order: Order, res: (number | null)[]): void {\n // \u82e5\u70ba\u7a7a\u4f4d\uff0c\u5247\u8fd4\u56de\n if (this.val(i) === null) return;\n // \u524d\u5e8f\u8d70\u8a2a\n if (order === 'pre') res.push(this.val(i));\n this.#dfs(this.left(i), order, res);\n // \u4e2d\u5e8f\u8d70\u8a2a\n if (order === 'in') res.push(this.val(i));\n this.#dfs(this.right(i), order, res);\n // \u5f8c\u5e8f\u8d70\u8a2a\n if (order === 'post') res.push(this.val(i));\n }\n\n /* \u524d\u5e8f\u8d70\u8a2a */\n preOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u8d70\u8a2a */\n inOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'in', res);\n return res;\n }\n\n /* \u5f8c\u5e8f\u8d70\u8a2a */\n postOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'post', res);\n return res;\n }\n}\n</code></pre> array_binary_tree.dart<pre><code>/* \u9663\u5217\u8868\u793a\u4e0b\u7684\u4e8c\u5143\u6a39\u985e\u5225 */\nclass ArrayBinaryTree {\n late List&lt;int?&gt; _tree;\n\n /* \u5efa\u69cb\u5b50 */\n ArrayBinaryTree(this._tree);\n\n /* \u4e32\u5217\u5bb9\u91cf */\n int size() {\n return _tree.length;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u503c */\n int? val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i &lt; 0 || i &gt;= size()) {\n return null;\n }\n return _tree[i];\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n int? left(int i) {\n return 2 * i + 1;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n int? right(int i) {\n return 2 * i + 2;\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\n int? parent(int i) {\n return (i - 1) ~/ 2;\n }\n\n /* \u5c64\u5e8f\u8d70\u8a2a */\n List&lt;int&gt; levelOrder() {\n List&lt;int&gt; res = [];\n for (int i = 0; i &lt; size(); i++) {\n if (val(i) != null) {\n res.add(val(i)!);\n }\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n void dfs(int i, String order, List&lt;int?&gt; res) {\n // \u82e5\u70ba\u7a7a\u4f4d\uff0c\u5247\u8fd4\u56de\n if (val(i) == null) {\n return;\n }\n // \u524d\u5e8f\u8d70\u8a2a\n if (order == 'pre') {\n res.add(val(i));\n }\n dfs(left(i)!, order, res);\n // \u4e2d\u5e8f\u8d70\u8a2a\n if (order == 'in') {\n res.add(val(i));\n }\n dfs(right(i)!, order, res);\n // \u5f8c\u5e8f\u8d70\u8a2a\n if (order == 'post') {\n res.add(val(i));\n }\n }\n\n /* \u524d\u5e8f\u8d70\u8a2a */\n List&lt;int?&gt; preOrder() {\n List&lt;int?&gt; res = [];\n dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u8d70\u8a2a */\n List&lt;int?&gt; inOrder() {\n List&lt;int?&gt; res = [];\n dfs(0, 'in', res);\n return res;\n }\n\n /* \u5f8c\u5e8f\u8d70\u8a2a */\n List&lt;int?&gt; postOrder() {\n List&lt;int?&gt; res = [];\n dfs(0, 'post', res);\n return res;\n }\n}\n</code></pre> array_binary_tree.rs<pre><code>/* \u9663\u5217\u8868\u793a\u4e0b\u7684\u4e8c\u5143\u6a39\u985e\u5225 */\nstruct ArrayBinaryTree {\n tree: Vec&lt;Option&lt;i32&gt;&gt;,\n}\n\nimpl ArrayBinaryTree {\n /* \u5efa\u69cb\u5b50 */\n fn new(arr: Vec&lt;Option&lt;i32&gt;&gt;) -&gt; Self {\n Self { tree: arr }\n }\n\n /* \u4e32\u5217\u5bb9\u91cf */\n fn size(&amp;self) -&gt; i32 {\n self.tree.len() as i32\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u503c */\n fn val(&amp;self, i: i32) -&gt; Option&lt;i32&gt; {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i &lt; 0 || i &gt;= self.size() {\n None\n } else {\n self.tree[i as usize]\n }\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n fn left(&amp;self, i: i32) -&gt; i32 {\n 2 * i + 1\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n fn right(&amp;self, i: i32) -&gt; i32 {\n 2 * i + 2\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\n fn parent(&amp;self, i: i32) -&gt; i32 {\n (i - 1) / 2\n }\n\n /* \u5c64\u5e8f\u8d70\u8a2a */\n fn level_order(&amp;self) -&gt; Vec&lt;i32&gt; {\n let mut res = vec![];\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\n for i in 0..self.size() {\n if let Some(val) = self.val(i) {\n res.push(val)\n }\n }\n res\n }\n\n /* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n fn dfs(&amp;self, i: i32, order: &amp;str, res: &amp;mut Vec&lt;i32&gt;) {\n if self.val(i).is_none() {\n return;\n }\n let val = self.val(i).unwrap();\n // \u524d\u5e8f\u8d70\u8a2a\n if order == \"pre\" {\n res.push(val);\n }\n self.dfs(self.left(i), order, res);\n // \u4e2d\u5e8f\u8d70\u8a2a\n if order == \"in\" {\n res.push(val);\n }\n self.dfs(self.right(i), order, res);\n // \u5f8c\u5e8f\u8d70\u8a2a\n if order == \"post\" {\n res.push(val);\n }\n }\n\n /* \u524d\u5e8f\u8d70\u8a2a */\n fn pre_order(&amp;self) -&gt; Vec&lt;i32&gt; {\n let mut res = vec![];\n self.dfs(0, \"pre\", &amp;mut res);\n res\n }\n\n /* \u4e2d\u5e8f\u8d70\u8a2a */\n fn in_order(&amp;self) -&gt; Vec&lt;i32&gt; {\n let mut res = vec![];\n self.dfs(0, \"in\", &amp;mut res);\n res\n }\n\n /* \u5f8c\u5e8f\u8d70\u8a2a */\n fn post_order(&amp;self) -&gt; Vec&lt;i32&gt; {\n let mut res = vec![];\n self.dfs(0, \"post\", &amp;mut res);\n res\n }\n}\n</code></pre> array_binary_tree.c<pre><code>/* \u9663\u5217\u8868\u793a\u4e0b\u7684\u4e8c\u5143\u6a39\u7d50\u69cb\u9ad4 */\ntypedef struct {\n int *tree;\n int size;\n} ArrayBinaryTree;\n\n/* \u5efa\u69cb\u5b50 */\nArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {\n ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));\n abt-&gt;tree = malloc(sizeof(int) * arrSize);\n memcpy(abt-&gt;tree, arr, sizeof(int) * arrSize);\n abt-&gt;size = arrSize;\n return abt;\n}\n\n/* \u6790\u69cb\u51fd\u5f0f */\nvoid delArrayBinaryTree(ArrayBinaryTree *abt) {\n free(abt-&gt;tree);\n free(abt);\n}\n\n/* \u4e32\u5217\u5bb9\u91cf */\nint size(ArrayBinaryTree *abt) {\n return abt-&gt;size;\n}\n\n/* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u503c */\nint val(ArrayBinaryTree *abt, int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i &lt; 0 || i &gt;= size(abt))\n return INT_MAX;\n return abt-&gt;tree[i];\n}\n\n/* \u5c64\u5e8f\u8d70\u8a2a */\nint *levelOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\n for (int i = 0; i &lt; size(abt); i++) {\n if (val(abt, i) != INT_MAX)\n res[index++] = val(abt, i);\n }\n *returnSize = index;\n return res;\n}\n\n/* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\nvoid dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {\n // \u82e5\u70ba\u7a7a\u4f4d\uff0c\u5247\u8fd4\u56de\n if (val(abt, i) == INT_MAX)\n return;\n // \u524d\u5e8f\u8d70\u8a2a\n if (strcmp(order, \"pre\") == 0)\n res[(*index)++] = val(abt, i);\n dfs(abt, left(i), order, res, index);\n // \u4e2d\u5e8f\u8d70\u8a2a\n if (strcmp(order, \"in\") == 0)\n res[(*index)++] = val(abt, i);\n dfs(abt, right(i), order, res, index);\n // \u5f8c\u5e8f\u8d70\u8a2a\n if (strcmp(order, \"post\") == 0)\n res[(*index)++] = val(abt, i);\n}\n\n/* \u524d\u5e8f\u8d70\u8a2a */\nint *preOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"pre\", res, &amp;index);\n *returnSize = index;\n return res;\n}\n\n/* \u4e2d\u5e8f\u8d70\u8a2a */\nint *inOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"in\", res, &amp;index);\n *returnSize = index;\n return res;\n}\n\n/* \u5f8c\u5e8f\u8d70\u8a2a */\nint *postOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"post\", res, &amp;index);\n *returnSize = index;\n return res;\n}\n</code></pre> array_binary_tree.kt<pre><code>/* \u9663\u5217\u8868\u793a\u4e0b\u7684\u4e8c\u5143\u6a39\u985e\u5225 */\nclass ArrayBinaryTree(val tree: MutableList&lt;Int?&gt;) {\n /* \u4e32\u5217\u5bb9\u91cf */\n fun size(): Int {\n return tree.size\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u503c */\n fun _val(i: Int): Int? {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i &lt; 0 || i &gt;= size()) return null\n return tree[i]\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n fun left(i: Int): Int {\n return 2 * i + 1\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 */\n fun right(i: Int): Int {\n return 2 * i + 2\n }\n\n /* \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 */\n fun parent(i: Int): Int {\n return (i - 1) / 2\n }\n\n /* \u5c64\u5e8f\u8d70\u8a2a */\n fun levelOrder(): MutableList&lt;Int?&gt; {\n val res = mutableListOf&lt;Int?&gt;()\n // \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\n for (i in 0..&lt;size()) {\n if (_val(i) != null)\n res.add(_val(i))\n }\n return res\n }\n\n /* \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a */\n fun dfs(i: Int, order: String, res: MutableList&lt;Int?&gt;) {\n // \u82e5\u70ba\u7a7a\u4f4d\uff0c\u5247\u8fd4\u56de\n if (_val(i) == null)\n return\n // \u524d\u5e8f\u8d70\u8a2a\n if (\"pre\" == order)\n res.add(_val(i))\n dfs(left(i), order, res)\n // \u4e2d\u5e8f\u8d70\u8a2a\n if (\"in\" == order)\n res.add(_val(i))\n dfs(right(i), order, res)\n // \u5f8c\u5e8f\u8d70\u8a2a\n if (\"post\" == order)\n res.add(_val(i))\n }\n\n /* \u524d\u5e8f\u8d70\u8a2a */\n fun preOrder(): MutableList&lt;Int?&gt; {\n val res = mutableListOf&lt;Int?&gt;()\n dfs(0, \"pre\", res)\n return res\n }\n\n /* \u4e2d\u5e8f\u8d70\u8a2a */\n fun inOrder(): MutableList&lt;Int?&gt; {\n val res = mutableListOf&lt;Int?&gt;()\n dfs(0, \"in\", res)\n return res\n }\n\n /* \u5f8c\u5e8f\u8d70\u8a2a */\n fun postOrder(): MutableList&lt;Int?&gt; {\n val res = mutableListOf&lt;Int?&gt;()\n dfs(0, \"post\", res)\n return res\n }\n}\n</code></pre> array_binary_tree.rb<pre><code>### \u9663\u5217\u8868\u793a\u4e0b\u7684\u4e8c\u5143\u6a39\u985e\u5225 ###\nclass ArrayBinaryTree\n ### \u5efa\u69cb\u5b50 ###\n def initialize(arr)\n @tree = arr.to_a\n end\n\n ### \u4e32\u5217\u5bb9\u91cf ###\n def size\n @tree.length\n end\n\n ### \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u503c ###\n def val(i)\n # \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5247\u8fd4\u56de nil \uff0c\u4ee3\u8868\u7a7a\u4f4d\n return if i &lt; 0 || i &gt;= size\n\n @tree[i]\n end\n\n ### \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 ###\n def left(i)\n 2 * i + 1\n end\n\n ### \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u53f3\u5b50\u7bc0\u9ede\u7684\u7d22\u5f15 ###\n def right(i)\n 2 * i + 2\n end\n\n ### \u7372\u53d6\u7d22\u5f15\u70ba i \u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\u7684\u7d22\u5f15 ###\n def parent(i)\n (i - 1) / 2\n end\n\n ### \u5c64\u5e8f\u8d70\u8a2a ###\n def level_order\n @res = []\n\n # \u76f4\u63a5\u8d70\u8a2a\u9663\u5217\n for i in 0...size\n @res &lt;&lt; val(i) unless val(i).nil?\n end\n\n @res\n end\n\n ### \u6df1\u5ea6\u512a\u5148\u8d70\u8a2a ###\n def dfs(i, order)\n return if val(i).nil?\n # \u524d\u5e8f\u8d70\u8a2a\n @res &lt;&lt; val(i) if order == :pre\n dfs(left(i), order)\n # \u4e2d\u5e8f\u8d70\u8a2a\n @res &lt;&lt; val(i) if order == :in\n dfs(right(i), order)\n # \u5f8c\u5e8f\u8d70\u8a2a\n @res &lt;&lt; val(i) if order == :post\n end\n\n ### \u524d\u5e8f\u8d70\u8a2a ###\n def pre_order\n @res = []\n dfs(0, :pre)\n @res\n end\n\n ### \u4e2d\u5e8f\u8d70\u8a2a ###\n def in_order\n @res = []\n dfs(0, :in)\n @res\n end\n\n ### \u5f8c\u5e8f\u8d70\u8a2a ###\n def post_order\n @res = []\n dfs(0, :post)\n @res\n end\nend\n</code></pre> array_binary_tree.zig<pre><code>[class]{ArrayBinaryTree}-[func]{}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_tree/array_representation_of_tree/#733","title":"7.3.3 \u00a0 \u512a\u9ede\u8207\u4fb7\u9650\u6027","text":"<p>\u4e8c\u5143\u6a39\u7684\u9663\u5217\u8868\u793a\u4e3b\u8981\u6709\u4ee5\u4e0b\u512a\u9ede\u3002</p> <ul> <li>\u9663\u5217\u5132\u5b58\u5728\u9023\u7e8c\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\u4e2d\uff0c\u5c0d\u5feb\u53d6\u53cb\u597d\uff0c\u8a2a\u554f\u8207\u8d70\u8a2a\u901f\u5ea6\u8f03\u5feb\u3002</li> <li>\u4e0d\u9700\u8981\u5132\u5b58\u6307\u6a19\uff0c\u6bd4\u8f03\u7bc0\u7701\u7a7a\u9593\u3002</li> <li>\u5141\u8a31\u96a8\u6a5f\u8a2a\u554f\u7bc0\u9ede\u3002</li> </ul> <p>\u7136\u800c\uff0c\u9663\u5217\u8868\u793a\u4e5f\u5b58\u5728\u4e00\u4e9b\u4fb7\u9650\u6027\u3002</p> <ul> <li>\u9663\u5217\u5132\u5b58\u9700\u8981\u9023\u7e8c\u8a18\u61b6\u9ad4\u7a7a\u9593\uff0c\u56e0\u6b64\u4e0d\u9069\u5408\u5132\u5b58\u8cc7\u6599\u91cf\u904e\u5927\u7684\u6a39\u3002</li> <li>\u589e\u522a\u7bc0\u9ede\u9700\u8981\u900f\u904e\u9663\u5217\u63d2\u5165\u8207\u522a\u9664\u64cd\u4f5c\u5be6\u73fe\uff0c\u6548\u7387\u8f03\u4f4e\u3002</li> <li>\u7576\u4e8c\u5143\u6a39\u4e2d\u5b58\u5728\u5927\u91cf <code>None</code> \u6642\uff0c\u9663\u5217\u4e2d\u5305\u542b\u7684\u7bc0\u9ede\u8cc7\u6599\u6bd4\u91cd\u8f03\u4f4e\uff0c\u7a7a\u9593\u5229\u7528\u7387\u8f03\u4f4e\u3002</li> </ul>"},{"location":"chapter_tree/avl_tree/","title":"7.5 \u00a0 AVL \u6a39 *","text":"<p>\u5728\u201c\u4e8c\u5143\u641c\u5c0b\u6a39\u201d\u7ae0\u7bc0\u4e2d\u6211\u5011\u63d0\u5230\uff0c\u5728\u591a\u6b21\u63d2\u5165\u548c\u522a\u9664\u64cd\u4f5c\u5f8c\uff0c\u4e8c\u5143\u641c\u5c0b\u6a39\u53ef\u80fd\u9000\u5316\u70ba\u93c8\u7d50\u4e32\u5217\u3002\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u6240\u6709\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u5c07\u5f9e \\(O(\\log n)\\) \u52a3\u5316\u70ba \\(O(n)\\) \u3002</p> <p>\u5982\u5716 7-24 \u6240\u793a\uff0c\u7d93\u904e\u5169\u6b21\u522a\u9664\u7bc0\u9ede\u64cd\u4f5c\uff0c\u9019\u68f5\u4e8c\u5143\u641c\u5c0b\u6a39\u4fbf\u6703\u9000\u5316\u70ba\u93c8\u7d50\u4e32\u5217\u3002</p> <p></p> <p> \u5716 7-24 \u00a0 AVL \u6a39\u5728\u522a\u9664\u7bc0\u9ede\u5f8c\u767c\u751f\u9000\u5316 </p> <p>\u518d\u4f8b\u5982\uff0c\u5728\u5716 7-25 \u6240\u793a\u7684\u5b8c\u7f8e\u4e8c\u5143\u6a39\u4e2d\u63d2\u5165\u5169\u500b\u7bc0\u9ede\u5f8c\uff0c\u6a39\u5c07\u56b4\u91cd\u5411\u5de6\u50be\u659c\uff0c\u67e5\u8a62\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u4e5f\u96a8\u4e4b\u52a3\u5316\u3002</p> <p></p> <p> \u5716 7-25 \u00a0 AVL \u6a39\u5728\u63d2\u5165\u7bc0\u9ede\u5f8c\u767c\u751f\u9000\u5316 </p> <p>1962 \u5e74 G. M. Adelson-Velsky \u548c E. M. Landis \u5728\u8ad6\u6587\u201cAn algorithm for the organization of information\u201d\u4e2d\u63d0\u51fa\u4e86 AVL \u6a39\u3002\u8ad6\u6587\u4e2d\u8a73\u7d30\u63cf\u8ff0\u4e86\u4e00\u7cfb\u5217\u64cd\u4f5c\uff0c\u78ba\u4fdd\u5728\u6301\u7e8c\u65b0\u589e\u548c\u522a\u9664\u7bc0\u9ede\u5f8c\uff0cAVL \u6a39\u4e0d\u6703\u9000\u5316\uff0c\u5f9e\u800c\u4f7f\u5f97\u5404\u7a2e\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u4fdd\u6301\u5728 \\(O(\\log n)\\) \u7d1a\u5225\u3002\u63db\u53e5\u8a71\u8aaa\uff0c\u5728\u9700\u8981\u983b\u7e41\u9032\u884c\u589e\u522a\u67e5\u6539\u64cd\u4f5c\u7684\u5834\u666f\u4e2d\uff0cAVL \u6a39\u80fd\u59cb\u7d42\u4fdd\u6301\u9ad8\u6548\u7684\u8cc7\u6599\u64cd\u4f5c\u6548\u80fd\uff0c\u5177\u6709\u5f88\u597d\u7684\u61c9\u7528\u50f9\u503c\u3002</p>"},{"location":"chapter_tree/avl_tree/#751-avl","title":"7.5.1 \u00a0 AVL \u6a39\u5e38\u898b\u8853\u8a9e","text":"<p>AVL \u6a39\u65e2\u662f\u4e8c\u5143\u641c\u5c0b\u6a39\uff0c\u4e5f\u662f\u5e73\u8861\u4e8c\u5143\u6a39\uff0c\u540c\u6642\u6eff\u8db3\u9019\u5169\u985e\u4e8c\u5143\u6a39\u7684\u6240\u6709\u6027\u8cea\uff0c\u56e0\u6b64\u662f\u4e00\u7a2e\u5e73\u8861\u4e8c\u5143\u641c\u5c0b\u6a39\uff08balanced binary search tree\uff09\u3002</p>"},{"location":"chapter_tree/avl_tree/#1","title":"1. \u00a0 \u7bc0\u9ede\u9ad8\u5ea6","text":"<p>\u7531\u65bc AVL \u6a39\u7684\u76f8\u95dc\u64cd\u4f5c\u9700\u8981\u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6\uff0c\u56e0\u6b64\u6211\u5011\u9700\u8981\u70ba\u7bc0\u9ede\u985e\u5225\u65b0\u589e <code>height</code> \u8b8a\u6578\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code>class TreeNode:\n \"\"\"AVL \u6a39\u7bc0\u9ede\u985e\u5225\"\"\"\n def __init__(self, val: int):\n self.val: int = val # \u7bc0\u9ede\u503c\n self.height: int = 0 # \u7bc0\u9ede\u9ad8\u5ea6\n self.left: TreeNode | None = None # \u5de6\u5b50\u7bc0\u9ede\u5f15\u7528\n self.right: TreeNode | None = None # \u53f3\u5b50\u7bc0\u9ede\u5f15\u7528\n</code></pre> <pre><code>/* AVL \u6a39\u7bc0\u9ede\u985e\u5225 */\nstruct TreeNode {\n int val{}; // \u7bc0\u9ede\u503c\n int height = 0; // \u7bc0\u9ede\u9ad8\u5ea6\n TreeNode *left{}; // \u5de6\u5b50\u7bc0\u9ede\n TreeNode *right{}; // \u53f3\u5b50\u7bc0\u9ede\n TreeNode() = default;\n explicit TreeNode(int x) : val(x){}\n};\n</code></pre> <pre><code>/* AVL \u6a39\u7bc0\u9ede\u985e\u5225 */\nclass TreeNode {\n public int val; // \u7bc0\u9ede\u503c\n public int height; // \u7bc0\u9ede\u9ad8\u5ea6\n public TreeNode left; // \u5de6\u5b50\u7bc0\u9ede\n public TreeNode right; // \u53f3\u5b50\u7bc0\u9ede\n public TreeNode(int x) { val = x; }\n}\n</code></pre> <pre><code>/* AVL \u6a39\u7bc0\u9ede\u985e\u5225 */\nclass TreeNode(int? x) {\n public int? val = x; // \u7bc0\u9ede\u503c\n public int height; // \u7bc0\u9ede\u9ad8\u5ea6\n public TreeNode? left; // \u5de6\u5b50\u7bc0\u9ede\u5f15\u7528\n public TreeNode? right; // \u53f3\u5b50\u7bc0\u9ede\u5f15\u7528\n}\n</code></pre> <pre><code>/* AVL \u6a39\u7bc0\u9ede\u7d50\u69cb\u9ad4 */\ntype TreeNode struct {\n Val int // \u7bc0\u9ede\u503c\n Height int // \u7bc0\u9ede\u9ad8\u5ea6\n Left *TreeNode // \u5de6\u5b50\u7bc0\u9ede\u5f15\u7528\n Right *TreeNode // \u53f3\u5b50\u7bc0\u9ede\u5f15\u7528\n}\n</code></pre> <pre><code>/* AVL \u6a39\u7bc0\u9ede\u985e\u5225 */\nclass TreeNode {\n var val: Int // \u7bc0\u9ede\u503c\n var height: Int // \u7bc0\u9ede\u9ad8\u5ea6\n var left: TreeNode? // \u5de6\u5b50\u7bc0\u9ede\n var right: TreeNode? // \u53f3\u5b50\u7bc0\u9ede\n\n init(x: Int) {\n val = x\n height = 0\n }\n}\n</code></pre> <pre><code>/* AVL \u6a39\u7bc0\u9ede\u985e\u5225 */\nclass TreeNode {\n val; // \u7bc0\u9ede\u503c\n height; //\u7bc0\u9ede\u9ad8\u5ea6\n left; // \u5de6\u5b50\u7bc0\u9ede\u6307\u6a19\n right; // \u53f3\u5b50\u7bc0\u9ede\u6307\u6a19\n constructor(val, left, right, height) {\n this.val = val === undefined ? 0 : val;\n this.height = height === undefined ? 0 : height;\n this.left = left === undefined ? null : left;\n this.right = right === undefined ? null : right;\n }\n}\n</code></pre> <pre><code>/* AVL \u6a39\u7bc0\u9ede\u985e\u5225 */\nclass TreeNode {\n val: number; // \u7bc0\u9ede\u503c\n height: number; // \u7bc0\u9ede\u9ad8\u5ea6\n left: TreeNode | null; // \u5de6\u5b50\u7bc0\u9ede\u6307\u6a19\n right: TreeNode | null; // \u53f3\u5b50\u7bc0\u9ede\u6307\u6a19\n constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\n this.val = val === undefined ? 0 : val;\n this.height = height === undefined ? 0 : height;\n this.left = left === undefined ? null : left;\n this.right = right === undefined ? null : right;\n }\n}\n</code></pre> <pre><code>/* AVL \u6a39\u7bc0\u9ede\u985e\u5225 */\nclass TreeNode {\n int val; // \u7bc0\u9ede\u503c\n int height; // \u7bc0\u9ede\u9ad8\u5ea6\n TreeNode? left; // \u5de6\u5b50\u7bc0\u9ede\n TreeNode? right; // \u53f3\u5b50\u7bc0\u9ede\n TreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n</code></pre> <pre><code>use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* AVL \u6a39\u7bc0\u9ede\u7d50\u69cb\u9ad4 */\nstruct TreeNode {\n val: i32, // \u7bc0\u9ede\u503c\n height: i32, // \u7bc0\u9ede\u9ad8\u5ea6\n left: Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, // \u5de6\u5b50\u7bc0\u9ede\n right: Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, // \u53f3\u5b50\u7bc0\u9ede\n}\n\nimpl TreeNode {\n /* \u5efa\u69cb\u5b50 */\n fn new(val: i32) -&gt; Rc&lt;RefCell&lt;Self&gt;&gt; {\n Rc::new(RefCell::new(Self {\n val,\n height: 0,\n left: None,\n right: None\n }))\n }\n}\n</code></pre> <pre><code>/* AVL \u6a39\u7bc0\u9ede\u7d50\u69cb\u9ad4 */\nTreeNode struct TreeNode {\n int val;\n int height;\n struct TreeNode *left;\n struct TreeNode *right;\n} TreeNode;\n\n/* \u5efa\u69cb\u5b50 */\nTreeNode *newTreeNode(int val) {\n TreeNode *node;\n\n node = (TreeNode *)malloc(sizeof(TreeNode));\n node-&gt;val = val;\n node-&gt;height = 0;\n node-&gt;left = NULL;\n node-&gt;right = NULL;\n return node;\n}\n</code></pre> <pre><code>/* AVL \u6a39\u7bc0\u9ede\u985e\u5225 */\nclass TreeNode(val _val: Int) { // \u7bc0\u9ede\u503c\n val height: Int = 0 // \u7bc0\u9ede\u9ad8\u5ea6\n val left: TreeNode? = null // \u5de6\u5b50\u7bc0\u9ede\n val right: TreeNode? = null // \u53f3\u5b50\u7bc0\u9ede\n}\n</code></pre> <pre><code>### AVL \u6a39\u7bc0\u9ede\u985e\u5225 ###\nclass TreeNode\n attr_accessor :val # \u7bc0\u9ede\u503c\n attr_accessor :height # \u7bc0\u9ede\u9ad8\u5ea6\n attr_accessor :left # \u5de6\u5b50\u7bc0\u9ede\u5f15\u7528\n attr_accessor :right # \u53f3\u5b50\u7bc0\u9ede\u5f15\u7528\n\n def initialize(val)\n @val = val\n @height = 0\n end\nend\n</code></pre> <pre><code>\n</code></pre> <p>\u201c\u7bc0\u9ede\u9ad8\u5ea6\u201d\u662f\u6307\u5f9e\u8a72\u7bc0\u9ede\u5230\u5b83\u7684\u6700\u9060\u8449\u7bc0\u9ede\u7684\u8ddd\u96e2\uff0c\u5373\u6240\u7d93\u904e\u7684\u201c\u908a\u201d\u7684\u6578\u91cf\u3002\u9700\u8981\u7279\u5225\u6ce8\u610f\u7684\u662f\uff0c\u8449\u7bc0\u9ede\u7684\u9ad8\u5ea6\u70ba \\(0\\) \uff0c\u800c\u7a7a\u7bc0\u9ede\u7684\u9ad8\u5ea6\u70ba \\(-1\\) \u3002\u6211\u5011\u5c07\u5efa\u7acb\u5169\u500b\u5de5\u5177\u51fd\u5f0f\uff0c\u5206\u5225\u7528\u65bc\u7372\u53d6\u548c\u66f4\u65b0\u7bc0\u9ede\u7684\u9ad8\u5ea6\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.py<pre><code>def height(self, node: TreeNode | None) -&gt; int:\n \"\"\"\u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6\"\"\"\n # \u7a7a\u7bc0\u9ede\u9ad8\u5ea6\u70ba -1 \uff0c\u8449\u7bc0\u9ede\u9ad8\u5ea6\u70ba 0\n if node is not None:\n return node.height\n return -1\n\ndef update_height(self, node: TreeNode | None):\n \"\"\"\u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\"\"\"\n # \u7bc0\u9ede\u9ad8\u5ea6\u7b49\u65bc\u6700\u9ad8\u5b50\u6a39\u9ad8\u5ea6 + 1\n node.height = max([self.height(node.left), self.height(node.right)]) + 1\n</code></pre> avl_tree.cpp<pre><code>/* \u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n // \u7a7a\u7bc0\u9ede\u9ad8\u5ea6\u70ba -1 \uff0c\u8449\u7bc0\u9ede\u9ad8\u5ea6\u70ba 0\n return node == nullptr ? -1 : node-&gt;height;\n}\n\n/* \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n // \u7bc0\u9ede\u9ad8\u5ea6\u7b49\u65bc\u6700\u9ad8\u5b50\u6a39\u9ad8\u5ea6 + 1\n node-&gt;height = max(height(node-&gt;left), height(node-&gt;right)) + 1;\n}\n</code></pre> avl_tree.java<pre><code>/* \u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6 */\nint height(TreeNode node) {\n // \u7a7a\u7bc0\u9ede\u9ad8\u5ea6\u70ba -1 \uff0c\u8449\u7bc0\u9ede\u9ad8\u5ea6\u70ba 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode node) {\n // \u7bc0\u9ede\u9ad8\u5ea6\u7b49\u65bc\u6700\u9ad8\u5b50\u6a39\u9ad8\u5ea6 + 1\n node.height = Math.max(height(node.left), height(node.right)) + 1;\n}\n</code></pre> avl_tree.cs<pre><code>/* \u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6 */\nint Height(TreeNode? node) {\n // \u7a7a\u7bc0\u9ede\u9ad8\u5ea6\u70ba -1 \uff0c\u8449\u7bc0\u9ede\u9ad8\u5ea6\u70ba 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6 */\nvoid UpdateHeight(TreeNode node) {\n // \u7bc0\u9ede\u9ad8\u5ea6\u7b49\u65bc\u6700\u9ad8\u5b50\u6a39\u9ad8\u5ea6 + 1\n node.height = Math.Max(Height(node.left), Height(node.right)) + 1;\n}\n</code></pre> avl_tree.go<pre><code>/* \u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6 */\nfunc (t *aVLTree) height(node *TreeNode) int {\n // \u7a7a\u7bc0\u9ede\u9ad8\u5ea6\u70ba -1 \uff0c\u8449\u7bc0\u9ede\u9ad8\u5ea6\u70ba 0\n if node != nil {\n return node.Height\n }\n return -1\n}\n\n/* \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6 */\nfunc (t *aVLTree) updateHeight(node *TreeNode) {\n lh := t.height(node.Left)\n rh := t.height(node.Right)\n // \u7bc0\u9ede\u9ad8\u5ea6\u7b49\u65bc\u6700\u9ad8\u5b50\u6a39\u9ad8\u5ea6 + 1\n if lh &gt; rh {\n node.Height = lh + 1\n } else {\n node.Height = rh + 1\n }\n}\n</code></pre> avl_tree.swift<pre><code>/* \u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6 */\nfunc height(node: TreeNode?) -&gt; Int {\n // \u7a7a\u7bc0\u9ede\u9ad8\u5ea6\u70ba -1 \uff0c\u8449\u7bc0\u9ede\u9ad8\u5ea6\u70ba 0\n node?.height ?? -1\n}\n\n/* \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6 */\nfunc updateHeight(node: TreeNode?) {\n // \u7bc0\u9ede\u9ad8\u5ea6\u7b49\u65bc\u6700\u9ad8\u5b50\u6a39\u9ad8\u5ea6 + 1\n node?.height = max(height(node: node?.left), height(node: node?.right)) + 1\n}\n</code></pre> avl_tree.js<pre><code>/* \u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6 */\nheight(node) {\n // \u7a7a\u7bc0\u9ede\u9ad8\u5ea6\u70ba -1 \uff0c\u8449\u7bc0\u9ede\u9ad8\u5ea6\u70ba 0\n return node === null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6 */\n#updateHeight(node) {\n // \u7bc0\u9ede\u9ad8\u5ea6\u7b49\u65bc\u6700\u9ad8\u5b50\u6a39\u9ad8\u5ea6 + 1\n node.height =\n Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6 */\nheight(node: TreeNode): number {\n // \u7a7a\u7bc0\u9ede\u9ad8\u5ea6\u70ba -1 \uff0c\u8449\u7bc0\u9ede\u9ad8\u5ea6\u70ba 0\n return node === null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6 */\nupdateHeight(node: TreeNode): void {\n // \u7bc0\u9ede\u9ad8\u5ea6\u7b49\u65bc\u6700\u9ad8\u5b50\u6a39\u9ad8\u5ea6 + 1\n node.height =\n Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n</code></pre> avl_tree.dart<pre><code>/* \u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6 */\nint height(TreeNode? node) {\n // \u7a7a\u7bc0\u9ede\u9ad8\u5ea6\u70ba -1 \uff0c\u8449\u7bc0\u9ede\u9ad8\u5ea6\u70ba 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode? node) {\n // \u7bc0\u9ede\u9ad8\u5ea6\u7b49\u65bc\u6700\u9ad8\u5b50\u6a39\u9ad8\u5ea6 + 1\n node!.height = max(height(node.left), height(node.right)) + 1;\n}\n</code></pre> avl_tree.rs<pre><code>/* \u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6 */\nfn height(node: OptionTreeNodeRc) -&gt; i32 {\n // \u7a7a\u7bc0\u9ede\u9ad8\u5ea6\u70ba -1 \uff0c\u8449\u7bc0\u9ede\u9ad8\u5ea6\u70ba 0\n match node {\n Some(node) =&gt; node.borrow().height,\n None =&gt; -1,\n }\n}\n\n/* \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6 */\nfn update_height(node: OptionTreeNodeRc) {\n if let Some(node) = node {\n let left = node.borrow().left.clone();\n let right = node.borrow().right.clone();\n // \u7bc0\u9ede\u9ad8\u5ea6\u7b49\u65bc\u6700\u9ad8\u5b50\u6a39\u9ad8\u5ea6 + 1\n node.borrow_mut().height = std::cmp::max(Self::height(left), Self::height(right)) + 1;\n }\n}\n</code></pre> avl_tree.c<pre><code>/* \u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n // \u7a7a\u7bc0\u9ede\u9ad8\u5ea6\u70ba -1 \uff0c\u8449\u7bc0\u9ede\u9ad8\u5ea6\u70ba 0\n if (node != NULL) {\n return node-&gt;height;\n }\n return -1;\n}\n\n/* \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n int lh = height(node-&gt;left);\n int rh = height(node-&gt;right);\n // \u7bc0\u9ede\u9ad8\u5ea6\u7b49\u65bc\u6700\u9ad8\u5b50\u6a39\u9ad8\u5ea6 + 1\n if (lh &gt; rh) {\n node-&gt;height = lh + 1;\n } else {\n node-&gt;height = rh + 1;\n }\n}\n</code></pre> avl_tree.kt<pre><code>/* \u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6 */\nfun height(node: TreeNode?): Int {\n // \u7a7a\u7bc0\u9ede\u9ad8\u5ea6\u70ba -1 \uff0c\u8449\u7bc0\u9ede\u9ad8\u5ea6\u70ba 0\n return node?.height ?: -1\n}\n\n/* \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6 */\nfun updateHeight(node: TreeNode?) {\n // \u7bc0\u9ede\u9ad8\u5ea6\u7b49\u65bc\u6700\u9ad8\u5b50\u6a39\u9ad8\u5ea6 + 1\n node?.height = max(height(node?.left), height(node?.right)) + 1\n}\n</code></pre> avl_tree.rb<pre><code>### \u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6 ###\ndef height(node)\n # \u7a7a\u7bc0\u9ede\u9ad8\u5ea6\u70ba -1 \uff0c\u8449\u7bc0\u9ede\u9ad8\u5ea6\u70ba 0\n return node.height unless node.nil?\n\n -1\nend\n\n### \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6 ###\ndef update_height(node)\n # \u7bc0\u9ede\u9ad8\u5ea6\u7b49\u65bc\u6700\u9ad8\u5b50\u6a39\u9ad8\u5ea6 + 1\n node.height = [height(node.left), height(node.right)].max + 1\nend\n</code></pre> avl_tree.zig<pre><code>// \u7372\u53d6\u7bc0\u9ede\u9ad8\u5ea6\nfn height(self: *Self, node: ?*inc.TreeNode(T)) i32 {\n _ = self;\n // \u7a7a\u7bc0\u9ede\u9ad8\u5ea6\u70ba -1 \uff0c\u8449\u7bc0\u9ede\u9ad8\u5ea6\u70ba 0\n return if (node == null) -1 else node.?.height;\n}\n\n// \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\nfn updateHeight(self: *Self, node: ?*inc.TreeNode(T)) void {\n // \u7bc0\u9ede\u9ad8\u5ea6\u7b49\u65bc\u6700\u9ad8\u5b50\u6a39\u9ad8\u5ea6 + 1\n node.?.height = @max(self.height(node.?.left), self.height(node.?.right)) + 1;\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#2","title":"2. \u00a0 \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50","text":"<p>\u7bc0\u9ede\u7684\u5e73\u8861\u56e0\u5b50\uff08balance factor\uff09\u5b9a\u7fa9\u70ba\u7bc0\u9ede\u5de6\u5b50\u6a39\u7684\u9ad8\u5ea6\u6e1b\u53bb\u53f3\u5b50\u6a39\u7684\u9ad8\u5ea6\uff0c\u540c\u6642\u898f\u5b9a\u7a7a\u7bc0\u9ede\u7684\u5e73\u8861\u56e0\u5b50\u70ba \\(0\\) \u3002\u6211\u5011\u540c\u6a23\u5c07\u7372\u53d6\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u7684\u529f\u80fd\u5c01\u88dd\u6210\u51fd\u5f0f\uff0c\u65b9\u4fbf\u5f8c\u7e8c\u4f7f\u7528\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.py<pre><code>def balance_factor(self, node: TreeNode | None) -&gt; int:\n \"\"\"\u7372\u53d6\u5e73\u8861\u56e0\u5b50\"\"\"\n # \u7a7a\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u70ba 0\n if node is None:\n return 0\n # \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6a39\u9ad8\u5ea6 - \u53f3\u5b50\u6a39\u9ad8\u5ea6\n return self.height(node.left) - self.height(node.right)\n</code></pre> avl_tree.cpp<pre><code>/* \u7372\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n // \u7a7a\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u70ba 0\n if (node == nullptr)\n return 0;\n // \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6a39\u9ad8\u5ea6 - \u53f3\u5b50\u6a39\u9ad8\u5ea6\n return height(node-&gt;left) - height(node-&gt;right);\n}\n</code></pre> avl_tree.java<pre><code>/* \u7372\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode node) {\n // \u7a7a\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u70ba 0\n if (node == null)\n return 0;\n // \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6a39\u9ad8\u5ea6 - \u53f3\u5b50\u6a39\u9ad8\u5ea6\n return height(node.left) - height(node.right);\n}\n</code></pre> avl_tree.cs<pre><code>/* \u7372\u53d6\u5e73\u8861\u56e0\u5b50 */\nint BalanceFactor(TreeNode? node) {\n // \u7a7a\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u70ba 0\n if (node == null) return 0;\n // \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6a39\u9ad8\u5ea6 - \u53f3\u5b50\u6a39\u9ad8\u5ea6\n return Height(node.left) - Height(node.right);\n}\n</code></pre> avl_tree.go<pre><code>/* \u7372\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc (t *aVLTree) balanceFactor(node *TreeNode) int {\n // \u7a7a\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u70ba 0\n if node == nil {\n return 0\n }\n // \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6a39\u9ad8\u5ea6 - \u53f3\u5b50\u6a39\u9ad8\u5ea6\n return t.height(node.Left) - t.height(node.Right)\n}\n</code></pre> avl_tree.swift<pre><code>/* \u7372\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc balanceFactor(node: TreeNode?) -&gt; Int {\n // \u7a7a\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u70ba 0\n guard let node = node else { return 0 }\n // \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6a39\u9ad8\u5ea6 - \u53f3\u5b50\u6a39\u9ad8\u5ea6\n return height(node: node.left) - height(node: node.right)\n}\n</code></pre> avl_tree.js<pre><code>/* \u7372\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node) {\n // \u7a7a\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u70ba 0\n if (node === null) return 0;\n // \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6a39\u9ad8\u5ea6 - \u53f3\u5b50\u6a39\u9ad8\u5ea6\n return this.height(node.left) - this.height(node.right);\n}\n</code></pre> avl_tree.ts<pre><code>/* \u7372\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node: TreeNode): number {\n // \u7a7a\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u70ba 0\n if (node === null) return 0;\n // \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6a39\u9ad8\u5ea6 - \u53f3\u5b50\u6a39\u9ad8\u5ea6\n return this.height(node.left) - this.height(node.right);\n}\n</code></pre> avl_tree.dart<pre><code>/* \u7372\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode? node) {\n // \u7a7a\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u70ba 0\n if (node == null) return 0;\n // \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6a39\u9ad8\u5ea6 - \u53f3\u5b50\u6a39\u9ad8\u5ea6\n return height(node.left) - height(node.right);\n}\n</code></pre> avl_tree.rs<pre><code>/* \u7372\u53d6\u5e73\u8861\u56e0\u5b50 */\nfn balance_factor(node: OptionTreeNodeRc) -&gt; i32 {\n match node {\n // \u7a7a\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u70ba 0\n None =&gt; 0,\n // \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6a39\u9ad8\u5ea6 - \u53f3\u5b50\u6a39\u9ad8\u5ea6\n Some(node) =&gt; {\n Self::height(node.borrow().left.clone()) - Self::height(node.borrow().right.clone())\n }\n }\n}\n</code></pre> avl_tree.c<pre><code>/* \u7372\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n // \u7a7a\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u70ba 0\n if (node == NULL) {\n return 0;\n }\n // \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6a39\u9ad8\u5ea6 - \u53f3\u5b50\u6a39\u9ad8\u5ea6\n return height(node-&gt;left) - height(node-&gt;right);\n}\n</code></pre> avl_tree.kt<pre><code>/* \u7372\u53d6\u5e73\u8861\u56e0\u5b50 */\nfun balanceFactor(node: TreeNode?): Int {\n // \u7a7a\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u70ba 0\n if (node == null) return 0\n // \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6a39\u9ad8\u5ea6 - \u53f3\u5b50\u6a39\u9ad8\u5ea6\n return height(node.left) - height(node.right)\n}\n</code></pre> avl_tree.rb<pre><code>### \u7372\u53d6\u5e73\u8861\u56e0\u5b50 ###\ndef balance_factor(node)\n # \u7a7a\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u70ba 0\n return 0 if node.nil?\n\n # \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6a39\u9ad8\u5ea6 - \u53f3\u5b50\u6a39\u9ad8\u5ea6\n height(node.left) - height(node.right)\nend\n</code></pre> avl_tree.zig<pre><code>// \u7372\u53d6\u5e73\u8861\u56e0\u5b50\nfn balanceFactor(self: *Self, node: ?*inc.TreeNode(T)) i32 {\n // \u7a7a\u7bc0\u9ede\u5e73\u8861\u56e0\u5b50\u70ba 0\n if (node == null) return 0;\n // \u7bc0\u9ede\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6a39\u9ad8\u5ea6 - \u53f3\u5b50\u6a39\u9ad8\u5ea6\n return self.height(node.?.left) - self.height(node.?.right);\n}\n</code></pre> <p>Tip</p> <p>\u8a2d\u5e73\u8861\u56e0\u5b50\u70ba \\(f\\) \uff0c\u5247\u4e00\u68f5 AVL \u6a39\u7684\u4efb\u610f\u7bc0\u9ede\u7684\u5e73\u8861\u56e0\u5b50\u7686\u6eff\u8db3 \\(-1 \\le f \\le 1\\) \u3002</p>"},{"location":"chapter_tree/avl_tree/#752-avl","title":"7.5.2 \u00a0 AVL \u6a39\u65cb\u8f49","text":"<p>AVL \u6a39\u7684\u7279\u9ede\u5728\u65bc\u201c\u65cb\u8f49\u201d\u64cd\u4f5c\uff0c\u5b83\u80fd\u5920\u5728\u4e0d\u5f71\u97ff\u4e8c\u5143\u6a39\u7684\u4e2d\u5e8f\u8d70\u8a2a\u5e8f\u5217\u7684\u524d\u63d0\u4e0b\uff0c\u4f7f\u5931\u8861\u7bc0\u9ede\u91cd\u65b0\u6062\u5fa9\u5e73\u8861\u3002\u63db\u53e5\u8a71\u8aaa\uff0c\u65cb\u8f49\u64cd\u4f5c\u65e2\u80fd\u4fdd\u6301\u201c\u4e8c\u5143\u641c\u5c0b\u6a39\u201d\u7684\u6027\u8cea\uff0c\u4e5f\u80fd\u4f7f\u6a39\u91cd\u65b0\u8b8a\u70ba\u201c\u5e73\u8861\u4e8c\u5143\u6a39\u201d\u3002</p> <p>\u6211\u5011\u5c07\u5e73\u8861\u56e0\u5b50\u7d55\u5c0d\u503c \\(&gt; 1\\) \u7684\u7bc0\u9ede\u7a31\u70ba\u201c\u5931\u8861\u7bc0\u9ede\u201d\u3002\u6839\u64da\u7bc0\u9ede\u5931\u8861\u60c5\u6cc1\u7684\u4e0d\u540c\uff0c\u65cb\u8f49\u64cd\u4f5c\u5206\u70ba\u56db\u7a2e\uff1a\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\u3002\u4e0b\u9762\u8a73\u7d30\u4ecb\u7d39\u9019\u4e9b\u65cb\u8f49\u64cd\u4f5c\u3002</p>"},{"location":"chapter_tree/avl_tree/#1_1","title":"1. \u00a0 \u53f3\u65cb","text":"<p>\u5982\u5716 7-26 \u6240\u793a\uff0c\u7bc0\u9ede\u4e0b\u65b9\u70ba\u5e73\u8861\u56e0\u5b50\u3002\u5f9e\u5e95\u81f3\u9802\u770b\uff0c\u4e8c\u5143\u6a39\u4e2d\u9996\u500b\u5931\u8861\u7bc0\u9ede\u662f\u201c\u7bc0\u9ede 3\u201d\u3002\u6211\u5011\u95dc\u6ce8\u4ee5\u8a72\u5931\u8861\u7bc0\u9ede\u70ba\u6839\u7bc0\u9ede\u7684\u5b50\u6a39\uff0c\u5c07\u8a72\u7bc0\u9ede\u8a18\u70ba <code>node</code> \uff0c\u5176\u5de6\u5b50\u7bc0\u9ede\u8a18\u70ba <code>child</code> \uff0c\u57f7\u884c\u201c\u53f3\u65cb\u201d\u64cd\u4f5c\u3002\u5b8c\u6210\u53f3\u65cb\u5f8c\uff0c\u5b50\u6a39\u6062\u5fa9\u5e73\u8861\uff0c\u4e26\u4e14\u4ecd\u7136\u4fdd\u6301\u4e8c\u5143\u641c\u5c0b\u6a39\u7684\u6027\u8cea\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt; <p></p> <p></p> <p></p> <p></p> <p> \u5716 7-26 \u00a0 \u53f3\u65cb\u64cd\u4f5c\u6b65\u9a5f </p> <p>\u5982\u5716 7-27 \u6240\u793a\uff0c\u7576\u7bc0\u9ede <code>child</code> \u6709\u53f3\u5b50\u7bc0\u9ede\uff08\u8a18\u70ba <code>grand_child</code> \uff09\u6642\uff0c\u9700\u8981\u5728\u53f3\u65cb\u4e2d\u65b0\u589e\u4e00\u6b65\uff1a\u5c07 <code>grand_child</code> \u4f5c\u70ba <code>node</code> \u7684\u5de6\u5b50\u7bc0\u9ede\u3002</p> <p></p> <p> \u5716 7-27 \u00a0 \u6709 grand_child \u7684\u53f3\u65cb\u64cd\u4f5c </p> <p>\u201c\u5411\u53f3\u65cb\u8f49\u201d\u662f\u4e00\u7a2e\u5f62\u8c61\u5316\u7684\u8aaa\u6cd5\uff0c\u5be6\u969b\u4e0a\u9700\u8981\u900f\u904e\u4fee\u6539\u7bc0\u9ede\u6307\u6a19\u4f86\u5be6\u73fe\uff0c\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.py<pre><code>def right_rotate(self, node: TreeNode | None) -&gt; TreeNode | None:\n \"\"\"\u53f3\u65cb\u64cd\u4f5c\"\"\"\n child = node.left\n grand_child = child.right\n # \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u53f3\u65cb\u8f49\n child.right = node\n node.left = grand_child\n # \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n self.update_height(node)\n self.update_height(child)\n # \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child\n</code></pre> avl_tree.cpp<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n TreeNode *child = node-&gt;left;\n TreeNode *grandChild = child-&gt;right;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u53f3\u65cb\u8f49\n child-&gt;right = node;\n node-&gt;left = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre> avl_tree.java<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode rightRotate(TreeNode node) {\n TreeNode child = node.left;\n TreeNode grandChild = child.right;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u53f3\u65cb\u8f49\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre> avl_tree.cs<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? RightRotate(TreeNode? node) {\n TreeNode? child = node?.left;\n TreeNode? grandChild = child?.right;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u53f3\u65cb\u8f49\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n UpdateHeight(node);\n UpdateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre> avl_tree.go<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {\n child := node.Left\n grandChild := child.Right\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u53f3\u65cb\u8f49\n child.Right = node\n node.Left = grandChild\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n t.updateHeight(node)\n t.updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child\n}\n</code></pre> avl_tree.swift<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nfunc rightRotate(node: TreeNode?) -&gt; TreeNode? {\n let child = node?.left\n let grandChild = child?.right\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u53f3\u65cb\u8f49\n child?.right = node\n node?.left = grandChild\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n updateHeight(node: node)\n updateHeight(node: child)\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child\n}\n</code></pre> avl_tree.js<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\n#rightRotate(node) {\n const child = node.left;\n const grandChild = child.right;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u53f3\u65cb\u8f49\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n this.#updateHeight(node);\n this.#updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nrightRotate(node: TreeNode): TreeNode {\n const child = node.left;\n const grandChild = child.right;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u53f3\u65cb\u8f49\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n this.updateHeight(node);\n this.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre> avl_tree.dart<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? rightRotate(TreeNode? node) {\n TreeNode? child = node!.left;\n TreeNode? grandChild = child!.right;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u53f3\u65cb\u8f49\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre> avl_tree.rs<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nfn right_rotate(node: OptionTreeNodeRc) -&gt; OptionTreeNodeRc {\n match node {\n Some(node) =&gt; {\n let child = node.borrow().left.clone().unwrap();\n let grand_child = child.borrow().right.clone();\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u53f3\u65cb\u8f49\n child.borrow_mut().right = Some(node.clone());\n node.borrow_mut().left = grand_child;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n Self::update_height(Some(node));\n Self::update_height(Some(child.clone()));\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n Some(child)\n }\n None =&gt; None,\n }\n}\n</code></pre> avl_tree.c<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n TreeNode *child, *grandChild;\n child = node-&gt;left;\n grandChild = child-&gt;right;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u53f3\u65cb\u8f49\n child-&gt;right = node;\n node-&gt;left = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre> avl_tree.kt<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nfun rightRotate(node: TreeNode?): TreeNode {\n val child = node!!.left\n val grandChild = child!!.right\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u53f3\u65cb\u8f49\n child.right = node\n node.left = grandChild\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n updateHeight(node)\n updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child\n}\n</code></pre> avl_tree.rb<pre><code>### \u53f3\u65cb\u64cd\u4f5c ###\ndef right_rotate(node)\n child = node.left\n grand_child = child.right\n # \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u53f3\u65cb\u8f49\n child.right = node\n node.left = grand_child\n # \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n update_height(node)\n update_height(child)\n # \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n child\nend\n</code></pre> avl_tree.zig<pre><code>// \u53f3\u65cb\u64cd\u4f5c\nfn rightRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n var child = node.?.left;\n var grandChild = child.?.right;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u53f3\u65cb\u8f49\n child.?.right = node;\n node.?.left = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n self.updateHeight(node);\n self.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#2_1","title":"2. \u00a0 \u5de6\u65cb","text":"<p>\u76f8\u61c9\u5730\uff0c\u5982\u679c\u8003\u616e\u4e0a\u8ff0\u5931\u8861\u4e8c\u5143\u6a39\u7684\u201c\u6620\u8c61\u201d\uff0c\u5247\u9700\u8981\u57f7\u884c\u5716 7-28 \u6240\u793a\u7684\u201c\u5de6\u65cb\u201d\u64cd\u4f5c\u3002</p> <p></p> <p> \u5716 7-28 \u00a0 \u5de6\u65cb\u64cd\u4f5c </p> <p>\u540c\u7406\uff0c\u5982\u5716 7-29 \u6240\u793a\uff0c\u7576\u7bc0\u9ede <code>child</code> \u6709\u5de6\u5b50\u7bc0\u9ede\uff08\u8a18\u70ba <code>grand_child</code> \uff09\u6642\uff0c\u9700\u8981\u5728\u5de6\u65cb\u4e2d\u65b0\u589e\u4e00\u6b65\uff1a\u5c07 <code>grand_child</code> \u4f5c\u70ba <code>node</code> \u7684\u53f3\u5b50\u7bc0\u9ede\u3002</p> <p></p> <p> \u5716 7-29 \u00a0 \u6709 grand_child \u7684\u5de6\u65cb\u64cd\u4f5c </p> <p>\u53ef\u4ee5\u89c0\u5bdf\u5230\uff0c\u53f3\u65cb\u548c\u5de6\u65cb\u64cd\u4f5c\u5728\u908f\u8f2f\u4e0a\u662f\u6620\u8c61\u5c0d\u7a31\u7684\uff0c\u5b83\u5011\u5206\u5225\u89e3\u6c7a\u7684\u5169\u7a2e\u5931\u8861\u60c5\u6cc1\u4e5f\u662f\u5c0d\u7a31\u7684\u3002\u57fa\u65bc\u5c0d\u7a31\u6027\uff0c\u6211\u5011\u53ea\u9700\u5c07\u53f3\u65cb\u7684\u5be6\u73fe\u7a0b\u5f0f\u78bc\u4e2d\u7684\u6240\u6709\u7684 <code>left</code> \u66ff\u63db\u70ba <code>right</code> \uff0c\u5c07\u6240\u6709\u7684 <code>right</code> \u66ff\u63db\u70ba <code>left</code> \uff0c\u5373\u53ef\u5f97\u5230\u5de6\u65cb\u7684\u5be6\u73fe\u7a0b\u5f0f\u78bc\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.py<pre><code>def left_rotate(self, node: TreeNode | None) -&gt; TreeNode | None:\n \"\"\"\u5de6\u65cb\u64cd\u4f5c\"\"\"\n child = node.right\n grand_child = child.left\n # \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u5de6\u65cb\u8f49\n child.left = node\n node.right = grand_child\n # \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n self.update_height(node)\n self.update_height(child)\n # \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child\n</code></pre> avl_tree.cpp<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n TreeNode *child = node-&gt;right;\n TreeNode *grandChild = child-&gt;left;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u5de6\u65cb\u8f49\n child-&gt;left = node;\n node-&gt;right = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre> avl_tree.java<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode leftRotate(TreeNode node) {\n TreeNode child = node.right;\n TreeNode grandChild = child.left;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u5de6\u65cb\u8f49\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre> avl_tree.cs<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? LeftRotate(TreeNode? node) {\n TreeNode? child = node?.right;\n TreeNode? grandChild = child?.left;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u5de6\u65cb\u8f49\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n UpdateHeight(node);\n UpdateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre> avl_tree.go<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {\n child := node.Right\n grandChild := child.Left\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u5de6\u65cb\u8f49\n child.Left = node\n node.Right = grandChild\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n t.updateHeight(node)\n t.updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child\n}\n</code></pre> avl_tree.swift<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nfunc leftRotate(node: TreeNode?) -&gt; TreeNode? {\n let child = node?.right\n let grandChild = child?.left\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u5de6\u65cb\u8f49\n child?.left = node\n node?.right = grandChild\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n updateHeight(node: node)\n updateHeight(node: child)\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child\n}\n</code></pre> avl_tree.js<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\n#leftRotate(node) {\n const child = node.right;\n const grandChild = child.left;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u5de6\u65cb\u8f49\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n this.#updateHeight(node);\n this.#updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nleftRotate(node: TreeNode): TreeNode {\n const child = node.right;\n const grandChild = child.left;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u5de6\u65cb\u8f49\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n this.updateHeight(node);\n this.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre> avl_tree.dart<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? leftRotate(TreeNode? node) {\n TreeNode? child = node!.right;\n TreeNode? grandChild = child!.left;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u5de6\u65cb\u8f49\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre> avl_tree.rs<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nfn left_rotate(node: OptionTreeNodeRc) -&gt; OptionTreeNodeRc {\n match node {\n Some(node) =&gt; {\n let child = node.borrow().right.clone().unwrap();\n let grand_child = child.borrow().left.clone();\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u5de6\u65cb\u8f49\n child.borrow_mut().left = Some(node.clone());\n node.borrow_mut().right = grand_child;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n Self::update_height(Some(node));\n Self::update_height(Some(child.clone()));\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n Some(child)\n }\n None =&gt; None,\n }\n}\n</code></pre> avl_tree.c<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n TreeNode *child, *grandChild;\n child = node-&gt;right;\n grandChild = child-&gt;left;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u5de6\u65cb\u8f49\n child-&gt;left = node;\n node-&gt;right = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre> avl_tree.kt<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nfun leftRotate(node: TreeNode?): TreeNode {\n val child = node!!.right\n val grandChild = child!!.left\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u5de6\u65cb\u8f49\n child.left = node\n node.right = grandChild\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n updateHeight(node)\n updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child\n}\n</code></pre> avl_tree.rb<pre><code>### \u5de6\u65cb\u64cd\u4f5c ###\ndef left_rotate(node)\n child = node.right\n grand_child = child.left\n # \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u5de6\u65cb\u8f49\n child.left = node\n node.right = grand_child\n # \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n update_height(node)\n update_height(child)\n # \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n child\nend\n</code></pre> avl_tree.zig<pre><code>// \u5de6\u65cb\u64cd\u4f5c\nfn leftRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n var child = node.?.right;\n var grandChild = child.?.left;\n // \u4ee5 child \u70ba\u539f\u9ede\uff0c\u5c07 node \u5411\u5de6\u65cb\u8f49\n child.?.left = node;\n node.?.right = grandChild;\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n self.updateHeight(node);\n self.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f49\u5f8c\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return child;\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#3","title":"3. \u00a0 \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb","text":"<p>\u5c0d\u65bc\u5716 7-30 \u4e2d\u7684\u5931\u8861\u7bc0\u9ede 3 \uff0c\u50c5\u4f7f\u7528\u5de6\u65cb\u6216\u53f3\u65cb\u90fd\u7121\u6cd5\u4f7f\u5b50\u6a39\u6062\u5fa9\u5e73\u8861\u3002\u6b64\u6642\u9700\u8981\u5148\u5c0d <code>child</code> \u57f7\u884c\u201c\u5de6\u65cb\u201d\uff0c\u518d\u5c0d <code>node</code> \u57f7\u884c\u201c\u53f3\u65cb\u201d\u3002</p> <p></p> <p> \u5716 7-30 \u00a0 \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb </p>"},{"location":"chapter_tree/avl_tree/#4","title":"4. \u00a0 \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb","text":"<p>\u5982\u5716 7-31 \u6240\u793a\uff0c\u5c0d\u65bc\u4e0a\u8ff0\u5931\u8861\u4e8c\u5143\u6a39\u7684\u6620\u8c61\u60c5\u6cc1\uff0c\u9700\u8981\u5148\u5c0d <code>child</code> \u57f7\u884c\u201c\u53f3\u65cb\u201d\uff0c\u518d\u5c0d <code>node</code> \u57f7\u884c\u201c\u5de6\u65cb\u201d\u3002</p> <p></p> <p> \u5716 7-31 \u00a0 \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb </p>"},{"location":"chapter_tree/avl_tree/#5","title":"5. \u00a0 \u65cb\u8f49\u7684\u9078\u64c7","text":"<p>\u5716 7-32 \u5c55\u793a\u7684\u56db\u7a2e\u5931\u8861\u60c5\u6cc1\u8207\u4e0a\u8ff0\u6848\u4f8b\u9010\u500b\u5c0d\u61c9\uff0c\u5206\u5225\u9700\u8981\u63a1\u7528\u53f3\u65cb\u3001\u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\u3001\u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\u3001\u5de6\u65cb\u7684\u64cd\u4f5c\u3002</p> <p></p> <p> \u5716 7-32 \u00a0 AVL \u6a39\u7684\u56db\u7a2e\u65cb\u8f49\u60c5\u6cc1 </p> <p>\u5982\u4e0b\u8868\u6240\u793a\uff0c\u6211\u5011\u900f\u904e\u5224\u65b7\u5931\u8861\u7bc0\u9ede\u7684\u5e73\u8861\u56e0\u5b50\u4ee5\u53ca\u8f03\u9ad8\u4e00\u5074\u5b50\u7bc0\u9ede\u7684\u5e73\u8861\u56e0\u5b50\u7684\u6b63\u8ca0\u865f\uff0c\u4f86\u78ba\u5b9a\u5931\u8861\u7bc0\u9ede\u5c6c\u65bc\u5716 7-32 \u4e2d\u7684\u54ea\u7a2e\u60c5\u6cc1\u3002</p> <p> \u8868 7-3 \u00a0 \u56db\u7a2e\u65cb\u8f49\u60c5\u6cc1\u7684\u9078\u64c7\u689d\u4ef6 </p> \u5931\u8861\u7bc0\u9ede\u7684\u5e73\u8861\u56e0\u5b50 \u5b50\u7bc0\u9ede\u7684\u5e73\u8861\u56e0\u5b50 \u61c9\u63a1\u7528\u7684\u65cb\u8f49\u65b9\u6cd5 \\(&gt; 1\\) \uff08\u5de6\u504f\u6a39\uff09 \\(\\geq 0\\) \u53f3\u65cb \\(&gt; 1\\) \uff08\u5de6\u504f\u6a39\uff09 \\(&lt;0\\) \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb \\(&lt; -1\\) \uff08\u53f3\u504f\u6a39\uff09 \\(\\leq 0\\) \u5de6\u65cb \\(&lt; -1\\) \uff08\u53f3\u504f\u6a39\uff09 \\(&gt;0\\) \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb <p>\u70ba\u4e86\u4fbf\u65bc\u4f7f\u7528\uff0c\u6211\u5011\u5c07\u65cb\u8f49\u64cd\u4f5c\u5c01\u88dd\u6210\u4e00\u500b\u51fd\u5f0f\u3002\u6709\u4e86\u9019\u500b\u51fd\u5f0f\uff0c\u6211\u5011\u5c31\u80fd\u5c0d\u5404\u7a2e\u5931\u8861\u60c5\u6cc1\u9032\u884c\u65cb\u8f49\uff0c\u4f7f\u5931\u8861\u7bc0\u9ede\u91cd\u65b0\u6062\u5fa9\u5e73\u8861\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.py<pre><code>def rotate(self, node: TreeNode | None) -&gt; TreeNode | None:\n \"\"\"\u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861\"\"\"\n # \u7372\u53d6\u7bc0\u9ede node \u7684\u5e73\u8861\u56e0\u5b50\n balance_factor = self.balance_factor(node)\n # \u5de6\u504f\u6a39\n if balance_factor &gt; 1:\n if self.balance_factor(node.left) &gt;= 0:\n # \u53f3\u65cb\n return self.right_rotate(node)\n else:\n # \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\n node.left = self.left_rotate(node.left)\n return self.right_rotate(node)\n # \u53f3\u504f\u6a39\n elif balance_factor &lt; -1:\n if self.balance_factor(node.right) &lt;= 0:\n # \u5de6\u65cb\n return self.left_rotate(node)\n else:\n # \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\n node.right = self.right_rotate(node.right)\n return self.left_rotate(node)\n # \u5e73\u8861\u6a39\uff0c\u7121\u9808\u65cb\u8f49\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n</code></pre> avl_tree.cpp<pre><code>/* \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n // \u7372\u53d6\u7bc0\u9ede node \u7684\u5e73\u8861\u56e0\u5b50\n int _balanceFactor = balanceFactor(node);\n // \u5de6\u504f\u6a39\n if (_balanceFactor &gt; 1) {\n if (balanceFactor(node-&gt;left) &gt;= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\n node-&gt;left = leftRotate(node-&gt;left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6a39\n if (_balanceFactor &lt; -1) {\n if (balanceFactor(node-&gt;right) &lt;= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\n node-&gt;right = rightRotate(node-&gt;right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6a39\uff0c\u7121\u9808\u65cb\u8f49\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n</code></pre> avl_tree.java<pre><code>/* \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\nTreeNode rotate(TreeNode node) {\n // \u7372\u53d6\u7bc0\u9ede node \u7684\u5e73\u8861\u56e0\u5b50\n int balanceFactor = balanceFactor(node);\n // \u5de6\u504f\u6a39\n if (balanceFactor &gt; 1) {\n if (balanceFactor(node.left) &gt;= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\n node.left = leftRotate(node.left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6a39\n if (balanceFactor &lt; -1) {\n if (balanceFactor(node.right) &lt;= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\n node.right = rightRotate(node.right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6a39\uff0c\u7121\u9808\u65cb\u8f49\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n</code></pre> avl_tree.cs<pre><code>/* \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\nTreeNode? Rotate(TreeNode? node) {\n // \u7372\u53d6\u7bc0\u9ede node \u7684\u5e73\u8861\u56e0\u5b50\n int balanceFactorInt = BalanceFactor(node);\n // \u5de6\u504f\u6a39\n if (balanceFactorInt &gt; 1) {\n if (BalanceFactor(node?.left) &gt;= 0) {\n // \u53f3\u65cb\n return RightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\n node!.left = LeftRotate(node!.left);\n return RightRotate(node);\n }\n }\n // \u53f3\u504f\u6a39\n if (balanceFactorInt &lt; -1) {\n if (BalanceFactor(node?.right) &lt;= 0) {\n // \u5de6\u65cb\n return LeftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\n node!.right = RightRotate(node!.right);\n return LeftRotate(node);\n }\n }\n // \u5e73\u8861\u6a39\uff0c\u7121\u9808\u65cb\u8f49\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n</code></pre> avl_tree.go<pre><code>/* \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\nfunc (t *aVLTree) rotate(node *TreeNode) *TreeNode {\n // \u7372\u53d6\u7bc0\u9ede node \u7684\u5e73\u8861\u56e0\u5b50\n // Go \u63a8\u85a6\u77ed\u8b8a\u6578\uff0c\u9019\u88e1 bf \u6307\u4ee3 t.balanceFactor\n bf := t.balanceFactor(node)\n // \u5de6\u504f\u6a39\n if bf &gt; 1 {\n if t.balanceFactor(node.Left) &gt;= 0 {\n // \u53f3\u65cb\n return t.rightRotate(node)\n } else {\n // \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\n node.Left = t.leftRotate(node.Left)\n return t.rightRotate(node)\n }\n }\n // \u53f3\u504f\u6a39\n if bf &lt; -1 {\n if t.balanceFactor(node.Right) &lt;= 0 {\n // \u5de6\u65cb\n return t.leftRotate(node)\n } else {\n // \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\n node.Right = t.rightRotate(node.Right)\n return t.leftRotate(node)\n }\n }\n // \u5e73\u8861\u6a39\uff0c\u7121\u9808\u65cb\u8f49\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n}\n</code></pre> avl_tree.swift<pre><code>/* \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\nfunc rotate(node: TreeNode?) -&gt; TreeNode? {\n // \u7372\u53d6\u7bc0\u9ede node \u7684\u5e73\u8861\u56e0\u5b50\n let balanceFactor = balanceFactor(node: node)\n // \u5de6\u504f\u6a39\n if balanceFactor &gt; 1 {\n if self.balanceFactor(node: node?.left) &gt;= 0 {\n // \u53f3\u65cb\n return rightRotate(node: node)\n } else {\n // \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\n node?.left = leftRotate(node: node?.left)\n return rightRotate(node: node)\n }\n }\n // \u53f3\u504f\u6a39\n if balanceFactor &lt; -1 {\n if self.balanceFactor(node: node?.right) &lt;= 0 {\n // \u5de6\u65cb\n return leftRotate(node: node)\n } else {\n // \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\n node?.right = rightRotate(node: node?.right)\n return leftRotate(node: node)\n }\n }\n // \u5e73\u8861\u6a39\uff0c\u7121\u9808\u65cb\u8f49\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n}\n</code></pre> avl_tree.js<pre><code>/* \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n#rotate(node) {\n // \u7372\u53d6\u7bc0\u9ede node \u7684\u5e73\u8861\u56e0\u5b50\n const balanceFactor = this.balanceFactor(node);\n // \u5de6\u504f\u6a39\n if (balanceFactor &gt; 1) {\n if (this.balanceFactor(node.left) &gt;= 0) {\n // \u53f3\u65cb\n return this.#rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\n node.left = this.#leftRotate(node.left);\n return this.#rightRotate(node);\n }\n }\n // \u53f3\u504f\u6a39\n if (balanceFactor &lt; -1) {\n if (this.balanceFactor(node.right) &lt;= 0) {\n // \u5de6\u65cb\n return this.#leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\n node.right = this.#rightRotate(node.right);\n return this.#leftRotate(node);\n }\n }\n // \u5e73\u8861\u6a39\uff0c\u7121\u9808\u65cb\u8f49\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\nrotate(node: TreeNode): TreeNode {\n // \u7372\u53d6\u7bc0\u9ede node \u7684\u5e73\u8861\u56e0\u5b50\n const balanceFactor = this.balanceFactor(node);\n // \u5de6\u504f\u6a39\n if (balanceFactor &gt; 1) {\n if (this.balanceFactor(node.left) &gt;= 0) {\n // \u53f3\u65cb\n return this.rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\n node.left = this.leftRotate(node.left);\n return this.rightRotate(node);\n }\n }\n // \u53f3\u504f\u6a39\n if (balanceFactor &lt; -1) {\n if (this.balanceFactor(node.right) &lt;= 0) {\n // \u5de6\u65cb\n return this.leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\n node.right = this.rightRotate(node.right);\n return this.leftRotate(node);\n }\n }\n // \u5e73\u8861\u6a39\uff0c\u7121\u9808\u65cb\u8f49\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n</code></pre> avl_tree.dart<pre><code>/* \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\nTreeNode? rotate(TreeNode? node) {\n // \u7372\u53d6\u7bc0\u9ede node \u7684\u5e73\u8861\u56e0\u5b50\n int factor = balanceFactor(node);\n // \u5de6\u504f\u6a39\n if (factor &gt; 1) {\n if (balanceFactor(node!.left) &gt;= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\n node.left = leftRotate(node.left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6a39\n if (factor &lt; -1) {\n if (balanceFactor(node!.right) &lt;= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\n node.right = rightRotate(node.right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6a39\uff0c\u7121\u9808\u65cb\u8f49\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n</code></pre> avl_tree.rs<pre><code>/* \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\nfn rotate(node: OptionTreeNodeRc) -&gt; OptionTreeNodeRc {\n // \u7372\u53d6\u7bc0\u9ede node \u7684\u5e73\u8861\u56e0\u5b50\n let balance_factor = Self::balance_factor(node.clone());\n // \u5de6\u504f\u6a39\n if balance_factor &gt; 1 {\n let node = node.unwrap();\n if Self::balance_factor(node.borrow().left.clone()) &gt;= 0 {\n // \u53f3\u65cb\n Self::right_rotate(Some(node))\n } else {\n // \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::left_rotate(left);\n Self::right_rotate(Some(node))\n }\n }\n // \u53f3\u504f\u6a39\n else if balance_factor &lt; -1 {\n let node = node.unwrap();\n if Self::balance_factor(node.borrow().right.clone()) &lt;= 0 {\n // \u5de6\u65cb\n Self::left_rotate(Some(node))\n } else {\n // \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::right_rotate(right);\n Self::left_rotate(Some(node))\n }\n } else {\n // \u5e73\u8861\u6a39\uff0c\u7121\u9808\u65cb\u8f49\uff0c\u76f4\u63a5\u8fd4\u56de\n node\n }\n}\n</code></pre> avl_tree.c<pre><code>/* \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n // \u7372\u53d6\u7bc0\u9ede node \u7684\u5e73\u8861\u56e0\u5b50\n int bf = balanceFactor(node);\n // \u5de6\u504f\u6a39\n if (bf &gt; 1) {\n if (balanceFactor(node-&gt;left) &gt;= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\n node-&gt;left = leftRotate(node-&gt;left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6a39\n if (bf &lt; -1) {\n if (balanceFactor(node-&gt;right) &lt;= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\n node-&gt;right = rightRotate(node-&gt;right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6a39\uff0c\u7121\u9808\u65cb\u8f49\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n</code></pre> avl_tree.kt<pre><code>/* \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\nfun rotate(node: TreeNode): TreeNode {\n // \u7372\u53d6\u7bc0\u9ede node \u7684\u5e73\u8861\u56e0\u5b50\n val balanceFactor = balanceFactor(node)\n // \u5de6\u504f\u6a39\n if (balanceFactor &gt; 1) {\n if (balanceFactor(node.left) &gt;= 0) {\n // \u53f3\u65cb\n return rightRotate(node)\n } else {\n // \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\n node.left = leftRotate(node.left)\n return rightRotate(node)\n }\n }\n // \u53f3\u504f\u6a39\n if (balanceFactor &lt; -1) {\n if (balanceFactor(node.right) &lt;= 0) {\n // \u5de6\u65cb\n return leftRotate(node)\n } else {\n // \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\n node.right = rightRotate(node.right)\n return leftRotate(node)\n }\n }\n // \u5e73\u8861\u6a39\uff0c\u7121\u9808\u65cb\u8f49\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n}\n</code></pre> avl_tree.rb<pre><code>### \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 ###\ndef rotate(node)\n # \u7372\u53d6\u7bc0\u9ede node \u7684\u5e73\u8861\u56e0\u5b50\n balance_factor = balance_factor(node)\n # \u5de6\u904d\u6a39\n if balance_factor &gt; 1\n if balance_factor(node.left) &gt;= 0\n # \u53f3\u65cb\n return right_rotate(node)\n else\n # \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\n node.left = left_rotate(node.left)\n return right_rotate(node)\n end\n # \u53f3\u904d\u6a39\n elsif balance_factor &lt; -1\n if balance_factor(node.right) &lt;= 0\n # \u5de6\u65cb\n return left_rotate(node)\n else\n # \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\n node.right = right_rotate(node.right)\n return left_rotate(node)\n end\n end\n # \u5e73\u8861\u6a39\uff0c\u7121\u9808\u65cb\u8f49\uff0c\u76f4\u63a5\u8fd4\u56de\n node\nend\n</code></pre> avl_tree.zig<pre><code>// \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861\nfn rotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n // \u7372\u53d6\u7bc0\u9ede node \u7684\u5e73\u8861\u56e0\u5b50\n var balance_factor = self.balanceFactor(node);\n // \u5de6\u504f\u6a39\n if (balance_factor &gt; 1) {\n if (self.balanceFactor(node.?.left) &gt;= 0) {\n // \u53f3\u65cb\n return self.rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u5f8c\u53f3\u65cb\n node.?.left = self.leftRotate(node.?.left);\n return self.rightRotate(node);\n }\n }\n // \u53f3\u504f\u6a39\n if (balance_factor &lt; -1) {\n if (self.balanceFactor(node.?.right) &lt;= 0) {\n // \u5de6\u65cb\n return self.leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u5f8c\u5de6\u65cb\n node.?.right = self.rightRotate(node.?.right);\n return self.leftRotate(node);\n }\n }\n // \u5e73\u8861\u6a39\uff0c\u7121\u9808\u65cb\u8f49\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#753-avl","title":"7.5.3 \u00a0 AVL \u6a39\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_tree/avl_tree/#1_2","title":"1. \u00a0 \u63d2\u5165\u7bc0\u9ede","text":"<p>AVL \u6a39\u7684\u7bc0\u9ede\u63d2\u5165\u64cd\u4f5c\u8207\u4e8c\u5143\u641c\u5c0b\u6a39\u5728\u4e3b\u9ad4\u4e0a\u985e\u4f3c\u3002\u552f\u4e00\u7684\u5340\u5225\u5728\u65bc\uff0c\u5728 AVL \u6a39\u4e2d\u63d2\u5165\u7bc0\u9ede\u5f8c\uff0c\u5f9e\u8a72\u7bc0\u9ede\u5230\u6839\u7bc0\u9ede\u7684\u8def\u5f91\u4e0a\u53ef\u80fd\u6703\u51fa\u73fe\u4e00\u7cfb\u5217\u5931\u8861\u7bc0\u9ede\u3002\u56e0\u6b64\uff0c\u6211\u5011\u9700\u8981\u5f9e\u9019\u500b\u7bc0\u9ede\u958b\u59cb\uff0c\u81ea\u5e95\u5411\u4e0a\u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u7bc0\u9ede\u6062\u5fa9\u5e73\u8861\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.py<pre><code>def insert(self, val):\n \"\"\"\u63d2\u5165\u7bc0\u9ede\"\"\"\n self._root = self.insert_helper(self._root, val)\n\ndef insert_helper(self, node: TreeNode | None, val: int) -&gt; TreeNode:\n \"\"\"\u905e\u8ff4\u63d2\u5165\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09\"\"\"\n if node is None:\n return TreeNode(val)\n # 1. \u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\u4e26\u63d2\u5165\u7bc0\u9ede\n if val &lt; node.val:\n node.left = self.insert_helper(node.left, val)\n elif val &gt; node.val:\n node.right = self.insert_helper(node.right, val)\n else:\n # \u91cd\u8907\u7bc0\u9ede\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n # \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n self.update_height(node)\n # 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861\n return self.rotate(node)\n</code></pre> avl_tree.cpp<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u905e\u8ff4\u63d2\u5165\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n if (node == nullptr)\n return new TreeNode(val);\n /* 1. \u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\u4e26\u63d2\u5165\u7bc0\u9ede */\n if (val &lt; node-&gt;val)\n node-&gt;left = insertHelper(node-&gt;left, val);\n else if (val &gt; node-&gt;val)\n node-&gt;right = insertHelper(node-&gt;right, val);\n else\n return node; // \u91cd\u8907\u7bc0\u9ede\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre> avl_tree.java<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u905e\u8ff4\u63d2\u5165\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nTreeNode insertHelper(TreeNode node, int val) {\n if (node == null)\n return new TreeNode(val);\n /* 1. \u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\u4e26\u63d2\u5165\u7bc0\u9ede */\n if (val &lt; node.val)\n node.left = insertHelper(node.left, val);\n else if (val &gt; node.val)\n node.right = insertHelper(node.right, val);\n else\n return node; // \u91cd\u8907\u7bc0\u9ede\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre> avl_tree.cs<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nvoid Insert(int val) {\n root = InsertHelper(root, val);\n}\n\n/* \u905e\u8ff4\u63d2\u5165\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? InsertHelper(TreeNode? node, int val) {\n if (node == null) return new TreeNode(val);\n /* 1. \u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\u4e26\u63d2\u5165\u7bc0\u9ede */\n if (val &lt; node.val)\n node.left = InsertHelper(node.left, val);\n else if (val &gt; node.val)\n node.right = InsertHelper(node.right, val);\n else\n return node; // \u91cd\u8907\u7bc0\u9ede\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n UpdateHeight(node); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = Rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre> avl_tree.go<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nfunc (t *aVLTree) insert(val int) {\n t.root = t.insertHelper(t.root, val)\n}\n\n/* \u905e\u8ff4\u63d2\u5165\u7bc0\u9ede\uff08\u8f14\u52a9\u51fd\u5f0f\uff09 */\nfunc (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {\n if node == nil {\n return NewTreeNode(val)\n }\n /* 1. \u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\u4e26\u63d2\u5165\u7bc0\u9ede */\n if val &lt; node.Val.(int) {\n node.Left = t.insertHelper(node.Left, val)\n } else if val &gt; node.Val.(int) {\n node.Right = t.insertHelper(node.Right, val)\n } else {\n // \u91cd\u8907\u7bc0\u9ede\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n }\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n t.updateHeight(node)\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = t.rotate(node)\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node\n}\n</code></pre> avl_tree.swift<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nfunc insert(val: Int) {\n root = insertHelper(node: root, val: val)\n}\n\n/* \u905e\u8ff4\u63d2\u5165\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nfunc insertHelper(node: TreeNode?, val: Int) -&gt; TreeNode? {\n var node = node\n if node == nil {\n return TreeNode(x: val)\n }\n /* 1. \u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\u4e26\u63d2\u5165\u7bc0\u9ede */\n if val &lt; node!.val {\n node?.left = insertHelper(node: node?.left, val: val)\n } else if val &gt; node!.val {\n node?.right = insertHelper(node: node?.right, val: val)\n } else {\n return node // \u91cd\u8907\u7bc0\u9ede\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n updateHeight(node: node) // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = rotate(node: node)\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node\n}\n</code></pre> avl_tree.js<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\ninsert(val) {\n this.root = this.#insertHelper(this.root, val);\n}\n\n/* \u905e\u8ff4\u63d2\u5165\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\n#insertHelper(node, val) {\n if (node === null) return new TreeNode(val);\n /* 1. \u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\u4e26\u63d2\u5165\u7bc0\u9ede */\n if (val &lt; node.val) node.left = this.#insertHelper(node.left, val);\n else if (val &gt; node.val)\n node.right = this.#insertHelper(node.right, val);\n else return node; // \u91cd\u8907\u7bc0\u9ede\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n this.#updateHeight(node); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = this.#rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\ninsert(val: number): void {\n this.root = this.insertHelper(this.root, val);\n}\n\n/* \u905e\u8ff4\u63d2\u5165\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\ninsertHelper(node: TreeNode, val: number): TreeNode {\n if (node === null) return new TreeNode(val);\n /* 1. \u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\u4e26\u63d2\u5165\u7bc0\u9ede */\n if (val &lt; node.val) {\n node.left = this.insertHelper(node.left, val);\n } else if (val &gt; node.val) {\n node.right = this.insertHelper(node.right, val);\n } else {\n return node; // \u91cd\u8907\u7bc0\u9ede\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n this.updateHeight(node); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = this.rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre> avl_tree.dart<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u905e\u8ff4\u63d2\u5165\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? insertHelper(TreeNode? node, int val) {\n if (node == null) return TreeNode(val);\n /* 1. \u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\u4e26\u63d2\u5165\u7bc0\u9ede */\n if (val &lt; node.val)\n node.left = insertHelper(node.left, val);\n else if (val &gt; node.val)\n node.right = insertHelper(node.right, val);\n else\n return node; // \u91cd\u8907\u7bc0\u9ede\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre> avl_tree.rs<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nfn insert(&amp;mut self, val: i32) {\n self.root = Self::insert_helper(self.root.clone(), val);\n}\n\n/* \u905e\u8ff4\u63d2\u5165\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nfn insert_helper(node: OptionTreeNodeRc, val: i32) -&gt; OptionTreeNodeRc {\n match node {\n Some(mut node) =&gt; {\n /* 1. \u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\u4e26\u63d2\u5165\u7bc0\u9ede */\n match {\n let node_val = node.borrow().val;\n node_val\n }\n .cmp(&amp;val)\n {\n Ordering::Greater =&gt; {\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::insert_helper(left, val);\n }\n Ordering::Less =&gt; {\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::insert_helper(right, val);\n }\n Ordering::Equal =&gt; {\n return Some(node); // \u91cd\u8907\u7bc0\u9ede\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n }\n Self::update_height(Some(node.clone())); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = Self::rotate(Some(node)).unwrap();\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n Some(node)\n }\n None =&gt; Some(TreeNode::new(val)),\n }\n}\n</code></pre> avl_tree.c<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nvoid insert(AVLTree *tree, int val) {\n tree-&gt;root = insertHelper(tree-&gt;root, val);\n}\n\n/* \u905e\u8ff4\u63d2\u5165\u7bc0\u9ede\uff08\u8f14\u52a9\u51fd\u5f0f\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n if (node == NULL) {\n return newTreeNode(val);\n }\n /* 1. \u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\u4e26\u63d2\u5165\u7bc0\u9ede */\n if (val &lt; node-&gt;val) {\n node-&gt;left = insertHelper(node-&gt;left, val);\n } else if (val &gt; node-&gt;val) {\n node-&gt;right = insertHelper(node-&gt;right, val);\n } else {\n // \u91cd\u8907\u7bc0\u9ede\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n }\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n updateHeight(node);\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre> avl_tree.kt<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nfun insert(_val: Int) {\n root = insertHelper(root, _val)\n}\n\n/* \u905e\u8ff4\u63d2\u5165\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nfun insertHelper(n: TreeNode?, _val: Int): TreeNode {\n if (n == null)\n return TreeNode(_val)\n var node = n\n /* 1. \u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\u4e26\u63d2\u5165\u7bc0\u9ede */\n if (_val &lt; node._val)\n node.left = insertHelper(node.left, _val)\n else if (_val &gt; node._val)\n node.right = insertHelper(node.right, _val)\n else\n return node // \u91cd\u8907\u7bc0\u9ede\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node) // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = rotate(node)\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node\n}\n</code></pre> avl_tree.rb<pre><code>### \u63d2\u5165\u7bc0\u9ede ###\ndef insert(val)\n @root = insert_helper(@root, val)\nend\n\n### \u905e\u8ff4\u63d2\u5165\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09###\ndef insert_helper(node, val)\n return TreeNode.new(val) if node.nil?\n # 1. \u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\u4e26\u63d2\u5165\u7bc0\u9ede\n if val &lt; node.val\n node.left = insert_helper(node.left, val)\n elsif val &gt; node.val\n node.right = insert_helper(node.right, val)\n else\n # \u91cd\u8907\u7bc0\u9ede\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n end\n # \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n update_height(node)\n # 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861\n rotate(node)\nend\n</code></pre> avl_tree.zig<pre><code>// \u63d2\u5165\u7bc0\u9ede\nfn insert(self: *Self, val: T) !void {\n self.root = (try self.insertHelper(self.root, val)).?;\n}\n\n// \u905e\u8ff4\u63d2\u5165\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09\nfn insertHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) !?*inc.TreeNode(T) {\n var node = node_;\n if (node == null) {\n var tmp_node = try self.mem_allocator.create(inc.TreeNode(T));\n tmp_node.init(val);\n return tmp_node;\n }\n // 1. \u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\u4e26\u63d2\u5165\u7bc0\u9ede\n if (val &lt; node.?.val) {\n node.?.left = try self.insertHelper(node.?.left, val);\n } else if (val &gt; node.?.val) {\n node.?.right = try self.insertHelper(node.?.right, val);\n } else {\n return node; // \u91cd\u8907\u7bc0\u9ede\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n self.updateHeight(node); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n // 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861\n node = self.rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#2_2","title":"2. \u00a0 \u522a\u9664\u7bc0\u9ede","text":"<p>\u985e\u4f3c\u5730\uff0c\u5728\u4e8c\u5143\u641c\u5c0b\u6a39\u7684\u522a\u9664\u7bc0\u9ede\u65b9\u6cd5\u7684\u57fa\u790e\u4e0a\uff0c\u9700\u8981\u5f9e\u5e95\u81f3\u9802\u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u7bc0\u9ede\u6062\u5fa9\u5e73\u8861\u3002\u7a0b\u5f0f\u78bc\u5982\u4e0b\u6240\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.py<pre><code>def remove(self, val: int):\n \"\"\"\u522a\u9664\u7bc0\u9ede\"\"\"\n self._root = self.remove_helper(self._root, val)\n\ndef remove_helper(self, node: TreeNode | None, val: int) -&gt; TreeNode | None:\n \"\"\"\u905e\u8ff4\u522a\u9664\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09\"\"\"\n if node is None:\n return None\n # 1. \u67e5\u8a62\u7bc0\u9ede\u4e26\u522a\u9664\n if val &lt; node.val:\n node.left = self.remove_helper(node.left, val)\n elif val &gt; node.val:\n node.right = self.remove_helper(node.right, val)\n else:\n if node.left is None or node.right is None:\n child = node.left or node.right\n # \u5b50\u7bc0\u9ede\u6578\u91cf = 0 \uff0c\u76f4\u63a5\u522a\u9664 node \u4e26\u8fd4\u56de\n if child is None:\n return None\n # \u5b50\u7bc0\u9ede\u6578\u91cf = 1 \uff0c\u76f4\u63a5\u522a\u9664 node\n else:\n node = child\n else:\n # \u5b50\u7bc0\u9ede\u6578\u91cf = 2 \uff0c\u5247\u5c07\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u500b\u7bc0\u9ede\u522a\u9664\uff0c\u4e26\u7528\u8a72\u7bc0\u9ede\u66ff\u63db\u7576\u524d\u7bc0\u9ede\n temp = node.right\n while temp.left is not None:\n temp = temp.left\n node.right = self.remove_helper(node.right, temp.val)\n node.val = temp.val\n # \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n self.update_height(node)\n # 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861\n return self.rotate(node)\n</code></pre> avl_tree.cpp<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u905e\u8ff4\u522a\u9664\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n if (node == nullptr)\n return nullptr;\n /* 1. \u67e5\u8a62\u7bc0\u9ede\u4e26\u522a\u9664 */\n if (val &lt; node-&gt;val)\n node-&gt;left = removeHelper(node-&gt;left, val);\n else if (val &gt; node-&gt;val)\n node-&gt;right = removeHelper(node-&gt;right, val);\n else {\n if (node-&gt;left == nullptr || node-&gt;right == nullptr) {\n TreeNode *child = node-&gt;left != nullptr ? node-&gt;left : node-&gt;right;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 \uff0c\u76f4\u63a5\u522a\u9664 node \u4e26\u8fd4\u56de\n if (child == nullptr) {\n delete node;\n return nullptr;\n }\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 1 \uff0c\u76f4\u63a5\u522a\u9664 node\n else {\n delete node;\n node = child;\n }\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2 \uff0c\u5247\u5c07\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u500b\u7bc0\u9ede\u522a\u9664\uff0c\u4e26\u7528\u8a72\u7bc0\u9ede\u66ff\u63db\u7576\u524d\u7bc0\u9ede\n TreeNode *temp = node-&gt;right;\n while (temp-&gt;left != nullptr) {\n temp = temp-&gt;left;\n }\n int tempVal = temp-&gt;val;\n node-&gt;right = removeHelper(node-&gt;right, temp-&gt;val);\n node-&gt;val = tempVal;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre> avl_tree.java<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u905e\u8ff4\u522a\u9664\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nTreeNode removeHelper(TreeNode node, int val) {\n if (node == null)\n return null;\n /* 1. \u67e5\u8a62\u7bc0\u9ede\u4e26\u522a\u9664 */\n if (val &lt; node.val)\n node.left = removeHelper(node.left, val);\n else if (val &gt; node.val)\n node.right = removeHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode child = node.left != null ? node.left : node.right;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 \uff0c\u76f4\u63a5\u522a\u9664 node \u4e26\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 1 \uff0c\u76f4\u63a5\u522a\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2 \uff0c\u5247\u5c07\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u500b\u7bc0\u9ede\u522a\u9664\uff0c\u4e26\u7528\u8a72\u7bc0\u9ede\u66ff\u63db\u7576\u524d\u7bc0\u9ede\n TreeNode temp = node.right;\n while (temp.left != null) {\n temp = temp.left;\n }\n node.right = removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre> avl_tree.cs<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nvoid Remove(int val) {\n root = RemoveHelper(root, val);\n}\n\n/* \u905e\u8ff4\u522a\u9664\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? RemoveHelper(TreeNode? node, int val) {\n if (node == null) return null;\n /* 1. \u67e5\u8a62\u7bc0\u9ede\u4e26\u522a\u9664 */\n if (val &lt; node.val)\n node.left = RemoveHelper(node.left, val);\n else if (val &gt; node.val)\n node.right = RemoveHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode? child = node.left ?? node.right;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 \uff0c\u76f4\u63a5\u522a\u9664 node \u4e26\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 1 \uff0c\u76f4\u63a5\u522a\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2 \uff0c\u5247\u5c07\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u500b\u7bc0\u9ede\u522a\u9664\uff0c\u4e26\u7528\u8a72\u7bc0\u9ede\u66ff\u63db\u7576\u524d\u7bc0\u9ede\n TreeNode? temp = node.right;\n while (temp.left != null) {\n temp = temp.left;\n }\n node.right = RemoveHelper(node.right, temp.val!.Value);\n node.val = temp.val;\n }\n }\n UpdateHeight(node); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = Rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre> avl_tree.go<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nfunc (t *aVLTree) remove(val int) {\n t.root = t.removeHelper(t.root, val)\n}\n\n/* \u905e\u8ff4\u522a\u9664\u7bc0\u9ede\uff08\u8f14\u52a9\u51fd\u5f0f\uff09 */\nfunc (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {\n if node == nil {\n return nil\n }\n /* 1. \u67e5\u8a62\u7bc0\u9ede\u4e26\u522a\u9664 */\n if val &lt; node.Val.(int) {\n node.Left = t.removeHelper(node.Left, val)\n } else if val &gt; node.Val.(int) {\n node.Right = t.removeHelper(node.Right, val)\n } else {\n if node.Left == nil || node.Right == nil {\n child := node.Left\n if node.Right != nil {\n child = node.Right\n }\n if child == nil {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 \uff0c\u76f4\u63a5\u522a\u9664 node \u4e26\u8fd4\u56de\n return nil\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 1 \uff0c\u76f4\u63a5\u522a\u9664 node\n node = child\n }\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2 \uff0c\u5247\u5c07\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u500b\u7bc0\u9ede\u522a\u9664\uff0c\u4e26\u7528\u8a72\u7bc0\u9ede\u66ff\u63db\u7576\u524d\u7bc0\u9ede\n temp := node.Right\n for temp.Left != nil {\n temp = temp.Left\n }\n node.Right = t.removeHelper(node.Right, temp.Val.(int))\n node.Val = temp.Val\n }\n }\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n t.updateHeight(node)\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = t.rotate(node)\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node\n}\n</code></pre> avl_tree.swift<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nfunc remove(val: Int) {\n root = removeHelper(node: root, val: val)\n}\n\n/* \u905e\u8ff4\u522a\u9664\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nfunc removeHelper(node: TreeNode?, val: Int) -&gt; TreeNode? {\n var node = node\n if node == nil {\n return nil\n }\n /* 1. \u67e5\u8a62\u7bc0\u9ede\u4e26\u522a\u9664 */\n if val &lt; node!.val {\n node?.left = removeHelper(node: node?.left, val: val)\n } else if val &gt; node!.val {\n node?.right = removeHelper(node: node?.right, val: val)\n } else {\n if node?.left == nil || node?.right == nil {\n let child = node?.left ?? node?.right\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 \uff0c\u76f4\u63a5\u522a\u9664 node \u4e26\u8fd4\u56de\n if child == nil {\n return nil\n }\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 1 \uff0c\u76f4\u63a5\u522a\u9664 node\n else {\n node = child\n }\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2 \uff0c\u5247\u5c07\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u500b\u7bc0\u9ede\u522a\u9664\uff0c\u4e26\u7528\u8a72\u7bc0\u9ede\u66ff\u63db\u7576\u524d\u7bc0\u9ede\n var temp = node?.right\n while temp?.left != nil {\n temp = temp?.left\n }\n node?.right = removeHelper(node: node?.right, val: temp!.val)\n node?.val = temp!.val\n }\n }\n updateHeight(node: node) // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = rotate(node: node)\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node\n}\n</code></pre> avl_tree.js<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nremove(val) {\n this.root = this.#removeHelper(this.root, val);\n}\n\n/* \u905e\u8ff4\u522a\u9664\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\n#removeHelper(node, val) {\n if (node === null) return null;\n /* 1. \u67e5\u8a62\u7bc0\u9ede\u4e26\u522a\u9664 */\n if (val &lt; node.val) node.left = this.#removeHelper(node.left, val);\n else if (val &gt; node.val)\n node.right = this.#removeHelper(node.right, val);\n else {\n if (node.left === null || node.right === null) {\n const child = node.left !== null ? node.left : node.right;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 \uff0c\u76f4\u63a5\u522a\u9664 node \u4e26\u8fd4\u56de\n if (child === null) return null;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 1 \uff0c\u76f4\u63a5\u522a\u9664 node\n else node = child;\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2 \uff0c\u5247\u5c07\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u500b\u7bc0\u9ede\u522a\u9664\uff0c\u4e26\u7528\u8a72\u7bc0\u9ede\u66ff\u63db\u7576\u524d\u7bc0\u9ede\n let temp = node.right;\n while (temp.left !== null) {\n temp = temp.left;\n }\n node.right = this.#removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n this.#updateHeight(node); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = this.#rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nremove(val: number): void {\n this.root = this.removeHelper(this.root, val);\n}\n\n/* \u905e\u8ff4\u522a\u9664\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nremoveHelper(node: TreeNode, val: number): TreeNode {\n if (node === null) return null;\n /* 1. \u67e5\u8a62\u7bc0\u9ede\u4e26\u522a\u9664 */\n if (val &lt; node.val) {\n node.left = this.removeHelper(node.left, val);\n } else if (val &gt; node.val) {\n node.right = this.removeHelper(node.right, val);\n } else {\n if (node.left === null || node.right === null) {\n const child = node.left !== null ? node.left : node.right;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 \uff0c\u76f4\u63a5\u522a\u9664 node \u4e26\u8fd4\u56de\n if (child === null) {\n return null;\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 1 \uff0c\u76f4\u63a5\u522a\u9664 node\n node = child;\n }\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2 \uff0c\u5247\u5c07\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u500b\u7bc0\u9ede\u522a\u9664\uff0c\u4e26\u7528\u8a72\u7bc0\u9ede\u66ff\u63db\u7576\u524d\u7bc0\u9ede\n let temp = node.right;\n while (temp.left !== null) {\n temp = temp.left;\n }\n node.right = this.removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n this.updateHeight(node); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = this.rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre> avl_tree.dart<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u905e\u8ff4\u522a\u9664\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? removeHelper(TreeNode? node, int val) {\n if (node == null) return null;\n /* 1. \u67e5\u8a62\u7bc0\u9ede\u4e26\u522a\u9664 */\n if (val &lt; node.val)\n node.left = removeHelper(node.left, val);\n else if (val &gt; node.val)\n node.right = removeHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode? child = node.left ?? node.right;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 \uff0c\u76f4\u63a5\u522a\u9664 node \u4e26\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 1 \uff0c\u76f4\u63a5\u522a\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2 \uff0c\u5247\u5c07\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u500b\u7bc0\u9ede\u522a\u9664\uff0c\u4e26\u7528\u8a72\u7bc0\u9ede\u66ff\u63db\u7576\u524d\u7bc0\u9ede\n TreeNode? temp = node.right;\n while (temp!.left != null) {\n temp = temp.left;\n }\n node.right = removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre> avl_tree.rs<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nfn remove(&amp;self, val: i32) {\n Self::remove_helper(self.root.clone(), val);\n}\n\n/* \u905e\u8ff4\u522a\u9664\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nfn remove_helper(node: OptionTreeNodeRc, val: i32) -&gt; OptionTreeNodeRc {\n match node {\n Some(mut node) =&gt; {\n /* 1. \u67e5\u8a62\u7bc0\u9ede\u4e26\u522a\u9664 */\n if val &lt; node.borrow().val {\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::remove_helper(left, val);\n } else if val &gt; node.borrow().val {\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::remove_helper(right, val);\n } else if node.borrow().left.is_none() || node.borrow().right.is_none() {\n let child = if node.borrow().left.is_some() {\n node.borrow().left.clone()\n } else {\n node.borrow().right.clone()\n };\n match child {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 \uff0c\u76f4\u63a5\u522a\u9664 node \u4e26\u8fd4\u56de\n None =&gt; {\n return None;\n }\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 1 \uff0c\u76f4\u63a5\u522a\u9664 node\n Some(child) =&gt; node = child,\n }\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2 \uff0c\u5247\u5c07\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u500b\u7bc0\u9ede\u522a\u9664\uff0c\u4e26\u7528\u8a72\u7bc0\u9ede\u66ff\u63db\u7576\u524d\u7bc0\u9ede\n let mut temp = node.borrow().right.clone().unwrap();\n loop {\n let temp_left = temp.borrow().left.clone();\n if temp_left.is_none() {\n break;\n }\n temp = temp_left.unwrap();\n }\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::remove_helper(right, temp.borrow().val);\n node.borrow_mut().val = temp.borrow().val;\n }\n Self::update_height(Some(node.clone())); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = Self::rotate(Some(node)).unwrap();\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n Some(node)\n }\n None =&gt; None,\n }\n}\n</code></pre> avl_tree.c<pre><code>/* \u522a\u9664\u7bc0\u9ede */\n// \u7531\u65bc\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u8655\u7121\u6cd5\u4f7f\u7528 remove \u95dc\u9375\u8a5e\nvoid removeItem(AVLTree *tree, int val) {\n TreeNode *root = removeHelper(tree-&gt;root, val);\n}\n\n/* \u905e\u8ff4\u522a\u9664\u7bc0\u9ede\uff08\u8f14\u52a9\u51fd\u5f0f\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n TreeNode *child, *grandChild;\n if (node == NULL) {\n return NULL;\n }\n /* 1. \u67e5\u8a62\u7bc0\u9ede\u4e26\u522a\u9664 */\n if (val &lt; node-&gt;val) {\n node-&gt;left = removeHelper(node-&gt;left, val);\n } else if (val &gt; node-&gt;val) {\n node-&gt;right = removeHelper(node-&gt;right, val);\n } else {\n if (node-&gt;left == NULL || node-&gt;right == NULL) {\n child = node-&gt;left;\n if (node-&gt;right != NULL) {\n child = node-&gt;right;\n }\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 \uff0c\u76f4\u63a5\u522a\u9664 node \u4e26\u8fd4\u56de\n if (child == NULL) {\n return NULL;\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 1 \uff0c\u76f4\u63a5\u522a\u9664 node\n node = child;\n }\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2 \uff0c\u5247\u5c07\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u500b\u7bc0\u9ede\u522a\u9664\uff0c\u4e26\u7528\u8a72\u7bc0\u9ede\u66ff\u63db\u7576\u524d\u7bc0\u9ede\n TreeNode *temp = node-&gt;right;\n while (temp-&gt;left != NULL) {\n temp = temp-&gt;left;\n }\n int tempVal = temp-&gt;val;\n node-&gt;right = removeHelper(node-&gt;right, temp-&gt;val);\n node-&gt;val = tempVal;\n }\n }\n // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n updateHeight(node);\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre> avl_tree.kt<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nfun remove(_val: Int) {\n root = removeHelper(root, _val)\n}\n\n/* \u905e\u8ff4\u522a\u9664\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09 */\nfun removeHelper(n: TreeNode?, _val: Int): TreeNode? {\n var node = n ?: return null\n /* 1. \u67e5\u8a62\u7bc0\u9ede\u4e26\u522a\u9664 */\n if (_val &lt; node._val)\n node.left = removeHelper(node.left, _val)\n else if (_val &gt; node._val)\n node.right = removeHelper(node.right, _val)\n else {\n if (node.left == null || node.right == null) {\n val child = if (node.left != null)\n node.left\n else\n node.right\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 \uff0c\u76f4\u63a5\u522a\u9664 node \u4e26\u8fd4\u56de\n if (child == null)\n return null\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 1 \uff0c\u76f4\u63a5\u522a\u9664 node\n else\n node = child\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2 \uff0c\u5247\u5c07\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u500b\u7bc0\u9ede\u522a\u9664\uff0c\u4e26\u7528\u8a72\u7bc0\u9ede\u66ff\u63db\u7576\u524d\u7bc0\u9ede\n var temp = node.right\n while (temp!!.left != null) {\n temp = temp.left\n }\n node.right = removeHelper(node.right, temp._val)\n node._val = temp._val\n }\n }\n updateHeight(node) // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n /* 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861 */\n node = rotate(node)\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node\n}\n</code></pre> avl_tree.rb<pre><code>### \u522a\u9664\u7bc0\u9ede ###\ndef remove(val)\n @root = remove_helper(@root, val)\nend\n\n### \u905e\u8ff4\u522a\u9664\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09###\ndef remove_helper(node, val)\n return if node.nil?\n # 1. \u67e5\u8a62\u7bc0\u9ede\u4e26\u522a\u9664\n if val &lt; node.val\n node.left = remove_helper(node.left, val)\n elsif val &gt; node.val\n node.right = remove_helper(node.right, val)\n else\n if node.left.nil? || node.right.nil?\n child = node.left || node.right\n # \u5b50\u7bc0\u9ede\u6578\u91cf = 0 \uff0c\u76f4\u63a5\u522a\u9664 node \u4e26\u8fd4\u56de\n return if child.nil?\n # \u5b50\u7bc0\u9ede\u6578\u91cf = 1 \uff0c\u76f4\u63a5\u522a\u9664 node\n node = child\n else\n # \u5b50\u7bc0\u9ede\u6578\u91cf = 2 \uff0c\u5247\u5c07\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u500b\u7bc0\u9ede\u522a\u9664\uff0c\u4e26\u7528\u8a72\u7bc0\u9ede\u66ff\u63db\u7576\u524d\u7bc0\u9ede\n temp = node.right\n while !temp.left.nil?\n temp = temp.left\n end\n node.right = remove_helper(node.right, temp.val)\n node.val = temp.val\n end\n end\n # \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n update_height(node)\n # 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861\n rotate(node)\nend\n</code></pre> avl_tree.zig<pre><code>// \u522a\u9664\u7bc0\u9ede\nfn remove(self: *Self, val: T) void {\n self.root = self.removeHelper(self.root, val).?;\n}\n\n// \u905e\u8ff4\u522a\u9664\u7bc0\u9ede\uff08\u8f14\u52a9\u65b9\u6cd5\uff09\nfn removeHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) ?*inc.TreeNode(T) {\n var node = node_;\n if (node == null) return null;\n // 1. \u67e5\u8a62\u7bc0\u9ede\u4e26\u522a\u9664\n if (val &lt; node.?.val) {\n node.?.left = self.removeHelper(node.?.left, val);\n } else if (val &gt; node.?.val) {\n node.?.right = self.removeHelper(node.?.right, val);\n } else {\n if (node.?.left == null or node.?.right == null) {\n var child = if (node.?.left != null) node.?.left else node.?.right;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 \uff0c\u76f4\u63a5\u522a\u9664 node \u4e26\u8fd4\u56de\n if (child == null) {\n return null;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 1 \uff0c\u76f4\u63a5\u522a\u9664 node\n } else {\n node = child;\n }\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2 \uff0c\u5247\u5c07\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u500b\u7bc0\u9ede\u522a\u9664\uff0c\u4e26\u7528\u8a72\u7bc0\u9ede\u66ff\u63db\u7576\u524d\u7bc0\u9ede\n var temp = node.?.right;\n while (temp.?.left != null) {\n temp = temp.?.left;\n }\n node.?.right = self.removeHelper(node.?.right, temp.?.val);\n node.?.val = temp.?.val;\n }\n }\n self.updateHeight(node); // \u66f4\u65b0\u7bc0\u9ede\u9ad8\u5ea6\n // 2. \u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u8a72\u5b50\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861\n node = self.rotate(node);\n // \u8fd4\u56de\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\n return node;\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#3_1","title":"3. \u00a0 \u67e5\u8a62\u7bc0\u9ede","text":"<p>AVL \u6a39\u7684\u7bc0\u9ede\u67e5\u8a62\u64cd\u4f5c\u8207\u4e8c\u5143\u641c\u5c0b\u6a39\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d05\u8ff0\u3002</p>"},{"location":"chapter_tree/avl_tree/#754-avl","title":"7.5.4 \u00a0 AVL \u6a39\u5178\u578b\u61c9\u7528","text":"<ul> <li>\u7d44\u7e54\u548c\u5132\u5b58\u5927\u578b\u8cc7\u6599\uff0c\u9069\u7528\u65bc\u9ad8\u983b\u67e5\u8a62\u3001\u4f4e\u983b\u589e\u522a\u7684\u5834\u666f\u3002</li> <li>\u7528\u65bc\u69cb\u5efa\u8cc7\u6599\u5eab\u4e2d\u7684\u7d22\u5f15\u7cfb\u7d71\u3002</li> <li>\u7d05\u9ed1\u6a39\u4e5f\u662f\u4e00\u7a2e\u5e38\u898b\u7684\u5e73\u8861\u4e8c\u5143\u641c\u5c0b\u6a39\u3002\u76f8\u8f03\u65bc AVL \u6a39\uff0c\u7d05\u9ed1\u6a39\u7684\u5e73\u8861\u689d\u4ef6\u66f4\u5bec\u9b06\uff0c\u63d2\u5165\u8207\u522a\u9664\u7bc0\u9ede\u6240\u9700\u7684\u65cb\u8f49\u64cd\u4f5c\u66f4\u5c11\uff0c\u7bc0\u9ede\u589e\u522a\u64cd\u4f5c\u7684\u5e73\u5747\u6548\u7387\u66f4\u9ad8\u3002</li> </ul>"},{"location":"chapter_tree/binary_search_tree/","title":"7.4 \u00a0 \u4e8c\u5143\u641c\u5c0b\u6a39","text":"<p>\u5982\u5716 7-16 \u6240\u793a\uff0c\u4e8c\u5143\u641c\u5c0b\u6a39\uff08binary search tree\uff09\u6eff\u8db3\u4ee5\u4e0b\u689d\u4ef6\u3002</p> <ol> <li>\u5c0d\u65bc\u6839\u7bc0\u9ede\uff0c\u5de6\u5b50\u6a39\u4e2d\u6240\u6709\u7bc0\u9ede\u7684\u503c \\(&lt;\\) \u6839\u7bc0\u9ede\u7684\u503c \\(&lt;\\) \u53f3\u5b50\u6a39\u4e2d\u6240\u6709\u7bc0\u9ede\u7684\u503c\u3002</li> <li>\u4efb\u610f\u7bc0\u9ede\u7684\u5de6\u3001\u53f3\u5b50\u6a39\u4e5f\u662f\u4e8c\u5143\u641c\u5c0b\u6a39\uff0c\u5373\u540c\u6a23\u6eff\u8db3\u689d\u4ef6 <code>1.</code> \u3002</li> </ol> <p></p> <p> \u5716 7-16 \u00a0 \u4e8c\u5143\u641c\u5c0b\u6a39 </p>"},{"location":"chapter_tree/binary_search_tree/#741","title":"7.4.1 \u00a0 \u4e8c\u5143\u641c\u5c0b\u6a39\u7684\u64cd\u4f5c","text":"<p>\u6211\u5011\u5c07\u4e8c\u5143\u641c\u5c0b\u6a39\u5c01\u88dd\u70ba\u4e00\u500b\u985e\u5225 <code>BinarySearchTree</code> \uff0c\u4e26\u5ba3\u544a\u4e00\u500b\u6210\u54e1\u8b8a\u6578 <code>root</code> \uff0c\u6307\u5411\u6a39\u7684\u6839\u7bc0\u9ede\u3002</p>"},{"location":"chapter_tree/binary_search_tree/#1","title":"1. \u00a0 \u67e5\u8a62\u7bc0\u9ede","text":"<p>\u7d66\u5b9a\u76ee\u6a19\u7bc0\u9ede\u503c <code>num</code> \uff0c\u53ef\u4ee5\u6839\u64da\u4e8c\u5143\u641c\u5c0b\u6a39\u7684\u6027\u8cea\u4f86\u67e5\u8a62\u3002\u5982\u5716 7-17 \u6240\u793a\uff0c\u6211\u5011\u5ba3\u544a\u4e00\u500b\u7bc0\u9ede <code>cur</code> \uff0c\u5f9e\u4e8c\u5143\u6a39\u7684\u6839\u7bc0\u9ede <code>root</code> \u51fa\u767c\uff0c\u8ff4\u5708\u6bd4\u8f03\u7bc0\u9ede\u503c <code>cur.val</code> \u548c <code>num</code> \u4e4b\u9593\u7684\u5927\u5c0f\u95dc\u4fc2\u3002</p> <ul> <li>\u82e5 <code>cur.val &lt; num</code> \uff0c\u8aaa\u660e\u76ee\u6a19\u7bc0\u9ede\u5728 <code>cur</code> \u7684\u53f3\u5b50\u6a39\u4e2d\uff0c\u56e0\u6b64\u57f7\u884c <code>cur = cur.right</code> \u3002</li> <li>\u82e5 <code>cur.val &gt; num</code> \uff0c\u8aaa\u660e\u76ee\u6a19\u7bc0\u9ede\u5728 <code>cur</code> \u7684\u5de6\u5b50\u6a39\u4e2d\uff0c\u56e0\u6b64\u57f7\u884c <code>cur = cur.left</code> \u3002</li> <li>\u82e5 <code>cur.val = num</code> \uff0c\u8aaa\u660e\u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\u4e26\u8fd4\u56de\u8a72\u7bc0\u9ede\u3002</li> </ul> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt; <p></p> <p></p> <p></p> <p></p> <p> \u5716 7-17 \u00a0 \u4e8c\u5143\u641c\u5c0b\u6a39\u67e5\u8a62\u7bc0\u9ede\u793a\u4f8b </p> <p>\u4e8c\u5143\u641c\u5c0b\u6a39\u7684\u67e5\u8a62\u64cd\u4f5c\u8207\u4e8c\u5206\u641c\u5c0b\u6f14\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u4e00\u81f4\uff0c\u90fd\u662f\u6bcf\u8f2a\u6392\u9664\u4e00\u534a\u60c5\u6cc1\u3002\u8ff4\u5708\u6b21\u6578\u6700\u591a\u70ba\u4e8c\u5143\u6a39\u7684\u9ad8\u5ea6\uff0c\u7576\u4e8c\u5143\u6a39\u5e73\u8861\u6642\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u6642\u9593\u3002\u793a\u4f8b\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_search_tree.py<pre><code>def search(self, num: int) -&gt; TreeNode | None:\n \"\"\"\u67e5\u8a62\u7bc0\u9ede\"\"\"\n cur = self._root\n # \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while cur is not None:\n # \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if cur.val &lt; num:\n cur = cur.right\n # \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n elif cur.val &gt; num:\n cur = cur.left\n # \u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n else:\n break\n return cur\n</code></pre> binary_search_tree.cpp<pre><code>/* \u67e5\u8a62\u7bc0\u9ede */\nTreeNode *search(int num) {\n TreeNode *cur = root;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != nullptr) {\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur-&gt;val &lt; num)\n cur = cur-&gt;right;\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else if (cur-&gt;val &gt; num)\n cur = cur-&gt;left;\n // \u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6a19\u7bc0\u9ede\n return cur;\n}\n</code></pre> binary_search_tree.java<pre><code>/* \u67e5\u8a62\u7bc0\u9ede */\nTreeNode search(int num) {\n TreeNode cur = root;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; num)\n cur = cur.right;\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else if (cur.val &gt; num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6a19\u7bc0\u9ede\n return cur;\n}\n</code></pre> binary_search_tree.cs<pre><code>/* \u67e5\u8a62\u7bc0\u9ede */\nTreeNode? Search(int num) {\n TreeNode? cur = root;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; num) cur =\n cur.right;\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else if (cur.val &gt; num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6a19\u7bc0\u9ede\n return cur;\n}\n</code></pre> binary_search_tree.go<pre><code>/* \u67e5\u8a62\u7bc0\u9ede */\nfunc (bst *binarySearchTree) search(num int) *TreeNode {\n node := bst.root\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n for node != nil {\n if node.Val.(int) &lt; num {\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n node = node.Right\n } else if node.Val.(int) &gt; num {\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n node = node.Left\n } else {\n // \u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n break\n }\n }\n // \u8fd4\u56de\u76ee\u6a19\u7bc0\u9ede\n return node\n}\n</code></pre> binary_search_tree.swift<pre><code>/* \u67e5\u8a62\u7bc0\u9ede */\nfunc search(num: Int) -&gt; TreeNode? {\n var cur = root\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while cur != nil {\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if cur!.val &lt; num {\n cur = cur?.right\n }\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else if cur!.val &gt; num {\n cur = cur?.left\n }\n // \u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n else {\n break\n }\n }\n // \u8fd4\u56de\u76ee\u6a19\u7bc0\u9ede\n return cur\n}\n</code></pre> binary_search_tree.js<pre><code>/* \u67e5\u8a62\u7bc0\u9ede */\nsearch(num) {\n let cur = this.root;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur !== null) {\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; num) cur = cur.right;\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else if (cur.val &gt; num) cur = cur.left;\n // \u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n else break;\n }\n // \u8fd4\u56de\u76ee\u6a19\u7bc0\u9ede\n return cur;\n}\n</code></pre> binary_search_tree.ts<pre><code>/* \u67e5\u8a62\u7bc0\u9ede */\nsearch(num: number): TreeNode | null {\n let cur = this.root;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur !== null) {\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; num) cur = cur.right;\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else if (cur.val &gt; num) cur = cur.left;\n // \u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n else break;\n }\n // \u8fd4\u56de\u76ee\u6a19\u7bc0\u9ede\n return cur;\n}\n</code></pre> binary_search_tree.dart<pre><code>/* \u67e5\u8a62\u7bc0\u9ede */\nTreeNode? search(int _num) {\n TreeNode? cur = _root;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; _num)\n cur = cur.right;\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else if (cur.val &gt; _num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6a19\u7bc0\u9ede\n return cur;\n}\n</code></pre> binary_search_tree.rs<pre><code>/* \u67e5\u8a62\u7bc0\u9ede */\npub fn search(&amp;self, num: i32) -&gt; OptionTreeNodeRc {\n let mut cur = self.root.clone();\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while let Some(node) = cur.clone() {\n match num.cmp(&amp;node.borrow().val) {\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n Ordering::Greater =&gt; cur = node.borrow().right.clone(),\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n Ordering::Less =&gt; cur = node.borrow().left.clone(),\n // \u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n Ordering::Equal =&gt; break,\n }\n }\n\n // \u8fd4\u56de\u76ee\u6a19\u7bc0\u9ede\n cur\n}\n</code></pre> binary_search_tree.c<pre><code>/* \u67e5\u8a62\u7bc0\u9ede */\nTreeNode *search(BinarySearchTree *bst, int num) {\n TreeNode *cur = bst-&gt;root;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != NULL) {\n if (cur-&gt;val &lt; num) {\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n cur = cur-&gt;right;\n } else if (cur-&gt;val &gt; num) {\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n cur = cur-&gt;left;\n } else {\n // \u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n break;\n }\n }\n // \u8fd4\u56de\u76ee\u6a19\u7bc0\u9ede\n return cur;\n}\n</code></pre> binary_search_tree.kt<pre><code>/* \u67e5\u8a62\u7bc0\u9ede */\nfun search(num: Int): TreeNode? {\n var cur = root\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n cur = if (cur._val &lt; num)\n cur.right\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else if (cur._val &gt; num)\n cur.left\n // \u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n else\n break\n }\n // \u8fd4\u56de\u76ee\u6a19\u7bc0\u9ede\n return cur\n}\n</code></pre> binary_search_tree.rb<pre><code>### \u67e5\u8a62\u7bc0\u9ede ###\ndef search(num)\n cur = @root\n\n # \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while !cur.nil?\n # \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if cur.val &lt; num\n cur = cur.right\n # \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n elsif cur.val &gt; num\n cur = cur.left\n # \u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n else\n break\n end\n end\n\n cur\nend\n</code></pre> binary_search_tree.zig<pre><code>// \u67e5\u8a62\u7bc0\u9ede\nfn search(self: *Self, num: T) ?*inc.TreeNode(T) {\n var cur = self.root;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.?.val &lt; num) {\n cur = cur.?.right;\n // \u76ee\u6a19\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n } else if (cur.?.val &gt; num) {\n cur = cur.?.left;\n // \u627e\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n } else {\n break;\n }\n }\n // \u8fd4\u56de\u76ee\u6a19\u7bc0\u9ede\n return cur;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_tree/binary_search_tree/#2","title":"2. \u00a0 \u63d2\u5165\u7bc0\u9ede","text":"<p>\u7d66\u5b9a\u4e00\u500b\u5f85\u63d2\u5165\u5143\u7d20 <code>num</code> \uff0c\u70ba\u4e86\u4fdd\u6301\u4e8c\u5143\u641c\u5c0b\u6a39\u201c\u5de6\u5b50\u6a39 &lt; \u6839\u7bc0\u9ede &lt; \u53f3\u5b50\u6a39\u201d\u7684\u6027\u8cea\uff0c\u63d2\u5165\u64cd\u4f5c\u6d41\u7a0b\u5982\u5716 7-18 \u6240\u793a\u3002</p> <ol> <li>\u67e5\u8a62\u63d2\u5165\u4f4d\u7f6e\uff1a\u8207\u67e5\u8a62\u64cd\u4f5c\u76f8\u4f3c\uff0c\u5f9e\u6839\u7bc0\u9ede\u51fa\u767c\uff0c\u6839\u64da\u7576\u524d\u7bc0\u9ede\u503c\u548c <code>num</code> \u7684\u5927\u5c0f\u95dc\u4fc2\u8ff4\u5708\u5411\u4e0b\u641c\u5c0b\uff0c\u76f4\u5230\u8d8a\u904e\u8449\u7bc0\u9ede\uff08\u8d70\u8a2a\u81f3 <code>None</code> \uff09\u6642\u8df3\u51fa\u8ff4\u5708\u3002</li> <li>\u5728\u8a72\u4f4d\u7f6e\u63d2\u5165\u7bc0\u9ede\uff1a\u521d\u59cb\u5316\u7bc0\u9ede <code>num</code> \uff0c\u5c07\u8a72\u7bc0\u9ede\u7f6e\u65bc <code>None</code> \u7684\u4f4d\u7f6e\u3002</li> </ol> <p></p> <p> \u5716 7-18 \u00a0 \u5728\u4e8c\u5143\u641c\u5c0b\u6a39\u4e2d\u63d2\u5165\u7bc0\u9ede </p> <p>\u5728\u7a0b\u5f0f\u78bc\u5be6\u73fe\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u5169\u9ede\u3002</p> <ul> <li>\u4e8c\u5143\u641c\u5c0b\u6a39\u4e0d\u5141\u8a31\u5b58\u5728\u91cd\u8907\u7bc0\u9ede\uff0c\u5426\u5247\u5c07\u9055\u53cd\u5176\u5b9a\u7fa9\u3002\u56e0\u6b64\uff0c\u82e5\u5f85\u63d2\u5165\u7bc0\u9ede\u5728\u6a39\u4e2d\u5df2\u5b58\u5728\uff0c\u5247\u4e0d\u57f7\u884c\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\u3002</li> <li>\u70ba\u4e86\u5be6\u73fe\u63d2\u5165\u7bc0\u9ede\uff0c\u6211\u5011\u9700\u8981\u85c9\u52a9\u7bc0\u9ede <code>pre</code> \u5132\u5b58\u4e0a\u4e00\u8f2a\u8ff4\u5708\u7684\u7bc0\u9ede\u3002\u9019\u6a23\u5728\u8d70\u8a2a\u81f3 <code>None</code> \u6642\uff0c\u6211\u5011\u53ef\u4ee5\u7372\u53d6\u5230\u5176\u7236\u7bc0\u9ede\uff0c\u5f9e\u800c\u5b8c\u6210\u7bc0\u9ede\u63d2\u5165\u64cd\u4f5c\u3002</li> </ul> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_search_tree.py<pre><code>def insert(self, num: int):\n \"\"\"\u63d2\u5165\u7bc0\u9ede\"\"\"\n # \u82e5\u6a39\u70ba\u7a7a\uff0c\u5247\u521d\u59cb\u5316\u6839\u7bc0\u9ede\n if self._root is None:\n self._root = TreeNode(num)\n return\n # \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n cur, pre = self._root, None\n while cur is not None:\n # \u627e\u5230\u91cd\u8907\u7bc0\u9ede\uff0c\u76f4\u63a5\u8fd4\u56de\n if cur.val == num:\n return\n pre = cur\n # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if cur.val &lt; num:\n cur = cur.right\n # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else:\n cur = cur.left\n # \u63d2\u5165\u7bc0\u9ede\n node = TreeNode(num)\n if pre.val &lt; num:\n pre.right = node\n else:\n pre.left = node\n</code></pre> binary_search_tree.cpp<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nvoid insert(int num) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u5247\u521d\u59cb\u5316\u6839\u7bc0\u9ede\n if (root == nullptr) {\n root = new TreeNode(num);\n return;\n }\n TreeNode *cur = root, *pre = nullptr;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != nullptr) {\n // \u627e\u5230\u91cd\u8907\u7bc0\u9ede\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur-&gt;val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur-&gt;val &lt; num)\n cur = cur-&gt;right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else\n cur = cur-&gt;left;\n }\n // \u63d2\u5165\u7bc0\u9ede\n TreeNode *node = new TreeNode(num);\n if (pre-&gt;val &lt; num)\n pre-&gt;right = node;\n else\n pre-&gt;left = node;\n}\n</code></pre> binary_search_tree.java<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nvoid insert(int num) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u5247\u521d\u59cb\u5316\u6839\u7bc0\u9ede\n if (root == null) {\n root = new TreeNode(num);\n return;\n }\n TreeNode cur = root, pre = null;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u8907\u7bc0\u9ede\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else\n cur = cur.left;\n }\n // \u63d2\u5165\u7bc0\u9ede\n TreeNode node = new TreeNode(num);\n if (pre.val &lt; num)\n pre.right = node;\n else\n pre.left = node;\n}\n</code></pre> binary_search_tree.cs<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nvoid Insert(int num) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u5247\u521d\u59cb\u5316\u6839\u7bc0\u9ede\n if (root == null) {\n root = new TreeNode(num);\n return;\n }\n TreeNode? cur = root, pre = null;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u8907\u7bc0\u9ede\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else\n cur = cur.left;\n }\n\n // \u63d2\u5165\u7bc0\u9ede\n TreeNode node = new(num);\n if (pre != null) {\n if (pre.val &lt; num)\n pre.right = node;\n else\n pre.left = node;\n }\n}\n</code></pre> binary_search_tree.go<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nfunc (bst *binarySearchTree) insert(num int) {\n cur := bst.root\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u5247\u521d\u59cb\u5316\u6839\u7bc0\u9ede\n if cur == nil {\n bst.root = NewTreeNode(num)\n return\n }\n // \u5f85\u63d2\u5165\u7bc0\u9ede\u4e4b\u524d\u7684\u7bc0\u9ede\u4f4d\u7f6e\n var pre *TreeNode = nil\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n for cur != nil {\n if cur.Val == num {\n return\n }\n pre = cur\n if cur.Val.(int) &lt; num {\n cur = cur.Right\n } else {\n cur = cur.Left\n }\n }\n // \u63d2\u5165\u7bc0\u9ede\n node := NewTreeNode(num)\n if pre.Val.(int) &lt; num {\n pre.Right = node\n } else {\n pre.Left = node\n }\n}\n</code></pre> binary_search_tree.swift<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nfunc insert(num: Int) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u5247\u521d\u59cb\u5316\u6839\u7bc0\u9ede\n if root == nil {\n root = TreeNode(x: num)\n return\n }\n var cur = root\n var pre: TreeNode?\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while cur != nil {\n // \u627e\u5230\u91cd\u8907\u7bc0\u9ede\uff0c\u76f4\u63a5\u8fd4\u56de\n if cur!.val == num {\n return\n }\n pre = cur\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if cur!.val &lt; num {\n cur = cur?.right\n }\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else {\n cur = cur?.left\n }\n }\n // \u63d2\u5165\u7bc0\u9ede\n let node = TreeNode(x: num)\n if pre!.val &lt; num {\n pre?.right = node\n } else {\n pre?.left = node\n }\n}\n</code></pre> binary_search_tree.js<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\ninsert(num) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u5247\u521d\u59cb\u5316\u6839\u7bc0\u9ede\n if (this.root === null) {\n this.root = new TreeNode(num);\n return;\n }\n let cur = this.root,\n pre = null;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u91cd\u8907\u7bc0\u9ede\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val === num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; num) cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else cur = cur.left;\n }\n // \u63d2\u5165\u7bc0\u9ede\n const node = new TreeNode(num);\n if (pre.val &lt; num) pre.right = node;\n else pre.left = node;\n}\n</code></pre> binary_search_tree.ts<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\ninsert(num: number): void {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u5247\u521d\u59cb\u5316\u6839\u7bc0\u9ede\n if (this.root === null) {\n this.root = new TreeNode(num);\n return;\n }\n let cur: TreeNode | null = this.root,\n pre: TreeNode | null = null;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u91cd\u8907\u7bc0\u9ede\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val === num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; num) cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else cur = cur.left;\n }\n // \u63d2\u5165\u7bc0\u9ede\n const node = new TreeNode(num);\n if (pre!.val &lt; num) pre!.right = node;\n else pre!.left = node;\n}\n</code></pre> binary_search_tree.dart<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nvoid insert(int _num) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u5247\u521d\u59cb\u5316\u6839\u7bc0\u9ede\n if (_root == null) {\n _root = TreeNode(_num);\n return;\n }\n TreeNode? cur = _root;\n TreeNode? pre = null;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u8907\u7bc0\u9ede\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == _num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; _num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else\n cur = cur.left;\n }\n // \u63d2\u5165\u7bc0\u9ede\n TreeNode? node = TreeNode(_num);\n if (pre!.val &lt; _num)\n pre.right = node;\n else\n pre.left = node;\n}\n</code></pre> binary_search_tree.rs<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\npub fn insert(&amp;mut self, num: i32) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u5247\u521d\u59cb\u5316\u6839\u7bc0\u9ede\n if self.root.is_none() {\n self.root = Some(TreeNode::new(num));\n return;\n }\n let mut cur = self.root.clone();\n let mut pre = None;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while let Some(node) = cur.clone() {\n match num.cmp(&amp;node.borrow().val) {\n // \u627e\u5230\u91cd\u8907\u7bc0\u9ede\uff0c\u76f4\u63a5\u8fd4\u56de\n Ordering::Equal =&gt; return,\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n Ordering::Greater =&gt; {\n pre = cur.clone();\n cur = node.borrow().right.clone();\n }\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n Ordering::Less =&gt; {\n pre = cur.clone();\n cur = node.borrow().left.clone();\n }\n }\n }\n // \u63d2\u5165\u7bc0\u9ede\n let pre = pre.unwrap();\n let node = Some(TreeNode::new(num));\n if num &gt; pre.borrow().val {\n pre.borrow_mut().right = node;\n } else {\n pre.borrow_mut().left = node;\n }\n}\n</code></pre> binary_search_tree.c<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nvoid insert(BinarySearchTree *bst, int num) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u5247\u521d\u59cb\u5316\u6839\u7bc0\u9ede\n if (bst-&gt;root == NULL) {\n bst-&gt;root = newTreeNode(num);\n return;\n }\n TreeNode *cur = bst-&gt;root, *pre = NULL;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != NULL) {\n // \u627e\u5230\u91cd\u8907\u7bc0\u9ede\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur-&gt;val == num) {\n return;\n }\n pre = cur;\n if (cur-&gt;val &lt; num) {\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n cur = cur-&gt;right;\n } else {\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n cur = cur-&gt;left;\n }\n }\n // \u63d2\u5165\u7bc0\u9ede\n TreeNode *node = newTreeNode(num);\n if (pre-&gt;val &lt; num) {\n pre-&gt;right = node;\n } else {\n pre-&gt;left = node;\n }\n}\n</code></pre> binary_search_tree.kt<pre><code>/* \u63d2\u5165\u7bc0\u9ede */\nfun insert(num: Int) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u5247\u521d\u59cb\u5316\u6839\u7bc0\u9ede\n if (root == null) {\n root = TreeNode(num)\n return\n }\n var cur = root\n var pre: TreeNode? = null\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u8907\u7bc0\u9ede\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur._val == num)\n return\n pre = cur\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n cur = if (cur._val &lt; num)\n cur.right\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else\n cur.left\n }\n // \u63d2\u5165\u7bc0\u9ede\n val node = TreeNode(num)\n if (pre?._val!! &lt; num)\n pre.right = node\n else\n pre.left = node\n}\n</code></pre> binary_search_tree.rb<pre><code>### \u63d2\u5165\u7bc0\u9ede ###\ndef insert(num)\n # \u82e5\u6a39\u70ba\u7a7a\uff0c\u5247\u521d\u59cb\u5316\u6839\u7bc0\u9ede\n if @root.nil?\n @root = TreeNode.new(num)\n return\n end\n\n # \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n cur, pre = @root, nil\n while !cur.nil?\n # \u627e\u5230\u91cd\u8907\u7bc0\u9ede\uff0c\u76f4\u63a5\u8fd4\u56de\n return if cur.val == num\n\n pre = cur\n # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if cur.val &lt; num\n cur = cur.right\n # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else\n cur = cur.left\n end\n end\n\n # \u63d2\u5165\u7bc0\u9ede\n node = TreeNode.new(num)\n if pre.val &lt; num\n pre.right = node\n else\n pre.left = node\n end\nend\n</code></pre> binary_search_tree.zig<pre><code>// \u63d2\u5165\u7bc0\u9ede\nfn insert(self: *Self, num: T) !void {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u5247\u521d\u59cb\u5316\u6839\u7bc0\u9ede\n if (self.root == null) {\n self.root = try self.mem_allocator.create(inc.TreeNode(T));\n return;\n }\n var cur = self.root;\n var pre: ?*inc.TreeNode(T) = null;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u8907\u7bc0\u9ede\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.?.val == num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.?.val &lt; num) {\n cur = cur.?.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n } else {\n cur = cur.?.left;\n }\n }\n // \u63d2\u5165\u7bc0\u9ede\n var node = try self.mem_allocator.create(inc.TreeNode(T));\n node.init(num);\n if (pre.?.val &lt; num) {\n pre.?.right = node;\n } else {\n pre.?.left = node;\n }\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>\u8207\u67e5\u8a62\u7bc0\u9ede\u76f8\u540c\uff0c\u63d2\u5165\u7bc0\u9ede\u4f7f\u7528 \\(O(\\log n)\\) \u6642\u9593\u3002</p>"},{"location":"chapter_tree/binary_search_tree/#3","title":"3. \u00a0 \u522a\u9664\u7bc0\u9ede","text":"<p>\u5148\u5728\u4e8c\u5143\u6a39\u4e2d\u67e5\u8a62\u5230\u76ee\u6a19\u7bc0\u9ede\uff0c\u518d\u5c07\u5176\u522a\u9664\u3002\u8207\u63d2\u5165\u7bc0\u9ede\u985e\u4f3c\uff0c\u6211\u5011\u9700\u8981\u4fdd\u8b49\u5728\u522a\u9664\u64cd\u4f5c\u5b8c\u6210\u5f8c\uff0c\u4e8c\u5143\u641c\u5c0b\u6a39\u7684\u201c\u5de6\u5b50\u6a39 &lt; \u6839\u7bc0\u9ede &lt; \u53f3\u5b50\u6a39\u201d\u7684\u6027\u8cea\u4ecd\u7136\u6eff\u8db3\u3002\u56e0\u6b64\uff0c\u6211\u5011\u6839\u64da\u76ee\u6a19\u7bc0\u9ede\u7684\u5b50\u7bc0\u9ede\u6578\u91cf\uff0c\u5206 0\u30011 \u548c 2 \u4e09\u7a2e\u60c5\u6cc1\uff0c\u57f7\u884c\u5c0d\u61c9\u7684\u522a\u9664\u7bc0\u9ede\u64cd\u4f5c\u3002</p> <p>\u5982\u5716 7-19 \u6240\u793a\uff0c\u7576\u5f85\u522a\u9664\u7bc0\u9ede\u7684\u5ea6\u70ba \\(0\\) \u6642\uff0c\u8868\u793a\u8a72\u7bc0\u9ede\u662f\u8449\u7bc0\u9ede\uff0c\u53ef\u4ee5\u76f4\u63a5\u522a\u9664\u3002</p> <p></p> <p> \u5716 7-19 \u00a0 \u5728\u4e8c\u5143\u641c\u5c0b\u6a39\u4e2d\u522a\u9664\u7bc0\u9ede\uff08\u5ea6\u70ba 0 \uff09 </p> <p>\u5982\u5716 7-20 \u6240\u793a\uff0c\u7576\u5f85\u522a\u9664\u7bc0\u9ede\u7684\u5ea6\u70ba \\(1\\) \u6642\uff0c\u5c07\u5f85\u522a\u9664\u7bc0\u9ede\u66ff\u63db\u70ba\u5176\u5b50\u7bc0\u9ede\u5373\u53ef\u3002</p> <p></p> <p> \u5716 7-20 \u00a0 \u5728\u4e8c\u5143\u641c\u5c0b\u6a39\u4e2d\u522a\u9664\u7bc0\u9ede\uff08\u5ea6\u70ba 1 \uff09 </p> <p>\u7576\u5f85\u522a\u9664\u7bc0\u9ede\u7684\u5ea6\u70ba \\(2\\) \u6642\uff0c\u6211\u5011\u7121\u6cd5\u76f4\u63a5\u522a\u9664\u5b83\uff0c\u800c\u9700\u8981\u4f7f\u7528\u4e00\u500b\u7bc0\u9ede\u66ff\u63db\u8a72\u7bc0\u9ede\u3002\u7531\u65bc\u8981\u4fdd\u6301\u4e8c\u5143\u641c\u5c0b\u6a39\u201c\u5de6\u5b50\u6a39 \\(&lt;\\) \u6839\u7bc0\u9ede \\(&lt;\\) \u53f3\u5b50\u6a39\u201d\u7684\u6027\u8cea\uff0c\u56e0\u6b64\u9019\u500b\u7bc0\u9ede\u53ef\u4ee5\u662f\u53f3\u5b50\u6a39\u7684\u6700\u5c0f\u7bc0\u9ede\u6216\u5de6\u5b50\u6a39\u7684\u6700\u5927\u7bc0\u9ede\u3002</p> <p>\u5047\u8a2d\u6211\u5011\u9078\u64c7\u53f3\u5b50\u6a39\u7684\u6700\u5c0f\u7bc0\u9ede\uff08\u4e2d\u5e8f\u8d70\u8a2a\u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\uff09\uff0c\u5247\u522a\u9664\u64cd\u4f5c\u6d41\u7a0b\u5982\u5716 7-21 \u6240\u793a\u3002</p> <ol> <li>\u627e\u5230\u5f85\u522a\u9664\u7bc0\u9ede\u5728\u201c\u4e2d\u5e8f\u8d70\u8a2a\u5e8f\u5217\u201d\u4e2d\u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\uff0c\u8a18\u70ba <code>tmp</code> \u3002</li> <li>\u7528 <code>tmp</code> \u7684\u503c\u8986\u84cb\u5f85\u522a\u9664\u7bc0\u9ede\u7684\u503c\uff0c\u4e26\u5728\u6a39\u4e2d\u905e\u8ff4\u522a\u9664\u7bc0\u9ede <code>tmp</code> \u3002</li> </ol> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt; <p></p> <p></p> <p></p> <p></p> <p> \u5716 7-21 \u00a0 \u5728\u4e8c\u5143\u641c\u5c0b\u6a39\u4e2d\u522a\u9664\u7bc0\u9ede\uff08\u5ea6\u70ba 2 \uff09 </p> <p>\u522a\u9664\u7bc0\u9ede\u64cd\u4f5c\u540c\u6a23\u4f7f\u7528 \\(O(\\log n)\\) \u6642\u9593\uff0c\u5176\u4e2d\u67e5\u8a62\u5f85\u522a\u9664\u7bc0\u9ede\u9700\u8981 \\(O(\\log n)\\) \u6642\u9593\uff0c\u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u5f8c\u7e7c\u7bc0\u9ede\u9700\u8981 \\(O(\\log n)\\) \u6642\u9593\u3002\u793a\u4f8b\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_search_tree.py<pre><code>def remove(self, num: int):\n \"\"\"\u522a\u9664\u7bc0\u9ede\"\"\"\n # \u82e5\u6a39\u70ba\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if self._root is None:\n return\n # \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n cur, pre = self._root, None\n while cur is not None:\n # \u627e\u5230\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n if cur.val == num:\n break\n pre = cur\n # \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if cur.val &lt; num:\n cur = cur.right\n # \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else:\n cur = cur.left\n # \u82e5\u7121\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if cur is None:\n return\n\n # \u5b50\u7bc0\u9ede\u6578\u91cf = 0 or 1\n if cur.left is None or cur.right is None:\n # \u7576\u5b50\u7bc0\u9ede\u6578\u91cf = 0 / 1 \u6642\uff0c child = null / \u8a72\u5b50\u7bc0\u9ede\n child = cur.left or cur.right\n # \u522a\u9664\u7bc0\u9ede cur\n if cur != self._root:\n if pre.left == cur:\n pre.left = child\n else:\n pre.right = child\n else:\n # \u82e5\u522a\u9664\u7bc0\u9ede\u70ba\u6839\u7bc0\u9ede\uff0c\u5247\u91cd\u65b0\u6307\u5b9a\u6839\u7bc0\u9ede\n self._root = child\n # \u5b50\u7bc0\u9ede\u6578\u91cf = 2\n else:\n # \u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u4e2d cur \u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\n tmp: TreeNode = cur.right\n while tmp.left is not None:\n tmp = tmp.left\n # \u905e\u8ff4\u522a\u9664\u7bc0\u9ede tmp\n self.remove(tmp.val)\n # \u7528 tmp \u8986\u84cb cur\n cur.val = tmp.val\n</code></pre> binary_search_tree.cpp<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nvoid remove(int num) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == nullptr)\n return;\n TreeNode *cur = root, *pre = nullptr;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != nullptr) {\n // \u627e\u5230\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n if (cur-&gt;val == num)\n break;\n pre = cur;\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur-&gt;val &lt; num)\n cur = cur-&gt;right;\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else\n cur = cur-&gt;left;\n }\n // \u82e5\u7121\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (cur == nullptr)\n return;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 or 1\n if (cur-&gt;left == nullptr || cur-&gt;right == nullptr) {\n // \u7576\u5b50\u7bc0\u9ede\u6578\u91cf = 0 / 1 \u6642\uff0c child = nullptr / \u8a72\u5b50\u7bc0\u9ede\n TreeNode *child = cur-&gt;left != nullptr ? cur-&gt;left : cur-&gt;right;\n // \u522a\u9664\u7bc0\u9ede cur\n if (cur != root) {\n if (pre-&gt;left == cur)\n pre-&gt;left = child;\n else\n pre-&gt;right = child;\n } else {\n // \u82e5\u522a\u9664\u7bc0\u9ede\u70ba\u6839\u7bc0\u9ede\uff0c\u5247\u91cd\u65b0\u6307\u5b9a\u6839\u7bc0\u9ede\n root = child;\n }\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n delete cur;\n }\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2\n else {\n // \u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u4e2d cur \u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\n TreeNode *tmp = cur-&gt;right;\n while (tmp-&gt;left != nullptr) {\n tmp = tmp-&gt;left;\n }\n int tmpVal = tmp-&gt;val;\n // \u905e\u8ff4\u522a\u9664\u7bc0\u9ede tmp\n remove(tmp-&gt;val);\n // \u7528 tmp \u8986\u84cb cur\n cur-&gt;val = tmpVal;\n }\n}\n</code></pre> binary_search_tree.java<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nvoid remove(int num) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == null)\n return;\n TreeNode cur = root, pre = null;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n if (cur.val == num)\n break;\n pre = cur;\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; num)\n cur = cur.right;\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u7121\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (cur == null)\n return;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u7576\u5b50\u7bc0\u9ede\u6578\u91cf = 0 / 1 \u6642\uff0c child = null / \u8a72\u5b50\u7bc0\u9ede\n TreeNode child = cur.left != null ? cur.left : cur.right;\n // \u522a\u9664\u7bc0\u9ede cur\n if (cur != root) {\n if (pre.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u522a\u9664\u7bc0\u9ede\u70ba\u6839\u7bc0\u9ede\uff0c\u5247\u91cd\u65b0\u6307\u5b9a\u6839\u7bc0\u9ede\n root = child;\n }\n }\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2\n else {\n // \u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u4e2d cur \u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\n TreeNode tmp = cur.right;\n while (tmp.left != null) {\n tmp = tmp.left;\n }\n // \u905e\u8ff4\u522a\u9664\u7bc0\u9ede tmp\n remove(tmp.val);\n // \u7528 tmp \u8986\u84cb cur\n cur.val = tmp.val;\n }\n}\n</code></pre> binary_search_tree.cs<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nvoid Remove(int num) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == null)\n return;\n TreeNode? cur = root, pre = null;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n if (cur.val == num)\n break;\n pre = cur;\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; num)\n cur = cur.right;\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u7121\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (cur == null)\n return;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u7576\u5b50\u7bc0\u9ede\u6578\u91cf = 0 / 1 \u6642\uff0c child = null / \u8a72\u5b50\u7bc0\u9ede\n TreeNode? child = cur.left ?? cur.right;\n // \u522a\u9664\u7bc0\u9ede cur\n if (cur != root) {\n if (pre!.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u522a\u9664\u7bc0\u9ede\u70ba\u6839\u7bc0\u9ede\uff0c\u5247\u91cd\u65b0\u6307\u5b9a\u6839\u7bc0\u9ede\n root = child;\n }\n }\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2\n else {\n // \u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u4e2d cur \u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\n TreeNode? tmp = cur.right;\n while (tmp.left != null) {\n tmp = tmp.left;\n }\n // \u905e\u8ff4\u522a\u9664\u7bc0\u9ede tmp\n Remove(tmp.val!.Value);\n // \u7528 tmp \u8986\u84cb cur\n cur.val = tmp.val;\n }\n}\n</code></pre> binary_search_tree.go<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nfunc (bst *binarySearchTree) remove(num int) {\n cur := bst.root\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5f85\u522a\u9664\u7bc0\u9ede\u4e4b\u524d\u7684\u7bc0\u9ede\u4f4d\u7f6e\n var pre *TreeNode = nil\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n for cur != nil {\n if cur.Val == num {\n break\n }\n pre = cur\n if cur.Val.(int) &lt; num {\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728\u53f3\u5b50\u6a39\u4e2d\n cur = cur.Right\n } else {\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728\u5de6\u5b50\u6a39\u4e2d\n cur = cur.Left\n }\n }\n // \u82e5\u7121\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5b50\u7bc0\u9ede\u6578\u70ba 0 \u6216 1\n if cur.Left == nil || cur.Right == nil {\n var child *TreeNode = nil\n // \u53d6\u51fa\u5f85\u522a\u9664\u7bc0\u9ede\u7684\u5b50\u7bc0\u9ede\n if cur.Left != nil {\n child = cur.Left\n } else {\n child = cur.Right\n }\n // \u522a\u9664\u7bc0\u9ede cur\n if cur != bst.root {\n if pre.Left == cur {\n pre.Left = child\n } else {\n pre.Right = child\n }\n } else {\n // \u82e5\u522a\u9664\u7bc0\u9ede\u70ba\u6839\u7bc0\u9ede\uff0c\u5247\u91cd\u65b0\u6307\u5b9a\u6839\u7bc0\u9ede\n bst.root = child\n }\n // \u5b50\u7bc0\u9ede\u6578\u70ba 2\n } else {\n // \u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u4e2d\u5f85\u522a\u9664\u7bc0\u9ede cur \u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\n tmp := cur.Right\n for tmp.Left != nil {\n tmp = tmp.Left\n }\n // \u905e\u8ff4\u522a\u9664\u7bc0\u9ede tmp\n bst.remove(tmp.Val.(int))\n // \u7528 tmp \u8986\u84cb cur\n cur.Val = tmp.Val\n }\n}\n</code></pre> binary_search_tree.swift<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nfunc remove(num: Int) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if root == nil {\n return\n }\n var cur = root\n var pre: TreeNode?\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while cur != nil {\n // \u627e\u5230\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n if cur!.val == num {\n break\n }\n pre = cur\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if cur!.val &lt; num {\n cur = cur?.right\n }\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else {\n cur = cur?.left\n }\n }\n // \u82e5\u7121\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 or 1\n if cur?.left == nil || cur?.right == nil {\n // \u7576\u5b50\u7bc0\u9ede\u6578\u91cf = 0 / 1 \u6642\uff0c child = null / \u8a72\u5b50\u7bc0\u9ede\n let child = cur?.left ?? cur?.right\n // \u522a\u9664\u7bc0\u9ede cur\n if cur !== root {\n if pre?.left === cur {\n pre?.left = child\n } else {\n pre?.right = child\n }\n } else {\n // \u82e5\u522a\u9664\u7bc0\u9ede\u70ba\u6839\u7bc0\u9ede\uff0c\u5247\u91cd\u65b0\u6307\u5b9a\u6839\u7bc0\u9ede\n root = child\n }\n }\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2\n else {\n // \u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u4e2d cur \u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\n var tmp = cur?.right\n while tmp?.left != nil {\n tmp = tmp?.left\n }\n // \u905e\u8ff4\u522a\u9664\u7bc0\u9ede tmp\n remove(num: tmp!.val)\n // \u7528 tmp \u8986\u84cb cur\n cur?.val = tmp!.val\n }\n}\n</code></pre> binary_search_tree.js<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nremove(num) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (this.root === null) return;\n let cur = this.root,\n pre = null;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n if (cur.val === num) break;\n pre = cur;\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; num) cur = cur.right;\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else cur = cur.left;\n }\n // \u82e5\u7121\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (cur === null) return;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 or 1\n if (cur.left === null || cur.right === null) {\n // \u7576\u5b50\u7bc0\u9ede\u6578\u91cf = 0 / 1 \u6642\uff0c child = null / \u8a72\u5b50\u7bc0\u9ede\n const child = cur.left !== null ? cur.left : cur.right;\n // \u522a\u9664\u7bc0\u9ede cur\n if (cur !== this.root) {\n if (pre.left === cur) pre.left = child;\n else pre.right = child;\n } else {\n // \u82e5\u522a\u9664\u7bc0\u9ede\u70ba\u6839\u7bc0\u9ede\uff0c\u5247\u91cd\u65b0\u6307\u5b9a\u6839\u7bc0\u9ede\n this.root = child;\n }\n }\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2\n else {\n // \u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u4e2d cur \u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\n let tmp = cur.right;\n while (tmp.left !== null) {\n tmp = tmp.left;\n }\n // \u905e\u8ff4\u522a\u9664\u7bc0\u9ede tmp\n this.remove(tmp.val);\n // \u7528 tmp \u8986\u84cb cur\n cur.val = tmp.val;\n }\n}\n</code></pre> binary_search_tree.ts<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nremove(num: number): void {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (this.root === null) return;\n let cur: TreeNode | null = this.root,\n pre: TreeNode | null = null;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n if (cur.val === num) break;\n pre = cur;\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; num) cur = cur.right;\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else cur = cur.left;\n }\n // \u82e5\u7121\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (cur === null) return;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 or 1\n if (cur.left === null || cur.right === null) {\n // \u7576\u5b50\u7bc0\u9ede\u6578\u91cf = 0 / 1 \u6642\uff0c child = null / \u8a72\u5b50\u7bc0\u9ede\n const child: TreeNode | null =\n cur.left !== null ? cur.left : cur.right;\n // \u522a\u9664\u7bc0\u9ede cur\n if (cur !== this.root) {\n if (pre!.left === cur) pre!.left = child;\n else pre!.right = child;\n } else {\n // \u82e5\u522a\u9664\u7bc0\u9ede\u70ba\u6839\u7bc0\u9ede\uff0c\u5247\u91cd\u65b0\u6307\u5b9a\u6839\u7bc0\u9ede\n this.root = child;\n }\n }\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2\n else {\n // \u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u4e2d cur \u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\n let tmp: TreeNode | null = cur.right;\n while (tmp!.left !== null) {\n tmp = tmp!.left;\n }\n // \u905e\u8ff4\u522a\u9664\u7bc0\u9ede tmp\n this.remove(tmp!.val);\n // \u7528 tmp \u8986\u84cb cur\n cur.val = tmp!.val;\n }\n}\n</code></pre> binary_search_tree.dart<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nvoid remove(int _num) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (_root == null) return;\n TreeNode? cur = _root;\n TreeNode? pre = null;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n if (cur.val == _num) break;\n pre = cur;\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.val &lt; _num)\n cur = cur.right;\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u7121\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur == null) return;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u7576\u5b50\u7bc0\u9ede\u6578\u91cf = 0 / 1 \u6642\uff0c child = null / \u8a72\u5b50\u7bc0\u9ede\n TreeNode? child = cur.left ?? cur.right;\n // \u522a\u9664\u7bc0\u9ede cur\n if (cur != _root) {\n if (pre!.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u522a\u9664\u7bc0\u9ede\u70ba\u6839\u7bc0\u9ede\uff0c\u5247\u91cd\u65b0\u6307\u5b9a\u6839\u7bc0\u9ede\n _root = child;\n }\n } else {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2\n // \u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u4e2d cur \u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\n TreeNode? tmp = cur.right;\n while (tmp!.left != null) {\n tmp = tmp.left;\n }\n // \u905e\u8ff4\u522a\u9664\u7bc0\u9ede tmp\n remove(tmp.val);\n // \u7528 tmp \u8986\u84cb cur\n cur.val = tmp.val;\n }\n}\n</code></pre> binary_search_tree.rs<pre><code>/* \u522a\u9664\u7bc0\u9ede */\npub fn remove(&amp;mut self, num: i32) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if self.root.is_none() {\n return;\n }\n let mut cur = self.root.clone();\n let mut pre = None;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while let Some(node) = cur.clone() {\n match num.cmp(&amp;node.borrow().val) {\n // \u627e\u5230\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n Ordering::Equal =&gt; break,\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n Ordering::Greater =&gt; {\n pre = cur.clone();\n cur = node.borrow().right.clone();\n }\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n Ordering::Less =&gt; {\n pre = cur.clone();\n cur = node.borrow().left.clone();\n }\n }\n }\n // \u82e5\u7121\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if cur.is_none() {\n return;\n }\n let cur = cur.unwrap();\n let (left_child, right_child) = (cur.borrow().left.clone(), cur.borrow().right.clone());\n match (left_child.clone(), right_child.clone()) {\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 or 1\n (None, None) | (Some(_), None) | (None, Some(_)) =&gt; {\n // \u7576\u5b50\u7bc0\u9ede\u6578\u91cf = 0 / 1 \u6642\uff0c child = nullptr / \u8a72\u5b50\u7bc0\u9ede\n let child = left_child.or(right_child);\n let pre = pre.unwrap();\n // \u522a\u9664\u7bc0\u9ede cur\n if !Rc::ptr_eq(&amp;cur, self.root.as_ref().unwrap()) {\n let left = pre.borrow().left.clone();\n if left.is_some() &amp;&amp; Rc::ptr_eq(&amp;left.as_ref().unwrap(), &amp;cur) {\n pre.borrow_mut().left = child;\n } else {\n pre.borrow_mut().right = child;\n }\n } else {\n // \u82e5\u522a\u9664\u7bc0\u9ede\u70ba\u6839\u7bc0\u9ede\uff0c\u5247\u91cd\u65b0\u6307\u5b9a\u6839\u7bc0\u9ede\n self.root = child;\n }\n }\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2\n (Some(_), Some(_)) =&gt; {\n // \u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u4e2d cur \u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\n let mut tmp = cur.borrow().right.clone();\n while let Some(node) = tmp.clone() {\n if node.borrow().left.is_some() {\n tmp = node.borrow().left.clone();\n } else {\n break;\n }\n }\n let tmpval = tmp.unwrap().borrow().val;\n // \u905e\u8ff4\u522a\u9664\u7bc0\u9ede tmp\n self.remove(tmpval);\n // \u7528 tmp \u8986\u84cb cur\n cur.borrow_mut().val = tmpval;\n }\n }\n}\n</code></pre> binary_search_tree.c<pre><code>/* \u522a\u9664\u7bc0\u9ede */\n// \u7531\u65bc\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u8655\u7121\u6cd5\u4f7f\u7528 remove \u95dc\u9375\u8a5e\nvoid removeItem(BinarySearchTree *bst, int num) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (bst-&gt;root == NULL)\n return;\n TreeNode *cur = bst-&gt;root, *pre = NULL;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != NULL) {\n // \u627e\u5230\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n if (cur-&gt;val == num)\n break;\n pre = cur;\n if (cur-&gt;val &lt; num) {\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 root \u7684\u53f3\u5b50\u6a39\u4e2d\n cur = cur-&gt;right;\n } else {\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 root \u7684\u5de6\u5b50\u6a39\u4e2d\n cur = cur-&gt;left;\n }\n }\n // \u82e5\u7121\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (cur == NULL)\n return;\n // \u5224\u65b7\u5f85\u522a\u9664\u7bc0\u9ede\u662f\u5426\u5b58\u5728\u5b50\u7bc0\u9ede\n if (cur-&gt;left == NULL || cur-&gt;right == NULL) {\n /* \u5b50\u7bc0\u9ede\u6578\u91cf = 0 or 1 */\n // \u7576\u5b50\u7bc0\u9ede\u6578\u91cf = 0 / 1 \u6642\uff0c child = nullptr / \u8a72\u5b50\u7bc0\u9ede\n TreeNode *child = cur-&gt;left != NULL ? cur-&gt;left : cur-&gt;right;\n // \u522a\u9664\u7bc0\u9ede cur\n if (pre-&gt;left == cur) {\n pre-&gt;left = child;\n } else {\n pre-&gt;right = child;\n }\n // \u91cb\u653e\u8a18\u61b6\u9ad4\n free(cur);\n } else {\n /* \u5b50\u7bc0\u9ede\u6578\u91cf = 2 */\n // \u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u4e2d cur \u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\n TreeNode *tmp = cur-&gt;right;\n while (tmp-&gt;left != NULL) {\n tmp = tmp-&gt;left;\n }\n int tmpVal = tmp-&gt;val;\n // \u905e\u8ff4\u522a\u9664\u7bc0\u9ede tmp\n removeItem(bst, tmp-&gt;val);\n // \u7528 tmp \u8986\u84cb cur\n cur-&gt;val = tmpVal;\n }\n}\n</code></pre> binary_search_tree.kt<pre><code>/* \u522a\u9664\u7bc0\u9ede */\nfun remove(num: Int) {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == null)\n return\n var cur = root\n var pre: TreeNode? = null\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n if (cur._val == num)\n break\n pre = cur\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n cur = if (cur._val &lt; num)\n cur.right\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else\n cur.left\n }\n // \u82e5\u7121\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (cur == null)\n return\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u7576\u5b50\u7bc0\u9ede\u6578\u91cf = 0 / 1 \u6642\uff0c child = null / \u8a72\u5b50\u7bc0\u9ede\n val child = if (cur.left != null)\n cur.left\n else\n cur.right\n // \u522a\u9664\u7bc0\u9ede cur\n if (cur != root) {\n if (pre!!.left == cur)\n pre.left = child\n else\n pre.right = child\n } else {\n // \u82e5\u522a\u9664\u7bc0\u9ede\u70ba\u6839\u7bc0\u9ede\uff0c\u5247\u91cd\u65b0\u6307\u5b9a\u6839\u7bc0\u9ede\n root = child\n }\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2\n } else {\n // \u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u4e2d cur \u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\n var tmp = cur.right\n while (tmp!!.left != null) {\n tmp = tmp.left\n }\n // \u905e\u8ff4\u522a\u9664\u7bc0\u9ede tmp\n remove(tmp._val)\n // \u7528 tmp \u8986\u84cb cur\n cur._val = tmp._val\n }\n}\n</code></pre> binary_search_tree.rb<pre><code>### \u522a\u9664\u7bc0\u9ede ###\ndef remove(num)\n # \u82e5\u6a39\u70ba\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n return if @root.nil?\n\n # \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n cur, pre = @root, nil\n while !cur.nil?\n # \u627e\u5230\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n break if cur.val == num\n\n pre = cur\n # \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if cur.val &lt; num\n cur = cur.right\n # \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n else\n cur = cur.left\n end\n end\n # \u82e5\u7121\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n return if cur.nil?\n\n # \u5b50\u7bc0\u9ede\u6578\u91cf = 0 or 1\n if cur.left.nil? || cur.right.nil?\n # \u7576\u5b50\u7bc0\u9ede\u6578\u91cf = 0 / 1 \u6642\uff0c child = null / \u8a72\u5b50\u7bc0\u9ede\n child = cur.left || cur.right\n # \u522a\u9664\u7bc0\u9ede cur\n if cur != @root\n if pre.left == cur\n pre.left = child\n else\n pre.right = child\n end\n else\n # \u82e5\u522a\u9664\u7bc0\u9ede\u70ba\u6839\u7bc0\u9ede\uff0c\u5247\u91cd\u65b0\u6307\u5b9a\u6839\u7bc0\u9ede\n @root = child\n end\n # \u5b50\u7bc0\u9ede\u6578\u91cf = 2\n else\n # \u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u4e2d cur \u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\n tmp = cur.right\n while !tmp.left.nil?\n tmp = tmp.left\n end\n # \u905e\u8ff4\u522a\u9664\u7bc0\u9ede tmp\n remove(tmp.val)\n # \u7528 tmp \u8986\u84cb cur\n cur.val = tmp.val\n end\nend\n</code></pre> binary_search_tree.zig<pre><code>// \u522a\u9664\u7bc0\u9ede\nfn remove(self: *Self, num: T) void {\n // \u82e5\u6a39\u70ba\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (self.root == null) return;\n var cur = self.root;\n var pre: ?*inc.TreeNode(T) = null;\n // \u8ff4\u5708\u67e5\u8a62\uff0c\u8d8a\u904e\u8449\u7bc0\u9ede\u5f8c\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u8df3\u51fa\u8ff4\u5708\n if (cur.?.val == num) break;\n pre = cur;\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u53f3\u5b50\u6a39\u4e2d\n if (cur.?.val &lt; num) {\n cur = cur.?.right;\n // \u5f85\u522a\u9664\u7bc0\u9ede\u5728 cur \u7684\u5de6\u5b50\u6a39\u4e2d\n } else {\n cur = cur.?.left;\n }\n }\n // \u82e5\u7121\u5f85\u522a\u9664\u7bc0\u9ede\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\n if (cur == null) return;\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 0 or 1\n if (cur.?.left == null or cur.?.right == null) {\n // \u7576\u5b50\u7bc0\u9ede\u6578\u91cf = 0 / 1 \u6642\uff0c child = null / \u8a72\u5b50\u7bc0\u9ede\n var child = if (cur.?.left != null) cur.?.left else cur.?.right;\n // \u522a\u9664\u7bc0\u9ede cur\n if (pre.?.left == cur) {\n pre.?.left = child;\n } else {\n pre.?.right = child;\n }\n // \u5b50\u7bc0\u9ede\u6578\u91cf = 2\n } else {\n // \u7372\u53d6\u4e2d\u5e8f\u8d70\u8a2a\u4e2d cur \u7684\u4e0b\u4e00\u500b\u7bc0\u9ede\n var tmp = cur.?.right;\n while (tmp.?.left != null) {\n tmp = tmp.?.left;\n }\n var tmp_val = tmp.?.val;\n // \u905e\u8ff4\u522a\u9664\u7bc0\u9ede tmp\n self.remove(tmp.?.val);\n // \u7528 tmp \u8986\u84cb cur\n cur.?.val = tmp_val;\n }\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_tree/binary_search_tree/#4","title":"4. \u00a0 \u4e2d\u5e8f\u8d70\u8a2a\u6709\u5e8f","text":"<p>\u5982\u5716 7-22 \u6240\u793a\uff0c\u4e8c\u5143\u6a39\u7684\u4e2d\u5e8f\u8d70\u8a2a\u9075\u5faa\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u7684\u8d70\u8a2a\u9806\u5e8f\uff0c\u800c\u4e8c\u5143\u641c\u5c0b\u6a39\u6eff\u8db3\u201c\u5de6\u5b50\u7bc0\u9ede \\(&lt;\\) \u6839\u7bc0\u9ede \\(&lt;\\) \u53f3\u5b50\u7bc0\u9ede\u201d\u7684\u5927\u5c0f\u95dc\u4fc2\u3002</p> <p>\u9019\u610f\u5473\u8457\u5728\u4e8c\u5143\u641c\u5c0b\u6a39\u4e2d\u9032\u884c\u4e2d\u5e8f\u8d70\u8a2a\u6642\uff0c\u7e3d\u662f\u6703\u512a\u5148\u8d70\u8a2a\u4e0b\u4e00\u500b\u6700\u5c0f\u7bc0\u9ede\uff0c\u5f9e\u800c\u5f97\u51fa\u4e00\u500b\u91cd\u8981\u6027\u8cea\uff1a\u4e8c\u5143\u641c\u5c0b\u6a39\u7684\u4e2d\u5e8f\u8d70\u8a2a\u5e8f\u5217\u662f\u5347\u5e8f\u7684\u3002</p> <p>\u5229\u7528\u4e2d\u5e8f\u8d70\u8a2a\u5347\u5e8f\u7684\u6027\u8cea\uff0c\u6211\u5011\u5728\u4e8c\u5143\u641c\u5c0b\u6a39\u4e2d\u7372\u53d6\u6709\u5e8f\u8cc7\u6599\u50c5\u9700 \\(O(n)\\) \u6642\u9593\uff0c\u7121\u9808\u9032\u884c\u984d\u5916\u7684\u6392\u5e8f\u64cd\u4f5c\uff0c\u975e\u5e38\u9ad8\u6548\u3002</p> <p></p> <p> \u5716 7-22 \u00a0 \u4e8c\u5143\u641c\u5c0b\u6a39\u7684\u4e2d\u5e8f\u8d70\u8a2a\u5e8f\u5217 </p>"},{"location":"chapter_tree/binary_search_tree/#742","title":"7.4.2 \u00a0 \u4e8c\u5143\u641c\u5c0b\u6a39\u7684\u6548\u7387","text":"<p>\u7d66\u5b9a\u4e00\u7d44\u8cc7\u6599\uff0c\u6211\u5011\u8003\u616e\u4f7f\u7528\u9663\u5217\u6216\u4e8c\u5143\u641c\u5c0b\u6a39\u5132\u5b58\u3002\u89c0\u5bdf\u8868 7-2 \uff0c\u4e8c\u5143\u641c\u5c0b\u6a39\u7684\u5404\u9805\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u90fd\u662f\u5c0d\u6578\u968e\uff0c\u5177\u6709\u7a69\u5b9a\u4e14\u9ad8\u6548\u7684\u6548\u80fd\u3002\u53ea\u6709\u5728\u9ad8\u983b\u65b0\u589e\u3001\u4f4e\u983b\u67e5\u8a62\u522a\u9664\u8cc7\u6599\u7684\u5834\u666f\u4e0b\uff0c\u9663\u5217\u6bd4\u4e8c\u5143\u641c\u5c0b\u6a39\u7684\u6548\u7387\u66f4\u9ad8\u3002</p> <p> \u8868 7-2 \u00a0 \u9663\u5217\u8207\u641c\u5c0b\u6a39\u7684\u6548\u7387\u5c0d\u6bd4 </p> \u7121\u5e8f\u9663\u5217 \u4e8c\u5143\u641c\u5c0b\u6a39 \u67e5\u8a62\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(\\log n)\\) \u522a\u9664\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) <p>\u5728\u7406\u60f3\u60c5\u6cc1\u4e0b\uff0c\u4e8c\u5143\u641c\u5c0b\u6a39\u662f\u201c\u5e73\u8861\u201d\u7684\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u5728 \\(\\log n\\) \u8f2a\u8ff4\u5708\u5167\u67e5\u8a62\u4efb\u610f\u7bc0\u9ede\u3002</p> <p>\u7136\u800c\uff0c\u5982\u679c\u6211\u5011\u5728\u4e8c\u5143\u641c\u5c0b\u6a39\u4e2d\u4e0d\u65b7\u5730\u63d2\u5165\u548c\u522a\u9664\u7bc0\u9ede\uff0c\u53ef\u80fd\u5c0e\u81f4\u4e8c\u5143\u6a39\u9000\u5316\u70ba\u5716 7-23 \u6240\u793a\u7684\u93c8\u7d50\u4e32\u5217\uff0c\u9019\u6642\u5404\u7a2e\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u4e5f\u6703\u9000\u5316\u70ba \\(O(n)\\) \u3002</p> <p></p> <p> \u5716 7-23 \u00a0 \u4e8c\u5143\u641c\u5c0b\u6a39\u9000\u5316 </p>"},{"location":"chapter_tree/binary_search_tree/#743","title":"7.4.3 \u00a0 \u4e8c\u5143\u641c\u5c0b\u6a39\u5e38\u898b\u61c9\u7528","text":"<ul> <li>\u7528\u4f5c\u7cfb\u7d71\u4e2d\u7684\u591a\u7d1a\u7d22\u5f15\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u67e5\u8a62\u3001\u63d2\u5165\u3001\u522a\u9664\u64cd\u4f5c\u3002</li> <li>\u4f5c\u70ba\u67d0\u4e9b\u641c\u5c0b\u6f14\u7b97\u6cd5\u7684\u5e95\u5c64\u8cc7\u6599\u7d50\u69cb\u3002</li> <li>\u7528\u65bc\u5132\u5b58\u8cc7\u6599\u6d41\uff0c\u4ee5\u4fdd\u6301\u5176\u6709\u5e8f\u72c0\u614b\u3002</li> </ul>"},{"location":"chapter_tree/binary_tree/","title":"7.1 \u00a0 \u4e8c\u5143\u6a39","text":"<p>\u4e8c\u5143\u6a39\uff08binary tree\uff09\u662f\u4e00\u7a2e\u975e\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\uff0c\u4ee3\u8868\u201c\u7956\u5148\u201d\u8207\u201c\u5f8c\u4ee3\u201d\u4e4b\u9593\u7684\u6d3e\u751f\u95dc\u4fc2\uff0c\u9ad4\u73fe\u4e86\u201c\u4e00\u5206\u70ba\u4e8c\u201d\u7684\u5206\u6cbb\u908f\u8f2f\u3002\u8207\u93c8\u7d50\u4e32\u5217\u985e\u4f3c\uff0c\u4e8c\u5143\u6a39\u7684\u57fa\u672c\u55ae\u5143\u662f\u7bc0\u9ede\uff0c\u6bcf\u500b\u7bc0\u9ede\u5305\u542b\u503c\u3001\u5de6\u5b50\u7bc0\u9ede\u5f15\u7528\u548c\u53f3\u5b50\u7bc0\u9ede\u5f15\u7528\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig <pre><code>class TreeNode:\n \"\"\"\u4e8c\u5143\u6a39\u7bc0\u9ede\u985e\u5225\"\"\"\n def __init__(self, val: int):\n self.val: int = val # \u7bc0\u9ede\u503c\n self.left: TreeNode | None = None # \u5de6\u5b50\u7bc0\u9ede\u5f15\u7528\n self.right: TreeNode | None = None # \u53f3\u5b50\u7bc0\u9ede\u5f15\u7528\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7bc0\u9ede\u7d50\u69cb\u9ad4 */\nstruct TreeNode {\n int val; // \u7bc0\u9ede\u503c\n TreeNode *left; // \u5de6\u5b50\u7bc0\u9ede\u6307\u6a19\n TreeNode *right; // \u53f3\u5b50\u7bc0\u9ede\u6307\u6a19\n TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7bc0\u9ede\u985e\u5225 */\nclass TreeNode {\n int val; // \u7bc0\u9ede\u503c\n TreeNode left; // \u5de6\u5b50\u7bc0\u9ede\u5f15\u7528\n TreeNode right; // \u53f3\u5b50\u7bc0\u9ede\u5f15\u7528\n TreeNode(int x) { val = x; }\n}\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7bc0\u9ede\u985e\u5225 */\nclass TreeNode(int? x) {\n public int? val = x; // \u7bc0\u9ede\u503c\n public TreeNode? left; // \u5de6\u5b50\u7bc0\u9ede\u5f15\u7528\n public TreeNode? right; // \u53f3\u5b50\u7bc0\u9ede\u5f15\u7528\n}\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7bc0\u9ede\u7d50\u69cb\u9ad4 */\ntype TreeNode struct {\n Val int\n Left *TreeNode\n Right *TreeNode\n}\n/* \u5efa\u69cb\u5b50 */\nfunc NewTreeNode(v int) *TreeNode {\n return &amp;TreeNode{\n Left: nil, // \u5de6\u5b50\u7bc0\u9ede\u6307\u6a19\n Right: nil, // \u53f3\u5b50\u7bc0\u9ede\u6307\u6a19\n Val: v, // \u7bc0\u9ede\u503c\n }\n}\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7bc0\u9ede\u985e\u5225 */\nclass TreeNode {\n var val: Int // \u7bc0\u9ede\u503c\n var left: TreeNode? // \u5de6\u5b50\u7bc0\u9ede\u5f15\u7528\n var right: TreeNode? // \u53f3\u5b50\u7bc0\u9ede\u5f15\u7528\n\n init(x: Int) {\n val = x\n }\n}\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7bc0\u9ede\u985e\u5225 */\nclass TreeNode {\n val; // \u7bc0\u9ede\u503c\n left; // \u5de6\u5b50\u7bc0\u9ede\u6307\u6a19\n right; // \u53f3\u5b50\u7bc0\u9ede\u6307\u6a19\n constructor(val, left, right) {\n this.val = val === undefined ? 0 : val;\n this.left = left === undefined ? null : left;\n this.right = right === undefined ? null : right;\n }\n}\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7bc0\u9ede\u985e\u5225 */\nclass TreeNode {\n val: number;\n left: TreeNode | null;\n right: TreeNode | null;\n\n constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n this.val = val === undefined ? 0 : val; // \u7bc0\u9ede\u503c\n this.left = left === undefined ? null : left; // \u5de6\u5b50\u7bc0\u9ede\u5f15\u7528\n this.right = right === undefined ? null : right; // \u53f3\u5b50\u7bc0\u9ede\u5f15\u7528\n }\n}\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7bc0\u9ede\u985e\u5225 */\nclass TreeNode {\n int val; // \u7bc0\u9ede\u503c\n TreeNode? left; // \u5de6\u5b50\u7bc0\u9ede\u5f15\u7528\n TreeNode? right; // \u53f3\u5b50\u7bc0\u9ede\u5f15\u7528\n TreeNode(this.val, [this.left, this.right]);\n}\n</code></pre> <pre><code>use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u4e8c\u5143\u6a39\u7bc0\u9ede\u7d50\u69cb\u9ad4 */\nstruct TreeNode {\n val: i32, // \u7bc0\u9ede\u503c\n left: Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, // \u5de6\u5b50\u7bc0\u9ede\u5f15\u7528\n right: Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, // \u53f3\u5b50\u7bc0\u9ede\u5f15\u7528\n}\n\nimpl TreeNode {\n /* \u5efa\u69cb\u5b50 */\n fn new(val: i32) -&gt; Rc&lt;RefCell&lt;Self&gt;&gt; {\n Rc::new(RefCell::new(Self {\n val,\n left: None,\n right: None\n }))\n }\n}\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7bc0\u9ede\u7d50\u69cb\u9ad4 */\ntypedef struct TreeNode {\n int val; // \u7bc0\u9ede\u503c\n int height; // \u7bc0\u9ede\u9ad8\u5ea6\n struct TreeNode *left; // \u5de6\u5b50\u7bc0\u9ede\u6307\u6a19\n struct TreeNode *right; // \u53f3\u5b50\u7bc0\u9ede\u6307\u6a19\n} TreeNode;\n\n/* \u5efa\u69cb\u5b50 */\nTreeNode *newTreeNode(int val) {\n TreeNode *node;\n\n node = (TreeNode *)malloc(sizeof(TreeNode));\n node-&gt;val = val;\n node-&gt;height = 0;\n node-&gt;left = NULL;\n node-&gt;right = NULL;\n return node;\n}\n</code></pre> <pre><code>/* \u4e8c\u5143\u6a39\u7bc0\u9ede\u985e\u5225 */\nclass TreeNode(val _val: Int) { // \u7bc0\u9ede\u503c\n val left: TreeNode? = null // \u5de6\u5b50\u7bc0\u9ede\u5f15\u7528\n val right: TreeNode? = null // \u53f3\u5b50\u7bc0\u9ede\u5f15\u7528\n}\n</code></pre> <pre><code>### \u4e8c\u5143\u6a39\u7bc0\u9ede\u985e\u5225 ###\nclass TreeNode\n attr_accessor :val # \u7bc0\u9ede\u503c\n attr_accessor :left # \u5de6\u5b50\u7bc0\u9ede\u5f15\u7528\n attr_accessor :right # \u53f3\u5b50\u7bc0\u9ede\u5f15\u7528\n\n def initialize(val)\n @val = val\n end\nend\n</code></pre> <pre><code>\n</code></pre> <p>\u6bcf\u500b\u7bc0\u9ede\u90fd\u6709\u5169\u500b\u5f15\u7528\uff08\u6307\u6a19\uff09\uff0c\u5206\u5225\u6307\u5411\u5de6\u5b50\u7bc0\u9ede\uff08left-child node\uff09\u548c\u53f3\u5b50\u7bc0\u9ede\uff08right-child node\uff09\uff0c\u8a72\u7bc0\u9ede\u88ab\u7a31\u70ba\u9019\u5169\u500b\u5b50\u7bc0\u9ede\u7684\u7236\u7bc0\u9ede\uff08parent node\uff09\u3002\u7576\u7d66\u5b9a\u4e00\u500b\u4e8c\u5143\u6a39\u7684\u7bc0\u9ede\u6642\uff0c\u6211\u5011\u5c07\u8a72\u7bc0\u9ede\u7684\u5de6\u5b50\u7bc0\u9ede\u53ca\u5176\u4ee5\u4e0b\u7bc0\u9ede\u5f62\u6210\u7684\u6a39\u7a31\u70ba\u8a72\u7bc0\u9ede\u7684\u5de6\u5b50\u6a39\uff08left subtree\uff09\uff0c\u540c\u7406\u53ef\u5f97\u53f3\u5b50\u6a39\uff08right subtree\uff09\u3002</p> <p>\u5728\u4e8c\u5143\u6a39\u4e2d\uff0c\u9664\u8449\u7bc0\u9ede\u5916\uff0c\u5176\u4ed6\u6240\u6709\u7bc0\u9ede\u90fd\u5305\u542b\u5b50\u7bc0\u9ede\u548c\u975e\u7a7a\u5b50\u6a39\u3002\u5982\u5716 7-1 \u6240\u793a\uff0c\u5982\u679c\u5c07\u201c\u7bc0\u9ede 2\u201d\u8996\u70ba\u7236\u7bc0\u9ede\uff0c\u5247\u5176\u5de6\u5b50\u7bc0\u9ede\u548c\u53f3\u5b50\u7bc0\u9ede\u5206\u5225\u662f\u201c\u7bc0\u9ede 4\u201d\u548c\u201c\u7bc0\u9ede 5\u201d\uff0c\u5de6\u5b50\u6a39\u662f\u201c\u7bc0\u9ede 4 \u53ca\u5176\u4ee5\u4e0b\u7bc0\u9ede\u5f62\u6210\u7684\u6a39\u201d\uff0c\u53f3\u5b50\u6a39\u662f\u201c\u7bc0\u9ede 5 \u53ca\u5176\u4ee5\u4e0b\u7bc0\u9ede\u5f62\u6210\u7684\u6a39\u201d\u3002</p> <p></p> <p> \u5716 7-1 \u00a0 \u7236\u7bc0\u9ede\u3001\u5b50\u7bc0\u9ede\u3001\u5b50\u6a39 </p>"},{"location":"chapter_tree/binary_tree/#711","title":"7.1.1 \u00a0 \u4e8c\u5143\u6a39\u5e38\u898b\u8853\u8a9e","text":"<p>\u4e8c\u5143\u6a39\u7684\u5e38\u7528\u8853\u8a9e\u5982\u5716 7-2 \u6240\u793a\u3002</p> <ul> <li>\u6839\u7bc0\u9ede\uff08root node\uff09\uff1a\u4f4d\u65bc\u4e8c\u5143\u6a39\u9802\u5c64\u7684\u7bc0\u9ede\uff0c\u6c92\u6709\u7236\u7bc0\u9ede\u3002</li> <li>\u8449\u7bc0\u9ede\uff08leaf node\uff09\uff1a\u6c92\u6709\u5b50\u7bc0\u9ede\u7684\u7bc0\u9ede\uff0c\u5176\u5169\u500b\u6307\u6a19\u5747\u6307\u5411 <code>None</code> \u3002</li> <li>\u908a\uff08edge\uff09\uff1a\u9023\u7dda\u5169\u500b\u7bc0\u9ede\u7684\u7dda\u6bb5\uff0c\u5373\u7bc0\u9ede\u5f15\u7528\uff08\u6307\u6a19\uff09\u3002</li> <li>\u7bc0\u9ede\u6240\u5728\u7684\u5c64\uff08level\uff09\uff1a\u5f9e\u9802\u81f3\u5e95\u905e\u589e\uff0c\u6839\u7bc0\u9ede\u6240\u5728\u5c64\u70ba 1 \u3002</li> <li>\u7bc0\u9ede\u7684\u5ea6\uff08degree\uff09\uff1a\u7bc0\u9ede\u7684\u5b50\u7bc0\u9ede\u7684\u6578\u91cf\u3002\u5728\u4e8c\u5143\u6a39\u4e2d\uff0c\u5ea6\u7684\u53d6\u503c\u7bc4\u570d\u662f 0\u30011\u30012 \u3002</li> <li>\u4e8c\u5143\u6a39\u7684\u9ad8\u5ea6\uff08height\uff09\uff1a\u5f9e\u6839\u7bc0\u9ede\u5230\u6700\u9060\u8449\u7bc0\u9ede\u6240\u7d93\u904e\u7684\u908a\u7684\u6578\u91cf\u3002</li> <li>\u7bc0\u9ede\u7684\u6df1\u5ea6\uff08depth\uff09\uff1a\u5f9e\u6839\u7bc0\u9ede\u5230\u8a72\u7bc0\u9ede\u6240\u7d93\u904e\u7684\u908a\u7684\u6578\u91cf\u3002</li> <li>\u7bc0\u9ede\u7684\u9ad8\u5ea6\uff08height\uff09\uff1a\u5f9e\u8ddd\u96e2\u8a72\u7bc0\u9ede\u6700\u9060\u7684\u8449\u7bc0\u9ede\u5230\u8a72\u7bc0\u9ede\u6240\u7d93\u904e\u7684\u908a\u7684\u6578\u91cf\u3002</li> </ul> <p></p> <p> \u5716 7-2 \u00a0 \u4e8c\u5143\u6a39\u7684\u5e38\u7528\u8853\u8a9e </p> <p>Tip</p> <p>\u8acb\u6ce8\u610f\uff0c\u6211\u5011\u901a\u5e38\u5c07\u201c\u9ad8\u5ea6\u201d\u548c\u201c\u6df1\u5ea6\u201d\u5b9a\u7fa9\u70ba\u201c\u7d93\u904e\u7684\u908a\u7684\u6578\u91cf\u201d\uff0c\u4f46\u6709\u4e9b\u984c\u76ee\u6216\u6559\u6750\u53ef\u80fd\u6703\u5c07\u5176\u5b9a\u7fa9\u70ba\u201c\u7d93\u904e\u7684\u7bc0\u9ede\u7684\u6578\u91cf\u201d\u3002\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u9ad8\u5ea6\u548c\u6df1\u5ea6\u90fd\u9700\u8981\u52a0 1 \u3002</p>"},{"location":"chapter_tree/binary_tree/#712","title":"7.1.2 \u00a0 \u4e8c\u5143\u6a39\u57fa\u672c\u64cd\u4f5c","text":""},{"location":"chapter_tree/binary_tree/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u4e8c\u5143\u6a39","text":"<p>\u8207\u93c8\u7d50\u4e32\u5217\u985e\u4f3c\uff0c\u9996\u5148\u521d\u59cb\u5316\u7bc0\u9ede\uff0c\u7136\u5f8c\u69cb\u5efa\u5f15\u7528\uff08\u6307\u6a19\uff09\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_tree.py<pre><code># \u521d\u59cb\u5316\u4e8c\u5143\u6a39\n# \u521d\u59cb\u5316\u7bc0\u9ede\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n</code></pre> binary_tree.cpp<pre><code>/* \u521d\u59cb\u5316\u4e8c\u5143\u6a39 */\n// \u521d\u59cb\u5316\u7bc0\u9ede\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// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\nn1-&gt;left = n2;\nn1-&gt;right = n3;\nn2-&gt;left = n4;\nn2-&gt;right = n5;\n</code></pre> binary_tree.java<pre><code>// \u521d\u59cb\u5316\u7bc0\u9ede\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// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n</code></pre> binary_tree.cs<pre><code>/* \u521d\u59cb\u5316\u4e8c\u5143\u6a39 */\n// \u521d\u59cb\u5316\u7bc0\u9ede\nTreeNode n1 = new(1);\nTreeNode n2 = new(2);\nTreeNode n3 = new(3);\nTreeNode n4 = new(4);\nTreeNode n5 = new(5);\n// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n</code></pre> binary_tree.go<pre><code>/* \u521d\u59cb\u5316\u4e8c\u5143\u6a39 */\n// \u521d\u59cb\u5316\u7bc0\u9ede\nn1 := NewTreeNode(1)\nn2 := NewTreeNode(2)\nn3 := NewTreeNode(3)\nn4 := NewTreeNode(4)\nn5 := NewTreeNode(5)\n// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\nn1.Left = n2\nn1.Right = n3\nn2.Left = n4\nn2.Right = n5\n</code></pre> binary_tree.swift<pre><code>// \u521d\u59cb\u5316\u7bc0\u9ede\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// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n</code></pre> binary_tree.js<pre><code>/* \u521d\u59cb\u5316\u4e8c\u5143\u6a39 */\n// \u521d\u59cb\u5316\u7bc0\u9ede\nlet n1 = new TreeNode(1),\n n2 = new TreeNode(2),\n n3 = new TreeNode(3),\n n4 = new TreeNode(4),\n n5 = new TreeNode(5);\n// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n</code></pre> binary_tree.ts<pre><code>/* \u521d\u59cb\u5316\u4e8c\u5143\u6a39 */\n// \u521d\u59cb\u5316\u7bc0\u9ede\nlet n1 = new TreeNode(1),\n n2 = new TreeNode(2),\n n3 = new TreeNode(3),\n n4 = new TreeNode(4),\n n5 = new TreeNode(5);\n// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n</code></pre> binary_tree.dart<pre><code>/* \u521d\u59cb\u5316\u4e8c\u5143\u6a39 */\n// \u521d\u59cb\u5316\u7bc0\u9ede\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// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n</code></pre> binary_tree.rs<pre><code>// \u521d\u59cb\u5316\u7bc0\u9ede\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// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\uff08\u6307\u6a19\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</code></pre> binary_tree.c<pre><code>/* \u521d\u59cb\u5316\u4e8c\u5143\u6a39 */\n// \u521d\u59cb\u5316\u7bc0\u9ede\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\nn1-&gt;left = n2;\nn1-&gt;right = n3;\nn2-&gt;left = n4;\nn2-&gt;right = n5;\n</code></pre> binary_tree.kt<pre><code>// \u521d\u59cb\u5316\u7bc0\u9ede\nval n1 = TreeNode(1)\nval n2 = TreeNode(2)\nval n3 = TreeNode(3)\nval n4 = TreeNode(4)\nval n5 = TreeNode(5)\n// \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n</code></pre> binary_tree.rb<pre><code># \u521d\u59cb\u5316\u4e8c\u5143\u6a39\n# \u521d\u59cb\u5316\u7bc0\u9ede\nn1 = TreeNode.new(1)\nn2 = TreeNode.new(2)\nn3 = TreeNode.new(3)\nn4 = TreeNode.new(4)\nn5 = TreeNode.new(5)\n# \u69cb\u5efa\u7bc0\u9ede\u4e4b\u9593\u7684\u5f15\u7528\uff08\u6307\u6a19\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n</code></pre> binary_tree.zig<pre><code>\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_tree/binary_tree/#2","title":"2. \u00a0 \u63d2\u5165\u8207\u522a\u9664\u7bc0\u9ede","text":"<p>\u8207\u93c8\u7d50\u4e32\u5217\u985e\u4f3c\uff0c\u5728\u4e8c\u5143\u6a39\u4e2d\u63d2\u5165\u8207\u522a\u9664\u7bc0\u9ede\u53ef\u4ee5\u900f\u904e\u4fee\u6539\u6307\u6a19\u4f86\u5be6\u73fe\u3002\u5716 7-3 \u7d66\u51fa\u4e86\u4e00\u500b\u793a\u4f8b\u3002</p> <p></p> <p> \u5716 7-3 \u00a0 \u5728\u4e8c\u5143\u6a39\u4e2d\u63d2\u5165\u8207\u522a\u9664\u7bc0\u9ede </p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_tree.py<pre><code># \u63d2\u5165\u8207\u522a\u9664\u7bc0\u9ede\np = TreeNode(0)\n# \u5728 n1 -&gt; n2 \u4e2d\u9593\u63d2\u5165\u7bc0\u9ede P\nn1.left = p\np.left = n2\n# \u522a\u9664\u7bc0\u9ede P\nn1.left = n2\n</code></pre> binary_tree.cpp<pre><code>/* \u63d2\u5165\u8207\u522a\u9664\u7bc0\u9ede */\nTreeNode* P = new TreeNode(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u9593\u63d2\u5165\u7bc0\u9ede P\nn1-&gt;left = P;\nP-&gt;left = n2;\n// \u522a\u9664\u7bc0\u9ede P\nn1-&gt;left = n2;\n</code></pre> binary_tree.java<pre><code>TreeNode P = new TreeNode(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u9593\u63d2\u5165\u7bc0\u9ede P\nn1.left = P;\nP.left = n2;\n// \u522a\u9664\u7bc0\u9ede P\nn1.left = n2;\n</code></pre> binary_tree.cs<pre><code>/* \u63d2\u5165\u8207\u522a\u9664\u7bc0\u9ede */\nTreeNode P = new(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u9593\u63d2\u5165\u7bc0\u9ede P\nn1.left = P;\nP.left = n2;\n// \u522a\u9664\u7bc0\u9ede P\nn1.left = n2;\n</code></pre> binary_tree.go<pre><code>/* \u63d2\u5165\u8207\u522a\u9664\u7bc0\u9ede */\n// \u5728 n1 -&gt; n2 \u4e2d\u9593\u63d2\u5165\u7bc0\u9ede P\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// \u522a\u9664\u7bc0\u9ede P\nn1.Left = n2\n</code></pre> binary_tree.swift<pre><code>let P = TreeNode(x: 0)\n// \u5728 n1 -&gt; n2 \u4e2d\u9593\u63d2\u5165\u7bc0\u9ede P\nn1.left = P\nP.left = n2\n// \u522a\u9664\u7bc0\u9ede P\nn1.left = n2\n</code></pre> binary_tree.js<pre><code>/* \u63d2\u5165\u8207\u522a\u9664\u7bc0\u9ede */\nlet P = new TreeNode(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u9593\u63d2\u5165\u7bc0\u9ede P\nn1.left = P;\nP.left = n2;\n// \u522a\u9664\u7bc0\u9ede P\nn1.left = n2;\n</code></pre> binary_tree.ts<pre><code>/* \u63d2\u5165\u8207\u522a\u9664\u7bc0\u9ede */\nconst P = new TreeNode(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u9593\u63d2\u5165\u7bc0\u9ede P\nn1.left = P;\nP.left = n2;\n// \u522a\u9664\u7bc0\u9ede P\nn1.left = n2;\n</code></pre> binary_tree.dart<pre><code>/* \u63d2\u5165\u8207\u522a\u9664\u7bc0\u9ede */\nTreeNode P = new TreeNode(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u9593\u63d2\u5165\u7bc0\u9ede P\nn1.left = P;\nP.left = n2;\n// \u522a\u9664\u7bc0\u9ede P\nn1.left = n2;\n</code></pre> binary_tree.rs<pre><code>let p = TreeNode::new(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u9593\u63d2\u5165\u7bc0\u9ede P\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// \u522a\u9664\u7bc0\u9ede p\nn1.borrow_mut().left = Some(n2);\n</code></pre> binary_tree.c<pre><code>/* \u63d2\u5165\u8207\u522a\u9664\u7bc0\u9ede */\nTreeNode *P = newTreeNode(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u9593\u63d2\u5165\u7bc0\u9ede P\nn1-&gt;left = P;\nP-&gt;left = n2;\n// \u522a\u9664\u7bc0\u9ede P\nn1-&gt;left = n2;\n</code></pre> binary_tree.kt<pre><code>val P = TreeNode(0)\n// \u5728 n1 -&gt; n2 \u4e2d\u9593\u63d2\u5165\u7bc0\u9ede P\nn1.left = P\nP.left = n2\n// \u522a\u9664\u7bc0\u9ede P\nn1.left = n2\n</code></pre> binary_tree.rb<pre><code># \u63d2\u5165\u8207\u522a\u9664\u7bc0\u9ede\n_p = TreeNode.new(0)\n# \u5728 n1 -&gt; n2 \u4e2d\u9593\u63d2\u5165\u7bc0\u9ede _p\nn1.left = _p\n_p.left = n2\n# \u522a\u9664\u7bc0\u9ede\nn1.left = n2\n</code></pre> binary_tree.zig<pre><code>\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>Tip</p> <p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u63d2\u5165\u7bc0\u9ede\u53ef\u80fd\u6703\u6539\u8b8a\u4e8c\u5143\u6a39\u7684\u539f\u6709\u908f\u8f2f\u7d50\u69cb\uff0c\u800c\u522a\u9664\u7bc0\u9ede\u901a\u5e38\u610f\u5473\u8457\u522a\u9664\u8a72\u7bc0\u9ede\u53ca\u5176\u6240\u6709\u5b50\u6a39\u3002\u56e0\u6b64\uff0c\u5728\u4e8c\u5143\u6a39\u4e2d\uff0c\u63d2\u5165\u8207\u522a\u9664\u901a\u5e38\u662f\u7531\u4e00\u5957\u64cd\u4f5c\u914d\u5408\u5b8c\u6210\u7684\uff0c\u4ee5\u5be6\u73fe\u6709\u5be6\u969b\u610f\u7fa9\u7684\u64cd\u4f5c\u3002</p>"},{"location":"chapter_tree/binary_tree/#713","title":"7.1.3 \u00a0 \u5e38\u898b\u4e8c\u5143\u6a39\u578b\u5225","text":""},{"location":"chapter_tree/binary_tree/#1_1","title":"1. \u00a0 \u5b8c\u7f8e\u4e8c\u5143\u6a39","text":"<p>\u5982\u5716 7-4 \u6240\u793a\uff0c\u5b8c\u7f8e\u4e8c\u5143\u6a39\uff08perfect binary tree\uff09\u6240\u6709\u5c64\u7684\u7bc0\u9ede\u90fd\u88ab\u5b8c\u5168\u586b\u6eff\u3002\u5728\u5b8c\u7f8e\u4e8c\u5143\u6a39\u4e2d\uff0c\u8449\u7bc0\u9ede\u7684\u5ea6\u70ba \\(0\\) \uff0c\u5176\u9918\u6240\u6709\u7bc0\u9ede\u7684\u5ea6\u90fd\u70ba \\(2\\) \uff1b\u82e5\u6a39\u7684\u9ad8\u5ea6\u70ba \\(h\\) \uff0c\u5247\u7bc0\u9ede\u7e3d\u6578\u70ba \\(2^{h+1} - 1\\) \uff0c\u5448\u73fe\u6a19\u6e96\u7684\u6307\u6578\u7d1a\u95dc\u4fc2\uff0c\u53cd\u6620\u4e86\u81ea\u7136\u754c\u4e2d\u5e38\u898b\u7684\u7d30\u80de\u5206\u88c2\u73fe\u8c61\u3002</p> <p>Tip</p> <p>\u8acb\u6ce8\u610f\uff0c\u5728\u4e2d\u6587\u793e\u7fa4\u4e2d\uff0c\u5b8c\u7f8e\u4e8c\u5143\u6a39\u5e38\u88ab\u7a31\u70ba\u6eff\u4e8c\u5143\u6a39\u3002</p> <p></p> <p> \u5716 7-4 \u00a0 \u5b8c\u7f8e\u4e8c\u5143\u6a39 </p>"},{"location":"chapter_tree/binary_tree/#2_1","title":"2. \u00a0 \u5b8c\u5168\u4e8c\u5143\u6a39","text":"<p>\u5982\u5716 7-5 \u6240\u793a\uff0c\u5b8c\u5168\u4e8c\u5143\u6a39\uff08complete binary tree\uff09\u53ea\u6709\u6700\u5e95\u5c64\u7684\u7bc0\u9ede\u672a\u88ab\u586b\u6eff\uff0c\u4e14\u6700\u5e95\u5c64\u7bc0\u9ede\u5118\u91cf\u9760\u5de6\u586b\u5145\u3002</p> <p></p> <p> \u5716 7-5 \u00a0 \u5b8c\u5168\u4e8c\u5143\u6a39 </p>"},{"location":"chapter_tree/binary_tree/#3","title":"3. \u00a0 \u5b8c\u6eff\u4e8c\u5143\u6a39","text":"<p>\u5982\u5716 7-6 \u6240\u793a\uff0c\u5b8c\u6eff\u4e8c\u5143\u6a39\uff08full binary tree\uff09\u9664\u4e86\u8449\u7bc0\u9ede\u4e4b\u5916\uff0c\u5176\u9918\u6240\u6709\u7bc0\u9ede\u90fd\u6709\u5169\u500b\u5b50\u7bc0\u9ede\u3002</p> <p></p> <p> \u5716 7-6 \u00a0 \u5b8c\u6eff\u4e8c\u5143\u6a39 </p>"},{"location":"chapter_tree/binary_tree/#4","title":"4. \u00a0 \u5e73\u8861\u4e8c\u5143\u6a39","text":"<p>\u5982\u5716 7-7 \u6240\u793a\uff0c\u5e73\u8861\u4e8c\u5143\u6a39\uff08balanced binary tree\uff09\u4e2d\u4efb\u610f\u7bc0\u9ede\u7684\u5de6\u5b50\u6a39\u548c\u53f3\u5b50\u6a39\u7684\u9ad8\u5ea6\u4e4b\u5dee\u7684\u7d55\u5c0d\u503c\u4e0d\u8d85\u904e 1 \u3002</p> <p></p> <p> \u5716 7-7 \u00a0 \u5e73\u8861\u4e8c\u5143\u6a39 </p>"},{"location":"chapter_tree/binary_tree/#714","title":"7.1.4 \u00a0 \u4e8c\u5143\u6a39\u7684\u9000\u5316","text":"<p>\u5716 7-8 \u5c55\u793a\u4e86\u4e8c\u5143\u6a39\u7684\u7406\u60f3\u7d50\u69cb\u8207\u9000\u5316\u7d50\u69cb\u3002\u7576\u4e8c\u5143\u6a39\u7684\u6bcf\u5c64\u7bc0\u9ede\u90fd\u88ab\u586b\u6eff\u6642\uff0c\u9054\u5230\u201c\u5b8c\u7f8e\u4e8c\u5143\u6a39\u201d\uff1b\u800c\u7576\u6240\u6709\u7bc0\u9ede\u90fd\u504f\u5411\u4e00\u5074\u6642\uff0c\u4e8c\u5143\u6a39\u9000\u5316\u70ba\u201c\u93c8\u7d50\u4e32\u5217\u201d\u3002</p> <ul> <li>\u5b8c\u7f8e\u4e8c\u5143\u6a39\u662f\u7406\u60f3\u60c5\u6cc1\uff0c\u53ef\u4ee5\u5145\u5206\u767c\u63ee\u4e8c\u5143\u6a39\u201c\u5206\u6cbb\u201d\u7684\u512a\u52e2\u3002</li> <li>\u93c8\u7d50\u4e32\u5217\u5247\u662f\u53e6\u4e00\u500b\u6975\u7aef\uff0c\u5404\u9805\u64cd\u4f5c\u90fd\u8b8a\u70ba\u7dda\u6027\u64cd\u4f5c\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \u3002</li> </ul> <p></p> <p> \u5716 7-8 \u00a0 \u4e8c\u5143\u6a39\u7684\u6700\u4f73\u7d50\u69cb\u8207\u6700\u5dee\u7d50\u69cb </p> <p>\u5982\u8868 7-1 \u6240\u793a\uff0c\u5728\u6700\u4f73\u7d50\u69cb\u548c\u6700\u5dee\u7d50\u69cb\u4e0b\uff0c\u4e8c\u5143\u6a39\u7684\u8449\u7bc0\u9ede\u6578\u91cf\u3001\u7bc0\u9ede\u7e3d\u6578\u3001\u9ad8\u5ea6\u7b49\u9054\u5230\u6975\u5927\u503c\u6216\u6975\u5c0f\u503c\u3002</p> <p> \u8868 7-1 \u00a0 \u4e8c\u5143\u6a39\u7684\u6700\u4f73\u7d50\u69cb\u8207\u6700\u5dee\u7d50\u69cb </p> \u5b8c\u7f8e\u4e8c\u5143\u6a39 \u93c8\u7d50\u4e32\u5217 \u7b2c \\(i\\) \u5c64\u7684\u7bc0\u9ede\u6578\u91cf \\(2^{i-1}\\) \\(1\\) \u9ad8\u5ea6\u70ba \\(h\\) \u7684\u6a39\u7684\u8449\u7bc0\u9ede\u6578\u91cf \\(2^h\\) \\(1\\) \u9ad8\u5ea6\u70ba \\(h\\) \u7684\u6a39\u7684\u7bc0\u9ede\u7e3d\u6578 \\(2^{h+1} - 1\\) \\(h + 1\\) \u7bc0\u9ede\u7e3d\u6578\u70ba \\(n\\) \u7684\u6a39\u7684\u9ad8\u5ea6 \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2 \u00a0 \u4e8c\u5143\u6a39\u8d70\u8a2a","text":"<p>\u5f9e\u7269\u7406\u7d50\u69cb\u7684\u89d2\u5ea6\u4f86\u770b\uff0c\u6a39\u662f\u4e00\u7a2e\u57fa\u65bc\u93c8\u7d50\u4e32\u5217\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u56e0\u6b64\u5176\u8d70\u8a2a\u65b9\u5f0f\u662f\u900f\u904e\u6307\u6a19\u9010\u500b\u8a2a\u554f\u7bc0\u9ede\u3002\u7136\u800c\uff0c\u6a39\u662f\u4e00\u7a2e\u975e\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\uff0c\u9019\u4f7f\u5f97\u8d70\u8a2a\u6a39\u6bd4\u8d70\u8a2a\u93c8\u7d50\u4e32\u5217\u66f4\u52a0\u8907\u96dc\uff0c\u9700\u8981\u85c9\u52a9\u641c\u5c0b\u6f14\u7b97\u6cd5\u4f86\u5be6\u73fe\u3002</p> <p>\u4e8c\u5143\u6a39\u5e38\u898b\u7684\u8d70\u8a2a\u65b9\u5f0f\u5305\u62ec\u5c64\u5e8f\u8d70\u8a2a\u3001\u524d\u5e8f\u8d70\u8a2a\u3001\u4e2d\u5e8f\u8d70\u8a2a\u548c\u5f8c\u5e8f\u8d70\u8a2a\u7b49\u3002</p>"},{"location":"chapter_tree/binary_tree_traversal/#721","title":"7.2.1 \u00a0 \u5c64\u5e8f\u8d70\u8a2a","text":"<p>\u5982\u5716 7-9 \u6240\u793a\uff0c\u5c64\u5e8f\u8d70\u8a2a\uff08level-order traversal\uff09\u5f9e\u9802\u90e8\u5230\u5e95\u90e8\u9010\u5c64\u8d70\u8a2a\u4e8c\u5143\u6a39\uff0c\u4e26\u5728\u6bcf\u4e00\u5c64\u6309\u7167\u5f9e\u5de6\u5230\u53f3\u7684\u9806\u5e8f\u8a2a\u554f\u7bc0\u9ede\u3002</p> <p>\u5c64\u5e8f\u8d70\u8a2a\u672c\u8cea\u4e0a\u5c6c\u65bc\u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a\uff08breadth-first traversal\uff09\uff0c\u4e5f\u7a31\u5ee3\u5ea6\u512a\u5148\u641c\u5c0b\uff08breadth-first search, BFS\uff09\uff0c\u5b83\u9ad4\u73fe\u4e86\u4e00\u7a2e\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u64f4\u5c55\u201d\u7684\u9010\u5c64\u8d70\u8a2a\u65b9\u5f0f\u3002</p> <p></p> <p> \u5716 7-9 \u00a0 \u4e8c\u5143\u6a39\u7684\u5c64\u5e8f\u8d70\u8a2a </p>"},{"location":"chapter_tree/binary_tree_traversal/#1","title":"1. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"<p>\u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a\u901a\u5e38\u85c9\u52a9\u201c\u4f47\u5217\u201d\u4f86\u5be6\u73fe\u3002\u4f47\u5217\u9075\u5faa\u201c\u5148\u9032\u5148\u51fa\u201d\u7684\u898f\u5247\uff0c\u800c\u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a\u5247\u9075\u5faa\u201c\u9010\u5c64\u63a8\u9032\u201d\u7684\u898f\u5247\uff0c\u5169\u8005\u80cc\u5f8c\u7684\u601d\u60f3\u662f\u4e00\u81f4\u7684\u3002\u5be6\u73fe\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_tree_bfs.py<pre><code>def level_order(root: TreeNode | None) -&gt; list[int]:\n \"\"\"\u5c64\u5e8f\u8d70\u8a2a\"\"\"\n # \u521d\u59cb\u5316\u4f47\u5217\uff0c\u52a0\u5165\u6839\u7bc0\u9ede\n queue: deque[TreeNode] = deque()\n queue.append(root)\n # \u521d\u59cb\u5316\u4e00\u500b\u4e32\u5217\uff0c\u7528\u65bc\u5132\u5b58\u8d70\u8a2a\u5e8f\u5217\n res = []\n while queue:\n node: TreeNode = queue.popleft() # \u968a\u5217\u51fa\u968a\n res.append(node.val) # \u5132\u5b58\u7bc0\u9ede\u503c\n if node.left is not None:\n queue.append(node.left) # \u5de6\u5b50\u7bc0\u9ede\u5165\u5217\n if node.right is not None:\n queue.append(node.right) # \u53f3\u5b50\u7bc0\u9ede\u5165\u5217\n return res\n</code></pre> binary_tree_bfs.cpp<pre><code>/* \u5c64\u5e8f\u8d70\u8a2a */\nvector&lt;int&gt; levelOrder(TreeNode *root) {\n // \u521d\u59cb\u5316\u4f47\u5217\uff0c\u52a0\u5165\u6839\u7bc0\u9ede\n queue&lt;TreeNode *&gt; queue;\n queue.push(root);\n // \u521d\u59cb\u5316\u4e00\u500b\u4e32\u5217\uff0c\u7528\u65bc\u5132\u5b58\u8d70\u8a2a\u5e8f\u5217\n vector&lt;int&gt; vec;\n while (!queue.empty()) {\n TreeNode *node = queue.front();\n queue.pop(); // \u968a\u5217\u51fa\u968a\n vec.push_back(node-&gt;val); // \u5132\u5b58\u7bc0\u9ede\u503c\n if (node-&gt;left != nullptr)\n queue.push(node-&gt;left); // \u5de6\u5b50\u7bc0\u9ede\u5165\u5217\n if (node-&gt;right != nullptr)\n queue.push(node-&gt;right); // \u53f3\u5b50\u7bc0\u9ede\u5165\u5217\n }\n return vec;\n}\n</code></pre> binary_tree_bfs.java<pre><code>/* \u5c64\u5e8f\u8d70\u8a2a */\nList&lt;Integer&gt; levelOrder(TreeNode root) {\n // \u521d\u59cb\u5316\u4f47\u5217\uff0c\u52a0\u5165\u6839\u7bc0\u9ede\n Queue&lt;TreeNode&gt; queue = new LinkedList&lt;&gt;();\n queue.add(root);\n // \u521d\u59cb\u5316\u4e00\u500b\u4e32\u5217\uff0c\u7528\u65bc\u5132\u5b58\u8d70\u8a2a\u5e8f\u5217\n List&lt;Integer&gt; list = new ArrayList&lt;&gt;();\n while (!queue.isEmpty()) {\n TreeNode node = queue.poll(); // \u968a\u5217\u51fa\u968a\n list.add(node.val); // \u5132\u5b58\u7bc0\u9ede\u503c\n if (node.left != null)\n queue.offer(node.left); // \u5de6\u5b50\u7bc0\u9ede\u5165\u5217\n if (node.right != null)\n queue.offer(node.right); // \u53f3\u5b50\u7bc0\u9ede\u5165\u5217\n }\n return list;\n}\n</code></pre> binary_tree_bfs.cs<pre><code>/* \u5c64\u5e8f\u8d70\u8a2a */\nList&lt;int&gt; LevelOrder(TreeNode root) {\n // \u521d\u59cb\u5316\u4f47\u5217\uff0c\u52a0\u5165\u6839\u7bc0\u9ede\n Queue&lt;TreeNode&gt; queue = new();\n queue.Enqueue(root);\n // \u521d\u59cb\u5316\u4e00\u500b\u4e32\u5217\uff0c\u7528\u65bc\u5132\u5b58\u8d70\u8a2a\u5e8f\u5217\n List&lt;int&gt; list = [];\n while (queue.Count != 0) {\n TreeNode node = queue.Dequeue(); // \u968a\u5217\u51fa\u968a\n list.Add(node.val!.Value); // \u5132\u5b58\u7bc0\u9ede\u503c\n if (node.left != null)\n queue.Enqueue(node.left); // \u5de6\u5b50\u7bc0\u9ede\u5165\u5217\n if (node.right != null)\n queue.Enqueue(node.right); // \u53f3\u5b50\u7bc0\u9ede\u5165\u5217\n }\n return list;\n}\n</code></pre> binary_tree_bfs.go<pre><code>/* \u5c64\u5e8f\u8d70\u8a2a */\nfunc levelOrder(root *TreeNode) []any {\n // \u521d\u59cb\u5316\u4f47\u5217\uff0c\u52a0\u5165\u6839\u7bc0\u9ede\n queue := list.New()\n queue.PushBack(root)\n // \u521d\u59cb\u5316\u4e00\u500b\u5207\u7247\uff0c\u7528\u65bc\u5132\u5b58\u8d70\u8a2a\u5e8f\u5217\n nums := make([]any, 0)\n for queue.Len() &gt; 0 {\n // \u968a\u5217\u51fa\u968a\n node := queue.Remove(queue.Front()).(*TreeNode)\n // \u5132\u5b58\u7bc0\u9ede\u503c\n nums = append(nums, node.Val)\n if node.Left != nil {\n // \u5de6\u5b50\u7bc0\u9ede\u5165\u5217\n queue.PushBack(node.Left)\n }\n if node.Right != nil {\n // \u53f3\u5b50\u7bc0\u9ede\u5165\u5217\n queue.PushBack(node.Right)\n }\n }\n return nums\n}\n</code></pre> binary_tree_bfs.swift<pre><code>/* \u5c64\u5e8f\u8d70\u8a2a */\nfunc levelOrder(root: TreeNode) -&gt; [Int] {\n // \u521d\u59cb\u5316\u4f47\u5217\uff0c\u52a0\u5165\u6839\u7bc0\u9ede\n var queue: [TreeNode] = [root]\n // \u521d\u59cb\u5316\u4e00\u500b\u4e32\u5217\uff0c\u7528\u65bc\u5132\u5b58\u8d70\u8a2a\u5e8f\u5217\n var list: [Int] = []\n while !queue.isEmpty {\n let node = queue.removeFirst() // \u968a\u5217\u51fa\u968a\n list.append(node.val) // \u5132\u5b58\u7bc0\u9ede\u503c\n if let left = node.left {\n queue.append(left) // \u5de6\u5b50\u7bc0\u9ede\u5165\u5217\n }\n if let right = node.right {\n queue.append(right) // \u53f3\u5b50\u7bc0\u9ede\u5165\u5217\n }\n }\n return list\n}\n</code></pre> binary_tree_bfs.js<pre><code>/* \u5c64\u5e8f\u8d70\u8a2a */\nfunction levelOrder(root) {\n // \u521d\u59cb\u5316\u4f47\u5217\uff0c\u52a0\u5165\u6839\u7bc0\u9ede\n const queue = [root];\n // \u521d\u59cb\u5316\u4e00\u500b\u4e32\u5217\uff0c\u7528\u65bc\u5132\u5b58\u8d70\u8a2a\u5e8f\u5217\n const list = [];\n while (queue.length) {\n let node = queue.shift(); // \u968a\u5217\u51fa\u968a\n list.push(node.val); // \u5132\u5b58\u7bc0\u9ede\u503c\n if (node.left) queue.push(node.left); // \u5de6\u5b50\u7bc0\u9ede\u5165\u5217\n if (node.right) queue.push(node.right); // \u53f3\u5b50\u7bc0\u9ede\u5165\u5217\n }\n return list;\n}\n</code></pre> binary_tree_bfs.ts<pre><code>/* \u5c64\u5e8f\u8d70\u8a2a */\nfunction levelOrder(root: TreeNode | null): number[] {\n // \u521d\u59cb\u5316\u4f47\u5217\uff0c\u52a0\u5165\u6839\u7bc0\u9ede\n const queue = [root];\n // \u521d\u59cb\u5316\u4e00\u500b\u4e32\u5217\uff0c\u7528\u65bc\u5132\u5b58\u8d70\u8a2a\u5e8f\u5217\n const list: number[] = [];\n while (queue.length) {\n let node = queue.shift() as TreeNode; // \u968a\u5217\u51fa\u968a\n list.push(node.val); // \u5132\u5b58\u7bc0\u9ede\u503c\n if (node.left) {\n queue.push(node.left); // \u5de6\u5b50\u7bc0\u9ede\u5165\u5217\n }\n if (node.right) {\n queue.push(node.right); // \u53f3\u5b50\u7bc0\u9ede\u5165\u5217\n }\n }\n return list;\n}\n</code></pre> binary_tree_bfs.dart<pre><code>/* \u5c64\u5e8f\u8d70\u8a2a */\nList&lt;int&gt; levelOrder(TreeNode? root) {\n // \u521d\u59cb\u5316\u4f47\u5217\uff0c\u52a0\u5165\u6839\u7bc0\u9ede\n Queue&lt;TreeNode?&gt; queue = Queue();\n queue.add(root);\n // \u521d\u59cb\u5316\u4e00\u500b\u4e32\u5217\uff0c\u7528\u65bc\u5132\u5b58\u8d70\u8a2a\u5e8f\u5217\n List&lt;int&gt; res = [];\n while (queue.isNotEmpty) {\n TreeNode? node = queue.removeFirst(); // \u968a\u5217\u51fa\u968a\n res.add(node!.val); // \u5132\u5b58\u7bc0\u9ede\u503c\n if (node.left != null) queue.add(node.left); // \u5de6\u5b50\u7bc0\u9ede\u5165\u5217\n if (node.right != null) queue.add(node.right); // \u53f3\u5b50\u7bc0\u9ede\u5165\u5217\n }\n return res;\n}\n</code></pre> binary_tree_bfs.rs<pre><code>/* \u5c64\u5e8f\u8d70\u8a2a */\nfn level_order(root: &amp;Rc&lt;RefCell&lt;TreeNode&gt;&gt;) -&gt; Vec&lt;i32&gt; {\n // \u521d\u59cb\u5316\u4f47\u5217\uff0c\u52a0\u5165\u6839\u7bc0\u9ede\n let mut que = VecDeque::new();\n que.push_back(root.clone());\n // \u521d\u59cb\u5316\u4e00\u500b\u4e32\u5217\uff0c\u7528\u65bc\u5132\u5b58\u8d70\u8a2a\u5e8f\u5217\n let mut vec = Vec::new();\n\n while let Some(node) = que.pop_front() {\n // \u968a\u5217\u51fa\u968a\n vec.push(node.borrow().val); // \u5132\u5b58\u7bc0\u9ede\u503c\n if let Some(left) = node.borrow().left.as_ref() {\n que.push_back(left.clone()); // \u5de6\u5b50\u7bc0\u9ede\u5165\u5217\n }\n if let Some(right) = node.borrow().right.as_ref() {\n que.push_back(right.clone()); // \u53f3\u5b50\u7bc0\u9ede\u5165\u5217\n };\n }\n vec\n}\n</code></pre> binary_tree_bfs.c<pre><code>/* \u5c64\u5e8f\u8d70\u8a2a */\nint *levelOrder(TreeNode *root, int *size) {\n /* \u8f14\u52a9\u4f47\u5217 */\n int front, rear;\n int index, *arr;\n TreeNode *node;\n TreeNode **queue;\n\n /* \u8f14\u52a9\u4f47\u5217 */\n queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_SIZE);\n // \u4f47\u5217\u6307\u6a19\n front = 0, rear = 0;\n // \u52a0\u5165\u6839\u7bc0\u9ede\n queue[rear++] = root;\n // \u521d\u59cb\u5316\u4e00\u500b\u4e32\u5217\uff0c\u7528\u65bc\u5132\u5b58\u8d70\u8a2a\u5e8f\u5217\n /* \u8f14\u52a9\u9663\u5217 */\n arr = (int *)malloc(sizeof(int) * MAX_SIZE);\n // \u9663\u5217\u6307\u6a19\n index = 0;\n while (front &lt; rear) {\n // \u968a\u5217\u51fa\u968a\n node = queue[front++];\n // \u5132\u5b58\u7bc0\u9ede\u503c\n arr[index++] = node-&gt;val;\n if (node-&gt;left != NULL) {\n // \u5de6\u5b50\u7bc0\u9ede\u5165\u5217\n queue[rear++] = node-&gt;left;\n }\n if (node-&gt;right != NULL) {\n // \u53f3\u5b50\u7bc0\u9ede\u5165\u5217\n queue[rear++] = node-&gt;right;\n }\n }\n // \u66f4\u65b0\u9663\u5217\u9577\u5ea6\u7684\u503c\n *size = index;\n arr = realloc(arr, sizeof(int) * (*size));\n\n // \u91cb\u653e\u8f14\u52a9\u9663\u5217\u7a7a\u9593\n free(queue);\n return arr;\n}\n</code></pre> binary_tree_bfs.kt<pre><code>/* \u5c64\u5e8f\u8d70\u8a2a */\nfun levelOrder(root: TreeNode?): MutableList&lt;Int&gt; {\n // \u521d\u59cb\u5316\u4f47\u5217\uff0c\u52a0\u5165\u6839\u7bc0\u9ede\n val queue = LinkedList&lt;TreeNode?&gt;()\n queue.add(root)\n // \u521d\u59cb\u5316\u4e00\u500b\u4e32\u5217\uff0c\u7528\u65bc\u5132\u5b58\u8d70\u8a2a\u5e8f\u5217\n val list = mutableListOf&lt;Int&gt;()\n while (queue.isNotEmpty()) {\n val node = queue.poll() // \u968a\u5217\u51fa\u968a\n list.add(node?._val!!) // \u5132\u5b58\u7bc0\u9ede\u503c\n if (node.left != null)\n queue.offer(node.left) // \u5de6\u5b50\u7bc0\u9ede\u5165\u5217\n if (node.right != null)\n queue.offer(node.right) // \u53f3\u5b50\u7bc0\u9ede\u5165\u5217\n }\n return list\n}\n</code></pre> binary_tree_bfs.rb<pre><code>### \u5c64\u5e8f\u8d70\u8a2a ###\ndef level_order(root)\n # \u521d\u59cb\u5316\u4f47\u5217\uff0c\u52a0\u5165\u6839\u7bc0\u9ede\n queue = [root]\n # \u521d\u59cb\u5316\u4e00\u500b\u4e32\u5217\uff0c\u7528\u65bc\u5132\u5b58\u8d70\u8a2a\u5e8f\u5217\n res = []\n while !queue.empty?\n node = queue.shift # \u968a\u5217\u51fa\u968a\n res &lt;&lt; node.val # \u5132\u5b58\u7bc0\u9ede\u503c\n queue &lt;&lt; node.left unless node.left.nil? # \u5de6\u5b50\u7bc0\u9ede\u5165\u5217\n queue &lt;&lt; node.right unless node.right.nil? # \u53f3\u5b50\u7bc0\u9ede\u5165\u5217\n end\n res\nend\n</code></pre> binary_tree_bfs.zig<pre><code>// \u5c64\u5e8f\u8d70\u8a2a\nfn levelOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) {\n // \u521d\u59cb\u5316\u4f47\u5217\uff0c\u52a0\u5165\u6839\u7bc0\u9ede\n const L = std.TailQueue(*inc.TreeNode(T));\n var queue = L{};\n var root_node = try mem_allocator.create(L.Node);\n root_node.data = root;\n queue.append(root_node); \n // \u521d\u59cb\u5316\u4e00\u500b\u4e32\u5217\uff0c\u7528\u65bc\u5132\u5b58\u8d70\u8a2a\u5e8f\u5217\n var list = std.ArrayList(T).init(std.heap.page_allocator);\n while (queue.len &gt; 0) {\n var queue_node = queue.popFirst().?; // \u968a\u5217\u51fa\u968a\n var node = queue_node.data;\n try list.append(node.val); // \u5132\u5b58\u7bc0\u9ede\u503c\n if (node.left != null) {\n var tmp_node = try mem_allocator.create(L.Node);\n tmp_node.data = node.left.?;\n queue.append(tmp_node); // \u5de6\u5b50\u7bc0\u9ede\u5165\u5217\n }\n if (node.right != null) {\n var tmp_node = try mem_allocator.create(L.Node);\n tmp_node.data = node.right.?;\n queue.append(tmp_node); // \u53f3\u5b50\u7bc0\u9ede\u5165\u5217\n } \n }\n return list;\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p>"},{"location":"chapter_tree/binary_tree_traversal/#2","title":"2. \u00a0 \u8907\u96dc\u5ea6\u5206\u6790","text":"<ul> <li>\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \uff1a\u6240\u6709\u7bc0\u9ede\u88ab\u8a2a\u554f\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u6642\u9593\uff0c\u5176\u4e2d \\(n\\) \u70ba\u7bc0\u9ede\u6578\u91cf\u3002</li> <li>\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \uff1a\u5728\u6700\u5dee\u60c5\u6cc1\u4e0b\uff0c\u5373\u6eff\u4e8c\u5143\u6a39\u6642\uff0c\u8d70\u8a2a\u5230\u6700\u5e95\u5c64\u4e4b\u524d\uff0c\u4f47\u5217\u4e2d\u6700\u591a\u540c\u6642\u5b58\u5728 \\((n + 1) / 2\\) \u500b\u7bc0\u9ede\uff0c\u4f54\u7528 \\(O(n)\\) \u7a7a\u9593\u3002</li> </ul>"},{"location":"chapter_tree/binary_tree_traversal/#722","title":"7.2.2 \u00a0 \u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u5f8c\u5e8f\u8d70\u8a2a","text":"<p>\u76f8\u61c9\u5730\uff0c\u524d\u5e8f\u3001\u4e2d\u5e8f\u548c\u5f8c\u5e8f\u8d70\u8a2a\u90fd\u5c6c\u65bc\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\uff08depth-first traversal\uff09\uff0c\u4e5f\u7a31\u6df1\u5ea6\u512a\u5148\u641c\u5c0b\uff08depth-first search, DFS\uff09\uff0c\u5b83\u9ad4\u73fe\u4e86\u4e00\u7a2e\u201c\u5148\u8d70\u5230\u76e1\u982d\uff0c\u518d\u56de\u6eaf\u7e7c\u7e8c\u201d\u7684\u8d70\u8a2a\u65b9\u5f0f\u3002</p> <p>\u5716 7-10 \u5c55\u793a\u4e86\u5c0d\u4e8c\u5143\u6a39\u9032\u884c\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u7684\u5de5\u4f5c\u539f\u7406\u3002\u6df1\u5ea6\u512a\u5148\u8d70\u8a2a\u5c31\u50cf\u662f\u7e5e\u8457\u6574\u68f5\u4e8c\u5143\u6a39\u7684\u5916\u570d\u201c\u8d70\u201d\u4e00\u5708\uff0c\u5728\u6bcf\u500b\u7bc0\u9ede\u90fd\u6703\u9047\u5230\u4e09\u500b\u4f4d\u7f6e\uff0c\u5206\u5225\u5c0d\u61c9\u524d\u5e8f\u8d70\u8a2a\u3001\u4e2d\u5e8f\u8d70\u8a2a\u548c\u5f8c\u5e8f\u8d70\u8a2a\u3002</p> <p></p> <p> \u5716 7-10 \u00a0 \u4e8c\u5143\u641c\u5c0b\u6a39\u7684\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u5f8c\u5e8f\u8d70\u8a2a </p>"},{"location":"chapter_tree/binary_tree_traversal/#1_1","title":"1. \u00a0 \u7a0b\u5f0f\u78bc\u5be6\u73fe","text":"<p>\u6df1\u5ea6\u512a\u5148\u641c\u5c0b\u901a\u5e38\u57fa\u65bc\u905e\u8ff4\u5be6\u73fe\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_tree_dfs.py<pre><code>def pre_order(root: TreeNode | None):\n \"\"\"\u524d\u5e8f\u8d70\u8a2a\"\"\"\n if root is None:\n return\n # \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u6839\u7bc0\u9ede -&gt; \u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39\n res.append(root.val)\n pre_order(root=root.left)\n pre_order(root=root.right)\n\ndef in_order(root: TreeNode | None):\n \"\"\"\u4e2d\u5e8f\u8d70\u8a2a\"\"\"\n if root is None:\n return\n # \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede -&gt; \u53f3\u5b50\u6a39\n in_order(root=root.left)\n res.append(root.val)\n in_order(root=root.right)\n\ndef post_order(root: TreeNode | None):\n \"\"\"\u5f8c\u5e8f\u8d70\u8a2a\"\"\"\n if root is None:\n return\n # \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede\n post_order(root=root.left)\n post_order(root=root.right)\n res.append(root.val)\n</code></pre> binary_tree_dfs.cpp<pre><code>/* \u524d\u5e8f\u8d70\u8a2a */\nvoid preOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u6839\u7bc0\u9ede -&gt; \u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39\n vec.push_back(root-&gt;val);\n preOrder(root-&gt;left);\n preOrder(root-&gt;right);\n}\n\n/* \u4e2d\u5e8f\u8d70\u8a2a */\nvoid inOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede -&gt; \u53f3\u5b50\u6a39\n inOrder(root-&gt;left);\n vec.push_back(root-&gt;val);\n inOrder(root-&gt;right);\n}\n\n/* \u5f8c\u5e8f\u8d70\u8a2a */\nvoid postOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede\n postOrder(root-&gt;left);\n postOrder(root-&gt;right);\n vec.push_back(root-&gt;val);\n}\n</code></pre> binary_tree_dfs.java<pre><code>/* \u524d\u5e8f\u8d70\u8a2a */\nvoid preOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u6839\u7bc0\u9ede -&gt; \u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39\n list.add(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u8d70\u8a2a */\nvoid inOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede -&gt; \u53f3\u5b50\u6a39\n inOrder(root.left);\n list.add(root.val);\n inOrder(root.right);\n}\n\n/* \u5f8c\u5e8f\u8d70\u8a2a */\nvoid postOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede\n postOrder(root.left);\n postOrder(root.right);\n list.add(root.val);\n}\n</code></pre> binary_tree_dfs.cs<pre><code>/* \u524d\u5e8f\u8d70\u8a2a */\nvoid PreOrder(TreeNode? root) {\n if (root == null) return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u6839\u7bc0\u9ede -&gt; \u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39\n list.Add(root.val!.Value);\n PreOrder(root.left);\n PreOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u8d70\u8a2a */\nvoid InOrder(TreeNode? root) {\n if (root == null) return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede -&gt; \u53f3\u5b50\u6a39\n InOrder(root.left);\n list.Add(root.val!.Value);\n InOrder(root.right);\n}\n\n/* \u5f8c\u5e8f\u8d70\u8a2a */\nvoid PostOrder(TreeNode? root) {\n if (root == null) return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede\n PostOrder(root.left);\n PostOrder(root.right);\n list.Add(root.val!.Value);\n}\n</code></pre> binary_tree_dfs.go<pre><code>/* \u524d\u5e8f\u8d70\u8a2a */\nfunc preOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u6839\u7bc0\u9ede -&gt; \u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39\n nums = append(nums, node.Val)\n preOrder(node.Left)\n preOrder(node.Right)\n}\n\n/* \u4e2d\u5e8f\u8d70\u8a2a */\nfunc inOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede -&gt; \u53f3\u5b50\u6a39\n inOrder(node.Left)\n nums = append(nums, node.Val)\n inOrder(node.Right)\n}\n\n/* \u5f8c\u5e8f\u8d70\u8a2a */\nfunc postOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede\n postOrder(node.Left)\n postOrder(node.Right)\n nums = append(nums, node.Val)\n}\n</code></pre> binary_tree_dfs.swift<pre><code>/* \u524d\u5e8f\u8d70\u8a2a */\nfunc preOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u6839\u7bc0\u9ede -&gt; \u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39\n list.append(root.val)\n preOrder(root: root.left)\n preOrder(root: root.right)\n}\n\n/* \u4e2d\u5e8f\u8d70\u8a2a */\nfunc inOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede -&gt; \u53f3\u5b50\u6a39\n inOrder(root: root.left)\n list.append(root.val)\n inOrder(root: root.right)\n}\n\n/* \u5f8c\u5e8f\u8d70\u8a2a */\nfunc postOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede\n postOrder(root: root.left)\n postOrder(root: root.right)\n list.append(root.val)\n}\n</code></pre> binary_tree_dfs.js<pre><code>/* \u524d\u5e8f\u8d70\u8a2a */\nfunction preOrder(root) {\n if (root === null) return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u6839\u7bc0\u9ede -&gt; \u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39\n list.push(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u8d70\u8a2a */\nfunction inOrder(root) {\n if (root === null) return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede -&gt; \u53f3\u5b50\u6a39\n inOrder(root.left);\n list.push(root.val);\n inOrder(root.right);\n}\n\n/* \u5f8c\u5e8f\u8d70\u8a2a */\nfunction postOrder(root) {\n if (root === null) return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede\n postOrder(root.left);\n postOrder(root.right);\n list.push(root.val);\n}\n</code></pre> binary_tree_dfs.ts<pre><code>/* \u524d\u5e8f\u8d70\u8a2a */\nfunction preOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u6839\u7bc0\u9ede -&gt; \u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39\n list.push(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u8d70\u8a2a */\nfunction inOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede -&gt; \u53f3\u5b50\u6a39\n inOrder(root.left);\n list.push(root.val);\n inOrder(root.right);\n}\n\n/* \u5f8c\u5e8f\u8d70\u8a2a */\nfunction postOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede\n postOrder(root.left);\n postOrder(root.right);\n list.push(root.val);\n}\n</code></pre> binary_tree_dfs.dart<pre><code>/* \u524d\u5e8f\u8d70\u8a2a */\nvoid preOrder(TreeNode? node) {\n if (node == null) return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u6839\u7bc0\u9ede -&gt; \u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39\n list.add(node.val);\n preOrder(node.left);\n preOrder(node.right);\n}\n\n/* \u4e2d\u5e8f\u8d70\u8a2a */\nvoid inOrder(TreeNode? node) {\n if (node == null) return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede -&gt; \u53f3\u5b50\u6a39\n inOrder(node.left);\n list.add(node.val);\n inOrder(node.right);\n}\n\n/* \u5f8c\u5e8f\u8d70\u8a2a */\nvoid postOrder(TreeNode? node) {\n if (node == null) return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede\n postOrder(node.left);\n postOrder(node.right);\n list.add(node.val);\n}\n</code></pre> binary_tree_dfs.rs<pre><code>/* \u524d\u5e8f\u8d70\u8a2a */\nfn pre_order(root: Option&lt;&amp;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;) -&gt; Vec&lt;i32&gt; {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u6839\u7bc0\u9ede -&gt; \u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39\n result.push(node.borrow().val);\n result.extend(pre_order(node.borrow().left.as_ref()));\n result.extend(pre_order(node.borrow().right.as_ref()));\n }\n result\n}\n\n/* \u4e2d\u5e8f\u8d70\u8a2a */\nfn in_order(root: Option&lt;&amp;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;) -&gt; Vec&lt;i32&gt; {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede -&gt; \u53f3\u5b50\u6a39\n result.extend(in_order(node.borrow().left.as_ref()));\n result.push(node.borrow().val);\n result.extend(in_order(node.borrow().right.as_ref()));\n }\n result\n}\n\n/* \u5f8c\u5e8f\u8d70\u8a2a */\nfn post_order(root: Option&lt;&amp;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;) -&gt; Vec&lt;i32&gt; {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede\n result.extend(post_order(node.borrow().left.as_ref()));\n result.extend(post_order(node.borrow().right.as_ref()));\n result.push(node.borrow().val);\n }\n result\n}\n</code></pre> binary_tree_dfs.c<pre><code>/* \u524d\u5e8f\u8d70\u8a2a */\nvoid preOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u6839\u7bc0\u9ede -&gt; \u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39\n arr[(*size)++] = root-&gt;val;\n preOrder(root-&gt;left, size);\n preOrder(root-&gt;right, size);\n}\n\n/* \u4e2d\u5e8f\u8d70\u8a2a */\nvoid inOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede -&gt; \u53f3\u5b50\u6a39\n inOrder(root-&gt;left, size);\n arr[(*size)++] = root-&gt;val;\n inOrder(root-&gt;right, size);\n}\n\n/* \u5f8c\u5e8f\u8d70\u8a2a */\nvoid postOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede\n postOrder(root-&gt;left, size);\n postOrder(root-&gt;right, size);\n arr[(*size)++] = root-&gt;val;\n}\n</code></pre> binary_tree_dfs.kt<pre><code>/* \u524d\u5e8f\u8d70\u8a2a */\nfun preOrder(root: TreeNode?) {\n if (root == null) return\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u6839\u7bc0\u9ede -&gt; \u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39\n list.add(root._val)\n preOrder(root.left)\n preOrder(root.right)\n}\n\n/* \u4e2d\u5e8f\u8d70\u8a2a */\nfun inOrder(root: TreeNode?) {\n if (root == null) return\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede -&gt; \u53f3\u5b50\u6a39\n inOrder(root.left)\n list.add(root._val)\n inOrder(root.right)\n}\n\n/* \u5f8c\u5e8f\u8d70\u8a2a */\nfun postOrder(root: TreeNode?) {\n if (root == null) return\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede\n postOrder(root.left)\n postOrder(root.right)\n list.add(root._val)\n}\n</code></pre> binary_tree_dfs.rb<pre><code>### \u524d\u5e8f\u8d70\u8a2a ###\ndef pre_order(root)\n return if root.nil?\n\n # \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u6839\u7bc0\u9ede -&gt; \u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39\n $res &lt;&lt; root.val\n pre_order(root.left)\n pre_order(root.right)\nend\n\n### \u4e2d\u5e8f\u8d70\u8a2a ###\ndef in_order(root)\n return if root.nil?\n\n # \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede -&gt; \u53f3\u5b50\u6a39\n in_order(root.left)\n $res &lt;&lt; root.val\n in_order(root.right)\nend\n\n### \u5f8c\u5e8f\u8d70\u8a2a ###\ndef post_order(root)\n return if root.nil?\n\n # \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede\n post_order(root.left)\n post_order(root.right)\n $res &lt;&lt; root.val\nend\n</code></pre> binary_tree_dfs.zig<pre><code>// \u524d\u5e8f\u8d70\u8a2a\nfn preOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u6839\u7bc0\u9ede -&gt; \u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39\n try list.append(root.?.val);\n try preOrder(T, root.?.left);\n try preOrder(T, root.?.right);\n}\n\n// \u4e2d\u5e8f\u8d70\u8a2a\nfn inOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede -&gt; \u53f3\u5b50\u6a39\n try inOrder(T, root.?.left);\n try list.append(root.?.val);\n try inOrder(T, root.?.right);\n}\n\n// \u5f8c\u5e8f\u8d70\u8a2a\nfn postOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8a2a\u554f\u512a\u5148\u9806\u5e8f\uff1a\u5de6\u5b50\u6a39 -&gt; \u53f3\u5b50\u6a39 -&gt; \u6839\u7bc0\u9ede\n try postOrder(T, root.?.left);\n try postOrder(T, root.?.right);\n try list.append(root.?.val);\n}\n</code></pre> \u8996\u89ba\u5316\u57f7\u884c <p> \u5168\u87a2\u5e55\u89c0\u770b &gt;</p> <p>Tip</p> <p>\u6df1\u5ea6\u512a\u5148\u641c\u5c0b\u4e5f\u53ef\u4ee5\u57fa\u65bc\u8fed\u4ee3\u5be6\u73fe\uff0c\u6709\u8208\u8da3\u7684\u8b80\u8005\u53ef\u4ee5\u81ea\u884c\u7814\u7a76\u3002</p> <p>\u5716 7-11 \u5c55\u793a\u4e86\u524d\u5e8f\u8d70\u8a2a\u4e8c\u5143\u6a39\u7684\u905e\u8ff4\u904e\u7a0b\uff0c\u5176\u53ef\u5206\u70ba\u201c\u905e\u201d\u548c\u201c\u8ff4\u201d\u5169\u500b\u9006\u5411\u7684\u90e8\u5206\u3002</p> <ol> <li>\u201c\u905e\u201d\u8868\u793a\u958b\u555f\u65b0\u65b9\u6cd5\uff0c\u7a0b\u5f0f\u5728\u6b64\u904e\u7a0b\u4e2d\u8a2a\u554f\u4e0b\u4e00\u500b\u7bc0\u9ede\u3002</li> <li>\u201c\u8ff4\u201d\u8868\u793a\u51fd\u5f0f\u8fd4\u56de\uff0c\u4ee3\u8868\u7576\u524d\u7bc0\u9ede\u5df2\u7d93\u8a2a\u554f\u5b8c\u7562\u3002</li> </ol> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt;&lt;5&gt;&lt;6&gt;&lt;7&gt;&lt;8&gt;&lt;9&gt;&lt;10&gt;&lt;11&gt; <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u5716 7-11 \u00a0 \u524d\u5e8f\u8d70\u8a2a\u7684\u905e\u8ff4\u904e\u7a0b </p>"},{"location":"chapter_tree/binary_tree_traversal/#2_1","title":"2. \u00a0 \u8907\u96dc\u5ea6\u5206\u6790","text":"<ul> <li>\u6642\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \uff1a\u6240\u6709\u7bc0\u9ede\u88ab\u8a2a\u554f\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u6642\u9593\u3002</li> <li>\u7a7a\u9593\u8907\u96dc\u5ea6\u70ba \\(O(n)\\) \uff1a\u5728\u6700\u5dee\u60c5\u6cc1\u4e0b\uff0c\u5373\u6a39\u9000\u5316\u70ba\u93c8\u7d50\u4e32\u5217\u6642\uff0c\u905e\u8ff4\u6df1\u5ea6\u9054\u5230 \\(n\\) \uff0c\u7cfb\u7d71\u4f54\u7528 \\(O(n)\\) \u5806\u758a\u5e40\u7a7a\u9593\u3002</li> </ul>"},{"location":"chapter_tree/summary/","title":"7.6 \u00a0 \u5c0f\u7d50","text":""},{"location":"chapter_tree/summary/#1","title":"1. \u00a0 \u91cd\u9ede\u56de\u9867","text":"<ul> <li>\u4e8c\u5143\u6a39\u662f\u4e00\u7a2e\u975e\u7dda\u6027\u8cc7\u6599\u7d50\u69cb\uff0c\u9ad4\u73fe\u201c\u4e00\u5206\u70ba\u4e8c\u201d\u7684\u5206\u6cbb\u908f\u8f2f\u3002\u6bcf\u500b\u4e8c\u5143\u6a39\u7bc0\u9ede\u5305\u542b\u4e00\u500b\u503c\u4ee5\u53ca\u5169\u500b\u6307\u6a19\uff0c\u5206\u5225\u6307\u5411\u5176\u5de6\u5b50\u7bc0\u9ede\u548c\u53f3\u5b50\u7bc0\u9ede\u3002</li> <li>\u5c0d\u65bc\u4e8c\u5143\u6a39\u4e2d\u7684\u67d0\u500b\u7bc0\u9ede\uff0c\u5176\u5de6\uff08\u53f3\uff09\u5b50\u7bc0\u9ede\u53ca\u5176\u4ee5\u4e0b\u5f62\u6210\u7684\u6a39\u88ab\u7a31\u70ba\u8a72\u7bc0\u9ede\u7684\u5de6\uff08\u53f3\uff09\u5b50\u6a39\u3002</li> <li>\u4e8c\u5143\u6a39\u7684\u76f8\u95dc\u8853\u8a9e\u5305\u62ec\u6839\u7bc0\u9ede\u3001\u8449\u7bc0\u9ede\u3001\u5c64\u3001\u5ea6\u3001\u908a\u3001\u9ad8\u5ea6\u548c\u6df1\u5ea6\u7b49\u3002</li> <li>\u4e8c\u5143\u6a39\u7684\u521d\u59cb\u5316\u3001\u7bc0\u9ede\u63d2\u5165\u548c\u7bc0\u9ede\u522a\u9664\u64cd\u4f5c\u8207\u93c8\u7d50\u4e32\u5217\u64cd\u4f5c\u65b9\u6cd5\u985e\u4f3c\u3002</li> <li>\u5e38\u898b\u7684\u4e8c\u5143\u6a39\u578b\u5225\u6709\u5b8c\u7f8e\u4e8c\u5143\u6a39\u3001\u5b8c\u5168\u4e8c\u5143\u6a39\u3001\u5b8c\u6eff\u4e8c\u5143\u6a39\u548c\u5e73\u8861\u4e8c\u5143\u6a39\u3002\u5b8c\u7f8e\u4e8c\u5143\u6a39\u662f\u6700\u7406\u60f3\u7684\u72c0\u614b\uff0c\u800c\u93c8\u7d50\u4e32\u5217\u662f\u9000\u5316\u5f8c\u7684\u6700\u5dee\u72c0\u614b\u3002</li> <li>\u4e8c\u5143\u6a39\u53ef\u4ee5\u7528\u9663\u5217\u8868\u793a\uff0c\u65b9\u6cd5\u662f\u5c07\u7bc0\u9ede\u503c\u548c\u7a7a\u4f4d\u6309\u5c64\u5e8f\u8d70\u8a2a\u9806\u5e8f\u6392\u5217\uff0c\u4e26\u6839\u64da\u7236\u7bc0\u9ede\u8207\u5b50\u7bc0\u9ede\u4e4b\u9593\u7684\u7d22\u5f15\u5c0d\u6620\u95dc\u4fc2\u4f86\u5be6\u73fe\u6307\u6a19\u3002</li> <li>\u4e8c\u5143\u6a39\u7684\u5c64\u5e8f\u8d70\u8a2a\u662f\u4e00\u7a2e\u5ee3\u5ea6\u512a\u5148\u641c\u5c0b\u65b9\u6cd5\uff0c\u5b83\u9ad4\u73fe\u4e86\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u64f4\u5c55\u201d\u7684\u9010\u5c64\u8d70\u8a2a\u65b9\u5f0f\uff0c\u901a\u5e38\u900f\u904e\u4f47\u5217\u4f86\u5be6\u73fe\u3002</li> <li>\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u5f8c\u5e8f\u8d70\u8a2a\u7686\u5c6c\u65bc\u6df1\u5ea6\u512a\u5148\u641c\u5c0b\uff0c\u5b83\u5011\u9ad4\u73fe\u4e86\u201c\u5148\u8d70\u5230\u76e1\u982d\uff0c\u518d\u56de\u6eaf\u7e7c\u7e8c\u201d\u7684\u8d70\u8a2a\u65b9\u5f0f\uff0c\u901a\u5e38\u4f7f\u7528\u905e\u8ff4\u4f86\u5be6\u73fe\u3002</li> <li>\u4e8c\u5143\u641c\u5c0b\u6a39\u662f\u4e00\u7a2e\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u8a62\u8cc7\u6599\u7d50\u69cb\uff0c\u5176\u67e5\u8a62\u3001\u63d2\u5165\u548c\u522a\u9664\u64cd\u4f5c\u7684\u6642\u9593\u8907\u96dc\u5ea6\u5747\u70ba \\(O(\\log n)\\) \u3002\u7576\u4e8c\u5143\u641c\u5c0b\u6a39\u9000\u5316\u70ba\u93c8\u7d50\u4e32\u5217\u6642\uff0c\u5404\u9805\u6642\u9593\u8907\u96dc\u5ea6\u6703\u52a3\u5316\u81f3 \\(O(n)\\) \u3002</li> <li>AVL \u6a39\uff0c\u4e5f\u7a31\u5e73\u8861\u4e8c\u5143\u641c\u5c0b\u6a39\uff0c\u5b83\u900f\u904e\u65cb\u8f49\u64cd\u4f5c\u78ba\u4fdd\u5728\u4e0d\u65b7\u63d2\u5165\u548c\u522a\u9664\u7bc0\u9ede\u5f8c\u6a39\u4ecd\u7136\u4fdd\u6301\u5e73\u8861\u3002</li> <li>AVL \u6a39\u7684\u65cb\u8f49\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\u522a\u9664\u7bc0\u9ede\u5f8c\uff0cAVL \u6a39\u6703\u5f9e\u5e95\u5411\u9802\u57f7\u884c\u65cb\u8f49\u64cd\u4f5c\uff0c\u4f7f\u6a39\u91cd\u65b0\u6062\u5fa9\u5e73\u8861\u3002</li> </ul>"},{"location":"chapter_tree/summary/#2-q-a","title":"2. \u00a0 Q &amp; A","text":"<p>Q\uff1a\u5c0d\u65bc\u53ea\u6709\u4e00\u500b\u7bc0\u9ede\u7684\u4e8c\u5143\u6a39\uff0c\u6a39\u7684\u9ad8\u5ea6\u548c\u6839\u7bc0\u9ede\u7684\u6df1\u5ea6\u90fd\u662f \\(0\\) \u55ce\uff1f</p> <p>\u662f\u7684\uff0c\u56e0\u70ba\u9ad8\u5ea6\u548c\u6df1\u5ea6\u901a\u5e38\u5b9a\u7fa9\u70ba\u201c\u7d93\u904e\u7684\u908a\u7684\u6578\u91cf\u201d\u3002</p> <p>Q\uff1a\u4e8c\u5143\u6a39\u4e2d\u7684\u63d2\u5165\u8207\u522a\u9664\u4e00\u822c\u7531\u4e00\u5957\u64cd\u4f5c\u914d\u5408\u5b8c\u6210\uff0c\u9019\u88e1\u7684\u201c\u4e00\u5957\u64cd\u4f5c\u201d\u6307\u4ec0\u9ebc\u5462\uff1f\u53ef\u4ee5\u7406\u89e3\u70ba\u8cc7\u6e90\u7684\u5b50\u7bc0\u9ede\u7684\u8cc7\u6e90\u91cb\u653e\u55ce\uff1f</p> <p>\u62ff\u4e8c\u5143\u641c\u5c0b\u6a39\u4f86\u8209\u4f8b\uff0c\u522a\u9664\u7bc0\u9ede\u64cd\u4f5c\u8981\u5206\u4e09\u7a2e\u60c5\u6cc1\u8655\u7406\uff0c\u5176\u4e2d\u6bcf\u7a2e\u60c5\u6cc1\u90fd\u9700\u8981\u9032\u884c\u591a\u500b\u6b65\u9a5f\u7684\u7bc0\u9ede\u64cd\u4f5c\u3002</p> <p>Q\uff1a\u70ba\u4ec0\u9ebc DFS \u8d70\u8a2a\u4e8c\u5143\u6a39\u6709\u524d\u3001\u4e2d\u3001\u5f8c\u4e09\u7a2e\u9806\u5e8f\uff0c\u5206\u5225\u6709\u4ec0\u9ebc\u7528\u5462\uff1f</p> <p>\u8207\u9806\u5e8f\u548c\u9006\u5e8f\u8d70\u8a2a\u9663\u5217\u985e\u4f3c\uff0c\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u5f8c\u5e8f\u8d70\u8a2a\u662f\u4e09\u7a2e\u4e8c\u5143\u6a39\u8d70\u8a2a\u65b9\u6cd5\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u5b83\u5011\u5f97\u5230\u4e00\u500b\u7279\u5b9a\u9806\u5e8f\u7684\u8d70\u8a2a\u7d50\u679c\u3002\u4f8b\u5982\u5728\u4e8c\u5143\u641c\u5c0b\u6a39\u4e2d\uff0c\u7531\u65bc\u7bc0\u9ede\u5927\u5c0f\u6eff\u8db3 <code>\u5de6\u5b50\u7bc0\u9ede\u503c &lt; \u6839\u7bc0\u9ede\u503c &lt; \u53f3\u5b50\u7bc0\u9ede\u503c</code> \uff0c\u56e0\u6b64\u6211\u5011\u53ea\u8981\u6309\u7167\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u7684\u512a\u5148\u9806\u5e8f\u8d70\u8a2a\u6a39\uff0c\u5c31\u53ef\u4ee5\u7372\u5f97\u6709\u5e8f\u7684\u7bc0\u9ede\u5e8f\u5217\u3002</p> <p>Q\uff1a\u53f3\u65cb\u64cd\u4f5c\u662f\u8655\u7406\u5931\u8861\u7bc0\u9ede <code>node</code>\u3001<code>child</code>\u3001<code>grand_child</code> \u4e4b\u9593\u7684\u95dc\u4fc2\uff0c\u90a3 <code>node</code> \u7684\u7236\u7bc0\u9ede\u548c <code>node</code> \u539f\u4f86\u7684\u9023\u7dda\u4e0d\u9700\u8981\u7dad\u8b77\u55ce\uff1f\u53f3\u65cb\u64cd\u4f5c\u5f8c\u8c48\u4e0d\u662f\u65b7\u6389\u4e86\uff1f</p> <p>\u6211\u5011\u9700\u8981\u5f9e\u905e\u8ff4\u7684\u8996\u89d2\u4f86\u770b\u9019\u500b\u554f\u984c\u3002\u53f3\u65cb\u64cd\u4f5c <code>right_rotate(root)</code> \u50b3\u5165\u7684\u662f\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\uff0c\u6700\u7d42 <code>return child</code> \u8fd4\u56de\u65cb\u8f49\u4e4b\u5f8c\u7684\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\u3002\u5b50\u6a39\u7684\u6839\u7bc0\u9ede\u548c\u5176\u7236\u7bc0\u9ede\u7684\u9023\u7dda\u662f\u5728\u8a72\u51fd\u5f0f\u8fd4\u56de\u5f8c\u5b8c\u6210\u7684\uff0c\u4e0d\u5c6c\u65bc\u53f3\u65cb\u64cd\u4f5c\u7684\u7dad\u8b77\u7bc4\u570d\u3002</p> <p>Q\uff1a\u5728 C++ \u4e2d\uff0c\u51fd\u5f0f\u88ab\u5283\u5206\u5230 <code>private</code> \u548c <code>public</code> \u4e2d\uff0c\u9019\u65b9\u9762\u6709\u4ec0\u9ebc\u8003\u91cf\u55ce\uff1f\u70ba\u4ec0\u9ebc\u8981\u5c07 <code>height()</code> \u51fd\u5f0f\u548c <code>updateHeight()</code> \u51fd\u5f0f\u5206\u5225\u653e\u5728 <code>public</code> \u548c <code>private</code> \u4e2d\u5462\uff1f</p> <p>\u4e3b\u8981\u770b\u65b9\u6cd5\u7684\u4f7f\u7528\u7bc4\u570d\uff0c\u5982\u679c\u65b9\u6cd5\u53ea\u5728\u985e\u5225\u5167\u90e8\u4f7f\u7528\uff0c\u90a3\u9ebc\u5c31\u8a2d\u8a08\u70ba <code>private</code> \u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u8005\u55ae\u7368\u547c\u53eb <code>updateHeight()</code> \u662f\u6c92\u6709\u610f\u7fa9\u7684\uff0c\u5b83\u53ea\u662f\u63d2\u5165\u3001\u522a\u9664\u64cd\u4f5c\u4e2d\u7684\u4e00\u6b65\u3002\u800c <code>height()</code> \u662f\u8a2a\u554f\u7bc0\u9ede\u9ad8\u5ea6\uff0c\u985e\u4f3c\u65bc <code>vector.size()</code> \uff0c\u56e0\u6b64\u8a2d\u5b9a\u6210 <code>public</code> \u4ee5\u4fbf\u4f7f\u7528\u3002</p> <p>Q\uff1a\u5982\u4f55\u5f9e\u4e00\u7d44\u8f38\u5165\u8cc7\u6599\u69cb\u5efa\u4e00\u68f5\u4e8c\u5143\u641c\u5c0b\u6a39\uff1f\u6839\u7bc0\u9ede\u7684\u9078\u64c7\u662f\u4e0d\u662f\u5f88\u91cd\u8981\uff1f</p> <p>\u662f\u7684\uff0c\u69cb\u5efa\u6a39\u7684\u65b9\u6cd5\u5df2\u5728\u4e8c\u5143\u641c\u5c0b\u6a39\u7a0b\u5f0f\u78bc\u4e2d\u7684 <code>build_tree()</code> \u65b9\u6cd5\u4e2d\u7d66\u51fa\u3002\u81f3\u65bc\u6839\u7bc0\u9ede\u7684\u9078\u64c7\uff0c\u6211\u5011\u901a\u5e38\u6703\u5c07\u8f38\u5165\u8cc7\u6599\u6392\u5e8f\uff0c\u7136\u5f8c\u5c07\u4e2d\u9ede\u5143\u7d20\u4f5c\u70ba\u6839\u7bc0\u9ede\uff0c\u518d\u905e\u8ff4\u5730\u69cb\u5efa\u5de6\u53f3\u5b50\u6a39\u3002\u9019\u6a23\u505a\u53ef\u4ee5\u6700\u5927\u7a0b\u5ea6\u4fdd\u8b49\u6a39\u7684\u5e73\u8861\u6027\u3002</p> <p>Q\uff1a\u5728 Java \u4e2d\uff0c\u5b57\u4e32\u5c0d\u6bd4\u662f\u5426\u4e00\u5b9a\u8981\u7528 <code>equals()</code> \u65b9\u6cd5\uff1f</p> <p>\u5728 Java \u4e2d\uff0c\u5c0d\u65bc\u57fa\u672c\u8cc7\u6599\u578b\u5225\uff0c<code>==</code> \u7528\u65bc\u5c0d\u6bd4\u5169\u500b\u8b8a\u6578\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u5c0d\u65bc\u5f15\u7528\u578b\u5225\uff0c\u5169\u7a2e\u7b26\u865f\u7684\u5de5\u4f5c\u539f\u7406\u662f\u4e0d\u540c\u7684\u3002</p> <ul> <li><code>==</code> \uff1a\u7528\u4f86\u6bd4\u8f03\u5169\u500b\u8b8a\u6578\u662f\u5426\u6307\u5411\u540c\u4e00\u500b\u7269\u4ef6\uff0c\u5373\u5b83\u5011\u5728\u8a18\u61b6\u9ad4\u4e2d\u7684\u4f4d\u7f6e\u662f\u5426\u76f8\u540c\u3002</li> <li><code>equals()</code>\uff1a\u7528\u4f86\u5c0d\u6bd4\u5169\u500b\u7269\u4ef6\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002</li> </ul> <p>\u56e0\u6b64\uff0c\u5982\u679c\u8981\u5c0d\u6bd4\u503c\uff0c\u6211\u5011\u61c9\u8a72\u4f7f\u7528 <code>equals()</code> \u3002\u7136\u800c\uff0c\u900f\u904e <code>String a = \"hi\"; String b = \"hi\";</code> \u521d\u59cb\u5316\u7684\u5b57\u4e32\u90fd\u5132\u5b58\u5728\u5b57\u4e32\u5e38\u6578\u6c60\u4e2d\uff0c\u5b83\u5011\u6307\u5411\u540c\u4e00\u500b\u7269\u4ef6\uff0c\u56e0\u6b64\u4e5f\u53ef\u4ee5\u7528 <code>a == b</code> \u4f86\u6bd4\u8f03\u5169\u500b\u5b57\u4e32\u7684\u5167\u5bb9\u3002</p> <p>Q\uff1a\u5ee3\u5ea6\u512a\u5148\u8d70\u8a2a\u5230\u6700\u5e95\u5c64\u4e4b\u524d\uff0c\u4f47\u5217\u4e2d\u7684\u7bc0\u9ede\u6578\u91cf\u662f \\(2^h\\) \u55ce\uff1f</p> <p>\u662f\u7684\uff0c\u4f8b\u5982\u9ad8\u5ea6 \\(h = 2\\) \u7684\u6eff\u4e8c\u5143\u6a39\uff0c\u5176\u7bc0\u9ede\u7e3d\u6578 \\(n = 7\\) \uff0c\u5247\u5e95\u5c64\u7bc0\u9ede\u6578\u91cf \\(4 = 2^h = (n + 1) / 2\\) \u3002</p>"}]}