hello-algo/search/search_index.json
2023-08-19 19:21:30 +08:00

1 line
No EOL
2.1 MiB

{"config":{"lang":["en"],"separator":"[\\s\\u200b\\u3000\\-\u3001\u3002\uff0c\uff0e\uff1f\uff01\uff1b]+","pipeline":["stemmer"]},"docs":[{"location":"","title":"Home","text":"\u300a Hello \u7b97\u6cd5 \u300b <p> \u52a8\u753b\u56fe\u89e3\u3001\u4e00\u952e\u8fd0\u884c\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u6559\u7a0b </p> <p> </p> \u63a8\u8350\u8bed <p>Quote</p> <p>\u201c\u4e00\u672c\u901a\u4fd7\u6613\u61c2\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5165\u95e8\u4e66\uff0c\u5f15\u5bfc\u8bfb\u8005\u624b\u8111\u5e76\u7528\u5730\u5b66\u4e60\uff0c\u5f3a\u70c8\u63a8\u8350\u7b97\u6cd5\u521d\u5b66\u8005\u9605\u8bfb\u3002\u201d</p> <p>\u2014\u2014 \u9093\u4fca\u8f89\uff0c\u6e05\u534e\u5927\u5b66\u8ba1\u7b97\u673a\u7cfb\u6559\u6388</p> <p>Quote</p> <p>\u201c\u5982\u679c\u6211\u5f53\u5e74\u5b66\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u65f6\u5019\u6709\u300aHello \u7b97\u6cd5\u300b\uff0c\u5b66\u8d77\u6765\u5e94\u8be5\u4f1a\u7b80\u5355 10 \u500d\uff01\u201d</p> <p>\u2014\u2014 \u674e\u6c90\uff0c\u4e9a\u9a6c\u900a\u8d44\u6df1\u9996\u5e2d\u79d1\u5b66\u5bb6</p> \u5168\u4e66\u52a8\u753b\u56fe\u89e3 <p> \u5185\u5bb9\u6e05\u6670\u6613\u61c2\u3001\u5b66\u4e60\u66f2\u7ebf\u5e73\u6ed1\u7535\u8111\u3001\u5e73\u677f\u3001\u624b\u673a\u5168\u7ec8\u7aef\u9605\u8bfb </p> <p></p> <p><p> \"A picture is worth a thousand words.\" </p> <p> \u201c\u4e00\u56fe\u80dc\u5343\u8a00\u201d </p></p> \u4ee3\u7801\u4e00\u952e\u8fd0\u884c <p> \u63d0\u4f9b\u5404\u4e2a\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u7684\u7b80\u6d01\u5b9e\u73b0\u4e0e\u6d4b\u8bd5\u6837\u4f8b\uff0c\u7686\u53ef\u76f4\u63a5\u8fd0\u884c\u652f\u6301 Java, C++, Python, Go, JS, TS, C#, Swift, Zig \u7b49\u8bed\u8a00 </p> <p></p> <p><p> \"Talk is cheap. Show me the code.\" </p> <p> \u201c\u5c11\u5439\u725b\uff0c\u770b\u4ee3\u7801\u201d </p></p> \u53ef\u8ba8\u8bba\u4e0e\u63d0\u95ee <p> \u9f13\u52b1\u5c0f\u4f19\u4f34\u4eec\u4e92\u5e2e\u4e92\u52a9\u3001\u5171\u540c\u6210\u957f\u63d0\u95ee\u4e0e\u8bc4\u8bba\u4e00\u822c\u80fd\u5728\u4e24\u65e5\u5185\u5f97\u5230\u56de\u590d </p> <p></p> <p><p> \u201c\u8ffd\u98ce\u8d76\u6708\u83ab\u505c\u7559\uff0c\u5e73\u829c\u5c3d\u5904\u662f\u6625\u5c71\u201d </p> <p> \u4e00\u8d77\u52a0\u6cb9\uff01 </p></p> \u5e8f <p>\u4e24\u5e74\u524d\uff0c\u6211\u5728\u529b\u6263\u4e0a\u5206\u4eab\u4e86\u300a\u5251\u6307 Offer\u300b\u7cfb\u5217\u9898\u89e3\uff0c\u53d7\u5230\u4e86\u8bb8\u591a\u540c\u5b66\u7684\u559c\u7231\u548c\u652f\u6301\u3002\u5728\u4e0e\u8bfb\u8005\u7684\u4ea4\u6d41\u671f\u95f4\uff0c\u6700\u5e38\u6536\u5230\u7684\u4e00\u4e2a\u95ee\u9898\u662f\u201c\u5982\u4f55\u5165\u95e8\u5b66\u4e60\u7b97\u6cd5\u201d\u3002\u9010\u6e10\u5730\uff0c\u6211\u5bf9\u8fd9\u4e2a\u95ee\u9898\u4ea7\u751f\u4e86\u6d53\u539a\u7684\u5174\u8da3\u3002</p> <p>\u4e24\u773c\u4e00\u62b9\u9ed1\u5730\u5237\u9898\u4f3c\u4e4e\u662f\u6700\u53d7\u6b22\u8fce\u7684\u65b9\u6cd5\uff0c\u7b80\u5355\u76f4\u63a5\u4e14\u6709\u6548\u3002\u7136\u800c\u5237\u9898\u5c31\u5982\u540c\u73a9\u201c\u626b\u96f7\u201d\u6e38\u620f\uff0c\u81ea\u5b66\u80fd\u529b\u5f3a\u7684\u540c\u5b66\u80fd\u591f\u987a\u5229\u5730\u5c06\u5730\u96f7\u9010\u4e2a\u6392\u6389\uff0c\u800c\u57fa\u7840\u4e0d\u8db3\u7684\u540c\u5b66\u5f88\u53ef\u80fd\u88ab\u70b8\u7684\u6ee1\u5934\u662f\u5305\uff0c\u5e76\u5728\u632b\u6298\u4e2d\u6b65\u6b65\u9000\u7f29\u3002\u901a\u8bfb\u6559\u6750\u4e66\u7c4d\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u505a\u6cd5\uff0c\u4f46\u5bf9\u4e8e\u9762\u5411\u6c42\u804c\u7684\u540c\u5b66\u6765\u8bf4\uff0c\u6bd5\u4e1a\u5b63\u3001\u6295\u9012\u7b80\u5386\u3001\u51c6\u5907\u7b14\u8bd5\u9762\u8bd5\u5df2\u7ecf\u5360\u636e\u4e86\u5927\u90e8\u5206\u7cbe\u529b\uff0c\u539a\u91cd\u7684\u4e66\u7c4d\u5f80\u5f80\u53d8\u6210\u4e86\u4e00\u9879\u8270\u5de8\u7684\u6311\u6218\u3002</p> <p>\u5982\u679c\u4f60\u4e5f\u9762\u4e34\u7c7b\u4f3c\u7684\u56f0\u6270\uff0c\u90a3\u4e48\u5f88\u5e78\u8fd0\u8fd9\u672c\u4e66\u627e\u5230\u4e86\u4f60\u3002\u672c\u4e66\u662f\u6211\u5bf9\u6b64\u95ee\u9898\u7684\u7ed9\u51fa\u7684\u7b54\u6848\uff0c\u5373\u4f7f\u4e0d\u662f\u6700\u4f18\u89e3\uff0c\u4e5f\u81f3\u5c11\u662f\u4e00\u6b21\u79ef\u6781\u7684\u5c1d\u8bd5\u3002\u8fd9\u672c\u4e66\u867d\u7136\u4e0d\u8db3\u4ee5\u8ba9\u4f60\u76f4\u63a5\u62ff\u5230 Offer \uff0c\u4f46\u4f1a\u5f15\u5bfc\u4f60\u63a2\u7d22\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u201c\u77e5\u8bc6\u5730\u56fe\u201d\uff0c\u5e26\u4f60\u4e86\u89e3\u4e0d\u540c\u201c\u5730\u96f7\u201d\u7684\u5f62\u72b6\u5927\u5c0f\u548c\u5206\u5e03\u4f4d\u7f6e\uff0c\u8ba9\u4f60\u638c\u63e1\u5404\u79cd\u201c\u6392\u96f7\u65b9\u6cd5\u201d\u3002\u6709\u4e86\u8fd9\u4e9b\u672c\u9886\uff0c\u76f8\u4fe1\u4f60\u53ef\u4ee5\u66f4\u52a0\u81ea\u5982\u5730\u5e94\u5bf9\u5237\u9898\u548c\u9605\u8bfb\u6587\u732e\uff0c\u9010\u6b65\u6784\u5efa\u8d77\u5b8c\u6574\u7684\u77e5\u8bc6\u4f53\u7cfb\u3002</p> \u4f5c\u8005\u7b80\u4ecb <p>\u9773\u5b87\u680b (Krahets)\uff0c\u5927\u5382\u9ad8\u7ea7\u7b97\u6cd5\u5de5\u7a0b\u5e08\uff0c\u4e0a\u6d77\u4ea4\u901a\u5927\u5b66\u7855\u58eb\u3002\u529b\u6263\uff08LeetCode\uff09\u5168\u7f51\u9605\u8bfb\u91cf\u6700\u9ad8\u535a\u4e3b\uff0c\u5176 LeetBook\u300a\u56fe\u89e3\u7b97\u6cd5\u6570\u636e\u7ed3\u6784\u300b\u5df2\u88ab\u8ba2\u9605 24 \u4e07\u672c\u3002</p> \u81f4\u8c22 <p>\u672c\u4e66\u5728\u5f00\u6e90\u793e\u533a\u4f17\u591a\u8d21\u732e\u8005\u7684\u5171\u540c\u52aa\u529b\u4e0b\u4e0d\u65ad\u6210\u957f\u3002\u611f\u8c22\u6bcf\u4e00\u4f4d\u6295\u5165\u65f6\u95f4\u4e0e\u7cbe\u529b\u7684\u64b0\u7a3f\u4eba\uff0c\u662f\u4ed6\u4eec\u7684\u65e0\u79c1\u5949\u732e\u4f7f\u8fd9\u672c\u4e66\u53d8\u5f97\u66f4\u597d\uff0c\u4ed6\u4eec\u662f\uff08\u6309\u7167 GitHub \u81ea\u52a8\u751f\u6210\u7684\u987a\u5e8f\uff09\uff1a</p> <p> </p> <p>\u672c\u4e66\u7684\u4ee3\u7801\u5ba1\u9605\u5de5\u4f5c\u7531 Gonglja, gvenusleo, justin\u2010tse, krahets, nuomi1, Reanon, sjinzh \u5b8c\u6210\uff08\u6309\u7167\u9996\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\uff09\u3002\u611f\u8c22\u4ed6\u4eec\u4ed8\u51fa\u7684\u65f6\u95f4\u4e0e\u7cbe\u529b\uff0c\u6b63\u662f\u4ed6\u4eec\u786e\u4fdd\u4e86\u5404\u8bed\u8a00\u4ee3\u7801\u7684\u89c4\u8303\u4e0e\u7edf\u4e00\u3002</p> <sub>Gonglja</sub><sub>C / C++</sub> <sub>gvenusleo</sub><sub>Dart</sub> <sub>hpstory</sub><sub>C#</sub> <sub>justin-tse</sub><sub>JS / TS</sub> <sub>krahets</sub><sub>Java / Python</sub> <sub>nuomi1</sub><sub>Swift</sub> <sub>Reanon</sub><sub>Go / C</sub> <sub>sjinzh</sub><sub>Rust / Zig</sub>"},{"location":"chapter_appendix/","title":"16. \u00a0 \u9644\u5f55","text":""},{"location":"chapter_appendix/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>16.1 \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5</li> <li>16.2 \u00a0 \u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c</li> </ul>"},{"location":"chapter_appendix/contribution/","title":"16.2. \u00a0 \u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c","text":"<p>\u7531\u4e8e\u4f5c\u8005\u80fd\u529b\u6709\u9650\uff0c\u4e66\u4e2d\u96be\u514d\u5b58\u5728\u4e00\u4e9b\u9057\u6f0f\u548c\u9519\u8bef\uff0c\u8bf7\u60a8\u8c05\u89e3\u3002\u5982\u679c\u60a8\u53d1\u73b0\u4e86\u7b14\u8bef\u3001\u5931\u6548\u94fe\u63a5\u3001\u5185\u5bb9\u7f3a\u5931\u3001\u6587\u5b57\u6b67\u4e49\u3001\u89e3\u91ca\u4e0d\u6e05\u6670\u6216\u884c\u6587\u7ed3\u6784\u4e0d\u5408\u7406\u7b49\u95ee\u9898\uff0c\u8bf7\u534f\u52a9\u6211\u4eec\u8fdb\u884c\u4fee\u6b63\uff0c\u4ee5\u5e2e\u52a9\u5176\u4ed6\u8bfb\u8005\u83b7\u5f97\u66f4\u4f18\u8d28\u7684\u5b66\u4e60\u8d44\u6e90\u3002</p> <p>\u6240\u6709\u64b0\u7a3f\u4eba\u7684 GitHub ID \u5c06\u5728\u4ed3\u5e93\u3001\u7f51\u9875\u7248\u548c PDF \u7248\u7684\u4e3b\u9875\u4e0a\u8fdb\u884c\u5c55\u793a\uff0c\u4ee5\u611f\u8c22\u4ed6\u4eec\u5bf9\u5f00\u6e90\u793e\u533a\u7684\u65e0\u79c1\u5949\u732e\u3002</p> <p>\u5f00\u6e90\u7684\u9b45\u529b</p> <p>\u7eb8\u8d28\u4e66\u7c4d\u7684\u4e24\u6b21\u5370\u5237\u7684\u95f4\u9694\u65f6\u95f4\u5f80\u5f80\u9700\u8981\u6570\u5e74\uff0c\u5185\u5bb9\u66f4\u65b0\u975e\u5e38\u4e0d\u65b9\u4fbf\u3002</p> <p>\u7136\u800c\u5728\u672c\u5f00\u6e90\u4e66\u4e2d\uff0c\u5185\u5bb9\u66f4\u8fed\u7684\u65f6\u95f4\u88ab\u7f29\u77ed\u81f3\u6570\u65e5\u751a\u81f3\u51e0\u4e2a\u5c0f\u65f6\u3002</p>"},{"location":"chapter_appendix/contribution/#1621","title":"16.2.1. \u00a0 \u5185\u5bb9\u5fae\u8c03","text":"<p>\u5728\u6bcf\u4e2a\u9875\u9762\u7684\u53f3\u4e0a\u89d2\u6709\u4e00\u4e2a\u300c\u7f16\u8f91\u300d\u56fe\u6807\uff0c\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u4fee\u6539\u6587\u672c\u6216\u4ee3\u7801\uff1a</p> <ol> <li>\u70b9\u51fb\u7f16\u8f91\u6309\u94ae\uff0c\u5982\u679c\u9047\u5230\u201c\u9700\u8981 Fork \u6b64\u4ed3\u5e93\u201d\u7684\u63d0\u793a\uff0c\u8bf7\u540c\u610f\u8be5\u64cd\u4f5c\u3002</li> <li>\u4fee\u6539 Markdown \u6e90\u6587\u4ef6\u5185\u5bb9\uff0c\u68c0\u67e5\u5185\u5bb9\u7684\u6b63\u786e\u6027\uff0c\u5e76\u5c3d\u91cf\u4fdd\u6301\u6392\u7248\u683c\u5f0f\u7684\u7edf\u4e00\u3002</li> <li>\u5728\u9875\u9762\u5e95\u90e8\u586b\u5199\u4fee\u6539\u8bf4\u660e\uff0c\u7136\u540e\u70b9\u51fb\u201cPropose file change\u201d\u6309\u94ae\u3002\u9875\u9762\u8df3\u8f6c\u540e\uff0c\u70b9\u51fb\u201cCreate pull request\u201d\u6309\u94ae\u5373\u53ef\u53d1\u8d77\u62c9\u53d6\u8bf7\u6c42\u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u9875\u9762\u7f16\u8f91\u6309\u952e </p> <p>\u56fe\u7247\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\uff0c\u9700\u8981\u901a\u8fc7\u65b0\u5efa Issue \u6216\u8bc4\u8bba\u7559\u8a00\u6765\u63cf\u8ff0\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5c3d\u5feb\u91cd\u65b0\u7ed8\u5236\u5e76\u66ff\u6362\u56fe\u7247\u3002</p>"},{"location":"chapter_appendix/contribution/#1622","title":"16.2.2. \u00a0 \u5185\u5bb9\u521b\u4f5c","text":"<p>\u5982\u679c\u60a8\u6709\u5174\u8da3\u53c2\u4e0e\u6b64\u5f00\u6e90\u9879\u76ee\uff0c\u5305\u62ec\u5c06\u4ee3\u7801\u7ffb\u8bd1\u6210\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u3001\u6269\u5c55\u6587\u7ae0\u5185\u5bb9\u7b49\uff0c\u90a3\u4e48\u9700\u8981\u5b9e\u65bd Pull Request \u5de5\u4f5c\u6d41\u7a0b\uff1a</p> <ol> <li>\u767b\u5f55 GitHub \uff0c\u5c06\u672c\u4ed3\u5e93 Fork \u5230\u4e2a\u4eba\u8d26\u53f7\u4e0b\u3002</li> <li>\u8fdb\u5165\u60a8\u7684 Fork \u4ed3\u5e93\u7f51\u9875\uff0c\u4f7f\u7528 <code>git clone</code> \u547d\u4ee4\u5c06\u4ed3\u5e93\u514b\u9686\u81f3\u672c\u5730\u3002</li> <li>\u5728\u672c\u5730\u8fdb\u884c\u5185\u5bb9\u521b\u4f5c\uff0c\u5e76\u8fdb\u884c\u5b8c\u6574\u6d4b\u8bd5\uff0c\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\u3002</li> <li>\u5c06\u672c\u5730\u6240\u505a\u66f4\u6539 Commit \uff0c\u7136\u540e Push \u81f3\u8fdc\u7a0b\u4ed3\u5e93\u3002</li> <li>\u5237\u65b0\u4ed3\u5e93\u7f51\u9875\uff0c\u70b9\u51fb\u201cCreate pull request\u201d\u6309\u94ae\u5373\u53ef\u53d1\u8d77\u62c9\u53d6\u8bf7\u6c42\u3002</li> </ol>"},{"location":"chapter_appendix/contribution/#1623-docker","title":"16.2.3. \u00a0 Docker \u90e8\u7f72","text":"<p>\u6267\u884c\u4ee5\u4e0b Docker \u811a\u672c\uff0c\u7a0d\u7b49\u7247\u523b\uff0c\u5373\u53ef\u5728\u7f51\u9875 <code>http://localhost:8000</code> \u8bbf\u95ee\u672c\u9879\u76ee\u3002</p> <pre><code>git clone https://github.com/krahets/hello-algo.git\ncd hello-algo\ndocker-compose up -d\n</code></pre> <p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\u5220\u9664\u90e8\u7f72\u3002</p> <pre><code>docker-compose down\n</code></pre>"},{"location":"chapter_appendix/installation/","title":"16.1. \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5","text":""},{"location":"chapter_appendix/installation/#1611-vscode","title":"16.1.1. \u00a0 VSCode","text":"<p>\u672c\u4e66\u63a8\u8350\u4f7f\u7528\u5f00\u6e90\u8f7b\u91cf\u7684 VSCode \u4f5c\u4e3a\u672c\u5730 IDE \uff0c\u4e0b\u8f7d\u5e76\u5b89\u88c5 VSCode \u3002</p>"},{"location":"chapter_appendix/installation/#1612-java","title":"16.1.2. \u00a0 Java \u73af\u5883","text":"<ol> <li>\u4e0b\u8f7d\u5e76\u5b89\u88c5 OpenJDK\uff08\u7248\u672c\u9700\u6ee1\u8db3 &gt; JDK 9\uff09\u3002</li> <li>\u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 <code>java</code> \uff0c\u5b89\u88c5 Extension Pack for Java \u3002</li> </ol>"},{"location":"chapter_appendix/installation/#1613-cc","title":"16.1.3. \u00a0 C/C++ \u73af\u5883","text":"<ol> <li>Windows \u7cfb\u7edf\u9700\u8981\u5b89\u88c5 MinGW\uff08\u914d\u7f6e\u6559\u7a0b\uff09\uff0cMacOS \u81ea\u5e26 Clang \u65e0\u9700\u5b89\u88c5\u3002</li> <li>\u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 <code>c++</code> \uff0c\u5b89\u88c5 C/C++ Extension Pack \u3002</li> <li>\uff08\u53ef\u9009\uff09\u6253\u5f00 Settings \u9875\u9762\uff0c\u641c\u7d22 <code>Clang_format_fallback Style</code> \u4ee3\u7801\u683c\u5f0f\u5316\u9009\u9879\uff0c\u8bbe\u7f6e\u4e3a <code>{ BasedOnStyle: Microsoft, BreakBeforeBraces: Attach }</code> \u3002</li> </ol>"},{"location":"chapter_appendix/installation/#1614-python","title":"16.1.4. \u00a0 Python \u73af\u5883","text":"<ol> <li>\u4e0b\u8f7d\u5e76\u5b89\u88c5 Miniconda3 \u3002</li> <li>\u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 <code>python</code> \uff0c\u5b89\u88c5 Python Extension Pack \u3002</li> <li>\uff08\u53ef\u9009\uff09\u5728\u547d\u4ee4\u884c\u8f93\u5165 <code>pip install black</code> \uff0c\u5b89\u88c5\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002</li> </ol>"},{"location":"chapter_appendix/installation/#1615-go","title":"16.1.5. \u00a0 Go \u73af\u5883","text":"<ol> <li>\u4e0b\u8f7d\u5e76\u5b89\u88c5 go \u3002</li> <li>\u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 <code>go</code> \uff0c\u5b89\u88c5 Go \u3002</li> <li>\u5feb\u6377\u952e <code>Ctrl + Shift + P</code> \u547c\u51fa\u547d\u4ee4\u680f\uff0c\u8f93\u5165 go \uff0c\u9009\u62e9 <code>Go: Install/Update Tools</code> \uff0c\u5168\u90e8\u52fe\u9009\u5e76\u5b89\u88c5\u5373\u53ef\u3002</li> </ol>"},{"location":"chapter_appendix/installation/#1616-javascript","title":"16.1.6. \u00a0 JavaScript \u73af\u5883","text":"<ol> <li>\u4e0b\u8f7d\u5e76\u5b89\u88c5 node.js \u3002</li> <li>\u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 <code>javascript</code> \uff0c\u5b89\u88c5 JavaScript (ES6) code snippets \u3002</li> <li>\uff08\u53ef\u9009\uff09\u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 <code>Prettier</code> \uff0c\u5b89\u88c5\u4ee3\u7801\u683c\u5f0f\u5316\u5de5\u5177\u3002</li> </ol>"},{"location":"chapter_appendix/installation/#1617-c","title":"16.1.7. \u00a0 C# \u73af\u5883","text":"<ol> <li>\u4e0b\u8f7d\u5e76\u5b89\u88c5 .Net 6.0 \u3002</li> <li>\u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 <code>C# Dev Kit</code> \uff0c\u5b89\u88c5 C# Dev Kit \uff08\u914d\u7f6e\u6559\u7a0b\uff09\u3002</li> <li>\u4e5f\u53ef\u4f7f\u7528 Visual Studio\uff08\u5b89\u88c5\u6559\u7a0b\uff09\u3002</li> </ol>"},{"location":"chapter_appendix/installation/#1618-swift","title":"16.1.8. \u00a0 Swift \u73af\u5883","text":"<ol> <li>\u4e0b\u8f7d\u5e76\u5b89\u88c5 Swift\u3002</li> <li>\u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 <code>swift</code> \uff0c\u5b89\u88c5 Swift for Visual Studio Code\u3002</li> </ol>"},{"location":"chapter_appendix/installation/#1619-dart","title":"16.1.9. \u00a0 Dart \u73af\u5883","text":"<ol> <li>\u4e0b\u8f7d\u5e76\u5b89\u88c5 Dart \u3002</li> <li>\u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 <code>dart</code> \uff0c\u5b89\u88c5 Dart \u3002</li> </ol>"},{"location":"chapter_appendix/installation/#16110-rust","title":"16.1.10. \u00a0 Rust \u73af\u5883","text":"<ol> <li>\u4e0b\u8f7d\u5e76\u5b89\u88c5 Rust\u3002</li> <li>\u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 <code>rust</code> \uff0c\u5b89\u88c5 rust-analyzer\u3002</li> </ol>"},{"location":"chapter_array_and_linkedlist/","title":"4. \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868","text":"<p>Abstract</p> <p>\u6570\u636e\u7ed3\u6784\u7684\u4e16\u754c\u5982\u540c\u4e00\u7779\u539a\u5b9e\u7684\u7816\u5899\u3002</p> <p>\u6570\u7ec4\u7684\u7816\u5757\u6574\u9f50\u6392\u5217\uff0c\u9010\u4e2a\u7d27\u8d34\u3002\u94fe\u8868\u7684\u7816\u5757\u5206\u6563\u5404\u5904\uff0c\u8fde\u63a5\u7684\u85e4\u8513\u81ea\u7531\u5730\u7a7f\u68ad\u4e8e\u7816\u7f1d\u4e4b\u95f4\u3002</p>"},{"location":"chapter_array_and_linkedlist/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>4.1 \u00a0 \u6570\u7ec4</li> <li>4.2 \u00a0 \u94fe\u8868</li> <li>4.3 \u00a0 \u5217\u8868</li> <li>4.4 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_array_and_linkedlist/array/","title":"4.1. \u00a0 \u6570\u7ec4","text":"<p>\u300c\u6570\u7ec4 Array\u300d\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u5c06\u76f8\u540c\u7c7b\u578b\u5143\u7d20\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\u3002\u6211\u4eec\u5c06\u67d0\u4e2a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u79f0\u4e3a\u8be5\u5143\u7d20\u7684\u300c\u7d22\u5f15 Index\u300d\u3002</p> <p></p> <p> \u56fe\uff1a\u6570\u7ec4\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f </p>"},{"location":"chapter_array_and_linkedlist/array/#411","title":"4.1.1. \u00a0 \u6570\u7ec4\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/array/#_1","title":"\u521d\u59cb\u5316\u6570\u7ec4","text":"<p>\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u7528\u6570\u7ec4\u7684\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\uff1a\u65e0\u521d\u59cb\u503c\u3001\u7ed9\u5b9a\u521d\u59cb\u503c\u3002\u5728\u672a\u6307\u5b9a\u521d\u59cb\u503c\u7684\u60c5\u51b5\u4e0b\uff0c\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4f1a\u5c06\u6570\u7ec4\u5143\u7d20\u521d\u59cb\u5316\u4e3a \\(0\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust array.java<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n</code></pre> array.cpp<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\n// \u5b58\u50a8\u5728\u6808\u4e0a\nint arr[5];\nint nums[5] { 1, 3, 2, 5, 4 };\n// \u5b58\u50a8\u5728\u5806\u4e0a\uff08\u9700\u8981\u624b\u52a8\u91ca\u653e\u7a7a\u95f4\uff09\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n</code></pre> array.py<pre><code># \u521d\u59cb\u5316\u6570\u7ec4\narr: list[int] = [0] * 5 # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4] \n</code></pre> array.go<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr [5]int\n// \u5728 Go \u4e2d\uff0c\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[5]int\uff09\u4e3a\u6570\u7ec4\uff0c\u4e0d\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[]int\uff09\u4e3a\u5207\u7247\n// \u7531\u4e8e Go \u7684\u6570\u7ec4\u88ab\u8bbe\u8ba1\u4e3a\u5728\u7f16\u8bd1\u671f\u786e\u5b9a\u957f\u5ea6\uff0c\u56e0\u6b64\u53ea\u80fd\u4f7f\u7528\u5e38\u91cf\u6765\u6307\u5b9a\u957f\u5ea6\n// \u4e3a\u4e86\u65b9\u4fbf\u5b9e\u73b0\u6269\u5bb9 extend() \u65b9\u6cd5\uff0c\u4ee5\u4e0b\u5c06\u5207\u7247\uff08Slice\uff09\u770b\u4f5c\u6570\u7ec4\uff08Array\uff09\nnums := []int{1, 3, 2, 5, 4}\n</code></pre> array.js<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n</code></pre> array.ts<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n</code></pre> array.c<pre><code>int arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n</code></pre> array.cs<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n</code></pre> array.swift<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n</code></pre> array.zig<pre><code>// \u521d\u59cb\u5316\u6570\u7ec4\nvar arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 }\nvar nums = [_]i32{ 1, 3, 2, 5, 4 };\n</code></pre> array.dart<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\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\u6570\u7ec4 */\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>"},{"location":"chapter_array_and_linkedlist/array/#_2","title":"\u8bbf\u95ee\u5143\u7d20","text":"<p>\u6570\u7ec4\u5143\u7d20\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u610f\u5473\u7740\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u975e\u5e38\u5bb9\u6613\u3002\u7ed9\u5b9a\u6570\u7ec4\u5185\u5b58\u5730\u5740\uff08\u5373\u9996\u5143\u7d20\u5185\u5b58\u5730\u5740\uff09\u548c\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u516c\u5f0f\u8ba1\u7b97\u5f97\u5230\u8be5\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u76f4\u63a5\u8bbf\u95ee\u6b64\u5143\u7d20\u3002</p> <pre><code># \u5143\u7d20\u5185\u5b58\u5730\u5740 = \u6570\u7ec4\u5185\u5b58\u5730\u5740\uff08\u9996\u5143\u7d20\u5185\u5b58\u5730\u5740\uff09 + \u5143\u7d20\u957f\u5ea6 * \u5143\u7d20\u7d22\u5f15\nelementAddr = firtstElementAddr + elementLength * elementIndex\n</code></pre> <p></p> <p> \u56fe\uff1a\u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u8ba1\u7b97 </p> <p>\u89c2\u5bdf\u4e0a\u56fe\uff0c\u6211\u4eec\u53d1\u73b0\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u4e3a \\(0\\) \uff0c\u8fd9\u4f3c\u4e4e\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u56e0\u4e3a\u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\u4f1a\u66f4\u81ea\u7136\u3002\u4f46\u4ece\u5730\u5740\u8ba1\u7b97\u516c\u5f0f\u7684\u89d2\u5ea6\u770b\uff0c\u7d22\u5f15\u7684\u542b\u4e49\u672c\u8d28\u4e0a\u662f\u5185\u5b58\u5730\u5740\u7684\u504f\u79fb\u91cf\u3002\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u504f\u79fb\u91cf\u662f \\(0\\) \uff0c\u56e0\u6b64\u5b83\u7684\u7d22\u5f15\u4e3a \\(0\\) \u4e5f\u662f\u5408\u7406\u7684\u3002</p> <p>\u5728\u6570\u7ec4\u4e2d\u8bbf\u95ee\u5143\u7d20\u662f\u975e\u5e38\u9ad8\u6548\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u968f\u673a\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u5143\u7d20\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust array.java<pre><code>/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int[] nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n</code></pre> array.cpp<pre><code>/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n// \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = rand() % size;\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n</code></pre> array.py<pre><code>def random_access(nums: list[int]) -&gt; int:\n\"\"\"\u968f\u673a\u8bbf\u95ee\u5143\u7d20\"\"\"\n# \u5728\u533a\u95f4 [0, len(nums)-1] \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nrandom_index = random.randint(0, len(nums) - 1)\n# \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nrandom_num = nums[random_index]\nreturn random_num\n</code></pre> array.go<pre><code>/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums []int) (randomNum int) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nrandomIndex := rand.Intn(len(nums))\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nrandomNum = nums[randomIndex]\nreturn\n}\n</code></pre> array.js<pre><code>/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nconst random_index = Math.floor(Math.random() * nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nconst random_num = nums[random_index];\nreturn random_num;\n}\n</code></pre> array.ts<pre><code>/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums: number[]): number {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nconst random_index = Math.floor(Math.random() * nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nconst random_num = nums[random_index];\nreturn random_num;\n}\n</code></pre> array.c<pre><code>/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n// \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = rand() % size;\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n</code></pre> array.cs<pre><code>/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int[] nums) {\nRandom random = new();\n// \u5728\u533a\u95f4 [0, nums.Length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = random.Next(nums.Length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n</code></pre> array.swift<pre><code>/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums: [Int]) -&gt; Int {\n// \u5728\u533a\u95f4 [0, nums.count) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nlet randomIndex = nums.indices.randomElement()!\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nlet randomNum = nums[randomIndex]\nreturn randomNum\n}\n</code></pre> array.zig<pre><code>// \u968f\u673a\u8bbf\u95ee\u5143\u7d20\nfn randomAccess(nums: []i32) i32 {\n// \u5728\u533a\u95f4 [0, nums.len) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6574\u6570\nvar randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nvar randomNum = nums[randomIndex];\nreturn randomNum;\n}\n</code></pre> array.dart<pre><code>/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(List nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = Random().nextInt(nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n</code></pre> array.rs<pre><code>/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfn random_access(nums: &amp;[i32]) -&gt; i32 {\n// \u5728\u533a\u95f4 [0, nums.len()) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nlet random_index = rand::thread_rng().gen_range(0..nums.len());\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nlet random_num = nums[random_index];\nrandom_num\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/array/#_3","title":"\u63d2\u5165\u5143\u7d20","text":"<p>\u6570\u7ec4\u5143\u7d20\u5728\u5185\u5b58\u4e2d\u662f\u201c\u7d27\u6328\u7740\u7684\u201d\uff0c\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u7a7a\u95f4\u518d\u5b58\u653e\u4efb\u4f55\u6570\u636e\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u60f3\u8981\u5728\u6570\u7ec4\u4e2d\u95f4\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u5219\u9700\u8981\u5c06\u8be5\u5143\u7d20\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u4e4b\u540e\u518d\u628a\u5143\u7d20\u8d4b\u503c\u7ed9\u8be5\u7d22\u5f15\u3002</p> <p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u56fa\u5b9a\u7684\uff0c\u56e0\u6b64\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u4f1a\u5bfc\u81f4\u6570\u7ec4\u5c3e\u90e8\u5143\u7d20\u7684\u201c\u4e22\u5931\u201d\u3002\u6211\u4eec\u5c06\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u7559\u5728\u5217\u8868\u7ae0\u8282\u4e2d\u8ba8\u8bba\u3002</p> <p></p> <p> \u56fe\uff1a\u6570\u7ec4\u63d2\u5165\u5143\u7d20 </p> JavaC++PythonGoJSTSCC#SwiftZigDartRust array.java<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int[] nums, int num, int index) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int i = nums.length - 1; i &gt; index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n</code></pre> array.cpp<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int i = size - 1; i &gt; index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n</code></pre> array.py<pre><code>def insert(nums: list[int], num: int, index: int):\n\"\"\"\u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num\"\"\"\n# \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor i in range(len(nums) - 1, index, -1):\nnums[i] = nums[i - 1]\n# \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num\n</code></pre> array.go<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums []int, num int, index int) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor i := len(nums) - 1; i &gt; index; i-- {\nnums[i] = nums[i-1]\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num\n}\n</code></pre> array.js<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums, num, index) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (let i = nums.length - 1; i &gt; index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n</code></pre> array.ts<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums: number[], num: number, index: number): void {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (let i = nums.length - 1; i &gt; index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n</code></pre> array.c<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int i = size - 1; i &gt; index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n</code></pre> array.cs<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int[] nums, int num, int index) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int i = nums.Length - 1; i &gt; index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n</code></pre> array.swift<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums: inout [Int], num: Int, index: Int) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor i in sequence(first: nums.count - 1, next: { $0 &gt; index + 1 ? $0 - 1 : nil }) {\nnums[i] = nums[i - 1]\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num\n}\n</code></pre> array.zig<pre><code>// \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num\nfn insert(nums: []i32, num: i32, index: usize) void {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nvar i = nums.len - 1;\nwhile (i &gt; index) : (i -= 1) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n</code></pre> array.dart<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(List nums, int num, int index) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (var i = nums.length - 1; i &gt; index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n</code></pre> array.rs<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\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\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor i in (index + 1..nums.len()).rev() {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/array/#_4","title":"\u5220\u9664\u5143\u7d20","text":"<p>\u540c\u7406\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u5220\u9664\u7d22\u5f15 \\(i\\) \u5904\u7684\u5143\u7d20\uff0c\u5219\u9700\u8981\u628a\u7d22\u5f15 \\(i\\) \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\u3002</p> <p>\u8bf7\u6ce8\u610f\uff0c\u5220\u9664\u5143\u7d20\u5b8c\u6210\u540e\uff0c\u539f\u5148\u672b\u5c3e\u7684\u5143\u7d20\u53d8\u5f97\u201c\u65e0\u610f\u4e49\u201d\u4e86\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u9700\u7279\u610f\u53bb\u4fee\u6539\u5b83\u3002</p> <p></p> <p> \u56fe\uff1a\u6570\u7ec4\u5220\u9664\u5143\u7d20 </p> JavaC++PythonGoJSTSCC#SwiftZigDartRust array.java<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nvoid remove(int[] nums, int index) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int i = index; i &lt; nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> array.cpp<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nvoid remove(int *nums, int size, int index) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int i = index; i &lt; size - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> array.py<pre><code>def remove(nums: list[int], index: int):\n\"\"\"\u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20\"\"\"\n# \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor i in range(index, len(nums) - 1):\nnums[i] = nums[i + 1]\n</code></pre> array.go<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfunc remove(nums []int, index int) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor i := index; i &lt; len(nums)-1; i++ {\nnums[i] = nums[i+1]\n}\n}\n</code></pre> array.js<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfunction remove(nums, index) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (let i = index; i &lt; nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> array.ts<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfunction remove(nums: number[], index: number): void {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (let i = index; i &lt; nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> array.c<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(int *nums, int size, int index) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int i = index; i &lt; size - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> array.cs<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nvoid remove(int[] nums, int index) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int i = index; i &lt; nums.Length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> array.swift<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfunc remove(nums: inout [Int], index: Int) {\nlet count = nums.count\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor i in sequence(first: index, next: { $0 &lt; count - 1 - 1 ? $0 + 1 : nil }) {\nnums[i] = nums[i + 1]\n}\n}\n</code></pre> array.zig<pre><code>// \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20\nfn remove(nums: []i32, index: usize) void {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nvar i = index;\nwhile (i &lt; nums.len - 1) : (i += 1) {\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> array.dart<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nvoid remove(List nums, int index) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (var i = index; i &lt; nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> array.rs<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfn remove(nums: &amp;mut Vec&lt;i32&gt;, index: usize) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor i in index..nums.len() - 1 {\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> <p>\u603b\u7684\u6765\u770b\uff0c\u6570\u7ec4\u7684\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u6709\u4ee5\u4e0b\u7f3a\u70b9\uff1a</p> <ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6\u9ad8\uff1a\u6570\u7ec4\u7684\u63d2\u5165\u548c\u5220\u9664\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u957f\u5ea6\u3002</li> <li>\u4e22\u5931\u5143\u7d20\uff1a\u7531\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u5728\u63d2\u5165\u5143\u7d20\u540e\uff0c\u8d85\u51fa\u6570\u7ec4\u957f\u5ea6\u8303\u56f4\u7684\u5143\u7d20\u4f1a\u4e22\u5931\u3002</li> <li>\u5185\u5b58\u6d6a\u8d39\uff1a\u6211\u4eec\u53ef\u4ee5\u521d\u59cb\u5316\u4e00\u4e2a\u6bd4\u8f83\u957f\u7684\u6570\u7ec4\uff0c\u53ea\u7528\u524d\u9762\u4e00\u90e8\u5206\uff0c\u8fd9\u6837\u5728\u63d2\u5165\u6570\u636e\u65f6\uff0c\u4e22\u5931\u7684\u672b\u5c3e\u5143\u7d20\u90fd\u662f\u201c\u65e0\u610f\u4e49\u201d\u7684\uff0c\u4f46\u8fd9\u6837\u505a\u4e5f\u4f1a\u9020\u6210\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002</li> </ul>"},{"location":"chapter_array_and_linkedlist/array/#_5","title":"\u904d\u5386\u6570\u7ec4","text":"<p>\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u65e2\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust array.java<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int[] nums) {\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i &lt; nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int num : nums) {\ncount++;\n}\n}\n</code></pre> array.cpp<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i &lt; size; i++) {\ncount++;\n}\n}\n</code></pre> array.py<pre><code>def traverse(nums: list[int]):\n\"\"\"\u904d\u5386\u6570\u7ec4\"\"\"\ncount = 0\n# \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor i in range(len(nums)):\ncount += 1\n# \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor num in nums:\ncount += 1\n# \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\nfor i, num in enumerate(nums):\ncount += 1\n</code></pre> array.go<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums []int) {\ncount := 0\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor i := 0; i &lt; len(nums); i++ {\ncount++\n}\ncount = 0\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor range nums {\ncount++\n}\n}\n</code></pre> array.js<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums) {\nlet count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (let i = 0; i &lt; nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (const num of nums) {\ncount += 1;\n}\n}\n</code></pre> array.ts<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums: number[]): void {\nlet count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (let i = 0; i &lt; nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (const num of nums) {\ncount += 1;\n}\n}\n</code></pre> array.c<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i &lt; size; i++) {\ncount++;\n}\n}\n</code></pre> array.cs<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int[] nums) {\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i &lt; nums.Length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nforeach (int num in nums) {\ncount++;\n}\n}\n</code></pre> array.swift<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums: [Int]) {\nvar count = 0\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor _ in nums.indices {\ncount += 1\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor _ in nums {\ncount += 1\n}\n}\n</code></pre> array.zig<pre><code>// \u904d\u5386\u6570\u7ec4\nfn traverse(nums: []i32) void {\nvar count: i32 = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nvar i: i32 = 0;\nwhile (i &lt; nums.len) : (i += 1) {\ncount += 1;\n}\ncount = 0;\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (nums) |_| {\ncount += 1;\n}\n}\n</code></pre> array.dart<pre><code>/* \u904d\u5386\u6570\u7ec4\u5143\u7d20 */\nvoid traverse(List nums) {\nvar count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (var i = 0; i &lt; nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (var num in nums) {\ncount++;\n}\n// \u901a\u8fc7 forEach \u65b9\u6cd5\u904d\u5386\u6570\u7ec4\nnums.forEach((element) {\ncount++;\n});\n}\n</code></pre> array.rs<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nfn traverse(nums: &amp;[i32]) {\nlet mut _count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor _ in 0..nums.len() {\n_count += 1;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor _ in nums {\n_count += 1;\n}\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/array/#_6","title":"\u67e5\u627e\u5143\u7d20","text":"<p>\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\u9700\u8981\u904d\u5386\u6570\u7ec4\uff0c\u6bcf\u8f6e\u5224\u65ad\u5143\u7d20\u503c\u662f\u5426\u5339\u914d\uff0c\u82e5\u5339\u914d\u5219\u8f93\u51fa\u5bf9\u5e94\u7d22\u5f15\u3002</p> <p>\u56e0\u4e3a\u6570\u7ec4\u662f\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u6240\u4ee5\u4e0a\u8ff0\u67e5\u627e\u64cd\u4f5c\u88ab\u79f0\u4e3a\u300c\u7ebf\u6027\u67e5\u627e\u300d\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust array.java<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target) {\nfor (int i = 0; i &lt; nums.length; i++) {\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n</code></pre> array.cpp<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\nfor (int i = 0; i &lt; size; i++) {\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n</code></pre> array.py<pre><code>def find(nums: list[int], target: int) -&gt; int:\n\"\"\"\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\"\"\"\nfor i in range(len(nums)):\nif nums[i] == target:\nreturn i\nreturn -1\n</code></pre> array.go<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums []int, target int) (index int) {\nindex = -1\nfor i := 0; i &lt; len(nums); i++ {\nif nums[i] == target {\nindex = i\nbreak\n}\n}\nreturn\n}\n</code></pre> array.js<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums, target) {\nfor (let i = 0; i &lt; nums.length; i++) {\nif (nums[i] === target) return i;\n}\nreturn -1;\n}\n</code></pre> array.ts<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums: number[], target: number): number {\nfor (let i = 0; i &lt; nums.length; i++) {\nif (nums[i] === target) {\nreturn i;\n}\n}\nreturn -1;\n}\n</code></pre> array.c<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\nfor (int i = 0; i &lt; size; i++) {\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n</code></pre> array.cs<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target) {\nfor (int i = 0; i &lt; nums.Length; i++) {\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n</code></pre> array.swift<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums: [Int], target: Int) -&gt; Int {\nfor i in nums.indices {\nif nums[i] == target {\nreturn i\n}\n}\nreturn -1\n}\n</code></pre> array.zig<pre><code>// \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\nfn find(nums: []i32, target: i32) i32 {\nfor (nums, 0..) |num, i| {\nif (num == target) return @intCast(i);\n}\nreturn -1;\n}\n</code></pre> array.dart<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(List nums, int target) {\nfor (var i = 0; i &lt; nums.length; i++) {\nif (nums[i] == target) return i;\n}\nreturn -1;\n}\n</code></pre> array.rs<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfn find(nums: &amp;[i32], target: i32) -&gt; Option&lt;usize&gt; {\nfor i in 0..nums.len() {\nif nums[i] == target {\nreturn Some(i);\n}\n}\nNone\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/array/#_7","title":"\u6269\u5bb9\u6570\u7ec4","text":"<p>\u5728\u590d\u6742\u7684\u7cfb\u7edf\u73af\u5883\u4e2d\uff0c\u7a0b\u5e8f\u96be\u4ee5\u4fdd\u8bc1\u6570\u7ec4\u4e4b\u540e\u7684\u5185\u5b58\u7a7a\u95f4\u662f\u53ef\u7528\u7684\uff0c\u4ece\u800c\u65e0\u6cd5\u5b89\u5168\u5730\u6269\u5c55\u6570\u7ec4\u5bb9\u91cf\u3002\u56e0\u6b64\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u6570\u7ec4\u7684\u957f\u5ea6\u662f\u4e0d\u53ef\u53d8\u7684\u3002</p> <p>\u5982\u679c\u6211\u4eec\u5e0c\u671b\u6269\u5bb9\u6570\u7ec4\uff0c\u5219\u9700\u91cd\u65b0\u5efa\u7acb\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u7136\u540e\u628a\u539f\u6570\u7ec4\u5143\u7d20\u4f9d\u6b21\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\u3002\u8fd9\u662f\u4e00\u4e2a \\(O(n)\\) \u7684\u64cd\u4f5c\uff0c\u5728\u6570\u7ec4\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\u662f\u975e\u5e38\u8017\u65f6\u7684\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust array.java<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] extend(int[] nums, int enlarge) {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nint[] res = new int[nums.length + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i &lt; nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n</code></pre> array.cpp<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nint *res = new int[size + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i &lt; size; i++) {\nres[i] = nums[i];\n}\n// \u91ca\u653e\u5185\u5b58\ndelete[] nums;\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n</code></pre> array.py<pre><code>def extend(nums: list[int], enlarge: int) -&gt; list[int]:\n\"\"\"\u6269\u5c55\u6570\u7ec4\u957f\u5ea6\"\"\"\n# \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nres = [0] * (len(nums) + enlarge)\n# \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor i in range(len(nums)):\nres[i] = nums[i]\n# \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res\n</code></pre> array.go<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums []int, enlarge int) []int {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nres := make([]int, len(nums)+enlarge)\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor i, num := range nums {\nres[i] = num\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res\n}\n</code></pre> array.js<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cJavaScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u662f\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums, enlarge) {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nconst res = new Array(nums.length + enlarge).fill(0);\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (let i = 0; i &lt; nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n</code></pre> array.ts<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cTypeScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u662f\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums: number[], enlarge: number): number[] {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nconst res = new Array(nums.length + enlarge).fill(0);\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (let i = 0; i &lt; nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n</code></pre> array.c<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nint *res = (int *)malloc(sizeof(int) * (size + enlarge));\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i &lt; size; i++) {\nres[i] = nums[i];\n}\n// \u521d\u59cb\u5316\u6269\u5c55\u540e\u7684\u7a7a\u95f4\nfor (int i = size; i &lt; size + enlarge; i++) {\nres[i] = 0;\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n</code></pre> array.cs<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] extend(int[] nums, int enlarge) {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nint[] res = new int[nums.Length + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i &lt; nums.Length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n</code></pre> array.swift<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums: [Int], enlarge: Int) -&gt; [Int] {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nvar res = Array(repeating: 0, count: nums.count + enlarge)\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor i in nums.indices {\nres[i] = nums[i]\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res\n}\n</code></pre> array.zig<pre><code>// \u6269\u5c55\u6570\u7ec4\u957f\u5ea6\nfn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]i32 {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nvar res = try mem_allocator.alloc(i32, nums.len + enlarge);\n@memset(res, 0);\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nstd.mem.copy(i32, res, nums);\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n</code></pre> array.dart<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nList extend(List nums, int enlarge) {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nList&lt;int&gt; res = List.filled(nums.length + enlarge, 0);\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (var i = 0; i &lt; nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n</code></pre> array.rs<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfn extend(nums: Vec&lt;i32&gt;, enlarge: usize) -&gt; Vec&lt;i32&gt; {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nlet mut res: Vec&lt;i32&gt; = vec![0; nums.len() + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\nfor i in 0..nums.len() {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nres\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/array/#412","title":"4.1.2. \u00a0 \u6570\u7ec4\u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"<p>\u6570\u7ec4\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u4e14\u5143\u7d20\u7c7b\u578b\u76f8\u540c\u3002\u8fd9\u5305\u542b\u4e30\u5bcc\u7684\u5148\u9a8c\u4fe1\u606f\uff0c\u7cfb\u7edf\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u4fe1\u606f\u6765\u4f18\u5316\u64cd\u4f5c\u548c\u8fd0\u884c\u6548\u7387\uff0c\u5305\u62ec\uff1a</p> <ul> <li>\u7a7a\u95f4\u6548\u7387\u9ad8: \u6570\u7ec4\u4e3a\u6570\u636e\u5206\u914d\u4e86\u8fde\u7eed\u7684\u5185\u5b58\u5757\uff0c\u65e0\u9700\u989d\u5916\u7684\u7ed3\u6784\u5f00\u9500\u3002</li> <li>\u652f\u6301\u968f\u673a\u8bbf\u95ee: \u6570\u7ec4\u5141\u8bb8\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u8bbf\u95ee\u4efb\u4f55\u5143\u7d20\u3002</li> <li>\u7f13\u5b58\u5c40\u90e8\u6027: \u5f53\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u65f6\uff0c\u8ba1\u7b97\u673a\u4e0d\u4ec5\u4f1a\u52a0\u8f7d\u5b83\uff0c\u8fd8\u4f1a\u7f13\u5b58\u5176\u5468\u56f4\u7684\u5176\u4ed6\u6570\u636e\uff0c\u4ece\u800c\u501f\u52a9\u9ad8\u901f\u7f13\u5b58\u6765\u63d0\u5347\u540e\u7eed\u64cd\u4f5c\u7684\u6267\u884c\u901f\u5ea6\u3002</li> </ul> <p>\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u662f\u4e00\u628a\u53cc\u5203\u5251\uff0c\u5b83\u5bfc\u81f4\u7684\u7f3a\u70b9\u6709\uff1a</p> <ul> <li>\u63d2\u5165\u4e0e\u5220\u9664\u6548\u7387\u4f4e:\u5f53\u6570\u7ec4\u4e2d\u5143\u7d20\u8f83\u591a\u65f6\uff0c\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u9700\u8981\u79fb\u52a8\u5927\u91cf\u7684\u5143\u7d20\u3002</li> <li>\u957f\u5ea6\u4e0d\u53ef\u53d8: \u6570\u7ec4\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u5c31\u56fa\u5b9a\u4e86\uff0c\u6269\u5bb9\u6570\u7ec4\u9700\u8981\u5c06\u6240\u6709\u6570\u636e\u590d\u5236\u5230\u65b0\u6570\u7ec4\uff0c\u5f00\u9500\u5f88\u5927\u3002</li> <li>\u7a7a\u95f4\u6d6a\u8d39: \u5982\u679c\u6570\u7ec4\u5206\u914d\u7684\u5927\u5c0f\u8d85\u8fc7\u4e86\u5b9e\u9645\u6240\u9700\uff0c\u90a3\u4e48\u591a\u4f59\u7684\u7a7a\u95f4\u5c31\u88ab\u6d6a\u8d39\u4e86\u3002</li> </ul>"},{"location":"chapter_array_and_linkedlist/array/#413","title":"4.1.3. \u00a0 \u6570\u7ec4\u5178\u578b\u5e94\u7528","text":"<p>\u6570\u7ec4\u662f\u4e00\u79cd\u57fa\u7840\u4e14\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\uff0c\u65e2\u9891\u7e41\u5e94\u7528\u5728\u5404\u7c7b\u7b97\u6cd5\u4e4b\u4e2d\uff0c\u4e5f\u53ef\u7528\u4e8e\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u6570\u636e\u7ed3\u6784\uff0c\u4e3b\u8981\u5305\u62ec\uff1a</p> <ul> <li>\u968f\u673a\u8bbf\u95ee\uff1a\u5982\u679c\u6211\u4eec\u60f3\u8981\u968f\u673a\u62bd\u53d6\u4e00\u4e9b\u6837\u672c\uff0c\u90a3\u4e48\u53ef\u4ee5\u7528\u6570\u7ec4\u5b58\u50a8\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5e8f\u5217\uff0c\u6839\u636e\u7d22\u5f15\u5b9e\u73b0\u6837\u672c\u7684\u968f\u673a\u62bd\u53d6\u3002</li> <li>\u6392\u5e8f\u548c\u641c\u7d22\uff1a\u6570\u7ec4\u662f\u6392\u5e8f\u548c\u641c\u7d22\u7b97\u6cd5\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u4e8c\u5206\u67e5\u627e\u7b49\u90fd\u4e3b\u8981\u5728\u6570\u7ec4\u4e0a\u8fdb\u884c\u3002</li> <li>\u67e5\u627e\u8868\uff1a\u5f53\u6211\u4eec\u9700\u8981\u5feb\u901f\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u6216\u8005\u9700\u8981\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u7684\u5bf9\u5e94\u5173\u7cfb\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u4f5c\u4e3a\u67e5\u627e\u8868\u3002\u5047\u5982\u6211\u4eec\u60f3\u8981\u5b9e\u73b0\u5b57\u7b26\u5230 ASCII \u7801\u7684\u6620\u5c04\uff0c\u5219\u53ef\u4ee5\u5c06\u5b57\u7b26\u7684 ASCII \u7801\u503c\u4f5c\u4e3a\u7d22\u5f15\uff0c\u5bf9\u5e94\u7684\u5143\u7d20\u5b58\u653e\u5728\u6570\u7ec4\u4e2d\u7684\u5bf9\u5e94\u4f4d\u7f6e\u3002</li> <li>\u673a\u5668\u5b66\u4e60\uff1a\u795e\u7ecf\u7f51\u7edc\u4e2d\u5927\u91cf\u4f7f\u7528\u4e86\u5411\u91cf\u3001\u77e9\u9635\u3001\u5f20\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\uff0c\u8fd9\u4e9b\u6570\u636e\u90fd\u662f\u4ee5\u6570\u7ec4\u7684\u5f62\u5f0f\u6784\u5efa\u7684\u3002\u6570\u7ec4\u662f\u795e\u7ecf\u7f51\u7edc\u7f16\u7a0b\u4e2d\u6700\u5e38\u4f7f\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002</li> <li>\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\uff1a\u6570\u7ec4\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u5806\u3001\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002</li> </ul>"},{"location":"chapter_array_and_linkedlist/linked_list/","title":"4.2. \u00a0 \u94fe\u8868","text":"<p>\u5185\u5b58\u7a7a\u95f4\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u516c\u5171\u8d44\u6e90\uff0c\u5728\u4e00\u4e2a\u590d\u6742\u7684\u7cfb\u7edf\u8fd0\u884c\u73af\u5883\u4e0b\uff0c\u7a7a\u95f2\u7684\u5185\u5b58\u7a7a\u95f4\u53ef\u80fd\u6563\u843d\u5728\u5185\u5b58\u5404\u5904\u3002\u6211\u4eec\u77e5\u9053\uff0c\u5b58\u50a8\u6570\u7ec4\u7684\u5185\u5b58\u7a7a\u95f4\u5fc5\u987b\u662f\u8fde\u7eed\u7684\uff0c\u800c\u5f53\u6570\u7ec4\u975e\u5e38\u5927\u65f6\uff0c\u5185\u5b58\u53ef\u80fd\u65e0\u6cd5\u63d0\u4f9b\u5982\u6b64\u5927\u7684\u8fde\u7eed\u7a7a\u95f4\u3002\u6b64\u65f6\u94fe\u8868\u7684\u7075\u6d3b\u6027\u4f18\u52bf\u5c31\u4f53\u73b0\u51fa\u6765\u4e86\u3002</p> <p>\u300c\u94fe\u8868 Linked List\u300d\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u5404\u4e2a\u8282\u70b9\u901a\u8fc7\u201c\u5f15\u7528\u201d\u76f8\u8fde\u63a5\u3002\u5f15\u7528\u8bb0\u5f55\u4e86\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u5185\u5b58\u5730\u5740\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b83\u4ece\u5f53\u524d\u8282\u70b9\u8bbf\u95ee\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002\u8fd9\u610f\u5473\u7740\u94fe\u8868\u7684\u5404\u4e2a\u8282\u70b9\u53ef\u4ee5\u88ab\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\uff0c\u5b83\u4eec\u7684\u5185\u5b58\u5730\u5740\u662f\u65e0\u9700\u8fde\u7eed\u7684\u3002</p> <p></p> <p> \u56fe\uff1a\u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f </p> <p>\u89c2\u5bdf\u4e0a\u56fe\uff0c\u94fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u300c\u8282\u70b9 Node\u300d\u5bf9\u8c61\u90fd\u5305\u542b\u4e24\u9879\u6570\u636e\uff1a\u8282\u70b9\u7684\u201c\u503c\u201d\u3001\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u201c\u5f15\u7528\u201d\u3002</p> <ul> <li>\u94fe\u8868\u7684\u9996\u4e2a\u8282\u70b9\u88ab\u79f0\u4e3a\u201c\u5934\u8282\u70b9\u201d\uff0c\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u88ab\u79f0\u4e3a\u201c\u5c3e\u8282\u70b9\u201d\u3002</li> <li>\u5c3e\u8282\u70b9\u6307\u5411\u7684\u662f\u201c\u7a7a\u201d\uff0c\u5b83\u5728 Java, C++, Python \u4e2d\u5206\u522b\u88ab\u8bb0\u4e3a \\(\\text{null}\\) , \\(\\text{nullptr}\\) , \\(\\text{None}\\) \u3002</li> <li>\u5728 C, C++, Go, Rust \u7b49\u652f\u6301\u6307\u9488\u7684\u8bed\u8a00\u4e2d\uff0c\u4e0a\u8ff0\u7684\u201c\u5f15\u7528\u201d\u5e94\u88ab\u66ff\u6362\u4e3a\u201c\u6307\u9488\u201d\u3002</li> </ul> <p>\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u94fe\u8868\u4ee5\u8282\u70b9\u5bf9\u8c61 <code>ListNode</code> \u4e3a\u5355\u4f4d\uff0c\u6bcf\u4e2a\u8282\u70b9\u9664\u4e86\u5305\u542b\u503c\uff0c\u8fd8\u9700\u989d\u5916\u4fdd\u5b58\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\u3002\u56e0\u6b64\u5728\u76f8\u540c\u6570\u636e\u91cf\u4e0b\uff0c\u94fe\u8868\u901a\u5e38\u6bd4\u6570\u7ec4\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\nListNode(int x) { val = x; } // \u6784\u9020\u51fd\u6570\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val; // \u8282\u70b9\u503c\nListNode *next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\nListNode(int x) : val(x), next(nullptr) {} // \u6784\u9020\u51fd\u6570\n};\n</code></pre> <pre><code>class ListNode:\n\"\"\"\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val # \u8282\u70b9\u503c\nself.next: Optional[ListNode] = None # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n</code></pre> <pre><code>/* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype ListNode struct {\nVal int // \u8282\u70b9\u503c\nNext *ListNode // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n}\n// NewListNode \u6784\u9020\u51fd\u6570\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u94fe\u8868\nfunc NewListNode(val int) *ListNode {\nreturn &amp;ListNode{\nVal: val,\nNext: nil,\n}\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nval;\nnext;\nconstructor(val, next) {\nthis.val = (val === undefined ? 0 : val); // \u8282\u70b9\u503c\nthis.next = (next === undefined ? null : next); // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nval: number;\nnext: ListNode | null;\nconstructor(val?: number, next?: ListNode | null) {\nthis.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\nthis.next = next === undefined ? null : next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val; // \u8282\u70b9\u503c\nstruct ListNode *next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n};\ntypedef struct ListNode ListNode;\n/* \u6784\u9020\u51fd\u6570 */\nListNode *newListNode(int val) {\nListNode *node, *next;\nnode = (ListNode *) malloc(sizeof(ListNode));\nnode-&gt;val = val;\nnode-&gt;next = NULL;\nreturn node;\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\nListNode(int x) =&gt; val = x; //\u6784\u9020\u51fd\u6570\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\ninit(x: Int) { // \u6784\u9020\u51fd\u6570\nval = x\n}\n}\n</code></pre> <pre><code>// \u94fe\u8868\u8282\u70b9\u7c7b\npub fn ListNode(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nval: T = 0, // \u8282\u70b9\u503c\nnext: ?*Self = null, // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n// \u6784\u9020\u51fd\u6570\npub fn init(self: *Self, x: i32) void {\nself.val = x;\nself.next = null;\n}\n};\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode? next; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\nListNode(this.val, [this.next]); // \u6784\u9020\u51fd\u6570\n}\n</code></pre> <pre><code>use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u94fe\u8868\u8282\u70b9\u7c7b */\n#[derive(Debug)]\nstruct ListNode {\nval: i32, // \u8282\u70b9\u503c\nnext: Option&lt;Rc&lt;RefCell&lt;ListNode&gt;&gt;&gt;, // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1. \u00a0 \u94fe\u8868\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#_1","title":"\u521d\u59cb\u5316\u94fe\u8868","text":"<p>\u5efa\u7acb\u94fe\u8868\u5206\u4e3a\u4e24\u6b65\uff0c\u7b2c\u4e00\u6b65\u662f\u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u6b65\u662f\u6784\u5efa\u5f15\u7528\u6307\u5411\u5173\u7cfb\u3002\u521d\u59cb\u5316\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4ece\u94fe\u8868\u7684\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u901a\u8fc7\u5f15\u7528\u6307\u5411 <code>next</code> \u4f9d\u6b21\u8bbf\u95ee\u6240\u6709\u8282\u70b9\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust linked_list.java<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.cpp<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode* n0 = new ListNode(1);\nListNode* n1 = new ListNode(3);\nListNode* n2 = new ListNode(2);\nListNode* n3 = new ListNode(5);\nListNode* n4 = new ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0-&gt;next = n1;\nn1-&gt;next = n2;\nn2-&gt;next = n3;\nn3-&gt;next = n4;\n</code></pre> linked_list.py<pre><code># \u521d\u59cb\u5316\u94fe\u8868 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4\n# \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n</code></pre> linked_list.go<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n</code></pre> linked_list.js<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.ts<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.c<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0-&gt;next = n1;\nn1-&gt;next = n2;\nn2-&gt;next = n3;\nn3-&gt;next = n4;\n</code></pre> linked_list.cs<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.swift<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nlet n0 = ListNode(x: 1)\nlet n1 = ListNode(x: 3)\nlet n2 = ListNode(x: 2)\nlet n3 = ListNode(x: 5)\nlet n4 = ListNode(x: 4)\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n</code></pre> linked_list.zig<pre><code>// \u521d\u59cb\u5316\u94fe\u8868\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nvar n0 = inc.ListNode(i32){.val = 1};\nvar n1 = inc.ListNode(i32){.val = 3};\nvar n2 = inc.ListNode(i32){.val = 2};\nvar n3 = inc.ListNode(i32){.val = 5};\nvar n4 = inc.ListNode(i32){.val = 4};\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = &amp;n1;\nn1.next = &amp;n2;\nn2.next = &amp;n3;\nn3.next = &amp;n4;\n</code></pre> linked_list.dart<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\\\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = ListNode(1);\nListNode n1 = ListNode(3);\nListNode n2 = ListNode(2);\nListNode n3 = ListNode(5);\nListNode n4 = ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.rs<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n</code></pre> <p>\u6570\u7ec4\u6574\u4f53\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u6bd4\u5982\u6570\u7ec4 <code>nums</code> \u5305\u542b\u5143\u7d20 <code>nums[0]</code> , <code>nums[1]</code> \u7b49\uff0c\u800c\u94fe\u8868\u662f\u7531\u591a\u4e2a\u72ec\u7acb\u7684\u8282\u70b9\u5bf9\u8c61\u7ec4\u6210\u7684\u3002\u6211\u4eec\u901a\u5e38\u5c06\u5934\u8282\u70b9\u5f53\u4f5c\u94fe\u8868\u7684\u4ee3\u79f0\uff0c\u6bd4\u5982\u4ee5\u4e0a\u4ee3\u7801\u4e2d\u7684\u94fe\u8868\u53ef\u88ab\u8bb0\u505a\u94fe\u8868 <code>n0</code> \u3002</p>"},{"location":"chapter_array_and_linkedlist/linked_list/#_2","title":"\u63d2\u5165\u8282\u70b9","text":"<p>\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u8282\u70b9\u975e\u5e38\u5bb9\u6613\u3002\u5047\u8bbe\u6211\u4eec\u60f3\u5728\u76f8\u90bb\u7684\u4e24\u4e2a\u8282\u70b9 <code>n0</code> , <code>n1</code> \u4e4b\u95f4\u63d2\u5165\u4e00\u4e2a\u65b0\u8282\u70b9 <code>P</code> \uff0c\u5219\u53ea\u9700\u8981\u6539\u53d8\u4e24\u4e2a\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002</p> <p>\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5728\u6570\u7ec4\u4e2d\u63d2\u5165\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5728\u5927\u6570\u636e\u91cf\u4e0b\u7684\u6548\u7387\u8f83\u4f4e\u3002</p> <p></p> <p> \u56fe\uff1a\u94fe\u8868\u63d2\u5165\u8282\u70b9 </p> JavaC++PythonGoJSTSCC#SwiftZigDartRust linked_list.java<pre><code>/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\nListNode n1 = n0.next;\nP.next = n1;\nn0.next = P;\n}\n</code></pre> linked_list.cpp<pre><code>/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\nListNode *n1 = n0-&gt;next;\nP-&gt;next = n1;\nn0-&gt;next = P;\n}\n</code></pre> linked_list.py<pre><code>def insert(n0: ListNode, P: ListNode):\n\"\"\"\u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P\"\"\"\nn1 = n0.next\nP.next = n1\nn0.next = P\n</code></pre> linked_list.go<pre><code>/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insertNode(n0 *ListNode, P *ListNode) {\nn1 := n0.Next\nP.Next = n1\nn0.Next = P\n}\n</code></pre> linked_list.js<pre><code>/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunction insert(n0, P) {\nconst n1 = n0.next;\nP.next = n1;\nn0.next = P;\n}\n</code></pre> linked_list.ts<pre><code>/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunction insert(n0: ListNode, P: ListNode): void {\nconst n1 = n0.next;\nP.next = n1;\nn0.next = P;\n}\n</code></pre> linked_list.c<pre><code>/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\nListNode *n1 = n0-&gt;next;\nP-&gt;next = n1;\nn0-&gt;next = P;\n}\n</code></pre> linked_list.cs<pre><code>/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\nListNode? n1 = n0.next;\nP.next = n1;\nn0.next = P;\n}\n</code></pre> linked_list.swift<pre><code>/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insert(n0: ListNode, P: ListNode) {\nlet n1 = n0.next\nP.next = n1\nn0.next = P\n}\n</code></pre> linked_list.zig<pre><code>// \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P\nfn insert(n0: ?*inc.ListNode(i32), P: ?*inc.ListNode(i32)) void {\nvar n1 = n0.?.next;\nP.?.next = n1;\nn0.?.next = P;\n}\n</code></pre> linked_list.dart<pre><code>/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\nListNode? n1 = n0.next;\nP.next = n1;\nn0.next = P;\n}\n</code></pre> linked_list.rs<pre><code>/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 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;) {\nlet n1 = n0.borrow_mut().next.take();\nP.borrow_mut().next = n1;\nn0.borrow_mut().next = Some(P);\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/linked_list/#_3","title":"\u5220\u9664\u8282\u70b9","text":"<p>\u5728\u94fe\u8868\u4e2d\u5220\u9664\u8282\u70b9\u4e5f\u975e\u5e38\u7b80\u4fbf\uff0c\u53ea\u9700\u6539\u53d8\u4e00\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\u3002</p> <p>\u8bf7\u6ce8\u610f\uff0c\u5c3d\u7ba1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\u8282\u70b9 <code>P</code> \u4ecd\u7136\u6307\u5411 <code>n1</code> \uff0c\u4f46\u5b9e\u9645\u4e0a\u904d\u5386\u6b64\u94fe\u8868\u5df2\u7ecf\u65e0\u6cd5\u8bbf\u95ee\u5230 <code>P</code> \uff0c\u8fd9\u610f\u5473\u7740 <code>P</code> \u5df2\u7ecf\u4e0d\u518d\u5c5e\u4e8e\u8be5\u94fe\u8868\u4e86\u3002</p> <p></p> <p> \u56fe\uff1a\u94fe\u8868\u5220\u9664\u8282\u70b9 </p> JavaC++PythonGoJSTSCC#SwiftZigDartRust linked_list.java<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\nif (n0.next == null)\nreturn;\n// n0 -&gt; P -&gt; n1\nListNode P = n0.next;\nListNode n1 = P.next;\nn0.next = n1;\n}\n</code></pre> linked_list.cpp<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode *n0) {\nif (n0-&gt;next == nullptr)\nreturn;\n// n0 -&gt; P -&gt; n1\nListNode *P = n0-&gt;next;\nListNode *n1 = P-&gt;next;\nn0-&gt;next = n1;\n// \u91ca\u653e\u5185\u5b58\ndelete P;\n}\n</code></pre> linked_list.py<pre><code>def remove(n0: ListNode):\n\"\"\"\u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9\"\"\"\nif not n0.next:\nreturn\n# n0 -&gt; P -&gt; n1\nP = n0.next\nn1 = P.next\nn0.next = n1\n</code></pre> linked_list.go<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc removeNode(n0 *ListNode) {\nif n0.Next == nil {\nreturn\n}\n// n0 -&gt; P -&gt; n1\nP := n0.Next\nn1 := P.Next\nn0.Next = n1\n}\n</code></pre> linked_list.js<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0) {\nif (!n0.next) return;\n// n0 -&gt; P -&gt; n1\nconst P = n0.next;\nconst n1 = P.next;\nn0.next = n1;\n}\n</code></pre> linked_list.ts<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0: ListNode): void {\nif (!n0.next) {\nreturn;\n}\n// n0 -&gt; P -&gt; n1\nconst P = n0.next;\nconst n1 = P.next;\nn0.next = n1;\n}\n</code></pre> linked_list.c<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeNode(ListNode *n0) {\nif (!n0-&gt;next)\nreturn;\n// n0 -&gt; P -&gt; n1\nListNode *P = n0-&gt;next;\nListNode *n1 = P-&gt;next;\nn0-&gt;next = n1;\n// \u91ca\u653e\u5185\u5b58\nfree(P);\n}\n</code></pre> linked_list.cs<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\nif (n0.next == null)\nreturn;\n// n0 -&gt; P -&gt; n1\nListNode P = n0.next;\nListNode? n1 = P.next;\nn0.next = n1;\n}\n</code></pre> linked_list.swift<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc remove(n0: ListNode) {\nif n0.next == nil {\nreturn\n}\n// n0 -&gt; P -&gt; n1\nlet P = n0.next\nlet n1 = P?.next\nn0.next = n1\nP?.next = nil\n}\n</code></pre> linked_list.zig<pre><code>// \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9\nfn remove(n0: ?*inc.ListNode(i32)) void {\nif (n0.?.next == null) return;\n// n0 -&gt; P -&gt; n1\nvar P = n0.?.next;\nvar n1 = P.?.next;\nn0.?.next = n1;\n}\n</code></pre> linked_list.dart<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\nif (n0.next == null) return;\n// n0 -&gt; P -&gt; n1\nListNode P = n0.next!;\nListNode? n1 = P.next;\nn0.next = n1;\n}\n</code></pre> linked_list.rs<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n#[allow(non_snake_case)]\npub fn remove&lt;T&gt;(n0: &amp;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;) {\nif n0.borrow().next.is_none() {return};\n// n0 -&gt; P -&gt; n1\nlet P = n0.borrow_mut().next.take();\nif let Some(node) = P {\nlet n1 = node.borrow_mut().next.take();\nn0.borrow_mut().next = n1;\n}\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/linked_list/#_4","title":"\u8bbf\u95ee\u8282\u70b9","text":"<p>\u5728\u94fe\u8868\u8bbf\u95ee\u8282\u70b9\u7684\u6548\u7387\u8f83\u4f4e\u3002\u5982\u4e0a\u8282\u6240\u8ff0\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u4e0b\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u5143\u7d20\u3002\u94fe\u8868\u5219\u4e0d\u7136\uff0c\u7a0b\u5e8f\u9700\u8981\u4ece\u5934\u8282\u70b9\u51fa\u53d1\uff0c\u9010\u4e2a\u5411\u540e\u904d\u5386\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6807\u8282\u70b9\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8bbf\u95ee\u94fe\u8868\u7684\u7b2c \\(i\\) \u4e2a\u8282\u70b9\u9700\u8981\u5faa\u73af \\(i - 1\\) \u8f6e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust linked_list.java<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode access(ListNode head, int index) {\nfor (int i = 0; i &lt; index; i++) {\nif (head == null)\nreturn null;\nhead = head.next;\n}\nreturn head;\n}\n</code></pre> linked_list.cpp<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\nfor (int i = 0; i &lt; index; i++) {\nif (head == nullptr)\nreturn nullptr;\nhead = head-&gt;next;\n}\nreturn head;\n}\n</code></pre> linked_list.py<pre><code>def access(head: ListNode, index: int) -&gt; ListNode | None:\n\"\"\"\u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9\"\"\"\nfor _ in range(index):\nif not head:\nreturn None\nhead = head.next\nreturn head\n</code></pre> linked_list.go<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head *ListNode, index int) *ListNode {\nfor i := 0; i &lt; index; i++ {\nif head == nil {\nreturn nil\n}\nhead = head.Next\n}\nreturn head\n}\n</code></pre> linked_list.js<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head, index) {\nfor (let i = 0; i &lt; index; i++) {\nif (!head) {\nreturn null;\n}\nhead = head.next;\n}\nreturn head;\n}\n</code></pre> linked_list.ts<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head: ListNode | null, index: number): ListNode | null {\nfor (let i = 0; i &lt; index; i++) {\nif (!head) {\nreturn null;\n}\nhead = head.next;\n}\nreturn head;\n}\n</code></pre> linked_list.c<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\nwhile (head &amp;&amp; head-&gt;next &amp;&amp; index) {\nhead = head-&gt;next;\nindex--;\n}\nreturn head;\n}\n</code></pre> linked_list.cs<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? access(ListNode head, int index) {\nfor (int i = 0; i &lt; index; i++) {\nif (head == null)\nreturn null;\nhead = head.next;\n}\nreturn head;\n}\n</code></pre> linked_list.swift<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head: ListNode, index: Int) -&gt; ListNode? {\nvar head: ListNode? = head\nfor _ in 0 ..&lt; index {\nif head == nil {\nreturn nil\n}\nhead = head?.next\n}\nreturn head\n}\n</code></pre> linked_list.zig<pre><code>// \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9\nfn access(node: ?*inc.ListNode(i32), index: i32) ?*inc.ListNode(i32) {\nvar head = node;\nvar i: i32 = 0;\nwhile (i &lt; index) : (i += 1) {\nhead = head.?.next;\nif (head == null) return null;\n}\nreturn head;\n}\n</code></pre> linked_list.dart<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? access(ListNode? head, int index) {\nfor (var i = 0; i &lt; index; i++) {\nif (head == null) return null;\nhead = head.next;\n}\nreturn head;\n}\n</code></pre> linked_list.rs<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\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; {\nif index &lt;= 0 {return head};\nif let Some(node) = &amp;head.borrow_mut().next {\nreturn access(node.clone(), index - 1);\n}\nreturn head;\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/linked_list/#_5","title":"\u67e5\u627e\u8282\u70b9","text":"<p>\u904d\u5386\u94fe\u8868\uff0c\u67e5\u627e\u94fe\u8868\u5185\u503c\u4e3a <code>target</code> \u7684\u8282\u70b9\uff0c\u8f93\u51fa\u8282\u70b9\u5728\u94fe\u8868\u4e2d\u7684\u7d22\u5f15\u3002\u6b64\u8fc7\u7a0b\u4e5f\u5c5e\u4e8e\u300c\u7ebf\u6027\u67e5\u627e\u300d\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust linked_list.java<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode head, int target) {\nint index = 0;\nwhile (head != null) {\nif (head.val == target)\nreturn index;\nhead = head.next;\nindex++;\n}\nreturn -1;\n}\n</code></pre> linked_list.cpp<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\nint index = 0;\nwhile (head != nullptr) {\nif (head-&gt;val == target)\nreturn index;\nhead = head-&gt;next;\nindex++;\n}\nreturn -1;\n}\n</code></pre> linked_list.py<pre><code>def find(head: ListNode, target: int) -&gt; int:\n\"\"\"\u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9\"\"\"\nindex = 0\nwhile head:\nif head.val == target:\nreturn index\nhead = head.next\nindex += 1\nreturn -1\n</code></pre> linked_list.go<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunc findNode(head *ListNode, target int) int {\nindex := 0\nfor head != nil {\nif head.Val == target {\nreturn index\n}\nhead = head.Next\nindex++\n}\nreturn -1\n}\n</code></pre> linked_list.js<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head, target) {\nlet index = 0;\nwhile (head !== null) {\nif (head.val === target) {\nreturn index;\n}\nhead = head.next;\nindex += 1;\n}\nreturn -1;\n}\n</code></pre> linked_list.ts<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head: ListNode | null, target: number): number {\nlet index = 0;\nwhile (head !== null) {\nif (head.val === target) {\nreturn index;\n}\nhead = head.next;\nindex += 1;\n}\nreturn -1;\n}\n</code></pre> linked_list.c<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\nint index = 0;\nwhile (head) {\nif (head-&gt;val == target)\nreturn index;\nhead = head-&gt;next;\nindex++;\n}\nreturn -1;\n}\n</code></pre> linked_list.cs<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode head, int target) {\nint index = 0;\nwhile (head != null) {\nif (head.val == target)\nreturn index;\nhead = head.next;\nindex++;\n}\nreturn -1;\n}\n</code></pre> linked_list.swift<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunc find(head: ListNode, target: Int) -&gt; Int {\nvar head: ListNode? = head\nvar index = 0\nwhile head != nil {\nif head?.val == target {\nreturn index\n}\nhead = head?.next\nindex += 1\n}\nreturn -1\n}\n</code></pre> linked_list.zig<pre><code>// \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9\nfn find(node: ?*inc.ListNode(i32), target: i32) i32 {\nvar head = node;\nvar index: i32 = 0;\nwhile (head != null) {\nif (head.?.val == target) return index;\nhead = head.?.next;\nindex += 1;\n}\nreturn -1;\n}\n</code></pre> linked_list.dart<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode? head, int target) {\nint index = 0;\nwhile (head != null) {\nif (head.val == target) {\nreturn index;\n}\nhead = head.next;\nindex++;\n}\nreturn -1;\n}\n</code></pre> linked_list.rs<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\npub fn find&lt;T: PartialEq&gt;(head: Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;, target: T, index: i32) -&gt; i32 {\nif head.borrow().val == target {return index};\nif let Some(node) = &amp;head.borrow_mut().next {\nreturn find(node.clone(), target, index + 1);\n}\nreturn -1;\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/linked_list/#422-vs","title":"4.2.2. \u00a0 \u6570\u7ec4 VS \u94fe\u8868","text":"<p>\u4e0b\u8868\u603b\u7ed3\u5bf9\u6bd4\u4e86\u6570\u7ec4\u548c\u94fe\u8868\u7684\u5404\u9879\u7279\u70b9\u4e0e\u64cd\u4f5c\u6548\u7387\u3002\u7531\u4e8e\u5b83\u4eec\u91c7\u7528\u4e24\u79cd\u76f8\u53cd\u7684\u5b58\u50a8\u7b56\u7565\uff0c\u56e0\u6b64\u5404\u79cd\u6027\u8d28\u548c\u64cd\u4f5c\u6548\u7387\u4e5f\u5448\u73b0\u5bf9\u7acb\u7684\u7279\u70b9\u3002</p> <p> \u8868\uff1a\u6570\u7ec4\u4e0e\u94fe\u8868\u7684\u6548\u7387\u5bf9\u6bd4 </p> \u6570\u7ec4 \u94fe\u8868 \u5b58\u50a8\u65b9\u5f0f \u8fde\u7eed\u5185\u5b58\u7a7a\u95f4 \u79bb\u6563\u5185\u5b58\u7a7a\u95f4 \u7f13\u5b58\u5c40\u90e8\u6027 \u53cb\u597d \u4e0d\u53cb\u597d \u5bb9\u91cf\u6269\u5c55 \u957f\u5ea6\u4e0d\u53ef\u53d8 \u53ef\u7075\u6d3b\u6269\u5c55 \u5185\u5b58\u6548\u7387 \u5360\u7528\u5185\u5b58\u5c11\u3001\u6d6a\u8d39\u90e8\u5206\u7a7a\u95f4 \u5360\u7528\u5185\u5b58\u591a \u8bbf\u95ee\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3. \u00a0 \u5e38\u89c1\u94fe\u8868\u7c7b\u578b","text":"<p>\u5355\u5411\u94fe\u8868\u3002\u5373\u4e0a\u8ff0\u4ecb\u7ecd\u7684\u666e\u901a\u94fe\u8868\u3002\u5355\u5411\u94fe\u8868\u7684\u8282\u70b9\u5305\u542b\u503c\u548c\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\u4e24\u9879\u6570\u636e\u3002\u6211\u4eec\u5c06\u9996\u4e2a\u8282\u70b9\u79f0\u4e3a\u5934\u8282\u70b9\uff0c\u5c06\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u6210\u4e3a\u5c3e\u8282\u70b9\uff0c\u5c3e\u8282\u70b9\u6307\u5411\u7a7a \\(\\text{None}\\) \u3002</p> <p>\u73af\u5f62\u94fe\u8868\u3002\u5982\u679c\u6211\u4eec\u4ee4\u5355\u5411\u94fe\u8868\u7684\u5c3e\u8282\u70b9\u6307\u5411\u5934\u8282\u70b9\uff08\u5373\u9996\u5c3e\u76f8\u63a5\uff09\uff0c\u5219\u5f97\u5230\u4e00\u4e2a\u73af\u5f62\u94fe\u8868\u3002\u5728\u73af\u5f62\u94fe\u8868\u4e2d\uff0c\u4efb\u610f\u8282\u70b9\u90fd\u53ef\u4ee5\u89c6\u4f5c\u5934\u8282\u70b9\u3002</p> <p>\u53cc\u5411\u94fe\u8868\u3002\u4e0e\u5355\u5411\u94fe\u8868\u76f8\u6bd4\uff0c\u53cc\u5411\u94fe\u8868\u8bb0\u5f55\u4e86\u4e24\u4e2a\u65b9\u5411\u7684\u5f15\u7528\u3002\u53cc\u5411\u94fe\u8868\u7684\u8282\u70b9\u5b9a\u4e49\u540c\u65f6\u5305\u542b\u6307\u5411\u540e\u7ee7\u8282\u70b9\uff08\u4e0b\u4e00\u4e2a\u8282\u70b9\uff09\u548c\u524d\u9a71\u8282\u70b9\uff08\u4e0a\u4e00\u4e2a\u8282\u70b9\uff09\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\u3002\u76f8\u8f83\u4e8e\u5355\u5411\u94fe\u8868\uff0c\u53cc\u5411\u94fe\u8868\u66f4\u5177\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u671d\u4e24\u4e2a\u65b9\u5411\u904d\u5386\u94fe\u8868\uff0c\u4f46\u76f8\u5e94\u5730\u4e5f\u9700\u8981\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\nListNode prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\nListNode(int x) { val = x; } // \u6784\u9020\u51fd\u6570\n}\n</code></pre> <pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val; // \u8282\u70b9\u503c\nListNode *next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\nListNode *prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\nListNode(int x) : val(x), next(nullptr), prev(nullptr) {} // \u6784\u9020\u51fd\u6570\n};\n</code></pre> <pre><code>class ListNode:\n\"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val # \u8282\u70b9\u503c\nself.next: Optional[ListNode] = None # \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\nself.prev: Optional[ListNode] = None # \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n</code></pre> <pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype DoublyListNode struct {\nVal int // \u8282\u70b9\u503c\nNext *DoublyListNode // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\nPrev *DoublyListNode // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n}\n// NewDoublyListNode \u521d\u59cb\u5316\nfunc NewDoublyListNode(val int) *DoublyListNode {\nreturn &amp;DoublyListNode{\nVal: val,\nNext: nil,\nPrev: nil,\n}\n}\n</code></pre> <pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nval;\nnext;\nprev;\nconstructor(val, next, prev) {\nthis.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\nthis.next = next === undefined ? null : next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\nthis.prev = prev === undefined ? null : prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n</code></pre> <pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nval: number;\nnext: ListNode | null;\nprev: ListNode | null;\nconstructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\nthis.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\nthis.next = next === undefined ? null : next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\nthis.prev = prev === undefined ? null : prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n</code></pre> <pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val; // \u8282\u70b9\u503c\nstruct ListNode *next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\nstruct ListNode *prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n};\ntypedef struct ListNode ListNode;\n/* \u6784\u9020\u51fd\u6570 */\nListNode *newListNode(int val) {\nListNode *node, *next;\nnode = (ListNode *) malloc(sizeof(ListNode));\nnode-&gt;val = val;\nnode-&gt;next = NULL;\nnode-&gt;prev = NULL;\nreturn node;\n}\n</code></pre> <pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\nListNode prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\nListNode(int x) =&gt; val = x; // \u6784\u9020\u51fd\u6570\n}\n</code></pre> <pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\nvar prev: ListNode? // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\ninit(x: Int) { // \u6784\u9020\u51fd\u6570\nval = x\n}\n}\n</code></pre> <pre><code>// \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\npub fn ListNode(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nval: T = 0, // \u8282\u70b9\u503c\nnext: ?*Self = null, // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\nprev: ?*Self = null, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n// \u6784\u9020\u51fd\u6570\npub fn init(self: *Self, x: i32) void {\nself.val = x;\nself.next = null;\nself.prev = null;\n}\n};\n}\n</code></pre> <pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode next; // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\nListNode prev; // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\nListNode(this.val, [this.next, this.prev]); // \u6784\u9020\u51fd\u6570\n}\n</code></pre> <pre><code>use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u53cc\u5411\u94fe\u8868\u8282\u70b9\u7c7b\u578b */\n#[derive(Debug)]\nstruct ListNode {\nval: i32, // \u8282\u70b9\u503c\nnext: Option&lt;Rc&lt;RefCell&lt;ListNode&gt;&gt;&gt;, // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\nprev: Option&lt;Rc&lt;RefCell&lt;ListNode&gt;&gt;&gt;, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n}\n/* \u6784\u9020\u51fd\u6570 */\nimpl ListNode {\nfn new(val: i32) -&gt; Self {\nListNode {\nval,\nnext: None,\nprev: None,\n}\n}\n}\n</code></pre> <p></p> <p> \u56fe\uff1a\u5e38\u89c1\u94fe\u8868\u79cd\u7c7b </p>"},{"location":"chapter_array_and_linkedlist/linked_list/#424","title":"4.2.4. \u00a0 \u94fe\u8868\u5178\u578b\u5e94\u7528","text":"<p>\u5355\u5411\u94fe\u8868\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u6808\u3001\u961f\u5217\u3001\u6563\u5217\u8868\u548c\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u3002</p> <ul> <li>\u6808\u4e0e\u961f\u5217\uff1a\u5f53\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u90fd\u5728\u94fe\u8868\u7684\u4e00\u7aef\u8fdb\u884c\u65f6\uff0c\u5b83\u8868\u73b0\u51fa\u5148\u8fdb\u540e\u51fa\u7684\u7684\u7279\u6027\uff0c\u5bf9\u5e94\u6808\uff1b\u5f53\u63d2\u5165\u64cd\u4f5c\u5728\u94fe\u8868\u7684\u4e00\u7aef\u8fdb\u884c\uff0c\u5220\u9664\u64cd\u4f5c\u5728\u94fe\u8868\u7684\u53e6\u4e00\u7aef\u8fdb\u884c\uff0c\u5b83\u8868\u73b0\u51fa\u5148\u8fdb\u5148\u51fa\u7684\u7279\u6027\uff0c\u5bf9\u5e94\u961f\u5217\u3002</li> <li>\u6563\u5217\u8868\uff1a\u94fe\u5730\u5740\u6cd5\u662f\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u7684\u4e3b\u6d41\u65b9\u6848\u4e4b\u4e00\uff0c\u5728\u8be5\u65b9\u6848\u4e2d\uff0c\u6240\u6709\u51b2\u7a81\u7684\u5143\u7d20\u90fd\u4f1a\u88ab\u653e\u5230\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002</li> <li>\u56fe\uff1a\u90bb\u63a5\u8868\u662f\u8868\u793a\u56fe\u7684\u4e00\u79cd\u5e38\u7528\u65b9\u5f0f\uff0c\u5728\u5176\u4e2d\uff0c\u56fe\u7684\u6bcf\u4e2a\u9876\u70b9\u90fd\u4e0e\u4e00\u4e2a\u94fe\u8868\u76f8\u5173\u8054\uff0c\u94fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u4ee3\u8868\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u5176\u4ed6\u9876\u70b9\u3002</li> </ul> <p>\u53cc\u5411\u94fe\u8868\u5e38\u88ab\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u524d\u4e00\u4e2a\u548c\u4e0b\u4e00\u4e2a\u5143\u7d20\u7684\u573a\u666f\u3002</p> <ul> <li>\u9ad8\u7ea7\u6570\u636e\u7ed3\u6784\uff1a\u6bd4\u5982\u5728\u7ea2\u9ed1\u6811\u3001B \u6811\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u8bbf\u95ee\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728\u8282\u70b9\u4e2d\u4fdd\u5b58\u4e00\u4e2a\u6307\u5411\u7236\u8282\u70b9\u7684\u5f15\u7528\u6765\u5b9e\u73b0\uff0c\u7c7b\u4f3c\u4e8e\u53cc\u5411\u94fe\u8868\u3002</li> <li>\u6d4f\u89c8\u5668\u5386\u53f2\uff1a\u5728\u7f51\u9875\u6d4f\u89c8\u5668\u4e2d\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u524d\u8fdb\u6216\u540e\u9000\u6309\u94ae\u65f6\uff0c\u6d4f\u89c8\u5668\u9700\u8981\u77e5\u9053\u7528\u6237\u8bbf\u95ee\u8fc7\u7684\u524d\u4e00\u4e2a\u548c\u540e\u4e00\u4e2a\u7f51\u9875\u3002\u53cc\u5411\u94fe\u8868\u7684\u7279\u6027\u4f7f\u5f97\u8fd9\u79cd\u64cd\u4f5c\u53d8\u5f97\u7b80\u5355\u3002</li> <li>LRU \u7b97\u6cd5\uff1a\u5728\u7f13\u5b58\u6dd8\u6c70\u7b97\u6cd5\uff08LRU\uff09\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5feb\u901f\u627e\u5230\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u6570\u636e\uff0c\u4ee5\u53ca\u652f\u6301\u5feb\u901f\u5730\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u3002\u8fd9\u65f6\u5019\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u5c31\u975e\u5e38\u5408\u9002\u3002</li> </ul> <p>\u5faa\u73af\u94fe\u8868\u5e38\u88ab\u7528\u4e8e\u9700\u8981\u5468\u671f\u6027\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u6bd4\u5982\u64cd\u4f5c\u7cfb\u7edf\u7684\u8d44\u6e90\u8c03\u5ea6\u3002</p> <ul> <li>\u65f6\u95f4\u7247\u8f6e\u8f6c\u8c03\u5ea6\u7b97\u6cd5\uff1a\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u65f6\u95f4\u7247\u8f6e\u8f6c\u8c03\u5ea6\u7b97\u6cd5\u662f\u4e00\u79cd\u5e38\u89c1\u7684 CPU \u8c03\u5ea6\u7b97\u6cd5\uff0c\u5b83\u9700\u8981\u5bf9\u4e00\u7ec4\u8fdb\u7a0b\u8fdb\u884c\u5faa\u73af\u3002\u6bcf\u4e2a\u8fdb\u7a0b\u88ab\u8d4b\u4e88\u4e00\u4e2a\u65f6\u95f4\u7247\uff0c\u5f53\u65f6\u95f4\u7247\u7528\u5b8c\u65f6\uff0cCPU \u5c06\u5207\u6362\u5230\u4e0b\u4e00\u4e2a\u8fdb\u7a0b\u3002\u8fd9\u79cd\u5faa\u73af\u7684\u64cd\u4f5c\u5c31\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u94fe\u8868\u6765\u5b9e\u73b0\u3002</li> <li>\u6570\u636e\u7f13\u51b2\u533a\uff1a\u5728\u67d0\u4e9b\u6570\u636e\u7f13\u51b2\u533a\u7684\u5b9e\u73b0\u4e2d\uff0c\u4e5f\u53ef\u80fd\u4f1a\u4f7f\u7528\u5230\u5faa\u73af\u94fe\u8868\u3002\u6bd4\u5982\u5728\u97f3\u9891\u3001\u89c6\u9891\u64ad\u653e\u5668\u4e2d\uff0c\u6570\u636e\u6d41\u53ef\u80fd\u4f1a\u88ab\u5206\u6210\u591a\u4e2a\u7f13\u51b2\u5757\u5e76\u653e\u5165\u4e00\u4e2a\u5faa\u73af\u94fe\u8868\uff0c\u4ee5\u4fbf\u5b9e\u73b0\u65e0\u7f1d\u64ad\u653e\u3002</li> </ul>"},{"location":"chapter_array_and_linkedlist/list/","title":"4.3. \u00a0 \u5217\u8868","text":"<p>\u6570\u7ec4\u957f\u5ea6\u4e0d\u53ef\u53d8\u5bfc\u81f4\u5b9e\u7528\u6027\u964d\u4f4e\u3002\u5728\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u4e8b\u5148\u65e0\u6cd5\u786e\u5b9a\u9700\u8981\u5b58\u50a8\u591a\u5c11\u6570\u636e\uff0c\u8fd9\u4f7f\u6570\u7ec4\u957f\u5ea6\u7684\u9009\u62e9\u53d8\u5f97\u56f0\u96be\u3002\u82e5\u957f\u5ea6\u8fc7\u5c0f\uff0c\u9700\u8981\u5728\u6301\u7eed\u6dfb\u52a0\u6570\u636e\u65f6\u9891\u7e41\u6269\u5bb9\u6570\u7ec4\uff1b\u82e5\u957f\u5ea6\u8fc7\u5927\uff0c\u5219\u4f1a\u9020\u6210\u5185\u5b58\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002</p> <p>\u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u51fa\u73b0\u4e86\u4e00\u79cd\u88ab\u79f0\u4e3a\u300c\u52a8\u6001\u6570\u7ec4 Dynamic Array\u300d\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5373\u957f\u5ea6\u53ef\u53d8\u7684\u6570\u7ec4\uff0c\u4e5f\u5e38\u88ab\u79f0\u4e3a\u300c\u5217\u8868 List\u300d\u3002\u5217\u8868\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\uff0c\u7ee7\u627f\u4e86\u6570\u7ec4\u7684\u4f18\u70b9\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u52a8\u6001\u6269\u5bb9\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u5217\u8868\u4e2d\u81ea\u7531\u5730\u6dfb\u52a0\u5143\u7d20\uff0c\u800c\u65e0\u9700\u62c5\u5fc3\u8d85\u8fc7\u5bb9\u91cf\u9650\u5236\u3002</p>"},{"location":"chapter_array_and_linkedlist/list/#431","title":"4.3.1. \u00a0 \u5217\u8868\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/list/#_1","title":"\u521d\u59cb\u5316\u5217\u8868","text":"<p>\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u201c\u65e0\u521d\u59cb\u503c\u201d\u548c\u201c\u6709\u521d\u59cb\u503c\u201d\u8fd9\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u6cd5\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust list.java<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList&lt;Integer&gt; list1 = new ArrayList&lt;&gt;();\n// \u6709\u521d\u59cb\u503c\uff08\u6ce8\u610f\u6570\u7ec4\u7684\u5143\u7d20\u7c7b\u578b\u9700\u4e3a int[] \u7684\u5305\u88c5\u7c7b Integer[]\uff09\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList&lt;Integer&gt; list = new ArrayList&lt;&gt;(Arrays.asList(numbers));\n</code></pre> list.cpp<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u9700\u6ce8\u610f\uff0cC++ \u4e2d vector \u5373\u662f\u672c\u6587\u63cf\u8ff0\u7684 list\n// \u65e0\u521d\u59cb\u503c\nvector&lt;int&gt; list1;\n// \u6709\u521d\u59cb\u503c\nvector&lt;int&gt; list = { 1, 3, 2, 5, 4 };\n</code></pre> list.py<pre><code># \u521d\u59cb\u5316\u5217\u8868\n# \u65e0\u521d\u59cb\u503c\nlist1: list[int] = []\n# \u6709\u521d\u59cb\u503c\nlist: list[int] = [1, 3, 2, 5, 4]\n</code></pre> list_test.go<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlist1 := []int\n// \u6709\u521d\u59cb\u503c\nlist := []int{1, 3, 2, 5, 4}\n</code></pre> list.js<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst list1 = [];\n// \u6709\u521d\u59cb\u503c\nconst list = [1, 3, 2, 5, 4];\n</code></pre> list.ts<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst list1: number[] = [];\n// \u6709\u521d\u59cb\u503c\nconst list: number[] = [1, 3, 2, 5, 4];\n</code></pre> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n</code></pre> list.cs<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList&lt;int&gt; list1 = new ();\n// \u6709\u521d\u59cb\u503c\nint[] numbers = new int[] { 1, 3, 2, 5, 4 };\nList&lt;int&gt; list = numbers.ToList();\n</code></pre> list.swift<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet list1: [Int] = []\n// \u6709\u521d\u59cb\u503c\nvar list = [1, 3, 2, 5, 4]\n</code></pre> list.zig<pre><code>// \u521d\u59cb\u5316\u5217\u8868\nvar list = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer list.deinit();\ntry list.appendSlice(&amp;[_]i32{ 1, 3, 2, 5, 4 });\n</code></pre> list.dart<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList&lt;int&gt; list1 = [];\n// \u6709\u521d\u59cb\u503c\nList&lt;int&gt; list = [1, 3, 2, 5, 4];\n</code></pre> list.rs<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet list1: Vec&lt;i32&gt; = Vec::new();\n// \u6709\u521d\u59cb\u503c\nlet list2: Vec&lt;i32&gt; = vec![1, 3, 2, 5, 4];\n</code></pre>"},{"location":"chapter_array_and_linkedlist/list/#_2","title":"\u8bbf\u95ee\u5143\u7d20","text":"<p>\u5217\u8868\u672c\u8d28\u4e0a\u662f\u6570\u7ec4\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u8bbf\u95ee\u548c\u66f4\u65b0\u5143\u7d20\uff0c\u6548\u7387\u5f88\u9ad8\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust list.java<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nint num = list.get(1); // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist.set(1, 0); // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.cpp<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nint num = list[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.py<pre><code># \u8bbf\u95ee\u5143\u7d20\nnum: int = list[1] # \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n# \u66f4\u65b0\u5143\u7d20\nlist[1] = 0 # \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list_test.go<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nnum := list[1] // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0 // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.js<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nconst num = list[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.ts<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nconst num: number = list[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n</code></pre> list.cs<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nint num = list[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.swift<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nlet num = list[1] // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0 // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.zig<pre><code>// \u8bbf\u95ee\u5143\u7d20\nvar num = list.items[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n// \u66f4\u65b0\u5143\u7d20\nlist.items[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0 \n</code></pre> list.dart<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nint num = list[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.rs<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nlet num: i32 = list[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre>"},{"location":"chapter_array_and_linkedlist/list/#_3","title":"\u63d2\u5165\u4e0e\u5220\u9664\u5143\u7d20","text":"<p>\u76f8\u8f83\u4e8e\u6570\u7ec4\uff0c\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5730\u6dfb\u52a0\u4e0e\u5220\u9664\u5143\u7d20\u3002\u5728\u5217\u8868\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u4f46\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u7684\u6548\u7387\u4ecd\u4e0e\u6570\u7ec4\u76f8\u540c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust list.java<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist.clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.add(1);\nlist.add(3);\nlist.add(2);\nlist.add(5);\nlist.add(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.add(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.remove(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n</code></pre> list.cpp<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist.clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.push_back(1);\nlist.push_back(3);\nlist.push_back(2);\nlist.push_back(5);\nlist.push_back(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.insert(list.begin() + 3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.erase(list.begin() + 3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n</code></pre> list.py<pre><code># \u6e05\u7a7a\u5217\u8868\nlist.clear()\n# \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\nlist.append(1)\nlist.append(3)\nlist.append(2)\nlist.append(5)\nlist.append(4)\n# \u4e2d\u95f4\u63d2\u5165\u5143\u7d20\nlist.insert(3, 6) # \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n# \u5220\u9664\u5143\u7d20\nlist.pop(3) # \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n</code></pre> list_test.go<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist = nil\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist = append(list, 1)\nlist = append(list, 3)\nlist = append(list, 2)\nlist = append(list, 5)\nlist = append(list, 4)\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist = append(list[:3], append([]int{6}, list[3:]...)...) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist = append(list[:3], list[4:]...) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n</code></pre> list.js<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist.length = 0;\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.push(1);\nlist.push(3);\nlist.push(2);\nlist.push(5);\nlist.push(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.splice(3, 0, 6);\n/* \u5220\u9664\u5143\u7d20 */\nlist.splice(3, 1);\n</code></pre> list.ts<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist.length = 0;\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.push(1);\nlist.push(3);\nlist.push(2);\nlist.push(5);\nlist.push(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.splice(3, 0, 6);\n/* \u5220\u9664\u5143\u7d20 */\nlist.splice(3, 1);\n</code></pre> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n</code></pre> list.cs<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist.Clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.Add(1);\nlist.Add(3);\nlist.Add(2);\nlist.Add(5);\nlist.Add(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.Insert(3, 6);\n/* \u5220\u9664\u5143\u7d20 */\nlist.RemoveAt(3);\n</code></pre> list.swift<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist.removeAll()\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.append(1)\nlist.append(3)\nlist.append(2)\nlist.append(5)\nlist.append(4)\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.insert(6, at: 3) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.remove(at: 3) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n</code></pre> list.zig<pre><code>// \u6e05\u7a7a\u5217\u8868\nlist.clearRetainingCapacity();\n// \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\ntry list.append(1);\ntry list.append(3);\ntry list.append(2);\ntry list.append(5);\ntry list.append(4);\n// \u4e2d\u95f4\u63d2\u5165\u5143\u7d20\ntry list.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n// \u5220\u9664\u5143\u7d20\n_ = list.orderedRemove(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n</code></pre> list.dart<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist.clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.add(1);\nlist.add(3);\nlist.add(2);\nlist.add(5);\nlist.add(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.removeAt(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n</code></pre> list.rs<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist.clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.push(1);\nlist.push(3);\nlist.push(2);\nlist.push(5);\nlist.push(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.remove(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n</code></pre>"},{"location":"chapter_array_and_linkedlist/list/#_4","title":"\u904d\u5386\u5217\u8868","text":"<p>\u4e0e\u6570\u7ec4\u4e00\u6837\uff0c\u5217\u8868\u53ef\u4ee5\u6839\u636e\u7d22\u5f15\u904d\u5386\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u5404\u5143\u7d20\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust list.java<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i &lt; list.size(); i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int n : list) {\ncount++;\n}\n</code></pre> list.cpp<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i &lt; list.size(); i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int n : list) {\ncount++;\n}\n</code></pre> list.py<pre><code># \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\ncount = 0\nfor i in range(len(list)):\ncount += 1\n# \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\ncount = 0\nfor n in list:\ncount += 1\n</code></pre> list_test.go<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\ncount := 0\nfor i := 0; i &lt; len(list); i++ {\ncount++\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor range list {\ncount++\n}\n</code></pre> list.js<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i &lt; list.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const n of list) {\ncount++;\n}\n</code></pre> list.ts<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i &lt; list.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const n of list) {\ncount++;\n}\n</code></pre> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n</code></pre> list.cs<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i &lt; list.Count; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nforeach (int n in list) {\ncount++;\n}\n</code></pre> list.swift<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nvar count = 0\nfor _ in list.indices {\ncount += 1\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor _ in list {\ncount += 1\n}\n</code></pre> list.zig<pre><code>// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i &lt; list.items.len) : (i += 1) {\ncount += 1;\n}\n// \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20\ncount = 0;\nfor (list.items) |_| {\ncount += 1;\n}\n</code></pre> list.dart<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i &lt; list.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int n in list) {\ncount++;\n}\n</code></pre> list.rs<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet mut count = 0;\nfor (index, value) in list.iter().enumerate() {\ncount += 1;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\nlet mut count = 0;\nfor value in list.iter() {\ncount += 1;\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/list/#_5","title":"\u62fc\u63a5\u5217\u8868","text":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u65b0\u5217\u8868 <code>list1</code> \uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u5217\u8868\u62fc\u63a5\u5230\u539f\u5217\u8868\u7684\u5c3e\u90e8\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust list.java<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList&lt;Integer&gt; list1 = new ArrayList&lt;&gt;(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nlist.addAll(list1); // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list.cpp<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nvector&lt;int&gt; list1 = { 6, 8, 7, 10, 9 };\n// \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\nlist.insert(list.end(), list1.begin(), list1.end());\n</code></pre> list.py<pre><code># \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nlist1: list[int] = [6, 8, 7, 10, 9]\nlist += list1 # \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list_test.go<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlist1 := []int{6, 8, 7, 10, 9}\nlist = append(list, list1...) // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list.js<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst list1 = [6, 8, 7, 10, 9];\nlist.push(...list1); // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list.ts<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst list1: number[] = [6, 8, 7, 10, 9];\nlist.push(...list1); // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n</code></pre> list.cs<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList&lt;int&gt; list1 = new() { 6, 8, 7, 10, 9 };\nlist.AddRange(list1); // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list.swift<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet list1 = [6, 8, 7, 10, 9]\nlist.append(contentsOf: list1) // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list.zig<pre><code>// \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nvar list1 = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer list1.deinit();\ntry list1.appendSlice(&amp;[_]i32{ 6, 8, 7, 10, 9 });\ntry list.insertSlice(list.items.len, list1.items); // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list.dart<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList&lt;int&gt; list1 = [6, 8, 7, 10, 9];\nlist.addAll(list1); // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list.rs<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet list1: Vec&lt;i32&gt; = vec![6, 8, 7, 10, 9];\nlist.extend(list1);\n</code></pre>"},{"location":"chapter_array_and_linkedlist/list/#_6","title":"\u6392\u5e8f\u5217\u8868","text":"<p>\u5b8c\u6210\u5217\u8868\u6392\u5e8f\u540e\uff0c\u6211\u4eec\u4fbf\u53ef\u4ee5\u4f7f\u7528\u5728\u6570\u7ec4\u7c7b\u7b97\u6cd5\u9898\u4e2d\u7ecf\u5e38\u8003\u5bdf\u7684\u201c\u4e8c\u5206\u67e5\u627e\u201d\u548c\u201c\u53cc\u6307\u9488\u201d\u7b97\u6cd5\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust list.java<pre><code>/* \u6392\u5e8f\u5217\u8868 */\nCollections.sort(list); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.cpp<pre><code>/* \u6392\u5e8f\u5217\u8868 */\nsort(list.begin(), list.end()); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.py<pre><code># \u6392\u5e8f\u5217\u8868\nlist.sort() # \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list_test.go<pre><code>/* \u6392\u5e8f\u5217\u8868 */\nsort.Ints(list) // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.js<pre><code>/* \u6392\u5e8f\u5217\u8868 */ list.sort((a, b) =&gt; a - b); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.ts<pre><code>/* \u6392\u5e8f\u5217\u8868 */\nlist.sort((a, b) =&gt; a - b); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\n</code></pre> list.cs<pre><code>/* \u6392\u5e8f\u5217\u8868 */\nlist.Sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.swift<pre><code>/* \u6392\u5e8f\u5217\u8868 */\nlist.sort() // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.zig<pre><code>// \u6392\u5e8f\u5217\u8868\nstd.sort.sort(i32, list.items, {}, comptime std.sort.asc(i32));\n</code></pre> list.dart<pre><code>/* \u6392\u5e8f\u5217\u8868 */\nlist.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.rs<pre><code>/* \u6392\u5e8f\u5217\u8868 */\nlist.sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre>"},{"location":"chapter_array_and_linkedlist/list/#432","title":"4.3.2. \u00a0 \u5217\u8868\u5b9e\u73b0","text":"<p>\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u90fd\u63d0\u4f9b\u5185\u7f6e\u7684\u5217\u8868\uff0c\u4f8b\u5982 Java, C++, Python \u7b49\u3002\u5b83\u4eec\u7684\u5b9e\u73b0\u6bd4\u8f83\u590d\u6742\uff0c\u5404\u4e2a\u53c2\u6570\u7684\u8bbe\u5b9a\u4e5f\u975e\u5e38\u6709\u8003\u7a76\uff0c\u4f8b\u5982\u521d\u59cb\u5bb9\u91cf\u3001\u6269\u5bb9\u500d\u6570\u7b49\u3002\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u67e5\u9605\u6e90\u7801\u8fdb\u884c\u5b66\u4e60\u3002</p> <p>\u4e3a\u4e86\u5e2e\u52a9\u4f60\u7406\u89e3\u5217\u8868\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u6211\u4eec\u5728\u6b64\u63d0\u4f9b\u4e00\u4e2a\u7b80\u6613\u7248\u5217\u8868\u5b9e\u73b0\uff0c\u91cd\u70b9\u5305\u62ec\uff1a</p> <ul> <li>\u521d\u59cb\u5bb9\u91cf\uff1a\u9009\u53d6\u4e00\u4e2a\u5408\u7406\u7684\u6570\u7ec4\u521d\u59cb\u5bb9\u91cf\u3002\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9009\u62e9 10 \u4f5c\u4e3a\u521d\u59cb\u5bb9\u91cf\u3002</li> <li>\u6570\u91cf\u8bb0\u5f55\uff1a\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf size\uff0c\u7528\u4e8e\u8bb0\u5f55\u5217\u8868\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff0c\u5e76\u968f\u7740\u5143\u7d20\u63d2\u5165\u548c\u5220\u9664\u5b9e\u65f6\u66f4\u65b0\u3002\u6839\u636e\u6b64\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4f4d\u5217\u8868\u5c3e\u90e8\uff0c\u4ee5\u53ca\u5224\u65ad\u662f\u5426\u9700\u8981\u6269\u5bb9\u3002</li> <li>\u6269\u5bb9\u673a\u5236\uff1a\u82e5\u63d2\u5165\u5143\u7d20\u65f6\u5217\u8868\u5bb9\u91cf\u5df2\u6ee1\uff0c\u5219\u9700\u8981\u8fdb\u884c\u6269\u5bb9\u3002\u9996\u5148\u6839\u636e\u6269\u5bb9\u500d\u6570\u521b\u5efa\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\uff0c\u518d\u5c06\u5f53\u524d\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u4f9d\u6b21\u79fb\u52a8\u81f3\u65b0\u6570\u7ec4\u3002\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u89c4\u5b9a\u6bcf\u6b21\u5c06\u6570\u7ec4\u6269\u5bb9\u81f3\u4e4b\u524d\u7684 2 \u500d\u3002</li> </ul> JavaC++PythonGoJSTSCC#SwiftZigDartRust my_list.java<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate int[] nums; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate int capacity = 10; // \u5217\u8868\u5bb9\u91cf\nprivate int size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\npublic MyList() {\nnums = new int[capacity];\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\npublic int size() {\nreturn size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npublic int capacity() {\nreturn capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npublic int get(int index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index &lt; 0 || index &gt;= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npublic void set(int index, int num) {\nif (index &lt; 0 || index &gt;= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\nnums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npublic void add(int num) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size == capacity())\nextendCapacity();\nnums[size] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nsize++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npublic void insert(int index, int num) {\nif (index &lt; 0 || index &gt;= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size == capacity())\nextendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int j = size - 1; j &gt;= index; j--) {\nnums[j + 1] = nums[j];\n}\nnums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nsize++;\n}\n/* \u5220\u9664\u5143\u7d20 */\npublic int remove(int index) {\nif (index &lt; 0 || index &gt;= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\nint num = nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int j = index; j &lt; size - 1; j++) {\nnums[j] = nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nsize--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npublic void extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nnums = Arrays.copyOf(nums, capacity() * extendRatio);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\ncapacity = nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npublic int[] toArray() {\nint size = size();\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] nums = new int[size];\nfor (int i = 0; i &lt; size; i++) {\nnums[i] = get(i);\n}\nreturn nums;\n}\n}\n</code></pre> my_list.cpp<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate:\nint *nums; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nint numsCapacity = 10; // \u5217\u8868\u5bb9\u91cf\nint numsSize = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nint extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nMyList() {\nnums = new int[numsCapacity];\n}\n/* \u6790\u6784\u65b9\u6cd5 */\n~MyList() {\ndelete[] nums;\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nint size() {\nreturn numsSize;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity() {\nreturn numsCapacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(int index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index &lt; 0 || index &gt;= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(int index, int num) {\nif (index &lt; 0 || index &gt;= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\nnums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(int num) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size() == capacity())\nextendCapacity();\nnums[size()] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(int index, int num) {\nif (index &lt; 0 || index &gt;= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size() == capacity())\nextendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int j = size() - 1; j &gt;= index; j--) {\nnums[j + 1] = nums[j];\n}\nnums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u5220\u9664\u5143\u7d20 */\nint remove(int index) {\nif (index &lt; 0 || index &gt;= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\nint num = nums[index];\n// \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int j = index; j &lt; size() - 1; j++) {\nnums[j] = nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\nint newCapacity = capacity() * extendRatio;\nint *tmp = nums;\nnums = new int[newCapacity];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i &lt; size(); i++) {\nnums[i] = tmp[i];\n}\n// \u91ca\u653e\u5185\u5b58\ndelete[] tmp;\nnumsCapacity = newCapacity;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Vector \u7528\u4e8e\u6253\u5370 */\nvector&lt;int&gt; toVector() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvector&lt;int&gt; vec(size());\nfor (int i = 0; i &lt; size(); i++) {\nvec[i] = nums[i];\n}\nreturn vec;\n}\n};\n</code></pre> my_list.py<pre><code>class MyList:\n\"\"\"\u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__capacity: int = 10 # \u5217\u8868\u5bb9\u91cf\nself.__nums: list[int] = [0] * self.__capacity # \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nself.__size: int = 0 # \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nself.__extend_ratio: int = 2 # \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\ndef size(self) -&gt; int:\n\"\"\"\u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\"\"\"\nreturn self.__size\ndef capacity(self) -&gt; int:\n\"\"\"\u83b7\u53d6\u5217\u8868\u5bb9\u91cf\"\"\"\nreturn self.__capacity\ndef get(self, index: int) -&gt; int:\n\"\"\"\u8bbf\u95ee\u5143\u7d20\"\"\"\n# \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif index &lt; 0 or index &gt;= self.__size:\nraise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\nreturn self.__nums[index]\ndef set(self, num: int, index: int):\n\"\"\"\u66f4\u65b0\u5143\u7d20\"\"\"\nif index &lt; 0 or index &gt;= self.__size:\nraise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\nself.__nums[index] = num\ndef add(self, num: int):\n\"\"\"\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\"\"\"\n# \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif self.size() == self.capacity():\nself.extend_capacity()\nself.__nums[self.__size] = num\nself.__size += 1\ndef insert(self, num: int, index: int):\n\"\"\"\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\"\"\"\nif index &lt; 0 or index &gt;= self.__size:\nraise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n# \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif self.__size == self.capacity():\nself.extend_capacity()\n# \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j in range(self.__size - 1, index - 1, -1):\nself.__nums[j + 1] = self.__nums[j]\nself.__nums[index] = num\n# \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.__size += 1\ndef remove(self, index: int) -&gt; int:\n\"\"\"\u5220\u9664\u5143\u7d20\"\"\"\nif index &lt; 0 or index &gt;= self.__size:\nraise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\nnum = self.__nums[index]\n# \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j in range(index, self.__size - 1):\nself.__nums[j] = self.__nums[j + 1]\n# \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.__size -= 1\n# \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num\ndef extend_capacity(self):\n\"\"\"\u5217\u8868\u6269\u5bb9\"\"\"\n# \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 __extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nself.__nums = self.__nums + [0] * self.capacity() * (self.__extend_ratio - 1)\n# \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself.__capacity = len(self.__nums)\ndef to_array(self) -&gt; list[int]:\n\"\"\"\u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868\"\"\"\nreturn self.__nums[: self.__size]\n</code></pre> my_list.go<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\ntype myList struct {\nnumsCapacity int\nnums []int\nnumsSize int\nextendRatio int\n}\n/* \u6784\u9020\u51fd\u6570 */\nfunc newMyList() *myList {\nreturn &amp;myList{\nnumsCapacity: 10, // \u5217\u8868\u5bb9\u91cf\nnums: make([]int, 10), // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nnumsSize: 0, // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nextendRatio: 2, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n}\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\nfunc (l *myList) size() int {\nreturn l.numsSize\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc (l *myList) capacity() int {\nreturn l.numsCapacity\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nfunc (l *myList) get(index int) int {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif index &lt; 0 || index &gt;= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nreturn l.nums[index]\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc (l *myList) set(num, index int) {\nif index &lt; 0 || index &gt;= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nl.nums[index] = num\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nfunc (l *myList) add(num int) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif l.numsSize == l.numsCapacity {\nl.extendCapacity()\n}\nl.nums[l.numsSize] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.numsSize++\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc (l *myList) insert(num, index int) {\nif index &lt; 0 || index &gt;= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif l.numsSize == l.numsCapacity {\nl.extendCapacity()\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j := l.numsSize - 1; j &gt;= index; j-- {\nl.nums[j+1] = l.nums[j]\n}\nl.nums[index] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.numsSize++\n}\n/* \u5220\u9664\u5143\u7d20 */\nfunc (l *myList) remove(index int) int {\nif index &lt; 0 || index &gt;= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nnum := l.nums[index]\n// \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j := index; j &lt; l.numsSize-1; j++ {\nl.nums[j] = l.nums[j+1]\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.numsSize--\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num\n}\n/* \u5217\u8868\u6269\u5bb9 */\nfunc (l *myList) extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nl.nums = append(l.nums, make([]int, l.numsCapacity*(l.extendRatio-1))...)\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nl.numsCapacity = len(l.nums)\n}\n/* \u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868 */\nfunc (l *myList) toArray() []int {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nreturn l.nums[:l.numsSize]\n}\n</code></pre> my_list.js<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\n#nums = new Array(); // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n#capacity = 10; // \u5217\u8868\u5bb9\u91cf\n#size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n#extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#nums = new Array(this.#capacity);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nsize() {\nreturn this.#size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\ncapacity() {\nreturn this.#capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nget(index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index &lt; 0 || index &gt;= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nreturn this.#nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nset(index, num) {\nif (index &lt; 0 || index &gt;= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nthis.#nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nadd(num) {\n// \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\nif (this.#size === this.#capacity) {\nthis.extendCapacity();\n}\n// \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\nthis.#nums[this.#size] = num;\nthis.#size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\ninsert(index, num) {\nif (index &lt; 0 || index &gt;= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (this.#size === this.#capacity) {\nthis.extendCapacity();\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (let j = this.#size - 1; j &gt;= index; j--) {\nthis.#nums[j + 1] = this.#nums[j];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.#nums[index] = num;\nthis.#size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\nremove(index) {\nif (index &lt; 0 || index &gt;= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nlet num = this.#nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (let j = index; j &lt; this.#size - 1; j++) {\nthis.#nums[j] = this.#nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.#size--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nextendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nthis.#nums = this.#nums.concat(\nnew Array(this.capacity() * (this.#extendRatio - 1))\n);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nthis.#capacity = this.#nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\ntoArray() {\nlet size = this.size();\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst nums = new Array(size);\nfor (let i = 0; i &lt; size; i++) {\nnums[i] = this.get(i);\n}\nreturn nums;\n}\n}\n</code></pre> my_list.ts<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate nums: Array&lt;number&gt;; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate _capacity: number = 10; // \u5217\u8868\u5bb9\u91cf\nprivate _size: number = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate extendRatio: number = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.nums = new Array(this._capacity);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\npublic size(): number {\nreturn this._size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npublic capacity(): number {\nreturn this._capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npublic get(index: number): number {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index &lt; 0 || index &gt;= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nreturn this.nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npublic set(index: number, num: number): void {\nif (index &lt; 0 || index &gt;= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nthis.nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npublic add(num: number): void {\n// \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\nif (this._size === this._capacity) this.extendCapacity();\n// \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\nthis.nums[this._size] = num;\nthis._size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npublic insert(index: number, num: number): void {\nif (index &lt; 0 || index &gt;= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (this._size === this._capacity) {\nthis.extendCapacity();\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (let j = this._size - 1; j &gt;= index; j--) {\nthis.nums[j + 1] = this.nums[j];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.nums[index] = num;\nthis._size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\npublic remove(index: number): number {\nif (index &lt; 0 || index &gt;= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\nlet num = this.nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (let j = index; j &lt; this._size - 1; j++) {\nthis.nums[j] = this.nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis._size--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npublic extendCapacity(): void {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nthis.nums = this.nums.concat(\nnew Array(this.capacity() * (this.extendRatio - 1))\n);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nthis._capacity = this.nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npublic toArray(): number[] {\nlet size = this.size();\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst nums = new Array(size);\nfor (let i = 0; i &lt; size; i++) {\nnums[i] = this.get(i);\n}\nreturn nums;\n}\n}\n</code></pre> my_list.c<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nstruct myList {\nint *nums; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nint capacity; // \u5217\u8868\u5bb9\u91cf\nint size; // \u5217\u8868\u5927\u5c0f\nint extendRatio; // \u5217\u8868\u6bcf\u6b21\u6269\u5bb9\u7684\u500d\u6570\n};\ntypedef struct myList myList;\n/* \u6784\u9020\u51fd\u6570 */\nmyList *newMyList() {\nmyList *list = malloc(sizeof(myList));\nlist-&gt;capacity = 10;\nlist-&gt;nums = malloc(sizeof(int) * list-&gt;capacity);\nlist-&gt;size = 0;\nlist-&gt;extendRatio = 2;\nreturn list;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delMyList(myList *list) {\nfree(list-&gt;nums);\nfree(list);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6 */\nint size(myList *list) {\nreturn list-&gt;size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity(myList *list) {\nreturn list-&gt;capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(myList *list, int index) {\nassert(index &gt;= 0 &amp;&amp; index &lt; list-&gt;size);\nreturn list-&gt;nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(myList *list, int index, int num) {\nassert(index &gt;= 0 &amp;&amp; index &lt; list-&gt;size);\nlist-&gt;nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(myList *list, int num) {\nif (size(list) == capacity(list)) {\nextendCapacity(list); // \u6269\u5bb9\n}\nlist-&gt;nums[size(list)] = num;\nlist-&gt;size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(myList *list, int index, int num) {\nassert(index &gt;= 0 &amp;&amp; index &lt; size(list));\nfor (int i = size(list); i &gt; index; --i) {\nlist-&gt;nums[i] = list-&gt;nums[i - 1];\n}\nlist-&gt;nums[index] = num;\nlist-&gt;size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nint removeNum(myList *list, int index) {\nassert(index &gt;= 0 &amp;&amp; index &lt; size(list));\nint num = list-&gt;nums[index];\nfor (int i = index; i &lt; size(list) - 1; i++) {\nlist-&gt;nums[i] = list-&gt;nums[i + 1];\n}\nlist-&gt;size--;\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity(myList *list) {\n// \u5148\u5206\u914d\u7a7a\u95f4\nint newCapacity = capacity(list) * list-&gt;extendRatio;\nint *extend = (int *)malloc(sizeof(int) * newCapacity);\nint *temp = list-&gt;nums;\n// \u62f7\u8d1d\u65e7\u6570\u636e\u5230\u65b0\u6570\u636e\nfor (int i = 0; i &lt; size(list); i++)\nextend[i] = list-&gt;nums[i];\n// \u91ca\u653e\u65e7\u6570\u636e\nfree(temp);\n// \u66f4\u65b0\u65b0\u6570\u636e\nlist-&gt;nums = extend;\nlist-&gt;capacity = newCapacity;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Array \u7528\u4e8e\u6253\u5370 */\nint *toArray(myList *list) {\nreturn list-&gt;nums;\n}\n</code></pre> my_list.cs<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate int[] nums; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate int numsCapacity = 10; // \u5217\u8868\u5bb9\u91cf\nprivate int numsSize = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\npublic MyList() {\nnums = new int[numsCapacity];\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\npublic int size() {\nreturn numsSize;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npublic int capacity() {\nreturn numsCapacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npublic int get(int index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index &lt; 0 || index &gt;= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npublic void set(int index, int num) {\nif (index &lt; 0 || index &gt;= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nnums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npublic void add(int num) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (numsSize == numsCapacity)\nextendCapacity();\nnums[numsSize] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npublic void insert(int index, int num) {\nif (index &lt; 0 || index &gt;= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (numsSize == numsCapacity)\nextendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int j = numsSize - 1; j &gt;= index; j--) {\nnums[j + 1] = nums[j];\n}\nnums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u5220\u9664\u5143\u7d20 */\npublic int remove(int index) {\nif (index &lt; 0 || index &gt;= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nint num = nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int j = index; j &lt; numsSize - 1; j++) {\nnums[j] = nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npublic void extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a numsCapacity * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nArray.Resize(ref nums, numsCapacity * extendRatio);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nnumsCapacity = nums.Length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] nums = new int[numsSize];\nfor (int i = 0; i &lt; numsSize; i++) {\nnums[i] = get(i);\n}\nreturn nums;\n}\n}\n</code></pre> my_list.swift<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate var nums: [Int] // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate var _capacity = 10 // \u5217\u8868\u5bb9\u91cf\nprivate var _size = 0 // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate let extendRatio = 2 // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\ninit() {\nnums = Array(repeating: 0, count: _capacity)\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nfunc size() -&gt; Int {\n_size\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc capacity() -&gt; Int {\n_capacity\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nfunc get(index: Int) -&gt; Int {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u9519\u8bef\uff0c\u4e0b\u540c\nif index &lt; 0 || index &gt;= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nreturn nums[index]\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc set(index: Int, num: Int) {\nif index &lt; 0 || index &gt;= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nnums[index] = num\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nfunc add(num: Int) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif _size == _capacity {\nextendCapacity()\n}\nnums[_size] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size += 1\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc insert(index: Int, num: Int) {\nif index &lt; 0 || index &gt;= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif _size == _capacity {\nextendCapacity()\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j in sequence(first: _size - 1, next: { $0 &gt;= index + 1 ? $0 - 1 : nil }) {\nnums[j + 1] = nums[j]\n}\nnums[index] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size += 1\n}\n/* \u5220\u9664\u5143\u7d20 */\n@discardableResult\nfunc remove(index: Int) -&gt; Int {\nif index &lt; 0 || index &gt;= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nlet num = nums[index]\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j in index ..&lt; (_size - 1) {\nnums[j] = nums[j + 1]\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size -= 1\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num\n}\n/* \u5217\u8868\u6269\u5bb9 */\nfunc extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nnums = nums + Array(repeating: 0, count: _capacity * (extendRatio - 1))\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n_capacity = nums.count\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\nfunc toArray() -&gt; [Int] {\nvar nums = Array(repeating: 0, count: _size)\nfor i in 0 ..&lt; _size {\nnums[i] = get(index: i)\n}\nreturn nums\n}\n}\n</code></pre> my_list.zig<pre><code>// \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0\nfn MyList(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nnums: []T = undefined, // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nnumsCapacity: usize = 10, // \u5217\u8868\u5bb9\u91cf\nnumSize: usize = 0, // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nextendRatio: usize = 2, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u5217\u8868\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.nums = try self.mem_allocator.alloc(T, self.numsCapacity);\n@memset(self.nums, @as(T, 0));\n}\n// \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\npub fn size(self: *Self) usize {\nreturn self.numSize;\n}\n// \u83b7\u53d6\u5217\u8868\u5bb9\u91cf\npub fn capacity(self: *Self) usize {\nreturn self.numsCapacity;\n}\n// \u8bbf\u95ee\u5143\u7d20\npub fn get(self: *Self, index: usize) T {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index &lt; 0 or index &gt;= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn self.nums[index];\n} // \u66f4\u65b0\u5143\u7d20\npub fn set(self: *Self, index: usize, num: T) void {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index &lt; 0 or index &gt;= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\nself.nums[index] = num;\n} // \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\npub fn add(self: *Self, num: T) !void {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (self.size() == self.capacity()) try self.extendCapacity();\nself.nums[self.size()] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.numSize += 1;\n} // \u4e2d\u95f4\u63d2\u5165\u5143\u7d20\npub fn insert(self: *Self, index: usize, num: T) !void {\nif (index &lt; 0 or index &gt;= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (self.size() == self.capacity()) try self.extendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nvar j = self.size() - 1;\nwhile (j &gt;= index) : (j -= 1) {\nself.nums[j + 1] = self.nums[j];\n}\nself.nums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.numSize += 1;\n}\n// \u5220\u9664\u5143\u7d20\npub fn remove(self: *Self, index: usize) T {\nif (index &lt; 0 or index &gt;= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\nvar num = self.nums[index];\n// \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nvar j = index;\nwhile (j &lt; self.size() - 1) : (j += 1) {\nself.nums[j] = self.nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.numSize -= 1;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n// \u5217\u8868\u6269\u5bb9\npub fn extendCapacity(self: *Self) !void {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nvar newCapacity = self.capacity() * self.extendRatio;\nvar extend = try self.mem_allocator.alloc(T, newCapacity);\n@memset(extend, @as(T, 0));\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nstd.mem.copy(T, extend, self.nums);\nself.nums = extend;\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself.numsCapacity = newCapacity;\n}\n// \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar nums = try self.mem_allocator.alloc(T, self.size());\n@memset(nums, @as(T, 0));\nfor (nums, 0..) |*num, i| {\nnum.* = self.get(i);\n}\nreturn nums;\n}\n};\n}\n</code></pre> my_list.dart<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nlate List&lt;int&gt; _nums; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nint _capacity = 10; // \u5217\u8868\u5bb9\u91cf\nint _size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nint _extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\nMyList() {\n_nums = List.filled(_capacity, 0);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nint size() =&gt; _size;\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity() =&gt; _capacity;\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(int index) {\nif (index &gt;= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\nreturn _nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(int index, int num) {\nif (index &gt;= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n_nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(int num) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (_size == _capacity) extendCapacity();\n_nums[_size] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(int index, int num) {\nif (index &gt;= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (_size == _capacity) extendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (var j = _size - 1; j &gt;= index; j--) {\n_nums[j + 1] = _nums[j];\n}\n_nums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\nint remove(int index) {\nif (index &gt;= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\nint num = _nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (var j = index; j &lt; _size - 1; j++) {\n_nums[j] = _nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 _extendRatio \u500d\u7684\u65b0\u6570\u7ec4\nfinal _newNums = List.filled(_capacity * _extendRatio, 0);\n// \u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nList.copyRange(_newNums, 0, _nums);\n// \u66f4\u65b0 _nums \u7684\u5f15\u7528\n_nums = _newNums;\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n_capacity = _nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\nList&lt;int&gt; toArray() {\nList&lt;int&gt; nums = [];\nfor (var i = 0; i &lt; _size; i++) {\nnums.add(get(i));\n}\nreturn nums;\n}\n}\n</code></pre> my_list.rs<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\n#[allow(dead_code)]\nstruct MyList {\nnums: Vec&lt;i32&gt;, // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\ncapacity: usize, // \u5217\u8868\u5bb9\u91cf\nsize: usize, // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nextend_ratio: usize, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n}\n#[allow(unused,unused_comparisons)]\nimpl MyList {\n/* \u6784\u9020\u65b9\u6cd5 */\npub fn new(capacity: usize) -&gt; Self {\nlet mut vec = Vec::new(); vec.resize(capacity, 0);\nSelf {\nnums: vec,\ncapacity,\nsize: 0,\nextend_ratio: 2,\n}\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\npub fn size(&amp;self) -&gt; usize {\nreturn self.size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npub fn capacity(&amp;self) -&gt; usize {\nreturn self.capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npub fn get(&amp;self, index: usize) -&gt; i32 {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif index &lt; 0 || index &gt;= self.size {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\nreturn self.nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npub fn set(&amp;mut self, index: usize, num: i32) {\nif index &lt; 0 || index &gt;= self.size {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\nself.nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npub fn add(&amp;mut self, num: i32) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif self.size == self.capacity() {\nself.extend_capacity();\n}\nself.nums[self.size] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.size += 1;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npub fn insert(&amp;mut self, index: usize, num: i32) {\nif index &lt; 0 || index &gt;= self.size() {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif self.size == self.capacity() {\nself.extend_capacity();\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j in (index..self.size).rev() {\nself.nums[j + 1] = self.nums[j];\n}\nself.nums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.size += 1;\n}\n/* \u5220\u9664\u5143\u7d20 */\npub fn remove(&amp;mut self, index: usize) -&gt; i32 {\nif index &lt; 0 || index &gt;= self.size() {panic!(\"\u7d22\u5f15\u8d8a\u754c\")};\nlet num = self.nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j in (index..self.size - 1) {\nself.nums[j] = self.nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.size -= 1;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npub fn extend_capacity(&amp;mut self) {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nlet new_capacity = self.capacity * self.extend_ratio;\nself.nums.resize(new_capacity, 0);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself.capacity = new_capacity;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npub fn to_array(&amp;mut self) -&gt; Vec&lt;i32&gt; {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nlet mut nums = Vec::new();\nfor i in 0..self.size {\nnums.push(self.get(i));\n}\nnums\n}\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/summary/","title":"4.4. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u6570\u7ec4\u548c\u94fe\u8868\u662f\u4e24\u79cd\u57fa\u672c\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5206\u522b\u4ee3\u8868\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u4e24\u79cd\u5b58\u50a8\u65b9\u5f0f\uff1a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u548c\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8\u3002\u4e24\u8005\u7684\u7279\u70b9\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u6027\u3002</li> <li>\u6570\u7ec4\u652f\u6301\u968f\u673a\u8bbf\u95ee\u3001\u5360\u7528\u5185\u5b58\u8f83\u5c11\uff1b\u4f46\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u6548\u7387\u4f4e\uff0c\u4e14\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002</li> <li>\u94fe\u8868\u901a\u8fc7\u66f4\u6539\u5f15\u7528\uff08\u6307\u9488\uff09\u5b9e\u73b0\u9ad8\u6548\u7684\u8282\u70b9\u63d2\u5165\u4e0e\u5220\u9664\uff0c\u4e14\u53ef\u4ee5\u7075\u6d3b\u8c03\u6574\u957f\u5ea6\uff1b\u4f46\u8282\u70b9\u8bbf\u95ee\u6548\u7387\u4f4e\u3001\u5360\u7528\u5185\u5b58\u8f83\u591a\u3002\u5e38\u89c1\u7684\u94fe\u8868\u7c7b\u578b\u5305\u62ec\u5355\u5411\u94fe\u8868\u3001\u5faa\u73af\u94fe\u8868\u3001\u53cc\u5411\u94fe\u8868\u3002</li> <li>\u52a8\u6001\u6570\u7ec4\uff0c\u53c8\u79f0\u5217\u8868\uff0c\u662f\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\u3002\u5b83\u4fdd\u7559\u4e86\u6570\u7ec4\u7684\u4f18\u52bf\uff0c\u540c\u65f6\u53ef\u4ee5\u7075\u6d3b\u8c03\u6574\u957f\u5ea6\u3002\u5217\u8868\u7684\u51fa\u73b0\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u6570\u7ec4\u7684\u6613\u7528\u6027\uff0c\u4f46\u53ef\u80fd\u5bfc\u81f4\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u3002</li> </ul>"},{"location":"chapter_array_and_linkedlist/summary/#441-q-a","title":"4.4.1. \u00a0 Q &amp; A","text":"<p>\u6570\u7ec4\u5b58\u50a8\u5728\u6808\u4e0a\u548c\u5b58\u50a8\u5728\u5806\u4e0a\uff0c\u5bf9\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u662f\u5426\u6709\u5f71\u54cd\uff1f</p> <p>\u6808\u5185\u5b58\u5206\u914d\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u5b8c\u6210\uff0c\u800c\u5806\u5185\u5b58\u7531\u7a0b\u5e8f\u5458\u5728\u4ee3\u7801\u4e2d\u5206\u914d\uff08\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684\u6808\u548c\u5806\u548c\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u6808\u548c\u5806\u4e0d\u662f\u540c\u4e00\u6982\u5ff5\uff09\u3002</p> <ol> <li>\u6808\u4e0d\u7075\u6d3b\uff0c\u5206\u914d\u7684\u5185\u5b58\u5927\u5c0f\u4e0d\u53ef\u66f4\u6539\uff1b\u5806\u76f8\u5bf9\u7075\u6d3b\uff0c\u53ef\u4ee5\u52a8\u6001\u5206\u914d\u5185\u5b58\u3002</li> <li>\u6808\u662f\u4e00\u5757\u6bd4\u8f83\u5c0f\u7684\u5185\u5b58\uff0c\u5bb9\u6613\u51fa\u73b0\u5185\u5b58\u4e0d\u8db3\uff1b\u5806\u5185\u5b58\u5f88\u5927\uff0c\u4f46\u662f\u7531\u4e8e\u662f\u52a8\u6001\u5206\u914d\uff0c\u5bb9\u6613\u788e\u7247\u5316\uff0c\u7ba1\u7406\u5806\u5185\u5b58\u7684\u96be\u5ea6\u66f4\u5927\u3001\u6210\u672c\u66f4\u9ad8\u3002</li> <li>\u8bbf\u95ee\u6808\u6bd4\u8bbf\u95ee\u5806\u66f4\u5feb\uff0c\u56e0\u4e3a\u6808\u5185\u5b58\u8f83\u5c0f\u3001\u5bf9\u7f13\u5b58\u53cb\u597d\uff0c\u5806\u5e27\u5206\u6563\u5728\u5f88\u5927\u7684\u7a7a\u95f4\u5185\uff0c\u4f1a\u51fa\u73b0\u66f4\u591a\u7684\u7f13\u5b58\u672a\u547d\u4e2d\u3002</li> </ol> <p>\u4e3a\u4ec0\u4e48\u6570\u7ec4\u8981\u6c42\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u800c\u5728\u94fe\u8868\u4e2d\u5374\u6ca1\u6709\u5f3a\u8c03\u540c\u7c7b\u578b\u5462\uff1f</p> <p>\u94fe\u8868\u7531\u7ed3\u70b9\u7ec4\u6210\uff0c\u7ed3\u70b9\u4e4b\u95f4\u901a\u8fc7\u5f15\u7528\uff08\u6307\u9488\uff09\u8fde\u63a5\uff0c\u5404\u4e2a\u7ed3\u70b9\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u4f8b\u5982 int, double, string, object \u7b49\u3002</p> <p>\u76f8\u5bf9\u5730\uff0c\u6570\u7ec4\u5143\u7d20\u5219\u5fc5\u987b\u662f\u76f8\u540c\u7c7b\u578b\u7684\uff0c\u8fd9\u6837\u624d\u80fd\u901a\u8fc7\u8ba1\u7b97\u504f\u79fb\u91cf\u6765\u83b7\u53d6\u5bf9\u5e94\u5143\u7d20\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6570\u7ec4\u540c\u65f6\u5305\u542b int \u548c long \u4e24\u79cd\u7c7b\u578b\uff0c\u5355\u4e2a\u5143\u7d20\u5206\u522b\u5360\u7528 4 bytes \u548c 8 bytes \uff0c\u90a3\u4e48\u6b64\u65f6\u5c31\u4e0d\u80fd\u7528\u4ee5\u4e0b\u516c\u5f0f\u8ba1\u7b97\u504f\u79fb\u91cf\u4e86\uff0c\u56e0\u4e3a\u6570\u7ec4\u4e2d\u5305\u542b\u4e86\u4e24\u79cd <code>elementLength</code> \u3002</p> <pre><code>// \u5143\u7d20\u5185\u5b58\u5730\u5740 = \u6570\u7ec4\u5185\u5b58\u5730\u5740 + \u5143\u7d20\u957f\u5ea6 * \u5143\u7d20\u7d22\u5f15\nelementAddr = firtstElementAddr + elementLength * elementIndex\n</code></pre> <p>\u5220\u9664\u8282\u70b9\u540e\uff0c\u662f\u5426\u9700\u8981\u628a <code>P.next</code> \u8bbe\u4e3a \\(\\text{None}\\) \u5462\uff1f</p> <p>\u4e0d\u4fee\u6539 <code>P.next</code> \u4e5f\u53ef\u4ee5\u3002\u4ece\u8be5\u94fe\u8868\u7684\u89d2\u5ea6\u770b\uff0c\u4ece\u5934\u7ed3\u70b9\u904d\u5386\u5230\u5c3e\u7ed3\u70b9\u5df2\u7ecf\u9047\u4e0d\u5230 <code>P</code> \u4e86\u3002\u8fd9\u610f\u5473\u7740\u7ed3\u70b9 <code>P</code> \u5df2\u7ecf\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u4e86\uff0c\u6b64\u65f6\u7ed3\u70b9 <code>P</code> \u6307\u5411\u54ea\u91cc\u90fd\u4e0d\u4f1a\u5bf9\u8fd9\u6761\u94fe\u8868\u4ea7\u751f\u5f71\u54cd\u4e86\u3002</p> <p>\u4ece\u5783\u573e\u56de\u6536\u7684\u89d2\u5ea6\u770b\uff0c\u5bf9\u4e8e Java, Python, Go \u7b49\u62e5\u6709\u81ea\u52a8\u5783\u573e\u56de\u6536\u7684\u8bed\u8a00\u6765\u8bf4\uff0c\u8282\u70b9 <code>P</code> \u662f\u5426\u88ab\u56de\u6536\u53d6\u51b3\u4e8e\u662f\u5426\u6709\u4ecd\u5b58\u5728\u6307\u5411\u5b83\u7684\u5f15\u7528\uff0c\u800c\u4e0d\u662f <code>P.next</code> \u7684\u503c\u3002\u5728 C, C++ \u7b49\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u624b\u52a8\u91ca\u653e\u8282\u70b9\u5185\u5b58\u3002</p> <p>\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002\u4f46\u662f\u589e\u5220\u4e4b\u524d\u90fd\u9700\u8981 \\(O(n)\\) \u67e5\u627e\u5143\u7d20\uff0c\u90a3\u4e3a\u4ec0\u4e48\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u662f \\(O(n)\\) \u5462\uff1f</p> <p>\u5982\u679c\u662f\u5148\u67e5\u627e\u5143\u7d20\u3001\u518d\u5220\u9664\u5143\u7d20\uff0c\u786e\u5b9e\u662f \\(O(n)\\) \u3002\u7136\u800c\uff0c\u94fe\u8868\u7684 \\(O(1)\\) \u589e\u5220\u7684\u4f18\u52bf\u53ef\u4ee5\u5728\u5176\u4ed6\u5e94\u7528\u4e0a\u5f97\u5230\u4f53\u73b0\u3002\u4f8b\u5982\uff0c\u53cc\u5411\u961f\u5217\u9002\u5408\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\uff0c\u6211\u4eec\u7ef4\u62a4\u4e00\u4e2a\u6307\u9488\u53d8\u91cf\u59cb\u7ec8\u6307\u5411\u5934\u7ed3\u70b9\u3001\u5c3e\u7ed3\u70b9\uff0c\u6bcf\u6b21\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u90fd\u662f \\(O(1)\\) \u3002</p> <p>\u56fe\u7247\u201c\u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f\u201d\u4e2d\uff0c\u6d45\u84dd\u8272\u7684\u5b58\u50a8\u7ed3\u70b9\u6307\u9488\u662f\u5360\u7528\u4e00\u5757\u5185\u5b58\u5730\u5740\u5417\uff1f\u8fd8\u662f\u548c\u7ed3\u70b9\u503c\u5404\u5360\u4e00\u534a\u5462\uff1f</p> <p>\u6587\u4e2d\u53ea\u662f\u4e00\u4e2a\u793a\u610f\u56fe\uff0c\u53ea\u662f\u5b9a\u6027\u8868\u793a\u3002\u5b9a\u91cf\u7684\u8bdd\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u5206\u6790\uff1a</p> <ul> <li>\u4e0d\u540c\u7c7b\u578b\u7684\u7ed3\u70b9\u503c\u5360\u7528\u7684\u7a7a\u95f4\u662f\u4e0d\u540c\u7684\uff0c\u6bd4\u5982 int, long, double, \u6216\u8005\u662f\u7c7b\u7684\u5b9e\u4f8b\u7b49\u7b49\u3002</li> <li>\u6307\u9488\u53d8\u91cf\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u5927\u5c0f\u6839\u636e\u6240\u4f7f\u7528\u7684\u64cd\u4f5c\u7cfb\u7edf\u53ca\u7f16\u8bd1\u73af\u5883\u800c\u5b9a\uff0c\u5927\u591a\u4e3a 8 \u5b57\u8282\u6216 4 \u5b57\u8282\u3002</li> </ul> <p>\u5728\u5217\u8868\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20\u662f\u5426\u65f6\u65f6\u523b\u523b\u90fd\u4e3a \\(O(1)\\) \uff1f</p> <p>\u5982\u679c\u6dfb\u52a0\u5143\u7d20\u65f6\u8d85\u51fa\u5217\u8868\u957f\u5ea6\uff0c\u5219\u9700\u8981\u5148\u6269\u5bb9\u5217\u8868\u518d\u6dfb\u52a0\u3002\u7cfb\u7edf\u4f1a\u7533\u8bf7\u4e00\u5757\u65b0\u7684\u5185\u5b58\uff0c\u5e76\u5c06\u539f\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u642c\u8fd0\u8fc7\u53bb\uff0c\u8fd9\u65f6\u5019\u65f6\u95f4\u590d\u6742\u5ea6\u5c31\u4f1a\u662f \\(O(n)\\) \u3002</p> <p>\u201c\u5217\u8868\u7684\u51fa\u73b0\u5927\u5927\u63d0\u5347\u4e86\u6570\u7ec4\u7684\u5b9e\u7528\u6027\uff0c\u4f46\u526f\u4f5c\u7528\u662f\u4f1a\u9020\u6210\u90e8\u5206\u5185\u5b58\u7a7a\u95f4\u6d6a\u8d39\u201d\uff0c\u8fd9\u91cc\u7684\u7a7a\u95f4\u6d6a\u8d39\u662f\u6307\u989d\u5916\u589e\u52a0\u7684\u53d8\u91cf\u5982\u5bb9\u91cf\u3001\u957f\u5ea6\u3001\u6269\u5bb9\u500d\u6570\u6240\u5360\u7684\u5185\u5b58\u5417\uff1f</p> <p>\u8fd9\u91cc\u7684\u7a7a\u95f4\u6d6a\u8d39\u4e3b\u8981\u6709\u4e24\u65b9\u9762\u542b\u4e49\uff1a\u4e00\u65b9\u9762\uff0c\u5217\u8868\u90fd\u4f1a\u8bbe\u5b9a\u4e00\u4e2a\u521d\u59cb\u957f\u5ea6\uff0c\u6211\u4eec\u4e0d\u4e00\u5b9a\u9700\u8981\u7528\u8fd9\u4e48\u591a\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u4e3a\u4e86\u9632\u6b62\u9891\u7e41\u6269\u5bb9\uff0c\u6269\u5bb9\u4e00\u822c\u90fd\u4f1a\u4e58\u4ee5\u4e00\u4e2a\u7cfb\u6570\uff0c\u6bd4\u5982 \\(\\times 1.5\\) \u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4e5f\u4f1a\u51fa\u73b0\u5f88\u591a\u7a7a\u4f4d\uff0c\u6211\u4eec\u901a\u5e38\u4e0d\u80fd\u5b8c\u5168\u586b\u6ee1\u5b83\u4eec\u3002</p> <p>\u5728 Python \u4e2d\u521d\u59cb\u5316 <code>n = [1, 2, 3]</code> \u540e\uff0c\u8fd9 3 \u4e2a\u5143\u7d20\u7684\u5730\u5740\u662f\u76f8\u8fde\u7684\uff0c\u4f46\u662f\u521d\u59cb\u5316 <code>m = [2, 1, 3]</code> \u4f1a\u53d1\u73b0\u5b83\u4eec\u6bcf\u4e2a\u5143\u7d20\u7684 id \u5e76\u4e0d\u662f\u8fde\u7eed\u7684\uff0c\u800c\u662f\u5206\u522b\u8ddf <code>n</code> \u4e2d\u7684\u76f8\u540c\u3002\u8fd9\u4e9b\u5143\u7d20\u5730\u5740\u4e0d\u8fde\u7eed\uff0c\u90a3\u4e48 <code>m</code> \u8fd8\u662f\u6570\u7ec4\u5417\uff1f</p> <p>\u5047\u5982\u628a\u5217\u8868\u5143\u7d20\u6362\u6210\u94fe\u8868\u8282\u70b9 <code>n = [n1, n2, n3, n4, n5]</code> \uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u8fd9\u4e94\u4e2a\u8282\u70b9\u5bf9\u8c61\u4e5f\u662f\u88ab\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\u7684\u3002\u7136\u800c\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5217\u8868\u7d22\u5f15\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u5230\u8282\u70b9\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u8bbf\u95ee\u5230\u5bf9\u5e94\u7684\u8282\u70b9\u3002\u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u4e2d\u5b58\u50a8\u7684\u662f\u8282\u70b9\u7684\u5f15\u7528\uff0c\u800c\u975e\u8282\u70b9\u672c\u8eab\u3002</p> <p>\u4e0e\u8bb8\u591a\u8bed\u8a00\u4e0d\u540c\u7684\u662f\uff0c\u5728 Python \u4e2d\u6570\u5b57\u4e5f\u88ab\u5305\u88c5\u4e3a\u5bf9\u8c61\uff0c\u5217\u8868\u4e2d\u5b58\u50a8\u7684\u4e0d\u662f\u6570\u5b57\u672c\u8eab\uff0c\u800c\u662f\u5bf9\u6570\u5b57\u7684\u5f15\u7528\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4f1a\u53d1\u73b0\u4e24\u4e2a\u6570\u7ec4\u4e2d\u7684\u76f8\u540c\u6570\u5b57\u62e5\u6709\u540c\u4e00\u4e2a id \uff0c\u5e76\u4e14\u8fd9\u4e9b\u6570\u5b57\u7684\u5185\u5b58\u5730\u5740\u662f\u65e0\u9700\u8fde\u7eed\u7684\u3002</p> <p>C++ STL \u91cc\u9762\u7684 std::list \u5df2\u7ecf\u5b9e\u73b0\u4e86\u53cc\u5411\u94fe\u8868\uff0c\u4f46\u597d\u50cf\u4e00\u4e9b\u7b97\u6cd5\u7684\u4e66\u4e0a\u90fd\u4e0d\u600e\u4e48\u76f4\u63a5\u7528\u8fd9\u4e2a\uff0c\u662f\u4e0d\u662f\u6709\u4ec0\u4e48\u5c40\u9650\u6027\u5462?</p> <p>\u4e00\u65b9\u9762\uff0c\u6211\u4eec\u5f80\u5f80\u66f4\u9752\u7750\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u7b97\u6cd5\uff0c\u800c\u53ea\u6709\u5728\u5fc5\u8981\u65f6\u624d\u4f7f\u7528\u94fe\u8868\u3002\u8fd9\u662f\u56e0\u4e3a\uff1a</p> <ol> <li>\u7a7a\u95f4\u5f00\u9500\uff1a\u7531\u4e8e\u6bcf\u4e2a\u5143\u7d20\u9700\u8981\u4e24\u4e2a\u989d\u5916\u7684\u6307\u9488\uff08\u4e00\u4e2a\u7528\u4e8e\u524d\u4e00\u4e2a\u5143\u7d20\uff0c\u4e00\u4e2a\u7528\u4e8e\u540e\u4e00\u4e2a\u5143\u7d20\uff09\uff0c\u6240\u4ee5 <code>std::list</code> \u901a\u5e38\u6bd4 <code>std::vector</code> \u66f4\u5360\u7528\u7a7a\u95f4\u3002</li> <li>\u7f13\u5b58\u4e0d\u53cb\u597d\uff1a\u7531\u4e8e\u6570\u636e\u4e0d\u662f\u8fde\u7eed\u5b58\u653e\u7684\uff0c<code>std::list</code> \u5bf9\u7f13\u5b58\u7684\u5229\u7528\u7387\u8f83\u4f4e\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c<code>std::vector</code> \u7684\u6027\u80fd\u4f1a\u66f4\u597d\u3002</li> </ol> <p>\u53e6\u4e00\u65b9\u9762\uff0c\u5fc5\u8981\u4f7f\u7528\u94fe\u8868\u7684\u60c5\u51b5\u4e3b\u8981\u662f\u4e8c\u53c9\u6811\u548c\u56fe\u3002\u6808\u548c\u961f\u5217\u5f80\u5f80\u4f1a\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684 <code>stack</code> \u548c <code>queue</code> \uff0c\u800c\u975e\u94fe\u8868\u3002</p>"},{"location":"chapter_backtracking/","title":"13. \u00a0 \u56de\u6eaf","text":"<p>Abstract</p> <p>\u6211\u4eec\u5982\u540c\u8ff7\u5bab\u4e2d\u7684\u63a2\u7d22\u8005\uff0c\u5728\u524d\u8fdb\u7684\u9053\u8def\u4e0a\u53ef\u80fd\u4f1a\u9047\u5230\u56f0\u96be\u3002</p> <p>\u56de\u6eaf\u7684\u529b\u91cf\u8ba9\u6211\u4eec\u80fd\u591f\u91cd\u65b0\u5f00\u59cb\uff0c\u4e0d\u65ad\u5c1d\u8bd5\uff0c\u6700\u7ec8\u627e\u5230\u901a\u5f80\u5149\u660e\u7684\u51fa\u53e3\u3002</p>"},{"location":"chapter_backtracking/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>13.1 \u00a0 \u56de\u6eaf\u7b97\u6cd5</li> <li>13.2 \u00a0 \u5168\u6392\u5217\u95ee\u9898</li> <li>13.3 \u00a0 \u5b50\u96c6\u548c\u95ee\u9898</li> <li>13.4 \u00a0 N \u7687\u540e\u95ee\u9898</li> <li>13.5 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_backtracking/backtracking_algorithm/","title":"13.1. \u00a0 \u56de\u6eaf\u7b97\u6cd5","text":"<p>\u300c\u56de\u6eaf\u7b97\u6cd5 Backtracking Algorithm\u300d\u662f\u4e00\u79cd\u901a\u8fc7\u7a77\u4e3e\u6765\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\uff0c\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u662f\u4ece\u4e00\u4e2a\u521d\u59cb\u72b6\u6001\u51fa\u53d1\uff0c\u66b4\u529b\u641c\u7d22\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5f53\u9047\u5230\u6b63\u786e\u7684\u89e3\u5219\u5c06\u5176\u8bb0\u5f55\uff0c\u76f4\u5230\u627e\u5230\u89e3\u6216\u8005\u5c1d\u8bd5\u4e86\u6240\u6709\u53ef\u80fd\u7684\u9009\u62e9\u90fd\u65e0\u6cd5\u627e\u5230\u89e3\u4e3a\u6b62\u3002</p> <p>\u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u91c7\u7528\u300c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u300d\u6765\u904d\u5386\u89e3\u7a7a\u95f4\u3002\u5728\u4e8c\u53c9\u6811\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u524d\u5e8f\u3001\u4e2d\u5e8f\u548c\u540e\u5e8f\u904d\u5386\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5229\u7528\u524d\u5e8f\u904d\u5386\u6784\u9020\u4e00\u4e2a\u56de\u6eaf\u95ee\u9898\uff0c\u9010\u6b65\u4e86\u89e3\u56de\u6eaf\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002</p> <p>\u4f8b\u9898\u4e00</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\uff0c\u641c\u7d22\u5e76\u8bb0\u5f55\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u8282\u70b9\u5217\u8868\u3002</p> <p>\u5bf9\u4e8e\u6b64\u9898\uff0c\u6211\u4eec\u524d\u5e8f\u904d\u5386\u8fd9\u9897\u6811\uff0c\u5e76\u5224\u65ad\u5f53\u524d\u8282\u70b9\u7684\u503c\u662f\u5426\u4e3a \\(7\\) \uff0c\u82e5\u662f\u5219\u5c06\u8be5\u8282\u70b9\u7684\u503c\u52a0\u5165\u5230\u7ed3\u679c\u5217\u8868 <code>res</code> \u4e4b\u4e2d\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust preorder_traversal_i_compact.java<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode root) {\nif (root == null) {\nreturn;\n}\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(root);\n}\npreOrder(root.left);\npreOrder(root.right);\n}\n</code></pre> preorder_traversal_i_compact.cpp<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode *root) {\nif (root == nullptr) {\nreturn;\n}\nif (root-&gt;val == 7) {\n// \u8bb0\u5f55\u89e3\nres.push_back(root);\n}\npreOrder(root-&gt;left);\npreOrder(root-&gt;right);\n}\n</code></pre> preorder_traversal_i_compact.py<pre><code>def pre_order(root: TreeNode):\n\"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00\"\"\"\nif root is None:\nreturn\nif root.val == 7:\n# \u8bb0\u5f55\u89e3\nres.append(root)\npre_order(root.left)\npre_order(root.right)\n</code></pre> preorder_traversal_i_compact.go<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrderI(root *TreeNode, res *[]*TreeNode) {\nif root == nil {\nreturn\n}\nif (root.Val).(int) == 7 {\n// \u8bb0\u5f55\u89e3\n*res = append(*res, root)\n}\npreOrderI(root.Left, res)\npreOrderI(root.Right, res)\n}\n</code></pre> preorder_traversal_i_compact.js<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunction preOrder(root, res) {\nif (root === null) {\nreturn;\n}\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push(root);\n}\npreOrder(root.left, res);\npreOrder(root.right, res);\n}\n</code></pre> preorder_traversal_i_compact.ts<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunction preOrder(root: TreeNode | null, res: TreeNode[]): void {\nif (root === null) {\nreturn;\n}\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push(root);\n}\npreOrder(root.left, res);\npreOrder(root.right, res);\n}\n</code></pre> preorder_traversal_i_compact.c<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_i_compact.cs<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode root) {\nif (root == null) {\nreturn;\n}\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.Add(root);\n}\npreOrder(root.left);\npreOrder(root.right);\n}\n</code></pre> preorder_traversal_i_compact.swift<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nfunc preOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\nif root.val == 7 {\n// \u8bb0\u5f55\u89e3\nres.append(root)\n}\npreOrder(root: root.left)\npreOrder(root: root.right)\n}\n</code></pre> preorder_traversal_i_compact.zig<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_i_compact.dart<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e00 */\nvoid preOrder(TreeNode? root, List&lt;TreeNode&gt; res) {\nif (root == null) {\nreturn;\n}\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(root);\n}\npreOrder(root.left, res);\npreOrder(root.right, res);\n}\n</code></pre> preorder_traversal_i_compact.rs<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\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;) {\nif root.is_none() {\nreturn;\n}\nif let Some(node) = root {\nif node.borrow().val == 7 {\n// \u8bb0\u5f55\u89e3\nres.push(node.clone());\n}\npre_order(res, node.borrow().left.clone());\npre_order(res, node.borrow().right.clone());\n}\n}\n</code></pre> <p></p> <p> \u56fe\uff1a\u5728\u524d\u5e8f\u904d\u5386\u4e2d\u641c\u7d22\u8282\u70b9 </p>"},{"location":"chapter_backtracking/backtracking_algorithm/#1311","title":"13.1.1. \u00a0 \u5c1d\u8bd5\u4e0e\u56de\u9000","text":"<p>\u4e4b\u6240\u4ee5\u79f0\u4e4b\u4e3a\u56de\u6eaf\u7b97\u6cd5\uff0c\u662f\u56e0\u4e3a\u8be5\u7b97\u6cd5\u5728\u641c\u7d22\u89e3\u7a7a\u95f4\u65f6\u4f1a\u91c7\u7528\u201c\u5c1d\u8bd5\u201d\u4e0e\u201c\u56de\u9000\u201d\u7684\u7b56\u7565\u3002\u5f53\u7b97\u6cd5\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u9047\u5230\u67d0\u4e2a\u72b6\u6001\u65e0\u6cd5\u7ee7\u7eed\u524d\u8fdb\u6216\u65e0\u6cd5\u5f97\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u65f6\uff0c\u5b83\u4f1a\u64a4\u9500\u4e0a\u4e00\u6b65\u7684\u9009\u62e9\uff0c\u9000\u56de\u5230\u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5e76\u5c1d\u8bd5\u5176\u4ed6\u53ef\u80fd\u7684\u9009\u62e9\u3002</p> <p>\u5bf9\u4e8e\u4f8b\u9898\u4e00\uff0c\u8bbf\u95ee\u6bcf\u4e2a\u8282\u70b9\u90fd\u4ee3\u8868\u4e00\u6b21\u201c\u5c1d\u8bd5\u201d\uff0c\u800c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u6216\u8fd4\u56de\u7236\u8282\u70b9\u7684 <code>return</code> \u5219\u8868\u793a\u201c\u56de\u9000\u201d\u3002</p> <p>\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u56de\u9000\u5e76\u4e0d\u4ec5\u4ec5\u5305\u62ec\u51fd\u6570\u8fd4\u56de\u3002\u4e3a\u89e3\u91ca\u8fd9\u4e00\u70b9\uff0c\u6211\u4eec\u5bf9\u4f8b\u9898\u4e00\u7a0d\u4f5c\u62d3\u5c55\u3002</p> <p>\u4f8b\u9898\u4e8c</p> <p>\u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\u3002</p> <p>\u5728\u4f8b\u9898\u4e00\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u5217\u8868 <code>path</code> \u8bb0\u5f55\u8bbf\u95ee\u8fc7\u7684\u8282\u70b9\u8def\u5f84\u3002\u5f53\u8bbf\u95ee\u5230\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\u65f6\uff0c\u5219\u590d\u5236 <code>path</code> \u5e76\u6dfb\u52a0\u8fdb\u7ed3\u679c\u5217\u8868 <code>res</code> \u3002\u904d\u5386\u5b8c\u6210\u540e\uff0c<code>res</code> \u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6240\u6709\u7684\u89e3\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust preorder_traversal_ii_compact.java<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode root) {\nif (root == null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(new ArrayList&lt;&gt;(path));\n}\npreOrder(root.left);\npreOrder(root.right);\n// \u56de\u9000\npath.remove(path.size() - 1);\n}\n</code></pre> preorder_traversal_ii_compact.cpp<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode *root) {\nif (root == nullptr) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push_back(root);\nif (root-&gt;val == 7) {\n// \u8bb0\u5f55\u89e3\nres.push_back(path);\n}\npreOrder(root-&gt;left);\npreOrder(root-&gt;right);\n// \u56de\u9000\npath.pop_back();\n}\n</code></pre> preorder_traversal_ii_compact.py<pre><code>def pre_order(root: TreeNode):\n\"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c\"\"\"\nif root is None:\nreturn\n# \u5c1d\u8bd5\npath.append(root)\nif root.val == 7:\n# \u8bb0\u5f55\u89e3\nres.append(list(path))\npre_order(root.left)\npre_order(root.right)\n# \u56de\u9000\npath.pop()\n</code></pre> preorder_traversal_ii_compact.go<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunc preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\nif root == nil {\nreturn\n}\n// \u5c1d\u8bd5\n*path = append(*path, root)\nif root.Val.(int) == 7 {\n// \u8bb0\u5f55\u89e3\n*res = append(*res, *path)\n}\npreOrderII(root.Left, res, path)\npreOrderII(root.Right, res, path)\n// \u56de\u9000\n*path = (*path)[:len(*path)-1]\n}\n</code></pre> preorder_traversal_ii_compact.js<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunction preOrder(root, path, res) {\nif (root === null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push(root);\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push([...path]);\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.pop();\n}\n</code></pre> preorder_traversal_ii_compact.ts<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunction preOrder(\nroot: TreeNode | null,\npath: TreeNode[],\nres: TreeNode[][]\n): void {\nif (root === null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push(root);\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push([...path]);\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.pop();\n}\n</code></pre> preorder_traversal_ii_compact.c<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode *root, vector *path, vector *res) {\nif (root == NULL) {\nreturn;\n}\n// \u5c1d\u8bd5\nvectorPushback(path, root, sizeof(TreeNode));\nif (root-&gt;val == 7) {\n// \u8bb0\u5f55\u89e3\nvector *newPath = newVector();\nfor (int i = 0; i &lt; path-&gt;size; i++) {\nvectorPushback(newPath, path-&gt;data[i], sizeof(int));\n}\nvectorPushback(res, newPath, sizeof(vector));\n}\npreOrder(root-&gt;left, path, res);\npreOrder(root-&gt;right, path, res);\n// \u56de\u9000\nvectorPopback(path);\n}\n</code></pre> preorder_traversal_ii_compact.cs<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(TreeNode root) {\nif (root == null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.Add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.Add(new List&lt;TreeNode&gt;(path));\n}\npreOrder(root.left);\npreOrder(root.right);\n// \u56de\u9000\npath.RemoveAt(path.Count - 1);\n}\n</code></pre> preorder_traversal_ii_compact.swift<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfunc preOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u5c1d\u8bd5\npath.append(root)\nif root.val == 7 {\n// \u8bb0\u5f55\u89e3\nres.append(path)\n}\npreOrder(root: root.left)\npreOrder(root: root.right)\n// \u56de\u9000\npath.removeLast()\n}\n</code></pre> preorder_traversal_ii_compact.zig<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_ii_compact.dart<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nvoid preOrder(\nTreeNode? root,\nList&lt;TreeNode&gt; path,\nList&lt;List&lt;TreeNode&gt;&gt; res,\n) {\nif (root == null) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(List.from(path));\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.removeLast();\n}\n</code></pre> preorder_traversal_ii_compact.rs<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e8c */\nfn pre_order(res: &amp;mut Vec&lt;Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;&gt;, path: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, root: Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;) {\nif root.is_none() {\nreturn;\n}\nif let Some(node) = root {\n// \u5c1d\u8bd5\npath.push(node.clone());\nif node.borrow().val == 7 {\n// \u8bb0\u5f55\u89e3\nres.push(path.clone());\n}\npre_order(res, path, node.borrow().left.clone());\npre_order(res, path, node.borrow().right.clone());\n// \u56de\u9000\npath.remove(path.len() - 1);\n}\n}\n</code></pre> <p>\u5728\u6bcf\u6b21\u201c\u5c1d\u8bd5\u201d\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5c06\u5f53\u524d\u8282\u70b9\u6dfb\u52a0\u8fdb <code>path</code> \u6765\u8bb0\u5f55\u8def\u5f84\uff1b\u800c\u5728\u201c\u56de\u9000\u201d\u524d\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8be5\u8282\u70b9\u4ece <code>path</code> \u4e2d\u5f39\u51fa\uff0c\u4ee5\u6062\u590d\u672c\u6b21\u5c1d\u8bd5\u4e4b\u524d\u7684\u72b6\u6001\u3002</p> <p>\u89c2\u5bdf\u8be5\u8fc7\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5c1d\u8bd5\u548c\u56de\u9000\u7406\u89e3\u4e3a\u201c\u524d\u8fdb\u201d\u4e0e\u201c\u64a4\u9500\u201d\uff0c\u4e24\u4e2a\u64cd\u4f5c\u662f\u4e92\u4e3a\u9006\u5411\u7684\u3002</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> \u56fe\uff1a\u5c1d\u8bd5\u4e0e\u56de\u9000 </p>"},{"location":"chapter_backtracking/backtracking_algorithm/#1312","title":"13.1.2. \u00a0 \u526a\u679d","text":"<p>\u590d\u6742\u7684\u56de\u6eaf\u95ee\u9898\u901a\u5e38\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u7ea6\u675f\u6761\u4ef6\uff0c\u7ea6\u675f\u6761\u4ef6\u901a\u5e38\u53ef\u7528\u4e8e\u201c\u526a\u679d\u201d\u3002</p> <p>\u4f8b\u9898\u4e09</p> <p>\u5728\u4e8c\u53c9\u6811\u4e2d\u641c\u7d22\u6240\u6709\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\uff0c\u8bf7\u8fd4\u56de\u6839\u8282\u70b9\u5230\u8fd9\u4e9b\u8282\u70b9\u7684\u8def\u5f84\uff0c\u5e76\u8981\u6c42\u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u3002</p> <p>\u4e3a\u4e86\u6ee1\u8db3\u4ee5\u4e0a\u7ea6\u675f\u6761\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u526a\u679d\u64cd\u4f5c\uff1a\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u82e5\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\uff0c\u5219\u63d0\u524d\u8fd4\u56de\uff0c\u505c\u6b62\u7ee7\u7eed\u641c\u7d22\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust preorder_traversal_iii_compact.java<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode root) {\n// \u526a\u679d\nif (root == null || root.val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(new ArrayList&lt;&gt;(path));\npath.remove(path.size() - 1);\nreturn;\n}\npreOrder(root.left);\npreOrder(root.right);\n// \u56de\u9000\npath.remove(path.size() - 1);\n}\n</code></pre> preorder_traversal_iii_compact.cpp<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode *root) {\n// \u526a\u679d\nif (root == nullptr || root-&gt;val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push_back(root);\nif (root-&gt;val == 7) {\n// \u8bb0\u5f55\u89e3\nres.push_back(path);\npath.pop_back();\nreturn;\n}\npreOrder(root-&gt;left);\npreOrder(root-&gt;right);\n// \u56de\u9000\npath.pop_back();\n}\n</code></pre> preorder_traversal_iii_compact.py<pre><code>def pre_order(root: TreeNode):\n\"\"\"\u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09\"\"\"\n# \u526a\u679d\nif root is None or root.val == 3:\nreturn\n# \u5c1d\u8bd5\npath.append(root)\nif root.val == 7:\n# \u8bb0\u5f55\u89e3\nres.append(list(path))\npath.pop()\nreturn\npre_order(root.left)\npre_order(root.right)\n# \u56de\u9000\npath.pop()\n</code></pre> preorder_traversal_iii_compact.go<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunc preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n// \u526a\u679d\nif root == nil || root.Val == 3 {\nreturn\n}\n// \u5c1d\u8bd5\n*path = append(*path, root)\nif root.Val.(int) == 7 {\n// \u8bb0\u5f55\u89e3\n*res = append(*res, *path)\n*path = (*path)[:len(*path)-1]\nreturn\n}\npreOrderIII(root.Left, res, path)\npreOrderIII(root.Right, res, path)\n// \u56de\u9000\n*path = (*path)[:len(*path)-1]\n}\n</code></pre> preorder_traversal_iii_compact.js<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunction preOrder(root, path, res) {\n// \u526a\u679d\nif (root === null || root.val === 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push(root);\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push([...path]);\npath.pop();\nreturn;\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.pop();\n}\n</code></pre> preorder_traversal_iii_compact.ts<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunction preOrder(\nroot: TreeNode | null,\npath: TreeNode[],\nres: TreeNode[][]\n): void {\n// \u526a\u679d\nif (root === null || root.val === 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.push(root);\nif (root.val === 7) {\n// \u8bb0\u5f55\u89e3\nres.push([...path]);\npath.pop();\nreturn;\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.pop();\n}\n</code></pre> preorder_traversal_iii_compact.c<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode *root, vector *path, vector *res) {\n// \u526a\u679d\nif (root == NULL || root-&gt;val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\nvectorPushback(path, root, sizeof(TreeNode));\nif (root-&gt;val == 7) {\n// \u8bb0\u5f55\u89e3\nvector *newPath = newVector();\nfor (int i = 0; i &lt; path-&gt;size; i++) {\nvectorPushback(newPath, path-&gt;data[i], sizeof(int));\n}\nvectorPushback(res, newPath, sizeof(vector));\nres-&gt;depth++;\n}\npreOrder(root-&gt;left, path, res);\npreOrder(root-&gt;right, path, res);\n// \u56de\u9000\nvectorPopback(path);\n}\n</code></pre> preorder_traversal_iii_compact.cs<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(TreeNode root) {\n// \u526a\u679d\nif (root == null || root.val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.Add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.Add(new List&lt;TreeNode&gt;(path));\npath.RemoveAt(path.Count - 1);\nreturn;\n}\npreOrder(root.left);\npreOrder(root.right);\n// \u56de\u9000\npath.RemoveAt(path.Count - 1);\n}\n</code></pre> preorder_traversal_iii_compact.swift<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfunc preOrder(root: TreeNode?) {\n// \u526a\u679d\nguard let root = root, root.val != 3 else {\nreturn\n}\n// \u5c1d\u8bd5\npath.append(root)\nif root.val == 7 {\n// \u8bb0\u5f55\u89e3\nres.append(path)\npath.removeLast()\nreturn\n}\npreOrder(root: root.left)\npreOrder(root: root.right)\n// \u56de\u9000\npath.removeLast()\n}\n</code></pre> preorder_traversal_iii_compact.zig<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_iii_compact.dart<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid preOrder(\nTreeNode? root,\nList&lt;TreeNode&gt; path,\nList&lt;List&lt;TreeNode&gt;&gt; res,\n) {\nif (root == null || root.val == 3) {\nreturn;\n}\n// \u5c1d\u8bd5\npath.add(root);\nif (root.val == 7) {\n// \u8bb0\u5f55\u89e3\nres.add(List.from(path));\npath.removeLast();\nreturn;\n}\npreOrder(root.left, path, res);\npreOrder(root.right, path, res);\n// \u56de\u9000\npath.removeLast();\n}\n</code></pre> preorder_traversal_iii_compact.rs<pre><code>/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nfn pre_order(res: &amp;mut Vec&lt;Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;&gt;, path: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, root: Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;) {\n// \u526a\u679d\nif root.is_none() || root.as_ref().unwrap().borrow().val == 3 {\nreturn;\n}\nif let Some(node) = root {\n// \u5c1d\u8bd5\npath.push(node.clone());\nif node.borrow().val == 7 {\n// \u8bb0\u5f55\u89e3\nres.push(path.clone());\npath.remove(path.len() - 1);\nreturn;\n}\npre_order(res, path, node.borrow().left.clone());\npre_order(res, path, node.borrow().right.clone());\n// \u56de\u9000\npath.remove(path.len() - 1);\n}\n}\n</code></pre> <p>\u526a\u679d\u662f\u4e00\u4e2a\u975e\u5e38\u5f62\u8c61\u7684\u540d\u8bcd\u3002\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u201c\u526a\u6389\u201d\u4e86\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u641c\u7d22\u5206\u652f\uff0c\u907f\u514d\u8bb8\u591a\u65e0\u610f\u4e49\u7684\u5c1d\u8bd5\uff0c\u4ece\u800c\u5b9e\u73b0\u641c\u7d22\u6548\u7387\u7684\u63d0\u9ad8\u3002</p> <p></p> <p> \u56fe\uff1a\u6839\u636e\u7ea6\u675f\u6761\u4ef6\u526a\u679d </p>"},{"location":"chapter_backtracking/backtracking_algorithm/#1313","title":"13.1.3. \u00a0 \u6846\u67b6\u4ee3\u7801","text":"<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c1d\u8bd5\u5c06\u56de\u6eaf\u7684\u201c\u5c1d\u8bd5\u3001\u56de\u9000\u3001\u526a\u679d\u201d\u7684\u4e3b\u4f53\u6846\u67b6\u63d0\u70bc\u51fa\u6765\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u901a\u7528\u6027\u3002</p> <p>\u5728\u4ee5\u4e0b\u6846\u67b6\u4ee3\u7801\u4e2d\uff0c<code>state</code> \u8868\u793a\u95ee\u9898\u7684\u5f53\u524d\u72b6\u6001\uff0c<code>choices</code> \u8868\u793a\u5f53\u524d\u72b6\u6001\u4e0b\u53ef\u4ee5\u505a\u51fa\u7684\u9009\u62e9\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List&lt;Choice&gt; choices, List&lt;State&gt; res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Choice choice : choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State *state, vector&lt;Choice *&gt; &amp;choices, vector&lt;State *&gt; &amp;res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Choice choice : choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</code></pre> <pre><code>def backtrack(state: State, choices: list[choice], res: list[state]):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\u6846\u67b6\"\"\"\n# \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif is_solution(state):\n# \u8bb0\u5f55\u89e3\nrecord_solution(state, res)\n# \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices:\n# \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif is_valid(state, choice):\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmake_choice(state, choice)\nbacktrack(state, choices, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundo_choice(state, choice)\n</code></pre> <pre><code>/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunc backtrack(state *State, choices []Choice, res *[]State) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif isSolution(state) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res)\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor _, choice := range choices {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif isValid(state, choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice)\nbacktrack(state, choices, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice)\n}\n}\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunction backtrack(state, choices, res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (let choice of choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunction backtrack(state: State, choices: Choice[], res: State[]): void {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (let choice of choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res, numRes);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i &lt; numChoices; i++) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, &amp;choices[i])) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, &amp;choices[i]);\nbacktrack(state, choices, numChoices, res, numRes);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, &amp;choices[i]);\n}\n}\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List&lt;Choice&gt; choices, List&lt;State&gt; res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nforeach (Choice choice in choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfunc backtrack(state: inout State, choices: [Choice], res: inout [State]) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif isSolution(state: state) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state: state, res: &amp;res)\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif isValid(state: state, choice: choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state: &amp;state, choice: choice)\nbacktrack(state: &amp;state, choices: choices, res: &amp;res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state: &amp;state, choice: choice)\n}\n}\n}\n</code></pre> <pre><code>\n</code></pre> <pre><code>/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List&lt;Choice&gt;, List&lt;State&gt; res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Choice choice in choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</code></pre> <pre><code>\n</code></pre> <p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u57fa\u4e8e\u6846\u67b6\u4ee3\u7801\u6765\u89e3\u51b3\u4f8b\u9898\u4e09\u3002\u72b6\u6001 <code>state</code> \u4e3a\u8282\u70b9\u904d\u5386\u8def\u5f84\uff0c\u9009\u62e9 <code>choices</code> \u4e3a\u5f53\u524d\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\uff0c\u7ed3\u679c <code>res</code> \u662f\u8def\u5f84\u5217\u8868\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust preorder_traversal_iii_template.java<pre><code>/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nboolean isSolution(List&lt;TreeNode&gt; state) {\nreturn !state.isEmpty() &amp;&amp; state.get(state.size() - 1).val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List&lt;TreeNode&gt; state, List&lt;List&lt;TreeNode&gt;&gt; res) {\nres.add(new ArrayList&lt;&gt;(state));\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nboolean isValid(List&lt;TreeNode&gt; state, TreeNode choice) {\nreturn choice != null &amp;&amp; choice.val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List&lt;TreeNode&gt; state, TreeNode choice) {\nstate.add(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List&lt;TreeNode&gt; state, TreeNode choice) {\nstate.remove(state.size() - 1);\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(List&lt;TreeNode&gt; state, List&lt;TreeNode&gt; choices, List&lt;List&lt;TreeNode&gt;&gt; res) {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (TreeNode choice : choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, Arrays.asList(choice.left, choice.right), res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</code></pre> preorder_traversal_iii_template.cpp<pre><code>/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(vector&lt;TreeNode *&gt; &amp;state) {\nreturn !state.empty() &amp;&amp; state.back()-&gt;val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(vector&lt;TreeNode *&gt; &amp;state, vector&lt;vector&lt;TreeNode *&gt;&gt; &amp;res) {\nres.push_back(state);\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(vector&lt;TreeNode *&gt; &amp;state, TreeNode *choice) {\nreturn choice != nullptr &amp;&amp; choice-&gt;val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(vector&lt;TreeNode *&gt; &amp;state, TreeNode *choice) {\nstate.push_back(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(vector&lt;TreeNode *&gt; &amp;state, TreeNode *choice) {\nstate.pop_back();\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\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// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (TreeNode *choice : choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nvector&lt;TreeNode *&gt; nextChoices{choice-&gt;left, choice-&gt;right};\nbacktrack(state, nextChoices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</code></pre> preorder_traversal_iii_template.py<pre><code>def is_solution(state: list[TreeNode]) -&gt; bool:\n\"\"\"\u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3\"\"\"\nreturn state and state[-1].val == 7\ndef record_solution(state: list[TreeNode], res: list[list[TreeNode]]):\n\"\"\"\u8bb0\u5f55\u89e3\"\"\"\nres.append(list(state))\ndef is_valid(state: list[TreeNode], choice: TreeNode) -&gt; bool:\n\"\"\"\u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5\"\"\"\nreturn choice is not None and choice.val != 3\ndef make_choice(state: list[TreeNode], choice: TreeNode):\n\"\"\"\u66f4\u65b0\u72b6\u6001\"\"\"\nstate.append(choice)\ndef undo_choice(state: list[TreeNode], choice: TreeNode):\n\"\"\"\u6062\u590d\u72b6\u6001\"\"\"\nstate.pop()\ndef backtrack(\nstate: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09\"\"\"\n# \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif is_solution(state):\n# \u8bb0\u5f55\u89e3\nrecord_solution(state, res)\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices:\n# \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif is_valid(state, choice):\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmake_choice(state, choice)\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, [choice.left, choice.right], res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundo_choice(state, choice)\n</code></pre> preorder_traversal_iii_template.go<pre><code>/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunc isSolution(state *[]*TreeNode) bool {\nreturn len(*state) != 0 &amp;&amp; (*state)[len(*state)-1].Val == 7\n}\n/* \u8bb0\u5f55\u89e3 */\nfunc recordSolution(state *[]*TreeNode, res *[][]*TreeNode) {\n*res = append(*res, *state)\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunc isValid(state *[]*TreeNode, choice *TreeNode) bool {\nreturn choice != nil &amp;&amp; choice.Val != 3\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfunc makeChoice(state *[]*TreeNode, choice *TreeNode) {\n*state = append(*state, choice)\n}\n/* \u6062\u590d\u72b6\u6001 */\nfunc undoChoice(state *[]*TreeNode, choice *TreeNode) {\n*state = (*state)[:len(*state)-1]\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunc backtrackIII(state *[]*TreeNode, choices *[]*TreeNode, res *[][]*TreeNode) {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif isSolution(state) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res)\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor _, choice := range *choices {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif isValid(state, choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\ntemp := make([]*TreeNode, 0)\ntemp = append(temp, choice.Left, choice.Right)\nbacktrackIII(state, &amp;temp, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice)\n}\n}\n}\n</code></pre> preorder_traversal_iii_template.js<pre><code>/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunction isSolution(state) {\nreturn state &amp;&amp; state[state.length - 1]?.val === 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nfunction recordSolution(state, res) {\nres.push([...state]);\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunction isValid(state, choice) {\nreturn choice !== null &amp;&amp; choice.val !== 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfunction makeChoice(state, choice) {\nstate.push(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nfunction undoChoice(state) {\nstate.pop();\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunction backtrack(state, choices, res) {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (const choice of choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, [choice.left, choice.right], res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state);\n}\n}\n}\n</code></pre> preorder_traversal_iii_template.ts<pre><code>/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunction isSolution(state: TreeNode[]): boolean {\nreturn state &amp;&amp; state[state.length - 1]?.val === 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nfunction recordSolution(state: TreeNode[], res: TreeNode[][]): void {\nres.push([...state]);\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunction isValid(state: TreeNode[], choice: TreeNode): boolean {\nreturn choice !== null &amp;&amp; choice.val !== 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfunction makeChoice(state: TreeNode[], choice: TreeNode): void {\nstate.push(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nfunction undoChoice(state: TreeNode[]): void {\nstate.pop();\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunction backtrack(\nstate: TreeNode[],\nchoices: TreeNode[],\nres: TreeNode[][]\n): void {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (const choice of choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, [choice.left, choice.right], res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state);\n}\n}\n}\n</code></pre> preorder_traversal_iii_template.c<pre><code>/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(vector *state) {\nreturn state-&gt;size != 0 &amp;&amp; ((TreeNode *)(state-&gt;data[state-&gt;size - 1]))-&gt;val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(vector *state, vector *res) {\nvector *newPath = newVector();\nfor (int i = 0; i &lt; state-&gt;size; i++) {\nvectorPushback(newPath, state-&gt;data[i], sizeof(int));\n}\nvectorPushback(res, newPath, sizeof(vector));\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(vector *state, TreeNode *choice) {\nreturn choice != NULL &amp;&amp; choice-&gt;val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(vector *state, TreeNode *choice) {\nvectorPushback(state, choice, sizeof(TreeNode));\n}\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(vector *state, TreeNode *choice) {\nvectorPopback(state);\n}\n/* \u524d\u5e8f\u904d\u5386\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(vector *state, vector *choices, vector *res) {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i &lt; choices-&gt;size; i++) {\nTreeNode *choice = choices-&gt;data[i];\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nvector *nextChoices = newVector();\nvectorPushback(nextChoices, choice-&gt;left, sizeof(TreeNode));\nvectorPushback(nextChoices, choice-&gt;right, sizeof(TreeNode));\nbacktrack(state, nextChoices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</code></pre> preorder_traversal_iii_template.cs<pre><code>/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(List&lt;TreeNode&gt; state) {\nreturn state.Count != 0 &amp;&amp; state[^1].val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List&lt;TreeNode&gt; state, List&lt;List&lt;TreeNode&gt;&gt; res) {\nres.Add(new List&lt;TreeNode&gt;(state));\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(List&lt;TreeNode&gt; state, TreeNode choice) {\nreturn choice != null &amp;&amp; choice.val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List&lt;TreeNode&gt; state, TreeNode choice) {\nstate.Add(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List&lt;TreeNode&gt; state, TreeNode choice) {\nstate.RemoveAt(state.Count - 1);\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(List&lt;TreeNode&gt; state, List&lt;TreeNode&gt; choices, List&lt;List&lt;TreeNode&gt;&gt; res) {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nforeach (TreeNode choice in choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, new List&lt;TreeNode&gt; { choice.left, choice.right }, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</code></pre> preorder_traversal_iii_template.swift<pre><code>/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfunc isSolution(state: [TreeNode]) -&gt; Bool {\n!state.isEmpty &amp;&amp; state.last!.val == 7\n}\n/* \u8bb0\u5f55\u89e3 */\nfunc recordSolution(state: [TreeNode], res: inout [[TreeNode]]) {\nres.append(state)\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfunc isValid(state: [TreeNode], choice: TreeNode?) -&gt; Bool {\nchoice != nil &amp;&amp; choice!.val != 3\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfunc makeChoice(state: inout [TreeNode], choice: TreeNode) {\nstate.append(choice)\n}\n/* \u6062\u590d\u72b6\u6001 */\nfunc undoChoice(state: inout [TreeNode], choice: TreeNode) {\nstate.removeLast()\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfunc backtrack(state: inout [TreeNode], choices: [TreeNode], res: inout [[TreeNode]]) {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif isSolution(state: state) {\nrecordSolution(state: state, res: &amp;res)\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif isValid(state: state, choice: choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state: &amp;state, choice: choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state: &amp;state, choices: [choice.left, choice.right].compactMap { $0 }, res: &amp;res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state: &amp;state, choice: choice)\n}\n}\n}\n</code></pre> preorder_traversal_iii_template.zig<pre><code>[class]{}-[func]{isSolution}\n[class]{}-[func]{recordSolution}\n[class]{}-[func]{isValid}\n[class]{}-[func]{makeChoice}\n[class]{}-[func]{undoChoice}\n[class]{}-[func]{backtrack}\n</code></pre> preorder_traversal_iii_template.dart<pre><code>/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nbool isSolution(List&lt;TreeNode&gt; state) {\nreturn state.isNotEmpty &amp;&amp; state.last.val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nvoid recordSolution(List&lt;TreeNode&gt; state, List&lt;List&lt;TreeNode&gt;&gt; res) {\nres.add(List.from(state));\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nbool isValid(List&lt;TreeNode&gt; state, TreeNode? choice) {\nreturn choice != null &amp;&amp; choice.val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nvoid makeChoice(List&lt;TreeNode&gt; state, TreeNode? choice) {\nstate.add(choice!);\n}\n/* \u6062\u590d\u72b6\u6001 */\nvoid undoChoice(List&lt;TreeNode&gt; state, TreeNode? choice) {\nstate.removeLast();\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nvoid backtrack(\nList&lt;TreeNode&gt; state,\nList&lt;TreeNode?&gt; choices,\nList&lt;List&lt;TreeNode&gt;&gt; res,\n) {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (TreeNode? choice in choices) {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, [choice!.left, choice.right], res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</code></pre> preorder_traversal_iii_template.rs<pre><code>/* \u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u5426\u4e3a\u89e3 */\nfn is_solution(state: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;) -&gt; bool {\nreturn !state.is_empty() &amp;&amp; state.get(state.len() - 1).unwrap().borrow().val == 7;\n}\n/* \u8bb0\u5f55\u89e3 */\nfn record_solution(state: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, res: &amp;mut Vec&lt;Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;&gt;) {\nres.push(state.clone());\n}\n/* \u5224\u65ad\u5728\u5f53\u524d\u72b6\u6001\u4e0b\uff0c\u8be5\u9009\u62e9\u662f\u5426\u5408\u6cd5 */\nfn is_valid(_: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, choice: Rc&lt;RefCell&lt;TreeNode&gt;&gt;) -&gt; bool {\nreturn choice.borrow().val != 3;\n}\n/* \u66f4\u65b0\u72b6\u6001 */\nfn make_choice(state: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, choice: Rc&lt;RefCell&lt;TreeNode&gt;&gt;) {\nstate.push(choice);\n}\n/* \u6062\u590d\u72b6\u6001 */\nfn undo_choice(state: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, _: Rc&lt;RefCell&lt;TreeNode&gt;&gt;) {\nstate.remove(state.len() - 1);\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u4f8b\u9898\u4e09 */\nfn backtrack(state: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, choices: &amp;mut Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;, res: &amp;mut Vec&lt;Vec&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;&gt;) {\n// \u68c0\u67e5\u662f\u5426\u4e3a\u89e3\nif is_solution(state) {\n// \u8bb0\u5f55\u89e3\nrecord_solution(state, res);\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices {\n// \u526a\u679d\uff1a\u68c0\u67e5\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif is_valid(state, choice.clone()) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmake_choice(state, choice.clone());\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, &amp;mut vec![choice.borrow().left.clone().unwrap(), choice.borrow().right.clone().unwrap()], res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundo_choice(state, choice.clone());\n}\n}\n}\n</code></pre> <p>\u6839\u636e\u9898\u610f\uff0c\u5f53\u627e\u5230\u503c\u4e3a 7 \u7684\u8282\u70b9\u540e\u5e94\u8be5\u7ee7\u7eed\u641c\u7d22\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5c06\u8bb0\u5f55\u89e3\u4e4b\u540e\u7684 <code>return</code> \u8bed\u53e5\u5220\u9664\u3002\u4e0b\u56fe\u5bf9\u6bd4\u4e86\u4fdd\u7559\u6216\u5220\u9664 <code>return</code> \u8bed\u53e5\u7684\u641c\u7d22\u8fc7\u7a0b\u3002</p> <p></p> <p> \u56fe\uff1a\u4fdd\u7559\u4e0e\u5220\u9664 return \u7684\u641c\u7d22\u8fc7\u7a0b\u5bf9\u6bd4 </p> <p>\u76f8\u6bd4\u57fa\u4e8e\u524d\u5e8f\u904d\u5386\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u57fa\u4e8e\u56de\u6eaf\u7b97\u6cd5\u6846\u67b6\u7684\u4ee3\u7801\u5b9e\u73b0\u867d\u7136\u663e\u5f97\u5570\u55e6\uff0c\u4f46\u901a\u7528\u6027\u66f4\u597d\u3002\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u56de\u6eaf\u95ee\u9898\u90fd\u53ef\u4ee5\u5728\u8be5\u6846\u67b6\u4e0b\u89e3\u51b3\u3002\u6211\u4eec\u53ea\u9700\u6839\u636e\u5177\u4f53\u95ee\u9898\u6765\u5b9a\u4e49 <code>state</code> \u548c <code>choices</code> \uff0c\u5e76\u5b9e\u73b0\u6846\u67b6\u4e2d\u7684\u5404\u4e2a\u65b9\u6cd5\u5373\u53ef\u3002</p>"},{"location":"chapter_backtracking/backtracking_algorithm/#1314","title":"13.1.4. \u00a0 \u5e38\u7528\u672f\u8bed","text":"<p>\u4e3a\u4e86\u66f4\u6e05\u6670\u5730\u5206\u6790\u7b97\u6cd5\u95ee\u9898\uff0c\u6211\u4eec\u603b\u7ed3\u4e00\u4e0b\u56de\u6eaf\u7b97\u6cd5\u4e2d\u5e38\u7528\u672f\u8bed\u7684\u542b\u4e49\uff0c\u5e76\u5bf9\u7167\u4f8b\u9898\u4e09\u7ed9\u51fa\u5bf9\u5e94\u793a\u4f8b\u3002</p> \u540d\u8bcd \u5b9a\u4e49 \u4f8b\u9898\u4e09 \u89e3 Solution \u89e3\u662f\u6ee1\u8db3\u95ee\u9898\u7279\u5b9a\u6761\u4ef6\u7684\u7b54\u6848\uff0c\u53ef\u80fd\u6709\u4e00\u4e2a\u6216\u591a\u4e2a \u6839\u8282\u70b9\u5230\u8282\u70b9 \\(7\\) \u7684\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u6240\u6709\u8def\u5f84 \u7ea6\u675f\u6761\u4ef6 Constraint \u7ea6\u675f\u6761\u4ef6\u662f\u95ee\u9898\u4e2d\u9650\u5236\u89e3\u7684\u53ef\u884c\u6027\u7684\u6761\u4ef6\uff0c\u901a\u5e38\u7528\u4e8e\u526a\u679d \u8def\u5f84\u4e2d\u4e0d\u5305\u542b\u8282\u70b9 \\(3\\) \uff0c\u53ea\u5305\u542b\u4e00\u4e2a\u8282\u70b9 \\(7\\) \u72b6\u6001 State \u72b6\u6001\u8868\u793a\u95ee\u9898\u5728\u67d0\u4e00\u65f6\u523b\u7684\u60c5\u51b5\uff0c\u5305\u62ec\u5df2\u7ecf\u505a\u51fa\u7684\u9009\u62e9 \u5f53\u524d\u5df2\u8bbf\u95ee\u7684\u8282\u70b9\u8def\u5f84\uff0c\u5373 <code>path</code> \u8282\u70b9\u5217\u8868 \u5c1d\u8bd5 Attempt \u5c1d\u8bd5\u662f\u6839\u636e\u53ef\u7528\u9009\u62e9\u6765\u63a2\u7d22\u89e3\u7a7a\u95f4\u7684\u8fc7\u7a0b\uff0c\u5305\u62ec\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\uff0c\u68c0\u67e5\u662f\u5426\u4e3a\u89e3 \u9012\u5f52\u8bbf\u95ee\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\uff0c\u5c06\u8282\u70b9\u6dfb\u52a0\u8fdb <code>path</code> \uff0c\u5224\u65ad\u8282\u70b9\u7684\u503c\u662f\u5426\u4e3a \\(7\\) \u56de\u9000 Backtracking \u56de\u9000\u6307\u9047\u5230\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u72b6\u6001\u65f6\uff0c\u64a4\u9500\u524d\u9762\u505a\u51fa\u7684\u9009\u62e9\uff0c\u56de\u5230\u4e0a\u4e00\u4e2a\u72b6\u6001 \u5f53\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u3001\u7ed3\u675f\u7ed3\u70b9\u8bbf\u95ee\u3001\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u65f6\u7ec8\u6b62\u641c\u7d22\uff0c\u51fd\u6570\u8fd4\u56de \u526a\u679d Pruning \u526a\u679d\u662f\u6839\u636e\u95ee\u9898\u7279\u6027\u548c\u7ea6\u675f\u6761\u4ef6\u907f\u514d\u65e0\u610f\u4e49\u7684\u641c\u7d22\u8def\u5f84\u7684\u65b9\u6cd5\uff0c\u53ef\u63d0\u9ad8\u641c\u7d22\u6548\u7387 \u5f53\u9047\u5230\u503c\u4e3a \\(3\\) \u7684\u8282\u70b9\u65f6\uff0c\u5219\u7ec8\u6b62\u7ee7\u7eed\u641c\u7d22 <p>Tip</p> <p>\u95ee\u9898\u3001\u89e3\u3001\u72b6\u6001\u7b49\u6982\u5ff5\u662f\u901a\u7528\u7684\uff0c\u5728\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u4e2d\u90fd\u6709\u6d89\u53ca\u3002</p>"},{"location":"chapter_backtracking/backtracking_algorithm/#1315","title":"13.1.5. \u00a0 \u4f18\u52bf\u4e0e\u5c40\u9650\u6027","text":"<p>\u56de\u6eaf\u7b97\u6cd5\u672c\u8d28\u4e0a\u662f\u4e00\u79cd\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\uff0c\u5b83\u5c1d\u8bd5\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u76f4\u5230\u627e\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u80fd\u591f\u627e\u5230\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u4e14\u5728\u5408\u7406\u7684\u526a\u679d\u64cd\u4f5c\u4e0b\uff0c\u5177\u6709\u5f88\u9ad8\u7684\u6548\u7387\u3002</p> <p>\u7136\u800c\uff0c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6216\u8005\u590d\u6742\u95ee\u9898\u65f6\uff0c\u56de\u6eaf\u7b97\u6cd5\u7684\u8fd0\u884c\u6548\u7387\u53ef\u80fd\u96be\u4ee5\u63a5\u53d7\u3002</p> <ul> <li>\u65f6\u95f4\uff1a\u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u9700\u8981\u904d\u5386\u72b6\u6001\u7a7a\u95f4\u7684\u6240\u6709\u53ef\u80fd\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230\u6307\u6570\u9636\u6216\u9636\u4e58\u9636\u3002</li> <li>\u7a7a\u95f4\uff1a\u5728\u9012\u5f52\u8c03\u7528\u4e2d\u9700\u8981\u4fdd\u5b58\u5f53\u524d\u7684\u72b6\u6001\uff08\u4f8b\u5982\u8def\u5f84\u3001\u7528\u4e8e\u526a\u679d\u7684\u8f85\u52a9\u53d8\u91cf\u7b49\uff09\uff0c\u5f53\u6df1\u5ea6\u5f88\u5927\u65f6\uff0c\u7a7a\u95f4\u9700\u6c42\u53ef\u80fd\u4f1a\u53d8\u5f97\u5f88\u5927\u3002</li> </ul> <p>\u5373\u4fbf\u5982\u6b64\uff0c\u56de\u6eaf\u7b97\u6cd5\u4ecd\u7136\u662f\u67d0\u4e9b\u641c\u7d22\u95ee\u9898\u548c\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u7684\u6700\u4f73\u89e3\u51b3\u65b9\u6848\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u95ee\u9898\uff0c\u7531\u4e8e\u65e0\u6cd5\u9884\u6d4b\u54ea\u4e9b\u9009\u62e9\u53ef\u751f\u6210\u6709\u6548\u7684\u89e3\uff0c\u56e0\u6b64\u6211\u4eec\u5fc5\u987b\u5bf9\u6240\u6709\u53ef\u80fd\u7684\u9009\u62e9\u8fdb\u884c\u904d\u5386\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5173\u952e\u662f\u5982\u4f55\u8fdb\u884c\u6548\u7387\u4f18\u5316\uff0c\u5e38\u89c1\u65b9\u6cd5\u6709\uff1a</p> <ul> <li>\u526a\u679d\uff1a\u907f\u514d\u641c\u7d22\u90a3\u4e9b\u80af\u5b9a\u4e0d\u4f1a\u4ea7\u751f\u89e3\u7684\u8def\u5f84\uff0c\u4ece\u800c\u8282\u7701\u65f6\u95f4\u548c\u7a7a\u95f4\u3002</li> <li>\u542f\u53d1\u5f0f\u641c\u7d22\uff1a\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u5f15\u5165\u4e00\u4e9b\u7b56\u7565\u6216\u8005\u4f30\u8ba1\u503c\uff0c\u4ece\u800c\u4f18\u5148\u641c\u7d22\u6700\u6709\u53ef\u80fd\u4ea7\u751f\u6709\u6548\u89e3\u7684\u8def\u5f84\u3002</li> </ul>"},{"location":"chapter_backtracking/backtracking_algorithm/#1316","title":"13.1.6. \u00a0 \u56de\u6eaf\u5178\u578b\u4f8b\u9898","text":"<p>\u56de\u6eaf\u7b97\u6cd5\u53ef\u7528\u4e8e\u89e3\u51b3\u8bb8\u591a\u641c\u7d22\u95ee\u9898\u3001\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u548c\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u3002</p> <p>\u641c\u7d22\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u89e3\u51b3\u65b9\u6848\u3002</p> <ul> <li>\u5168\u6392\u5217\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\uff0c\u6c42\u51fa\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u7ec4\u5408\u3002</li> <li>\u5b50\u96c6\u548c\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\u548c\u4e00\u4e2a\u76ee\u6807\u548c\uff0c\u627e\u5230\u96c6\u5408\u4e2d\u6240\u6709\u548c\u4e3a\u76ee\u6807\u548c\u7684\u5b50\u96c6\u3002</li> <li>\u6c49\u8bfa\u5854\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e09\u4e2a\u67f1\u5b50\u548c\u4e00\u7cfb\u5217\u5927\u5c0f\u4e0d\u540c\u7684\u5706\u76d8\uff0c\u8981\u6c42\u5c06\u6240\u6709\u5706\u76d8\u4ece\u4e00\u4e2a\u67f1\u5b50\u79fb\u52a8\u5230\u53e6\u4e00\u4e2a\u67f1\u5b50\uff0c\u6bcf\u6b21\u53ea\u80fd\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\uff0c\u4e14\u4e0d\u80fd\u5c06\u5927\u5706\u76d8\u653e\u5728\u5c0f\u5706\u76d8\u4e0a\u3002</li> </ul> <p>\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u5230\u6ee1\u8db3\u6240\u6709\u7ea6\u675f\u6761\u4ef6\u7684\u89e3\u3002</p> <ul> <li>\\(n\\) \u7687\u540e\uff1a\u5728 \\(n \\times n\\) \u7684\u68cb\u76d8\u4e0a\u653e\u7f6e \\(n\\) \u4e2a\u7687\u540e\uff0c\u4f7f\u5f97\u5b83\u4eec\u4e92\u4e0d\u653b\u51fb\u3002</li> <li>\u6570\u72ec\uff1a\u5728 \\(9 \\times 9\\) \u7684\u7f51\u683c\u4e2d\u586b\u5165\u6570\u5b57 \\(1\\) ~ \\(9\\) \uff0c\u4f7f\u5f97\u6bcf\u884c\u3001\u6bcf\u5217\u548c\u6bcf\u4e2a \\(3 \\times 3\\) \u5b50\u7f51\u683c\u4e2d\u7684\u6570\u5b57\u4e0d\u91cd\u590d\u3002</li> <li>\u56fe\u7740\u8272\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5411\u56fe\uff0c\u7528\u6700\u5c11\u7684\u989c\u8272\u7ed9\u56fe\u7684\u6bcf\u4e2a\u9876\u70b9\u7740\u8272\uff0c\u4f7f\u5f97\u76f8\u90bb\u9876\u70b9\u989c\u8272\u4e0d\u540c\u3002</li> </ul> <p>\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff1a\u8fd9\u7c7b\u95ee\u9898\u7684\u76ee\u6807\u662f\u5728\u4e00\u4e2a\u7ec4\u5408\u7a7a\u95f4\u4e2d\u627e\u5230\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u6700\u4f18\u89e3\u3002</p> <ul> <li>0-1 \u80cc\u5305\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u7269\u54c1\u548c\u4e00\u4e2a\u80cc\u5305\uff0c\u6bcf\u4e2a\u7269\u54c1\u6709\u4e00\u5b9a\u7684\u4ef7\u503c\u548c\u91cd\u91cf\uff0c\u8981\u6c42\u5728\u80cc\u5305\u5bb9\u91cf\u9650\u5236\u5185\uff0c\u9009\u62e9\u7269\u54c1\u4f7f\u5f97\u603b\u4ef7\u503c\u6700\u5927\u3002</li> <li>\u65c5\u884c\u5546\u95ee\u9898\uff1a\u5728\u4e00\u4e2a\u56fe\u4e2d\uff0c\u4ece\u4e00\u4e2a\u70b9\u51fa\u53d1\uff0c\u8bbf\u95ee\u6240\u6709\u5176\u4ed6\u70b9\u6070\u597d\u4e00\u6b21\u540e\u8fd4\u56de\u8d77\u70b9\uff0c\u6c42\u6700\u77ed\u8def\u5f84\u3002</li> <li>\u6700\u5927\u56e2\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u5411\u56fe\uff0c\u627e\u5230\u6700\u5927\u7684\u5b8c\u5168\u5b50\u56fe\uff0c\u5373\u5b50\u56fe\u4e2d\u7684\u4efb\u610f\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u90fd\u6709\u8fb9\u76f8\u8fde\u3002</li> </ul> <p>\u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e\u8bb8\u591a\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff0c\u56de\u6eaf\u90fd\u4e0d\u662f\u6700\u4f18\u89e3\u51b3\u65b9\u6848\uff0c\u4f8b\u5982\uff1a</p> <ul> <li>0-1 \u80cc\u5305\u95ee\u9898\u901a\u5e38\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u89e3\u51b3\uff0c\u4ee5\u8fbe\u5230\u66f4\u9ad8\u7684\u65f6\u95f4\u6548\u7387\u3002</li> <li>\u65c5\u884c\u5546\u662f\u4e00\u4e2a\u8457\u540d\u7684 NP-Hard \u95ee\u9898\uff0c\u5e38\u7528\u89e3\u6cd5\u6709\u9057\u4f20\u7b97\u6cd5\u548c\u8681\u7fa4\u7b97\u6cd5\u7b49\u3002</li> <li>\u6700\u5927\u56e2\u95ee\u9898\u662f\u56fe\u8bba\u4e2d\u7684\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\uff0c\u53ef\u7528\u8d2a\u5fc3\u7b49\u542f\u53d1\u5f0f\u7b97\u6cd5\u6765\u89e3\u51b3\u3002</li> </ul>"},{"location":"chapter_backtracking/n_queens_problem/","title":"13.4. \u00a0 N \u7687\u540e\u95ee\u9898","text":"<p>Question</p> <p>\u6839\u636e\u56fd\u9645\u8c61\u68cb\u7684\u89c4\u5219\uff0c\u7687\u540e\u53ef\u4ee5\u653b\u51fb\u4e0e\u4e4b\u5904\u5728\u540c\u4e00\u884c\u6216\u540c\u4e00\u5217\u6216\u540c\u4e00\u659c\u7ebf\u4e0a\u7684\u68cb\u5b50\u3002\u7ed9\u5b9a \\(n\\) \u4e2a\u7687\u540e\u548c\u4e00\u4e2a \\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5bfb\u627e\u4f7f\u5f97\u6240\u6709\u7687\u540e\u4e4b\u95f4\u65e0\u6cd5\u76f8\u4e92\u653b\u51fb\u7684\u6446\u653e\u65b9\u6848\u3002</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5f53 \\(n = 4\\) \u65f6\uff0c\u5171\u53ef\u4ee5\u627e\u5230\u4e24\u4e2a\u89e3\u3002\u4ece\u56de\u6eaf\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76d8\u5171\u6709 \\(n^2\\) \u4e2a\u683c\u5b50\uff0c\u7ed9\u51fa\u4e86\u6240\u6709\u7684\u9009\u62e9 <code>choices</code> \u3002\u5728\u9010\u4e2a\u653e\u7f6e\u7687\u540e\u7684\u8fc7\u7a0b\u4e2d\uff0c\u68cb\u76d8\u72b6\u6001\u5728\u4e0d\u65ad\u5730\u53d8\u5316\uff0c\u6bcf\u4e2a\u65f6\u523b\u7684\u68cb\u76d8\u5c31\u662f\u72b6\u6001 <code>state</code> \u3002</p> <p></p> <p> \u56fe\uff1a4 \u7687\u540e\u95ee\u9898\u7684\u89e3 </p> <p>\u672c\u9898\u5171\u5305\u542b\u4e09\u4e2a\u7ea6\u675f\u6761\u4ef6\uff1a\u591a\u4e2a\u7687\u540e\u4e0d\u80fd\u5728\u540c\u4e00\u884c\u3001\u540c\u4e00\u5217\u3001\u540c\u4e00\u5bf9\u89d2\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u89d2\u7ebf\u5206\u4e3a\u4e3b\u5bf9\u89d2\u7ebf <code>\\</code> \u548c\u6b21\u5bf9\u89d2\u7ebf <code>/</code> \u4e24\u79cd\u3002</p> <p></p> <p> \u56fe\uff1an \u7687\u540e\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6 </p>"},{"location":"chapter_backtracking/n_queens_problem/#_1","title":"\u9010\u884c\u653e\u7f6e\u7b56\u7565","text":"<p>\u7687\u540e\u7684\u6570\u91cf\u548c\u68cb\u76d8\u7684\u884c\u6570\u90fd\u4e3a \\(n\\) \uff0c\u56e0\u6b64\u6211\u4eec\u5bb9\u6613\u5f97\u5230\u4e00\u4e2a\u63a8\u8bba\uff1a\u68cb\u76d8\u6bcf\u884c\u90fd\u5141\u8bb8\u4e14\u53ea\u5141\u8bb8\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\u3002</p> <p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u9010\u884c\u653e\u7f6e\u7b56\u7565\uff1a\u4ece\u7b2c\u4e00\u884c\u5f00\u59cb\uff0c\u5728\u6bcf\u884c\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\uff0c\u76f4\u81f3\u6700\u540e\u4e00\u884c\u7ed3\u675f\u3002</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4e3a \\(4\\) \u7687\u540e\u95ee\u9898\u7684\u9010\u884c\u653e\u7f6e\u8fc7\u7a0b\u3002\u53d7\u753b\u5e45\u9650\u5236\uff0c\u4e0b\u56fe\u4ec5\u5c55\u5f00\u4e86\u7b2c\u4e00\u884c\u7684\u5176\u4e2d\u4e00\u4e2a\u641c\u7d22\u5206\u652f\uff0c\u5e76\u4e14\u5c06\u4e0d\u6ee1\u8db3\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u65b9\u6848\u90fd\u8fdb\u884c\u4e86\u526a\u679d\u3002</p> <p></p> <p> \u56fe\uff1a\u9010\u884c\u653e\u7f6e\u7b56\u7565 </p> <p>\u672c\u8d28\u4e0a\u770b\uff0c\u9010\u884c\u653e\u7f6e\u7b56\u7565\u8d77\u5230\u4e86\u526a\u679d\u7684\u4f5c\u7528\uff0c\u5b83\u907f\u514d\u4e86\u540c\u4e00\u884c\u51fa\u73b0\u591a\u4e2a\u7687\u540e\u7684\u6240\u6709\u641c\u7d22\u5206\u652f\u3002</p>"},{"location":"chapter_backtracking/n_queens_problem/#_2","title":"\u5217\u4e0e\u5bf9\u89d2\u7ebf\u526a\u679d","text":"<p>\u4e3a\u4e86\u6ee1\u8db3\u5217\u7ea6\u675f\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u5e03\u5c14\u578b\u6570\u7ec4 <code>cols</code> \u8bb0\u5f55\u6bcf\u4e00\u5217\u662f\u5426\u6709\u7687\u540e\u3002\u5728\u6bcf\u6b21\u51b3\u5b9a\u653e\u7f6e\u524d\uff0c\u6211\u4eec\u901a\u8fc7 <code>cols</code> \u5c06\u5df2\u6709\u7687\u540e\u7684\u5217\u8fdb\u884c\u526a\u679d\uff0c\u5e76\u5728\u56de\u6eaf\u4e2d\u52a8\u6001\u66f4\u65b0 <code>cols</code> \u7684\u72b6\u6001\u3002</p> <p>\u90a3\u4e48\uff0c\u5982\u4f55\u5904\u7406\u5bf9\u89d2\u7ebf\u7ea6\u675f\u5462\uff1f\u8bbe\u68cb\u76d8\u4e2d\u67d0\u4e2a\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u4e3a \\((row, col)\\) \uff0c\u9009\u5b9a\u77e9\u9635\u4e2d\u7684\u67d0\u6761\u4e3b\u5bf9\u89d2\u7ebf\uff0c\u6211\u4eec\u53d1\u73b0\u8be5\u5bf9\u89d2\u7ebf\u4e0a\u6240\u6709\u683c\u5b50\u7684\u884c\u7d22\u5f15\u51cf\u5217\u7d22\u5f15\u90fd\u76f8\u7b49\uff0c\u5373\u5bf9\u89d2\u7ebf\u4e0a\u6240\u6709\u683c\u5b50\u7684 \\(row - col\\) \u4e3a\u6052\u5b9a\u503c\u3002</p> <p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4e24\u4e2a\u683c\u5b50\u6ee1\u8db3 \\(row_1 - col_1 = row_2 - col_2\\) \uff0c\u5219\u5b83\u4eec\u4e00\u5b9a\u5904\u5728\u540c\u4e00\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u3002\u5229\u7528\u8be5\u89c4\u5f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u4e00\u4e2a\u6570\u7ec4 <code>diag1</code> \u6765\u8bb0\u5f55\u6bcf\u6761\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u662f\u5426\u6709\u7687\u540e\u3002</p> <p>\u540c\u7406\uff0c\u6b21\u5bf9\u89d2\u7ebf\u4e0a\u7684\u6240\u6709\u683c\u5b50\u7684 \\(row + col\\) \u662f\u6052\u5b9a\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u65b9\u6cd5\uff0c\u501f\u52a9\u6570\u7ec4 <code>diag2</code> \u6765\u5904\u7406\u6b21\u5bf9\u89d2\u7ebf\u7ea6\u675f\u3002</p> <p></p> <p> \u56fe\uff1a\u5904\u7406\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f </p>"},{"location":"chapter_backtracking/n_queens_problem/#_3","title":"\u4ee3\u7801\u5b9e\u73b0","text":"<p>\u8bf7\u6ce8\u610f\uff0c\\(n\\) \u7ef4\u65b9\u9635\u4e2d \\(row - col\\) \u7684\u8303\u56f4\u662f \\([-n + 1, n - 1]\\) \uff0c\\(row + col\\) \u7684\u8303\u56f4\u662f \\([0, 2n - 2]\\) \uff0c\u6240\u4ee5\u4e3b\u5bf9\u89d2\u7ebf\u548c\u6b21\u5bf9\u89d2\u7ebf\u7684\u6570\u91cf\u90fd\u4e3a \\(2n - 1\\) \uff0c\u5373\u6570\u7ec4 <code>diag1</code> \u548c <code>diag2</code> \u7684\u957f\u5ea6\u90fd\u4e3a \\(2n - 1\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust n_queens.java<pre><code>/* \u56de\u6eaf\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,\nboolean[] cols, boolean[] diags1, boolean[] diags2) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row == n) {\nList&lt;List&lt;String&gt;&gt; copyState = new ArrayList&lt;&gt;();\nfor (List&lt;String&gt; sRow : state) {\ncopyState.add(new ArrayList&lt;&gt;(sRow));\n}\nres.add(copyState);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (int col = 0; col &lt; n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nint diag1 = row - col + n - 1;\nint diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif (!cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2]) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate.get(row).set(col, \"Q\");\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate.get(row).set(col, \"#\");\ncols[col] = diags1[diag1] = diags2[diag2] = false;\n}\n}\n}\n/* \u6c42\u89e3 N \u7687\u540e */\nList&lt;List&lt;List&lt;String&gt;&gt;&gt; nQueens(int n) {\n// \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\nList&lt;List&lt;String&gt;&gt; state = new ArrayList&lt;&gt;();\nfor (int i = 0; i &lt; n; i++) {\nList&lt;String&gt; row = new ArrayList&lt;&gt;();\nfor (int j = 0; j &lt; n; j++) {\nrow.add(\"#\");\n}\nstate.add(row);\n}\nboolean[] cols = new boolean[n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nboolean[] diags1 = new boolean[2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nboolean[] diags2 = new boolean[2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nList&lt;List&lt;List&lt;String&gt;&gt;&gt; res = new ArrayList&lt;&gt;();\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n</code></pre> n_queens.cpp<pre><code>/* \u56de\u6eaf\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,\nvector&lt;bool&gt; &amp;diags1, vector&lt;bool&gt; &amp;diags2) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row == n) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (int col = 0; col &lt; n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nint diag1 = row - col + n - 1;\nint diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif (!cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2]) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = \"Q\";\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = \"#\";\ncols[col] = diags1[diag1] = diags2[diag2] = false;\n}\n}\n}\n/* \u6c42\u89e3 N \u7687\u540e */\nvector&lt;vector&lt;vector&lt;string&gt;&gt;&gt; nQueens(int n) {\n// \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\nvector&lt;vector&lt;string&gt;&gt; state(n, vector&lt;string&gt;(n, \"#\"));\nvector&lt;bool&gt; cols(n, false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nvector&lt;bool&gt; diags1(2 * n - 1, false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nvector&lt;bool&gt; diags2(2 * n - 1, false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nvector&lt;vector&lt;vector&lt;string&gt;&gt;&gt; res;\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n</code></pre> n_queens.py<pre><code>def backtrack(\nrow: int,\nn: int,\nstate: list[list[str]],\nres: list[list[list[str]]],\ncols: list[bool],\ndiags1: list[bool],\ndiags2: list[bool],\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e\"\"\"\n# \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif row == n:\nres.append([list(row) for row in state])\nreturn\n# \u904d\u5386\u6240\u6709\u5217\nfor col in range(n):\n# \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\ndiag1 = row - col + n - 1\ndiag2 = row + col\n# \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif not cols[col] and not diags1[diag1] and not diags2[diag2]:\n# \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = \"Q\"\ncols[col] = diags1[diag1] = diags2[diag2] = True\n# \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2)\n# \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = \"#\"\ncols[col] = diags1[diag1] = diags2[diag2] = False\ndef n_queens(n: int) -&gt; list[list[list[str]]]:\n\"\"\"\u6c42\u89e3 N \u7687\u540e\"\"\"\n# \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\nstate = [[\"#\" for _ in range(n)] for _ in range(n)]\ncols = [False] * n # \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\ndiags1 = [False] * (2 * n - 1) # \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\ndiags2 = [False] * (2 * n - 1) # \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nres = []\nbacktrack(0, n, state, res, cols, diags1, diags2)\nreturn res\n</code></pre> n_queens.go<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunc backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif row == n {\nnewState := make([][]string, len(*state))\nfor i, _ := range newState {\nnewState[i] = make([]string, len((*state)[0]))\ncopy(newState[i], (*state)[i])\n}\n*res = append(*res, newState)\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor col := 0; col &lt; n; col++ {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\ndiag1 := row - col + n - 1\ndiag2 := row + col\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif !(*cols)[col] &amp;&amp; !(*diags1)[diag1] &amp;&amp; !(*diags2)[diag2] {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n(*state)[row][col] = \"Q\"\n(*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = true, true, true\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row+1, n, state, res, cols, diags1, diags2)\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n(*state)[row][col] = \"#\"\n(*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false\n}\n}\n}\n/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunc backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif row == n {\nnewState := make([][]string, len(*state))\nfor i, _ := range newState {\nnewState[i] = make([]string, len((*state)[0]))\ncopy(newState[i], (*state)[i])\n}\n*res = append(*res, newState)\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor col := 0; col &lt; n; col++ {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\ndiag1 := row - col + n - 1\ndiag2 := row + col\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif !(*cols)[col] &amp;&amp; !(*diags1)[diag1] &amp;&amp; !(*diags2)[diag2] {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\n(*state)[row][col] = \"Q\"\n(*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = true, true, true\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row+1, n, state, res, cols, diags1, diags2)\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\n(*state)[row][col] = \"#\"\n(*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false\n}\n}\n}\nfunc nQueens(n int) [][][]string {\n// \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\nstate := make([][]string, n)\nfor i := 0; i &lt; n; i++ {\nrow := make([]string, n)\nfor i := 0; i &lt; n; i++ {\nrow[i] = \"#\"\n}\nstate[i] = row\n}\n// \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\ncols := make([]bool, n)\ndiags1 := make([]bool, 2*n-1)\ndiags2 := make([]bool, 2*n-1)\nres := make([][][]string, 0)\nbacktrack(0, n, &amp;state, &amp;res, &amp;cols, &amp;diags1, &amp;diags2)\nreturn res\n}\n</code></pre> n_queens.js<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunction backtrack(row, n, state, res, cols, diags1, diags2) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row === n) {\nres.push(state.map((row) =&gt; row.slice()));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (let col = 0; col &lt; n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nconst diag1 = row - col + n - 1;\nconst diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif (!cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2]) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = 'Q';\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = '#';\ncols[col] = diags1[diag1] = diags2[diag2] = false;\n}\n}\n}\n/* \u6c42\u89e3 N \u7687\u540e */\nfunction nQueens(n) {\n// \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\nconst state = Array.from({ length: n }, () =&gt; Array(n).fill('#'));\nconst cols = Array(n).fill(false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nconst diags1 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nconst diags2 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nconst res = [];\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n</code></pre> n_queens.ts<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunction backtrack(\nrow: number,\nn: number,\nstate: string[][],\nres: string[][][],\ncols: boolean[],\ndiags1: boolean[],\ndiags2: boolean[]\n): void {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row === n) {\nres.push(state.map((row) =&gt; row.slice()));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (let col = 0; col &lt; n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nconst diag1 = row - col + n - 1;\nconst diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif (!cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2]) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = 'Q';\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = '#';\ncols[col] = diags1[diag1] = diags2[diag2] = false;\n}\n}\n}\n/* \u6c42\u89e3 N \u7687\u540e */\nfunction nQueens(n: number): string[][][] {\n// \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\nconst state = Array.from({ length: n }, () =&gt; Array(n).fill('#'));\nconst cols = Array(n).fill(false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nconst diags1 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nconst diags2 = Array(2 * n - 1).fill(false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nconst res: string[][][] = [];\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n</code></pre> n_queens.c<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n</code></pre> n_queens.cs<pre><code>/* \u56de\u6eaf\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,\nbool[] cols, bool[] diags1, bool[] diags2) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row == n) {\nList&lt;List&lt;string&gt;&gt; copyState = new List&lt;List&lt;string&gt;&gt;();\nforeach (List&lt;string&gt; sRow in state) {\ncopyState.Add(new List&lt;string&gt;(sRow));\n}\nres.Add(copyState);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (int col = 0; col &lt; n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nint diag1 = row - col + n - 1;\nint diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif (!cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2]) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = \"Q\";\ncols[col] = diags1[diag1] = diags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = \"#\";\ncols[col] = diags1[diag1] = diags2[diag2] = false;\n}\n}\n}\n/* \u6c42\u89e3 N \u7687\u540e */\nList&lt;List&lt;List&lt;string&gt;&gt;&gt; nQueens(int n) {\n// \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\nList&lt;List&lt;string&gt;&gt; state = new List&lt;List&lt;string&gt;&gt;();\nfor (int i = 0; i &lt; n; i++) {\nList&lt;string&gt; row = new List&lt;string&gt;();\nfor (int j = 0; j &lt; n; j++) {\nrow.Add(\"#\");\n}\nstate.Add(row);\n}\nbool[] cols = new bool[n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nbool[] diags1 = new bool[2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nbool[] diags2 = new bool[2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nList&lt;List&lt;List&lt;string&gt;&gt;&gt; res = new List&lt;List&lt;List&lt;string&gt;&gt;&gt;();\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n</code></pre> n_queens.swift<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfunc backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]], cols: inout [Bool], diags1: inout [Bool], diags2: inout [Bool]) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif row == n {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor col in 0 ..&lt; n {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nlet diag1 = row - col + n - 1\nlet diag2 = row + col\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif !cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2] {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = \"Q\"\ncols[col] = true\ndiags1[diag1] = true\ndiags2[diag2] = true\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row: row + 1, n: n, state: &amp;state, res: &amp;res, cols: &amp;cols, diags1: &amp;diags1, diags2: &amp;diags2)\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = \"#\"\ncols[col] = false\ndiags1[diag1] = false\ndiags2[diag2] = false\n}\n}\n}\n/* \u6c42\u89e3 N \u7687\u540e */\nfunc nQueens(n: Int) -&gt; [[[String]]] {\n// \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\nvar state = Array(repeating: Array(repeating: \"#\", count: n), count: n)\nvar cols = Array(repeating: false, count: n) // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nvar diags1 = Array(repeating: false, count: 2 * n - 1) // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nvar diags2 = Array(repeating: false, count: 2 * n - 1) // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nvar res: [[[String]]] = []\nbacktrack(row: 0, n: n, state: &amp;state, res: &amp;res, cols: &amp;cols, diags1: &amp;diags1, diags2: &amp;diags2)\nreturn res\n}\n</code></pre> n_queens.zig<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n</code></pre> n_queens.dart<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nvoid backtrack(\nint row,\nint n,\nList&lt;List&lt;String&gt;&gt; state,\nList&lt;List&lt;List&lt;String&gt;&gt;&gt; res,\nList&lt;bool&gt; cols,\nList&lt;bool&gt; diags1,\nList&lt;bool&gt; diags2,\n) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (row == n) {\nList&lt;List&lt;String&gt;&gt; copyState = [];\nfor (List&lt;String&gt; sRow in state) {\ncopyState.add(List.from(sRow));\n}\nres.add(copyState);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor (int col = 0; col &lt; n; col++) {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nint diag1 = row - col + n - 1;\nint diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif (!cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2]) {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate[row][col] = \"Q\";\ncols[col] = true;\ndiags1[diag1] = true;\ndiags2[diag2] = true;\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate[row][col] = \"#\";\ncols[col] = false;\ndiags1[diag1] = false;\ndiags2[diag2] = false;\n}\n}\n}\n/* \u6c42\u89e3 N \u7687\u540e */\nList&lt;List&lt;List&lt;String&gt;&gt;&gt; nQueens(int n) {\n// \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\nList&lt;List&lt;String&gt;&gt; state = List.generate(n, (index) =&gt; List.filled(n, \"#\"));\nList&lt;bool&gt; cols = List.filled(n, false); // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nList&lt;bool&gt; diags1 = List.filled(2 * n - 1, false); // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nList&lt;bool&gt; diags2 = List.filled(2 * n - 1, false); // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nList&lt;List&lt;List&lt;String&gt;&gt;&gt; res = [];\nbacktrack(0, n, state, res, cols, diags1, diags2);\nreturn res;\n}\n</code></pre> n_queens.rs<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1aN \u7687\u540e */\nfn backtrack(row: usize, n: usize, state: &amp;mut Vec&lt;Vec&lt;String&gt;&gt;, res: &amp;mut Vec&lt;Vec&lt;Vec&lt;String&gt;&gt;&gt;,\ncols: &amp;mut [bool], diags1: &amp;mut [bool], diags2: &amp;mut [bool]) {\n// \u5f53\u653e\u7f6e\u5b8c\u6240\u6709\u884c\u65f6\uff0c\u8bb0\u5f55\u89e3\nif row == n {\nlet mut copy_state: Vec&lt;Vec&lt;String&gt;&gt; = Vec::new();\nfor s_row in state.clone() {\ncopy_state.push(s_row);\n}\nres.push(copy_state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u5217\nfor col in 0..n {\n// \u8ba1\u7b97\u8be5\u683c\u5b50\u5bf9\u5e94\u7684\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\nlet diag1 = row + n - 1 - col;\nlet diag2 = row + col;\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8be5\u683c\u5b50\u6240\u5728\u5217\u3001\u4e3b\u5bf9\u89d2\u7ebf\u3001\u526f\u5bf9\u89d2\u7ebf\u5b58\u5728\u7687\u540e\nif !cols[col] &amp;&amp; !diags1[diag1] &amp;&amp; !diags2[diag2] {\n// \u5c1d\u8bd5\uff1a\u5c06\u7687\u540e\u653e\u7f6e\u5728\u8be5\u683c\u5b50\nstate.get_mut(row).unwrap()[col] = \"Q\".into();\n(cols[col], diags1[diag1], diags2[diag2]) = (true, true, true);\n// \u653e\u7f6e\u4e0b\u4e00\u884c\nbacktrack(row + 1, n, state, res, cols, diags1, diags2);\n// \u56de\u9000\uff1a\u5c06\u8be5\u683c\u5b50\u6062\u590d\u4e3a\u7a7a\u4f4d\nstate.get_mut(row).unwrap()[col] = \"#\".into();\n(cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);\n}\n}\n}\n/* \u6c42\u89e3 N \u7687\u540e */\nfn n_queens(n: usize) -&gt; Vec&lt;Vec&lt;Vec&lt;String&gt;&gt;&gt; {\n// \u521d\u59cb\u5316 n*n \u5927\u5c0f\u7684\u68cb\u76d8\uff0c\u5176\u4e2d 'Q' \u4ee3\u8868\u7687\u540e\uff0c'#' \u4ee3\u8868\u7a7a\u4f4d\nlet mut state: Vec&lt;Vec&lt;String&gt;&gt; = Vec::new();\nfor _ in 0..n {\nlet mut row: Vec&lt;String&gt; = Vec::new();\nfor _ in 0..n {\nrow.push(\"#\".into());\n}\nstate.push(row);\n}\nlet mut cols = vec![false; n]; // \u8bb0\u5f55\u5217\u662f\u5426\u6709\u7687\u540e\nlet mut diags1 = vec![false; 2 * n - 1]; // \u8bb0\u5f55\u4e3b\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nlet mut diags2 = vec![false; 2 * n - 1]; // \u8bb0\u5f55\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u6709\u7687\u540e\nlet mut res: Vec&lt;Vec&lt;Vec&lt;String&gt;&gt;&gt; = Vec::new();\nbacktrack(0, n, &amp;mut state, &amp;mut res, &amp;mut cols, &amp;mut diags1, &amp;mut diags2);\nres\n}\n</code></pre> <p>\u9010\u884c\u653e\u7f6e \\(n\\) \u6b21\uff0c\u8003\u8651\u5217\u7ea6\u675f\uff0c\u5219\u4ece\u7b2c\u4e00\u884c\u5230\u6700\u540e\u4e00\u884c\u5206\u522b\u6709 \\(n, n-1, \\cdots, 2, 1\\) \u4e2a\u9009\u62e9\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n!)\\) \u3002\u5b9e\u9645\u4e0a\uff0c\u6839\u636e\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u526a\u679d\u4e5f\u80fd\u591f\u5927\u5e45\u5730\u7f29\u5c0f\u641c\u7d22\u7a7a\u95f4\uff0c\u56e0\u800c\u641c\u7d22\u6548\u7387\u5f80\u5f80\u4f18\u4e8e\u4ee5\u4e0a\u65f6\u95f4\u590d\u6742\u5ea6\u3002</p> <p>\u6570\u7ec4 <code>state</code> \u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff0c\u6570\u7ec4 <code>cols</code> , <code>diags1</code> , <code>diags2</code> \u7686\u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002</p>"},{"location":"chapter_backtracking/permutations_problem/","title":"13.2. \u00a0 \u5168\u6392\u5217\u95ee\u9898","text":"<p>\u5168\u6392\u5217\u95ee\u9898\u662f\u56de\u6eaf\u7b97\u6cd5\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u3002\u5b83\u7684\u5b9a\u4e49\u662f\u5728\u7ed9\u5b9a\u4e00\u4e2a\u96c6\u5408\uff08\u5982\u4e00\u4e2a\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\uff09\u7684\u60c5\u51b5\u4e0b\uff0c\u627e\u51fa\u8fd9\u4e2a\u96c6\u5408\u4e2d\u5143\u7d20\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002</p> <p>\u4e0b\u8868\u5217\u4e3e\u4e86\u51e0\u4e2a\u793a\u4f8b\u6570\u636e\uff0c\u5305\u62ec\u8f93\u5165\u6570\u7ec4\u548c\u5bf9\u5e94\u7684\u6240\u6709\u6392\u5217\u3002</p> <p> \u8868\uff1a\u6570\u7ec4\u4e0e\u94fe\u8868\u7684\u6548\u7387\u5bf9\u6bd4 </p> \u8f93\u5165\u6570\u7ec4 \u6240\u6709\u6392\u5217 \\([1]\\) \\([1]\\) \\([1, 2]\\) \\([1, 2], [2, 1]\\) \\([1, 2, 3]\\) \\([1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]\\)"},{"location":"chapter_backtracking/permutations_problem/#1321","title":"13.2.1. \u00a0 \u65e0\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u51b5","text":"<p>Question</p> <p>\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u4e0d\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002</p> <p>\u4ece\u56de\u6eaf\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u751f\u6210\u6392\u5217\u7684\u8fc7\u7a0b\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u9009\u62e9\u7684\u7ed3\u679c\u3002\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u4e3a \\([1, 2, 3]\\) \uff0c\u5982\u679c\u6211\u4eec\u5148\u9009\u62e9 \\(1\\) \u3001\u518d\u9009\u62e9 \\(3\\) \u3001\u6700\u540e\u9009\u62e9 \\(2\\) \uff0c\u5219\u83b7\u5f97\u6392\u5217 \\([1, 3, 2]\\) \u3002\u56de\u9000\u8868\u793a\u64a4\u9500\u4e00\u4e2a\u9009\u62e9\uff0c\u4e4b\u540e\u7ee7\u7eed\u5c1d\u8bd5\u5176\u4ed6\u9009\u62e9\u3002</p> <p>\u4ece\u56de\u6eaf\u4ee3\u7801\u7684\u89d2\u5ea6\u770b\uff0c\u5019\u9009\u96c6\u5408 <code>choices</code> \u662f\u8f93\u5165\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u72b6\u6001 <code>state</code> \u662f\u76f4\u81f3\u76ee\u524d\u5df2\u88ab\u9009\u62e9\u7684\u5143\u7d20\u3002\u8bf7\u6ce8\u610f\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u5141\u8bb8\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u56e0\u6b64 <code>state</code> \u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u5e94\u8be5\u662f\u552f\u4e00\u7684\u3002</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u641c\u7d22\u8fc7\u7a0b\u5c55\u5f00\u6210\u4e00\u4e2a\u9012\u5f52\u6811\uff0c\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u5f53\u524d\u72b6\u6001 <code>state</code> \u3002\u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u7ecf\u8fc7\u4e09\u8f6e\u9009\u62e9\u540e\u5230\u8fbe\u53f6\u8282\u70b9\uff0c\u6bcf\u4e2a\u53f6\u8282\u70b9\u90fd\u5bf9\u5e94\u4e00\u4e2a\u6392\u5217\u3002</p> <p></p> <p> \u56fe\uff1a\u5168\u6392\u5217\u7684\u9012\u5f52\u6811 </p>"},{"location":"chapter_backtracking/permutations_problem/#_1","title":"\u91cd\u590d\u9009\u62e9\u526a\u679d","text":"<p>\u4e3a\u4e86\u5b9e\u73b0\u6bcf\u4e2a\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u6211\u4eec\u8003\u8651\u5f15\u5165\u4e00\u4e2a\u5e03\u5c14\u578b\u6570\u7ec4 <code>selected</code> \uff0c\u5176\u4e2d <code>selected[i]</code> \u8868\u793a <code>choices[i]</code> \u662f\u5426\u5df2\u88ab\u9009\u62e9\u3002\u526a\u679d\u7684\u5b9e\u73b0\u539f\u7406\u4e3a\uff1a</p> <ul> <li>\u5728\u505a\u51fa\u9009\u62e9 <code>choice[i]</code> \u540e\uff0c\u6211\u4eec\u5c31\u5c06 <code>selected[i]</code> \u8d4b\u503c\u4e3a \\(\\text{True}\\) \uff0c\u4ee3\u8868\u5b83\u5df2\u88ab\u9009\u62e9\u3002</li> <li>\u904d\u5386\u9009\u62e9\u5217\u8868 <code>choices</code> \u65f6\uff0c\u8df3\u8fc7\u6240\u6709\u5df2\u88ab\u9009\u62e9\u8fc7\u7684\u8282\u70b9\uff0c\u5373\u526a\u679d\u3002</li> </ul> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u7b2c\u4e00\u8f6e\u9009\u62e9 1 \uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9 3 \uff0c\u7b2c\u4e09\u8f6e\u9009\u62e9 2 \uff0c\u5219\u9700\u8981\u5728\u7b2c\u4e8c\u8f6e\u526a\u6389\u5143\u7d20 1 \u7684\u5206\u652f\uff0c\u5728\u7b2c\u4e09\u8f6e\u526a\u6389\u5143\u7d20 1, 3 \u7684\u5206\u652f\u3002</p> <p></p> <p> \u56fe\uff1a\u5168\u6392\u5217\u526a\u679d\u793a\u4f8b </p> <p>\u89c2\u5bdf\u4e0a\u56fe\u53d1\u73b0\uff0c\u8be5\u526a\u679d\u64cd\u4f5c\u5c06\u641c\u7d22\u7a7a\u95f4\u5927\u5c0f\u4ece \\(O(n^n)\\) \u964d\u4f4e\u81f3 \\(O(n!)\\) \u3002</p>"},{"location":"chapter_backtracking/permutations_problem/#_2","title":"\u4ee3\u7801\u5b9e\u73b0","text":"<p>\u60f3\u6e05\u695a\u4ee5\u4e0a\u4fe1\u606f\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u6846\u67b6\u4ee3\u7801\u4e2d\u505a\u201c\u5b8c\u5f62\u586b\u7a7a\u201d\u4e86\u3002\u4e3a\u4e86\u7f29\u77ed\u4ee3\u7801\u884c\u6570\uff0c\u6211\u4eec\u4e0d\u5355\u72ec\u5b9e\u73b0\u6846\u67b6\u4ee3\u7801\u4e2d\u7684\u5404\u4e2a\u51fd\u6570\uff0c\u800c\u662f\u5c06\u4ed6\u4eec\u5c55\u5f00\u5728 <code>backtrack()</code> \u51fd\u6570\u4e2d\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust permutations_i.java<pre><code>/* \u56de\u6eaf\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// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.size() == choices.length) {\nres.add(new ArrayList&lt;Integer&gt;(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i &lt; choices.length; i++) {\nint choice = choices[i];\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.add(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.remove(state.size() - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nList&lt;List&lt;Integer&gt;&gt; permutationsI(int[] nums) {\nList&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;List&lt;Integer&gt;&gt;();\nbacktrack(new ArrayList&lt;Integer&gt;(), nums, new boolean[nums.length], res);\nreturn res;\n}\n</code></pre> permutations_i.cpp<pre><code>/* \u56de\u6eaf\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// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.size() == choices.size()) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i &lt; choices.size(); i++) {\nint choice = choices[i];\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.push_back(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop_back();\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nvector&lt;vector&lt;int&gt;&gt; permutationsI(vector&lt;int&gt; nums) {\nvector&lt;int&gt; state;\nvector&lt;bool&gt; selected(nums.size(), false);\nvector&lt;vector&lt;int&gt;&gt; res;\nbacktrack(state, nums, selected, res);\nreturn res;\n}\n</code></pre> permutations_i.py<pre><code>def backtrack(\nstate: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I\"\"\"\n# \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif len(state) == len(choices):\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i, choice in enumerate(choices):\n# \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20\nif not selected[i]:\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = True\nstate.append(choice)\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = False\nstate.pop()\ndef permutations_i(nums: list[int]) -&gt; list[list[int]]:\n\"\"\"\u5168\u6392\u5217 I\"\"\"\nres = []\nbacktrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\nreturn res\n</code></pre> permutations_i.go<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunc backtrackI(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif len(*state) == len(*choices) {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i := 0; i &lt; len(*choices); i++ {\nchoice := (*choices)[i]\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif !(*selected)[i] {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n(*selected)[i] = true\n*state = append(*state, choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrackI(state, choices, selected, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n(*selected)[i] = false\n*state = (*state)[:len(*state)-1]\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums []int) [][]int {\nres := make([][]int, 0)\nstate := make([]int, 0)\nselected := make([]bool, len(nums))\nbacktrackI(&amp;state, &amp;nums, &amp;selected, &amp;res)\nreturn res\n}\n</code></pre> permutations_i.js<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunction backtrack(state, choices, selected, res) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.length === choices.length) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nchoices.forEach((choice, i) =&gt; {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.push(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop();\n}\n});\n}\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums) {\nconst res = [];\nbacktrack([], nums, Array(nums.length).fill(false), res);\nreturn res;\n}\n</code></pre> permutations_i.ts<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunction backtrack(\nstate: number[],\nchoices: number[],\nselected: boolean[],\nres: number[][]\n): void {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.length === choices.length) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nchoices.forEach((choice, i) =&gt; {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.push(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop();\n}\n});\n}\n/* \u5168\u6392\u5217 I */\nfunction permutationsI(nums: number[]): number[][] {\nconst res: number[][] = [];\nbacktrack([], nums, Array(nums.length).fill(false), res);\nreturn res;\n}\n</code></pre> permutations_i.c<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(vector *state, vector *choices, vector *selected, vector *res) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state-&gt;size == choices-&gt;size) {\nvector *newState = newVector();\nfor (int i = 0; i &lt; state-&gt;size; i++) {\nvectorPushback(newState, state-&gt;data[i], sizeof(int));\n}\nvectorPushback(res, newState, sizeof(vector));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i &lt; choices-&gt;size; i++) {\nint *choice = malloc(sizeof(int));\n*choice = *((int *)(choices-&gt;data[i]));\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nbool select = *((bool *)(selected-&gt;data[i]));\nif (!select) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n*((bool *)selected-&gt;data[i]) = true;\nvectorPushback(state, choice, sizeof(int));\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n*((bool *)selected-&gt;data[i]) = false;\nvectorPopback(state);\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nvector *permutationsI(vector *nums) {\nvector *iState = newVector();\nint select[3] = {false, false, false};\nvector *bSelected = newVector();\nfor (int i = 0; i &lt; nums-&gt;size; i++) {\nvectorPushback(bSelected, &amp;select[i], sizeof(int));\n}\nvector *res = newVector();\n// \u524d\u5e8f\u904d\u5386\nbacktrack(iState, nums, bSelected, res);\nreturn res;\n}\n</code></pre> permutations_i.cs<pre><code>/* \u56de\u6eaf\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// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.Count == choices.Length) {\nres.Add(new List&lt;int&gt;(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i &lt; choices.Length; i++) {\nint choice = choices[i];\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.Add(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.RemoveAt(state.Count - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nList&lt;List&lt;int&gt;&gt; permutationsI(int[] nums) {\nList&lt;List&lt;int&gt;&gt; res = new List&lt;List&lt;int&gt;&gt;();\nbacktrack(new List&lt;int&gt;(), nums, new bool[nums.Length], res);\nreturn res;\n}\n</code></pre> permutations_i.swift<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif state.count == choices.count {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (i, choice) in choices.enumerated() {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif !selected[i] {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true\nstate.append(choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state: &amp;state, choices: choices, selected: &amp;selected, res: &amp;res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false\nstate.removeLast()\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nfunc permutationsI(nums: [Int]) -&gt; [[Int]] {\nvar state: [Int] = []\nvar selected = Array(repeating: false, count: nums.count)\nvar res: [[Int]] = []\nbacktrack(state: &amp;state, choices: nums, selected: &amp;selected, res: &amp;res)\nreturn res\n}\n</code></pre> permutations_i.zig<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\n</code></pre> permutations_i.dart<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 I */\nvoid backtrack(\nList&lt;int&gt; state,\nList&lt;int&gt; choices,\nList&lt;bool&gt; selected,\nList&lt;List&lt;int&gt;&gt; res,\n) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.length == choices.length) {\nres.add(List.from(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i &lt; choices.length; i++) {\nint choice = choices[i];\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif (!selected[i]) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.add(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.removeLast();\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nList&lt;List&lt;int&gt;&gt; permutationsI(List&lt;int&gt; nums) {\nList&lt;List&lt;int&gt;&gt; res = [];\nbacktrack([], nums, List.filled(nums.length, false), res);\nreturn res;\n}\n</code></pre> permutations_i.rs<pre><code>/* \u56de\u6eaf\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// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif state.len() == choices.len() {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i in 0..choices.len() {\nlet choice = choices[i];\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif !selected[i] {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nselected[i] = true;\nstate.push(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state.clone(), choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.remove(state.len() - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 I */\nfn permutations_i(nums: &amp;mut [i32]) -&gt; Vec&lt;Vec&lt;i32&gt;&gt; {\nlet mut res = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nbacktrack(Vec::new(), nums, &amp;mut vec![false; nums.len()], &amp;mut res);\nres\n}\n</code></pre>"},{"location":"chapter_backtracking/permutations_problem/#1322","title":"13.2.2. \u00a0 \u8003\u8651\u76f8\u7b49\u5143\u7d20\u7684\u60c5\u51b5","text":"<p>Question</p> <p>\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd4\u56de\u6240\u6709\u4e0d\u91cd\u590d\u7684\u6392\u5217\u3002</p> <p>\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u4e3a \\([1, 1, 2]\\) \u3002\u4e3a\u4e86\u65b9\u4fbf\u533a\u5206\u4e24\u4e2a\u91cd\u590d\u5143\u7d20 \\(1\\) \uff0c\u6211\u4eec\u5c06\u7b2c\u4e8c\u4e2a \\(1\\) \u8bb0\u4e3a \\(\\hat{1}\\) \u3002</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4e0a\u8ff0\u65b9\u6cd5\u751f\u6210\u7684\u6392\u5217\u6709\u4e00\u534a\u90fd\u662f\u91cd\u590d\u7684\u3002</p> <p></p> <p> \u56fe\uff1a\u91cd\u590d\u6392\u5217 </p> <p>\u90a3\u4e48\u5982\u4f55\u53bb\u9664\u91cd\u590d\u7684\u6392\u5217\u5462\uff1f\u6700\u76f4\u63a5\u5730\uff0c\u8003\u8651\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u76f4\u63a5\u5bf9\u6392\u5217\u7ed3\u679c\u8fdb\u884c\u53bb\u91cd\u3002\u7136\u800c\u8fd9\u6837\u505a\u4e0d\u591f\u4f18\u96c5\uff0c\u56e0\u4e3a\u751f\u6210\u91cd\u590d\u6392\u5217\u7684\u641c\u7d22\u5206\u652f\u662f\u6ca1\u6709\u5fc5\u8981\u7684\uff0c\u5e94\u5f53\u88ab\u63d0\u524d\u8bc6\u522b\u5e76\u526a\u679d\uff0c\u8fd9\u6837\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u7b97\u6cd5\u6548\u7387\u3002</p>"},{"location":"chapter_backtracking/permutations_problem/#_3","title":"\u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"<p>\u89c2\u5bdf\u53d1\u73b0\uff0c\u5728\u7b2c\u4e00\u8f6e\u4e2d\uff0c\u9009\u62e9 \\(1\\) \u6216\u9009\u62e9 \\(\\hat{1}\\) \u662f\u7b49\u4ef7\u7684\uff0c\u5728\u8fd9\u4e24\u4e2a\u9009\u62e9\u4e4b\u4e0b\u751f\u6210\u7684\u6240\u6709\u6392\u5217\u90fd\u662f\u91cd\u590d\u7684\u3002\u56e0\u6b64\u5e94\u8be5\u628a \\(\\hat{1}\\) \u526a\u679d\u6389\u3002</p> <p>\u540c\u7406\uff0c\u5728\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(2\\) \u540e\uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9\u4e2d\u7684 \\(1\\) \u548c \\(\\hat{1}\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5206\u652f\uff0c\u56e0\u6b64\u4e5f\u5e94\u5c06\u7b2c\u4e8c\u8f6e\u7684 \\(\\hat{1}\\) \u526a\u679d\u3002</p> <p>\u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u5728\u67d0\u4e00\u8f6e\u9009\u62e9\u4e2d\uff0c\u4fdd\u8bc1\u591a\u4e2a\u76f8\u7b49\u7684\u5143\u7d20\u4ec5\u88ab\u9009\u62e9\u4e00\u6b21\u3002</p> <p></p> <p> \u56fe\uff1a\u91cd\u590d\u6392\u5217\u526a\u679d </p>"},{"location":"chapter_backtracking/permutations_problem/#_4","title":"\u4ee3\u7801\u5b9e\u73b0","text":"<p>\u5728\u4e0a\u4e00\u9898\u7684\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u8003\u8651\u5728\u6bcf\u4e00\u8f6e\u9009\u62e9\u4e2d\u5f00\u542f\u4e00\u4e2a\u54c8\u5e0c\u8868 <code>duplicated</code> \uff0c\u7528\u4e8e\u8bb0\u5f55\u8be5\u8f6e\u4e2d\u5df2\u7ecf\u5c1d\u8bd5\u8fc7\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u91cd\u590d\u5143\u7d20\u526a\u679d\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust permutations_ii.java<pre><code>/* \u56de\u6eaf\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// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.size() == choices.length) {\nres.add(new ArrayList&lt;Integer&gt;(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nSet&lt;Integer&gt; duplicated = new HashSet&lt;Integer&gt;();\nfor (int i = 0; i &lt; choices.length; i++) {\nint choice = choices[i];\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif (!selected[i] &amp;&amp; !duplicated.contains(choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.add(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.remove(state.size() - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nList&lt;List&lt;Integer&gt;&gt; permutationsII(int[] nums) {\nList&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;List&lt;Integer&gt;&gt;();\nbacktrack(new ArrayList&lt;Integer&gt;(), nums, new boolean[nums.length], res);\nreturn res;\n}\n</code></pre> permutations_ii.cpp<pre><code>/* \u56de\u6eaf\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// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.size() == choices.size()) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nunordered_set&lt;int&gt; duplicated;\nfor (int i = 0; i &lt; choices.size(); i++) {\nint choice = choices[i];\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif (!selected[i] &amp;&amp; duplicated.find(choice) == duplicated.end()) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.emplace(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.push_back(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop_back();\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nvector&lt;vector&lt;int&gt;&gt; permutationsII(vector&lt;int&gt; nums) {\nvector&lt;int&gt; state;\nvector&lt;bool&gt; selected(nums.size(), false);\nvector&lt;vector&lt;int&gt;&gt; res;\nbacktrack(state, nums, selected, res);\nreturn res;\n}\n</code></pre> permutations_ii.py<pre><code>def backtrack(\nstate: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II\"\"\"\n# \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif len(state) == len(choices):\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nduplicated = set[int]()\nfor i, choice in enumerate(choices):\n# \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif not selected[i] and choice not in duplicated:\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.add(choice) # \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = True\nstate.append(choice)\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = False\nstate.pop()\ndef permutations_ii(nums: list[int]) -&gt; list[list[int]]:\n\"\"\"\u5168\u6392\u5217 II\"\"\"\nres = []\nbacktrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\nreturn res\n</code></pre> permutations_ii.go<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunc backtrackII(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif len(*state) == len(*choices) {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nduplicated := make(map[int]struct{}, 0)\nfor i := 0; i &lt; len(*choices); i++ {\nchoice := (*choices)[i]\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif _, ok := duplicated[choice]; !ok &amp;&amp; !(*selected)[i] {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\n// \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nduplicated[choice] = struct{}{}\n(*selected)[i] = true\n*state = append(*state, choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrackI(state, choices, selected, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n(*selected)[i] = false\n*state = (*state)[:len(*state)-1]\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums []int) [][]int {\nres := make([][]int, 0)\nstate := make([]int, 0)\nselected := make([]bool, len(nums))\nbacktrackII(&amp;state, &amp;nums, &amp;selected, &amp;res)\nreturn res\n}\n</code></pre> permutations_ii.js<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunction backtrack(state, choices, selected, res) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.length === choices.length) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nconst duplicated = new Set();\nchoices.forEach((choice, i) =&gt; {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif (!selected[i] &amp;&amp; !duplicated.has(choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.push(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop();\n}\n});\n}\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums) {\nconst res = [];\nbacktrack([], nums, Array(nums.length).fill(false), res);\nreturn res;\n}\n</code></pre> permutations_ii.ts<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunction backtrack(\nstate: number[],\nchoices: number[],\nselected: boolean[],\nres: number[][]\n): void {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.length === choices.length) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nconst duplicated = new Set();\nchoices.forEach((choice, i) =&gt; {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif (!selected[i] &amp;&amp; !duplicated.has(choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.push(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.pop();\n}\n});\n}\n/* \u5168\u6392\u5217 II */\nfunction permutationsII(nums: number[]): number[][] {\nconst res: number[][] = [];\nbacktrack([], nums, Array(nums.length).fill(false), res);\nreturn res;\n}\n</code></pre> permutations_ii.c<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n</code></pre> permutations_ii.cs<pre><code>/* \u56de\u6eaf\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// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.Count == choices.Length) {\nres.Add(new List&lt;int&gt;(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nISet&lt;int&gt; duplicated = new HashSet&lt;int&gt;();\nfor (int i = 0; i &lt; choices.Length; i++) {\nint choice = choices[i];\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif (!selected[i] &amp;&amp; !duplicated.Contains(choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.Add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.Add(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.RemoveAt(state.Count - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nList&lt;List&lt;int&gt;&gt; permutationsII(int[] nums) {\nList&lt;List&lt;int&gt;&gt; res = new List&lt;List&lt;int&gt;&gt;();\nbacktrack(new List&lt;int&gt;(), nums, new bool[nums.Length], res);\nreturn res;\n}\n</code></pre> permutations_ii.swift<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif state.count == choices.count {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nvar duplicated: Set&lt;Int&gt; = []\nfor (i, choice) in choices.enumerated() {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif !selected[i], !duplicated.contains(choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.insert(choice) // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true\nstate.append(choice)\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state: &amp;state, choices: choices, selected: &amp;selected, res: &amp;res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false\nstate.removeLast()\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nfunc permutationsII(nums: [Int]) -&gt; [[Int]] {\nvar state: [Int] = []\nvar selected = Array(repeating: false, count: nums.count)\nvar res: [[Int]] = []\nbacktrack(state: &amp;state, choices: nums, selected: &amp;selected, res: &amp;res)\nreturn res\n}\n</code></pre> permutations_ii.zig<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n</code></pre> permutations_ii.dart<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5168\u6392\u5217 II */\nvoid backtrack(\nList&lt;int&gt; state,\nList&lt;int&gt; choices,\nList&lt;bool&gt; selected,\nList&lt;List&lt;int&gt;&gt; res,\n) {\n// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif (state.length == choices.length) {\nres.add(List.from(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nSet&lt;int&gt; duplicated = {};\nfor (int i = 0; i &lt; choices.length; i++) {\nint choice = choices[i];\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif (!selected[i] &amp;&amp; !duplicated.contains(choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.add(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.add(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.removeLast();\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nList&lt;List&lt;int&gt;&gt; permutationsII(List&lt;int&gt; nums) {\nList&lt;List&lt;int&gt;&gt; res = [];\nbacktrack([], nums, List.filled(nums.length, false), res);\nreturn res;\n}\n</code></pre> permutations_ii.rs<pre><code>/* \u56de\u6eaf\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// \u5f53\u72b6\u6001\u957f\u5ea6\u7b49\u4e8e\u5143\u7d20\u6570\u91cf\u65f6\uff0c\u8bb0\u5f55\u89e3\nif state.len() == choices.len() {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nlet mut duplicated = HashSet::&lt;i32&gt;::new();\nfor i in 0..choices.len() {\nlet choice = choices[i];\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u5143\u7d20 \u4e14 \u4e0d\u5141\u8bb8\u91cd\u590d\u9009\u62e9\u76f8\u7b49\u5143\u7d20\nif !selected[i] &amp;&amp; !duplicated.contains(&amp;choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nduplicated.insert(choice); // \u8bb0\u5f55\u9009\u62e9\u8fc7\u7684\u5143\u7d20\u503c\nselected[i] = true;\nstate.push(choice);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state.clone(), choices, selected, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nselected[i] = false;\nstate.remove(state.len() - 1);\n}\n}\n}\n/* \u5168\u6392\u5217 II */\nfn permutations_ii(nums: &amp;mut [i32]) -&gt; Vec&lt;Vec&lt;i32&gt;&gt; {\nlet mut res = Vec::new();\nbacktrack(Vec::new(), nums, &amp;mut vec![false; nums.len()], &amp;mut res);\nres\n}\n</code></pre> <p>\u5047\u8bbe\u5143\u7d20\u4e24\u4e24\u4e4b\u95f4\u4e92\u4e0d\u76f8\u540c\uff0c\u5219 \\(n\\) \u4e2a\u5143\u7d20\u5171\u6709 \\(n!\\) \u79cd\u6392\u5217\uff08\u9636\u4e58\uff09\uff1b\u5728\u8bb0\u5f55\u7ed3\u679c\u65f6\uff0c\u9700\u8981\u590d\u5236\u957f\u5ea6\u4e3a \\(n\\) \u7684\u5217\u8868\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002\u56e0\u6b64\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n!n)\\) \u3002</p> <p>\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002<code>selected</code> \u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002\u540c\u4e00\u65f6\u523b\u6700\u591a\u5171\u6709 \\(n\\) \u4e2a <code>duplicated</code> \uff0c\u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\u3002\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002</p>"},{"location":"chapter_backtracking/permutations_problem/#_5","title":"\u4e24\u79cd\u526a\u679d\u5bf9\u6bd4","text":"<p>\u8bf7\u6ce8\u610f\uff0c\u867d\u7136 <code>selected</code> \u548c <code>duplicated</code> \u90fd\u7528\u4f5c\u526a\u679d\uff0c\u4f46\u4e24\u8005\u7684\u76ee\u6807\u4e0d\u540c\uff1a</p> <ul> <li>\u91cd\u590d\u9009\u62e9\u526a\u679d\uff1a\u6574\u4e2a\u641c\u7d22\u8fc7\u7a0b\u4e2d\u53ea\u6709\u4e00\u4e2a <code>selected</code> \u3002\u5b83\u8bb0\u5f55\u7684\u662f\u5f53\u524d\u72b6\u6001\u4e2d\u5305\u542b\u54ea\u4e9b\u5143\u7d20\uff0c\u4f5c\u7528\u662f\u907f\u514d\u67d0\u4e2a\u5143\u7d20\u5728 <code>state</code> \u4e2d\u91cd\u590d\u51fa\u73b0\u3002</li> <li>\u76f8\u7b49\u5143\u7d20\u526a\u679d\uff1a\u6bcf\u8f6e\u9009\u62e9\uff08\u5373\u6bcf\u4e2a\u5f00\u542f\u7684 <code>backtrack</code> \u51fd\u6570\uff09\u90fd\u5305\u542b\u4e00\u4e2a <code>duplicated</code> \u3002\u5b83\u8bb0\u5f55\u7684\u662f\u5728\u904d\u5386\u4e2d\u54ea\u4e9b\u5143\u7d20\u5df2\u88ab\u9009\u62e9\u8fc7\uff0c\u4f5c\u7528\u662f\u4fdd\u8bc1\u76f8\u7b49\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\u3002</li> </ul> <p>\u4e0b\u56fe\u5c55\u793a\u4e86\u4e24\u4e2a\u526a\u679d\u6761\u4ef6\u7684\u751f\u6548\u8303\u56f4\u3002\u6ce8\u610f\uff0c\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u9009\u62e9\uff0c\u4ece\u6839\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u7684\u5404\u4e2a\u8282\u70b9\u6784\u6210\u4e00\u4e2a\u6392\u5217\u3002</p> <p></p> <p> \u56fe\uff1a\u4e24\u79cd\u526a\u679d\u6761\u4ef6\u7684\u4f5c\u7528\u8303\u56f4 </p>"},{"location":"chapter_backtracking/subset_sum_problem/","title":"13.3. \u00a0 \u5b50\u96c6\u548c\u95ee\u9898","text":""},{"location":"chapter_backtracking/subset_sum_problem/#1331","title":"13.3.1. \u00a0 \u65e0\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"<p>Question</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 <code>nums</code> \u548c\u4e00\u4e2a\u76ee\u6807\u6b63\u6574\u6570 <code>target</code> \uff0c\u8bf7\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4f7f\u5f97\u7ec4\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u4e8e <code>target</code> \u3002\u7ed9\u5b9a\u6570\u7ec4\u65e0\u91cd\u590d\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ef\u4ee5\u88ab\u9009\u53d6\u591a\u6b21\u3002\u8bf7\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\uff0c\u5217\u8868\u4e2d\u4e0d\u5e94\u5305\u542b\u91cd\u590d\u7ec4\u5408\u3002</p> <p>\u4f8b\u5982\uff0c\u8f93\u5165\u96c6\u5408 \\(\\{3, 4, 5\\}\\) \u548c\u76ee\u6807\u6574\u6570 \\(9\\) \uff0c\u89e3\u4e3a \\(\\{3, 3, 3\\}, \\{4, 5\\}\\) \u3002\u9700\u8981\u6ce8\u610f\u4e24\u70b9\uff1a</p> <ul> <li>\u8f93\u5165\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u88ab\u65e0\u9650\u6b21\u91cd\u590d\u9009\u53d6\u3002</li> <li>\u5b50\u96c6\u662f\u4e0d\u533a\u5206\u5143\u7d20\u987a\u5e8f\u7684\uff0c\u6bd4\u5982 \\(\\{4, 5\\}\\) \u548c \\(\\{5, 4\\}\\) \u662f\u540c\u4e00\u4e2a\u5b50\u96c6\u3002</li> </ul>"},{"location":"chapter_backtracking/subset_sum_problem/#_1","title":"\u53c2\u8003\u5168\u6392\u5217\u89e3\u6cd5","text":"<p>\u7c7b\u4f3c\u4e8e\u5168\u6392\u5217\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b50\u96c6\u7684\u751f\u6210\u8fc7\u7a0b\u60f3\u8c61\u6210\u4e00\u7cfb\u5217\u9009\u62e9\u7684\u7ed3\u679c\uff0c\u5e76\u5728\u9009\u62e9\u8fc7\u7a0b\u4e2d\u5b9e\u65f6\u66f4\u65b0\u201c\u5143\u7d20\u548c\u201d\uff0c\u5f53\u5143\u7d20\u548c\u7b49\u4e8e <code>target</code> \u65f6\uff0c\u5c31\u5c06\u5b50\u96c6\u8bb0\u5f55\u81f3\u7ed3\u679c\u5217\u8868\u3002</p> <p>\u800c\u4e0e\u5168\u6392\u5217\u95ee\u9898\u4e0d\u540c\u7684\u662f\uff0c\u672c\u9898\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u88ab\u65e0\u9650\u6b21\u9009\u53d6\uff0c\u56e0\u6b64\u65e0\u9700\u501f\u52a9 <code>selected</code> \u5e03\u5c14\u5217\u8868\u6765\u8bb0\u5f55\u5143\u7d20\u662f\u5426\u5df2\u88ab\u9009\u62e9\u3002\u6211\u4eec\u53ef\u4ee5\u5bf9\u5168\u6392\u5217\u4ee3\u7801\u8fdb\u884c\u5c0f\u5e45\u4fee\u6539\uff0c\u521d\u6b65\u5f97\u5230\u89e3\u9898\u4ee3\u7801\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust subset_sum_i_naive.java<pre><code>/* \u56de\u6eaf\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\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nres.add(new ArrayList&lt;&gt;(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i &lt; choices.length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] &gt; target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.remove(state.size() - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList&lt;List&lt;Integer&gt;&gt; subsetSumINaive(int[] nums, int target) {\nList&lt;Integer&gt; state = new ArrayList&lt;&gt;(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0; // \u5b50\u96c6\u548c\nList&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;&gt;(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n</code></pre> subset_sum_i_naive.cpp<pre><code>/* \u56de\u6eaf\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\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (size_t i = 0; i &lt; choices.size(); i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] &gt; target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.push_back(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop_back();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nvector&lt;vector&lt;int&gt;&gt; subsetSumINaive(vector&lt;int&gt; &amp;nums, int target) {\nvector&lt;int&gt; state; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0; // \u5b50\u96c6\u548c\nvector&lt;vector&lt;int&gt;&gt; res; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n</code></pre> subset_sum_i_naive.py<pre><code>def backtrack(\nstate: list[int],\ntarget: int,\ntotal: int,\nchoices: list[int],\nres: list[list[int]],\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n# \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif total == target:\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i in range(len(choices)):\n# \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif total + choices[i] &gt; target:\ncontinue\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.append(choices[i])\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop()\ndef subset_sum_i_naive(nums: list[int], target: int) -&gt; list[list[int]]:\n\"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09\"\"\"\nstate = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\ntotal = 0 # \u5b50\u96c6\u548c\nres = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res)\nreturn res\n</code></pre> subset_sum_i_naive.go<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumINaive(total, target int, state, choices *[]int, res *[][]int) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == total {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i := 0; i &lt; len(*choices); i++ {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif total+(*choices)[i] &gt; target {\ncontinue\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\n*state = append(*state, (*choices)[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrackSubsetSumINaive(total+(*choices)[i], target, state, choices, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n*state = (*state)[:len(*state)-1]\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums []int, target int) [][]int {\nstate := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\ntotal := 0 // \u5b50\u96c6\u548c\nres := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrackSubsetSumINaive(total, target, &amp;state, &amp;nums, &amp;res)\nreturn res\n}\n</code></pre> subset_sum_i_naive.js<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, total, choices, res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total === target) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (let i = 0; i &lt; choices.length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] &gt; target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums, target) {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nconst total = 0; // \u5b50\u96c6\u548c\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n</code></pre> subset_sum_i_naive.ts<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\nstate: number[],\ntarget: number,\ntotal: number,\nchoices: number[],\nres: number[][]\n): void {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total === target) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (let i = 0; i &lt; choices.length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] &gt; target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunction subsetSumINaive(nums: number[], target: number): number[][] {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nconst total = 0; // \u5b50\u96c6\u548c\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n</code></pre> subset_sum_i_naive.c<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector *state, int target, int total, vector *choices, vector *res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nvector *tmpVector = newVector();\nfor (int i = 0; i &lt; state-&gt;size; i++) {\nvectorPushback(tmpVector, state-&gt;data[i], sizeof(int));\n}\nvectorPushback(res, tmpVector, sizeof(vector));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (size_t i = 0; i &lt; choices-&gt;size; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + *(int *)(choices-&gt;data[i]) &gt; target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nvectorPushback(state, choices-&gt;data[i], sizeof(int));\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + *(int *)(choices-&gt;data[i]), choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nvectorPopback(state);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nvector *subsetSumINaive(vector *nums, int target) {\nvector *state = newVector(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0; // \u5b50\u96c6\u548c\nvector *res = newVector(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n</code></pre> subset_sum_i_naive.cs<pre><code>/* \u56de\u6eaf\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\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nres.Add(new List&lt;int&gt;(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i &lt; choices.Length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] &gt; target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.Add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.RemoveAt(state.Count - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList&lt;List&lt;int&gt;&gt; subsetSumINaive(int[] nums, int target) {\nList&lt;int&gt; state = new List&lt;int&gt;(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0; // \u5b50\u96c6\u548c\nList&lt;List&lt;int&gt;&gt; res = new List&lt;List&lt;int&gt;&gt;(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n</code></pre> subset_sum_i_naive.swift<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrack(state: inout [Int], target: Int, total: Int, choices: [Int], res: inout [[Int]]) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif total == target {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i in stride(from: 0, to: choices.count, by: 1) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif total + choices[i] &gt; target {\ncontinue\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.append(choices[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state: &amp;state, target: target, total: total + choices[i], choices: choices, res: &amp;res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast()\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfunc subsetSumINaive(nums: [Int], target: Int) -&gt; [[Int]] {\nvar state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nlet total = 0 // \u5b50\u96c6\u548c\nvar res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state: &amp;state, target: target, total: total, choices: nums, res: &amp;res)\nreturn res\n}\n</code></pre> subset_sum_i_naive.zig<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumINaive}\n</code></pre> subset_sum_i_naive.dart<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\nList&lt;int&gt; state,\nint target,\nint total,\nList&lt;int&gt; choices,\nList&lt;List&lt;int&gt;&gt; res,\n) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (total == target) {\nres.add(List.from(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int i = 0; i &lt; choices.length; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (total + choices[i] &gt; target) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nList&lt;List&lt;int&gt;&gt; subsetSumINaive(List&lt;int&gt; nums, int target) {\nList&lt;int&gt; state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nint total = 0; // \u5143\u7d20\u548c\nList&lt;List&lt;int&gt;&gt; res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, res);\nreturn res;\n}\n</code></pre> subset_sum_i_naive.rs<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(mut state: Vec&lt;i32&gt;, target: i32, total: i32, choices: &amp;[i32], res: &amp;mut Vec&lt;Vec&lt;i32&gt;&gt;) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif total == target {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor i in 0..choices.len() {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif total + choices[i] &gt; target {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u5143\u7d20\u548c total\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state.clone(), target, total + choices[i], choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I\uff08\u5305\u542b\u91cd\u590d\u5b50\u96c6\uff09 */\nfn subset_sum_i_naive(nums: &amp;[i32], target: i32) -&gt; Vec&lt;Vec&lt;i32&gt;&gt; {\nlet state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nlet total = 0; // \u5b50\u96c6\u548c\nlet mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, total, nums, &amp;mut res);\nres\n}\n</code></pre> <p>\u5411\u4ee5\u4e0a\u4ee3\u7801\u8f93\u5165\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a \\([3, 3, 3], [4, 5], [5, 4]\\) \u3002\u867d\u7136\u6210\u529f\u627e\u51fa\u4e86\u6240\u6709\u548c\u4e3a \\(9\\) \u7684\u5b50\u96c6\uff0c\u4f46\u5176\u4e2d\u5b58\u5728\u91cd\u590d\u7684\u5b50\u96c6 \\([4, 5]\\) \u548c \\([5, 4]\\) \u3002</p> <p>\u8fd9\u662f\u56e0\u4e3a\u641c\u7d22\u8fc7\u7a0b\u662f\u533a\u5206\u9009\u62e9\u987a\u5e8f\u7684\uff0c\u7136\u800c\u5b50\u96c6\u4e0d\u533a\u5206\u9009\u62e9\u987a\u5e8f\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5148\u9009 \\(4\\) \u540e\u9009 \\(5\\) \u4e0e\u5148\u9009 \\(5\\) \u540e\u9009 \\(4\\) \u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5206\u652f\uff0c\u4f46\u4e24\u8005\u5bf9\u5e94\u540c\u4e00\u4e2a\u5b50\u96c6\u3002</p> <p></p> <p> \u56fe\uff1a\u5b50\u96c6\u641c\u7d22\u4e0e\u8d8a\u754c\u526a\u679d </p> <p>\u4e3a\u4e86\u53bb\u9664\u91cd\u590d\u5b50\u96c6\uff0c\u4e00\u79cd\u76f4\u63a5\u7684\u601d\u8def\u662f\u5bf9\u7ed3\u679c\u5217\u8868\u8fdb\u884c\u53bb\u91cd\u3002\u4f46\u8fd9\u4e2a\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\uff0c\u56e0\u4e3a\uff1a</p> <ul> <li>\u5f53\u6570\u7ec4\u5143\u7d20\u8f83\u591a\uff0c\u5c24\u5176\u662f\u5f53 <code>target</code> \u8f83\u5927\u65f6\uff0c\u641c\u7d22\u8fc7\u7a0b\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u91cd\u590d\u5b50\u96c6\u3002</li> <li>\u6bd4\u8f83\u5b50\u96c6\uff08\u6570\u7ec4\uff09\u7684\u5f02\u540c\u975e\u5e38\u8017\u65f6\uff0c\u9700\u8981\u5148\u6392\u5e8f\u6570\u7ec4\uff0c\u518d\u6bd4\u8f83\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5f02\u540c\u3002</li> </ul>"},{"location":"chapter_backtracking/subset_sum_problem/#_2","title":"\u91cd\u590d\u5b50\u96c6\u526a\u679d","text":"<p>\u6211\u4eec\u8003\u8651\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u901a\u8fc7\u526a\u679d\u8fdb\u884c\u53bb\u91cd\u3002\u89c2\u5bdf\u4e0b\u56fe\uff0c\u91cd\u590d\u5b50\u96c6\u662f\u5728\u4ee5\u4e0d\u540c\u987a\u5e8f\u9009\u62e9\u6570\u7ec4\u5143\u7d20\u65f6\u4ea7\u751f\u7684\uff0c\u5177\u4f53\u6765\u770b\uff1a</p> <ol> <li>\u7b2c\u4e00\u8f6e\u548c\u7b2c\u4e8c\u8f6e\u5206\u522b\u9009\u62e9 \\(3\\) , \\(4\\) \uff0c\u4f1a\u751f\u6210\u5305\u542b\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u6240\u6709\u5b50\u96c6\uff0c\u8bb0\u4e3a \\([3, 4, \\cdots]\\) \u3002</li> <li>\u82e5\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(4\\) \uff0c\u5219\u7b2c\u4e8c\u8f6e\u5e94\u8be5\u8df3\u8fc7 \\(3\\) \uff0c\u56e0\u4e3a\u8be5\u9009\u62e9\u4ea7\u751f\u7684\u5b50\u96c6 \\([4, 3, \\cdots]\\) \u548c <code>1.</code> \u4e2d\u751f\u6210\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002</li> </ol> <p>\u5206\u652f\u8d8a\u9760\u53f3\uff0c\u9700\u8981\u6392\u9664\u7684\u5206\u652f\u4e5f\u8d8a\u591a\uff0c\u4f8b\u5982\uff1a</p> <ol> <li>\u524d\u4e24\u8f6e\u9009\u62e9 \\(3\\) , \\(5\\) \uff0c\u751f\u6210\u5b50\u96c6 \\([3, 5, \\cdots]\\) \u3002</li> <li>\u524d\u4e24\u8f6e\u9009\u62e9 \\(4\\) , \\(5\\) \uff0c\u751f\u6210\u5b50\u96c6 \\([4, 5, \\cdots]\\) \u3002</li> <li>\u82e5\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(5\\) \uff0c\u5219\u7b2c\u4e8c\u8f6e\u5e94\u8be5\u8df3\u8fc7 \\(3\\) \u548c \\(4\\) \uff0c\u56e0\u4e3a\u5b50\u96c6 \\([5, 3, \\cdots]\\) \u548c\u5b50\u96c6 \\([5, 4, \\cdots]\\) \u548c <code>1.</code> , <code>2.</code> \u4e2d\u751f\u6210\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u4e0d\u540c\u9009\u62e9\u987a\u5e8f\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6 </p> <p>\u603b\u7ed3\u6765\u770b\uff0c\u7ed9\u5b9a\u8f93\u5165\u6570\u7ec4 \\([x_1, x_2, \\cdots, x_n]\\) \uff0c\u8bbe\u641c\u7d22\u8fc7\u7a0b\u4e2d\u7684\u9009\u62e9\u5e8f\u5217\u4e3a \\([x_{i_1}, x_{i_2}, \\cdots , x_{i_m}]\\) \uff0c\u5219\u8be5\u9009\u62e9\u5e8f\u5217\u9700\u8981\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\cdots \\leq i_m\\) \uff0c\u4e0d\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u9009\u62e9\u5e8f\u5217\u90fd\u4f1a\u9020\u6210\u91cd\u590d\uff0c\u5e94\u5f53\u526a\u679d\u3002</p>"},{"location":"chapter_backtracking/subset_sum_problem/#_3","title":"\u4ee3\u7801\u5b9e\u73b0","text":"<p>\u4e3a\u5b9e\u73b0\u8be5\u526a\u679d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u53d8\u91cf <code>start</code> \uff0c\u7528\u4e8e\u6307\u793a\u904d\u5386\u8d77\u70b9\u3002\u5f53\u505a\u51fa\u9009\u62e9 \\(x_{i}\\) \u540e\uff0c\u8bbe\u5b9a\u4e0b\u4e00\u8f6e\u4ece\u7d22\u5f15 \\(i\\) \u5f00\u59cb\u904d\u5386\u3002\u8fd9\u6837\u505a\u5c31\u53ef\u4ee5\u8ba9\u9009\u62e9\u5e8f\u5217\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\cdots \\leq i_m\\) \uff0c\u4ece\u800c\u4fdd\u8bc1\u5b50\u96c6\u552f\u4e00\u3002</p> <p>\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u5bf9\u4ee3\u7801\u8fdb\u884c\u4e86\u4e24\u9879\u4f18\u5316\uff1a</p> <ul> <li>\u5728\u5f00\u542f\u641c\u7d22\u524d\uff0c\u5148\u5c06\u6570\u7ec4 <code>nums</code> \u6392\u5e8f\u3002\u5728\u904d\u5386\u6240\u6709\u9009\u62e9\u65f6\uff0c\u5f53\u5b50\u96c6\u548c\u8d85\u8fc7 <code>target</code> \u65f6\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\uff0c\u56e0\u4e3a\u540e\u8fb9\u7684\u5143\u7d20\u66f4\u5927\uff0c\u5176\u5b50\u96c6\u548c\u90fd\u4e00\u5b9a\u4f1a\u8d85\u8fc7 <code>target</code> \u3002</li> <li>\u7701\u53bb\u5143\u7d20\u548c\u53d8\u91cf <code>total</code>\uff0c\u901a\u8fc7\u5728 <code>target</code> \u4e0a\u6267\u884c\u51cf\u6cd5\u6765\u7edf\u8ba1\u5143\u7d20\u548c\uff0c\u5f53 <code>target</code> \u7b49\u4e8e \\(0\\) \u65f6\u8bb0\u5f55\u89e3\u3002</li> </ul> JavaC++PythonGoJSTSCC#SwiftZigDartRust subset_sum_i.java<pre><code>/* \u56de\u6eaf\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\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.add(new ArrayList&lt;&gt;(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i &lt; choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] &lt; 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.remove(state.size() - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList&lt;List&lt;Integer&gt;&gt; subsetSumI(int[] nums, int target) {\nList&lt;Integer&gt; state = new ArrayList&lt;&gt;(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nArrays.sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;&gt;(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n</code></pre> subset_sum_i.cpp<pre><code>/* \u56de\u6eaf\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\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i &lt; choices.size(); i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] &lt; 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push_back(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop_back();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvector&lt;vector&lt;int&gt;&gt; subsetSumI(vector&lt;int&gt; &amp;nums, int target) {\nvector&lt;int&gt; state; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nsort(nums.begin(), nums.end()); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nvector&lt;vector&lt;int&gt;&gt; res; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n</code></pre> subset_sum_i.py<pre><code>def backtrack(\nstate: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I\"\"\"\n# \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0:\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\n# \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor i in range(start, len(choices)):\n# \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n# \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] &lt; 0:\nbreak\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.append(choices[i])\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop()\ndef subset_sum_i(nums: list[int], target: int) -&gt; list[list[int]]:\n\"\"\"\u6c42\u89e3\u5b50\u96c6\u548c I\"\"\"\nstate = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort() # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nstart = 0 # \u904d\u5386\u8d77\u59cb\u70b9\nres = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res)\nreturn res\n</code></pre> subset_sum_i.go<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrackSubsetSumI(start, target int, state, choices *[]int, res *[][]int) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor i := start; i &lt; len(*choices); i++ {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target-(*choices)[i] &lt; 0 {\nbreak\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n*state = append(*state, (*choices)[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrackSubsetSumI(i, target-(*choices)[i], state, choices, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n*state = (*state)[:len(*state)-1]\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums []int, target int) [][]int {\nstate := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nsort.Ints(nums) // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nstart := 0 // \u904d\u5386\u8d77\u59cb\u70b9\nres := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrackSubsetSumI(start, target, &amp;state, &amp;nums, &amp;res)\nreturn res\n}\n</code></pre> subset_sum_i.js<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(state, target, choices, start, res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target === 0) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (let i = start; i &lt; choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] &lt; 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums, target) {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nconst start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n</code></pre> subset_sum_i.ts<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunction backtrack(\nstate: number[],\ntarget: number,\nchoices: number[],\nstart: number,\nres: number[][]\n): void {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target === 0) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (let i = start; i &lt; choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] &lt; 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunction subsetSumI(nums: number[], target: number): number[][] {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nconst start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n</code></pre> subset_sum_i.c<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(vector *state, int target, vector *choices, int start, vector *res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nvector *tmpVector = newVector();\nfor (int i = 0; i &lt; state-&gt;size; i++) {\nvectorPushback(tmpVector, state-&gt;data[i], sizeof(int));\n}\nvectorPushback(res, tmpVector, sizeof(vector));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i &lt; choices-&gt;size; i++) {\n// \u526a\u679d\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u8df3\u8fc7\u8be5\u9009\u62e9\nif (target - *(int *)(choices-&gt;data[i]) &lt; 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nvectorPushback(state, choices-&gt;data[i], sizeof(int));\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - *(int *)(choices-&gt;data[i]), choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nvectorPopback(state);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nvector *subsetSumI(vector *nums, int target) {\nvector *state = newVector(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nqsort(nums-&gt;data, nums-&gt;size, sizeof(int *), comp); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u5b50\u96c6\u548c\nvector *res = newVector(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n</code></pre> subset_sum_i.cs<pre><code>/* \u56de\u6eaf\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\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.Add(new List&lt;int&gt;(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i &lt; choices.Length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] &lt; 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.Add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.RemoveAt(state.Count - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList&lt;List&lt;int&gt;&gt; subsetSumI(int[] nums, int target) {\nList&lt;int&gt; state = new List&lt;int&gt;(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nArray.Sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList&lt;List&lt;int&gt;&gt; res = new List&lt;List&lt;int&gt;&gt;(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n</code></pre> subset_sum_i.swift<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor i in stride(from: start, to: choices.count, by: 1) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] &lt; 0 {\nbreak\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.append(choices[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state: &amp;state, target: target - choices[i], choices: choices, start: i, res: &amp;res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast()\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfunc subsetSumI(nums: [Int], target: Int) -&gt; [[Int]] {\nvar state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nlet nums = nums.sorted() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nlet start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\nvar res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state: &amp;state, target: target, choices: nums, start: start, res: &amp;res)\nreturn res\n}\n</code></pre> subset_sum_i.zig<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumI}\n</code></pre> subset_sum_i.dart<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nvoid backtrack(\nList&lt;int&gt; state,\nint target,\nList&lt;int&gt; choices,\nint start,\nList&lt;List&lt;int&gt;&gt; res,\n) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.add(List.from(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor (int i = start; i &lt; choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] &lt; 0) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nList&lt;List&lt;int&gt;&gt; subsetSumI(List&lt;int&gt; nums, int target) {\nList&lt;int&gt; state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList&lt;List&lt;int&gt;&gt; res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n</code></pre> subset_sum_i.rs<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c I */\nfn backtrack(mut state: Vec&lt;i32&gt;, target: i32, choices: &amp;[i32], start: usize, res: &amp;mut Vec&lt;Vec&lt;i32&gt;&gt;) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\nfor i in start..choices.len() {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] &lt; 0 {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state.clone(), target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c I */\nfn subset_sum_i(nums: &amp;mut [i32], target: i32) -&gt; Vec&lt;Vec&lt;i32&gt;&gt; {\nlet state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nlet start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nlet mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, &amp;mut res);\nres\n}\n</code></pre> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4e3a\u5c06\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u8f93\u5165\u5230\u4ee5\u4e0a\u4ee3\u7801\u540e\u7684\u6574\u4f53\u56de\u6eaf\u8fc7\u7a0b\u3002</p> <p></p> <p> \u56fe\uff1a\u5b50\u96c6\u548c I \u56de\u6eaf\u8fc7\u7a0b </p>"},{"location":"chapter_backtracking/subset_sum_problem/#1332","title":"13.3.2. \u00a0 \u8003\u8651\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"<p>Question</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 <code>nums</code> \u548c\u4e00\u4e2a\u76ee\u6807\u6b63\u6574\u6570 <code>target</code> \uff0c\u8bf7\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u4f7f\u5f97\u7ec4\u5408\u4e2d\u7684\u5143\u7d20\u548c\u7b49\u4e8e <code>target</code> \u3002\u7ed9\u5b9a\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u53ea\u53ef\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u8bf7\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u8fd9\u4e9b\u7ec4\u5408\uff0c\u5217\u8868\u4e2d\u4e0d\u5e94\u5305\u542b\u91cd\u590d\u7ec4\u5408\u3002</p> <p>\u76f8\u6bd4\u4e8e\u4e0a\u9898\uff0c\u672c\u9898\u7684\u8f93\u5165\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u8fd9\u5f15\u5165\u4e86\u65b0\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u6570\u7ec4 \\([4, \\hat{4}, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \uff0c\u5219\u73b0\u6709\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a \\([4, 5], [\\hat{4}, 5]\\) \uff0c\u51fa\u73b0\u4e86\u91cd\u590d\u5b50\u96c6\u3002</p> <p>\u9020\u6210\u8fd9\u79cd\u91cd\u590d\u7684\u539f\u56e0\u662f\u76f8\u7b49\u5143\u7d20\u5728\u67d0\u8f6e\u4e2d\u88ab\u591a\u6b21\u9009\u62e9\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u7b2c\u4e00\u8f6e\u5171\u6709\u4e09\u4e2a\u9009\u62e9\uff0c\u5176\u4e2d\u4e24\u4e2a\u90fd\u4e3a \\(4\\) \uff0c\u4f1a\u4ea7\u751f\u4e24\u4e2a\u91cd\u590d\u7684\u641c\u7d22\u5206\u652f\uff0c\u4ece\u800c\u8f93\u51fa\u91cd\u590d\u5b50\u96c6\uff1b\u540c\u7406\uff0c\u7b2c\u4e8c\u8f6e\u7684\u4e24\u4e2a \\(4\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5b50\u96c6\u3002</p> <p></p> <p> \u56fe\uff1a\u76f8\u7b49\u5143\u7d20\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6 </p>"},{"location":"chapter_backtracking/subset_sum_problem/#_4","title":"\u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"<p>\u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u9650\u5236\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u4e00\u8f6e\u4e2d\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u5b9e\u73b0\u65b9\u5f0f\u6bd4\u8f83\u5de7\u5999\uff1a\u7531\u4e8e\u6570\u7ec4\u662f\u5df2\u6392\u5e8f\u7684\uff0c\u56e0\u6b64\u76f8\u7b49\u5143\u7d20\u90fd\u662f\u76f8\u90bb\u7684\u3002\u8fd9\u610f\u5473\u7740\u5728\u67d0\u8f6e\u9009\u62e9\u4e2d\uff0c\u82e5\u5f53\u524d\u5143\u7d20\u4e0e\u5176\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u5219\u8bf4\u660e\u5b83\u5df2\u7ecf\u88ab\u9009\u62e9\u8fc7\uff0c\u56e0\u6b64\u76f4\u63a5\u8df3\u8fc7\u5f53\u524d\u5143\u7d20\u3002</p> <p>\u4e0e\u6b64\u540c\u65f6\uff0c\u672c\u9898\u89c4\u5b9a\u4e2d\u7684\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528\u53d8\u91cf <code>start</code> \u6765\u6ee1\u8db3\u8be5\u7ea6\u675f\uff1a\u5f53\u505a\u51fa\u9009\u62e9 \\(x_{i}\\) \u540e\uff0c\u8bbe\u5b9a\u4e0b\u4e00\u8f6e\u4ece\u7d22\u5f15 \\(i + 1\\) \u5f00\u59cb\u5411\u540e\u904d\u5386\u3002\u8fd9\u6837\u5373\u80fd\u53bb\u9664\u91cd\u590d\u5b50\u96c6\uff0c\u4e5f\u80fd\u907f\u514d\u91cd\u590d\u9009\u62e9\u5143\u7d20\u3002</p>"},{"location":"chapter_backtracking/subset_sum_problem/#_5","title":"\u4ee3\u7801\u5b9e\u73b0","text":"JavaC++PythonGoJSTSCC#SwiftZigDartRust subset_sum_ii.java<pre><code>/* \u56de\u6eaf\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\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.add(new ArrayList&lt;&gt;(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i &lt; choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] &lt; 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i &gt; start &amp;&amp; choices[i] == choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.remove(state.size() - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList&lt;List&lt;Integer&gt;&gt; subsetSumII(int[] nums, int target) {\nList&lt;Integer&gt; state = new ArrayList&lt;&gt;(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nArrays.sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;&gt;(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n</code></pre> subset_sum_ii.cpp<pre><code>/* \u56de\u6eaf\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\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.push_back(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i &lt; choices.size(); i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] &lt; 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i &gt; start &amp;&amp; choices[i] == choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push_back(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop_back();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvector&lt;vector&lt;int&gt;&gt; subsetSumII(vector&lt;int&gt; &amp;nums, int target) {\nvector&lt;int&gt; state; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nsort(nums.begin(), nums.end()); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nvector&lt;vector&lt;int&gt;&gt; res; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n</code></pre> subset_sum_ii.py<pre><code>def backtrack(\nstate: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n\"\"\"\u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II\"\"\"\n# \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0:\nres.append(list(state))\nreturn\n# \u904d\u5386\u6240\u6709\u9009\u62e9\n# \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n# \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor i in range(start, len(choices)):\n# \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n# \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] &lt; 0:\nbreak\n# \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif i &gt; start and choices[i] == choices[i - 1]:\ncontinue\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.append(choices[i])\n# \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res)\n# \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop()\ndef subset_sum_ii(nums: list[int], target: int) -&gt; list[list[int]]:\n\"\"\"\u6c42\u89e3\u5b50\u96c6\u548c II\"\"\"\nstate = [] # \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort() # \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nstart = 0 # \u904d\u5386\u8d77\u59cb\u70b9\nres = [] # \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res)\nreturn res\n</code></pre> subset_sum_ii.go<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrackSubsetSumII(start, target int, state, choices *[]int, res *[][]int) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nnewState := append([]int{}, *state...)\n*res = append(*res, newState)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor i := start; i &lt; len(*choices); i++ {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target-(*choices)[i] &lt; 0 {\nbreak\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif i &gt; start &amp;&amp; (*choices)[i] == (*choices)[i-1] {\ncontinue\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\n*state = append(*state, (*choices)[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrackSubsetSumII(i+1, target-(*choices)[i], state, choices, res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\n*state = (*state)[:len(*state)-1]\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums []int, target int) [][]int {\nstate := make([]int, 0) // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nsort.Ints(nums) // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nstart := 0 // \u904d\u5386\u8d77\u59cb\u70b9\nres := make([][]int, 0) // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrackSubsetSumII(start, target, &amp;state, &amp;nums, &amp;res)\nreturn res\n}\n</code></pre> subset_sum_ii.js<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(state, target, choices, start, res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target === 0) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (let i = start; i &lt; choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] &lt; 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i &gt; start &amp;&amp; choices[i] === choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums, target) {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nconst start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n</code></pre> subset_sum_ii.ts<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunction backtrack(\nstate: number[],\ntarget: number,\nchoices: number[],\nstart: number,\nres: number[][]\n): void {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target === 0) {\nres.push([...state]);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (let i = start; i &lt; choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] &lt; 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i &gt; start &amp;&amp; choices[i] === choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunction subsetSumII(nums: number[], target: number): number[][] {\nconst state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nconst start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nconst res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n</code></pre> subset_sum_ii.c<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(vector *state, int target, vector *choices, int start, vector *res) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nvector *tmpVector = newVector();\nfor (int i = 0; i &lt; state-&gt;size; i++) {\nvectorPushback(tmpVector, state-&gt;data[i], sizeof(int));\n}\nvectorPushback(res, tmpVector, sizeof(vector));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i &lt; choices-&gt;size; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - *(int *)(choices-&gt;data[i]) &lt; 0) {\ncontinue;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i &gt; start &amp;&amp; *(int *)(choices-&gt;data[i]) == *(int *)(choices-&gt;data[i - 1])) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nvectorPushback(state, choices-&gt;data[i], sizeof(int));\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - *(int *)(choices-&gt;data[i]), choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nvectorPopback(state);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nvector *subsetSumII(vector *nums, int target) {\nvector *state = newVector(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nqsort(nums-&gt;data, nums-&gt;size, sizeof(int *), comp); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u5b50\u96c6\u548c\nvector *res = newVector(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n</code></pre> subset_sum_ii.cs<pre><code>/* \u56de\u6eaf\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\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.Add(new List&lt;int&gt;(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i &lt; choices.Length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] &lt; 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i &gt; start &amp;&amp; choices[i] == choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.Add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.RemoveAt(state.Count - 1);\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList&lt;List&lt;int&gt;&gt; subsetSumII(int[] nums, int target) {\nList&lt;int&gt; state = new List&lt;int&gt;(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nArray.Sort(nums); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList&lt;List&lt;int&gt;&gt; res = new List&lt;List&lt;int&gt;&gt;(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n</code></pre> subset_sum_ii.swift<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nres.append(state)\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor i in stride(from: start, to: choices.count, by: 1) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] &lt; 0 {\nbreak\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif i &gt; start, choices[i] == choices[i - 1] {\ncontinue\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.append(choices[i])\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state: &amp;state, target: target - choices[i], choices: choices, start: i + 1, res: &amp;res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast()\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfunc subsetSumII(nums: [Int], target: Int) -&gt; [[Int]] {\nvar state: [Int] = [] // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nlet nums = nums.sorted() // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nlet start = 0 // \u904d\u5386\u8d77\u59cb\u70b9\nvar res: [[Int]] = [] // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state: &amp;state, target: target, choices: nums, start: start, res: &amp;res)\nreturn res\n}\n</code></pre> subset_sum_ii.zig<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumII}\n</code></pre> subset_sum_ii.dart<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nvoid backtrack(\nList&lt;int&gt; state,\nint target,\nList&lt;int&gt; choices,\nint start,\nList&lt;List&lt;int&gt;&gt; res,\n) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif (target == 0) {\nres.add(List.from(state));\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor (int i = start; i &lt; choices.length; i++) {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif (target - choices[i] &lt; 0) {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif (i &gt; start &amp;&amp; choices[i] == choices[i - 1]) {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.add(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state, target - choices[i], choices, i + 1, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.removeLast();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nList&lt;List&lt;int&gt;&gt; subsetSumII(List&lt;int&gt; nums, int target) {\nList&lt;int&gt; state = []; // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nint start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nList&lt;List&lt;int&gt;&gt; res = []; // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, res);\nreturn res;\n}\n</code></pre> subset_sum_ii.rs<pre><code>/* \u56de\u6eaf\u7b97\u6cd5\uff1a\u5b50\u96c6\u548c II */\nfn backtrack(mut state: Vec&lt;i32&gt;, target: i32, choices: &amp;[i32], start: usize, res: &amp;mut Vec&lt;Vec&lt;i32&gt;&gt;) {\n// \u5b50\u96c6\u548c\u7b49\u4e8e target \u65f6\uff0c\u8bb0\u5f55\u89e3\nif target == 0 {\nres.push(state);\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\n// \u526a\u679d\u4e8c\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u751f\u6210\u91cd\u590d\u5b50\u96c6\n// \u526a\u679d\u4e09\uff1a\u4ece start \u5f00\u59cb\u904d\u5386\uff0c\u907f\u514d\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\nfor i in start..choices.len() {\n// \u526a\u679d\u4e00\uff1a\u82e5\u5b50\u96c6\u548c\u8d85\u8fc7 target \uff0c\u5219\u76f4\u63a5\u7ed3\u675f\u5faa\u73af\n// \u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u540e\u8fb9\u5143\u7d20\u66f4\u5927\uff0c\u5b50\u96c6\u548c\u4e00\u5b9a\u8d85\u8fc7 target\nif target - choices[i] &lt; 0 {\nbreak;\n}\n// \u526a\u679d\u56db\uff1a\u5982\u679c\u8be5\u5143\u7d20\u4e0e\u5de6\u8fb9\u5143\u7d20\u76f8\u7b49\uff0c\u8bf4\u660e\u8be5\u641c\u7d22\u5206\u652f\u91cd\u590d\uff0c\u76f4\u63a5\u8df3\u8fc7\nif i &gt; start &amp;&amp; choices[i] == choices[i - 1] {\ncontinue;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0 target, start\nstate.push(choices[i]);\n// \u8fdb\u884c\u4e0b\u4e00\u8f6e\u9009\u62e9\nbacktrack(state.clone(), target - choices[i], choices, i, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nstate.pop();\n}\n}\n/* \u6c42\u89e3\u5b50\u96c6\u548c II */\nfn subset_sum_ii(nums: &amp;mut [i32], target: i32) -&gt; Vec&lt;Vec&lt;i32&gt;&gt; {\nlet state = Vec::new(); // \u72b6\u6001\uff08\u5b50\u96c6\uff09\nnums.sort(); // \u5bf9 nums \u8fdb\u884c\u6392\u5e8f\nlet start = 0; // \u904d\u5386\u8d77\u59cb\u70b9\nlet mut res = Vec::new(); // \u7ed3\u679c\u5217\u8868\uff08\u5b50\u96c6\u5217\u8868\uff09\nbacktrack(state, target, nums, start, &amp;mut res);\nres\n}\n</code></pre> <p>\u4e0b\u56fe\u5c55\u793a\u4e86\u6570\u7ec4 \\([4, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u7684\u56de\u6eaf\u8fc7\u7a0b\uff0c\u5171\u5305\u542b\u56db\u79cd\u526a\u679d\u64cd\u4f5c\u3002\u8bf7\u4f60\u5c06\u56fe\u793a\u4e0e\u4ee3\u7801\u6ce8\u91ca\u76f8\u7ed3\u5408\uff0c\u7406\u89e3\u6574\u4e2a\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ee5\u53ca\u6bcf\u79cd\u526a\u679d\u64cd\u4f5c\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002</p> <p></p> <p> \u56fe\uff1a\u5b50\u96c6\u548c II \u56de\u6eaf\u8fc7\u7a0b </p>"},{"location":"chapter_backtracking/summary/","title":"13.5. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u56de\u6eaf\u7b97\u6cd5\u672c\u8d28\u662f\u7a77\u4e3e\u6cd5\uff0c\u901a\u8fc7\u5bf9\u89e3\u7a7a\u95f4\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u6765\u5bfb\u627e\u7b26\u5408\u6761\u4ef6\u7684\u89e3\u3002\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u9047\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u89e3\u5219\u8bb0\u5f55\uff0c\u76f4\u81f3\u627e\u5230\u6240\u6709\u89e3\u6216\u904d\u5386\u5b8c\u6210\u540e\u7ed3\u675f\u3002</li> <li>\u56de\u6eaf\u7b97\u6cd5\u7684\u641c\u7d22\u8fc7\u7a0b\u5305\u62ec\u5c1d\u8bd5\u4e0e\u56de\u9000\u4e24\u4e2a\u90e8\u5206\u3002\u5b83\u901a\u8fc7\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u6765\u5c1d\u8bd5\u5404\u79cd\u9009\u62e9\uff0c\u5f53\u9047\u5230\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u60c5\u51b5\u65f6\uff0c\u5219\u64a4\u9500\u4e0a\u4e00\u6b65\u7684\u9009\u62e9\uff0c\u9000\u56de\u5230\u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5e76\u7ee7\u7eed\u5c1d\u8bd5\u5176\u4ed6\u9009\u62e9\u3002\u5c1d\u8bd5\u4e0e\u56de\u9000\u662f\u4e24\u4e2a\u65b9\u5411\u76f8\u53cd\u7684\u64cd\u4f5c\u3002</li> <li>\u56de\u6eaf\u95ee\u9898\u901a\u5e38\u5305\u542b\u591a\u4e2a\u7ea6\u675f\u6761\u4ef6\uff0c\u5b83\u4eec\u53ef\u7528\u4e8e\u5b9e\u73b0\u526a\u679d\u64cd\u4f5c\u3002\u526a\u679d\u53ef\u4ee5\u63d0\u524d\u7ed3\u675f\u4e0d\u5fc5\u8981\u7684\u641c\u7d22\u5206\u652f\uff0c\u5927\u5e45\u63d0\u5347\u641c\u7d22\u6548\u7387\u3002</li> <li>\u56de\u6eaf\u7b97\u6cd5\u4e3b\u8981\u53ef\u7528\u4e8e\u89e3\u51b3\u641c\u7d22\u95ee\u9898\u548c\u7ea6\u675f\u6ee1\u8db3\u95ee\u9898\u3002\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u867d\u7136\u53ef\u4ee5\u7528\u56de\u6eaf\u7b97\u6cd5\u89e3\u51b3\uff0c\u4f46\u5f80\u5f80\u5b58\u5728\u66f4\u9ad8\u6548\u7387\u6216\u66f4\u597d\u6548\u679c\u7684\u89e3\u6cd5\u3002</li> <li>\u5168\u6392\u5217\u95ee\u9898\u65e8\u5728\u641c\u7d22\u7ed9\u5b9a\u96c6\u5408\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u6570\u7ec4\u6765\u8bb0\u5f55\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u88ab\u9009\u62e9\uff0c\u526a\u679d\u6389\u91cd\u590d\u9009\u62e9\u540c\u4e00\u5143\u7d20\u7684\u641c\u7d22\u5206\u652f\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5143\u7d20\u53ea\u88ab\u9009\u62e9\u4e00\u6b21\u3002</li> <li>\u5728\u5168\u6392\u5217\u95ee\u9898\u4e2d\uff0c\u5982\u679c\u96c6\u5408\u4e2d\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff0c\u5219\u6700\u7ec8\u7ed3\u679c\u4f1a\u51fa\u73b0\u91cd\u590d\u6392\u5217\u3002\u6211\u4eec\u9700\u8981\u7ea6\u675f\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u8f6e\u4e2d\u53ea\u80fd\u88ab\u9009\u62e9\u4e00\u6b21\uff0c\u8fd9\u901a\u5e38\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u3002</li> <li>\u5b50\u96c6\u548c\u95ee\u9898\u7684\u76ee\u6807\u662f\u5728\u7ed9\u5b9a\u96c6\u5408\u4e2d\u627e\u5230\u548c\u4e3a\u76ee\u6807\u503c\u7684\u6240\u6709\u5b50\u96c6\u3002\u96c6\u5408\u4e0d\u533a\u5206\u5143\u7d20\u987a\u5e8f\uff0c\u800c\u641c\u7d22\u8fc7\u7a0b\u4f1a\u8f93\u51fa\u6240\u6709\u987a\u5e8f\u7684\u7ed3\u679c\uff0c\u4ea7\u751f\u91cd\u590d\u5b50\u96c6\u3002\u6211\u4eec\u5728\u56de\u6eaf\u524d\u5c06\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8bbe\u7f6e\u4e00\u4e2a\u53d8\u91cf\u6765\u6307\u793a\u6bcf\u4e00\u8f6e\u7684\u904d\u5386\u8d77\u70b9\uff0c\u4ece\u800c\u5c06\u751f\u6210\u91cd\u590d\u5b50\u96c6\u7684\u641c\u7d22\u5206\u652f\u8fdb\u884c\u526a\u679d\u3002</li> <li>\u5bf9\u4e8e\u5b50\u96c6\u548c\u95ee\u9898\uff0c\u6570\u7ec4\u4e2d\u7684\u76f8\u7b49\u5143\u7d20\u4f1a\u4ea7\u751f\u91cd\u590d\u96c6\u5408\u3002\u6211\u4eec\u5229\u7528\u6570\u7ec4\u5df2\u6392\u5e8f\u7684\u524d\u7f6e\u6761\u4ef6\uff0c\u901a\u8fc7\u5224\u65ad\u76f8\u90bb\u5143\u7d20\u662f\u5426\u76f8\u7b49\u5b9e\u73b0\u526a\u679d\uff0c\u4ece\u800c\u786e\u4fdd\u76f8\u7b49\u5143\u7d20\u5728\u6bcf\u8f6e\u4e2d\u53ea\u80fd\u88ab\u9009\u4e2d\u4e00\u6b21\u3002</li> <li>\\(n\\) \u7687\u540e\u65e8\u5728\u5bfb\u627e\u5c06 \\(n\\) \u4e2a\u7687\u540e\u653e\u7f6e\u5230 \\(n \\times n\\) \u5c3a\u5bf8\u68cb\u76d8\u4e0a\u7684\u65b9\u6848\uff0c\u8981\u6c42\u6240\u6709\u7687\u540e\u4e24\u4e24\u4e4b\u95f4\u65e0\u6cd5\u653b\u51fb\u5bf9\u65b9\u3002\u8be5\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6\u6709\u884c\u7ea6\u675f\u3001\u5217\u7ea6\u675f\u3001\u4e3b\u5bf9\u89d2\u7ebf\u548c\u526f\u5bf9\u89d2\u7ebf\u7ea6\u675f\u3002\u4e3a\u6ee1\u8db3\u884c\u7ea6\u675f\uff0c\u6211\u4eec\u91c7\u7528\u6309\u884c\u653e\u7f6e\u7684\u7b56\u7565\uff0c\u4fdd\u8bc1\u6bcf\u4e00\u884c\u653e\u7f6e\u4e00\u4e2a\u7687\u540e\u3002</li> <li>\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u5904\u7406\u65b9\u5f0f\u7c7b\u4f3c\u3002\u5bf9\u4e8e\u5217\u7ea6\u675f\uff0c\u6211\u4eec\u5229\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u8bb0\u5f55\u6bcf\u4e00\u5217\u662f\u5426\u6709\u7687\u540e\uff0c\u4ece\u800c\u6307\u793a\u9009\u4e2d\u7684\u683c\u5b50\u662f\u5426\u5408\u6cd5\u3002\u5bf9\u4e8e\u5bf9\u89d2\u7ebf\u7ea6\u675f\uff0c\u6211\u4eec\u501f\u52a9\u4e24\u4e2a\u6570\u7ec4\u6765\u5206\u522b\u8bb0\u5f55\u8be5\u4e3b\u3001\u526f\u5bf9\u89d2\u7ebf\u662f\u5426\u5b58\u5728\u7687\u540e\uff1b\u96be\u70b9\u5728\u4e8e\u627e\u5904\u5728\u5230\u540c\u4e00\u4e3b\uff08\u526f\uff09\u5bf9\u89d2\u7ebf\u4e0a\u683c\u5b50\u6ee1\u8db3\u7684\u884c\u5217\u7d22\u5f15\u89c4\u5f8b\u3002</li> </ul>"},{"location":"chapter_computational_complexity/","title":"2. \u00a0 \u590d\u6742\u5ea6","text":"<p>Abstract</p> <p>\u590d\u6742\u5ea6\u72b9\u5982\u6d69\u701a\u7684\u7b97\u6cd5\u5b87\u5b99\u4e2d\u7684\u6307\u5357\u9488\u3002</p> <p>\u5b83\u5f15\u5bfc\u6211\u4eec\u5728\u65f6\u95f4\u4e0e\u7a7a\u95f4\u7684\u7ef4\u5ea6\u4e0a\u6df1\u5165\u63a2\u7d22\uff0c\u5bfb\u627e\u66f4\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002</p>"},{"location":"chapter_computational_complexity/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>2.1 \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30</li> <li>2.2 \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6</li> <li>2.3 \u00a0 \u7a7a\u95f4\u590d\u6742\u5ea6</li> <li>2.4 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1. \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30","text":"<p>\u5728\u7b97\u6cd5\u8bbe\u8ba1\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u8ffd\u6c42\u4ee5\u4e0b\u4e24\u4e2a\u5c42\u9762\u7684\u76ee\u6807\uff1a</p> <ol> <li>\u627e\u5230\u95ee\u9898\u89e3\u6cd5\uff1a\u7b97\u6cd5\u9700\u8981\u5728\u89c4\u5b9a\u7684\u8f93\u5165\u8303\u56f4\u5185\uff0c\u53ef\u9760\u5730\u6c42\u5f97\u95ee\u9898\u7684\u6b63\u786e\u89e3\u3002</li> <li>\u5bfb\u6c42\u6700\u4f18\u89e3\u6cd5\uff1a\u540c\u4e00\u4e2a\u95ee\u9898\u53ef\u80fd\u5b58\u5728\u591a\u79cd\u89e3\u6cd5\uff0c\u6211\u4eec\u5e0c\u671b\u627e\u5230\u5c3d\u53ef\u80fd\u9ad8\u6548\u7684\u7b97\u6cd5\u3002</li> </ol> <p>\u56e0\u6b64\u5728\u80fd\u591f\u89e3\u51b3\u95ee\u9898\u7684\u524d\u63d0\u4e0b\uff0c\u7b97\u6cd5\u6548\u7387\u6210\u4e3a\u4e3b\u8981\u7684\u8bc4\u4ef7\u7ef4\u5ea6\uff0c\u5305\u62ec\uff1a</p> <ul> <li>\u65f6\u95f4\u6548\u7387\uff1a\u7b97\u6cd5\u8fd0\u884c\u901f\u5ea6\u7684\u5feb\u6162\u3002</li> <li>\u7a7a\u95f4\u6548\u7387\uff1a\u7b97\u6cd5\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u7684\u5927\u5c0f\u3002</li> </ul> <p>\u7b80\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u8bbe\u8ba1\u201c\u65e2\u5feb\u53c8\u7701\u201d\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u3002\u800c\u6709\u6548\u5730\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u53ea\u6709\u8fd9\u6837\u6211\u4eec\u624d\u80fd\u5c06\u5404\u79cd\u7b97\u6cd5\u8fdb\u884c\u5bf9\u6bd4\uff0c\u4ece\u800c\u6307\u5bfc\u7b97\u6cd5\u8bbe\u8ba1\u4e0e\u4f18\u5316\u8fc7\u7a0b\u3002</p> <p>\u6548\u7387\u8bc4\u4f30\u65b9\u6cd5\u4e3b\u8981\u5206\u4e3a\u4e24\u79cd\uff1a\u5b9e\u9645\u6d4b\u8bd5\u548c\u7406\u8bba\u4f30\u7b97\u3002</p>"},{"location":"chapter_computational_complexity/performance_evaluation/#211","title":"2.1.1. \u00a0 \u5b9e\u9645\u6d4b\u8bd5","text":"<p>\u5047\u8bbe\u6211\u4eec\u73b0\u5728\u6709\u7b97\u6cd5 <code>A</code> \u548c\u7b97\u6cd5 <code>B</code> \uff0c\u5b83\u4eec\u90fd\u80fd\u89e3\u51b3\u540c\u4e00\u95ee\u9898\uff0c\u73b0\u5728\u9700\u8981\u5bf9\u6bd4\u8fd9\u4e24\u4e2a\u7b97\u6cd5\u7684\u6548\u7387\u3002\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u627e\u4e00\u53f0\u8ba1\u7b97\u673a\uff0c\u8fd0\u884c\u8fd9\u4e24\u4e2a\u7b97\u6cd5\uff0c\u5e76\u76d1\u63a7\u8bb0\u5f55\u5b83\u4eec\u7684\u8fd0\u884c\u65f6\u95f4\u548c\u5185\u5b58\u5360\u7528\u60c5\u51b5\u3002\u8fd9\u79cd\u8bc4\u4f30\u65b9\u5f0f\u80fd\u591f\u53cd\u6620\u771f\u5b9e\u60c5\u51b5\uff0c\u4f46\u4e5f\u5b58\u5728\u8f83\u5927\u5c40\u9650\u6027\u3002</p> <p>\u96be\u4ee5\u6392\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5e72\u6270\u56e0\u7d20\u3002\u786c\u4ef6\u914d\u7f6e\u4f1a\u5f71\u54cd\u7b97\u6cd5\u7684\u6027\u80fd\u8868\u73b0\u3002\u6bd4\u5982\u5728\u67d0\u53f0\u8ba1\u7b97\u673a\u4e2d\uff0c\u7b97\u6cd5 <code>A</code> \u7684\u8fd0\u884c\u65f6\u95f4\u6bd4\u7b97\u6cd5 <code>B</code> \u77ed\uff1b\u4f46\u5728\u53e6\u4e00\u53f0\u914d\u7f6e\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u5f97\u5230\u76f8\u53cd\u7684\u6d4b\u8bd5\u7ed3\u679c\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u5728\u5404\u79cd\u673a\u5668\u4e0a\u8fdb\u884c\u6d4b\u8bd5\uff0c\u7edf\u8ba1\u5e73\u5747\u6548\u7387\uff0c\u800c\u8fd9\u662f\u4e0d\u73b0\u5b9e\u7684\u3002</p> <p>\u5c55\u5f00\u5b8c\u6574\u6d4b\u8bd5\u975e\u5e38\u8017\u8d39\u8d44\u6e90\u3002\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u7684\u53d8\u5316\uff0c\u7b97\u6cd5\u4f1a\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 <code>A</code> \u7684\u8fd0\u884c\u65f6\u95f4\u6bd4\u7b97\u6cd5 <code>B</code> \u66f4\u5c11\uff1b\u800c\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5927\u65f6\uff0c\u6d4b\u8bd5\u7ed3\u679c\u53ef\u80fd\u6070\u6070\u76f8\u53cd\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u5f97\u5230\u6709\u8bf4\u670d\u529b\u7684\u7ed3\u8bba\uff0c\u6211\u4eec\u9700\u8981\u6d4b\u8bd5\u5404\u79cd\u89c4\u6a21\u7684\u8f93\u5165\u6570\u636e\uff0c\u800c\u8fd9\u6837\u9700\u8981\u8017\u8d39\u5927\u91cf\u7684\u8ba1\u7b97\u8d44\u6e90\u3002</p>"},{"location":"chapter_computational_complexity/performance_evaluation/#212","title":"2.1.2. \u00a0 \u7406\u8bba\u4f30\u7b97","text":"<p>\u7531\u4e8e\u5b9e\u9645\u6d4b\u8bd5\u5177\u6709\u8f83\u5927\u7684\u5c40\u9650\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u4ec5\u901a\u8fc7\u4e00\u4e9b\u8ba1\u7b97\u6765\u8bc4\u4f30\u7b97\u6cd5\u7684\u6548\u7387\u3002\u8fd9\u79cd\u4f30\u7b97\u65b9\u6cd5\u88ab\u79f0\u4e3a\u300c\u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 Asymptotic Complexity Analysis\u300d\uff0c\u7b80\u79f0\u4e3a\u300c\u590d\u6742\u5ea6\u5206\u6790\u300d\u3002</p> <p>\u590d\u6742\u5ea6\u5206\u6790\u8bc4\u4f30\u7684\u662f\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u589e\u591a\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u5b9a\u4e49\u6709\u4e9b\u62d7\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u5206\u4e3a\u4e09\u4e2a\u91cd\u70b9\u6765\u7406\u89e3\uff1a</p> <ol> <li>\u201c\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u201d\u53ef\u5206\u4e3a\u8fd0\u884c\u65f6\u95f4\u548c\u5360\u7528\u7a7a\u95f4\u4e24\u90e8\u5206\uff0c\u4e0e\u4e4b\u5bf9\u5e94\u5730\uff0c\u590d\u6742\u5ea6\u53ef\u5206\u4e3a\u300c\u65f6\u95f4\u590d\u6742\u5ea6 Time Complexity\u300d\u548c\u300c\u7a7a\u95f4\u590d\u6742\u5ea6 Space Complexity\u300d\u3002</li> <li>\u201c\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u589e\u591a\u65f6\u201d\u610f\u5473\u7740\u590d\u6742\u5ea6\u53cd\u6620\u4e86\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u4e0e\u8f93\u5165\u6570\u636e\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u3002</li> <li>\u201c\u589e\u957f\u8d8b\u52bf\u201d\u8868\u793a\u590d\u6742\u5ea6\u5206\u6790\u5173\u6ce8\u7684\u662f\u7b97\u6cd5\u65f6\u95f4\u4e0e\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\uff0c\u800c\u975e\u5177\u4f53\u7684\u8fd0\u884c\u65f6\u95f4\u6216\u5360\u7528\u7a7a\u95f4\u3002</li> </ol> <p>\u590d\u6742\u5ea6\u5206\u6790\u514b\u670d\u4e86\u5b9e\u9645\u6d4b\u8bd5\u65b9\u6cd5\u7684\u5f0a\u7aef\u3002\u9996\u5148\uff0c\u5b83\u72ec\u7acb\u4e8e\u6d4b\u8bd5\u73af\u5883\uff0c\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\u3002\u5176\u6b21\uff0c\u5b83\u53ef\u4ee5\u4f53\u73b0\u4e0d\u540c\u6570\u636e\u91cf\u4e0b\u7684\u7b97\u6cd5\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u5927\u6570\u636e\u91cf\u4e0b\u7684\u7b97\u6cd5\u6027\u80fd\u3002</p> <p>\u5982\u679c\u4f60\u5bf9\u590d\u6742\u5ea6\u5206\u6790\u7684\u6982\u5ff5\u4ecd\u611f\u5230\u56f0\u60d1\uff0c\u65e0\u9700\u62c5\u5fc3\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7ae0\u8282\u8be6\u7ec6\u4ecb\u7ecd\u3002</p>"},{"location":"chapter_computational_complexity/performance_evaluation/#213","title":"2.1.3. \u00a0 \u590d\u6742\u5ea6\u7684\u91cd\u8981\u6027","text":"<p>\u590d\u6742\u5ea6\u5206\u6790\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u628a\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u7684\u201c\u6807\u5c3a\u201d\uff0c\u5e2e\u52a9\u6211\u4eec\u8861\u91cf\u4e86\u6267\u884c\u67d0\u4e2a\u7b97\u6cd5\u6240\u9700\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\uff0c\u5e76\u4f7f\u6211\u4eec\u80fd\u591f\u5bf9\u6bd4\u4e0d\u540c\u7b97\u6cd5\u4e4b\u95f4\u7684\u6548\u7387\u3002</p> <p>\u590d\u6742\u5ea6\u662f\u4e2a\u6570\u5b66\u6982\u5ff5\uff0c\u5bf9\u4e8e\u521d\u5b66\u8005\u53ef\u80fd\u6bd4\u8f83\u62bd\u8c61\uff0c\u5b66\u4e60\u96be\u5ea6\u76f8\u5bf9\u8f83\u9ad8\u3002\u4ece\u8fd9\u4e2a\u89d2\u5ea6\u770b\uff0c\u590d\u6742\u5ea6\u5206\u6790\u53ef\u80fd\u4e0d\u592a\u9002\u5408\u4f5c\u4e3a\u7b2c\u4e00\u7ae0\u7684\u5185\u5bb9\u3002</p> <p>\u7136\u800c\uff0c\u5f53\u6211\u4eec\u8ba8\u8bba\u67d0\u4e2a\u6570\u636e\u7ed3\u6784\u6216\u7b97\u6cd5\u7684\u7279\u70b9\u65f6\uff0c\u96be\u4ee5\u907f\u514d\u8981\u5206\u6790\u5176\u8fd0\u884c\u901f\u5ea6\u548c\u7a7a\u95f4\u4f7f\u7528\u60c5\u51b5\u3002\u56e0\u6b64\uff0c\u5728\u6df1\u5165\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u524d\uff0c\u5efa\u8bae\u4f60\u5148\u5bf9\u590d\u6742\u5ea6\u5efa\u7acb\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u80fd\u591f\u5b8c\u6210\u7b80\u5355\u7b97\u6cd5\u7684\u590d\u6742\u5ea6\u5206\u6790\u3002</p>"},{"location":"chapter_computational_complexity/space_complexity/","title":"2.3. \u00a0 \u7a7a\u95f4\u590d\u6742\u5ea6","text":"<p>\u300c\u7a7a\u95f4\u590d\u6742\u5ea6 Space Complexity\u300d\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u6982\u5ff5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ea\u9700\u5c06\u201c\u8fd0\u884c\u65f6\u95f4\u201d\u66ff\u6362\u4e3a\u201c\u5360\u7528\u5185\u5b58\u7a7a\u95f4\u201d\u3002</p>"},{"location":"chapter_computational_complexity/space_complexity/#231","title":"2.3.1. \u00a0 \u7b97\u6cd5\u76f8\u5173\u7a7a\u95f4","text":"<p>\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\uff1a</p> <ul> <li>\u8f93\u5165\u7a7a\u95f4\uff1a\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u7684\u8f93\u5165\u6570\u636e\u3002</li> <li>\u6682\u5b58\u7a7a\u95f4\uff1a\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u53d8\u91cf\u3001\u5bf9\u8c61\u3001\u51fd\u6570\u4e0a\u4e0b\u6587\u7b49\u6570\u636e\u3002</li> <li>\u8f93\u51fa\u7a7a\u95f4\uff1a\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u7684\u8f93\u51fa\u6570\u636e\u3002</li> </ul> <p>\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u7edf\u8ba1\u8303\u56f4\u662f\u201c\u6682\u5b58\u7a7a\u95f4\u201d\u52a0\u4e0a\u201c\u8f93\u51fa\u7a7a\u95f4\u201d\u3002</p> <p>\u6682\u5b58\u7a7a\u95f4\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a</p> <ul> <li>\u6682\u5b58\u6570\u636e\uff1a\u7528\u4e8e\u4fdd\u5b58\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u5404\u79cd\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u7b49\u3002</li> <li>\u6808\u5e27\u7a7a\u95f4\uff1a\u7528\u4e8e\u4fdd\u5b58\u8c03\u7528\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u6570\u636e\u3002\u7cfb\u7edf\u5728\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u90fd\u4f1a\u5728\u6808\u9876\u90e8\u521b\u5efa\u4e00\u4e2a\u6808\u5e27\uff0c\u51fd\u6570\u8fd4\u56de\u540e\uff0c\u6808\u5e27\u7a7a\u95f4\u4f1a\u88ab\u91ca\u653e\u3002</li> <li>\u6307\u4ee4\u7a7a\u95f4\uff1a\u7528\u4e8e\u4fdd\u5b58\u7f16\u8bd1\u540e\u7684\u7a0b\u5e8f\u6307\u4ee4\uff0c\u5728\u5b9e\u9645\u7edf\u8ba1\u4e2d\u901a\u5e38\u5ffd\u7565\u4e0d\u8ba1\u3002</li> </ul> <p>\u56e0\u6b64\u5728\u5206\u6790\u4e00\u6bb5\u7a0b\u5e8f\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u7edf\u8ba1\u6682\u5b58\u6570\u636e\u3001\u8f93\u51fa\u6570\u636e\u3001\u6808\u5e27\u7a7a\u95f4\u4e09\u90e8\u5206\u3002</p> <p></p> <p> \u56fe\uff1a\u7b97\u6cd5\u4f7f\u7528\u7684\u76f8\u5173\u7a7a\u95f4 </p> JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>/* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(int x) { val = x; }\n}\n/* \u51fd\u6570 */\nint function() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\nfinal int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>/* \u7ed3\u6784\u4f53 */\nstruct Node {\nint val;\nNode *next;\nNode(int x) : val(x), next(nullptr) {}\n};\n/* \u51fd\u6570 */\nint func() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\nconst int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode* node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = func(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>class Node:\n\"\"\"\u7c7b\"\"\"\ndef __init__(self, x: int):\nself.val: int = x # \u8282\u70b9\u503c\nself.next: Optional[Node] = None # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\ndef function() -&gt; int:\n\"\"\"\u51fd\u6570\"\"\"\n# do something...\nreturn 0\ndef algorithm(n) -&gt; int: # \u8f93\u5165\u6570\u636e\nA = 0 # \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff0c\u4e00\u822c\u7528\u5927\u5199\u5b57\u6bcd\u8868\u793a\uff09\nb = 0 # \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nnode = Node(0) # \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nc = function() # \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn A + b + c # \u8f93\u51fa\u6570\u636e\n</code></pre> <pre><code>/* \u7ed3\u6784\u4f53 */\ntype node struct {\nval int\nnext *node\n}\n/* \u521b\u5efa node \u7ed3\u6784\u4f53 */\nfunc newNode(val int) *node {\nreturn &amp;node{val: val}\n}\n/* \u51fd\u6570 */\nfunc function() int {\n// do something...\nreturn 0\n}\nfunc algorithm(n int) int { // \u8f93\u5165\u6570\u636e\nconst a = 0 // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nb := 0 // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nnewNode(0) // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nc := function() // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>/* \u7c7b */\nclass Node {\nval;\nnext;\nconstructor(val) {\nthis.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\nthis.next = null; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n/* \u51fd\u6570 */\nfunction constFunc() {\n// do something\nreturn 0;\n}\nfunction algorithm(n) { // \u8f93\u5165\u6570\u636e\nconst a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nlet b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nconst node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nconst c = constFunc(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>/* \u7c7b */\nclass Node {\nval: number;\nnext: Node | null;\nconstructor(val?: number) {\nthis.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\nthis.next = null; // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n}\n}\n/* \u51fd\u6570 */\nfunction constFunc(): number {\n// do something\nreturn 0;\n}\nfunction algorithm(n: number): number { // \u8f93\u5165\u6570\u636e\nconst a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nlet b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nconst node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nconst c = constFunc(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>/* \u51fd\u6570 */\nint func() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\nconst int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nint c = func(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>/* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(int x) { val = x; }\n}\n/* \u51fd\u6570 */\nint function() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\nconst int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>/* \u7c7b */\nclass Node {\nvar val: Int\nvar next: Node?\ninit(x: Int) {\nval = x\n}\n}\n/* \u51fd\u6570 */\nfunc function() -&gt; Int {\n// do something...\nreturn 0\n}\nfunc algorithm(n: Int) -&gt; Int { // \u8f93\u5165\u6570\u636e\nlet a = 0 // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nvar b = 0 // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nlet node = Node(x: 0) // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nlet c = function() // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>\n</code></pre> <pre><code>/* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(this.val, [this.next]);\n}\n/* \u51fd\u6570 */\nint function() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\nconst int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode node = Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>\n</code></pre>"},{"location":"chapter_computational_complexity/space_complexity/#232","title":"2.3.2. \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"<p>\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u5927\u81f4\u76f8\u540c\uff0c\u53ea\u9700\u5c06\u7edf\u8ba1\u5bf9\u8c61\u4ece\u201c\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\u201d\u8f6c\u4e3a\u201c\u4f7f\u7528\u7a7a\u95f4\u5927\u5c0f\u201d\u3002</p> <p>\u800c\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u300c\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u300d\u3002\u8fd9\u662f\u56e0\u4e3a\u5185\u5b58\u7a7a\u95f4\u662f\u4e00\u9879\u786c\u6027\u8981\u6c42\uff0c\u6211\u4eec\u5fc5\u987b\u786e\u4fdd\u5728\u6240\u6709\u8f93\u5165\u6570\u636e\u4e0b\u90fd\u6709\u8db3\u591f\u7684\u5185\u5b58\u7a7a\u95f4\u9884\u7559\u3002</p> <p>\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u201c\u6700\u5dee\u201d\u6709\u4e24\u5c42\u542b\u4e49\u3002</p> <ol> <li>\u4ee5\u6700\u5dee\u8f93\u5165\u6570\u636e\u4e3a\u51c6\uff1a\u5f53 \\(n &lt; 10\\) \u65f6\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff1b\u4f46\u5f53 \\(n &gt; 10\\) \u65f6\uff0c\u521d\u59cb\u5316\u7684\u6570\u7ec4 <code>nums</code> \u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\uff1b\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002</li> <li>\u4ee5\u7b97\u6cd5\u8fd0\u884c\u4e2d\u7684\u5cf0\u503c\u5185\u5b58\u4e3a\u51c6\uff1a\u4f8b\u5982\uff0c\u7a0b\u5e8f\u5728\u6267\u884c\u6700\u540e\u4e00\u884c\u4e4b\u524d\uff0c\u5360\u7528 \\(O(1)\\) \u7a7a\u95f4\uff1b\u5f53\u521d\u59cb\u5316\u6570\u7ec4 <code>nums</code> \u65f6\uff0c\u7a0b\u5e8f\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\uff1b\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002</li> </ol> JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>void algorithm(int n) {\nint a = 0; // O(1)\nint[] b = new int[10000]; // O(1)\nif (n &gt; 10)\nint[] nums = new int[n]; // O(n)\n}\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 0; // O(1)\nvector&lt;int&gt; b(10000); // O(1)\nif (n &gt; 10)\nvector&lt;int&gt; nums(n); // O(n)\n}\n</code></pre> <pre><code>def algorithm(n: int):\na = 0 # O(1)\nb = [0] * 10000 # O(1)\nif n &gt; 10:\nnums = [0] * n # O(n)\n</code></pre> <pre><code>func algorithm(n int) {\na := 0 // O(1)\nb := make([]int, 10000) // O(1)\nvar nums []int\nif n &gt; 10 {\nnums := make([]int, n) // O(n)\n}\nfmt.Println(a, b, nums)\n}\n</code></pre> <pre><code>function algorithm(n) {\nconst a = 0; // O(1)\nconst b = new Array(10000); // O(1)\nif (n &gt; 10) {\nconst nums = new Array(n); // O(n)\n}\n}\n</code></pre> <pre><code>function algorithm(n: number): void {\nconst a = 0; // O(1)\nconst b = new Array(10000); // O(1)\nif (n &gt; 10) {\nconst nums = new Array(n); // O(n)\n}\n}\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 0; // O(1)\nint b[10000]; // O(1)\nif (n &gt; 10)\nint nums[n] = {0}; // O(n)\n}\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 0; // O(1)\nint[] b = new int[10000]; // O(1)\nif (n &gt; 10) {\nint[] nums = new int[n]; // O(n)\n}\n}\n</code></pre> <pre><code>func algorithm(n: Int) {\nlet a = 0 // O(1)\nlet b = Array(repeating: 0, count: 10000) // O(1)\nif n &gt; 10 {\nlet nums = Array(repeating: 0, count: n) // O(n)\n}\n}\n</code></pre> <pre><code>\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 0; // O(1)\nList&lt;int&gt; b = List.filled(10000, 0); // O(1)\nif (n &gt; 10) {\nList&lt;int&gt; nums = List.filled(n, 0); // O(n)\n}\n}\n</code></pre> <pre><code>\n</code></pre> <p>\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u7edf\u8ba1\u6808\u5e27\u7a7a\u95f4\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff1a</p> <ul> <li>\u51fd\u6570 <code>loop()</code> \u5728\u5faa\u73af\u4e2d\u8c03\u7528\u4e86 \\(n\\) \u6b21 <code>function()</code> \uff0c\u6bcf\u8f6e\u4e2d\u7684 <code>function()</code> \u90fd\u8fd4\u56de\u5e76\u91ca\u653e\u4e86\u6808\u5e27\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \u3002</li> <li>\u9012\u5f52\u51fd\u6570 <code>recur()</code> \u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4f1a\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 <code>recur()</code> \uff0c\u4ece\u800c\u5360\u7528 \\(O(n)\\) \u7684\u6808\u5e27\u7a7a\u95f4\u3002</li> </ul> JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>int function() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i &lt; n; i++) {\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>int func() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i &lt; n; i++) {\nfunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>def function() -&gt; int:\n# do something\nreturn 0\ndef loop(n: int):\n\"\"\"\u5faa\u73af O(1)\"\"\"\nfor _ in range(n):\nfunction()\ndef recur(n: int) -&gt; int:\n\"\"\"\u9012\u5f52 O(n)\"\"\"\nif n == 1: return\nreturn recur(n - 1)\n</code></pre> <pre><code>func function() int {\n// do something\nreturn 0\n}\n/* \u5faa\u73af O(1) */\nfunc loop(n int) {\nfor i := 0; i &lt; n; i++ {\nfunction()\n}\n}\n/* \u9012\u5f52 O(n) */\nfunc recur(n int) {\nif n == 1 {\nreturn\n}\nrecur(n - 1)\n}\n</code></pre> <pre><code>function constFunc() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nfunction loop(n) {\nfor (let i = 0; i &lt; n; i++) {\nconstFunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nfunction recur(n) {\nif (n === 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>function constFunc(): number {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nfunction loop(n: number): void {\nfor (let i = 0; i &lt; n; i++) {\nconstFunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nfunction recur(n: number): void {\nif (n === 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>int func() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i &lt; n; i++) {\nfunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>int function() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i &lt; n; i++) {\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nint recur(int n) {\nif (n == 1) return 1;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>@discardableResult\nfunc function() -&gt; Int {\n// do something\nreturn 0\n}\n/* \u5faa\u73af O(1) */\nfunc loop(n: Int) {\nfor _ in 0 ..&lt; n {\nfunction()\n}\n}\n/* \u9012\u5f52 O(n) */\nfunc recur(n: Int) {\nif n == 1 {\nreturn\n}\nrecur(n: n - 1)\n}\n</code></pre> <pre><code>\n</code></pre> <pre><code>int function() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i &lt; n; i++) {\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>\n</code></pre>"},{"location":"chapter_computational_complexity/space_complexity/#233","title":"2.3.3. \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"<p>\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u6709\uff08\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\uff09\uff1a</p> \\[ \\begin{aligned} O(1) &lt; O(\\log n) &lt; O(n) &lt; O(n^2) &lt; O(2^n) \\newline \\text{\u5e38\u6570\u9636} &lt; \\text{\u5bf9\u6570\u9636} &lt; \\text{\u7ebf\u6027\u9636} &lt; \\text{\u5e73\u65b9\u9636} &lt; \\text{\u6307\u6570\u9636} \\end{aligned} \\] <p></p> <p> \u56fe\uff1a\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u89c1\u7c7b\u578b </p> <p>Tip</p> <p>\u90e8\u5206\u793a\u4f8b\u4ee3\u7801\u9700\u8981\u4e00\u4e9b\u524d\u7f6e\u77e5\u8bc6\uff0c\u5305\u62ec\u6570\u7ec4\u3001\u94fe\u8868\u3001\u4e8c\u53c9\u6811\u3001\u9012\u5f52\u7b97\u6cd5\u7b49\u3002\u5982\u679c\u4f60\u9047\u5230\u770b\u4e0d\u61c2\u7684\u5730\u65b9\uff0c\u53ef\u4ee5\u5728\u5b66\u4e60\u5b8c\u540e\u9762\u7ae0\u8282\u540e\u518d\u6765\u590d\u4e60\u3002</p>"},{"location":"chapter_computational_complexity/space_complexity/#o1","title":"\u5e38\u6570\u9636 \\(O(1)\\)","text":"<p>\u5e38\u6570\u9636\u5e38\u89c1\u4e8e\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u7684\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u3002</p> <p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u5faa\u73af\u4e2d\u521d\u59cb\u5316\u53d8\u91cf\u6216\u8c03\u7528\u51fd\u6570\u800c\u5360\u7528\u7684\u5185\u5b58\uff0c\u5728\u8fdb\u5165\u4e0b\u4e00\u5faa\u73af\u540e\u5c31\u4f1a\u88ab\u91ca\u653e\uff0c\u5373\u4e0d\u4f1a\u7d2f\u79ef\u5360\u7528\u7a7a\u95f4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust space_complexity.java<pre><code>/* \u51fd\u6570 */\nint function() {\n// do something\nreturn 0;\n}\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nfinal int a = 0;\nint b = 0;\nint[] nums = new int[10000];\nListNode node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i &lt; n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i &lt; n; i++) {\nfunction();\n}\n}\n</code></pre> space_complexity.cpp<pre><code>/* \u51fd\u6570 */\nint func() {\n// do something\nreturn 0;\n}\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst int a = 0;\nint b = 0;\nvector&lt;int&gt; nums(10000);\nListNode node(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i &lt; n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i &lt; n; i++) {\nfunc();\n}\n}\n</code></pre> space_complexity.py<pre><code>def function() -&gt; int:\n\"\"\"\u51fd\u6570\"\"\"\n# do something\nreturn 0\ndef constant(n: int):\n\"\"\"\u5e38\u6570\u9636\"\"\"\n# \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\na = 0\nnums = [0] * 10000\nnode = ListNode(0)\n# \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in range(n):\nc = 0\n# \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in range(n):\nfunction()\n</code></pre> space_complexity.go<pre><code>/* \u51fd\u6570 */\nfunc function() int {\n// do something...\nreturn 0\n}\n/* \u5e38\u6570\u9636 */\nfunc spaceConstant(n int) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a = 0\nb := 0\nnums := make([]int, 10000)\nListNode := newNode(0)\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nvar c int\nfor i := 0; i &lt; n; i++ {\nc = 0\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor i := 0; i &lt; n; i++ {\nfunction()\n}\nfmt.Println(a, b, nums, c, ListNode)\n}\n</code></pre> space_complexity.js<pre><code>/* \u51fd\u6570 */\nfunction constFunc() {\n// do something\nreturn 0;\n}\n/* \u5e38\u6570\u9636 */\nfunction constant(n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a = 0;\nconst b = 0;\nconst nums = new Array(10000);\nconst node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i &lt; n; i++) {\nconst c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i &lt; n; i++) {\nconstFunc();\n}\n}\n</code></pre> space_complexity.ts<pre><code>/* \u51fd\u6570 */\nfunction constFunc(): number {\n// do something\nreturn 0;\n}\n/* \u5e38\u6570\u9636 */\nfunction constant(n: number): void {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a = 0;\nconst b = 0;\nconst nums = new Array(10000);\nconst node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i &lt; n; i++) {\nconst c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i &lt; n; i++) {\nconstFunc();\n}\n}\n</code></pre> space_complexity.c<pre><code>/* \u51fd\u6570 */\nint func() {\n// do something\nreturn 0;\n}\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst int a = 0;\nint b = 0;\nint nums[1000];\nListNode *node = newListNode(0);\nfree(node);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i &lt; n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i &lt; n; i++) {\nfunc();\n}\n}\n</code></pre> space_complexity.cs<pre><code>/* \u51fd\u6570 */\nint function() {\n// do something\nreturn 0;\n}\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nint a = 0;\nint b = 0;\nint[] nums = new int[10000];\nListNode node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i &lt; n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i &lt; n; i++) {\nfunction();\n}\n}\n</code></pre> space_complexity.swift<pre><code>/* \u51fd\u6570 */\n@discardableResult\nfunc function() -&gt; Int {\n// do something\nreturn 0\n}\n/* \u5e38\u6570\u9636 */\nfunc constant(n: Int) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nlet a = 0\nvar b = 0\nlet nums = Array(repeating: 0, count: 10000)\nlet node = ListNode(x: 0)\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in 0 ..&lt; n {\nlet c = 0\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in 0 ..&lt; n {\nfunction()\n}\n}\n</code></pre> space_complexity.zig<pre><code>[class]{}-[func]{function}\n// \u5e38\u6570\u9636\nfn constant(n: i32) void {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a: i32 = 0;\nvar b: i32 = 0;\nvar nums = [_]i32{0}**10000;\nvar node = inc.ListNode(i32){.val = 0};\nvar i: i32 = 0;\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nwhile (i &lt; n) : (i += 1) {\nvar c: i32 = 0;\n_ = c;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\ni = 0;\nwhile (i &lt; n) : (i += 1) {\n_ = function();\n}\n_ = a;\n_ = b;\n_ = nums;\n_ = node;\n}\n</code></pre> space_complexity.dart<pre><code>/* \u51fd\u6570 */\nint function() {\n// do something\nreturn 0;\n}\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nfinal int a = 0;\nint b = 0;\nList&lt;int&gt; nums = List.filled(10000, 0);\nListNode node = ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (var i = 0; i &lt; n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (var i = 0; i &lt; n; i++) {\nfunction();\n}\n}\n</code></pre> space_complexity.rs<pre><code>/* \u51fd\u6570 */\nfn function() -&gt;i32 {\n// do something\nreturn 0;\n}\n/* \u5e38\u6570\u9636 */\n#[allow(unused)]\nfn constant(n: i32) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst A: i32 = 0;\nlet b = 0;\nlet nums = vec![0; 10000];\nlet node = ListNode::new(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor i in 0..n {\nlet c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor i in 0..n {\nfunction();\n}\n}\n</code></pre>"},{"location":"chapter_computational_complexity/space_complexity/#on","title":"\u7ebf\u6027\u9636 \\(O(n)\\)","text":"<p>\u7ebf\u6027\u9636\u5e38\u89c1\u4e8e\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u6b63\u6bd4\u7684\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u7b49\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust space_complexity.java<pre><code>/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nint[] nums = new int[n];\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nList&lt;ListNode&gt; nodes = new ArrayList&lt;&gt;();\nfor (int i = 0; i &lt; n; i++) {\nnodes.add(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nMap&lt;Integer, String&gt; map = new HashMap&lt;&gt;();\nfor (int i = 0; i &lt; n; i++) {\nmap.put(i, String.valueOf(i));\n}\n}\n</code></pre> space_complexity.cpp<pre><code>/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nvector&lt;int&gt; nums(n);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nvector&lt;ListNode&gt; nodes;\nfor (int i = 0; i &lt; n; i++) {\nnodes.push_back(ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nunordered_map&lt;int, string&gt; map;\nfor (int i = 0; i &lt; n; i++) {\nmap[i] = to_string(i);\n}\n}\n</code></pre> space_complexity.py<pre><code>def linear(n: int):\n\"\"\"\u7ebf\u6027\u9636\"\"\"\n# \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nnums = [0] * n\n# \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nhmap = dict[int, str]()\nfor i in range(n):\nhmap[i] = str(i)\n</code></pre> space_complexity.go<pre><code>/* \u7ebf\u6027\u9636 */\nfunc spaceLinear(n int) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n_ = make([]int, n)\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nvar nodes []*node\nfor i := 0; i &lt; n; i++ {\nnodes = append(nodes, newNode(i))\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nm := make(map[int]string, n)\nfor i := 0; i &lt; n; i++ {\nm[i] = strconv.Itoa(i)\n}\n}\n</code></pre> space_complexity.js<pre><code>/* \u7ebf\u6027\u9636 */\nfunction linear(n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nconst nums = new Array(n);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst nodes = [];\nfor (let i = 0; i &lt; n; i++) {\nnodes.push(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst map = new Map();\nfor (let i = 0; i &lt; n; i++) {\nmap.set(i, i.toString());\n}\n}\n</code></pre> space_complexity.ts<pre><code>/* \u7ebf\u6027\u9636 */\nfunction linear(n: number): void {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nconst nums = new Array(n);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst nodes: ListNode[] = [];\nfor (let i = 0; i &lt; n; i++) {\nnodes.push(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst map = new Map();\nfor (let i = 0; i &lt; n; i++) {\nmap.set(i, i.toString());\n}\n}\n</code></pre> space_complexity.c<pre><code>/* \u54c8\u5e0c\u8868 */\nstruct hashTable {\nint key;\nint val;\nUT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n};\ntypedef struct hashTable hashTable;\n/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nint *nums = malloc(sizeof(int) * n);\nfree(nums);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nListNode **nodes = malloc(sizeof(ListNode *) * n);\nfor (int i = 0; i &lt; n; i++) {\nnodes[i] = newListNode(i);\n}\n// \u5185\u5b58\u91ca\u653e\nfor (int i = 0; i &lt; n; i++) {\nfree(nodes[i]);\n}\nfree(nodes);\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nhashTable *h = NULL;\nfor (int i = 0; i &lt; n; i++) {\nhashTable *tmp = malloc(sizeof(hashTable));\ntmp-&gt;key = i;\ntmp-&gt;val = i;\nHASH_ADD_INT(h, key, tmp);\n}\n// \u5185\u5b58\u91ca\u653e\nhashTable *curr, *tmp;\nHASH_ITER(hh, h, curr, tmp) {\nHASH_DEL(h, curr);\nfree(curr);\n}\n}\n</code></pre> space_complexity.cs<pre><code>/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nint[] nums = new int[n];\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nList&lt;ListNode&gt; nodes = new();\nfor (int i = 0; i &lt; n; i++) {\nnodes.Add(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nDictionary&lt;int, string&gt; map = new();\nfor (int i = 0; i &lt; n; i++) {\nmap.Add(i, i.ToString());\n}\n}\n</code></pre> space_complexity.swift<pre><code>/* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nlet nums = Array(repeating: 0, count: n)\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nlet nodes = (0 ..&lt; n).map { ListNode(x: $0) }\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nlet map = Dictionary(uniqueKeysWithValues: (0 ..&lt; n).map { ($0, \"\\($0)\") })\n}\n</code></pre> space_complexity.zig<pre><code>// \u7ebf\u6027\u9636\nfn linear(comptime n: i32) !void {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nvar nums = [_]i32{0}**n;\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nvar nodes = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nodes.deinit();\nvar i: i32 = 0;\nwhile (i &lt; n) : (i += 1) {\ntry nodes.append(i);\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nvar map = std.AutoArrayHashMap(i32, []const u8).init(std.heap.page_allocator);\ndefer map.deinit();\nvar j: i32 = 0;\nwhile (j &lt; n) : (j += 1) {\nconst string = try std.fmt.allocPrint(std.heap.page_allocator, \"{d}\", .{j});\ndefer std.heap.page_allocator.free(string);\ntry map.put(i, string);\n}\n_ = nums;\n}\n</code></pre> space_complexity.dart<pre><code>/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nList&lt;int&gt; nums = List.filled(n, 0);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nList&lt;ListNode&gt; nodes = [];\nfor (var i = 0; i &lt; n; i++) {\nnodes.add(ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nMap&lt;int, String&gt; map = HashMap();\nfor (var i = 0; i &lt; n; i++) {\nmap.putIfAbsent(i, () =&gt; i.toString());\n}\n}\n</code></pre> space_complexity.rs<pre><code>/* \u7ebf\u6027\u9636 */\n#[allow(unused)]\nfn linear(n: i32) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nlet mut nums = vec![0; n as usize];\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nlet mut nodes = Vec::new();\nfor i in 0..n {\nnodes.push(ListNode::new(i))\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nlet mut map = HashMap::new();\nfor i in 0..n {\nmap.insert(i, i.to_string());\n}\n}\n</code></pre> <p>\u4ee5\u4e0b\u9012\u5f52\u51fd\u6570\u4f1a\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 <code>algorithm()</code> \u51fd\u6570\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust space_complexity.java<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\nSystem.out.println(\"\u9012\u5f52 n = \" + n);\nif (n == 1)\nreturn;\nlinearRecur(n - 1);\n}\n</code></pre> space_complexity.cpp<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\ncout &lt;&lt; \"\u9012\u5f52 n = \" &lt;&lt; n &lt;&lt; endl;\nif (n == 1)\nreturn;\nlinearRecur(n - 1);\n}\n</code></pre> space_complexity.py<pre><code>def linear_recur(n: int):\n\"\"\"\u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nprint(\"\u9012\u5f52 n =\", n)\nif n == 1:\nreturn\nlinear_recur(n - 1)\n</code></pre> space_complexity.go<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceLinearRecur(n int) {\nfmt.Println(\"\u9012\u5f52 n =\", n)\nif n == 1 {\nreturn\n}\nspaceLinearRecur(n - 1)\n}\n</code></pre> space_complexity.js<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n) {\nconsole.log(`\u9012\u5f52 n = ${n}`);\nif (n === 1) return;\nlinearRecur(n - 1);\n}\n</code></pre> space_complexity.ts<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n: number): void {\nconsole.log(`\u9012\u5f52 n = ${n}`);\nif (n === 1) return;\nlinearRecur(n - 1);\n}\n</code></pre> space_complexity.c<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\nprintf(\"\u9012\u5f52 n = %d\\r\\n\", n);\nif (n == 1)\nreturn;\nlinearRecur(n - 1);\n}\n</code></pre> space_complexity.cs<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\nConsole.WriteLine(\"\u9012\u5f52 n = \" + n);\nif (n == 1) return;\nlinearRecur(n - 1);\n}\n</code></pre> space_complexity.swift<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc linearRecur(n: Int) {\nprint(\"\u9012\u5f52 n = \\(n)\")\nif n == 1 {\nreturn\n}\nlinearRecur(n: n - 1)\n}\n</code></pre> space_complexity.zig<pre><code>// \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn linearRecur(comptime n: i32) void {\nstd.debug.print(\"\u9012\u5f52 n = {}\\n\", .{n});\nif (n == 1) return;\nlinearRecur(n - 1);\n}\n</code></pre> space_complexity.dart<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\nprint('\u9012\u5f52 n = $n');\nif (n == 1) return;\nlinearRecur(n - 1);\n}\n</code></pre> space_complexity.rs<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn linear_recur(n: i32) {\nprintln!(\"\u9012\u5f52 n = {}\", n);\nif n == 1 {return};\nlinear_recur(n - 1);\n}\n</code></pre> <p></p> <p> \u56fe\uff1a\u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u7ebf\u6027\u9636\u7a7a\u95f4\u590d\u6742\u5ea6 </p>"},{"location":"chapter_computational_complexity/space_complexity/#on2","title":"\u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"<p>\u5e73\u65b9\u9636\u5e38\u89c1\u4e8e\u77e9\u9635\u548c\u56fe\uff0c\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u5e73\u65b9\u5173\u7cfb\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust space_complexity.java<pre><code>/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nint[][] numMatrix = new int[n][n];\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nList&lt;List&lt;Integer&gt;&gt; numList = new ArrayList&lt;&gt;();\nfor (int i = 0; i &lt; n; i++) {\nList&lt;Integer&gt; tmp = new ArrayList&lt;&gt;();\nfor (int j = 0; j &lt; n; j++) {\ntmp.add(0);\n}\nnumList.add(tmp);\n}\n}\n</code></pre> space_complexity.cpp<pre><code>/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nvector&lt;vector&lt;int&gt;&gt; numMatrix;\nfor (int i = 0; i &lt; n; i++) {\nvector&lt;int&gt; tmp;\nfor (int j = 0; j &lt; n; j++) {\ntmp.push_back(0);\n}\nnumMatrix.push_back(tmp);\n}\n}\n</code></pre> space_complexity.py<pre><code>def quadratic(n: int):\n\"\"\"\u5e73\u65b9\u9636\"\"\"\n# \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nnum_matrix = [[0] * n for _ in range(n)]\n</code></pre> space_complexity.go<pre><code>/* \u5e73\u65b9\u9636 */\nfunc spaceQuadratic(n int) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nnumMatrix := make([][]int, n)\nfor i := 0; i &lt; n; i++ {\nnumMatrix[i] = make([]int, n)\n}\n}\n</code></pre> space_complexity.js<pre><code>/* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nconst numMatrix = Array(n)\n.fill(null)\n.map(() =&gt; Array(n).fill(null));\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nconst numList = [];\nfor (let i = 0; i &lt; n; i++) {\nconst tmp = [];\nfor (let j = 0; j &lt; n; j++) {\ntmp.push(0);\n}\nnumList.push(tmp);\n}\n}\n</code></pre> space_complexity.ts<pre><code>/* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): void {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nconst numMatrix = Array(n)\n.fill(null)\n.map(() =&gt; Array(n).fill(null));\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nconst numList = [];\nfor (let i = 0; i &lt; n; i++) {\nconst tmp = [];\nfor (let j = 0; j &lt; n; j++) {\ntmp.push(0);\n}\nnumList.push(tmp);\n}\n}\n</code></pre> space_complexity.c<pre><code>/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nint **numMatrix = malloc(sizeof(int *) * n);\nfor (int i = 0; i &lt; n; i++) {\nint *tmp = malloc(sizeof(int) * n);\nfor (int j = 0; j &lt; n; j++) {\ntmp[j] = 0;\n}\nnumMatrix[i] = tmp;\n}\n// \u5185\u5b58\u91ca\u653e\nfor (int i = 0; i &lt; n; i++) {\nfree(numMatrix[i]);\n}\nfree(numMatrix);\n}\n</code></pre> space_complexity.cs<pre><code>/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nint[,] numMatrix = new int[n, n];\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nList&lt;List&lt;int&gt;&gt; numList = new();\nfor (int i = 0; i &lt; n; i++) {\nList&lt;int&gt; tmp = new();\nfor (int j = 0; j &lt; n; j++) {\ntmp.Add(0);\n}\nnumList.Add(tmp);\n}\n}\n</code></pre> space_complexity.swift<pre><code>/* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) {\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nlet numList = Array(repeating: Array(repeating: 0, count: n), count: n)\n}\n</code></pre> space_complexity.zig<pre><code>// \u5e73\u65b9\u9636\nfn quadratic(n: i32) !void {\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nvar nodes = std.ArrayList(std.ArrayList(i32)).init(std.heap.page_allocator);\ndefer nodes.deinit();\nvar i: i32 = 0;\nwhile (i &lt; n) : (i += 1) {\nvar tmp = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer tmp.deinit();\nvar j: i32 = 0;\nwhile (j &lt; n) : (j += 1) {\ntry tmp.append(0);\n}\ntry nodes.append(tmp);\n}\n}\n</code></pre> space_complexity.dart<pre><code>/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nList&lt;List&lt;int&gt;&gt; numMatrix = List.generate(n, (_) =&gt; List.filled(n, 0));\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nList&lt;List&lt;int&gt;&gt; numList = [];\nfor (var i = 0; i &lt; n; i++) {\nList&lt;int&gt; tmp = [];\nfor (int j = 0; j &lt; n; j++) {\ntmp.add(0);\n}\nnumList.add(tmp);\n}\n}\n</code></pre> space_complexity.rs<pre><code>/* \u5e73\u65b9\u9636 */\n#[allow(unused)]\nfn quadratic(n: i32) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nlet num_matrix = vec![vec![0; n as usize]; n as usize];\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nlet mut num_list = Vec::new();\nfor i in 0..n {\nlet mut tmp = Vec::new();\nfor j in 0..n {\ntmp.push(0);\n}\nnum_list.push(tmp);\n}\n}\n</code></pre> <p>\u5728\u4ee5\u4e0b\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 <code>algorithm()</code> \uff0c\u5e76\u4e14\u6bcf\u4e2a\u51fd\u6570\u4e2d\u90fd\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u6570\u7ec4\uff0c\u957f\u5ea6\u5206\u522b\u4e3a \\(n, n-1, n-2, ..., 2, 1\\) \uff0c\u5e73\u5747\u957f\u5ea6\u4e3a \\(\\frac{n}{2}\\) \uff0c\u56e0\u6b64\u603b\u4f53\u5360\u7528 \\(O(n^2)\\) \u7a7a\u95f4\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust space_complexity.java<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n &lt;= 0)\nreturn 0;\n// \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nint[] nums = new int[n];\nSystem.out.println(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.length);\nreturn quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.cpp<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n &lt;= 0)\nreturn 0;\nvector&lt;int&gt; nums(n);\ncout &lt;&lt; \"\u9012\u5f52 n = \" &lt;&lt; n &lt;&lt; \" \u4e2d\u7684 nums \u957f\u5ea6 = \" &lt;&lt; nums.size() &lt;&lt; endl;\nreturn quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.py<pre><code>def quadratic_recur(n: int) -&gt; int:\n\"\"\"\u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nif n &lt;= 0:\nreturn 0\n# \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nnums = [0] * n\nreturn quadratic_recur(n - 1)\n</code></pre> space_complexity.go<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceQuadraticRecur(n int) int {\nif n &lt;= 0 {\nreturn 0\n}\nnums := make([]int, n)\nfmt.Printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d \\n\", n, len(nums))\nreturn spaceQuadraticRecur(n - 1)\n}\n</code></pre> space_complexity.js<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n) {\nif (n &lt;= 0) return 0;\nconst nums = new Array(n);\nconsole.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\nreturn quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.ts<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n: number): number {\nif (n &lt;= 0) return 0;\nconst nums = new Array(n);\nconsole.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\nreturn quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.c<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n &lt;= 0)\nreturn 0;\nint *nums = malloc(sizeof(int) * n);\nprintf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d\\r\\n\", n, n);\nint res = quadraticRecur(n - 1);\nfree(nums);\nreturn res;\n}\n</code></pre> space_complexity.cs<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n &lt;= 0) return 0;\nint[] nums = new int[n];\nConsole.WriteLine(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.Length);\nreturn quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.swift<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\n@discardableResult\nfunc quadraticRecur(n: Int) -&gt; Int {\nif n &lt;= 0 {\nreturn 0\n}\n// \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nlet nums = Array(repeating: 0, count: n)\nprint(\"\u9012\u5f52 n = \\(n) \u4e2d\u7684 nums \u957f\u5ea6 = \\(nums.count)\")\nreturn quadraticRecur(n: n - 1)\n}\n</code></pre> space_complexity.zig<pre><code>// \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn quadraticRecur(comptime n: i32) i32 {\nif (n &lt;= 0) return 0;\nvar nums = [_]i32{0}**n;\nstd.debug.print(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\\n\", .{n, nums.len});\nreturn quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.dart<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n &lt;= 0) return 0;\nList&lt;int&gt; nums = List.filled(n, 0);\nprint('\u9012\u5f52 n = $n \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}');\nreturn quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.rs<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn quadratic_recur(n: i32) -&gt; i32 {\nif n &lt;= 0 {return 0};\n// \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nlet nums = vec![0; n as usize];\nprintln!(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\", n, nums.len());\nreturn quadratic_recur(n - 1);\n}\n</code></pre> <p></p> <p> \u56fe\uff1a\u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u5e73\u65b9\u9636\u7a7a\u95f4\u590d\u6742\u5ea6 </p>"},{"location":"chapter_computational_complexity/space_complexity/#o2n","title":"\u6307\u6570\u9636 \\(O(2^n)\\)","text":"<p>\u6307\u6570\u9636\u5e38\u89c1\u4e8e\u4e8c\u53c9\u6811\u3002\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u300c\u6ee1\u4e8c\u53c9\u6811\u300d\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(2^n - 1\\) \uff0c\u5360\u7528 \\(O(2^n)\\) \u7a7a\u95f4\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust space_complexity.java<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode buildTree(int n) {\nif (n == 0)\nreturn null;\nTreeNode root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n</code></pre> space_complexity.cpp<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\nif (n == 0)\nreturn nullptr;\nTreeNode *root = new TreeNode(0);\nroot-&gt;left = buildTree(n - 1);\nroot-&gt;right = buildTree(n - 1);\nreturn root;\n}\n</code></pre> space_complexity.py<pre><code>def build_tree(n: int) -&gt; TreeNode | None:\n\"\"\"\u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\"\"\"\nif n == 0:\nreturn None\nroot = TreeNode(0)\nroot.left = build_tree(n - 1)\nroot.right = build_tree(n - 1)\nreturn root\n</code></pre> space_complexity.go<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n int) *treeNode {\nif n == 0 {\nreturn nil\n}\nroot := newTreeNode(0)\nroot.left = buildTree(n - 1)\nroot.right = buildTree(n - 1)\nreturn root\n}\n</code></pre> space_complexity.js<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n) {\nif (n === 0) return null;\nconst root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n</code></pre> space_complexity.ts<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n: number): TreeNode | null {\nif (n === 0) return null;\nconst root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n</code></pre> space_complexity.c<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\nif (n == 0)\nreturn NULL;\nTreeNode *root = newTreeNode(0);\nroot-&gt;left = buildTree(n - 1);\nroot-&gt;right = buildTree(n - 1);\nreturn root;\n}\n</code></pre> space_complexity.cs<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? buildTree(int n) {\nif (n == 0) return null;\nTreeNode root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n</code></pre> space_complexity.swift<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n: Int) -&gt; TreeNode? {\nif n == 0 {\nreturn nil\n}\nlet root = TreeNode(x: 0)\nroot.left = buildTree(n: n - 1)\nroot.right = buildTree(n: n - 1)\nreturn root\n}\n</code></pre> space_complexity.zig<pre><code>// \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\nfn buildTree(mem_allocator: std.mem.Allocator, n: i32) !?*inc.TreeNode(i32) {\nif (n == 0) return null;\nconst root = try mem_allocator.create(inc.TreeNode(i32));\nroot.init(0);\nroot.left = try buildTree(mem_allocator, n - 1);\nroot.right = try buildTree(mem_allocator, n - 1);\nreturn root;\n}\n</code></pre> space_complexity.dart<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? buildTree(int n) {\nif (n == 0) return null;\nTreeNode root = TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n</code></pre> space_complexity.rs<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfn build_tree(n: i32) -&gt; Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt; {\nif n == 0 {return None};\nlet root = TreeNode::new(0);\nroot.borrow_mut().left = build_tree(n - 1);\nroot.borrow_mut().right = build_tree(n - 1);\nreturn Some(root);\n}\n</code></pre> <p></p> <p> \u56fe\uff1a\u6ee1\u4e8c\u53c9\u6811\u4ea7\u751f\u7684\u6307\u6570\u9636\u7a7a\u95f4\u590d\u6742\u5ea6 </p>"},{"location":"chapter_computational_complexity/space_complexity/#olog-n","title":"\u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"<p>\u5bf9\u6570\u9636\u5e38\u89c1\u4e8e\u5206\u6cbb\u7b97\u6cd5\u548c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7b49\u3002</p> <p>\u4f8b\u5982\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u7b97\u6cd5\uff0c\u8f93\u5165\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u6bcf\u8f6e\u9012\u5f52\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5212\u5206\u4e3a\u4e24\u534a\uff0c\u5f62\u6210\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u6808\u5e27\u7a7a\u95f4\u3002</p> <p>\u518d\u4f8b\u5982\u201c\u6570\u5b57\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u201d\uff0c\u8f93\u5165\u4efb\u610f\u6b63\u6574\u6570 \\(n\\) \uff0c\u5b83\u7684\u4f4d\u6570\u4e3a \\(\\log_{10} n\\) \uff0c\u5373\u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a \\(\\log_{10} n\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_{10} n) = O(\\log n)\\) \u3002</p>"},{"location":"chapter_computational_complexity/space_complexity/#234","title":"2.3.4. \u00a0 \u6743\u8861\u65f6\u95f4\u4e0e\u7a7a\u95f4","text":"<p>\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u80fd\u8fbe\u5230\u6700\u4f18\u3002\u7136\u800c\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u540c\u65f6\u4f18\u5316\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u662f\u975e\u5e38\u56f0\u96be\u7684\u3002</p> <p>\u964d\u4f4e\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u9700\u8981\u4ee5\u63d0\u5347\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a\u4ee3\u4ef7\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u6211\u4eec\u5c06\u727a\u7272\u5185\u5b58\u7a7a\u95f4\u6765\u63d0\u5347\u7b97\u6cd5\u8fd0\u884c\u901f\u5ea6\u7684\u601d\u8def\u79f0\u4e3a\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\uff1b\u53cd\u4e4b\uff0c\u5219\u79f0\u4e3a\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u3002</p> <p>\u9009\u62e9\u54ea\u79cd\u601d\u8def\u53d6\u51b3\u4e8e\u6211\u4eec\u66f4\u770b\u91cd\u54ea\u4e2a\u65b9\u9762\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u65f6\u95f4\u6bd4\u7a7a\u95f4\u66f4\u5b9d\u8d35\uff0c\u56e0\u6b64\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u901a\u5e38\u662f\u66f4\u5e38\u7528\u7684\u7b56\u7565\u3002\u5f53\u7136\uff0c\u5728\u6570\u636e\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u63a7\u5236\u7a7a\u95f4\u590d\u6742\u5ea6\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002</p>"},{"location":"chapter_computational_complexity/summary/","title":"2.4. \u00a0 \u5c0f\u7ed3","text":"<p>\u7b97\u6cd5\u6548\u7387\u8bc4\u4f30</p> <ul> <li>\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u662f\u8bc4\u4ef7\u7b97\u6cd5\u6027\u80fd\u7684\u4e24\u4e2a\u5173\u952e\u7ef4\u5ea6\u3002</li> <li>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9e\u9645\u6d4b\u8bd5\u6765\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\uff0c\u4f46\u96be\u4ee5\u6d88\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5f71\u54cd\uff0c\u4e14\u4f1a\u8017\u8d39\u5927\u91cf\u8ba1\u7b97\u8d44\u6e90\u3002</li> <li>\u590d\u6742\u5ea6\u5206\u6790\u53ef\u4ee5\u514b\u670d\u5b9e\u9645\u6d4b\u8bd5\u7684\u5f0a\u7aef\uff0c\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\uff0c\u5e76\u4e14\u80fd\u591f\u63ed\u793a\u7b97\u6cd5\u5728\u4e0d\u540c\u6570\u636e\u89c4\u6a21\u4e0b\u7684\u6548\u7387\u3002</li> </ul> <p>\u65f6\u95f4\u590d\u6742\u5ea6</p> <ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u6570\u636e\u91cf\u589e\u957f\u7684\u8d8b\u52bf\uff0c\u53ef\u4ee5\u6709\u6548\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u5931\u6548\uff0c\u5982\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u6216\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u540c\u65f6\uff0c\u65e0\u6cd5\u7cbe\u786e\u5bf9\u6bd4\u7b97\u6cd5\u6548\u7387\u7684\u4f18\u52a3\u3002</li> <li>\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4f7f\u7528\u5927 \\(O\\) \u7b26\u53f7\u8868\u793a\uff0c\u5373\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u53cd\u6620\u5f53 \\(n\\) \u8d8b\u5411\u6b63\u65e0\u7a77\u65f6\uff0c\\(T(n)\\) \u7684\u589e\u957f\u7ea7\u522b\u3002</li> <li>\u63a8\u7b97\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u4e24\u6b65\uff0c\u9996\u5148\u7edf\u8ba1\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\uff0c\u7136\u540e\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c\u3002</li> <li>\u5e38\u89c1\u65f6\u95f4\u590d\u6742\u5ea6\u4ece\u5c0f\u5230\u5927\u6392\u5217\u6709 \\(O(1)\\) , \\(O(\\log n)\\) , \\(O(n)\\) , \\(O(n \\log n)\\) , \\(O(n^2)\\) , \\(O(2^n)\\) , \\(O(n!)\\) \u7b49\u3002</li> <li>\u67d0\u4e9b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u975e\u56fa\u5b9a\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u51e0\u4e4e\u4e0d\u7528\uff0c\u56e0\u4e3a\u8f93\u5165\u6570\u636e\u4e00\u822c\u9700\u8981\u6ee1\u8db3\u4e25\u683c\u6761\u4ef6\u624d\u80fd\u8fbe\u5230\u6700\u4f73\u60c5\u51b5\u3002</li> <li>\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u53cd\u6620\u7b97\u6cd5\u5728\u968f\u673a\u6570\u636e\u8f93\u5165\u4e0b\u7684\u8fd0\u884c\u6548\u7387\uff0c\u6700\u63a5\u8fd1\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u7b97\u6cd5\u6027\u80fd\u3002\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u9700\u8981\u7edf\u8ba1\u8f93\u5165\u6570\u636e\u5206\u5e03\u4ee5\u53ca\u7efc\u5408\u540e\u7684\u6570\u5b66\u671f\u671b\u3002</li> </ul> <p>\u7a7a\u95f4\u590d\u6742\u5ea6</p> <ul> <li>\u7c7b\u4f3c\u4e8e\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u5360\u7528\u7a7a\u95f4\u968f\u6570\u636e\u91cf\u589e\u957f\u7684\u8d8b\u52bf\u3002</li> <li>\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u76f8\u5173\u5185\u5b58\u7a7a\u95f4\u53ef\u5206\u4e3a\u8f93\u5165\u7a7a\u95f4\u3001\u6682\u5b58\u7a7a\u95f4\u3001\u8f93\u51fa\u7a7a\u95f4\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u8f93\u5165\u7a7a\u95f4\u4e0d\u8ba1\u5165\u7a7a\u95f4\u590d\u6742\u5ea6\u8ba1\u7b97\u3002\u6682\u5b58\u7a7a\u95f4\u53ef\u5206\u4e3a\u6307\u4ee4\u7a7a\u95f4\u3001\u6570\u636e\u7a7a\u95f4\u3001\u6808\u5e27\u7a7a\u95f4\uff0c\u5176\u4e2d\u6808\u5e27\u7a7a\u95f4\u901a\u5e38\u4ec5\u5728\u9012\u5f52\u51fd\u6570\u4e2d\u5f71\u54cd\u7a7a\u95f4\u590d\u6742\u5ea6\u3002</li> <li>\u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5373\u7edf\u8ba1\u7b97\u6cd5\u5728\u6700\u5dee\u8f93\u5165\u6570\u636e\u548c\u6700\u5dee\u8fd0\u884c\u65f6\u95f4\u70b9\u4e0b\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u3002</li> <li>\u5e38\u89c1\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece\u5c0f\u5230\u5927\u6392\u5217\u6709 \\(O(1)\\) , \\(O(\\log n)\\) , \\(O(n)\\) , \\(O(n^2)\\) , \\(O(2^n)\\) \u7b49\u3002</li> </ul>"},{"location":"chapter_computational_complexity/summary/#241-q-a","title":"2.4.1. \u00a0 Q &amp; A","text":"<p>\u5c3e\u9012\u5f52\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u5417\uff1f</p> <p>\u7406\u8bba\u4e0a\uff0c\u5c3e\u9012\u5f52\u51fd\u6570\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u88ab\u4f18\u5316\u81f3 \\(O(1)\\) \u3002\u4e0d\u8fc7\u7edd\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982 Java, Python, C++, Go, C# \u7b49\uff09\u90fd\u4e0d\u652f\u6301\u81ea\u52a8\u4f18\u5316\u5c3e\u9012\u5f52\uff0c\u56e0\u6b64\u901a\u5e38\u8ba4\u4e3a\u7a7a\u95f4\u590d\u6742\u5ea6\u662f \\(O(n)\\) \u3002</p> <p>\u51fd\u6570\u548c\u65b9\u6cd5\u8fd9\u4e24\u4e2a\u672f\u8bed\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f</p> <p>\u51fd\u6570\uff08function\uff09\u53ef\u4ee5\u72ec\u7acb\u88ab\u6267\u884c\uff0c\u6240\u6709\u53c2\u6570\u90fd\u4ee5\u663e\u5f0f\u4f20\u9012\u3002\u65b9\u6cd5\uff08method\uff09\u4e0e\u4e00\u4e2a\u5bf9\u8c61\u5173\u8054\uff0c\u65b9\u6cd5\u88ab\u9690\u5f0f\u4f20\u9012\u7ed9\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff0c\u65b9\u6cd5\u80fd\u591f\u5bf9\u7c7b\u7684\u5b9e\u4f8b\u4e2d\u5305\u542b\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002</p> <p>\u4ee5\u51e0\u4e2a\u5e38\u89c1\u7684\u7f16\u7a0b\u8bed\u8a00\u4e3a\u4f8b\uff1a</p> <ul> <li>C \u8bed\u8a00\u662f\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\u8bed\u8a00\uff0c\u6ca1\u6709\u9762\u5411\u5bf9\u8c61\u7684\u6982\u5ff5\uff0c\u6240\u4ee5\u53ea\u6709\u51fd\u6570\u3002\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u7ed3\u6784\uff08struct\uff09\u6765\u6a21\u62df\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u4e0e\u7ed3\u6784\u4f53\u76f8\u5173\u8054\u7684\u51fd\u6570\u5c31\u76f8\u5f53\u4e8e\u5176\u4ed6\u8bed\u8a00\u4e2d\u7684\u65b9\u6cd5\u3002</li> <li>Java, C# \u662f\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4ee3\u7801\u5757\uff08\u65b9\u6cd5\uff09\u901a\u5e38\u90fd\u662f\u4f5c\u4e3a\u67d0\u4e2a\u7c7b\u7684\u4e00\u90e8\u5206\u3002\u9759\u6001\u65b9\u6cd5\u7684\u884c\u4e3a\u7c7b\u4f3c\u4e8e\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u88ab\u7ed1\u5b9a\u5728\u7c7b\u4e0a\uff0c\u4e0d\u80fd\u8bbf\u95ee\u7279\u5b9a\u7684\u5b9e\u4f8b\u53d8\u91cf\u3002</li> <li>C++, Python \u65e2\u652f\u6301\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\uff08\u51fd\u6570\uff09\u4e5f\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08\u65b9\u6cd5\uff09\u3002</li> </ul> <p>\u56fe\u7247\u201c\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u89c1\u7c7b\u578b\u201d\u53cd\u6620\u7684\u662f\u5426\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\uff1f</p> <p>\u4e0d\u662f\uff0c\u8be5\u56fe\u7247\u5c55\u793a\u7684\u662f\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5176\u53cd\u6620\u7684\u662f\u5373\u589e\u957f\u8d8b\u52bf\uff0c\u800c\u4e0d\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\u3002</p> <p>\u5047\u8bbe\u53d6 \\(n = 8\\) \uff0c\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u6bcf\u6761\u66f2\u7ebf\u7684\u503c\u4e0e\u51fd\u6570\u5bf9\u5e94\u4e0d\u4e0a\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6761\u66f2\u7ebf\u90fd\u5305\u542b\u4e00\u4e2a\u5e38\u6570\u9879\uff0c\u7528\u4e8e\u5c06\u53d6\u503c\u8303\u56f4\u538b\u7f29\u5230\u4e00\u4e2a\u89c6\u89c9\u8212\u9002\u7684\u8303\u56f4\u5185\u3002</p> <p>\u5728\u5b9e\u9645\u4e2d\uff0c\u56e0\u4e3a\u6211\u4eec\u901a\u5e38\u4e0d\u77e5\u9053\u6bcf\u4e2a\u65b9\u6cd5\u7684\u201c\u5e38\u6570\u9879\u201d\u590d\u6742\u5ea6\u662f\u591a\u5c11\uff0c\u6240\u4ee5\u4e00\u822c\u65e0\u6cd5\u4ec5\u51ed\u590d\u6742\u5ea6\u6765\u9009\u62e9 \\(n = 8\\) \u4e4b\u4e0b\u7684\u6700\u4f18\u89e3\u6cd5\u3002\u4f46\u5bf9\u4e8e \\(n = 8^5\\) \u5c31\u5f88\u597d\u9009\u4e86\uff0c\u8fd9\u65f6\u589e\u957f\u8d8b\u52bf\u5df2\u7ecf\u5360\u4e3b\u5bfc\u4e86\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/","title":"2.2. \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6","text":"<p>\u8fd0\u884c\u65f6\u95f4\u53ef\u4ee5\u76f4\u89c2\u4e14\u51c6\u786e\u5730\u53cd\u6620\u7b97\u6cd5\u7684\u6548\u7387\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u51c6\u786e\u9884\u4f30\u4e00\u6bb5\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u5e94\u8be5\u5982\u4f55\u64cd\u4f5c\u5462\uff1f</p> <ol> <li>\u786e\u5b9a\u8fd0\u884c\u5e73\u53f0\uff0c\u5305\u62ec\u786c\u4ef6\u914d\u7f6e\u3001\u7f16\u7a0b\u8bed\u8a00\u3001\u7cfb\u7edf\u73af\u5883\u7b49\uff0c\u8fd9\u4e9b\u56e0\u7d20\u90fd\u4f1a\u5f71\u54cd\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\u3002</li> <li>\u8bc4\u4f30\u5404\u79cd\u8ba1\u7b97\u64cd\u4f5c\u6240\u9700\u7684\u8fd0\u884c\u65f6\u95f4\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\u6253\u5370\u64cd\u4f5c\u9700\u8981 5 ns \u7b49\u3002</li> <li>\u7edf\u8ba1\u4ee3\u7801\u4e2d\u6240\u6709\u7684\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5e76\u5c06\u6240\u6709\u64cd\u4f5c\u7684\u6267\u884c\u65f6\u95f4\u6c42\u548c\uff0c\u4ece\u800c\u5f97\u5230\u8fd0\u884c\u65f6\u95f4\u3002</li> </ol> <p>\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \u3002\u6839\u636e\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f97\u5230\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u4e3a \\(6n + 12\\) ns \u3002</p> \\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\] JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\nint a = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i &lt; n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nSystem.out.println(0); // 5 ns\n}\n}\n</code></pre> <pre><code>// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\nint a = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i &lt; n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\ncout &lt;&lt; 0 &lt;&lt; endl; // 5 ns\n}\n}\n</code></pre> <pre><code># \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\ndef algorithm(n: int):\na = 2 # 1 ns\na = a + 1 # 1 ns\na = a * 2 # 10 ns\n# \u5faa\u73af n \u6b21\nfor _ in range(n): # 1 ns\nprint(0) # 5 ns\n</code></pre> <pre><code>// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n int) {\na := 2 // 1 ns\na = a + 1 // 1 ns\na = a * 2 // 10 ns\n// \u5faa\u73af n \u6b21\nfor i := 0; i &lt; n; i++ { // 1 ns\nfmt.Println(a) // 5 ns\n}\n}\n</code></pre> <pre><code>// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n) {\nvar a = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor(let i = 0; i &lt; n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nconsole.log(0); // 5 ns\n}\n}\n</code></pre> <pre><code>// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n: number): void {\nvar a: number = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor(let i = 0; i &lt; n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nconsole.log(0); // 5 ns\n}\n}\n</code></pre> <pre><code>// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\nint a = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i &lt; n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nprintf(\"%d\", 0); // 5 ns\n}\n}\n</code></pre> <pre><code>// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\nint a = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i &lt; n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nConsole.WriteLine(0); // 5 ns\n}\n}\n</code></pre> <pre><code>// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n: Int) {\nvar a = 2 // 1 ns\na = a + 1 // 1 ns\na = a * 2 // 10 ns\n// \u5faa\u73af n \u6b21\nfor _ in 0 ..&lt; n { // 1 ns\nprint(0) // 5 ns\n}\n}\n</code></pre> <pre><code>\n</code></pre> <pre><code>// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n) {\nint a = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i &lt; n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nprint(0); // 5 ns\n}\n}\n</code></pre> <pre><code>// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfn algorithm(n: i32) {\nlet mut a = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor _ in 0..n { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nprintln!(\"{}\", 0); // 5 ns\n}\n}\n</code></pre> <p>\u4f46\u5b9e\u9645\u4e0a\uff0c\u7edf\u8ba1\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\u65e2\u4e0d\u5408\u7406\u4e5f\u4e0d\u73b0\u5b9e\u3002\u9996\u5148\uff0c\u6211\u4eec\u4e0d\u5e0c\u671b\u9884\u4f30\u65f6\u95f4\u548c\u8fd0\u884c\u5e73\u53f0\u7ed1\u5b9a\uff0c\u56e0\u4e3a\u7b97\u6cd5\u9700\u8981\u5728\u5404\u79cd\u4e0d\u540c\u7684\u5e73\u53f0\u4e0a\u8fd0\u884c\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5f88\u96be\u83b7\u77e5\u6bcf\u79cd\u64cd\u4f5c\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u8fd9\u7ed9\u9884\u4f30\u8fc7\u7a0b\u5e26\u6765\u4e86\u6781\u5927\u7684\u96be\u5ea6\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#221","title":"2.2.1. \u00a0 \u7edf\u8ba1\u65f6\u95f4\u589e\u957f\u8d8b\u52bf","text":"<p>\u300c\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u300d\u91c7\u53d6\u4e86\u4e00\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u5176\u7edf\u8ba1\u7684\u4e0d\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u800c\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002</p> <p>\u201c\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u201d\u8fd9\u4e2a\u6982\u5ff5\u6bd4\u8f83\u62bd\u8c61\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u52a0\u4ee5\u7406\u89e3\u3002\u5047\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7ed9\u5b9a\u4e09\u4e2a\u7b97\u6cd5\u51fd\u6570 <code>A</code> , <code>B</code> , <code>C</code> \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nSystem.out.println(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i &lt; n; i++) {\nSystem.out.println(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i &lt; 1000000; i++) {\nSystem.out.println(0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\ncout &lt;&lt; 0 &lt;&lt; endl;\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i &lt; n; i++) {\ncout &lt;&lt; 0 &lt;&lt; endl;\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i &lt; 1000000; i++) {\ncout &lt;&lt; 0 &lt;&lt; endl;\n}\n}\n</code></pre> <pre><code># \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_A(n: int):\nprint(0)\n# \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\ndef algorithm_B(n: int):\nfor _ in range(n):\nprint(0)\n# \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_C(n: int):\nfor _ in range(1000000):\nprint(0)\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_A(n int) {\nfmt.Println(0)\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithm_B(n int) {\nfor i := 0; i &lt; n; i++ {\nfmt.Println(0)\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_C(n int) {\nfor i := 0; i &lt; 1000000; i++ {\nfmt.Println(0)\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n) {\nconsole.log(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n) {\nfor (let i = 0; i &lt; n; i++) {\nconsole.log(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n) {\nfor (let i = 0; i &lt; 1000000; i++) {\nconsole.log(0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n: number): void {\nconsole.log(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n: number): void {\nfor (let i = 0; i &lt; n; i++) {\nconsole.log(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n: number): void {\nfor (let i = 0; i &lt; 1000000; i++) {\nconsole.log(0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nprintf(\"%d\", 0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i &lt; n; i++) {\nprintf(\"%d\", 0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i &lt; 1000000; i++) {\nprintf(\"%d\", 0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nConsole.WriteLine(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i &lt; n; i++) {\nConsole.WriteLine(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i &lt; 1000000; i++) {\nConsole.WriteLine(0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmA(n: Int) {\nprint(0)\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithmB(n: Int) {\nfor _ in 0 ..&lt; n {\nprint(0)\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmC(n: Int) {\nfor _ in 0 ..&lt; 1000000 {\nprint(0)\n}\n}\n</code></pre> <pre><code>\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmA(int n) {\nprint(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithmB(int n) {\nfor (int i = 0; i &lt; n; i++) {\nprint(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmC(int n) {\nfor (int i = 0; i &lt; 1000000; i++) {\nprint(0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_A(n: i32) {\nprintln!(\"{}\", 0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfn algorithm_B(n: i32) {\nfor _ in 0..n {\nprintln!(\"{}\", 0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_C(n: i32) {\nfor _ in 0..1000000 {\nprintln!(\"{}\", 0);\n}\n}\n</code></pre> <p>\u7b97\u6cd5 <code>A</code> \u53ea\u6709 \\(1\\) \u4e2a\u6253\u5370\u64cd\u4f5c\uff0c\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u4e0d\u968f\u7740 \\(n\\) \u589e\u5927\u800c\u589e\u957f\u3002\u6211\u4eec\u79f0\u6b64\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u300c\u5e38\u6570\u9636\u300d\u3002</p> <p>\u7b97\u6cd5 <code>B</code> \u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(n\\) \u6b21\uff0c\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740 \\(n\\) \u589e\u5927\u5448\u7ebf\u6027\u589e\u957f\u3002\u6b64\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u88ab\u79f0\u4e3a\u300c\u7ebf\u6027\u9636\u300d\u3002</p> <p>\u7b97\u6cd5 <code>C</code> \u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(1000000\\) \u6b21\uff0c\u867d\u7136\u8fd0\u884c\u65f6\u95f4\u5f88\u957f\uff0c\u4f46\u5b83\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u3002\u56e0\u6b64 <code>C</code> \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c <code>A</code> \u76f8\u540c\uff0c\u4ecd\u4e3a\u300c\u5e38\u6570\u9636\u300d\u3002</p> <p></p> <p> \u56fe\uff1a\u7b97\u6cd5 A, B, C \u7684\u65f6\u95f4\u589e\u957f\u8d8b\u52bf </p> <p>\u76f8\u8f83\u4e8e\u76f4\u63a5\u7edf\u8ba1\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u6709\u54ea\u4e9b\u7279\u70b9\u5462\uff1f</p> <p>\u65f6\u95f4\u590d\u6742\u5ea6\u80fd\u591f\u6709\u6548\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u3002\u4f8b\u5982\uff0c\u7b97\u6cd5 <code>B</code> \u7684\u8fd0\u884c\u65f6\u95f4\u5448\u7ebf\u6027\u589e\u957f\uff0c\u5728 \\(n &gt; 1\\) \u65f6\u6bd4\u7b97\u6cd5 <code>A</code> \u66f4\u6162\uff0c\u5728 \\(n &gt; 1000000\\) \u65f6\u6bd4\u7b97\u6cd5 <code>C</code> \u66f4\u6162\u3002\u4e8b\u5b9e\u4e0a\uff0c\u53ea\u8981\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8db3\u591f\u5927\uff0c\u590d\u6742\u5ea6\u4e3a\u201c\u5e38\u6570\u9636\u201d\u7684\u7b97\u6cd5\u4e00\u5b9a\u4f18\u4e8e\u201c\u7ebf\u6027\u9636\u201d\u7684\u7b97\u6cd5\uff0c\u8fd9\u6b63\u662f\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u6240\u8868\u8fbe\u7684\u542b\u4e49\u3002</p> <p>\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u66f4\u7b80\u4fbf\u3002\u663e\u7136\uff0c\u8fd0\u884c\u5e73\u53f0\u548c\u8ba1\u7b97\u64cd\u4f5c\u7c7b\u578b\u90fd\u4e0e\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u65e0\u5173\u3002\u56e0\u6b64\u5728\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u6240\u6709\u8ba1\u7b97\u64cd\u4f5c\u7684\u6267\u884c\u65f6\u95f4\u89c6\u4e3a\u76f8\u540c\u7684\u201c\u5355\u4f4d\u65f6\u95f4\u201d\uff0c\u4ece\u800c\u5c06\u201c\u8ba1\u7b97\u64cd\u4f5c\u7684\u8fd0\u884c\u65f6\u95f4\u7684\u7edf\u8ba1\u201d\u7b80\u5316\u4e3a\u201c\u8ba1\u7b97\u64cd\u4f5c\u7684\u6570\u91cf\u7684\u7edf\u8ba1\u201d\uff0c\u8fd9\u6837\u4ee5\u6765\u4f30\u7b97\u96be\u5ea6\u5c31\u5927\u5927\u964d\u4f4e\u4e86\u3002</p> <p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u5b58\u5728\u4e00\u5b9a\u7684\u5c40\u9650\u6027\u3002\u4f8b\u5982\uff0c\u5c3d\u7ba1\u7b97\u6cd5 <code>A</code> \u548c <code>C</code> \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u540c\uff0c\u4f46\u5b9e\u9645\u8fd0\u884c\u65f6\u95f4\u5dee\u522b\u5f88\u5927\u3002\u540c\u6837\uff0c\u5c3d\u7ba1\u7b97\u6cd5 <code>B</code> \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u6bd4 <code>C</code> \u9ad8\uff0c\u4f46\u5728\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 <code>B</code> \u660e\u663e\u4f18\u4e8e\u7b97\u6cd5 <code>C</code> \u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5f88\u96be\u4ec5\u51ed\u65f6\u95f4\u590d\u6742\u5ea6\u5224\u65ad\u7b97\u6cd5\u6548\u7387\u9ad8\u4f4e\u3002\u5f53\u7136\uff0c\u5c3d\u7ba1\u5b58\u5728\u4e0a\u8ff0\u95ee\u9898\uff0c\u590d\u6742\u5ea6\u5206\u6790\u4ecd\u7136\u662f\u8bc4\u5224\u7b97\u6cd5\u6548\u7387\u6700\u6709\u6548\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#222","title":"2.2.2. \u00a0 \u51fd\u6570\u6e10\u8fd1\u4e0a\u754c","text":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u51fd\u6570 <code>algorithm()</code> \uff1a</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>void algorithm(int n) {\nint a = 1; // +1\na = a + 1; // +1\na = a * 2; // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i &lt; n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nSystem.out.println(0); // +1\n}\n}\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 1; // +1\na = a + 1; // +1\na = a * 2; // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i &lt; n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\ncout &lt;&lt; 0 &lt;&lt; endl; // +1\n}\n}\n</code></pre> <pre><code>def algorithm(n: int):\na = 1 # +1\na = a + 1 # +1\na = a * 2 # +1\n# \u5faa\u73af n \u6b21\nfor i in range(n): # +1\nprint(0) # +1\n</code></pre> <pre><code>func algorithm(n int) {\na := 1 // +1\na = a + 1 // +1\na = a * 2 // +1\n// \u5faa\u73af n \u6b21\nfor i := 0; i &lt; n; i++ { // +1\nfmt.Println(a) // +1\n}\n}\n</code></pre> <pre><code>function algorithm(n) {\nvar a = 1; // +1\na += 1; // +1\na *= 2; // +1\n// \u5faa\u73af n \u6b21\nfor(let i = 0; i &lt; n; i++){ // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nconsole.log(0); // +1\n}\n}\n</code></pre> <pre><code>function algorithm(n: number): void{\nvar a: number = 1; // +1\na += 1; // +1\na *= 2; // +1\n// \u5faa\u73af n \u6b21\nfor(let i = 0; i &lt; n; i++){ // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nconsole.log(0); // +1\n}\n}\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 1; // +1\na = a + 1; // +1\na = a * 2; // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i &lt; n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nprintf(\"%d\", 0); // +1\n}\n} </code></pre> <pre><code>void algorithm(int n) {\nint a = 1; // +1\na = a + 1; // +1\na = a * 2; // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i &lt; n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nConsole.WriteLine(0); // +1\n}\n}\n</code></pre> <pre><code>func algorithm(n: Int) {\nvar a = 1 // +1\na = a + 1 // +1\na = a * 2 // +1\n// \u5faa\u73af n \u6b21\nfor _ in 0 ..&lt; n { // +1\nprint(0) // +1\n}\n}\n</code></pre> <pre><code>\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 1; // +1\na = a + 1; // +1\na = a * 2; // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i &lt; n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nprint(0); // +1\n}\n}\n</code></pre> <pre><code>fn algorithm(n: i32) {\nlet mut a = 1; // +1\na = a + 1; // +1\na = a * 2; // +1\n// \u5faa\u73af n \u6b21\nfor _ in 0..n { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nprintln!(\"{}\", 0); // +1\n}\n}\n</code></pre> <p>\u8bbe\u7b97\u6cd5\u7684\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\u662f\u4e00\u4e2a\u5173\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u7684\u51fd\u6570\uff0c\u8bb0\u4e3a \\(T(n)\\) \uff0c\u5219\u4ee5\u4e0a\u51fd\u6570\u7684\u7684\u64cd\u4f5c\u6570\u91cf\u4e3a\uff1a</p> \\[ T(n) = 3 + 2n \\] <p>\\(T(n)\\) \u662f\u4e00\u6b21\u51fd\u6570\uff0c\u8bf4\u660e\u65f6\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u662f\u7ebf\u6027\u7684\uff0c\u56e0\u6b64\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u7ebf\u6027\u9636\u3002</p> <p>\u6211\u4eec\u5c06\u7ebf\u6027\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8bb0\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4e2a\u6570\u5b66\u7b26\u53f7\u79f0\u4e3a\u300c\u5927 \\(O\\) \u8bb0\u53f7 Big-\\(O\\) Notation\u300d\uff0c\u8868\u793a\u51fd\u6570 \\(T(n)\\) \u7684\u300c\u6e10\u8fd1\u4e0a\u754c Asymptotic Upper Bound\u300d\u3002</p> <p>\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u672c\u8d28\u4e0a\u662f\u8ba1\u7b97\u201c\u64cd\u4f5c\u6570\u91cf\u51fd\u6570 \\(T(n)\\)\u201d\u7684\u6e10\u8fd1\u4e0a\u754c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u6765\u770b\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\u7684\u6570\u5b66\u5b9a\u4e49\u3002</p> <p>\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c</p> <p>\u82e5\u5b58\u5728\u6b63\u5b9e\u6570 \\(c\\) \u548c\u5b9e\u6570 \\(n_0\\) \uff0c\u4f7f\u5f97\u5bf9\u4e8e\u6240\u6709\u7684 \\(n &gt; n_0\\) \uff0c\u5747\u6709 $$ T(n) \\leq c \\cdot f(n) $$ \u5219\u53ef\u8ba4\u4e3a \\(f(n)\\) \u7ed9\u51fa\u4e86 \\(T(n)\\) \u7684\u4e00\u4e2a\u6e10\u8fd1\u4e0a\u754c\uff0c\u8bb0\u4e3a $$ T(n) = O(f(n)) $$</p> <p></p> <p> \u56fe\uff1a\u51fd\u6570\u7684\u6e10\u8fd1\u4e0a\u754c </p> <p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u8ba1\u7b97\u6e10\u8fd1\u4e0a\u754c\u5c31\u662f\u5bfb\u627e\u4e00\u4e2a\u51fd\u6570 \\(f(n)\\) \uff0c\u4f7f\u5f97\u5f53 \\(n\\) \u8d8b\u5411\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\\(T(n)\\) \u548c \\(f(n)\\) \u5904\u4e8e\u76f8\u540c\u7684\u589e\u957f\u7ea7\u522b\uff0c\u4ec5\u76f8\u5dee\u4e00\u4e2a\u5e38\u6570\u9879 \\(c\\) \u7684\u500d\u6570\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#223","title":"2.2.3. \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"<p>\u6e10\u8fd1\u4e0a\u754c\u7684\u6570\u5b66\u5473\u513f\u6709\u70b9\u91cd\uff0c\u5982\u679c\u4f60\u611f\u89c9\u6ca1\u6709\u5b8c\u5168\u7406\u89e3\uff0c\u4e5f\u65e0\u9700\u62c5\u5fc3\u3002\u56e0\u4e3a\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u638c\u63e1\u63a8\u7b97\u65b9\u6cd5\uff0c\u6570\u5b66\u610f\u4e49\u53ef\u4ee5\u9010\u6e10\u9886\u609f\u3002</p> <p>\u6839\u636e\u5b9a\u4e49\uff0c\u786e\u5b9a \\(f(n)\\) \u4e4b\u540e\uff0c\u6211\u4eec\u4fbf\u53ef\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(f(n))\\) \u3002\u90a3\u4e48\u5982\u4f55\u786e\u5b9a\u6e10\u8fd1\u4e0a\u754c \\(f(n)\\) \u5462\uff1f\u603b\u4f53\u5206\u4e3a\u4e24\u6b65\uff1a\u9996\u5148\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\uff0c\u7136\u540e\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#_1","title":"\u7b2c\u4e00\u6b65\uff1a\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf","text":"<p>\u9488\u5bf9\u4ee3\u7801\uff0c\u9010\u884c\u4ece\u4e0a\u5230\u4e0b\u8ba1\u7b97\u5373\u53ef\u3002\u7136\u800c\uff0c\u7531\u4e8e\u4e0a\u8ff0 \\(c \\cdot f(n)\\) \u4e2d\u7684\u5e38\u6570\u9879 \\(c\\) \u53ef\u4ee5\u53d6\u4efb\u610f\u5927\u5c0f\uff0c\u56e0\u6b64\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u4e2d\u7684\u5404\u79cd\u7cfb\u6570\u3001\u5e38\u6570\u9879\u90fd\u53ef\u4ee5\u88ab\u5ffd\u7565\u3002\u6839\u636e\u6b64\u539f\u5219\uff0c\u53ef\u4ee5\u603b\u7ed3\u51fa\u4ee5\u4e0b\u8ba1\u6570\u7b80\u5316\u6280\u5de7\uff1a</p> <ol> <li>\u5ffd\u7565 \\(T(n)\\) \u4e2d\u7684\u5e38\u6570\u9879\u3002\u56e0\u4e3a\u5b83\u4eec\u90fd\u4e0e \\(n\\) \u65e0\u5173\uff0c\u6240\u4ee5\u5bf9\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u4ea7\u751f\u5f71\u54cd\u3002</li> <li>\u7701\u7565\u6240\u6709\u7cfb\u6570\u3002\u4f8b\u5982\uff0c\u5faa\u73af \\(2n\\) \u6b21\u3001\\(5n + 1\\) \u6b21\u7b49\uff0c\u90fd\u53ef\u4ee5\u7b80\u5316\u8bb0\u4e3a \\(n\\) \u6b21\uff0c\u56e0\u4e3a \\(n\\) \u524d\u9762\u7684\u7cfb\u6570\u5bf9\u65f6\u95f4\u590d\u6742\u5ea6\u6ca1\u6709\u5f71\u54cd\u3002</li> <li>\u5faa\u73af\u5d4c\u5957\u65f6\u4f7f\u7528\u4e58\u6cd5\u3002\u603b\u64cd\u4f5c\u6570\u91cf\u7b49\u4e8e\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u64cd\u4f5c\u6570\u91cf\u4e4b\u79ef\uff0c\u6bcf\u4e00\u5c42\u5faa\u73af\u4f9d\u7136\u53ef\u4ee5\u5206\u522b\u5957\u7528\u4e0a\u8ff0 <code>1.</code> \u548c <code>2.</code> \u6280\u5de7\u3002</li> </ol> <p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u4f7f\u7528\u4e0a\u8ff0\u6280\u5de7\u524d\u3001\u540e\u7684\u7edf\u8ba1\u7ed3\u679c\u3002\u4e24\u8005\u63a8\u51fa\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u540c\uff0c\u5373\u4e3a \\(O(n^2)\\) \u3002</p> \\[ \\begin{aligned} T(n) &amp; = 2n(n + 1) + (5n + 1) + 2 &amp; \\text{\u5b8c\u6574\u7edf\u8ba1 (-.-|||)} \\newline &amp; = 2n^2 + 7n + 3 \\newline T(n) &amp; = n^2 + n &amp; \\text{\u5077\u61d2\u7edf\u8ba1 (o.O)} \\end{aligned} \\] JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>void algorithm(int n) {\nint a = 1; // +0\uff08\u6280\u5de7 1\uff09\na = a + n; // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i &lt; 5 * n + 1; i++) {\nSystem.out.println(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i &lt; 2 * n; i++) {\nfor (int j = 0; j &lt; n + 1; j++) {\nSystem.out.println(0);\n}\n}\n}\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 1; // +0\uff08\u6280\u5de7 1\uff09\na = a + n; // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i &lt; 5 * n + 1; i++) {\ncout &lt;&lt; 0 &lt;&lt; endl;\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i &lt; 2 * n; i++) {\nfor (int j = 0; j &lt; n + 1; j++) {\ncout &lt;&lt; 0 &lt;&lt; endl;\n}\n}\n}\n</code></pre> <pre><code>def algorithm(n: int):\na = 1 # +0\uff08\u6280\u5de7 1\uff09\na = a + n # +0\uff08\u6280\u5de7 1\uff09\n# +n\uff08\u6280\u5de7 2\uff09\nfor i in range(5 * n + 1):\nprint(0)\n# +n*n\uff08\u6280\u5de7 3\uff09\nfor i in range(2 * n):\nfor j in range(n + 1):\nprint(0)\n</code></pre> <pre><code>func algorithm(n int) {\na := 1 // +0\uff08\u6280\u5de7 1\uff09\na = a + n // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor i := 0; i &lt; 5 * n + 1; i++ {\nfmt.Println(0)\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor i := 0; i &lt; 2 * n; i++ {\nfor j := 0; j &lt; n + 1; j++ {\nfmt.Println(0)\n}\n}\n}\n</code></pre> <pre><code>function algorithm(n) {\nlet a = 1; // +0\uff08\u6280\u5de7 1\uff09\na = a + n; // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (let i = 0; i &lt; 5 * n + 1; i++) {\nconsole.log(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (let i = 0; i &lt; 2 * n; i++) {\nfor (let j = 0; j &lt; n + 1; j++) {\nconsole.log(0);\n}\n}\n}\n</code></pre> <pre><code>function algorithm(n: number): void {\nlet a = 1; // +0\uff08\u6280\u5de7 1\uff09\na = a + n; // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (let i = 0; i &lt; 5 * n + 1; i++) {\nconsole.log(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (let i = 0; i &lt; 2 * n; i++) {\nfor (let j = 0; j &lt; n + 1; j++) {\nconsole.log(0);\n}\n}\n}\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 1; // +0\uff08\u6280\u5de7 1\uff09\na = a + n; // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i &lt; 5 * n + 1; i++) {\nprintf(\"%d\", 0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i &lt; 2 * n; i++) {\nfor (int j = 0; j &lt; n + 1; j++) {\nprintf(\"%d\", 0);\n}\n}\n}\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 1; // +0\uff08\u6280\u5de7 1\uff09\na = a + n; // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i &lt; 5 * n + 1; i++) {\nConsole.WriteLine(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i &lt; 2 * n; i++) {\nfor (int j = 0; j &lt; n + 1; j++) {\nConsole.WriteLine(0);\n}\n}\n}\n</code></pre> <pre><code>func algorithm(n: Int) {\nvar a = 1 // +0\uff08\u6280\u5de7 1\uff09\na = a + n // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor _ in 0 ..&lt; (5 * n + 1) {\nprint(0)\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor _ in 0 ..&lt; (2 * n) {\nfor _ in 0 ..&lt; (n + 1) {\nprint(0)\n}\n}\n}\n</code></pre> <pre><code>\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 1; // +0\uff08\u6280\u5de7 1\uff09\na = a + n; // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i &lt; 5 * n + 1; i++) {\nprint(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i &lt; 2 * n; i++) {\nfor (int j = 0; j &lt; n + 1; j++) {\nprint(0);\n}\n}\n}\n</code></pre> <pre><code>fn algorithm(n: i32) {\nlet mut a = 1; // +0\uff08\u6280\u5de7 1\uff09\na = a + n; // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor i in 0..(5 * n + 1) {\nprintln!(\"{}\", 0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor i in 0..(2 * n) {\nfor j in 0..(n + 1) {\nprintln!(\"{}\", 0);\n}\n}\n}\n</code></pre>"},{"location":"chapter_computational_complexity/time_complexity/#_2","title":"\u7b2c\u4e8c\u6b65\uff1a\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6\u7531\u591a\u9879\u5f0f \\(T(n)\\) \u4e2d\u6700\u9ad8\u9636\u7684\u9879\u6765\u51b3\u5b9a\u3002\u8fd9\u662f\u56e0\u4e3a\u5728 \\(n\\) \u8d8b\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\u6700\u9ad8\u9636\u7684\u9879\u5c06\u53d1\u6325\u4e3b\u5bfc\u4f5c\u7528\uff0c\u5176\u4ed6\u9879\u7684\u5f71\u54cd\u90fd\u53ef\u4ee5\u88ab\u5ffd\u7565\u3002</p> <p>\u4ee5\u4e0b\u8868\u683c\u5c55\u793a\u4e86\u4e00\u4e9b\u4f8b\u5b50\uff0c\u5176\u4e2d\u4e00\u4e9b\u5938\u5f20\u7684\u503c\u662f\u4e3a\u4e86\u5f3a\u8c03\u201c\u7cfb\u6570\u65e0\u6cd5\u64bc\u52a8\u9636\u6570\u201d\u8fd9\u4e00\u7ed3\u8bba\u3002\u5f53 \\(n\\) \u8d8b\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\u8fd9\u4e9b\u5e38\u6570\u53d8\u5f97\u65e0\u8db3\u8f7b\u91cd\u3002</p> <p> \u8868\uff1a\u591a\u9879\u5f0f\u65f6\u95f4\u590d\u6742\u5ea6\u793a\u4f8b </p> \u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u65f6\u95f4\u590d\u6742\u5ea6 \\(O(f(n))\\) \\(100000\\) \\(O(1)\\) \\(3n + 2\\) \\(O(n)\\) \\(2n^2 + 3n + 2\\) \\(O(n^2)\\) \\(n^3 + 10000n^2\\) \\(O(n^3)\\) \\(2^n + 10000n^{10000}\\) \\(O(2^n)\\)"},{"location":"chapter_computational_complexity/time_complexity/#224","title":"2.2.4. \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"<p>\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u5e38\u89c1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u5305\u62ec\uff08\u6309\u7167\u4ece\u4f4e\u5230\u9ad8\u7684\u987a\u5e8f\u6392\u5217\uff09\uff1a</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\u6570\u9636} &lt; \\text{\u5bf9\u6570\u9636} &lt; \\text{\u7ebf\u6027\u9636} &lt; \\text{\u7ebf\u6027\u5bf9\u6570\u9636} &lt; \\text{\u5e73\u65b9\u9636} &lt; \\text{\u6307\u6570\u9636} &lt; \\text{\u9636\u4e58\u9636} \\end{aligned} \\] <p></p> <p> \u56fe\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u89c1\u7c7b\u578b </p> <p>Tip</p> <p>\u90e8\u5206\u793a\u4f8b\u4ee3\u7801\u9700\u8981\u4e00\u4e9b\u9884\u5907\u77e5\u8bc6\uff0c\u5305\u62ec\u6570\u7ec4\u3001\u9012\u5f52\u7b49\u3002\u5982\u679c\u4f60\u9047\u5230\u4e0d\u7406\u89e3\u7684\u90e8\u5206\uff0c\u53ef\u4ee5\u5728\u5b66\u4e60\u5b8c\u540e\u9762\u7ae0\u8282\u540e\u518d\u56de\u987e\u3002\u73b0\u9636\u6bb5\uff0c\u8bf7\u5148\u4e13\u6ce8\u4e8e\u7406\u89e3\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u542b\u4e49\u548c\u63a8\u7b97\u65b9\u6cd5\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#o1","title":"\u5e38\u6570\u9636 \\(O(1)\\)","text":"<p>\u5e38\u6570\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u5373\u4e0d\u968f\u7740 \\(n\\) \u7684\u53d8\u5316\u800c\u53d8\u5316\u3002</p> <p>\u5bf9\u4e8e\u4ee5\u4e0b\u7b97\u6cd5\uff0c\u5c3d\u7ba1\u64cd\u4f5c\u6570\u91cf <code>size</code> \u53ef\u80fd\u5f88\u5927\uff0c\u4f46\u7531\u4e8e\u5176\u4e0e\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust time_complexity.java<pre><code>/* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nfor (int i = 0; i &lt; size; i++)\ncount++;\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nfor (int i = 0; i &lt; size; i++)\ncount++;\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>def constant(n: int) -&gt; int:\n\"\"\"\u5e38\u6570\u9636\"\"\"\ncount = 0\nsize = 100000\nfor _ in range(size):\ncount += 1\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u5e38\u6570\u9636 */\nfunc constant(n int) int {\ncount := 0\nsize := 100000\nfor i := 0; i &lt; size; i++ {\ncount++\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u5e38\u6570\u9636 */\nfunction constant(n) {\nlet count = 0;\nconst size = 100000;\nfor (let i = 0; i &lt; size; i++) count++;\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5e38\u6570\u9636 */\nfunction constant(n: number): number {\nlet count = 0;\nconst size = 100000;\nfor (let i = 0; i &lt; size; i++) count++;\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>/* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nint i = 0;\nfor (int i = 0; i &lt; size; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nfor (int i = 0; i &lt; size; i++)\ncount++;\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5e38\u6570\u9636 */\nfunc constant(n: Int) -&gt; Int {\nvar count = 0\nlet size = 100_000\nfor _ in 0 ..&lt; size {\ncount += 1\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u5e38\u6570\u9636\nfn constant(n: i32) i32 {\n_ = n;\nvar count: i32 = 0;\nconst size: i32 = 100_000;\nvar i: i32 = 0;\nwhile(i&lt;size) : (i += 1) {\ncount += 1;\n}\nreturn count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nfor (var i = 0; i &lt; size; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u5e38\u6570\u9636 */\nfn constant(n: i32) -&gt; i32 {\n_ = n;\nlet mut count = 0;\nlet size = 100_000;\nfor _ in 0..size {\ncount += 1;\n}\ncount\n}\n</code></pre>"},{"location":"chapter_computational_complexity/time_complexity/#on","title":"\u7ebf\u6027\u9636 \\(O(n)\\)","text":"<p>\u7ebf\u6027\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4ee5\u7ebf\u6027\u7ea7\u522b\u589e\u957f\u3002\u7ebf\u6027\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5355\u5c42\u5faa\u73af\u4e2d\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust time_complexity.java<pre><code>/* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (int i = 0; i &lt; n; i++)\ncount++;\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (int i = 0; i &lt; n; i++)\ncount++;\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>def linear(n: int) -&gt; int:\n\"\"\"\u7ebf\u6027\u9636\"\"\"\ncount = 0\nfor _ in range(n):\ncount += 1\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u7ebf\u6027\u9636 */\nfunc linear(n int) int {\ncount := 0\nfor i := 0; i &lt; n; i++ {\ncount++\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u7ebf\u6027\u9636 */\nfunction linear(n) {\nlet count = 0;\nfor (let i = 0; i &lt; n; i++) count++;\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u7ebf\u6027\u9636 */\nfunction linear(n: number): number {\nlet count = 0;\nfor (let i = 0; i &lt; n; i++) count++;\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>/* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (int i = 0; i &lt; n; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (int i = 0; i &lt; n; i++)\ncount++;\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) -&gt; Int {\nvar count = 0\nfor _ in 0 ..&lt; n {\ncount += 1\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u7ebf\u6027\u9636\nfn linear(n: i32) i32 {\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i &lt; n) : (i += 1) {\ncount += 1;\n}\nreturn count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (var i = 0; i &lt; n; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u7ebf\u6027\u9636 */\nfn linear(n: i32) -&gt; i32 {\nlet mut count = 0;\nfor _ in 0..n {\ncount += 1;\n}\ncount\n}\n</code></pre> <p>\u904d\u5386\u6570\u7ec4\u548c\u904d\u5386\u94fe\u8868\u7b49\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u6216\u94fe\u8868\u7684\u957f\u5ea6\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust time_complexity.java<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int[] nums) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (int num : nums) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(vector&lt;int&gt; &amp;nums) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (int num : nums) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>def array_traversal(nums: list[int]) -&gt; int:\n\"\"\"\u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\"\"\"\ncount = 0\n# \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor num in nums:\ncount += 1\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums []int) int {\ncount := 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor range nums {\ncount++\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums) {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (let i = 0; i &lt; nums.length; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums: number[]): number {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (let i = 0; i &lt; nums.length; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int *nums, int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (int i = 0; i &lt; n; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int[] nums) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nforeach (int num in nums) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums: [Int]) -&gt; Int {\nvar count = 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor _ in nums {\ncount += 1\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\nfn arrayTraversal(nums: []i32) i32 {\nvar count: i32 = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (nums) |_| {\ncount += 1;\n}\nreturn count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(List&lt;int&gt; nums) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (var num in nums) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfn array_traversal(nums: &amp;[i32]) -&gt; i32 {\nlet mut count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor _ in nums {\ncount += 1;\n}\ncount\n}\n</code></pre> <p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6570\u636e\u5927\u5c0f \\(n\\) \u9700\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\u6765\u5177\u4f53\u786e\u5b9a\u3002\u6bd4\u5982\u5728\u7b2c\u4e00\u4e2a\u793a\u4f8b\u4e2d\uff0c\u53d8\u91cf \\(n\\) \u4e3a\u8f93\u5165\u6570\u636e\u5927\u5c0f\uff1b\u5728\u7b2c\u4e8c\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u4e3a\u6570\u636e\u5927\u5c0f\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#on2","title":"\u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"<p>\u5e73\u65b9\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4ee5\u5e73\u65b9\u7ea7\u522b\u589e\u957f\u3002\u5e73\u65b9\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u90fd\u4e3a \\(O(n)\\) \uff0c\u56e0\u6b64\u603b\u4f53\u4e3a \\(O(n^2)\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust time_complexity.java<pre><code>/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i &lt; n; i++) {\nfor (int j = 0; j &lt; n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i &lt; n; i++) {\nfor (int j = 0; j &lt; n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>def quadratic(n: int) -&gt; int:\n\"\"\"\u5e73\u65b9\u9636\"\"\"\ncount = 0\n# \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor i in range(n):\nfor j in range(n):\ncount += 1\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u5e73\u65b9\u9636 */\nfunc quadratic(n int) int {\ncount := 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor i := 0; i &lt; n; i++ {\nfor j := 0; j &lt; n; j++ {\ncount++\n}\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (let i = 0; i &lt; n; i++) {\nfor (let j = 0; j &lt; n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): number {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (let i = 0; i &lt; n; i++) {\nfor (let j = 0; j &lt; n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i &lt; n; i++) {\nfor (int j = 0; j &lt; n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i &lt; n; i++) {\nfor (int j = 0; j &lt; n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) -&gt; Int {\nvar count = 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor _ in 0 ..&lt; n {\nfor _ in 0 ..&lt; n {\ncount += 1\n}\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u5e73\u65b9\u9636\nfn quadratic(n: i32) i32 {\nvar count: i32 = 0;\nvar i: i32 = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nwhile (i &lt; n) : (i += 1) {\nvar j: i32 = 0;\nwhile (j &lt; n) : (j += 1) {\ncount += 1;\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i &lt; n; i++) {\nfor (int j = 0; j &lt; n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u5e73\u65b9\u9636 */\nfn quadratic(n: i32) -&gt; i32 {\nlet mut count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor _ in 0..n {\nfor _ in 0..n {\ncount += 1;\n}\n}\ncount\n}\n</code></pre> <p></p> <p> \u56fe\uff1a\u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u3001\u5e73\u65b9\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6 </p> <p>\u4ee5\u300c\u5192\u6ce1\u6392\u5e8f\u300d\u4e3a\u4f8b\uff0c\u5916\u5c42\u5faa\u73af\u6267\u884c \\(n - 1\\) \u6b21\uff0c\u5185\u5c42\u5faa\u73af\u6267\u884c \\(n-1, n-2, \\cdots, 2, 1\\) \u6b21\uff0c\u5e73\u5747\u4e3a \\(\\frac{n}{2}\\) \u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002</p> \\[ O((n - 1) \\frac{n}{2}) = O(n^2) \\] JavaC++PythonGoJSTSCC#SwiftZigDartRust time_complexity.java<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (int i = nums.length - 1; i &gt; 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (int j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(vector&lt;int&gt; &amp;nums) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (int i = nums.size() - 1; i &gt; 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (int j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>def bubble_sort(nums: list[int]) -&gt; int:\n\"\"\"\u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\"\"\"\ncount = 0 # \u8ba1\u6570\u5668\n# \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in range(len(nums) - 1, 0, -1):\n# \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor j in range(i):\nif nums[j] &gt; nums[j + 1]:\n# \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\ntmp: int = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\ncount += 3 # \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums []int) int {\ncount := 0 // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i := len(nums) - 1; i &gt; 0; i-- {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\nfor j := 0; j &lt; i; j++ {\nif nums[j] &gt; nums[j+1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\ntmp := nums[j]\nnums[j] = nums[j+1]\nnums[j+1] = tmp\ncount += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums) {\nlet count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (let i = nums.length - 1; i &gt; 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (let j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums: number[]): number {\nlet count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (let i = nums.length - 1; i &gt; 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (let j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int *nums, int n) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (int i = n - 1; i &gt; 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (int j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (int i = nums.Length - 1; i &gt; 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (int j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums: inout [Int]) -&gt; Int {\nvar count = 0 // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in stride(from: nums.count - 1, to: 0, by: -1) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor j in 0 ..&lt; i {\nif nums[j] &gt; nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\ncount += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\nfn bubbleSort(nums: []i32) i32 {\nvar count: i32 = 0; // \u8ba1\u6570\u5668 \n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nvar i: i32 = @as(i32, @intCast(nums.len)) - 1;\nwhile (i &gt; 0) : (i -= 1) {\nvar j: usize = 0;\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nwhile (j &lt; i) : (j += 1) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nvar tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(List&lt;int&gt; nums) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (var i = nums.length - 1; i &gt; 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\nfor (var j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfn bubble_sort(nums: &amp;mut [i32]) -&gt; i32 {\nlet mut count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in (1..nums.len()).rev() {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor j in 0..i {\nif nums[j] &gt; nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\ncount\n}\n</code></pre>"},{"location":"chapter_computational_complexity/time_complexity/#o2n","title":"\u6307\u6570\u9636 \\(O(2^n)\\)","text":"<p>\u751f\u7269\u5b66\u7684\u201c\u7ec6\u80de\u5206\u88c2\u201d\u662f\u6307\u6570\u9636\u589e\u957f\u7684\u5178\u578b\u4f8b\u5b50\uff1a\u521d\u59cb\u72b6\u6001\u4e3a \\(1\\) \u4e2a\u7ec6\u80de\uff0c\u5206\u88c2\u4e00\u8f6e\u540e\u53d8\u4e3a \\(2\\) \u4e2a\uff0c\u5206\u88c2\u4e24\u8f6e\u540e\u53d8\u4e3a \\(4\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5206\u88c2 \\(n\\) \u8f6e\u540e\u6709 \\(2^n\\) \u4e2a\u7ec6\u80de\u3002</p> <p>\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u7ec6\u80de\u5206\u88c2\u7684\u8fc7\u7a0b\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust time_complexity.java<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i &lt; n; i++) {\nfor (int j = 0; j &lt; base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i &lt; n; i++) {\nfor (int j = 0; j &lt; base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>def exponential(n: int) -&gt; int:\n\"\"\"\u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\ncount = 0\nbase = 1\n# \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor _ in range(n):\nfor _ in range(base):\ncount += 1\nbase *= 2\n# count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc exponential(n int) int {\ncount, base := 0, 1\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor i := 0; i &lt; n; i++ {\nfor j := 0; j &lt; base; j++ {\ncount++\n}\nbase *= 2\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n) {\nlet count = 0,\nbase = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (let i = 0; i &lt; n; i++) {\nfor (let j = 0; j &lt; base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n: number): number {\nlet count = 0,\nbase = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (let i = 0; i &lt; n; i++) {\nfor (let j = 0; j &lt; base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0;\nint bas = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i &lt; n; i++) {\nfor (int j = 0; j &lt; bas; j++) {\ncount++;\n}\nbas *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, bas = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i &lt; n; i++) {\nfor (int j = 0; j &lt; bas; j++) {\ncount++;\n}\nbas *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc exponential(n: Int) -&gt; Int {\nvar count = 0\nvar base = 1\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor _ in 0 ..&lt; n {\nfor _ in 0 ..&lt; base {\ncount += 1\n}\nbase *= 2\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn exponential(n: i32) i32 {\nvar count: i32 = 0;\nvar bas: i32 = 1;\nvar i: i32 = 0;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nwhile (i &lt; n) : (i += 1) {\nvar j: i32 = 0;\nwhile (j &lt; bas) : (j += 1) {\ncount += 1;\n}\nbas *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (var i = 0; i &lt; n; i++) {\nfor (var j = 0; j &lt; base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn exponential(n: i32) -&gt; i32 {\nlet mut count = 0;\nlet mut base = 1;\n// \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor _ in 0..n {\nfor _ in 0..base {\ncount += 1\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\ncount\n}\n</code></pre> <p></p> <p> \u56fe\uff1a\u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6 </p> <p>\u5728\u5b9e\u9645\u7b97\u6cd5\u4e2d\uff0c\u6307\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5176\u9012\u5f52\u5730\u4e00\u5206\u4e3a\u4e8c\uff0c\u7ecf\u8fc7 \\(n\\) \u6b21\u5206\u88c2\u540e\u505c\u6b62\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust time_complexity.java<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1)\nreturn 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1)\nreturn 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.py<pre><code>def exp_recur(n: int) -&gt; int:\n\"\"\"\u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nif n == 1:\nreturn 1\nreturn exp_recur(n - 1) + exp_recur(n - 1) + 1\n</code></pre> time_complexity.go<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc expRecur(n int) int {\nif n == 1 {\nreturn 1\n}\nreturn expRecur(n-1) + expRecur(n-1) + 1\n}\n</code></pre> time_complexity.js<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n) {\nif (n === 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n: number): number {\nif (n === 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.c<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1)\nreturn 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc expRecur(n: Int) -&gt; Int {\nif n == 1 {\nreturn 1\n}\nreturn expRecur(n: n - 1) + expRecur(n: n - 1) + 1\n}\n</code></pre> time_complexity.zig<pre><code>// \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn expRecur(n: i32) i32 {\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn exp_recur(n: i32) -&gt; i32 {\nif n == 1 {\nreturn 1;\n}\nexp_recur(n - 1) + exp_recur(n - 1) + 1\n}\n</code></pre> <p>\u6307\u6570\u9636\u589e\u957f\u975e\u5e38\u8fc5\u901f\uff0c\u5728\u7a77\u4e3e\u6cd5\uff08\u66b4\u529b\u641c\u7d22\u3001\u56de\u6eaf\u7b49\uff09\u4e2d\u6bd4\u8f83\u5e38\u89c1\u3002\u5bf9\u4e8e\u6570\u636e\u89c4\u6a21\u8f83\u5927\u7684\u95ee\u9898\uff0c\u6307\u6570\u9636\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u300c\u52a8\u6001\u89c4\u5212\u300d\u6216\u300c\u8d2a\u5fc3\u300d\u7b49\u7b97\u6cd5\u6765\u89e3\u51b3\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#olog-n","title":"\u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"<p>\u4e0e\u6307\u6570\u9636\u76f8\u53cd\uff0c\u5bf9\u6570\u9636\u53cd\u6620\u4e86\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u201d\u7684\u60c5\u51b5\u3002\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7531\u4e8e\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\uff0c\u56e0\u6b64\u5faa\u73af\u6b21\u6570\u662f \\(\\log_2 n\\) \uff0c\u5373 \\(2^n\\) \u7684\u53cd\u51fd\u6570\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust time_complexity.java<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\nint count = 0;\nwhile (n &gt; 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\nint count = 0;\nwhile (n &gt; 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>def logarithmic(n: float) -&gt; int:\n\"\"\"\u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\ncount = 0\nwhile n &gt; 1:\nn = n / 2\ncount += 1\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc logarithmic(n float64) int {\ncount := 0\nfor n &gt; 1 {\nn = n / 2\ncount++\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n) {\nlet count = 0;\nwhile (n &gt; 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n: number): number {\nlet count = 0;\nwhile (n &gt; 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\nint count = 0;\nwhile (n &gt; 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\nint count = 0;\nwhile (n &gt; 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc logarithmic(n: Double) -&gt; Int {\nvar count = 0\nvar n = n\nwhile n &gt; 1 {\nn = n / 2\ncount += 1\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn logarithmic(n: f32) i32 {\nvar count: i32 = 0;\nvar n_var = n;\nwhile (n_var &gt; 1)\n{\nn_var = n_var / 2;\ncount +=1;\n}\nreturn count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(num n) {\nint count = 0;\nwhile (n &gt; 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn logarithmic(mut n: f32) -&gt; i32 {\nlet mut count = 0;\nwhile n &gt; 1.0 {\nn = n / 2.0;\ncount += 1;\n}\ncount\n}\n</code></pre> <p></p> <p> \u56fe\uff1a\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6 </p> <p>\u4e0e\u6307\u6570\u9636\u7c7b\u4f3c\uff0c\u5bf9\u6570\u9636\u4e5f\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u3002\u4ee5\u4e0b\u4ee3\u7801\u5f62\u6210\u4e86\u4e00\u4e2a\u9ad8\u5ea6\u4e3a \\(\\log_2 n\\) \u7684\u9012\u5f52\u6811\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust time_complexity.java<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\nif (n &lt;= 1)\nreturn 0;\nreturn logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\nif (n &lt;= 1)\nreturn 0;\nreturn logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.py<pre><code>def log_recur(n: float) -&gt; int:\n\"\"\"\u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nif n &lt;= 1:\nreturn 0\nreturn log_recur(n / 2) + 1\n</code></pre> time_complexity.go<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc logRecur(n float64) int {\nif n &lt;= 1 {\nreturn 0\n}\nreturn logRecur(n/2) + 1\n}\n</code></pre> time_complexity.js<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n) {\nif (n &lt;= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n: number): number {\nif (n &lt;= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.c<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\nif (n &lt;= 1)\nreturn 0;\nreturn logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\nif (n &lt;= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc logRecur(n: Double) -&gt; Int {\nif n &lt;= 1 {\nreturn 0\n}\nreturn logRecur(n: n / 2) + 1\n}\n</code></pre> time_complexity.zig<pre><code>// \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn logRecur(n: f32) i32 {\nif (n &lt;= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(num n) {\nif (n &lt;= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn log_recur(n: f32) -&gt; i32 {\nif n &lt;= 1.0 {\nreturn 0;\n}\nlog_recur(n / 2.0) + 1\n}\n</code></pre> <p>\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u57fa\u4e8e\u300c\u5206\u6cbb\u300d\u7684\u7b97\u6cd5\u4e2d\uff0c\u4f53\u73b0\u4e86\u201c\u4e00\u5206\u4e3a\u591a\u201d\u548c\u201c\u5316\u7e41\u4e3a\u7b80\u201d\u7684\u7b97\u6cd5\u601d\u60f3\u3002\u5b83\u589e\u957f\u7f13\u6162\uff0c\u662f\u7406\u60f3\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u4ec5\u6b21\u4e8e\u5e38\u6570\u9636\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#on-log-n","title":"\u7ebf\u6027\u5bf9\u6570\u9636 \\(O(n \\log n)\\)","text":"<p>\u7ebf\u6027\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u4e24\u5c42\u5faa\u73af\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u522b\u4e3a \\(O(\\log n)\\) \u548c \\(O(n)\\) \u3002</p> <p>\u4e3b\u6d41\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(n \\log n)\\) \uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u7b49\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust time_complexity.java<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\nif (n &lt;= 1)\nreturn 1;\nint count = linearLogRecur(n / 2) +\nlinearLogRecur(n / 2);\nfor (int i = 0; i &lt; n; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\nif (n &lt;= 1)\nreturn 1;\nint count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (int i = 0; i &lt; n; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>def linear_log_recur(n: float) -&gt; int:\n\"\"\"\u7ebf\u6027\u5bf9\u6570\u9636\"\"\"\nif n &lt;= 1:\nreturn 1\ncount: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)\nfor _ in range(n):\ncount += 1\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n float64) int {\nif n &lt;= 1 {\nreturn 1\n}\ncount := linearLogRecur(n/2) +\nlinearLogRecur(n/2)\nfor i := 0.0; i &lt; n; i++ {\ncount++\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n) {\nif (n &lt;= 1) return 1;\nlet count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (let i = 0; i &lt; n; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n: number): number {\nif (n &lt;= 1) return 1;\nlet count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (let i = 0; i &lt; n; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\nif (n &lt;= 1)\nreturn 1;\nint count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (int i = 0; i &lt; n; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\nif (n &lt;= 1) return 1;\nint count = linearLogRecur(n / 2) +\nlinearLogRecur(n / 2);\nfor (int i = 0; i &lt; n; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n: Double) -&gt; Int {\nif n &lt;= 1 {\nreturn 1\n}\nvar count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)\nfor _ in stride(from: 0, to: n, by: 1) {\ncount += 1\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u7ebf\u6027\u5bf9\u6570\u9636\nfn linearLogRecur(n: f32) i32 {\nif (n &lt;= 1) return 1;\nvar count: i32 = linearLogRecur(n / 2) +\nlinearLogRecur(n / 2);\nvar i: f32 = 0;\nwhile (i &lt; n) : (i += 1) {\ncount += 1;\n}\nreturn count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(num n) {\nif (n &lt;= 1) return 1;\nint count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (var i = 0; i &lt; n; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfn linear_log_recur(n: f32) -&gt; i32 {\nif n &lt;= 1.0 {\nreturn 1;\n}\nlet mut count = linear_log_recur(n / 2.0) + linear_log_recur(n / 2.0);\nfor _ in 0 ..n as i32 {\ncount += 1;\n}\nreturn count\n}\n</code></pre> <p></p> <p> \u56fe\uff1a\u7ebf\u6027\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6 </p>"},{"location":"chapter_computational_complexity/time_complexity/#on_1","title":"\u9636\u4e58\u9636 \\(O(n!)\\)","text":"<p>\u9636\u4e58\u9636\u5bf9\u5e94\u6570\u5b66\u4e0a\u7684\u201c\u5168\u6392\u5217\u201d\u95ee\u9898\u3002\u7ed9\u5b9a \\(n\\) \u4e2a\u4e92\u4e0d\u91cd\u590d\u7684\u5143\u7d20\uff0c\u6c42\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u65b9\u6848\uff0c\u65b9\u6848\u6570\u91cf\u4e3a\uff1a</p> \\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\cdots \\times 2 \\times 1 \\] <p>\u9636\u4e58\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u7b2c\u4e00\u5c42\u5206\u88c2\u51fa \\(n\\) \u4e2a\uff0c\u7b2c\u4e8c\u5c42\u5206\u88c2\u51fa \\(n - 1\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u7b2c \\(n\\) \u5c42\u65f6\u7ec8\u6b62\u5206\u88c2\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust time_complexity.java<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0)\nreturn 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (int i = 0; i &lt; n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0)\nreturn 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (int i = 0; i &lt; n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>def factorial_recur(n: int) -&gt; int:\n\"\"\"\u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\nif n == 0:\nreturn 1\ncount = 0\n# \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor _ in range(n):\ncount += factorial_recur(n - 1)\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n int) int {\nif n == 0 {\nreturn 1\n}\ncount := 0\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor i := 0; i &lt; n; i++ {\ncount += factorialRecur(n - 1)\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n) {\nif (n === 0) return 1;\nlet count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (let i = 0; i &lt; n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n: number): number {\nif (n === 0) return 1;\nlet count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (let i = 0; i &lt; n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0)\nreturn 1;\nint count = 0;\nfor (int i = 0; i &lt; n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n</code></pre> time_complexity.cs<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0) return 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (int i = 0; i &lt; n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n: Int) -&gt; Int {\nif n == 0 {\nreturn 1\n}\nvar count = 0\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor _ in 0 ..&lt; n {\ncount += factorialRecur(n: n - 1)\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn factorialRecur(n: i32) i32 {\nif (n == 0) return 1;\nvar count: i32 = 0;\nvar i: i32 = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nwhile (i &lt; n) : (i += 1) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n</code></pre> time_complexity.dart<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0) return 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (var i = 0; i &lt; n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n</code></pre> time_complexity.rs<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn factorial_recur(n: i32) -&gt; i32 {\nif n == 0 {\nreturn 1;\n}\nlet mut count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor _ in 0..n {\ncount += factorial_recur(n - 1);\n}\ncount\n}\n</code></pre> <p></p> <p> \u56fe\uff1a\u9636\u4e58\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6 </p> <p>\u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a \\(n! &gt; 2^n\\) \uff0c\u6240\u4ee5\u9636\u4e58\u9636\u6bd4\u6307\u6570\u9636\u589e\u957f\u5730\u66f4\u5feb\uff0c\u5728 \\(n\\) \u8f83\u5927\u65f6\u4e5f\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#225","title":"2.2.5. \u00a0 \u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6","text":"<p>\u7b97\u6cd5\u7684\u65f6\u95f4\u6548\u7387\u5f80\u5f80\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u5047\u8bbe\u8f93\u5165\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 <code>nums</code> \uff0c\u5176\u4e2d <code>nums</code> \u7531\u4ece \\(1\\) \u81f3 \\(n\\) \u7684\u6570\u5b57\u7ec4\u6210\uff0c\u4f46\u5143\u7d20\u987a\u5e8f\u662f\u968f\u673a\u6253\u4e71\u7684\uff0c\u4efb\u52a1\u76ee\u6807\u662f\u8fd4\u56de\u5143\u7d20 \\(1\\) \u7684\u7d22\u5f15\u3002\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\uff1a</p> <ul> <li>\u5f53 <code>nums = [?, ?, ..., 1]</code> \uff0c\u5373\u5f53\u672b\u5c3e\u5143\u7d20\u662f \\(1\\) \u65f6\uff0c\u9700\u8981\u5b8c\u6574\u904d\u5386\u6570\u7ec4\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002</li> <li>\u5f53 <code>nums = [1, ?, ?, ...]</code> \uff0c\u5373\u5f53\u9996\u4e2a\u6570\u5b57\u4e3a \\(1\\) \u65f6\uff0c\u65e0\u8bba\u6570\u7ec4\u591a\u957f\u90fd\u4e0d\u9700\u8981\u7ee7\u7eed\u904d\u5386\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(\\Omega(1)\\) \u3002</li> </ul> <p>\u300c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u4f7f\u7528\u5927 \\(O\\) \u8bb0\u53f7\u8868\u793a\u3002\u76f8\u5e94\u5730\uff0c\u300c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0b\u754c\uff0c\u7528 \\(\\Omega\\) \u8bb0\u53f7\u8868\u793a\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust worst_best_time_complexity.java<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] randomNumbers(int n) {\nInteger[] nums = new Integer[n];\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i &lt; n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nCollections.shuffle(Arrays.asList(nums));\n// Integer[] -&gt; int[]\nint[] res = new int[n];\nfor (int i = 0; i &lt; n; i++) {\nres[i] = nums[i];\n}\nreturn res;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums) {\nfor (int i = 0; i &lt; nums.length; i++) {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -1;\n}\n</code></pre> worst_best_time_complexity.cpp<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nvector&lt;int&gt; randomNumbers(int n) {\nvector&lt;int&gt; nums(n);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i &lt; n; i++) {\nnums[i] = i + 1;\n}\n// \u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u751f\u6210\u968f\u673a\u79cd\u5b50\nunsigned seed = chrono::system_clock::now().time_since_epoch().count();\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nshuffle(nums.begin(), nums.end(), default_random_engine(seed));\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(vector&lt;int&gt; &amp;nums) {\nfor (int i = 0; i &lt; nums.size(); i++) {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -1;\n}\n</code></pre> worst_best_time_complexity.py<pre><code>def random_numbers(n: int) -&gt; list[int]:\n\"\"\"\u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a: 1, 2, ..., n \uff0c\u987a\u5e8f\u88ab\u6253\u4e71\"\"\"\n# \u751f\u6210\u6570\u7ec4 nums =: 1, 2, 3, ..., n\nnums = [i for i in range(1, n + 1)]\n# \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nrandom.shuffle(nums)\nreturn nums\ndef find_one(nums: list[int]) -&gt; int:\n\"\"\"\u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\"\"\"\nfor i in range(len(nums)):\n# \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n# \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif nums[i] == 1:\nreturn i\nreturn -1\n</code></pre> worst_best_time_complexity.go<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n int) []int {\nnums := make([]int, n)\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor i := 0; i &lt; n; i++ {\nnums[i] = i + 1\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nrand.Shuffle(len(nums), func(i, j int) {\nnums[i], nums[j] = nums[j], nums[i]\n})\nreturn nums\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums []int) int {\nfor i := 0; i &lt; len(nums); i++ {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif nums[i] == 1 {\nreturn i\n}\n}\nreturn -1\n}\n</code></pre> worst_best_time_complexity.js<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n) {\nconst nums = Array(n);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (let i = 0; i &lt; n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (let i = 0; i &lt; n; i++) {\nconst r = Math.floor(Math.random() * (i + 1));\nconst temp = nums[i];\nnums[i] = nums[r];\nnums[r] = temp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums) {\nfor (let i = 0; i &lt; nums.length; i++) {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (nums[i] === 1) {\nreturn i;\n}\n}\nreturn -1;\n}\n</code></pre> worst_best_time_complexity.ts<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n: number): number[] {\nconst nums = Array(n);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (let i = 0; i &lt; n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (let i = 0; i &lt; n; i++) {\nconst r = Math.floor(Math.random() * (i + 1));\nconst temp = nums[i];\nnums[i] = nums[r];\nnums[r] = temp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums: number[]): number {\nfor (let i = 0; i &lt; nums.length; i++) {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (nums[i] === 1) {\nreturn i;\n}\n}\nreturn -1;\n}\n</code></pre> worst_best_time_complexity.c<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint *randomNumbers(int n) {\n// \u5206\u914d\u5806\u533a\u5185\u5b58\uff08\u521b\u5efa\u4e00\u7ef4\u53ef\u53d8\u957f\u6570\u7ec4\uff1a\u6570\u7ec4\u4e2d\u5143\u7d20\u6570\u91cf\u4e3an\uff0c\u5143\u7d20\u7c7b\u578b\u4e3aint\uff09\nint *nums = (int *)malloc(n * sizeof(int));\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i &lt; n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (int i = n - 1; i &gt; 0; i--) {\nint j = rand() % (i + 1);\nint temp = nums[i];\nnums[i] = nums[j];\nnums[j] = temp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int *nums, int n) {\nfor (int i = 0; i &lt; n; i++) {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -1;\n}\n</code></pre> worst_best_time_complexity.cs<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] randomNumbers(int n) {\nint[] nums = new int[n];\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i &lt; n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (int i = 0; i &lt; nums.Length; i++) {\nvar index = new Random().Next(i, nums.Length);\nvar tmp = nums[i];\nvar ran = nums[index];\nnums[i] = ran;\nnums[index] = tmp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums) {\nfor (int i = 0; i &lt; nums.Length; i++) {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -1;\n}\n</code></pre> worst_best_time_complexity.swift<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n: Int) -&gt; [Int] {\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nvar nums = Array(1 ... n)\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nnums.shuffle()\nreturn nums\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums: [Int]) -&gt; Int {\nfor i in nums.indices {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif nums[i] == 1 {\nreturn i\n}\n}\nreturn -1\n}\n</code></pre> worst_best_time_complexity.zig<pre><code>// \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71\npub fn randomNumbers(comptime n: usize) [n]i32 {\nvar nums: [n]i32 = undefined;\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (nums) |*num, i| {\nnum.* = @intCast(i32, i) + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nconst rand = std.crypto.random;\nrand.shuffle(i32, &amp;nums);\nreturn nums;\n}\n// \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\npub fn findOne(nums: []i32) i32 {\nfor (nums) |num, i| {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (num == 1) return @intCast(i32, i);\n}\nreturn -1;\n}\n</code></pre> worst_best_time_complexity.dart<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nList&lt;int&gt; randomNumbers(int n) {\nfinal nums = List.filled(n, 0);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (var i = 0; i &lt; n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nnums.shuffle();\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(List&lt;int&gt; nums) {\nfor (var i = 0; i &lt; nums.length; i++) {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (nums[i] == 1) return i;\n}\nreturn -1;\n}\n</code></pre> worst_best_time_complexity.rs<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfn random_numbers(n: i32) -&gt; Vec&lt;i32&gt; {\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nlet mut nums = (1..=n).collect::&lt;Vec&lt;i32&gt;&gt;();\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nnums.shuffle(&amp;mut thread_rng());\nnums\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfn find_one(nums: &amp;[i32]) -&gt; Option&lt;usize&gt; {\nfor i in 0..nums.len() {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif nums[i] == 1 {\nreturn Some(i);\n}\n}\nNone\n}\n</code></pre> <p>\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6211\u4eec\u5728\u5b9e\u9645\u4e2d\u5f88\u5c11\u4f7f\u7528\u300c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u300d\uff0c\u56e0\u4e3a\u901a\u5e38\u53ea\u6709\u5728\u5f88\u5c0f\u6982\u7387\u4e0b\u624d\u80fd\u8fbe\u5230\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u8bef\u5bfc\u6027\u3002\u800c\u300c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u66f4\u4e3a\u5b9e\u7528\uff0c\u56e0\u4e3a\u5b83\u7ed9\u51fa\u4e86\u4e00\u4e2a\u6548\u7387\u5b89\u5168\u503c\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u653e\u5fc3\u5730\u4f7f\u7528\u7b97\u6cd5\u3002</p> <p>\u4ece\u4e0a\u8ff0\u793a\u4f8b\u53ef\u4ee5\u770b\u51fa\uff0c\u6700\u5dee\u6216\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ea\u51fa\u73b0\u4e8e\u201c\u7279\u6b8a\u7684\u6570\u636e\u5206\u5e03\u201d\uff0c\u8fd9\u4e9b\u60c5\u51b5\u7684\u51fa\u73b0\u6982\u7387\u53ef\u80fd\u5f88\u5c0f\uff0c\u5e76\u4e0d\u80fd\u771f\u5b9e\u5730\u53cd\u6620\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u300c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u53ef\u4ee5\u4f53\u73b0\u7b97\u6cd5\u5728\u968f\u673a\u8f93\u5165\u6570\u636e\u4e0b\u7684\u8fd0\u884c\u6548\u7387\uff0c\u7528 \\(\\Theta\\) \u8bb0\u53f7\u6765\u8868\u793a\u3002</p> <p>\u5bf9\u4e8e\u90e8\u5206\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u63a8\u7b97\u51fa\u968f\u673a\u6570\u636e\u5206\u5e03\u4e0b\u7684\u5e73\u5747\u60c5\u51b5\u3002\u6bd4\u5982\u4e0a\u8ff0\u793a\u4f8b\uff0c\u7531\u4e8e\u8f93\u5165\u6570\u7ec4\u662f\u88ab\u6253\u4e71\u7684\uff0c\u56e0\u6b64\u5143\u7d20 \\(1\\) \u51fa\u73b0\u5728\u4efb\u610f\u7d22\u5f15\u7684\u6982\u7387\u90fd\u662f\u76f8\u7b49\u7684\uff0c\u90a3\u4e48\u7b97\u6cd5\u7684\u5e73\u5747\u5faa\u73af\u6b21\u6570\u5219\u662f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a \\(\\frac{n}{2}\\) \uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(\\Theta(\\frac{n}{2}) = \\Theta(n)\\) \u3002</p> <p>\u4f46\u5bf9\u4e8e\u8f83\u4e3a\u590d\u6742\u7684\u7b97\u6cd5\uff0c\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5f80\u5f80\u662f\u6bd4\u8f83\u56f0\u96be\u7684\uff0c\u56e0\u4e3a\u5f88\u96be\u5206\u6790\u51fa\u5728\u6570\u636e\u5206\u5e03\u4e0b\u7684\u6574\u4f53\u6570\u5b66\u671f\u671b\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4f5c\u4e3a\u7b97\u6cd5\u6548\u7387\u7684\u8bc4\u5224\u6807\u51c6\u3002</p> <p>\u4e3a\u4ec0\u4e48\u5f88\u5c11\u770b\u5230 \\(\\Theta\\) \u7b26\u53f7\uff1f</p> <p>\u53ef\u80fd\u7531\u4e8e \\(O\\) \u7b26\u53f7\u8fc7\u4e8e\u6717\u6717\u4e0a\u53e3\uff0c\u6211\u4eec\u5e38\u5e38\u4f7f\u7528\u5b83\u6765\u8868\u793a\u300c\u5e73\u5747\u590d\u6742\u5ea6\u300d\uff0c\u4f46\u4ece\u4e25\u683c\u610f\u4e49\u4e0a\u770b\uff0c\u8fd9\u79cd\u505a\u6cd5\u5e76\u4e0d\u89c4\u8303\u3002\u5728\u672c\u4e66\u548c\u5176\u4ed6\u8d44\u6599\u4e2d\uff0c\u82e5\u9047\u5230\u7c7b\u4f3c\u201c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\)\u201d\u7684\u8868\u8ff0\uff0c\u8bf7\u5c06\u5176\u76f4\u63a5\u7406\u89e3\u4e3a \\(\\Theta(n)\\) \u3002</p>"},{"location":"chapter_data_structure/","title":"3. \u00a0 \u6570\u636e\u7ed3\u6784","text":"<p>Abstract</p> <p>\u6570\u636e\u7ed3\u6784\u5982\u540c\u4e00\u526f\u7a33\u56fa\u800c\u591a\u6837\u7684\u6846\u67b6\u3002</p> <p>\u5b83\u4e3a\u6570\u636e\u7684\u6709\u5e8f\u7ec4\u7ec7\u63d0\u4f9b\u4e86\u84dd\u56fe\uff0c\u4f7f\u7b97\u6cd5\u5f97\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u751f\u52a8\u8d77\u6765\u3002</p>"},{"location":"chapter_data_structure/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>3.1 \u00a0 \u6570\u636e\u7ed3\u6784\u5206\u7c7b</li> <li>3.2 \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b</li> <li>3.3 \u00a0 \u6570\u5b57\u7f16\u7801 *</li> <li>3.4 \u00a0 \u5b57\u7b26\u7f16\u7801 *</li> <li>3.5 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_data_structure/basic_data_types/","title":"3.2. \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b","text":"<p>\u8c08\u53ca\u8ba1\u7b97\u673a\u4e2d\u7684\u6570\u636e\uff0c\u6211\u4eec\u4f1a\u60f3\u5230\u6587\u672c\u3001\u56fe\u7247\u3001\u89c6\u9891\u3001\u8bed\u97f3\u30013D \u6a21\u578b\u7b49\u5404\u79cd\u5f62\u5f0f\u3002\u5c3d\u7ba1\u8fd9\u4e9b\u6570\u636e\u7684\u7ec4\u7ec7\u5f62\u5f0f\u5404\u5f02\uff0c\u4f46\u5b83\u4eec\u90fd\u7531\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6784\u6210\u3002</p> <p>\u57fa\u672c\u6570\u636e\u7c7b\u578b\u662f CPU \u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u8fd0\u7b97\u7684\u7c7b\u578b\uff0c\u5728\u7b97\u6cd5\u4e2d\u76f4\u63a5\u88ab\u4f7f\u7528\u3002\u5b83\u5305\u62ec\uff1a</p> <ul> <li>\u6574\u6570\u7c7b\u578b <code>byte</code> , <code>short</code> , <code>int</code> , <code>long</code> \u3002</li> <li>\u6d6e\u70b9\u6570\u7c7b\u578b <code>float</code> , <code>double</code> \uff0c\u7528\u4e8e\u8868\u793a\u5c0f\u6570\u3002</li> <li>\u5b57\u7b26\u7c7b\u578b <code>char</code> \uff0c\u7528\u4e8e\u8868\u793a\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u6bcd\u3001\u6807\u70b9\u7b26\u53f7\u3001\u751a\u81f3\u8868\u60c5\u7b26\u53f7\u7b49\u3002</li> <li>\u5e03\u5c14\u7c7b\u578b <code>bool</code> \uff0c\u7528\u4e8e\u8868\u793a\u201c\u662f\u201d\u4e0e\u201c\u5426\u201d\u5224\u65ad\u3002</li> </ul> <p>\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4ee5\u4e8c\u8fdb\u5236\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u3002\u4e00\u4e2a\u4e8c\u8fdb\u5236\u4f4d\u5373\u4e3a \\(1\\) \u6bd4\u7279\u3002\u5728\u7edd\u5927\u591a\u6570\u73b0\u4ee3\u7cfb\u7edf\u4e2d\uff0c\\(1\\) \u5b57\u8282\uff08byte\uff09\u7531 \\(8\\) \u6bd4\u7279\uff08bits\uff09\u7ec4\u6210\u3002</p> <p>\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u53d6\u51b3\u4e8e\u5176\u5360\u7528\u7684\u7a7a\u95f4\u5927\u5c0f\uff0c\u4f8b\u5982 Java \u89c4\u5b9a\uff1a</p> <ul> <li>\u6574\u6570\u7c7b\u578b <code>byte</code> \u5360\u7528 \\(1\\) byte = \\(8\\) bits \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{8}\\) \u4e2a\u6570\u5b57\u3002</li> <li>\u6574\u6570\u7c7b\u578b <code>int</code> \u5360\u7528 \\(4\\) bytes = \\(32\\) bits \uff0c\u53ef\u4ee5\u8868\u793a \\(2^{32}\\) \u4e2a\u6570\u5b57\u3002</li> </ul> <p>\u4e0b\u8868\u5217\u4e3e\u4e86\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5360\u7528\u7a7a\u95f4\u3001\u53d6\u503c\u8303\u56f4\u548c\u9ed8\u8ba4\u503c\u3002\u6b64\u8868\u683c\u65e0\u9700\u786c\u80cc\uff0c\u5927\u81f4\u7406\u89e3\u5373\u53ef\uff0c\u9700\u8981\u65f6\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u6765\u56de\u5fc6\u3002</p> <p> \u8868\uff1a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5360\u7528\u7a7a\u95f4\u548c\u53d6\u503c\u8303\u56f4 </p> \u7c7b\u578b \u7b26\u53f7 \u5360\u7528\u7a7a\u95f4 \u6700\u5c0f\u503c \u6700\u5927\u503c \u9ed8\u8ba4\u503c \u6574\u6570 <code>byte</code> 1 byte \\(-2^7\\) (\\(-128\\)) \\(2^7 - 1\\) (\\(127\\)) \\(0\\) <code>short</code> 2 bytes \\(-2^{15}\\) \\(2^{15} - 1\\) \\(0\\) <code>int</code> 4 bytes \\(-2^{31}\\) \\(2^{31} - 1\\) \\(0\\) <code>long</code> 8 bytes \\(-2^{63}\\) \\(2^{63} - 1\\) \\(0\\) \u6d6e\u70b9\u6570 <code>float</code> 4 bytes \\(1.175 \\times 10^{-38}\\) \\(3.403 \\times 10^{38}\\) \\(0.0 f\\) <code>double</code> 8 bytes \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) \\(0.0\\) \u5b57\u7b26 <code>char</code> 2 bytes / 1 byte \\(0\\) \\(2^{16} - 1\\) \\(0\\) \u5e03\u5c14 <code>bool</code> 1 byte \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\) <p>\u5bf9\u4e8e\u4e0a\u8868\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a</p> <ul> <li>C, C++ \u672a\u660e\u786e\u89c4\u5b9a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5927\u5c0f\uff0c\u800c\u56e0\u5b9e\u73b0\u548c\u5e73\u53f0\u5404\u5f02\u3002\u4e0a\u8868\u9075\u5faa LP64 \u6570\u636e\u6a21\u578b\uff0c\u5176\u7528\u4e8e Unix 64 \u4f4d\u64cd\u4f5c\u7cfb\u7edf\uff08\u4f8b\u5982 Linux , macOS\uff09\u3002</li> <li>\u5b57\u7b26 <code>char</code> \u7684\u5927\u5c0f\u5728 C, C++ \u4e2d\u4e3a 1 \u5b57\u8282\uff0c\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\u53d6\u51b3\u4e8e\u7279\u5b9a\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\uff0c\u8be6\u89c1\u201c\u5b57\u7b26\u7f16\u7801\u201d\u7ae0\u8282\u3002</li> <li>\u5373\u4f7f\u8868\u793a\u5e03\u5c14\u91cf\u4ec5\u9700 1 \u4f4d\uff08\\(0\\) \u6216 \\(1\\)\uff09\uff0c\u5b83\u5728\u5185\u5b58\u4e2d\u901a\u5e38\u88ab\u5b58\u50a8\u4e3a 1 \u5b57\u8282\u3002\u8fd9\u662f\u56e0\u4e3a\u73b0\u4ee3\u8ba1\u7b97\u673a CPU \u901a\u5e38\u5c06 1 \u5b57\u8282\u4f5c\u4e3a\u6700\u5c0f\u5bfb\u5740\u5185\u5b58\u5355\u5143\u3002</li> </ul> <p>\u90a3\u4e48\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u95f4\u6709\u4ec0\u4e48\u8054\u7cfb\u5462\uff1f\u6211\u4eec\u77e5\u9053\uff0c\u6570\u636e\u7ed3\u6784\u662f\u5728\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u4e0e\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002\u5b83\u7684\u4e3b\u8bed\u662f\u201c\u7ed3\u6784\u201d\u800c\u975e\u201c\u6570\u636e\u201d\u3002</p> <p>\u5982\u679c\u60f3\u8981\u8868\u793a\u201c\u4e00\u6392\u6570\u5b57\u201d\uff0c\u6211\u4eec\u81ea\u7136\u4f1a\u60f3\u5230\u4f7f\u7528\u6570\u7ec4\u3002\u8fd9\u662f\u56e0\u4e3a\u6570\u7ec4\u7684\u7ebf\u6027\u7ed3\u6784\u53ef\u4ee5\u8868\u793a\u6570\u5b57\u7684\u76f8\u90bb\u5173\u7cfb\u548c\u987a\u5e8f\u5173\u7cfb\uff0c\u4f46\u81f3\u4e8e\u5b58\u50a8\u7684\u5185\u5bb9\u662f\u6574\u6570 <code>int</code> \u3001\u5c0f\u6570 <code>float</code> \u3001\u8fd8\u662f\u5b57\u7b26 <code>char</code> \uff0c\u5219\u4e0e\u201c\u6570\u636e\u7ed3\u6784\u201d\u65e0\u5173\u3002</p> <p>\u6362\u53e5\u8bdd\u8bf4\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u201c\u5185\u5bb9\u7c7b\u578b\u201d\uff0c\u800c\u6570\u636e\u7ed3\u6784\u63d0\u4f9b\u4e86\u6570\u636e\u7684\u201c\u7ec4\u7ec7\u65b9\u5f0f\u201d\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6211\u4eec\u7528\u76f8\u540c\u7684\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\uff09\u6765\u5b58\u50a8\u4e0e\u8868\u793a\u4e0d\u540c\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08<code>int</code> , <code>float</code> , <code>chat</code>, <code>bool</code>\uff09\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n</code></pre> <pre><code># \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# Python \u7684\u5b57\u7b26\u5e94\u88ab\u770b\u4f5c\u957f\u5ea6\u4e3a\u4e00\u7684\u5b57\u7b26\u4e32\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# Python \u7684\u5217\u8868\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\u5f15\u7528\ndata = [0, 0.0, 'a', False, ListNode(0)]\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n</code></pre> <pre><code>// JavaScript \u7684\u6570\u7ec4\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61\nconst array = [0, 0.0, 'a', false];\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nlet numbers = Array(repeating: Int(), count: 5)\nlet decimals = Array(repeating: Double(), count: 5)\nlet characters = Array(repeating: Character(\"a\"), count: 5)\nlet bools = Array(repeating: Bool(), count: 5)\n</code></pre> <pre><code>\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\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>\n</code></pre>"},{"location":"chapter_data_structure/character_encoding/","title":"3.4. \u00a0 \u5b57\u7b26\u7f16\u7801 *","text":"<p>\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u6240\u6709\u6570\u636e\u90fd\u662f\u4ee5\u4e8c\u8fdb\u5236\u6570\u7684\u5f62\u5f0f\u5b58\u50a8\u7684\uff0c\u5b57\u7b26 <code>char</code> \u4e5f\u4e0d\u4f8b\u5916\u3002\u4e3a\u4e86\u8868\u793a\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u5efa\u7acb\u4e00\u5957\u201c\u5b57\u7b26\u96c6\u201d\uff0c\u89c4\u5b9a\u6bcf\u4e2a\u5b57\u7b26\u548c\u4e8c\u8fdb\u5236\u6570\u4e4b\u95f4\u7684\u4e00\u4e00\u5bf9\u5e94\u5173\u7cfb\u3002\u6709\u4e86\u5b57\u7b26\u96c6\u4e4b\u540e\uff0c\u8ba1\u7b97\u673a\u5c31\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u5b8c\u6210\u4e8c\u8fdb\u5236\u6570\u5230\u5b57\u7b26\u7684\u8f6c\u6362\u3002</p>"},{"location":"chapter_data_structure/character_encoding/#341-ascii","title":"3.4.1. \u00a0 ASCII \u5b57\u7b26\u96c6","text":"<p>\u300cASCII \u7801\u300d\u662f\u6700\u65e9\u51fa\u73b0\u7684\u5b57\u7b26\u96c6\uff0c\u5168\u79f0\u4e3a\u201c\u7f8e\u56fd\u6807\u51c6\u4fe1\u606f\u4ea4\u6362\u4ee3\u7801\u201d\u3002\u5b83\u4f7f\u7528 7 \u4f4d\u4e8c\u8fdb\u5236\u6570\uff08\u5373\u4e00\u4e2a\u5b57\u8282\u7684\u4f4e 7 \u4f4d\uff09\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u6700\u591a\u80fd\u591f\u8868\u793a 128 \u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u3002\u8fd9\u5305\u62ec\u82f1\u6587\u5b57\u6bcd\u7684\u5927\u5c0f\u5199\u3001\u6570\u5b57 0-9 \u3001\u4e00\u4e9b\u6807\u70b9\u7b26\u53f7\uff0c\u4ee5\u53ca\u4e00\u4e9b\u63a7\u5236\u5b57\u7b26\uff08\u5982\u6362\u884c\u7b26\u548c\u5236\u8868\u7b26\uff09\u3002</p> <p></p> <p> \u56fe\uff1aASCII \u7801 </p> <p>\u7136\u800c\uff0cASCII \u7801\u4ec5\u80fd\u591f\u8868\u793a\u82f1\u6587\u3002\u968f\u7740\u8ba1\u7b97\u673a\u7684\u5168\u7403\u5316\uff0c\u8bde\u751f\u4e86\u4e00\u79cd\u80fd\u591f\u8868\u793a\u66f4\u591a\u8bed\u8a00\u7684\u5b57\u7b26\u96c6\u300cEASCII\u300d\u3002\u5b83\u5728 ASCII \u7684 7 \u4f4d\u57fa\u7840\u4e0a\u6269\u5c55\u5230 8 \u4f4d\uff0c\u80fd\u591f\u8868\u793a 256 \u4e2a\u4e0d\u540c\u7684\u5b57\u7b26\u3002</p> <p>\u5728\u4e16\u754c\u8303\u56f4\u5185\uff0c\u9646\u7eed\u51fa\u73b0\u4e86\u4e00\u6279\u9002\u7528\u4e8e\u4e0d\u540c\u5730\u533a\u7684 EASCII \u5b57\u7b26\u96c6\u3002\u8fd9\u4e9b\u5b57\u7b26\u96c6\u7684\u524d 128 \u4e2a\u5b57\u7b26\u7edf\u4e00\u4e3a ASCII \u7801\uff0c\u540e 128 \u4e2a\u5b57\u7b26\u5b9a\u4e49\u4e0d\u540c\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u8bed\u8a00\u7684\u9700\u6c42\u3002</p>"},{"location":"chapter_data_structure/character_encoding/#342-gbk","title":"3.4.2. \u00a0 GBK \u5b57\u7b26\u96c6","text":"<p>\u540e\u6765\u4eba\u4eec\u53d1\u73b0\uff0cEASCII \u7801\u4ecd\u7136\u65e0\u6cd5\u6ee1\u8db3\u8bb8\u591a\u8bed\u8a00\u7684\u5b57\u7b26\u6570\u91cf\u8981\u6c42\u3002\u6bd4\u5982\u6c49\u5b57\u5927\u7ea6\u6709\u8fd1\u5341\u4e07\u4e2a\uff0c\u5149\u65e5\u5e38\u4f7f\u7528\u7684\u5c31\u6709\u51e0\u5343\u4e2a\u3002\u4e2d\u56fd\u56fd\u5bb6\u6807\u51c6\u603b\u5c40\u4e8e 1980 \u5e74\u53d1\u5e03\u4e86\u300cGB2312\u300d\u5b57\u7b26\u96c6\uff0c\u5176\u6536\u5f55\u4e86 6763 \u4e2a\u6c49\u5b57\uff0c\u57fa\u672c\u6ee1\u8db3\u4e86\u6c49\u5b57\u7684\u8ba1\u7b97\u673a\u5904\u7406\u9700\u8981\u3002</p> <p>\u7136\u800c\uff0cGB2312 \u65e0\u6cd5\u5904\u7406\u90e8\u5206\u7684\u7f55\u89c1\u5b57\u548c\u7e41\u4f53\u5b57\u3002\u300cGBK\u300d\u5b57\u7b26\u96c6\u662f\u5728 GB2312 \u7684\u57fa\u7840\u4e0a\u6269\u5c55\u5f97\u5230\u7684\uff0c\u5b83\u5171\u6536\u5f55\u4e86 21886 \u4e2a\u6c49\u5b57\u3002\u5728 GBK \u7684\u7f16\u7801\u65b9\u6848\u4e2d\uff0cASCII \u5b57\u7b26\u4f7f\u7528\u4e00\u4e2a\u5b57\u8282\u8868\u793a\uff0c\u6c49\u5b57\u4f7f\u7528\u4e24\u4e2a\u5b57\u8282\u8868\u793a\u3002</p>"},{"location":"chapter_data_structure/character_encoding/#343-unicode","title":"3.4.3. \u00a0 Unicode \u5b57\u7b26\u96c6","text":"<p>\u968f\u7740\u8ba1\u7b97\u673a\u7684\u84ec\u52c3\u53d1\u5c55\uff0c\u5b57\u7b26\u96c6\u4e0e\u7f16\u7801\u6807\u51c6\u767e\u82b1\u9f50\u653e\uff0c\u800c\u8fd9\u5e26\u6765\u4e86\u8bb8\u591a\u95ee\u9898\u3002\u4e00\u65b9\u9762\uff0c\u8fd9\u4e9b\u5b57\u7b26\u96c6\u4e00\u822c\u53ea\u5b9a\u4e49\u4e86\u7279\u5b9a\u8bed\u8a00\u7684\u5b57\u7b26\uff0c\u65e0\u6cd5\u5728\u591a\u8bed\u8a00\u73af\u5883\u4e0b\u6b63\u5e38\u5de5\u4f5c\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u540c\u4e00\u79cd\u8bed\u8a00\u4e5f\u5b58\u5728\u591a\u79cd\u5b57\u7b26\u96c6\u6807\u51c6\uff0c\u5982\u679c\u4e24\u53f0\u7535\u8111\u5b89\u88c5\u7684\u662f\u4e0d\u540c\u7684\u7f16\u7801\u6807\u51c6\uff0c\u5219\u5728\u4fe1\u606f\u4f20\u9012\u65f6\u5c31\u4f1a\u51fa\u73b0\u4e71\u7801\u3002</p> <p>\u90a3\u4e2a\u65f6\u4ee3\u7684\u7814\u7a76\u4eba\u5458\u5c31\u5728\u60f3\uff1a\u5982\u679c\u63a8\u51fa\u4e00\u4e2a\u8db3\u591f\u5b8c\u6574\u7684\u5b57\u7b26\u96c6\uff0c\u5c06\u4e16\u754c\u8303\u56f4\u5185\u7684\u6240\u6709\u8bed\u8a00\u548c\u7b26\u53f7\u90fd\u6536\u5f55\u5176\u4e2d\uff0c\u4e0d\u5c31\u53ef\u4ee5\u89e3\u51b3\u8de8\u8bed\u8a00\u73af\u5883\u548c\u4e71\u7801\u95ee\u9898\u4e86\u5417\uff1f\u5728\u8fd9\u79cd\u60f3\u6cd5\u7684\u9a71\u52a8\u4e0b\uff0c\u4e00\u4e2a\u5927\u800c\u5168\u7684\u5b57\u7b26\u96c6 Unicode \u5e94\u8fd0\u800c\u751f\u3002</p> <p>\u300cUnicode\u300d\u7684\u5168\u79f0\u4e3a\u201c\u7edf\u4e00\u5b57\u7b26\u7f16\u7801\u201d\uff0c\u7406\u8bba\u4e0a\u80fd\u5bb9\u7eb3\u4e00\u767e\u591a\u4e07\u4e2a\u5b57\u7b26\u3002\u5b83\u81f4\u529b\u4e8e\u5c06\u5168\u7403\u8303\u56f4\u5185\u7684\u5b57\u7b26\u7eb3\u5165\u5230\u7edf\u4e00\u7684\u5b57\u7b26\u96c6\u4e4b\u4e2d\uff0c\u63d0\u4f9b\u4e00\u79cd\u901a\u7528\u7684\u5b57\u7b26\u96c6\u6765\u5904\u7406\u548c\u663e\u793a\u5404\u79cd\u8bed\u8a00\u6587\u5b57\uff0c\u51cf\u5c11\u56e0\u4e3a\u7f16\u7801\u6807\u51c6\u4e0d\u540c\u800c\u4ea7\u751f\u7684\u4e71\u7801\u95ee\u9898\u3002</p> <p>\u81ea 1991 \u5e74\u53d1\u5e03\u4ee5\u6765\uff0cUnicode \u4e0d\u65ad\u6269\u5145\u65b0\u7684\u8bed\u8a00\u4e0e\u5b57\u7b26\u3002\u622a\u6b62 2022 \u5e74 9 \u6708\uff0cUnicode \u5df2\u7ecf\u5305\u542b 149186 \u4e2a\u5b57\u7b26\uff0c\u5305\u62ec\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u7b26\u3001\u7b26\u53f7\u3001\u751a\u81f3\u662f\u8868\u60c5\u7b26\u53f7\u7b49\u3002\u5728\u5e9e\u5927\u7684 Unicode \u5b57\u7b26\u96c6\u4e2d\uff0c\u5e38\u7528\u7684\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\uff0c\u6709\u4e9b\u751f\u50fb\u7684\u5b57\u7b26\u5360 3 \u5b57\u8282\u751a\u81f3 4 \u5b57\u8282\u3002</p> <p>Unicode \u662f\u4e00\u79cd\u5b57\u7b26\u96c6\u6807\u51c6\uff0c\u672c\u8d28\u4e0a\u662f\u7ed9\u6bcf\u4e2a\u5b57\u7b26\u5206\u914d\u4e00\u4e2a\u7f16\u53f7\uff08\u79f0\u4e3a\u201c\u7801\u70b9\u201d\uff09\uff0c\u4f46\u5b83\u5e76\u6ca1\u6709\u89c4\u5b9a\u5728\u8ba1\u7b97\u673a\u4e2d\u5982\u4f55\u5b58\u50a8\u8fd9\u4e9b\u5b57\u7b26\u7801\u70b9\u3002\u6211\u4eec\u4e0d\u7981\u4f1a\u95ee\uff1a\u5f53\u591a\u79cd\u957f\u5ea6\u7684 Unicode \u7801\u70b9\u540c\u65f6\u51fa\u73b0\u5728\u540c\u4e00\u4e2a\u6587\u672c\u4e2d\u65f6\uff0c\u7cfb\u7edf\u5982\u4f55\u89e3\u6790\u5b57\u7b26\uff1f\u4f8b\u5982\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a 2 \u5b57\u8282\u7684\u7f16\u7801\uff0c\u7cfb\u7edf\u5982\u4f55\u786e\u8ba4\u5b83\u662f\u4e00\u4e2a 2 \u5b57\u8282\u7684\u5b57\u7b26\u8fd8\u662f\u4e24\u4e2a 1 \u5b57\u8282\u7684\u5b57\u7b26\uff1f</p> <p>\u5bf9\u4e8e\u4ee5\u4e0a\u95ee\u9898\uff0c\u4e00\u79cd\u76f4\u63a5\u7684\u89e3\u51b3\u65b9\u6848\u662f\u5c06\u6240\u6709\u5b57\u7b26\u5b58\u50a8\u4e3a\u7b49\u957f\u7684\u7f16\u7801\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u201cHello\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u5b57\u8282\uff0c\u201c\u7b97\u6cd5\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9ad8\u4f4d\u586b 0 \uff0c\u5c06\u201cHello \u7b97\u6cd5\u201d\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u90fd\u7f16\u7801\u4e3a 2 \u5b57\u8282\u957f\u5ea6\u3002\u8fd9\u6837\u7cfb\u7edf\u5c31\u53ef\u4ee5\u6bcf\u9694 2 \u5b57\u8282\u89e3\u6790\u4e00\u4e2a\u5b57\u7b26\uff0c\u6062\u590d\u51fa\u8fd9\u4e2a\u77ed\u8bed\u7684\u5185\u5bb9\u4e86\u3002</p> <p></p> <p> \u56fe\uff1aUnicode \u7f16\u7801\u793a\u4f8b </p> <p>\u7136\u800c ASCII \u7801\u5df2\u7ecf\u5411\u6211\u4eec\u8bc1\u660e\uff0c\u7f16\u7801\u82f1\u6587\u53ea\u9700\u8981 1 \u5b57\u8282\u3002\u82e5\u91c7\u7528\u4e0a\u8ff0\u65b9\u6848\uff0c\u82f1\u6587\u6587\u672c\u5360\u7528\u7a7a\u95f4\u7684\u5927\u5c0f\u5c06\u4f1a\u662f ASCII \u7f16\u7801\u4e0b\u5927\u5c0f\u7684\u4e24\u500d\uff0c\u975e\u5e38\u6d6a\u8d39\u5185\u5b58\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u66f4\u52a0\u9ad8\u6548\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\u3002</p>"},{"location":"chapter_data_structure/character_encoding/#344-utf-8","title":"3.4.4. \u00a0 UTF-8 \u7f16\u7801","text":"<p>\u76ee\u524d\uff0cUTF-8 \u5df2\u6210\u4e3a\u56fd\u9645\u4e0a\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\u3002\u5b83\u662f\u4e00\u79cd\u53ef\u53d8\u957f\u7684\u7f16\u7801\uff0c\u4f7f\u7528 1 \u5230 4 \u4e2a\u5b57\u8282\u6765\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u6839\u636e\u5b57\u7b26\u7684\u590d\u6742\u6027\u800c\u53d8\u3002ASCII \u5b57\u7b26\u53ea\u9700\u8981 1 \u4e2a\u5b57\u8282\uff0c\u62c9\u4e01\u5b57\u6bcd\u548c\u5e0c\u814a\u5b57\u6bcd\u9700\u8981 2 \u4e2a\u5b57\u8282\uff0c\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u7b26\u9700\u8981 3 \u4e2a\u5b57\u8282\uff0c\u5176\u4ed6\u7684\u4e00\u4e9b\u751f\u50fb\u5b57\u7b26\u9700\u8981 4 \u4e2a\u5b57\u8282\u3002</p> <p>UTF-8 \u7684\u7f16\u7801\u89c4\u5219\u5e76\u4e0d\u590d\u6742\uff0c\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5\uff1a</p> <ol> <li>\u5bf9\u4e8e\u957f\u5ea6\u4e3a 1 \u5b57\u8282\u7684\u5b57\u7b26\uff0c\u5c06\u6700\u9ad8\u4f4d\u8bbe\u7f6e\u4e3a \\(0\\) \u3001\u5176\u4f59 7 \u4f4d\u8bbe\u7f6e\u4e3a Unicode \u7801\u70b9\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cASCII \u5b57\u7b26\u5728 Unicode \u5b57\u7b26\u96c6\u4e2d\u5360\u636e\u4e86\u524d 128 \u4e2a\u7801\u70b9\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cUTF-8 \u7f16\u7801\u53ef\u4ee5\u5411\u4e0b\u517c\u5bb9 ASCII \u7801\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 UTF-8 \u6765\u89e3\u6790\u5e74\u4ee3\u4e45\u8fdc\u7684 ASCII \u7801\u6587\u672c\u3002</li> <li>\u5bf9\u4e8e\u957f\u5ea6\u4e3a \\(n\\) \u5b57\u8282\u7684\u5b57\u7b26\uff08\u5176\u4e2d \\(n &gt; 1\\)\uff09\uff0c\u5c06\u9996\u4e2a\u5b57\u8282\u7684\u9ad8 \\(n\\) \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(1\\) \u3001\u7b2c \\(n + 1\\) \u4f4d\u8bbe\u7f6e\u4e3a \\(0\\) \uff1b\u4ece\u7b2c\u4e8c\u4e2a\u5b57\u8282\u5f00\u59cb\uff0c\u5c06\u6bcf\u4e2a\u5b57\u8282\u7684\u9ad8 2 \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(10\\) \uff1b\u5176\u4f59\u6240\u6709\u4f4d\u7528\u4e8e\u586b\u5145\u5b57\u7b26\u7684 Unicode \u7801\u70b9\u3002</li> </ol> <p>\u4e0b\u56fe\u5c55\u793a\u4e86\u201cHello\u7b97\u6cd5\u201d\u5bf9\u5e94\u7684 UTF-8 \u7f16\u7801\u3002\u89c2\u5bdf\u53d1\u73b0\uff0c\u7531\u4e8e\u6700\u9ad8 \\(n\\) \u4f4d\u90fd\u88ab\u8bbe\u7f6e\u4e3a \\(1\\) \uff0c\u56e0\u6b64\u7cfb\u7edf\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6700\u9ad8\u4f4d \\(1\\) \u7684\u4e2a\u6570\u6765\u89e3\u6790\u51fa\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(n\\) \u3002</p> <p>\u4f46\u4e3a\u4ec0\u4e48\u8981\u5c06\u5176\u4f59\u6240\u6709\u5b57\u8282\u7684\u9ad8 2 \u4f4d\u90fd\u8bbe\u7f6e\u4e3a \\(10\\) \u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u8fd9\u4e2a \\(10\\) \u80fd\u591f\u8d77\u5230\u6821\u9a8c\u7b26\u7684\u4f5c\u7528\u3002\u5047\u8bbe\u7cfb\u7edf\u4ece\u4e00\u4e2a\u9519\u8bef\u7684\u5b57\u8282\u5f00\u59cb\u89e3\u6790\u6587\u672c\uff0c\u5b57\u8282\u5934\u90e8\u7684 \\(10\\) \u80fd\u591f\u5e2e\u52a9\u7cfb\u7edf\u5feb\u901f\u7684\u5224\u65ad\u51fa\u5f02\u5e38\u3002</p> <p>\u4e4b\u6240\u4ee5\u5c06 \\(10\\) \u5f53\u4f5c\u6821\u9a8c\u7b26\uff0c\u662f\u56e0\u4e3a\u5728 UTF-8 \u7f16\u7801\u89c4\u5219\u4e0b\uff0c\u4e0d\u53ef\u80fd\u6709\u5b57\u7b26\u7684\u6700\u9ad8\u4e24\u4f4d\u662f \\(10\\) \u3002\u8fd9\u4e2a\u7ed3\u8bba\u53ef\u4ee5\u7528\u53cd\u8bc1\u6cd5\u6765\u8bc1\u660e\uff1a\u5047\u8bbe\u4e00\u4e2a\u5b57\u7b26\u7684\u6700\u9ad8\u4e24\u4f4d\u662f \\(10\\) \uff0c\u8bf4\u660e\u8be5\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(1\\) \uff0c\u5bf9\u5e94 ASCII \u7801\u3002\u800c ASCII \u7801\u7684\u6700\u9ad8\u4f4d\u5e94\u8be5\u662f \\(0\\) \uff0c\u4e0e\u5047\u8bbe\u77db\u76fe\u3002</p> <p></p> <p> \u56fe\uff1aUTF-8 \u7f16\u7801\u793a\u4f8b </p> <p>\u9664\u4e86 UTF-8 \u4e4b\u5916\uff0c\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\u8fd8\u5305\u62ec\uff1a</p> <ul> <li>UTF-16 \u7f16\u7801\uff1a\u4f7f\u7528 2 \u6216 4 \u4e2a\u5b57\u8282\u6765\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\u3002\u6240\u6709\u7684 ASCII \u5b57\u7b26\u548c\u5e38\u7528\u7684\u975e\u82f1\u6587\u5b57\u7b26\uff0c\u90fd\u7528 2 \u4e2a\u5b57\u8282\u8868\u793a\uff1b\u5c11\u6570\u5b57\u7b26\u9700\u8981\u7528\u5230 4 \u4e2a\u5b57\u8282\u8868\u793a\u3002\u5bf9\u4e8e 2 \u5b57\u8282\u7684\u5b57\u7b26\uff0cUTF-16 \u7f16\u7801\u4e0e Unicode \u7801\u70b9\u76f8\u7b49\u3002</li> <li>UTF-32 \u7f16\u7801\uff1a\u6bcf\u4e2a\u5b57\u7b26\u90fd\u4f7f\u7528 4 \u4e2a\u5b57\u8282\u3002\u8fd9\u610f\u5473\u7740 UTF-32 \u4f1a\u6bd4 UTF-8 \u548c UTF-16 \u66f4\u5360\u7528\u7a7a\u95f4\uff0c\u7279\u522b\u662f\u5bf9\u4e8e ASCII \u5b57\u7b26\u5360\u6bd4\u8f83\u9ad8\u7684\u6587\u672c\u3002</li> </ul> <p>\u4ece\u5b58\u50a8\u7a7a\u95f4\u7684\u89d2\u5ea6\u770b\uff0c\u4f7f\u7528 UTF-8 \u8868\u793a\u82f1\u6587\u5b57\u7b26\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4ec5\u9700 1 \u4e2a\u5b57\u8282\uff1b\u4f7f\u7528 UTF-16 \u7f16\u7801\u67d0\u4e9b\u975e\u82f1\u6587\u5b57\u7b26\uff08\u4f8b\u5982\u4e2d\u6587\uff09\u4f1a\u66f4\u52a0\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u53ea\u9700\u8981 2 \u4e2a\u5b57\u8282\uff0c\u800c UTF-8 \u53ef\u80fd\u9700\u8981 3 \u4e2a\u5b57\u8282\u3002</p> <p>\u4ece\u517c\u5bb9\u6027\u7684\u89d2\u5ea6\u770b\uff0cUTF-8 \u7684\u901a\u7528\u6027\u6700\u4f73\uff0c\u8bb8\u591a\u5de5\u5177\u548c\u5e93\u90fd\u4f18\u5148\u652f\u6301 UTF-8 \u3002</p>"},{"location":"chapter_data_structure/character_encoding/#345","title":"3.4.5. \u00a0 \u7f16\u7a0b\u8bed\u8a00\u7684\u5b57\u7b26\u7f16\u7801","text":"<p>\u5bf9\u4e8e\u4ee5\u5f80\u7684\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\uff0c\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u7684\u5b57\u7b26\u4e32\u90fd\u91c7\u7528 UTF-16 \u6216 UTF-32 \u8fd9\u7c7b\u7b49\u957f\u7684\u7f16\u7801\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u7b49\u957f\u7f16\u7801\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u770b\u4f5c\u6570\u7ec4\u6765\u5904\u7406\uff0c\u5176\u4f18\u70b9\u5305\u62ec\uff1a</p> <ul> <li>\u968f\u673a\u8bbf\u95ee: UTF-16 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8fdb\u884c\u968f\u673a\u8bbf\u95ee\u3002UTF-8 \u662f\u4e00\u79cd\u53d8\u957f\u7f16\u7801\uff0c\u8981\u627e\u5230\u7b2c \\(i\\) \u4e2a\u5b57\u7b26\uff0c\u6211\u4eec\u9700\u8981\u4ece\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u5904\u904d\u5386\u5230\u7b2c \\(i\\) \u4e2a\u5b57\u7b26\uff0c\u8fd9\u9700\u8981 \\(O(n)\\) \u7684\u65f6\u95f4\u3002</li> <li>\u5b57\u7b26\u8ba1\u6570: \u4e0e\u968f\u673a\u8bbf\u95ee\u7c7b\u4f3c\uff0c\u8ba1\u7b97 UTF-16 \u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4e5f\u662f \\(O(1)\\) \u7684\u64cd\u4f5c\u3002\u4f46\u662f\uff0c\u8ba1\u7b97 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u9700\u8981\u904d\u5386\u6574\u4e2a\u5b57\u7b26\u4e32\u3002</li> <li>\u5b57\u7b26\u4e32\u64cd\u4f5c: \u5728 UTF-16 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u5f88\u591a\u5b57\u7b26\u4e32\u64cd\u4f5c\uff08\u5982\u5206\u5272\u3001\u8fde\u63a5\u3001\u63d2\u5165\u3001\u5220\u9664\u7b49\uff09\u90fd\u66f4\u5bb9\u6613\u8fdb\u884c\u3002\u5728 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u4e0a\u8fdb\u884c\u8fd9\u4e9b\u64cd\u4f5c\u901a\u5e38\u9700\u8981\u989d\u5916\u7684\u8ba1\u7b97\uff0c\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u4ea7\u751f\u65e0\u6548\u7684 UTF-8 \u7f16\u7801\u3002</li> </ul> <p>\u5b9e\u9645\u4e0a\uff0c\u7f16\u7a0b\u8bed\u8a00\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6848\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5f88\u6709\u8da3\u7684\u8bdd\u9898\uff0c\u5176\u6d89\u53ca\u5230\u8bb8\u591a\u56e0\u7d20\uff1a</p> <ul> <li>Java \u7684 <code>String</code> \u7c7b\u578b\u4f7f\u7528 UTF-16 \u7f16\u7801\uff0c\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\u3002\u8fd9\u662f\u56e0\u4e3a Java \u8bed\u8a00\u8bbe\u8ba1\u4e4b\u521d\uff0c\u4eba\u4eec\u8ba4\u4e3a 16 \u4f4d\u8db3\u4ee5\u8868\u793a\u6240\u6709\u53ef\u80fd\u7684\u5b57\u7b26\u3002\u7136\u800c\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e0d\u6b63\u786e\u7684\u5224\u65ad\u3002\u540e\u6765 Unicode \u89c4\u8303\u6269\u5c55\u5230\u4e86\u8d85\u8fc7 16 \u4f4d\uff0c\u6240\u4ee5 Java \u4e2d\u7684\u5b57\u7b26\u73b0\u5728\u53ef\u80fd\u7531\u4e00\u5bf9 16 \u4f4d\u7684\u503c\uff08\u79f0\u4e3a\u201c\u4ee3\u7406\u5bf9\u201d\uff09\u8868\u793a\u3002</li> <li>JavaScript \u548c TypeScript \u7684\u5b57\u7b26\u4e32\u4f7f\u7528 UTF-16 \u7f16\u7801\u7684\u539f\u56e0\u4e0e Java \u7c7b\u4f3c\u3002\u5f53 JavaScript \u8bed\u8a00\u5728 1995 \u5e74\u88ab Netscape \u516c\u53f8\u9996\u6b21\u5f15\u5165\u65f6\uff0cUnicode \u8fd8\u5904\u4e8e\u76f8\u5bf9\u65e9\u671f\u7684\u9636\u6bb5\uff0c\u90a3\u65f6\u5019\u4f7f\u7528 16 \u4f4d\u7684\u7f16\u7801\u5c31\u8db3\u591f\u8868\u793a\u6240\u6709\u7684 Unicode \u5b57\u7b26\u4e86\u3002</li> <li>C# \u4f7f\u7528 UTF-16 \u7f16\u7801\uff0c\u4e3b\u8981\u56e0\u4e3a .NET \u5e73\u53f0\u662f\u7531 Microsoft \u8bbe\u8ba1\u7684\uff0c\u800c Microsoft \u7684\u5f88\u591a\u6280\u672f\uff0c\u5305\u62ec Windows \u64cd\u4f5c\u7cfb\u7edf\uff0c\u90fd\u5e7f\u6cdb\u5730\u4f7f\u7528 UTF-16 \u7f16\u7801\u3002</li> </ul> <p>\u7531\u4e8e\u4ee5\u4e0a\u7f16\u7a0b\u8bed\u8a00\u5bf9\u5b57\u7b26\u6570\u91cf\u7684\u4f4e\u4f30\uff0c\u5b83\u4eec\u4e0d\u5f97\u4e0d\u91c7\u53d6\u201c\u4ee3\u7406\u5bf9\u201d\u7684\u65b9\u5f0f\u6765\u8868\u793a\u8d85\u8fc7 16 \u4f4d\u957f\u5ea6\u7684 Unicode \u5b57\u7b26\u3002\u8fd9\u662f\u4e00\u4e2a\u4e0d\u5f97\u5df2\u4e3a\u4e4b\u7684\u65e0\u5948\u4e4b\u4e3e\u3002\u4e00\u65b9\u9762\uff0c\u5305\u542b\u4ee3\u7406\u5bf9\u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u4e00\u4e2a\u5b57\u7b26\u53ef\u80fd\u5360\u7528 2 \u5b57\u8282\u6216 4 \u5b57\u8282\uff0c\u4ece\u800c\u4e27\u5931\u4e86\u7b49\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5904\u7406\u4ee3\u7406\u5bf9\u9700\u8981\u589e\u52a0\u989d\u5916\u4ee3\u7801\uff0c\u8fd9\u589e\u52a0\u4e86\u7f16\u7a0b\u7684\u590d\u6742\u6027\u548c Debug \u96be\u5ea6\u3002</p> <p>\u51fa\u4e8e\u4ee5\u4e0a\u539f\u56e0\uff0c\u90e8\u5206\u7f16\u7a0b\u8bed\u8a00\u63d0\u51fa\u4e86\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u6848\uff1a</p> <ul> <li>Python 3 \u4f7f\u7528\u4e00\u79cd\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u5b58\u50a8\u7684\u5b57\u7b26\u957f\u5ea6\u53d6\u51b3\u4e8e\u5b57\u7b26\u4e32\u4e2d\u6700\u5927\u7684 Unicode \u7801\u70b9\u3002\u5bf9\u4e8e\u5168\u90e8\u662f ASCII \u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u4e2a\u5b57\u8282\uff1b\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7684\u5b57\u7b26\u8d85\u51fa\u4e86 ASCII \u8303\u56f4\uff0c\u4f46\u5168\u90e8\u5728\u57fa\u672c\u591a\u8bed\u8a00\u5e73\u9762\uff08BMP\uff09\u5185\uff0c\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u4e2a\u5b57\u8282\uff1b\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u6709\u8d85\u51fa BMP \u7684\u5b57\u7b26\uff0c\u90a3\u4e48\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 4 \u4e2a\u5b57\u8282\u3002</li> <li>Go \u8bed\u8a00\u7684 <code>string</code> \u7c7b\u578b\u5728\u5185\u90e8\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002Go \u8bed\u8a00\u8fd8\u63d0\u4f9b\u4e86 <code>rune</code> \u7c7b\u578b\uff0c\u5b83\u7528\u4e8e\u8868\u793a\u5355\u4e2a Unicode \u7801\u70b9\u3002</li> <li>Rust \u8bed\u8a00\u7684 str \u548c String \u7c7b\u578b\u5728\u5185\u90e8\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002Rust \u4e5f\u63d0\u4f9b\u4e86 char \u7c7b\u578b\uff0c\u7528\u4e8e\u8868\u793a\u5355\u4e2a Unicode \u7801\u70b9\u3002</li> </ul> <p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ee5\u4e0a\u8ba8\u8bba\u7684\u90fd\u662f\u5b57\u7b26\u4e32\u5728\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u8fd9\u548c\u5b57\u7b26\u4e32\u5982\u4f55\u5728\u6587\u4ef6\u4e2d\u5b58\u50a8\u6216\u5728\u7f51\u7edc\u4e2d\u4f20\u8f93\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u95ee\u9898\u3002\u5728\u6587\u4ef6\u5b58\u50a8\u6216\u7f51\u7edc\u4f20\u8f93\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a UTF-8 \u683c\u5f0f\uff0c\u4ee5\u8fbe\u5230\u6700\u4f18\u7684\u517c\u5bb9\u6027\u548c\u7a7a\u95f4\u6548\u7387\u3002</p>"},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1. \u00a0 \u6570\u636e\u7ed3\u6784\u5206\u7c7b","text":"<p>\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\u5305\u62ec\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\uff0c\u5b83\u4eec\u53ef\u4ee5\u4ece\u201c\u903b\u8f91\u7ed3\u6784\u201d\u548c\u201c\u7269\u7406\u7ed3\u6784\u201d\u4e24\u4e2a\u7ef4\u5ea6\u8fdb\u884c\u5206\u7c7b\u3002</p>"},{"location":"chapter_data_structure/classification_of_data_structure/#311","title":"3.1.1. \u00a0 \u903b\u8f91\u7ed3\u6784\uff1a\u7ebf\u6027\u4e0e\u975e\u7ebf\u6027","text":"<p>\u300c\u903b\u8f91\u7ed3\u6784\u300d\u63ed\u793a\u4e86\u6570\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\u3002\u5728\u6570\u7ec4\u548c\u94fe\u8868\u4e2d\uff0c\u6570\u636e\u6309\u7167\u987a\u5e8f\u4f9d\u6b21\u6392\u5217\uff0c\u4f53\u73b0\u4e86\u6570\u636e\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\uff1b\u800c\u5728\u6811\u4e2d\uff0c\u6570\u636e\u4ece\u9876\u90e8\u5411\u4e0b\u6309\u5c42\u6b21\u6392\u5217\uff0c\u8868\u73b0\u51fa\u7956\u5148\u4e0e\u540e\u4ee3\u4e4b\u95f4\u7684\u6d3e\u751f\u5173\u7cfb\uff1b\u56fe\u5219\u7531\u8282\u70b9\u548c\u8fb9\u6784\u6210\uff0c\u53cd\u6620\u4e86\u590d\u6742\u7684\u7f51\u7edc\u5173\u7cfb\u3002</p> <p>\u903b\u8f91\u7ed3\u6784\u53ef\u88ab\u5206\u4e3a\u201c\u7ebf\u6027\u201d\u548c\u201c\u975e\u7ebf\u6027\u201d\u4e24\u5927\u7c7b\u3002\u7ebf\u6027\u7ed3\u6784\u6bd4\u8f83\u76f4\u89c2\uff0c\u6307\u6570\u636e\u5728\u903b\u8f91\u5173\u7cfb\u4e0a\u5448\u7ebf\u6027\u6392\u5217\uff1b\u975e\u7ebf\u6027\u7ed3\u6784\u5219\u76f8\u53cd\uff0c\u5448\u975e\u7ebf\u6027\u6392\u5217\u3002</p> <ul> <li>\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1a\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3002</li> <li>\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1a\u6811\u3001\u5806\u3001\u56fe\u3001\u54c8\u5e0c\u8868\u3002</li> </ul> <p></p> <p> \u56fe\uff1a\u7ebf\u6027\u4e0e\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784 </p> <p>\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u8fdb\u4e00\u6b65\u88ab\u5212\u5206\u4e3a\u6811\u5f62\u7ed3\u6784\u548c\u7f51\u72b6\u7ed3\u6784\u3002</p> <ul> <li>\u7ebf\u6027\u7ed3\u6784\uff1a\u6570\u7ec4\u3001\u94fe\u8868\u3001\u961f\u5217\u3001\u6808\u3001\u54c8\u5e0c\u8868\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u4e00\u5bf9\u4e00\u7684\u987a\u5e8f\u5173\u7cfb\u3002</li> <li>\u6811\u5f62\u7ed3\u6784\uff1a\u6811\u3001\u5806\u3001\u54c8\u5e0c\u8868\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u4e00\u5bf9\u591a\u7684\u5173\u7cfb\u3002</li> <li>\u7f51\u72b6\u7ed3\u6784\uff1a\u56fe\uff0c\u5143\u7d20\u4e4b\u95f4\u662f\u591a\u5bf9\u591a\u7684\u5173\u7cfb\u3002</li> </ul>"},{"location":"chapter_data_structure/classification_of_data_structure/#312","title":"3.1.2. \u00a0 \u7269\u7406\u7ed3\u6784\uff1a\u8fde\u7eed\u4e0e\u79bb\u6563","text":"<p>\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u5185\u5b58\u548c\u786c\u76d8\u662f\u4e24\u79cd\u4e3b\u8981\u7684\u5b58\u50a8\u786c\u4ef6\u8bbe\u5907\u3002\u786c\u76d8\u4e3b\u8981\u7528\u4e8e\u957f\u671f\u5b58\u50a8\u6570\u636e\uff0c\u5bb9\u91cf\u8f83\u5927\uff08\u901a\u5e38\u53ef\u8fbe\u5230 TB \u7ea7\u522b\uff09\u3001\u901f\u5ea6\u8f83\u6162\u3002\u5185\u5b58\u7528\u4e8e\u8fd0\u884c\u7a0b\u5e8f\u65f6\u6682\u5b58\u6570\u636e\uff0c\u901f\u5ea6\u8f83\u5feb\uff0c\u4f46\u5bb9\u91cf\u8f83\u5c0f\uff08\u901a\u5e38\u4e3a GB \u7ea7\u522b\uff09\u3002</p> <p>\u5728\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u76f8\u5173\u6570\u636e\u90fd\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u3002\u4e0b\u56fe\u5c55\u793a\u4e86\u4e00\u4e2a\u8ba1\u7b97\u673a\u5185\u5b58\u6761\uff0c\u5176\u4e2d\u6bcf\u4e2a\u9ed1\u8272\u65b9\u5757\u90fd\u5305\u542b\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5185\u5b58\u60f3\u8c61\u6210\u4e00\u4e2a\u5de8\u5927\u7684 Excel \u8868\u683c\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5355\u5143\u683c\u90fd\u53ef\u4ee5\u5b58\u50a8\u4e00\u5b9a\u5927\u5c0f\u7684\u6570\u636e\uff0c\u5728\u7b97\u6cd5\u8fd0\u884c\u65f6\uff0c\u6240\u6709\u6570\u636e\u90fd\u88ab\u5b58\u50a8\u5728\u8fd9\u4e9b\u5355\u5143\u683c\u4e2d\u3002</p> <p>\u7cfb\u7edf\u901a\u8fc7\u5185\u5b58\u5730\u5740\u6765\u8bbf\u95ee\u76ee\u6807\u4f4d\u7f6e\u7684\u6570\u636e\u3002\u8ba1\u7b97\u673a\u6839\u636e\u7279\u5b9a\u89c4\u5219\u4e3a\u8868\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5206\u914d\u7f16\u53f7\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5185\u5b58\u7a7a\u95f4\u90fd\u6709\u552f\u4e00\u7684\u5185\u5b58\u5730\u5740\u3002\u6709\u4e86\u8fd9\u4e9b\u5730\u5740\uff0c\u7a0b\u5e8f\u4fbf\u53ef\u4ee5\u8bbf\u95ee\u5185\u5b58\u4e2d\u7684\u6570\u636e\u3002</p> <p></p> <p> \u56fe\uff1a\u5185\u5b58\u6761\u3001\u5185\u5b58\u7a7a\u95f4\u3001\u5185\u5b58\u5730\u5740 </p> <p>\u5185\u5b58\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u5171\u4eab\u8d44\u6e90\uff0c\u5f53\u67d0\u5757\u5185\u5b58\u88ab\u67d0\u4e2a\u7a0b\u5e8f\u5360\u7528\u65f6\uff0c\u5219\u65e0\u6cd5\u88ab\u5176\u4ed6\u7a0b\u5e8f\u540c\u65f6\u4f7f\u7528\u4e86\u3002\u56e0\u6b64\u5728\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8bbe\u8ba1\u4e2d\uff0c\u5185\u5b58\u8d44\u6e90\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u6bd4\u5982\uff0c\u7b97\u6cd5\u6240\u5360\u7528\u7684\u5185\u5b58\u5cf0\u503c\u4e0d\u5e94\u8d85\u8fc7\u7cfb\u7edf\u5269\u4f59\u7a7a\u95f2\u5185\u5b58\uff1b\u5982\u679c\u7f3a\u5c11\u8fde\u7eed\u5927\u5757\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u90a3\u4e48\u6240\u9009\u7528\u7684\u6570\u636e\u7ed3\u6784\u5fc5\u987b\u80fd\u591f\u5b58\u50a8\u5728\u79bb\u6563\u7684\u5185\u5b58\u7a7a\u95f4\u5185\u3002</p> <p>\u300c\u7269\u7406\u7ed3\u6784\u300d\u53cd\u6620\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u53ef\u5206\u4e3a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\uff08\u6570\u7ec4\uff09\u548c\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\u3002\u7269\u7406\u7ed3\u6784\u4ece\u5e95\u5c42\u51b3\u5b9a\u4e86\u6570\u636e\u7684\u8bbf\u95ee\u3001\u66f4\u65b0\u3001\u589e\u5220\u7b49\u64cd\u4f5c\u65b9\u6cd5\uff0c\u540c\u65f6\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u65b9\u9762\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u70b9\u3002</p> <p></p> <p> \u56fe\uff1a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u4e0e\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8 </p> <p>\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u57fa\u4e8e\u6570\u7ec4\u3001\u94fe\u8868\u6216\u4e8c\u8005\u7684\u7ec4\u5408\u5b9e\u73b0\u7684\u3002\u4f8b\u5982\uff0c\u6808\u548c\u961f\u5217\u65e2\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\uff1b\u800c\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0\u53ef\u80fd\u540c\u65f6\u5305\u542b\u6570\u7ec4\u548c\u94fe\u8868\u3002</p> <ul> <li>\u57fa\u4e8e\u6570\u7ec4\u53ef\u5b9e\u73b0\uff1a\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u3001\u77e9\u9635\u3001\u5f20\u91cf\uff08\u7ef4\u5ea6 \\(\\geq 3\\) \u7684\u6570\u7ec4\uff09\u7b49\u3002</li> <li>\u57fa\u4e8e\u94fe\u8868\u53ef\u5b9e\u73b0\uff1a\u6808\u3001\u961f\u5217\u3001\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\u3002</li> </ul> <p>\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u4e5f\u88ab\u79f0\u4e3a\u201c\u9759\u6001\u6570\u636e\u7ed3\u6784\u201d\uff0c\u8fd9\u610f\u5473\u7740\u6b64\u7c7b\u6570\u636e\u7ed3\u6784\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u76f8\u5bf9\u5e94\u5730\uff0c\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u88ab\u79f0\u4e3a\u201c\u52a8\u6001\u6570\u636e\u7ed3\u6784\u201d\uff0c\u8fd9\u7c7b\u6570\u636e\u7ed3\u6784\u5728\u521d\u59cb\u5316\u540e\uff0c\u4ecd\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u5bf9\u5176\u957f\u5ea6\u8fdb\u884c\u8c03\u6574\u3002</p> <p>Tip</p> <p>\u5982\u82e5\u611f\u89c9\u7406\u89e3\u7269\u7406\u7ed3\u6784\u6709\u56f0\u96be\uff0c\u5efa\u8bae\u5148\u9605\u8bfb\u4e0b\u4e00\u7ae0\u201c\u6570\u7ec4\u4e0e\u94fe\u8868\u201d\uff0c\u7136\u540e\u518d\u56de\u987e\u672c\u8282\u5185\u5bb9\u3002</p>"},{"location":"chapter_data_structure/number_encoding/","title":"3.3. \u00a0 \u6570\u5b57\u7f16\u7801 *","text":"<p>Note</p> <p>\u5728\u672c\u4e66\u4e2d\uff0c\u6807\u9898\u5e26\u6709\u7684 * \u7b26\u53f7\u7684\u662f\u9009\u8bfb\u7ae0\u8282\u3002\u5982\u679c\u4f60\u65f6\u95f4\u6709\u9650\u6216\u611f\u5230\u7406\u89e3\u56f0\u96be\uff0c\u53ef\u4ee5\u5148\u8df3\u8fc7\uff0c\u7b49\u5b66\u5b8c\u5fc5\u8bfb\u7ae0\u8282\u540e\u518d\u5355\u72ec\u653b\u514b\u3002</p>"},{"location":"chapter_data_structure/number_encoding/#331","title":"3.3.1. \u00a0 \u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801","text":"<p>\u4ece\u4e0a\u4e00\u8282\u7684\u8868\u683c\u4e2d\u6211\u4eec\u53d1\u73b0\uff0c\u6240\u6709\u6574\u6570\u7c7b\u578b\u80fd\u591f\u8868\u793a\u7684\u8d1f\u6570\u90fd\u6bd4\u6b63\u6570\u591a\u4e00\u4e2a\u3002\u4f8b\u5982\uff0c<code>byte</code> \u7684\u53d6\u503c\u8303\u56f4\u662f \\([-128, 127]\\) \u3002\u8fd9\u4e2a\u73b0\u8c61\u6bd4\u8f83\u53cd\u76f4\u89c9\uff0c\u5b83\u7684\u5185\u5728\u539f\u56e0\u6d89\u53ca\u5230\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u76f8\u5173\u77e5\u8bc6\u3002</p> <p>\u5728\u5c55\u5f00\u5206\u6790\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u7ed9\u51fa\u4e09\u8005\u7684\u5b9a\u4e49\uff1a</p> <ul> <li>\u539f\u7801\uff1a\u6211\u4eec\u5c06\u6570\u5b57\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u7684\u6700\u9ad8\u4f4d\u89c6\u4e3a\u7b26\u53f7\u4f4d\uff0c\u5176\u4e2d \\(0\\) \u8868\u793a\u6b63\u6570\uff0c\\(1\\) \u8868\u793a\u8d1f\u6570\uff0c\u5176\u4f59\u4f4d\u8868\u793a\u6570\u5b57\u7684\u503c\u3002</li> <li>\u53cd\u7801\uff1a\u6b63\u6570\u7684\u53cd\u7801\u4e0e\u5176\u539f\u7801\u76f8\u540c\uff0c\u8d1f\u6570\u7684\u53cd\u7801\u662f\u5bf9\u5176\u539f\u7801\u9664\u7b26\u53f7\u4f4d\u5916\u7684\u6240\u6709\u4f4d\u53d6\u53cd\u3002</li> <li>\u8865\u7801\uff1a\u6b63\u6570\u7684\u8865\u7801\u4e0e\u5176\u539f\u7801\u76f8\u540c\uff0c\u8d1f\u6570\u7684\u8865\u7801\u662f\u5728\u5176\u53cd\u7801\u7684\u57fa\u7840\u4e0a\u52a0 \\(1\\) \u3002</li> </ul> <p></p> <p> \u56fe\uff1a\u539f\u7801\u3001\u53cd\u7801\u4e0e\u8865\u7801\u4e4b\u95f4\u7684\u76f8\u4e92\u8f6c\u6362 </p> <p>\u663e\u7136\u300c\u539f\u7801\u300d\u6700\u4e3a\u76f4\u89c2\u3002\u4f46\u5b9e\u9645\u4e0a\uff0c\u6570\u5b57\u662f\u4ee5\u300c\u8865\u7801\u300d\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u539f\u7801\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002</p> <p>\u4e00\u65b9\u9762\uff0c\u8d1f\u6570\u7684\u539f\u7801\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u5728\u539f\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f \\(-3\\) \uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002</p> \\[ \\begin{aligned} &amp; 1 + (-2) \\newline &amp; = 0000 \\space 0001 + 1000 \\space 0010 \\newline &amp; = 1000 \\space 0011 \\newline &amp; = -3 \\end{aligned} \\] <p>\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8ba1\u7b97\u673a\u5f15\u5165\u4e86\u300c\u53cd\u7801\u300d\u3002\u5982\u679c\u6211\u4eec\u5148\u5c06\u539f\u7801\u8f6c\u6362\u4e3a\u53cd\u7801\uff0c\u5e76\u5728\u53cd\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u6700\u540e\u5c06\u7ed3\u679c\u4ece\u53cd\u7801\u8f6c\u5316\u56de\u539f\u7801\uff0c\u5219\u53ef\u5f97\u5230\u6b63\u786e\u7ed3\u679c \\(-1\\) \u3002</p> \\[ \\begin{aligned} &amp; 1 + (-2) \\newline &amp; \\rightarrow 0000 \\space 0001 \\space \\text{(\u539f\u7801)} + 1000 \\space 0010 \\space \\text{(\u539f\u7801)} \\newline &amp; = 0000 \\space 0001 \\space \\text{(\u53cd\u7801)} + 1111 \\space 1101 \\space \\text{(\u53cd\u7801)} \\newline &amp; = 1111 \\space 1110 \\space \\text{(\u53cd\u7801)} \\newline &amp; = 1000 \\space 0001 \\space \\text{(\u539f\u7801)} \\newline &amp; \\rightarrow -1 \\end{aligned} \\] <p>\u53e6\u4e00\u65b9\u9762\uff0c\u6570\u5b57\u96f6\u7684\u539f\u7801\u6709 \\(+0\\) \u548c \\(-0\\) \u4e24\u79cd\u8868\u793a\u65b9\u5f0f\u3002\u8fd9\u610f\u5473\u7740\u6570\u5b57\u96f6\u5bf9\u5e94\u7740\u4e24\u4e2a\u4e0d\u540c\u7684\u4e8c\u8fdb\u5236\u7f16\u7801\uff0c\u5176\u53ef\u80fd\u4f1a\u5e26\u6765\u6b67\u4e49\u3002\u6bd4\u5982\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u533a\u5206\u6b63\u96f6\u548c\u8d1f\u96f6\uff0c\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5224\u65ad\u7ed3\u679c\u51fa\u9519\u3002\u800c\u5982\u679c\u6211\u4eec\u60f3\u8981\u5904\u7406\u6b63\u96f6\u548c\u8d1f\u96f6\u6b67\u4e49\uff0c\u5219\u9700\u8981\u5f15\u5165\u989d\u5916\u7684\u5224\u65ad\u64cd\u4f5c\uff0c\u5176\u53ef\u80fd\u4f1a\u964d\u4f4e\u8ba1\u7b97\u673a\u7684\u8fd0\u7b97\u6548\u7387\u3002</p> \\[ \\begin{aligned} +0 &amp; = 0000 \\space 0000 \\newline -0 &amp; = 1000 \\space 0000 \\end{aligned} \\] <p>\u4e0e\u539f\u7801\u4e00\u6837\uff0c\u53cd\u7801\u4e5f\u5b58\u5728\u6b63\u8d1f\u96f6\u6b67\u4e49\u95ee\u9898\uff0c\u56e0\u6b64\u8ba1\u7b97\u673a\u8fdb\u4e00\u6b65\u5f15\u5165\u4e86\u300c\u8865\u7801\u300d\u3002\u6211\u4eec\u5148\u6765\u89c2\u5bdf\u4e00\u4e0b\u8d1f\u96f6\u7684\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u8f6c\u6362\u8fc7\u7a0b\uff1a</p> \\[ \\begin{aligned} -0 = \\space &amp; 1000 \\space 0000 \\space \\text{(\u539f\u7801)} \\newline = \\space &amp; 1111 \\space 1111 \\space \\text{(\u53cd\u7801)} \\newline = 1 \\space &amp; 0000 \\space 0000 \\space \\text{(\u8865\u7801)} \\newline \\end{aligned} \\] <p>\u5728\u8d1f\u96f6\u7684\u53cd\u7801\u57fa\u7840\u4e0a\u52a0 \\(1\\) \u4f1a\u4ea7\u751f\u8fdb\u4f4d\uff0c\u4f46 <code>byte</code> \u7c7b\u578b\u7684\u957f\u5ea6\u53ea\u6709 8 \u4f4d\uff0c\u56e0\u6b64\u6ea2\u51fa\u5230\u7b2c 9 \u4f4d\u7684 \\(1\\) \u4f1a\u88ab\u820d\u5f03\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8d1f\u96f6\u7684\u8865\u7801\u4e3a \\(0000 \\space 0000\\) \uff0c\u4e0e\u6b63\u96f6\u7684\u8865\u7801\u76f8\u540c\u3002\u8fd9\u610f\u5473\u7740\u5728\u8865\u7801\u8868\u793a\u4e2d\u53ea\u5b58\u5728\u4e00\u4e2a\u96f6\uff0c\u6b63\u8d1f\u96f6\u6b67\u4e49\u4ece\u800c\u5f97\u5230\u89e3\u51b3\u3002</p> <p>\u8fd8\u5269\u4f59\u6700\u540e\u4e00\u4e2a\u7591\u60d1\uff1a<code>byte</code> \u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u662f \\([-128, 127]\\) \uff0c\u591a\u51fa\u6765\u7684\u4e00\u4e2a\u8d1f\u6570 \\(-128\\) \u662f\u5982\u4f55\u5f97\u5230\u7684\u5462\uff1f\u6211\u4eec\u6ce8\u610f\u5230\uff0c\u533a\u95f4 \\([-127, +127]\\) \u5185\u7684\u6240\u6709\u6574\u6570\u90fd\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\uff0c\u5e76\u4e14\u539f\u7801\u548c\u8865\u7801\u4e4b\u95f4\u662f\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\u7684\u3002</p> <p>\u7136\u800c\uff0c\u8865\u7801 \\(1000 \\space 0000\\) \u662f\u4e00\u4e2a\u4f8b\u5916\uff0c\u5b83\u5e76\u6ca1\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3002\u6839\u636e\u8f6c\u6362\u65b9\u6cd5\uff0c\u6211\u4eec\u5f97\u5230\u8be5\u8865\u7801\u7684\u539f\u7801\u4e3a \\(0000 \\space 0000\\) \u3002\u8fd9\u663e\u7136\u662f\u77db\u76fe\u7684\uff0c\u56e0\u4e3a\u8be5\u539f\u7801\u8868\u793a\u6570\u5b57 \\(0\\) \uff0c\u5b83\u7684\u8865\u7801\u5e94\u8be5\u662f\u81ea\u8eab\u3002\u8ba1\u7b97\u673a\u89c4\u5b9a\u8fd9\u4e2a\u7279\u6b8a\u7684\u8865\u7801 \\(1000 \\space 0000\\) \u4ee3\u8868 \\(-128\\) \u3002\u5b9e\u9645\u4e0a\uff0c\\((-1) + (-127)\\) \u5728\u8865\u7801\u4e0b\u7684\u8ba1\u7b97\u7ed3\u679c\u5c31\u662f \\(-128\\) \u3002</p> \\[ \\begin{aligned} &amp; (-127) + (-1) \\newline &amp; \\rightarrow 1111 \\space 1111 \\space \\text{(\u539f\u7801)} + 1000 \\space 0001 \\space \\text{(\u539f\u7801)} \\newline &amp; = 1000 \\space 0000 \\space \\text{(\u53cd\u7801)} + 1111 \\space 1110 \\space \\text{(\u53cd\u7801)} \\newline &amp; = 1000 \\space 0001 \\space \\text{(\u8865\u7801)} + 1111 \\space 1111 \\space \\text{(\u8865\u7801)} \\newline &amp; = 1000 \\space 0000 \\space \\text{(\u8865\u7801)} \\newline &amp; \\rightarrow -128 \\end{aligned} \\] <p>\u4f60\u53ef\u80fd\u5df2\u7ecf\u53d1\u73b0\uff0c\u4e0a\u8ff0\u7684\u6240\u6709\u8ba1\u7b97\u90fd\u662f\u52a0\u6cd5\u8fd0\u7b97\u3002\u8fd9\u6697\u793a\u7740\u4e00\u4e2a\u91cd\u8981\u4e8b\u5b9e\uff1a\u8ba1\u7b97\u673a\u5185\u90e8\u7684\u786c\u4ef6\u7535\u8def\u4e3b\u8981\u662f\u57fa\u4e8e\u52a0\u6cd5\u8fd0\u7b97\u8bbe\u8ba1\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u52a0\u6cd5\u8fd0\u7b97\u76f8\u5bf9\u4e8e\u5176\u4ed6\u8fd0\u7b97\uff08\u6bd4\u5982\u4e58\u6cd5\u3001\u9664\u6cd5\u548c\u51cf\u6cd5\uff09\u6765\u8bf4\uff0c\u786c\u4ef6\u5b9e\u73b0\u8d77\u6765\u66f4\u7b80\u5355\uff0c\u66f4\u5bb9\u6613\u8fdb\u884c\u5e76\u884c\u5316\u5904\u7406\uff0c\u8fd0\u7b97\u901f\u5ea6\u66f4\u5feb\u3002</p> <p>\u8bf7\u6ce8\u610f\uff0c\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u8ba1\u7b97\u673a\u53ea\u80fd\u505a\u52a0\u6cd5\u3002\u901a\u8fc7\u5c06\u52a0\u6cd5\u4e0e\u4e00\u4e9b\u57fa\u672c\u903b\u8f91\u8fd0\u7b97\u7ed3\u5408\uff0c\u8ba1\u7b97\u673a\u80fd\u591f\u5b9e\u73b0\u5404\u79cd\u5176\u4ed6\u7684\u6570\u5b66\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u51cf\u6cd5 \\(a - b\\) \u53ef\u4ee5\u8f6c\u6362\u4e3a\u8ba1\u7b97\u52a0\u6cd5 \\(a + (-b)\\) \uff1b\u8ba1\u7b97\u4e58\u6cd5\u548c\u9664\u6cd5\u53ef\u4ee5\u8f6c\u6362\u4e3a\u8ba1\u7b97\u591a\u6b21\u52a0\u6cd5\u6216\u51cf\u6cd5\u3002</p> <p>\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u8ba1\u7b97\u673a\u4f7f\u7528\u8865\u7801\u7684\u539f\u56e0\uff1a\u57fa\u4e8e\u8865\u7801\u8868\u793a\uff0c\u8ba1\u7b97\u673a\u53ef\u4ee5\u7528\u540c\u6837\u7684\u7535\u8def\u548c\u64cd\u4f5c\u6765\u5904\u7406\u6b63\u6570\u548c\u8d1f\u6570\u7684\u52a0\u6cd5\uff0c\u4e0d\u9700\u8981\u8bbe\u8ba1\u7279\u6b8a\u7684\u786c\u4ef6\u7535\u8def\u6765\u5904\u7406\u51cf\u6cd5\uff0c\u5e76\u4e14\u65e0\u9700\u7279\u522b\u5904\u7406\u6b63\u8d1f\u96f6\u7684\u6b67\u4e49\u95ee\u9898\u3002\u8fd9\u5927\u5927\u7b80\u5316\u4e86\u786c\u4ef6\u8bbe\u8ba1\uff0c\u63d0\u9ad8\u4e86\u8fd0\u7b97\u6548\u7387\u3002</p> <p>\u8865\u7801\u7684\u8bbe\u8ba1\u975e\u5e38\u7cbe\u5999\uff0c\u56e0\u7bc7\u5e45\u5173\u7cfb\u6211\u4eec\u5c31\u5148\u4ecb\u7ecd\u5230\u8fd9\u91cc\uff0c\u5efa\u8bae\u6709\u5174\u8da3\u7684\u8bfb\u8005\u8fdb\u4e00\u6b65\u6df1\u5ea6\u4e86\u89e3\u3002</p>"},{"location":"chapter_data_structure/number_encoding/#332","title":"3.3.2. \u00a0 \u6d6e\u70b9\u6570\u7f16\u7801","text":"<p>\u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\uff1a<code>int</code> \u548c <code>float</code> \u957f\u5ea6\u76f8\u540c\uff0c\u90fd\u662f 4 bytes\uff0c\u4f46\u4e3a\u4ec0\u4e48 <code>float</code> \u7684\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e <code>int</code> \uff1f\u8fd9\u975e\u5e38\u53cd\u76f4\u89c9\uff0c\u56e0\u4e3a\u6309\u7406\u8bf4 <code>float</code> \u9700\u8981\u8868\u793a\u5c0f\u6570\uff0c\u53d6\u503c\u8303\u56f4\u5e94\u8be5\u53d8\u5c0f\u624d\u5bf9\u3002</p> <p>\u5b9e\u9645\u4e0a\uff0c\u8fd9\u662f\u56e0\u4e3a\u6d6e\u70b9\u6570 <code>float</code> \u91c7\u7528\u4e86\u4e0d\u540c\u7684\u8868\u793a\u65b9\u5f0f\u3002\u8bb0\u4e00\u4e2a 32-bit \u957f\u5ea6\u7684\u4e8c\u8fdb\u5236\u6570\u4e3a\uff1a</p> \\[ b_{31} b_{30} b_{29} \\ldots b_2 b_1 b_0 \\] <p>\u6839\u636e IEEE 754 \u6807\u51c6\uff0c32-bit \u957f\u5ea6\u7684 <code>float</code> \u7531\u4ee5\u4e0b\u90e8\u5206\u6784\u6210\uff1a</p> <ul> <li>\u7b26\u53f7\u4f4d \\(\\mathrm{S}\\) \uff1a\u5360 1 bit \uff0c\u5bf9\u5e94 \\(b_{31}\\) \u3002</li> <li>\u6307\u6570\u4f4d \\(\\mathrm{E}\\) \uff1a\u5360 8 bits \uff0c\u5bf9\u5e94 \\(b_{30} b_{29} \\ldots b_{23}\\) \u3002</li> <li>\u5206\u6570\u4f4d \\(\\mathrm{N}\\) \uff1a\u5360 23 bits \uff0c\u5bf9\u5e94 \\(b_{22} b_{21} \\ldots b_0\\) \u3002</li> </ul> <p>\u4e8c\u8fdb\u5236\u6570 <code>float</code> \u5bf9\u5e94\u7684\u503c\u7684\u8ba1\u7b97\u65b9\u6cd5\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>\u8f6c\u5316\u5230\u5341\u8fdb\u5236\u4e0b\u7684\u8ba1\u7b97\u516c\u5f0f\uff1a</p> \\[ \\text {val}=(-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} -127} \\times (1 + \\mathrm{N}) \\] <p>\u5176\u4e2d\u5404\u9879\u7684\u53d6\u503c\u8303\u56f4\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> \u56fe\uff1aIEEE 754 \u6807\u51c6\u4e0b\u7684 float \u8868\u793a\u65b9\u5f0f </p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u793a\u4f8b\u6570\u636e \\(\\mathrm{S} = 0\\) \uff0c \\(\\mathrm{E} = 124\\) \uff0c\\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\) \uff0c\u5219\u6709\uff1a</p> \\[ \\text { val } = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\] <p>\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u56de\u7b54\u6700\u521d\u7684\u95ee\u9898\uff1a<code>float</code> \u7684\u8868\u793a\u65b9\u5f0f\u5305\u542b\u6307\u6570\u4f4d\uff0c\u5bfc\u81f4\u5176\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e <code>int</code> \u3002\u6839\u636e\u4ee5\u4e0a\u8ba1\u7b97\uff0c<code>float</code> \u53ef\u8868\u793a\u7684\u6700\u5927\u6b63\u6570\u4e3a \\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\) \uff0c\u5207\u6362\u7b26\u53f7\u4f4d\u4fbf\u53ef\u5f97\u5230\u6700\u5c0f\u8d1f\u6570\u3002</p> <p>\u5c3d\u7ba1\u6d6e\u70b9\u6570 <code>float</code> \u6269\u5c55\u4e86\u53d6\u503c\u8303\u56f4\uff0c\u4f46\u5176\u526f\u4f5c\u7528\u662f\u727a\u7272\u4e86\u7cbe\u5ea6\u3002\u6574\u6570\u7c7b\u578b <code>int</code> \u5c06\u5168\u90e8 32 \u4f4d\u7528\u4e8e\u8868\u793a\u6570\u5b57\uff0c\u6570\u5b57\u662f\u5747\u5300\u5206\u5e03\u7684\uff1b\u800c\u7531\u4e8e\u6307\u6570\u4f4d\u7684\u5b58\u5728\uff0c\u6d6e\u70b9\u6570 <code>float</code> \u7684\u6570\u503c\u8d8a\u5927\uff0c\u76f8\u90bb\u4e24\u4e2a\u6570\u5b57\u4e4b\u95f4\u7684\u5dee\u503c\u5c31\u4f1a\u8d8b\u5411\u8d8a\u5927\u3002</p> <p>\u8fdb\u4e00\u6b65\u5730\uff0c\u6307\u6570\u4f4d \\(E = 0\\) \u548c \\(E = 255\\) \u5177\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u7528\u4e8e\u8868\u793a\u96f6\u3001\u65e0\u7a77\u5927\u3001\\(\\mathrm{NaN}\\) \u7b49\u3002</p> <p> \u8868\uff1a\u6307\u6570\u4f4d\u542b\u4e49 </p> \u6307\u6570\u4f4d E \u5206\u6570\u4f4d \\(\\mathrm{N} = 0\\) \u5206\u6570\u4f4d \\(\\mathrm{N} \\ne 0\\) \u8ba1\u7b97\u516c\u5f0f \\(0\\) \\(\\pm 0\\) \u6b21\u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) \u6b63\u89c4\u6570 \u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\) <p>\u7279\u522b\u5730\uff0c\u6b21\u6b63\u89c4\u6570\u663e\u8457\u63d0\u5347\u4e86\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\uff0c\u8fd9\u662f\u56e0\u4e3a\uff1a</p> <ul> <li>\u6700\u5c0f\u6b63\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\approx 1.18 \\times 10^{-38}\\) \u3002</li> <li>\u6700\u5c0f\u6b63\u6b21\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\times 2^{-23} \\approx 1.4 \\times 10^{-45}\\) \u3002</li> </ul> <p>\u53cc\u7cbe\u5ea6 <code>double</code> \u4e5f\u91c7\u7528\u7c7b\u4f3c <code>float</code> \u7684\u8868\u793a\u65b9\u6cd5\uff0c\u6b64\u5904\u4e0d\u518d\u8be6\u8ff0\u3002</p>"},{"location":"chapter_data_structure/summary/","title":"3.5. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u4ece\u903b\u8f91\u7ed3\u6784\u548c\u7269\u7406\u7ed3\u6784\u4e24\u4e2a\u89d2\u5ea6\u8fdb\u884c\u5206\u7c7b\u3002\u903b\u8f91\u7ed3\u6784\u63cf\u8ff0\u4e86\u6570\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\uff0c\u800c\u7269\u7406\u7ed3\u6784\u63cf\u8ff0\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\u3002</li> <li>\u5e38\u89c1\u7684\u903b\u8f91\u7ed3\u6784\u5305\u62ec\u7ebf\u6027\u3001\u6811\u72b6\u548c\u7f51\u72b6\u7b49\u3002\u901a\u5e38\u6211\u4eec\u6839\u636e\u903b\u8f91\u7ed3\u6784\u5c06\u6570\u636e\u7ed3\u6784\u5206\u4e3a\u7ebf\u6027\uff08\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\uff09\u548c\u975e\u7ebf\u6027\uff08\u6811\u3001\u56fe\u3001\u5806\uff09\u4e24\u79cd\u3002\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0\u53ef\u80fd\u540c\u65f6\u5305\u542b\u7ebf\u6027\u548c\u975e\u7ebf\u6027\u7ed3\u6784\u3002</li> <li>\u5f53\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6570\u636e\u88ab\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u3002\u6bcf\u4e2a\u5185\u5b58\u7a7a\u95f4\u90fd\u62e5\u6709\u5bf9\u5e94\u7684\u5185\u5b58\u5730\u5740\uff0c\u7a0b\u5e8f\u901a\u8fc7\u8fd9\u4e9b\u5185\u5b58\u5730\u5740\u8bbf\u95ee\u6570\u636e\u3002</li> <li>\u7269\u7406\u7ed3\u6784\u4e3b\u8981\u5206\u4e3a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\uff08\u6570\u7ec4\uff09\u548c\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\u3002\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u7531\u6570\u7ec4\u3001\u94fe\u8868\u6216\u4e24\u8005\u7684\u7ec4\u5408\u5b9e\u73b0\u7684\u3002</li> <li>\u8ba1\u7b97\u673a\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5305\u62ec\u6574\u6570 <code>byte</code> , <code>short</code> , <code>int</code> , <code>long</code> \u3001\u6d6e\u70b9\u6570 <code>float</code> , <code>double</code> \u3001\u5b57\u7b26 <code>char</code> \u548c\u5e03\u5c14 <code>boolean</code> \u3002\u5b83\u4eec\u7684\u53d6\u503c\u8303\u56f4\u53d6\u51b3\u4e8e\u5360\u7528\u7a7a\u95f4\u5927\u5c0f\u548c\u8868\u793a\u65b9\u5f0f\u3002</li> <li>\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\u662f\u5728\u8ba1\u7b97\u673a\u4e2d\u7f16\u7801\u6570\u5b57\u7684\u4e09\u79cd\u65b9\u6cd5\uff0c\u5b83\u4eec\u4e4b\u95f4\u662f\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\u7684\u3002\u6574\u6570\u7684\u539f\u7801\u7684\u6700\u9ad8\u4f4d\u662f\u7b26\u53f7\u4f4d\uff0c\u5176\u4f59\u4f4d\u662f\u6570\u5b57\u7684\u503c\u3002</li> <li>\u6574\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u662f\u4ee5\u8865\u7801\u7684\u5f62\u5f0f\u5b58\u50a8\u7684\u3002\u5728\u8865\u7801\u8868\u793a\u4e0b\uff0c\u8ba1\u7b97\u673a\u53ef\u4ee5\u5bf9\u6b63\u6570\u548c\u8d1f\u6570\u7684\u52a0\u6cd5\u4e00\u89c6\u540c\u4ec1\uff0c\u4e0d\u9700\u8981\u4e3a\u51cf\u6cd5\u64cd\u4f5c\u5355\u72ec\u8bbe\u8ba1\u7279\u6b8a\u7684\u786c\u4ef6\u7535\u8def\uff0c\u5e76\u4e14\u4e0d\u5b58\u5728\u6b63\u8d1f\u96f6\u6b67\u4e49\u7684\u95ee\u9898\u3002</li> <li>\u6d6e\u70b9\u6570\u7684\u7f16\u7801\u7531 1 \u4f4d\u7b26\u53f7\u4f4d\u30018 \u4f4d\u6307\u6570\u4f4d\u548c 23 \u4f4d\u5206\u6570\u4f4d\u6784\u6210\u3002\u7531\u4e8e\u5b58\u5728\u6307\u6570\u4f4d\uff0c\u6d6e\u70b9\u6570\u7684\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e\u6574\u6570\uff0c\u4ee3\u4ef7\u662f\u727a\u7272\u4e86\u7cbe\u5ea6\u3002</li> <li>ASCII \u7801\u662f\u6700\u65e9\u51fa\u73b0\u7684\u82f1\u6587\u5b57\u7b26\u96c6\uff0c\u957f\u5ea6\u4e3a 1 \u5b57\u8282\uff0c\u5171\u6536\u5f55 127 \u4e2a\u5b57\u7b26\u3002GBK \u5b57\u7b26\u96c6\u662f\u5e38\u7528\u7684\u4e2d\u6587\u5b57\u7b26\u96c6\uff0c\u5171\u6536\u5f55\u4e24\u4e07\u591a\u4e2a\u6c49\u5b57\u3002Unicode \u81f4\u529b\u4e8e\u63d0\u4f9b\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\u96c6\u6807\u51c6\uff0c\u6536\u5f55\u4e16\u754c\u5185\u5404\u79cd\u8bed\u8a00\u7684\u5b57\u7b26\uff0c\u4ece\u800c\u89e3\u51b3\u7531\u4e8e\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\u4e0d\u4e00\u81f4\u800c\u5bfc\u81f4\u7684\u4e71\u7801\u95ee\u9898\u3002</li> <li>UTF-8 \u662f\u6700\u53d7\u6b22\u8fce\u7684 Unicode \u7f16\u7801\u65b9\u6cd5\uff0c\u901a\u7528\u6027\u975e\u5e38\u597d\u3002\u5b83\u662f\u4e00\u79cd\u53d8\u957f\u7684\u7f16\u7801\u65b9\u6cd5\uff0c\u5177\u6709\u5f88\u597d\u7684\u6269\u5c55\u6027\uff0c\u6709\u6548\u63d0\u5347\u4e86\u5b58\u50a8\u7a7a\u95f4\u7684\u4f7f\u7528\u6548\u7387\u3002UTF-16 \u548c UTF-32 \u662f\u7b49\u957f\u7684\u7f16\u7801\u65b9\u6cd5\u3002\u5728\u7f16\u7801\u4e2d\u6587\u65f6\uff0cUTF-16 \u6bd4 UTF-8 \u7684\u5360\u7528\u7a7a\u95f4\u66f4\u5c0f\u3002Java, C# \u7b49\u7f16\u7a0b\u8bed\u8a00\u9ed8\u8ba4\u4f7f\u7528 UTF-16 \u7f16\u7801\u3002</li> </ul>"},{"location":"chapter_data_structure/summary/#351-q-a","title":"3.5.1. \u00a0 Q &amp; A","text":"<p>\u4e3a\u4ec0\u4e48\u54c8\u5e0c\u8868\u540c\u65f6\u5305\u542b\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u548c\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1f</p> <p>\u54c8\u5e0c\u8868\u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u800c\u4e3a\u4e86\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u4f7f\u7528\u201c\u94fe\u5f0f\u5730\u5740\u201d\uff08\u540e\u7eed\u6563\u5217\u8868\u7ae0\u8282\u4f1a\u8bb2\uff09\u3002\u5728\u62c9\u94fe\u6cd5\u4e2d\uff0c\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5730\u5740\uff08\u6876\uff09\u6307\u5411\u4e00\u4e2a\u94fe\u8868\uff1b\u5f53\u8fd9\u4e2a\u94fe\u8868\u957f\u5ea6\u8d85\u8fc7\u4e00\u5b9a\u9608\u503c\u65f6\uff0c\u53c8\u53ef\u80fd\u88ab\u8f6c\u5316\u4e3a\u6811\uff08\u901a\u5e38\u4e3a\u7ea2\u9ed1\u6811\uff09\u3002\u56e0\u6b64\uff0c\u54c8\u5e0c\u8868\u53ef\u80fd\u540c\u65f6\u5305\u542b\u7ebf\u6027\uff08\u6570\u7ec4\u3001\u94fe\u8868\uff09\u548c\u975e\u7ebf\u6027\uff08\u6811\uff09\u6570\u636e\u7ed3\u6784\u3002</p> <p><code>char</code> \u7c7b\u578b\u7684\u957f\u5ea6\u662f 1 byte \u5417\uff1f</p> <p><code>char</code> \u7c7b\u578b\u7684\u957f\u5ea6\u7531\u7f16\u7a0b\u8bed\u8a00\u91c7\u7528\u7684\u7f16\u7801\u65b9\u6cd5\u51b3\u5b9a\u3002\u4f8b\u5982\uff0cJava, JS, TS, C# \u90fd\u91c7\u7528 UTF-16 \u7f16\u7801\uff08\u4fdd\u5b58 Unicode \u7801\u70b9\uff09\uff0c\u56e0\u6b64 char \u7c7b\u578b\u7684\u957f\u5ea6\u4e3a 2 bytes \u3002</p>"},{"location":"chapter_divide_and_conquer/","title":"12. \u00a0 \u5206\u6cbb","text":"<p>Abstract</p> <p>\u96be\u9898\u88ab\u9010\u5c42\u62c6\u89e3\uff0c\u6bcf\u4e00\u6b21\u7684\u62c6\u89e3\u90fd\u4f7f\u5b83\u53d8\u5f97\u66f4\u4e3a\u7b80\u5355\u3002</p> <p>\u5206\u800c\u6cbb\u4e4b\u63ed\u793a\u4e86\u4e00\u4e2a\u91cd\u8981\u7684\u4e8b\u5b9e\uff1a\u4ece\u7b80\u5355\u505a\u8d77\uff0c\u4e00\u5207\u90fd\u4e0d\u518d\u590d\u6742\u3002</p>"},{"location":"chapter_divide_and_conquer/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>12.1 \u00a0 \u5206\u6cbb\u7b97\u6cd5</li> <li>12.2 \u00a0 \u5206\u6cbb\u641c\u7d22\u7b56\u7565</li> <li>12.3 \u00a0 \u6784\u5efa\u6811\u95ee\u9898</li> <li>12.4 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898</li> <li>12.5 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_divide_and_conquer/binary_search_recur/","title":"12.2. \u00a0 \u5206\u6cbb\u641c\u7d22\u7b56\u7565","text":"<p>\u6211\u4eec\u5df2\u7ecf\u5b66\u8fc7\uff0c\u641c\u7d22\u7b97\u6cd5\u5206\u4e3a\u4e24\u5927\u7c7b\uff1a</p> <ul> <li>\u66b4\u529b\u641c\u7d22\uff1a\u5b83\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002</li> <li>\u81ea\u9002\u5e94\u641c\u7d22\uff1a\u5b83\u5229\u7528\u7279\u6709\u7684\u6570\u636e\u7ec4\u7ec7\u5f62\u5f0f\u6216\u5148\u9a8c\u4fe1\u606f\uff0c\u53ef\u8fbe\u5230 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002</li> </ul> <p>\u5b9e\u9645\u4e0a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u7684\u641c\u7d22\u7b97\u6cd5\u901a\u5e38\u90fd\u662f\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982\uff1a</p> <ul> <li>\u4e8c\u5206\u67e5\u627e\u7684\u6bcf\u4e00\u6b65\u90fd\u5c06\u95ee\u9898\uff08\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff09\u5206\u89e3\u4e3a\u4e00\u4e2a\u5c0f\u95ee\u9898\uff08\u5728\u6570\u7ec4\u7684\u4e00\u534a\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff09\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e00\u76f4\u6301\u7eed\u5230\u6570\u7ec4\u4e3a\u7a7a\u6216\u627e\u5230\u76ee\u6807\u5143\u7d20\u4e3a\u6b62\u3002</li> <li>\u6811\u662f\u5206\u6cbb\u5173\u7cfb\u7684\u4ee3\u8868\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u3001AVL \u6811\u3001\u5806\u7b49\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7686\u4e3a \\(O(\\log n)\\) \u3002</li> </ul> <p>\u4ee5\u4e8c\u5206\u67e5\u627e\u4e3a\u4f8b\uff1a</p> <ul> <li>\u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\uff1a\u4e8c\u5206\u67e5\u627e\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\uff08\u5728\u6570\u7ec4\u4e2d\u8fdb\u884c\u67e5\u627e\uff09\u5206\u89e3\u4e3a\u5b50\u95ee\u9898\uff08\u5728\u6570\u7ec4\u7684\u4e00\u534a\u4e2d\u8fdb\u884c\u67e5\u627e\uff09\uff0c\u8fd9\u662f\u901a\u8fc7\u6bd4\u8f83\u4e2d\u95f4\u5143\u7d20\u548c\u76ee\u6807\u5143\u7d20\u6765\u5b9e\u73b0\u7684\u3002</li> <li>\u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5728\u4e8c\u5206\u67e5\u627e\u4e2d\uff0c\u6bcf\u8f6e\u53ea\u5904\u7406\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u5b83\u4e0d\u53d7\u53e6\u5916\u5b50\u95ee\u9898\u7684\u5f71\u54cd\u3002</li> <li>\u5b50\u95ee\u9898\u7684\u89e3\u65e0\u9700\u5408\u5e76\uff1a\u4e8c\u5206\u67e5\u627e\u65e8\u5728\u67e5\u627e\u4e00\u4e2a\u7279\u5b9a\u5143\u7d20\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\u3002\u5f53\u5b50\u95ee\u9898\u5f97\u5230\u89e3\u51b3\u65f6\uff0c\u539f\u95ee\u9898\u4e5f\u4f1a\u540c\u65f6\u5f97\u5230\u89e3\u51b3\u3002</li> </ul> <p>\u5206\u6cbb\u80fd\u591f\u63d0\u5347\u641c\u7d22\u6548\u7387\uff0c\u672c\u8d28\u4e0a\u662f\u56e0\u4e3a\u66b4\u529b\u641c\u7d22\u6bcf\u8f6e\u53ea\u80fd\u6392\u9664\u4e00\u4e2a\u9009\u9879\uff0c\u800c\u5206\u6cbb\u641c\u7d22\u6bcf\u8f6e\u53ef\u4ee5\u6392\u9664\u4e00\u534a\u9009\u9879\u3002</p>"},{"location":"chapter_divide_and_conquer/binary_search_recur/#_1","title":"\u57fa\u4e8e\u5206\u6cbb\u5b9e\u73b0\u4e8c\u5206","text":"<p>\u5728\u4e4b\u524d\u7684\u7ae0\u8282\u4e2d\uff0c\u4e8c\u5206\u67e5\u627e\u662f\u57fa\u4e8e\u9012\u63a8\uff08\u8fed\u4ee3\uff09\u5b9e\u73b0\u7684\u3002\u73b0\u5728\u6211\u4eec\u57fa\u4e8e\u5206\u6cbb\uff08\u9012\u5f52\uff09\u6765\u5b9e\u73b0\u5b83\u3002</p> <p>Question</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 <code>nums</code> \uff0c\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u662f\u552f\u4e00\u7684\uff0c\u8bf7\u67e5\u627e\u5143\u7d20 <code>target</code> \u3002</p> <p>\u4ece\u5206\u6cbb\u89d2\u5ea6\uff0c\u6211\u4eec\u5c06\u641c\u7d22\u533a\u95f4 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u8bb0\u4e3a \\(f(i, j)\\) \u3002</p> <p>\u4ece\u539f\u95ee\u9898 \\(f(0, n-1)\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u6b65\u9aa4\u4e3a\uff1a</p> <ol> <li>\u8ba1\u7b97\u641c\u7d22\u533a\u95f4 \\([i, j]\\) \u7684\u4e2d\u70b9 \\(m\\) \uff0c\u6839\u636e\u5b83\u6392\u9664\u4e00\u534a\u641c\u7d22\u533a\u95f4\u3002</li> <li>\u9012\u5f52\u6c42\u89e3\u89c4\u6a21\u51cf\u5c0f\u4e00\u534a\u7684\u5b50\u95ee\u9898\uff0c\u53ef\u80fd\u4e3a \\(f(i, m-1)\\) \u6216 \\(f(m+1, j)\\) \u3002</li> <li>\u5faa\u73af\u7b2c <code>1.</code> , <code>2.</code> \u6b65\uff0c\u76f4\u81f3\u627e\u5230 <code>target</code> \u6216\u533a\u95f4\u4e3a\u7a7a\u65f6\u8fd4\u56de\u3002</li> </ol> <p>\u4e0b\u56fe\u5c55\u793a\u4e86\u5728\u6570\u7ec4\u4e2d\u4e8c\u5206\u67e5\u627e\u5143\u7d20 \\(6\\) \u7684\u5206\u6cbb\u8fc7\u7a0b\u3002</p> <p></p> <p> \u56fe\uff1a\u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u8fc7\u7a0b </p> <p>\u5728\u5b9e\u73b0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 <code>dfs()</code> \u6765\u6c42\u89e3\u95ee\u9898 \\(f(i, j)\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust binary_search_recur.java<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(int[] nums, int target, int i, int j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i &gt; j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nint m = (i + j) / 2;\nif (nums[m] &lt; target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] &gt; target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(int[] nums, int target) {\nint n = nums.length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n</code></pre> binary_search_recur.cpp<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(vector&lt;int&gt; &amp;nums, int target, int i, int j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i &gt; j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nint m = (i + j) / 2;\nif (nums[m] &lt; target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] &gt; target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(vector&lt;int&gt; &amp;nums, int target) {\nint n = nums.size();\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n</code></pre> binary_search_recur.py<pre><code>def dfs(nums: list[int], target: int, i: int, j: int) -&gt; int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j)\"\"\"\n# \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif i &gt; j:\nreturn -1\n# \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nm = (i + j) // 2\nif nums[m] &lt; target:\n# \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j)\nelif nums[m] &gt; target:\n# \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1)\nelse:\n# \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\ndef binary_search(nums: list[int], target: int) -&gt; int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\"\"\"\nn = len(nums)\n# \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1)\n</code></pre> binary_search_recur.go<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunc dfs(nums []int, target, i, j int) int {\n// \u5982\u679c\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u6ca1\u6709\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif i &gt; j {\nreturn -1\n}\n// \u8ba1\u7b97\u7d22\u5f15\u4e2d\u70b9\nm := i + ((j - i) &gt;&gt; 1)\n//\u5224\u65ad\u4e2d\u70b9\u4e0e\u76ee\u6807\u5143\u7d20\u5927\u5c0f\nif nums[m] &lt; target {\n// \u5c0f\u4e8e\u5219\u9012\u5f52\u53f3\u534a\u6570\u7ec4\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m+1, j)\n} else if nums[m] &gt; target {\n// \u5c0f\u4e8e\u5219\u9012\u5f52\u5de6\u534a\u6570\u7ec4\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m-1)\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nfunc binarySearch(nums []int, target int) int {\nn := len(nums)\nreturn dfs(nums, target, 0, n-1)\n}\n</code></pre> binary_search_recur.js<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunction dfs(nums, target, i, j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i &gt; j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nconst m = i + ((j - i) &gt;&gt; 1);\nif (nums[m] &lt; target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] &gt; target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nfunction binarySearch(nums, target) {\nconst n = nums.length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n</code></pre> binary_search_recur.ts<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfunction dfs(nums: number[], target: number, i: number, j: number): number {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i &gt; j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nconst m = i + ((j - i) &gt;&gt; 1);\nif (nums[m] &lt; target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] &gt; target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nfunction binarySearch(nums: number[], target: number): number {\nconst n = nums.length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n</code></pre> binary_search_recur.c<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{binarySearch}\n</code></pre> binary_search_recur.cs<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(int[] nums, int target, int i, int j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i &gt; j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nint m = (i + j) / 2;\nif (nums[m] &lt; target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] &gt; target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(int[] nums, int target) {\nint n = nums.Length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n</code></pre> binary_search_recur.swift<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{binarySearch}\n</code></pre> binary_search_recur.zig<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{binarySearch}\n</code></pre> binary_search_recur.dart<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nint dfs(List&lt;int&gt; nums, int target, int i, int j) {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif (i &gt; j) {\nreturn -1;\n}\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nint m = (i + j) ~/ 2;\nif (nums[m] &lt; target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if (nums[m] &gt; target) {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nint binarySearch(List&lt;int&gt; nums, int target) {\nint n = nums.length;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\nreturn dfs(nums, target, 0, n - 1);\n}\n</code></pre> binary_search_recur.rs<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff1a\u95ee\u9898 f(i, j) */\nfn dfs(nums: &amp;[i32], target: i32, i: i32, j: i32) -&gt; i32 {\n// \u82e5\u533a\u95f4\u4e3a\u7a7a\uff0c\u4ee3\u8868\u65e0\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de -1\nif i &gt; j { return -1; }\nlet m: i32 = (i + j) / 2;\nif nums[m as usize] &lt; target {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(m+1, j)\nreturn dfs(nums, target, m + 1, j);\n} else if nums[m as usize] &gt; target {\n// \u9012\u5f52\u5b50\u95ee\u9898 f(i, m-1)\nreturn dfs(nums, target, i, m - 1);\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n/* \u4e8c\u5206\u67e5\u627e */\nfn binary_search(nums: &amp;[i32], target: i32) -&gt; i32 {\nlet n = nums.len() as i32;\n// \u6c42\u89e3\u95ee\u9898 f(0, n-1)\ndfs(nums, target, 0, n - 1)\n}\n</code></pre>"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/","title":"12.3. \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u95ee\u9898","text":"<p>Question</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u524d\u5e8f\u904d\u5386 <code>preorder</code> \u548c\u4e2d\u5e8f\u904d\u5386 <code>inorder</code> \uff0c\u8bf7\u4ece\u4e2d\u6784\u5efa\u4e8c\u53c9\u6811\uff0c\u8fd4\u56de\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u3002</p> <p></p> <p> \u56fe\uff1a\u6784\u5efa\u4e8c\u53c9\u6811\u7684\u793a\u4f8b\u6570\u636e </p>"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#_1","title":"\u5224\u65ad\u662f\u5426\u4e3a\u5206\u6cbb\u95ee\u9898","text":"<p>\u539f\u95ee\u9898\u5b9a\u4e49\u4e3a\u4ece <code>preorder</code> \u548c <code>inorder</code> \u6784\u5efa\u4e8c\u53c9\u6811\u3002\u6211\u4eec\u9996\u5148\u4ece\u5206\u6cbb\u7684\u89d2\u5ea6\u5206\u6790\u8fd9\u9053\u9898\uff1a</p> <ul> <li>\u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\uff1a\u4ece\u5206\u6cbb\u7684\u89d2\u5ea6\u5207\u5165\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u539f\u95ee\u9898\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\u3001\u6784\u5efa\u53f3\u5b50\u6811\uff0c\u52a0\u4e0a\u4e00\u6b65\u64cd\u4f5c\uff1a\u521d\u59cb\u5316\u6839\u8282\u70b9\u3002\u800c\u5bf9\u4e8e\u6bcf\u4e2a\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u590d\u7528\u4ee5\u4e0a\u5212\u5206\u65b9\u6cd5\uff0c\u5c06\u5176\u5212\u5206\u4e3a\u66f4\u5c0f\u7684\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5c0f\u5b50\u95ee\u9898\uff08\u7a7a\u5b50\u6811\uff09\u65f6\u7ec8\u6b62\u3002</li> <li>\u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u4ea4\u96c6\u3002\u5728\u6784\u5efa\u5de6\u5b50\u6811\u65f6\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5173\u6ce8\u4e2d\u5e8f\u904d\u5386\u548c\u524d\u5e8f\u904d\u5386\u4e2d\u4e0e\u5de6\u5b50\u6811\u5bf9\u5e94\u7684\u90e8\u5206\u3002\u53f3\u5b50\u6811\u540c\u7406\u3002</li> <li>\u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u5408\u5e76\uff1a\u4e00\u65e6\u5f97\u5230\u4e86\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u5b83\u4eec\u94fe\u63a5\u5230\u6839\u8282\u70b9\u4e0a\uff0c\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002</li> </ul>"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#_2","title":"\u5982\u4f55\u5212\u5206\u5b50\u6811","text":"<p>\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u8fd9\u9053\u9898\u662f\u53ef\u4ee5\u4f7f\u7528\u5206\u6cbb\u6765\u6c42\u89e3\u7684\uff0c\u4f46\u95ee\u9898\u662f\uff1a\u5982\u4f55\u901a\u8fc7\u524d\u5e8f\u904d\u5386 <code>preorder</code> \u548c\u4e2d\u5e8f\u904d\u5386 <code>inorder</code> \u6765\u5212\u5206\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u5462\uff1f</p> <p>\u6839\u636e\u5b9a\u4e49\uff0c<code>preorder</code> \u548c <code>inorder</code> \u90fd\u53ef\u4ee5\u88ab\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a</p> <ul> <li>\u524d\u5e8f\u904d\u5386\uff1a<code>[ \u6839\u8282\u70b9 | \u5de6\u5b50\u6811 | \u53f3\u5b50\u6811 ]</code> \uff0c\u4f8b\u5982\u4e0a\u56fe <code>[ 3 | 9 | 2 1 7 ]</code> \u3002</li> <li>\u4e2d\u5e8f\u904d\u5386\uff1a<code>[ \u5de6\u5b50\u6811 | \u6839\u8282\u70b9 \uff5c \u53f3\u5b50\u6811 ]</code> \uff0c\u4f8b\u5982\u4e0a\u56fe <code>[ 9 | 3 | 1 2 7 ]</code> \u3002</li> </ul> <p>\u4ee5\u4e0a\u56fe\u6570\u636e\u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5f97\u5230\u4e0a\u8ff0\u7684\u5212\u5206\u7ed3\u679c\uff1a</p> <ol> <li>\u524d\u5e8f\u904d\u5386\u7684\u9996\u5143\u7d20 3 \u662f\u6839\u8282\u70b9\u7684\u503c\u3002</li> <li>\u67e5\u627e\u6839\u8282\u70b9 3 \u5728 <code>inorder</code> \u4e2d\u7684\u7d22\u5f15\uff0c\u5229\u7528\u8be5\u7d22\u5f15\u53ef\u5c06 <code>inorder</code> \u5212\u5206\u4e3a <code>[ 9 | 3 \uff5c 1 2 7 ]</code> \u3002</li> <li>\u6839\u636e <code>inorder</code> \u5212\u5206\u7ed3\u679c\uff0c\u6613\u5f97\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u8282\u70b9\u6570\u91cf\u5206\u522b\u4e3a 1 \u548c 3 \uff0c\u4ece\u800c\u53ef\u5c06 <code>preorder</code> \u5212\u5206\u4e3a <code>[ 3 | 9 | 2 1 7 ]</code> \u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u5728\u524d\u5e8f\u548c\u4e2d\u5e8f\u904d\u5386\u4e2d\u5212\u5206\u5b50\u6811 </p>"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#_3","title":"\u57fa\u4e8e\u53d8\u91cf\u63cf\u8ff0\u5b50\u6811\u533a\u95f4","text":"<p>\u6839\u636e\u4ee5\u4e0a\u5212\u5206\u65b9\u6cd5\uff0c\u6211\u4eec\u5df2\u7ecf\u5f97\u5230\u6839\u8282\u70b9\u3001\u5de6\u5b50\u6811\u3001\u53f3\u5b50\u6811\u5728 <code>preorder</code> \u548c <code>inorder</code> \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u3002\u800c\u4e3a\u4e86\u63cf\u8ff0\u8fd9\u4e9b\u7d22\u5f15\u533a\u95f4\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u51e0\u4e2a\u6307\u9488\u53d8\u91cf\uff1a</p> <ul> <li>\u5c06\u5f53\u524d\u6811\u7684\u6839\u8282\u70b9\u5728 <code>preorder</code> \u4e2d\u7684\u7d22\u5f15\u8bb0\u4e3a \\(i\\) \u3002</li> <li>\u5c06\u5f53\u524d\u6811\u7684\u6839\u8282\u70b9\u5728 <code>inorder</code> \u4e2d\u7684\u7d22\u5f15\u8bb0\u4e3a \\(m\\) \u3002</li> <li>\u5c06\u5f53\u524d\u6811\u5728 <code>inorder</code> \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u8bb0\u4e3a \\([l, r]\\) \u3002</li> </ul> <p>\u5982\u4e0b\u8868\u6240\u793a\uff0c\u901a\u8fc7\u4ee5\u4e0a\u53d8\u91cf\u5373\u53ef\u8868\u793a\u6839\u8282\u70b9\u5728 <code>preorder</code> \u4e2d\u7684\u7d22\u5f15\uff0c\u4ee5\u53ca\u5b50\u6811\u5728 <code>inorder</code> \u4e2d\u7684\u7d22\u5f15\u533a\u95f4\u3002</p> <p> \u8868\uff1a\u6839\u8282\u70b9\u548c\u5b50\u6811\u5728\u524d\u5e8f\u548c\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u7d22\u5f15 </p> \u6839\u8282\u70b9\u5728 <code>preorder</code> \u4e2d\u7684\u7d22\u5f15 \u5b50\u6811\u5728 <code>inorder</code> \u4e2d\u7684\u7d22\u5f15\u533a\u95f4 \u5f53\u524d\u6811 \\(i\\) \\([l, r]\\) \u5de6\u5b50\u6811 \\(i + 1\\) \\([l, m-1]\\) \u53f3\u5b50\u6811 \\(i + 1 + (m - l)\\) \\([m+1, r]\\) <p>\u8bf7\u6ce8\u610f\uff0c\u53f3\u5b50\u6811\u6839\u8282\u70b9\u7d22\u5f15\u4e2d\u7684 \\((m-l)\\) \u7684\u542b\u4e49\u662f\u201c\u5de6\u5b50\u6811\u7684\u8282\u70b9\u6570\u91cf\u201d\uff0c\u5efa\u8bae\u914d\u5408\u4e0b\u56fe\u7406\u89e3\u3002</p> <p></p> <p> \u56fe\uff1a\u6839\u8282\u70b9\u548c\u5de6\u53f3\u5b50\u6811\u7684\u7d22\u5f15\u533a\u95f4\u8868\u793a </p>"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#_4","title":"\u4ee3\u7801\u5b9e\u73b0","text":"<p>\u4e3a\u4e86\u63d0\u5347\u67e5\u8be2 \\(m\\) \u7684\u6548\u7387\uff0c\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 <code>hmap</code> \u6765\u5b58\u50a8\u6570\u7ec4 <code>inorder</code> \u4e2d\u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust build_tree.java<pre><code>/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode dfs(int[] preorder, int[] inorder, Map&lt;Integer, Integer&gt; hmap, int i, int l, int r) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l &lt; 0)\nreturn null;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nTreeNode root = new TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nint m = hmap.get(preorder[i]);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode buildTree(int[] preorder, int[] inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nMap&lt;Integer, Integer&gt; hmap = new HashMap&lt;&gt;();\nfor (int i = 0; i &lt; inorder.length; i++) {\nhmap.put(inorder[i], i);\n}\nTreeNode root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1);\nreturn root;\n}\n</code></pre> build_tree.cpp<pre><code>/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode *dfs(vector&lt;int&gt; &amp;preorder, vector&lt;int&gt; &amp;inorder, unordered_map&lt;int, int&gt; &amp;hmap, int i, int l, int r) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l &lt; 0)\nreturn NULL;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nTreeNode *root = new TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nint m = hmap[preorder[i]];\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot-&gt;left = dfs(preorder, inorder, hmap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot-&gt;right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode *buildTree(vector&lt;int&gt; &amp;preorder, vector&lt;int&gt; &amp;inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nunordered_map&lt;int, int&gt; hmap;\nfor (int i = 0; i &lt; inorder.size(); i++) {\nhmap[inorder[i]] = i;\n}\nTreeNode *root = dfs(preorder, inorder, hmap, 0, 0, inorder.size() - 1);\nreturn root;\n}\n</code></pre> build_tree.py<pre><code>def dfs(\npreorder: list[int],\ninorder: list[int],\nhmap: dict[int, int],\ni: int,\nl: int,\nr: int,\n) -&gt; TreeNode | None:\n\"\"\"\u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb\"\"\"\n# \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif r - l &lt; 0:\nreturn None\n# \u521d\u59cb\u5316\u6839\u8282\u70b9\nroot = TreeNode(preorder[i])\n# \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nm = hmap[preorder[i]]\n# \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1)\n# \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r)\n# \u8fd4\u56de\u6839\u8282\u70b9\nreturn root\ndef build_tree(preorder: list[int], inorder: list[int]) -&gt; TreeNode | None:\n\"\"\"\u6784\u5efa\u4e8c\u53c9\u6811\"\"\"\n# \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nhmap = {val: i for i, val in enumerate(inorder)}\nroot = dfs(preorder, inorder, hmap, 0, 0, len(inorder) - 1)\nreturn root\n</code></pre> build_tree.go<pre><code>/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunc dfsBuildTree(preorder, inorder []int, hmap map[int]int, i, l, r int) *TreeNode {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif r-l &lt; 0 {\nreturn nil\n}\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nroot := NewTreeNode(preorder[i])\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nm := hmap[preorder[i]]\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.Left = dfsBuildTree(preorder, inorder, hmap, i+1, l, m-1)\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.Right = dfsBuildTree(preorder, inorder, hmap, i+1+m-l, m+1, r)\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunc buildTree(preorder, inorder []int) *TreeNode {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nhmap := make(map[int]int, len(inorder))\nfor i := 0; i &lt; len(inorder); i++ {\nhmap[inorder[i]] = i\n}\nroot := dfsBuildTree(preorder, inorder, hmap, 0, 0, len(inorder)-1)\nreturn root\n}\n</code></pre> build_tree.js<pre><code>/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunction dfs(preorder, inorder, hmap, i, l, r) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l &lt; 0) return null;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nconst root = new TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nconst m = hmap.get(preorder[i]);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunction buildTree(preorder, inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nlet hmap = new Map();\nfor (let i = 0; i &lt; inorder.length; i++) {\nhmap.set(inorder[i], i);\n}\nconst root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1);\nreturn root;\n}\n</code></pre> build_tree.ts<pre><code>/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfunction dfs(\npreorder: number[],\ninorder: number[],\nhmap: Map&lt;number, number&gt;,\ni: number,\nl: number,\nr: number\n): TreeNode | null {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l &lt; 0) return null;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nconst root: TreeNode = new TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nconst m = hmap.get(preorder[i]);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfunction buildTree(preorder: number[], inorder: number[]): TreeNode | null {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nlet hmap = new Map&lt;number, number&gt;();\nfor (let i = 0; i &lt; inorder.length; i++) {\nhmap.set(inorder[i], i);\n}\nconst root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1);\nreturn root;\n}\n</code></pre> build_tree.c<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{buildTree}\n</code></pre> build_tree.cs<pre><code>/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode dfs(int[] preorder, int[] inorder, Dictionary&lt;int, int&gt; hmap, int i, int l, int r) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l &lt; 0)\nreturn null;\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nTreeNode root = new TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nint m = hmap[preorder[i]];\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode buildTree(int[] preorder, int[] inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nDictionary&lt;int, int&gt; hmap = new Dictionary&lt;int, int&gt;();\nfor (int i = 0; i &lt; inorder.Length; i++) {\nhmap.TryAdd(inorder[i], i);\n}\nTreeNode root = dfs(preorder, inorder, hmap, 0, 0, inorder.Length - 1);\nreturn root;\n}\n</code></pre> build_tree.swift<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{buildTree}\n</code></pre> build_tree.zig<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{buildTree}\n</code></pre> build_tree.dart<pre><code>/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nTreeNode? dfs(\nList&lt;int&gt; preorder,\nList&lt;int&gt; inorder,\nMap&lt;int, int&gt; hmap,\nint i,\nint l,\nint r,\n) {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif (r - l &lt; 0) {\nreturn null;\n}\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nTreeNode? root = TreeNode(preorder[i]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nint m = hmap[preorder[i]]!;\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nreturn root;\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nTreeNode? buildTree(List&lt;int&gt; preorder, List&lt;int&gt; inorder) {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nMap&lt;int, int&gt; hmap = {};\nfor (int i = 0; i &lt; inorder.length; i++) {\nhmap[inorder[i]] = i;\n}\nTreeNode? root = dfs(preorder, inorder, hmap, 0, 0, inorder.length - 1);\nreturn root;\n}\n</code></pre> build_tree.rs<pre><code>/* \u6784\u5efa\u4e8c\u53c9\u6811\uff1a\u5206\u6cbb */\nfn dfs(preorder: &amp;[i32], inorder: &amp;[i32], hmap: &amp;HashMap&lt;i32, i32&gt;, i: i32, l: i32, r: i32) -&gt; Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt; {\n// \u5b50\u6811\u533a\u95f4\u4e3a\u7a7a\u65f6\u7ec8\u6b62\nif r - l &lt; 0 { return None; }\n// \u521d\u59cb\u5316\u6839\u8282\u70b9\nlet root = TreeNode::new(preorder[i as usize]);\n// \u67e5\u8be2 m \uff0c\u4ece\u800c\u5212\u5206\u5de6\u53f3\u5b50\u6811\nlet m = hmap.get(&amp;preorder[i as usize]).unwrap();\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u5de6\u5b50\u6811\nroot.borrow_mut().left = dfs(preorder, inorder, hmap, i + 1, l, m - 1);\n// \u5b50\u95ee\u9898\uff1a\u6784\u5efa\u53f3\u5b50\u6811\nroot.borrow_mut().right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r);\n// \u8fd4\u56de\u6839\u8282\u70b9\nSome(root)\n}\n/* \u6784\u5efa\u4e8c\u53c9\u6811 */\nfn build_tree(preorder: &amp;[i32], inorder: &amp;[i32]) -&gt; Option&lt;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt; {\n// \u521d\u59cb\u5316\u54c8\u5e0c\u8868\uff0c\u5b58\u50a8 inorder \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\nlet mut hmap: HashMap&lt;i32, i32&gt; = HashMap::new();\nfor i in 0..inorder.len() {\nhmap.insert(inorder[i], i as i32);\n}\nlet root = dfs(preorder, inorder, &amp;hmap, 0, 0, inorder.len() as i32 - 1);\nroot\n}\n</code></pre> <p>\u4e0b\u56fe\u5c55\u793a\u4e86\u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5404\u4e2a\u8282\u70b9\u662f\u5728\u5411\u4e0b\u201c\u9012\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\uff0c\u800c\u5404\u6761\u8fb9\uff08\u5373\u5f15\u7528\uff09\u662f\u5728\u5411\u4e0a\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\u3002</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> \u56fe\uff1a\u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b </p> <p>\u8bbe\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u521d\u59cb\u5316\u6bcf\u4e00\u4e2a\u8282\u70b9\uff08\u6267\u884c\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 <code>dfs()</code> \uff09\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u56e0\u6b64\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002</p> <p>\u54c8\u5e0c\u8868\u5b58\u50a8 <code>inorder</code> \u5143\u7d20\u5230\u7d22\u5f15\u7684\u6620\u5c04\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u9012\u5f52\u6df1\u5ea6\u8fbe\u5230 \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u7684\u6808\u5e27\u7a7a\u95f4\u3002\u56e0\u6b64\u603b\u4f53\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002</p>"},{"location":"chapter_divide_and_conquer/divide_and_conquer/","title":"12.1. \u00a0 \u5206\u6cbb\u7b97\u6cd5","text":"<p>\u300c\u5206\u6cbb Divide and Conquer\u300d\uff0c\u5168\u79f0\u5206\u800c\u6cbb\u4e4b\uff0c\u662f\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u4e14\u5e38\u89c1\u7684\u7b97\u6cd5\u7b56\u7565\u3002\u5206\u6cbb\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\uff0c\u5305\u62ec\u201c\u5206\u201d\u548c\u201c\u6cbb\u201d\u4e24\u6b65\uff1a</p> <ol> <li>\u5206\uff08\u5212\u5206\u9636\u6bb5\uff09\uff1a\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u4e24\u4e2a\u6216\u591a\u4e2a\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u5230\u8fbe\u6700\u5c0f\u5b50\u95ee\u9898\u65f6\u7ec8\u6b62\u3002</li> <li>\u6cbb\uff08\u5408\u5e76\u9636\u6bb5\uff09\uff1a\u4ece\u5df2\u77e5\u89e3\u7684\u6700\u5c0f\u5b50\u95ee\u9898\u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\uff0c\u4ece\u800c\u6784\u5efa\u51fa\u539f\u95ee\u9898\u7684\u89e3\u3002</li> </ol> <p>\u5df2\u4ecb\u7ecd\u8fc7\u7684\u300c\u5f52\u5e76\u6392\u5e8f\u300d\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u5e94\u7528\u4e4b\u4e00\uff0c\u5b83\u7684\u5206\u6cbb\u7b56\u7565\u4e3a\uff1a</p> <ol> <li>\u5206\uff1a\u9012\u5f52\u5730\u5c06\u539f\u6570\u7ec4\uff08\u539f\u95ee\u9898\uff09\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\uff09\uff0c\u76f4\u5230\u5b50\u6570\u7ec4\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\uff08\u6700\u5c0f\u5b50\u95ee\u9898\uff09\u3002</li> <li>\u6cbb\uff1a\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u6709\u5e8f\u7684\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\u8fdb\u884c\u5408\u5e76\uff0c\u4ece\u800c\u5f97\u5230\u6709\u5e8f\u7684\u539f\u6570\u7ec4\uff08\u539f\u95ee\u9898\u7684\u89e3\uff09\u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u5f52\u5e76\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\u65ad\u5206\u6cbb\u95ee\u9898","text":"<p>\u4e00\u4e2a\u95ee\u9898\u662f\u5426\u9002\u5408\u4f7f\u7528\u5206\u6cbb\u89e3\u51b3\uff0c\u901a\u5e38\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u51e0\u4e2a\u5224\u65ad\u4f9d\u636e\uff1a</p> <ol> <li>\u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\uff1a\u539f\u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\u6210\u89c4\u6a21\u66f4\u5c0f\u3001\u7c7b\u4f3c\u7684\u5b50\u95ee\u9898\uff0c\u4ee5\u53ca\u80fd\u591f\u4ee5\u76f8\u540c\u65b9\u5f0f\u9012\u5f52\u5730\u8fdb\u884c\u5212\u5206\u3002</li> <li>\u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u5b50\u95ee\u9898\u4e4b\u95f4\u662f\u6ca1\u6709\u91cd\u53e0\u7684\uff0c\u4e92\u76f8\u6ca1\u6709\u4f9d\u8d56\uff0c\u53ef\u4ee5\u88ab\u72ec\u7acb\u89e3\u51b3\u3002</li> <li>\u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u88ab\u5408\u5e76\uff1a\u539f\u95ee\u9898\u7684\u89e3\u901a\u8fc7\u5408\u5e76\u5b50\u95ee\u9898\u7684\u89e3\u5f97\u6765\u3002</li> </ol> <p>\u663e\u7136\u5f52\u5e76\u6392\u5e8f\uff0c\u6ee1\u8db3\u4ee5\u4e0a\u4e09\u6761\u5224\u65ad\u4f9d\u636e\uff1a</p> <ol> <li>\u9012\u5f52\u5730\u5c06\u6570\u7ec4\uff08\u539f\u95ee\u9898\uff09\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\uff09\u3002</li> <li>\u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u53ef\u4ee5\u72ec\u7acb\u5730\u8fdb\u884c\u6392\u5e8f\uff08\u5b50\u95ee\u9898\u53ef\u4ee5\u72ec\u7acb\u8fdb\u884c\u6c42\u89e3\uff09\u3002</li> <li>\u4e24\u4e2a\u6709\u5e8f\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\u53ef\u4ee5\u88ab\u5408\u5e76\u4e3a\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\uff08\u539f\u95ee\u9898\u7684\u89e3\uff09\u3002</li> </ol>"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1212","title":"12.1.2. \u00a0 \u901a\u8fc7\u5206\u6cbb\u63d0\u5347\u6548\u7387","text":"<p>\u5206\u6cbb\u4e0d\u4ec5\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u51b3\u7b97\u6cd5\u95ee\u9898\uff0c\u5f80\u5f80\u8fd8\u53ef\u4ee5\u5e26\u6765\u7b97\u6cd5\u6548\u7387\u7684\u63d0\u5347\u3002\u5728\u6392\u5e8f\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u76f8\u8f83\u4e8e\u9009\u62e9\u3001\u5192\u6ce1\u3001\u63d2\u5165\u6392\u5e8f\u66f4\u5feb\uff0c\u5c31\u662f\u56e0\u4e3a\u5b83\u4eec\u5e94\u7528\u4e86\u5206\u6cbb\u7b56\u7565\u3002</p> <p>\u90a3\u4e48\uff0c\u6211\u4eec\u4e0d\u7981\u53d1\u95ee\uff1a\u4e3a\u4ec0\u4e48\u5206\u6cbb\u53ef\u4ee5\u63d0\u5347\u7b97\u6cd5\u6548\u7387\uff0c\u5176\u5e95\u5c42\u903b\u8f91\u662f\u4ec0\u4e48\uff1f\u6362\u53e5\u8bdd\u8bf4\uff0c\u5c06\u5927\u95ee\u9898\u5206\u89e3\u4e3a\u591a\u4e2a\u5b50\u95ee\u9898\u3001\u89e3\u51b3\u5b50\u95ee\u9898\u3001\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u5408\u5e76\u4e3a\u539f\u95ee\u9898\u7684\u89e3\uff0c\u8fd9\u51e0\u6b65\u7684\u6548\u7387\u4e3a\u4ec0\u4e48\u6bd4\u76f4\u63a5\u89e3\u51b3\u539f\u95ee\u9898\u7684\u6548\u7387\u66f4\u9ad8\uff1f\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u4ece\u64cd\u4f5c\u6570\u91cf\u548c\u5e76\u884c\u8ba1\u7b97\u4e24\u65b9\u9762\u6765\u8ba8\u8bba\u3002</p>"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#_1","title":"\u64cd\u4f5c\u6570\u91cf\u4f18\u5316","text":"<p>\u4ee5\u300c\u5192\u6ce1\u6392\u5e8f\u300d\u4e3a\u4f8b\uff0c\u5176\u5904\u7406\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\u9700\u8981 \\(O(n^2)\\) \u65f6\u95f4\u3002\u5047\u8bbe\u6211\u4eec\u628a\u6570\u7ec4\u4ece\u4e2d\u70b9\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u5219\u5212\u5206\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u6392\u5e8f\u6bcf\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O((\\frac{n}{2})^2)\\) \u65f6\u95f4\uff0c\u5408\u5e76\u4e24\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a</p> \\[ O(n + (\\frac{n}{2})^2 \\times 2 + n) = O(\\frac{n^2}{2} + 2n) \\] <p></p> <p> \u56fe\uff1a\u5212\u5206\u6570\u7ec4\u524d\u540e\u7684\u5192\u6ce1\u6392\u5e8f </p> <p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8ba1\u7b97\u4ee5\u4e0b\u4e0d\u7b49\u5f0f\uff0c\u5176\u5de6\u8fb9\u548c\u53f3\u8fb9\u5206\u522b\u4e3a\u5212\u5206\u524d\u548c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u603b\u6570\uff1a</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>\u8fd9\u610f\u5473\u7740\u5f53 \\(n &gt; 4\\) \u65f6\uff0c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u6570\u91cf\u66f4\u5c11\uff0c\u6392\u5e8f\u6548\u7387\u5e94\u8be5\u66f4\u9ad8\u3002\u8bf7\u6ce8\u610f\uff0c\u5212\u5206\u540e\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u7136\u662f\u5e73\u65b9\u9636 \\(O(n^2)\\) \uff0c\u53ea\u662f\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u53d8\u5c0f\u4e86\u3002</p> <p>\u8fdb\u4e00\u6b65\u60f3\uff0c\u5982\u679c\u6211\u4eec\u628a\u5b50\u6570\u7ec4\u4e0d\u65ad\u5730\u518d\u4ece\u4e2d\u70b9\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\u65f6\u505c\u6b62\u5212\u5206\u5462\uff1f\u8fd9\u79cd\u601d\u8def\u5b9e\u9645\u4e0a\u5c31\u662f\u300c\u5f52\u5e76\u6392\u5e8f\u300d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002</p> <p>\u518d\u601d\u8003\uff0c\u5982\u679c\u6211\u4eec\u591a\u8bbe\u7f6e\u51e0\u4e2a\u5212\u5206\u70b9\uff0c\u5c06\u539f\u6570\u7ec4\u5e73\u5747\u5212\u5206\u4e3a \\(k\\) \u4e2a\u5b50\u6570\u7ec4\u5462\uff1f\u8fd9\u79cd\u60c5\u51b5\u4e0e\u300c\u6876\u6392\u5e8f\u300d\u975e\u5e38\u7c7b\u4f3c\uff0c\u5b83\u975e\u5e38\u9002\u5408\u6392\u5e8f\u6d77\u91cf\u6570\u636e\uff0c\u7406\u8bba\u4e0a\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230 \\(O(n + k)\\) \u3002</p>"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#_2","title":"\u5e76\u884c\u8ba1\u7b97\u4f18\u5316","text":"<p>\u6211\u4eec\u77e5\u9053\uff0c\u5206\u6cbb\u751f\u6210\u7684\u5b50\u95ee\u9898\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u56e0\u6b64\u901a\u5e38\u53ef\u4ee5\u5e76\u884c\u89e3\u51b3\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5206\u6cbb\u4e0d\u4ec5\u53ef\u4ee5\u964d\u4f4e\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u8fd8\u6709\u5229\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u5e76\u884c\u4f18\u5316\u3002</p> <p>\u5e76\u884c\u4f18\u5316\u5728\u591a\u6838\u6216\u591a\u5904\u7406\u5668\u7684\u73af\u5883\u4e2d\u5c24\u5176\u6709\u6548\uff0c\u56e0\u4e3a\u7cfb\u7edf\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5b50\u95ee\u9898\uff0c\u66f4\u52a0\u5145\u5206\u5730\u5229\u7528\u8ba1\u7b97\u8d44\u6e90\uff0c\u4ece\u800c\u663e\u8457\u51cf\u5c11\u603b\u4f53\u7684\u8fd0\u884c\u65f6\u95f4\u3002</p> <p>\u6bd4\u5982\u5728\u6876\u6392\u5e8f\u4e2d\uff0c\u6211\u4eec\u5c06\u6d77\u91cf\u7684\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff0c\u5219\u53ef\u6240\u6709\u6876\u7684\u6392\u5e8f\u4efb\u52a1\u5206\u6563\u5230\u5404\u4e2a\u8ba1\u7b97\u5355\u5143\uff0c\u5b8c\u6210\u540e\u518d\u8fdb\u884c\u7ed3\u679c\u5408\u5e76\u3002</p> <p></p> <p> \u56fe\uff1a\u6876\u6392\u5e8f\u7684\u5e76\u884c\u8ba1\u7b97 </p>"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1213","title":"12.1.3. \u00a0 \u5206\u6cbb\u5e38\u89c1\u5e94\u7528","text":"<p>\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u8bb8\u591a\u7ecf\u5178\u7b97\u6cd5\u95ee\u9898\uff1a</p> <ul> <li>\u5bfb\u627e\u6700\u8fd1\u70b9\u5bf9\uff1a\u8be5\u7b97\u6cd5\u9996\u5148\u5c06\u70b9\u96c6\u5206\u6210\u4e24\u90e8\u5206\uff0c\u7136\u540e\u5206\u522b\u627e\u51fa\u4e24\u90e8\u5206\u4e2d\u7684\u6700\u8fd1\u70b9\u5bf9\uff0c\u6700\u540e\u518d\u627e\u51fa\u8de8\u8d8a\u4e24\u90e8\u5206\u7684\u6700\u8fd1\u70b9\u5bf9\u3002</li> <li>\u5927\u6574\u6570\u4e58\u6cd5\uff1a\u4f8b\u5982 Karatsuba \u7b97\u6cd5\uff0c\u5b83\u662f\u5c06\u5927\u6574\u6570\u4e58\u6cd5\u5206\u89e3\u4e3a\u51e0\u4e2a\u8f83\u5c0f\u7684\u6574\u6570\u7684\u4e58\u6cd5\u548c\u52a0\u6cd5\u3002</li> <li>\u77e9\u9635\u4e58\u6cd5\uff1a\u4f8b\u5982 Strassen \u7b97\u6cd5\uff0c\u5b83\u662f\u5c06\u5927\u77e9\u9635\u4e58\u6cd5\u5206\u89e3\u4e3a\u591a\u4e2a\u5c0f\u77e9\u9635\u7684\u4e58\u6cd5\u548c\u52a0\u6cd5\u3002</li> <li>\u6c49\u8bfa\u5854\u95ee\u9898\uff1a\u6c49\u8bfa\u5854\u95ee\u9898\u53ef\u4ee5\u89c6\u4e3a\u5178\u578b\u7684\u5206\u6cbb\u7b56\u7565\uff0c\u901a\u8fc7\u9012\u5f52\u89e3\u51b3\u3002</li> <li>\u6c42\u89e3\u9006\u5e8f\u5bf9\uff1a\u5728\u4e00\u4e2a\u5e8f\u5217\u4e2d\uff0c\u5982\u679c\u524d\u9762\u7684\u6570\u5b57\u5927\u4e8e\u540e\u9762\u7684\u6570\u5b57\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u6570\u5b57\u6784\u6210\u4e00\u4e2a\u9006\u5e8f\u5bf9\u3002\u6c42\u89e3\u9006\u5e8f\u5bf9\u95ee\u9898\u53ef\u4ee5\u901a\u8fc7\u5206\u6cbb\u7684\u601d\u60f3\uff0c\u501f\u52a9\u5f52\u5e76\u6392\u5e8f\u8fdb\u884c\u6c42\u89e3\u3002</li> </ul> <p>\u53e6\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u5728\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\u4e2d\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\uff0c\u4e3e\u51e0\u4e2a\u5df2\u7ecf\u5b66\u8fc7\u7684\u4f8b\u5b50\uff1a</p> <ul> <li>\u4e8c\u5206\u67e5\u627e\uff1a\u4e8c\u5206\u67e5\u627e\u662f\u5c06\u6709\u5e8f\u6570\u7ec4\u4ece\u4e2d\u70b9\u7d22\u5f15\u5206\u4e3a\u4e24\u90e8\u5206\uff0c\u7136\u540e\u6839\u636e\u76ee\u6807\u503c\u4e0e\u4e2d\u95f4\u5143\u7d20\u503c\u6bd4\u8f83\u7ed3\u679c\uff0c\u51b3\u5b9a\u6392\u9664\u54ea\u4e00\u534a\u533a\u95f4\uff0c\u7136\u540e\u5728\u5269\u4f59\u533a\u95f4\u6267\u884c\u76f8\u540c\u7684\u4e8c\u5206\u64cd\u4f5c\u3002</li> <li>\u5f52\u5e76\u6392\u5e8f\uff1a\u6587\u7ae0\u5f00\u5934\u5df2\u4ecb\u7ecd\uff0c\u4e0d\u518d\u8d58\u8ff0\u3002</li> <li>\u5feb\u901f\u6392\u5e8f\uff1a\u5feb\u901f\u6392\u5e8f\u662f\u9009\u53d6\u4e00\u4e2a\u57fa\u51c6\u503c\uff0c\u7136\u540e\u628a\u6570\u7ec4\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u4e00\u4e2a\u5b50\u6570\u7ec4\u7684\u5143\u7d20\u6bd4\u57fa\u51c6\u503c\u5c0f\uff0c\u53e6\u4e00\u5b50\u6570\u7ec4\u7684\u5143\u7d20\u6bd4\u57fa\u51c6\u503c\u5927\uff0c\u7136\u540e\u518d\u5bf9\u8fd9\u4e24\u90e8\u5206\u8fdb\u884c\u76f8\u540c\u7684\u5212\u5206\u64cd\u4f5c\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u53ea\u5269\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002</li> <li>\u6876\u6392\u5e8f\uff1a\u6876\u6392\u5e8f\u7684\u57fa\u672c\u601d\u60f3\u662f\u5c06\u6570\u636e\u5206\u6563\u5230\u591a\u4e2a\u6876\uff0c\u7136\u540e\u5bf9\u6bcf\u4e2a\u6876\u5185\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u540e\u5c06\u5404\u4e2a\u6876\u7684\u5143\u7d20\u4f9d\u6b21\u53d6\u51fa\uff0c\u4ece\u800c\u5f97\u5230\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\u3002</li> <li>\u6811\uff1a\u4f8b\u5982\u4e8c\u53c9\u641c\u7d22\u6811\u3001AVL \u6811\u3001\u7ea2\u9ed1\u6811\u3001B \u6811\u3001B+ \u6811\u7b49\uff0c\u5b83\u4eec\u7684\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u7b49\u64cd\u4f5c\u90fd\u53ef\u4ee5\u89c6\u4e3a\u5206\u6cbb\u7684\u5e94\u7528\u3002</li> <li>\u5806\uff1a\u5806\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u5176\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u63d2\u5165\u3001\u5220\u9664\u548c\u5806\u5316\uff0c\u5b9e\u9645\u4e0a\u90fd\u9690\u542b\u4e86\u5206\u6cbb\u7684\u601d\u60f3\u3002</li> <li>\u54c8\u5e0c\u8868\uff1a\u867d\u7136\u54c8\u5e0c\u8868\u6765\u5e76\u4e0d\u76f4\u63a5\u5e94\u7528\u5206\u6cbb\uff0c\u4f46\u67d0\u4e9b\u54c8\u5e0c\u51b2\u7a81\u89e3\u51b3\u7b56\u7565\u95f4\u63a5\u5e94\u7528\u4e86\u5206\u6cbb\u7b56\u7565\uff0c\u4f8b\u5982\uff0c\u94fe\u5f0f\u5730\u5740\u4e2d\u7684\u957f\u94fe\u8868\u4f1a\u88ab\u8f6c\u5316\u4e3a\u7ea2\u9ed1\u6811\uff0c\u4ee5\u63d0\u5347\u67e5\u8be2\u6548\u7387\u3002</li> </ul> <p>\u53ef\u4ee5\u770b\u51fa\uff0c\u5206\u6cbb\u662f\u4e00\u79cd\u201c\u6da6\u7269\u7ec6\u65e0\u58f0\u201d\u7684\u7b97\u6cd5\u601d\u60f3\uff0c\u9690\u542b\u5728\u5404\u79cd\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u4e4b\u4e2d\u3002</p>"},{"location":"chapter_divide_and_conquer/hanota_problem/","title":"12.4. \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898","text":"<p>\u5728\u5f52\u5e76\u6392\u5e8f\u548c\u6784\u5efa\u4e8c\u53c9\u6811\u4e2d\uff0c\u6211\u4eec\u90fd\u662f\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u4e24\u4e2a\u89c4\u6a21\u4e3a\u539f\u95ee\u9898\u4e00\u534a\u7684\u5b50\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u6c49\u8bfa\u5854\u95ee\u9898\uff0c\u6211\u4eec\u91c7\u7528\u4e0d\u540c\u7684\u5206\u89e3\u7b56\u7565\u3002</p> <p>Question</p> <p>\u7ed9\u5b9a\u4e09\u6839\u67f1\u5b50\uff0c\u8bb0\u4e3a <code>A</code> , <code>B</code> , <code>C</code> \u3002\u8d77\u59cb\u72b6\u6001\u4e0b\uff0c\u67f1\u5b50 <code>A</code> \u4e0a\u5957\u7740 \\(n\\) \u4e2a\u5706\u76d8\uff0c\u5b83\u4eec\u4ece\u4e0a\u5230\u4e0b\u6309\u7167\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\u3002\u6211\u4eec\u7684\u4efb\u52a1\u662f\u8981\u628a\u8fd9 \\(n\\) \u4e2a\u5706\u76d8\u79fb\u5230\u67f1\u5b50 <code>C</code> \u4e0a\uff0c\u5e76\u4fdd\u6301\u5b83\u4eec\u7684\u539f\u6709\u987a\u5e8f\u4e0d\u53d8\u3002\u5728\u79fb\u52a8\u5706\u76d8\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u9075\u5b88\u4ee5\u4e0b\u89c4\u5219\uff1a</p> <ol> <li>\u5706\u76d8\u53ea\u80fd\u4ece\u4e00\u4e2a\u67f1\u5b50\u9876\u90e8\u62ff\u51fa\uff0c\u4ece\u53e6\u4e00\u4e2a\u67f1\u5b50\u9876\u90e8\u653e\u5165\u3002</li> <li>\u6bcf\u6b21\u53ea\u80fd\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\u3002</li> <li>\u5c0f\u5706\u76d8\u5fc5\u987b\u65f6\u523b\u4f4d\u4e8e\u5927\u5706\u76d8\u4e4b\u4e0a\u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u6c49\u8bfa\u5854\u95ee\u9898\u793a\u4f8b </p> <p>\u6211\u4eec\u5c06\u89c4\u6a21\u4e3a \\(i\\) \u7684\u6c49\u8bfa\u5854\u95ee\u9898\u8bb0\u505a \\(f(i)\\) \u3002\u4f8b\u5982 \\(f(3)\\) \u4ee3\u8868\u5c06 \\(3\\) \u4e2a\u5706\u76d8\u4ece <code>A</code> \u79fb\u52a8\u81f3 <code>C</code> \u7684\u6c49\u8bfa\u5854\u95ee\u9898\u3002</p>"},{"location":"chapter_divide_and_conquer/hanota_problem/#_1","title":"\u8003\u8651\u57fa\u672c\u60c5\u51b5","text":"<p>\u5bf9\u4e8e\u95ee\u9898 \\(f(1)\\) \uff0c\u5373\u5f53\u53ea\u6709\u4e00\u4e2a\u5706\u76d8\u65f6\uff0c\u5219\u5c06\u5b83\u76f4\u63a5\u4ece <code>A</code> \u79fb\u52a8\u81f3 <code>C</code> \u5373\u53ef\u3002</p> &lt;1&gt;&lt;2&gt; <p></p> <p></p> <p> \u56fe\uff1a\u89c4\u6a21\u4e3a 1 \u95ee\u9898\u7684\u89e3 </p> <p>\u5bf9\u4e8e\u95ee\u9898 \\(f(2)\\) \uff0c\u5373\u5f53\u6709\u4e24\u4e2a\u5706\u76d8\u65f6\uff0c\u7531\u4e8e\u8981\u65f6\u523b\u6ee1\u8db3\u5c0f\u5706\u76d8\u5728\u5927\u5706\u76d8\u4e4b\u4e0a\uff0c\u56e0\u6b64\u9700\u8981\u501f\u52a9 <code>B</code> \u6765\u5b8c\u6210\u79fb\u52a8\uff0c\u5305\u62ec\u4e09\u6b65\uff1a</p> <ol> <li>\u5148\u5c06\u4e0a\u9762\u7684\u5c0f\u5706\u76d8\u4ece <code>A</code> \u79fb\u81f3 <code>B</code> \u3002</li> <li>\u518d\u5c06\u5927\u5706\u76d8\u4ece <code>A</code> \u79fb\u81f3 <code>C</code> \u3002</li> <li>\u6700\u540e\u5c06\u5c0f\u5706\u76d8\u4ece <code>B</code> \u79fb\u81f3 <code>C</code> \u3002</li> </ol> <p>\u89e3\u51b3\u95ee\u9898 \\(f(2)\\) \u7684\u8fc7\u7a0b\u53ef\u603b\u7ed3\u4e3a\uff1a\u5c06\u4e24\u4e2a\u5706\u76d8\u501f\u52a9 <code>B</code> \u4ece <code>A</code> \u79fb\u81f3 <code>C</code> \u3002\u5176\u4e2d\uff0c<code>C</code> \u79f0\u4e3a\u76ee\u6807\u67f1\u3001<code>B</code> \u79f0\u4e3a\u7f13\u51b2\u67f1\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt; <p></p> <p></p> <p></p> <p></p> <p> \u56fe\uff1a\u89c4\u6a21\u4e3a 2 \u95ee\u9898\u7684\u89e3 </p>"},{"location":"chapter_divide_and_conquer/hanota_problem/#_2","title":"\u5b50\u95ee\u9898\u5206\u89e3","text":"<p>\u5bf9\u4e8e\u95ee\u9898 \\(f(3)\\) \uff0c\u5373\u5f53\u6709\u4e09\u4e2a\u5706\u76d8\u65f6\uff0c\u60c5\u51b5\u53d8\u5f97\u7a0d\u5fae\u590d\u6742\u4e86\u4e00\u4e9b\u3002\u7531\u4e8e\u5df2\u77e5 \\(f(1)\\) \u548c \\(f(2)\\) \u7684\u89e3\uff0c\u56e0\u6b64\u53ef\u4ece\u5206\u6cbb\u89d2\u5ea6\u601d\u8003\uff0c\u5c06 <code>A</code> \u9876\u90e8\u7684\u4e24\u4e2a\u5706\u76d8\u770b\u505a\u4e00\u4e2a\u6574\u4f53\uff0c\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a</p> <ol> <li>\u4ee4 <code>B</code> \u4e3a\u76ee\u6807\u67f1\u3001<code>C</code> \u4e3a\u7f13\u51b2\u67f1\uff0c\u5c06\u4e24\u4e2a\u5706\u76d8\u4ece <code>A</code> \u79fb\u52a8\u81f3 <code>B</code> \u3002</li> <li>\u5c06 <code>A</code> \u4e2d\u5269\u4f59\u7684\u4e00\u4e2a\u5706\u76d8\u4ece <code>A</code> \u76f4\u63a5\u79fb\u52a8\u81f3 <code>C</code> \u3002</li> <li>\u4ee4 <code>C</code> \u4e3a\u76ee\u6807\u67f1\u3001<code>A</code> \u4e3a\u7f13\u51b2\u67f1\uff0c\u5c06\u4e24\u4e2a\u5706\u76d8\u4ece <code>B</code> \u79fb\u52a8\u81f3 <code>C</code> \u3002</li> </ol> <p>\u8fd9\u6837\u4e09\u4e2a\u5706\u76d8\u5c31\u88ab\u987a\u5229\u5730\u4ece <code>A</code> \u79fb\u52a8\u81f3 <code>C</code> \u4e86\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt; <p></p> <p></p> <p></p> <p></p> <p> \u56fe\uff1a\u89c4\u6a21\u4e3a 3 \u95ee\u9898\u7684\u89e3 </p> <p>\u672c\u8d28\u4e0a\u770b\uff0c\u6211\u4eec\u5c06\u95ee\u9898 \\(f(3)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(2)\\) \u548c\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u6309\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u4e4b\u540e\uff0c\u539f\u95ee\u9898\u968f\u4e4b\u5f97\u5230\u89e3\u51b3\u3002\u8fd9\u8bf4\u660e\u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff0c\u800c\u4e14\u89e3\u662f\u53ef\u4ee5\u5408\u5e76\u7684\u3002</p> <p>\u81f3\u6b64\uff0c\u6211\u4eec\u53ef\u603b\u7ed3\u51fa\u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565\uff1a\u5c06\u539f\u95ee\u9898 \\(f(n)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \u548c\u4e00\u4e2a\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u5b50\u95ee\u9898\u7684\u89e3\u51b3\u987a\u5e8f\u4e3a\uff1a</p> <ol> <li>\u5c06 \\(n-1\\) \u4e2a\u5706\u76d8\u501f\u52a9 <code>C</code> \u4ece <code>A</code> \u79fb\u81f3 <code>B</code> \u3002</li> <li>\u5c06\u5269\u4f59 \\(1\\) \u4e2a\u5706\u76d8\u4ece <code>A</code> \u76f4\u63a5\u79fb\u81f3 <code>C</code> \u3002</li> <li>\u5c06 \\(n-1\\) \u4e2a\u5706\u76d8\u501f\u52a9 <code>A</code> \u4ece <code>B</code> \u79fb\u81f3 <code>C</code> \u3002</li> </ol> <p>\u5bf9\u4e8e\u8fd9\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \uff0c\u53ef\u4ee5\u901a\u8fc7\u76f8\u540c\u7684\u65b9\u5f0f\u8fdb\u884c\u9012\u5f52\u5212\u5206\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5c0f\u5b50\u95ee\u9898 \\(f(1)\\) \u3002\u800c \\(f(1)\\) \u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u53ea\u9700\u4e00\u6b21\u79fb\u52a8\u64cd\u4f5c\u5373\u53ef\u3002</p> <p></p> <p> \u56fe\uff1a\u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565 </p>"},{"location":"chapter_divide_and_conquer/hanota_problem/#_3","title":"\u4ee3\u7801\u5b9e\u73b0","text":"<p>\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 <code>dfs(i, src, buf, tar)</code> \uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5c06\u67f1 <code>src</code> \u9876\u90e8\u7684 \\(i\\) \u4e2a\u5706\u76d8\u501f\u52a9\u7f13\u51b2\u67f1 <code>buf</code> \u79fb\u52a8\u81f3\u76ee\u6807\u67f1 <code>tar</code> \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust hanota.java<pre><code>/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List&lt;Integer&gt; src, List&lt;Integer&gt; tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nInteger pan = src.remove(src.size() - 1);\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.add(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nvoid dfs(int i, List&lt;Integer&gt; src, List&lt;Integer&gt; buf, List&lt;Integer&gt; tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i == 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nvoid solveHanota(List&lt;Integer&gt; A, List&lt;Integer&gt; B, List&lt;Integer&gt; C) {\nint n = A.size();\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n</code></pre> hanota.cpp<pre><code>/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(vector&lt;int&gt; &amp;src, vector&lt;int&gt; &amp;tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nint pan = src.back();\nsrc.pop_back();\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.push_back(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nvoid dfs(int i, vector&lt;int&gt; &amp;src, vector&lt;int&gt; &amp;buf, vector&lt;int&gt; &amp;tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i == 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nvoid hanota(vector&lt;int&gt; &amp;A, vector&lt;int&gt; &amp;B, vector&lt;int&gt; &amp;C) {\nint n = A.size();\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n</code></pre> hanota.py<pre><code>def move(src: list[int], tar: list[int]):\n\"\"\"\u79fb\u52a8\u4e00\u4e2a\u5706\u76d8\"\"\"\n# \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\npan = src.pop()\n# \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.append(pan)\ndef dfs(i: int, src: list[int], buf: list[int], tar: list[int]):\n\"\"\"\u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i)\"\"\"\n# \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif i == 1:\nmove(src, tar)\nreturn\n# \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf)\n# \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar)\n# \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar)\ndef hanota(A: list[int], B: list[int], C: list[int]):\n\"\"\"\u6c42\u89e3\u6c49\u8bfa\u5854\"\"\"\nn = len(A)\n# \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C)\n</code></pre> hanota.go<pre><code>/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunc move(src, tar *list.List) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\npan := src.Back()\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.PushBack(pan.Value)\n// \u79fb\u9664 src \u9876\u90e8\u5706\u76d8\nsrc.Remove(pan)\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nfunc dfsHanota(i int, src, buf, tar *list.List) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif i == 1 {\nmove(src, tar)\nreturn\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfsHanota(i-1, src, tar, buf)\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar)\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfsHanota(i-1, buf, src, tar)\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nfunc hanota(A, B, C *list.List) {\nn := A.Len()\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfsHanota(n, A, B, C)\n}\n</code></pre> hanota.js<pre><code>/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunction move(src, tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nconst pan = src.pop();\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.push(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nfunction dfs(i, src, buf, tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i === 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nfunction hanota(A, B, C) {\nconst n = A.length;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n</code></pre> hanota.ts<pre><code>/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfunction move(src: number[], tar: number[]): void {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nconst pan = src.pop();\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.push(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nfunction dfs(i: number, src: number[], buf: number[], tar: number[]): void {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i === 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nfunction hanota(A: number[], B: number[], C: number[]): void {\nconst n = A.length;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n</code></pre> hanota.c<pre><code>[class]{}-[func]{move}\n[class]{}-[func]{dfs}\n[class]{}-[func]{hanota}\n</code></pre> hanota.cs<pre><code>/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List&lt;int&gt; src, List&lt;int&gt; tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nint pan = src[^1];\nsrc.RemoveAt(src.Count - 1);\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.Add(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nvoid dfs(int i, List&lt;int&gt; src, List&lt;int&gt; buf, List&lt;int&gt; tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i == 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nvoid solveHanota(List&lt;int&gt; A, List&lt;int&gt; B, List&lt;int&gt; C) {\nint n = A.Count;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n</code></pre> hanota.swift<pre><code>[class]{}-[func]{move}\n[class]{}-[func]{dfs}\n[class]{}-[func]{hanota}\n</code></pre> hanota.zig<pre><code>[class]{}-[func]{move}\n[class]{}-[func]{dfs}\n[class]{}-[func]{hanota}\n</code></pre> hanota.dart<pre><code>/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nvoid move(List&lt;int&gt; src, List&lt;int&gt; tar) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nint pan = src.removeLast();\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.add(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nvoid dfs(int i, List&lt;int&gt; src, List&lt;int&gt; buf, List&lt;int&gt; tar) {\n// \u82e5 src \u53ea\u5269\u4e0b\u4e00\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif (i == 1) {\nmove(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nvoid hanota(List&lt;int&gt; A, List&lt;int&gt; B, List&lt;int&gt; C) {\nint n = A.length;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n</code></pre> hanota.rs<pre><code>/* \u79fb\u52a8\u4e00\u4e2a\u5706\u76d8 */\nfn move_pan(src: &amp;mut Vec&lt;i32&gt;, tar: &amp;mut Vec&lt;i32&gt;) {\n// \u4ece src \u9876\u90e8\u62ff\u51fa\u4e00\u4e2a\u5706\u76d8\nlet pan = src.remove(src.len() - 1);\n// \u5c06\u5706\u76d8\u653e\u5165 tar \u9876\u90e8\ntar.push(pan);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854\uff1a\u95ee\u9898 f(i) */\nfn dfs(i: i32, src: &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\u4e2a\u5706\u76d8\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u79fb\u5230 tar\nif i == 1 {\nmove_pan(src, tar);\nreturn;\n}\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 src \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 tar \u79fb\u5230 buf\ndfs(i - 1, src, tar, buf);\n// \u5b50\u95ee\u9898 f(1) \uff1a\u5c06 src \u5269\u4f59\u4e00\u4e2a\u5706\u76d8\u79fb\u5230 tar\nmove_pan(src, tar);\n// \u5b50\u95ee\u9898 f(i-1) \uff1a\u5c06 buf \u9876\u90e8 i-1 \u4e2a\u5706\u76d8\u501f\u52a9 src \u79fb\u5230 tar\ndfs(i - 1, buf, src, tar);\n}\n/* \u6c42\u89e3\u6c49\u8bfa\u5854 */\nfn hanota(A: &amp;mut Vec&lt;i32&gt;, B: &amp;mut Vec&lt;i32&gt;, C: &amp;mut Vec&lt;i32&gt;) {\nlet n = A.len() as i32;\n// \u5c06 A \u9876\u90e8 n \u4e2a\u5706\u76d8\u501f\u52a9 B \u79fb\u5230 C\ndfs(n, A, B, C);\n}\n</code></pre> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u6c49\u8bfa\u5854\u95ee\u9898\u5f62\u6210\u4e00\u4e2a\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u9012\u5f52\u6811\uff0c\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u5b50\u95ee\u9898\u3001\u5bf9\u5e94\u4e00\u4e2a\u5f00\u542f\u7684 <code>dfs()</code> \u51fd\u6570\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002</p> <p></p> <p> \u56fe\uff1a\u6c49\u8bfa\u5854\u95ee\u9898\u7684\u9012\u5f52\u6811 </p> <p>Quote</p> <p>\u6c49\u8bfa\u5854\u95ee\u9898\u6e90\u81ea\u4e00\u79cd\u53e4\u8001\u7684\u4f20\u8bf4\u6545\u4e8b\u3002\u5728\u53e4\u5370\u5ea6\u7684\u4e00\u4e2a\u5bfa\u5e99\u91cc\uff0c\u50e7\u4fa3\u4eec\u6709\u4e09\u6839\u9ad8\u5927\u7684\u94bb\u77f3\u67f1\u5b50\uff0c\u4ee5\u53ca \\(64\\) \u4e2a\u5927\u5c0f\u4e0d\u4e00\u7684\u91d1\u5706\u76d8\u3002\u50e7\u4fa3\u4eec\u4e0d\u65ad\u5730\u79fb\u52a8\u539f\u76d8\uff0c\u4ed6\u4eec\u76f8\u4fe1\u5728\u6700\u540e\u4e00\u4e2a\u5706\u76d8\u88ab\u6b63\u786e\u653e\u7f6e\u7684\u90a3\u4e00\u523b\uff0c\u8fd9\u4e2a\u4e16\u754c\u5c31\u4f1a\u7ed3\u675f\u3002</p> <p>\u7136\u800c\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u5373\u4f7f\u50e7\u4fa3\u4eec\u6bcf\u79d2\u949f\u79fb\u52a8\u4e00\u6b21\uff0c\u603b\u5171\u9700\u8981\u5927\u7ea6 \\(2^{64} \\approx 1.84\u00d710^{19}\\) \u79d2\uff0c\u5408\u7ea6 \\(5850\\) \u4ebf\u5e74\uff0c\u8fdc\u8fdc\u8d85\u8fc7\u4e86\u73b0\u5728\u5bf9\u5b87\u5b99\u5e74\u9f84\u7684\u4f30\u8ba1\u3002\u6240\u4ee5\uff0c\u5018\u82e5\u8fd9\u4e2a\u4f20\u8bf4\u662f\u771f\u7684\uff0c\u6211\u4eec\u5e94\u8be5\u4e0d\u9700\u8981\u62c5\u5fc3\u4e16\u754c\u672b\u65e5\u7684\u5230\u6765\u3002</p>"},{"location":"chapter_divide_and_conquer/summary/","title":"12.5. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u5206\u6cbb\u7b97\u6cd5\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7b97\u6cd5\u8bbe\u8ba1\u7b56\u7565\uff0c\u5305\u62ec\u5206\uff08\u5212\u5206\uff09\u548c\u6cbb\uff08\u5408\u5e76\uff09\u4e24\u4e2a\u9636\u6bb5\uff0c\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\u3002</li> <li>\u5224\u65ad\u662f\u5426\u662f\u5206\u6cbb\u7b97\u6cd5\u95ee\u9898\u7684\u4f9d\u636e\u5305\u62ec\uff1a\u95ee\u9898\u80fd\u5426\u88ab\u5206\u89e3\u3001\u5b50\u95ee\u9898\u662f\u5426\u72ec\u7acb\u3001\u5b50\u95ee\u9898\u662f\u5426\u53ef\u4ee5\u88ab\u5408\u5e76\u3002</li> <li>\u5f52\u5e76\u6392\u5e8f\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u5e94\u7528\uff0c\u5176\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u7b49\u957f\u7684\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u5230\u53ea\u5269\u4e00\u4e2a\u5143\u7d20\u65f6\u5f00\u59cb\u9010\u5c42\u5408\u5e76\uff0c\u4ece\u800c\u5b8c\u6210\u6392\u5e8f\u3002</li> <li>\u5f15\u5165\u5206\u6cbb\u7b56\u7565\u5f80\u5f80\u53ef\u4ee5\u5e26\u6765\u7b97\u6cd5\u6548\u7387\u7684\u63d0\u5347\u3002\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u7b56\u7565\u51cf\u5c11\u4e86\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\uff1b\u53e6\u4e00\u65b9\u9762\uff0c\u5206\u6cbb\u540e\u6709\u5229\u4e8e\u7cfb\u7edf\u7684\u5e76\u884c\u4f18\u5316\u3002</li> <li>\u5206\u6cbb\u65e2\u53ef\u4ee5\u89e3\u51b3\u8bb8\u591a\u7b97\u6cd5\u95ee\u9898\uff0c\u4e5f\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u8bbe\u8ba1\u4e2d\uff0c\u5904\u5904\u53ef\u89c1\u5176\u8eab\u5f71\u3002</li> <li>\u76f8\u8f83\u4e8e\u66b4\u529b\u641c\u7d22\uff0c\u81ea\u9002\u5e94\u641c\u7d22\u6548\u7387\u66f4\u9ad8\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u7684\u641c\u7d22\u7b97\u6cd5\u901a\u5e38\u90fd\u662f\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u5b9e\u73b0\u7684\u3002</li> <li>\u4e8c\u5206\u67e5\u627e\u662f\u5206\u6cbb\u601d\u60f3\u7684\u53e6\u4e00\u4e2a\u5178\u578b\u5e94\u7528\uff0c\u5b83\u4e0d\u5305\u542b\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\u7684\u6b65\u9aa4\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u5206\u6cbb\u5b9e\u73b0\u4e8c\u5206\u67e5\u627e\u3002</li> <li>\u5728\u6784\u5efa\u4e8c\u53c9\u6811\u95ee\u9898\u4e2d\uff0c\u6784\u5efa\u6811\uff08\u539f\u95ee\u9898\uff09\u53ef\u4ee5\u88ab\u5212\u5206\u4e3a\u6784\u5efa\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff08\u5b50\u95ee\u9898\uff09\uff0c\u5176\u53ef\u4ee5\u901a\u8fc7\u5212\u5206\u524d\u5e8f\u904d\u5386\u548c\u4e2d\u5e8f\u904d\u5386\u7684\u7d22\u5f15\u533a\u95f4\u6765\u5b9e\u73b0\u3002</li> <li>\u5728\u6c49\u8bfa\u5854\u95ee\u9898\u4e2d\uff0c\u4e00\u4e2a\u89c4\u6a21\u4e3a \\(n\\) \u7684\u95ee\u9898\u53ef\u4ee5\u88ab\u5212\u5206\u4e3a\u4e24\u4e2a\u89c4\u6a21\u4e3a \\(n-1\\) \u7684\u5b50\u95ee\u9898\u548c\u4e00\u4e2a\u89c4\u6a21\u4e3a \\(1\\) \u7684\u5b50\u95ee\u9898\u3002\u6309\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u540e\uff0c\u539f\u95ee\u9898\u968f\u4e4b\u5f97\u5230\u89e3\u51b3\u3002</li> </ul>"},{"location":"chapter_dynamic_programming/","title":"14. \u00a0 \u52a8\u6001\u89c4\u5212","text":"<p>Abstract</p> <p>\u5c0f\u6eaa\u6c47\u5165\u6cb3\u6d41\uff0c\u6c5f\u6cb3\u6c47\u5165\u5927\u6d77\u3002</p> <p>\u52a8\u6001\u89c4\u5212\u5c06\u5c0f\u95ee\u9898\u7684\u89e3\u6c47\u96c6\u6210\u5927\u95ee\u9898\u7684\u7b54\u6848\uff0c\u4e00\u6b65\u6b65\u5f15\u9886\u6211\u4eec\u8d70\u5411\u89e3\u51b3\u95ee\u9898\u7684\u5f7c\u5cb8\u3002</p>"},{"location":"chapter_dynamic_programming/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>14.1 \u00a0 \u521d\u63a2\u52a8\u6001\u89c4\u5212</li> <li>14.2 \u00a0 DP \u95ee\u9898\u7279\u6027</li> <li>14.3 \u00a0 DP \u89e3\u9898\u601d\u8def</li> <li>14.4 \u00a0 0-1 \u80cc\u5305\u95ee\u9898</li> <li>14.5 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898</li> <li>14.6 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898</li> <li>14.7 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_dynamic_programming/dp_problem_features/","title":"14.2. \u00a0 \u52a8\u6001\u89c4\u5212\u95ee\u9898\u7279\u6027","text":"<p>\u5728\u4e0a\u8282\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u52a8\u6001\u89c4\u5212\u662f\u5982\u4f55\u901a\u8fc7\u5b50\u95ee\u9898\u5206\u89e3\u6765\u6c42\u89e3\u95ee\u9898\u7684\u3002\u5b9e\u9645\u4e0a\uff0c\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u901a\u7528\u7684\u7b97\u6cd5\u601d\u8def\uff0c\u5728\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u3001\u56de\u6eaf\u4e2d\u7684\u4fa7\u91cd\u70b9\u4e0d\u540c\uff1a</p> <ul> <li>\u300c\u5206\u6cbb\u7b97\u6cd5\u300d\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\u5212\u5206\u4e3a\u591a\u4e2a\u76f8\u4e92\u72ec\u7acb\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u6700\u5c0f\u5b50\u95ee\u9898\uff0c\u5e76\u5728\u56de\u6eaf\u4e2d\u5408\u5e76\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u6700\u7ec8\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002</li> <li>\u300c\u52a8\u6001\u89c4\u5212\u300d\u4e5f\u5bf9\u95ee\u9898\u8fdb\u884c\u9012\u5f52\u5206\u89e3\uff0c\u4f46\u4e0e\u5206\u6cbb\u7b97\u6cd5\u7684\u4e3b\u8981\u533a\u522b\u662f\uff0c\u52a8\u6001\u89c4\u5212\u4e2d\u7684\u5b50\u95ee\u9898\u662f\u76f8\u4e92\u4f9d\u8d56\u7684\uff0c\u5728\u5206\u89e3\u8fc7\u7a0b\u4e2d\u4f1a\u51fa\u73b0\u8bb8\u591a\u91cd\u53e0\u5b50\u95ee\u9898\u3002</li> <li>\u300c\u56de\u6eaf\u7b97\u6cd5\u300d\u5728\u5c1d\u8bd5\u548c\u56de\u9000\u4e2d\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u7684\u89e3\uff0c\u5e76\u901a\u8fc7\u526a\u679d\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u641c\u7d22\u5206\u652f\u3002\u539f\u95ee\u9898\u7684\u89e3\u7531\u4e00\u7cfb\u5217\u51b3\u7b56\u6b65\u9aa4\u6784\u6210\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u51b3\u7b56\u6b65\u9aa4\u4e4b\u524d\u7684\u5b50\u5e8f\u5217\u770b\u4f5c\u4e3a\u4e00\u4e2a\u5b50\u95ee\u9898\u3002</li> </ul> <p>\u5b9e\u9645\u4e0a\uff0c\u52a8\u6001\u89c4\u5212\u5e38\u7528\u6765\u6c42\u89e3\u6700\u4f18\u5316\u95ee\u9898\uff0c\u5b83\u4eec\u4e0d\u4ec5\u5305\u542b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u8fd8\u5177\u6709\u53e6\u5916\u4e24\u5927\u7279\u6027\uff1a\u6700\u4f18\u5b50\u7ed3\u6784\u3001\u65e0\u540e\u6548\u6027\u3002</p>"},{"location":"chapter_dynamic_programming/dp_problem_features/#1421","title":"14.2.1. \u00a0 \u6700\u4f18\u5b50\u7ed3\u6784","text":"<p>\u6211\u4eec\u5bf9\u722c\u697c\u68af\u95ee\u9898\u7a0d\u4f5c\u6539\u52a8\uff0c\u4f7f\u4e4b\u66f4\u52a0\u9002\u5408\u5c55\u793a\u6700\u4f18\u5b50\u7ed3\u6784\u6982\u5ff5\u3002</p> <p>\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u6bcf\u4e00\u9636\u697c\u68af\u4e0a\u90fd\u8d34\u6709\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u8868\u793a\u4f60\u5728\u8be5\u53f0\u9636\u6240\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u4ef7\u3002\u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u6570\u7ec4 \\(cost\\) \uff0c\u5176\u4e2d \\(cost[i]\\) \u8868\u793a\u5728\u7b2c \\(i\\) \u4e2a\u53f0\u9636\u9700\u8981\u4ed8\u51fa\u7684\u4ee3\u4ef7\uff0c\\(cost[0]\\) \u4e3a\u5730\u9762\u8d77\u59cb\u70b9\u3002\u8bf7\u8ba1\u7b97\u6700\u5c11\u9700\u8981\u4ed8\u51fa\u591a\u5c11\u4ee3\u4ef7\u624d\u80fd\u5230\u8fbe\u9876\u90e8\uff1f</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u82e5\u7b2c \\(1\\) , \\(2\\) , \\(3\\) \u9636\u7684\u4ee3\u4ef7\u5206\u522b\u4e3a \\(1\\) , \\(10\\) , \\(1\\) \uff0c\u5219\u4ece\u5730\u9762\u722c\u5230\u7b2c \\(3\\) \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7\u4e3a \\(2\\) \u3002</p> <p></p> <p> \u56fe\uff1a\u722c\u5230\u7b2c 3 \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7 </p> <p>\u8bbe \\(dp[i]\\) \u4e3a\u722c\u5230\u7b2c \\(i\\) \u9636\u7d2f\u8ba1\u4ed8\u51fa\u7684\u4ee3\u4ef7\uff0c\u7531\u4e8e\u7b2c \\(i\\) \u9636\u53ea\u53ef\u80fd\u4ece \\(i - 1\\) \u9636\u6216 \\(i - 2\\) \u9636\u8d70\u6765\uff0c\u56e0\u6b64 \\(dp[i]\\) \u53ea\u53ef\u80fd\u7b49\u4e8e \\(dp[i - 1] + cost[i]\\) \u6216 \\(dp[i - 2] + cost[i]\\) \u3002\u4e3a\u4e86\u5c3d\u53ef\u80fd\u51cf\u5c11\u4ee3\u4ef7\uff0c\u6211\u4eec\u5e94\u8be5\u9009\u62e9\u4e24\u8005\u4e2d\u8f83\u5c0f\u7684\u90a3\u4e00\u4e2a\uff0c\u5373\uff1a</p> \\[ dp[i] = \\min(dp[i-1], dp[i-2]) + cost[i] \\] <p>\u8fd9\u4fbf\u53ef\u4ee5\u5f15\u51fa\u300c\u6700\u4f18\u5b50\u7ed3\u6784\u300d\u7684\u542b\u4e49\uff1a\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u662f\u4ece\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6784\u5efa\u5f97\u6765\u7684\u3002</p> <p>\u672c\u9898\u663e\u7136\u5177\u6709\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6211\u4eec\u4ece\u4e24\u4e2a\u5b50\u95ee\u9898\u6700\u4f18\u89e3 \\(dp[i-1]\\) , \\(dp[i-2]\\) \u4e2d\u6311\u9009\u51fa\u8f83\u4f18\u7684\u90a3\u4e00\u4e2a\uff0c\u5e76\u7528\u5b83\u6784\u5efa\u51fa\u539f\u95ee\u9898 \\(dp[i]\\) \u7684\u6700\u4f18\u89e3\u3002</p> <p>\u90a3\u4e48\uff0c\u4e0a\u8282\u7684\u722c\u697c\u68af\u9898\u76ee\u6709\u6ca1\u6709\u6700\u4f18\u5b50\u7ed3\u6784\u5462\uff1f\u5b83\u7684\u76ee\u6807\u662f\u6c42\u89e3\u65b9\u6848\u6570\u91cf\uff0c\u770b\u4f3c\u662f\u4e00\u4e2a\u8ba1\u6570\u95ee\u9898\uff0c\u4f46\u5982\u679c\u6362\u4e00\u79cd\u95ee\u6cd5\uff1a\u201c\u6c42\u89e3\u6700\u5927\u65b9\u6848\u6570\u91cf\u201d\u3002\u6211\u4eec\u610f\u5916\u5730\u53d1\u73b0\uff0c\u867d\u7136\u9898\u76ee\u4fee\u6539\u524d\u540e\u662f\u7b49\u4ef7\u7684\uff0c\u4f46\u6700\u4f18\u5b50\u7ed3\u6784\u6d6e\u73b0\u51fa\u6765\u4e86\uff1a\u7b2c \\(n\\) \u9636\u6700\u5927\u65b9\u6848\u6570\u91cf\u7b49\u4e8e\u7b2c \\(n-1\\) \u9636\u548c\u7b2c \\(n-2\\) \u9636\u6700\u5927\u65b9\u6848\u6570\u91cf\u4e4b\u548c\u3002\u6240\u4ee5\u8bf4\uff0c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u89e3\u91ca\u65b9\u5f0f\u6bd4\u8f83\u7075\u6d3b\uff0c\u5728\u4e0d\u540c\u95ee\u9898\u4e2d\u4f1a\u6709\u4e0d\u540c\u7684\u542b\u4e49\u3002</p> <p>\u6839\u636e\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u4ee5\u53ca\u521d\u59cb\u72b6\u6001 \\(dp[1] = cost[1]\\) , \\(dp[2] = cost[2]\\) \uff0c\u53ef\u4ee5\u5f97\u51fa\u52a8\u6001\u89c4\u5212\u4ee3\u7801\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust min_cost_climbing_stairs_dp.java<pre><code>/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(int[] cost) {\nint n = cost.length - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[] dp = new int[n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i &lt;= n; i++) {\ndp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n</code></pre> min_cost_climbing_stairs_dp.cpp<pre><code>/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(vector&lt;int&gt; &amp;cost) {\nint n = cost.size() - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvector&lt;int&gt; dp(n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i &lt;= n; i++) {\ndp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n</code></pre> min_cost_climbing_stairs_dp.py<pre><code>def min_cost_climbing_stairs_dp(cost: list[int]) -&gt; int:\n\"\"\"\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(cost) - 1\nif n == 1 or n == 2:\nreturn cost[n]\n# \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp = [0] * (n + 1)\n# \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1], dp[2] = cost[1], cost[2]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in range(3, n + 1):\ndp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\nreturn dp[n]\n</code></pre> min_cost_climbing_stairs_dp.go<pre><code>/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDP(cost []int) int {\nn := len(cost) - 1\nif n == 1 || n == 2 {\nreturn cost[n]\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp := make([]int, n+1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1]\ndp[2] = cost[2]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i &lt;= n; i++ {\ndp[i] = int(math.Min(float64(dp[i-1]), float64(dp[i-2]+cost[i])))\n}\nreturn dp[n]\n}\n</code></pre> min_cost_climbing_stairs_dp.js<pre><code>[class]{}-[func]{minCostClimbingStairsDP}\n</code></pre> min_cost_climbing_stairs_dp.ts<pre><code>[class]{}-[func]{minCostClimbingStairsDP}\n</code></pre> min_cost_climbing_stairs_dp.c<pre><code>[class]{}-[func]{minCostClimbingStairsDP}\n</code></pre> min_cost_climbing_stairs_dp.cs<pre><code>/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(int[] cost) {\nint n = cost.Length - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[] dp = new int[n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i &lt;= n; i++) {\ndp[i] = Math.Min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n</code></pre> min_cost_climbing_stairs_dp.swift<pre><code>/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDP(cost: [Int]) -&gt; Int {\nlet n = cost.count - 1\nif n == 1 || n == 2 {\nreturn cost[n]\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = Array(repeating: 0, count: n + 1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1\ndp[2] = 2\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in stride(from: 3, through: n, by: 1) {\ndp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n}\nreturn dp[n]\n}\n</code></pre> min_cost_climbing_stairs_dp.zig<pre><code>// \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212\nfn minCostClimbingStairsDP(comptime cost: []i32) i32 {\ncomptime var n = cost.len - 1;\nif (n == 1 or n == 2) {\nreturn cost[n];\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = [_]i32{-1} ** (n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (3..n + 1) |i| {\ndp[i] = @min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n</code></pre> min_cost_climbing_stairs_dp.dart<pre><code>/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDP(List&lt;int&gt; cost) {\nint n = cost.length - 1;\nif (n == 1 || n == 2) return cost[n];\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nList&lt;int&gt; dp = List.filled(n + 1, 0);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i &lt;= n; i++) {\ndp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n}\nreturn dp[n];\n}\n</code></pre> min_cost_climbing_stairs_dp.rs<pre><code>/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u52a8\u6001\u89c4\u5212 */\nfn min_cost_climbing_stairs_dp(cost: &amp;[i32]) -&gt; i32 {\nlet n = cost.len() - 1;\nif n == 1 || n == 2 { return cost[n]; }\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nlet mut dp = vec![-1; n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = cost[1];\ndp[2] = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in 3..=n {\ndp[i] = cmp::min(dp[i - 1], dp[i - 2]) + cost[i];\n}\ndp[n]\n}\n</code></pre> <p></p> <p> \u56fe\uff1a\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b </p> <p>\u672c\u9898\u4e5f\u53ef\u4ee5\u8fdb\u884c\u72b6\u6001\u538b\u7f29\uff0c\u5c06\u4e00\u7ef4\u538b\u7f29\u81f3\u96f6\u7ef4\uff0c\u4f7f\u5f97\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u4f4e\u81f3 \\(O(1)\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust min_cost_climbing_stairs_dp.java<pre><code>/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(int[] cost) {\nint n = cost.length - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\nint a = cost[1], b = cost[2];\nfor (int i = 3; i &lt;= n; i++) {\nint tmp = b;\nb = Math.min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n</code></pre> min_cost_climbing_stairs_dp.cpp<pre><code>/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(vector&lt;int&gt; &amp;cost) {\nint n = cost.size() - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\nint a = cost[1], b = cost[2];\nfor (int i = 3; i &lt;= n; i++) {\nint tmp = b;\nb = min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n</code></pre> min_cost_climbing_stairs_dp.py<pre><code>def min_cost_climbing_stairs_dp_comp(cost: list[int]) -&gt; int:\n\"\"\"\u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(cost) - 1\nif n == 1 or n == 2:\nreturn cost[n]\na, b = cost[1], cost[2]\nfor i in range(3, n + 1):\na, b = b, min(a, b) + cost[i]\nreturn b\n</code></pre> min_cost_climbing_stairs_dp.go<pre><code>/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDPComp(cost []int) int {\nn := len(cost) - 1\nif n == 1 || n == 2 {\nreturn cost[n]\n}\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\na, b := cost[1], cost[2]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i &lt;= n; i++ {\ntmp := b\nb = int(math.Min(float64(a), float64(tmp+cost[i])))\na = tmp\n}\nreturn b\n}\n</code></pre> min_cost_climbing_stairs_dp.js<pre><code>[class]{}-[func]{minCostClimbingStairsDPComp}\n</code></pre> min_cost_climbing_stairs_dp.ts<pre><code>[class]{}-[func]{minCostClimbingStairsDPComp}\n</code></pre> min_cost_climbing_stairs_dp.c<pre><code>[class]{}-[func]{minCostClimbingStairsDPComp}\n</code></pre> min_cost_climbing_stairs_dp.cs<pre><code>/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(int[] cost) {\nint n = cost.Length - 1;\nif (n == 1 || n == 2)\nreturn cost[n];\nint a = cost[1], b = cost[2];\nfor (int i = 3; i &lt;= n; i++) {\nint tmp = b;\nb = Math.Min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n</code></pre> min_cost_climbing_stairs_dp.swift<pre><code>/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minCostClimbingStairsDPComp(cost: [Int]) -&gt; Int {\nlet n = cost.count - 1\nif n == 1 || n == 2 {\nreturn cost[n]\n}\nvar (a, b) = (cost[1], cost[2])\nfor i in stride(from: 3, through: n, by: 1) {\n(a, b) = (b, min(a, b) + cost[i])\n}\nreturn b\n}\n</code></pre> min_cost_climbing_stairs_dp.zig<pre><code>// \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn minCostClimbingStairsDPComp(cost: []i32) i32 {\nvar n = cost.len - 1;\nif (n == 1 or n == 2) {\nreturn cost[n];\n}\nvar a = cost[1];\nvar b = cost[2];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (3..n + 1) |i| {\nvar tmp = b;\nb = @min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n</code></pre> min_cost_climbing_stairs_dp.dart<pre><code>/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minCostClimbingStairsDPComp(List&lt;int&gt; cost) {\nint n = cost.length - 1;\nif (n == 1 || n == 2) return cost[n];\nint a = cost[1], b = cost[2];\nfor (int i = 3; i &lt;= n; i++) {\nint tmp = b;\nb = min(a, tmp) + cost[i];\na = tmp;\n}\nreturn b;\n}\n</code></pre> min_cost_climbing_stairs_dp.rs<pre><code>/* \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn min_cost_climbing_stairs_dp_comp(cost: &amp;[i32]) -&gt; i32 {\nlet n = cost.len() - 1;\nif n == 1 || n == 2 { return cost[n] };\nlet (mut a, mut b) = (cost[1], cost[2]);\nfor i in 3..=n {\nlet tmp = b;\nb = cmp::min(a, tmp) + cost[i];\na = tmp;\n}\nb\n}\n</code></pre>"},{"location":"chapter_dynamic_programming/dp_problem_features/#1422","title":"14.2.2. \u00a0 \u65e0\u540e\u6548\u6027","text":"<p>\u300c\u65e0\u540e\u6548\u6027\u300d\u662f\u52a8\u6001\u89c4\u5212\u80fd\u591f\u6709\u6548\u89e3\u51b3\u95ee\u9898\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c\u5b9a\u4e49\u4e3a\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u786e\u5b9a\u7684\u72b6\u6001\uff0c\u5b83\u7684\u672a\u6765\u53d1\u5c55\u53ea\u4e0e\u5f53\u524d\u72b6\u6001\u6709\u5173\uff0c\u800c\u4e0e\u5f53\u524d\u72b6\u6001\u8fc7\u53bb\u6240\u7ecf\u5386\u8fc7\u7684\u6240\u6709\u72b6\u6001\u65e0\u5173\u3002</p> <p>\u4ee5\u722c\u697c\u68af\u95ee\u9898\u4e3a\u4f8b\uff0c\u7ed9\u5b9a\u72b6\u6001 \\(i\\) \uff0c\u5b83\u4f1a\u53d1\u5c55\u51fa\u72b6\u6001 \\(i+1\\) \u548c\u72b6\u6001 \\(i+2\\) \uff0c\u5206\u522b\u5bf9\u5e94\u8df3 \\(1\\) \u6b65\u548c\u8df3 \\(2\\) \u6b65\u3002\u5728\u505a\u51fa\u8fd9\u4e24\u79cd\u9009\u62e9\u65f6\uff0c\u6211\u4eec\u65e0\u9700\u8003\u8651\u72b6\u6001 \\(i\\) \u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5b83\u4eec\u5bf9\u72b6\u6001 \\(i\\) \u7684\u672a\u6765\u6ca1\u6709\u5f71\u54cd\u3002</p> <p>\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u5411\u722c\u697c\u68af\u95ee\u9898\u6dfb\u52a0\u4e00\u4e2a\u7ea6\u675f\uff0c\u60c5\u51b5\u5c31\u4e0d\u4e00\u6837\u4e86\u3002</p> <p>\u5e26\u7ea6\u675f\u722c\u697c\u68af</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u4f46\u4e0d\u80fd\u8fde\u7eed\u4e24\u8f6e\u8df3 \\(1\\) \u9636\uff0c\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002</p> <p>\u4f8b\u5982\uff0c\u722c\u4e0a\u7b2c \\(3\\) \u9636\u4ec5\u5269 \\(2\\) \u79cd\u53ef\u884c\u65b9\u6848\uff0c\u5176\u4e2d\u8fde\u7eed\u4e09\u6b21\u8df3 \\(1\\) \u9636\u7684\u65b9\u6848\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\uff0c\u56e0\u6b64\u88ab\u820d\u5f03\u3002</p> <p></p> <p> \u56fe\uff1a\u5e26\u7ea6\u675f\u722c\u5230\u7b2c 3 \u9636\u7684\u65b9\u6848\u6570\u91cf </p> <p>\u5728\u8be5\u95ee\u9898\u4e2d\uff0c\u5982\u679c\u4e0a\u4e00\u8f6e\u662f\u8df3 \\(1\\) \u9636\u4e0a\u6765\u7684\uff0c\u90a3\u4e48\u4e0b\u4e00\u8f6e\u5c31\u5fc5\u987b\u8df3 \\(2\\) \u9636\u3002\u8fd9\u610f\u5473\u7740\uff0c\u4e0b\u4e00\u6b65\u9009\u62e9\u4e0d\u80fd\u7531\u5f53\u524d\u72b6\u6001\uff08\u5f53\u524d\u697c\u68af\u9636\u6570\uff09\u72ec\u7acb\u51b3\u5b9a\uff0c\u8fd8\u548c\u524d\u4e00\u4e2a\u72b6\u6001\uff08\u4e0a\u8f6e\u697c\u68af\u9636\u6570\uff09\u6709\u5173\u3002</p> <p>\u4e0d\u96be\u53d1\u73b0\uff0c\u6b64\u95ee\u9898\u5df2\u4e0d\u6ee1\u8db3\u65e0\u540e\u6548\u6027\uff0c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b \\(dp[i] = dp[i-1] + dp[i-2]\\) \u4e5f\u5931\u6548\u4e86\uff0c\u56e0\u4e3a \\(dp[i-1]\\) \u4ee3\u8868\u672c\u8f6e\u8df3 \\(1\\) \u9636\uff0c\u4f46\u5176\u4e2d\u5305\u542b\u4e86\u8bb8\u591a\u201c\u4e0a\u4e00\u8f6e\u8df3 \\(1\\) \u9636\u4e0a\u6765\u7684\u201d\u65b9\u6848\uff0c\u800c\u4e3a\u4e86\u6ee1\u8db3\u7ea6\u675f\uff0c\u6211\u4eec\u5c31\u4e0d\u80fd\u5c06 \\(dp[i-1]\\) \u76f4\u63a5\u8ba1\u5165 \\(dp[i]\\) \u4e2d\u3002</p> <p>\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u6269\u5c55\u72b6\u6001\u5b9a\u4e49\uff1a\u72b6\u6001 \\([i, j]\\) \u8868\u793a\u5904\u5728\u7b2c \\(i\\) \u9636\u3001\u5e76\u4e14\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(j\\) \u9636\uff0c\u5176\u4e2d \\(j \\in \\{1, 2\\}\\) \u3002\u6b64\u72b6\u6001\u5b9a\u4e49\u6709\u6548\u5730\u533a\u5206\u4e86\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(1\\) \u9636\u8fd8\u662f \\(2\\) \u9636\uff0c\u6211\u4eec\u53ef\u4ee5\u636e\u6b64\u6765\u51b3\u5b9a\u4e0b\u4e00\u6b65\u8be5\u600e\u4e48\u8df3\uff1a</p> <ul> <li>\u5f53 \\(j\\) \u7b49\u4e8e \\(1\\) \uff0c\u5373\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(1\\) \u9636\u65f6\uff0c\u8fd9\u4e00\u8f6e\u53ea\u80fd\u9009\u62e9\u8df3 \\(2\\) \u9636\u3002</li> <li>\u5f53 \\(j\\) \u7b49\u4e8e \\(2\\) \uff0c\u5373\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(2\\) \u9636\u65f6\uff0c\u8fd9\u4e00\u8f6e\u53ef\u9009\u62e9\u8df3 \\(1\\) \u9636\u6216\u8df3 \\(2\\) \u9636\u3002</li> </ul> <p>\u5728\u8be5\u5b9a\u4e49\u4e0b\uff0c\\(dp[i, j]\\) \u8868\u793a\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u65b9\u6848\u6570\u3002\u5728\u8be5\u5b9a\u4e49\u4e0b\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\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> \u56fe\uff1a\u8003\u8651\u7ea6\u675f\u4e0b\u7684\u9012\u63a8\u5173\u7cfb </p> <p>\u6700\u7ec8\uff0c\u8fd4\u56de \\(dp[n, 1] + dp[n, 2]\\) \u5373\u53ef\uff0c\u4e24\u8005\u4e4b\u548c\u4ee3\u8868\u722c\u5230\u7b2c \\(n\\) \u9636\u7684\u65b9\u6848\u603b\u6570\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust climbing_stairs_constraint_dp.java<pre><code>/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\nif (n == 1 || n == 2) {\nreturn n;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[][] dp = new int[n + 1][3];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i &lt;= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n</code></pre> climbing_stairs_constraint_dp.cpp<pre><code>/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\nif (n == 1 || n == 2) {\nreturn n;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(3, 0));\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i &lt;= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n</code></pre> climbing_stairs_constraint_dp.py<pre><code>def climbing_stairs_constraint_dp(n: int) -&gt; int:\n\"\"\"\u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nif n == 1 or n == 2:\nreturn n\n# \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp = [[0] * 3 for _ in range(n + 1)]\n# \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1], dp[1][2] = 1, 0\ndp[2][1], dp[2][2] = 0, 1\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in range(3, n + 1):\ndp[i][1] = dp[i - 1][2]\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2]\nreturn dp[n][1] + dp[n][2]\n</code></pre> climbing_stairs_constraint_dp.go<pre><code>/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsConstraintDP(n int) int {\nif n == 1 || n == 2 {\nreturn n\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp := make([][3]int, n+1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1\ndp[1][2] = 0\ndp[2][1] = 0\ndp[2][2] = 1\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i &lt;= n; i++ {\ndp[i][1] = dp[i-1][2]\ndp[i][2] = dp[i-2][1] + dp[i-2][2]\n}\nreturn dp[n][1] + dp[n][2]\n}\n</code></pre> climbing_stairs_constraint_dp.js<pre><code>[class]{}-[func]{climbingStairsConstraintDP}\n</code></pre> climbing_stairs_constraint_dp.ts<pre><code>[class]{}-[func]{climbingStairsConstraintDP}\n</code></pre> climbing_stairs_constraint_dp.c<pre><code>[class]{}-[func]{climbingStairsConstraintDP}\n</code></pre> climbing_stairs_constraint_dp.cs<pre><code>/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\nif (n == 1 || n == 2) {\nreturn n;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[,] dp = new int[n + 1, 3];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1, 1] = 1;\ndp[1, 2] = 0;\ndp[2, 1] = 0;\ndp[2, 2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i &lt;= n; i++) {\ndp[i, 1] = dp[i - 1, 2];\ndp[i, 2] = dp[i - 2, 1] + dp[i - 2, 2];\n}\nreturn dp[n, 1] + dp[n, 2];\n}\n</code></pre> climbing_stairs_constraint_dp.swift<pre><code>/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsConstraintDP(n: Int) -&gt; Int {\nif n == 1 || n == 2 {\nreturn n\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1\ndp[1][2] = 0\ndp[2][1] = 0\ndp[2][2] = 1\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in stride(from: 3, through: n, by: 1) {\ndp[i][1] = dp[i - 1][2]\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n}\nreturn dp[n][1] + dp[n][2]\n}\n</code></pre> climbing_stairs_constraint_dp.zig<pre><code>// \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\nfn climbingStairsConstraintDP(comptime n: usize) i32 {\nif (n == 1 or n == 2) {\nreturn @intCast(n);\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = [_][3]i32{ [_]i32{ -1, -1, -1 } } ** (n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (3..n + 1) |i| {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n</code></pre> climbing_stairs_constraint_dp.dart<pre><code>/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsConstraintDP(int n) {\nif (n == 1 || n == 2) {\nreturn n;\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nList&lt;List&lt;int&gt;&gt; dp = List.generate(n + 1, (index) =&gt; List.filled(3, 0));\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i &lt;= n; i++) {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\nreturn dp[n][1] + dp[n][2];\n}\n</code></pre> climbing_stairs_constraint_dp.rs<pre><code>/* \u5e26\u7ea6\u675f\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_constraint_dp(n: usize) -&gt; i32 {\nif n == 1 || n == 2 { return n as i32 };\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nlet mut dp = vec![vec![-1; 3]; n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1][1] = 1;\ndp[1][2] = 0;\ndp[2][1] = 0;\ndp[2][2] = 1;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in 3..=n {\ndp[i][1] = dp[i - 1][2];\ndp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n}\ndp[n][1] + dp[n][2]\n}\n</code></pre> <p>\u5728\u4e0a\u9762\u7684\u6848\u4f8b\u4e2d\uff0c\u7531\u4e8e\u4ec5\u9700\u591a\u8003\u8651\u524d\u9762\u4e00\u4e2a\u72b6\u6001\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u6269\u5c55\u72b6\u6001\u5b9a\u4e49\uff0c\u4f7f\u5f97\u95ee\u9898\u6062\u590d\u65e0\u540e\u6548\u6027\u3002\u7136\u800c\uff0c\u8bb8\u591a\u95ee\u9898\u5177\u6709\u975e\u5e38\u4e25\u91cd\u7684\u201c\u6709\u540e\u6548\u6027\u201d\uff0c\u4f8b\u5982\uff1a</p> <p>\u722c\u697c\u68af\u4e0e\u969c\u788d\u751f\u6210</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\u3002\u89c4\u5b9a\u5f53\u722c\u5230\u7b2c \\(i\\) \u9636\u65f6\uff0c\u7cfb\u7edf\u81ea\u52a8\u4f1a\u7ed9\u7b2c \\(2i\\) \u9636\u4e0a\u653e\u4e0a\u969c\u788d\u7269\uff0c\u4e4b\u540e\u6240\u6709\u8f6e\u90fd\u4e0d\u5141\u8bb8\u8df3\u5230\u7b2c \\(2i\\) \u9636\u4e0a\u3002\u4f8b\u5982\uff0c\u524d\u4e24\u8f6e\u5206\u522b\u8df3\u5230\u4e86\u7b2c \\(2, 3\\) \u9636\u4e0a\uff0c\u5219\u4e4b\u540e\u5c31\u4e0d\u80fd\u8df3\u5230\u7b2c \\(4, 6\\) \u9636\u4e0a\u3002\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002</p> <p>\u5728\u8fd9\u4e2a\u95ee\u9898\u4e2d\uff0c\u4e0b\u6b21\u8df3\u8dc3\u4f9d\u8d56\u4e8e\u8fc7\u53bb\u6240\u6709\u7684\u72b6\u6001\uff0c\u56e0\u4e3a\u6bcf\u4e00\u6b21\u8df3\u8dc3\u90fd\u4f1a\u5728\u66f4\u9ad8\u7684\u9636\u68af\u4e0a\u8bbe\u7f6e\u969c\u788d\uff0c\u5e76\u5f71\u54cd\u672a\u6765\u7684\u8df3\u8dc3\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u95ee\u9898\uff0c\u52a8\u6001\u89c4\u5212\u5f80\u5f80\u96be\u4ee5\u89e3\u51b3\u3002</p> <p>\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u590d\u6742\u7684\u7ec4\u5408\u4f18\u5316\u95ee\u9898\uff08\u4f8b\u5982\u65c5\u884c\u5546\u95ee\u9898\uff09\u90fd\u4e0d\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u3002\u5bf9\u4e8e\u8fd9\u7c7b\u95ee\u9898\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u62e9\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\uff0c\u4f8b\u5982\u542f\u53d1\u5f0f\u641c\u7d22\u3001\u9057\u4f20\u7b97\u6cd5\u3001\u5f3a\u5316\u5b66\u4e60\u7b49\uff0c\u4ece\u800c\u5728\u6709\u9650\u65f6\u95f4\u5185\u5f97\u5230\u53ef\u7528\u7684\u5c40\u90e8\u6700\u4f18\u89e3\u3002</p>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/","title":"14.3. \u00a0 \u52a8\u6001\u89c4\u5212\u89e3\u9898\u601d\u8def","text":"<p>\u4e0a\u4e24\u8282\u4ecb\u7ecd\u4e86\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4e3b\u8981\u7279\u5f81\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u4e00\u8d77\u63a2\u7a76\u4e24\u4e2a\u66f4\u52a0\u5b9e\u7528\u7684\u95ee\u9898\uff1a</p> <ol> <li>\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u95ee\u9898\u662f\u4e0d\u662f\u52a8\u6001\u89c4\u5212\u95ee\u9898\uff1f</li> <li>\u6c42\u89e3\u52a8\u6001\u89c4\u5212\u95ee\u9898\u8be5\u4ece\u4f55\u5904\u5165\u624b\uff0c\u5b8c\u6574\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f</li> </ol>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1431","title":"14.3.1. \u00a0 \u95ee\u9898\u5224\u65ad","text":"<p>\u603b\u7684\u6765\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u95ee\u9898\u5305\u542b\u91cd\u53e0\u5b50\u95ee\u9898\u3001\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5e76\u6ee1\u8db3\u65e0\u540e\u6548\u6027\uff0c\u90a3\u4e48\u5b83\u901a\u5e38\u5c31\u9002\u5408\u7528\u52a8\u6001\u89c4\u5212\u6c42\u89e3\u3002\u7136\u800c\uff0c\u6211\u4eec\u5f88\u96be\u4ece\u95ee\u9898\u63cf\u8ff0\u4e0a\u76f4\u63a5\u63d0\u53d6\u51fa\u8fd9\u4e9b\u7279\u6027\u3002\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u653e\u5bbd\u6761\u4ef6\uff0c\u5148\u89c2\u5bdf\u95ee\u9898\u662f\u5426\u9002\u5408\u4f7f\u7528\u56de\u6eaf\uff08\u7a77\u4e3e\uff09\u89e3\u51b3\u3002</p> <p>\u9002\u5408\u7528\u56de\u6eaf\u89e3\u51b3\u7684\u95ee\u9898\u901a\u5e38\u6ee1\u8db3\u201c\u51b3\u7b56\u6811\u6a21\u578b\u201d\uff0c\u8fd9\u79cd\u95ee\u9898\u53ef\u4ee5\u4f7f\u7528\u6811\u5f62\u7ed3\u6784\u6765\u63cf\u8ff0\uff0c\u5176\u4e2d\u6bcf\u4e00\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u51b3\u7b56\uff0c\u6bcf\u4e00\u6761\u8def\u5f84\u4ee3\u8868\u4e00\u4e2a\u51b3\u7b56\u5e8f\u5217\u3002</p> <p>\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u95ee\u9898\u5305\u542b\u660e\u786e\u7684\u51b3\u7b56\u6982\u5ff5\uff0c\u5e76\u4e14\u89e3\u662f\u901a\u8fc7\u4e00\u7cfb\u5217\u51b3\u7b56\u4ea7\u751f\u7684\uff0c\u90a3\u4e48\u5b83\u5c31\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u56de\u6eaf\u6765\u89e3\u51b3\u3002</p> <p>\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u8fd8\u6709\u4e00\u4e9b\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u201c\u52a0\u5206\u9879\u201d\uff0c\u5305\u62ec\uff1a</p> <ul> <li>\u95ee\u9898\u5305\u542b\u6700\u5927\uff08\u5c0f\uff09\u6216\u6700\u591a\uff08\u5c11\uff09\u7b49\u6700\u4f18\u5316\u63cf\u8ff0\u3002</li> <li>\u95ee\u9898\u7684\u72b6\u6001\u80fd\u591f\u4f7f\u7528\u4e00\u4e2a\u5217\u8868\u3001\u591a\u7ef4\u77e9\u9635\u6216\u6811\u6765\u8868\u793a\uff0c\u5e76\u4e14\u4e00\u4e2a\u72b6\u6001\u4e0e\u5176\u5468\u56f4\u7684\u72b6\u6001\u5b58\u5728\u9012\u63a8\u5173\u7cfb\u3002</li> </ul> <p>\u800c\u76f8\u5e94\u7684\u201c\u51cf\u5206\u9879\u201d\u5305\u62ec\uff1a</p> <ul> <li>\u95ee\u9898\u7684\u76ee\u6807\u662f\u627e\u51fa\u6240\u6709\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u4e0d\u662f\u627e\u51fa\u6700\u4f18\u89e3\u3002</li> <li>\u95ee\u9898\u63cf\u8ff0\u4e2d\u6709\u660e\u663e\u7684\u6392\u5217\u7ec4\u5408\u7684\u7279\u5f81\uff0c\u9700\u8981\u8fd4\u56de\u5177\u4f53\u7684\u591a\u4e2a\u65b9\u6848\u3002</li> </ul> <p>\u5982\u679c\u4e00\u4e2a\u95ee\u9898\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u5e76\u5177\u6709\u8f83\u4e3a\u660e\u663e\u7684\u201c\u52a0\u5206\u9879\u201c\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5047\u8bbe\u5b83\u662f\u4e00\u4e2a\u52a8\u6001\u89c4\u5212\u95ee\u9898\uff0c\u5e76\u5728\u6c42\u89e3\u8fc7\u7a0b\u4e2d\u9a8c\u8bc1\u5b83\u3002</p>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1432","title":"14.3.2. \u00a0 \u95ee\u9898\u6c42\u89e3\u6b65\u9aa4","text":"<p>\u52a8\u6001\u89c4\u5212\u7684\u89e3\u9898\u6d41\u7a0b\u4f1a\u56e0\u95ee\u9898\u7684\u6027\u8d28\u548c\u96be\u5ea6\u800c\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u901a\u5e38\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u63cf\u8ff0\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u5efa\u7acb \\(dp\\) \u8868\uff0c\u63a8\u5bfc\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u7b49\u3002</p> <p>\u4e3a\u4e86\u66f4\u5f62\u8c61\u5730\u5c55\u793a\u89e3\u9898\u6b65\u9aa4\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\u300c\u6700\u5c0f\u8def\u5f84\u548c\u300d\u6765\u4e3e\u4f8b\u3002</p> <p>Question</p> <p>\u7ed9\u5b9a\u4e00\u4e2a \\(n \\times m\\) \u7684\u4e8c\u7ef4\u7f51\u683c <code>grid</code> \uff0c\u7f51\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5305\u542b\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\uff0c\u8868\u793a\u8be5\u5355\u5143\u683c\u7684\u4ee3\u4ef7\u3002\u673a\u5668\u4eba\u4ee5\u5de6\u4e0a\u89d2\u5355\u5143\u683c\u4e3a\u8d77\u59cb\u70b9\uff0c\u6bcf\u6b21\u53ea\u80fd\u5411\u4e0b\u6216\u8005\u5411\u53f3\u79fb\u52a8\u4e00\u6b65\uff0c\u76f4\u81f3\u5230\u8fbe\u53f3\u4e0b\u89d2\u5355\u5143\u683c\u3002\u8bf7\u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230\u53f3\u4e0b\u89d2\u7684\u6700\u5c0f\u8def\u5f84\u548c\u3002</p> <p>\u4f8b\u5982\u4ee5\u4e0b\u793a\u4f8b\u6570\u636e\uff0c\u7ed9\u5b9a\u7f51\u683c\u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e3a \\(13\\) \u3002</p> <p></p> <p> \u56fe\uff1a\u6700\u5c0f\u8def\u5f84\u548c\u793a\u4f8b\u6570\u636e </p> <p>\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868</p> <p>\u672c\u9898\u7684\u6bcf\u4e00\u8f6e\u7684\u51b3\u7b56\u5c31\u662f\u4ece\u5f53\u524d\u683c\u5b50\u5411\u4e0b\u6216\u5411\u53f3\u4e00\u6b65\u3002\u8bbe\u5f53\u524d\u683c\u5b50\u7684\u884c\u5217\u7d22\u5f15\u4e3a \\([i, j]\\) \uff0c\u5219\u5411\u4e0b\u6216\u5411\u53f3\u8d70\u4e00\u6b65\u540e\uff0c\u7d22\u5f15\u53d8\u4e3a \\([i+1, j]\\) \u6216 \\([i, j+1]\\) \u3002\u56e0\u6b64\uff0c\u72b6\u6001\u5e94\u5305\u542b\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u4e24\u4e2a\u53d8\u91cf\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002</p> <p>\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u4ece\u8d77\u59cb\u70b9 \\([0, 0]\\) \u8d70\u5230 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\uff0c\u89e3\u8bb0\u4e3a \\(dp[i, j]\\) \u3002</p> <p>\u81f3\u6b64\uff0c\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u4e00\u4e2a\u4e8c\u7ef4 \\(dp\\) \u77e9\u9635\uff0c\u5176\u5c3a\u5bf8\u4e0e\u8f93\u5165\u7f51\u683c \\(grid\\) \u76f8\u540c\u3002</p> <p></p> <p> \u56fe\uff1a\u72b6\u6001\u5b9a\u4e49\u4e0e dp \u8868 </p> <p>Note</p> <p>\u52a8\u6001\u89c4\u5212\u548c\u56de\u6eaf\u8fc7\u7a0b\u53ef\u4ee5\u88ab\u63cf\u8ff0\u4e3a\u4e00\u4e2a\u51b3\u7b56\u5e8f\u5217\uff0c\u800c\u72b6\u6001\u7531\u6240\u6709\u51b3\u7b56\u53d8\u91cf\u6784\u6210\u3002\u5b83\u5e94\u5f53\u5305\u542b\u63cf\u8ff0\u89e3\u9898\u8fdb\u5ea6\u7684\u6240\u6709\u53d8\u91cf\uff0c\u5176\u5305\u542b\u4e86\u8db3\u591f\u7684\u4fe1\u606f\uff0c\u80fd\u591f\u7528\u6765\u63a8\u5bfc\u51fa\u4e0b\u4e00\u4e2a\u72b6\u6001\u3002</p> <p>\u6bcf\u4e2a\u72b6\u6001\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5b50\u95ee\u9898\uff0c\u6211\u4eec\u4f1a\u5b9a\u4e49\u4e00\u4e2a \\(dp\\) \u8868\u6765\u5b58\u50a8\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u72b6\u6001\u7684\u6bcf\u4e2a\u72ec\u7acb\u53d8\u91cf\u90fd\u662f \\(dp\\) \u8868\u7684\u4e00\u4e2a\u7ef4\u5ea6\u3002\u672c\u8d28\u4e0a\u770b\uff0c\\(dp\\) \u8868\u662f\u72b6\u6001\u548c\u5b50\u95ee\u9898\u7684\u89e3\u4e4b\u95f4\u7684\u6620\u5c04\u3002</p> <p>\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b</p> <p>\u5bf9\u4e8e\u72b6\u6001 \\([i, j]\\) \uff0c\u5b83\u53ea\u80fd\u4ece\u4e0a\u8fb9\u683c\u5b50 \\([i-1, j]\\) \u548c\u5de6\u8fb9\u683c\u5b50 \\([i, j-1]\\) \u8f6c\u79fb\u800c\u6765\u3002\u56e0\u6b64\u6700\u4f18\u5b50\u7ed3\u6784\u4e3a\uff1a\u5230\u8fbe \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u7531 \\([i, j-1]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e0e \\([i-1, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c\uff0c\u8fd9\u4e24\u8005\u8f83\u5c0f\u7684\u90a3\u4e00\u4e2a\u51b3\u5b9a\u3002</p> <p>\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u63a8\u51fa\u4ee5\u4e0b\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a</p> \\[ dp[i, j] = \\min(dp[i-1, j], dp[i, j-1]) + grid[i, j] \\] <p></p> <p> \u56fe\uff1a\u6700\u4f18\u5b50\u7ed3\u6784\u4e0e\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b </p> <p>Note</p> <p>\u6839\u636e\u5b9a\u4e49\u597d\u7684 \\(dp\\) \u8868\uff0c\u601d\u8003\u539f\u95ee\u9898\u548c\u5b50\u95ee\u9898\u7684\u5173\u7cfb\uff0c\u627e\u51fa\u901a\u8fc7\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6765\u6784\u9020\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u7684\u65b9\u6cd5\uff0c\u5373\u6700\u4f18\u5b50\u7ed3\u6784\u3002</p> <p>\u4e00\u65e6\u6211\u4eec\u627e\u5230\u4e86\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u6784\u5efa\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002</p> <p>\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f</p> <p>\u5728\u672c\u9898\u4e2d\uff0c\u5904\u5728\u9996\u884c\u7684\u72b6\u6001\u53ea\u80fd\u5411\u53f3\u8f6c\u79fb\uff0c\u9996\u5217\u72b6\u6001\u53ea\u80fd\u5411\u4e0b\u8f6c\u79fb\uff0c\u56e0\u6b64\u9996\u884c \\(i = 0\\) \u548c\u9996\u5217 \\(j = 0\\) \u662f\u8fb9\u754c\u6761\u4ef6\u3002</p> <p>\u6bcf\u4e2a\u683c\u5b50\u662f\u7531\u5176\u5de6\u65b9\u683c\u5b50\u548c\u4e0a\u65b9\u683c\u5b50\u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528\u91c7\u7528\u5faa\u73af\u6765\u904d\u5386\u77e9\u9635\uff0c\u5916\u5faa\u73af\u904d\u5386\u5404\u884c\u3001\u5185\u5faa\u73af\u904d\u5386\u5404\u5217\u3002</p> <p></p> <p> \u56fe\uff1a\u8fb9\u754c\u6761\u4ef6\u4e0e\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f </p> <p>Note</p> <p>\u8fb9\u754c\u6761\u4ef6\u5728\u52a8\u6001\u89c4\u5212\u4e2d\u7528\u4e8e\u521d\u59cb\u5316 \\(dp\\) \u8868\uff0c\u5728\u641c\u7d22\u4e2d\u7528\u4e8e\u526a\u679d\u3002</p> <p>\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f\u7684\u6838\u5fc3\u662f\u8981\u4fdd\u8bc1\u5728\u8ba1\u7b97\u5f53\u524d\u95ee\u9898\u7684\u89e3\u65f6\uff0c\u6240\u6709\u5b83\u4f9d\u8d56\u7684\u66f4\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u90fd\u5df2\u7ecf\u88ab\u6b63\u786e\u5730\u8ba1\u7b97\u51fa\u6765\u3002</p> <p>\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u5df2\u7ecf\u53ef\u4ee5\u76f4\u63a5\u5199\u51fa\u52a8\u6001\u89c4\u5212\u4ee3\u7801\u3002\u7136\u800c\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u4ece\u9876\u81f3\u5e95\u7684\u601d\u60f3\uff0c\u56e0\u6b64\u6309\u7167\u201c\u66b4\u529b\u641c\u7d22 \\(\\rightarrow\\) \u8bb0\u5fc6\u5316\u641c\u7d22 \\(\\rightarrow\\) \u52a8\u6001\u89c4\u5212\u201d\u7684\u987a\u5e8f\u5b9e\u73b0\u66f4\u52a0\u7b26\u5408\u601d\u7ef4\u4e60\u60ef\u3002</p>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#_1","title":"\u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"<p>\u4ece\u72b6\u6001 \\([i, j]\\) \u5f00\u59cb\u641c\u7d22\uff0c\u4e0d\u65ad\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u72b6\u6001 \\([i-1, j]\\) \u548c \\([i, j-1]\\) \uff0c\u5305\u62ec\u4ee5\u4e0b\u9012\u5f52\u8981\u7d20\uff1a</p> <ul> <li>\u9012\u5f52\u53c2\u6570\uff1a\u72b6\u6001 \\([i, j]\\) \u3002</li> <li>\u8fd4\u56de\u503c\uff1a\u4ece \\([0, 0]\\) \u5230 \\([i, j]\\) \u7684\u6700\u5c0f\u8def\u5f84\u548c \\(dp[i, j]\\) \u3002</li> <li>\u7ec8\u6b62\u6761\u4ef6\uff1a\u5f53 \\(i = 0\\) \u4e14 \\(j = 0\\) \u65f6\uff0c\u8fd4\u56de\u4ee3\u4ef7 \\(grid[0, 0]\\) \u3002</li> <li>\u526a\u679d\uff1a\u5f53 \\(i &lt; 0\\) \u65f6\u6216 \\(j &lt; 0\\) \u65f6\u7d22\u5f15\u8d8a\u754c\uff0c\u6b64\u65f6\u8fd4\u56de\u4ee3\u4ef7 \\(+\\infty\\) \uff0c\u4ee3\u8868\u4e0d\u53ef\u884c\u3002</li> </ul> JavaC++PythonGoJSTSCC#SwiftZigDartRust min_path_sum.java<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(int[][] grid, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 &amp;&amp; j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i &lt; 0 || j &lt; 0) {\nreturn Integer.MAX_VALUE;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint left = minPathSumDFS(grid, i - 1, j);\nint up = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn Math.min(left, up) + grid[i][j];\n}\n</code></pre> min_path_sum.cpp<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(vector&lt;vector&lt;int&gt;&gt; &amp;grid, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 &amp;&amp; j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i &lt; 0 || j &lt; 0) {\nreturn INT_MAX;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint left = minPathSumDFS(grid, i - 1, j);\nint up = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n}\n</code></pre> 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\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22\"\"\"\n# \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 and j == 0:\nreturn grid[0][0]\n# \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i &lt; 0 or j &lt; 0:\nreturn inf\n# \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nleft = min_path_sum_dfs(grid, i - 1, j)\nup = min_path_sum_dfs(grid, i, j - 1)\n# \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn min(left, up) + grid[i][j]\n</code></pre> min_path_sum.go<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc minPathSumDFS(grid [][]int, i, j int) int {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 &amp;&amp; j == 0 {\nreturn grid[0][0]\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i &lt; 0 || j &lt; 0 {\nreturn math.MaxInt\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nleft := minPathSumDFS(grid, i-1, j)\nup := minPathSumDFS(grid, i, j-1)\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn int(math.Min(float64(left), float64(up))) + grid[i][j]\n}\n</code></pre> min_path_sum.js<pre><code>[class]{}-[func]{minPathSumDFS}\n</code></pre> min_path_sum.ts<pre><code>[class]{}-[func]{minPathSumDFS}\n</code></pre> min_path_sum.c<pre><code>[class]{}-[func]{minPathSumDFS}\n</code></pre> min_path_sum.cs<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(int[][] grid, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 &amp;&amp; j == 0){\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i &lt; 0 || j &lt; 0) {\nreturn int.MaxValue;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint left = minPathSumDFS(grid, i - 1, j);\nint up = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn Math.Min(left, up) + grid[i][j];\n}\n</code></pre> min_path_sum.swift<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc minPathSumDFS(grid: [[Int]], i: Int, j: Int) -&gt; Int {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0, j == 0 {\nreturn grid[0][0]\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i &lt; 0 || j &lt; 0 {\nreturn .max\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nlet left = minPathSumDFS(grid: grid, i: i - 1, j: j)\nlet up = minPathSumDFS(grid: grid, i: i, j: j - 1)\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn min(left, up) + grid[i][j]\n}\n</code></pre> min_path_sum.zig<pre><code>// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22\nfn minPathSumDFS(grid: anytype, i: i32, j: i32) i32 {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 and j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i &lt; 0 or j &lt; 0) {\nreturn std.math.maxInt(i32);\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nvar left = minPathSumDFS(grid, i - 1, j);\nvar up = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n</code></pre> min_path_sum.dart<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nint minPathSumDFS(List&lt;List&lt;int&gt;&gt; grid, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 &amp;&amp; j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i &lt; 0 || j &lt; 0) {\n// \u5728 Dart \u4e2d\uff0cint \u7c7b\u578b\u662f\u56fa\u5b9a\u8303\u56f4\u7684\u6574\u6570\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u65e0\u7a77\u5927\u201d\u7684\u503c\nreturn BigInt.from(2).pow(31).toInt();\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint left = minPathSumDFS(grid, i - 1, j);\nint up = minPathSumDFS(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nreturn min(left, up) + grid[i][j];\n}\n</code></pre> min_path_sum.rs<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u66b4\u529b\u641c\u7d22 */\nfn min_path_sum_dfs(grid: &amp;Vec&lt;Vec&lt;i32&gt;&gt;, i: i32, j: i32) -&gt; i32 {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 &amp;&amp; j == 0 {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i &lt; 0 || j &lt; 0 {\nreturn i32::MAX;\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nlet left = min_path_sum_dfs(grid, i - 1, j);\nlet up = min_path_sum_dfs(grid, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nstd::cmp::min(left, up) + grid[i as usize][j as usize]\n}\n</code></pre> <p>\u4e0b\u56fe\u7ed9\u51fa\u4e86\u4ee5 \\(dp[2, 1]\\) \u4e3a\u6839\u8282\u70b9\u7684\u9012\u5f52\u6811\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e9b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5176\u6570\u91cf\u4f1a\u968f\u7740\u7f51\u683c <code>grid</code> \u7684\u5c3a\u5bf8\u53d8\u5927\u800c\u6025\u5267\u589e\u591a\u3002</p> <p>\u672c\u8d28\u4e0a\u770b\uff0c\u9020\u6210\u91cd\u53e0\u5b50\u95ee\u9898\u7684\u539f\u56e0\u4e3a\uff1a\u5b58\u5728\u591a\u6761\u8def\u5f84\u53ef\u4ee5\u4ece\u5de6\u4e0a\u89d2\u5230\u8fbe\u67d0\u4e00\u5355\u5143\u683c\u3002</p> <p></p> <p> \u56fe\uff1a\u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811 </p> <p>\u6bcf\u4e2a\u72b6\u6001\u90fd\u6709\u5411\u4e0b\u548c\u5411\u53f3\u4e24\u79cd\u9009\u62e9\uff0c\u4ece\u5de6\u4e0a\u89d2\u8d70\u5230\u53f3\u4e0b\u89d2\u603b\u5171\u9700\u8981 \\(m + n - 2\\) \u6b65\uff0c\u6240\u4ee5\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^{m + n})\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u8fd9\u79cd\u8ba1\u7b97\u65b9\u5f0f\u672a\u8003\u8651\u4e34\u8fd1\u7f51\u683c\u8fb9\u754c\u7684\u60c5\u51b5\uff0c\u5f53\u5230\u8fbe\u7f51\u7edc\u8fb9\u754c\u65f6\u53ea\u5269\u4e0b\u4e00\u79cd\u9009\u62e9\u3002\u56e0\u6b64\u5b9e\u9645\u7684\u8def\u5f84\u6570\u91cf\u4f1a\u5c11\u4e00\u4e9b\u3002</p>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#_2","title":"\u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"<p>\u6211\u4eec\u5f15\u5165\u4e00\u4e2a\u548c\u7f51\u683c <code>grid</code> \u76f8\u540c\u5c3a\u5bf8\u7684\u8bb0\u5fc6\u5217\u8868 <code>mem</code> \uff0c\u7528\u4e8e\u8bb0\u5f55\u5404\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5e76\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u8fdb\u884c\u526a\u679d\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust min_path_sum.java<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 &amp;&amp; j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i &lt; 0 || j &lt; 0) {\nreturn Integer.MAX_VALUE;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint left = minPathSumDFSMem(grid, mem, i - 1, j);\nint up = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = Math.min(left, up) + grid[i][j];\nreturn mem[i][j];\n}\n</code></pre> min_path_sum.cpp<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\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\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 &amp;&amp; j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i &lt; 0 || j &lt; 0) {\nreturn INT_MAX;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint left = minPathSumDFSMem(grid, mem, i - 1, j);\nint up = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\nreturn mem[i][j];\n}\n</code></pre> min_path_sum.py<pre><code>def min_path_sum_dfs_mem(\ngrid: list[list[int]], mem: list[list[int]], i: int, j: int\n) -&gt; int:\n\"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n# \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 and j == 0:\nreturn grid[0][0]\n# \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i &lt; 0 or j &lt; 0:\nreturn inf\n# \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][j] != -1:\nreturn mem[i][j]\n# \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nleft = min_path_sum_dfs_mem(grid, mem, i - 1, j)\nup = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n# \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = min(left, up) + grid[i][j]\nreturn mem[i][j]\n</code></pre> min_path_sum.go<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc minPathSumDFSMem(grid, mem [][]int, i, j int) int {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 &amp;&amp; j == 0 {\nreturn grid[0][0]\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i &lt; 0 || j &lt; 0 {\nreturn math.MaxInt\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][j] != -1 {\nreturn mem[i][j]\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nleft := minPathSumDFSMem(grid, mem, i-1, j)\nup := minPathSumDFSMem(grid, mem, i, j-1)\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = int(math.Min(float64(left), float64(up))) + grid[i][j]\nreturn mem[i][j]\n}\n</code></pre> min_path_sum.js<pre><code>[class]{}-[func]{minPathSumDFSMem}\n</code></pre> min_path_sum.ts<pre><code>[class]{}-[func]{minPathSumDFSMem}\n</code></pre> min_path_sum.c<pre><code>[class]{}-[func]{minPathSumDFSMem}\n</code></pre> min_path_sum.cs<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 &amp;&amp; j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i &lt; 0 || j &lt; 0) {\nreturn int.MaxValue;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint left = minPathSumDFSMem(grid, mem, i - 1, j);\nint up = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = Math.Min(left, up) + grid[i][j];\nreturn mem[i][j];\n}\n</code></pre> min_path_sum.swift<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc minPathSumDFSMem(grid: [[Int]], mem: inout [[Int]], i: Int, j: Int) -&gt; Int {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0, j == 0 {\nreturn grid[0][0]\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i &lt; 0 || j &lt; 0 {\nreturn .max\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][j] != -1 {\nreturn mem[i][j]\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nlet left = minPathSumDFSMem(grid: grid, mem: &amp;mem, i: i - 1, j: j)\nlet up = minPathSumDFSMem(grid: grid, mem: &amp;mem, i: i, j: j - 1)\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = min(left, up) + grid[i][j]\nreturn mem[i][j]\n}\n</code></pre> min_path_sum.zig<pre><code>// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn minPathSumDFSMem(grid: anytype, mem: anytype, i: i32, j: i32) i32 {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 and j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i &lt; 0 or j &lt; 0) {\nreturn std.math.maxInt(i32);\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] != -1) {\nreturn mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n// \u8ba1\u7b97\u4ece\u5de6\u4e0a\u89d2\u5230 (i-1, j) \u548c (i, j-1) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nvar left = minPathSumDFSMem(grid, mem, i - 1, j);\nvar up = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8fd4\u56de\u4ece\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[@as(usize, @intCast(i))][@as(usize, @intCast(j))] = @min(left, up) + grid[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\nreturn mem[@as(usize, @intCast(i))][@as(usize, @intCast(j))];\n}\n</code></pre> min_path_sum.dart<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint minPathSumDFSMem(List&lt;List&lt;int&gt;&gt; grid, List&lt;List&lt;int&gt;&gt; mem, int i, int j) {\n// \u82e5\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif (i == 0 &amp;&amp; j == 0) {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif (i &lt; 0 || j &lt; 0) {\n// \u5728 Dart \u4e2d\uff0cint \u7c7b\u578b\u662f\u56fa\u5b9a\u8303\u56f4\u7684\u6574\u6570\uff0c\u4e0d\u5b58\u5728\u8868\u793a\u201c\u65e0\u7a77\u5927\u201d\u7684\u503c\nreturn BigInt.from(2).pow(31).toInt();\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][j] != -1) {\nreturn mem[i][j];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nint left = minPathSumDFSMem(grid, mem, i - 1, j);\nint up = minPathSumDFSMem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i][j] = min(left, up) + grid[i][j];\nreturn mem[i][j];\n}\n</code></pre> min_path_sum.rs<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\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\u4e3a\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u5219\u7ec8\u6b62\u641c\u7d22\nif i == 0 &amp;&amp; j == 0 {\nreturn grid[0][0];\n}\n// \u82e5\u884c\u5217\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de +\u221e \u4ee3\u4ef7\nif i &lt; 0 || j &lt; 0 {\nreturn i32::MAX;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i as usize][j as usize] != -1 {\nreturn mem[i as usize][j as usize];\n}\n// \u5de6\u8fb9\u548c\u4e0a\u8fb9\u5355\u5143\u683c\u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nlet left = min_path_sum_dfs_mem(grid, mem, i - 1, j);\nlet up = min_path_sum_dfs_mem(grid, mem, i, j - 1);\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u5de6\u4e0a\u89d2\u5230 (i, j) \u7684\u6700\u5c0f\u8def\u5f84\u4ee3\u4ef7\nmem[i as usize][j as usize] = std::cmp::min(left, up) + grid[i as usize][j as usize];\nmem[i as usize][j as usize]\n}\n</code></pre> <p>\u5f15\u5165\u8bb0\u5fc6\u5316\u540e\uff0c\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\u53ea\u9700\u8ba1\u7b97\u4e00\u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u72b6\u6001\u603b\u6570\uff0c\u5373\u7f51\u683c\u5c3a\u5bf8 \\(O(nm)\\) \u3002</p> <p></p> <p> \u56fe\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811 </p>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#_3","title":"\u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"<p>\u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust min_path_sum.java<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(int[][] grid) {\nint n = grid.length, m = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n][m];\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j &lt; m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (int i = 1; i &lt; n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i &lt; n; i++) {\nfor (int j = 1; j &lt; m; j++) {\ndp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n</code></pre> min_path_sum.cpp<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(vector&lt;vector&lt;int&gt;&gt; &amp;grid) {\nint n = grid.size(), m = grid[0].size();\n// \u521d\u59cb\u5316 dp \u8868\nvector&lt;vector&lt;int&gt;&gt; dp(n, vector&lt;int&gt;(m));\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j &lt; m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (int i = 1; i &lt; n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i &lt; n; i++) {\nfor (int j = 1; j &lt; m; j++) {\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n</code></pre> min_path_sum.py<pre><code>def min_path_sum_dp(grid: list[list[int]]) -&gt; int:\n\"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn, m = len(grid), len(grid[0])\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * m for _ in range(n)]\ndp[0][0] = grid[0][0]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in range(1, m):\ndp[0][j] = dp[0][j - 1] + grid[0][j]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor i in range(1, n):\ndp[i][0] = dp[i - 1][0] + grid[i][0]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in range(1, n):\nfor j in range(1, m):\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\nreturn dp[n - 1][m - 1]\n</code></pre> min_path_sum.go<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDP(grid [][]int) int {\nn, m := len(grid), len(grid[0])\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n)\nfor i := 0; i &lt; n; i++ {\ndp[i] = make([]int, m)\n}\ndp[0][0] = grid[0][0]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j := 1; j &lt; m; j++ {\ndp[0][j] = dp[0][j-1] + grid[0][j]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor i := 1; i &lt; n; i++ {\ndp[i][0] = dp[i-1][0] + grid[i][0]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i &lt; n; i++ {\nfor j := 1; j &lt; m; j++ {\ndp[i][j] = int(math.Min(float64(dp[i][j-1]), float64(dp[i-1][j]))) + grid[i][j]\n}\n}\nreturn dp[n-1][m-1]\n}\n</code></pre> min_path_sum.js<pre><code>[class]{}-[func]{minPathSumDP}\n</code></pre> min_path_sum.ts<pre><code>[class]{}-[func]{minPathSumDP}\n</code></pre> min_path_sum.c<pre><code>[class]{}-[func]{minPathSumDP}\n</code></pre> min_path_sum.cs<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(int[][] grid) {\nint n = grid.Length, m = grid[0].Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n, m];\ndp[0, 0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j &lt; m; j++) {\ndp[0, j] = dp[0, j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (int i = 1; i &lt; n; i++) {\ndp[i, 0] = dp[i - 1, 0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i &lt; n; i++) {\nfor (int j = 1; j &lt; m; j++) {\ndp[i, j] = Math.Min(dp[i, j - 1], dp[i - 1, j]) + grid[i][j];\n}\n}\nreturn dp[n - 1, m - 1];\n}\n</code></pre> min_path_sum.swift<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDP(grid: [[Int]]) -&gt; Int {\nlet n = grid.count\nlet m = grid[0].count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: m), count: n)\ndp[0][0] = grid[0][0]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in stride(from: 1, to: m, by: 1) {\ndp[0][j] = dp[0][j - 1] + grid[0][j]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor i in stride(from: 1, to: n, by: 1) {\ndp[i][0] = dp[i - 1][0] + grid[i][0]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in stride(from: 1, to: n, by: 1) {\nfor j in stride(from: 1, to: m, by: 1) {\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n}\n}\nreturn dp[n - 1][m - 1]\n}\n</code></pre> min_path_sum.zig<pre><code>// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212\nfn minPathSumDP(comptime grid: anytype) i32 {\ncomptime var n = grid.len;\ncomptime var m = grid[0].len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][m]i32{[_]i32{0} ** m} ** n;\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (1..m) |j| {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (1..n) |i| {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (1..n) |i| {\nfor (1..m) |j| {\ndp[i][j] = @min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n</code></pre> min_path_sum.dart<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nint minPathSumDP(List&lt;List&lt;int&gt;&gt; grid) {\nint n = grid.length, m = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nList&lt;List&lt;int&gt;&gt; dp = List.generate(n, (i) =&gt; List.filled(m, 0));\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j &lt; m; j++) {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor (int i = 1; i &lt; n; i++) {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i &lt; n; i++) {\nfor (int j = 1; j &lt; m; j++) {\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\nreturn dp[n - 1][m - 1];\n}\n</code></pre> min_path_sum.rs<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u52a8\u6001\u89c4\u5212 */\nfn min_path_sum_dp(grid: &amp;Vec&lt;Vec&lt;i32&gt;&gt;) -&gt; i32 {\nlet (n, m) = (grid.len(), grid[0].len());\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; m]; n];\ndp[0][0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in 1..m {\ndp[0][j] = dp[0][j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nfor i in 1..n {\ndp[i][0] = dp[i - 1][0] + grid[i][0];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in 1..n {\nfor j in 1..m {\ndp[i][j] = std::cmp::min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n}\n}\ndp[n - 1][m - 1]\n}\n</code></pre> <p>\u4e0b\u56fe\u5c55\u793a\u4e86\u6700\u5c0f\u8def\u5f84\u548c\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\uff0c\u5176\u904d\u5386\u4e86\u6574\u4e2a\u7f51\u683c\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nm)\\) \u3002</p> <p>\u6570\u7ec4 <code>dp</code> \u5927\u5c0f\u4e3a \\(n \\times m\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(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> \u56fe\uff1a\u6700\u5c0f\u8def\u5f84\u548c\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b </p>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#_4","title":"\u72b6\u6001\u538b\u7f29","text":"<p>\u7531\u4e8e\u6bcf\u4e2a\u683c\u5b50\u53ea\u4e0e\u5176\u5de6\u8fb9\u548c\u4e0a\u8fb9\u7684\u683c\u5b50\u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u53ea\u7528\u4e00\u4e2a\u5355\u884c\u6570\u7ec4\u6765\u5b9e\u73b0 \\(dp\\) \u8868\u3002</p> <p>\u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u6570\u7ec4 <code>dp</code> \u53ea\u80fd\u8868\u793a\u4e00\u884c\u7684\u72b6\u6001\uff0c\u6240\u4ee5\u6211\u4eec\u65e0\u6cd5\u63d0\u524d\u521d\u59cb\u5316\u9996\u5217\u72b6\u6001\uff0c\u800c\u662f\u5728\u904d\u5386\u6bcf\u884c\u4e2d\u66f4\u65b0\u5b83\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust min_path_sum.java<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(int[][] grid) {\nint n = grid.length, m = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[m];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (int j = 1; j &lt; m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i &lt; n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j &lt; m; j++) {\ndp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n</code></pre> min_path_sum.cpp<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(vector&lt;vector&lt;int&gt;&gt; &amp;grid) {\nint n = grid.size(), m = grid[0].size();\n// \u521d\u59cb\u5316 dp \u8868\nvector&lt;int&gt; dp(m);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (int j = 1; j &lt; m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i &lt; n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j &lt; m; j++) {\ndp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n</code></pre> min_path_sum.py<pre><code>def min_path_sum_dp_comp(grid: list[list[int]]) -&gt; int:\n\"\"\"\u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn, m = len(grid), len(grid[0])\n# \u521d\u59cb\u5316 dp \u8868\ndp = [0] * m\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0]\nfor j in range(1, m):\ndp[j] = dp[j - 1] + grid[0][j]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in range(1, n):\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in range(1, m):\ndp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\nreturn dp[m - 1]\n</code></pre> min_path_sum.go<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDPComp(grid [][]int) int {\nn, m := len(grid), len(grid[0])\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, m)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0]\nfor j := 1; j &lt; m; j++ {\ndp[j] = dp[j-1] + grid[0][j]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i &lt; n; i++ {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j := 1; j &lt; m; j++ {\ndp[j] = int(math.Min(float64(dp[j-1]), float64(dp[j]))) + grid[i][j]\n}\n}\nreturn dp[m-1]\n}\n</code></pre> min_path_sum.js<pre><code>[class]{}-[func]{minPathSumDPComp}\n</code></pre> min_path_sum.ts<pre><code>[class]{}-[func]{minPathSumDPComp}\n</code></pre> min_path_sum.c<pre><code>[class]{}-[func]{minPathSumDPComp}\n</code></pre> min_path_sum.cs<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(int[][] grid) {\nint n = grid.Length, m = grid[0].Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[m];\ndp[0] = grid[0][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j &lt; m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i &lt; n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j &lt; m; j++) {\ndp[j] = Math.Min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n</code></pre> min_path_sum.swift<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc minPathSumDPComp(grid: [[Int]]) -&gt; Int {\nlet n = grid.count\nlet m = grid[0].count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: 0, count: m)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0]\nfor j in stride(from: 1, to: m, by: 1) {\ndp[j] = dp[j - 1] + grid[0][j]\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in stride(from: 1, to: n, by: 1) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0]\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in stride(from: 1, to: m, by: 1) {\ndp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n}\n}\nreturn dp[m - 1]\n}\n</code></pre> min_path_sum.zig<pre><code>// \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn minPathSumDPComp(comptime grid: anytype) i32 {\ncomptime var n = grid.len;\ncomptime var m = grid[0].len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** m;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor (1..m) |j| {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (1..n) |i| {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\nfor (1..m) |j| {\ndp[j] = @min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n</code></pre> min_path_sum.dart<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint minPathSumDPComp(List&lt;List&lt;int&gt;&gt; grid) {\nint n = grid.length, m = grid[0].length;\n// \u521d\u59cb\u5316 dp \u8868\nList&lt;int&gt; dp = List.filled(m, 0);\ndp[0] = grid[0][0];\nfor (int j = 1; j &lt; m; j++) {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i &lt; n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j &lt; m; j++) {\ndp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\nreturn dp[m - 1];\n}\n</code></pre> min_path_sum.rs<pre><code>/* \u6700\u5c0f\u8def\u5f84\u548c\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn min_path_sum_dp_comp(grid: &amp;Vec&lt;Vec&lt;i32&gt;&gt;) -&gt; i32 {\nlet (n, m) = (grid.len(), grid[0].len());\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; m];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\ndp[0] = grid[0][0];\nfor j in 1..m {\ndp[j] = dp[j - 1] + grid[0][j];\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in 1..n {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\ndp[0] = dp[0] + grid[i][0];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in 1..m {\ndp[j] = std::cmp::min(dp[j - 1], dp[j]) + grid[i][j];\n}\n}\ndp[m - 1]\n}\n</code></pre>"},{"location":"chapter_dynamic_programming/edit_distance_problem/","title":"14.6. \u00a0 \u7f16\u8f91\u8ddd\u79bb\u95ee\u9898","text":"<p>\u7f16\u8f91\u8ddd\u79bb\uff0c\u4e5f\u88ab\u79f0\u4e3a Levenshtein \u8ddd\u79bb\uff0c\u6307\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u4e92\u76f8\u8f6c\u6362\u7684\u6700\u5c0f\u4fee\u6539\u6b21\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u5728\u4fe1\u606f\u68c0\u7d22\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\u5ea6\u91cf\u4e24\u4e2a\u5e8f\u5217\u7684\u76f8\u4f3c\u5ea6\u3002</p> <p>Question</p> <p>\u8f93\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32 \\(s\\) \u548c \\(t\\) \uff0c\u8fd4\u56de\u5c06 \\(s\\) \u8f6c\u6362\u4e3a \\(t\\) \u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002</p> <p>\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u8fdb\u884c\u4e09\u79cd\u7f16\u8f91\u64cd\u4f5c\uff1a\u63d2\u5165\u4e00\u4e2a\u5b57\u7b26\u3001\u5220\u9664\u4e00\u4e2a\u5b57\u7b26\u3001\u66ff\u6362\u5b57\u7b26\u4e3a\u4efb\u610f\u4e00\u4e2a\u5b57\u7b26\u3002</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5c06 <code>kitten</code> \u8f6c\u6362\u4e3a <code>sitting</code> \u9700\u8981\u7f16\u8f91 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u6362\u64cd\u4f5c\u4e0e 1 \u6b21\u6dfb\u52a0\u64cd\u4f5c\uff1b\u5c06 <code>hello</code> \u8f6c\u6362\u4e3a <code>algo</code> \u9700\u8981 3 \u6b65\uff0c\u5305\u62ec 2 \u6b21\u66ff\u6362\u64cd\u4f5c\u548c 1 \u6b21\u5220\u9664\u64cd\u4f5c\u3002</p> <p></p> <p> \u56fe\uff1a\u7f16\u8f91\u8ddd\u79bb\u7684\u793a\u4f8b\u6570\u636e </p> <p>\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u53ef\u4ee5\u5f88\u81ea\u7136\u5730\u7528\u51b3\u7b56\u6811\u6a21\u578b\u6765\u89e3\u91ca\u3002\u5b57\u7b26\u4e32\u5bf9\u5e94\u6811\u8282\u70b9\uff0c\u4e00\u8f6e\u51b3\u7b56\uff08\u4e00\u6b21\u7f16\u8f91\u64cd\u4f5c\uff09\u5bf9\u5e94\u6811\u7684\u4e00\u6761\u8fb9\u3002</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5728\u4e0d\u9650\u5236\u64cd\u4f5c\u7684\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ef\u4ee5\u6d3e\u751f\u51fa\u8bb8\u591a\u6761\u8fb9\uff0c\u6bcf\u6761\u8fb9\u5bf9\u5e94\u4e00\u79cd\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u4ece <code>hello</code> \u8f6c\u6362\u5230 <code>algo</code> \u6709\u8bb8\u591a\u79cd\u53ef\u80fd\u7684\u8def\u5f84\u3002</p> <p>\u4ece\u51b3\u7b56\u6811\u7684\u89d2\u5ea6\u770b\uff0c\u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u8282\u70b9 <code>hello</code> \u548c\u8282\u70b9 <code>algo</code> \u4e4b\u95f4\u7684\u6700\u77ed\u8def\u5f84\u3002</p> <p></p> <p> \u56fe\uff1a\u57fa\u4e8e\u51b3\u7b56\u6811\u6a21\u578b\u8868\u793a\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898 </p> <p>\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868</p> <p>\u6bcf\u4e00\u8f6e\u7684\u51b3\u7b56\u662f\u5bf9\u5b57\u7b26\u4e32 \\(s\\) \u8fdb\u884c\u4e00\u6b21\u7f16\u8f91\u64cd\u4f5c\u3002</p> <p>\u6211\u4eec\u5e0c\u671b\u5728\u7f16\u8f91\u64cd\u4f5c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u95ee\u9898\u7684\u89c4\u6a21\u9010\u6e10\u7f29\u5c0f\uff0c\u8fd9\u6837\u624d\u80fd\u6784\u5efa\u5b50\u95ee\u9898\u3002\u8bbe\u5b57\u7b26\u4e32 \\(s\\) \u548c \\(t\\) \u7684\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\) \u548c \\(m\\) \uff0c\u6211\u4eec\u5148\u8003\u8651\u4e24\u5b57\u7b26\u4e32\u5c3e\u90e8\u7684\u5b57\u7b26 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \uff1a</p> <ul> <li>\u82e5 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u76f8\u540c\uff0c\u6211\u4eec\u53ef\u4ee5\u8df3\u8fc7\u5b83\u4eec\uff0c\u76f4\u63a5\u8003\u8651 \\(s[n-2]\\) \u548c \\(t[m-2]\\) \u3002</li> <li>\u82e5 \\(s[n-1]\\) \u548c \\(t[m-1]\\) \u4e0d\u540c\uff0c\u6211\u4eec\u9700\u8981\u5bf9 \\(s\\) \u8fdb\u884c\u4e00\u6b21\u7f16\u8f91\uff08\u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\uff09\uff0c\u4f7f\u5f97\u4e24\u5b57\u7b26\u4e32\u5c3e\u90e8\u7684\u5b57\u7b26\u76f8\u540c\uff0c\u4ece\u800c\u53ef\u4ee5\u8df3\u8fc7\u5b83\u4eec\uff0c\u8003\u8651\u89c4\u6a21\u66f4\u5c0f\u7684\u95ee\u9898\u3002</li> </ul> <p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u5728\u5b57\u7b26\u4e32 \\(s\\) \u4e2d\u8fdb\u884c\u7684\u6bcf\u4e00\u8f6e\u51b3\u7b56\uff08\u7f16\u8f91\u64cd\u4f5c\uff09\uff0c\u90fd\u4f1a\u4f7f\u5f97 \\(s\\) \u548c \\(t\\) \u4e2d\u5269\u4f59\u7684\u5f85\u5339\u914d\u5b57\u7b26\u53d1\u751f\u53d8\u5316\u3002\u56e0\u6b64\uff0c\u72b6\u6001\u4e3a\u5f53\u524d\u5728 \\(s\\) , \\(t\\) \u4e2d\u8003\u8651\u7684\u7b2c \\(i\\) , \\(j\\) \u4e2a\u5b57\u7b26\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002</p> <p>\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\uff1a\u5c06 \\(s\\) \u7684\u524d \\(i\\) \u4e2a\u5b57\u7b26\u66f4\u6539\u4e3a \\(t\\) \u7684\u524d \\(j\\) \u4e2a\u5b57\u7b26\u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002</p> <p>\u81f3\u6b64\uff0c\u5f97\u5230\u4e00\u4e2a\u5c3a\u5bf8\u4e3a \\((i+1) \\times (j+1)\\) \u7684\u4e8c\u7ef4 \\(dp\\) \u8868\u3002</p> <p>\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b</p> <p>\u8003\u8651\u5b50\u95ee\u9898 \\(dp[i, j]\\) \uff0c\u5176\u5bf9\u5e94\u7684\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5c3e\u90e8\u5b57\u7b26\u4e3a \\(s[i-1]\\) \u548c \\(t[j-1]\\) \uff0c\u53ef\u6839\u636e\u4e0d\u540c\u7f16\u8f91\u64cd\u4f5c\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\uff1a</p> <ol> <li>\u5728 \\(s[i-1]\\) \u4e4b\u540e\u6dfb\u52a0 \\(t[j-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i, j-1]\\) \u3002</li> <li>\u5220\u9664 \\(s[i-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i-1, j]\\) \u3002</li> <li>\u5c06 \\(s[i-1]\\) \u66ff\u6362\u4e3a \\(t[j-1]\\) \uff0c\u5219\u5269\u4f59\u5b50\u95ee\u9898 \\(dp[i-1, j-1]\\) \u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u7f16\u8f91\u8ddd\u79bb\u7684\u72b6\u6001\u8f6c\u79fb </p> <p>\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u53ef\u5f97\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\\(dp[i, j]\\) \u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(dp[i, j-1]\\) , \\(dp[i-1, j]\\) , \\(dp[i-1, j-1]\\) \u4e09\u8005\u4e2d\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\uff0c\u518d\u52a0\u4e0a\u672c\u6b21\u7684\u7f16\u8f91\u6b65\u6570 \\(1\\) \u3002\u5bf9\u5e94\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a</p> \\[ dp[i, j] = \\min(dp[i, j-1], dp[i-1, j], dp[i-1, j-1]) + 1 \\] <p>\u8bf7\u6ce8\u610f\uff0c\u5f53 \\(s[i-1]\\) \u548c \\(t[j-1]\\) \u76f8\u540c\u65f6\uff0c\u65e0\u9700\u7f16\u8f91\u5f53\u524d\u5b57\u7b26\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a</p> \\[ dp[i, j] = dp[i-1, j-1] \\] <p>\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f</p> <p>\u5f53\u4e24\u5b57\u7b26\u4e32\u90fd\u4e3a\u7a7a\u65f6\uff0c\u7f16\u8f91\u6b65\u6570\u4e3a \\(0\\) \uff0c\u5373 \\(dp[0, 0] = 0\\) \u3002\u5f53 \\(s\\) \u4e3a\u7a7a\u4f46 \\(t\\) \u4e0d\u4e3a\u7a7a\u65f6\uff0c\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u7b49\u4e8e \\(t\\) \u7684\u957f\u5ea6\uff0c\u5373\u9996\u884c \\(dp[0, j] = j\\) \u3002\u5f53 \\(s\\) \u4e0d\u4e3a\u7a7a\u4f46 \\(t\\) \u4e3a\u7a7a\u65f6\uff0c\u7b49\u4e8e \\(s\\) \u7684\u957f\u5ea6\uff0c\u5373\u9996\u5217 \\(dp[i, 0] = i\\) \u3002</p> <p>\u89c2\u5bdf\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff0c\u89e3 \\(dp[i, j]\\) \u4f9d\u8d56\u5de6\u65b9\u3001\u4e0a\u65b9\u3001\u5de6\u4e0a\u65b9\u7684\u89e3\uff0c\u56e0\u6b64\u901a\u8fc7\u4e24\u5c42\u5faa\u73af\u6b63\u5e8f\u904d\u5386\u6574\u4e2a \\(dp\\) \u8868\u5373\u53ef\u3002</p>"},{"location":"chapter_dynamic_programming/edit_distance_problem/#_1","title":"\u4ee3\u7801\u5b9e\u73b0","text":"JavaC++PythonGoJSTSCC#SwiftZigDartRust edit_distance.java<pre><code>/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(String s, String t) {\nint n = s.length(), m = t.length();\nint[][] dp = new int[n + 1][m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int i = 1; i &lt;= n; i++) {\ndp[i][0] = i;\n}\nfor (int j = 1; j &lt;= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i &lt;= n; i++) {\nfor (int j = 1; j &lt;= m; j++) {\nif (s.charAt(i - 1) == t.charAt(j - 1)) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n</code></pre> edit_distance.cpp<pre><code>/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(string s, string t) {\nint n = s.length(), m = t.length();\nvector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(m + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int i = 1; i &lt;= n; i++) {\ndp[i][0] = i;\n}\nfor (int j = 1; j &lt;= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i &lt;= n; i++) {\nfor (int j = 1; j &lt;= m; j++) {\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n</code></pre> edit_distance.py<pre><code>def edit_distance_dp(s: str, t: str) -&gt; int:\n\"\"\"\u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn, m = len(s), len(t)\ndp = [[0] * (m + 1) for _ in range(n + 1)]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor i in range(1, n + 1):\ndp[i][0] = i\nfor j in range(1, m + 1):\ndp[0][j] = j\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in range(1, n + 1):\nfor j in range(1, m + 1):\nif s[i - 1] == t[j - 1]:\n# \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1]\nelse:\n# \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1\nreturn dp[n][m]\n</code></pre> edit_distance.go<pre><code>/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDP(s string, t string) int {\nn := len(s)\nm := len(t)\ndp := make([][]int, n+1)\nfor i := 0; i &lt;= n; i++ {\ndp[i] = make([]int, m+1)\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor i := 1; i &lt;= n; i++ {\ndp[i][0] = i\n}\nfor j := 1; j &lt;= m; j++ {\ndp[0][j] = j\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i &lt;= n; i++ {\nfor j := 1; j &lt;= m; j++ {\nif s[i-1] == t[j-1] {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i-1][j-1]\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = MinInt(MinInt(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1\n}\n}\n}\nreturn dp[n][m]\n}\n</code></pre> edit_distance.js<pre><code>[class]{}-[func]{editDistanceDP}\n</code></pre> edit_distance.ts<pre><code>[class]{}-[func]{editDistanceDP}\n</code></pre> edit_distance.c<pre><code>[class]{}-[func]{editDistanceDP}\n</code></pre> edit_distance.cs<pre><code>/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(string s, string t) {\nint n = s.Length, m = t.Length;\nint[,] dp = new int[n + 1, m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int i = 1; i &lt;= n; i++) {\ndp[i, 0] = i;\n}\nfor (int j = 1; j &lt;= m; j++) {\ndp[0, j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i &lt;= n; i++) {\nfor (int j = 1; j &lt;= m; j++) {\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i, j] = dp[i - 1, j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i, j] = Math.Min(Math.Min(dp[i, j - 1], dp[i - 1, j]), dp[i - 1, j - 1]) + 1;\n}\n}\n}\nreturn dp[n, m];\n}\n</code></pre> edit_distance.swift<pre><code>/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDP(s: String, t: String) -&gt; Int {\nlet n = s.utf8CString.count\nlet m = t.utf8CString.count\nvar dp = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor i in stride(from: 1, through: n, by: 1) {\ndp[i][0] = i\n}\nfor j in stride(from: 1, through: m, by: 1) {\ndp[0][j] = j\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in stride(from: 1, through: n, by: 1) {\nfor j in stride(from: 1, through: m, by: 1) {\nif s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1]\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n}\n}\n}\nreturn dp[n][m]\n}\n</code></pre> edit_distance.zig<pre><code>// \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212\nfn editDistanceDP(comptime s: []const u8, comptime t: []const u8) i32 {\ncomptime var n = s.len;\ncomptime var m = t.len;\nvar dp = [_][m + 1]i32{[_]i32{0} ** (m + 1)} ** (n + 1);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (1..n + 1) |i| {\ndp[i][0] = @intCast(i);\n}\nfor (1..m + 1) |j| {\ndp[0][j] = @intCast(j);\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (1..n + 1) |i| {\nfor (1..m + 1) |j| {\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = @min(@min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n</code></pre> edit_distance.dart<pre><code>/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nint editDistanceDP(String s, String t) {\nint n = s.length, m = t.length;\nList&lt;List&lt;int&gt;&gt; dp = List.generate(n + 1, (_) =&gt; List.filled(m + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int i = 1; i &lt;= n; i++) {\ndp[i][0] = i;\n}\nfor (int j = 1; j &lt;= m; j++) {\ndp[0][j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i &lt;= n; i++) {\nfor (int j = 1; j &lt;= m; j++) {\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\nreturn dp[n][m];\n}\n</code></pre> edit_distance.rs<pre><code>/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u52a8\u6001\u89c4\u5212 */\nfn edit_distance_dp(s: &amp;str, t: &amp;str) -&gt; i32 {\nlet (n, m) = (s.len(), t.len());\nlet mut dp = vec![vec![0; m + 1]; n + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor i in 1..= n {\ndp[i][0] = i as i32;\n}\nfor j in 1..m {\ndp[0][j] = j as i32;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in 1..=n {\nfor j in 1..=m {\nif s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[i][j] = dp[i - 1][j - 1];\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[i][j] = std::cmp::min(std::cmp::min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n}\n}\n}\ndp[n][m]\n}\n</code></pre> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\u4e0e\u80cc\u5305\u95ee\u9898\u975e\u5e38\u7c7b\u4f3c\uff0c\u90fd\u53ef\u4ee5\u770b\u4f5c\u662f\u586b\u5199\u4e00\u4e2a\u4e8c\u7ef4\u7f51\u683c\u7684\u8fc7\u7a0b\u3002</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> \u56fe\uff1a\u7f16\u8f91\u8ddd\u79bb\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b </p>"},{"location":"chapter_dynamic_programming/edit_distance_problem/#_2","title":"\u72b6\u6001\u538b\u7f29","text":"<p>\u7531\u4e8e \\(dp[i,j]\\) \u662f\u7531\u4e0a\u65b9 \\(dp[i-1, j]\\) \u3001\u5de6\u65b9 \\(dp[i, j-1]\\) \u3001\u5de6\u4e0a\u65b9\u72b6\u6001 \\(dp[i-1, j-1]\\) \u8f6c\u79fb\u800c\u6765\uff0c\u800c\u6b63\u5e8f\u904d\u5386\u4f1a\u4e22\u5931\u5de6\u4e0a\u65b9 \\(dp[i-1, j-1]\\) \uff0c\u5012\u5e8f\u904d\u5386\u65e0\u6cd5\u63d0\u524d\u6784\u5efa \\(dp[i, j-1]\\) \uff0c\u56e0\u6b64\u4e24\u79cd\u904d\u5386\u987a\u5e8f\u90fd\u4e0d\u53ef\u53d6\u3002</p> <p>\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf <code>leftup</code> \u6765\u6682\u5b58\u5de6\u4e0a\u65b9\u7684\u89e3 \\(dp[i-1, j-1]\\) \uff0c\u4ece\u800c\u53ea\u9700\u8003\u8651\u5de6\u65b9\u548c\u4e0a\u65b9\u7684\u89e3\u3002\u6b64\u65f6\u7684\u60c5\u51b5\u4e0e\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u76f8\u540c\uff0c\u53ef\u4f7f\u7528\u6b63\u5e8f\u904d\u5386\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust edit_distance.java<pre><code>/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(String s, String t) {\nint n = s.length(), m = t.length();\nint[] dp = new int[m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j &lt;= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i &lt;= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nint leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j &lt;= m; j++) {\nint temp = dp[j];\nif (s.charAt(i - 1) == t.charAt(j - 1)) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n</code></pre> edit_distance.cpp<pre><code>/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(string s, string t) {\nint n = s.length(), m = t.length();\nvector&lt;int&gt; dp(m + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j &lt;= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i &lt;= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nint leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j &lt;= m; j++) {\nint temp = dp[j];\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n</code></pre> edit_distance.py<pre><code>def edit_distance_dp_comp(s: str, t: str) -&gt; int:\n\"\"\"\u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn, m = len(s), len(t)\ndp = [0] * (m + 1)\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in range(1, m + 1):\ndp[j] = j\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in range(1, n + 1):\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nleftup = dp[0] # \u6682\u5b58 dp[i-1, j-1]\ndp[0] += 1\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in range(1, m + 1):\ntemp = dp[j]\nif s[i - 1] == t[j - 1]:\n# \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup\nelse:\n# \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = min(dp[j - 1], dp[j], leftup) + 1\nleftup = temp # \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\nreturn dp[m]\n</code></pre> edit_distance.go<pre><code>/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDPComp(s string, t string) int {\nn := len(s)\nm := len(t)\ndp := make([]int, m+1)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j := 1; j &lt;= m; j++ {\ndp[j] = j\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i := 1; i &lt;= n; i++ {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nleftUp := dp[0] // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j := 1; j &lt;= m; j++ {\ntemp := dp[j]\nif s[i-1] == t[j-1] {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftUp\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = MinInt(MinInt(dp[j-1], dp[j]), leftUp) + 1\n}\nleftUp = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m]\n}\n</code></pre> edit_distance.js<pre><code>[class]{}-[func]{editDistanceDPComp}\n</code></pre> edit_distance.ts<pre><code>[class]{}-[func]{editDistanceDPComp}\n</code></pre> edit_distance.c<pre><code>[class]{}-[func]{editDistanceDPComp}\n</code></pre> edit_distance.cs<pre><code>/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(string s, string t) {\nint n = s.Length, m = t.Length;\nint[] dp = new int[m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j &lt;= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i &lt;= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nint leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j &lt;= m; j++) {\nint temp = dp[j];\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = Math.Min(Math.Min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n</code></pre> edit_distance.swift<pre><code>/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc editDistanceDPComp(s: String, t: String) -&gt; Int {\nlet n = s.utf8CString.count\nlet m = t.utf8CString.count\nvar dp = Array(repeating: 0, count: m + 1)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in stride(from: 1, through: m, by: 1) {\ndp[j] = j\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in stride(from: 1, through: n, by: 1) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nvar leftup = dp[0] // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in stride(from: 1, through: m, by: 1) {\nlet temp = dp[j]\nif s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n}\nleftup = temp // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m]\n}\n</code></pre> edit_distance.zig<pre><code>// \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn editDistanceDPComp(comptime s: []const u8, comptime t: []const u8) i32 {\ncomptime var n = s.len;\ncomptime var m = t.len;\nvar dp = [_]i32{0} ** (m + 1);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (1..m + 1) |j| {\ndp[j] = @intCast(j);\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (1..n + 1) |i| {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nvar leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = @intCast(i);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (1..m + 1) |j| {\nvar temp = dp[j];\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = @min(@min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n</code></pre> edit_distance.dart<pre><code>/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint editDistanceDPComp(String s, String t) {\nint n = s.length, m = t.length;\nList&lt;int&gt; dp = List.filled(m + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor (int j = 1; j &lt;= m; j++) {\ndp[j] = j;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor (int i = 1; i &lt;= n; i++) {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nint leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor (int j = 1; j &lt;= m; j++) {\nint temp = dp[j];\nif (s[i - 1] == t[j - 1]) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\nreturn dp[m];\n}\n</code></pre> edit_distance.rs<pre><code>/* \u7f16\u8f91\u8ddd\u79bb\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn edit_distance_dp_comp(s: &amp;str, t: &amp;str) -&gt; i32 {\nlet (n, m) = (s.len(), t.len());\nlet mut dp = vec![0; m + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\nfor j in 1..m {\ndp[j] = j as i32;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\nfor i in 1..=n {\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u5217\nlet mut leftup = dp[0]; // \u6682\u5b58 dp[i-1, j-1]\ndp[0] = i as i32;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u5217\nfor j in 1..=m {\nlet temp = dp[j];\nif s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n// \u82e5\u4e24\u5b57\u7b26\u76f8\u7b49\uff0c\u5219\u76f4\u63a5\u8df3\u8fc7\u6b64\u4e24\u5b57\u7b26\ndp[j] = leftup;\n} else {\n// \u6700\u5c11\u7f16\u8f91\u6b65\u6570 = \u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u8fd9\u4e09\u79cd\u64cd\u4f5c\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570 + 1\ndp[j] = std::cmp::min(std::cmp::min(dp[j - 1], dp[j]), leftup) + 1;\n}\nleftup = temp; // \u66f4\u65b0\u4e3a\u4e0b\u4e00\u8f6e\u7684 dp[i-1, j-1]\n}\n}\ndp[m]\n}\n</code></pre>"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/","title":"14.1. \u00a0 \u521d\u63a2\u52a8\u6001\u89c4\u5212","text":"<p>\u300c\u52a8\u6001\u89c4\u5212 Dynamic Programming\u300d\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u7b97\u6cd5\u8303\u5f0f\uff0c\u5b83\u5c06\u4e00\u4e2a\u95ee\u9898\u5206\u89e3\u4e3a\u4e00\u7cfb\u5217\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u5e76\u901a\u8fc7\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\u6765\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u4ece\u800c\u5927\u5e45\u63d0\u5347\u65f6\u95f4\u6548\u7387\u3002</p> <p>\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u4ece\u4e00\u4e2a\u7ecf\u5178\u4f8b\u9898\u5165\u624b\uff0c\u5148\u7ed9\u51fa\u5b83\u7684\u66b4\u529b\u56de\u6eaf\u89e3\u6cd5\uff0c\u89c2\u5bdf\u5176\u4e2d\u5305\u542b\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u518d\u9010\u6b65\u5bfc\u51fa\u66f4\u9ad8\u6548\u7684\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002</p> <p>\u722c\u697c\u68af</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u5171\u6709 \\(n\\) \u9636\u7684\u697c\u68af\uff0c\u4f60\u6bcf\u6b65\u53ef\u4ee5\u4e0a \\(1\\) \u9636\u6216\u8005 \\(2\\) \u9636\uff0c\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5bf9\u4e8e\u4e00\u4e2a \\(3\\) \u9636\u697c\u68af\uff0c\u5171\u6709 \\(3\\) \u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002</p> <p></p> <p> \u56fe\uff1a\u722c\u5230\u7b2c 3 \u9636\u7684\u65b9\u6848\u6570\u91cf </p> <p>\u672c\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u65b9\u6848\u6570\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u901a\u8fc7\u56de\u6eaf\u6765\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u6027\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5c06\u722c\u697c\u68af\u60f3\u8c61\u4e3a\u4e00\u4e2a\u591a\u8f6e\u9009\u62e9\u7684\u8fc7\u7a0b\uff1a\u4ece\u5730\u9762\u51fa\u53d1\uff0c\u6bcf\u8f6e\u9009\u62e9\u4e0a \\(1\\) \u9636\u6216 \\(2\\) \u9636\uff0c\u6bcf\u5f53\u5230\u8fbe\u697c\u68af\u9876\u90e8\u65f6\u5c31\u5c06\u65b9\u6848\u6570\u91cf\u52a0 \\(1\\) \uff0c\u5f53\u8d8a\u8fc7\u697c\u68af\u9876\u90e8\u65f6\u5c31\u5c06\u5176\u526a\u679d\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust 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// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n)\nres.set(0, res.get(0) + 1);\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Integer choice : choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice &gt; n)\nbreak;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\nList&lt;Integer&gt; choices = Arrays.asList(1, 2); // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nint state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nList&lt;Integer&gt; res = new ArrayList&lt;&gt;();\nres.add(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn res.get(0);\n}\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// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n)\nres[0]++;\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (auto &amp;choice : choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice &gt; n)\nbreak;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\nvector&lt;int&gt; choices = {1, 2}; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nint state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nvector&lt;int&gt; res = {0}; // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn res[0];\n}\n</code></pre> climbing_stairs_backtrack.py<pre><code>def backtrack(choices: list[int], state: int, n: int, res: list[int]) -&gt; int:\n\"\"\"\u56de\u6eaf\"\"\"\n# \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif state == n:\nres[0] += 1\n# \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices:\n# \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif state + choice &gt; n:\nbreak\n# \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res)\n# \u56de\u9000\ndef climbing_stairs_backtrack(n: int) -&gt; int:\n\"\"\"\u722c\u697c\u68af\uff1a\u56de\u6eaf\"\"\"\nchoices = [1, 2] # \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nstate = 0 # \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nres = [0] # \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res)\nreturn res[0]\n</code></pre> climbing_stairs_backtrack.go<pre><code>/* \u56de\u6eaf */\nfunc backtrack(choices []int, state, n int, res []int) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif state == n {\nres[0] = res[0] + 1\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor _, choice := range choices {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif state+choice &gt; n {\nbreak\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state+choice, n, res)\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n int) int {\n// \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nchoices := []int{1, 2}\n// \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nstate := 0\nres := make([]int, 1)\n// \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nres[0] = 0\nbacktrack(choices, state, n, res)\nreturn res[0]\n}\n</code></pre> climbing_stairs_backtrack.js<pre><code>/* \u56de\u6eaf */\nfunction backtrack(choices, state, n, res) {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state === n) res.set(0, res.get(0) + 1);\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (const choice of choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice &gt; n) break;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n) {\nconst choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nconst state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nconst res = new Map();\nres.set(0, 0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn res.get(0);\n}\n</code></pre> climbing_stairs_backtrack.ts<pre><code>/* \u56de\u6eaf */\nfunction backtrack(\nchoices: number[],\nstate: number,\nn: number,\nres: Map&lt;0, any&gt;\n): void {\n// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state === n) res.set(0, res.get(0) + 1);\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (const choice of choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice &gt; n) break;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunction climbingStairsBacktrack(n: number): number {\nconst choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nconst state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nconst res = new Map();\nres.set(0, 0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn res.get(0);\n}\n</code></pre> climbing_stairs_backtrack.c<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{climbingStairsBacktrack}\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// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n)\nres[0]++;\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nforeach (int choice in choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice &gt; n)\nbreak;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\nList&lt;int&gt; choices = new List&lt;int&gt; { 1, 2 }; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nint state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nList&lt;int&gt; res = new List&lt;int&gt; { 0 }; // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn 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// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif state == n {\nres[0] += 1\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif state + choice &gt; n {\nbreak\n}\nbacktrack(choices: choices, state: state + choice, n: n, res: &amp;res)\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfunc climbingStairsBacktrack(n: Int) -&gt; Int {\nlet choices = [1, 2] // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nlet state = 0 // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nvar res: [Int] = []\nres.append(0) // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices: choices, state: state, n: n, res: &amp;res)\nreturn res[0]\n}\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// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n) {\nres.items[0] = res.items[0] + 1;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (choices) |choice| {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice &gt; n) {\nbreak;\n}\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n// \u722c\u697c\u68af\uff1a\u56de\u6eaf\nfn climbingStairsBacktrack(n: usize) !i32 {\nvar choices = [_]i32{ 1, 2 }; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nvar state: i32 = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nvar res = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer res.deinit();\ntry res.append(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(&amp;choices, state, @intCast(n), res);\nreturn res.items[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// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif (state == n) {\nres[0]++;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (int choice in choices) {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif (state + choice &gt; n) break;\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nint climbingStairsBacktrack(int n) {\nList&lt;int&gt; choices = [1, 2]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nint state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nList&lt;int&gt; res = [];\nres.add(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(choices, state, n, res);\nreturn 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// \u5f53\u722c\u5230\u7b2c n \u9636\u65f6\uff0c\u65b9\u6848\u6570\u91cf\u52a0 1\nif state == n { res[0] = res[0] + 1; }\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor &amp;choice in choices {\n// \u526a\u679d\uff1a\u4e0d\u5141\u8bb8\u8d8a\u8fc7\u7b2c n \u9636\nif state + choice &gt; n { break; }\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nbacktrack(choices, state + choice, n, res);\n// \u56de\u9000\n}\n}\n/* \u722c\u697c\u68af\uff1a\u56de\u6eaf */\nfn climbing_stairs_backtrack(n: usize) -&gt; i32 {\nlet choices = vec![ 1, 2 ]; // \u53ef\u9009\u62e9\u5411\u4e0a\u722c 1 \u6216 2 \u9636\nlet state = 0; // \u4ece\u7b2c 0 \u9636\u5f00\u59cb\u722c\nlet mut res = Vec::new();\nres.push(0); // \u4f7f\u7528 res[0] \u8bb0\u5f55\u65b9\u6848\u6570\u91cf\nbacktrack(&amp;choices, state, n as i32, &amp;mut res);\nres[0]\n}\n</code></pre>"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1411","title":"14.1.1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"<p>\u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u5e76\u4e0d\u663e\u5f0f\u5730\u5bf9\u95ee\u9898\u8fdb\u884c\u62c6\u89e3\uff0c\u800c\u662f\u5c06\u95ee\u9898\u770b\u4f5c\u4e00\u7cfb\u5217\u51b3\u7b56\u6b65\u9aa4\uff0c\u901a\u8fc7\u8bd5\u63a2\u548c\u526a\u679d\uff0c\u641c\u7d22\u6240\u6709\u53ef\u80fd\u7684\u89e3\u3002</p> <p>\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u4ece\u95ee\u9898\u5206\u89e3\u7684\u89d2\u5ea6\u5206\u6790\u8fd9\u9053\u9898\u3002\u8bbe\u722c\u5230\u7b2c \\(i\\) \u9636\u5171\u6709 \\(dp[i]\\) \u79cd\u65b9\u6848\uff0c\u90a3\u4e48 \\(dp[i]\\) \u5c31\u662f\u539f\u95ee\u9898\uff0c\u5176\u5b50\u95ee\u9898\u5305\u62ec:</p> \\[ dp[i-1] , dp[i-2] , \\cdots , dp[2] , dp[1] \\] <p>\u7531\u4e8e\u6bcf\u8f6e\u53ea\u80fd\u4e0a \\(1\\) \u9636\u6216 \\(2\\) \u9636\uff0c\u56e0\u6b64\u5f53\u6211\u4eec\u7ad9\u5728\u7b2c \\(i\\) \u9636\u697c\u68af\u4e0a\u65f6\uff0c\u4e0a\u4e00\u8f6e\u53ea\u53ef\u80fd\u7ad9\u5728\u7b2c \\(i - 1\\) \u9636\u6216\u7b2c \\(i - 2\\) \u9636\u4e0a\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ea\u80fd\u4ece\u7b2c \\(i -1\\) \u9636\u6216\u7b2c \\(i - 2\\) \u9636\u524d\u5f80\u7b2c \\(i\\) \u9636\u3002</p> <p>\u7531\u6b64\u4fbf\u53ef\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u63a8\u8bba\uff1a\u722c\u5230\u7b2c \\(i - 1\\) \u9636\u7684\u65b9\u6848\u6570\u52a0\u4e0a\u722c\u5230\u7b2c \\(i - 2\\) \u9636\u7684\u65b9\u6848\u6570\u5c31\u7b49\u4e8e\u722c\u5230\u7b2c \\(i\\) \u9636\u7684\u65b9\u6848\u6570\u3002\u516c\u5f0f\u5982\u4e0b\uff1a</p> \\[ dp[i] = dp[i-1] + dp[i-2] \\] <p>\u8fd9\u610f\u5473\u7740\u5728\u722c\u697c\u68af\u95ee\u9898\u4e2d\uff0c\u5404\u4e2a\u5b50\u95ee\u9898\u4e4b\u95f4\u5b58\u5728\u9012\u63a8\u5173\u7cfb\uff0c\u539f\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u7531\u5b50\u95ee\u9898\u7684\u89e3\u6784\u5efa\u5f97\u6765\u3002</p> <p></p> <p> \u56fe\uff1a\u65b9\u6848\u6570\u91cf\u9012\u63a8\u5173\u7cfb </p> <p>\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9012\u63a8\u516c\u5f0f\u5f97\u5230\u66b4\u529b\u641c\u7d22\u89e3\u6cd5\uff1a</p> <ul> <li>\u4ee5 \\(dp[n]\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u9012\u5f52\u5730\u5c06\u4e00\u4e2a\u8f83\u5927\u95ee\u9898\u62c6\u89e3\u4e3a\u4e24\u4e2a\u8f83\u5c0f\u95ee\u9898\u7684\u548c\uff0c\u76f4\u81f3\u5230\u8fbe\u6700\u5c0f\u5b50\u95ee\u9898 \\(dp[1]\\) \u548c \\(dp[2]\\) \u65f6\u8fd4\u56de\u3002</li> <li>\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3 \\(dp[1] = 1\\) , \\(dp[2] = 2\\) \u662f\u5df2\u77e5\u7684\uff0c\u4ee3\u8868\u722c\u5230\u7b2c \\(1\\) , \\(2\\) \u9636\u5206\u522b\u6709 \\(1\\) , \\(2\\) \u79cd\u65b9\u6848\u3002</li> </ul> <p>\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5b83\u548c\u6807\u51c6\u56de\u6eaf\u4ee3\u7801\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u4f46\u66f4\u52a0\u7b80\u6d01\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust climbing_stairs_dfs.java<pre><code>/* \u641c\u7d22 */\nint dfs(int i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\nreturn dfs(n);\n}\n</code></pre> climbing_stairs_dfs.cpp<pre><code>/* \u641c\u7d22 */\nint dfs(int i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\nreturn dfs(n);\n}\n</code></pre> climbing_stairs_dfs.py<pre><code>def dfs(i: int) -&gt; int:\n\"\"\"\u641c\u7d22\"\"\"\n# \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 or i == 2:\nreturn i\n# dp[i] = dp[i-1] + dp[i-2]\ncount = dfs(i - 1) + dfs(i - 2)\nreturn count\ndef climbing_stairs_dfs(n: int) -&gt; int:\n\"\"\"\u722c\u697c\u68af\uff1a\u641c\u7d22\"\"\"\nreturn dfs(n)\n</code></pre> climbing_stairs_dfs.go<pre><code>/* \u641c\u7d22 */\nfunc dfs(i int) int {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 {\nreturn i\n}\n// dp[i] = dp[i-1] + dp[i-2]\ncount := dfs(i-1) + dfs(i-2)\nreturn count\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunc climbingStairsDFS(n int) int {\nreturn dfs(n)\n}\n</code></pre> climbing_stairs_dfs.js<pre><code>/* \u641c\u7d22 */\nfunction dfs(i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i === 1 || i === 2) return i;\n// dp[i] = dp[i-1] + dp[i-2]\nconst count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunction climbingStairsDFS(n) {\nreturn dfs(n);\n}\n</code></pre> climbing_stairs_dfs.ts<pre><code>/* \u641c\u7d22 */\nfunction dfs(i: number): number {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i === 1 || i === 2) return i;\n// dp[i] = dp[i-1] + dp[i-2]\nconst count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunction climbingStairsDFS(n: number): number {\nreturn dfs(n);\n}\n</code></pre> climbing_stairs_dfs.c<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFS}\n</code></pre> climbing_stairs_dfs.cs<pre><code>/* \u641c\u7d22 */\nint dfs(int i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\nreturn dfs(n);\n}\n</code></pre> climbing_stairs_dfs.swift<pre><code>/* \u641c\u7d22 */\nfunc dfs(i: Int) -&gt; Int {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 {\nreturn i\n}\n// dp[i] = dp[i-1] + dp[i-2]\nlet count = dfs(i: i - 1) + dfs(i: i - 2)\nreturn count\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfunc climbingStairsDFS(n: Int) -&gt; Int {\ndfs(i: n)\n}\n</code></pre> climbing_stairs_dfs.zig<pre><code>// \u641c\u7d22\nfn dfs(i: usize) i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 or i == 2) {\nreturn @intCast(i);\n}\n// dp[i] = dp[i-1] + dp[i-2]\nvar count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n// \u722c\u697c\u68af\uff1a\u641c\u7d22\nfn climbingStairsDFS(comptime n: usize) i32 {\nreturn dfs(n);\n}\n</code></pre> climbing_stairs_dfs.dart<pre><code>/* \u641c\u7d22 */\nint dfs(int i) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2) return i;\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1) + dfs(i - 2);\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nint climbingStairsDFS(int n) {\nreturn dfs(n);\n}\n</code></pre> climbing_stairs_dfs.rs<pre><code>/* \u641c\u7d22 */\nfn dfs(i: usize) -&gt; i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 { return i as i32; }\n// dp[i] = dp[i-1] + dp[i-2]\nlet count = dfs(i - 1) + dfs(i - 2);\ncount\n}\n/* \u722c\u697c\u68af\uff1a\u641c\u7d22 */\nfn climbing_stairs_dfs(n: usize) -&gt; i32 {\ndfs(n) }\n</code></pre> <p>\u4e0b\u56fe\u5c55\u793a\u4e86\u66b4\u529b\u641c\u7d22\u5f62\u6210\u7684\u9012\u5f52\u6811\u3002\u5bf9\u4e8e\u95ee\u9898 \\(dp[n]\\) \uff0c\u5176\u9012\u5f52\u6811\u7684\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002\u6307\u6570\u9636\u5c5e\u4e8e\u7206\u70b8\u5f0f\u589e\u957f\uff0c\u5982\u679c\u6211\u4eec\u8f93\u5165\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684 \\(n\\) \uff0c\u5219\u4f1a\u9677\u5165\u6f2b\u957f\u7684\u7b49\u5f85\u4e4b\u4e2d\u3002</p> <p></p> <p> \u56fe\uff1a\u722c\u697c\u68af\u5bf9\u5e94\u9012\u5f52\u6811 </p> <p>\u89c2\u5bdf\u4e0a\u56fe\u53d1\u73b0\uff0c\u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u7531\u4e8e\u300c\u91cd\u53e0\u5b50\u95ee\u9898\u300d\u5bfc\u81f4\u7684\u3002\u4f8b\u5982\uff1a\\(dp[9]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[8]\\) \u548c \\(dp[7]\\) \uff0c\\(dp[8]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[7]\\) \u548c \\(dp[6]\\) \uff0c\u4e24\u8005\u90fd\u5305\u542b\u5b50\u95ee\u9898 \\(dp[7]\\) \u3002</p> <p>\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5b50\u95ee\u9898\u4e2d\u5305\u542b\u66f4\u5c0f\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5b50\u5b50\u5b59\u5b59\u65e0\u7a77\u5c3d\u4e5f\u3002\u7edd\u5927\u90e8\u5206\u8ba1\u7b97\u8d44\u6e90\u90fd\u6d6a\u8d39\u5728\u8fd9\u4e9b\u91cd\u53e0\u7684\u95ee\u9898\u4e0a\u3002</p>"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1412","title":"14.1.2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"<p>\u4e3a\u4e86\u63d0\u5347\u7b97\u6cd5\u6548\u7387\uff0c\u6211\u4eec\u5e0c\u671b\u6240\u6709\u7684\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u6570\u7ec4 <code>mem</code> \u6765\u8bb0\u5f55\u6bcf\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5e76\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u8fd9\u6837\u505a\uff1a</p> <ol> <li>\u5f53\u9996\u6b21\u8ba1\u7b97 \\(dp[i]\\) \u65f6\uff0c\u6211\u4eec\u5c06\u5176\u8bb0\u5f55\u81f3 <code>mem[i]</code> \uff0c\u4ee5\u4fbf\u4e4b\u540e\u4f7f\u7528\u3002</li> <li>\u5f53\u518d\u6b21\u9700\u8981\u8ba1\u7b97 \\(dp[i]\\) \u65f6\uff0c\u6211\u4eec\u4fbf\u53ef\u76f4\u63a5\u4ece <code>mem[i]</code> \u4e2d\u83b7\u53d6\u7ed3\u679c\uff0c\u4ece\u800c\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u526a\u679d\u3002</li> </ol> JavaC++PythonGoJSTSCC#SwiftZigDartRust climbing_stairs_dfs_mem.java<pre><code>/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, int[] mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1)\nreturn mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nint[] mem = new int[n + 1];\nArrays.fill(mem, -1);\nreturn dfs(n, mem);\n}\n</code></pre> climbing_stairs_dfs_mem.cpp<pre><code>/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, vector&lt;int&gt; &amp;mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1)\nreturn mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nvector&lt;int&gt; mem(n + 1, -1);\nreturn dfs(n, mem);\n}\n</code></pre> climbing_stairs_dfs_mem.py<pre><code>def dfs(i: int, mem: list[int]) -&gt; int:\n\"\"\"\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n# \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 or i == 2:\nreturn i\n# \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif mem[i] != -1:\nreturn mem[i]\n# dp[i] = dp[i-1] + dp[i-2]\ncount = dfs(i - 1, mem) + dfs(i - 2, mem)\n# \u8bb0\u5f55 dp[i]\nmem[i] = count\nreturn count\ndef climbing_stairs_dfs_mem(n: int) -&gt; int:\n\"\"\"\u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n# mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nmem = [-1] * (n + 1)\nreturn dfs(n, mem)\n</code></pre> climbing_stairs_dfs_mem.go<pre><code>/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc dfsMem(i int, mem []int) int {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 {\nreturn i\n}\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif mem[i] != -1 {\nreturn mem[i]\n}\n// dp[i] = dp[i-1] + dp[i-2]\ncount := dfsMem(i-1, mem) + dfsMem(i-2, mem)\n// \u8bb0\u5f55 dp[i]\nmem[i] = count\nreturn count\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc climbingStairsDFSMem(n int) int {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nmem := make([]int, n+1)\nfor i := range mem {\nmem[i] = -1\n}\nreturn dfsMem(n, mem)\n}\n</code></pre> climbing_stairs_dfs_mem.js<pre><code>/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction dfs(i, mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i === 1 || i === 2) return i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1) return mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nconst count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction climbingStairsDFSMem(n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nconst mem = new Array(n + 1).fill(-1);\nreturn dfs(n, mem);\n}\n</code></pre> climbing_stairs_dfs_mem.ts<pre><code>/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction dfs(i: number, mem: number[]): number {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i === 1 || i === 2) return i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1) return mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nconst count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunction climbingStairsDFSMem(n: number): number {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nconst mem = new Array(n + 1).fill(-1);\nreturn dfs(n, mem);\n}\n</code></pre> climbing_stairs_dfs_mem.c<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFSMem}\n</code></pre> climbing_stairs_dfs_mem.cs<pre><code>/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, int[] mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2)\nreturn i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1)\nreturn mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nint[] mem = new int[n + 1];\nArray.Fill(mem, -1);\nreturn dfs(n, mem);\n}\n</code></pre> climbing_stairs_dfs_mem.swift<pre><code>/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc dfs(i: Int, mem: inout [Int]) -&gt; Int {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 {\nreturn i\n}\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif mem[i] != -1 {\nreturn mem[i]\n}\n// dp[i] = dp[i-1] + dp[i-2]\nlet count = dfs(i: i - 1, mem: &amp;mem) + dfs(i: i - 2, mem: &amp;mem)\n// \u8bb0\u5f55 dp[i]\nmem[i] = count\nreturn count\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc climbingStairsDFSMem(n: Int) -&gt; Int {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nvar mem = Array(repeating: -1, count: n + 1)\nreturn dfs(i: n, mem: &amp;mem)\n}\n</code></pre> climbing_stairs_dfs_mem.zig<pre><code>// \u8bb0\u5fc6\u5316\u641c\u7d22\nfn dfs(i: usize, mem: []i32) i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 or i == 2) {\nreturn @intCast(i);\n}\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1) {\nreturn mem[i];\n}\n// dp[i] = dp[i-1] + dp[i-2]\nvar count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n// \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn climbingStairsDFSMem(comptime n: usize) i32 {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nvar mem = [_]i32{ -1 } ** (n + 1);\nreturn dfs(n, &amp;mem);\n}\n</code></pre> climbing_stairs_dfs_mem.dart<pre><code>/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nint dfs(int i, List&lt;int&gt; mem) {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (i == 1 || i == 2) return i;\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif (mem[i] != -1) return mem[i];\n// dp[i] = dp[i-1] + dp[i-2]\nint count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\nreturn count;\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint climbingStairsDFSMem(int n) {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nList&lt;int&gt; mem = List.filled(n + 1, -1);\nreturn dfs(n, mem);\n}\n</code></pre> climbing_stairs_dfs_mem.rs<pre><code>/* \u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn dfs(i: usize, mem: &amp;mut [i32]) -&gt; i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif i == 1 || i == 2 { return i as i32; }\n// \u82e5\u5b58\u5728\u8bb0\u5f55 dp[i] \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u4e4b\nif mem[i] != -1 { return mem[i]; }\n// dp[i] = dp[i-1] + dp[i-2]\nlet count = dfs(i - 1, mem) + dfs(i - 2, mem);\n// \u8bb0\u5f55 dp[i]\nmem[i] = count;\ncount\n}\n/* \u722c\u697c\u68af\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfn climbing_stairs_dfs_mem(n: usize) -&gt; i32 {\n// mem[i] \u8bb0\u5f55\u722c\u5230\u7b2c i \u9636\u7684\u65b9\u6848\u603b\u6570\uff0c-1 \u4ee3\u8868\u65e0\u8bb0\u5f55\nlet mut mem = vec![-1; n + 1];\ndfs(n, &amp;mut mem)\n}\n</code></pre> <p>\u89c2\u5bdf\u4e0b\u56fe\uff0c\u7ecf\u8fc7\u8bb0\u5fc6\u5316\u5904\u7406\u540e\uff0c\u6240\u6709\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u9700\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u88ab\u4f18\u5316\u81f3 \\(O(n)\\) \uff0c\u8fd9\u662f\u4e00\u4e2a\u5de8\u5927\u7684\u98de\u8dc3\u3002</p> <p></p> <p> \u56fe\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\u5bf9\u5e94\u9012\u5f52\u6811 </p>"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1413","title":"14.1.3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"<p>\u8bb0\u5fc6\u5316\u641c\u7d22\u662f\u4e00\u79cd\u201c\u4ece\u9876\u81f3\u5e95\u201d\u7684\u65b9\u6cd5\uff1a\u6211\u4eec\u4ece\u539f\u95ee\u9898\uff08\u6839\u8282\u70b9\uff09\u5f00\u59cb\uff0c\u9012\u5f52\u5730\u5c06\u8f83\u5927\u5b50\u95ee\u9898\u5206\u89e3\u4e3a\u8f83\u5c0f\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u89e3\u5df2\u77e5\u7684\u6700\u5c0f\u5b50\u95ee\u9898\uff08\u53f6\u8282\u70b9\uff09\u3002\u4e4b\u540e\uff0c\u901a\u8fc7\u56de\u6eaf\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u9010\u5c42\u6536\u96c6\uff0c\u6784\u5efa\u51fa\u539f\u95ee\u9898\u7684\u89e3\u3002</p> <p>\u4e0e\u4e4b\u76f8\u53cd\uff0c\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u201c\u4ece\u5e95\u81f3\u9876\u201d\u7684\u65b9\u6cd5\uff1a\u4ece\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u5f00\u59cb\uff0c\u8fed\u4ee3\u5730\u6784\u5efa\u66f4\u5927\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u76f4\u81f3\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002</p> <p>\u7531\u4e8e\u52a8\u6001\u89c4\u5212\u4e0d\u5305\u542b\u56de\u6eaf\u8fc7\u7a0b\uff0c\u56e0\u6b64\u53ea\u9700\u4f7f\u7528\u5faa\u73af\u8fed\u4ee3\u5b9e\u73b0\uff0c\u65e0\u9700\u4f7f\u7528\u9012\u5f52\u3002\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u7ec4 <code>dp</code> \u6765\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5b83\u8d77\u5230\u4e86\u8bb0\u5fc6\u5316\u641c\u7d22\u4e2d\u6570\u7ec4 <code>mem</code> \u76f8\u540c\u7684\u8bb0\u5f55\u4f5c\u7528\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust climbing_stairs_dp.java<pre><code>/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\nif (n == 1 || n == 2)\nreturn n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[] dp = new int[n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i &lt;= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n</code></pre> climbing_stairs_dp.cpp<pre><code>/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\nif (n == 1 || n == 2)\nreturn n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvector&lt;int&gt; dp(n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i &lt;= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n</code></pre> climbing_stairs_dp.py<pre><code>def climbing_stairs_dp(n: int) -&gt; int:\n\"\"\"\u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nif n == 1 or n == 2:\nreturn n\n# \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp = [0] * (n + 1)\n# \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1], dp[2] = 1, 2\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in range(3, n + 1):\ndp[i] = dp[i - 1] + dp[i - 2]\nreturn dp[n]\n</code></pre> climbing_stairs_dp.go<pre><code>/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDP(n int) int {\nif n == 1 || n == 2 {\nreturn n\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\ndp := make([]int, n+1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1\ndp[2] = 2\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i &lt;= n; i++ {\ndp[i] = dp[i-1] + dp[i-2]\n}\nreturn dp[n]\n}\n</code></pre> climbing_stairs_dp.js<pre><code>/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDP(n) {\nif (n === 1 || n === 2) return n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nconst dp = new Array(n + 1).fill(-1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (let i = 3; i &lt;= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n</code></pre> climbing_stairs_dp.ts<pre><code>/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDP(n: number): number {\nif (n === 1 || n === 2) return n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nconst dp = new Array(n + 1).fill(-1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (let i = 3; i &lt;= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n</code></pre> climbing_stairs_dp.c<pre><code>[class]{}-[func]{climbingStairsDP}\n</code></pre> climbing_stairs_dp.cs<pre><code>/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\nif (n == 1 || n == 2)\nreturn n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nint[] dp = new int[n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i &lt;= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n</code></pre> climbing_stairs_dp.swift<pre><code>/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDP(n: Int) -&gt; Int {\nif n == 1 || n == 2 {\nreturn n\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = Array(repeating: 0, count: n + 1)\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1\ndp[2] = 2\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in stride(from: 3, through: n, by: 1) {\ndp[i] = dp[i - 1] + dp[i - 2]\n}\nreturn dp[n]\n}\n</code></pre> climbing_stairs_dp.zig<pre><code>// \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212\nfn climbingStairsDP(comptime n: usize) i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif (n == 1 or n == 2) {\nreturn @intCast(n);\n}\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nvar dp = [_]i32{-1} ** (n + 1);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (3..n + 1) |i| {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n</code></pre> climbing_stairs_dp.dart<pre><code>/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDP(int n) {\nif (n == 1 || n == 2) return n;\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nList&lt;int&gt; dp = List.filled(n + 1, 0);\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor (int i = 3; i &lt;= n; i++) {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\nreturn dp[n];\n}\n</code></pre> climbing_stairs_dp.rs<pre><code>/* \u722c\u697c\u68af\uff1a\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_dp(n: usize) -&gt; i32 {\n// \u5df2\u77e5 dp[1] \u548c dp[2] \uff0c\u8fd4\u56de\u4e4b\nif n == 1 || n == 2 { return n as i32; }\n// \u521d\u59cb\u5316 dp \u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\nlet mut dp = vec![-1; n + 1];\n// \u521d\u59cb\u72b6\u6001\uff1a\u9884\u8bbe\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\ndp[1] = 1;\ndp[2] = 2;\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i in 3..=n {\ndp[i] = dp[i - 1] + dp[i - 2];\n}\ndp[n]\n}\n</code></pre> <p>\u4e0e\u56de\u6eaf\u7b97\u6cd5\u4e00\u6837\uff0c\u52a8\u6001\u89c4\u5212\u4e5f\u4f7f\u7528\u201c\u72b6\u6001\u201d\u6982\u5ff5\u6765\u8868\u793a\u95ee\u9898\u6c42\u89e3\u7684\u67d0\u4e2a\u7279\u5b9a\u9636\u6bb5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u5bf9\u5e94\u4e00\u4e2a\u5b50\u95ee\u9898\u4ee5\u53ca\u76f8\u5e94\u7684\u5c40\u90e8\u6700\u4f18\u89e3\u3002\u4f8b\u5982\uff0c\u722c\u697c\u68af\u95ee\u9898\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u5f53\u524d\u6240\u5728\u697c\u68af\u9636\u6570 \\(i\\) \u3002</p> <p>\u603b\u7ed3\u4ee5\u4e0a\uff0c\u52a8\u6001\u89c4\u5212\u7684\u5e38\u7528\u672f\u8bed\u5305\u62ec\uff1a</p> <ul> <li>\u5c06\u6570\u7ec4 <code>dp</code> \u79f0\u4e3a\u300c\\(dp\\) \u8868\u300d\uff0c\\(dp[i]\\) \u8868\u793a\u72b6\u6001 \\(i\\) \u5bf9\u5e94\u5b50\u95ee\u9898\u7684\u89e3\u3002</li> <li>\u5c06\u6700\u5c0f\u5b50\u95ee\u9898\u5bf9\u5e94\u7684\u72b6\u6001\uff08\u5373\u7b2c \\(1\\) , \\(2\\) \u9636\u697c\u68af\uff09\u79f0\u4e3a\u300c\u521d\u59cb\u72b6\u6001\u300d\u3002</li> <li>\u5c06\u9012\u63a8\u516c\u5f0f \\(dp[i] = dp[i-1] + dp[i-2]\\) \u79f0\u4e3a\u300c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u300d\u3002</li> </ul> <p></p> <p> \u56fe\uff1a\u722c\u697c\u68af\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b </p>"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1414","title":"14.1.4. \u00a0 \u72b6\u6001\u538b\u7f29","text":"<p>\u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u53d1\u73b0\uff0c\u7531\u4e8e \\(dp[i]\\) \u53ea\u4e0e \\(dp[i-1]\\) \u548c \\(dp[i-2]\\) \u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u9700\u4f7f\u7528\u4e00\u4e2a\u6570\u7ec4 <code>dp</code> \u6765\u5b58\u50a8\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u800c\u53ea\u9700\u4e24\u4e2a\u53d8\u91cf\u6eda\u52a8\u524d\u8fdb\u5373\u53ef\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust climbing_stairs_dp.java<pre><code>/* \u722c\u697c\u68af\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\nif (n == 1 || n == 2)\nreturn n;\nint a = 1, b = 2;\nfor (int i = 3; i &lt;= n; i++) {\nint tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n</code></pre> climbing_stairs_dp.cpp<pre><code>/* \u722c\u697c\u68af\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\nif (n == 1 || n == 2)\nreturn n;\nint a = 1, b = 2;\nfor (int i = 3; i &lt;= n; i++) {\nint tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n</code></pre> climbing_stairs_dp.py<pre><code>def climbing_stairs_dp_comp(n: int) -&gt; int:\n\"\"\"\u722c\u697c\u68af\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nif n == 1 or n == 2:\nreturn n\na, b = 1, 2\nfor _ in range(3, n + 1):\na, b = b, a + b\nreturn b\n</code></pre> climbing_stairs_dp.go<pre><code>/* \u722c\u697c\u68af\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDPComp(n int) int {\nif n == 1 || n == 2 {\nreturn n\n}\na, b := 1, 2\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u4ece\u8f83\u5c0f\u5b50\u95ee\u9898\u9010\u6b65\u6c42\u89e3\u8f83\u5927\u5b50\u95ee\u9898\nfor i := 3; i &lt;= n; i++ {\na, b = b, a+b\n}\nreturn b\n}\n</code></pre> climbing_stairs_dp.js<pre><code>/* \u722c\u697c\u68af\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDPComp(n) {\nif (n === 1 || n === 2) return n;\nlet a = 1,\nb = 2;\nfor (let i = 3; i &lt;= n; i++) {\nconst tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n</code></pre> climbing_stairs_dp.ts<pre><code>/* \u722c\u697c\u68af\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunction climbingStairsDPComp(n: number): number {\nif (n === 1 || n === 2) return n;\nlet a = 1,\nb = 2;\nfor (let i = 3; i &lt;= n; i++) {\nconst tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n</code></pre> climbing_stairs_dp.c<pre><code>[class]{}-[func]{climbingStairsDPComp}\n</code></pre> climbing_stairs_dp.cs<pre><code>/* \u722c\u697c\u68af\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\nif (n == 1 || n == 2)\nreturn n;\nint a = 1, b = 2;\nfor (int i = 3; i &lt;= n; i++) {\nint tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n</code></pre> climbing_stairs_dp.swift<pre><code>/* \u722c\u697c\u68af\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc climbingStairsDPComp(n: Int) -&gt; Int {\nif n == 1 || n == 2 {\nreturn n\n}\nvar a = 1\nvar b = 2\nfor _ in stride(from: 3, through: n, by: 1) {\n(a, b) = (b, a + b)\n}\nreturn b\n}\n</code></pre> climbing_stairs_dp.zig<pre><code>// \u722c\u697c\u68af\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn climbingStairsDPComp(comptime n: usize) i32 {\nif (n == 1 or n == 2) {\nreturn @intCast(n);\n}\nvar a: i32 = 1;\nvar b: i32 = 2;\nfor (3..n + 1) |_| {\nvar tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n</code></pre> climbing_stairs_dp.dart<pre><code>/* \u722c\u697c\u68af\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint climbingStairsDPComp(int n) {\nif (n == 1 || n == 2) return n;\nint a = 1, b = 2;\nfor (int i = 3; i &lt;= n; i++) {\nint tmp = b;\nb = a + b;\na = tmp;\n}\nreturn b;\n}\n</code></pre> climbing_stairs_dp.rs<pre><code>/* \u722c\u697c\u68af\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn climbing_stairs_dp_comp(n: usize) -&gt; i32 {\nif n == 1 || n == 2 { return n as i32; }\nlet (mut a, mut b) = (1, 2);\nfor _ in 3..=n {\nlet tmp = b;\nb = a + b;\na = tmp;\n}\nb\n}\n</code></pre> <p>\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u7531\u4e8e\u7701\u53bb\u4e86\u6570\u7ec4 <code>dp</code> \u5360\u7528\u7684\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u4f4e\u81f3 \\(O(1)\\) \u3002</p> <p>\u8fd9\u79cd\u7a7a\u95f4\u4f18\u5316\u6280\u5de7\u88ab\u79f0\u4e3a\u300c\u72b6\u6001\u538b\u7f29\u300d\u3002\u5728\u5e38\u89c1\u7684\u52a8\u6001\u89c4\u5212\u95ee\u9898\u4e2d\uff0c\u5f53\u524d\u72b6\u6001\u4ec5\u4e0e\u524d\u9762\u6709\u9650\u4e2a\u72b6\u6001\u6709\u5173\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u5e94\u7528\u72b6\u6001\u538b\u7f29\uff0c\u53ea\u4fdd\u7559\u5fc5\u8981\u7684\u72b6\u6001\uff0c\u901a\u8fc7\u201c\u964d\u7ef4\u201d\u6765\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u3002</p>"},{"location":"chapter_dynamic_programming/knapsack_problem/","title":"14.4. \u00a0 0-1 \u80cc\u5305\u95ee\u9898","text":"<p>\u80cc\u5305\u95ee\u9898\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u52a8\u6001\u89c4\u5212\u5165\u95e8\u9898\u76ee\uff0c\u662f\u52a8\u6001\u89c4\u5212\u4e2d\u6700\u5e38\u89c1\u7684\u95ee\u9898\u5f62\u5f0f\u3002\u5176\u5177\u6709\u5f88\u591a\u53d8\u79cd\uff0c\u4f8b\u5982 0-1 \u80cc\u5305\u95ee\u9898\u3001\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u3001\u591a\u91cd\u80cc\u5305\u95ee\u9898\u7b49\u3002</p> <p>\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5148\u6765\u6c42\u89e3\u6700\u5e38\u89c1\u7684 0-1 \u80cc\u5305\u95ee\u9898\u3002</p> <p>Question</p> <p>\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\) \u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ea\u80fd\u9009\u62e9\u4e00\u6b21\uff0c\u95ee\u5728\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002</p> <p>\u8bf7\u6ce8\u610f\uff0c\u7269\u54c1\u7f16\u53f7 \\(i\\) \u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u6570\u7ec4\u7d22\u5f15\u4ece \\(0\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u56e0\u6b64\u7269\u54c1 \\(i\\) \u5bf9\u5e94\u91cd\u91cf \\(wgt[i-1]\\) \u548c\u4ef7\u503c \\(val[i-1]\\) \u3002</p> <p></p> <p> \u56fe\uff1a0-1 \u80cc\u5305\u7684\u793a\u4f8b\u6570\u636e </p> <p>\u6211\u4eec\u53ef\u4ee5\u5c06 0-1 \u80cc\u5305\u95ee\u9898\u770b\u4f5c\u662f\u4e00\u4e2a\u7531 \\(n\\) \u8f6e\u51b3\u7b56\u7ec4\u6210\u7684\u8fc7\u7a0b\uff0c\u6bcf\u4e2a\u7269\u4f53\u90fd\u6709\u4e0d\u653e\u5165\u548c\u653e\u5165\u4e24\u79cd\u51b3\u7b56\uff0c\u56e0\u6b64\u8be5\u95ee\u9898\u662f\u6ee1\u8db3\u51b3\u7b56\u6811\u6a21\u578b\u7684\u3002</p> <p>\u8be5\u95ee\u9898\u7684\u76ee\u6807\u662f\u6c42\u89e3\u201c\u5728\u9650\u5b9a\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u6700\u5927\u4ef7\u503c\u201d\uff0c\u56e0\u6b64\u8f83\u5927\u6982\u7387\u662f\u4e2a\u52a8\u6001\u89c4\u5212\u95ee\u9898\u3002</p> <p>\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868</p> <p>\u5bf9\u4e8e\u6bcf\u4e2a\u7269\u54c1\u6765\u8bf4\uff0c\u4e0d\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u4e0d\u53d8\uff1b\u653e\u5165\u80cc\u5305\uff0c\u80cc\u5305\u5bb9\u91cf\u51cf\u5c0f\u3002\u7531\u6b64\u53ef\u5f97\u72b6\u6001\u5b9a\u4e49\uff1a\u5f53\u524d\u7269\u54c1\u7f16\u53f7 \\(i\\) \u548c\u5269\u4f59\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u8bb0\u4e3a \\([i, c]\\) \u3002</p> <p>\u72b6\u6001 \\([i, c]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u524d \\(i\\) \u4e2a\u7269\u54c1\u5728\u5269\u4f59\u5bb9\u91cf\u4e3a \\(c\\) \u7684\u80cc\u5305\u4e2d\u7684\u6700\u5927\u4ef7\u503c\uff0c\u8bb0\u4e3a \\(dp[i, c]\\) \u3002</p> <p>\u5f85\u6c42\u89e3\u7684\u662f \\(dp[n, cap]\\) \uff0c\u56e0\u6b64\u9700\u8981\u4e00\u4e2a\u5c3a\u5bf8\u4e3a \\((n+1) \\times (cap+1)\\) \u7684\u4e8c\u7ef4 \\(dp\\) \u8868\u3002</p> <p>\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b</p> <p>\u5f53\u6211\u4eec\u505a\u51fa\u7269\u54c1 \\(i\\) \u7684\u51b3\u7b56\u540e\uff0c\u5269\u4f59\u7684\u662f\u524d \\(i-1\\) \u4e2a\u7269\u54c1\u7684\u51b3\u7b56\u3002\u56e0\u6b64\uff0c\u72b6\u6001\u8f6c\u79fb\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5\uff1a</p> <ul> <li>\u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u80cc\u5305\u5bb9\u91cf\u4e0d\u53d8\uff0c\u72b6\u6001\u8f6c\u79fb\u81f3 \\([i-1, c]\\) \u3002</li> <li>\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u80cc\u5305\u5bb9\u91cf\u51cf\u5c0f \\(wgt[i-1]\\) \uff0c\u4ef7\u503c\u589e\u52a0 \\(val[i-1]\\) \uff0c\u72b6\u6001\u8f6c\u79fb\u81f3 \\([i-1, c-wgt[i-1]]\\) \u3002</li> </ul> <p>\u4e0a\u8ff0\u7684\u72b6\u6001\u8f6c\u79fb\u5411\u6211\u4eec\u63ed\u793a\u4e86\u672c\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6700\u5927\u4ef7\u503c \\(dp[i, c]\\) \u7b49\u4e8e\u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \u548c\u653e\u5165\u7269\u54c1 \\(i\\) \u4e24\u79cd\u65b9\u6848\u4e2d\u7684\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\u3002\u7531\u6b64\u53ef\u63a8\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a</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\u5f53\u524d\u7269\u54c1\u91cd\u91cf \\(wgt[i - 1]\\) \u8d85\u51fa\u5269\u4f59\u80cc\u5305\u5bb9\u91cf \\(c\\) \uff0c\u5219\u53ea\u80fd\u9009\u62e9\u4e0d\u653e\u5165\u80cc\u5305\u3002</p> <p>\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f</p> <p>\u5f53\u65e0\u7269\u54c1\u6216\u65e0\u5269\u4f59\u80cc\u5305\u5bb9\u91cf\u65f6\u6700\u5927\u4ef7\u503c\u4e3a \\(0\\) \uff0c\u5373\u9996\u5217 \\(dp[i, 0]\\) \u548c\u9996\u884c \\(dp[0, c]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002</p> <p>\u5f53\u524d\u72b6\u6001 \\([i, c]\\) \u4ece\u4e0a\u65b9\u7684\u72b6\u6001 \\([i-1, c]\\) \u548c\u5de6\u4e0a\u65b9\u7684\u72b6\u6001 \\([i-1, c-wgt[i-1]]\\) \u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u901a\u8fc7\u4e24\u5c42\u5faa\u73af\u6b63\u5e8f\u904d\u5386\u6574\u4e2a \\(dp\\) \u8868\u5373\u53ef\u3002</p> <p>\u6839\u636e\u4ee5\u4e0a\u5206\u6790\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u6309\u987a\u5e8f\u5b9e\u73b0\u66b4\u529b\u641c\u7d22\u3001\u8bb0\u5fc6\u5316\u641c\u7d22\u3001\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002</p>"},{"location":"chapter_dynamic_programming/knapsack_problem/#_1","title":"\u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"<p>\u641c\u7d22\u4ee3\u7801\u5305\u542b\u4ee5\u4e0b\u8981\u7d20\uff1a</p> <ul> <li>\u9012\u5f52\u53c2\u6570\uff1a\u72b6\u6001 \\([i, c]\\) \u3002</li> <li>\u8fd4\u56de\u503c\uff1a\u5b50\u95ee\u9898\u7684\u89e3 \\(dp[i, c]\\) \u3002</li> <li>\u7ec8\u6b62\u6761\u4ef6\uff1a\u5f53\u7269\u54c1\u7f16\u53f7\u8d8a\u754c \\(i = 0\\) \u6216\u80cc\u5305\u5269\u4f59\u5bb9\u91cf\u4e3a \\(0\\) \u65f6\uff0c\u7ec8\u6b62\u9012\u5f52\u5e76\u8fd4\u56de\u4ef7\u503c \\(0\\) \u3002</li> <li>\u526a\u679d\uff1a\u82e5\u5f53\u524d\u7269\u54c1\u91cd\u91cf\u8d85\u51fa\u80cc\u5305\u5269\u4f59\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\u3002</li> </ul> JavaC++PythonGoJSTSCC#SwiftZigDartRust knapsack.java<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(int[] wgt, int[] val, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] &gt; c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFS(wgt, val, i - 1, c);\nint yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn Math.max(no, yes);\n}\n</code></pre> knapsack.cpp<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(vector&lt;int&gt; &amp;wgt, vector&lt;int&gt; &amp;val, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] &gt; c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFS(wgt, val, i - 1, c);\nint yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn max(no, yes);\n}\n</code></pre> 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\u7d22\"\"\"\n# \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 or c == 0:\nreturn 0\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] &gt; c:\nreturn knapsack_dfs(wgt, val, i - 1, c)\n# \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nno = knapsack_dfs(wgt, val, i - 1, c)\nyes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n# \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn max(no, yes)\n</code></pre> knapsack.go<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc knapsackDFS(wgt, val []int, i, c int) int {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i-1] &gt; c {\nreturn knapsackDFS(wgt, val, i-1, c)\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nno := knapsackDFS(wgt, val, i-1, c)\nyes := knapsackDFS(wgt, val, i-1, c-wgt[i-1]) + val[i-1]\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn int(math.Max(float64(no), float64(yes)))\n}\n</code></pre> knapsack.js<pre><code>[class]{}-[func]{knapsackDFS}\n</code></pre> knapsack.ts<pre><code>[class]{}-[func]{knapsackDFS}\n</code></pre> knapsack.c<pre><code>[class]{}-[func]{knapsackDFS}\n</code></pre> knapsack.cs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(int[] weight, int[] val, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (weight[i - 1] &gt; c) {\nreturn knapsackDFS(weight, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFS(weight, val, i - 1, c);\nint yes = knapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn Math.Max(no, yes);\n}\n</code></pre> knapsack.swift<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfunc knapsackDFS(wgt: [Int], val: [Int], i: Int, c: Int) -&gt; Int {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] &gt; c {\nreturn knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nlet no = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\nlet yes = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn max(no, yes)\n}\n</code></pre> knapsack.zig<pre><code>// 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22\nfn knapsackDFS(wgt: []i32, val: []i32, i: usize, c: usize) i32 {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 or c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] &gt; c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nvar no = knapsackDFS(wgt, val, i - 1, c);\nvar yes = knapsackDFS(wgt, val, i - 1, c - @as(usize, @intCast(wgt[i - 1]))) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn @max(no, yes);\n}\n</code></pre> knapsack.dart<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nint knapsackDFS(List&lt;int&gt; wgt, List&lt;int&gt; val, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] &gt; c) {\nreturn knapsackDFS(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFS(wgt, val, i - 1, c);\nint yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nreturn max(no, yes);\n}\n</code></pre> knapsack.rs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u66b4\u529b\u641c\u7d22 */\nfn knapsack_dfs(wgt: &amp;[i32], val: &amp;[i32], i: usize, c: usize) -&gt; i32 {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0;\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] &gt; c as i32 {\nreturn knapsack_dfs(wgt, val, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nlet no = knapsack_dfs(wgt, val, i - 1, c);\nlet yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nstd::cmp::max(no, yes)\n}\n</code></pre> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u7269\u54c1\u90fd\u4f1a\u4ea7\u751f\u4e0d\u9009\u548c\u9009\u4e24\u6761\u641c\u7d22\u5206\u652f\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002</p> <p>\u89c2\u5bdf\u9012\u5f52\u6811\uff0c\u5bb9\u6613\u53d1\u73b0\u5176\u4e2d\u5b58\u5728\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u4f8b\u5982 \\(dp[1, 10]\\) \u7b49\u3002\u800c\u5f53\u7269\u54c1\u8f83\u591a\u3001\u80cc\u5305\u5bb9\u91cf\u8f83\u5927\uff0c\u5c24\u5176\u662f\u76f8\u540c\u91cd\u91cf\u7684\u7269\u54c1\u8f83\u591a\u65f6\uff0c\u91cd\u53e0\u5b50\u95ee\u9898\u7684\u6570\u91cf\u5c06\u4f1a\u5927\u5e45\u589e\u591a\u3002</p> <p></p> <p> \u56fe\uff1a0-1 \u80cc\u5305\u7684\u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811 </p>"},{"location":"chapter_dynamic_programming/knapsack_problem/#_2","title":"\u65b9\u6cd5\u4e8c\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22","text":"<p>\u4e3a\u4e86\u4fdd\u8bc1\u91cd\u53e0\u5b50\u95ee\u9898\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u6211\u4eec\u501f\u52a9\u8bb0\u5fc6\u5217\u8868 <code>mem</code> \u6765\u8bb0\u5f55\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u5176\u4e2d <code>mem[i][c]</code> \u5bf9\u5e94 \\(dp[i, c]\\) \u3002</p> <p>\u5f15\u5165\u8bb0\u5fc6\u5316\u4e4b\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u5b50\u95ee\u9898\u6570\u91cf\uff0c\u4e5f\u5c31\u662f \\(O(n \\times cap)\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust knapsack.java<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] &gt; c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nint yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = Math.max(no, yes);\nreturn mem[i][c];\n}\n</code></pre> knapsack.cpp<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\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\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] &gt; c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nint yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = max(no, yes);\nreturn mem[i][c];\n}\n</code></pre> knapsack.py<pre><code>def knapsack_dfs_mem(\nwgt: list[int], val: list[int], mem: list[list[int]], i: int, c: int\n) -&gt; int:\n\"\"\"0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\"\"\"\n# \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 or c == 0:\nreturn 0\n# \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][c] != -1:\nreturn mem[i][c]\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] &gt; c:\nreturn knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n# \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nno = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\nyes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n# \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = max(no, yes)\nreturn mem[i][c]\n</code></pre> knapsack.go<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc knapsackDFSMem(wgt, val []int, mem [][]int, i, c int) int {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][c] != -1 {\nreturn mem[i][c]\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i-1] &gt; c {\nreturn knapsackDFSMem(wgt, val, mem, i-1, c)\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nno := knapsackDFSMem(wgt, val, mem, i-1, c)\nyes := knapsackDFSMem(wgt, val, mem, i-1, c-wgt[i-1]) + val[i-1]\n// \u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = int(math.Max(float64(no), float64(yes)))\nreturn mem[i][c]\n}\n</code></pre> knapsack.js<pre><code>[class]{}-[func]{knapsackDFSMem}\n</code></pre> knapsack.ts<pre><code>[class]{}-[func]{knapsackDFSMem}\n</code></pre> knapsack.c<pre><code>[class]{}-[func]{knapsackDFSMem}\n</code></pre> knapsack.cs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (weight[i - 1] &gt; c) {\nreturn knapsackDFSMem(weight, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFSMem(weight, val, mem, i - 1, c);\nint yes = knapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = Math.Max(no, yes);\nreturn mem[i][c];\n}\n</code></pre> knapsack.swift<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nfunc knapsackDFSMem(wgt: [Int], val: [Int], mem: inout [[Int]], i: Int, c: Int) -&gt; Int {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][c] != -1 {\nreturn mem[i][c]\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] &gt; c {\nreturn knapsackDFSMem(wgt: wgt, val: val, mem: &amp;mem, i: i - 1, c: c)\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nlet no = knapsackDFSMem(wgt: wgt, val: val, mem: &amp;mem, i: i - 1, c: c)\nlet yes = knapsackDFSMem(wgt: wgt, val: val, mem: &amp;mem, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = max(no, yes)\nreturn mem[i][c]\n}\n</code></pre> knapsack.zig<pre><code>// 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22\nfn knapsackDFSMem(wgt: []i32, val: []i32, mem: anytype, i: usize, c: usize) i32 {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 or c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] &gt; c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nvar no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nvar yes = knapsackDFSMem(wgt, val, mem, i - 1, c - @as(usize, @intCast(wgt[i - 1]))) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = @max(no, yes);\nreturn mem[i][c];\n}\n</code></pre> knapsack.dart<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\nint knapsackDFSMem(\nList&lt;int&gt; wgt,\nList&lt;int&gt; val,\nList&lt;List&lt;int&gt;&gt; mem,\nint i,\nint c,\n) {\n// \u82e5\u5df2\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif (i == 0 || c == 0) {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (mem[i][c] != -1) {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif (wgt[i - 1] &gt; c) {\nreturn knapsackDFSMem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nint no = knapsackDFSMem(wgt, val, mem, i - 1, c);\nint yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = max(no, yes);\nreturn mem[i][c];\n}\n</code></pre> knapsack.rs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u8bb0\u5fc6\u5316\u641c\u7d22 */\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\u9009\u5b8c\u6240\u6709\u7269\u54c1\u6216\u80cc\u5305\u65e0\u5bb9\u91cf\uff0c\u5219\u8fd4\u56de\u4ef7\u503c 0\nif i == 0 || c == 0 {\nreturn 0;\n}\n// \u82e5\u5df2\u6709\u8bb0\u5f55\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif mem[i][c] != -1 {\nreturn mem[i][c];\n}\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u53ea\u80fd\u4e0d\u653e\u5165\u80cc\u5305\nif wgt[i - 1] &gt; c as i32 {\nreturn knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n}\n// \u8ba1\u7b97\u4e0d\u653e\u5165\u548c\u653e\u5165\u7269\u54c1 i \u7684\u6700\u5927\u4ef7\u503c\nlet no = knapsack_dfs_mem(wgt, val, mem, i - 1, c);\nlet yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n// \u8bb0\u5f55\u5e76\u8fd4\u56de\u4e24\u79cd\u65b9\u6848\u4e2d\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\nmem[i][c] = std::cmp::max(no, yes);\nmem[i][c]\n}\n</code></pre> <p></p> <p> \u56fe\uff1a0-1 \u80cc\u5305\u7684\u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811 </p>"},{"location":"chapter_dynamic_programming/knapsack_problem/#_3","title":"\u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"<p>\u52a8\u6001\u89c4\u5212\u5b9e\u8d28\u4e0a\u5c31\u662f\u5728\u72b6\u6001\u8f6c\u79fb\u4e2d\u586b\u5145 \\(dp\\) \u8868\u7684\u8fc7\u7a0b\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust knapsack.java<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(int[] wgt, int[] val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n + 1][cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int c = 1; c &lt;= cap; c++) {\nif (wgt[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n</code></pre> knapsack.cpp<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(vector&lt;int&gt; &amp;wgt, vector&lt;int&gt; &amp;val, int cap) {\nint n = wgt.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(cap + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int c = 1; c &lt;= cap; c++) {\nif (wgt[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n</code></pre> knapsack.py<pre><code>def knapsack_dp(wgt: list[int], val: list[int], cap: int) -&gt; int:\n\"\"\"0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(wgt)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * (cap + 1) for _ in range(n + 1)]\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\nfor c in range(1, cap + 1):\nif wgt[i - 1] &gt; c:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c]\nelse:\n# \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\nreturn dp[n][cap]\n</code></pre> knapsack.go<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDP(wgt, val []int, cap int) int {\nn := len(wgt)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n+1)\nfor i := 0; i &lt;= n; i++ {\ndp[i] = make([]int, cap+1)\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i &lt;= n; i++ {\nfor c := 1; c &lt;= cap; c++ {\nif wgt[i-1] &gt; c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i-1][c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i-1][c-wgt[i-1]]+val[i-1])))\n}\n}\n}\nreturn dp[n][cap]\n}\n</code></pre> knapsack.js<pre><code>[class]{}-[func]{knapsackDP}\n</code></pre> knapsack.ts<pre><code>[class]{}-[func]{knapsackDP}\n</code></pre> knapsack.c<pre><code>[class]{}-[func]{knapsackDP}\n</code></pre> knapsack.cs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(int[] weight, int[] val, int cap) {\nint n = weight.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n + 1, cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int c = 1; c &lt;= cap; c++) {\nif (weight[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i, c] = dp[i - 1, c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i, c] = Math.Max(dp[i - 1, c - weight[i - 1]] + val[i - 1], dp[i - 1, c]);\n}\n}\n}\nreturn dp[n, cap];\n}\n</code></pre> knapsack.swift<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDP(wgt: [Int], val: [Int], cap: Int) -&gt; Int {\nlet n = wgt.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor c in stride(from: 1, through: cap, by: 1) {\nif wgt[i - 1] &gt; c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n}\n}\n}\nreturn dp[n][cap]\n}\n</code></pre> knapsack.zig<pre><code>// 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\nfn knapsackDP(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\ncomptime var n = wgt.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][cap + 1]i32{[_]i32{0} ** (cap + 1)} ** (n + 1);\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..cap + 1) |c| {\nif (wgt[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = @max(dp[i - 1][c], dp[i - 1][c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n</code></pre> knapsack.dart<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint knapsackDP(List&lt;int&gt; wgt, List&lt;int&gt; val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nList&lt;List&lt;int&gt;&gt; dp = List.generate(n + 1, (index) =&gt; List.filled(cap + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int c = 1; c &lt;= cap; c++) {\nif (wgt[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n</code></pre> knapsack.rs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfn knapsack_dp(wgt: &amp;[i32], val: &amp;[i32], cap: usize) -&gt; i32 {\nlet n = wgt.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; cap + 1]; n + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor c in 1..=cap {\nif wgt[i - 1] &gt; c as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = std::cmp::max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1] as usize] + val[i - 1]);\n}\n}\n}\ndp[n][cap]\n}\n</code></pre> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u90fd\u7531\u6570\u7ec4 <code>dp</code> \u5927\u5c0f\u51b3\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> \u56fe\uff1a0-1 \u80cc\u5305\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b </p>"},{"location":"chapter_dynamic_programming/knapsack_problem/#_4","title":"\u72b6\u6001\u538b\u7f29","text":"<p>\u7531\u4e8e\u6bcf\u4e2a\u72b6\u6001\u90fd\u53ea\u4e0e\u5176\u4e0a\u4e00\u884c\u7684\u72b6\u6001\u6709\u5173\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u6570\u7ec4\u6eda\u52a8\u524d\u8fdb\uff0c\u5c06\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n^2)\\) \u5c06\u4f4e\u81f3 \\(O(n)\\) \u3002</p> <p>\u8fdb\u4e00\u6b65\u601d\u8003\uff0c\u6211\u4eec\u662f\u5426\u53ef\u4ee5\u4ec5\u7528\u4e00\u4e2a\u6570\u7ec4\u5b9e\u73b0\u72b6\u6001\u538b\u7f29\u5462\uff1f\u89c2\u5bdf\u53ef\u77e5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u662f\u7531\u6b63\u4e0a\u65b9\u6216\u5de6\u4e0a\u65b9\u7684\u683c\u5b50\u8f6c\u79fb\u8fc7\u6765\u7684\u3002\u5047\u8bbe\u53ea\u6709\u4e00\u4e2a\u6570\u7ec4\uff0c\u5f53\u5f00\u59cb\u904d\u5386\u7b2c \\(i\\) \u884c\u65f6\uff0c\u8be5\u6570\u7ec4\u5b58\u50a8\u7684\u4ecd\u7136\u662f\u7b2c \\(i-1\\) \u884c\u7684\u72b6\u6001\u3002</p> <ul> <li>\u5982\u679c\u91c7\u53d6\u6b63\u5e8f\u904d\u5386\uff0c\u90a3\u4e48\u904d\u5386\u5230 \\(dp[i, j]\\) \u65f6\uff0c\u5de6\u4e0a\u65b9 \\(dp[i-1, 1]\\) ~ \\(dp[i-1, j-1]\\) \u503c\u53ef\u80fd\u5df2\u7ecf\u88ab\u8986\u76d6\uff0c\u6b64\u65f6\u5c31\u65e0\u6cd5\u5f97\u5230\u6b63\u786e\u7684\u72b6\u6001\u8f6c\u79fb\u7ed3\u679c\u3002</li> <li>\u5982\u679c\u91c7\u53d6\u5012\u5e8f\u904d\u5386\uff0c\u5219\u4e0d\u4f1a\u53d1\u751f\u8986\u76d6\u95ee\u9898\uff0c\u72b6\u6001\u8f6c\u79fb\u53ef\u4ee5\u6b63\u786e\u8fdb\u884c\u3002</li> </ul> <p>\u4ee5\u4e0b\u52a8\u753b\u5c55\u793a\u4e86\u5728\u5355\u4e2a\u6570\u7ec4\u4e0b\u4ece\u7b2c \\(i = 1\\) \u884c\u8f6c\u6362\u81f3\u7b2c \\(i = 2\\) \u884c\u7684\u8fc7\u7a0b\u3002\u8bf7\u601d\u8003\u6b63\u5e8f\u904d\u5386\u548c\u5012\u5e8f\u904d\u5386\u7684\u533a\u522b\u3002</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> \u56fe\uff1a0-1 \u80cc\u5305\u7684\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b </p> <p>\u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4ec5\u9700\u5c06\u6570\u7ec4 <code>dp</code> \u7684\u7b2c\u4e00\u7ef4 \\(i\\) \u76f4\u63a5\u5220\u9664\uff0c\u5e76\u4e14\u628a\u5185\u5faa\u73af\u66f4\u6539\u4e3a\u5012\u5e8f\u904d\u5386\u5373\u53ef\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust knapsack.java<pre><code>/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(int[] wgt, int[] val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (int c = cap; c &gt;= 1; c--) {\nif (wgt[i - 1] &lt;= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n</code></pre> knapsack.cpp<pre><code>/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(vector&lt;int&gt; &amp;wgt, vector&lt;int&gt; &amp;val, int cap) {\nint n = wgt.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector&lt;int&gt; dp(cap + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (int c = cap; c &gt;= 1; c--) {\nif (wgt[i - 1] &lt;= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n</code></pre> knapsack.py<pre><code>def knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -&gt; int:\n\"\"\"0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(wgt)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [0] * (cap + 1)\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\n# \u5012\u5e8f\u904d\u5386\nfor c in range(cap, 0, -1):\nif wgt[i - 1] &gt; c:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c]\nelse:\n# \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\nreturn dp[cap]\n</code></pre> knapsack.go<pre><code>/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDPComp(wgt, val []int, cap int) int {\nn := len(wgt)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, cap+1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i &lt;= n; i++ {\n// \u5012\u5e8f\u904d\u5386\nfor c := cap; c &gt;= 1; c-- {\nif wgt[i-1] &lt;= c {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n}\n}\n}\nreturn dp[cap]\n}\n</code></pre> knapsack.js<pre><code>[class]{}-[func]{knapsackDPComp}\n</code></pre> knapsack.ts<pre><code>[class]{}-[func]{knapsackDPComp}\n</code></pre> knapsack.c<pre><code>[class]{}-[func]{knapsackDPComp}\n</code></pre> knapsack.cs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(int[] weight, int[] val, int cap) {\nint n = weight.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (int c = cap; c &gt; 0; c--) {\nif (weight[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.Max(dp[c], dp[c - weight[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n</code></pre> knapsack.swift<pre><code>/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc knapsackDPComp(wgt: [Int], val: [Int], cap: Int) -&gt; Int {\nlet n = wgt.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: 0, count: cap + 1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\n// \u5012\u5e8f\u904d\u5386\nfor c in stride(from: cap, through: 1, by: -1) {\nif wgt[i - 1] &lt;= c {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n}\n}\n}\nreturn dp[cap]\n}\n</code></pre> knapsack.zig<pre><code>// 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn knapsackDPComp(wgt: []i32, val: []i32, comptime cap: usize) i32 {\nvar n = wgt.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** (cap + 1);\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\n// \u5012\u5e8f\u904d\u5386\nvar c = cap;\nwhile (c &gt; 0) : (c -= 1) {\nif (wgt[i - 1] &lt; c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = @max(dp[c], dp[c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n</code></pre> knapsack.dart<pre><code>/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint knapsackDPComp(List&lt;int&gt; wgt, List&lt;int&gt; val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nList&lt;int&gt; dp = List.filled(cap + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\n// \u5012\u5e8f\u904d\u5386\nfor (int c = cap; c &gt;= 1; c--) {\nif (wgt[i - 1] &lt;= c) {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n</code></pre> knapsack.rs<pre><code>/* 0-1 \u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn knapsack_dp_comp(wgt: &amp;[i32], val: &amp;[i32], cap: usize) -&gt; i32 {\nlet n = wgt.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\n// \u5012\u5e8f\u904d\u5386\nfor c in (1..=cap).rev() {\nif wgt[i - 1] &lt;= c as i32 {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n}\n}\n}\ndp[cap]\n}\n</code></pre>"},{"location":"chapter_dynamic_programming/summary/","title":"14.7. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u52a8\u6001\u89c4\u5212\u5bf9\u95ee\u9898\u8fdb\u884c\u5206\u89e3\uff0c\u5e76\u901a\u8fc7\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u89e3\u6765\u89c4\u907f\u91cd\u590d\u8ba1\u7b97\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u8ba1\u7b97\u6548\u7387\u3002</li> <li>\u4e0d\u8003\u8651\u65f6\u95f4\u7684\u524d\u63d0\u4e0b\uff0c\u6240\u6709\u52a8\u6001\u89c4\u5212\u95ee\u9898\u90fd\u53ef\u4ee5\u7528\u56de\u6eaf\uff08\u66b4\u529b\u641c\u7d22\uff09\u8fdb\u884c\u6c42\u89e3\uff0c\u4f46\u9012\u5f52\u6811\u4e2d\u5b58\u5728\u5927\u91cf\u7684\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u6548\u7387\u6781\u4f4e\u3002\u901a\u8fc7\u5f15\u5165\u8bb0\u5fc6\u5316\u5217\u8868\uff0c\u53ef\u4ee5\u5b58\u50a8\u6240\u6709\u8ba1\u7b97\u8fc7\u7684\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u4ece\u800c\u4fdd\u8bc1\u91cd\u53e0\u5b50\u95ee\u9898\u53ea\u88ab\u8ba1\u7b97\u4e00\u6b21\u3002</li> <li>\u8bb0\u5fc6\u5316\u9012\u5f52\u662f\u4e00\u79cd\u4ece\u9876\u81f3\u5e95\u7684\u9012\u5f52\u5f0f\u89e3\u6cd5\uff0c\u800c\u4e0e\u4e4b\u5bf9\u5e94\u7684\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u4ece\u5e95\u81f3\u9876\u7684\u9012\u63a8\u5f0f\u89e3\u6cd5\uff0c\u5176\u5982\u540c\u201c\u586b\u5199\u8868\u683c\u201d\u4e00\u6837\u3002\u7531\u4e8e\u5f53\u524d\u72b6\u6001\u4ec5\u4f9d\u8d56\u4e8e\u67d0\u4e9b\u5c40\u90e8\u72b6\u6001\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u6d88\u9664 \\(dp\\) \u8868\u7684\u4e00\u4e2a\u7ef4\u5ea6\uff0c\u4ece\u800c\u964d\u4f4e\u7a7a\u95f4\u590d\u6742\u5ea6\u3002</li> <li>\u5b50\u95ee\u9898\u5206\u89e3\u662f\u4e00\u79cd\u901a\u7528\u7684\u7b97\u6cd5\u601d\u8def\uff0c\u5728\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u3001\u56de\u6eaf\u4e2d\u5177\u6709\u4e0d\u540c\u7684\u6027\u8d28\u3002</li> <li>\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4e09\u5927\u7279\u6027\uff1a\u91cd\u53e0\u5b50\u95ee\u9898\u3001\u6700\u4f18\u5b50\u7ed3\u6784\u3001\u65e0\u540e\u6548\u6027\u3002</li> <li>\u5982\u679c\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u53ef\u4ee5\u4ece\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6784\u5efa\u5f97\u6765\uff0c\u5219\u5b83\u5c31\u5177\u6709\u6700\u4f18\u5b50\u7ed3\u6784\u3002</li> <li>\u65e0\u540e\u6548\u6027\u6307\u5bf9\u4e8e\u4e00\u4e2a\u72b6\u6001\uff0c\u5176\u672a\u6765\u53d1\u5c55\u53ea\u4e0e\u8be5\u72b6\u6001\u6709\u5173\uff0c\u4e0e\u5176\u6240\u7ecf\u5386\u7684\u8fc7\u53bb\u7684\u6240\u6709\u72b6\u6001\u65e0\u5173\u3002\u8bb8\u591a\u7ec4\u5408\u4f18\u5316\u95ee\u9898\u90fd\u4e0d\u5177\u6709\u65e0\u540e\u6548\u6027\uff0c\u65e0\u6cd5\u4f7f\u7528\u52a8\u6001\u89c4\u5212\u5feb\u901f\u6c42\u89e3\u3002</li> </ul> <p>\u80cc\u5305\u95ee\u9898</p> <ul> <li>\u80cc\u5305\u95ee\u9898\u662f\u6700\u5178\u578b\u7684\u52a8\u6001\u89c4\u5212\u9898\u76ee\uff0c\u5177\u6709 0-1 \u80cc\u5305\u3001\u5b8c\u5168\u80cc\u5305\u3001\u591a\u91cd\u80cc\u5305\u7b49\u53d8\u79cd\u95ee\u9898\u3002</li> <li>0-1 \u80cc\u5305\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u524d \\(i\\) \u4e2a\u7269\u54c1\u5728\u5269\u4f59\u5bb9\u91cf\u4e3a \\(c\\) \u7684\u80cc\u5305\u4e2d\u7684\u6700\u5927\u4ef7\u503c\u3002\u6839\u636e\u4e0d\u653e\u5165\u80cc\u5305\u548c\u653e\u5165\u80cc\u5305\u4e24\u79cd\u51b3\u7b56\uff0c\u53ef\u5f97\u5230\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u5e76\u6784\u5efa\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002\u5728\u72b6\u6001\u538b\u7f29\u4e2d\uff0c\u7531\u4e8e\u6bcf\u4e2a\u72b6\u6001\u4f9d\u8d56\u6b63\u4e0a\u65b9\u548c\u5de6\u4e0a\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u9700\u8981\u5012\u5e8f\u904d\u5386\u5217\u8868\uff0c\u907f\u514d\u5de6\u4e0a\u65b9\u72b6\u6001\u88ab\u8986\u76d6\u3002</li> <li>\u5b8c\u5168\u80cc\u5305\u7684\u6bcf\u79cd\u7269\u54c1\u7684\u9009\u53d6\u6570\u91cf\u65e0\u9650\u5236\uff0c\u56e0\u6b64\u9009\u62e9\u653e\u5165\u7269\u54c1\u7684\u72b6\u6001\u8f6c\u79fb\u4e0e 0-1 \u80cc\u5305\u4e0d\u540c\u3002\u7531\u4e8e\u72b6\u6001\u4f9d\u8d56\u4e8e\u6b63\u4e0a\u65b9\u548c\u6b63\u5de6\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u5728\u72b6\u6001\u538b\u7f29\u4e2d\u5e94\u5f53\u6b63\u5e8f\u904d\u5386\u3002</li> <li>\u96f6\u94b1\u5151\u6362\u95ee\u9898\u662f\u5b8c\u5168\u80cc\u5305\u7684\u4e00\u4e2a\u53d8\u79cd\u3002\u5b83\u4ece\u6c42\u201c\u6700\u5927\u201d\u4ef7\u503c\u53d8\u4e3a\u6c42\u201c\u6700\u5c0f\u201d\u786c\u5e01\u6570\u91cf\uff0c\u56e0\u6b64\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(\\max()\\) \u5e94\u6539\u4e3a \\(\\min()\\) \u3002\u4ece\u6c42\u201c\u4e0d\u8d85\u8fc7\u201d\u80cc\u5305\u5bb9\u91cf\u5230\u6c42\u201c\u6070\u597d\u201d\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u4f7f\u7528 \\(amt + 1\\) \u6765\u8868\u793a\u201c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u201d\u7684\u65e0\u6548\u89e3\u3002</li> <li>\u96f6\u94b1\u5151\u6362 II \u95ee\u9898\u4ece\u6c42\u201c\u6700\u5c11\u786c\u5e01\u6570\u91cf\u201d\u6539\u4e3a\u6c42\u201c\u786c\u5e01\u7ec4\u5408\u6570\u91cf\u201d\uff0c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u76f8\u5e94\u5730\u4ece \\(\\min()\\) \u6539\u4e3a\u6c42\u548c\u8fd0\u7b97\u7b26\u3002</li> </ul> <p>\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898</p> <ul> <li>\u7f16\u8f91\u8ddd\u79bb\uff08Levenshtein \u8ddd\u79bb\uff09\u7528\u4e8e\u8861\u91cf\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u76f8\u4f3c\u5ea6\uff0c\u5176\u5b9a\u4e49\u4e3a\u4ece\u4e00\u4e2a\u5b57\u7b26\u4e32\u5230\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u6700\u5c0f\u7f16\u8f91\u6b65\u6570\uff0c\u7f16\u8f91\u64cd\u4f5c\u5305\u62ec\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66ff\u6362\u3002</li> <li>\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u7684\u72b6\u6001\u5b9a\u4e49\u4e3a\u5c06 \\(s\\) \u7684\u524d \\(i\\) \u4e2a\u5b57\u7b26\u66f4\u6539\u4e3a \\(t\\) \u7684\u524d \\(j\\) \u4e2a\u5b57\u7b26\u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\u3002\u5f53 \\(s[i] \\ne t[j]\\) \u65f6\uff0c\u5177\u6709\u4e09\u79cd\u51b3\u7b56\uff1a\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66ff\u6362\uff0c\u5b83\u4eec\u90fd\u6709\u76f8\u5e94\u7684\u5269\u4f59\u5b50\u95ee\u9898\u3002\u636e\u6b64\u4fbf\u53ef\u4ee5\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\u4e0e\u6784\u5efa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002\u800c\u5f53 \\(s[i] = t[j]\\) \u65f6\uff0c\u65e0\u9700\u7f16\u8f91\u5f53\u524d\u5b57\u7b26\u3002</li> <li>\u5728\u7f16\u8f91\u8ddd\u79bb\u4e2d\uff0c\u72b6\u6001\u4f9d\u8d56\u4e8e\u5176\u6b63\u4e0a\u65b9\u3001\u6b63\u5de6\u65b9\u3001\u5de6\u4e0a\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u72b6\u6001\u538b\u7f29\u540e\u6b63\u5e8f\u6216\u5012\u5e8f\u904d\u5386\u90fd\u65e0\u6cd5\u6b63\u786e\u5730\u8fdb\u884c\u72b6\u6001\u8f6c\u79fb\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5229\u7528\u4e00\u4e2a\u53d8\u91cf\u6682\u5b58\u5de6\u4e0a\u65b9\u72b6\u6001\uff0c\u4ece\u800c\u8f6c\u5316\u5230\u4e0e\u5b8c\u5168\u80cc\u5305\u7b49\u4ef7\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u72b6\u6001\u538b\u7f29\u540e\u8fdb\u884c\u6b63\u5e8f\u904d\u5386\u3002</li> </ul>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/","title":"14.5. \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898","text":"<p>\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5148\u6c42\u89e3\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u80cc\u5305\u95ee\u9898\uff1a\u5b8c\u5168\u80cc\u5305\uff0c\u518d\u4e86\u89e3\u5b83\u7684\u4e00\u79cd\u7279\u4f8b\uff1a\u96f6\u94b1\u5151\u6362\u3002</p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1451","title":"14.5.1. \u00a0 \u5b8c\u5168\u80cc\u5305","text":"<p>Question</p> <p>\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\) \u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u5728\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u4e0b\u80fd\u653e\u5165\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002</p> <p></p> <p> \u56fe\uff1a\u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e </p> <p>\u5b8c\u5168\u80cc\u5305\u548c 0-1 \u80cc\u5305\u95ee\u9898\u975e\u5e38\u76f8\u4f3c\uff0c\u533a\u522b\u4ec5\u5728\u4e8e\u4e0d\u9650\u5236\u7269\u54c1\u7684\u9009\u62e9\u6b21\u6570\u3002</p> <ul> <li>\u5728 0-1 \u80cc\u5305\u4e2d\uff0c\u6bcf\u4e2a\u7269\u54c1\u53ea\u6709\u4e00\u4e2a\uff0c\u56e0\u6b64\u5c06\u7269\u54c1 \\(i\\) \u653e\u5165\u80cc\u5305\u540e\uff0c\u53ea\u80fd\u4ece\u524d \\(i-1\\) \u4e2a\u7269\u54c1\u4e2d\u9009\u62e9\u3002</li> <li>\u5728\u5b8c\u5168\u80cc\u5305\u4e2d\uff0c\u6bcf\u4e2a\u7269\u54c1\u6709\u65e0\u6570\u4e2a\uff0c\u56e0\u6b64\u5c06\u7269\u54c1 \\(i\\) \u653e\u5165\u80cc\u5305\u540e\uff0c\u4ecd\u53ef\u4ee5\u4ece\u524d \\(i\\) \u4e2a\u7269\u54c1\u4e2d\u9009\u62e9\u3002</li> </ul> <p>\u8fd9\u5c31\u5bfc\u81f4\u4e86\u72b6\u6001\u8f6c\u79fb\u7684\u53d8\u5316\uff0c\u5bf9\u4e8e\u72b6\u6001 \\([i, c]\\) \u6709\uff1a</p> <ul> <li>\u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u4e0e 0-1 \u80cc\u5305\u76f8\u540c\uff0c\u8f6c\u79fb\u81f3 \\([i-1, c]\\) \u3002</li> <li>\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u4e0e 0-1 \u80cc\u5305\u4e0d\u540c\uff0c\u8f6c\u79fb\u81f3 \\([i, c-wgt[i-1]]\\) \u3002</li> </ul> <p>\u4ece\u800c\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u53d8\u4e3a\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/#_1","title":"\u4ee3\u7801\u5b9e\u73b0","text":"<p>\u5bf9\u6bd4\u4e24\u9053\u9898\u76ee\u7684\u4ee3\u7801\uff0c\u72b6\u6001\u8f6c\u79fb\u4e2d\u6709\u4e00\u5904\u4ece \\(i-1\\) \u53d8\u4e3a \\(i\\) \uff0c\u5176\u4f59\u5b8c\u5168\u4e00\u81f4\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust unbounded_knapsack.java<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(int[] wgt, int[] val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n + 1][cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int c = 1; c &lt;= cap; c++) {\nif (wgt[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n</code></pre> unbounded_knapsack.cpp<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(vector&lt;int&gt; &amp;wgt, vector&lt;int&gt; &amp;val, int cap) {\nint n = wgt.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(cap + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int c = 1; c &lt;= cap; c++) {\nif (wgt[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n</code></pre> 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\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(wgt)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * (cap + 1) for _ in range(n + 1)]\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\nfor c in range(1, cap + 1):\nif wgt[i - 1] &gt; c:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c]\nelse:\n# \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\nreturn dp[n][cap]\n</code></pre> unbounded_knapsack.go<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDP(wgt, val []int, cap int) int {\nn := len(wgt)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n+1)\nfor i := 0; i &lt;= n; i++ {\ndp[i] = make([]int, cap+1)\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i &lt;= n; i++ {\nfor c := 1; c &lt;= cap; c++ {\nif wgt[i-1] &gt; c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i-1][c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i][c-wgt[i-1]]+val[i-1])))\n}\n}\n}\nreturn dp[n][cap]\n}\n</code></pre> unbounded_knapsack.js<pre><code>[class]{}-[func]{unboundedKnapsackDP}\n</code></pre> unbounded_knapsack.ts<pre><code>[class]{}-[func]{unboundedKnapsackDP}\n</code></pre> unbounded_knapsack.c<pre><code>[class]{}-[func]{unboundedKnapsackDP}\n</code></pre> unbounded_knapsack.cs<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(int[] wgt, int[] val, int cap) {\nint n = wgt.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n + 1, cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int c = 1; c &lt;= cap; c++) {\nif (wgt[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i, c] = dp[i - 1, c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n, cap];\n}\n</code></pre> unbounded_knapsack.swift<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDP(wgt: [Int], val: [Int], cap: Int) -&gt; Int {\nlet n = wgt.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor c in stride(from: 1, through: cap, by: 1) {\nif wgt[i - 1] &gt; c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n}\n}\n}\nreturn dp[n][cap]\n}\n</code></pre> unbounded_knapsack.zig<pre><code>// \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212\nfn unboundedKnapsackDP(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\ncomptime var n = wgt.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][cap + 1]i32{[_]i32{0} ** (cap + 1)} ** (n + 1);\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..cap + 1) |c| {\nif (wgt[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = @max(dp[i - 1][c], dp[i][c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n</code></pre> unbounded_knapsack.dart<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDP(List&lt;int&gt; wgt, List&lt;int&gt; val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nList&lt;List&lt;int&gt;&gt; dp = List.generate(n + 1, (index) =&gt; List.filled(cap + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int c = 1; c &lt;= cap; c++) {\nif (wgt[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n</code></pre> unbounded_knapsack.rs<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u52a8\u6001\u89c4\u5212 */\nfn unbounded_knapsack_dp(wgt: &amp;[i32], val: &amp;[i32], cap: usize) -&gt; i32 {\nlet n = wgt.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; cap + 1]; n + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor c in 1..=cap {\nif wgt[i - 1] &gt; c as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[i][c] = dp[i - 1][c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[i][c] = std::cmp::max(dp[i - 1][c], dp[i][c - wgt[i - 1] as usize] + val[i - 1]);\n}\n}\n}\nreturn dp[n][cap];\n}\n</code></pre>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#_2","title":"\u72b6\u6001\u538b\u7f29","text":"<p>\u7531\u4e8e\u5f53\u524d\u72b6\u6001\u662f\u4ece\u5de6\u8fb9\u548c\u4e0a\u8fb9\u7684\u72b6\u6001\u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u72b6\u6001\u538b\u7f29\u540e\u5e94\u8be5\u5bf9 \\(dp\\) \u8868\u4e2d\u7684\u6bcf\u4e00\u884c\u91c7\u53d6\u6b63\u5e8f\u904d\u5386\u3002</p> <p>\u8fd9\u4e2a\u904d\u5386\u987a\u5e8f\u4e0e 0-1 \u80cc\u5305\u6b63\u597d\u76f8\u53cd\u3002\u8bf7\u901a\u8fc7\u4ee5\u4e0b\u52a8\u753b\u6765\u7406\u89e3\u4e24\u8005\u7684\u533a\u522b\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> \u56fe\uff1a\u5b8c\u5168\u80cc\u5305\u7684\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b </p> <p>\u4ee3\u7801\u5b9e\u73b0\u6bd4\u8f83\u7b80\u5355\uff0c\u4ec5\u9700\u5c06\u6570\u7ec4 <code>dp</code> \u7684\u7b2c\u4e00\u7ef4\u5220\u9664\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust unbounded_knapsack.java<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int c = 1; c &lt;= cap; c++) {\nif (wgt[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n</code></pre> unbounded_knapsack.cpp<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(vector&lt;int&gt; &amp;wgt, vector&lt;int&gt; &amp;val, int cap) {\nint n = wgt.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector&lt;int&gt; dp(cap + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int c = 1; c &lt;= cap; c++) {\nif (wgt[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n</code></pre> 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\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(wgt)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [0] * (cap + 1)\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\n# \u6b63\u5e8f\u904d\u5386\nfor c in range(1, cap + 1):\nif wgt[i - 1] &gt; c:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c]\nelse:\n# \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\nreturn dp[cap]\n</code></pre> unbounded_knapsack.go<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDPComp(wgt, val []int, cap int) int {\nn := len(wgt)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, cap+1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i &lt;= n; i++ {\nfor c := 1; c &lt;= cap; c++ {\nif wgt[i-1] &gt; c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n}\n}\n}\nreturn dp[cap]\n}\n</code></pre> unbounded_knapsack.js<pre><code>[class]{}-[func]{unboundedKnapsackDPComp}\n</code></pre> unbounded_knapsack.ts<pre><code>[class]{}-[func]{unboundedKnapsackDPComp}\n</code></pre> unbounded_knapsack.c<pre><code>[class]{}-[func]{unboundedKnapsackDPComp}\n</code></pre> unbounded_knapsack.cs<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\nint n = wgt.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int c = 1; c &lt;= cap; c++) {\nif (wgt[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n</code></pre> unbounded_knapsack.swift<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc unboundedKnapsackDPComp(wgt: [Int], val: [Int], cap: Int) -&gt; Int {\nlet n = wgt.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: 0, count: cap + 1)\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor c in stride(from: 1, through: cap, by: 1) {\nif wgt[i - 1] &gt; c {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c]\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n}\n}\n}\nreturn dp[cap]\n}\n</code></pre> unbounded_knapsack.zig<pre><code>// \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn unboundedKnapsackDPComp(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {\ncomptime var n = wgt.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** (cap + 1);\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..cap + 1) |c| {\nif (wgt[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = @max(dp[c], dp[c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n</code></pre> unbounded_knapsack.dart<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint unboundedKnapsackDPComp(List&lt;int&gt; wgt, List&lt;int&gt; val, int cap) {\nint n = wgt.length;\n// \u521d\u59cb\u5316 dp \u8868\nList&lt;int&gt; dp = List.filled(cap + 1, 0);\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int c = 1; c &lt;= cap; c++) {\nif (wgt[i - 1] &gt; c) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n}\n}\n}\nreturn dp[cap];\n}\n</code></pre> unbounded_knapsack.rs<pre><code>/* \u5b8c\u5168\u80cc\u5305\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn unbounded_knapsack_dp_comp(wgt: &amp;[i32], val: &amp;[i32], cap: usize) -&gt; i32 {\nlet n = wgt.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; cap + 1];\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor c in 1..=cap {\nif wgt[i - 1] &gt; c as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u7269\u54c1 i\ndp[c] = dp[c];\n} else {\n// \u4e0d\u9009\u548c\u9009\u7269\u54c1 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5927\u503c\ndp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n}\n}\n}\ndp[cap]\n}\n</code></pre>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1452","title":"14.5.2. \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898","text":"<p>\u80cc\u5305\u95ee\u9898\u662f\u4e00\u5927\u7c7b\u52a8\u6001\u89c4\u5212\u95ee\u9898\u7684\u4ee3\u8868\uff0c\u5176\u62e5\u6709\u5f88\u591a\u7684\u53d8\u79cd\uff0c\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002</p> <p>Question</p> <p>\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u80fd\u591f\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u4e2a\u6570\u3002\u5982\u679c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u5219\u8fd4\u56de \\(-1\\) \u3002</p> <p></p> <p> \u56fe\uff1a\u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e </p> <p>\u96f6\u94b1\u5151\u6362\u53ef\u4ee5\u770b\u4f5c\u662f\u5b8c\u5168\u80cc\u5305\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\uff0c\u4e24\u8005\u5177\u6709\u4ee5\u4e0b\u8054\u7cfb\u4e0e\u4e0d\u540c\u70b9\uff1a</p> <ul> <li>\u4e24\u9053\u9898\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\uff0c\u201c\u7269\u54c1\u201d\u5bf9\u5e94\u4e8e\u201c\u786c\u5e01\u201d\u3001\u201c\u7269\u54c1\u91cd\u91cf\u201d\u5bf9\u5e94\u4e8e\u201c\u786c\u5e01\u9762\u503c\u201d\u3001\u201c\u80cc\u5305\u5bb9\u91cf\u201d\u5bf9\u5e94\u4e8e\u201c\u76ee\u6807\u91d1\u989d\u201d\u3002</li> <li>\u4f18\u5316\u76ee\u6807\u76f8\u53cd\uff0c\u80cc\u5305\u95ee\u9898\u662f\u8981\u6700\u5927\u5316\u7269\u54c1\u4ef7\u503c\uff0c\u96f6\u94b1\u5151\u6362\u95ee\u9898\u662f\u8981\u6700\u5c0f\u5316\u786c\u5e01\u6570\u91cf\u3002</li> <li>\u80cc\u5305\u95ee\u9898\u662f\u6c42\u201c\u4e0d\u8d85\u8fc7\u201d\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u89e3\uff0c\u96f6\u94b1\u5151\u6362\u662f\u6c42\u201c\u6070\u597d\u201d\u51d1\u5230\u76ee\u6807\u91d1\u989d\u7684\u89e3\u3002</li> </ul> <p>\u7b2c\u4e00\u6b65\uff1a\u601d\u8003\u6bcf\u8f6e\u7684\u51b3\u7b56\uff0c\u5b9a\u4e49\u72b6\u6001\uff0c\u4ece\u800c\u5f97\u5230 \\(dp\\) \u8868</p> <p>\u72b6\u6001 \\([i, a]\\) \u5bf9\u5e94\u7684\u5b50\u95ee\u9898\u4e3a\uff1a\u524d \\(i\\) \u79cd\u786c\u5e01\u80fd\u591f\u51d1\u51fa\u91d1\u989d \\(a\\) \u7684\u6700\u5c11\u786c\u5e01\u4e2a\u6570\uff0c\u8bb0\u4e3a \\(dp[i, a]\\) \u3002</p> <p>\u4e8c\u7ef4 \\(dp\\) \u8868\u7684\u5c3a\u5bf8\u4e3a \\((n+1) \\times (amt+1)\\) \u3002</p> <p>\u7b2c\u4e8c\u6b65\uff1a\u627e\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\uff0c\u8fdb\u800c\u63a8\u5bfc\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b</p> <p>\u4e0e\u5b8c\u5168\u80cc\u5305\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u57fa\u672c\u76f8\u540c\uff0c\u4e0d\u540c\u70b9\u5728\u4e8e\uff1a</p> <ul> <li>\u672c\u9898\u8981\u6c42\u6700\u5c0f\u503c\uff0c\u56e0\u6b64\u9700\u5c06\u8fd0\u7b97\u7b26 \\(\\max()\\) \u66f4\u6539\u4e3a \\(\\min()\\) \u3002</li> <li>\u4f18\u5316\u4e3b\u4f53\u662f\u786c\u5e01\u6570\u91cf\u800c\u975e\u5546\u54c1\u4ef7\u503c\uff0c\u56e0\u6b64\u5728\u9009\u4e2d\u786c\u5e01\u65f6\u6267\u884c \\(+1\\) \u5373\u53ef\u3002</li> </ul> \\[ dp[i, a] = \\min(dp[i-1, a], dp[i, a - coins[i-1]] + 1) \\] <p>\u7b2c\u4e09\u6b65\uff1a\u786e\u5b9a\u8fb9\u754c\u6761\u4ef6\u548c\u72b6\u6001\u8f6c\u79fb\u987a\u5e8f</p> <p>\u5f53\u76ee\u6807\u91d1\u989d\u4e3a \\(0\\) \u65f6\uff0c\u51d1\u51fa\u5b83\u7684\u6700\u5c11\u786c\u5e01\u4e2a\u6570\u4e3a \\(0\\) \uff0c\u5373\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002</p> <p>\u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u610f \\(&gt; 0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u5373\u662f\u65e0\u6548\u89e3\u3002\u4e3a\u4f7f\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(\\min()\\) \u51fd\u6570\u80fd\u591f\u8bc6\u522b\u5e76\u8fc7\u6ee4\u65e0\u6548\u89e3\uff0c\u6211\u4eec\u8003\u8651\u4f7f\u7528 \\(+ \\infty\\) \u6765\u8868\u793a\u5b83\u4eec\uff0c\u5373\u4ee4\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(+ \\infty\\) \u3002</p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#_3","title":"\u4ee3\u7801\u5b9e\u73b0","text":"<p>\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u5e76\u672a\u63d0\u4f9b \\(+ \\infty\\) \u53d8\u91cf\uff0c\u53ea\u80fd\u4f7f\u7528\u6574\u578b <code>int</code> \u7684\u6700\u5927\u503c\u6765\u4ee3\u66ff\u3002\u800c\u8fd9\u53c8\u4f1a\u5bfc\u81f4\u5927\u6570\u8d8a\u754c\uff1a\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e2d\u7684 \\(+ 1\\) \u64cd\u4f5c\u53ef\u80fd\u53d1\u751f\u6ea2\u51fa\u3002</p> <p>\u4e3a\u6b64\uff0c\u6211\u4eec\u91c7\u7528\u6570\u5b57 \\(amt + 1\\) \u6765\u8868\u793a\u65e0\u6548\u89e3\uff0c\u56e0\u4e3a\u51d1\u51fa \\(amt\\) \u7684\u786c\u5e01\u4e2a\u6570\u6700\u591a\u4e3a \\(amt\\) \u4e2a\u3002</p> <p>\u6700\u540e\u8fd4\u56de\u524d\uff0c\u5224\u65ad \\(dp[n, amt]\\) \u662f\u5426\u7b49\u4e8e \\(amt + 1\\) \uff0c\u82e5\u662f\u5219\u8fd4\u56de \\(-1\\) \uff0c\u4ee3\u8868\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust coin_change.java<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(int[] coins, int amt) {\nint n = coins.length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n + 1][amt + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int a = 1; a &lt;= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n</code></pre> coin_change.cpp<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(vector&lt;int&gt; &amp;coins, int amt) {\nint n = coins.size();\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nvector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(amt + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int a = 1; a &lt;= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n</code></pre> coin_change.py<pre><code>def coin_change_dp(coins: list[int], amt: int) -&gt; int:\n\"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(coins)\nMAX = amt + 1\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * (amt + 1) for _ in range(n + 1)]\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor a in range(1, amt + 1):\ndp[0][a] = MAX\n# \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in range(1, n + 1):\nfor a in range(1, amt + 1):\nif coins[i - 1] &gt; a:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a]\nelse:\n# \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\nreturn dp[n][amt] if dp[n][amt] != MAX else -1\n</code></pre> coin_change.go<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDP(coins []int, amt int) int {\nn := len(coins)\nmax := amt + 1\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n+1)\nfor i := 0; i &lt;= n; i++ {\ndp[i] = make([]int, amt+1)\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor a := 1; a &lt;= amt; a++ {\ndp[0][a] = max\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i &lt;= n; i++ {\nfor a := 1; a &lt;= amt; a++ {\nif coins[i-1] &gt; a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i-1][a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = int(math.Min(float64(dp[i-1][a]), float64(dp[i][a-coins[i-1]]+1)))\n}\n}\n}\nif dp[n][amt] != max {\nreturn dp[n][amt]\n}\nreturn -1\n}\n</code></pre> coin_change.js<pre><code>[class]{}-[func]{coinChangeDP}\n</code></pre> coin_change.ts<pre><code>[class]{}-[func]{coinChangeDP}\n</code></pre> coin_change.c<pre><code>[class]{}-[func]{coinChangeDP}\n</code></pre> coin_change.cs<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(int[] coins, int amt) {\nint n = coins.Length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n + 1, amt + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int a = 1; a &lt;= amt; a++) {\ndp[0, a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i, a] = dp[i - 1, a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i, a] = Math.Min(dp[i - 1, a], dp[i, a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n, amt] != MAX ? dp[n, amt] : -1;\n}\n</code></pre> coin_change.swift<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDP(coins: [Int], amt: Int) -&gt; Int {\nlet n = coins.count\nlet MAX = amt + 1\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor a in stride(from: 1, through: amt, by: 1) {\ndp[0][a] = MAX\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in stride(from: 1, through: n, by: 1) {\nfor a in stride(from: 1, through: amt, by: 1) {\nif coins[i - 1] &gt; a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n}\n}\n}\nreturn dp[n][amt] != MAX ? dp[n][amt] : -1\n}\n</code></pre> coin_change.zig<pre><code>// \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212\nfn coinChangeDP(comptime coins: []i32, comptime amt: usize) i32 {\ncomptime var n = coins.len;\ncomptime var max = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][amt + 1]i32{[_]i32{0} ** (amt + 1)} ** (n + 1);\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (1..amt + 1) |a| {\ndp[0][a] = max;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (1..n + 1) |i| {\nfor (1..amt + 1) |a| {\nif (coins[i - 1] &gt; @as(i32, @intCast(a))) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = @min(dp[i - 1][a], dp[i][a - @as(usize, @intCast(coins[i - 1]))] + 1);\n}\n}\n}\nif (dp[n][amt] != max) {\nreturn @intCast(dp[n][amt]);\n} else {\nreturn -1;\n}\n}\n</code></pre> coin_change.dart<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeDP(List&lt;int&gt; coins, int amt) {\nint n = coins.length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nList&lt;List&lt;int&gt;&gt; dp = List.generate(n + 1, (index) =&gt; List.filled(amt + 1, 0));\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor (int a = 1; a &lt;= amt; a++) {\ndp[0][a] = MAX;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n</code></pre> coin_change.rs<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfn coin_change_dp(coins: &amp;[i32], amt: usize) -&gt; i32 {\nlet n = coins.len();\nlet max = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; amt + 1]; n + 1];\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u9996\u884c\u9996\u5217\nfor a in 1..= amt {\ndp[0][a] = max;\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i in 1..=n {\nfor a in 1..=amt {\nif coins[i - 1] &gt; a as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = std::cmp::min(dp[i - 1][a], dp[i][a - coins[i - 1] as usize] + 1);\n}\n}\n}\nif dp[n][amt] != max { return dp[n][amt] as i32; } else { -1 }\n}\n</code></pre> <p>\u4e0b\u56fe\u5c55\u793a\u4e86\u96f6\u94b1\u5151\u6362\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\uff0c\u548c\u5b8c\u5168\u80cc\u5305\u975e\u5e38\u76f8\u4f3c\u3002</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> \u56fe\uff1a\u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b </p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#_4","title":"\u72b6\u6001\u538b\u7f29","text":"<p>\u96f6\u94b1\u5151\u6362\u7684\u72b6\u6001\u538b\u7f29\u7684\u5904\u7406\u65b9\u5f0f\u548c\u5b8c\u5168\u80cc\u5305\u4e00\u81f4\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust coin_change.java<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(int[] coins, int amt) {\nint n = coins.length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[amt + 1];\nArrays.fill(dp, MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1;\n}\n</code></pre> coin_change.cpp<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(vector&lt;int&gt; &amp;coins, int amt) {\nint n = coins.size();\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nvector&lt;int&gt; dp(amt + 1, MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1;\n}\n</code></pre> coin_change.py<pre><code>def coin_change_dp_comp(coins: list[int], amt: int) -&gt; int:\n\"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(coins)\nMAX = amt + 1\n# \u521d\u59cb\u5316 dp \u8868\ndp = [MAX] * (amt + 1)\ndp[0] = 0\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\n# \u6b63\u5e8f\u904d\u5386\nfor a in range(1, amt + 1):\nif coins[i - 1] &gt; a:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\nelse:\n# \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\nreturn dp[amt] if dp[amt] != MAX else -1\n</code></pre> coin_change.go<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDPComp(coins []int, amt int) int {\nn := len(coins)\nmax := amt + 1\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, amt+1)\nfor i := 1; i &lt;= amt; i++ {\ndp[i] = max\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i &lt;= n; i++ {\n// \u5012\u5e8f\u904d\u5386\nfor a := 1; a &lt;= amt; a++ {\nif coins[i-1] &gt; a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = int(math.Min(float64(dp[a]), float64(dp[a-coins[i-1]]+1)))\n}\n}\n}\nif dp[amt] != max {\nreturn dp[amt]\n}\nreturn -1\n}\n</code></pre> coin_change.js<pre><code>[class]{}-[func]{coinChangeDPComp}\n</code></pre> coin_change.ts<pre><code>[class]{}-[func]{coinChangeDPComp}\n</code></pre> coin_change.c<pre><code>[class]{}-[func]{coinChangeDPComp}\n</code></pre> coin_change.cs<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(int[] coins, int amt) {\nint n = coins.Length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[amt + 1];\nArray.Fill(dp, MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = Math.Min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1;\n}\n</code></pre> coin_change.swift<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeDPComp(coins: [Int], amt: Int) -&gt; Int {\nlet n = coins.count\nlet MAX = amt + 1\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: MAX, count: amt + 1)\ndp[0] = 0\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor a in stride(from: 1, through: amt, by: 1) {\nif coins[i - 1] &gt; a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1\n}\n</code></pre> coin_change.zig<pre><code>// \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn coinChangeDPComp(comptime coins: []i32, comptime amt: usize) i32 {\ncomptime var n = coins.len;\ncomptime var max = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** (amt + 1);\n@memset(&amp;dp, max);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..amt + 1) |a| {\nif (coins[i - 1] &gt; @as(i32, @intCast(a))) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = @min(dp[a], dp[a - @as(usize, @intCast(coins[i - 1]))] + 1);\n}\n}\n}\nif (dp[amt] != max) {\nreturn @intCast(dp[amt]);\n} else {\nreturn -1;\n}\n}\n</code></pre> coin_change.dart<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeDPComp(List&lt;int&gt; coins, int amt) {\nint n = coins.length;\nint MAX = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nList&lt;int&gt; dp = List.filled(amt + 1, MAX);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n}\n}\n}\nreturn dp[amt] != MAX ? dp[amt] : -1;\n}\n</code></pre> coin_change.rs<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn coin_change_dp_comp(coins: &amp;[i32], amt: usize) -&gt; i32 {\nlet n = coins.len();\nlet max = amt + 1;\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; amt + 1];\ndp.fill(max);\ndp[0] = 0;\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor a in 1..=amt {\nif coins[i - 1] &gt; a as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = std::cmp::min(dp[a], dp[a - coins[i - 1] as usize] + 1);\n}\n}\n}\nif dp[amt] != max { return dp[amt] as i32; } else { -1 }\n}\n</code></pre>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1453-ii","title":"14.5.3. \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898 II","text":"<p>Question</p> <p>\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u5728\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u786c\u5e01\u7ec4\u5408\u6570\u91cf\u3002</p> <p></p> <p> \u56fe\uff1a\u96f6\u94b1\u5151\u6362\u95ee\u9898 II \u7684\u793a\u4f8b\u6570\u636e </p> <p>\u76f8\u6bd4\u4e8e\u4e0a\u4e00\u9898\uff0c\u672c\u9898\u76ee\u6807\u662f\u7ec4\u5408\u6570\u91cf\uff0c\u56e0\u6b64\u5b50\u95ee\u9898\u53d8\u4e3a\uff1a\u524d \\(i\\) \u79cd\u786c\u5e01\u80fd\u591f\u51d1\u51fa\u91d1\u989d \\(a\\) \u7684\u7ec4\u5408\u6570\u91cf\u3002\u800c \\(dp\\) \u8868\u4ecd\u7136\u662f\u5c3a\u5bf8\u4e3a \\((n+1) \\times (amt + 1)\\) \u7684\u4e8c\u7ef4\u77e9\u9635\u3002</p> <p>\u5f53\u524d\u72b6\u6001\u7684\u7ec4\u5408\u6570\u91cf\u7b49\u4e8e\u4e0d\u9009\u5f53\u524d\u786c\u5e01\u4e0e\u9009\u5f53\u524d\u786c\u5e01\u8fd9\u4e24\u79cd\u51b3\u7b56\u7684\u7ec4\u5408\u6570\u91cf\u4e4b\u548c\u3002\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a</p> \\[ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]] \\] <p>\u5f53\u76ee\u6807\u91d1\u989d\u4e3a \\(0\\) \u65f6\uff0c\u65e0\u9700\u9009\u62e9\u4efb\u4f55\u786c\u5e01\u5373\u53ef\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u5e94\u5c06\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u521d\u59cb\u5316\u4e3a \\(1\\) \u3002\u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u4f55 \\(&gt;0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002</p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#_5","title":"\u4ee3\u7801\u5b9e\u73b0","text":"JavaC++PythonGoJSTSCC#SwiftZigDartRust coin_change_ii.java<pre><code>/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(int[] coins, int amt) {\nint n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[][] dp = new int[n + 1][amt + 1];\n// \u521d\u59cb\u5316\u9996\u5217\nfor (int i = 0; i &lt;= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n</code></pre> coin_change_ii.cpp<pre><code>/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(vector&lt;int&gt; &amp;coins, int amt) {\nint n = coins.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(amt + 1, 0));\n// \u521d\u59cb\u5316\u9996\u5217\nfor (int i = 0; i &lt;= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n</code></pre> coin_change_ii.py<pre><code>def coin_change_ii_dp(coins: list[int], amt: int) -&gt; int:\n\"\"\"\u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(coins)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [[0] * (amt + 1) for _ in range(n + 1)]\n# \u521d\u59cb\u5316\u9996\u5217\nfor i in range(n + 1):\ndp[i][0] = 1\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\nfor a in range(1, amt + 1):\nif coins[i - 1] &gt; a:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a]\nelse:\n# \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\nreturn dp[n][amt]\n</code></pre> coin_change_ii.go<pre><code>/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDP(coins []int, amt int) int {\nn := len(coins)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([][]int, n+1)\nfor i := 0; i &lt;= n; i++ {\ndp[i] = make([]int, amt+1)\n}\n// \u521d\u59cb\u5316\u9996\u5217\nfor i := 0; i &lt;= n; i++ {\ndp[i][0] = 1\n}\n// \u72b6\u6001\u8f6c\u79fb\uff1a\u5176\u4f59\u884c\u5217\nfor i := 1; i &lt;= n; i++ {\nfor a := 1; a &lt;= amt; a++ {\nif coins[i-1] &gt; a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i-1][a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = dp[i-1][a] + dp[i][a-coins[i-1]]\n}\n}\n}\nreturn dp[n][amt]\n}\n</code></pre> coin_change_ii.js<pre><code>[class]{}-[func]{coinChangeIIDP}\n</code></pre> coin_change_ii.ts<pre><code>[class]{}-[func]{coinChangeIIDP}\n</code></pre> coin_change_ii.c<pre><code>[class]{}-[func]{coinChangeIIDP}\n</code></pre> coin_change_ii.cs<pre><code>/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(int[] coins, int amt) {\nint n = coins.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[,] dp = new int[n + 1, amt + 1];\n// \u521d\u59cb\u5316\u9996\u5217\nfor (int i = 0; i &lt;= n; i++) {\ndp[i, 0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i, a] = dp[i - 1, a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i, a] = dp[i - 1, a] + dp[i, a - coins[i - 1]];\n}\n}\n}\nreturn dp[n, amt];\n}\n</code></pre> coin_change_ii.swift<pre><code>/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDP(coins: [Int], amt: Int) -&gt; Int {\nlet n = coins.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n// \u521d\u59cb\u5316\u9996\u5217\nfor i in stride(from: 0, through: n, by: 1) {\ndp[i][0] = 1\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor a in stride(from: 1, through: amt, by: 1) {\nif coins[i - 1] &gt; a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n}\n}\n}\nreturn dp[n][amt]\n}\n</code></pre> coin_change_ii.zig<pre><code>// \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212\nfn coinChangeIIDP(comptime coins: []i32, comptime amt: usize) i32 {\ncomptime var n = coins.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_][amt + 1]i32{[_]i32{0} ** (amt + 1)} ** (n + 1);\n// \u521d\u59cb\u5316\u9996\u5217\nfor (0..n + 1) |i| {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..amt + 1) |a| {\nif (coins[i - 1] &gt; @as(i32, @intCast(a))) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = dp[i - 1][a] + dp[i][a - @as(usize, @intCast(coins[i - 1]))];\n}\n}\n}\nreturn dp[n][amt];\n}\n</code></pre> coin_change_ii.dart<pre><code>/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDP(List&lt;int&gt; coins, int amt) {\nint n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nList&lt;List&lt;int&gt;&gt; dp = List.generate(n + 1, (index) =&gt; List.filled(amt + 1, 0));\n// \u521d\u59cb\u5316\u9996\u5217\nfor (int i = 0; i &lt;= n; i++) {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n}\n}\n}\nreturn dp[n][amt];\n}\n</code></pre> coin_change_ii.rs<pre><code>/* \u96f6\u94b1\u5151\u6362 II\uff1a\u52a8\u6001\u89c4\u5212 */\nfn coin_change_ii_dp(coins: &amp;[i32], amt: usize) -&gt; i32 {\nlet n = coins.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![vec![0; amt + 1]; n + 1];\n// \u521d\u59cb\u5316\u9996\u5217\nfor i in 0..= n {\ndp[i][0] = 1;\n}\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor a in 1..=amt {\nif coins[i - 1] &gt; a as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[i][a] = dp[i - 1][a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1] as usize];\n}\n}\n}\ndp[n][amt]\n}\n</code></pre>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#_6","title":"\u72b6\u6001\u538b\u7f29","text":"<p>\u72b6\u6001\u538b\u7f29\u5904\u7406\u65b9\u5f0f\u76f8\u540c\uff0c\u5220\u9664\u786c\u5e01\u7ef4\u5ea6\u5373\u53ef\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust coin_change_ii.java<pre><code>/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(int[] coins, int amt) {\nint n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[amt + 1];\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n</code></pre> coin_change_ii.cpp<pre><code>/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(vector&lt;int&gt; &amp;coins, int amt) {\nint n = coins.size();\n// \u521d\u59cb\u5316 dp \u8868\nvector&lt;int&gt; dp(amt + 1, 0);\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n</code></pre> coin_change_ii.py<pre><code>def coin_change_ii_dp_comp(coins: list[int], amt: int) -&gt; int:\n\"\"\"\u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\"\"\"\nn = len(coins)\n# \u521d\u59cb\u5316 dp \u8868\ndp = [0] * (amt + 1)\ndp[0] = 1\n# \u72b6\u6001\u8f6c\u79fb\nfor i in range(1, n + 1):\n# \u6b63\u5e8f\u904d\u5386\nfor a in range(1, amt + 1):\nif coins[i - 1] &gt; a:\n# \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\nelse:\n# \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]]\nreturn dp[amt]\n</code></pre> coin_change_ii.go<pre><code>/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDPComp(coins []int, amt int) int {\nn := len(coins)\n// \u521d\u59cb\u5316 dp \u8868\ndp := make([]int, amt+1)\ndp[0] = 1\n// \u72b6\u6001\u8f6c\u79fb\nfor i := 1; i &lt;= n; i++ {\n// \u5012\u5e8f\u904d\u5386\nfor a := 1; a &lt;= amt; a++ {\nif coins[i-1] &gt; a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a-coins[i-1]]\n}\n}\n}\nreturn dp[amt]\n}\n</code></pre> coin_change_ii.js<pre><code>[class]{}-[func]{coinChangeIIDPComp}\n</code></pre> coin_change_ii.ts<pre><code>[class]{}-[func]{coinChangeIIDPComp}\n</code></pre> coin_change_ii.c<pre><code>[class]{}-[func]{coinChangeIIDPComp}\n</code></pre> coin_change_ii.cs<pre><code>/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(int[] coins, int amt) {\nint n = coins.Length;\n// \u521d\u59cb\u5316 dp \u8868\nint[] dp = new int[amt + 1];\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n</code></pre> coin_change_ii.swift<pre><code>/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfunc coinChangeIIDPComp(coins: [Int], amt: Int) -&gt; Int {\nlet n = coins.count\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = Array(repeating: 0, count: amt + 1)\ndp[0] = 1\n// \u72b6\u6001\u8f6c\u79fb\nfor i in stride(from: 1, through: n, by: 1) {\nfor a in stride(from: 1, through: amt, by: 1) {\nif coins[i - 1] &gt; a {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a]\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]]\n}\n}\n}\nreturn dp[amt]\n}\n</code></pre> coin_change_ii.zig<pre><code>// \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212\nfn coinChangeIIDPComp(comptime coins: []i32, comptime amt: usize) i32 {\ncomptime var n = coins.len;\n// \u521d\u59cb\u5316 dp \u8868\nvar dp = [_]i32{0} ** (amt + 1);\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (1..n + 1) |i| {\nfor (1..amt + 1) |a| {\nif (coins[i - 1] &gt; @as(i32, @intCast(a))) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = dp[a] + dp[a - @as(usize, @intCast(coins[i - 1]))];\n}\n}\n}\nreturn dp[amt];\n}\n</code></pre> coin_change_ii.dart<pre><code>/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nint coinChangeIIDPComp(List&lt;int&gt; coins, int amt) {\nint n = coins.length;\n// \u521d\u59cb\u5316 dp \u8868\nList&lt;int&gt; dp = List.filled(amt + 1, 0);\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor (int i = 1; i &lt;= n; i++) {\nfor (int a = 1; a &lt;= amt; a++) {\nif (coins[i - 1] &gt; a) {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u4e4b\u548c\ndp[a] = dp[a] + dp[a - coins[i - 1]];\n}\n}\n}\nreturn dp[amt];\n}\n</code></pre> coin_change_ii.rs<pre><code>/* \u96f6\u94b1\u5151\u6362 II\uff1a\u72b6\u6001\u538b\u7f29\u540e\u7684\u52a8\u6001\u89c4\u5212 */\nfn coin_change_ii_dp_comp(coins: &amp;[i32], amt: usize) -&gt; i32 {\nlet n = coins.len();\n// \u521d\u59cb\u5316 dp \u8868\nlet mut dp = vec![0; amt + 1];\ndp[0] = 1;\n// \u72b6\u6001\u8f6c\u79fb\nfor i in 1..=n {\nfor a in 1..=amt {\nif coins[i - 1] &gt; a as i32 {\n// \u82e5\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\uff0c\u5219\u4e0d\u9009\u786c\u5e01 i\ndp[a] = dp[a];\n} else {\n// \u4e0d\u9009\u548c\u9009\u786c\u5e01 i \u8fd9\u4e24\u79cd\u65b9\u6848\u7684\u8f83\u5c0f\u503c\ndp[a] = dp[a] + dp[a - coins[i - 1] as usize];\n}\n}\n}\ndp[amt]\n}\n</code></pre>"},{"location":"chapter_graph/","title":"9. \u00a0 \u56fe","text":"<p>Abstract</p> <p>\u5728\u751f\u547d\u65c5\u9014\u4e2d\uff0c\u6211\u4eec\u5c31\u50cf\u662f\u6bcf\u4e2a\u8282\u70b9\uff0c\u88ab\u65e0\u6570\u770b\u4e0d\u89c1\u7684\u8fb9\u76f8\u8fde\u3002</p> <p>\u6bcf\u4e00\u6b21\u7684\u76f8\u8bc6\u4e0e\u76f8\u79bb\uff0c\u90fd\u5728\u8fd9\u5f20\u5de8\u5927\u7684\u7f51\u7edc\u56fe\u4e2d\u7559\u4e0b\u72ec\u7279\u7684\u5370\u8bb0\u3002</p>"},{"location":"chapter_graph/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>9.1 \u00a0 \u56fe</li> <li>9.2 \u00a0 \u56fe\u57fa\u7840\u64cd\u4f5c</li> <li>9.3 \u00a0 \u56fe\u7684\u904d\u5386</li> <li>9.4 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_graph/graph/","title":"9.1. \u00a0 \u56fe","text":"<p>\u300c\u56fe Graph\u300d\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u7531\u300c\u9876\u70b9 Vertex\u300d\u548c\u300c\u8fb9 Edge\u300d\u7ec4\u6210\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u56fe \\(G\\) \u62bd\u8c61\u5730\u8868\u793a\u4e3a\u4e00\u7ec4\u9876\u70b9 \\(V\\) \u548c\u4e00\u7ec4\u8fb9 \\(E\\) \u7684\u96c6\u5408\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u4e00\u4e2a\u5305\u542b 5 \u4e2a\u9876\u70b9\u548c 7 \u6761\u8fb9\u7684\u56fe\u3002</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></p> <p> \u56fe\uff1a\u94fe\u8868\u3001\u6811\u3001\u56fe\u4e4b\u95f4\u7684\u5173\u7cfb </p> <p>\u90a3\u4e48\uff0c\u56fe\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u7684\u5173\u7cfb\u662f\u4ec0\u4e48\uff1f\u5982\u679c\u6211\u4eec\u628a\u300c\u9876\u70b9\u300d\u770b\u4f5c\u8282\u70b9\uff0c\u628a\u300c\u8fb9\u300d\u770b\u4f5c\u8fde\u63a5\u5404\u4e2a\u8282\u70b9\u7684\u6307\u9488\uff0c\u5219\u53ef\u5c06\u300c\u56fe\u300d\u770b\u4f5c\u662f\u4e00\u79cd\u4ece\u300c\u94fe\u8868\u300d\u62d3\u5c55\u800c\u6765\u7684\u6570\u636e\u7ed3\u6784\u3002\u76f8\u8f83\u4e8e\u7ebf\u6027\u5173\u7cfb\uff08\u94fe\u8868\uff09\u548c\u5206\u6cbb\u5173\u7cfb\uff08\u6811\uff09\uff0c\u7f51\u7edc\u5173\u7cfb\uff08\u56fe\uff09\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\uff0c\u4ece\u800c\u66f4\u4e3a\u590d\u6742\u3002</p>"},{"location":"chapter_graph/graph/#911","title":"9.1.1. \u00a0 \u56fe\u5e38\u89c1\u7c7b\u578b","text":"<p>\u6839\u636e\u8fb9\u662f\u5426\u5177\u6709\u65b9\u5411\uff0c\u53ef\u5206\u4e3a\u300c\u65e0\u5411\u56fe Undirected Graph\u300d\u548c\u300c\u6709\u5411\u56fe Directed Graph\u300d\u3002</p> <ul> <li>\u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u8fb9\u8868\u793a\u4e24\u9876\u70b9\u4e4b\u95f4\u7684\u201c\u53cc\u5411\u201d\u8fde\u63a5\u5173\u7cfb\uff0c\u4f8b\u5982\u5fae\u4fe1\u6216 QQ \u4e2d\u7684\u201c\u597d\u53cb\u5173\u7cfb\u201d\u3002</li> <li>\u5728\u6709\u5411\u56fe\u4e2d\uff0c\u8fb9\u5177\u6709\u65b9\u5411\u6027\uff0c\u5373 \\(A \\rightarrow B\\) \u548c \\(A \\leftarrow B\\) \u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u4f8b\u5982\u5fae\u535a\u6216\u6296\u97f3\u4e0a\u7684\u201c\u5173\u6ce8\u201d\u4e0e\u201c\u88ab\u5173\u6ce8\u201d\u5173\u7cfb\u3002</li> </ul> <p></p> <p> \u56fe\uff1a\u6709\u5411\u56fe\u4e0e\u65e0\u5411\u56fe </p> <p>\u6839\u636e\u6240\u6709\u9876\u70b9\u662f\u5426\u8fde\u901a\uff0c\u53ef\u5206\u4e3a\u300c\u8fde\u901a\u56fe Connected Graph\u300d\u548c\u300c\u975e\u8fde\u901a\u56fe Disconnected Graph\u300d\u3002</p> <ul> <li>\u5bf9\u4e8e\u8fde\u901a\u56fe\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u53ef\u4ee5\u5230\u8fbe\u5176\u4f59\u4efb\u610f\u9876\u70b9\u3002</li> <li>\u5bf9\u4e8e\u975e\u8fde\u901a\u56fe\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u81f3\u5c11\u6709\u4e00\u4e2a\u9876\u70b9\u65e0\u6cd5\u5230\u8fbe\u3002</li> </ul> <p></p> <p> \u56fe\uff1a\u8fde\u901a\u56fe\u4e0e\u975e\u8fde\u901a\u56fe </p> <p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3a\u8fb9\u6dfb\u52a0\u201c\u6743\u91cd\u201d\u53d8\u91cf\uff0c\u4ece\u800c\u5f97\u5230\u300c\u6709\u6743\u56fe Weighted Graph\u300d\u3002\u4f8b\u5982\uff0c\u5728\u738b\u8005\u8363\u8000\u7b49\u624b\u6e38\u4e2d\uff0c\u7cfb\u7edf\u4f1a\u6839\u636e\u5171\u540c\u6e38\u620f\u65f6\u95f4\u6765\u8ba1\u7b97\u73a9\u5bb6\u4e4b\u95f4\u7684\u201c\u4eb2\u5bc6\u5ea6\u201d\uff0c\u8fd9\u79cd\u4eb2\u5bc6\u5ea6\u7f51\u7edc\u5c31\u53ef\u4ee5\u7528\u6709\u6743\u56fe\u6765\u8868\u793a\u3002</p> <p></p> <p> \u56fe\uff1a\u6709\u6743\u56fe\u4e0e\u65e0\u6743\u56fe </p>"},{"location":"chapter_graph/graph/#912","title":"9.1.2. \u00a0 \u56fe\u5e38\u7528\u672f\u8bed","text":"<ul> <li>\u300c\u90bb\u63a5 Adjacency\u300d\uff1a\u5f53\u4e24\u9876\u70b9\u4e4b\u95f4\u5b58\u5728\u8fb9\u76f8\u8fde\u65f6\uff0c\u79f0\u8fd9\u4e24\u9876\u70b9\u201c\u90bb\u63a5\u201d\u3002\u5728\u4e0a\u56fe\u4e2d\uff0c\u9876\u70b9 1 \u7684\u90bb\u63a5\u9876\u70b9\u4e3a\u9876\u70b9 2\u30013\u30015\u3002</li> <li>\u300c\u8def\u5f84 Path\u300d\uff1a\u4ece\u9876\u70b9 A \u5230\u9876\u70b9 B \u7ecf\u8fc7\u7684\u8fb9\u6784\u6210\u7684\u5e8f\u5217\u88ab\u79f0\u4e3a\u4ece A \u5230 B \u7684\u201c\u8def\u5f84\u201d\u3002\u5728\u4e0a\u56fe\u4e2d\uff0c\u8fb9\u5e8f\u5217 1-5-2-4 \u662f\u9876\u70b9 1 \u5230\u9876\u70b9 4 \u7684\u4e00\u6761\u8def\u5f84\u3002</li> <li>\u300c\u5ea6 Degree\u300d\u8868\u793a\u4e00\u4e2a\u9876\u70b9\u62e5\u6709\u7684\u8fb9\u6570\u3002\u5bf9\u4e8e\u6709\u5411\u56fe\uff0c\u300c\u5165\u5ea6 In-Degree\u300d\u8868\u793a\u6709\u591a\u5c11\u6761\u8fb9\u6307\u5411\u8be5\u9876\u70b9\uff0c\u300c\u51fa\u5ea6 Out-Degree\u300d\u8868\u793a\u6709\u591a\u5c11\u6761\u8fb9\u4ece\u8be5\u9876\u70b9\u6307\u51fa\u3002</li> </ul>"},{"location":"chapter_graph/graph/#913","title":"9.1.3. \u00a0 \u56fe\u7684\u8868\u793a","text":"<p>\u56fe\u7684\u5e38\u7528\u8868\u793a\u65b9\u6cd5\u5305\u62ec\u300c\u90bb\u63a5\u77e9\u9635\u300d\u548c\u300c\u90bb\u63a5\u8868\u300d\u3002\u4ee5\u4e0b\u4f7f\u7528\u65e0\u5411\u56fe\u8fdb\u884c\u4e3e\u4f8b\u3002</p>"},{"location":"chapter_graph/graph/#_1","title":"\u90bb\u63a5\u77e9\u9635","text":"<p>\u8bbe\u56fe\u7684\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u300c\u90bb\u63a5\u77e9\u9635 Adjacency Matrix\u300d\u4f7f\u7528\u4e00\u4e2a \\(n \\times n\\) \u5927\u5c0f\u7684\u77e9\u9635\u6765\u8868\u793a\u56fe\uff0c\u6bcf\u4e00\u884c\uff08\u5217\uff09\u4ee3\u8868\u4e00\u4e2a\u9876\u70b9\uff0c\u77e9\u9635\u5143\u7d20\u4ee3\u8868\u8fb9\uff0c\u7528 \\(1\\) \u6216 \\(0\\) \u8868\u793a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u662f\u5426\u5b58\u5728\u8fb9\u3002</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u8bbe\u90bb\u63a5\u77e9\u9635\u4e3a \\(M\\) \u3001\u9876\u70b9\u5217\u8868\u4e3a \\(V\\) \uff0c\u90a3\u4e48\u77e9\u9635\u5143\u7d20 \\(M[i][j] = 1\\) \u8868\u793a\u9876\u70b9 \\(V[i]\\) \u5230\u9876\u70b9 \\(V[j]\\) \u4e4b\u95f4\u5b58\u5728\u8fb9\uff0c\u53cd\u4e4b \\(M[i][j] = 0\\) \u8868\u793a\u4e24\u9876\u70b9\u4e4b\u95f4\u65e0\u8fb9\u3002</p> <p></p> <p> \u56fe\uff1a\u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a </p> <p>\u90bb\u63a5\u77e9\u9635\u5177\u6709\u4ee5\u4e0b\u7279\u6027\uff1a</p> <ul> <li>\u9876\u70b9\u4e0d\u80fd\u4e0e\u81ea\u8eab\u76f8\u8fde\uff0c\u56e0\u6b64\u90bb\u63a5\u77e9\u9635\u4e3b\u5bf9\u89d2\u7ebf\u5143\u7d20\u6ca1\u6709\u610f\u4e49\u3002</li> <li>\u5bf9\u4e8e\u65e0\u5411\u56fe\uff0c\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u7b49\u4ef7\uff0c\u6b64\u65f6\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\u3002</li> <li>\u5c06\u90bb\u63a5\u77e9\u9635\u7684\u5143\u7d20\u4ece \\(1\\) , \\(0\\) \u66ff\u6362\u4e3a\u6743\u91cd\uff0c\u5219\u53ef\u8868\u793a\u6709\u6743\u56fe\u3002</li> </ul> <p>\u4f7f\u7528\u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u77e9\u9635\u5143\u7d20\u4ee5\u83b7\u53d6\u8fb9\uff0c\u56e0\u6b64\u589e\u5220\u67e5\u64cd\u4f5c\u7684\u6548\u7387\u5f88\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(1)\\) \u3002\u7136\u800c\uff0c\u77e9\u9635\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u5185\u5b58\u5360\u7528\u8f83\u591a\u3002</p>"},{"location":"chapter_graph/graph/#_2","title":"\u90bb\u63a5\u8868","text":"<p>\u300c\u90bb\u63a5\u8868 Adjacency List\u300d\u4f7f\u7528 \\(n\\) \u4e2a\u94fe\u8868\u6765\u8868\u793a\u56fe\uff0c\u94fe\u8868\u8282\u70b9\u8868\u793a\u9876\u70b9\u3002\u7b2c \\(i\\) \u6761\u94fe\u8868\u5bf9\u5e94\u9876\u70b9 \\(i\\) \uff0c\u5176\u4e2d\u5b58\u50a8\u4e86\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff08\u5373\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u9876\u70b9\uff09\u3002</p> <p></p> <p> \u56fe\uff1a\u56fe\u7684\u90bb\u63a5\u8868\u8868\u793a </p> <p>\u90bb\u63a5\u8868\u4ec5\u5b58\u50a8\u5b9e\u9645\u5b58\u5728\u7684\u8fb9\uff0c\u800c\u8fb9\u7684\u603b\u6570\u901a\u5e38\u8fdc\u5c0f\u4e8e \\(n^2\\) \uff0c\u56e0\u6b64\u5b83\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\u3002\u7136\u800c\uff0c\u5728\u90bb\u63a5\u8868\u4e2d\u9700\u8981\u901a\u8fc7\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u8fb9\uff0c\u56e0\u6b64\u5176\u65f6\u95f4\u6548\u7387\u4e0d\u5982\u90bb\u63a5\u77e9\u9635\u3002</p> <p>\u89c2\u5bdf\u4e0a\u56fe\u53ef\u53d1\u73b0\uff0c\u90bb\u63a5\u8868\u7ed3\u6784\u4e0e\u54c8\u5e0c\u8868\u4e2d\u7684\u300c\u94fe\u5730\u5740\u6cd5\u300d\u975e\u5e38\u76f8\u4f3c\uff0c\u56e0\u6b64\u6211\u4eec\u4e5f\u53ef\u4ee5\u91c7\u7528\u7c7b\u4f3c\u65b9\u6cd5\u6765\u4f18\u5316\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5f53\u94fe\u8868\u8f83\u957f\u65f6\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u5316\u4e3a AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u6548\u7387\u4ece \\(O(n)\\) \u4f18\u5316\u81f3 \\(O(\\log n)\\) \uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4e2d\u5e8f\u904d\u5386\u83b7\u53d6\u6709\u5e8f\u5e8f\u5217\uff1b\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u54c8\u5e0c\u8868\uff0c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4f4e\u81f3 \\(O(1)\\) \u3002</p>"},{"location":"chapter_graph/graph/#914","title":"9.1.4. \u00a0 \u56fe\u5e38\u89c1\u5e94\u7528","text":"<p>\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8bb8\u591a\u7cfb\u7edf\u90fd\u53ef\u4ee5\u7528\u56fe\u6765\u5efa\u6a21\uff0c\u76f8\u5e94\u7684\u5f85\u6c42\u89e3\u95ee\u9898\u4e5f\u53ef\u4ee5\u7ea6\u5316\u4e3a\u56fe\u8ba1\u7b97\u95ee\u9898\u3002</p> <p> \u8868\uff1a\u73b0\u5b9e\u751f\u6d3b\u4e2d\u5e38\u89c1\u7684\u56fe </p> \u9876\u70b9 \u8fb9 \u56fe\u8ba1\u7b97\u95ee\u9898 \u793e\u4ea4\u7f51\u7edc \u7528\u6237 \u597d\u53cb\u5173\u7cfb \u6f5c\u5728\u597d\u53cb\u63a8\u8350 \u5730\u94c1\u7ebf\u8def \u7ad9\u70b9 \u7ad9\u70b9\u95f4\u7684\u8fde\u901a\u6027 \u6700\u77ed\u8def\u7ebf\u63a8\u8350 \u592a\u9633\u7cfb \u661f\u4f53 \u661f\u4f53\u95f4\u7684\u4e07\u6709\u5f15\u529b\u4f5c\u7528 \u884c\u661f\u8f68\u9053\u8ba1\u7b97"},{"location":"chapter_graph/graph_operations/","title":"9.2. \u00a0 \u56fe\u57fa\u7840\u64cd\u4f5c","text":"<p>\u56fe\u7684\u57fa\u7840\u64cd\u4f5c\u53ef\u5206\u4e3a\u5bf9\u300c\u8fb9\u300d\u7684\u64cd\u4f5c\u548c\u5bf9\u300c\u9876\u70b9\u300d\u7684\u64cd\u4f5c\u3002\u5728\u300c\u90bb\u63a5\u77e9\u9635\u300d\u548c\u300c\u90bb\u63a5\u8868\u300d\u4e24\u79cd\u8868\u793a\u65b9\u6cd5\u4e0b\uff0c\u5b9e\u73b0\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\u3002</p>"},{"location":"chapter_graph/graph_operations/#921","title":"9.2.1. \u00a0 \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u7684\u5b9e\u73b0","text":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \u7684\u65e0\u5411\u56fe\uff0c\u5219\u6709\uff1a</p> <ul> <li>\u6dfb\u52a0\u6216\u5220\u9664\u8fb9\uff1a\u76f4\u63a5\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u4fee\u6539\u6307\u5b9a\u7684\u8fb9\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u800c\u7531\u4e8e\u662f\u65e0\u5411\u56fe\uff0c\u56e0\u6b64\u9700\u8981\u540c\u65f6\u66f4\u65b0\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002</li> <li>\u6dfb\u52a0\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u77e9\u9635\u7684\u5c3e\u90e8\u6dfb\u52a0\u4e00\u884c\u4e00\u5217\uff0c\u5e76\u5168\u90e8\u586b \\(0\\) \u5373\u53ef\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002</li> <li>\u5220\u9664\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u4e00\u884c\u4e00\u5217\u3002\u5f53\u5220\u9664\u9996\u884c\u9996\u5217\u65f6\u8fbe\u5230\u6700\u5dee\u60c5\u51b5\uff0c\u9700\u8981\u5c06 \\((n-1)^2\\) \u4e2a\u5143\u7d20\u201c\u5411\u5de6\u4e0a\u79fb\u52a8\u201d\uff0c\u4ece\u800c\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002</li> <li>\u521d\u59cb\u5316\uff1a\u4f20\u5165 \\(n\\) \u4e2a\u9876\u70b9\uff0c\u521d\u59cb\u5316\u957f\u5ea6\u4e3a \\(n\\) \u7684\u9876\u70b9\u5217\u8868 <code>vertices</code> \uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff1b\u521d\u59cb\u5316 \\(n \\times n\\) \u5927\u5c0f\u7684\u90bb\u63a5\u77e9\u9635 <code>adjMat</code> \uff0c\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002</li> </ul> \u521d\u59cb\u5316\u90bb\u63a5\u77e9\u9635\u6dfb\u52a0\u8fb9\u5220\u9664\u8fb9\u6dfb\u52a0\u9876\u70b9\u5220\u9664\u9876\u70b9 <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe\uff1a\u90bb\u63a5\u77e9\u9635\u7684\u521d\u59cb\u5316\u3001\u589e\u5220\u8fb9\u3001\u589e\u5220\u9876\u70b9 </p> <p>\u4ee5\u4e0b\u662f\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\u7684\u5b9e\u73b0\u4ee3\u7801\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust graph_adjacency_matrix.java<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nList&lt;Integer&gt; vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nList&lt;List&lt;Integer&gt;&gt; adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u65b9\u6cd5 */\npublic GraphAdjMat(int[] vertices, int[][] edges) {\nthis.vertices = new ArrayList&lt;&gt;();\nthis.adjMat = new ArrayList&lt;&gt;();\n// \u6dfb\u52a0\u9876\u70b9\nfor (int val : vertices) {\naddVertex(val);\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor (int[] e : edges) {\naddEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size() {\nreturn vertices.size();\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(int val) {\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.add(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nList&lt;Integer&gt; newRow = new ArrayList&lt;&gt;(n);\nfor (int j = 0; j &lt; n; j++) {\nnewRow.add(0);\n}\nadjMat.add(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (List&lt;Integer&gt; row : adjMat) {\nrow.add(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(int index) {\nif (index &gt;= size())\nthrow new IndexOutOfBoundsException();\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.remove(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.remove(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (List&lt;Integer&gt; row : adjMat) {\nrow.remove(index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void addEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j)\nthrow new IndexOutOfBoundsException();\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat.get(i).set(j, 1);\nadjMat.get(j).set(i, 1);\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void removeEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j)\nthrow new IndexOutOfBoundsException();\nadjMat.get(i).set(j, 0);\nadjMat.get(j).set(i, 0);\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\npublic void print() {\nSystem.out.print(\"\u9876\u70b9\u5217\u8868 = \");\nSystem.out.println(vertices);\nSystem.out.println(\"\u90bb\u63a5\u77e9\u9635 =\");\nPrintUtil.printMatrix(adjMat);\n}\n}\n</code></pre> graph_adjacency_matrix.cpp<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nvector&lt;int&gt; vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nvector&lt;vector&lt;int&gt;&gt; adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nGraphAdjMat(const vector&lt;int&gt; &amp;vertices, const vector&lt;vector&lt;int&gt;&gt; &amp;edges) {\n// \u6dfb\u52a0\u9876\u70b9\nfor (int val : vertices) {\naddVertex(val);\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor (const vector&lt;int&gt; &amp;edge : edges) {\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nint size() const {\nreturn vertices.size();\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(int val) {\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.push_back(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nadjMat.emplace_back(vector&lt;int&gt;(n, 0));\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (vector&lt;int&gt; &amp;row : adjMat) {\nrow.push_back(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(int index) {\nif (index &gt;= size()) {\nthrow out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.erase(vertices.begin() + index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.erase(adjMat.begin() + index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (vector&lt;int&gt; &amp;row : adjMat) {\nrow.erase(row.begin() + index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j) {\nthrow out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1;\nadjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j) {\nthrow out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n}\nadjMat[i][j] = 0;\nadjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nvoid print() {\ncout &lt;&lt; \"\u9876\u70b9\u5217\u8868 = \";\nprintVector(vertices);\ncout &lt;&lt; \"\u90bb\u63a5\u77e9\u9635 =\" &lt;&lt; endl;\nprintVectorMatrix(adjMat);\n}\n};\n</code></pre> graph_adjacency_matrix.py<pre><code>class GraphAdjMat:\n\"\"\"\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\n# \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nvertices: list[int] = []\n# \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\nadj_mat: list[list[int]] = []\ndef __init__(self, vertices: list[int], edges: list[list[int]]):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.vertices: list[int] = []\nself.adj_mat: list[list[int]] = []\n# \u6dfb\u52a0\u9876\u70b9\nfor val in vertices:\nself.add_vertex(val)\n# \u6dfb\u52a0\u8fb9\n# \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor e in edges:\nself.add_edge(e[0], e[1])\ndef size(self) -&gt; int:\n\"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\nreturn len(self.vertices)\ndef add_vertex(self, val: int):\n\"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\nn = self.size()\n# \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nself.vertices.append(val)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nnew_row = [0] * n\nself.adj_mat.append(new_row)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor row in self.adj_mat:\nrow.append(0)\ndef remove_vertex(self, index: int):\n\"\"\"\u5220\u9664\u9876\u70b9\"\"\"\nif index &gt;= self.size():\nraise IndexError()\n# \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nself.vertices.pop(index)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nself.adj_mat.pop(index)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor row in self.adj_mat:\nrow.pop(index)\ndef add_edge(self, i: int, j: int):\n\"\"\"\u6dfb\u52a0\u8fb9\"\"\"\n# \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n# \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i &lt; 0 or j &lt; 0 or i &gt;= self.size() or j &gt;= self.size() or i == j:\nraise IndexError()\n# \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nself.adj_mat[i][j] = 1\nself.adj_mat[j][i] = 1\ndef remove_edge(self, i: int, j: int):\n\"\"\"\u5220\u9664\u8fb9\"\"\"\n# \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n# \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i &lt; 0 or j &lt; 0 or i &gt;= self.size() or j &gt;= self.size() or i == j:\nraise IndexError()\nself.adj_mat[i][j] = 0\nself.adj_mat[j][i] = 0\ndef print(self):\n\"\"\"\u6253\u5370\u90bb\u63a5\u77e9\u9635\"\"\"\nprint(\"\u9876\u70b9\u5217\u8868 =\", self.vertices)\nprint(\"\u90bb\u63a5\u77e9\u9635 =\")\nprint_matrix(self.adj_mat)\n</code></pre> graph_adjacency_matrix.go<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjMat struct {\n// \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nvertices []int\n// \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\nadjMat [][]int\n}\n/* \u6784\u9020\u51fd\u6570 */\nfunc newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {\n// \u6dfb\u52a0\u9876\u70b9\nn := len(vertices)\nadjMat := make([][]int, n)\nfor i := range adjMat {\nadjMat[i] = make([]int, n)\n}\n// \u521d\u59cb\u5316\u56fe\ng := &amp;graphAdjMat{\nvertices: vertices,\nadjMat: adjMat,\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor i := range edges {\ng.addEdge(edges[i][0], edges[i][1])\n}\nreturn g\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjMat) size() int {\nreturn len(g.vertices)\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjMat) addVertex(val int) {\nn := g.size()\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\ng.vertices = append(g.vertices, val)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nnewRow := make([]int, n)\ng.adjMat = append(g.adjMat, newRow)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor i := range g.adjMat {\ng.adjMat[i] = append(g.adjMat[i], 0)\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjMat) removeVertex(index int) {\nif index &gt;= g.size() {\nreturn\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\ng.vertices = append(g.vertices[:index], g.vertices[index+1:]...)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\ng.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor i := range g.adjMat {\ng.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) addEdge(i, j int) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i &lt; 0 || j &lt; 0 || i &gt;= g.size() || j &gt;= g.size() || i == j {\nfmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\ng.adjMat[i][j] = 1\ng.adjMat[j][i] = 1\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) removeEdge(i, j int) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i &lt; 0 || j &lt; 0 || i &gt;= g.size() || j &gt;= g.size() || i == j {\nfmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n}\ng.adjMat[i][j] = 0\ng.adjMat[j][i] = 0\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nfunc (g *graphAdjMat) print() {\nfmt.Printf(\"\\t\u9876\u70b9\u5217\u8868 = %v\\n\", g.vertices)\nfmt.Printf(\"\\t\u90bb\u63a5\u77e9\u9635 = \\n\")\nfor i := range g.adjMat {\nfmt.Printf(\"\\t\\t\\t%v\\n\", g.adjMat[i])\n}\n}\n</code></pre> graph_adjacency_matrix.js<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nvertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nadjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u51fd\u6570 */\nconstructor(vertices, edges) {\nthis.vertices = [];\nthis.adjMat = [];\n// \u6dfb\u52a0\u9876\u70b9\nfor (const val of vertices) {\nthis.addVertex(val);\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor (const e of edges) {\nthis.addEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize() {\nreturn this.vertices.length;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(val) {\nconst n = this.size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nthis.vertices.push(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nconst newRow = [];\nfor (let j = 0; j &lt; n; j++) {\nnewRow.push(0);\n}\nthis.adjMat.push(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (const row of this.adjMat) {\nrow.push(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(index) {\nif (index &gt;= this.size()) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nthis.vertices.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nthis.adjMat.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (const row of this.adjMat) {\nrow.splice(index, 1);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\naddEdge(i, j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i &lt; 0 || j &lt; 0 || i &gt;= this.size() || j &gt;= this.size() || i === j) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) === (j, i)\nthis.adjMat[i][j] = 1;\nthis.adjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nremoveEdge(i, j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i &lt; 0 || j &lt; 0 || i &gt;= this.size() || j &gt;= this.size() || i === j) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\nthis.adjMat[i][j] = 0;\nthis.adjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nprint() {\nconsole.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\nconsole.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n}\n}\n</code></pre> graph_adjacency_matrix.ts<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nvertices: number[]; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nadjMat: number[][]; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u51fd\u6570 */\nconstructor(vertices: number[], edges: number[][]) {\nthis.vertices = [];\nthis.adjMat = [];\n// \u6dfb\u52a0\u9876\u70b9\nfor (const val of vertices) {\nthis.addVertex(val);\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor (const e of edges) {\nthis.addEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize(): number {\nreturn this.vertices.length;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(val: number): void {\nconst n: number = this.size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nthis.vertices.push(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nconst newRow: number[] = [];\nfor (let j: number = 0; j &lt; n; j++) {\nnewRow.push(0);\n}\nthis.adjMat.push(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (const row of this.adjMat) {\nrow.push(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(index: number): void {\nif (index &gt;= this.size()) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nthis.vertices.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nthis.adjMat.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (const row of this.adjMat) {\nrow.splice(index, 1);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\naddEdge(i: number, j: number): void {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i &lt; 0 || j &lt; 0 || i &gt;= this.size() || j &gt;= this.size() || i === j) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) === (j, i)\nthis.adjMat[i][j] = 1;\nthis.adjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nremoveEdge(i: number, j: number): void {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i &lt; 0 || j &lt; 0 || i &gt;= this.size() || j &gt;= this.size() || i === j) {\nthrow new RangeError('Index Out Of Bounds Exception');\n}\nthis.adjMat[i][j] = 0;\nthis.adjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nprint(): void {\nconsole.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\nconsole.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n}\n}\n</code></pre> graph_adjacency_matrix.c<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u7ed3\u6784 */\nstruct graphAdjMat {\nint *vertices; // \u9876\u70b9\u5217\u8868\nunsigned int **adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u8fb9\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nunsigned int size; // \u9876\u70b9\u6570\u91cf\nunsigned int capacity; // \u56fe\u5bb9\u91cf\n};\ntypedef struct graphAdjMat graphAdjMat;\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(graphAdjMat *t, int i, int j) {\n// \u8d8a\u754c\u68c0\u67e5\nif (i &lt; 0 || j &lt; 0 || i &gt;= t-&gt;size || j &gt;= t-&gt;size || i == j) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nexit(1);\n}\n// \u6dfb\u52a0\u8fb9\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nt-&gt;adjMat[i][j] = 1;\nt-&gt;adjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(graphAdjMat *t, int i, int j) {\n// \u8d8a\u754c\u68c0\u67e5\nif (i &lt; 0 || j &lt; 0 || i &gt;= t-&gt;size || j &gt;= t-&gt;size || i == j) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nexit(1);\n}\n// \u5220\u9664\u8fb9\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nt-&gt;adjMat[i][j] = 0;\nt-&gt;adjMat[j][i] = 0;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(graphAdjMat *t, int val) {\n// \u5982\u679c\u5b9e\u9645\u4f7f\u7528\u4e0d\u5927\u4e8e\u9884\u8bbe\u7a7a\u95f4\uff0c\u5219\u76f4\u63a5\u521d\u59cb\u5316\u65b0\u7a7a\u95f4\nif (t-&gt;size &lt; t-&gt;capacity) {\nt-&gt;vertices[t-&gt;size] = val; // \u521d\u59cb\u5316\u65b0\u9876\u70b9\u503c\nfor (int i = 0; i &lt; t-&gt;size; i++) {\nt-&gt;adjMat[i][t-&gt;size] = 0; // \u90bb\u63a5\u77e9\u65b0\u5217\u9635\u7f6e0\n}\nmemset(t-&gt;adjMat[t-&gt;size], 0, sizeof(unsigned int) * (t-&gt;size + 1)); // \u5c06\u65b0\u589e\u884c\u7f6e 0\nt-&gt;size++;\nreturn;\n}\n// \u6269\u5bb9\uff0c\u7533\u8bf7\u65b0\u7684\u9876\u70b9\u6570\u7ec4\nint *temp = (int *)malloc(sizeof(int) * (t-&gt;size * 2));\nmemcpy(temp, t-&gt;vertices, sizeof(int) * t-&gt;size);\ntemp[t-&gt;size] = val;\n// \u91ca\u653e\u539f\u6570\u7ec4\nfree(t-&gt;vertices);\nt-&gt;vertices = temp;\n// \u6269\u5bb9\uff0c\u7533\u8bf7\u65b0\u7684\u4e8c\u7ef4\u6570\u7ec4\nunsigned int **tempMat = (unsigned int **)malloc(sizeof(unsigned int *) * t-&gt;size * 2);\nunsigned int *tempMatLine = (unsigned int *)malloc(sizeof(unsigned int) * (t-&gt;size * 2) * (t-&gt;size * 2));\nmemset(tempMatLine, 0, sizeof(unsigned int) * (t-&gt;size * 2) * (t-&gt;size * 2));\nfor (int k = 0; k &lt; t-&gt;size * 2; k++) {\ntempMat[k] = tempMatLine + k * (t-&gt;size * 2);\n}\nfor (int i = 0; i &lt; t-&gt;size; i++) {\nmemcpy(tempMat[i], t-&gt;adjMat[i], sizeof(unsigned int) * t-&gt;size); // \u539f\u6570\u636e\u590d\u5236\u5230\u65b0\u6570\u7ec4\n}\nfor (int i = 0; i &lt; t-&gt;size; i++) {\ntempMat[i][t-&gt;size] = 0; // \u5c06\u65b0\u589e\u5217\u7f6e 0\n}\nmemset(tempMat[t-&gt;size], 0, sizeof(unsigned int) * (t-&gt;size + 1)); // \u5c06\u65b0\u589e\u884c\u7f6e 0\n// \u91ca\u653e\u539f\u6570\u7ec4\nfree(t-&gt;adjMat[0]);\nfree(t-&gt;adjMat);\n// \u6269\u5bb9\u540e\uff0c\u6307\u5411\u65b0\u5730\u5740\nt-&gt;adjMat = tempMat; // \u6307\u5411\u65b0\u7684\u90bb\u63a5\u77e9\u9635\u5730\u5740\nt-&gt;capacity = t-&gt;size * 2;\nt-&gt;size++;\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(graphAdjMat *t, unsigned int index) {\n// \u8d8a\u754c\u68c0\u67e5\nif (index &lt; 0 || index &gt;= t-&gt;size) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nexit(1);\n}\nfor (int i = index; i &lt; t-&gt;size - 1; i++) {\nt-&gt;vertices[i] = t-&gt;vertices[i + 1]; // \u6e05\u9664\u5220\u9664\u7684\u9876\u70b9\uff0c\u5e76\u5c06\u5176\u540e\u6240\u6709\u9876\u70b9\u524d\u79fb\n}\nt-&gt;vertices[t-&gt;size - 1] = 0; // \u5c06\u88ab\u524d\u79fb\u7684\u6700\u540e\u4e00\u4e2a\u9876\u70b9\u7f6e 0\n// \u6e05\u9664\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7684\u5217\nfor (int i = 0; i &lt; t-&gt;size - 1; i++) {\nif (i &lt; index) {\nfor (int j = index; j &lt; t-&gt;size - 1; j++) {\nt-&gt;adjMat[i][j] = t-&gt;adjMat[i][j + 1]; // \u88ab\u5220\u9664\u5217\u540e\u7684\u6240\u6709\u5217\u524d\u79fb\n}\n} else { memcpy(t-&gt;adjMat[i], t-&gt;adjMat[i + 1], sizeof(unsigned int) * t-&gt;size); // \u88ab\u5220\u9664\u884c\u7684\u4e0b\u65b9\u6240\u6709\u884c\u4e0a\u79fb\nfor (int j = index; j &lt; t-&gt;size; j++) {\nt-&gt;adjMat[i][j] = t-&gt;adjMat[i][j + 1]; // \u88ab\u5220\u9664\u5217\u540e\u7684\u6240\u6709\u5217\u524d\u79fb\n}\n}\n}\nt-&gt;size--;\n}\n/* \u6253\u5370\u9876\u70b9\u4e0e\u90bb\u63a5\u77e9\u9635 */\nvoid printGraph(graphAdjMat *t) {\nif (t-&gt;size == 0) {\nprintf(\"graph is empty\\n\");\nreturn;\n}\nprintf(\"\u9876\u70b9\u5217\u8868 = [\");\nfor (int i = 0; i &lt; t-&gt;size; i++) {\nif (i != t-&gt;size - 1) {\nprintf(\"%d, \", t-&gt;vertices[i]);\n} else {\nprintf(\"%d\", t-&gt;vertices[i]);\n}\n}\nprintf(\"]\\n\");\nprintf(\"\u90bb\u63a5\u77e9\u9635 =\\n[\\n\");\nfor (int i = 0; i &lt; t-&gt;size; i++) {\nprintf(\" [\");\nfor (int j = 0; j &lt; t-&gt;size; j++) {\nif (j != t-&gt;size - 1) {\nprintf(\"%u, \", t-&gt;adjMat[i][j]);\n} else {\nprintf(\"%u\", t-&gt;adjMat[i][j]);\n}\n}\nprintf(\"],\\n\");\n}\nprintf(\"]\\n\");\n}\n/* \u6784\u9020\u51fd\u6570 */\ngraphAdjMat *newGraphAjdMat(unsigned int numberVertices, int *vertices, unsigned int **adjMat) {\n// \u7533\u8bf7\u5185\u5b58\ngraphAdjMat *newGraph = (graphAdjMat *)malloc(sizeof(graphAdjMat)); // \u4e3a\u56fe\u5206\u914d\u5185\u5b58\nnewGraph-&gt;vertices = (int *)malloc(sizeof(int) * numberVertices * 2); // \u4e3a\u9876\u70b9\u5217\u8868\u5206\u914d\u5185\u5b58\nnewGraph-&gt;adjMat = (unsigned int **)malloc(sizeof(unsigned int *) * numberVertices * 2); // \u4e3a\u90bb\u63a5\u77e9\u9635\u5206\u914d\u4e8c\u7ef4\u5185\u5b58\nunsigned int *temp = (unsigned int *)malloc(sizeof(unsigned int) * numberVertices * 2 * numberVertices * 2); // \u4e3a\u90bb\u63a5\u77e9\u9635\u5206\u914d\u4e00\u7ef4\u5185\u5b58\nnewGraph-&gt;size = numberVertices; // \u521d\u59cb\u5316\u9876\u70b9\u6570\u91cf\nnewGraph-&gt;capacity = numberVertices * 2; // \u521d\u59cb\u5316\u56fe\u5bb9\u91cf\n// \u914d\u7f6e\u4e8c\u7ef4\u6570\u7ec4\nfor (int i = 0; i &lt; numberVertices * 2; i++) {\nnewGraph-&gt;adjMat[i] = temp + i * numberVertices * 2; // \u5c06\u4e8c\u7ef4\u6307\u9488\u6307\u5411\u4e00\u7ef4\u6570\u7ec4\n}\n// \u8d4b\u503c\nmemcpy(newGraph-&gt;vertices, vertices, sizeof(int) * numberVertices);\nfor (int i = 0; i &lt; numberVertices; i++) {\nmemcpy(newGraph-&gt;adjMat[i], adjMat[i], sizeof(unsigned int) * numberVertices); // \u5c06\u4f20\u5165\u7684\u90bb\u63a5\u77e9\u9635\u8d4b\u503c\u7ed9\u7ed3\u6784\u4f53\u5185\u90bb\u63a5\u77e9\u9635\n}\n// \u8fd4\u56de\u7ed3\u6784\u4f53\u6307\u9488\nreturn newGraph;\n}\n</code></pre> graph_adjacency_matrix.cs<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nList&lt;int&gt; vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nList&lt;List&lt;int&gt;&gt; adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u51fd\u6570 */\npublic GraphAdjMat(int[] vertices, int[][] edges) {\nthis.vertices = new List&lt;int&gt;();\nthis.adjMat = new List&lt;List&lt;int&gt;&gt;();\n// \u6dfb\u52a0\u9876\u70b9\nforeach (int val in vertices) {\naddVertex(val);\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nforeach (int[] e in edges) {\naddEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size() {\nreturn vertices.Count;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(int val) {\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.Add(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nList&lt;int&gt; newRow = new List&lt;int&gt;(n);\nfor (int j = 0; j &lt; n; j++) {\nnewRow.Add(0);\n}\nadjMat.Add(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nforeach (List&lt;int&gt; row in adjMat) {\nrow.Add(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(int index) {\nif (index &gt;= size())\nthrow new IndexOutOfRangeException();\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.RemoveAt(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.RemoveAt(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nforeach (List&lt;int&gt; row in adjMat) {\nrow.RemoveAt(index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void addEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j)\nthrow new IndexOutOfRangeException();\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1;\nadjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void removeEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j)\nthrow new IndexOutOfRangeException();\nadjMat[i][j] = 0;\nadjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\npublic void print() {\nConsole.Write(\"\u9876\u70b9\u5217\u8868 = \");\nPrintUtil.PrintList(vertices);\nConsole.WriteLine(\"\u90bb\u63a5\u77e9\u9635 =\");\nPrintUtil.PrintMatrix(adjMat);\n}\n}\n</code></pre> graph_adjacency_matrix.swift<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nprivate var vertices: [Int] // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nprivate var adjMat: [[Int]] // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u65b9\u6cd5 */\ninit(vertices: [Int], edges: [[Int]]) {\nself.vertices = []\nadjMat = []\n// \u6dfb\u52a0\u9876\u70b9\nfor val in vertices {\naddVertex(val: val)\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor e in edges {\naddEdge(i: e[0], j: e[1])\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc size() -&gt; Int {\nvertices.count\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc addVertex(val: Int) {\nlet n = size()\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.append(val)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nlet newRow = Array(repeating: 0, count: n)\nadjMat.append(newRow)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor i in adjMat.indices {\nadjMat[i].append(0)\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nfunc removeVertex(index: Int) {\nif index &gt;= size() {\nfatalError(\"\u8d8a\u754c\")\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.remove(at: index)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.remove(at: index)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor i in adjMat.indices {\nadjMat[i].remove(at: index)\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc addEdge(i: Int, j: Int) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j {\nfatalError(\"\u8d8a\u754c\")\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1\nadjMat[j][i] = 1\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc removeEdge(i: Int, j: Int) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j {\nfatalError(\"\u8d8a\u754c\")\n}\nadjMat[i][j] = 0\nadjMat[j][i] = 0\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nfunc print() {\nSwift.print(\"\u9876\u70b9\u5217\u8868 = \", terminator: \"\")\nSwift.print(vertices)\nSwift.print(\"\u90bb\u63a5\u77e9\u9635 =\")\nPrintUtil.printMatrix(matrix: adjMat)\n}\n}\n</code></pre> graph_adjacency_matrix.zig<pre><code>\n</code></pre> graph_adjacency_matrix.dart<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nList&lt;int&gt; vertices = []; // \u9876\u70b9\u5143\u7d20\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nList&lt;List&lt;int&gt;&gt; adjMat = []; //\u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u65b9\u6cd5 */\nGraphAdjMat(List&lt;int&gt; vertices, List&lt;List&lt;int&gt;&gt; edges) {\nthis.vertices = [];\nthis.adjMat = [];\n// \u6dfb\u52a0\u9876\u70b9\nfor (int val in vertices) {\naddVertex(val);\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor (List&lt;int&gt; e in edges) {\naddEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nint size() {\nreturn vertices.length;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(int val) {\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.add(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nList&lt;int&gt; newRow = List.filled(n, 0, growable: true);\nadjMat.add(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (List&lt;int&gt; row in adjMat) {\nrow.add(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(int index) {\nif (index &gt;= size()) {\nthrow IndexError;\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.removeAt(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.removeAt(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (List&lt;int&gt; row in adjMat) {\nrow.removeAt(index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j) {\nthrow IndexError;\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1;\nadjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i &lt; 0 || j &lt; 0 || i &gt;= size() || j &gt;= size() || i == j) {\nthrow IndexError;\n}\nadjMat[i][j] = 0;\nadjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nvoid printAdjMat() {\nprint(\"\u9876\u70b9\u5217\u8868 = $vertices\");\nprint(\"\u90bb\u63a5\u77e9\u9635 = \");\nprintMatrix(adjMat);\n}\n}\n</code></pre> graph_adjacency_matrix.rs<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjMat {\n// \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\npub vertices: Vec&lt;i32&gt;,\n// \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\npub adj_mat: Vec&lt;Vec&lt;i32&gt;&gt;,\n}\nimpl GraphAdjMat {\n/* \u6784\u9020\u65b9\u6cd5 */\npub fn new(vertices: Vec&lt;i32&gt;, edges: Vec&lt;[usize; 2]&gt;) -&gt; Self {\nlet mut graph = GraphAdjMat {\nvertices: vec![],\nadj_mat: vec![],\n};\n// \u6dfb\u52a0\u9876\u70b9\nfor val in vertices {\ngraph.add_vertex(val);\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor edge in edges {\ngraph.add_edge(edge[0], edge[1])\n}\ngraph\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npub fn size(&amp;self) -&gt; usize {\nself.vertices.len()\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npub fn add_vertex(&amp;mut self, val: i32) {\nlet n = self.size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nself.vertices.push(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nself.adj_mat.push(vec![0; n]);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor row in &amp;mut self.adj_mat {\nrow.push(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\npub fn remove_vertex(&amp;mut self, index: usize) {\nif index &gt;= self.size() {\npanic!(\"index error\")\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nself.vertices.remove(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nself.adj_mat.remove(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor row in &amp;mut self.adj_mat {\nrow.remove(index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\npub fn add_edge(&amp;mut self, i: usize, j: usize) {\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i &gt;= self.size() || j &gt;= self.size() || i == j {\npanic!(\"index error\")\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nself.adj_mat[i][j] = 1;\nself.adj_mat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npub fn remove_edge(&amp;mut self, i: usize, j: usize) {\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i &gt;= self.size() || j &gt;= self.size() || i == j {\npanic!(\"index error\")\n}\nself.adj_mat[i][j] = 0;\nself.adj_mat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\npub fn print(&amp;self) {\nprintln!(\"\u9876\u70b9\u5217\u8868 = {:?}\", self.vertices);\nprintln!(\"\u90bb\u63a5\u77e9\u9635 =\");\nprintln!(\"[\");\nfor row in &amp;self.adj_mat {\nprintln!(\" {:?},\", row);\n}\nprintln!(\"]\")\n}\n}\n</code></pre>"},{"location":"chapter_graph/graph_operations/#922","title":"9.2.2. \u00a0 \u57fa\u4e8e\u90bb\u63a5\u8868\u7684\u5b9e\u73b0","text":"<p>\u8bbe\u65e0\u5411\u56fe\u7684\u9876\u70b9\u603b\u6570\u4e3a \\(n\\) \u3001\u8fb9\u603b\u6570\u4e3a \\(m\\) \uff0c\u5219\u6709\uff1a</p> <ul> <li>\u6dfb\u52a0\u8fb9\uff1a\u5728\u9876\u70b9\u5bf9\u5e94\u94fe\u8868\u7684\u672b\u5c3e\u6dfb\u52a0\u8fb9\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002\u56e0\u4e3a\u662f\u65e0\u5411\u56fe\uff0c\u6240\u4ee5\u9700\u8981\u540c\u65f6\u6dfb\u52a0\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002</li> <li>\u5220\u9664\u8fb9\uff1a\u5728\u9876\u70b9\u5bf9\u5e94\u94fe\u8868\u4e2d\u67e5\u627e\u5e76\u5220\u9664\u6307\u5b9a\u8fb9\uff0c\u4f7f\u7528 \\(O(m)\\) \u65f6\u95f4\u3002\u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u9700\u8981\u540c\u65f6\u5220\u9664\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u3002</li> <li>\u6dfb\u52a0\u9876\u70b9\uff1a\u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u94fe\u8868\uff0c\u5e76\u5c06\u65b0\u589e\u9876\u70b9\u4f5c\u4e3a\u94fe\u8868\u5934\u8282\u70b9\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002</li> <li>\u5220\u9664\u9876\u70b9\uff1a\u9700\u904d\u5386\u6574\u4e2a\u90bb\u63a5\u8868\uff0c\u5220\u9664\u5305\u542b\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u8fb9\uff0c\u4f7f\u7528 \\(O(n + m)\\) \u65f6\u95f4\u3002</li> <li>\u521d\u59cb\u5316\uff1a\u5728\u90bb\u63a5\u8868\u4e2d\u521b\u5efa \\(n\\) \u4e2a\u9876\u70b9\u548c \\(2m\\) \u6761\u8fb9\uff0c\u4f7f\u7528 \\(O(n + m)\\) \u65f6\u95f4\u3002</li> </ul> \u521d\u59cb\u5316\u90bb\u63a5\u8868\u6dfb\u52a0\u8fb9\u5220\u9664\u8fb9\u6dfb\u52a0\u9876\u70b9\u5220\u9664\u9876\u70b9 <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe\uff1a\u90bb\u63a5\u8868\u7684\u521d\u59cb\u5316\u3001\u589e\u5220\u8fb9\u3001\u589e\u5220\u9876\u70b9 </p> <p>\u4ee5\u4e0b\u662f\u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u56fe\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u7ec6\u5fc3\u7684\u540c\u5b66\u53ef\u80fd\u6ce8\u610f\u5230\uff0c\u6211\u4eec\u5728\u90bb\u63a5\u8868\u4e2d\u4f7f\u7528 <code>Vertex</code> \u8282\u70b9\u7c7b\u6765\u8868\u793a\u9876\u70b9\uff0c\u8fd9\u6837\u505a\u7684\u539f\u56e0\u6709\uff1a</p> <ul> <li>\u5982\u679c\u6211\u4eec\u9009\u62e9\u901a\u8fc7\u9876\u70b9\u503c\u6765\u533a\u5206\u4e0d\u540c\u9876\u70b9\uff0c\u90a3\u4e48\u503c\u91cd\u590d\u7684\u9876\u70b9\u5c06\u65e0\u6cd5\u88ab\u533a\u5206\u3002</li> <li>\u5982\u679c\u7c7b\u4f3c\u90bb\u63a5\u77e9\u9635\u90a3\u6837\uff0c\u4f7f\u7528\u9876\u70b9\u5217\u8868\u7d22\u5f15\u6765\u533a\u5206\u4e0d\u540c\u9876\u70b9\u3002\u90a3\u4e48\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u5220\u9664\u7d22\u5f15\u4e3a \\(i\\) \u7684\u9876\u70b9\uff0c\u5219\u9700\u8981\u904d\u5386\u6574\u4e2a\u90bb\u63a5\u8868\uff0c\u5c06\u5176\u4e2d \\(&gt; i\\) \u7684\u7d22\u5f15\u5168\u90e8\u51cf \\(1\\) \uff0c\u8fd9\u6837\u64cd\u4f5c\u6548\u7387\u8f83\u4f4e\u3002</li> <li>\u56e0\u6b64\u6211\u4eec\u8003\u8651\u5f15\u5165\u9876\u70b9\u7c7b <code>Vertex</code> \uff0c\u4f7f\u5f97\u6bcf\u4e2a\u9876\u70b9\u90fd\u662f\u552f\u4e00\u7684\u5bf9\u8c61\uff0c\u6b64\u65f6\u5220\u9664\u9876\u70b9\u65f6\u5c31\u65e0\u9700\u6539\u52a8\u5176\u4f59\u9876\u70b9\u4e86\u3002</li> </ul> JavaC++PythonGoJSTSCC#SwiftZigDartRust graph_adjacency_list.java<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nMap&lt;Vertex, List&lt;Vertex&gt;&gt; adjList;\n/* \u6784\u9020\u65b9\u6cd5 */\npublic GraphAdjList(Vertex[][] edges) {\nthis.adjList = new HashMap&lt;&gt;();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (Vertex[] edge : edges) {\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size() {\nreturn adjList.size();\n}\n/* \u6dfb\u52a0\u8fb9 */\npublic void addEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\nthrow new IllegalArgumentException();\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList.get(vet1).add(vet2);\nadjList.get(vet2).add(vet1);\n}\n/* \u5220\u9664\u8fb9 */\npublic void removeEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\nthrow new IllegalArgumentException();\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList.get(vet1).remove(vet2);\nadjList.get(vet2).remove(vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(Vertex vet) {\nif (adjList.containsKey(vet))\nreturn;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList.put(vet, new ArrayList&lt;&gt;());\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(Vertex vet) {\nif (!adjList.containsKey(vet))\nthrow new IllegalArgumentException();\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.remove(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (List&lt;Vertex&gt; list : adjList.values()) {\nlist.remove(vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npublic void print() {\nSystem.out.println(\"\u90bb\u63a5\u8868 =\");\nfor (Map.Entry&lt;Vertex, List&lt;Vertex&gt;&gt; pair : adjList.entrySet()) {\nList&lt;Integer&gt; tmp = new ArrayList&lt;&gt;();\nfor (Vertex vertex : pair.getValue())\ntmp.add(vertex.val);\nSystem.out.println(pair.getKey().val + \": \" + tmp + \",\");\n}\n}\n}\n</code></pre> graph_adjacency_list.cpp<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\npublic:\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nunordered_map&lt;Vertex *, vector&lt;Vertex *&gt;&gt; adjList;\n/* \u5728 vector \u4e2d\u5220\u9664\u6307\u5b9a\u8282\u70b9 */\nvoid remove(vector&lt;Vertex *&gt; &amp;vec, Vertex *vet) {\nfor (int i = 0; i &lt; vec.size(); i++) {\nif (vec[i] == vet) {\nvec.erase(vec.begin() + i);\nbreak;\n}\n}\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nGraphAdjList(const vector&lt;vector&lt;Vertex *&gt;&gt; &amp;edges) {\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (const vector&lt;Vertex *&gt; &amp;edge : edges) {\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nint size() {\nreturn adjList.size();\n}\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(Vertex *vet1, Vertex *vet2) {\nif (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\nthrow invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1].push_back(vet2);\nadjList[vet2].push_back(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(Vertex *vet1, Vertex *vet2) {\nif (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\nthrow invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n// \u5220\u9664\u8fb9 vet1 - vet2\nremove(adjList[vet1], vet2);\nremove(adjList[vet2], vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(Vertex *vet) {\nif (adjList.count(vet))\nreturn;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList[vet] = vector&lt;Vertex *&gt;();\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(Vertex *vet) {\nif (!adjList.count(vet))\nthrow invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.erase(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (auto &amp;adj : adjList) {\nremove(adj.second, vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nvoid print() {\ncout &lt;&lt; \"\u90bb\u63a5\u8868 =\" &lt;&lt; endl;\nfor (auto &amp;adj : adjList) {\nconst auto &amp;key = adj.first;\nconst auto &amp;vec = adj.second;\ncout &lt;&lt; key-&gt;val &lt;&lt; \": \";\nprintVector(vetsToVals(vec));\n}\n}\n};\n</code></pre> graph_adjacency_list.py<pre><code>class GraphAdjList:\n\"\"\"\u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\ndef __init__(self, edges: list[list[Vertex]]):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n# \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nself.adj_list = dict[Vertex, Vertex]()\n# \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor edge in edges:\nself.add_vertex(edge[0])\nself.add_vertex(edge[1])\nself.add_edge(edge[0], edge[1])\ndef size(self) -&gt; int:\n\"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\nreturn len(self.adj_list)\ndef add_edge(self, vet1: Vertex, vet2: Vertex):\n\"\"\"\u6dfb\u52a0\u8fb9\"\"\"\nif vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\nraise ValueError()\n# \u6dfb\u52a0\u8fb9 vet1 - vet2\nself.adj_list[vet1].append(vet2)\nself.adj_list[vet2].append(vet1)\ndef remove_edge(self, vet1: Vertex, vet2: Vertex):\n\"\"\"\u5220\u9664\u8fb9\"\"\"\nif vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\nraise ValueError()\n# \u5220\u9664\u8fb9 vet1 - vet2\nself.adj_list[vet1].remove(vet2)\nself.adj_list[vet2].remove(vet1)\ndef add_vertex(self, vet: Vertex):\n\"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\nif vet in self.adj_list:\nreturn\n# \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nself.adj_list[vet] = []\ndef remove_vertex(self, vet: Vertex):\n\"\"\"\u5220\u9664\u9876\u70b9\"\"\"\nif vet not in self.adj_list:\nraise ValueError()\n# \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nself.adj_list.pop(vet)\n# \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor vertex in self.adj_list:\nif vet in self.adj_list[vertex]:\nself.adj_list[vertex].remove(vet)\ndef print(self):\n\"\"\"\u6253\u5370\u90bb\u63a5\u8868\"\"\"\nprint(\"\u90bb\u63a5\u8868 =\")\nfor vertex in self.adj_list:\ntmp = [v.val for v in self.adj_list[vertex]]\nprint(f\"{vertex.val}: {tmp},\")\n</code></pre> graph_adjacency_list.go<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjList struct {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nadjList map[Vertex][]Vertex\n}\n/* \u6784\u9020\u51fd\u6570 */\nfunc newGraphAdjList(edges [][]Vertex) *graphAdjList {\ng := &amp;graphAdjList{\nadjList: make(map[Vertex][]Vertex),\n}\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor _, edge := range edges {\ng.addVertex(edge[0])\ng.addVertex(edge[1])\ng.addEdge(edge[0], edge[1])\n}\nreturn g\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjList) size() int {\nreturn len(g.adjList)\n}\n/* \u6dfb\u52a0\u8fb9 */\nfunc (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {\n_, ok1 := g.adjList[vet1]\n_, ok2 := g.adjList[vet2]\nif !ok1 || !ok2 || vet1 == vet2 {\npanic(\"error\")\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2, \u6dfb\u52a0\u533f\u540d struct{},\ng.adjList[vet1] = append(g.adjList[vet1], vet2)\ng.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n/* \u5220\u9664\u8fb9 */\nfunc (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {\n_, ok1 := g.adjList[vet1]\n_, ok2 := g.adjList[vet2]\nif !ok1 || !ok2 || vet1 == vet2 {\npanic(\"error\")\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\ng.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)\ng.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjList) addVertex(vet Vertex) {\n_, ok := g.adjList[vet]\nif ok {\nreturn\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\ng.adjList[vet] = make([]Vertex, 0)\n}\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjList) removeVertex(vet Vertex) {\n_, ok := g.adjList[vet]\nif !ok {\npanic(\"error\")\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\ndelete(g.adjList, vet)\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor v, list := range g.adjList {\ng.adjList[v] = DeleteSliceElms(list, vet)\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nfunc (g *graphAdjList) print() {\nvar builder strings.Builder\nfmt.Printf(\"\u90bb\u63a5\u8868 = \\n\")\nfor k, v := range g.adjList {\nbuilder.WriteString(\"\\t\\t\" + strconv.Itoa(k.Val) + \": \")\nfor _, vet := range v {\nbuilder.WriteString(strconv.Itoa(vet.Val) + \" \")\n}\nfmt.Println(builder.String())\nbuilder.Reset()\n}\n}\n</code></pre> graph_adjacency_list.js<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nadjList;\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(edges) {\nthis.adjList = new Map();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (const edge of edges) {\nthis.addVertex(edge[0]);\nthis.addVertex(edge[1]);\nthis.addEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize() {\nreturn this.adjList.size;\n}\n/* \u6dfb\u52a0\u8fb9 */\naddEdge(vet1, vet2) {\nif (\n!this.adjList.has(vet1) ||\n!this.adjList.has(vet2) ||\nvet1 === vet2\n) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).push(vet2);\nthis.adjList.get(vet2).push(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nremoveEdge(vet1, vet2) {\nif (\n!this.adjList.has(vet1) ||\n!this.adjList.has(vet2) ||\nvet1 === vet2\n) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\nthis.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(vet) {\nif (this.adjList.has(vet)) return;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nthis.adjList.set(vet, []);\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(vet) {\nif (!this.adjList.has(vet)) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nthis.adjList.delete(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (const set of this.adjList.values()) {\nconst index = set.indexOf(vet);\nif (index &gt; -1) {\nset.splice(index, 1);\n}\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nprint() {\nconsole.log('\u90bb\u63a5\u8868 =');\nfor (const [key, value] of this.adjList) {\nconst tmp = [];\nfor (const vertex of value) {\ntmp.push(vertex.val);\n}\nconsole.log(key.val + ': ' + tmp.join());\n}\n}\n}\n</code></pre> graph_adjacency_list.ts<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nadjList: Map&lt;Vertex, Vertex[]&gt;;\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(edges: Vertex[][]) {\nthis.adjList = new Map();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (const edge of edges) {\nthis.addVertex(edge[0]);\nthis.addVertex(edge[1]);\nthis.addEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize(): number {\nreturn this.adjList.size;\n}\n/* \u6dfb\u52a0\u8fb9 */\naddEdge(vet1: Vertex, vet2: Vertex): void {\nif (\n!this.adjList.has(vet1) ||\n!this.adjList.has(vet2) ||\nvet1 === vet2\n) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).push(vet2);\nthis.adjList.get(vet2).push(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nremoveEdge(vet1: Vertex, vet2: Vertex): void {\nif (\n!this.adjList.has(vet1) ||\n!this.adjList.has(vet2) ||\nvet1 === vet2\n) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\nthis.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(vet: Vertex): void {\nif (this.adjList.has(vet)) return;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nthis.adjList.set(vet, []);\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(vet: Vertex): void {\nif (!this.adjList.has(vet)) {\nthrow new Error('Illegal Argument Exception');\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nthis.adjList.delete(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (const set of this.adjList.values()) {\nconst index: number = set.indexOf(vet);\nif (index &gt; -1) {\nset.splice(index, 1);\n}\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nprint(): void {\nconsole.log('\u90bb\u63a5\u8868 =');\nfor (const [key, value] of this.adjList.entries()) {\nconst tmp = [];\nfor (const vertex of value) {\ntmp.push(vertex.val);\n}\nconsole.log(key.val + ': ' + tmp.join());\n}\n}\n}\n</code></pre> graph_adjacency_list.c<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u94fe\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u7ed3\u6784 */\nstruct graphAdjList {\nVertex **verticesList; // \u90bb\u63a5\u8868\nunsigned int size; // \u9876\u70b9\u6570\u91cf\nunsigned int capacity; // \u9876\u70b9\u5bb9\u91cf\n};\ntypedef struct graphAdjList graphAdjList;\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(graphAdjList *t, int i, int j) {\n// \u8d8a\u754c\u68c0\u67e5\nif (i &lt; 0 || j &lt; 0 || i == j || i &gt;= t-&gt;size || j &gt;= t-&gt;size) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nreturn;\n}\n// \u67e5\u627e\u6b32\u6dfb\u52a0\u8fb9\u7684\u9876\u70b9 vet1 - vet2\nVertex *vet1 = t-&gt;verticesList[i];\nVertex *vet2 = t-&gt;verticesList[j];\n// \u8fde\u63a5\u9876\u70b9 vet1 - vet2\npushBack(vet1-&gt;linked, vet2);\npushBack(vet2-&gt;linked, vet1);\n}\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(graphAdjList *t, int i, int j) {\n// \u8d8a\u754c\u68c0\u67e5\nif (i &lt; 0 || j &lt; 0 || i == j || i &gt;= t-&gt;size || j &gt;= t-&gt;size) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nreturn;\n}\n// \u67e5\u627e\u6b32\u5220\u9664\u8fb9\u7684\u9876\u70b9 vet1 - vet2\nVertex *vet1 = t-&gt;verticesList[i];\nVertex *vet2 = t-&gt;verticesList[j];\n// \u79fb\u9664\u5f85\u5220\u9664\u8fb9 vet1 - vet2\nremoveLink(vet1-&gt;linked, vet2);\nremoveLink(vet2-&gt;linked, vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(graphAdjList *t, int val) {\n// \u82e5\u5927\u5c0f\u8d85\u8fc7\u5bb9\u91cf\uff0c\u5219\u6269\u5bb9\nif (t-&gt;size &gt;= t-&gt;capacity) {\nVertex **tempList = (Vertex **)malloc(sizeof(Vertex *) * 2 * t-&gt;capacity);\nmemcpy(tempList, t-&gt;verticesList, sizeof(Vertex *) * t-&gt;size);\nfree(t-&gt;verticesList); // \u91ca\u653e\u539f\u90bb\u63a5\u8868\u5185\u5b58\nt-&gt;verticesList = tempList; // \u6307\u5411\u65b0\u90bb\u63a5\u8868\nt-&gt;capacity = t-&gt;capacity * 2; // \u5bb9\u91cf\u6269\u5927\u81f32\u500d\n}\n// \u7533\u8bf7\u65b0\u9876\u70b9\u5185\u5b58\u5e76\u5c06\u65b0\u9876\u70b9\u5730\u5740\u5b58\u5165\u9876\u70b9\u5217\u8868\nVertex *newV = newVertex(val); // \u5efa\u7acb\u65b0\u9876\u70b9\nnewV-&gt;pos = t-&gt;size; // \u4e3a\u65b0\u9876\u70b9\u6807\u8bb0\u4e0b\u6807\nnewV-&gt;linked = newLinklist(newV); // \u4e3a\u65b0\u9876\u70b9\u5efa\u7acb\u94fe\u8868\nt-&gt;verticesList[t-&gt;size] = newV; // \u5c06\u65b0\u9876\u70b9\u52a0\u5165\u90bb\u63a5\u8868\nt-&gt;size++;\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(graphAdjList *t, unsigned int index) {\n// \u8d8a\u754c\u68c0\u67e5\nif (index &lt; 0 || index &gt;= t-&gt;size) {\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nexit(1);\n}\nVertex *vet = t-&gt;verticesList[index]; // \u67e5\u627e\u5f85\u5220\u8282\u70b9\nif (vet == 0) { // \u82e5\u4e0d\u5b58\u5728\u8be5\u8282\u70b9\uff0c\u5219\u8fd4\u56de\nprintf(\"index is:%d\\n\", index);\nprintf(\"Out of range in %s:%d\\n\", __FILE__, __LINE__);\nreturn;\n}\n// \u904d\u5386\u5f85\u5220\u9664\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5c06\u6240\u6709\u4e0e\u5f85\u5220\u9664\u7ed3\u70b9\u6709\u5173\u7684\u8fb9\u5220\u9664\nNode *temp = vet-&gt;linked-&gt;head-&gt;next;\nwhile (temp != 0) {\nremoveLink(temp-&gt;val-&gt;linked, vet); // \u5220\u9664\u4e0e\u8be5\u9876\u70b9\u6709\u5173\u7684\u8fb9\ntemp = temp-&gt;next; }\n// \u5c06\u9876\u70b9\u524d\u79fb\nfor (int i = index; i &lt; t-&gt;size - 1; i++) {\nt-&gt;verticesList[i] = t-&gt;verticesList[i + 1]; // \u9876\u70b9\u524d\u79fb\nt-&gt;verticesList[i]-&gt;pos--; // \u6240\u6709\u524d\u79fb\u7684\u9876\u70b9\u7d22\u5f15\u503c\u51cf1\n}\nt-&gt;verticesList[t-&gt;size - 1] = 0; // \u5c06\u88ab\u5220\u9664\u9876\u70b9\u7684\u4f4d\u7f6e\u7f6e 0\nt-&gt;size--;\n//\u91ca\u653e\u88ab\u5220\u9664\u9876\u70b9\u7684\u5185\u5b58\nfreeVertex(vet);\n}\n/* \u6253\u5370\u9876\u70b9\u4e0e\u90bb\u63a5\u77e9\u9635 */\nvoid printGraph(graphAdjList *t) {\nprintf(\"\u90bb\u63a5\u8868 =\\n\");\nfor (int i = 0; i &lt; t-&gt;size; i++) {\nNode *n = t-&gt;verticesList[i]-&gt;linked-&gt;head-&gt;next;\nprintf(\"%d: [\", t-&gt;verticesList[i]-&gt;val);\nwhile (n != 0) {\nif (n-&gt;next != 0) {\nprintf(\"%d, \", n-&gt;val-&gt;val);\n} else {\nprintf(\"%d\", n-&gt;val-&gt;val);\n}\nn = n-&gt;next;\n}\nprintf(\"]\\n\");\n}\n}\n/* \u6784\u9020\u51fd\u6570 */\ngraphAdjList *newGraphAdjList(unsigned int verticesCapacity) {\n// \u7533\u8bf7\u5185\u5b58\ngraphAdjList *newGraph = (graphAdjList *)malloc(sizeof(graphAdjList));\n// \u5efa\u7acb\u9876\u70b9\u8868\u5e76\u5206\u914d\u5185\u5b58\nnewGraph-&gt;verticesList = (Vertex **)malloc(sizeof(Vertex *) * verticesCapacity); // \u4e3a\u9876\u70b9\u5217\u8868\u5206\u914d\u5185\u5b58\nmemset(newGraph-&gt;verticesList, 0, sizeof(Vertex *) * verticesCapacity); // \u9876\u70b9\u5217\u8868\u7f6e 0\nnewGraph-&gt;size = 0; // \u521d\u59cb\u5316\u9876\u70b9\u6570\u91cf\nnewGraph-&gt;capacity = verticesCapacity; // \u521d\u59cb\u5316\u9876\u70b9\u5bb9\u91cf\n// \u8fd4\u56de\u56fe\u6307\u9488\nreturn newGraph; }\n</code></pre> graph_adjacency_list.cs<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\npublic Dictionary&lt;Vertex, List&lt;Vertex&gt;&gt; adjList;\n/* \u6784\u9020\u51fd\u6570 */\npublic GraphAdjList(Vertex[][] edges) {\nthis.adjList = new Dictionary&lt;Vertex, List&lt;Vertex&gt;&gt;();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nforeach (Vertex[] edge in edges) {\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size() {\nreturn adjList.Count;\n}\n/* \u6dfb\u52a0\u8fb9 */\npublic void addEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\nthrow new InvalidOperationException();\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1].Add(vet2);\nadjList[vet2].Add(vet1);\n}\n/* \u5220\u9664\u8fb9 */\npublic void removeEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\nthrow new InvalidOperationException();\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList[vet1].Remove(vet2);\nadjList[vet2].Remove(vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(Vertex vet) {\nif (adjList.ContainsKey(vet))\nreturn;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList.Add(vet, new List&lt;Vertex&gt;());\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(Vertex vet) {\nif (!adjList.ContainsKey(vet))\nthrow new InvalidOperationException();\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.Remove(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nforeach (List&lt;Vertex&gt; list in adjList.Values) {\nlist.Remove(vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npublic void print() {\nConsole.WriteLine(\"\u90bb\u63a5\u8868 =\");\nforeach (KeyValuePair&lt;Vertex, List&lt;Vertex&gt;&gt; pair in adjList) {\nList&lt;int&gt; tmp = new List&lt;int&gt;();\nforeach (Vertex vertex in pair.Value)\ntmp.Add(vertex.val);\nConsole.WriteLine(pair.Key.val + \": [\" + string.Join(\", \", tmp) + \"],\");\n}\n}\n}\n</code></pre> graph_adjacency_list.swift<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\npublic private(set) var adjList: [Vertex: [Vertex]]\n/* \u6784\u9020\u65b9\u6cd5 */\npublic init(edges: [[Vertex]]) {\nadjList = [:]\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor edge in edges {\naddVertex(vet: edge[0])\naddVertex(vet: edge[1])\naddEdge(vet1: edge[0], vet2: edge[1])\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic func size() -&gt; Int {\nadjList.count\n}\n/* \u6dfb\u52a0\u8fb9 */\npublic func addEdge(vet1: Vertex, vet2: Vertex) {\nif adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\nfatalError(\"\u53c2\u6570\u9519\u8bef\")\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1]?.append(vet2)\nadjList[vet2]?.append(vet1)\n}\n/* \u5220\u9664\u8fb9 */\npublic func removeEdge(vet1: Vertex, vet2: Vertex) {\nif adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\nfatalError(\"\u53c2\u6570\u9519\u8bef\")\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList[vet1]?.removeAll(where: { $0 == vet2 })\nadjList[vet2]?.removeAll(where: { $0 == vet1 })\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic func addVertex(vet: Vertex) {\nif adjList[vet] != nil {\nreturn\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList[vet] = []\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic func removeVertex(vet: Vertex) {\nif adjList[vet] == nil {\nfatalError(\"\u53c2\u6570\u9519\u8bef\")\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.removeValue(forKey: vet)\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor key in adjList.keys {\nadjList[key]?.removeAll(where: { $0 == vet })\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npublic func print() {\nSwift.print(\"\u90bb\u63a5\u8868 =\")\nfor pair in adjList {\nvar tmp: [Int] = []\nfor vertex in pair.value {\ntmp.append(vertex.val)\n}\nSwift.print(\"\\(pair.key.val): \\(tmp),\")\n}\n}\n}\n</code></pre> graph_adjacency_list.zig<pre><code>[class]{GraphAdjList}-[func]{}\n</code></pre> graph_adjacency_list.dart<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nMap&lt;Vertex, List&lt;Vertex&gt;&gt; adjList = {};\n/* \u6784\u9020\u65b9\u6cd5 */\nGraphAdjList(List&lt;List&lt;Vertex&gt;&gt; edges) {\nfor (List&lt;Vertex&gt; edge in edges) {\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nint size() {\nreturn adjList.length;\n}\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.containsKey(vet1) ||\n!adjList.containsKey(vet2) ||\nvet1 == vet2) {\nthrow ArgumentError;\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1]!.add(vet2);\nadjList[vet2]!.add(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.containsKey(vet1) ||\n!adjList.containsKey(vet2) ||\nvet1 == vet2) {\nthrow ArgumentError;\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList[vet1]!.remove(vet2);\nadjList[vet2]!.remove(vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(Vertex vet) {\nif (adjList.containsKey(vet)) return;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList[vet] = [];\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(Vertex vet) {\nif (!adjList.containsKey(vet)) {\nthrow ArgumentError;\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.remove(vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nadjList.forEach((key, value) {\nvalue.remove(vet);\n});\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nvoid printAdjList() {\nprint(\"\u90bb\u63a5\u8868 =\");\nadjList.forEach((key, value) {\nList&lt;int&gt; tmp = [];\nfor (Vertex vertex in value) {\ntmp.add(vertex.val);\n}\nprint(\"${key.val}: $tmp,\");\n});\n}\n}\n</code></pre> graph_adjacency_list.rs<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\npub adj_list: HashMap&lt;Vertex, Vec&lt;Vertex&gt;&gt;,\n}\nimpl GraphAdjList {\n/* \u6784\u9020\u65b9\u6cd5 */\npub fn new(edges: Vec&lt;[Vertex; 2]&gt;) -&gt; Self {\nlet mut graph = GraphAdjList {\nadj_list: HashMap::new(),\n};\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor edge in edges {\ngraph.add_vertex(edge[0]);\ngraph.add_vertex(edge[1]);\ngraph.add_edge(edge[0], edge[1]);\n}\ngraph\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n#[allow(unused)]\npub fn size(&amp;self) -&gt; usize {\nself.adj_list.len()\n}\n/* \u6dfb\u52a0\u8fb9 */\npub fn add_edge(&amp;mut self, vet1: Vertex, vet2: Vertex) {\nif !self.adj_list.contains_key(&amp;vet1) || !self.adj_list.contains_key(&amp;vet2) || vet1 == vet2\n{\npanic!(\"value error\");\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nself.adj_list.get_mut(&amp;vet1).unwrap().push(vet2);\nself.adj_list.get_mut(&amp;vet2).unwrap().push(vet1);\n}\n/* \u5220\u9664\u8fb9 */\n#[allow(unused)]\npub fn remove_edge(&amp;mut self, vet1: Vertex, vet2: Vertex) {\nif !self.adj_list.contains_key(&amp;vet1) || !self.adj_list.contains_key(&amp;vet2) || vet1 == vet2\n{\npanic!(\"value error\");\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nself.adj_list\n.get_mut(&amp;vet1)\n.unwrap()\n.retain(|&amp;vet| vet != vet2);\nself.adj_list\n.get_mut(&amp;vet2)\n.unwrap()\n.retain(|&amp;vet| vet != vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npub fn add_vertex(&amp;mut self, vet: Vertex) {\nif self.adj_list.contains_key(&amp;vet) {\nreturn;\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nself.adj_list.insert(vet, vec![]);\n}\n/* \u5220\u9664\u9876\u70b9 */\n#[allow(unused)]\npub fn remove_vertex(&amp;mut self, vet: Vertex) {\nif !self.adj_list.contains_key(&amp;vet) {\npanic!(\"value error\");\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nself.adj_list.remove(&amp;vet);\n// \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor list in self.adj_list.values_mut() {\nlist.retain(|&amp;v| v != vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npub fn print(&amp;self) {\nprintln!(\"\u90bb\u63a5\u8868 =\");\nfor (vertex, list) in &amp;self.adj_list {\nlet list = list.iter().map(|vertex| vertex.val).collect::&lt;Vec&lt;i32&gt;&gt;();\nprintln!(\"{}: {:?},\", vertex.val, list);\n}\n}\n}\n</code></pre>"},{"location":"chapter_graph/graph_operations/#923","title":"9.2.3. \u00a0 \u6548\u7387\u5bf9\u6bd4","text":"<p>\u8bbe\u56fe\u4e2d\u5171\u6709 \\(n\\) \u4e2a\u9876\u70b9\u548c \\(m\\) \u6761\u8fb9\uff0c\u4e0b\u8868\u4e3a\u90bb\u63a5\u77e9\u9635\u548c\u90bb\u63a5\u8868\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u6548\u7387\u5bf9\u6bd4\u3002</p> <p> \u8868\uff1a\u90bb\u63a5\u77e9\u9635\u4e0e\u90bb\u63a5\u8868\u5bf9\u6bd4 </p> \u90bb\u63a5\u77e9\u9635 \u90bb\u63a5\u8868\uff08\u94fe\u8868\uff09 \u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09 \u5224\u65ad\u662f\u5426\u90bb\u63a5 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u6dfb\u52a0\u8fb9 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u8fb9 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u6dfb\u52a0\u9876\u70b9 \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u9876\u70b9 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) \u5185\u5b58\u7a7a\u95f4\u5360\u7528 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\) <p>\u89c2\u5bdf\u4e0a\u8868\uff0c\u4f3c\u4e4e\u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09\u7684\u65f6\u95f4\u4e0e\u7a7a\u95f4\u6548\u7387\u6700\u4f18\u3002\u4f46\u5b9e\u9645\u4e0a\uff0c\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u64cd\u4f5c\u8fb9\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u53ea\u9700\u8981\u4e00\u6b21\u6570\u7ec4\u8bbf\u95ee\u6216\u8d4b\u503c\u64cd\u4f5c\u5373\u53ef\u3002\u7efc\u5408\u6765\u770b\uff0c\u90bb\u63a5\u77e9\u9635\u4f53\u73b0\u4e86\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u7684\u539f\u5219\uff0c\u800c\u90bb\u63a5\u8868\u4f53\u73b0\u4e86\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u7684\u539f\u5219\u3002</p>"},{"location":"chapter_graph/graph_traversal/","title":"9.3. \u00a0 \u56fe\u7684\u904d\u5386","text":"<p>\u56fe\u4e0e\u6811\u7684\u5173\u7cfb</p> <p>\u6811\u4ee3\u8868\u7684\u662f\u201c\u4e00\u5bf9\u591a\u201d\u7684\u5173\u7cfb\uff0c\u800c\u56fe\u5219\u5177\u6709\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\uff0c\u53ef\u4ee5\u8868\u793a\u4efb\u610f\u7684\u201c\u591a\u5bf9\u591a\u201d\u5173\u7cfb\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u6811\u770b\u4f5c\u662f\u56fe\u7684\u4e00\u79cd\u7279\u4f8b\u3002\u663e\u7136\uff0c\u6811\u7684\u904d\u5386\u64cd\u4f5c\u4e5f\u662f\u56fe\u7684\u904d\u5386\u64cd\u4f5c\u7684\u4e00\u79cd\u7279\u4f8b\uff0c\u5efa\u8bae\u4f60\u5728\u5b66\u4e60\u672c\u7ae0\u8282\u65f6\u878d\u4f1a\u8d2f\u901a\u4e24\u8005\u7684\u6982\u5ff5\u4e0e\u5b9e\u73b0\u65b9\u6cd5\u3002</p> <p>\u300c\u56fe\u300d\u548c\u300c\u6811\u300d\u90fd\u662f\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u90fd\u9700\u8981\u4f7f\u7528\u300c\u641c\u7d22\u7b97\u6cd5\u300d\u6765\u5b9e\u73b0\u904d\u5386\u64cd\u4f5c\u3002</p> <p>\u4e0e\u6811\u7c7b\u4f3c\uff0c\u56fe\u7684\u904d\u5386\u65b9\u5f0f\u4e5f\u53ef\u5206\u4e3a\u4e24\u79cd\uff0c\u5373\u300c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 Breadth-First Traversal\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u904d\u5386 Depth-First Traversal\u300d\uff0c\u4e5f\u79f0\u4e3a\u300c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 Breadth-First Search\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22 Depth-First Search\u300d\uff0c\u7b80\u79f0 BFS \u548c DFS\u3002</p>"},{"location":"chapter_graph/graph_traversal/#931","title":"9.3.1. \u00a0 \u5e7f\u5ea6\u4f18\u5148\u904d\u5386","text":"<p>\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u7531\u8fd1\u53ca\u8fdc\u7684\u904d\u5386\u65b9\u5f0f\uff0c\u4ece\u8ddd\u79bb\u6700\u8fd1\u7684\u9876\u70b9\u5f00\u59cb\u8bbf\u95ee\uff0c\u5e76\u4e00\u5c42\u5c42\u5411\u5916\u6269\u5f20\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u5148\u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u7136\u540e\u904d\u5386\u4e0b\u4e00\u4e2a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u8bbf\u95ee\u5b8c\u6bd5\u3002</p> <p></p> <p> \u56fe\uff1a\u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 </p>"},{"location":"chapter_graph/graph_traversal/#_1","title":"\u7b97\u6cd5\u5b9e\u73b0","text":"<p>BFS \u901a\u5e38\u501f\u52a9\u300c\u961f\u5217\u300d\u6765\u5b9e\u73b0\u3002\u961f\u5217\u5177\u6709\u201c\u5148\u5165\u5148\u51fa\u201d\u7684\u6027\u8d28\uff0c\u8fd9\u4e0e BFS \u7684\u201c\u7531\u8fd1\u53ca\u8fdc\u201d\u7684\u601d\u60f3\u5f02\u66f2\u540c\u5de5\u3002</p> <ol> <li>\u5c06\u904d\u5386\u8d77\u59cb\u9876\u70b9 <code>startVet</code> \u52a0\u5165\u961f\u5217\uff0c\u5e76\u5f00\u542f\u5faa\u73af\u3002</li> <li>\u5728\u5faa\u73af\u7684\u6bcf\u8f6e\u8fed\u4ee3\u4e2d\uff0c\u5f39\u51fa\u961f\u9996\u9876\u70b9\u5e76\u8bb0\u5f55\u8bbf\u95ee\uff0c\u7136\u540e\u5c06\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\u52a0\u5165\u5230\u961f\u5217\u5c3e\u90e8\u3002</li> <li>\u5faa\u73af\u6b65\u9aa4 <code>2.</code> \uff0c\u76f4\u5230\u6240\u6709\u9876\u70b9\u88ab\u8bbf\u95ee\u5b8c\u6210\u540e\u7ed3\u675f\u3002</li> </ol> <p>\u4e3a\u4e86\u9632\u6b62\u91cd\u590d\u904d\u5386\u9876\u70b9\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 <code>visited</code> \u6765\u8bb0\u5f55\u54ea\u4e9b\u8282\u70b9\u5df2\u88ab\u8bbf\u95ee\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust graph_bfs.java<pre><code>/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList&lt;Vertex&gt; graphBFS(GraphAdjList graph, Vertex startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nList&lt;Vertex&gt; res = new ArrayList&lt;&gt;();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nSet&lt;Vertex&gt; visited = new HashSet&lt;&gt;();\nvisited.add(startVet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nQueue&lt;Vertex&gt; que = new LinkedList&lt;&gt;();\nque.offer(startVet);\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (!que.isEmpty()) {\nVertex vet = que.poll(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex adjVet : graph.adjList.get(vet)) {\nif (visited.contains(adjVet))\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nque.offer(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n</code></pre> graph_bfs.cpp<pre><code>/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvector&lt;Vertex *&gt; graphBFS(GraphAdjList &amp;graph, Vertex *startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nvector&lt;Vertex *&gt; res;\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nunordered_set&lt;Vertex *&gt; visited = {startVet};\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nqueue&lt;Vertex *&gt; que;\nque.push(startVet);\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (!que.empty()) {\nVertex *vet = que.front();\nque.pop(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.push_back(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (auto adjVet : graph.adjList[vet]) {\nif (visited.count(adjVet))\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nque.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.emplace(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n</code></pre> graph_bfs.py<pre><code>def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -&gt; list[Vertex]:\n\"\"\"\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS\"\"\"\n# \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n# \u9876\u70b9\u904d\u5386\u5e8f\u5217\nres = []\n# \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvisited = set[Vertex]([start_vet])\n# \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nque = deque[Vertex]([start_vet])\n# \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile len(que) &gt; 0:\nvet = que.popleft() # \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.append(vet) # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n# \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adj_vet in graph.adj_list[vet]:\nif adj_vet in visited:\ncontinue # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nque.append(adj_vet) # \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adj_vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n# \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res\n</code></pre> graph_bfs.go<pre><code>/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(g *graphAdjList, startVet Vertex) []Vertex {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nres := make([]Vertex, 0)\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvisited := make(map[Vertex]struct{})\nvisited[startVet] = struct{}{}\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS, \u4f7f\u7528\u5207\u7247\u6a21\u62df\u961f\u5217\nqueue := make([]Vertex, 0)\nqueue = append(queue, startVet)\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nfor len(queue) &gt; 0 {\n// \u961f\u9996\u9876\u70b9\u51fa\u961f\nvet := queue[0]\nqueue = queue[1:]\n// \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nres = append(res, vet)\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor _, adjVet := range g.adjList[vet] {\n_, isExist := visited[adjVet]\n// \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nif !isExist {\nqueue = append(queue, adjVet)\nvisited[adjVet] = struct{}{}\n}\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res\n}\n</code></pre> graph_bfs.js<pre><code>/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph, startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nconst res = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited = new Set();\nvisited.add(startVet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nconst que = [startVet];\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que.length) {\nconst vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet) ?? []) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n</code></pre> graph_bfs.ts<pre><code>/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nconst res: Vertex[] = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited: Set&lt;Vertex&gt; = new Set();\nvisited.add(startVet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nconst que = [startVet];\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que.length) {\nconst vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet) ?? []) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n</code></pre> graph_bfs.c<pre><code>/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nVertex **graphBFS(graphAdjList *t, Vertex *startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nVertex **res = (Vertex **)malloc(sizeof(Vertex *) * t-&gt;size);\nmemset(res, 0, sizeof(Vertex *) * t-&gt;size);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nqueue *que = newQueue(t-&gt;size);\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nhashTable *visited = newHash(t-&gt;size);\nint resIndex = 0;\nqueuePush(que, startVet); // \u5c06\u7b2c\u4e00\u4e2a\u5143\u7d20\u5165\u961f\nhashMark(visited, startVet-&gt;pos); // \u6807\u8bb0\u7b2c\u4e00\u4e2a\u5165\u961f\u7684\u9876\u70b9\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que-&gt;head &lt; que-&gt;tail) {\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u8fb9\u94fe\u8868\uff0c\u5c06\u6240\u6709\u4e0e\u8be5\u9876\u70b9\u6709\u8fde\u63a5\u7684\uff0c\u5e76\u4e14\u672a\u88ab\u6807\u8bb0\u7684\u9876\u70b9\u5165\u961f\nNode *n = queueTop(que)-&gt;linked-&gt;head-&gt;next;\nwhile (n != 0) {\n// \u67e5\u8be2\u54c8\u5e0c\u8868\uff0c\u82e5\u8be5\u7d22\u5f15\u7684\u9876\u70b9\u5df2\u5165\u961f\uff0c\u5219\u8df3\u8fc7\uff0c\u5426\u5219\u5165\u961f\u5e76\u6807\u8bb0\nif (hashQuery(visited, n-&gt;val-&gt;pos) == 1) {\nn = n-&gt;next;\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nqueuePush(que, n-&gt;val); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nhashMark(visited, n-&gt;val-&gt;pos); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n// \u961f\u9996\u5143\u7d20\u5b58\u5165\u6570\u7ec4\nres[resIndex] = queueTop(que); // \u961f\u9996\u9876\u70b9\u52a0\u5165\u9876\u70b9\u904d\u5386\u5e8f\u5217\nresIndex++;\nqueuePop(que); // \u961f\u9996\u5143\u7d20\u51fa\u961f\n}\n// \u91ca\u653e\u5185\u5b58\nfreeQueue(que);\nfreeHash(visited);\nresIndex = 0;\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n</code></pre> graph_bfs.cs<pre><code>/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList&lt;Vertex&gt; graphBFS(GraphAdjList graph, Vertex startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nList&lt;Vertex&gt; res = new List&lt;Vertex&gt;();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nHashSet&lt;Vertex&gt; visited = new HashSet&lt;Vertex&gt;() { startVet };\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nQueue&lt;Vertex&gt; que = new Queue&lt;Vertex&gt;();\nque.Enqueue(startVet);\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que.Count &gt; 0) {\nVertex vet = que.Dequeue(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.Add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nforeach (Vertex adjVet in graph.adjList[vet]) {\nif (visited.Contains(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.Enqueue(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.Add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n</code></pre> graph_bfs.swift<pre><code>/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(graph: GraphAdjList, startVet: Vertex) -&gt; [Vertex] {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nvar res: [Vertex] = []\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvar visited: Set&lt;Vertex&gt; = [startVet]\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nvar que: [Vertex] = [startVet]\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile !que.isEmpty {\nlet vet = que.removeFirst() // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adjVet in graph.adjList[vet] ?? [] {\nif visited.contains(adjVet) {\ncontinue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.append(adjVet) // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.insert(adjVet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res\n}\n</code></pre> graph_bfs.zig<pre><code>[class]{}-[func]{graphBFS}\n</code></pre> graph_bfs.dart<pre><code>/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\nList&lt;Vertex&gt; graphBFS(GraphAdjList graph, Vertex startVet) {\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nList&lt;Vertex&gt; res = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nSet&lt;Vertex&gt; visited = {};\nvisited.add(startVet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nQueue&lt;Vertex&gt; que = Queue();\nque.add(startVet);\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que.isNotEmpty) {\nVertex vet = que.removeFirst(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex adjVet in graph.adjList[vet]!) {\nif (visited.contains(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.add(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n</code></pre> graph_bfs.rs<pre><code>/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -&gt; Vec&lt;Vertex&gt; {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nlet mut res = vec![];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nlet mut visited = HashSet::new();\nvisited.insert(start_vet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nlet mut que = VecDeque::new();\nque.push_back(start_vet);\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile !que.is_empty() {\nlet vet = que.pop_front().unwrap(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nif let Some(adj_vets) = graph.adj_list.get(&amp;vet) {\nfor &amp;adj_vet in adj_vets {\nif visited.contains(&amp;adj_vet) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.push_back(adj_vet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.insert(adj_vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nres\n}\n</code></pre> <p>\u4ee3\u7801\u76f8\u5bf9\u62bd\u8c61\uff0c\u5efa\u8bae\u5bf9\u7167\u4ee5\u4e0b\u52a8\u753b\u56fe\u793a\u6765\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> \u56fe\uff1a\u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u6b65\u9aa4 </p> <p>\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f</p> <p>\u4e0d\u552f\u4e00\u3002\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u53ea\u8981\u6c42\u6309\u201c\u7531\u8fd1\u53ca\u8fdc\u201d\u7684\u987a\u5e8f\u904d\u5386\uff0c\u800c\u591a\u4e2a\u76f8\u540c\u8ddd\u79bb\u7684\u9876\u70b9\u7684\u904d\u5386\u987a\u5e8f\u662f\u5141\u8bb8\u88ab\u4efb\u610f\u6253\u4e71\u7684\u3002\u4ee5\u4e0a\u56fe\u4e3a\u4f8b\uff0c\u9876\u70b9 \\(1\\) , \\(3\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u3001\u9876\u70b9 \\(2\\) , \\(4\\) , \\(6\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u4e5f\u53ef\u4ee5\u4efb\u610f\u4ea4\u6362\u3002</p>"},{"location":"chapter_graph/graph_traversal/#_2","title":"\u590d\u6742\u5ea6\u5206\u6790","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a \u6240\u6709\u9876\u70b9\u90fd\u4f1a\u5165\u961f\u5e76\u51fa\u961f\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u5728\u904d\u5386\u90bb\u63a5\u9876\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7531\u4e8e\u662f\u65e0\u5411\u56fe\uff0c\u56e0\u6b64\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a \u5217\u8868 <code>res</code> \uff0c\u54c8\u5e0c\u8868 <code>visited</code> \uff0c\u961f\u5217 <code>que</code> \u4e2d\u7684\u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002</p>"},{"location":"chapter_graph/graph_traversal/#932","title":"9.3.2. \u00a0 \u6df1\u5ea6\u4f18\u5148\u904d\u5386","text":"<p>\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u4f18\u5148\u8d70\u5230\u5e95\u3001\u65e0\u8def\u53ef\u8d70\u518d\u56de\u5934\u7684\u904d\u5386\u65b9\u5f0f\u3002\u5177\u4f53\u5730\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u8bbf\u95ee\u5f53\u524d\u9876\u70b9\u7684\u67d0\u4e2a\u90bb\u63a5\u9876\u70b9\uff0c\u76f4\u5230\u8d70\u5230\u5c3d\u5934\u65f6\u8fd4\u56de\uff0c\u518d\u7ee7\u7eed\u8d70\u5230\u5c3d\u5934\u5e76\u8fd4\u56de\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u904d\u5386\u5b8c\u6210\u3002</p> <p></p> <p> \u56fe\uff1a\u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386 </p>"},{"location":"chapter_graph/graph_traversal/#_3","title":"\u7b97\u6cd5\u5b9e\u73b0","text":"<p>\u8fd9\u79cd\u201c\u8d70\u5230\u5c3d\u5934 + \u56de\u6eaf\u201d\u7684\u7b97\u6cd5\u5f62\u5f0f\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002\u4e0e BFS \u7c7b\u4f3c\uff0c\u5728 DFS \u4e2d\u6211\u4eec\u4e5f\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 <code>visited</code> \u6765\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u8bbf\u95ee\u9876\u70b9\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust graph_dfs.java<pre><code>/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList graph, Set&lt;Vertex&gt; visited, List&lt;Vertex&gt; res, Vertex vet) {\nres.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex adjVet : graph.adjList.get(vet)) {\nif (visited.contains(adjVet))\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList&lt;Vertex&gt; graphDFS(GraphAdjList graph, Vertex startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nList&lt;Vertex&gt; res = new ArrayList&lt;&gt;();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nSet&lt;Vertex&gt; visited = new HashSet&lt;&gt;();\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n</code></pre> graph_dfs.cpp<pre><code>/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList &amp;graph, unordered_set&lt;Vertex *&gt; &amp;visited, vector&lt;Vertex *&gt; &amp;res, Vertex *vet) {\nres.push_back(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.emplace(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex *adjVet : graph.adjList[vet]) {\nif (visited.count(adjVet))\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvector&lt;Vertex *&gt; graphDFS(GraphAdjList &amp;graph, Vertex *startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nvector&lt;Vertex *&gt; res;\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nunordered_set&lt;Vertex *&gt; visited;\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n</code></pre> graph_dfs.py<pre><code>def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n\"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570\"\"\"\nres.append(vet) # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n# \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adjVet in graph.adj_list[vet]:\nif adjVet in visited:\ncontinue # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n# \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet)\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -&gt; list[Vertex]:\n\"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS\"\"\"\n# \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n# \u9876\u70b9\u904d\u5386\u5e8f\u5217\nres = []\n# \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvisited = set[Vertex]()\ndfs(graph, visited, res, start_vet)\nreturn res\n</code></pre> graph_dfs.go<pre><code>/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunc dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {\n// append \u64cd\u4f5c\u4f1a\u8fd4\u56de\u65b0\u7684\u7684\u5f15\u7528\uff0c\u5fc5\u987b\u8ba9\u539f\u5f15\u7528\u91cd\u65b0\u8d4b\u503c\u4e3a\u65b0slice\u7684\u5f15\u7528\n*res = append(*res, vet)\nvisited[vet] = struct{}{}\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor _, adjVet := range g.adjList[vet] {\n_, isExist := visited[adjVet]\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\nif !isExist {\ndfs(g, visited, res, adjVet)\n}\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(g *graphAdjList, startVet Vertex) []Vertex {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nres := make([]Vertex, 0)\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvisited := make(map[Vertex]struct{})\ndfs(g, visited, &amp;res, startVet)\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res\n}\n</code></pre> graph_dfs.js<pre><code>/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction dfs(graph, visited, res, vet) {\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet)) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph, startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nconst res = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited = new Set();\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n</code></pre> graph_dfs.ts<pre><code>/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunction dfs(\ngraph: GraphAdjList,\nvisited: Set&lt;Vertex&gt;,\nres: Vertex[],\nvet: Vertex\n): void {\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet)) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nconst res: Vertex[] = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited: Set&lt;Vertex&gt; = new Set();\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n</code></pre> graph_dfs.c<pre><code>/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nint resIndex = 0;\nvoid dfs(graphAdjList *graph, hashTable *visited, Vertex *vet, Vertex **res) {\nif (hashQuery(visited, vet-&gt;pos) == 1) {\nreturn; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nhashMark(visited, vet-&gt;pos); // \u6807\u8bb0\u9876\u70b9\u5e76\u5c06\u9876\u70b9\u5b58\u5165\u6570\u7ec4\nres[resIndex] = vet; // \u5c06\u9876\u70b9\u5b58\u5165\u6570\u7ec4\nresIndex++;\n// \u904d\u5386\u8be5\u9876\u70b9\u94fe\u8868\nNode *n = vet-&gt;linked-&gt;head-&gt;next;\nwhile (n != 0) {\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, n-&gt;val, res);\nn = n-&gt;next;\n}\nreturn;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nVertex **graphDFS(graphAdjList *graph, Vertex *startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nVertex **res = (Vertex **)malloc(sizeof(Vertex *) * graph-&gt;size);\nmemset(res, 0, sizeof(Vertex *) * graph-&gt;size);\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nhashTable *visited = newHash(graph-&gt;size);\ndfs(graph, visited, startVet, res);\n// \u91ca\u653e\u54c8\u5e0c\u8868\u5185\u5b58\u5e76\u5c06\u6570\u7ec4\u7d22\u5f15\u5f52\u96f6\nfreeHash(visited);\nresIndex = 0;\n// \u8fd4\u56de\u904d\u5386\u6570\u7ec4\nreturn res;\n}\n</code></pre> graph_dfs.cs<pre><code>/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList graph, HashSet&lt;Vertex&gt; visited, List&lt;Vertex&gt; res, Vertex vet) {\nres.Add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.Add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nforeach (Vertex adjVet in graph.adjList[vet]) {\nif (visited.Contains(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9 \n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList&lt;Vertex&gt; graphDFS(GraphAdjList graph, Vertex startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nList&lt;Vertex&gt; res = new List&lt;Vertex&gt;();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nHashSet&lt;Vertex&gt; visited = new HashSet&lt;Vertex&gt;();\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n</code></pre> graph_dfs.swift<pre><code>/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunc dfs(graph: GraphAdjList, visited: inout Set&lt;Vertex&gt;, res: inout [Vertex], vet: Vertex) {\nres.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.insert(vet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adjVet in graph.adjList[vet] ?? [] {\nif visited.contains(adjVet) {\ncontinue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph: graph, visited: &amp;visited, res: &amp;res, vet: adjVet)\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(graph: GraphAdjList, startVet: Vertex) -&gt; [Vertex] {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nvar res: [Vertex] = []\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvar visited: Set&lt;Vertex&gt; = []\ndfs(graph: graph, visited: &amp;visited, res: &amp;res, vet: startVet)\nreturn res\n}\n</code></pre> graph_dfs.zig<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n</code></pre> graph_dfs.dart<pre><code>/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(\nGraphAdjList graph,\nSet&lt;Vertex&gt; visited,\nList&lt;Vertex&gt; res,\nVertex vet,\n) {\nres.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex adjVet in graph.adjList[vet]!) {\nif (visited.contains(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\nList&lt;Vertex&gt; graphDFS(GraphAdjList graph, Vertex startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nList&lt;Vertex&gt; res = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nSet&lt;Vertex&gt; visited = {};\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n</code></pre> graph_dfs.rs<pre><code>/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfn dfs(graph: &amp;GraphAdjList, visited: &amp;mut HashSet&lt;Vertex&gt;, res: &amp;mut Vec&lt;Vertex&gt;, vet: Vertex) {\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.insert(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nif let Some(adj_vets) = graph.adj_list.get(&amp;vet) {\nfor &amp;adj_vet in adj_vets {\nif visited.contains(&amp;adj_vet) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adj_vet);\n}\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -&gt; Vec&lt;Vertex&gt; {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nlet mut res = vec![];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nlet mut visited = HashSet::new();\ndfs(&amp;graph, &amp;mut visited, &amp;mut res, start_vet);\nres\n}\n</code></pre> <p>\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5176\u4e2d\uff1a</p> <ul> <li>\u76f4\u865a\u7ebf\u4ee3\u8868\u5411\u4e0b\u9012\u63a8\uff0c\u8868\u793a\u5f00\u542f\u4e86\u4e00\u4e2a\u65b0\u7684\u9012\u5f52\u65b9\u6cd5\u6765\u8bbf\u95ee\u65b0\u9876\u70b9\u3002</li> <li>\u66f2\u865a\u7ebf\u4ee3\u8868\u5411\u4e0a\u56de\u6eaf\uff0c\u8868\u793a\u6b64\u9012\u5f52\u65b9\u6cd5\u5df2\u7ecf\u8fd4\u56de\uff0c\u56de\u6eaf\u5230\u4e86\u5f00\u542f\u6b64\u9012\u5f52\u65b9\u6cd5\u7684\u4f4d\u7f6e\u3002</li> </ul> <p>\u4e3a\u4e86\u52a0\u6df1\u7406\u89e3\uff0c\u5efa\u8bae\u5c06\u56fe\u793a\u4e0e\u4ee3\u7801\u7ed3\u5408\u8d77\u6765\uff0c\u5728\u8111\u4e2d\uff08\u6216\u8005\u7528\u7b14\u753b\u4e0b\u6765\uff09\u6a21\u62df\u6574\u4e2a DFS \u8fc7\u7a0b\uff0c\u5305\u62ec\u6bcf\u4e2a\u9012\u5f52\u65b9\u6cd5\u4f55\u65f6\u5f00\u542f\u3001\u4f55\u65f6\u8fd4\u56de\u3002</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> \u56fe\uff1a\u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u6b65\u9aa4 </p> <p>\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5e8f\u5217\u662f\u5426\u552f\u4e00\uff1f</p> <p>\u4e0e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7c7b\u4f3c\uff0c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5e8f\u5217\u7684\u987a\u5e8f\u4e5f\u4e0d\u662f\u552f\u4e00\u7684\u3002\u7ed9\u5b9a\u67d0\u9876\u70b9\uff0c\u5148\u5f80\u54ea\u4e2a\u65b9\u5411\u63a2\u7d22\u90fd\u53ef\u4ee5\uff0c\u5373\u90bb\u63a5\u9876\u70b9\u7684\u987a\u5e8f\u53ef\u4ee5\u4efb\u610f\u6253\u4e71\uff0c\u90fd\u662f\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002</p> <p>\u4ee5\u6811\u7684\u904d\u5386\u4e3a\u4f8b\uff0c\u201c\u6839 \\(\\rightarrow\\) \u5de6 \\(\\rightarrow\\) \u53f3\u201d\u3001\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u3001\u201c\u5de6 \\(\\rightarrow\\) \u53f3 \\(\\rightarrow\\) \u6839\u201d\u5206\u522b\u5bf9\u5e94\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\uff0c\u5b83\u4eec\u5c55\u793a\u4e86\u4e09\u79cd\u4e0d\u540c\u7684\u904d\u5386\u4f18\u5148\u7ea7\uff0c\u7136\u800c\u8fd9\u4e09\u8005\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002</p>"},{"location":"chapter_graph/graph_traversal/#_4","title":"\u590d\u6742\u5ea6\u5206\u6790","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a \u6240\u6709\u9876\u70b9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(1\\) \u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a \u5217\u8868 <code>res</code> \uff0c\u54c8\u5e0c\u8868 <code>visited</code> \u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u6700\u5927\u4e3a \\(|V|\\) \uff0c\u56e0\u6b64\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002</p>"},{"location":"chapter_graph/summary/","title":"9.4. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u56fe\u7531\u9876\u70b9\u548c\u8fb9\u7ec4\u6210\uff0c\u53ef\u4ee5\u88ab\u8868\u793a\u4e3a\u4e00\u7ec4\u9876\u70b9\u548c\u4e00\u7ec4\u8fb9\u6784\u6210\u7684\u96c6\u5408\u3002</li> <li>\u76f8\u8f83\u4e8e\u7ebf\u6027\u5173\u7cfb\uff08\u94fe\u8868\uff09\u548c\u5206\u6cbb\u5173\u7cfb\uff08\u6811\uff09\uff0c\u7f51\u7edc\u5173\u7cfb\uff08\u56fe\uff09\u5177\u6709\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\uff0c\u56e0\u800c\u66f4\u4e3a\u590d\u6742\u3002</li> <li>\u6709\u5411\u56fe\u7684\u8fb9\u5177\u6709\u65b9\u5411\u6027\uff0c\u8fde\u901a\u56fe\u4e2d\u7684\u4efb\u610f\u9876\u70b9\u5747\u53ef\u8fbe\uff0c\u6709\u6743\u56fe\u7684\u6bcf\u6761\u8fb9\u90fd\u5305\u542b\u6743\u91cd\u53d8\u91cf\u3002</li> <li>\u90bb\u63a5\u77e9\u9635\u5229\u7528\u77e9\u9635\u6765\u8868\u793a\u56fe\uff0c\u6bcf\u4e00\u884c\uff08\u5217\uff09\u4ee3\u8868\u4e00\u4e2a\u9876\u70b9\uff0c\u77e9\u9635\u5143\u7d20\u4ee3\u8868\u8fb9\uff0c\u7528 \\(1\\) \u6216 \\(0\\) \u8868\u793a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u6709\u8fb9\u6216\u65e0\u8fb9\u3002\u90bb\u63a5\u77e9\u9635\u5728\u589e\u5220\u67e5\u64cd\u4f5c\u4e0a\u6548\u7387\u5f88\u9ad8\uff0c\u4f46\u7a7a\u95f4\u5360\u7528\u8f83\u591a\u3002</li> <li>\u90bb\u63a5\u8868\u4f7f\u7528\u591a\u4e2a\u94fe\u8868\u6765\u8868\u793a\u56fe\uff0c\u7b2c \\(i\\) \u6761\u94fe\u8868\u5bf9\u5e94\u9876\u70b9 \\(i\\) \uff0c\u5176\u4e2d\u5b58\u50a8\u4e86\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\u3002\u90bb\u63a5\u8868\u76f8\u5bf9\u4e8e\u90bb\u63a5\u77e9\u9635\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\uff0c\u4f46\u7531\u4e8e\u9700\u8981\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u8fb9\uff0c\u65f6\u95f4\u6548\u7387\u8f83\u4f4e\u3002</li> <li>\u5f53\u90bb\u63a5\u8868\u4e2d\u7684\u94fe\u8868\u8fc7\u957f\u65f6\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u6216\u54c8\u5e0c\u8868\uff0c\u4ece\u800c\u63d0\u5347\u67e5\u8be2\u6548\u7387\u3002</li> <li>\u4ece\u7b97\u6cd5\u601d\u60f3\u89d2\u5ea6\u5206\u6790\uff0c\u90bb\u63a5\u77e9\u9635\u4f53\u73b0\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\uff0c\u90bb\u63a5\u8868\u4f53\u73b0\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u3002</li> <li>\u56fe\u53ef\u7528\u4e8e\u5efa\u6a21\u5404\u7c7b\u73b0\u5b9e\u7cfb\u7edf\uff0c\u5982\u793e\u4ea4\u7f51\u7edc\u3001\u5730\u94c1\u7ebf\u8def\u7b49\u3002</li> <li>\u6811\u662f\u56fe\u7684\u4e00\u79cd\u7279\u4f8b\uff0c\u6811\u7684\u904d\u5386\u4e5f\u662f\u56fe\u7684\u904d\u5386\u7684\u4e00\u79cd\u7279\u4f8b\u3002</li> <li>\u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u7531\u8fd1\u53ca\u8fdc\u3001\u5c42\u5c42\u6269\u5f20\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u901a\u5e38\u501f\u52a9\u961f\u5217\u5b9e\u73b0\u3002</li> <li>\u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u4f18\u5148\u8d70\u5230\u5e95\u3001\u65e0\u8def\u53ef\u8d70\u65f6\u518d\u56de\u6eaf\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002</li> </ul>"},{"location":"chapter_graph/summary/#941-q-a","title":"9.4.1. \u00a0 Q &amp; A","text":"<p>\u8def\u5f84\u7684\u5b9a\u4e49\u662f\u9876\u70b9\u5e8f\u5217\u8fd8\u662f\u8fb9\u5e8f\u5217\uff1f</p> <p>\u7ef4\u57fa\u767e\u79d1\u4e0a\u4e0d\u540c\u8bed\u8a00\u7248\u672c\u7684\u5b9a\u4e49\u4e0d\u4e00\u81f4\uff1a\u82f1\u6587\u7248\u662f\u201c\u8def\u5f84\u662f\u4e00\u4e2a\u8fb9\u5e8f\u5217\u201d\uff0c\u800c\u4e2d\u6587\u7248\u662f\u201c\u8def\u5f84\u662f\u4e00\u4e2a\u9876\u70b9\u5e8f\u5217\u201d\u3002\u4ee5\u4e0b\u662f\u82f1\u6587\u7248\u539f\u6587\uff1aIn graph theory, a path in a graph is a finite or infinite sequence of edges which joins a sequence of vertices. \u5728\u672c\u6587\u4e2d\uff0c\u8def\u5f84\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u8fb9\u5e8f\u5217\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u9876\u70b9\u5e8f\u5217\u3002\u8fd9\u662f\u56e0\u4e3a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u53ef\u80fd\u5b58\u5728\u591a\u6761\u8fb9\u8fde\u63a5\uff0c\u6b64\u65f6\u6bcf\u6761\u8fb9\u90fd\u5bf9\u5e94\u4e00\u6761\u8def\u5f84\u3002</p> <p>\u975e\u8fde\u901a\u56fe\u4e2d\uff0c\u662f\u5426\u4f1a\u6709\u65e0\u6cd5\u904d\u5386\u5230\u7684\u70b9\uff1f</p> <p>\u5728\u975e\u8fde\u901a\u56fe\u4e2d\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u81f3\u5c11\u6709\u4e00\u4e2a\u9876\u70b9\u65e0\u6cd5\u5230\u8fbe\u3002\u904d\u5386\u975e\u8fde\u901a\u56fe\u9700\u8981\u8bbe\u7f6e\u591a\u4e2a\u8d77\u70b9\uff0c\u4ee5\u904d\u5386\u5230\u56fe\u7684\u6240\u6709\u8fde\u901a\u5206\u91cf\u3002</p> <p>\u5728\u90bb\u63a5\u8868\u4e2d\uff0c\u201c\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u6240\u6709\u9876\u70b9\u201d\u7684\u9876\u70b9\u987a\u5e8f\u662f\u5426\u6709\u8981\u6c42\uff1f</p> <p>\u53ef\u4ee5\u662f\u4efb\u610f\u987a\u5e8f\u3002\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9700\u8981\u6309\u7167\u6307\u5b9a\u89c4\u5219\u6765\u6392\u5e8f\uff0c\u6bd4\u5982\u6309\u7167\u9876\u70b9\u6dfb\u52a0\u7684\u6b21\u5e8f\u3001\u6216\u8005\u6309\u7167\u9876\u70b9\u503c\u5927\u5c0f\u7684\u987a\u5e8f\u7b49\u7b49\uff0c\u8fd9\u6837\u53ef\u4ee5\u6709\u52a9\u4e8e\u5feb\u901f\u67e5\u627e\u201c\u5e26\u6709\u67d0\u79cd\u6781\u503c\u201d\u7684\u9876\u70b9\u3002</p>"},{"location":"chapter_greedy/","title":"15. \u00a0 \u8d2a\u5fc3","text":"<p>Abstract</p> <p>\u5411\u65e5\u8475\u671d\u7740\u592a\u9633\u8f6c\u52a8\uff0c\u65f6\u523b\u90fd\u5728\u8ffd\u6c42\u81ea\u8eab\u6210\u957f\u7684\u6700\u5927\u53ef\u80fd\u3002</p> <p>\u8d2a\u5fc3\u7b56\u7565\u5728\u4e00\u8f6e\u8f6e\u7684\u7b80\u5355\u9009\u62e9\u4e2d\uff0c\u9010\u6b65\u5bfc\u5411\u6700\u4f73\u7684\u7b54\u6848\u3002</p>"},{"location":"chapter_greedy/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>15.1 \u00a0 \u8d2a\u5fc3\u7b97\u6cd5</li> <li>15.2 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898</li> <li>15.3 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898</li> <li>15.4 \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898</li> <li>15.5 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_greedy/fractional_knapsack_problem/","title":"15.2. \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898","text":"<p>\u5206\u6570\u80cc\u5305\u662f 0-1 \u80cc\u5305\u7684\u4e00\u4e2a\u53d8\u79cd\u95ee\u9898\u3002</p> <p>Question</p> <p>\u7ed9\u5b9a \\(n\\) \u4e2a\u7269\u54c1\uff0c\u7b2c \\(i\\) \u4e2a\u7269\u54c1\u7684\u91cd\u91cf\u4e3a \\(wgt[i-1]\\) \u3001\u4ef7\u503c\u4e3a \\(val[i-1]\\) \uff0c\u548c\u4e00\u4e2a\u5bb9\u91cf\u4e3a \\(cap\\) \u7684\u80cc\u5305\u3002\u6bcf\u4e2a\u7269\u54c1\u53ea\u80fd\u9009\u62e9\u4e00\u6b21\uff0c\u4f46\u53ef\u4ee5\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u4ef7\u503c\u6839\u636e\u9009\u62e9\u7684\u91cd\u91cf\u6bd4\u4f8b\u8ba1\u7b97\uff0c\u95ee\u5728\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u4e0b\u80cc\u5305\u4e2d\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002</p> <p></p> <p> \u56fe\uff1a\u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e </p> <p>\u672c\u9898\u548c 0-1 \u80cc\u5305\u6574\u4f53\u4e0a\u975e\u5e38\u76f8\u4f3c\uff0c\u72b6\u6001\u5305\u542b\u5f53\u524d\u7269\u54c1 \\(i\\) \u548c\u5bb9\u91cf \\(c\\) \uff0c\u76ee\u6807\u662f\u6c42\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u6700\u5927\u4ef7\u503c\u3002</p> <p>\u4e0d\u540c\u70b9\u5728\u4e8e\uff0c\u672c\u9898\u5141\u8bb8\u53ea\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u5bf9\u7269\u54c1\u4efb\u610f\u5730\u8fdb\u884c\u5207\u5206\uff0c\u5e76\u6309\u7167\u91cd\u91cf\u6bd4\u4f8b\u6765\u8ba1\u7b97\u7269\u54c1\u4ef7\u503c\uff0c\u56e0\u6b64\u6709\uff1a</p> <ol> <li>\u5bf9\u4e8e\u7269\u54c1 \\(i\\) \uff0c\u5b83\u5728\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u4ef7\u503c\u4e3a \\(val[i-1] / wgt[i-1]\\) \uff0c\u7b80\u79f0\u4e3a\u5355\u4f4d\u4ef7\u503c\u3002</li> <li>\u5047\u8bbe\u653e\u5165\u4e00\u90e8\u5206\u7269\u54c1 \\(i\\) \uff0c\u91cd\u91cf\u4e3a \\(w\\) \uff0c\u5219\u80cc\u5305\u589e\u52a0\u7684\u4ef7\u503c\u4e3a \\(w \\times val[i-1] / wgt[i-1]\\) \u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u7269\u54c1\u5728\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u4ef7\u503c </p>"},{"location":"chapter_greedy/fractional_knapsack_problem/#_1","title":"\u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"<p>\u6700\u5927\u5316\u80cc\u5305\u5185\u7269\u54c1\u603b\u4ef7\u503c\uff0c\u672c\u8d28\u4e0a\u662f\u8981\u6700\u5927\u5316\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u7269\u54c1\u4ef7\u503c\u3002\u7531\u6b64\u4fbf\u53ef\u63a8\u51fa\u672c\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\uff1a</p> <ol> <li>\u5c06\u7269\u54c1\u6309\u7167\u5355\u4f4d\u4ef7\u503c\u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\u3002</li> <li>\u904d\u5386\u6240\u6709\u7269\u54c1\uff0c\u6bcf\u8f6e\u8d2a\u5fc3\u5730\u9009\u62e9\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\u7684\u7269\u54c1\u3002</li> <li>\u82e5\u5269\u4f59\u80cc\u5305\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u4f7f\u7528\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u586b\u6ee1\u80cc\u5305\u5373\u53ef\u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u5206\u6570\u80cc\u5305\u7684\u8d2a\u5fc3\u7b56\u7565 </p>"},{"location":"chapter_greedy/fractional_knapsack_problem/#_2","title":"\u4ee3\u7801\u5b9e\u73b0","text":"<p>\u6211\u4eec\u5efa\u7acb\u4e86\u4e00\u4e2a\u7269\u54c1\u7c7b <code>Item</code> \uff0c\u4ee5\u4fbf\u5c06\u7269\u54c1\u6309\u7167\u5355\u4f4d\u4ef7\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u5f53\u80cc\u5305\u5df2\u6ee1\u65f6\u8df3\u51fa\u5e76\u8fd4\u56de\u89e3\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust fractional_knapsack.java<pre><code>/* \u7269\u54c1 */\nclass Item {\nint w; // \u7269\u54c1\u91cd\u91cf\nint v; // \u7269\u54c1\u4ef7\u503c\npublic Item(int w, int v) {\nthis.w = w;\nthis.v = v;\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(int[] wgt, int[] val, int cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nItem[] items = new Item[wgt.length];\nfor (int i = 0; i &lt; wgt.length; i++) {\nitems[i] = new Item(wgt[i], val[i]);\n}\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nArrays.sort(items, Comparator.comparingDouble(item -&gt; -((double) item.v / item.w)));\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\ndouble res = 0;\nfor (Item item : items) {\nif (item.w &lt;= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (double) item.v / item.w * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n</code></pre> fractional_knapsack.cpp<pre><code>/* \u7269\u54c1 */\nclass Item {\npublic:\nint w; // \u7269\u54c1\u91cd\u91cf\nint v; // \u7269\u54c1\u4ef7\u503c\nItem(int w, int v) : w(w), v(v) {\n}\n};\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(vector&lt;int&gt; &amp;wgt, vector&lt;int&gt; &amp;val, int cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nvector&lt;Item&gt; items;\nfor (int i = 0; i &lt; wgt.size(); i++) {\nitems.push_back(Item(wgt[i], val[i]));\n}\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nsort(items.begin(), items.end(), [](Item &amp;a, Item &amp;b) { return (double)a.v / a.w &gt; (double)b.v / b.w; });\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\ndouble res = 0;\nfor (auto &amp;item : items) {\nif (item.w &lt;= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (double)item.v / item.w * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n</code></pre> fractional_knapsack.py<pre><code>class Item:\n\"\"\"\u7269\u54c1\"\"\"\ndef __init__(self, w: int, v: int):\nself.w = w # \u7269\u54c1\u91cd\u91cf\nself.v = v # \u7269\u54c1\u4ef7\u503c\ndef fractional_knapsack(wgt: list[int], val: list[int], cap: int) -&gt; int:\n\"\"\"\u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3\"\"\"\n# \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nitems = [Item(w, v) for w, v in zip(wgt, val)]\n# \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort(key=lambda item: item.v / item.w, reverse=True)\n# \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nres = 0\nfor item in items:\nif item.w &lt;= cap:\n# \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v\ncap -= item.w\nelse:\n# \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (item.v / item.w) * cap\n# \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak\nreturn res\n</code></pre> fractional_knapsack.go<pre><code>/* \u7269\u54c1 */\ntype Item struct {\nw int // \u7269\u54c1\u91cd\u91cf\nv int // \u7269\u54c1\u4ef7\u503c\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfunc fractionalKnapsack(wgt []int, val []int, cap int) float64 {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nitems := make([]Item, len(wgt))\nfor i := 0; i &lt; len(wgt); i++ {\nitems[i] = Item{wgt[i], val[i]}\n}\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nsort.Slice(items, func(i, j int) bool {\nreturn float64(items[i].v)/float64(items[i].w) &gt; float64(items[j].v)/float64(items[j].w)\n})\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nres := 0.0\nfor _, item := range items {\nif item.w &lt;= cap {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += float64(item.v)\ncap -= item.w\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += float64(item.v) / float64(item.w) * float64(cap)\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak\n}\n}\nreturn res\n}\n</code></pre> fractional_knapsack.js<pre><code>[class]{Item}-[func]{}\n[class]{}-[func]{fractionalKnapsack}\n</code></pre> fractional_knapsack.ts<pre><code>[class]{Item}-[func]{}\n[class]{}-[func]{fractionalKnapsack}\n</code></pre> fractional_knapsack.c<pre><code>[class]{Item}-[func]{}\n[class]{}-[func]{fractionalKnapsack}\n</code></pre> fractional_knapsack.cs<pre><code>/* \u7269\u54c1 */\nclass Item {\npublic int w; // \u7269\u54c1\u91cd\u91cf\npublic int v; // \u7269\u54c1\u4ef7\u503c\npublic Item(int w, int v) {\nthis.w = w;\nthis.v = v;\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(int[] wgt, int[] val, int cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nItem[] items = new Item[wgt.Length];\nfor (int i = 0; i &lt; wgt.Length; i++) {\nitems[i] = new Item(wgt[i], val[i]);\n}\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nArray.Sort(items, (x, y) =&gt; (y.v / y.w).CompareTo(x.v / x.w));\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\ndouble res = 0;\nforeach (Item item in items) {\nif (item.w &lt;= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += (double)item.v / item.w * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n</code></pre> fractional_knapsack.swift<pre><code>[class]{Item}-[func]{}\n[class]{}-[func]{fractionalKnapsack}\n</code></pre> fractional_knapsack.zig<pre><code>[class]{Item}-[func]{}\n[class]{}-[func]{fractionalKnapsack}\n</code></pre> fractional_knapsack.dart<pre><code>/* \u7269\u54c1 */\nclass Item {\nint w; // \u7269\u54c1\u91cd\u91cf\nint v; // \u7269\u54c1\u4ef7\u503c\nItem(this.w, this.v);\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\ndouble fractionalKnapsack(List&lt;int&gt; wgt, List&lt;int&gt; val, int cap) {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nList&lt;Item&gt; items = List.generate(wgt.length, (i) =&gt; Item(wgt[i], val[i]));\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort((a, b) =&gt; (b.v / b.w).compareTo(a.v / a.w));\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\ndouble res = 0;\nfor (Item item in items) {\nif (item.w &lt;= cap) {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += item.v / item.w * cap;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nreturn res;\n}\n</code></pre> fractional_knapsack.rs<pre><code>/* \u7269\u54c1 */\nstruct Item {\nw: i32, // \u7269\u54c1\u91cd\u91cf\nv: i32, // \u7269\u54c1\u4ef7\u503c\n}\nimpl Item {\nfn new(w: i32, v: i32) -&gt; Self {\nSelf { w, v }\n}\n}\n/* \u5206\u6570\u80cc\u5305\uff1a\u8d2a\u5fc3 */\nfn fractional_knapsack(wgt: &amp;[i32], val: &amp;[i32], mut cap: i32) -&gt; f64 {\n// \u521b\u5efa\u7269\u54c1\u5217\u8868\uff0c\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\uff1a\u91cd\u91cf\u3001\u4ef7\u503c\nlet mut items = wgt\n.iter()\n.zip(val.iter())\n.map(|(&amp;w, &amp;v)| Item::new(w, v))\n.collect::&lt;Vec&lt;Item&gt;&gt;();\n// \u6309\u7167\u5355\u4f4d\u4ef7\u503c item.v / item.w \u4ece\u9ad8\u5230\u4f4e\u8fdb\u884c\u6392\u5e8f\nitems.sort_by(|a, b| {\n(b.v as f64 / b.w as f64)\n.partial_cmp(&amp;(a.v as f64 / a.w as f64))\n.unwrap()\n});\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\nlet mut res = 0.0;\nfor item in &amp;items {\nif item.w &lt;= cap {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u5145\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u6574\u4e2a\u88c5\u8fdb\u80cc\u5305\nres += item.v as f64;\ncap -= item.w;\n} else {\n// \u82e5\u5269\u4f59\u5bb9\u91cf\u4e0d\u8db3\uff0c\u5219\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4e00\u90e8\u5206\u88c5\u8fdb\u80cc\u5305\nres += item.v as f64 / item.w as f64 * cap as f64;\n// \u5df2\u65e0\u5269\u4f59\u5bb9\u91cf\uff0c\u56e0\u6b64\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\nres\n}\n</code></pre> <p>\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u904d\u5386\u6574\u4e2a\u7269\u54c1\u5217\u8868\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u7269\u54c1\u6570\u91cf\u3002</p> <p>\u7531\u4e8e\u521d\u59cb\u5316\u4e86\u4e00\u4e2a <code>Item</code> \u5bf9\u8c61\u5217\u8868\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002</p>"},{"location":"chapter_greedy/fractional_knapsack_problem/#_3","title":"\u6b63\u786e\u6027\u8bc1\u660e","text":"<p>\u91c7\u7528\u53cd\u8bc1\u6cd5\u3002\u5047\u8bbe\u7269\u54c1 \\(x\\) \u662f\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\u7684\u7269\u54c1\uff0c\u4f7f\u7528\u67d0\u7b97\u6cd5\u6c42\u5f97\u6700\u5927\u4ef7\u503c\u4e3a <code>res</code> \uff0c\u4f46\u8be5\u89e3\u4e2d\u4e0d\u5305\u542b\u7269\u54c1 \\(x\\) \u3002</p> <p>\u73b0\u5728\u4ece\u80cc\u5305\u4e2d\u62ff\u51fa\u5355\u4f4d\u91cd\u91cf\u7684\u4efb\u610f\u7269\u54c1\uff0c\u5e76\u66ff\u6362\u4e3a\u5355\u4f4d\u91cd\u91cf\u7684\u7269\u54c1 \\(x\\) \u3002\u7531\u4e8e\u7269\u54c1 \\(x\\) \u7684\u5355\u4f4d\u4ef7\u503c\u6700\u9ad8\uff0c\u56e0\u6b64\u66ff\u6362\u540e\u7684\u603b\u4ef7\u503c\u4e00\u5b9a\u5927\u4e8e <code>res</code> \u3002\u8fd9\u4e0e <code>res</code> \u662f\u6700\u4f18\u89e3\u77db\u76fe\uff0c\u8bf4\u660e\u6700\u4f18\u89e3\u4e2d\u5fc5\u987b\u5305\u542b\u7269\u54c1 \\(x\\) \u3002</p> <p>\u5bf9\u4e8e\u8be5\u89e3\u4e2d\u7684\u5176\u4ed6\u7269\u54c1\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u6784\u5efa\u51fa\u4e0a\u8ff0\u77db\u76fe\u3002\u603b\u800c\u8a00\u4e4b\uff0c\u5355\u4f4d\u4ef7\u503c\u66f4\u5927\u7684\u7269\u54c1\u603b\u662f\u66f4\u4f18\u9009\u62e9\uff0c\u8fd9\u8bf4\u660e\u8d2a\u5fc3\u7b56\u7565\u662f\u6709\u6548\u7684\u3002</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5982\u679c\u5c06\u7269\u54c1\u91cd\u91cf\u548c\u7269\u54c1\u5355\u4f4d\u4ef7\u503c\u5206\u522b\u770b\u4f5c\u4e00\u4e2a 2D \u56fe\u8868\u7684\u6a2a\u8f74\u548c\u7eb5\u8f74\uff0c\u5219\u5206\u6570\u80cc\u5305\u95ee\u9898\u53ef\u88ab\u8f6c\u5316\u4e3a\u201c\u6c42\u5728\u6709\u9650\u6a2a\u8f74\u533a\u95f4\u4e0b\u7684\u6700\u5927\u56f4\u6210\u9762\u79ef\u201d\u3002</p> <p>\u901a\u8fc7\u8fd9\u4e2a\u7c7b\u6bd4\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u51e0\u4f55\u89d2\u5ea6\u7406\u89e3\u8d2a\u5fc3\u7b56\u7565\u7684\u6709\u6548\u6027\u3002</p> <p></p> <p> \u56fe\uff1a\u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u51e0\u4f55\u8868\u793a </p>"},{"location":"chapter_greedy/greedy_algorithm/","title":"15.1. \u00a0 \u8d2a\u5fc3\u7b97\u6cd5","text":"<p>\u8d2a\u5fc3\u7b97\u6cd5\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u7684\u7b97\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u60f3\u662f\u5728\u95ee\u9898\u7684\u6bcf\u4e2a\u51b3\u7b56\u9636\u6bb5\uff0c\u90fd\u9009\u62e9\u5f53\u524d\u770b\u8d77\u6765\u6700\u4f18\u7684\u9009\u62e9\uff0c\u5373\u8d2a\u5fc3\u5730\u505a\u51fa\u5c40\u90e8\u6700\u4f18\u7684\u51b3\u7b56\uff0c\u4ee5\u671f\u671b\u83b7\u5f97\u5168\u5c40\u6700\u4f18\u89e3\u3002\u8d2a\u5fc3\u7b97\u6cd5\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u5728\u8bb8\u591a\u5b9e\u9645\u95ee\u9898\u4e2d\u90fd\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002</p> <p>\u8d2a\u5fc3\u7b97\u6cd5\u548c\u52a8\u6001\u89c4\u5212\u90fd\u5e38\u7528\u4e8e\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u3002\u5b83\u4eec\u6709\u4e00\u4e9b\u76f8\u4f3c\u4e4b\u5904\uff0c\u6bd4\u5982\u90fd\u4f9d\u8d56\u6700\u4f18\u5b50\u7ed3\u6784\u6027\u8d28\u3002\u4e24\u8005\u7684\u4e0d\u540c\u70b9\u5728\u4e8e\uff1a</p> <ul> <li>\u52a8\u6001\u89c4\u5212\u4f1a\u6839\u636e\u4e4b\u524d\u9636\u6bb5\u7684\u6240\u6709\u51b3\u7b56\u6765\u8003\u8651\u5f53\u524d\u51b3\u7b56\uff0c\u5e76\u4f7f\u7528\u8fc7\u53bb\u5b50\u95ee\u9898\u7684\u89e3\u6765\u6784\u5efa\u5f53\u524d\u5b50\u95ee\u9898\u7684\u89e3\u3002</li> <li>\u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4f1a\u91cd\u65b0\u8003\u8651\u8fc7\u53bb\u7684\u51b3\u7b56\uff0c\u800c\u662f\u4e00\u8def\u5411\u524d\u5730\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u4e0d\u65ad\u7f29\u5c0f\u95ee\u9898\u8303\u56f4\uff0c\u76f4\u81f3\u95ee\u9898\u88ab\u89e3\u51b3\u3002</li> </ul> <p>\u6211\u4eec\u5148\u901a\u8fc7\u4f8b\u9898\u201c\u96f6\u94b1\u5151\u6362\u201d\u4e86\u89e3\u8d2a\u5fc3\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002\u8fd9\u9053\u9898\u5df2\u7ecf\u5728\u52a8\u6001\u89c4\u5212\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u76f8\u4fe1\u4f60\u5bf9\u5b83\u5e76\u4e0d\u964c\u751f\u3002</p> <p>Question</p> <p>\u7ed9\u5b9a \\(n\\) \u79cd\u786c\u5e01\uff0c\u7b2c \\(i\\) \u79cd\u786c\u5e01\u7684\u9762\u503c\u4e3a \\(coins[i - 1]\\) \uff0c\u76ee\u6807\u91d1\u989d\u4e3a \\(amt\\) \uff0c\u6bcf\u79cd\u786c\u5e01\u53ef\u4ee5\u91cd\u590d\u9009\u53d6\uff0c\u95ee\u80fd\u591f\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u7684\u6700\u5c11\u786c\u5e01\u4e2a\u6570\u3002\u5982\u679c\u65e0\u6cd5\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u5219\u8fd4\u56de \\(-1\\) \u3002</p> <p>\u8fd9\u9053\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\u5728\u751f\u6d3b\u4e2d\u5f88\u5e38\u89c1\uff1a\u7ed9\u5b9a\u76ee\u6807\u91d1\u989d\uff0c\u6211\u4eec\u8d2a\u5fc3\u5730\u9009\u62e9\u4e0d\u5927\u4e8e\u4e14\u6700\u63a5\u8fd1\u5b83\u7684\u786c\u5e01\uff0c\u4e0d\u65ad\u5faa\u73af\u8be5\u6b65\u9aa4\uff0c\u76f4\u81f3\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u4e3a\u6b62\u3002</p> <p></p> <p> \u56fe\uff1a\u96f6\u94b1\u5151\u6362\u7684\u8d2a\u5fc3\u7b56\u7565 </p> <p>\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002\u4f60\u53ef\u80fd\u4f1a\u4e0d\u7531\u5730\u53d1\u51fa\u611f\u53f9\uff1aSo Clean \uff01\u8d2a\u5fc3\u7b97\u6cd5\u4ec5\u7528\u5341\u884c\u4ee3\u7801\u5c31\u89e3\u51b3\u4e86\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust coin_change_greedy.java<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(int[] coins, int amt) {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nint i = coins.length - 1;\nint count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt &gt; 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i &gt; 0 &amp;&amp; coins[i] &gt; amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1;\n}\n</code></pre> coin_change_greedy.cpp<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(vector&lt;int&gt; &amp;coins, int amt) {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nint i = coins.size() - 1;\nint count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt &gt; 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i &gt; 0 &amp;&amp; coins[i] &gt; amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1;\n}\n</code></pre> coin_change_greedy.py<pre><code>def coin_change_greedy(coins: list[int], amt: int) -&gt; int:\n\"\"\"\u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3\"\"\"\n# \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\ni = len(coins) - 1\ncount = 0\n# \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile amt &gt; 0:\n# \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile i &gt; 0 and coins[i] &gt; amt:\ni -= 1\n# \u9009\u62e9 coins[i]\namt -= coins[i]\ncount += 1\n# \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn count if amt == 0 else -1\n</code></pre> coin_change_greedy.go<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfunc coinChangeGreedy(coins []int, amt int) int {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\ni := len(coins) - 1\ncount := 0\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nfor amt &gt; 0 {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nfor i &gt; 0 &amp;&amp; coins[i] &gt; amt {\ni--\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i]\ncount++\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nif amt != 0 {\nreturn -1\n}\nreturn count\n}\n</code></pre> coin_change_greedy.js<pre><code>[class]{}-[func]{coinChangeGreedy}\n</code></pre> coin_change_greedy.ts<pre><code>[class]{}-[func]{coinChangeGreedy}\n</code></pre> coin_change_greedy.c<pre><code>[class]{}-[func]{coinChangeGreedy}\n</code></pre> coin_change_greedy.cs<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(int[] coins, int amt) {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nint i = coins.Length - 1;\nint count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt &gt; 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i &gt; 0 &amp;&amp; coins[i] &gt; amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1;\n}\n</code></pre> coin_change_greedy.swift<pre><code>[class]{}-[func]{coinChangeGreedy}\n</code></pre> coin_change_greedy.zig<pre><code>[class]{}-[func]{coinChangeGreedy}\n</code></pre> coin_change_greedy.dart<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nint coinChangeGreedy(List&lt;int&gt; coins, int amt) {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nint i = coins.length - 1;\nint count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile (amt &gt; 0) {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile (i &gt; 0 &amp;&amp; coins[i] &gt; amt) {\ni--;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount++;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nreturn amt == 0 ? count : -1;\n}\n</code></pre> coin_change_greedy.rs<pre><code>/* \u96f6\u94b1\u5151\u6362\uff1a\u8d2a\u5fc3 */\nfn coin_change_greedy(coins: &amp;[i32], mut amt: i32) -&gt; i32 {\n// \u5047\u8bbe coins \u5217\u8868\u6709\u5e8f\nlet mut i = coins.len() - 1;\nlet mut count = 0;\n// \u5faa\u73af\u8fdb\u884c\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u5230\u65e0\u5269\u4f59\u91d1\u989d\nwhile amt &gt; 0 {\n// \u627e\u5230\u5c0f\u4e8e\u4e14\u6700\u63a5\u8fd1\u5269\u4f59\u91d1\u989d\u7684\u786c\u5e01\nwhile i &gt; 0 &amp;&amp; coins[i] &gt; amt {\ni -= 1;\n}\n// \u9009\u62e9 coins[i]\namt -= coins[i];\ncount += 1;\n}\n// \u82e5\u672a\u627e\u5230\u53ef\u884c\u65b9\u6848\uff0c\u5219\u8fd4\u56de -1\nif amt == 0 {\ncount\n} else {\n-1\n}\n}\n</code></pre>"},{"location":"chapter_greedy/greedy_algorithm/#1511","title":"15.1.1. \u00a0 \u8d2a\u5fc3\u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"<p>\u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4ec5\u64cd\u4f5c\u76f4\u63a5\u3001\u5b9e\u73b0\u7b80\u5355\uff0c\u800c\u4e14\u901a\u5e38\u6548\u7387\u4e5f\u5f88\u9ad8\u3002\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u8bb0\u786c\u5e01\u6700\u5c0f\u9762\u503c\u4e3a \\(\\min(coins)\\) \uff0c\u5219\u8d2a\u5fc3\u9009\u62e9\u6700\u591a\u5faa\u73af \\(amt / \\min(coins)\\) \u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(amt / \\min(coins))\\) \u3002\u8fd9\u6bd4\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\times amt)\\) \u63d0\u5347\u4e86\u4e00\u4e2a\u6570\u91cf\u7ea7\u3002</p> <p>\u7136\u800c\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u786c\u5e01\u9762\u503c\u7ec4\u5408\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5e76\u4e0d\u80fd\u627e\u5230\u6700\u4f18\u89e3\u3002\u6211\u4eec\u6765\u770b\u51e0\u4e2a\u4f8b\u5b50\uff1a</p> <ul> <li>\u6b63\u4f8b \\(coins = [1, 5, 10, 20, 50, 100]\\)\uff1a\u5728\u8be5\u786c\u5e01\u7ec4\u5408\u4e0b\uff0c\u7ed9\u5b9a\u4efb\u610f \\(amt\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u90fd\u53ef\u4ee5\u627e\u51fa\u6700\u4f18\u89e3\u3002</li> <li>\u53cd\u4f8b \\(coins = [1, 20, 50]\\)\uff1a\u5047\u8bbe \\(amt = 60\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ea\u80fd\u627e\u5230 \\(50 + 1 \\times 10\\) \u7684\u5151\u6362\u7ec4\u5408\uff0c\u5171\u8ba1 \\(11\\) \u679a\u786c\u5e01\uff0c\u4f46\u52a8\u6001\u89c4\u5212\u53ef\u4ee5\u627e\u5230\u6700\u4f18\u89e3 \\(20 + 20 + 20\\) \uff0c\u4ec5\u9700 \\(3\\) \u679a\u786c\u5e01\u3002</li> <li>\u53cd\u4f8b \\(coins = [1, 49, 50]\\)\uff1a\u5047\u8bbe \\(amt = 98\\) \uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ea\u80fd\u627e\u5230 \\(50 + 1 \\times 48\\) \u7684\u5151\u6362\u7ec4\u5408\uff0c\u5171\u8ba1 \\(49\\) \u679a\u786c\u5e01\uff0c\u4f46\u52a8\u6001\u89c4\u5212\u53ef\u4ee5\u627e\u5230\u6700\u4f18\u89e3 \\(49 + 49\\) \uff0c\u4ec5\u9700 \\(2\\) \u679a\u786c\u5e01\u3002</li> </ul> <p></p> <p> \u56fe\uff1a\u8d2a\u5fc3\u65e0\u6cd5\u627e\u51fa\u6700\u4f18\u89e3\u7684\u793a\u4f8b </p> <p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u65e0\u6cd5\u4fdd\u8bc1\u627e\u5230\u5168\u5c40\u6700\u4f18\u89e3\uff0c\u5e76\u4e14\u6709\u53ef\u80fd\u627e\u5230\u975e\u5e38\u5dee\u7684\u89e3\u3002\u5b83\u66f4\u9002\u5408\u7528\u52a8\u6001\u89c4\u5212\u89e3\u51b3\u3002</p> <p>\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u9002\u7528\u4e8e\u4ee5\u4e0b\u4e24\u7c7b\u95ee\u9898\uff1a</p> <ol> <li>\u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1a\u8d2a\u5fc3\u7b97\u6cd5\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5f80\u5f80\u662f\u6700\u4f18\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u5f80\u5f80\u6bd4\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u66f4\u9ad8\u6548\u3002</li> <li>\u53ef\u4ee5\u627e\u5230\u8fd1\u4f3c\u6700\u4f18\u89e3\uff1a\u8d2a\u5fc3\u7b97\u6cd5\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e5f\u662f\u53ef\u7528\u7684\u3002\u5bf9\u4e8e\u5f88\u591a\u590d\u6742\u95ee\u9898\u6765\u8bf4\uff0c\u5bfb\u627e\u5168\u5c40\u6700\u4f18\u89e3\u662f\u975e\u5e38\u56f0\u96be\u7684\uff0c\u80fd\u4ee5\u8f83\u9ad8\u6548\u7387\u627e\u5230\u6b21\u4f18\u89e3\u4e5f\u662f\u975e\u5e38\u4e0d\u9519\u7684\u3002</li> </ol>"},{"location":"chapter_greedy/greedy_algorithm/#1512","title":"15.1.2. \u00a0 \u8d2a\u5fc3\u7b97\u6cd5\u7279\u6027","text":"<p>\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u4ec0\u4e48\u6837\u7684\u95ee\u9898\u9002\u5408\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u5462\uff1f\u6216\u8005\u8bf4\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1f</p> <p>\u76f8\u8f83\u4e8e\u52a8\u6001\u89c4\u5212\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u4f7f\u7528\u6761\u4ef6\u66f4\u52a0\u82db\u523b\uff0c\u5176\u4e3b\u8981\u5173\u6ce8\u95ee\u9898\u7684\u4e24\u4e2a\u6027\u8d28\uff1a</p> <ul> <li>\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\uff1a\u53ea\u6709\u5f53\u5c40\u90e8\u6700\u4f18\u9009\u62e9\u59cb\u7ec8\u53ef\u4ee5\u5bfc\u81f4\u5168\u5c40\u6700\u4f18\u89e3\u65f6\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u624d\u80fd\u4fdd\u8bc1\u5f97\u5230\u6700\u4f18\u89e3\u3002</li> <li>\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u5305\u542b\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u3002</li> </ul> <p>\u6700\u4f18\u5b50\u7ed3\u6784\u5df2\u7ecf\u5728\u52a8\u6001\u89c4\u5212\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u4e0d\u518d\u8d58\u8ff0\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4e00\u4e9b\u95ee\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\u5e76\u4e0d\u660e\u663e\uff0c\u4f46\u4ecd\u7136\u53ef\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u89e3\u51b3\u3002</p> <p>\u6211\u4eec\u4e3b\u8981\u63a2\u7a76\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u7684\u5224\u65ad\u65b9\u6cd5\u3002\u867d\u7136\u5b83\u7684\u63cf\u8ff0\u770b\u4e0a\u53bb\u6bd4\u8f83\u7b80\u5355\uff0c\u4f46\u5b9e\u9645\u4e0a\u5bf9\u4e8e\u8bb8\u591a\u95ee\u9898\uff0c\u8bc1\u660e\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u4e0d\u662f\u4e00\u4ef6\u6613\u4e8b\u3002</p> <p>\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\uff0c\u6211\u4eec\u867d\u7136\u80fd\u591f\u5bb9\u6613\u5730\u4e3e\u51fa\u53cd\u4f8b\uff0c\u5bf9\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u8fdb\u884c\u8bc1\u4f2a\uff0c\u4f46\u8bc1\u5b9e\u7684\u96be\u5ea6\u8f83\u5927\u3002\u5982\u679c\u95ee\uff1a\u6ee1\u8db3\u4ec0\u4e48\u6761\u4ef6\u7684\u786c\u5e01\u7ec4\u5408\u53ef\u4ee5\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\uff1f\u6211\u4eec\u5f80\u5f80\u53ea\u80fd\u51ed\u501f\u76f4\u89c9\u6216\u4e3e\u4f8b\u5b50\u6765\u7ed9\u51fa\u4e00\u4e2a\u6a21\u68f1\u4e24\u53ef\u7684\u7b54\u6848\uff0c\u800c\u96be\u4ee5\u7ed9\u51fa\u4e25\u8c28\u7684\u6570\u5b66\u8bc1\u660e\u3002</p> <p>Quote</p> <p>\u6709\u4e00\u7bc7\u8bba\u6587\u4e13\u95e8\u8ba8\u8bba\u4e86\u8be5\u95ee\u9898\u3002\u4f5c\u8005\u7ed9\u51fa\u4e86\u4e00\u4e2a \\(O(n^3)\\) \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u7b97\u6cd5\uff0c\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u786c\u5e01\u7ec4\u5408\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u627e\u51fa\u4efb\u4f55\u91d1\u989d\u7684\u6700\u4f18\u89e3\u3002</p> <p>Pearson, David. A polynomial-time algorithm for the change-making problem. Operations Research Letters 33.3 (2005): 231-234.</p>"},{"location":"chapter_greedy/greedy_algorithm/#1513","title":"15.1.3. \u00a0 \u8d2a\u5fc3\u89e3\u9898\u6b65\u9aa4","text":"<p>\u8d2a\u5fc3\u95ee\u9898\u7684\u89e3\u51b3\u6d41\u7a0b\u5927\u4f53\u53ef\u5206\u4e3a\u4e09\u6b65\uff1a</p> <ol> <li>\u95ee\u9898\u5206\u6790\uff1a\u68b3\u7406\u4e0e\u7406\u89e3\u95ee\u9898\u7279\u6027\uff0c\u5305\u62ec\u72b6\u6001\u5b9a\u4e49\u3001\u4f18\u5316\u76ee\u6807\u548c\u7ea6\u675f\u6761\u4ef6\u7b49\u3002\u8fd9\u4e00\u6b65\u5728\u56de\u6eaf\u548c\u52a8\u6001\u89c4\u5212\u4e2d\u90fd\u6709\u6d89\u53ca\u3002</li> <li>\u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\uff1a\u786e\u5b9a\u5982\u4f55\u5728\u6bcf\u4e00\u6b65\u4e2d\u505a\u51fa\u8d2a\u5fc3\u9009\u62e9\u3002\u8fd9\u4e2a\u7b56\u7565\u80fd\u591f\u5728\u6bcf\u4e00\u6b65\u51cf\u5c0f\u95ee\u9898\u7684\u89c4\u6a21\uff0c\u5e76\u6700\u7ec8\u80fd\u89e3\u51b3\u6574\u4e2a\u95ee\u9898\u3002</li> <li>\u6b63\u786e\u6027\u8bc1\u660e\uff1a\u901a\u5e38\u9700\u8981\u8bc1\u660e\u95ee\u9898\u5177\u6709\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u3002\u8fd9\u4e2a\u6b65\u9aa4\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5230\u6570\u5b66\u8bc1\u660e\uff0c\u4f8b\u5982\u5f52\u7eb3\u6cd5\u6216\u53cd\u8bc1\u6cd5\u7b49\u3002</li> </ol> <p>\u786e\u5b9a\u8d2a\u5fc3\u7b56\u7565\u662f\u6c42\u89e3\u95ee\u9898\u7684\u6838\u5fc3\u6b65\u9aa4\uff0c\u4f46\u5b9e\u65bd\u8d77\u6765\u53ef\u80fd\u5e76\u4e0d\u5bb9\u6613\uff0c\u539f\u56e0\u5305\u62ec\uff1a</p> <ul> <li>\u4e0d\u540c\u95ee\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\u7684\u5dee\u5f02\u8f83\u5927\u3002\u5bf9\u4e8e\u8bb8\u591a\u95ee\u9898\u6765\u8bf4\uff0c\u8d2a\u5fc3\u7b56\u7565\u90fd\u6bd4\u8f83\u6d45\u663e\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e9b\u5927\u6982\u7684\u601d\u8003\u4e0e\u5c1d\u8bd5\u5c31\u80fd\u5f97\u51fa\u3002\u800c\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u8d2a\u5fc3\u7b56\u7565\u53ef\u80fd\u975e\u5e38\u9690\u853d\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u975e\u5e38\u8003\u9a8c\u4e2a\u4eba\u7684\u89e3\u9898\u7ecf\u9a8c\u4e0e\u7b97\u6cd5\u80fd\u529b\u4e86\u3002</li> <li>\u67d0\u4e9b\u8d2a\u5fc3\u7b56\u7565\u5177\u6709\u8f83\u5f3a\u7684\u8ff7\u60d1\u6027\u3002\u5f53\u6211\u4eec\u6ee1\u6000\u4fe1\u5fc3\u8bbe\u8ba1\u597d\u8d2a\u5fc3\u7b56\u7565\uff0c\u5199\u51fa\u89e3\u9898\u4ee3\u7801\u5e76\u63d0\u4ea4\u8fd0\u884c\uff0c\u5f88\u53ef\u80fd\u53d1\u73b0\u90e8\u5206\u6d4b\u8bd5\u6837\u4f8b\u65e0\u6cd5\u901a\u8fc7\u3002\u8fd9\u662f\u56e0\u4e3a\u8bbe\u8ba1\u7684\u8d2a\u5fc3\u7b56\u7565\u53ea\u662f\u201c\u90e8\u5206\u6b63\u786e\u201d\u7684\uff0c\u4e0a\u6587\u4ecb\u7ecd\u7684\u96f6\u94b1\u5151\u6362\u5c31\u662f\u4e2a\u5178\u578b\u6848\u4f8b\u3002</li> </ul> <p>\u4e3a\u4e86\u4fdd\u8bc1\u6b63\u786e\u6027\uff0c\u6211\u4eec\u5e94\u8be5\u5bf9\u8d2a\u5fc3\u7b56\u7565\u8fdb\u884c\u4e25\u8c28\u7684\u6570\u5b66\u8bc1\u660e\uff0c\u901a\u5e38\u9700\u8981\u7528\u5230\u53cd\u8bc1\u6cd5\u6216\u6570\u5b66\u5f52\u7eb3\u6cd5\u3002</p> <p>\u7136\u800c\uff0c\u6b63\u786e\u6027\u8bc1\u660e\u4e5f\u5f88\u53ef\u80fd\u4e0d\u662f\u4e00\u4ef6\u6613\u4e8b\u3002\u5982\u82e5\u6ca1\u6709\u5934\u7eea\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u62e9\u9762\u5411\u6d4b\u8bd5\u7528\u4f8b\u8fdb\u884c Debug \uff0c\u4e00\u6b65\u6b65\u4fee\u6539\u4e0e\u9a8c\u8bc1\u8d2a\u5fc3\u7b56\u7565\u3002</p>"},{"location":"chapter_greedy/greedy_algorithm/#1514","title":"15.1.4. \u00a0 \u8d2a\u5fc3\u5178\u578b\u4f8b\u9898","text":"<p>\u8d2a\u5fc3\u7b97\u6cd5\u5e38\u5e38\u5e94\u7528\u5728\u6ee1\u8db3\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u4f18\u5316\u95ee\u9898\u4e2d\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5178\u578b\u7684\u8d2a\u5fc3\u7b97\u6cd5\u95ee\u9898\uff1a</p> <ol> <li>\u786c\u5e01\u627e\u96f6\u95ee\u9898\uff1a\u5728\u67d0\u4e9b\u786c\u5e01\u7ec4\u5408\u4e0b\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u603b\u662f\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002</li> <li>\u533a\u95f4\u8c03\u5ea6\u95ee\u9898\uff1a\u5047\u8bbe\u4f60\u6709\u4e00\u4e9b\u4efb\u52a1\uff0c\u6bcf\u4e2a\u4efb\u52a1\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u8fdb\u884c\uff0c\u4f60\u7684\u76ee\u6807\u662f\u5b8c\u6210\u5c3d\u53ef\u80fd\u591a\u7684\u4efb\u52a1\u3002\u5982\u679c\u6bcf\u6b21\u90fd\u9009\u62e9\u7ed3\u675f\u65f6\u95f4\u6700\u65e9\u7684\u4efb\u52a1\uff0c\u90a3\u4e48\u8d2a\u5fc3\u7b97\u6cd5\u5c31\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002</li> <li>\u5206\u6570\u80cc\u5305\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u7269\u54c1\u548c\u4e00\u4e2a\u8f7d\u91cd\u91cf\uff0c\u4f60\u7684\u76ee\u6807\u662f\u9009\u62e9\u4e00\u7ec4\u7269\u54c1\uff0c\u4f7f\u5f97\u603b\u91cd\u91cf\u4e0d\u8d85\u8fc7\u8f7d\u91cd\u91cf\uff0c\u4e14\u603b\u4ef7\u503c\u6700\u5927\u3002\u5982\u679c\u6bcf\u6b21\u90fd\u9009\u62e9\u6027\u4ef7\u6bd4\u6700\u9ad8\uff08\u4ef7\u503c / \u91cd\u91cf\uff09\u7684\u7269\u54c1\uff0c\u90a3\u4e48\u8d2a\u5fc3\u7b97\u6cd5\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5f97\u5230\u6700\u4f18\u89e3\u3002</li> <li>\u80a1\u7968\u4e70\u5356\u95ee\u9898\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u80a1\u7968\u7684\u5386\u53f2\u4ef7\u683c\uff0c\u4f60\u53ef\u4ee5\u8fdb\u884c\u591a\u6b21\u4e70\u5356\uff0c\u4f46\u5982\u679c\u4f60\u5df2\u7ecf\u6301\u6709\u80a1\u7968\uff0c\u90a3\u4e48\u5728\u5356\u51fa\u4e4b\u524d\u4e0d\u80fd\u518d\u4e70\uff0c\u76ee\u6807\u662f\u83b7\u53d6\u6700\u5927\u5229\u6da6\u3002</li> <li>\u970d\u592b\u66fc\u7f16\u7801\uff1a\u970d\u592b\u66fc\u7f16\u7801\u662f\u4e00\u79cd\u7528\u4e8e\u65e0\u635f\u6570\u636e\u538b\u7f29\u7684\u8d2a\u5fc3\u7b97\u6cd5\u3002\u901a\u8fc7\u6784\u5efa\u970d\u592b\u66fc\u6811\uff0c\u6bcf\u6b21\u9009\u62e9\u51fa\u73b0\u9891\u7387\u6700\u5c0f\u7684\u4e24\u4e2a\u8282\u70b9\u5408\u5e76\uff0c\u6700\u540e\u5f97\u5230\u7684\u970d\u592b\u66fc\u6811\u7684\u5e26\u6743\u8def\u5f84\u957f\u5ea6\uff08\u5373\u7f16\u7801\u957f\u5ea6\uff09\u6700\u5c0f\u3002</li> <li>Dijkstra \u7b97\u6cd5\uff1a\u5b83\u662f\u4e00\u79cd\u89e3\u51b3\u7ed9\u5b9a\u6e90\u9876\u70b9\u5230\u5176\u4f59\u5404\u9876\u70b9\u7684\u6700\u77ed\u8def\u5f84\u95ee\u9898\u7684\u8d2a\u5fc3\u7b97\u6cd5\u3002</li> </ol>"},{"location":"chapter_greedy/max_capacity_problem/","title":"15.3. \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898","text":"<p>Question</p> <p>\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4 \\(ht\\) \uff0c\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u5782\u76f4\u9694\u677f\u7684\u9ad8\u5ea6\u3002\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e24\u4e2a\u9694\u677f\uff0c\u4ee5\u53ca\u5b83\u4eec\u4e4b\u95f4\u7684\u7a7a\u95f4\u53ef\u4ee5\u7ec4\u6210\u4e00\u4e2a\u5bb9\u5668\u3002</p> <p>\u5bb9\u5668\u7684\u5bb9\u91cf\u7b49\u4e8e\u9ad8\u5ea6\u548c\u5bbd\u5ea6\u7684\u4e58\u79ef\uff08\u5373\u9762\u79ef\uff09\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u8f83\u77ed\u7684\u9694\u677f\u51b3\u5b9a\uff0c\u5bbd\u5ea6\u662f\u4e24\u4e2a\u9694\u677f\u7684\u6570\u7ec4\u7d22\u5f15\u4e4b\u5dee\u3002</p> <p>\u8bf7\u5728\u6570\u7ec4\u4e2d\u9009\u62e9\u4e24\u4e2a\u9694\u677f\uff0c\u4f7f\u5f97\u7ec4\u6210\u7684\u5bb9\u5668\u7684\u5bb9\u91cf\u6700\u5927\uff0c\u8fd4\u56de\u6700\u5927\u5bb9\u91cf\u3002</p> <p></p> <p> \u56fe\uff1a\u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e </p> <p>\u5bb9\u5668\u7531\u4efb\u610f\u4e24\u4e2a\u9694\u677f\u56f4\u6210\uff0c\u56e0\u6b64\u672c\u9898\u7684\u72b6\u6001\u4e3a\u4e24\u4e2a\u9694\u677f\u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\([i, j]\\) \u3002</p> <p>\u6839\u636e\u9898\u610f\uff0c\u5bb9\u91cf\u7b49\u4e8e\u9ad8\u5ea6\u4e58\u4ee5\u5bbd\u5ea6\uff0c\u5176\u4e2d\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u5bbd\u5ea6\u662f\u4e24\u9694\u677f\u7684\u7d22\u5f15\u4e4b\u5dee\u3002\u8bbe\u5bb9\u91cf\u4e3a \\(cap[i, j]\\) \uff0c\u5219\u53ef\u5f97\u8ba1\u7b97\u516c\u5f0f\uff1a</p> \\[ cap[i, j] = \\min(ht[i], ht[j]) \\times (j - i) \\] <p>\u8bbe\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u4e24\u4e2a\u9694\u677f\u7684\u7ec4\u5408\u6570\u91cf\uff08\u5373\u72b6\u6001\u603b\u6570\uff09\u4e3a \\(C_n^2 = \\frac{n(n - 1)}{2}\\) \u4e2a\u3002\u6700\u76f4\u63a5\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u7a77\u4e3e\u6240\u6709\u72b6\u6001\uff0c\u4ece\u800c\u6c42\u5f97\u6700\u5927\u5bb9\u91cf\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002</p>"},{"location":"chapter_greedy/max_capacity_problem/#_1","title":"\u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"<p>\u8fd9\u9053\u9898\u8fd8\u6709\u66f4\u9ad8\u6548\u7387\u7684\u89e3\u6cd5\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u73b0\u9009\u53d6\u4e00\u4e2a\u72b6\u6001 \\([i, j]\\) \uff0c\u5176\u6ee1\u8db3\u7d22\u5f15 \\(i &lt; j\\) \u4e14\u9ad8\u5ea6 \\(ht[i] &lt; ht[j]\\) \uff0c\u5373 \\(i\\) \u4e3a\u77ed\u677f\u3001 \\(j\\) \u4e3a\u957f\u677f\u3002</p> <p></p> <p> \u56fe\uff1a\u521d\u59cb\u72b6\u6001 </p> <p>\u6211\u4eec\u53d1\u73b0\uff0c\u5982\u679c\u6b64\u65f6\u5c06\u957f\u677f \\(j\\) \u5411\u77ed\u677f \\(i\\) \u9760\u8fd1\uff0c\u5219\u5bb9\u91cf\u4e00\u5b9a\u53d8\u5c0f\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u79fb\u52a8\u957f\u677f \\(j\\) \u540e\uff1a</p> <ul> <li>\u5bbd\u5ea6 \\(j-i\\) \u80af\u5b9a\u53d8\u5c0f\u3002</li> <li>\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u56e0\u6b64\u9ad8\u5ea6\u53ea\u53ef\u80fd\u4e0d\u53d8\uff08 \\(i\\) \u4ecd\u4e3a\u77ed\u677f\uff09\u6216\u53d8\u5c0f\uff08\u79fb\u52a8\u540e\u7684 \\(j\\) \u6210\u4e3a\u77ed\u677f\uff09\u3002</li> </ul> <p></p> <p> \u56fe\uff1a\u5411\u5185\u79fb\u52a8\u957f\u677f\u540e\u7684\u72b6\u6001 </p> <p>\u53cd\u5411\u601d\u8003\uff0c\u6211\u4eec\u53ea\u6709\u5411\u5185\u6536\u7f29\u77ed\u677f \\(i\\) \uff0c\u624d\u6709\u53ef\u80fd\u4f7f\u5bb9\u91cf\u53d8\u5927\u3002\u56e0\u4e3a\u867d\u7136\u5bbd\u5ea6\u4e00\u5b9a\u53d8\u5c0f\uff0c\u4f46\u9ad8\u5ea6\u53ef\u80fd\u4f1a\u53d8\u5927\uff08\u79fb\u52a8\u540e\u7684\u77ed\u677f \\(i\\) \u53ef\u80fd\u4f1a\u53d8\u957f\uff09\u3002</p> <p></p> <p> \u56fe\uff1a\u5411\u5185\u79fb\u52a8\u957f\u677f\u540e\u7684\u72b6\u6001 </p> <p>\u7531\u6b64\u4fbf\u53ef\u63a8\u51fa\u672c\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\uff1a</p> <ol> <li>\u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6307\u9488 \\(i\\) , \\(j\\) \u5206\u5217\u4e0e\u6570\u7ec4\u4e24\u7aef\u3002</li> <li>\u8ba1\u7b97\u5f53\u524d\u72b6\u6001\u7684\u5bb9\u91cf \\(cap[i, j]\\) \uff0c\u5e76\u66f4\u65b0\u6700\u5927\u5bb9\u91cf\u3002</li> <li>\u6bd4\u8f83\u677f \\(i\\) \u548c \u677f \\(j\\) \u7684\u9ad8\u5ea6\uff0c\u5e76\u5c06\u77ed\u677f\u5411\u5185\u79fb\u52a8\u4e00\u683c\u3002</li> <li>\u5faa\u73af\u6267\u884c\u7b2c <code>2.</code> , <code>3.</code> \u6b65\uff0c\u76f4\u81f3 \\(i\\) \u548c \\(j\\) \u76f8\u9047\u65f6\u7ed3\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> \u56fe\uff1a\u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u8d2a\u5fc3\u8fc7\u7a0b </p>"},{"location":"chapter_greedy/max_capacity_problem/#_2","title":"\u4ee3\u7801\u5b9e\u73b0","text":"<p>\u4ee3\u7801\u5faa\u73af\u6700\u591a \\(n\\) \u8f6e\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002</p> <p>\u53d8\u91cf \\(i\\) , \\(j\\) , \\(res\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust max_capacity.java<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(int[] ht) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nint i = 0, j = ht.length - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nint res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i &lt; j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nint cap = Math.min(ht[i], ht[j]) * (j - i);\nres = Math.max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] &lt; ht[j]) {\ni++;\n} else {\nj--;\n}\n}\nreturn res;\n}\n</code></pre> max_capacity.cpp<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(vector&lt;int&gt; &amp;ht) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nint i = 0, j = ht.size() - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nint res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i &lt; j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nint cap = min(ht[i], ht[j]) * (j - i);\nres = max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] &lt; ht[j]) {\ni++;\n} else {\nj--;\n}\n}\nreturn res;\n}\n</code></pre> max_capacity.py<pre><code>def max_capacity(ht: list[int]) -&gt; int:\n\"\"\"\u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3\"\"\"\n# \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\ni, j = 0, len(ht) - 1\n# \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nres = 0\n# \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile i &lt; j:\n# \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\ncap = min(ht[i], ht[j]) * (j - i)\nres = max(res, cap)\n# \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif ht[i] &lt; ht[j]:\ni += 1\nelse:\nj -= 1\nreturn res\n</code></pre> max_capacity.go<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfunc maxCapacity(ht []int) int {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\ni, j := 0, len(ht)-1\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nres := 0\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nfor i &lt; j {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\ncapacity := int(math.Min(float64(ht[i]), float64(ht[j]))) * (j - i)\nres = int(math.Max(float64(res), float64(capacity)))\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif ht[i] &lt; ht[j] {\ni++\n} else {\nj--\n}\n}\nreturn res\n}\n</code></pre> max_capacity.js<pre><code>[class]{}-[func]{maxCapacity}\n</code></pre> max_capacity.ts<pre><code>[class]{}-[func]{maxCapacity}\n</code></pre> max_capacity.c<pre><code>[class]{}-[func]{maxCapacity}\n</code></pre> max_capacity.cs<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(int[] ht) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nint i = 0, j = ht.Length - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nint res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i &lt; j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nint cap = Math.Min(ht[i], ht[j]) * (j - i);\nres = Math.Max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] &lt; ht[j]) {\ni++;\n} else {\nj--;\n}\n}\nreturn res;\n}\n</code></pre> max_capacity.swift<pre><code>[class]{}-[func]{maxCapacity}\n</code></pre> max_capacity.zig<pre><code>[class]{}-[func]{maxCapacity}\n</code></pre> max_capacity.dart<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nint maxCapacity(List&lt;int&gt; ht) {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nint i = 0, j = ht.length - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nint res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile (i &lt; j) {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nint cap = min(ht[i], ht[j]) * (j - i);\nres = max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif (ht[i] &lt; ht[j]) {\ni++;\n} else {\nj--;\n}\n}\nreturn res;\n}\n</code></pre> max_capacity.rs<pre><code>/* \u6700\u5927\u5bb9\u91cf\uff1a\u8d2a\u5fc3 */\nfn max_capacity(ht: &amp;[i32]) -&gt; i32 {\n// \u521d\u59cb\u5316 i, j \u5206\u5217\u6570\u7ec4\u4e24\u7aef\nlet mut i = 0;\nlet mut j = ht.len() - 1;\n// \u521d\u59cb\u6700\u5927\u5bb9\u91cf\u4e3a 0\nlet mut res = 0;\n// \u5faa\u73af\u8d2a\u5fc3\u9009\u62e9\uff0c\u76f4\u81f3\u4e24\u677f\u76f8\u9047\nwhile i &lt; j {\n// \u66f4\u65b0\u6700\u5927\u5bb9\u91cf\nlet cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;\nres = std::cmp::max(res, cap);\n// \u5411\u5185\u79fb\u52a8\u77ed\u677f\nif ht[i] &lt; ht[j] {\ni += 1;\n} else {\nj -= 1;\n}\n}\nres\n}\n</code></pre>"},{"location":"chapter_greedy/max_capacity_problem/#_3","title":"\u6b63\u786e\u6027\u8bc1\u660e","text":"<p>\u4e4b\u6240\u4ee5\u8d2a\u5fc3\u6bd4\u7a77\u4e3e\u66f4\u5feb\uff0c\u662f\u56e0\u4e3a\u6bcf\u8f6e\u7684\u8d2a\u5fc3\u9009\u62e9\u90fd\u4f1a\u201c\u8df3\u8fc7\u201d\u4e00\u4e9b\u72b6\u6001\u3002</p> <p>\u6bd4\u5982\u5728\u72b6\u6001 \\(cap[i, j]\\) \u4e0b\uff0c\\(i\\) \u4e3a\u77ed\u677f\u3001\\(j\\) \u4e3a\u957f\u677f\u3002\u82e5\u8d2a\u5fc3\u5730\u5c06\u77ed\u677f \\(i\\) \u5411\u5185\u79fb\u52a8\u4e00\u683c\uff0c\u4f1a\u5bfc\u81f4\u4ee5\u4e0b\u72b6\u6001\u88ab\u201c\u8df3\u8fc7\u201d\u3002\u8fd9\u610f\u5473\u7740\u4e4b\u540e\u65e0\u6cd5\u9a8c\u8bc1\u8fd9\u4e9b\u72b6\u6001\u7684\u5bb9\u91cf\u5927\u5c0f\u3002</p> \\[ cap[i, i+1], cap[i, i+2], \\cdots, cap[i, j-2], cap[i, j-1] \\] <p></p> <p> \u56fe\uff1a\u79fb\u52a8\u77ed\u677f\u5bfc\u81f4\u88ab\u8df3\u8fc7\u7684\u72b6\u6001 </p> <p>\u89c2\u5bdf\u53d1\u73b0\uff0c\u8fd9\u4e9b\u88ab\u8df3\u8fc7\u7684\u72b6\u6001\u5b9e\u9645\u4e0a\u5c31\u662f\u5c06\u957f\u677f \\(j\\) \u5411\u5185\u79fb\u52a8\u7684\u6240\u6709\u72b6\u6001\u3002\u800c\u5728\u7b2c\u4e8c\u6b65\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u8bc1\u660e\u5185\u79fb\u957f\u677f\u4e00\u5b9a\u4f1a\u5bfc\u81f4\u5bb9\u91cf\u53d8\u5c0f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u88ab\u8df3\u8fc7\u7684\u72b6\u6001\u90fd\u4e0d\u53ef\u80fd\u662f\u6700\u4f18\u89e3\uff0c\u8df3\u8fc7\u5b83\u4eec\u4e0d\u4f1a\u5bfc\u81f4\u9519\u8fc7\u6700\u4f18\u89e3\u3002</p> <p>\u4ee5\u4e0a\u7684\u5206\u6790\u8bf4\u660e\uff0c\u79fb\u52a8\u77ed\u677f\u7684\u64cd\u4f5c\u662f\u201c\u5b89\u5168\u201d\u7684\uff0c\u8d2a\u5fc3\u7b56\u7565\u662f\u6709\u6548\u7684\u3002</p>"},{"location":"chapter_greedy/max_product_cutting_problem/","title":"15.4. \u00a0 \u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898","text":"<p>Question</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570 \\(n\\) \uff0c\u5c06\u5176\u5207\u5206\u4e3a\u81f3\u5c11\u4e24\u4e2a\u6b63\u6574\u6570\u7684\u548c\uff0c\u6c42\u5207\u5206\u540e\u6240\u6709\u6574\u6570\u7684\u4e58\u79ef\u6700\u5927\u662f\u591a\u5c11\u3002</p> <p></p> <p> \u56fe\uff1a\u6700\u5927\u5207\u5206\u4e58\u79ef\u7684\u95ee\u9898\u5b9a\u4e49 </p> <p>\u5047\u8bbe\u6211\u4eec\u5c06 \\(n\\) \u5207\u5206\u4e3a \\(m\\) \u4e2a\u6574\u6570\u56e0\u5b50\uff0c\u5176\u4e2d\u7b2c \\(i\\) \u4e2a\u56e0\u5b50\u8bb0\u4e3a \\(n_i\\) \uff0c\u5373</p> \\[ n = \\sum_{i=1}^{m}n_i \\] <p>\u672c\u9898\u76ee\u6807\u662f\u6c42\u5f97\u6240\u6709\u6574\u6570\u56e0\u5b50\u7684\u6700\u5927\u4e58\u79ef\uff0c\u5373</p> \\[ \\max(\\prod_{i=1}^{m}n_i) \\] <p>\u6211\u4eec\u9700\u8981\u601d\u8003\u7684\u662f\uff1a\u5207\u5206\u6570\u91cf \\(m\\) \u5e94\u8be5\u591a\u5927\uff0c\u6bcf\u4e2a \\(n_i\\) \u5e94\u8be5\u662f\u591a\u5c11\uff1f</p>"},{"location":"chapter_greedy/max_product_cutting_problem/#_1","title":"\u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"<p>\u6839\u636e\u7ecf\u9a8c\uff0c\u4e24\u4e2a\u6574\u6570\u7684\u4e58\u79ef\u5f80\u5f80\u6bd4\u5b83\u4eec\u7684\u52a0\u548c\u66f4\u5927\u3002\u5047\u8bbe\u4ece \\(n\\) \u4e2d\u5206\u51fa\u4e00\u4e2a\u56e0\u5b50 \\(2\\) \uff0c\u5219\u5b83\u4eec\u7684\u4e58\u79ef\u4e3a \\(2(n-2)\\) \u3002\u6211\u4eec\u5c06\u8be5\u4e58\u79ef\u4e0e \\(n\\) \u4f5c\u6bd4\u8f83\uff1a</p> \\[ \\begin{aligned} 2(n-2) &amp; \\geq n \\newline 2n - n - 4 &amp; \\geq 0 \\newline n &amp; \\geq 4 \\end{aligned} \\] <p>\u6211\u4eec\u53d1\u73b0\u5f53 \\(n \\geq 4\\) \u65f6\uff0c\u5207\u5206\u51fa\u4e00\u4e2a \\(2\\) \u540e\u4e58\u79ef\u4f1a\u53d8\u5927\uff0c\u8fd9\u8bf4\u660e\u5927\u4e8e\u7b49\u4e8e \\(4\\) \u7684\u6574\u6570\u90fd\u5e94\u8be5\u88ab\u5207\u5206\u3002</p> <p>\u8d2a\u5fc3\u7b56\u7565\u4e00\uff1a\u5982\u679c\u5207\u5206\u65b9\u6848\u4e2d\u5305\u542b \\(\\geq 4\\) \u7684\u56e0\u5b50\uff0c\u90a3\u4e48\u5b83\u5c31\u5e94\u8be5\u88ab\u7ee7\u7eed\u5207\u5206\u3002\u6700\u7ec8\u7684\u5207\u5206\u65b9\u6848\u53ea\u5e94\u51fa\u73b0 \\(1\\) , \\(2\\) , \\(3\\) \u8fd9\u4e09\u79cd\u56e0\u5b50\u3002</p> <p></p> <p> \u56fe\uff1a\u5207\u5206\u5bfc\u81f4\u4e58\u79ef\u53d8\u5927 </p> <p>\u63a5\u4e0b\u6765\u601d\u8003\u54ea\u4e2a\u56e0\u5b50\u662f\u6700\u4f18\u7684\u3002\u5728 \\(1\\) , \\(2\\) , \\(3\\) \u8fd9\u4e09\u4e2a\u56e0\u5b50\u4e2d\uff0c\u663e\u7136 \\(1\\) \u662f\u6700\u5dee\u7684\uff0c\u56e0\u4e3a \\(1 \\times (n-1) &lt; n\\) \u6052\u6210\u7acb\uff0c\u5373\u5207\u5206\u51fa \\(1\\) \u53cd\u800c\u4f1a\u5bfc\u81f4\u4e58\u79ef\u51cf\u5c0f\u3002</p> <p>\u6211\u4eec\u53d1\u73b0\uff0c\u5f53 \\(n = 6\\) \u65f6\uff0c\u6709 \\(3 \\times 3 &gt; 2 \\times 2 \\times 2\\) \u3002\u8fd9\u610f\u5473\u7740\u5207\u5206\u51fa \\(3\\) \u6bd4\u5207\u5206\u51fa \\(2\\) \u66f4\u4f18\u3002</p> <p>\u8d2a\u5fc3\u7b56\u7565\u4e8c\uff1a\u5728\u5207\u5206\u65b9\u6848\u4e2d\uff0c\u6700\u591a\u53ea\u5e94\u5b58\u5728\u4e24\u4e2a \\(2\\) \u3002\u56e0\u4e3a\u4e09\u4e2a \\(2\\) \u603b\u662f\u53ef\u4ee5\u88ab\u66ff\u6362\u4e3a\u4e24\u4e2a \\(3\\) \uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5927\u4e58\u79ef\u3002</p> <p></p> <p> \u56fe\uff1a\u6700\u4f18\u5207\u5206\u56e0\u5b50 </p> <p>\u603b\u7ed3\u4ee5\u4e0a\uff0c\u53ef\u63a8\u51fa\u8d2a\u5fc3\u7b56\u7565\uff1a</p> <ol> <li>\u8f93\u5165\u6574\u6570 \\(n\\) \uff0c\u4ece\u5176\u4e0d\u65ad\u5730\u5207\u5206\u51fa\u56e0\u5b50 \\(3\\) \uff0c\u76f4\u81f3\u4f59\u6570\u4e3a \\(0\\) , \\(1\\) , \\(2\\) \u3002</li> <li>\u5f53\u4f59\u6570\u4e3a \\(0\\) \u65f6\uff0c\u4ee3\u8868 \\(n\\) \u662f \\(3\\) \u7684\u500d\u6570\uff0c\u56e0\u6b64\u4e0d\u505a\u4efb\u4f55\u5904\u7406\u3002</li> <li>\u5f53\u4f59\u6570\u4e3a \\(2\\) \u65f6\uff0c\u4e0d\u7ee7\u7eed\u5212\u5206\uff0c\u4fdd\u7559\u4e4b\u3002</li> <li>\u5f53\u4f59\u6570\u4e3a \\(1\\) \u65f6\uff0c\u7531\u4e8e \\(2 \\times 2 &gt; 1 \\times 3\\) \uff0c\u56e0\u6b64\u5e94\u5c06\u6700\u540e\u4e00\u4e2a \\(3\\) \u66ff\u6362\u4e3a \\(2\\) \u3002</li> </ol>"},{"location":"chapter_greedy/max_product_cutting_problem/#_2","title":"\u4ee3\u7801\u5b9e\u73b0","text":"<p>\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u65e0\u9700\u901a\u8fc7\u5faa\u73af\u6765\u5207\u5206\u6574\u6570\uff0c\u800c\u53ef\u4ee5\u5229\u7528\u5411\u4e0b\u6574\u9664\u8fd0\u7b97\u5f97\u5230 \\(3\\) \u7684\u4e2a\u6570 \\(a\\) \uff0c\u7528\u53d6\u6a21\u8fd0\u7b97\u5f97\u5230\u4f59\u6570 \\(b\\) \uff0c\u6b64\u65f6\u6709\uff1a</p> \\[ n = 3 a + b \\] <p>\u8bf7\u6ce8\u610f\uff0c\u5bf9\u4e8e \\(n \\leq 3\\) \u7684\u8fb9\u754c\u60c5\u51b5\uff0c\u5fc5\u987b\u62c6\u5206\u51fa\u4e00\u4e2a \\(1\\) \uff0c\u4e58\u79ef\u4e3a \\(1 \\times (n - 1)\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust max_product_cutting.java<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n// \u5f53 n &lt;= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n &lt;= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nint a = n / 3;\nint b = n % 3;\nif (b == 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn (int) Math.pow(3, a - 1) * 2 * 2;\n}\nif (b == 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int) Math.pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int) Math.pow(3, a);\n}\n</code></pre> max_product_cutting.cpp<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n// \u5f53 n &lt;= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n &lt;= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nint a = n / 3;\nint b = n % 3;\nif (b == 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn (int)pow(3, a - 1) * 2 * 2;\n}\nif (b == 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int)pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int)pow(3, a);\n}\n</code></pre> max_product_cutting.py<pre><code>def max_product_cutting(n: int) -&gt; int:\n\"\"\"\u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3\"\"\"\n# \u5f53 n &lt;= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif n &lt;= 3:\nreturn 1 * (n - 1)\n# \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\na, b = n // 3, n % 3\nif b == 1:\n# \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn int(math.pow(3, a - 1)) * 2 * 2\nif b == 2:\n# \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn int(math.pow(3, a)) * 2\n# \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn int(math.pow(3, a))\n</code></pre> max_product_cutting.go<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfunc maxProductCutting(n int) int {\n// \u5f53 n &lt;= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif n &lt;= 3 {\nreturn 1 * (n - 1)\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\na := n / 3\nb := n % 3\nif b == 1 {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn int(math.Pow(3, float64(a-1))) * 2 * 2\n}\nif b == 2 {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn int(math.Pow(3, float64(a))) * 2\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn int(math.Pow(3, float64(a)))\n}\n</code></pre> max_product_cutting.js<pre><code>[class]{}-[func]{maxProductCutting}\n</code></pre> max_product_cutting.ts<pre><code>[class]{}-[func]{maxProductCutting}\n</code></pre> max_product_cutting.c<pre><code>[class]{}-[func]{maxProductCutting}\n</code></pre> max_product_cutting.cs<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n// \u5f53 n &lt;= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n &lt;= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nint a = n / 3;\nint b = n % 3;\nif (b == 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn (int)Math.Pow(3, a - 1) * 2 * 2;\n}\nif (b == 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int)Math.Pow(3, a) * 2;\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (int)Math.Pow(3, a);\n}\n</code></pre> max_product_cutting.swift<pre><code>[class]{}-[func]{maxProductCutting}\n</code></pre> max_product_cutting.zig<pre><code>[class]{}-[func]{maxProductCutting}\n</code></pre> max_product_cutting.dart<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nint maxProductCutting(int n) {\n// \u5f53 n &lt;= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif (n &lt;= 3) {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nint a = n ~/ 3;\nint b = n % 3;\nif (b == 1) {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\nreturn (pow(3, a - 1) * 2 * 2).toInt();\n}\nif (b == 2) {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn (pow(3, a) * 2).toInt();\n}\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\nreturn pow(3, a).toInt();\n}\n</code></pre> max_product_cutting.rs<pre><code>/* \u6700\u5927\u5207\u5206\u4e58\u79ef\uff1a\u8d2a\u5fc3 */\nfn max_product_cutting(n: i32) -&gt; i32 {\n// \u5f53 n &lt;= 3 \u65f6\uff0c\u5fc5\u987b\u5207\u5206\u51fa\u4e00\u4e2a 1\nif n &lt;= 3 {\nreturn 1 * (n - 1);\n}\n// \u8d2a\u5fc3\u5730\u5207\u5206\u51fa 3 \uff0ca \u4e3a 3 \u7684\u4e2a\u6570\uff0cb \u4e3a\u4f59\u6570\nlet a = n / 3;\nlet b = n % 3;\nif b == 1 {\n// \u5f53\u4f59\u6570\u4e3a 1 \u65f6\uff0c\u5c06\u4e00\u5bf9 1 * 3 \u8f6c\u5316\u4e3a 2 * 2\n3_i32.pow(a as u32 - 1) * 2 * 2\n} else if b == 2 {\n// \u5f53\u4f59\u6570\u4e3a 2 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n3_i32.pow(a as u32) * 2\n} else {\n// \u5f53\u4f59\u6570\u4e3a 0 \u65f6\uff0c\u4e0d\u505a\u5904\u7406\n3_i32.pow(a as u32)\n}\n}\n</code></pre> <p></p> <p> \u56fe\uff1a\u6700\u5927\u5207\u5206\u4e58\u79ef\u7684\u8ba1\u7b97\u65b9\u6cd5 </p> <p>\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u5e42\u8fd0\u7b97\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u5e38\u7528\u7684\u5e42\u8ba1\u7b97\u51fd\u6570\u6709\u4e09\u79cd\uff1a</p> <ul> <li>\u8fd0\u7b97\u7b26 <code>**</code> \u548c\u51fd\u6570 <code>pow()</code> \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log\u2061 a)\\) \u3002</li> <li>\u51fd\u6570 <code>math.pow()</code> \u5185\u90e8\u8c03\u7528 C \u8bed\u8a00\u5e93\u7684 <code>pow()</code> \u51fd\u6570\uff0c\u5176\u6267\u884c\u6d6e\u70b9\u53d6\u5e42\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002</li> </ul> <p>\u53d8\u91cf \\(a\\) , \\(b\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002</p>"},{"location":"chapter_greedy/max_product_cutting_problem/#_3","title":"\u6b63\u786e\u6027\u8bc1\u660e","text":"<p>\u4f7f\u7528\u53cd\u8bc1\u6cd5\uff0c\u53ea\u5206\u6790 \\(n \\geq 3\\) \u7684\u60c5\u51b5\u3002</p> <ol> <li>\u6240\u6709\u56e0\u5b50 \\(\\leq 3\\) :\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5b58\u5728 \\(\\geq 4\\) \u7684\u56e0\u5b50 \\(x\\) \uff0c\u90a3\u4e48\u4e00\u5b9a\u53ef\u4ee5\u5c06\u5176\u7ee7\u7eed\u5212\u5206\u4e3a \\(2(x-2)\\) \uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5927\u7684\u4e58\u79ef\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002</li> <li>\u5207\u5206\u65b9\u6848\u4e0d\u5305\u542b \\(1\\) :\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5b58\u5728\u4e00\u4e2a\u56e0\u5b50 \\(1\\) \uff0c\u90a3\u4e48\u5b83\u4e00\u5b9a\u53ef\u4ee5\u5408\u5e76\u5165\u53e6\u5916\u4e00\u4e2a\u56e0\u5b50\u4e2d\uff0c\u4ee5\u83b7\u53d6\u66f4\u5927\u4e58\u79ef\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002</li> <li>\u5207\u5206\u65b9\u6848\u6700\u591a\u5305\u542b\u4e24\u4e2a \\(2\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5305\u542b\u4e09\u4e2a \\(2\\) \uff0c\u90a3\u4e48\u4e00\u5b9a\u53ef\u4ee5\u66ff\u6362\u4e3a\u4e24\u4e2a \\(3\\) \uff0c\u4e58\u79ef\u66f4\u5927\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002</li> </ol>"},{"location":"chapter_greedy/summary/","title":"15.5. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u8d2a\u5fc3\u7b97\u6cd5\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u6700\u4f18\u5316\u95ee\u9898\uff0c\u5176\u539f\u7406\u662f\u5728\u6bcf\u4e2a\u51b3\u7b56\u9636\u6bb5\u90fd\u505a\u51fa\u5c40\u90e8\u6700\u4f18\u7684\u51b3\u7b56\uff0c\u4ee5\u671f\u671b\u83b7\u5f97\u5168\u5c40\u6700\u4f18\u89e3\u3002</li> <li>\u8d2a\u5fc3\u7b97\u6cd5\u4f1a\u8fed\u4ee3\u5730\u505a\u51fa\u4e00\u4e2a\u53c8\u4e00\u4e2a\u7684\u8d2a\u5fc3\u9009\u62e9\uff0c\u6bcf\u8f6e\u90fd\u5c06\u95ee\u9898\u8f6c\u5316\u6210\u4e00\u4e2a\u89c4\u6a21\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u5230\u95ee\u9898\u88ab\u89e3\u51b3\u3002</li> <li>\u8d2a\u5fc3\u7b97\u6cd5\u4e0d\u4ec5\u5b9e\u73b0\u7b80\u5355\uff0c\u8fd8\u5177\u6709\u5f88\u9ad8\u7684\u89e3\u9898\u6548\u7387\u3002\u76f8\u6bd4\u4e8e\u52a8\u6001\u89c4\u5212\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u66f4\u4f4e\u3002</li> <li>\u5728\u96f6\u94b1\u5151\u6362\u95ee\u9898\u4e2d\uff0c\u5bf9\u4e8e\u67d0\u4e9b\u786c\u5e01\u7ec4\u5408\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ef\u4ee5\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\uff1b\u5bf9\u4e8e\u53e6\u5916\u4e00\u4e9b\u786c\u5e01\u7ec4\u5408\u5219\u4e0d\u7136\uff0c\u8d2a\u5fc3\u7b97\u6cd5\u53ef\u80fd\u627e\u5230\u5f88\u5dee\u7684\u89e3\u3002</li> <li>\u9002\u5408\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u7684\u95ee\u9898\u5177\u6709\u4e24\u5927\u6027\u8d28\uff1a\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u3002\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u4ee3\u8868\u8d2a\u5fc3\u7b56\u7565\u7684\u6709\u6548\u6027\u3002</li> <li>\u5bf9\u4e8e\u67d0\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u8d2a\u5fc3\u9009\u62e9\u6027\u8d28\u7684\u8bc1\u660e\u5e76\u4e0d\u7b80\u5355\u3002\u76f8\u5bf9\u6765\u8bf4\uff0c\u8bc1\u4f2a\u66f4\u52a0\u5bb9\u6613\uff0c\u4f8b\u5982\u96f6\u94b1\u5151\u6362\u95ee\u9898\u3002</li> <li>\u6c42\u89e3\u8d2a\u5fc3\u95ee\u9898\u4e3b\u8981\u5206\u4e3a\u4e09\u6b65\uff1a\u95ee\u9898\u5206\u6790\u3001\u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a\u3001\u6b63\u786e\u6027\u8bc1\u660e\u3002\u5176\u4e2d\uff0c\u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a\u662f\u6838\u5fc3\u6b65\u9aa4\uff0c\u6b63\u786e\u6027\u8bc1\u660e\u5f80\u5f80\u662f\u96be\u70b9\u3002</li> <li>\u5206\u6570\u80cc\u5305\u95ee\u9898\u5728 0-1 \u80cc\u5305\u7684\u57fa\u7840\u4e0a\uff0c\u5141\u8bb8\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u53ef\u4f7f\u7528\u8d2a\u5fc3\u7b97\u6cd5\u6c42\u89e3\u3002\u8d2a\u5fc3\u7b56\u7565\u7684\u6b63\u786e\u6027\u53ef\u4ee5\u4f7f\u7528\u53cd\u8bc1\u6cd5\u6765\u8bc1\u660e\u3002</li> <li>\u6700\u5927\u5bb9\u91cf\u95ee\u9898\u53ef\u4f7f\u7528\u7a77\u4e3e\u6cd5\u6c42\u89e3\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002\u901a\u8fc7\u8bbe\u8ba1\u8d2a\u5fc3\u7b56\u7565\uff0c\u6bcf\u8f6e\u5411\u5185\u79fb\u52a8\u77ed\u677f\uff0c\u53ef\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(n)\\) \u3002</li> <li>\u5728\u6700\u5927\u5207\u5206\u4e58\u79ef\u95ee\u9898\u4e2d\uff0c\u6211\u4eec\u5148\u540e\u63a8\u7406\u51fa\u4e24\u4e2a\u8d2a\u5fc3\u7b56\u7565\uff1a\\(\\geq 4\\) \u7684\u6574\u6570\u90fd\u5e94\u8be5\u7ee7\u7eed\u5207\u5206\u3001\u6700\u4f18\u5207\u5206\u56e0\u5b50\u4e3a \\(3\\) \u3002\u4ee3\u7801\u4e2d\u5305\u542b\u5e42\u8fd0\u7b97\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u5e42\u8fd0\u7b97\u5b9e\u73b0\u65b9\u6cd5\uff0c\u901a\u5e38\u4e3a \\(O(1)\\) \u6216 \\(O(\\log n)\\) \u3002</li> </ul>"},{"location":"chapter_hashing/","title":"6. \u00a0 \u6563\u5217\u8868","text":"<p>Abstract</p> <p>\u5728\u8ba1\u7b97\u673a\u4e16\u754c\u4e2d\uff0c\u6563\u5217\u8868\u5982\u540c\u4e00\u4f4d\u667a\u80fd\u7684\u56fe\u4e66\u7ba1\u7406\u5458\u3002</p> <p>\u4ed6\u77e5\u9053\u5982\u4f55\u8ba1\u7b97\u7d22\u4e66\u53f7\uff0c\u4ece\u800c\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u76ee\u6807\u4e66\u7c4d\u3002</p>"},{"location":"chapter_hashing/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>6.1 \u00a0 \u54c8\u5e0c\u8868</li> <li>6.2 \u00a0 \u54c8\u5e0c\u51b2\u7a81</li> <li>6.3 \u00a0 \u54c8\u5e0c\u7b97\u6cd5</li> <li>6.4 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_hashing/hash_algorithm/","title":"6.3. \u00a0 \u54c8\u5e0c\u7b97\u6cd5","text":"<p>\u5728\u4e0a\u4e24\u8282\u4e2d\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u54c8\u5e0c\u8868\u7684\u5de5\u4f5c\u539f\u7406\u548c\u54c8\u5e0c\u51b2\u7a81\u7684\u5904\u7406\u65b9\u6cd5\u3002\u7136\u800c\u65e0\u8bba\u662f\u5f00\u653e\u5bfb\u5740\u8fd8\u662f\u94fe\u5730\u5740\u6cd5\uff0c\u5b83\u4eec\u53ea\u80fd\u4fdd\u8bc1\u54c8\u5e0c\u8868\u53ef\u4ee5\u5728\u53d1\u751f\u51b2\u7a81\u65f6\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u65e0\u6cd5\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u3002</p> <p>\u5982\u679c\u54c8\u5e0c\u51b2\u7a81\u8fc7\u4e8e\u9891\u7e41\uff0c\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u5219\u4f1a\u6025\u5267\u52a3\u5316\u3002\u5bf9\u4e8e\u94fe\u5730\u5740\u54c8\u5e0c\u8868\uff0c\u7406\u60f3\u60c5\u51b5\u4e0b\u952e\u503c\u5bf9\u5e73\u5747\u5206\u5e03\u5728\u5404\u4e2a\u6876\u4e2d\uff0c\u8fbe\u5230\u6700\u4f73\u67e5\u8be2\u6548\u7387\uff1b\u6700\u5dee\u60c5\u51b5\u4e0b\u6240\u6709\u952e\u503c\u5bf9\u90fd\u88ab\u5b58\u50a8\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \u3002</p> <p></p> <p> \u56fe\uff1a\u54c8\u5e0c\u51b2\u7a81\u7684\u6700\u4f73\u4e0e\u6700\u5dee\u60c5\u51b5 </p> <p>\u952e\u503c\u5bf9\u7684\u5206\u5e03\u60c5\u51b5\u7531\u54c8\u5e0c\u51fd\u6570\u51b3\u5b9a\u3002\u56de\u5fc6\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u6b65\u9aa4\uff0c\u5148\u8ba1\u7b97\u54c8\u5e0c\u503c\uff0c\u518d\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\uff1a</p> <pre><code>index = hash(key) % capacity\n</code></pre> <p>\u89c2\u5bdf\u4ee5\u4e0a\u516c\u5f0f\uff0c\u5f53\u54c8\u5e0c\u8868\u5bb9\u91cf <code>capacity</code> \u56fa\u5b9a\u65f6\uff0c\u54c8\u5e0c\u7b97\u6cd5 <code>hash()</code> \u51b3\u5b9a\u4e86\u8f93\u51fa\u503c\uff0c\u8fdb\u800c\u51b3\u5b9a\u4e86\u952e\u503c\u5bf9\u5728\u54c8\u5e0c\u8868\u4e2d\u7684\u5206\u5e03\u60c5\u51b5\u3002</p> <p>\u8fd9\u610f\u5473\u7740\uff0c\u4e3a\u4e86\u51cf\u5c0f\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u6982\u7387\uff0c\u6211\u4eec\u5e94\u5f53\u5c06\u6ce8\u610f\u529b\u96c6\u4e2d\u5728\u54c8\u5e0c\u7b97\u6cd5 <code>hash()</code> \u7684\u8bbe\u8ba1\u4e0a\u3002</p>"},{"location":"chapter_hashing/hash_algorithm/#631","title":"6.3.1. \u00a0 \u54c8\u5e0c\u7b97\u6cd5\u7684\u76ee\u6807","text":"<p>\u4e3a\u4e86\u5b9e\u73b0\u201c\u65e2\u5feb\u53c8\u7a33\u201d\u7684\u54c8\u5e0c\u8868\u6570\u636e\u7ed3\u6784\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5e94\u5305\u542b\u4ee5\u4e0b\u7279\u70b9\uff1a</p> <ul> <li>\u786e\u5b9a\u6027\uff1a\u5bf9\u4e8e\u76f8\u540c\u7684\u8f93\u5165\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5e94\u59cb\u7ec8\u4ea7\u751f\u76f8\u540c\u7684\u8f93\u51fa\u3002\u8fd9\u6837\u624d\u80fd\u786e\u4fdd\u54c8\u5e0c\u8868\u662f\u53ef\u9760\u7684\u3002</li> <li>\u6548\u7387\u9ad8\uff1a\u8ba1\u7b97\u54c8\u5e0c\u503c\u7684\u8fc7\u7a0b\u5e94\u8be5\u8db3\u591f\u5feb\u3002\u8ba1\u7b97\u5f00\u9500\u8d8a\u5c0f\uff0c\u54c8\u5e0c\u8868\u7684\u5b9e\u7528\u6027\u8d8a\u9ad8\u3002</li> <li>\u5747\u5300\u5206\u5e03\uff1a\u54c8\u5e0c\u7b97\u6cd5\u5e94\u4f7f\u5f97\u952e\u503c\u5bf9\u5e73\u5747\u5206\u5e03\u5728\u54c8\u5e0c\u8868\u4e2d\u3002\u5206\u5e03\u8d8a\u5e73\u5747\uff0c\u54c8\u5e0c\u51b2\u7a81\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\u3002</li> </ul> <p>\u5b9e\u9645\u4e0a\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9664\u4e86\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u54c8\u5e0c\u8868\uff0c\u8fd8\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5176\u4ed6\u9886\u57df\u4e2d\u3002\u4e3e\u4e24\u4e2a\u4f8b\u5b50\uff1a</p> <ul> <li>\u5bc6\u7801\u5b58\u50a8\uff1a\u4e3a\u4e86\u4fdd\u62a4\u7528\u6237\u5bc6\u7801\u7684\u5b89\u5168\uff0c\u7cfb\u7edf\u901a\u5e38\u4e0d\u4f1a\u76f4\u63a5\u5b58\u50a8\u7528\u6237\u7684\u660e\u6587\u5bc6\u7801\uff0c\u800c\u662f\u5b58\u50a8\u5bc6\u7801\u7684\u54c8\u5e0c\u503c\u3002\u5f53\u7528\u6237\u8f93\u5165\u5bc6\u7801\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5bf9\u8f93\u5165\u7684\u5bc6\u7801\u8ba1\u7b97\u54c8\u5e0c\u503c\uff0c\u7136\u540e\u4e0e\u5b58\u50a8\u7684\u54c8\u5e0c\u503c\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u4e24\u8005\u5339\u914d\uff0c\u90a3\u4e48\u5bc6\u7801\u5c31\u88ab\u89c6\u4e3a\u6b63\u786e\u3002</li> <li>\u6570\u636e\u5b8c\u6574\u6027\u68c0\u67e5\uff1a\u6570\u636e\u53d1\u9001\u65b9\u53ef\u4ee5\u8ba1\u7b97\u6570\u636e\u7684\u54c8\u5e0c\u503c\u5e76\u5c06\u5176\u4e00\u540c\u53d1\u9001\uff1b\u63a5\u6536\u65b9\u53ef\u4ee5\u91cd\u65b0\u8ba1\u7b97\u63a5\u6536\u5230\u7684\u6570\u636e\u7684\u54c8\u5e0c\u503c\uff0c\u5e76\u4e0e\u63a5\u6536\u5230\u7684\u54c8\u5e0c\u503c\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u4e24\u8005\u5339\u914d\uff0c\u90a3\u4e48\u6570\u636e\u5c31\u88ab\u89c6\u4e3a\u5b8c\u6574\u7684\u3002</li> </ul> <p>\u5bf9\u4e8e\u5bc6\u7801\u5b66\u7684\u76f8\u5173\u5e94\u7528\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9700\u8981\u6ee1\u8db3\u66f4\u9ad8\u7684\u5b89\u5168\u6807\u51c6\uff0c\u4ee5\u9632\u6b62\u4ece\u54c8\u5e0c\u503c\u63a8\u5bfc\u51fa\u539f\u59cb\u5bc6\u7801\u7b49\u9006\u5411\u5de5\u7a0b\uff0c\u5305\u62ec\uff1a</p> <ul> <li>\u6297\u78b0\u649e\u6027\uff1a\u5e94\u5f53\u6781\u5176\u56f0\u96be\u627e\u5230\u4e24\u4e2a\u4e0d\u540c\u7684\u8f93\u5165\uff0c\u4f7f\u5f97\u5b83\u4eec\u7684\u54c8\u5e0c\u503c\u76f8\u540c\u3002</li> <li>\u96ea\u5d29\u6548\u5e94\uff1a\u8f93\u5165\u7684\u5fae\u5c0f\u53d8\u5316\u5e94\u5f53\u5bfc\u81f4\u8f93\u51fa\u7684\u663e\u8457\u4e14\u4e0d\u53ef\u9884\u6d4b\u7684\u53d8\u5316\u3002</li> </ul> <p>\u8bf7\u6ce8\u610f\uff0c\u201c\u5747\u5300\u5206\u5e03\u201d\u4e0e\u201c\u6297\u78b0\u649e\u6027\u201d\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u6982\u5ff5\uff0c\u6ee1\u8db3\u5747\u5300\u5206\u5e03\u4e0d\u4e00\u5b9a\u6ee1\u8db3\u6297\u78b0\u649e\u6027\u3002\u4f8b\u5982\uff0c\u5728\u968f\u673a\u8f93\u5165 <code>key</code> \u4e0b\uff0c\u54c8\u5e0c\u51fd\u6570 <code>key % 100</code> \u53ef\u4ee5\u4ea7\u751f\u5747\u5300\u5206\u5e03\u7684\u8f93\u51fa\u3002\u7136\u800c\u8be5\u54c8\u5e0c\u7b97\u6cd5\u8fc7\u4e8e\u7b80\u5355\uff0c\u6240\u6709\u540e\u4e24\u4f4d\u76f8\u7b49\u7684 <code>key</code> \u7684\u8f93\u51fa\u90fd\u76f8\u540c\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u4ece\u54c8\u5e0c\u503c\u53cd\u63a8\u51fa\u53ef\u7528\u7684 <code>key</code> \uff0c\u4ece\u800c\u7834\u89e3\u5bc6\u7801\u3002</p>"},{"location":"chapter_hashing/hash_algorithm/#632","title":"6.3.2. \u00a0 \u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1","text":"<p>\u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1\u662f\u4e00\u4e2a\u590d\u6742\u4e14\u9700\u8981\u8003\u8651\u8bb8\u591a\u56e0\u7d20\u7684\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u7b80\u5355\u573a\u666f\uff0c\u6211\u4eec\u4e5f\u80fd\u8bbe\u8ba1\u4e00\u4e9b\u7b80\u5355\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002\u4ee5\u5b57\u7b26\u4e32\u54c8\u5e0c\u4e3a\u4f8b\uff1a</p> <ul> <li>\u52a0\u6cd5\u54c8\u5e0c\uff1a\u5bf9\u8f93\u5165\u7684\u6bcf\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u8fdb\u884c\u76f8\u52a0\uff0c\u5c06\u5f97\u5230\u7684\u603b\u548c\u4f5c\u4e3a\u54c8\u5e0c\u503c\u3002</li> <li>\u4e58\u6cd5\u54c8\u5e0c\uff1a\u5229\u7528\u4e86\u4e58\u6cd5\u7684\u4e0d\u76f8\u5173\u6027\uff0c\u6bcf\u8f6e\u4e58\u4ee5\u4e00\u4e2a\u5e38\u6570\uff0c\u5c06\u5404\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u7d2f\u79ef\u5230\u54c8\u5e0c\u503c\u4e2d\u3002</li> <li>\u5f02\u6216\u54c8\u5e0c\uff1a\u5c06\u8f93\u5165\u6570\u636e\u7684\u6bcf\u4e2a\u5143\u7d20\u901a\u8fc7\u5f02\u6216\u64cd\u4f5c\u7d2f\u79ef\u5230\u4e00\u4e2a\u54c8\u5e0c\u503c\u4e2d\u3002</li> <li>\u65cb\u8f6c\u54c8\u5e0c\uff1a\u5c06\u6bcf\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u7d2f\u79ef\u5230\u4e00\u4e2a\u54c8\u5e0c\u503c\u4e2d\uff0c\u6bcf\u6b21\u7d2f\u79ef\u4e4b\u524d\u90fd\u4f1a\u5bf9\u54c8\u5e0c\u503c\u8fdb\u884c\u65cb\u8f6c\u64cd\u4f5c\u3002</li> </ul> JavaC++PythonGoJSTSCC#SwiftZigDartRust simple_hash.java<pre><code>/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\nlong hash = 0;\nfinal int MODULUS = 1000000007;\nfor (char c : key.toCharArray()) {\nhash = (hash + (int) c) % MODULUS;\n}\nreturn (int) hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\nlong hash = 0;\nfinal int MODULUS = 1000000007;\nfor (char c : key.toCharArray()) {\nhash = (31 * hash + (int) c) % MODULUS;\n}\nreturn (int) hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (char c : key.toCharArray()) {\nhash ^= (int) c;\n}\nreturn hash &amp; MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\nlong hash = 0;\nfinal int MODULUS = 1000000007;\nfor (char c : key.toCharArray()) {\nhash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ (int) c) % MODULUS;\n}\nreturn (int) hash;\n}\n</code></pre> simple_hash.cpp<pre><code>/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(string key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (unsigned char c : key) {\nhash = (hash + (int)c) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(string key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (unsigned char c : key) {\nhash = (31 * hash + (int)c) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(string key) {\nint hash = 0;\nconst int MODULUS = 1000000007;\nfor (unsigned char c : key) {\ncout&lt;&lt;(int)c&lt;&lt;endl;\nhash ^= (int)c;\n}\nreturn hash &amp; MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(string key) {\nlong long hash = 0;\nconst int MODULUS = 1000000007;\nfor (unsigned char c : key) {\nhash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ (int)c) % MODULUS;\n}\nreturn (int)hash;\n}\n</code></pre> simple_hash.py<pre><code>def add_hash(key: str) -&gt; int:\n\"\"\"\u52a0\u6cd5\u54c8\u5e0c\"\"\"\nhash = 0\nmodulus = 1000000007\nfor c in key:\nhash += ord(c)\nreturn hash % modulus\ndef mul_hash(key: str) -&gt; int:\n\"\"\"\u4e58\u6cd5\u54c8\u5e0c\"\"\"\nhash = 0\nmodulus = 1000000007\nfor c in key:\nhash = 31 * hash + ord(c)\nreturn hash % modulus\ndef xor_hash(key: str) -&gt; int:\n\"\"\"\u5f02\u6216\u54c8\u5e0c\"\"\"\nhash = 0\nmodulus = 1000000007\nfor c in key:\nhash ^= ord(c)\nreturn hash % modulus\ndef rot_hash(key: str) -&gt; int:\n\"\"\"\u65cb\u8f6c\u54c8\u5e0c\"\"\"\nhash = 0\nmodulus = 1000000007\nfor c in key:\nhash = (hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ ord(c)\nreturn hash % modulus\n</code></pre> simple_hash.go<pre><code>/* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key string) int {\nvar hash int64\nvar modulus int64\nmodulus = 1000000007\nfor _, b := range []byte(key) {\nhash = (hash + int64(b)) % modulus\n}\nreturn int(hash)\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key string) int {\nvar hash int64\nvar modulus int64\nmodulus = 1000000007\nfor _, b := range []byte(key) {\nhash = (31*hash + int64(b)) % modulus\n}\nreturn int(hash)\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key string) int {\nhash := 0\nmodulus := 1000000007\nfor _, b := range []byte(key) {\nfmt.Println(int(b))\nhash ^= int(b)\nhash = (31*hash + int(b)) % modulus\n}\nreturn hash &amp; modulus\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key string) int {\nvar hash int64\nvar modulus int64\nmodulus = 1000000007\nfor _, b := range []byte(key) {\nhash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ int64(b)) % modulus\n}\nreturn int(hash)\n}\n</code></pre> simple_hash.js<pre><code>/* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key) {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = (hash + c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key) {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key) {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash ^= c.charCodeAt(0);\n}\nreturn hash &amp; MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key) {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n</code></pre> simple_hash.ts<pre><code>/* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key: string): number {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = (hash + c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key: string): number {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key: string): number {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash ^= c.charCodeAt(0);\n}\nreturn hash &amp; MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key: string): number {\nlet hash = 0;\nconst MODULUS = 1000000007;\nfor (const c of key) {\nhash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ c.charCodeAt(0)) % MODULUS;\n}\nreturn hash;\n}\n</code></pre> simple_hash.c<pre><code>[class]{}-[func]{addHash}\n[class]{}-[func]{mulHash}\n[class]{}-[func]{xorHash}\n[class]{}-[func]{rotHash}\n</code></pre> simple_hash.cs<pre><code>/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(string key) {\nlong hash = 0;\nconst int MODULUS = 1000000007;\nforeach (char c in key) {\nhash = (hash + c) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(string key) {\nlong hash = 0;\nconst int MODULUS = 1000000007;\nforeach (char c in key) {\nhash = (31 * hash + c) % MODULUS;\n}\nreturn (int)hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(string key) {\nint hash = 0;\nconst int MODULUS = 1000000007;\nforeach (char c in key) {\nhash ^= c;\n}\nreturn hash &amp; MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(string key) {\nlong hash = 0;\nconst int MODULUS = 1000000007;\nforeach (char c in key) {\nhash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ c) % MODULUS;\n}\nreturn (int)hash;\n}\n</code></pre> simple_hash.swift<pre><code>/* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key: String) -&gt; Int {\nvar hash = 0\nlet MODULUS = 1_000_000_007\nfor c in key {\nfor scalar in c.unicodeScalars {\nhash = (hash + Int(scalar.value)) % MODULUS\n}\n}\nreturn hash\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key: String) -&gt; Int {\nvar hash = 0\nlet MODULUS = 1_000_000_007\nfor c in key {\nfor scalar in c.unicodeScalars {\nhash = (31 * hash + Int(scalar.value)) % MODULUS\n}\n}\nreturn hash\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key: String) -&gt; Int {\nvar hash = 0\nlet MODULUS = 1_000_000_007\nfor c in key {\nfor scalar in c.unicodeScalars {\nhash ^= Int(scalar.value)\n}\n}\nreturn hash &amp; MODULUS\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key: String) -&gt; Int {\nvar hash = 0\nlet MODULUS = 1_000_000_007\nfor c in key {\nfor scalar in c.unicodeScalars {\nhash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ Int(scalar.value)) % MODULUS\n}\n}\nreturn hash\n}\n</code></pre> simple_hash.zig<pre><code>[class]{}-[func]{addHash}\n[class]{}-[func]{mulHash}\n[class]{}-[func]{xorHash}\n[class]{}-[func]{rotHash}\n</code></pre> simple_hash.dart<pre><code>/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (int i = 0; i &lt; key.length; i++) {\nhash = (hash + key.codeUnitAt(i)) % MODULUS;\n}\nreturn hash;\n}\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (int i = 0; i &lt; key.length; i++) {\nhash = (31 * hash + key.codeUnitAt(i)) % MODULUS;\n}\nreturn hash;\n}\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (int i = 0; i &lt; key.length; i++) {\nhash ^= key.codeUnitAt(i);\n}\nreturn hash &amp; MODULUS;\n}\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\nint hash = 0;\nfinal int MODULUS = 1000000007;\nfor (int i = 0; i &lt; key.length; i++) {\nhash = ((hash &lt;&lt; 4) ^ (hash &gt;&gt; 28) ^ key.codeUnitAt(i)) % MODULUS;\n}\nreturn hash;\n}\n</code></pre> simple_hash.rs<pre><code>[class]{}-[func]{add_hash}\n[class]{}-[func]{mul_hash}\n[class]{}-[func]{xor_hash}\n[class]{}-[func]{rot_hash}\n</code></pre> <p>\u89c2\u5bdf\u53d1\u73b0\uff0c\u6bcf\u79cd\u54c8\u5e0c\u7b97\u6cd5\u7684\u6700\u540e\u4e00\u6b65\u90fd\u662f\u5bf9\u5927\u8d28\u6570 \\(1000000007\\) \u53d6\u6a21\uff0c\u4ee5\u786e\u4fdd\u54c8\u5e0c\u503c\u5728\u5408\u9002\u7684\u8303\u56f4\u5185\u3002\u503c\u5f97\u601d\u8003\u7684\u662f\uff0c\u4e3a\u4ec0\u4e48\u8981\u5f3a\u8c03\u5bf9\u8d28\u6570\u53d6\u6a21\uff0c\u6216\u8005\u8bf4\u5bf9\u5408\u6570\u53d6\u6a21\u7684\u5f0a\u7aef\u662f\u4ec0\u4e48\uff1f\u8fd9\u662f\u4e00\u4e2a\u6709\u8da3\u7684\u95ee\u9898\u3002</p> <p>\u5148\u629b\u51fa\u7ed3\u8bba\uff1a\u5f53\u6211\u4eec\u4f7f\u7528\u5927\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\u65f6\uff0c\u53ef\u4ee5\u6700\u5927\u5316\u5730\u4fdd\u8bc1\u54c8\u5e0c\u503c\u7684\u5747\u5300\u5206\u5e03\u3002\u56e0\u4e3a\u8d28\u6570\u4e0d\u4f1a\u4e0e\u5176\u4ed6\u6570\u5b57\u5b58\u5728\u516c\u7ea6\u6570\uff0c\u53ef\u4ee5\u51cf\u5c11\u56e0\u53d6\u6a21\u64cd\u4f5c\u800c\u4ea7\u751f\u7684\u5468\u671f\u6027\u6a21\u5f0f\uff0c\u4ece\u800c\u907f\u514d\u54c8\u5e0c\u51b2\u7a81\u3002</p> <p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6211\u4eec\u9009\u62e9\u5408\u6570 \\(9\\) \u4f5c\u4e3a\u6a21\u6570\uff0c\u5b83\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\u3002\u90a3\u4e48\u6240\u6709\u53ef\u4ee5\u88ab \\(3\\) \u6574\u9664\u7684 <code>key</code> \u90fd\u4f1a\u88ab\u6620\u5c04\u5230 \\(0\\) , \\(3\\) , \\(6\\) \u8fd9\u4e09\u4e2a\u54c8\u5e0c\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, \\cdots \\} \\newline \\text{hash} &amp; = \\{ 0, 3, 6, 0, 3, 6, 0, 3, 6, 0, 3, 6,\\cdots \\} \\end{aligned} \\] <p>\u5982\u679c\u8f93\u5165 <code>key</code> \u6070\u597d\u6ee1\u8db3\u8fd9\u79cd\u7b49\u5dee\u6570\u5217\u7684\u6570\u636e\u5206\u5e03\uff0c\u90a3\u4e48\u54c8\u5e0c\u503c\u5c31\u4f1a\u51fa\u73b0\u805a\u5806\uff0c\u4ece\u800c\u52a0\u91cd\u54c8\u5e0c\u51b2\u7a81\u3002\u73b0\u5728\uff0c\u5047\u8bbe\u5c06 <code>modulus</code> \u66ff\u6362\u4e3a\u8d28\u6570 \\(13\\) \uff0c\u7531\u4e8e <code>key</code> \u548c <code>modulus</code> \u4e4b\u95f4\u4e0d\u5b58\u5728\u516c\u7ea6\u6570\uff0c\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u7684\u5747\u5300\u6027\u4f1a\u660e\u663e\u63d0\u5347\u3002</p> \\[ \\begin{aligned} \\text{modulus} &amp; = 13 \\newline \\text{key} &amp; = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\cdots \\} \\newline \\text{hash} &amp; = \\{ 0, 3, 6, 9, 12, 2, 5, 8, 11, 1, 4, 7, \\cdots \\} \\end{aligned} \\] <p>\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5982\u679c\u80fd\u591f\u4fdd\u8bc1 <code>key</code> \u662f\u968f\u673a\u5747\u5300\u5206\u5e03\u7684\uff0c\u90a3\u4e48\u9009\u62e9\u8d28\u6570\u6216\u8005\u5408\u6570\u4f5c\u4e3a\u6a21\u6570\u90fd\u662f\u53ef\u4ee5\u7684\uff0c\u5b83\u4eec\u90fd\u80fd\u8f93\u51fa\u5747\u5300\u5206\u5e03\u7684\u54c8\u5e0c\u503c\u3002\u800c\u5f53 <code>key</code> \u7684\u5206\u5e03\u5b58\u5728\u67d0\u79cd\u5468\u671f\u6027\u65f6\uff0c\u5bf9\u5408\u6570\u53d6\u6a21\u66f4\u5bb9\u6613\u51fa\u73b0\u805a\u96c6\u73b0\u8c61\u3002</p> <p>\u603b\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u901a\u5e38\u9009\u53d6\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u5e76\u4e14\u8fd9\u4e2a\u8d28\u6570\u6700\u597d\u8db3\u591f\u5927\uff0c\u4ee5\u5c3d\u53ef\u80fd\u6d88\u9664\u5468\u671f\u6027\u6a21\u5f0f\uff0c\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002</p>"},{"location":"chapter_hashing/hash_algorithm/#633","title":"6.3.3. \u00a0 \u5e38\u89c1\u54c8\u5e0c\u7b97\u6cd5","text":"<p>\u4e0d\u96be\u53d1\u73b0\uff0c\u4ee5\u4e0a\u4ecb\u7ecd\u7684\u7b80\u5355\u54c8\u5e0c\u7b97\u6cd5\u90fd\u6bd4\u8f83\u201c\u8106\u5f31\u201d\uff0c\u8fdc\u8fdc\u6ca1\u6709\u8fbe\u5230\u54c8\u5e0c\u7b97\u6cd5\u7684\u8bbe\u8ba1\u76ee\u6807\u3002\u4f8b\u5982\uff0c\u7531\u4e8e\u52a0\u6cd5\u548c\u5f02\u6216\u6ee1\u8db3\u4ea4\u6362\u5f8b\uff0c\u56e0\u6b64\u52a0\u6cd5\u54c8\u5e0c\u548c\u5f02\u6216\u54c8\u5e0c\u65e0\u6cd5\u533a\u5206\u5185\u5bb9\u76f8\u540c\u4f46\u987a\u5e8f\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u53ef\u80fd\u4f1a\u52a0\u5267\u54c8\u5e0c\u51b2\u7a81\uff0c\u5e76\u5f15\u8d77\u4e00\u4e9b\u5b89\u5168\u95ee\u9898\u3002</p> <p>\u5728\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u7528\u4e00\u4e9b\u6807\u51c6\u54c8\u5e0c\u7b97\u6cd5\uff0c\u4f8b\u5982 MD5 , SHA-1 , SHA-2 , SHA3 \u7b49\u3002\u5b83\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u957f\u5ea6\u7684\u8f93\u5165\u6570\u636e\u6620\u5c04\u5230\u6052\u5b9a\u957f\u5ea6\u7684\u54c8\u5e0c\u503c\u3002</p> <p>\u8fd1\u4e00\u4e2a\u4e16\u7eaa\u4ee5\u6765\uff0c\u54c8\u5e0c\u7b97\u6cd5\u5904\u5728\u4e0d\u65ad\u5347\u7ea7\u4e0e\u4f18\u5316\u7684\u8fc7\u7a0b\u4e2d\u3002\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u5458\u52aa\u529b\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u6027\u80fd\uff0c\u53e6\u4e00\u90e8\u5206\u7814\u7a76\u4eba\u5458\u548c\u9ed1\u5ba2\u5219\u81f4\u529b\u4e8e\u5bfb\u627e\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u95ee\u9898\u3002\u76f4\u81f3\u76ee\u524d\uff1a</p> <ul> <li>MD5 \u548c SHA-1 \u5df2\u591a\u6b21\u88ab\u6210\u529f\u653b\u51fb\uff0c\u56e0\u6b64\u5b83\u4eec\u88ab\u5404\u7c7b\u5b89\u5168\u5e94\u7528\u5f03\u7528\u3002</li> <li>SHA-2 \u7cfb\u5217\u4e2d\u7684 SHA-256 \u662f\u6700\u5b89\u5168\u7684\u54c8\u5e0c\u7b97\u6cd5\u4e4b\u4e00\uff0c\u4ecd\u672a\u51fa\u73b0\u6210\u529f\u7684\u653b\u51fb\u6848\u4f8b\uff0c\u56e0\u6b64\u5e38\u88ab\u7528\u5728\u5404\u7c7b\u5b89\u5168\u5e94\u7528\u4e0e\u534f\u8bae\u4e2d\u3002</li> <li>SHA-3 \u76f8\u8f83 SHA-2 \u7684\u5b9e\u73b0\u5f00\u9500\u66f4\u4f4e\u3001\u8ba1\u7b97\u6548\u7387\u66f4\u9ad8\uff0c\u4f46\u76ee\u524d\u4f7f\u7528\u8986\u76d6\u5ea6\u4e0d\u5982 SHA-2 \u7cfb\u5217\u3002</li> </ul> MD5 SHA-1 SHA-2 SHA-3 \u63a8\u51fa\u65f6\u95f4 1992 1995 2002 2008 \u8f93\u51fa\u957f\u5ea6 128 bits 160 bits 256 / 512 bits 224/256/384/512 bits \u54c8\u5e0c\u51b2\u7a81 \u8f83\u591a \u8f83\u591a \u5f88\u5c11 \u5f88\u5c11 \u5b89\u5168\u7b49\u7ea7 \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u51fb \u4f4e\uff0c\u5df2\u88ab\u6210\u529f\u653b\u51fb \u9ad8 \u9ad8 \u5e94\u7528 \u5df2\u88ab\u5f03\u7528\uff0c\u4ecd\u7528\u4e8e\u6570\u636e\u5b8c\u6574\u6027\u68c0\u67e5 \u5df2\u88ab\u5f03\u7528 \u52a0\u5bc6\u8d27\u5e01\u4ea4\u6613\u9a8c\u8bc1\u3001\u6570\u5b57\u7b7e\u540d\u7b49 \u53ef\u7528\u4e8e\u66ff\u4ee3 SHA-2"},{"location":"chapter_hashing/hash_algorithm/#634","title":"6.3.4. \u00a0 \u6570\u636e\u7ed3\u6784\u7684\u54c8\u5e0c\u503c","text":"<p>\u6211\u4eec\u77e5\u9053\uff0c\u54c8\u5e0c\u8868\u7684 <code>key</code> \u53ef\u4ee5\u662f\u6574\u6570\u3001\u5c0f\u6570\u6216\u5b57\u7b26\u4e32\u7b49\u6570\u636e\u7c7b\u578b\u3002\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u4e3a\u8fd9\u4e9b\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u5185\u7f6e\u7684\u54c8\u5e0c\u7b97\u6cd5\uff0c\u7528\u4e8e\u8ba1\u7b97\u54c8\u5e0c\u8868\u4e2d\u7684\u6876\u7d22\u5f15\u3002\u4ee5 Python \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528 <code>hash()</code> \u51fd\u6570\u6765\u8ba1\u7b97\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\uff0c\u5305\u62ec\uff1a</p> <ul> <li>\u6574\u6570\u548c\u5e03\u5c14\u91cf\u7684\u54c8\u5e0c\u503c\u5c31\u662f\u5176\u672c\u8eab\u3002</li> <li>\u6d6e\u70b9\u6570\u548c\u5b57\u7b26\u4e32\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\u8f83\u4e3a\u590d\u6742\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u8bf7\u81ea\u884c\u5b66\u4e60\u3002</li> <li>\u5143\u7ec4\u7684\u54c8\u5e0c\u503c\u662f\u5bf9\u5176\u4e2d\u6bcf\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u54c8\u5e0c\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u54c8\u5e0c\u503c\u7ec4\u5408\u8d77\u6765\uff0c\u5f97\u5230\u5355\u4e00\u7684\u54c8\u5e0c\u503c\u3002</li> <li>\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u57fa\u4e8e\u5176\u5185\u5b58\u5730\u5740\u751f\u6210\u3002\u901a\u8fc7\u91cd\u5199\u5bf9\u8c61\u7684\u54c8\u5e0c\u65b9\u6cd5\uff0c\u53ef\u5b9e\u73b0\u57fa\u4e8e\u5185\u5bb9\u751f\u6210\u54c8\u5e0c\u503c\u3002</li> </ul> <p>Tip</p> <p>\u8bf7\u6ce8\u610f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u5185\u7f6e\u54c8\u5e0c\u503c\u8ba1\u7b97\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u65b9\u6cd5\u4e0d\u540c\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust built_in_hash.java<pre><code>int num = 3;\nint hashNum = Integer.hashCode(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode();\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a -727081396\nObject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = Arrays.hashCode(arr);\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 1151158\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode@7dc5e7b4 \u7684\u54c8\u5e0c\u503c\u4e3a 2110121908\n</code></pre> built_in_hash.cpp<pre><code>int num = 3;\nsize_t hashNum = hash&lt;int&gt;()(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\nbool bol = true;\nsize_t hashBol = hash&lt;bool&gt;()(bol);\n// \u5e03\u5c14\u91cf 1 \u7684\u54c8\u5e0c\u503c\u4e3a 1\ndouble dec = 3.14159;\nsize_t hashDec = hash&lt;double&gt;()(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 4614256650576692846\nstring str = \"Hello \u7b97\u6cd5\";\nsize_t hashStr = hash&lt;string&gt;()(str);\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a 15466937326284535026\n// \u5728 C++ \u4e2d\uff0c\u5185\u7f6e std:hash() \u4ec5\u63d0\u4f9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\n// \u6570\u7ec4\u3001\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\u9700\u8981\u81ea\u884c\u5b9e\u73b0\n</code></pre> built_in_hash.py<pre><code>num = 3\nhash_num = hash(num)\n# \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\nbol = True\nhash_bol = hash(bol)\n# \u5e03\u5c14\u91cf True \u7684\u54c8\u5e0c\u503c\u4e3a 1\ndec = 3.14159\nhash_dec = hash(dec)\n# \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 326484311674566659\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = hash(str)\n# \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a 4617003410720528961\ntup = (12836, \"\u5c0f\u54c8\")\nhash_tup = hash(tup)\n# \u5143\u7ec4 (12836, '\u5c0f\u54c8') \u7684\u54c8\u5e0c\u503c\u4e3a 1029005403108185979\nobj = ListNode(0)\nhash_obj = hash(obj)\n# \u8282\u70b9\u5bf9\u8c61 &lt;ListNode object at 0x1058fd810&gt; \u7684\u54c8\u5e0c\u503c\u4e3a 274267521\n</code></pre> built_in_hash.go<pre><code>\n</code></pre> built_in_hash.js<pre><code>\n</code></pre> built_in_hash.ts<pre><code>\n</code></pre> built_in_hash.c<pre><code>\n</code></pre> built_in_hash.cs<pre><code>int num = 3;\nint hashNum = num.GetHashCode();\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3;\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 1;\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -1340954729;\nstring str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.GetHashCode();\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a -586107568;\nobject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = arr.GetHashCode();\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 42931033;\nListNode obj = new ListNode(0);\nint hashObj = obj.GetHashCode();\n// \u8282\u70b9\u5bf9\u8c61 0 \u7684\u54c8\u5e0c\u503c\u4e3a 39053774;\n</code></pre> built_in_hash.swift<pre><code>let num = 3\nlet hashNum = num.hashValue\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 9047044699613009734\nlet bol = true\nlet hashBol = bol.hashValue\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a -4431640247352757451\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a -2465384235396674631\nlet str = \"Hello \u7b97\u6cd5\"\nlet hashStr = str.hashValue\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a -7850626797806988787\nlet arr = [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\nlet hashTup = arr.hashValue\n// \u6570\u7ec4 [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")] \u7684\u54c8\u5e0c\u503c\u4e3a -2308633508154532996\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// \u8282\u70b9\u5bf9\u8c61 utils.ListNode \u7684\u54c8\u5e0c\u503c\u4e3a -2434780518035996159\n</code></pre> built_in_hash.zig<pre><code>\n</code></pre> built_in_hash.dart<pre><code>int num = 3;\nint hashNum = num.hashCode;\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 34803\nbool bol = true;\nint hashBol = bol.hashCode;\n// \u5e03\u5c14\u503c true \u7684\u54c8\u5e0c\u503c\u4e3a 1231\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 2570631074981783\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode;\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a 468167534\nList arr = [12836, \"\u5c0f\u54c8\"];\nint hashArr = arr.hashCode;\n// \u6570\u7ec4 [12836, \u5c0f\u54c8] \u7684\u54c8\u5e0c\u503c\u4e3a 976512528\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// \u8282\u70b9\u5bf9\u8c61 Instance of 'ListNode' \u7684\u54c8\u5e0c\u503c\u4e3a 1033450432\n</code></pre> built_in_hash.rs<pre><code>\n</code></pre> <p>\u5728\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u53ea\u6709\u4e0d\u53ef\u53d8\u5bf9\u8c61\u624d\u53ef\u4f5c\u4e3a\u54c8\u5e0c\u8868\u7684 <code>key</code> \u3002\u5047\u5982\u6211\u4eec\u5c06\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4f5c\u4e3a <code>key</code> \uff0c\u5f53\u5217\u8868\u7684\u5185\u5bb9\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5b83\u7684\u54c8\u5e0c\u503c\u4e5f\u968f\u4e4b\u6539\u53d8\uff0c\u6211\u4eec\u5c31\u65e0\u6cd5\u5728\u54c8\u5e0c\u8868\u4e2d\u67e5\u8be2\u5230\u539f\u5148\u7684 <code>value</code> \u4e86\u3002</p> <p>\u867d\u7136\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff08\u6bd4\u5982\u94fe\u8868\u8282\u70b9\uff09\u7684\u6210\u5458\u53d8\u91cf\u662f\u53ef\u53d8\u7684\uff0c\u4f46\u5b83\u662f\u53ef\u54c8\u5e0c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u901a\u5e38\u662f\u57fa\u4e8e\u5185\u5b58\u5730\u5740\u751f\u6210\u7684\uff0c\u5373\u4f7f\u5bf9\u8c61\u7684\u5185\u5bb9\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u4f46\u5b83\u7684\u5185\u5b58\u5730\u5740\u4e0d\u53d8\uff0c\u54c8\u5e0c\u503c\u4ecd\u7136\u662f\u4e0d\u53d8\u7684\u3002</p> <p>\u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u53d1\u73b0\u5728\u4e0d\u540c\u63a7\u5236\u53f0\u4e2d\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u8f93\u51fa\u7684\u54c8\u5e0c\u503c\u662f\u4e0d\u540c\u7684\u3002\u8fd9\u662f\u56e0\u4e3a Python \u89e3\u91ca\u5668\u5728\u6bcf\u6b21\u542f\u52a8\u65f6\uff0c\u90fd\u4f1a\u4e3a\u5b57\u7b26\u4e32\u54c8\u5e0c\u51fd\u6570\u52a0\u5165\u4e00\u4e2a\u968f\u673a\u7684\u76d0\uff08Salt\uff09\u503c\u3002\u8fd9\u79cd\u505a\u6cd5\u53ef\u4ee5\u6709\u6548\u9632\u6b62 HashDoS \u653b\u51fb\uff0c\u63d0\u5347\u54c8\u5e0c\u7b97\u6cd5\u7684\u5b89\u5168\u6027\u3002</p>"},{"location":"chapter_hashing/hash_collision/","title":"6.2. \u00a0 \u54c8\u5e0c\u51b2\u7a81","text":"<p>\u4e0a\u8282\u63d0\u5230\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u5165\u7a7a\u95f4\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff0c\u56e0\u6b64\u7406\u8bba\u4e0a\u54c8\u5e0c\u51b2\u7a81\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u6bd4\u5982\uff0c\u8f93\u5165\u7a7a\u95f4\u4e3a\u5168\u4f53\u6574\u6570\uff0c\u8f93\u51fa\u7a7a\u95f4\u4e3a\u6570\u7ec4\u5bb9\u91cf\u5927\u5c0f\uff0c\u5219\u5fc5\u7136\u6709\u591a\u4e2a\u6574\u6570\u6620\u5c04\u81f3\u540c\u4e00\u6570\u7ec4\u7d22\u5f15\u3002</p> <p>\u54c8\u5e0c\u51b2\u7a81\u4f1a\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u9519\u8bef\uff0c\u4e25\u91cd\u5f71\u54cd\u54c8\u5e0c\u8868\u7684\u53ef\u7528\u6027\u3002\u4e3a\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u6bcf\u5f53\u9047\u5230\u54c8\u5e0c\u51b2\u7a81\u65f6\u5c31\u8fdb\u884c\u54c8\u5e0c\u8868\u6269\u5bb9\uff0c\u76f4\u81f3\u51b2\u7a81\u6d88\u5931\u4e3a\u6b62\u3002\u6b64\u65b9\u6cd5\u7b80\u5355\u7c97\u66b4\u4e14\u6709\u6548\uff0c\u4f46\u6548\u7387\u592a\u4f4e\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u6570\u636e\u642c\u8fd0\u4e0e\u54c8\u5e0c\u503c\u8ba1\u7b97\u3002\u4e3a\u4e86\u63d0\u5347\u6548\u7387\uff0c\u6211\u4eec\u5207\u6362\u4e00\u4e0b\u601d\u8def\uff1a</p> <ol> <li>\u6539\u826f\u54c8\u5e0c\u8868\u6570\u636e\u7ed3\u6784\uff0c\u4f7f\u5f97\u54c8\u5e0c\u8868\u53ef\u4ee5\u5728\u5b58\u5728\u54c8\u5e0c\u51b2\u7a81\u65f6\u6b63\u5e38\u5de5\u4f5c\u3002</li> <li>\u4ec5\u5728\u5fc5\u8981\u65f6\uff0c\u5373\u5f53\u54c8\u5e0c\u51b2\u7a81\u6bd4\u8f83\u4e25\u91cd\u65f6\uff0c\u624d\u6267\u884c\u6269\u5bb9\u64cd\u4f5c\u3002</li> </ol> <p>\u54c8\u5e0c\u8868\u7684\u7ed3\u6784\u6539\u826f\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u94fe\u5f0f\u5730\u5740\u548c\u5f00\u653e\u5bfb\u5740\u3002</p>"},{"location":"chapter_hashing/hash_collision/#621","title":"6.2.1. \u00a0 \u94fe\u5f0f\u5730\u5740","text":"<p>\u5728\u539f\u59cb\u54c8\u5e0c\u8868\u4e2d\uff0c\u6bcf\u4e2a\u6876\u4ec5\u80fd\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u300c\u94fe\u5f0f\u5730\u5740 Separate Chaining\u300d\u5c06\u5355\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u94fe\u8868\uff0c\u5c06\u952e\u503c\u5bf9\u4f5c\u4e3a\u94fe\u8868\u8282\u70b9\uff0c\u5c06\u6240\u6709\u53d1\u751f\u51b2\u7a81\u7684\u952e\u503c\u5bf9\u90fd\u5b58\u50a8\u5728\u540c\u4e00\u94fe\u8868\u4e2d\u3002</p> <p></p> <p> \u56fe\uff1a\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 </p> <p>\u94fe\u5f0f\u5730\u5740\u4e0b\uff0c\u54c8\u5e0c\u8868\u7684\u64cd\u4f5c\u65b9\u6cd5\u5305\u62ec\uff1a</p> <ul> <li>\u67e5\u8be2\u5143\u7d20\uff1a\u8f93\u5165 <code>key</code> \uff0c\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u6570\u7ec4\u7d22\u5f15\uff0c\u5373\u53ef\u8bbf\u95ee\u94fe\u8868\u5934\u8282\u70b9\uff0c\u7136\u540e\u904d\u5386\u94fe\u8868\u5e76\u5bf9\u6bd4 <code>key</code> \u4ee5\u67e5\u627e\u76ee\u6807\u952e\u503c\u5bf9\u3002</li> <li>\u6dfb\u52a0\u5143\u7d20\uff1a\u5148\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8bbf\u95ee\u94fe\u8868\u5934\u8282\u70b9\uff0c\u7136\u540e\u5c06\u8282\u70b9\uff08\u5373\u952e\u503c\u5bf9\uff09\u6dfb\u52a0\u5230\u94fe\u8868\u4e2d\u3002</li> <li>\u5220\u9664\u5143\u7d20\uff1a\u6839\u636e\u54c8\u5e0c\u51fd\u6570\u7684\u7ed3\u679c\u8bbf\u95ee\u94fe\u8868\u5934\u90e8\uff0c\u63a5\u7740\u904d\u5386\u94fe\u8868\u4ee5\u67e5\u627e\u76ee\u6807\u8282\u70b9\uff0c\u5e76\u5c06\u5176\u5220\u9664\u3002</li> </ul> <p>\u8be5\u65b9\u6cd5\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\uff0c\u5305\u62ec\uff1a</p> <ul> <li>\u5360\u7528\u7a7a\u95f4\u589e\u5927\uff0c\u94fe\u8868\u5305\u542b\u8282\u70b9\u6307\u9488\uff0c\u5b83\u76f8\u6bd4\u6570\u7ec4\u66f4\u52a0\u8017\u8d39\u5185\u5b58\u7a7a\u95f4\u3002</li> <li>\u67e5\u8be2\u6548\u7387\u964d\u4f4e\uff0c\u56e0\u4e3a\u9700\u8981\u7ebf\u6027\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u5bf9\u5e94\u5143\u7d20\u3002</li> </ul> <p>\u4ee5\u4e0b\u7ed9\u51fa\u4e86\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u9700\u8981\u6ce8\u610f\uff1a</p> <ul> <li>\u4e3a\u4e86\u4f7f\u5f97\u4ee3\u7801\u5c3d\u91cf\u7b80\u77ed\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4ee3\u66ff\u94fe\u8868\u3002\u5728\u8fd9\u79cd\u8bbe\u5b9a\u4e0b\uff0c\u54c8\u5e0c\u8868\uff08\u6570\u7ec4\uff09\u5305\u542b\u591a\u4e2a\u6876\uff0c\u6bcf\u4e2a\u6876\u90fd\u662f\u4e00\u4e2a\u5217\u8868\u3002</li> <li>\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u54c8\u5e0c\u8868\u6269\u5bb9\u65b9\u6cd5\u3002\u5177\u4f53\u6765\u770b\uff0c\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(0.75\\) \u65f6\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u6269\u5bb9\u81f3 \\(2\\) \u500d\u3002</li> </ul> JavaC++PythonGoJSTSCC#SwiftZigDartRust hash_map_chaining.java<pre><code>/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nint size; // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\ndouble loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nint extendRatio; // \u6269\u5bb9\u500d\u6570\nList&lt;List&lt;Pair&gt;&gt; buckets; // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\npublic HashMapChaining() {\nsize = 0;\ncapacity = 4;\nloadThres = 2 / 3.0;\nextendRatio = 2;\nbuckets = new ArrayList&lt;&gt;(capacity);\nfor (int i = 0; i &lt; capacity; i++) {\nbuckets.add(new ArrayList&lt;&gt;());\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn (double) size / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nString get(int key) {\nint index = hashFunc(key);\nList&lt;Pair&gt; bucket = buckets.get(index);\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (Pair pair : bucket) {\nif (pair.key == key) {\nreturn pair.val;\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, String val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() &gt; loadThres) {\nextend();\n}\nint index = hashFunc(key);\nList&lt;Pair&gt; bucket = buckets.get(index);\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor (Pair pair : bucket) {\nif (pair.key == key) {\npair.val = val;\nreturn;\n}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nPair pair = new Pair(key, val);\nbucket.add(pair);\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\nList&lt;Pair&gt; bucket = buckets.get(index);\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (Pair pair : bucket) {\nif (pair.key == key) {\nbucket.remove(pair);\nsize--;\nbreak;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nList&lt;List&lt;Pair&gt;&gt; bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets = new ArrayList&lt;&gt;(capacity);\nfor (int i = 0; i &lt; capacity; i++) {\nbuckets.add(new ArrayList&lt;&gt;());\n}\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (List&lt;Pair&gt; bucket : bucketsTmp) {\nfor (Pair pair : bucket) {\nput(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (List&lt;Pair&gt; bucket : buckets) {\nList&lt;String&gt; res = new ArrayList&lt;&gt;();\nfor (Pair pair : bucket) {\nres.add(pair.key + \" -&gt; \" + pair.val);\n}\nSystem.out.println(res);\n}\n}\n}\n</code></pre> hash_map_chaining.cpp<pre><code>/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nprivate:\nint size; // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\ndouble loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nint extendRatio; // \u6269\u5bb9\u500d\u6570\nvector&lt;vector&lt;Pair *&gt;&gt; buckets; // \u6876\u6570\u7ec4\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nHashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3), extendRatio(2) {\nbuckets.resize(capacity);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn (double)size / (double)capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nstring get(int key) {\nint index = hashFunc(key);\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (Pair *pair : buckets[index]) {\nif (pair-&gt;key == key) {\nreturn pair-&gt;val;\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de nullptr\nreturn nullptr;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, string val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() &gt; loadThres) {\nextend();\n}\nint index = hashFunc(key);\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor (Pair *pair : buckets[index]) {\nif (pair-&gt;key == key) {\npair-&gt;val = val;\nreturn;\n}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nbuckets[index].push_back(new Pair(key, val));\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\nauto &amp;bucket = buckets[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (int i = 0; i &lt; bucket.size(); i++) {\nif (bucket[i]-&gt;key == key) {\nPair *tmp = bucket[i];\nbucket.erase(bucket.begin() + i); // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\ndelete tmp; // \u91ca\u653e\u5185\u5b58\nsize--;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nvector&lt;vector&lt;Pair *&gt;&gt; bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets.clear();\nbuckets.resize(capacity);\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (auto &amp;bucket : bucketsTmp) {\nfor (Pair *pair : bucket) {\nput(pair-&gt;key, pair-&gt;val);\n// \u91ca\u653e\u5185\u5b58\ndelete pair;\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (auto &amp;bucket : buckets) {\ncout &lt;&lt; \"[\";\nfor (Pair *pair : bucket) {\ncout &lt;&lt; pair-&gt;key &lt;&lt; \" -&gt; \" &lt;&lt; pair-&gt;val &lt;&lt; \", \";\n}\ncout &lt;&lt; \"]\\n\";\n}\n}\n};\n</code></pre> hash_map_chaining.py<pre><code>class HashMapChaining:\n\"\"\"\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.size = 0 # \u952e\u503c\u5bf9\u6570\u91cf\nself.capacity = 4 # \u54c8\u5e0c\u8868\u5bb9\u91cf\nself.load_thres = 2 / 3 # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nself.extend_ratio = 2 # \u6269\u5bb9\u500d\u6570\nself.buckets = [[] for _ in range(self.capacity)] # \u6876\u6570\u7ec4\ndef hash_func(self, key: int) -&gt; int:\n\"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\nreturn key % self.capacity\ndef load_factor(self) -&gt; float:\n\"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\nreturn self.size / self.capacity\ndef get(self, key: int) -&gt; str:\n\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\nindex = self.hash_func(key)\nbucket = self.buckets[index]\n# \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor pair in bucket:\nif pair.key == key:\nreturn pair.val\n# \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\nreturn None\ndef put(self, key: int, val: str):\n\"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n# \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif self.load_factor() &gt; self.load_thres:\nself.extend()\nindex = self.hash_func(key)\nbucket = self.buckets[index]\n# \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor pair in bucket:\nif pair.key == key:\npair.val = val\nreturn\n# \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\npair = Pair(key, val)\nbucket.append(pair)\nself.size += 1\ndef remove(self, key: int):\n\"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\nindex = self.hash_func(key)\nbucket = self.buckets[index]\n# \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor pair in bucket:\nif pair.key == key:\nbucket.remove(pair)\nself.size -= 1\nbreak\ndef extend(self):\n\"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n# \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nbuckets = self.buckets\n# \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nself.capacity *= self.extend_ratio\nself.buckets = [[] for _ in range(self.capacity)]\nself.size = 0\n# \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor bucket in buckets:\nfor pair in bucket:\nself.put(pair.key, pair.val)\ndef print(self):\n\"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\nfor bucket in self.buckets:\nres = []\nfor pair in bucket:\nres.append(str(pair.key) + \" -&gt; \" + pair.val)\nprint(res)\n</code></pre> hash_map_chaining.go<pre><code>/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntype hashMapChaining struct {\nsize int // \u952e\u503c\u5bf9\u6570\u91cf\ncapacity int // \u54c8\u5e0c\u8868\u5bb9\u91cf\nloadThres float64 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nextendRatio int // \u6269\u5bb9\u500d\u6570\nbuckets [][]pair // \u6876\u6570\u7ec4\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapChaining() *hashMapChaining {\nbuckets := make([][]pair, 4)\nfor i := 0; i &lt; 4; i++ {\nbuckets[i] = make([]pair, 0)\n}\nreturn &amp;hashMapChaining{\nsize: 0,\ncapacity: 4,\nloadThres: 2 / 3.0,\nextendRatio: 2,\nbuckets: buckets,\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (m *hashMapChaining) hashFunc(key int) int {\nreturn key % m.capacity\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (m *hashMapChaining) loadFactor() float64 {\nreturn float64(m.size / m.capacity)\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (m *hashMapChaining) get(key int) string {\nidx := m.hashFunc(key)\nbucket := m.buckets[idx]\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor _, p := range bucket {\nif p.key == key {\nreturn p.val\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\nreturn \"\"\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (m *hashMapChaining) put(key int, val string) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif m.loadFactor() &gt; m.loadThres {\nm.extend()\n}\nidx := m.hashFunc(key)\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor _, p := range m.buckets[idx] {\nif p.key == key {\np.val = val\nreturn\n}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\np := pair{\nkey: key,\nval: val,\n}\nm.buckets[idx] = append(m.buckets[idx], p)\nm.size += 1\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (m *hashMapChaining) remove(key int) {\nidx := m.hashFunc(key)\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor i, p := range m.buckets[idx] {\nif p.key == key {\n// \u5207\u7247\u5220\u9664\nm.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)\nm.size -= 1\nbreak\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\ntmpBuckets := make([][]pair, len(m.buckets))\nfor i := 0; i &lt; len(m.buckets); i++ {\ntmpBuckets[i] = make([]pair, len(m.buckets[i]))\ncopy(tmpBuckets[i], m.buckets[i])\n}\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nm.capacity *= m.extendRatio\nm.buckets = make([][]pair, m.capacity)\nfor i := 0; i &lt; m.capacity; i++ {\nm.buckets[i] = make([]pair, 0)\n}\nm.size = 0\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor _, bucket := range tmpBuckets {\nfor _, p := range bucket {\nm.put(p.key, p.val)\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) print() {\nvar builder strings.Builder\nfor _, bucket := range m.buckets {\nbuilder.WriteString(\"[\")\nfor _, p := range bucket {\nbuilder.WriteString(strconv.Itoa(p.key) + \" -&gt; \" + p.val + \" \")\n}\nbuilder.WriteString(\"]\")\nfmt.Println(builder.String())\nbuilder.Reset()\n}\n}\n</code></pre> hash_map_chaining.js<pre><code>/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n#size; // \u952e\u503c\u5bf9\u6570\u91cf\n#capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n#loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n#extendRatio; // \u6269\u5bb9\u500d\u6570\n#buckets; // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#size = 0;\nthis.#capacity = 4;\nthis.#loadThres = 2 / 3.0;\nthis.#extendRatio = 2;\nthis.#buckets = new Array(this.#capacity).fill(null).map((x) =&gt; []);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key) {\nreturn key % this.#capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\n#loadFactor() {\nreturn this.#size / this.#capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key) {\nconst index = this.#hashFunc(key);\nconst bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (const pair of bucket) {\nif (pair.key === key) {\nreturn pair.val;\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nput(key, val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (this.#loadFactor() &gt; this.#loadThres) {\nthis.#extend();\n}\nconst index = this.#hashFunc(key);\nconst bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor (const pair of bucket) {\nif (pair.key === key) {\npair.val = val;\nreturn;\n}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nconst pair = new Pair(key, val);\nbucket.push(pair);\nthis.#size++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nremove(key) {\nconst index = this.#hashFunc(key);\nlet bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (let i = 0; i &lt; bucket.length; i++) {\nif (bucket[i].key === key) {\nbucket.splice(i, 1);\nthis.#size--;\nbreak;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n#extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nconst bucketsTmp = this.#buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nthis.#capacity *= this.#extendRatio;\nthis.#buckets = new Array(this.#capacity).fill(null).map((x) =&gt; []);\nthis.#size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (const bucket of bucketsTmp) {\nfor (const pair of bucket) {\nthis.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint() {\nfor (const bucket of this.#buckets) {\nlet res = [];\nfor (const pair of bucket) {\nres.push(pair.key + ' -&gt; ' + pair.val);\n}\nconsole.log(res);\n}\n}\n}\n</code></pre> hash_map_chaining.ts<pre><code>/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n#size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n#capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n#loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n#extendRatio: number; // \u6269\u5bb9\u500d\u6570\n#buckets: Pair[][]; // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#size = 0;\nthis.#capacity = 4;\nthis.#loadThres = 2 / 3.0;\nthis.#extendRatio = 2;\nthis.#buckets = new Array(this.#capacity).fill(null).map((x) =&gt; []);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key: number): number {\nreturn key % this.#capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\n#loadFactor(): number {\nreturn this.#size / this.#capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key: number): string | null {\nconst index = this.#hashFunc(key);\nconst bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (const pair of bucket) {\nif (pair.key === key) {\nreturn pair.val;\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nput(key: number, val: string): void {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (this.#loadFactor() &gt; this.#loadThres) {\nthis.#extend();\n}\nconst index = this.#hashFunc(key);\nconst bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor (const pair of bucket) {\nif (pair.key === key) {\npair.val = val;\nreturn;\n}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nconst pair = new Pair(key, val);\nbucket.push(pair);\nthis.#size++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nremove(key: number): void {\nconst index = this.#hashFunc(key);\nlet bucket = this.#buckets[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (let i = 0; i &lt; bucket.length; i++) {\nif (bucket[i].key === key) {\nbucket.splice(i, 1);\nthis.#size--;\nbreak;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n#extend(): void {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nconst bucketsTmp = this.#buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nthis.#capacity *= this.#extendRatio;\nthis.#buckets = new Array(this.#capacity).fill(null).map((x) =&gt; []);\nthis.#size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (const bucket of bucketsTmp) {\nfor (const pair of bucket) {\nthis.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint(): void {\nfor (const bucket of this.#buckets) {\nlet res = [];\nfor (const pair of bucket) {\nres.push(pair.key + ' -&gt; ' + pair.val);\n}\nconsole.log(res);\n}\n}\n}\n</code></pre> hash_map_chaining.c<pre><code>[class]{hashMapChaining}-[func]{}\n</code></pre> hash_map_chaining.cs<pre><code>/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nint size; // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\ndouble loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nint extendRatio; // \u6269\u5bb9\u500d\u6570\nList&lt;List&lt;Pair&gt;&gt; buckets; // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\npublic HashMapChaining() {\nsize = 0;\ncapacity = 4;\nloadThres = 2 / 3.0;\nextendRatio = 2;\nbuckets = new List&lt;List&lt;Pair&gt;&gt;(capacity);\nfor (int i = 0; i &lt; capacity; i++) {\nbuckets.Add(new List&lt;Pair&gt;());\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nprivate double loadFactor() {\nreturn (double)size / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic string get(int key) {\nint index = hashFunc(key);\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nforeach (Pair pair in buckets[index]) {\nif (pair.key == key) {\nreturn pair.val;\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic void put(int key, string val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() &gt; loadThres) {\nextend();\n}\nint index = hashFunc(key);\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nforeach (Pair pair in buckets[index]) {\nif (pair.key == key) {\npair.val = val;\nreturn;\n}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nbuckets[index].Add(new Pair(key, val));\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key) {\nint index = hashFunc(key);\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nforeach (Pair pair in buckets[index].ToList()) {\nif (pair.key == key) {\nbuckets[index].Remove(pair);\nsize--;\nbreak;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nprivate void extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nList&lt;List&lt;Pair&gt;&gt; bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets = new List&lt;List&lt;Pair&gt;&gt;(capacity);\nfor (int i = 0; i &lt; capacity; i++) {\nbuckets.Add(new List&lt;Pair&gt;());\n}\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nforeach (List&lt;Pair&gt; bucket in bucketsTmp) {\nforeach (Pair pair in bucket) {\nput(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void print() {\nforeach (List&lt;Pair&gt; bucket in buckets) {\nList&lt;string&gt; res = new List&lt;string&gt;();\nforeach (Pair pair in bucket) {\nres.Add(pair.key + \" -&gt; \" + pair.val);\n}\nforeach (string kv in res) {\nConsole.WriteLine(kv);\n}\n}\n}\n}\n</code></pre> hash_map_chaining.swift<pre><code>/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nvar size: Int // \u952e\u503c\u5bf9\u6570\u91cf\nvar capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\nvar loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nvar extendRatio: Int // \u6269\u5bb9\u500d\u6570\nvar buckets: [[Pair]] // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\ninit() {\nsize = 0\ncapacity = 4\nloadThres = 2 / 3\nextendRatio = 2\nbuckets = Array(repeating: [], count: capacity)\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc hashFunc(key: Int) -&gt; Int {\nkey % capacity\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc loadFactor() -&gt; Double {\nDouble(size / capacity)\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc get(key: Int) -&gt; String? {\nlet index = hashFunc(key: key)\nlet bucket = buckets[index]\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor pair in bucket {\nif pair.key == key {\nreturn pair.val\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de nil\nreturn nil\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc put(key: Int, val: String) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif loadFactor() &gt; loadThres {\nextend()\n}\nlet index = hashFunc(key: key)\nlet bucket = buckets[index]\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor pair in bucket {\nif pair.key == key {\npair.val = val\nreturn\n}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nlet pair = Pair(key: key, val: val)\nbuckets[index].append(pair)\nsize += 1\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc remove(key: Int) {\nlet index = hashFunc(key: key)\nlet bucket = buckets[index]\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (pairIndex, pair) in bucket.enumerated() {\nif pair.key == key {\nbuckets[index].remove(at: pairIndex)\n}\n}\nsize -= 1\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nlet bucketsTmp = buckets\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio\nbuckets = Array(repeating: [], count: capacity)\nsize = 0\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor bucket in bucketsTmp {\nfor pair in bucket {\nput(key: pair.key, val: pair.val)\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc print() {\nfor bucket in buckets {\nlet res = bucket.map { \"\\($0.key) -&gt; \\($0.val)\" }\nSwift.print(res)\n}\n}\n}\n</code></pre> hash_map_chaining.zig<pre><code>[class]{HashMapChaining}-[func]{}\n</code></pre> hash_map_chaining.dart<pre><code>/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\nlate int size; // \u952e\u503c\u5bf9\u6570\u91cf\nlate int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\nlate double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nlate int extendRatio; // \u6269\u5bb9\u500d\u6570\nlate List&lt;List&lt;Pair&gt;&gt; buckets; // \u6876\u6570\u7ec4\n/* \u6784\u9020\u65b9\u6cd5 */\nHashMapChaining() {\nsize = 0;\ncapacity = 4;\nloadThres = 2 / 3.0;\nextendRatio = 2;\nbuckets = List.generate(capacity, (_) =&gt; []);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn size / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nString? get(int key) {\nint index = hashFunc(key);\nList&lt;Pair&gt; bucket = buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor (Pair pair in bucket) {\nif (pair.key == key) {\nreturn pair.val;\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de null\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, String val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() &gt; loadThres) {\nextend();\n}\nint index = hashFunc(key);\nList&lt;Pair&gt; bucket = buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor (Pair pair in bucket) {\nif (pair.key == key) {\npair.val = val;\nreturn;\n}\n}\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nPair pair = Pair(key, val);\nbucket.add(pair);\nsize++;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\nList&lt;Pair&gt; bucket = buckets[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor (Pair pair in bucket) {\nif (pair.key == key) {\nbucket.remove(pair);\nsize--;\nbreak;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nList&lt;List&lt;Pair&gt;&gt; bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets = List.generate(capacity, (_) =&gt; []);\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (List&lt;Pair&gt; bucket in bucketsTmp) {\nfor (Pair pair in bucket) {\nput(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid printHashMap() {\nfor (List&lt;Pair&gt; bucket in buckets) {\nList&lt;String&gt; res = [];\nfor (Pair pair in bucket) {\nres.add(\"${pair.key} -&gt; ${pair.val}\");\n}\nprint(res);\n}\n}\n}\n</code></pre> hash_map_chaining.rs<pre><code>/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapChaining {\nsize: i32,\ncapacity: i32,\nload_thres: f32,\nextend_ratio: i32,\nbuckets: Vec&lt;Vec&lt;Pair&gt;&gt;,\n}\nimpl HashMapChaining {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new() -&gt; Self {\nSelf {\nsize: 0,\ncapacity: 4,\nload_thres: 2.0 / 3.0,\nextend_ratio: 2,\nbuckets: vec![vec![]; 4],\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfn hash_func(&amp;self, key: i32) -&gt; usize {\nkey as usize % self.capacity as usize\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfn load_factor(&amp;self) -&gt; f32 {\nself.size as f32 / self.capacity as f32\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfn remove(&amp;mut self, key: i32) -&gt; Option&lt;String&gt; {\nlet index = self.hash_func(key);\nlet bucket = &amp;mut self.buckets[index];\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor i in 0..bucket.len() {\nif bucket[i].key == key {\nlet pair = bucket.remove(i);\nself.size -= 1;\nreturn Some(pair.val);\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\nNone\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfn extend(&amp;mut self) {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nlet buckets_tmp = std::mem::replace(&amp;mut self.buckets, vec![]);\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nself.capacity *= self.extend_ratio;\nself.buckets = vec![Vec::new(); self.capacity as usize];\nself.size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor bucket in buckets_tmp {\nfor pair in bucket {\nself.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfn print(&amp;self) {\nfor bucket in &amp;self.buckets {\nlet mut res = Vec::new();\nfor pair in bucket {\nres.push(format!(\"{} -&gt; {}\", pair.key, pair.val));\n}\nprintln!(\"{:?}\", res);\n}\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfn put(&amp;mut self, key: i32, val: String) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif self.load_factor() &gt; self.load_thres {\nself.extend();\n}\nlet index = self.hash_func(key);\nlet bucket = &amp;mut self.buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\nfor pair in bucket {\nif pair.key == key {\npair.val = val.clone();\nreturn;\n}\n}\nlet bucket = &amp;mut self.buckets[index];\n// \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\nlet pair = Pair {\nkey,\nval: val.clone(),\n};\nbucket.push(pair);\nself.size += 1;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfn get(&amp;self, key: i32) -&gt; Option&lt;&amp;str&gt; {\nlet index = self.hash_func(key);\nlet bucket = &amp;self.buckets[index];\n// \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \u5219\u8fd4\u56de\u5bf9\u5e94 val\nfor pair in bucket {\nif pair.key == key {\nreturn Some(&amp;pair.val);\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de None\nNone\n}\n}\n</code></pre> <p>Tip</p> <p>\u5f53\u94fe\u8868\u5f88\u957f\u65f6\uff0c\u67e5\u8be2\u6548\u7387 \\(O(n)\\) \u5f88\u5dee\uff0c\u6b64\u65f6\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u300cAVL \u6811\u300d\u6216\u300c\u7ea2\u9ed1\u6811\u300d\uff0c\u4ece\u800c\u5c06\u67e5\u8be2\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002</p>"},{"location":"chapter_hashing/hash_collision/#622","title":"6.2.2. \u00a0 \u5f00\u653e\u5bfb\u5740","text":"<p>\u300c\u5f00\u653e\u5bfb\u5740 Open Addressing\u300d\u4e0d\u5f15\u5165\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\uff0c\u800c\u662f\u901a\u8fc7\u201c\u591a\u6b21\u63a2\u6d4b\u201d\u6765\u5904\u7406\u54c8\u5e0c\u51b2\u7a81\uff0c\u63a2\u6d4b\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u3001\u591a\u6b21\u54c8\u5e0c\u7b49\u3002</p>"},{"location":"chapter_hashing/hash_collision/#_1","title":"\u7ebf\u6027\u63a2\u6d4b","text":"<p>\u7ebf\u6027\u63a2\u6d4b\u91c7\u7528\u56fa\u5b9a\u6b65\u957f\u7684\u7ebf\u6027\u67e5\u627e\u6765\u8fdb\u884c\u63a2\u6d4b\uff0c\u5bf9\u5e94\u7684\u54c8\u5e0c\u8868\u64cd\u4f5c\u65b9\u6cd5\u4e3a\uff1a</p> <ul> <li>\u63d2\u5165\u5143\u7d20\uff1a\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u6570\u7ec4\u7d22\u5f15\uff0c\u82e5\u53d1\u73b0\u6876\u5185\u5df2\u6709\u5143\u7d20\uff0c\u5219\u4ece\u51b2\u7a81\u4f4d\u7f6e\u5411\u540e\u7ebf\u6027\u904d\u5386\uff08\u6b65\u957f\u901a\u5e38\u4e3a \\(1\\) \uff09\uff0c\u76f4\u81f3\u627e\u5230\u7a7a\u4f4d\uff0c\u5c06\u5143\u7d20\u63d2\u5165\u5176\u4e2d\u3002</li> <li>\u67e5\u627e\u5143\u7d20\uff1a\u82e5\u53d1\u73b0\u54c8\u5e0c\u51b2\u7a81\uff0c\u5219\u4f7f\u7528\u76f8\u540c\u6b65\u957f\u5411\u540e\u7ebf\u6027\u904d\u5386\uff0c\u76f4\u5230\u627e\u5230\u5bf9\u5e94\u5143\u7d20\uff0c\u8fd4\u56de <code>value</code> \u5373\u53ef\uff1b\u5982\u679c\u9047\u5230\u7a7a\u4f4d\uff0c\u8bf4\u660e\u76ee\u6807\u952e\u503c\u5bf9\u4e0d\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8fd4\u56de \\(\\text{None}\\) \u3002</li> </ul> <p></p> <p> \u56fe\uff1a\u7ebf\u6027\u63a2\u6d4b </p> <p>\u7136\u800c\uff0c\u7ebf\u6027\u63a2\u6d4b\u5b58\u5728\u4ee5\u4e0b\u7f3a\u9677\uff1a</p> <ul> <li>\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u3002\u5220\u9664\u5143\u7d20\u4f1a\u5728\u6570\u7ec4\u5185\u4ea7\u751f\u4e00\u4e2a\u7a7a\u4f4d\uff0c\u5f53\u67e5\u627e\u8be5\u7a7a\u4f4d\u4e4b\u540e\u7684\u5143\u7d20\u65f6\uff0c\u8be5\u7a7a\u4f4d\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u8bef\u5224\u5143\u7d20\u4e0d\u5b58\u5728\u3002\u4e3a\u6b64\uff0c\u901a\u5e38\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u6807\u5fd7\u4f4d\u6765\u6807\u8bb0\u5df2\u5220\u9664\u5143\u7d20\u3002</li> <li>\u5bb9\u6613\u4ea7\u751f\u805a\u96c6\u3002\u6570\u7ec4\u5185\u8fde\u7eed\u88ab\u5360\u7528\u4f4d\u7f6e\u8d8a\u957f\uff0c\u8fd9\u4e9b\u8fde\u7eed\u4f4d\u7f6e\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u8d8a\u5927\uff0c\u8fdb\u4e00\u6b65\u4fc3\u4f7f\u8fd9\u4e00\u4f4d\u7f6e\u7684\u805a\u5806\u751f\u957f\uff0c\u5f62\u6210\u6076\u6027\u5faa\u73af\uff0c\u6700\u7ec8\u5bfc\u81f4\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u6548\u7387\u52a3\u5316\u3002</li> </ul> <p>\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u3002\u503c\u5f97\u6ce8\u610f\u4e24\u70b9\uff1a</p> <ul> <li>\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u56fa\u5b9a\u7684\u952e\u503c\u5bf9\u5b9e\u4f8b <code>removed</code> \u6765\u6807\u8bb0\u5df2\u5220\u9664\u5143\u7d20\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u4e00\u4e2a\u6876\u5185\u7684\u5143\u7d20\u4e3a \\(\\text{None}\\) \u6216 <code>removed</code> \u65f6\uff0c\u8bf4\u660e\u8fd9\u4e2a\u6876\u662f\u7a7a\u7684\uff0c\u53ef\u7528\u4e8e\u653e\u7f6e\u952e\u503c\u5bf9\u3002</li> <li>\u5728\u7ebf\u6027\u63a2\u6d4b\u65f6\uff0c\u6211\u4eec\u4ece\u5f53\u524d\u7d22\u5f15 <code>index</code> \u5411\u540e\u904d\u5386\uff1b\u800c\u5f53\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u9700\u8981\u56de\u5230\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002</li> </ul> JavaC++PythonGoJSTSCC#SwiftZigDartRust hash_map_open_addressing.java<pre><code>/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nprivate int size; // \u952e\u503c\u5bf9\u6570\u91cf\nprivate int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\nprivate double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nprivate int extendRatio; // \u6269\u5bb9\u500d\u6570\nprivate Pair[] buckets; // \u6876\u6570\u7ec4\nprivate Pair removed; // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\npublic HashMapOpenAddressing() {\nsize = 0;\ncapacity = 4;\nloadThres = 2.0 / 3.0;\nextendRatio = 2;\nbuckets = new Pair[capacity];\nremoved = new Pair(-1, \"-1\");\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\npublic int hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\npublic double loadFactor() {\nreturn (double) size / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic String get(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i &lt; capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\nif (buckets[j] == null)\nreturn null;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (buckets[j].key == key &amp;&amp; buckets[j] != removed)\nreturn buckets[j].val;\n}\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic void put(int key, String val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() &gt; loadThres) {\nextend();\n}\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i &lt; capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\u3001\u6216\u5e26\u6709\u5220\u9664\u6807\u8bb0\u7684\u6876\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u653e\u5165\u8be5\u6876\nif (buckets[j] == null || buckets[j] == removed) {\nbuckets[j] = new Pair(key, val);\nsize += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif (buckets[j].key == key) {\nbuckets[j].val = val;\nreturn;\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i &lt; capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (buckets[j] == null) {\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif (buckets[j].key == key) {\nbuckets[j] = removed;\nsize -= 1;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\npublic void extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nPair[] bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets = new Pair[capacity];\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (Pair pair : bucketsTmp) {\nif (pair != null &amp;&amp; pair != removed) {\nput(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void print() {\nfor (Pair pair : buckets) {\nif (pair != null) {\nSystem.out.println(pair.key + \" -&gt; \" + pair.val);\n} else {\nSystem.out.println(\"null\");\n}\n}\n}\n}\n</code></pre> hash_map_open_addressing.cpp<pre><code>/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nprivate:\nint size; // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\ndouble loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nint extendRatio; // \u6269\u5bb9\u500d\u6570\nvector&lt;Pair *&gt; buckets; // \u6876\u6570\u7ec4\nPair *removed; // \u5220\u9664\u6807\u8bb0\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nHashMapOpenAddressing() {\n// \u6784\u9020\u65b9\u6cd5\nsize = 0;\ncapacity = 4;\nloadThres = 2.0 / 3.0;\nextendRatio = 2;\nbuckets = vector&lt;Pair *&gt;(capacity, nullptr);\nremoved = new Pair(-1, \"-1\");\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn static_cast&lt;double&gt;(size) / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nstring get(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i &lt; capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de nullptr\nif (buckets[j] == nullptr)\nreturn nullptr;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (buckets[j]-&gt;key == key &amp;&amp; buckets[j] != removed)\nreturn buckets[j]-&gt;val;\n}\nreturn nullptr;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, string val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() &gt; loadThres)\nextend();\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i &lt; capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\u3001\u6216\u5e26\u6709\u5220\u9664\u6807\u8bb0\u7684\u6876\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u653e\u5165\u8be5\u6876\nif (buckets[j] == nullptr || buckets[j] == removed) {\nbuckets[j] = new Pair(key, val);\nsize += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif (buckets[j]-&gt;key == key) {\nbuckets[j]-&gt;val = val;\nreturn;\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i &lt; capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (buckets[j] == nullptr)\nreturn;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif (buckets[j]-&gt;key == key) {\ndelete buckets[j]; // \u91ca\u653e\u5185\u5b58\nbuckets[j] = removed;\nsize -= 1;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nvector&lt;Pair *&gt; bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets = vector&lt;Pair *&gt;(capacity, nullptr);\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (Pair *pair : bucketsTmp) {\nif (pair != nullptr &amp;&amp; pair != removed) {\nput(pair-&gt;key, pair-&gt;val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (auto &amp;pair : buckets) {\nif (pair != nullptr) {\ncout &lt;&lt; pair-&gt;key &lt;&lt; \" -&gt; \" &lt;&lt; pair-&gt;val &lt;&lt; endl;\n} else {\ncout &lt;&lt; \"nullptr\" &lt;&lt; endl;\n}\n}\n}\n};\n</code></pre> hash_map_open_addressing.py<pre><code>class HashMapOpenAddressing:\n\"\"\"\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.size = 0 # \u952e\u503c\u5bf9\u6570\u91cf\nself.capacity = 4 # \u54c8\u5e0c\u8868\u5bb9\u91cf\nself.load_thres = 2 / 3 # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nself.extend_ratio = 2 # \u6269\u5bb9\u500d\u6570\nself.buckets: list[Pair | None] = [None] * self.capacity # \u6876\u6570\u7ec4\nself.removed = Pair(-1, \"-1\") # \u5220\u9664\u6807\u8bb0\ndef hash_func(self, key: int) -&gt; int:\n\"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\nreturn key % self.capacity\ndef load_factor(self) -&gt; float:\n\"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\nreturn self.size / self.capacity\ndef get(self, key: int) -&gt; str:\n\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\nindex = self.hash_func(key)\n# \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i in range(self.capacity):\n# \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj = (index + i) % self.capacity\n# \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de None\nif self.buckets[j] is None:\nreturn None\n# \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif self.buckets[j].key == key and self.buckets[j] != self.removed:\nreturn self.buckets[j].val\ndef put(self, key: int, val: str):\n\"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n# \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif self.load_factor() &gt; self.load_thres:\nself.extend()\nindex = self.hash_func(key)\n# \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i in range(self.capacity):\n# \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj = (index + i) % self.capacity\n# \u82e5\u9047\u5230\u7a7a\u6876\u3001\u6216\u5e26\u6709\u5220\u9664\u6807\u8bb0\u7684\u6876\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u653e\u5165\u8be5\u6876\nif self.buckets[j] in [None, self.removed]:\nself.buckets[j] = Pair(key, val)\nself.size += 1\nreturn\n# \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif self.buckets[j].key == key:\nself.buckets[j].val = val\nreturn\ndef remove(self, key: int):\n\"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\nindex = self.hash_func(key)\n# \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i in range(self.capacity):\n# \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj = (index + i) % self.capacity\n# \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif self.buckets[j] is None:\nreturn\n# \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif self.buckets[j].key == key:\nself.buckets[j] = self.removed\nself.size -= 1\nreturn\ndef extend(self):\n\"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n# \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nbuckets_tmp = self.buckets\n# \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nself.capacity *= self.extend_ratio\nself.buckets = [None] * self.capacity\nself.size = 0\n# \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor pair in buckets_tmp:\nif pair not in [None, self.removed]:\nself.put(pair.key, pair.val)\ndef print(self):\n\"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\nfor pair in self.buckets:\nif pair is not None:\nprint(pair.key, \"-&gt;\", pair.val)\nelse:\nprint(\"None\")\n</code></pre> hash_map_open_addressing.go<pre><code>/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntype hashMapOpenAddressing struct {\nsize int // \u952e\u503c\u5bf9\u6570\u91cf\ncapacity int // \u54c8\u5e0c\u8868\u5bb9\u91cf\nloadThres float64 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nextendRatio int // \u6269\u5bb9\u500d\u6570\nbuckets []pair // \u6876\u6570\u7ec4\nremoved pair // \u5220\u9664\u6807\u8bb0\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapOpenAddressing() *hashMapOpenAddressing {\nbuckets := make([]pair, 4)\nreturn &amp;hashMapOpenAddressing{\nsize: 0,\ncapacity: 4,\nloadThres: 2 / 3.0,\nextendRatio: 2,\nbuckets: buckets,\nremoved: pair{\nkey: -1,\nval: \"-1\",\n},\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (m *hashMapOpenAddressing) hashFunc(key int) int {\nreturn key % m.capacity\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (m *hashMapOpenAddressing) loadFactor() float64 {\nreturn float64(m.size) / float64(m.capacity)\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) get(key int) string {\nidx := m.hashFunc(key)\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i := 0; i &lt; m.capacity; i++ {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj := (idx + 1) % m.capacity\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\nif m.buckets[j] == (pair{}) {\nreturn \"\"\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif m.buckets[j].key == key &amp;&amp; m.buckets[j] != m.removed {\nreturn m.buckets[j].val\n}\n}\n// \u82e5\u672a\u627e\u5230 key \u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\nreturn \"\"\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) put(key int, val string) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif m.loadFactor() &gt; m.loadThres {\nm.extend()\n}\nidx := m.hashFunc(key)\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i := 0; i &lt; m.capacity; i++ {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj := (idx + i) % m.capacity\n// \u82e5\u9047\u5230\u7a7a\u6876\u3001\u6216\u5e26\u6709\u5220\u9664\u6807\u8bb0\u7684\u6876\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u653e\u5165\u8be5\u6876\nif m.buckets[j] == (pair{}) || m.buckets[j] == m.removed {\nm.buckets[j] = pair{\nkey: key,\nval: val,\n}\nm.size += 1\nreturn\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif m.buckets[j].key == key {\nm.buckets[j].val = val\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (m *hashMapOpenAddressing) remove(key int) {\nidx := m.hashFunc(key)\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i := 0; i &lt; m.capacity; i++ {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nj := (idx + 1) % m.capacity\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif m.buckets[j] == (pair{}) {\nreturn\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif m.buckets[j].key == key {\nm.buckets[j] = m.removed\nm.size -= 1\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (m *hashMapOpenAddressing) extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\ntmpBuckets := make([]pair, len(m.buckets))\ncopy(tmpBuckets, m.buckets)\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nm.capacity *= m.extendRatio\nm.buckets = make([]pair, m.capacity)\nm.size = 0\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor _, p := range tmpBuckets {\nif p != (pair{}) &amp;&amp; p != m.removed {\nm.put(p.key, p.val)\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (m *hashMapOpenAddressing) print() {\nfor _, p := range m.buckets {\nif p != (pair{}) {\nfmt.Println(strconv.Itoa(p.key) + \" -&gt; \" + p.val)\n} else {\nfmt.Println(\"nil\")\n}\n}\n}\n</code></pre> hash_map_open_addressing.js<pre><code>/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n#size; // \u952e\u503c\u5bf9\u6570\u91cf\n#capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n#loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n#extendRatio; // \u6269\u5bb9\u500d\u6570\n#buckets; // \u6876\u6570\u7ec4\n#removed; // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#size = 0;\nthis.#capacity = 4;\nthis.#loadThres = 2.0 / 3.0;\nthis.#extendRatio = 2;\nthis.#buckets = new Array(this.#capacity).fill(null);\nthis.#removed = new Pair(-1, '-1');\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key) {\nreturn key % this.#capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\n#loadFactor() {\nreturn this.#size / this.#capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key) {\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i &lt; this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nconst j = (index + i) % this.#capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\nif (this.#buckets[j] === null) return null;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (\nthis.#buckets[j].key === key &amp;&amp;\nthis.#buckets[j][key] !== this.#removed.key\n)\nreturn this.#buckets[j].val;\n}\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nput(key, val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (this.#loadFactor() &gt; this.#loadThres) {\nthis.#extend();\n}\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i &lt; this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nlet j = (index + i) % this.#capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\u3001\u6216\u5e26\u6709\u5220\u9664\u6807\u8bb0\u7684\u6876\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u653e\u5165\u8be5\u6876\nif (\nthis.#buckets[j] === null ||\nthis.#buckets[j][key] === this.#removed.key\n) {\nthis.#buckets[j] = new Pair(key, val);\nthis.#size += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif (this.#buckets[j].key === key) {\nthis.#buckets[j].val = val;\nreturn;\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nremove(key) {\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i &lt; this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nconst j = (index + i) % this.#capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (this.#buckets[j] === null) {\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif (this.#buckets[j].key === key) {\nthis.#buckets[j] = this.#removed;\nthis.#size -= 1;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n#extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nconst bucketsTmp = this.#buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nthis.#capacity *= this.#extendRatio;\nthis.#buckets = new Array(this.#capacity).fill(null);\nthis.#size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (const pair of bucketsTmp) {\nif (pair !== null &amp;&amp; pair.key !== this.#removed.key) {\nthis.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint() {\nfor (const pair of this.#buckets) {\nif (pair !== null) {\nconsole.log(pair.key + ' -&gt; ' + pair.val);\n} else {\nconsole.log('null');\n}\n}\n}\n}\n</code></pre> hash_map_open_addressing.ts<pre><code>/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n#size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n#capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n#loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n#extendRatio: number; // \u6269\u5bb9\u500d\u6570\n#buckets: Pair[]; // \u6876\u6570\u7ec4\n#removed: Pair; // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#size = 0;\nthis.#capacity = 4;\nthis.#loadThres = 2.0 / 3.0;\nthis.#extendRatio = 2;\nthis.#buckets = new Array(this.#capacity).fill(null);\nthis.#removed = new Pair(-1, '-1');\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key: number): number {\nreturn key % this.#capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\n#loadFactor(): number {\nreturn this.#size / this.#capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key: number): string | null {\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i &lt; this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nconst j = (index + i) % this.#capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\nif (this.#buckets[j] === null) return null;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (\nthis.#buckets[j].key === key &amp;&amp;\nthis.#buckets[j][key] !== this.#removed.key\n)\nreturn this.#buckets[j].val;\n}\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nput(key: number, val: string): void {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (this.#loadFactor() &gt; this.#loadThres) {\nthis.#extend();\n}\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i &lt; this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nlet j = (index + i) % this.#capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\u3001\u6216\u5e26\u6709\u5220\u9664\u6807\u8bb0\u7684\u6876\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u653e\u5165\u8be5\u6876\nif (\nthis.#buckets[j] === null ||\nthis.#buckets[j][key] === this.#removed.key\n) {\nthis.#buckets[j] = new Pair(key, val);\nthis.#size += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif (this.#buckets[j].key === key) {\nthis.#buckets[j].val = val;\nreturn;\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nremove(key: number): void {\nconst index = this.#hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (let i = 0; i &lt; this.#capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nconst j = (index + i) % this.#capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (this.#buckets[j] === null) {\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif (this.#buckets[j].key === key) {\nthis.#buckets[j] = this.#removed;\nthis.#size -= 1;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n#extend(): void {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nconst bucketsTmp = this.#buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nthis.#capacity *= this.#extendRatio;\nthis.#buckets = new Array(this.#capacity).fill(null);\nthis.#size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (const pair of bucketsTmp) {\nif (pair !== null &amp;&amp; pair.key !== this.#removed.key) {\nthis.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint(): void {\nfor (const pair of this.#buckets) {\nif (pair !== null) {\nconsole.log(pair.key + ' -&gt; ' + pair.val);\n} else {\nconsole.log('null');\n}\n}\n}\n}\n</code></pre> hash_map_open_addressing.c<pre><code>[class]{hashMapOpenAddressing}-[func]{}\n</code></pre> hash_map_open_addressing.cs<pre><code>/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nint size; // \u952e\u503c\u5bf9\u6570\u91cf\nint capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\ndouble loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nint extendRatio; // \u6269\u5bb9\u500d\u6570\nPair[] buckets; // \u6876\u6570\u7ec4\nPair removed; // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\npublic HashMapOpenAddressing() {\nsize = 0;\ncapacity = 4;\nloadThres = 2.0 / 3.0;\nextendRatio = 2;\nbuckets = new Pair[capacity];\nremoved = new Pair(-1, \"-1\");\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int hashFunc(int key) {\nreturn key % capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nprivate double loadFactor() {\nreturn (double)size / capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic string get(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i &lt; capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\nif (buckets[j] == null)\nreturn null;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (buckets[j].key == key &amp;&amp; buckets[j] != removed)\nreturn buckets[j].val;\n}\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic void put(int key, string val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() &gt; loadThres) {\nextend();\n}\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i &lt; capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\u3001\u6216\u5e26\u6709\u5220\u9664\u6807\u8bb0\u7684\u6876\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u653e\u5165\u8be5\u6876\nif (buckets[j] == null || buckets[j] == removed) {\nbuckets[j] = new Pair(key, val);\nsize += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif (buckets[j].key == key) {\nbuckets[j].val = val;\nreturn;\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i &lt; capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (buckets[j] == null) {\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif (buckets[j].key == key) {\nbuckets[j] = removed;\nsize -= 1;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nprivate void extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nPair[] bucketsTmp = buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio;\nbuckets = new Pair[capacity];\nsize = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nforeach (Pair pair in bucketsTmp) {\nif (pair != null &amp;&amp; pair != removed) {\nput(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void print() {\nforeach (Pair pair in buckets) {\nif (pair != null) {\nConsole.WriteLine(pair.key + \" -&gt; \" + pair.val);\n} else {\nConsole.WriteLine(\"null\");\n}\n}\n}\n}\n</code></pre> hash_map_open_addressing.swift<pre><code>/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nvar size: Int // \u952e\u503c\u5bf9\u6570\u91cf\nvar capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\nvar loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nvar extendRatio: Int // \u6269\u5bb9\u500d\u6570\nvar buckets: [Pair?] // \u6876\u6570\u7ec4\nvar removed: Pair // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\ninit() {\nsize = 0\ncapacity = 4\nloadThres = 2 / 3\nextendRatio = 2\nbuckets = Array(repeating: nil, count: capacity)\nremoved = Pair(key: -1, val: \"-1\")\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc hashFunc(key: Int) -&gt; Int {\nkey % capacity\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc loadFactor() -&gt; Double {\nDouble(size / capacity)\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc get(key: Int) -&gt; String? {\nlet index = hashFunc(key: key)\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i in stride(from: 0, to: capacity, by: 1) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nlet j = (index + i) % capacity\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de nil\nif buckets[j] == nil {\nreturn nil\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif buckets[j]?.key == key, buckets[j] != removed {\nreturn buckets[j]?.val\n}\n}\nreturn nil\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc put(key: Int, val: String) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif loadFactor() &gt; loadThres {\nextend()\n}\nlet index = hashFunc(key: key)\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i in stride(from: 0, through: capacity, by: 1) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nlet j = (index + i) % capacity\n// \u82e5\u9047\u5230\u7a7a\u6876\u3001\u6216\u5e26\u6709\u5220\u9664\u6807\u8bb0\u7684\u6876\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u653e\u5165\u8be5\u6876\nif buckets[j] == nil || buckets[j] == removed {\nbuckets[j] = Pair(key: key, val: val)\nsize += 1\nreturn\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif buckets[j]?.key == key {\nbuckets[j]?.val = val\nreturn\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc remove(key: Int) {\nlet index = hashFunc(key: key)\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor i in stride(from: 0, to: capacity, by: 1) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nlet j = (index + i) % capacity\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif buckets[j] == nil {\nreturn\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif buckets[j]?.key == key {\nbuckets[j] = removed\nsize -= 1\nreturn\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nlet bucketsTmp = buckets\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\ncapacity *= extendRatio\nbuckets = Array(repeating: nil, count: capacity)\nsize = 0\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor pair in bucketsTmp {\nif let pair, pair != removed {\nput(key: pair.key, val: pair.val)\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc print() {\nfor pair in buckets {\nif let pair {\nSwift.print(\"\\(pair.key) -&gt; \\(pair.val)\")\n} else {\nSwift.print(\"null\")\n}\n}\n}\n}\n</code></pre> hash_map_open_addressing.zig<pre><code>[class]{HashMapOpenAddressing}-[func]{}\n</code></pre> hash_map_open_addressing.dart<pre><code>/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\nlate int _size; // \u952e\u503c\u5bf9\u6570\u91cf\nlate int _capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\nlate double _loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\nlate int _extendRatio; // \u6269\u5bb9\u500d\u6570\nlate List&lt;Pair?&gt; _buckets; // \u6876\u6570\u7ec4\nlate Pair _removed; // \u5220\u9664\u6807\u8bb0\n/* \u6784\u9020\u65b9\u6cd5 */\nHashMapOpenAddressing() {\n_size = 0;\n_capacity = 4;\n_loadThres = 2.0 / 3.0;\n_extendRatio = 2;\n_buckets = List.generate(_capacity, (index) =&gt; null);\n_removed = Pair(-1, \"-1\");\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nreturn key % _capacity;\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor() {\nreturn _size / _capacity;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nString? get(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i &lt; _capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % _capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de null\nif (_buckets[j] == null) return null;\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nif (_buckets[j]!.key == key &amp;&amp; _buckets[j] != _removed)\nreturn _buckets[j]!.val;\n}\nreturn null;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, String val) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif (loadFactor() &gt; _loadThres) {\nextend();\n}\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i &lt; _capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % _capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\u3001\u6216\u5e26\u6709\u5220\u9664\u6807\u8bb0\u7684\u6876\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u653e\u5165\u8be5\u6876\nif (_buckets[j] == null || _buckets[j] == _removed) {\n_buckets[j] = new Pair(key, val);\n_size += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nif (_buckets[j]!.key == key) {\n_buckets[j]!.val = val;\nreturn;\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor (int i = 0; i &lt; _capacity; i++) {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nint j = (index + i) % _capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (_buckets[j] == null) {\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nif (_buckets[j]!.key == key) {\n_buckets[j] = _removed;\n_size -= 1;\nreturn;\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend() {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nList&lt;Pair?&gt; bucketsTmp = _buckets;\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n_capacity *= _extendRatio;\n_buckets = List.generate(_capacity, (index) =&gt; null);\n_size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor (Pair? pair in bucketsTmp) {\nif (pair != null &amp;&amp; pair != _removed) {\nput(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid printHashMap() {\nfor (Pair? pair in _buckets) {\nif (pair != null) {\nprint(\"${pair.key} -&gt; ${pair.val}\");\n} else {\nprint(null);\n}\n}\n}\n}\n</code></pre> hash_map_open_addressing.rs<pre><code>/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapOpenAddressing {\nsize: usize,\ncapacity: usize,\nload_thres: f32,\nextend_ratio: usize,\nbuckets: Vec&lt;Option&lt;Pair&gt;&gt;,\nremoved: Pair,\n}\nimpl HashMapOpenAddressing {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new() -&gt; Self {\nSelf {\nsize: 0,\ncapacity: 4,\nload_thres: 2.0 / 3.0,\nextend_ratio: 2,\nbuckets: vec![None; 4],\nremoved: Pair {\nkey: -1,\nval: \"-1\".to_string(),\n},\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfn hash_func(&amp;self, key: i32) -&gt; usize {\n(key % self.capacity as i32) as usize\n}\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfn load_factor(&amp;self) -&gt; f32 {\nself.size as f32 / self.capacity as f32\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfn get(&amp;self, key: i32) -&gt; Option&lt;&amp;str&gt; {\nlet mut index = self.hash_func(key);\nlet capacity = self.capacity;\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor _ in 0..capacity {\n// \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nlet j = (index + 1) % capacity;\nmatch &amp;self.buckets[j] {\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u8fd4\u56de None\nNone =&gt; return None,\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\nSome(pair) if pair.key == key &amp;&amp; pair != &amp;self.removed =&gt; return Some(&amp;pair.val),\n_ =&gt; index = j,\n}\n}\nNone\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfn put(&amp;mut self, key: i32, val: String) {\n// \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\nif self.load_factor() &gt; self.load_thres {\nself.extend();\n}\nlet mut index = self.hash_func(key);\nlet capacity = self.capacity;\n// \u7ebf\u6027\u63a2\u6d4b\uff0c\u4ece index \u5f00\u59cb\u5411\u540e\u904d\u5386\nfor _ in 0..capacity {\n//\u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u8fd4\u56de\u5934\u90e8\nlet j = (index + 1) % capacity;\n// \u82e5\u9047\u5230\u7a7a\u6876\u3001\u6216\u5e26\u6709\u5220\u9664\u6807\u8bb0\u7684\u6876\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u653e\u5165\u8be5\u6876\nmatch &amp;mut self.buckets[j] {\nbucket @ &amp;mut None | bucket @ &amp;mut Some(Pair { key: -1, .. }) =&gt; {\n*bucket = Some(Pair { key, val });\nself.size += 1;\nreturn;\n}\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val\nSome(pair) if pair.key == key =&gt; {\npair.val = val;\nreturn;\n}\n_ =&gt; index = j,\n}\n}\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfn remove(&amp;mut self, key: i32) {\nlet mut index = self.hash_func(key);\nlet capacity = self.capacity;\n// \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\nfor _ in 0..capacity {\nlet j = (index + 1) % capacity;\nmatch &amp;mut self.buckets[j] {\n// \u82e5\u9047\u5230\u7a7a\u6876\uff0c\u8bf4\u660e\u65e0\u6b64 key \uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nNone =&gt; return,\n// \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u6807\u8bb0\u5220\u9664\u5e76\u8fd4\u56de\nSome(pair) if pair.key == key =&gt; {\n*pair = Pair {\nkey: -1,\nval: \"-1\".to_string(),\n};\nself.size -= 1;\nreturn;\n}\n_ =&gt; index = j,\n}\n}\n}\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfn extend(&amp;mut self) {\n// \u6682\u5b58\u539f\u54c8\u5e0c\u8868\nlet buckets_tmp = self.buckets.clone();\n// \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\nself.capacity *= self.extend_ratio;\nself.buckets = vec![None; self.capacity];\nself.size = 0;\n// \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\nfor pair in buckets_tmp {\nif let Some(pair) = pair {\nself.put(pair.key, pair.val);\n}\n}\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfn print(&amp;self) {\nfor pair in &amp;self.buckets {\nmatch pair {\nSome(pair) =&gt; println!(\"{} -&gt; {}\", pair.key, pair.val),\nNone =&gt; println!(\"None\"),\n}\n}\n}\n}\n</code></pre>"},{"location":"chapter_hashing/hash_collision/#_2","title":"\u591a\u6b21\u54c8\u5e0c","text":"<p>\u987e\u540d\u601d\u4e49\uff0c\u591a\u6b21\u54c8\u5e0c\u65b9\u6cd5\u662f\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\) , \\(f_2(x)\\) , \\(f_3(x)\\) , \\(\\cdots\\) \u8fdb\u884c\u63a2\u6d4b\u3002</p> <ul> <li>\u63d2\u5165\u5143\u7d20\uff1a\u82e5\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\) \u51fa\u73b0\u51b2\u7a81\uff0c\u5219\u5c1d\u8bd5 \\(f_2(x)\\) \uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u5230\u627e\u5230\u7a7a\u4f4d\u540e\u63d2\u5165\u5143\u7d20\u3002</li> <li>\u67e5\u627e\u5143\u7d20\uff1a\u5728\u76f8\u540c\u7684\u54c8\u5e0c\u51fd\u6570\u987a\u5e8f\u4e0b\u8fdb\u884c\u67e5\u627e\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u5143\u7d20\u65f6\u8fd4\u56de\uff1b\u6216\u9047\u5230\u7a7a\u4f4d\u6216\u5df2\u5c1d\u8bd5\u6240\u6709\u54c8\u5e0c\u51fd\u6570\uff0c\u8bf4\u660e\u54c8\u5e0c\u8868\u4e2d\u4e0d\u5b58\u5728\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(\\text{None}\\) \u3002</li> </ul> <p>\u4e0e\u7ebf\u6027\u63a2\u6d4b\u76f8\u6bd4\uff0c\u591a\u6b21\u54c8\u5e0c\u65b9\u6cd5\u4e0d\u6613\u4ea7\u751f\u805a\u96c6\uff0c\u4f46\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u4f1a\u589e\u52a0\u989d\u5916\u7684\u8ba1\u7b97\u91cf\u3002</p>"},{"location":"chapter_hashing/hash_collision/#623","title":"6.2.3. \u00a0 \u7f16\u7a0b\u8bed\u8a00\u7684\u9009\u62e9","text":"<p>Java \u91c7\u7528\u94fe\u5f0f\u5730\u5740\u3002\u81ea JDK 1.8 \u4ee5\u6765\uff0c\u5f53 HashMap \u5185\u6570\u7ec4\u957f\u5ea6\u8fbe\u5230 64 \u4e14\u94fe\u8868\u957f\u5ea6\u8fbe\u5230 8 \u65f6\uff0c\u94fe\u8868\u4f1a\u88ab\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u4ee5\u63d0\u5347\u67e5\u627e\u6027\u80fd\u3002</p> <p>Python \u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002\u5b57\u5178 dict \u4f7f\u7528\u4f2a\u968f\u673a\u6570\u8fdb\u884c\u63a2\u6d4b\u3002</p> <p>Golang \u91c7\u7528\u94fe\u5f0f\u5730\u5740\u3002Go \u89c4\u5b9a\u6bcf\u4e2a\u6876\u6700\u591a\u5b58\u50a8 8 \u4e2a\u952e\u503c\u5bf9\uff0c\u8d85\u51fa\u5bb9\u91cf\u5219\u8fde\u63a5\u4e00\u4e2a\u6ea2\u51fa\u6876\uff1b\u5f53\u6ea2\u51fa\u6876\u8fc7\u591a\u65f6\uff0c\u4f1a\u6267\u884c\u4e00\u6b21\u7279\u6b8a\u7684\u7b49\u91cf\u6269\u5bb9\u64cd\u4f5c\uff0c\u4ee5\u786e\u4fdd\u6027\u80fd\u3002</p>"},{"location":"chapter_hashing/hash_map/","title":"6.1. \u00a0 \u54c8\u5e0c\u8868","text":"<p>\u6563\u5217\u8868\uff0c\u53c8\u79f0\u300c\u54c8\u5e0c\u8868 Hash Table\u300d\uff0c\u5176\u901a\u8fc7\u5efa\u7acb\u952e <code>key</code> \u4e0e\u503c <code>value</code> \u4e4b\u95f4\u7684\u6620\u5c04\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u8be2\u3002\u5177\u4f53\u800c\u8a00\uff0c\u6211\u4eec\u5411\u54c8\u5e0c\u8868\u8f93\u5165\u4e00\u4e2a\u952e <code>key</code> \uff0c\u5219\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u83b7\u53d6\u5bf9\u5e94\u7684\u503c <code>value</code> \u3002</p> <p>\u4ee5\u4e00\u4e2a\u5305\u542b \\(n\\) \u4e2a\u5b66\u751f\u7684\u6570\u636e\u5e93\u4e3a\u4f8b\uff0c\u6bcf\u4e2a\u5b66\u751f\u90fd\u6709\u201c\u59d3\u540d\u201d\u548c\u201c\u5b66\u53f7\u201d\u4e24\u9879\u6570\u636e\u3002\u5047\u5982\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u201c\u8f93\u5165\u4e00\u4e2a\u5b66\u53f7\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u59d3\u540d\u201d\u7684\u67e5\u8be2\u529f\u80fd\uff0c\u5219\u53ef\u4ee5\u91c7\u7528\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u3002</p> <p></p> <p> \u56fe\uff1a\u54c8\u5e0c\u8868\u7684\u62bd\u8c61\u8868\u793a </p> <p>\u9664\u54c8\u5e0c\u8868\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u6216\u94fe\u8868\u5b9e\u73b0\u67e5\u8be2\u529f\u80fd\u3002\u82e5\u5c06\u5b66\u751f\u6570\u636e\u770b\u4f5c\u6570\u7ec4\uff08\u94fe\u8868\uff09\u5143\u7d20\uff0c\u5219\u6709\uff1a</p> <ul> <li>\u6dfb\u52a0\u5143\u7d20\uff1a\u4ec5\u9700\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u6570\u7ec4\uff08\u94fe\u8868\uff09\u7684\u5c3e\u90e8\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002</li> <li>\u67e5\u8be2\u5143\u7d20\uff1a\u7531\u4e8e\u6570\u7ec4\uff08\u94fe\u8868\uff09\u662f\u4e71\u5e8f\u7684\uff0c\u56e0\u6b64\u9700\u8981\u904d\u5386\u5176\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002</li> <li>\u5220\u9664\u5143\u7d20\uff1a\u9700\u8981\u5148\u67e5\u8be2\u5230\u5143\u7d20\uff0c\u518d\u4ece\u6570\u7ec4\u4e2d\u5220\u9664\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002</li> </ul> <p> \u8868\uff1a\u5143\u7d20\u67e5\u8be2\u6548\u7387\u5bf9\u6bd4 </p> \u6570\u7ec4 \u94fe\u8868 \u54c8\u5e0c\u8868 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) <p>\u89c2\u5bdf\u53d1\u73b0\uff0c\u5728\u54c8\u5e0c\u8868\u4e2d\u8fdb\u884c\u589e\u5220\u67e5\u6539\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f \\(O(1)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002</p>"},{"location":"chapter_hashing/hash_map/#611","title":"6.1.1. \u00a0 \u54c8\u5e0c\u8868\u5e38\u7528\u64cd\u4f5c","text":"<p>\u54c8\u5e0c\u8868\u7684\u5e38\u89c1\u64cd\u4f5c\u5305\u62ec\uff1a\u521d\u59cb\u5316\u3001\u67e5\u8be2\u64cd\u4f5c\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u548c\u5220\u9664\u952e\u503c\u5bf9\u7b49\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust hash_map.java<pre><code>/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap&lt;Integer, String&gt; map = new HashMap&lt;&gt;();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.put(12836, \"\u5c0f\u54c8\"); map.put(15937, \"\u5c0f\u5570\"); map.put(16750, \"\u5c0f\u7b97\"); map.put(13276, \"\u5c0f\u6cd5\");\nmap.put(10583, \"\u5c0f\u9e2d\");\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map.get(15937);\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n</code></pre> hash_map.cpp<pre><code>/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nunordered_map&lt;int, string&gt; map;\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nstring name = map[15937];\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.erase(10583);\n</code></pre> hash_map.py<pre><code># \u521d\u59cb\u5316\u54c8\u5e0c\u8868\nhmap: Dict = {}\n# \u6dfb\u52a0\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n# \u67e5\u8be2\u64cd\u4f5c\n# \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname: str = hmap[15937]\n# \u5220\u9664\u64cd\u4f5c\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nhmap.pop(10583)\n</code></pre> hash_map.go<pre><code>/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nhmap := make(map[int]string)\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nhmap[12836] = \"\u5c0f\u54c8\"\nhmap[15937] = \"\u5c0f\u5570\"\nhmap[16750] = \"\u5c0f\u7b97\"\nhmap[13276] = \"\u5c0f\u6cd5\"\nhmap[10583] = \"\u5c0f\u9e2d\"\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname := hmap[15937]\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\ndelete(hmap, 10583)\n</code></pre> hash_map.js<pre><code>/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nconst map = new ArrayHashMap();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.delete(10583);\n</code></pre> hash_map.ts<pre><code>/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nconst map = new Map&lt;number, string&gt;();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\nconsole.info('\\n\u6dfb\u52a0\u5b8c\u6210\u540e\uff0c\u54c8\u5e0c\u8868\u4e3a\\nKey -&gt; Value');\nconsole.info(map);\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\nconsole.info('\\n\u8f93\u5165\u5b66\u53f7 15937 \uff0c\u67e5\u8be2\u5230\u59d3\u540d ' + name);\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.delete(10583);\nconsole.info('\\n\u5220\u9664 10583 \u540e\uff0c\u54c8\u5e0c\u8868\u4e3a\\nKey -&gt; Value');\nconsole.info(map);\n</code></pre> hash_map.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n</code></pre> hash_map.cs<pre><code>/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nDictionary&lt;int, String&gt; map = new ();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.Add(12836, \"\u5c0f\u54c8\");\nmap.Add(15937, \"\u5c0f\u5570\");\nmap.Add(16750, \"\u5c0f\u7b97\");\nmap.Add(13276, \"\u5c0f\u6cd5\");\nmap.Add(10583, \"\u5c0f\u9e2d\");\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map[15937];\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.Remove(10583);\n</code></pre> hash_map.swift<pre><code>/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nvar map: [Int: String] = [:]\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\"\nmap[15937] = \"\u5c0f\u5570\"\nmap[16750] = \"\u5c0f\u7b97\"\nmap[13276] = \"\u5c0f\u6cd5\"\nmap[10583] = \"\u5c0f\u9e2d\"\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map[15937]!\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.removeValue(forKey: 10583)\n</code></pre> hash_map.zig<pre><code>\n</code></pre> hash_map.dart<pre><code>/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap&lt;int, String&gt; map = {};\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map[15937];\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n</code></pre> hash_map.rs<pre><code>\n</code></pre> <p>\u54c8\u5e0c\u8868\u6709\u4e09\u79cd\u5e38\u7528\u904d\u5386\u65b9\u5f0f\uff1a\u904d\u5386\u952e\u503c\u5bf9\u3001\u904d\u5386\u952e\u548c\u904d\u5386\u503c\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust hash_map.java<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key-&gt;value\nfor (Map.Entry &lt;Integer, String&gt; kv: map.entrySet()) {\nSystem.out.println(kv.getKey() + \" -&gt; \" + kv.getValue());\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor (int key: map.keySet()) {\nSystem.out.println(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (String val: map.values()) {\nSystem.out.println(val);\n}\n</code></pre> hash_map.cpp<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key-&gt;value\nfor (auto kv: map) {\ncout &lt;&lt; kv.first &lt;&lt; \" -&gt; \" &lt;&lt; kv.second &lt;&lt; endl;\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor (auto key: map) {\ncout &lt;&lt; key.first &lt;&lt; endl;\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (auto val: map) {\ncout &lt;&lt; val.second &lt;&lt; endl;\n}\n</code></pre> hash_map.py<pre><code># \u904d\u5386\u54c8\u5e0c\u8868\n# \u904d\u5386\u952e\u503c\u5bf9 key-&gt;value\nfor key, value in hmap.items():\nprint(key, \"-&gt;\", value)\n# \u5355\u72ec\u904d\u5386\u952e key\nfor key in hmap.keys():\nprint(key)\n# \u5355\u72ec\u904d\u5386\u503c value\nfor value in hmap.values():\nprint(value)\n</code></pre> hash_map_test.go<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key-&gt;value\nfor key, value := range hmap {\nfmt.Println(key, \"-&gt;\", value)\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor key := range hmap {\nfmt.Println(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor _, value := range hmap {\nfmt.Println(value)\n}\n</code></pre> hash_map.js<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\nconsole.info('\\n\u904d\u5386\u952e\u503c\u5bf9 Key-&gt;Value');\nfor (const [k, v] of map.entries()) {\nconsole.info(k + ' -&gt; ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\nconsole.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\nconsole.info(v);\n}\n</code></pre> hash_map.ts<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\nconsole.info('\\n\u904d\u5386\u952e\u503c\u5bf9 Key-&gt;Value');\nfor (const [k, v] of map.entries()) {\nconsole.info(k + ' -&gt; ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\nconsole.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\nconsole.info(v);\n}\n</code></pre> hash_map.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n</code></pre> hash_map.cs<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key-&gt;Value\nforeach (var kv in map) {\nConsole.WriteLine(kv.Key + \" -&gt; \" + kv.Value);\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nforeach (int key in map.Keys) {\nConsole.WriteLine(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nforeach (String val in map.Values) {\nConsole.WriteLine(val);\n}\n</code></pre> hash_map.swift<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key-&gt;Value\nfor (key, value) in map {\nprint(\"\\(key) -&gt; \\(value)\")\n}\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys {\nprint(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values {\nprint(value)\n}\n</code></pre> hash_map.zig<pre><code>\n</code></pre> hash_map.dart<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key-&gt;Value\nmap.forEach((key, value) {\nprint('$key -&gt; $value');\n});\n// \u5355\u72ec\u904d\u5386\u952e Key\nmap.keys.forEach((key) {\nprint(key);\n});\n// \u5355\u72ec\u904d\u5386\u503c Value\nmap.values.forEach((value) {\nprint(value);\n});\n</code></pre> hash_map.rs<pre><code>\n</code></pre>"},{"location":"chapter_hashing/hash_map/#612","title":"6.1.2. \u00a0 \u54c8\u5e0c\u8868\u7b80\u5355\u5b9e\u73b0","text":"<p>\u6211\u4eec\u5148\u8003\u8651\u6700\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u4ec5\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u5b9e\u73b0\u54c8\u5e0c\u8868\u3002\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u6211\u4eec\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u7a7a\u4f4d\u79f0\u4e3a\u300c\u6876 Bucket\u300d\uff0c\u6bcf\u4e2a\u6876\u53ef\u5b58\u50a8\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u56e0\u6b64\uff0c\u67e5\u8be2\u64cd\u4f5c\u5c31\u662f\u627e\u5230 <code>key</code> \u5bf9\u5e94\u7684\u6876\uff0c\u5e76\u5728\u6876\u4e2d\u83b7\u53d6 <code>value</code> \u3002</p> <p>\u90a3\u4e48\uff0c\u5982\u4f55\u57fa\u4e8e <code>key</code> \u6765\u5b9a\u4f4d\u5bf9\u5e94\u7684\u6876\u5462\uff1f\u8fd9\u662f\u901a\u8fc7\u300c\u54c8\u5e0c\u51fd\u6570 Hash Function\u300d\u5b9e\u73b0\u7684\u3002\u54c8\u5e0c\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u4e00\u4e2a\u8f83\u5927\u7684\u8f93\u5165\u7a7a\u95f4\u6620\u5c04\u5230\u4e00\u4e2a\u8f83\u5c0f\u7684\u8f93\u51fa\u7a7a\u95f4\u3002\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8f93\u5165\u7a7a\u95f4\u662f\u6240\u6709 <code>key</code> \uff0c\u8f93\u51fa\u7a7a\u95f4\u662f\u6240\u6709\u6876\uff08\u6570\u7ec4\u7d22\u5f15\uff09\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u8f93\u5165\u4e00\u4e2a <code>key</code> \uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u8be5 <code>key</code> \u5bf9\u5e94\u7684\u952e\u503c\u5bf9\u5728\u6570\u7ec4\u4e2d\u7684\u5b58\u50a8\u4f4d\u7f6e\u3002</p> <p>\u8f93\u5165\u4e00\u4e2a <code>key</code> \uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u8fc7\u7a0b\u5206\u4e3a\u4e24\u6b65\uff1a</p> <ol> <li>\u901a\u8fc7\u67d0\u79cd\u54c8\u5e0c\u7b97\u6cd5 <code>hash()</code> \u8ba1\u7b97\u5f97\u5230\u54c8\u5e0c\u503c\u3002</li> <li>\u5c06\u54c8\u5e0c\u503c\u5bf9\u6876\u6570\u91cf\uff08\u6570\u7ec4\u957f\u5ea6\uff09<code>capacity</code> \u53d6\u6a21\uff0c\u4ece\u800c\u83b7\u53d6\u8be5 <code>key</code> \u5bf9\u5e94\u7684\u6570\u7ec4\u7d22\u5f15 <code>index</code> \u3002</li> </ol> <pre><code>index = hash(key) % capacity\n</code></pre> <p>\u968f\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5229\u7528 <code>index</code> \u5728\u54c8\u5e0c\u8868\u4e2d\u8bbf\u95ee\u5bf9\u5e94\u7684\u6876\uff0c\u4ece\u800c\u83b7\u53d6 <code>value</code> \u3002</p> <p>\u8bbe\u6570\u7ec4\u957f\u5ea6 <code>capacity = 100</code> \u3001\u54c8\u5e0c\u7b97\u6cd5 <code>hash(key) = key</code> \uff0c\u6613\u5f97\u54c8\u5e0c\u51fd\u6570\u4e3a <code>key % 100</code> \u3002\u4e0b\u56fe\u4ee5 <code>key</code> \u5b66\u53f7\u548c <code>value</code> \u59d3\u540d\u4e3a\u4f8b\uff0c\u5c55\u793a\u4e86\u54c8\u5e0c\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u3002</p> <p></p> <p> \u56fe\uff1a\u54c8\u5e0c\u51fd\u6570\u5de5\u4f5c\u539f\u7406 </p> <p>\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u54c8\u5e0c\u8868\u3002\u5176\u4e2d\uff0c\u6211\u4eec\u5c06 <code>key</code> \u548c <code>value</code> \u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b <code>Pair</code> \uff0c\u4ee5\u8868\u793a\u952e\u503c\u5bf9\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust array_hash_map.java<pre><code>/* \u952e\u503c\u5bf9 */\nclass Pair {\npublic int key;\npublic String val;\npublic Pair(int key, String val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate List&lt;Pair&gt; buckets;\npublic ArrayHashMap() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nbuckets = new ArrayList&lt;&gt;();\nfor (int i = 0; i &lt; 100; i++) {\nbuckets.add(null);\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int hashFunc(int key) {\nint index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic String get(int key) {\nint index = hashFunc(key);\nPair pair = buckets.get(index);\nif (pair == null)\nreturn null;\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic void put(int key, String val) {\nPair pair = new Pair(key, val);\nint index = hashFunc(key);\nbuckets.set(index, pair);\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key) {\nint index = hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nbuckets.set(index, null);\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npublic List&lt;Pair&gt; pairSet() {\nList&lt;Pair&gt; pairSet = new ArrayList&lt;&gt;();\nfor (Pair pair : buckets) {\nif (pair != null)\npairSet.add(pair);\n}\nreturn pairSet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npublic List&lt;Integer&gt; keySet() {\nList&lt;Integer&gt; keySet = new ArrayList&lt;&gt;();\nfor (Pair pair : buckets) {\nif (pair != null)\nkeySet.add(pair.key);\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npublic List&lt;String&gt; valueSet() {\nList&lt;String&gt; valueSet = new ArrayList&lt;&gt;();\nfor (Pair pair : buckets) {\nif (pair != null)\nvalueSet.add(pair.val);\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void print() {\nfor (Pair kv : pairSet()) {\nSystem.out.println(kv.key + \" -&gt; \" + kv.val);\n}\n}\n}\n</code></pre> array_hash_map.cpp<pre><code>/* \u952e\u503c\u5bf9 */\nstruct Pair {\npublic:\nint key;\nstring val;\nPair(int key, string val) {\nthis-&gt;key = key;\nthis-&gt;val = val;\n}\n};\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate:\nvector&lt;Pair *&gt; buckets;\npublic:\nArrayHashMap() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nbuckets = vector&lt;Pair *&gt;(100);\n}\n~ArrayHashMap() {\n// \u91ca\u653e\u5185\u5b58\nfor (const auto &amp;bucket : buckets) {\ndelete bucket;\n}\nbuckets.clear();\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nint index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nstring get(int key) {\nint index = hashFunc(key);\nPair *pair = buckets[index];\nif (pair == nullptr)\nreturn nullptr;\nreturn pair-&gt;val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, string val) {\nPair *pair = new Pair(key, val);\nint index = hashFunc(key);\nbuckets[index] = pair;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\n// \u91ca\u653e\u5185\u5b58\u5e76\u7f6e\u4e3a nullptr\ndelete buckets[index];\nbuckets[index] = nullptr;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nvector&lt;Pair *&gt; pairSet() {\nvector&lt;Pair *&gt; pairSet;\nfor (Pair *pair : buckets) {\nif (pair != nullptr) {\npairSet.push_back(pair);\n}\n}\nreturn pairSet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nvector&lt;int&gt; keySet() {\nvector&lt;int&gt; keySet;\nfor (Pair *pair : buckets) {\nif (pair != nullptr) {\nkeySet.push_back(pair-&gt;key);\n}\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvector&lt;string&gt; valueSet() {\nvector&lt;string&gt; valueSet;\nfor (Pair *pair : buckets) {\nif (pair != nullptr) {\nvalueSet.push_back(pair-&gt;val);\n}\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (Pair *kv : pairSet()) {\ncout &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.py<pre><code>class Pair:\n\"\"\"\u952e\u503c\u5bf9\"\"\"\ndef __init__(self, key: int, val: str):\nself.key = key\nself.val = val\nclass ArrayHashMap:\n\"\"\"\u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n# \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nself.buckets: list[Pair | None] = [None] * 100\ndef hash_func(self, key: int) -&gt; int:\n\"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\nindex = key % 100\nreturn index\ndef get(self, key: int) -&gt; str:\n\"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\nindex: int = self.hash_func(key)\npair: Pair = self.buckets[index]\nif pair is None:\nreturn None\nreturn pair.val\ndef put(self, key: int, val: str):\n\"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\npair = Pair(key, val)\nindex: int = self.hash_func(key)\nself.buckets[index] = pair\ndef remove(self, key: int):\n\"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\nindex: int = self.hash_func(key)\n# \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\nself.buckets[index] = None\ndef entry_set(self) -&gt; list[Pair]:\n\"\"\"\u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\"\"\"\nresult: list[Pair] = []\nfor pair in self.buckets:\nif pair is not None:\nresult.append(pair)\nreturn result\ndef key_set(self) -&gt; list[int]:\n\"\"\"\u83b7\u53d6\u6240\u6709\u952e\"\"\"\nresult = []\nfor pair in self.buckets:\nif pair is not None:\nresult.append(pair.key)\nreturn result\ndef value_set(self) -&gt; list[str]:\n\"\"\"\u83b7\u53d6\u6240\u6709\u503c\"\"\"\nresult = []\nfor pair in self.buckets:\nif pair is not None:\nresult.append(pair.val)\nreturn result\ndef print(self):\n\"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\nfor pair in self.buckets:\nif pair is not None:\nprint(pair.key, \"-&gt;\", pair.val)\n</code></pre> array_hash_map.go<pre><code>/* \u952e\u503c\u5bf9 */\ntype pair struct {\nkey int\nval string\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntype arrayHashMap struct {\nbuckets []*pair\n}\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nfunc newArrayHashMap() *arrayHashMap {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nbuckets := make([]*pair, 100)\nreturn &amp;arrayHashMap{buckets: buckets}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (a *arrayHashMap) hashFunc(key int) int {\nindex := key % 100\nreturn index\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (a *arrayHashMap) get(key int) string {\nindex := a.hashFunc(key)\npair := a.buckets[index]\nif pair == nil {\nreturn \"Not Found\"\n}\nreturn pair.val\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (a *arrayHashMap) put(key int, val string) {\npair := &amp;pair{key: key, val: val}\nindex := a.hashFunc(key)\na.buckets[index] = pair\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (a *arrayHashMap) remove(key int) {\nindex := a.hashFunc(key)\n// \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\na.buckets[index] = nil\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u5bf9 */\nfunc (a *arrayHashMap) pairSet() []*pair {\nvar pairs []*pair\nfor _, pair := range a.buckets {\nif pair != nil {\npairs = append(pairs, pair)\n}\n}\nreturn pairs\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nfunc (a *arrayHashMap) keySet() []int {\nvar keys []int\nfor _, pair := range a.buckets {\nif pair != nil {\nkeys = append(keys, pair.key)\n}\n}\nreturn keys\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nfunc (a *arrayHashMap) valueSet() []string {\nvar values []string\nfor _, pair := range a.buckets {\nif pair != nil {\nvalues = append(values, pair.val)\n}\n}\nreturn values\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (a *arrayHashMap) print() {\nfor _, pair := range a.buckets {\nif pair != nil {\nfmt.Println(pair.key, \"-&gt;\", pair.val)\n}\n}\n}\n</code></pre> array_hash_map.js<pre><code>/* \u952e\u503c\u5bf9 Number -&gt; String */\nclass Pair {\nconstructor(key, val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n#buckets;\nconstructor() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nthis.#buckets = new Array(100).fill(null);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key) {\nreturn key % 100;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key) {\nlet index = this.#hashFunc(key);\nlet pair = this.#buckets[index];\nif (pair === null) return null;\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nset(key, val) {\nlet index = this.#hashFunc(key);\nthis.#buckets[index] = new Pair(key, val);\n}\n/* \u5220\u9664\u64cd\u4f5c */\ndelete(key) {\nlet index = this.#hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nthis.#buckets[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nentries() {\nlet arr = [];\nfor (let i = 0; i &lt; this.#buckets.length; i++) {\nif (this.#buckets[i]) {\narr.push(this.#buckets[i]);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nkeys() {\nlet arr = [];\nfor (let i = 0; i &lt; this.#buckets.length; i++) {\nif (this.#buckets[i]) {\narr.push(this.#buckets[i].key);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvalues() {\nlet arr = [];\nfor (let i = 0; i &lt; this.#buckets.length; i++) {\nif (this.#buckets[i]) {\narr.push(this.#buckets[i].val);\n}\n}\nreturn arr;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint() {\nlet pairSet = this.entries();\nfor (const pair of pairSet) {\nif (!pair) continue;\nconsole.info(`${pair.key} -&gt; ${pair.val}`);\n}\n}\n}\n</code></pre> array_hash_map.ts<pre><code>/* \u952e\u503c\u5bf9 Number -&gt; String */\nclass Pair {\npublic key: number;\npublic val: string;\nconstructor(key: number, val: string) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate readonly buckets: (Pair | null)[];\nconstructor() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nthis.buckets = new Array(100).fill(null);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate hashFunc(key: number): number {\nreturn key % 100;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic get(key: number): string | null {\nlet index = this.hashFunc(key);\nlet pair = this.buckets[index];\nif (pair === null) return null;\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic set(key: number, val: string) {\nlet index = this.hashFunc(key);\nthis.buckets[index] = new Pair(key, val);\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic delete(key: number) {\nlet index = this.hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nthis.buckets[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npublic entries(): (Pair | null)[] {\nlet arr: (Pair | null)[] = [];\nfor (let i = 0; i &lt; this.buckets.length; i++) {\nif (this.buckets[i]) {\narr.push(this.buckets[i]);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npublic keys(): (number | undefined)[] {\nlet arr: (number | undefined)[] = [];\nfor (let i = 0; i &lt; this.buckets.length; i++) {\nif (this.buckets[i]) {\narr.push(this.buckets[i].key);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npublic values(): (string | undefined)[] {\nlet arr: (string | undefined)[] = [];\nfor (let i = 0; i &lt; this.buckets.length; i++) {\nif (this.buckets[i]) {\narr.push(this.buckets[i].val);\n}\n}\nreturn arr;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic print() {\nlet pairSet = this.entries();\nfor (const pair of pairSet) {\nif (!pair) continue;\nconsole.info(`${pair.key} -&gt; ${pair.val}`);\n}\n}\n}\n</code></pre> array_hash_map.c<pre><code>/* \u952e\u503c\u5bf9 int-&gt;string */\nstruct pair {\nint key;\nchar *val;\n};\ntypedef struct pair pair;\n[class]{arrayHashMap}-[func]{}\n</code></pre> array_hash_map.cs<pre><code>/* \u952e\u503c\u5bf9 int-&gt;string */\nclass Pair {\npublic int key;\npublic string val;\npublic Pair(int key, string val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate List&lt;Pair?&gt; buckets;\npublic ArrayHashMap() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nbuckets = new();\nfor (int i = 0; i &lt; 100; i++) {\nbuckets.Add(null);\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int hashFunc(int key) {\nint index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic string? get(int key) {\nint index = hashFunc(key);\nPair? pair = buckets[index];\nif (pair == null) return null;\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic void put(int key, string val) {\nPair pair = new Pair(key, val);\nint index = hashFunc(key);\nbuckets[index] = pair;\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key) {\nint index = hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nbuckets[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npublic List&lt;Pair&gt; pairSet() {\nList&lt;Pair&gt; pairSet = new();\nforeach (Pair? pair in buckets) {\nif (pair != null)\npairSet.Add(pair);\n}\nreturn pairSet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npublic List&lt;int&gt; keySet() {\nList&lt;int&gt; keySet = new();\nforeach (Pair? pair in buckets) {\nif (pair != null)\nkeySet.Add(pair.key);\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npublic List&lt;string&gt; valueSet() {\nList&lt;string&gt; valueSet = new();\nforeach (Pair? pair in buckets) {\nif (pair != null)\nvalueSet.Add(pair.val);\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void print() {\nforeach (Pair kv in pairSet()) {\nConsole.WriteLine(kv.key + \" -&gt; \" + kv.val);\n}\n}\n}\n</code></pre> array_hash_map.swift<pre><code>/* \u952e\u503c\u5bf9 */\nclass Pair {\nvar key: Int\nvar val: String\ninit(key: Int, val: String) {\nself.key = key\nself.val = val\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate var buckets: [Pair?] = []\ninit() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nfor _ in 0 ..&lt; 100 {\nbuckets.append(nil)\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate func hashFunc(key: Int) -&gt; Int {\nlet index = key % 100\nreturn index\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc get(key: Int) -&gt; String? {\nlet index = hashFunc(key: key)\nlet pair = buckets[index]\nreturn pair?.val\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc put(key: Int, val: String) {\nlet pair = Pair(key: key, val: val)\nlet index = hashFunc(key: key)\nbuckets[index] = pair\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc remove(key: Int) {\nlet index = hashFunc(key: key)\n// \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\nbuckets[index] = nil\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nfunc pairSet() -&gt; [Pair] {\nvar pairSet: [Pair] = []\nfor pair in buckets {\nif let pair = pair {\npairSet.append(pair)\n}\n}\nreturn pairSet\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nfunc keySet() -&gt; [Int] {\nvar keySet: [Int] = []\nfor pair in buckets {\nif let pair = pair {\nkeySet.append(pair.key)\n}\n}\nreturn keySet\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nfunc valueSet() -&gt; [String] {\nvar valueSet: [String] = []\nfor pair in buckets {\nif let pair = pair {\nvalueSet.append(pair.val)\n}\n}\nreturn valueSet\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc print() {\nfor pair in pairSet() {\nSwift.print(\"\\(pair.key) -&gt; \\(pair.val)\")\n}\n}\n}\n</code></pre> array_hash_map.zig<pre><code>// \u952e\u503c\u5bf9\nconst Pair = struct {\nkey: usize = undefined,\nval: []const u8 = undefined,\npub fn init(key: usize, val: []const u8) Pair {\nreturn Pair {\n.key = key,\n.val = val,\n};\n}\n};\n// \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\nfn ArrayHashMap(comptime T: type) type {\nreturn struct {\nbucket: ?std.ArrayList(?T) = null,\nmem_allocator: std.mem.Allocator = undefined,\nconst Self = @This();\n// \u6784\u9020\u51fd\u6570\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nself.mem_allocator = allocator;\n// \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\nself.bucket = std.ArrayList(?T).init(self.mem_allocator);\nvar i: i32 = 0;\nwhile (i &lt; 100) : (i += 1) {\ntry self.bucket.?.append(null);\n}\n}\n// \u6790\u6784\u51fd\u6570\npub fn deinit(self: *Self) void {\nif (self.bucket != null) self.bucket.?.deinit();\n}\n// \u54c8\u5e0c\u51fd\u6570\nfn hashFunc(key: usize) usize {\nvar index = key % 100;\nreturn index;\n}\n// \u67e5\u8be2\u64cd\u4f5c\npub fn get(self: *Self, key: usize) []const u8 {\nvar index = hashFunc(key);\nvar pair = self.bucket.?.items[index];\nreturn pair.?.val;\n}\n// \u6dfb\u52a0\u64cd\u4f5c\npub fn put(self: *Self, key: usize, val: []const u8) !void {\nvar pair = Pair.init(key, val);\nvar index = hashFunc(key);\nself.bucket.?.items[index] = pair;\n}\n// \u5220\u9664\u64cd\u4f5c\npub fn remove(self: *Self, key: usize) !void {\nvar index = hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nself.bucket.?.items[index] = null;\n} // \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\npub fn pairSet(self: *Self) !std.ArrayList(T) {\nvar entry_set = std.ArrayList(T).init(self.mem_allocator);\nfor (self.bucket.?.items) |item| {\nif (item == null) continue;\ntry entry_set.append(item.?);\n}\nreturn entry_set;\n} // \u83b7\u53d6\u6240\u6709\u952e\npub fn keySet(self: *Self) !std.ArrayList(usize) {\nvar key_set = std.ArrayList(usize).init(self.mem_allocator);\nfor (self.bucket.?.items) |item| {\nif (item == null) continue;\ntry key_set.append(item.?.key);\n}\nreturn key_set;\n} // \u83b7\u53d6\u6240\u6709\u503c\npub fn valueSet(self: *Self) !std.ArrayList([]const u8) {\nvar value_set = std.ArrayList([]const u8).init(self.mem_allocator);\nfor (self.bucket.?.items) |item| {\nif (item == null) continue;\ntry value_set.append(item.?.val);\n}\nreturn value_set;\n}\n// \u6253\u5370\u54c8\u5e0c\u8868\npub fn print(self: *Self) !void {\nvar entry_set = try self.pairSet();\ndefer entry_set.deinit();\nfor (entry_set.items) |item| {\nstd.debug.print(\"{} -&gt; {s}\\n\", .{item.key, item.val});\n}\n}\n};\n}\n</code></pre> array_hash_map.dart<pre><code>/* \u952e\u503c\u5bf9 */\nclass Pair {\nint key;\nString val;\nPair(this.key, this.val);\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nlate List&lt;Pair?&gt; _buckets;\nArrayHashMap() {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n_buckets = List.filled(100, null);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint _hashFunc(int key) {\nfinal int index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nString? get(int key) {\nfinal int index = _hashFunc(key);\nfinal Pair? pair = _buckets[index];\nif (pair == null) {\nreturn null;\n}\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, String val) {\nfinal Pair pair = Pair(key, val);\nfinal int index = _hashFunc(key);\n_buckets[index] = pair;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nfinal int index = _hashFunc(key);\n_buckets[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nList&lt;Pair&gt; pairSet() {\nList&lt;Pair&gt; pairSet = [];\nfor (final Pair? pair in _buckets) {\nif (pair != null) {\npairSet.add(pair);\n}\n}\nreturn pairSet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nList&lt;int&gt; keySet() {\nList&lt;int&gt; keySet = [];\nfor (final Pair? pair in _buckets) {\nif (pair != null) {\nkeySet.add(pair.key);\n}\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nList&lt;String&gt; values() {\nList&lt;String&gt; valueSet = [];\nfor (final Pair? pair in _buckets) {\nif (pair != null) {\nvalueSet.add(pair.val);\n}\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid printHashMap() {\nfor (final Pair kv in pairSet()) {\nprint(\"${kv.key} -&gt; ${kv.val}\");\n}\n}\n}\n</code></pre> array_hash_map.rs<pre><code>/* \u952e\u503c\u5bf9 */\npub struct Pair {\npub key: i32,\npub val: String,\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\npub struct ArrayHashMap {\nbuckets: Vec&lt;Option&lt;Pair&gt;&gt;\n}\nimpl ArrayHashMap {\npub fn new() -&gt; ArrayHashMap {\n// \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\nSelf { buckets: vec![None; 100] }\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfn hash_func(&amp;self, key: i32) -&gt; usize {\nkey as usize % 100\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npub fn get(&amp;self, key: i32) -&gt; Option&lt;&amp;String&gt; {\nlet index = self.hash_func(key);\nself.buckets[index].as_ref().map(|pair| &amp;pair.val)\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npub fn put(&amp;mut self, key: i32, val: &amp;str) {\nlet index = self.hash_func(key);\nself.buckets[index] = Some(Pair {\nkey,\nval: val.to_string(),\n});\n}\n/* \u5220\u9664\u64cd\u4f5c */\npub fn remove(&amp;mut self, key: i32) {\nlet index = self.hash_func(key);\n// \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\nself.buckets[index] = None;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npub fn entry_set(&amp;self) -&gt; Vec&lt;&amp;Pair&gt; {\nself.buckets.iter().filter_map(|pair| pair.as_ref()).collect()\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npub fn key_set(&amp;self) -&gt; Vec&lt;&amp;i32&gt; {\nself.buckets.iter().filter_map(|pair| pair.as_ref().map(|pair| &amp;pair.key)).collect()\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npub fn value_set(&amp;self) -&gt; Vec&lt;&amp;String&gt; {\nself.buckets.iter().filter_map(|pair| pair.as_ref().map(|pair| &amp;pair.val)).collect()\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npub fn print(&amp;self) {\nfor pair in self.entry_set() {\nprintln!(\"{} -&gt; {}\", pair.key, pair.val);\n}\n}\n}\n</code></pre>"},{"location":"chapter_hashing/hash_map/#613","title":"6.1.3. \u00a0 \u54c8\u5e0c\u51b2\u7a81\u4e0e\u6269\u5bb9","text":"<p>\u672c\u8d28\u4e0a\u770b\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u6240\u6709 <code>key</code> \u6784\u6210\u7684\u8f93\u5165\u7a7a\u95f4\u6620\u5c04\u5230\u6570\u7ec4\u6240\u6709\u7d22\u5f15\u6784\u6210\u7684\u8f93\u51fa\u7a7a\u95f4\uff0c\u800c\u8f93\u5165\u7a7a\u95f4\u5f80\u5f80\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\u3002\u56e0\u6b64\uff0c\u7406\u8bba\u4e0a\u4e00\u5b9a\u5b58\u5728\u201c\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u76f8\u540c\u8f93\u51fa\u201d\u7684\u60c5\u51b5\u3002</p> <p>\u5bf9\u4e8e\u4e0a\u8ff0\u793a\u4f8b\u4e2d\u7684\u54c8\u5e0c\u51fd\u6570\uff0c\u5f53\u8f93\u5165\u7684 <code>key</code> \u540e\u4e24\u4f4d\u76f8\u540c\u65f6\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u51fa\u7ed3\u679c\u4e5f\u76f8\u540c\u3002\u4f8b\u5982\uff0c\u67e5\u8be2\u5b66\u53f7\u4e3a 12836 \u548c 20336 \u7684\u4e24\u4e2a\u5b66\u751f\u65f6\uff0c\u6211\u4eec\u5f97\u5230\uff1a</p> <pre><code>12836 % 100 = 36\n20336 % 100 = 36\n</code></pre> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4e24\u4e2a\u5b66\u53f7\u6307\u5411\u4e86\u540c\u4e00\u4e2a\u59d3\u540d\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002\u6211\u4eec\u5c06\u8fd9\u79cd\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u540c\u4e00\u8f93\u51fa\u7684\u60c5\u51b5\u79f0\u4e3a\u300c\u54c8\u5e0c\u51b2\u7a81 Hash Collision\u300d\u3002</p> <p></p> <p> \u56fe\uff1a\u54c8\u5e0c\u51b2\u7a81\u793a\u4f8b </p> <p>\u5bb9\u6613\u60f3\u5230\uff0c\u54c8\u5e0c\u8868\u5bb9\u91cf \\(n\\) \u8d8a\u5927\uff0c\u591a\u4e2a <code>key</code> \u88ab\u5206\u914d\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\uff0c\u51b2\u7a81\u5c31\u8d8a\u5c11\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6269\u5bb9\u54c8\u5e0c\u8868\u6765\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u6269\u5bb9\u524d\u952e\u503c\u5bf9 <code>(136, A)</code> \u548c <code>(236, D)</code> \u53d1\u751f\u51b2\u7a81\uff0c\u6269\u5bb9\u540e\u51b2\u7a81\u6d88\u5931\u3002</p> <p></p> <p> \u56fe\uff1a\u54c8\u5e0c\u8868\u6269\u5bb9 </p> <p>\u7c7b\u4f3c\u4e8e\u6570\u7ec4\u6269\u5bb9\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u9700\u5c06\u6240\u6709\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u8fc1\u79fb\u81f3\u65b0\u54c8\u5e0c\u8868\uff0c\u975e\u5e38\u8017\u65f6\u3002\u5e76\u4e14\u7531\u4e8e\u54c8\u5e0c\u8868\u5bb9\u91cf <code>capacity</code> \u6539\u53d8\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u6765\u91cd\u65b0\u8ba1\u7b97\u6240\u6709\u952e\u503c\u5bf9\u7684\u5b58\u50a8\u4f4d\u7f6e\uff0c\u8fd9\u8fdb\u4e00\u6b65\u63d0\u9ad8\u4e86\u6269\u5bb9\u8fc7\u7a0b\u7684\u8ba1\u7b97\u5f00\u9500\u3002\u4e3a\u6b64\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u9884\u7559\u8db3\u591f\u5927\u7684\u54c8\u5e0c\u8868\u5bb9\u91cf\uff0c\u9632\u6b62\u9891\u7e41\u6269\u5bb9\u3002</p> <p>\u300c\u8d1f\u8f7d\u56e0\u5b50 Load Factor\u300d\u662f\u54c8\u5e0c\u8868\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5176\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u7684\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\uff0c\u7528\u4e8e\u8861\u91cf\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u4e5f\u5e38\u88ab\u4f5c\u4e3a\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u89e6\u53d1\u6761\u4ef6\u3002\u4f8b\u5982\u5728 Java \u4e2d\uff0c\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(0.75\\) \u65f6\uff0c\u7cfb\u7edf\u4f1a\u5c06\u54c8\u5e0c\u8868\u5bb9\u91cf\u6269\u5c55\u4e3a\u539f\u5148\u7684 \\(2\\) \u500d\u3002</p>"},{"location":"chapter_hashing/summary/","title":"6.4. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u8f93\u5165 <code>key</code> \uff0c\u54c8\u5e0c\u8868\u80fd\u591f\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u67e5\u8be2\u5230 <code>value</code> \uff0c\u6548\u7387\u975e\u5e38\u9ad8\u3002</li> <li>\u5e38\u89c1\u7684\u54c8\u5e0c\u8868\u64cd\u4f5c\u5305\u62ec\u67e5\u8be2\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u3001\u5220\u9664\u952e\u503c\u5bf9\u548c\u904d\u5386\u54c8\u5e0c\u8868\u7b49\u3002</li> <li>\u54c8\u5e0c\u51fd\u6570\u5c06 <code>key</code> \u6620\u5c04\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u4ece\u800c\u8bbf\u95ee\u5bf9\u5e94\u6876\u5e76\u83b7\u53d6 <code>value</code> \u3002</li> <li>\u4e24\u4e2a\u4e0d\u540c\u7684 <code>key</code> \u53ef\u80fd\u5728\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u540e\u5f97\u5230\u76f8\u540c\u7684\u6570\u7ec4\u7d22\u5f15\uff0c\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u51fa\u9519\uff0c\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4e3a\u54c8\u5e0c\u51b2\u7a81\u3002</li> <li>\u54c8\u5e0c\u8868\u5bb9\u91cf\u8d8a\u5927\uff0c\u54c8\u5e0c\u51b2\u7a81\u7684\u6982\u7387\u5c31\u8d8a\u4f4e\u3002\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u6269\u5bb9\u54c8\u5e0c\u8868\u6765\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\u3002\u4e0e\u6570\u7ec4\u6269\u5bb9\u7c7b\u4f3c\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u64cd\u4f5c\u7684\u5f00\u9500\u5f88\u5927\u3002</li> <li>\u8d1f\u8f7d\u56e0\u5b50\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u4e2d\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\uff0c\u53cd\u6620\u4e86\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u5e38\u7528\u4f5c\u89e6\u53d1\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u6761\u4ef6\u3002</li> <li>\u94fe\u5f0f\u5730\u5740\u901a\u8fc7\u5c06\u5355\u4e2a\u5143\u7d20\u8f6c\u5316\u4e3a\u94fe\u8868\uff0c\u5c06\u6240\u6709\u51b2\u7a81\u5143\u7d20\u5b58\u50a8\u5728\u540c\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002\u7136\u800c\uff0c\u94fe\u8868\u8fc7\u957f\u4f1a\u964d\u4f4e\u67e5\u8be2\u6548\u7387\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u7ea2\u9ed1\u6811\u6765\u63d0\u9ad8\u6548\u7387\u3002</li> <li>\u5f00\u653e\u5bfb\u5740\u901a\u8fc7\u591a\u6b21\u63a2\u6d4b\u6765\u5904\u7406\u54c8\u5e0c\u51b2\u7a81\u3002\u7ebf\u6027\u63a2\u6d4b\u4f7f\u7528\u56fa\u5b9a\u6b65\u957f\uff0c\u7f3a\u70b9\u662f\u4e0d\u80fd\u5220\u9664\u5143\u7d20\uff0c\u4e14\u5bb9\u6613\u4ea7\u751f\u805a\u96c6\u3002\u591a\u6b21\u54c8\u5e0c\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u8fdb\u884c\u63a2\u6d4b\uff0c\u76f8\u8f83\u7ebf\u6027\u63a2\u6d4b\u66f4\u4e0d\u6613\u4ea7\u751f\u805a\u96c6\uff0c\u4f46\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u589e\u52a0\u4e86\u8ba1\u7b97\u91cf\u3002</li> <li>\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u91c7\u53d6\u4e86\u4e0d\u540c\u7684\u54c8\u5e0c\u8868\u5b9e\u73b0\u3002\u4f8b\u5982\uff0cJava \u7684 <code>HashMap</code> \u4f7f\u7528\u94fe\u5f0f\u5730\u5740\uff0c\u800c Python \u7684 <code>Dict</code> \u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002</li> <li>\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u6211\u4eec\u5e0c\u671b\u54c8\u5e0c\u7b97\u6cd5\u5177\u6709\u786e\u5b9a\u6027\u3001\u9ad8\u6548\u7387\u548c\u5747\u5300\u5206\u5e03\u7684\u7279\u70b9\u3002\u5728\u5bc6\u7801\u5b66\u4e2d\uff0c\u54c8\u5e0c\u7b97\u6cd5\u8fd8\u5e94\u8be5\u5177\u5907\u6297\u78b0\u649e\u6027\u548c\u96ea\u5d29\u6548\u5e94\u3002</li> <li>\u54c8\u5e0c\u7b97\u6cd5\u901a\u5e38\u91c7\u7528\u5927\u8d28\u6570\u4f5c\u4e3a\u6a21\u6570\uff0c\u4ee5\u6700\u5927\u5316\u5730\u4fdd\u8bc1\u54c8\u5e0c\u503c\u7684\u5747\u5300\u5206\u5e03\uff0c\u51cf\u5c11\u54c8\u5e0c\u51b2\u7a81\u3002</li> <li>\u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5\u5305\u62ec MD5, SHA-1, SHA-2, SHA3 \u7b49\u3002MD5 \u5e38\u7528\u4e8e\u6821\u9a8c\u6587\u4ef6\u5b8c\u6574\u6027\uff0cSHA-2 \u5e38\u7528\u4e8e\u5b89\u5168\u5e94\u7528\u4e0e\u534f\u8bae\u3002</li> <li>\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u4f1a\u4e3a\u6570\u636e\u7c7b\u578b\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u7b97\u6cd5\uff0c\u7528\u4e8e\u8ba1\u7b97\u54c8\u5e0c\u8868\u4e2d\u7684\u6876\u7d22\u5f15\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u53ea\u6709\u4e0d\u53ef\u53d8\u5bf9\u8c61\u662f\u53ef\u54c8\u5e0c\u7684\u3002</li> </ul>"},{"location":"chapter_hashing/summary/#641-q-a","title":"6.4.1. \u00a0 Q &amp; A","text":"<p>\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u4ec0\u4e48\u4e0d\u662f \\(O(n)\\) \uff1f</p> <p>\u5f53\u54c8\u5e0c\u51b2\u7a81\u6bd4\u8f83\u4e25\u91cd\u65f6\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u9000\u5316\u81f3 \\(O(n)\\) \u3002\u5f53\u54c8\u5e0c\u51fd\u6570\u8bbe\u8ba1\u7684\u6bd4\u8f83\u597d\u3001\u5bb9\u91cf\u8bbe\u7f6e\u6bd4\u8f83\u5408\u7406\u3001\u51b2\u7a81\u6bd4\u8f83\u5e73\u5747\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002\u6211\u4eec\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u7684\u54c8\u5e0c\u8868\u65f6\uff0c\u901a\u5e38\u8ba4\u4e3a\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(1)\\) \u3002</p> <p>\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528\u54c8\u5e0c\u51fd\u6570 \\(f(x) = x\\) \u5462\uff1f\u8fd9\u6837\u5c31\u4e0d\u4f1a\u6709\u51b2\u7a81\u4e86</p> <p>\u5728 \\(f(x) = x\\) \u54c8\u5e0c\u51fd\u6570\u4e0b\uff0c\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u552f\u4e00\u7684\u6876\u7d22\u5f15\uff0c\u8fd9\u4e0e\u6570\u7ec4\u7b49\u4ef7\u3002\u7136\u800c\uff0c\u8f93\u5165\u7a7a\u95f4\u901a\u5e38\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff08\u6570\u7ec4\u957f\u5ea6\uff09\uff0c\u56e0\u6b64\u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u5f80\u5f80\u662f\u5bf9\u6570\u7ec4\u957f\u5ea6\u53d6\u6a21\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u54c8\u5e0c\u8868\u7684\u76ee\u6807\u662f\u5c06\u4e00\u4e2a\u8f83\u5927\u7684\u72b6\u6001\u7a7a\u95f4\u6620\u5c04\u5230\u4e00\u4e2a\u8f83\u5c0f\u7684\u7a7a\u95f4\uff0c\u5e76\u63d0\u4f9b \\(O(1)\\) \u7684\u67e5\u8be2\u6548\u7387\u3002</p> <p>\u54c8\u5e0c\u8868\u5e95\u5c42\u5b9e\u73b0\u662f\u6570\u7ec4\u3001\u94fe\u8868\u3001\u4e8c\u53c9\u6811\uff0c\u4f46\u4e3a\u4ec0\u4e48\u6548\u7387\u53ef\u4ee5\u6bd4\u4ed6\u4eec\u66f4\u9ad8\u5462\uff1f</p> <p>\u9996\u5148\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u6548\u7387\u53d8\u9ad8\uff0c\u4f46\u7a7a\u95f4\u6548\u7387\u53d8\u4f4e\u4e86\u3002\u54c8\u5e0c\u8868\u6709\u76f8\u5f53\u4e00\u90e8\u5206\u7684\u5185\u5b58\u662f\u672a\u4f7f\u7528\u7684\uff0c</p> <p>\u5176\u6b21\uff0c\u53ea\u662f\u5728\u7279\u5b9a\u4f7f\u7528\u573a\u666f\u4e0b\u65f6\u95f4\u6548\u7387\u53d8\u9ad8\u4e86\u3002\u5982\u679c\u4e00\u4e2a\u529f\u80fd\u80fd\u591f\u5728\u76f8\u540c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u4f7f\u7528\u6570\u7ec4\u6216\u94fe\u8868\u5b9e\u73b0\uff0c\u90a3\u4e48\u901a\u5e38\u6bd4\u54c8\u5e0c\u8868\u66f4\u5feb\u3002\u8fd9\u662f\u56e0\u4e3a\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u9700\u8981\u5f00\u9500\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u6570\u9879\u66f4\u5927\u3002</p> <p>\u6700\u540e\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u80fd\u53d1\u751f\u52a3\u5316\u3002\u4f8b\u5982\u5728\u94fe\u5f0f\u5730\u5740\u4e2d\uff0c\u6211\u4eec\u91c7\u53d6\u5728\u94fe\u8868\u6216\u7ea2\u9ed1\u6811\u4e2d\u6267\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u4ecd\u7136\u6709\u9000\u5316\u81f3 \\(O(n)\\) \u65f6\u95f4\u7684\u98ce\u9669\u3002</p> <p>\u591a\u6b21\u54c8\u5e0c\u6709\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u7684\u7f3a\u9677\u5417\uff1f\u5bf9\u4e8e\u6807\u8bb0\u5df2\u5220\u9664\u7684\u7a7a\u95f4\uff0c\u8fd9\u4e2a\u7a7a\u95f4\u8fd8\u80fd\u518d\u6b21\u4f7f\u7528\u5417\uff1f</p> <p>\u591a\u6b21\u54c8\u5e0c\u662f\u5f00\u653e\u5bfb\u5740\u7684\u4e00\u79cd\uff0c\u5f00\u653e\u5bfb\u5740\u6cd5\u90fd\u6709\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u7684\u7f3a\u9677\uff0c\u9700\u8981\u901a\u8fc7\u6807\u8bb0\u5220\u9664\u3002\u88ab\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u7a7a\u95f4\u662f\u53ef\u4ee5\u518d\u6b21\u88ab\u4f7f\u7528\u7684\u3002\u5f53\u5c06\u65b0\u5143\u7d20\u63d2\u5165\u54c8\u5e0c\u8868\uff0c\u5e76\u4e14\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u4e86\u88ab\u6807\u8bb0\u4e3a\u5df2\u5220\u9664\u7684\u4f4d\u7f6e\u65f6\uff0c\u8be5\u4f4d\u7f6e\u53ef\u4ee5\u88ab\u65b0\u7684\u5143\u7d20\u4f7f\u7528\u3002\u8fd9\u6837\u505a\u65e2\u80fd\u4fdd\u6301\u54c8\u5e0c\u8868\u7684\u63a2\u6d4b\u5e8f\u5217\u4e0d\u53d8\uff0c\u53c8\u80fd\u4fdd\u8bc1\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\u4f7f\u7528\u7387\u3002</p> <p>\u4e3a\u4ec0\u4e48\u5728\u7ebf\u6027\u63a2\u6d4b\u4e2d\uff0c\u67e5\u627e\u5143\u7d20\u7684\u65f6\u5019\u4f1a\u51fa\u73b0\u54c8\u5e0c\u51b2\u7a81\u5462\uff1f</p> <p>\u67e5\u627e\u7684\u65f6\u5019\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u627e\u5230\u5bf9\u5e94\u7684\u6876\u548c\u952e\u503c\u5bf9\uff0c\u53d1\u73b0 <code>key</code> \u4e0d\u5339\u914d\uff0c\u8fd9\u5c31\u4ee3\u8868\u6709\u54c8\u5e0c\u51b2\u7a81\u3002\u56e0\u6b64\uff0c\u7ebf\u6027\u63a2\u6d4b\u6cd5\u4f1a\u6839\u636e\u9884\u5148\u8bbe\u5b9a\u7684\u6b65\u957f\u4f9d\u6b21\u5411\u4e0b\u67e5\u627e\uff0c\u76f4\u81f3\u627e\u5230\u6b63\u786e\u7684\u952e\u503c\u5bf9\u6216\u65e0\u6cd5\u627e\u5230\u8df3\u51fa\u4e3a\u6b62\u3002</p> <p>\u4e3a\u4ec0\u4e48\u54c8\u5e0c\u8868\u6269\u5bb9\u80fd\u591f\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\uff1f</p> <p>\u54c8\u5e0c\u51fd\u6570\u7684\u6700\u540e\u4e00\u6b65\u5f80\u5f80\u662f\u5bf9\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u53d6\u4f59\uff0c\u8ba9\u8f93\u51fa\u503c\u843d\u5165\u5728\u6570\u7ec4\u7d22\u5f15\u8303\u56f4\uff1b\u5728\u6269\u5bb9\u540e\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u53d1\u751f\u53d8\u5316\uff0c\u800c <code>key</code> \u5bf9\u5e94\u7684\u7d22\u5f15\u4e5f\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u3002\u539f\u5148\u843d\u5728\u540c\u4e00\u4e2a\u6876\u7684\u591a\u4e2a <code>key</code> \uff0c\u5728\u6269\u5bb9\u540e\u53ef\u80fd\u4f1a\u88ab\u5206\u914d\u5230\u591a\u4e2a\u6876\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u54c8\u5e0c\u51b2\u7a81\u7684\u7f13\u89e3\u3002</p>"},{"location":"chapter_heap/","title":"8. \u00a0 \u5806","text":"<p>Abstract</p> <p>\u5806\u5c31\u50cf\u662f\u5c71\u5ddd\u7684\u5cf0\u5ce6\uff0c\u5b83\u4eec\u5c42\u53e0\u8d77\u4f0f\u3001\u5f62\u6001\u5404\u5f02\u3002</p> <p>\u6bcf\u4e00\u5ea7\u5c71\u5cf0\u90fd\u6709\u5176\u9ad8\u4f4e\u4e4b\u5206\uff0c\u800c\u6700\u9ad8\u7684\u5c71\u5cf0\u603b\u662f\u6700\u5148\u6620\u5165\u773c\u5e18\u3002</p>"},{"location":"chapter_heap/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>8.1 \u00a0 \u5806</li> <li>8.2 \u00a0 \u5efa\u5806\u64cd\u4f5c</li> <li>8.3 \u00a0 Top-K \u95ee\u9898</li> <li>8.4 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_heap/build_heap/","title":"8.2. \u00a0 \u5efa\u5806\u64cd\u4f5c","text":"<p>\u5982\u679c\u6211\u4eec\u60f3\u8981\u6839\u636e\u8f93\u5165\u5217\u8868\u751f\u6210\u4e00\u4e2a\u5806\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u300c\u5efa\u5806\u300d\u3002</p>"},{"location":"chapter_heap/build_heap/#821","title":"8.2.1. \u00a0 \u501f\u52a9\u5165\u5806\u65b9\u6cd5\u5b9e\u73b0","text":"<p>\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u501f\u52a9\u201c\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u201d\u5b9e\u73b0\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5806\uff0c\u7136\u540e\u5c06\u5217\u8868\u5143\u7d20\u4f9d\u6b21\u6dfb\u52a0\u5230\u5806\u4e2d\u3002</p> <p>\u8bbe\u5143\u7d20\u6570\u91cf\u4e3a \\(n\\) \uff0c\u5219\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5165\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u5728\u4f9d\u6b21\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u5806\u7684\u5e73\u5747\u957f\u5ea6\u4e3a \\(\\frac{n}{2}\\) \uff0c\u56e0\u6b64\u8be5\u65b9\u6cd5\u7684\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002</p>"},{"location":"chapter_heap/build_heap/#822","title":"8.2.2. \u00a0 \u57fa\u4e8e\u5806\u5316\u64cd\u4f5c\u5b9e\u73b0","text":"<p>\u6709\u8da3\u7684\u662f\uff0c\u5b58\u5728\u4e00\u79cd\u66f4\u9ad8\u6548\u7684\u5efa\u5806\u65b9\u6cd5\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u4ec5\u4e3a \\(O(n)\\) \u3002\u6211\u4eec\u5148\u5c06\u5217\u8868\u6240\u6709\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u5230\u5806\u4e2d\uff0c\u7136\u540e\u8fed\u4ee3\u5730\u5bf9\u5404\u4e2a\u8282\u70b9\u6267\u884c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u3002\u5f53\u7136\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u5bf9\u53f6\u8282\u70b9\u6267\u884c\u5806\u5316\u64cd\u4f5c\uff0c\u56e0\u4e3a\u5b83\u4eec\u6ca1\u6709\u5b50\u8282\u70b9\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust my_heap.java<pre><code>/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List&lt;Integer&gt; nums) {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nmaxHeap = new ArrayList&lt;&gt;(nums);\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (int i = parent(size() - 1); i &gt;= 0; i--) {\nsiftDown(i);\n}\n}\n</code></pre> my_heap.cpp<pre><code>/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(vector&lt;int&gt; nums) {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nmaxHeap = nums;\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (int i = parent(size() - 1); i &gt;= 0; i--) {\nsiftDown(i);\n}\n}\n</code></pre> my_heap.py<pre><code>def __init__(self, nums: list[int]):\n\"\"\"\u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806\"\"\"\n# \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nself.max_heap = nums\n# \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i in range(self.parent(self.size() - 1), -1, -1):\nself.sift_down(i)\n</code></pre> my_heap.go<pre><code>/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nfunc newMaxHeap(nums []any) *maxHeap {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nh := &amp;maxHeap{data: nums}\nfor i := len(h.data) - 1; i &gt;= 0; i-- {\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nh.siftDown(i)\n}\nreturn h\n}\n</code></pre> my_heap.js<pre><code>/* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums) {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nthis.#maxHeap = nums === undefined ? [] : [...nums];\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (let i = this.#parent(this.size() - 1); i &gt;= 0; i--) {\nthis.#siftDown(i);\n}\n}\n</code></pre> my_heap.ts<pre><code>/* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums?: number[]) {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nthis.maxHeap = nums === undefined ? [] : [...nums];\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (let i = this.parent(this.size() - 1); i &gt;= 0; i--) {\nthis.siftDown(i);\n}\n}\n</code></pre> my_heap.c<pre><code>/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nmaxHeap *newMaxHeap(int nums[], int size) {\n// \u6240\u6709\u5143\u7d20\u5165\u5806\nmaxHeap *h = (maxHeap *)malloc(sizeof(maxHeap));\nh-&gt;size = size;\nmemcpy(h-&gt;data, nums, size * sizeof(int));\nfor (int i = size - 1; i &gt;= 0; i--) {\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nsiftDown(h, i);\n}\nreturn h;\n}\n</code></pre> my_heap.cs<pre><code>/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(IEnumerable&lt;int&gt; nums) {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nmaxHeap = new List&lt;int&gt;(nums);\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nvar size = parent(this.size() - 1);\nfor (int i = size; i &gt;= 0; i--) {\nsiftDown(i);\n}\n}\n</code></pre> my_heap.swift<pre><code>/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\ninit(nums: [Int]) {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nmaxHeap = nums\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i in stride(from: parent(i: size() - 1), through: 0, by: -1) {\nsiftDown(i: i)\n}\n}\n</code></pre> my_heap.zig<pre><code>// \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806\nfn init(self: *Self, allocator: std.mem.Allocator, nums: []const T) !void {\nif (self.max_heap != null) return;\nself.max_heap = std.ArrayList(T).init(allocator);\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\ntry self.max_heap.?.appendSlice(nums);\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nvar i: usize = parent(self.size() - 1) + 1;\nwhile (i &gt; 0) : (i -= 1) {\ntry self.siftDown(i - 1);\n}\n}\n</code></pre> my_heap.dart<pre><code>/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List&lt;int&gt; nums) {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n_maxHeap = nums;\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (int i = _parent(size() - 1); i &gt;= 0; i--) {\nsiftDown(i);\n}\n}\n</code></pre> my_heap.rs<pre><code>/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nfn new(nums: Vec&lt;i32&gt;) -&gt; Self {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nlet mut heap = MaxHeap { max_heap: nums };\n// \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i in (0..=Self::parent(heap.size() - 1)).rev() {\nheap.sift_down(i);\n}\nheap\n}\n</code></pre>"},{"location":"chapter_heap/build_heap/#823","title":"8.2.3. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"<p>\u4e3a\u4ec0\u4e48\u7b2c\u4e8c\u79cd\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(n)\\) \uff1f\u6211\u4eec\u6765\u5c55\u5f00\u63a8\u7b97\u4e00\u4e0b\u3002</p> <ul> <li>\u5b8c\u5168\u4e8c\u53c9\u6811\u4e2d\uff0c\u8bbe\u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \uff0c\u5219\u53f6\u8282\u70b9\u6570\u91cf\u4e3a \\((n + 1) / 2\\) \uff0c\u5176\u4e2d \\(/\\) \u4e3a\u5411\u4e0b\u6574\u9664\u3002\u56e0\u6b64\uff0c\u5728\u6392\u9664\u53f6\u8282\u70b9\u540e\uff0c\u9700\u8981\u5806\u5316\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\((n - 1)/2\\) \uff0c\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002</li> <li>\u5728\u4ece\u9876\u81f3\u5e95\u5806\u5316\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e2a\u8282\u70b9\u6700\u591a\u5806\u5316\u5230\u53f6\u8282\u70b9\uff0c\u56e0\u6b64\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u4e3a\u4e8c\u53c9\u6811\u9ad8\u5ea6 \\(O(\\log n)\\) \u3002</li> </ul> <p>\u5c06\u4e0a\u8ff0\u4e24\u8005\u76f8\u4e58\uff0c\u53ef\u5f97\u5230\u5efa\u5806\u8fc7\u7a0b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u7136\u800c\uff0c\u8fd9\u4e2a\u4f30\u7b97\u7ed3\u679c\u5e76\u4e0d\u51c6\u786e\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u8003\u8651\u5230\u4e8c\u53c9\u6811\u5e95\u5c42\u8282\u70b9\u6570\u91cf\u8fdc\u591a\u4e8e\u9876\u5c42\u8282\u70b9\u7684\u7279\u6027\u3002</p> <p>\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u8fdb\u884c\u66f4\u4e3a\u8be6\u7ec6\u7684\u8ba1\u7b97\u3002\u4e3a\u4e86\u51cf\u5c0f\u8ba1\u7b97\u96be\u5ea6\uff0c\u6211\u4eec\u5047\u8bbe\u6811\u662f\u4e00\u4e2a\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff0c\u8be5\u5047\u8bbe\u4e0d\u4f1a\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002\u8bbe\u4e8c\u53c9\u6811\uff08\u5373\u5806\uff09\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u6811\u9ad8\u5ea6\u4e3a \\(h\\) \u3002\u4e0a\u6587\u63d0\u5230\uff0c\u8282\u70b9\u5806\u5316\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u7b49\u4e8e\u8be5\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u800c\u8be5\u8ddd\u79bb\u6b63\u662f\u201c\u8282\u70b9\u9ad8\u5ea6\u201d\u3002</p> <p></p> <p> \u56fe\uff1a\u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u5404\u5c42\u8282\u70b9\u6570\u91cf </p> <p>\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5404\u5c42\u7684\u201c\u8282\u70b9\u6570\u91cf \\(\\times\\) \u8282\u70b9\u9ad8\u5ea6\u201d\u6c42\u548c\uff0c\u4ece\u800c\u5f97\u5230\u6240\u6709\u8282\u70b9\u7684\u5806\u5316\u8fed\u4ee3\u6b21\u6570\u7684\u603b\u548c\u3002</p> \\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\cdots + 2^{(h-1)}\\times1 \\] <p>\u5316\u7b80\u4e0a\u5f0f\u9700\u8981\u501f\u52a9\u4e2d\u5b66\u7684\u6570\u5217\u77e5\u8bc6\uff0c\u5148\u5bf9 \\(T(h)\\) \u4e58\u4ee5 \\(2\\) \uff0c\u5f97\u5230</p> \\[ \\begin{aligned} T(h) &amp; = 2^0h + 2^1(h-1) + 2^2(h-2) + \\cdots + 2^{h-1}\\times1 \\newline 2 T(h) &amp; = 2^1h + 2^2(h-1) + 2^3(h-2) + \\cdots + 2^{h}\\times1 \\newline \\end{aligned} \\] <p>\u4f7f\u7528\u9519\u4f4d\u76f8\u51cf\u6cd5\uff0c\u4ee4\u4e0b\u5f0f \\(2 T(h)\\) \u51cf\u53bb\u4e0a\u5f0f \\(T(h)\\) \uff0c\u53ef\u5f97</p> \\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\cdots + 2^{h-1} + 2^h \\] <p>\u89c2\u5bdf\u4e0a\u5f0f\uff0c\u53d1\u73b0 \\(T(h)\\) \u662f\u4e00\u4e2a\u7b49\u6bd4\u6570\u5217\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528\u6c42\u548c\u516c\u5f0f\uff0c\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a</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>\u8fdb\u4e00\u6b65\u5730\uff0c\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n = 2^{h+1} - 1\\) \uff0c\u6613\u5f97\u590d\u6742\u5ea6\u4e3a \\(O(2^h) = O(n)\\) \u3002\u4ee5\u4e0a\u63a8\u7b97\u8868\u660e\uff0c\u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002</p>"},{"location":"chapter_heap/heap/","title":"8.1. \u00a0 \u5806","text":"<p>\u300c\u5806 Heap\u300d\u662f\u4e00\u79cd\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u53ef\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff1a</p> <ul> <li>\u300c\u5927\u9876\u5806 Max Heap\u300d\uff0c\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\geq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\u3002</li> <li>\u300c\u5c0f\u9876\u5806 Min Heap\u300d\uff0c\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\leq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\u3002</li> </ul> <p></p> <p> \u56fe\uff1a\u5c0f\u9876\u5806\u4e0e\u5927\u9876\u5806 </p> <p>\u5806\u4f5c\u4e3a\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u4e00\u4e2a\u7279\u4f8b\uff0c\u5177\u6709\u4ee5\u4e0b\u7279\u6027\uff1a</p> <ul> <li>\u6700\u5e95\u5c42\u8282\u70b9\u9760\u5de6\u586b\u5145\uff0c\u5176\u4ed6\u5c42\u7684\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u3002</li> <li>\u6211\u4eec\u5c06\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\u79f0\u4e3a\u300c\u5806\u9876\u300d\uff0c\u5c06\u5e95\u5c42\u6700\u9760\u53f3\u7684\u8282\u70b9\u79f0\u4e3a\u300c\u5806\u5e95\u300d\u3002</li> <li>\u5bf9\u4e8e\u5927\u9876\u5806\uff08\u5c0f\u9876\u5806\uff09\uff0c\u5806\u9876\u5143\u7d20\uff08\u5373\u6839\u8282\u70b9\uff09\u7684\u503c\u5206\u522b\u662f\u6700\u5927\uff08\u6700\u5c0f\uff09\u7684\u3002</li> </ul>"},{"location":"chapter_heap/heap/#811","title":"8.1.1. \u00a0 \u5806\u5e38\u7528\u64cd\u4f5c","text":"<p>\u9700\u8981\u6307\u51fa\u7684\u662f\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684\u662f\u300c\u4f18\u5148\u961f\u5217 Priority Queue\u300d\uff0c\u8fd9\u662f\u4e00\u79cd\u62bd\u8c61\u6570\u636e\u7ed3\u6784\uff0c\u5b9a\u4e49\u4e3a\u5177\u6709\u4f18\u5148\u7ea7\u6392\u5e8f\u7684\u961f\u5217\u3002</p> <p>\u5b9e\u9645\u4e0a\uff0c\u5806\u901a\u5e38\u7528\u4f5c\u5b9e\u73b0\u4f18\u5148\u961f\u5217\uff0c\u5927\u9876\u5806\u76f8\u5f53\u4e8e\u5143\u7d20\u6309\u4ece\u5927\u5230\u5c0f\u987a\u5e8f\u51fa\u961f\u7684\u4f18\u5148\u961f\u5217\u3002\u4ece\u4f7f\u7528\u89d2\u5ea6\u6765\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u300c\u4f18\u5148\u961f\u5217\u300d\u548c\u300c\u5806\u300d\u770b\u4f5c\u7b49\u4ef7\u7684\u6570\u636e\u7ed3\u6784\u3002\u56e0\u6b64\uff0c\u672c\u4e66\u5bf9\u4e24\u8005\u4e0d\u505a\u7279\u522b\u533a\u5206\uff0c\u7edf\u4e00\u4f7f\u7528\u300c\u5806\u300d\u6765\u547d\u540d\u3002</p> <p>\u5806\u7684\u5e38\u7528\u64cd\u4f5c\u89c1\u4e0b\u8868\uff0c\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002</p> <p> \u8868\uff1a\u5806\u7684\u64cd\u4f5c\u6548\u7387 </p> \u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u5806 \\(O(\\log n)\\) pop() \u5806\u9876\u5143\u7d20\u51fa\u5806 \\(O(\\log n)\\) peek() \u8bbf\u95ee\u5806\u9876\u5143\u7d20\uff08\u5927 / \u5c0f\u9876\u5806\u5206\u522b\u4e3a\u6700\u5927 / \u5c0f\u503c\uff09 \\(O(1)\\) size() \u83b7\u53d6\u5806\u7684\u5143\u7d20\u6570\u91cf \\(O(1)\\) isEmpty() \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a \\(O(1)\\) <p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684\u5806\u7c7b\uff08\u6216\u4f18\u5148\u961f\u5217\u7c7b\uff09\u3002</p> <p>Tip</p> <p>\u7c7b\u4f3c\u4e8e\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u201c\u4ece\u5c0f\u5230\u5927\u6392\u5217\u201d\u548c\u201c\u4ece\u5927\u5230\u5c0f\u6392\u5217\u201d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539 Comparator \u6765\u5b9e\u73b0\u201c\u5c0f\u9876\u5806\u201d\u4e0e\u201c\u5927\u9876\u5806\u201d\u4e4b\u95f4\u7684\u8f6c\u6362\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust heap.java<pre><code>/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nQueue&lt;Integer&gt; minHeap = new PriorityQueue&lt;&gt;();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nQueue&lt;Integer&gt; maxHeap = new PriorityQueue&lt;&gt;((a, b) -&gt; b - a);\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.peek(); // 5\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = heap.poll(); // 5\npeek = heap.poll(); // 4\npeek = heap.poll(); // 3\npeek = heap.poll(); // 2\npeek = heap.poll(); // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = maxHeap.isEmpty();\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue&lt;&gt;(Arrays.asList(1, 3, 2, 5, 4));\n</code></pre> heap.cpp<pre><code>/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\npriority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; minHeap;\n// \u521d\u59cb\u5316\u5927\u9876\u5806\npriority_queue&lt;int, vector&lt;int&gt;, less&lt;int&gt;&gt; maxHeap;\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.top(); // 5\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nmaxHeap.pop(); // 5\nmaxHeap.pop(); // 4\nmaxHeap.pop(); // 3\nmaxHeap.pop(); // 2\nmaxHeap.pop(); // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.empty();\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nvector&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.py<pre><code># \u521d\u59cb\u5316\u5c0f\u9876\u5806\nmin_heap, flag = [], 1\n# \u521d\u59cb\u5316\u5927\u9876\u5806\nmax_heap, flag = [], -1\n# Python \u7684 heapq \u6a21\u5757\u9ed8\u8ba4\u5b9e\u73b0\u5c0f\u9876\u5806\n# \u8003\u8651\u5c06\u201c\u5143\u7d20\u53d6\u8d1f\u201d\u540e\u518d\u5165\u5806\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u5927\u5c0f\u5173\u7cfb\u98a0\u5012\uff0c\u4ece\u800c\u5b9e\u73b0\u5927\u9876\u5806\n# \u5728\u672c\u793a\u4f8b\u4e2d\uff0cflag = 1 \u65f6\u5bf9\u5e94\u5c0f\u9876\u5806\uff0cflag = -1 \u65f6\u5bf9\u5e94\u5927\u9876\u5806\n# \u5143\u7d20\u5165\u5806\nheapq.heappush(max_heap, flag * 1)\nheapq.heappush(max_heap, flag * 3)\nheapq.heappush(max_heap, flag * 2)\nheapq.heappush(max_heap, flag * 5)\nheapq.heappush(max_heap, flag * 4)\n# \u83b7\u53d6\u5806\u9876\u5143\u7d20\npeek: int = flag * max_heap[0] # 5\n# \u5806\u9876\u5143\u7d20\u51fa\u5806\n# \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nval = flag * heapq.heappop(max_heap) # 5\nval = flag * heapq.heappop(max_heap) # 4\nval = flag * heapq.heappop(max_heap) # 3\nval = flag * heapq.heappop(max_heap) # 2\nval = flag * heapq.heappop(max_heap) # 1\n# \u83b7\u53d6\u5806\u5927\u5c0f\nsize: int = len(max_heap)\n# \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = not max_heap\n# \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\nmin_heap: list[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\n</code></pre> heap.go<pre><code>// Go \u8bed\u8a00\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 heap.Interface \u6765\u6784\u5efa\u6574\u6570\u5927\u9876\u5806\n// \u5b9e\u73b0 heap.Interface \u9700\u8981\u540c\u65f6\u5b9e\u73b0 sort.Interface\ntype intHeap []any\n// Push heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u63a8\u5165\u5143\u7d20\u5230\u5806\nfunc (h *intHeap) Push(x any) {\n// Push \u548c Pop \u4f7f\u7528 pointer receiver \u4f5c\u4e3a\u53c2\u6570\n// \u56e0\u4e3a\u5b83\u4eec\u4e0d\u4ec5\u4f1a\u5bf9\u5207\u7247\u7684\u5185\u5bb9\u8fdb\u884c\u8c03\u6574\uff0c\u8fd8\u4f1a\u4fee\u6539\u5207\u7247\u7684\u957f\u5ea6\u3002\n*h = append(*h, x.(int))\n}\n// Pop heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u5f39\u51fa\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Pop() any {\n// \u5f85\u51fa\u5806\u5143\u7d20\u5b58\u653e\u5728\u6700\u540e\nlast := (*h)[len(*h)-1]\n*h = (*h)[:len(*h)-1]\nreturn last\n}\n// Len sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Len() int {\nreturn len(*h)\n}\n// Less sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Less(i, j int) bool {\n// \u5982\u679c\u5b9e\u73b0\u5c0f\u9876\u5806\uff0c\u5219\u9700\u8981\u8c03\u6574\u4e3a\u5c0f\u4e8e\u53f7\nreturn (*h)[i].(int) &gt; (*h)[j].(int)\n}\n// Swap sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Swap(i, j int) {\n(*h)[i], (*h)[j] = (*h)[j], (*h)[i]\n}\n// Top \u83b7\u53d6\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Top() any {\nreturn (*h)[0]\n}\n/* Driver Code */\nfunc TestHeap(t *testing.T) {\n/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5927\u9876\u5806\nmaxHeap := &amp;intHeap{}\nheap.Init(maxHeap)\n/* \u5143\u7d20\u5165\u5806 */\n// \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u6dfb\u52a0\u5143\u7d20\nheap.Push(maxHeap, 1)\nheap.Push(maxHeap, 3)\nheap.Push(maxHeap, 2)\nheap.Push(maxHeap, 4)\nheap.Push(maxHeap, 5)\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\ntop := maxHeap.Top()\nfmt.Printf(\"\u5806\u9876\u5143\u7d20\u4e3a %d\\n\", top)\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u79fb\u9664\u5143\u7d20\nheap.Pop(maxHeap) // 5\nheap.Pop(maxHeap) // 4\nheap.Pop(maxHeap) // 3\nheap.Pop(maxHeap) // 2\nheap.Pop(maxHeap) // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nsize := len(*maxHeap)\nfmt.Printf(\"\u5806\u5143\u7d20\u6570\u91cf\u4e3a %d\\n\", size)\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nisEmpty := len(*maxHeap) == 0\nfmt.Printf(\"\u5806\u662f\u5426\u4e3a\u7a7a %t\\n\", isEmpty)\n}\n</code></pre> heap.js<pre><code>// JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n</code></pre> heap.ts<pre><code>// TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n</code></pre> heap.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n</code></pre> heap.cs<pre><code>/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nPriorityQueue&lt;int, int&gt; minHeap = new PriorityQueue&lt;int, int&gt;();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nPriorityQueue&lt;int, int&gt; maxHeap = new PriorityQueue&lt;int, int&gt;(Comparer&lt;int&gt;.Create((x, y) =&gt; y - x));\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.Enqueue(1, 1);\nmaxHeap.Enqueue(3, 3);\nmaxHeap.Enqueue(2, 2);\nmaxHeap.Enqueue(5, 5);\nmaxHeap.Enqueue(4, 4);\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.Peek();//5\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.Dequeue(); // 5\npeek = maxHeap.Dequeue(); // 4\npeek = maxHeap.Dequeue(); // 3\npeek = maxHeap.Dequeue(); // 2\npeek = maxHeap.Dequeue(); // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.Count;\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.Count == 0;\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue&lt;int, int&gt;(new List&lt;(int, int)&gt; { (1, 1), (3, 3), (2, 2), (5, 5), (4, 4), });\n</code></pre> heap.swift<pre><code>// Swift \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n</code></pre> heap.zig<pre><code>\n</code></pre> heap.dart<pre><code>// Dart \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n</code></pre> heap.rs<pre><code>\n</code></pre>"},{"location":"chapter_heap/heap/#812","title":"8.1.2. \u00a0 \u5806\u7684\u5b9e\u73b0","text":"<p>\u4e0b\u6587\u5b9e\u73b0\u7684\u662f\u5927\u9876\u5806\u3002\u82e5\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5c0f\u9876\u5806\uff0c\u53ea\u9700\u5c06\u6240\u6709\u5927\u5c0f\u903b\u8f91\u5224\u65ad\u53d6\u9006\uff08\u4f8b\u5982\uff0c\u5c06 \\(\\geq\\) \u66ff\u6362\u4e3a \\(\\leq\\) \uff09\u3002\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002</p>"},{"location":"chapter_heap/heap/#_1","title":"\u5806\u7684\u5b58\u50a8\u4e0e\u8868\u793a","text":"<p>\u6211\u4eec\u5728\u4e8c\u53c9\u6811\u7ae0\u8282\u4e2d\u5b66\u4e60\u5230\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u7528\u6570\u7ec4\u6765\u8868\u793a\u3002\u7531\u4e8e\u5806\u6b63\u662f\u4e00\u79cd\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u6211\u4eec\u5c06\u91c7\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u5806\u3002</p> <p>\u5f53\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u4e8c\u53c9\u6811\u65f6\uff0c\u5143\u7d20\u4ee3\u8868\u8282\u70b9\u503c\uff0c\u7d22\u5f15\u4ee3\u8868\u8282\u70b9\u5728\u4e8c\u53c9\u6811\u4e2d\u7684\u4f4d\u7f6e\u3002\u8282\u70b9\u6307\u9488\u901a\u8fc7\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u6765\u5b9e\u73b0\u3002</p> <p>\u5177\u4f53\u800c\u8a00\uff0c\u7ed9\u5b9a\u7d22\u5f15 \\(i\\) \uff0c\u5176\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \uff0c\u7236\u8282\u70b9\u7d22\u5f15\u4e3a \\((i - 1) / 2\\)\uff08\u5411\u4e0b\u53d6\u6574\uff09\u3002\u5f53\u7d22\u5f15\u8d8a\u754c\u65f6\uff0c\u8868\u793a\u7a7a\u8282\u70b9\u6216\u8282\u70b9\u4e0d\u5b58\u5728\u3002</p> <p></p> <p> \u56fe\uff1a\u5806\u7684\u8868\u793a\u4e0e\u5b58\u50a8 </p> <p>\u6211\u4eec\u53ef\u4ee5\u5c06\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust my_heap.java<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(int i) {\nreturn (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.cpp<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(int i) {\nreturn (i - 1) / 2; // \u5411\u4e0b\u53d6\u6574\n}\n</code></pre> my_heap.py<pre><code>def left(self, i: int) -&gt; int:\n\"\"\"\u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\"\"\"\nreturn 2 * i + 1\ndef right(self, i: int) -&gt; int:\n\"\"\"\u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\"\"\"\nreturn 2 * i + 2\ndef parent(self, i: int) -&gt; int:\n\"\"\"\u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15\"\"\"\nreturn (i - 1) // 2 # \u5411\u4e0b\u6574\u9664\n</code></pre> my_heap.go<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) left(i int) int {\nreturn 2*i + 1\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) right(i int) int {\nreturn 2*i + 2\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) parent(i int) int {\n// \u5411\u4e0b\u6574\u9664\nreturn (i - 1) / 2\n}\n</code></pre> my_heap.js<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\n#left(i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\n#right(i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\n#parent(i) {\nreturn Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.ts<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nleft(i: number): number {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nright(i: number): number {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nparent(i: number): number {\nreturn Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.c<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(maxHeap *h, int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(maxHeap *h, int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(maxHeap *h, int i) {\nreturn (i - 1) / 2;\n}\n</code></pre> my_heap.cs<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint parent(int i) {\nreturn (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.swift<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc left(i: Int) -&gt; Int {\n2 * i + 1\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfunc right(i: Int) -&gt; Int {\n2 * i + 2\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nfunc parent(i: Int) -&gt; Int {\n(i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.zig<pre><code>// \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\nfn left(i: usize) usize {\nreturn 2 * i + 1;\n}\n// \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\nfn right(i: usize) usize {\nreturn 2 * i + 2;\n}\n// \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15\nfn parent(i: usize) usize {\n// return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\nreturn @divFloor(i - 1, 2);\n}\n</code></pre> my_heap.dart<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nint _left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nint _right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nint _parent(int i) {\nreturn (i - 1) ~/ 2; // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.rs<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7d22\u5f15 */\nfn left(i: usize) -&gt; usize {\n2 * i + 1\n}\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7d22\u5f15 */\nfn right(i: usize) -&gt; usize {\n2 * i + 2\n}\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7d22\u5f15 */\nfn parent(i: usize) -&gt; usize {\n(i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n</code></pre>"},{"location":"chapter_heap/heap/#_2","title":"\u8bbf\u95ee\u5806\u9876\u5143\u7d20","text":"<p>\u5806\u9876\u5143\u7d20\u5373\u4e3a\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u4e5f\u5c31\u662f\u5217\u8868\u7684\u9996\u4e2a\u5143\u7d20\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust my_heap.java<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap.get(0);\n}\n</code></pre> my_heap.cpp<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap[0];\n}\n</code></pre> my_heap.py<pre><code>def peek(self) -&gt; int:\n\"\"\"\u8bbf\u95ee\u5806\u9876\u5143\u7d20\"\"\"\nreturn self.max_heap[0]\n</code></pre> my_heap.go<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc (h *maxHeap) peek() any {\nreturn h.data[0]\n}\n</code></pre> my_heap.js<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek() {\nreturn this.#maxHeap[0];\n}\n</code></pre> my_heap.ts<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek(): number {\nreturn this.maxHeap[0];\n}\n</code></pre> my_heap.c<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek(maxHeap *h) {\nreturn h-&gt;data[0];\n}\n</code></pre> my_heap.cs<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap[0];\n}\n</code></pre> my_heap.swift<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc peek() -&gt; Int {\nmaxHeap[0]\n}\n</code></pre> my_heap.zig<pre><code>// \u8bbf\u95ee\u5806\u9876\u5143\u7d20\nfn peek(self: *Self) T {\nreturn self.max_heap.?.items[0];\n} </code></pre> my_heap.dart<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn _maxHeap[0];\n}\n</code></pre> my_heap.rs<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfn peek(&amp;self) -&gt; Option&lt;i32&gt; {\nself.max_heap.first().copied()\n}\n</code></pre>"},{"location":"chapter_heap/heap/#_3","title":"\u5143\u7d20\u5165\u5806","text":"<p>\u7ed9\u5b9a\u5143\u7d20 <code>val</code> \uff0c\u6211\u4eec\u9996\u5148\u5c06\u5176\u6dfb\u52a0\u5230\u5806\u5e95\u3002\u6dfb\u52a0\u4e4b\u540e\uff0c\u7531\u4e8e val \u53ef\u80fd\u5927\u4e8e\u5806\u4e2d\u5176\u4ed6\u5143\u7d20\uff0c\u5806\u7684\u6210\u7acb\u6761\u4ef6\u53ef\u80fd\u5df2\u88ab\u7834\u574f\u3002\u56e0\u6b64\uff0c\u9700\u8981\u4fee\u590d\u4ece\u63d2\u5165\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u7684\u5404\u4e2a\u8282\u70b9\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u88ab\u79f0\u4e3a\u300c\u5806\u5316 Heapify\u300d\u3002</p> <p>\u8003\u8651\u4ece\u5165\u5806\u8282\u70b9\u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u6267\u884c\u5806\u5316\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u6bd4\u8f83\u63d2\u5165\u8282\u70b9\u4e0e\u5176\u7236\u8282\u70b9\u7684\u503c\uff0c\u5982\u679c\u63d2\u5165\u8282\u70b9\u66f4\u5927\uff0c\u5219\u5c06\u5b83\u4eec\u4ea4\u6362\u3002\u7136\u540e\u7ee7\u7eed\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u4ece\u5e95\u81f3\u9876\u4fee\u590d\u5806\u4e2d\u7684\u5404\u4e2a\u8282\u70b9\uff0c\u76f4\u81f3\u8d8a\u8fc7\u6839\u8282\u70b9\u6216\u9047\u5230\u65e0\u9700\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002</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> \u56fe\uff1a\u5143\u7d20\u5165\u5806\u6b65\u9aa4 </p> <p>\u8bbe\u8282\u70b9\u603b\u6570\u4e3a \\(n\\) \uff0c\u5219\u6811\u7684\u9ad8\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u7531\u6b64\u53ef\u77e5\uff0c\u5806\u5316\u64cd\u4f5c\u7684\u5faa\u73af\u8f6e\u6570\u6700\u591a\u4e3a \\(O(\\log n)\\) \uff0c\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust my_heap.java<pre><code>/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n// \u6dfb\u52a0\u8282\u70b9\nmaxHeap.add(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nint p = parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p &lt; 0 || maxHeap.get(i) &lt;= maxHeap.get(p))\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre> my_heap.cpp<pre><code>/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n// \u6dfb\u52a0\u8282\u70b9\nmaxHeap.push_back(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nint p = parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p &lt; 0 || maxHeap[i] &lt;= maxHeap[p])\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(maxHeap[i], maxHeap[p]);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre> my_heap.py<pre><code>def push(self, val: int):\n\"\"\"\u5143\u7d20\u5165\u5806\"\"\"\n# \u6dfb\u52a0\u8282\u70b9\nself.max_heap.append(val)\n# \u4ece\u5e95\u81f3\u9876\u5806\u5316\nself.sift_up(self.size() - 1)\ndef sift_up(self, i: int):\n\"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\"\"\"\nwhile True:\n# \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\np = self.parent(i)\n# \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif p &lt; 0 or self.max_heap[i] &lt;= self.max_heap[p]:\nbreak\n# \u4ea4\u6362\u4e24\u8282\u70b9\nself.swap(i, p)\n# \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p\n</code></pre> my_heap.go<pre><code>/* \u5143\u7d20\u5165\u5806 */\nfunc (h *maxHeap) push(val any) {\n// \u6dfb\u52a0\u8282\u70b9\nh.data = append(h.data, val)\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nh.siftUp(len(h.data) - 1)\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc (h *maxHeap) siftUp(i int) {\nfor true {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\np := h.parent(i)\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif p &lt; 0 || h.data[i].(int) &lt;= h.data[p].(int) {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nh.swap(i, p)\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p\n}\n}\n</code></pre> my_heap.js<pre><code>/* \u5143\u7d20\u5165\u5806 */\npush(val) {\n// \u6dfb\u52a0\u8282\u70b9\nthis.#maxHeap.push(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nthis.#siftUp(this.size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\n#siftUp(i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nconst p = this.#parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p &lt; 0 || this.#maxHeap[i] &lt;= this.#maxHeap[p]) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nthis.#swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre> my_heap.ts<pre><code>/* \u5143\u7d20\u5165\u5806 */\npush(val: number): void {\n// \u6dfb\u52a0\u8282\u70b9\nthis.maxHeap.push(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nthis.siftUp(this.size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nsiftUp(i: number): void {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nconst p = this.parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p &lt; 0 || this.maxHeap[i] &lt;= this.maxHeap[p]) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nthis.swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre> my_heap.c<pre><code>/* \u5143\u7d20\u5165\u5806 */\nvoid push(maxHeap *h, int val) {\n// \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e0d\u5e94\u8be5\u6dfb\u52a0\u8fd9\u4e48\u591a\u8282\u70b9\nif (h-&gt;size == MAX_SIZE) {\nprintf(\"heap is full!\");\nreturn;\n}\n// \u6dfb\u52a0\u8282\u70b9\nh-&gt;data[h-&gt;size] = val;\nh-&gt;size++;\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(h, h-&gt;size - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(maxHeap *h, int i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nint p = parent(h, i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p &lt; 0 || h-&gt;data[i] &lt;= h-&gt;data[p]) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(h, i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre> my_heap.cs<pre><code>/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n// \u6dfb\u52a0\u8282\u70b9\nmaxHeap.Add(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nint p = parent(i);\n// \u82e5\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\nif (p &lt; 0 || maxHeap[i] &lt;= maxHeap[p])\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre> my_heap.swift<pre><code>/* \u5143\u7d20\u5165\u5806 */\nfunc push(val: Int) {\n// \u6dfb\u52a0\u8282\u70b9\nmaxHeap.append(val)\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(i: size() - 1)\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc siftUp(i: Int) {\nvar i = i\nwhile true {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nlet p = parent(i: i)\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif p &lt; 0 || maxHeap[i] &lt;= maxHeap[p] {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i: i, j: p)\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p\n}\n}\n</code></pre> my_heap.zig<pre><code>// \u5143\u7d20\u5165\u5806\nfn push(self: *Self, val: T) !void {\n// \u6dfb\u52a0\u8282\u70b9\ntry self.max_heap.?.append(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\ntry self.siftUp(self.size() - 1);\n} // \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\nfn siftUp(self: *Self, i_: usize) !void {\nvar i = i_;\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nvar p = parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p &lt; 0 or self.max_heap.?.items[i] &lt;= self.max_heap.?.items[p]) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\ntry self.swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre> my_heap.dart<pre><code>/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n// \u6dfb\u52a0\u8282\u70b9\n_maxHeap.add(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\nwhile (true) {\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nint p = _parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p &lt; 0 || _maxHeap[i] &lt;= _maxHeap[p]) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\n_swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre> my_heap.rs<pre><code>/* \u5143\u7d20\u5165\u5806 */\nfn push(&amp;mut self, val: i32) {\n// \u6dfb\u52a0\u8282\u70b9\nself.max_heap.push(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nself.sift_up(self.size() - 1);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfn sift_up(&amp;mut self, mut i: usize) {\nloop {\n// \u8282\u70b9 i \u5df2\u7ecf\u662f\u5806\u9876\u8282\u70b9\u4e86\uff0c\u7ed3\u675f\u5806\u5316\nif i == 0 {\nbreak;\n}\n// \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\nlet p = Self::parent(i);\n// \u5f53\u201c\u8282\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif self.max_heap[i] &lt;= self.max_heap[p] {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nself.swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre>"},{"location":"chapter_heap/heap/#_4","title":"\u5806\u9876\u5143\u7d20\u51fa\u5806","text":"<p>\u5806\u9876\u5143\u7d20\u662f\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9\uff0c\u5373\u5217\u8868\u9996\u5143\u7d20\u3002\u5982\u679c\u6211\u4eec\u76f4\u63a5\u4ece\u5217\u8868\u4e2d\u5220\u9664\u9996\u5143\u7d20\uff0c\u90a3\u4e48\u4e8c\u53c9\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u7d22\u5f15\u90fd\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u8fd9\u5c06\u4f7f\u5f97\u540e\u7eed\u4f7f\u7528\u5806\u5316\u4fee\u590d\u53d8\u5f97\u56f0\u96be\u3002\u4e3a\u4e86\u5c3d\u91cf\u51cf\u5c11\u5143\u7d20\u7d22\u5f15\u7684\u53d8\u52a8\uff0c\u6211\u4eec\u91c7\u53d6\u4ee5\u4e0b\u64cd\u4f5c\u6b65\u9aa4\uff1a</p> <ol> <li>\u4ea4\u6362\u5806\u9876\u5143\u7d20\u4e0e\u5806\u5e95\u5143\u7d20\uff08\u5373\u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff09\u3002</li> <li>\u4ea4\u6362\u5b8c\u6210\u540e\uff0c\u5c06\u5806\u5e95\u4ece\u5217\u8868\u4e2d\u5220\u9664\uff08\u6ce8\u610f\uff0c\u7531\u4e8e\u5df2\u7ecf\u4ea4\u6362\uff0c\u5b9e\u9645\u4e0a\u5220\u9664\u7684\u662f\u539f\u6765\u7684\u5806\u9876\u5143\u7d20\uff09\u3002</li> <li>\u4ece\u6839\u8282\u70b9\u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u6267\u884c\u5806\u5316\u3002</li> </ol> <p>\u987e\u540d\u601d\u4e49\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u7684\u64cd\u4f5c\u65b9\u5411\u4e0e\u4ece\u5e95\u81f3\u9876\u5806\u5316\u76f8\u53cd\uff0c\u6211\u4eec\u5c06\u6839\u8282\u70b9\u7684\u503c\u4e0e\u5176\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u5c06\u6700\u5927\u7684\u5b50\u8282\u70b9\u4e0e\u6839\u8282\u70b9\u4ea4\u6362\uff1b\u7136\u540e\u5faa\u73af\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\u6216\u9047\u5230\u65e0\u9700\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002</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> \u56fe\uff1a\u5806\u9876\u5143\u7d20\u51fa\u5806\u6b65\u9aa4 </p> <p>\u4e0e\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u76f8\u4f3c\uff0c\u5806\u9876\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4e3a \\(O(\\log n)\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust my_heap.java<pre><code>/* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(0, size() - 1);\n// \u5220\u9664\u8282\u70b9\nint val = maxHeap.remove(size() - 1);\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = left(i), r = right(i), ma = i;\nif (l &lt; size() &amp;&amp; maxHeap.get(l) &gt; maxHeap.get(ma))\nma = l;\nif (r &lt; size() &amp;&amp; maxHeap.get(r) &gt; maxHeap.get(ma))\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i)\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n</code></pre> my_heap.cpp<pre><code>/* \u5143\u7d20\u51fa\u5806 */\nvoid pop() {\n// \u5224\u7a7a\u5904\u7406\nif (empty()) {\nthrow out_of_range(\"\u5806\u4e3a\u7a7a\");\n}\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(maxHeap[0], maxHeap[size() - 1]);\n// \u5220\u9664\u8282\u70b9\nmaxHeap.pop_back();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(0);\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = left(i), r = right(i), ma = i;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (l &lt; size() &amp;&amp; maxHeap[l] &gt; maxHeap[ma])\nma = l;\nif (r &lt; size() &amp;&amp; maxHeap[r] &gt; maxHeap[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i)\nbreak;\nswap(maxHeap[i], maxHeap[ma]);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n</code></pre> my_heap.py<pre><code>def pop(self) -&gt; int:\n\"\"\"\u5143\u7d20\u51fa\u5806\"\"\"\n# \u5224\u7a7a\u5904\u7406\nif self.is_empty():\nraise IndexError(\"\u5806\u4e3a\u7a7a\")\n# \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nself.swap(0, self.size() - 1)\n# \u5220\u9664\u8282\u70b9\nval = self.max_heap.pop()\n# \u4ece\u9876\u81f3\u5e95\u5806\u5316\nself.sift_down(0)\n# \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val\ndef sift_down(self, i: int):\n\"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\"\"\"\nwhile True:\n# \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nl, r, ma = self.left(i), self.right(i), i\nif l &lt; self.size() and self.max_heap[l] &gt; self.max_heap[ma]:\nma = l\nif r &lt; self.size() and self.max_heap[r] &gt; self.max_heap[ma]:\nma = r\n# \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i:\nbreak\n# \u4ea4\u6362\u4e24\u8282\u70b9\nself.swap(i, ma)\n# \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\n</code></pre> my_heap.go<pre><code>/* \u5143\u7d20\u51fa\u5806 */\nfunc (h *maxHeap) pop() any {\n// \u5224\u7a7a\u5904\u7406\nif h.isEmpty() {\nfmt.Println(\"error\")\nreturn nil\n}\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nh.swap(0, h.size()-1)\n// \u5220\u9664\u8282\u70b9\nval := h.data[len(h.data)-1]\nh.data = h.data[:len(h.data)-1]\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nh.siftDown(0)\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc (h *maxHeap) siftDown(i int) {\nfor true {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\nl, r, max := h.left(i), h.right(i), i\nif l &lt; h.size() &amp;&amp; h.data[l].(int) &gt; h.data[max].(int) {\nmax = l\n}\nif r &lt; h.size() &amp;&amp; h.data[r].(int) &gt; h.data[max].(int) {\nmax = r\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif max == i {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nh.swap(i, max)\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = max\n}\n}\n</code></pre> my_heap.js<pre><code>/* \u5143\u7d20\u51fa\u5806 */\npop() {\n// \u5224\u7a7a\u5904\u7406\nif (this.isEmpty()) throw new Error('\u5806\u4e3a\u7a7a');\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nthis.#swap(0, this.size() - 1);\n// \u5220\u9664\u8282\u70b9\nconst val = this.#maxHeap.pop();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nthis.#siftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\n#siftDown(i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nconst l = this.#left(i),\nr = this.#right(i);\nlet ma = i;\nif (l &lt; this.size() &amp;&amp; this.#maxHeap[l] &gt; this.#maxHeap[ma]) ma = l;\nif (r &lt; this.size() &amp;&amp; this.#maxHeap[r] &gt; this.#maxHeap[ma]) ma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma === i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nthis.#swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n</code></pre> my_heap.ts<pre><code>/* \u5143\u7d20\u51fa\u5806 */\npop(): number {\n// \u5224\u7a7a\u5904\u7406\nif (this.isEmpty()) throw new RangeError('Heap is empty.');\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nthis.swap(0, this.size() - 1);\n// \u5220\u9664\u8282\u70b9\nconst val = this.maxHeap.pop();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nthis.siftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nsiftDown(i: number): void {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nconst l = this.left(i),\nr = this.right(i);\nlet ma = i;\nif (l &lt; this.size() &amp;&amp; this.maxHeap[l] &gt; this.maxHeap[ma]) ma = l;\nif (r &lt; this.size() &amp;&amp; this.maxHeap[r] &gt; this.maxHeap[ma]) ma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma === i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nthis.swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n</code></pre> my_heap.c<pre><code>/* \u5143\u7d20\u51fa\u5806 */\nint pop(maxHeap *h) {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty(h)) {\nprintf(\"heap is empty!\");\nreturn INT_MAX;\n}\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(h, 0, size(h) - 1);\n// \u5220\u9664\u8282\u70b9\nint val = h-&gt;data[h-&gt;size - 1];\nh-&gt;size--;\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(h, 0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(maxHeap *h, int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\nint l = left(h, i);\nint r = right(h, i);\nint max = i;\nif (l &lt; size(h) &amp;&amp; h-&gt;data[l] &gt; h-&gt;data[max]) {\nmax = l;\n}\nif (r &lt; size(h) &amp;&amp; h-&gt;data[r] &gt; h-&gt;data[max]) {\nmax = r;\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (max == i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(h, i, max);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = max;\n}\n}\n</code></pre> my_heap.cs<pre><code>/* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty())\nthrow new IndexOutOfRangeException();\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(0, size() - 1);\n// \u5220\u9664\u8282\u70b9\nint val = maxHeap.Last();\nmaxHeap.RemoveAt(size() - 1);\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = left(i), r = right(i), ma = i;\nif (l &lt; size() &amp;&amp; maxHeap[l] &gt; maxHeap[ma])\nma = l;\nif (r &lt; size() &amp;&amp; maxHeap[r] &gt; maxHeap[ma])\nma = r;\n// \u82e5\u201c\u8282\u70b9 i \u6700\u5927\u201d\u6216\u201c\u8d8a\u8fc7\u53f6\u8282\u70b9\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n</code></pre> my_heap.swift<pre><code>/* \u5143\u7d20\u51fa\u5806 */\nfunc pop() -&gt; Int {\n// \u5224\u7a7a\u5904\u7406\nif isEmpty() {\nfatalError(\"\u5806\u4e3a\u7a7a\")\n}\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(i: 0, j: size() - 1)\n// \u5220\u9664\u8282\u70b9\nlet val = maxHeap.remove(at: size() - 1)\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(i: 0)\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(i: Int) {\nvar i = i\nwhile true {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet l = left(i: i)\nlet r = right(i: i)\nvar ma = i\nif l &lt; size(), maxHeap[l] &gt; maxHeap[ma] {\nma = l\n}\nif r &lt; size(), maxHeap[r] &gt; maxHeap[ma] {\nma = r\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(i: i, j: ma)\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\n}\n}\n</code></pre> my_heap.zig<pre><code>// \u5143\u7d20\u51fa\u5806\nfn pop(self: *Self) !T {\n// \u5224\u65ad\u5904\u7406\nif (self.isEmpty()) unreachable;\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\ntry self.swap(0, self.size() - 1);\n// \u5220\u9664\u8282\u70b9\nvar val = self.max_heap.?.pop();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\ntry self.siftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n} // \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\nfn siftDown(self: *Self, i_: usize) !void {\nvar i = i_;\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nvar l = left(i);\nvar r = right(i);\nvar ma = i;\nif (l &lt; self.size() and self.max_heap.?.items[l] &gt; self.max_heap.?.items[ma]) ma = l;\nif (r &lt; self.size() and self.max_heap.?.items[r] &gt; self.max_heap.?.items[ma]) ma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\ntry self.swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n</code></pre> my_heap.dart<pre><code>/* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty()) throw Exception('\u5806\u4e3a\u7a7a');\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n_swap(0, size() - 1);\n// \u5220\u9664\u8282\u70b9\nint val = _maxHeap.removeLast();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = _left(i);\nint r = _right(i);\nint ma = i;\nif (l &lt; size() &amp;&amp; _maxHeap[l] &gt; _maxHeap[ma]) ma = l;\nif (r &lt; size() &amp;&amp; _maxHeap[r] &gt; _maxHeap[ma]) ma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\n_swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n</code></pre> my_heap.rs<pre><code>/* \u5143\u7d20\u51fa\u5806 */\nfn pop(&amp;mut self) -&gt; i32 {\n// \u5224\u7a7a\u5904\u7406\nif self.is_empty() {\npanic!(\"index out of bounds\");\n}\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nself.swap(0, self.size() - 1);\n// \u5220\u9664\u8282\u70b9\nlet val = self.max_heap.remove(self.size() - 1);\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nself.sift_down(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nval\n}\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(&amp;mut self, mut i: usize) {\nloop {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet (l, r, mut ma) = (Self::left(i), Self::right(i), i);\nif l &lt; self.size() &amp;&amp; self.max_heap[l] &gt; self.max_heap[ma] {\nma = l;\n}\nif r &lt; self.size() &amp;&amp; self.max_heap[r] &gt; self.max_heap[ma] {\nma = r;\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nself.swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n</code></pre>"},{"location":"chapter_heap/heap/#813","title":"8.1.3. \u00a0 \u5806\u5e38\u89c1\u5e94\u7528","text":"<ul> <li>\u4f18\u5148\u961f\u5217\uff1a\u5806\u901a\u5e38\u4f5c\u4e3a\u5b9e\u73b0\u4f18\u5148\u961f\u5217\u7684\u9996\u9009\u6570\u636e\u7ed3\u6784\uff0c\u5176\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log n)\\) \uff0c\u800c\u5efa\u961f\u64cd\u4f5c\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u975e\u5e38\u9ad8\u6548\u3002</li> <li>\u5806\u6392\u5e8f\uff1a\u7ed9\u5b9a\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5b83\u4eec\u5efa\u7acb\u4e00\u4e2a\u5806\uff0c\u7136\u540e\u4e0d\u65ad\u5730\u6267\u884c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\uff0c\u4ece\u800c\u5f97\u5230\u6709\u5e8f\u6570\u636e\u3002\u7136\u800c\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u4e00\u79cd\u66f4\u4f18\u96c5\u7684\u65b9\u5f0f\u5b9e\u73b0\u5806\u6392\u5e8f\uff0c\u8be6\u89c1\u540e\u7eed\u7684\u5806\u6392\u5e8f\u7ae0\u8282\u3002</li> <li>\u83b7\u53d6\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff1a\u8fd9\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u7b97\u6cd5\u95ee\u9898\uff0c\u540c\u65f6\u4e5f\u662f\u4e00\u79cd\u5178\u578b\u5e94\u7528\uff0c\u4f8b\u5982\u9009\u62e9\u70ed\u5ea6\u524d 10 \u7684\u65b0\u95fb\u4f5c\u4e3a\u5fae\u535a\u70ed\u641c\uff0c\u9009\u53d6\u9500\u91cf\u524d 10 \u7684\u5546\u54c1\u7b49\u3002</li> </ul>"},{"location":"chapter_heap/summary/","title":"8.4. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u5806\u662f\u4e00\u68f5\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u6839\u636e\u6210\u7acb\u6761\u4ef6\u53ef\u5206\u4e3a\u5927\u9876\u5806\u548c\u5c0f\u9876\u5806\u3002\u5927\uff08\u5c0f\uff09\u9876\u5806\u7684\u5806\u9876\u5143\u7d20\u662f\u6700\u5927\uff08\u5c0f\uff09\u7684\u3002</li> <li>\u4f18\u5148\u961f\u5217\u7684\u5b9a\u4e49\u662f\u5177\u6709\u51fa\u961f\u4f18\u5148\u7ea7\u7684\u961f\u5217\uff0c\u901a\u5e38\u4f7f\u7528\u5806\u6765\u5b9e\u73b0\u3002</li> <li>\u5806\u7684\u5e38\u7528\u64cd\u4f5c\u53ca\u5176\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5305\u62ec\uff1a\u5143\u7d20\u5165\u5806 \\(O(\\log n)\\) \u3001\u5806\u9876\u5143\u7d20\u51fa\u5806 \\(O(\\log n)\\) \u548c\u8bbf\u95ee\u5806\u9876\u5143\u7d20 \\(O(1)\\) \u7b49\u3002</li> <li>\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u7528\u6570\u7ec4\u8868\u793a\uff0c\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u5806\u3002</li> <li>\u5806\u5316\u64cd\u4f5c\u7528\u4e8e\u7ef4\u62a4\u5806\u7684\u6027\u8d28\uff0c\u5728\u5165\u5806\u548c\u51fa\u5806\u64cd\u4f5c\u4e2d\u90fd\u4f1a\u7528\u5230\u3002</li> <li>\u8f93\u5165 \\(n\\) \u4e2a\u5143\u7d20\u5e76\u5efa\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002</li> <li>Top-K \u662f\u4e00\u4e2a\u7ecf\u5178\u7b97\u6cd5\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u5806\u6570\u636e\u7ed3\u6784\u9ad8\u6548\u89e3\u51b3\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log k)\\) \u3002</li> </ul>"},{"location":"chapter_heap/summary/#841-q-a","title":"8.4.1. \u00a0 Q &amp; A","text":"<p>\u6570\u636e\u7ed3\u6784\u7684\u201c\u5806\u201d\u4e0e\u5185\u5b58\u7ba1\u7406\u7684\u201c\u5806\u201d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\u5417\uff1f</p> <p>\u4e24\u8005\u4e0d\u662f\u540c\u4e00\u4e2a\u6982\u5ff5\uff0c\u53ea\u662f\u78b0\u5de7\u90fd\u53eb\u5806\u3002\u8ba1\u7b97\u673a\u7cfb\u7edf\u5185\u5b58\u4e2d\u7684\u5806\u662f\u52a8\u6001\u5185\u5b58\u5206\u914d\u7684\u4e00\u90e8\u5206\uff0c\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5b58\u50a8\u6570\u636e\u3002\u7a0b\u5e8f\u53ef\u4ee5\u8bf7\u6c42\u4e00\u5b9a\u91cf\u7684\u5806\u5185\u5b58\uff0c\u7528\u4e8e\u5b58\u50a8\u5982\u5bf9\u8c61\u548c\u6570\u7ec4\u7b49\u590d\u6742\u7ed3\u6784\u3002\u5f53\u8fd9\u4e9b\u6570\u636e\u4e0d\u518d\u9700\u8981\u65f6\uff0c\u7a0b\u5e8f\u9700\u8981\u91ca\u653e\u8fd9\u4e9b\u5185\u5b58\uff0c\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u9732\u3002\u76f8\u8f83\u4e8e\u6808\u5185\u5b58\uff0c\u5806\u5185\u5b58\u7684\u7ba1\u7406\u548c\u4f7f\u7528\u9700\u8981\u66f4\u8c28\u614e\uff0c\u4e0d\u6070\u5f53\u7684\u4f7f\u7528\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u9732\u548c\u91ce\u6307\u9488\u7b49\u95ee\u9898\u3002</p>"},{"location":"chapter_heap/top_k/","title":"8.3. \u00a0 Top-K \u95ee\u9898","text":"<p>Question</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u65e0\u5e8f\u6570\u7ec4 <code>nums</code> \uff0c\u8bf7\u8fd4\u56de\u6570\u7ec4\u4e2d\u524d \\(k\\) \u5927\u7684\u5143\u7d20\u3002</p> <p>\u5bf9\u4e8e\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u5148\u4ecb\u7ecd\u4e24\u79cd\u601d\u8def\u6bd4\u8f83\u76f4\u63a5\u7684\u89e3\u6cd5\uff0c\u518d\u4ecb\u7ecd\u6548\u7387\u66f4\u9ad8\u7684\u5806\u89e3\u6cd5\u3002</p>"},{"location":"chapter_heap/top_k/#831","title":"8.3.1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u904d\u5386\u9009\u62e9","text":"<p>\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c \\(k\\) \u8f6e\u904d\u5386\uff0c\u5206\u522b\u5728\u6bcf\u8f6e\u4e2d\u63d0\u53d6\u7b2c \\(1\\) , \\(2\\) , \\(\\cdots\\) , \\(k\\) \u5927\u7684\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nk)\\) \u3002</p> <p>\u8be5\u65b9\u6cd5\u53ea\u9002\u7528\u4e8e \\(k \\ll n\\) \u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u5f53 \\(k\\) \u4e0e \\(n\\) \u6bd4\u8f83\u63a5\u8fd1\u65f6\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411\u4e8e \\(O(n^2)\\) \uff0c\u975e\u5e38\u8017\u65f6\u3002</p> <p></p> <p> \u56fe\uff1a\u904d\u5386\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 </p> <p>Tip</p> <p>\u5f53 \\(k = n\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\uff0c\u7b49\u4ef7\u4e8e\u300c\u9009\u62e9\u6392\u5e8f\u300d\u7b97\u6cd5\u3002</p>"},{"location":"chapter_heap/top_k/#832","title":"8.3.2. \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u6392\u5e8f","text":"<p>\u6211\u4eec\u53ef\u4ee5\u5bf9\u6570\u7ec4 <code>nums</code> \u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u8fd4\u56de\u6700\u53f3\u8fb9\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002</p> <p>\u663e\u7136\uff0c\u8be5\u65b9\u6cd5\u201c\u8d85\u989d\u201d\u5b8c\u6210\u4efb\u52a1\u4e86\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u9700\u8981\u627e\u51fa\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u5373\u53ef\uff0c\u800c\u4e0d\u9700\u8981\u6392\u5e8f\u5176\u4ed6\u5143\u7d20\u3002</p> <p></p> <p> \u56fe\uff1a\u6392\u5e8f\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 </p>"},{"location":"chapter_heap/top_k/#833","title":"8.3.3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u5806","text":"<p>\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u5806\u66f4\u52a0\u9ad8\u6548\u5730\u89e3\u51b3 Top-K \u95ee\u9898\uff0c\u6d41\u7a0b\u5982\u4e0b\uff1a</p> <ol> <li>\u521d\u59cb\u5316\u4e00\u4e2a\u5c0f\u9876\u5806\uff0c\u5176\u5806\u9876\u5143\u7d20\u6700\u5c0f\u3002</li> <li>\u5148\u5c06\u6570\u7ec4\u7684\u524d \\(k\\) \u4e2a\u5143\u7d20\u4f9d\u6b21\u5165\u5806\u3002</li> <li>\u4ece\u7b2c \\(k + 1\\) \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\uff0c\u5e76\u5c06\u5f53\u524d\u5143\u7d20\u5165\u5806\u3002</li> <li>\u904d\u5386\u5b8c\u6210\u540e\uff0c\u5806\u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u6700\u5927\u7684 \\(k\\) \u4e2a\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> \u56fe\uff1a\u57fa\u4e8e\u5806\u5bfb\u627e\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 </p> <p>\u603b\u5171\u6267\u884c\u4e86 \\(n\\) \u8f6e\u5165\u5806\u548c\u51fa\u5806\uff0c\u5806\u7684\u6700\u5927\u957f\u5ea6\u4e3a \\(k\\) \uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log k)\\) \u3002\u8be5\u65b9\u6cd5\u7684\u6548\u7387\u5f88\u9ad8\uff0c\u5f53 \\(k\\) \u8f83\u5c0f\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411 \\(O(n)\\) \uff1b\u5f53 \\(k\\) \u8f83\u5927\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u4f1a\u8d85\u8fc7 \\(O(n \\log n)\\) \u3002</p> <p>\u53e6\u5916\uff0c\u8be5\u65b9\u6cd5\u9002\u7528\u4e8e\u52a8\u6001\u6570\u636e\u6d41\u7684\u4f7f\u7528\u573a\u666f\u3002\u5728\u4e0d\u65ad\u52a0\u5165\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u6301\u7eed\u7ef4\u62a4\u5806\u5185\u7684\u5143\u7d20\uff0c\u4ece\u800c\u5b9e\u73b0\u6700\u5927 \\(k\\) \u4e2a\u5143\u7d20\u7684\u52a8\u6001\u66f4\u65b0\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust top_k.java<pre><code>/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nQueue&lt;Integer&gt; topKHeap(int[] nums, int k) {\nQueue&lt;Integer&gt; heap = new PriorityQueue&lt;Integer&gt;();\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor (int i = 0; i &lt; k; i++) {\nheap.offer(nums[i]);\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor (int i = k; i &lt; nums.length; i++) {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif (nums[i] &gt; heap.peek()) {\nheap.poll();\nheap.offer(nums[i]);\n}\n}\nreturn heap;\n}\n</code></pre> top_k.cpp<pre><code>/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\npriority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; topKHeap(vector&lt;int&gt; &amp;nums, int k) {\npriority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; heap;\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor (int i = 0; i &lt; k; i++) {\nheap.push(nums[i]);\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor (int i = k; i &lt; nums.size(); i++) {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif (nums[i] &gt; heap.top()) {\nheap.pop();\nheap.push(nums[i]);\n}\n}\nreturn heap;\n}\n</code></pre> top_k.py<pre><code>def top_k_heap(nums: list[int], k: int) -&gt; list[int]:\n\"\"\"\u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20\"\"\"\nheap = []\n# \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor i in range(k):\nheapq.heappush(heap, nums[i])\n# \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor i in range(k, len(nums)):\n# \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif nums[i] &gt; heap[0]:\nheapq.heappop(heap)\nheapq.heappush(heap, nums[i])\nreturn heap\n</code></pre> top_k.go<pre><code>/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums []int, k int) *minHeap {\nh := &amp;minHeap{}\nheap.Init(h)\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor i := 0; i &lt; k; i++ {\nheap.Push(h, nums[i])\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor i := k; i &lt; len(nums); i++ {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif nums[i] &gt; h.Top().(int) {\nheap.Pop(h)\nheap.Push(h, nums[i])\n}\n}\nreturn h\n}\n</code></pre> top_k.js<pre><code>[class]{}-[func]{topKHeap}\n</code></pre> top_k.ts<pre><code>[class]{}-[func]{topKHeap}\n</code></pre> top_k.c<pre><code>[class]{}-[func]{topKHeap}\n</code></pre> top_k.cs<pre><code>/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nPriorityQueue&lt;int, int&gt; topKHeap(int[] nums, int k) {\nPriorityQueue&lt;int, int&gt; heap = new PriorityQueue&lt;int, int&gt;();\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor (int i = 0; i &lt; k; i++) {\nheap.Enqueue(nums[i], nums[i]);\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor (int i = k; i &lt; nums.Length; i++) {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif (nums[i] &gt; heap.Peek()) {\nheap.Dequeue();\nheap.Enqueue(nums[i], nums[i]);\n}\n}\nreturn heap;\n}\n</code></pre> top_k.swift<pre><code>/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums: [Int], k: Int) -&gt; [Int] {\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nvar heap = Array(nums.prefix(k))\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor i in stride(from: k, to: nums.count, by: 1) {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif nums[i] &gt; heap.first! {\nheap.removeFirst()\nheap.insert(nums[i], at: 0)\n}\n}\nreturn heap\n}\n</code></pre> top_k.zig<pre><code>[class]{}-[func]{topKHeap}\n</code></pre> top_k.dart<pre><code>/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nMinHeap topKHeap(List&lt;int&gt; nums, int k) {\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nMinHeap heap = MinHeap(nums.sublist(0, k));\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor (int i = k; i &lt; nums.length; i++) {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif (nums[i] &gt; heap.peek()) {\nheap.pop();\nheap.push(nums[i]);\n}\n}\nreturn heap;\n}\n</code></pre> top_k.rs<pre><code>/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfn top_k_heap(nums: Vec&lt;i32&gt;, k: usize) -&gt; BinaryHeap&lt;Reverse&lt;i32&gt;&gt; {\n// Rust \u7684 BinaryHeap \u662f\u5927\u9876\u5806\uff0c\u4f7f\u7528 Reverse \u5c06\u5143\u7d20\u5927\u5c0f\u53cd\u8f6c\nlet mut heap = BinaryHeap::&lt;Reverse&lt;i32&gt;&gt;::new();\n// \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\nfor &amp;num in nums.iter().take(k) {\nheap.push(Reverse(num));\n}\n// \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\nfor &amp;num in nums.iter().skip(k) {\n// \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\nif num &gt; heap.peek().unwrap().0 {\nheap.pop();\nheap.push(Reverse(num));\n}\n}\nheap\n}\n</code></pre>"},{"location":"chapter_introduction/","title":"1. \u00a0 \u521d\u8bc6\u7b97\u6cd5","text":"<p>Abstract</p> <p>\u4e00\u4f4d\u5c11\u5973\u7fe9\u7fe9\u8d77\u821e\uff0c\u4e0e\u6570\u636e\u4ea4\u7ec7\u5728\u4e00\u8d77\uff0c\u88d9\u6446\u4e0a\u98d8\u626c\u7740\u7b97\u6cd5\u7684\u65cb\u5f8b\u3002</p> <p>\u5979\u9080\u8bf7\u4f60\u5171\u821e\uff0c\u8bf7\u7d27\u8ddf\u5979\u7684\u6b65\u4f10\uff0c\u8e0f\u5165\u5145\u6ee1\u903b\u8f91\u4e0e\u7f8e\u611f\u7684\u7b97\u6cd5\u4e16\u754c\u3002</p>"},{"location":"chapter_introduction/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>1.1 \u00a0 \u7b97\u6cd5\u65e0\u5904\u4e0d\u5728</li> <li>1.2 \u00a0 \u7b97\u6cd5\u662f\u4ec0\u4e48</li> <li>1.3 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_introduction/algorithms_are_everywhere/","title":"1.1. \u00a0 \u7b97\u6cd5\u65e0\u5904\u4e0d\u5728","text":"<p>\u5f53\u6211\u4eec\u542c\u5230\u201c\u7b97\u6cd5\u201d\u8fd9\u4e2a\u8bcd\u65f6\uff0c\u5f88\u81ea\u7136\u5730\u4f1a\u60f3\u5230\u6570\u5b66\u3002\u7136\u800c\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7b97\u6cd5\u5e76\u4e0d\u6d89\u53ca\u590d\u6742\u6570\u5b66\uff0c\u800c\u662f\u66f4\u591a\u5730\u4f9d\u8d56\u4e8e\u57fa\u672c\u903b\u8f91\uff0c\u8fd9\u4e9b\u903b\u8f91\u5728\u6211\u4eec\u7684\u65e5\u5e38\u751f\u6d3b\u4e2d\u5904\u5904\u53ef\u89c1\u3002</p> <p>\u5728\u6b63\u5f0f\u63a2\u8ba8\u7b97\u6cd5\u4e4b\u524d\uff0c\u6709\u4e00\u4e2a\u6709\u8da3\u7684\u4e8b\u5b9e\u503c\u5f97\u5206\u4eab\uff1a\u4f60\u5df2\u7ecf\u5728\u4e0d\u77e5\u4e0d\u89c9\u4e2d\u5b66\u4f1a\u4e86\u8bb8\u591a\u7b97\u6cd5\uff0c\u5e76\u4e60\u60ef\u5c06\u5b83\u4eec\u5e94\u7528\u5230\u65e5\u5e38\u751f\u6d3b\u4e2d\u4e86\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u4e3e\u51e0\u4e2a\u5177\u4f53\u4f8b\u5b50\u6765\u8bc1\u5b9e\u8fd9\u4e00\u70b9\u3002</p> <p>\u4f8b\u4e00\uff1a\u67e5\u9605\u5b57\u5178\u3002\u5728\u5b57\u5178\u91cc\uff0c\u6bcf\u4e2a\u6c49\u5b57\u90fd\u5bf9\u5e94\u4e00\u4e2a\u62fc\u97f3\uff0c\u800c\u5b57\u5178\u662f\u6309\u7167\u62fc\u97f3\u7684\u82f1\u6587\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\u7684\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u67e5\u627e\u4e00\u4e2a\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u5b57\uff0c\u901a\u5e38\u4f1a\u8fd9\u6837\u64cd\u4f5c\uff1a</p> <ol> <li>\u7ffb\u5f00\u5b57\u5178\u7ea6\u4e00\u534a\u7684\u9875\u6570\uff0c\u67e5\u770b\u8be5\u9875\u9996\u5b57\u6bcd\u662f\u4ec0\u4e48\uff0c\u5047\u8bbe\u9996\u5b57\u6bcd\u4e3a \\(m\\) \u3002</li> <li>\u7531\u4e8e\u5728\u82f1\u6587\u5b57\u6bcd\u8868\u4e2d \\(r\\) \u4f4d\u4e8e \\(m\\) \u4e4b\u540e\uff0c\u6240\u4ee5\u6392\u9664\u5b57\u5178\u524d\u534a\u90e8\u5206\uff0c\u67e5\u627e\u8303\u56f4\u7f29\u5c0f\u5230\u540e\u534a\u90e8\u5206\u3002</li> <li>\u4e0d\u65ad\u91cd\u590d\u6b65\u9aa4 1-2 \uff0c\u76f4\u81f3\u627e\u5230\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u9875\u7801\u4e3a\u6b62\u3002</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> \u56fe\uff1a\u67e5\u5b57\u5178\u6b65\u9aa4 </p> <p>\u67e5\u9605\u5b57\u5178\u8fd9\u4e2a\u5c0f\u5b66\u751f\u5fc5\u5907\u6280\u80fd\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u8457\u540d\u7684\u300c\u4e8c\u5206\u67e5\u627e\u300d\u3002\u4ece\u6570\u636e\u7ed3\u6784\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b57\u5178\u89c6\u4e3a\u4e00\u4e2a\u5df2\u6392\u5e8f\u7684\u300c\u6570\u7ec4\u300d\uff1b\u4ece\u7b97\u6cd5\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u67e5\u5b57\u5178\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u770b\u4f5c\u662f\u300c\u4e8c\u5206\u67e5\u627e\u300d\u7b97\u6cd5\u3002</p> <p>\u4f8b\u4e8c\uff1a\u6574\u7406\u6251\u514b\u3002\u6211\u4eec\u5728\u6253\u724c\u65f6\uff0c\u6bcf\u5c40\u90fd\u9700\u8981\u6574\u7406\u6251\u514b\u724c\uff0c\u4f7f\u5176\u4ece\u5c0f\u5230\u5927\u6392\u5217\uff0c\u5b9e\u73b0\u6d41\u7a0b\u5982\u4e0b\uff1a</p> <ol> <li>\u5c06\u6251\u514b\u724c\u5212\u5206\u4e3a\u201c\u6709\u5e8f\u201d\u548c\u201c\u65e0\u5e8f\u201d\u4e24\u90e8\u5206\uff0c\u5e76\u5047\u8bbe\u521d\u59cb\u72b6\u6001\u4e0b\u6700\u5de6 1 \u5f20\u6251\u514b\u724c\u5df2\u7ecf\u6709\u5e8f\u3002</li> <li>\u5728\u65e0\u5e8f\u90e8\u5206\u62bd\u51fa\u4e00\u5f20\u6251\u514b\u724c\uff0c\u63d2\u5165\u81f3\u6709\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\uff1b\u5b8c\u6210\u540e\u6700\u5de6 2 \u5f20\u6251\u514b\u5df2\u7ecf\u6709\u5e8f\u3002</li> <li>\u4e0d\u65ad\u5faa\u73af\u6b65\u9aa4 <code>2.</code> \uff0c\u6bcf\u4e00\u8f6e\u5c06\u4e00\u5f20\u6251\u514b\u724c\u4ece\u65e0\u5e8f\u90e8\u5206\u63d2\u5165\u81f3\u6709\u5e8f\u90e8\u5206\uff0c\u76f4\u81f3\u6240\u6709\u6251\u514b\u724c\u90fd\u6709\u5e8f\u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u6251\u514b\u6392\u5e8f\u6b65\u9aa4 </p> <p>\u4e0a\u8ff0\u6574\u7406\u6251\u514b\u724c\u7684\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u300c\u63d2\u5165\u6392\u5e8f\u300d\u7b97\u6cd5\uff0c\u5b83\u5728\u5904\u7406\u5c0f\u578b\u6570\u636e\u96c6\u65f6\u975e\u5e38\u9ad8\u6548\u3002\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u7684\u6392\u5e8f\u5e93\u51fd\u6570\u4e2d\u90fd\u5b58\u5728\u63d2\u5165\u6392\u5e8f\u7684\u8eab\u5f71\u3002</p> <p>\u4f8b\u4e09\uff1a\u8d27\u5e01\u627e\u96f6\u3002\u5047\u8bbe\u6211\u4eec\u5728\u8d85\u5e02\u8d2d\u4e70\u4e86 \\(69\\) \u5143\u7684\u5546\u54c1\uff0c\u7ed9\u6536\u94f6\u5458\u4ed8\u4e86 \\(100\\) \u5143\uff0c\u5219\u6536\u94f6\u5458\u9700\u8981\u7ed9\u6211\u4eec\u627e \\(31\\) \u5143\u3002\u4ed6\u4f1a\u5f88\u81ea\u7136\u5730\u5b8c\u6210\u4ee5\u4e0b\u601d\u8003\uff1a</p> <ol> <li>\u53ef\u9009\u9879\u662f\u6bd4 \\(31\\) \u5143\u9762\u503c\u66f4\u5c0f\u7684\u8d27\u5e01\uff0c\u5305\u62ec \\(1\\) , \\(5\\) , \\(10\\) , \\(20\\) \u5143\u3002</li> <li>\u4ece\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(20\\) \u5143\uff0c\u5269\u4f59 \\(31 - 20 = 11\\) \u5143\u3002</li> <li>\u4ece\u5269\u4f59\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(10\\) \u5143\uff0c\u5269\u4f59 \\(11 - 10 = 1\\) \u5143\u3002</li> <li>\u4ece\u5269\u4f59\u53ef\u9009\u9879\u4e2d\u62ff\u51fa\u6700\u5927\u7684 \\(1\\) \u5143\uff0c\u5269\u4f59 \\(1 - 1 = 0\\) \u5143\u3002</li> <li>\u5b8c\u6210\u627e\u96f6\uff0c\u65b9\u6848\u4e3a \\(20 + 10 + 1 = 31\\) \u5143\u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u8d27\u5e01\u627e\u96f6\u8fc7\u7a0b </p> <p>\u5728\u4ee5\u4e0a\u6b65\u9aa4\u4e2d\uff0c\u6211\u4eec\u6bcf\u4e00\u6b65\u90fd\u91c7\u53d6\u5f53\u524d\u770b\u6765\u6700\u597d\u7684\u9009\u62e9\uff08\u5c3d\u53ef\u80fd\u7528\u5927\u9762\u989d\u7684\u8d27\u5e01\uff09\uff0c\u6700\u7ec8\u5f97\u5230\u4e86\u53ef\u884c\u7684\u627e\u96f6\u65b9\u6848\u3002\u4ece\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u300c\u8d2a\u5fc3\u7b97\u6cd5\u300d\u3002</p> <p>\u5c0f\u5230\u70f9\u996a\u4e00\u9053\u83dc\uff0c\u5927\u5230\u661f\u9645\u822a\u884c\uff0c\u51e0\u4e4e\u6240\u6709\u95ee\u9898\u7684\u89e3\u51b3\u90fd\u79bb\u4e0d\u5f00\u7b97\u6cd5\u3002\u8ba1\u7b97\u673a\u7684\u51fa\u73b0\u4f7f\u6211\u4eec\u80fd\u591f\u901a\u8fc7\u7f16\u7a0b\u5c06\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u540c\u65f6\u7f16\u5199\u4ee3\u7801\u8c03\u7528 CPU \u548c GPU \u6267\u884c\u7b97\u6cd5\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6211\u4eec\u5c31\u80fd\u628a\u751f\u6d3b\u4e2d\u7684\u95ee\u9898\u8f6c\u79fb\u5230\u8ba1\u7b97\u673a\u4e0a\uff0c\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u89e3\u51b3\u5404\u79cd\u590d\u6742\u95ee\u9898\u3002</p> <p>Tip</p> <p>\u9605\u8bfb\u81f3\u6b64\uff0c\u5982\u679c\u4f60\u5bf9\u6570\u636e\u7ed3\u6784\u3001\u7b97\u6cd5\u3001\u6570\u7ec4\u548c\u4e8c\u5206\u67e5\u627e\u7b49\u6982\u5ff5\u4ecd\u611f\u5230\u4e00\u77e5\u534a\u89e3\uff0c\u90a3\u4e48\u592a\u597d\u4e86\uff01\u56e0\u4e3a\u8fd9\u6b63\u662f\u672c\u4e66\u5b58\u5728\u7684\u610f\u4e49\u3002\u63a5\u4e0b\u6765\uff0c\u8fd9\u672c\u4e66\u5c06\u5f15\u5bfc\u4f60\u4e00\u6b65\u6b65\u6df1\u5165\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u77e5\u8bc6\u6bbf\u5802\u3002</p>"},{"location":"chapter_introduction/summary/","title":"1.3. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u7b97\u6cd5\u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\u65e0\u5904\u4e0d\u5728\uff0c\u5e76\u4e0d\u662f\u9065\u4e0d\u53ef\u53ca\u7684\u9ad8\u6df1\u77e5\u8bc6\u3002\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u5df2\u7ecf\u5728\u4e0d\u77e5\u4e0d\u89c9\u4e2d\u5b66\u4f1a\u4e86\u8bb8\u591a\u7b97\u6cd5\uff0c\u7528\u4ee5\u89e3\u51b3\u751f\u6d3b\u4e2d\u7684\u5927\u5c0f\u95ee\u9898\u3002</li> <li>\u67e5\u9605\u5b57\u5178\u7684\u539f\u7406\u4e0e\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u76f8\u4e00\u81f4\u3002\u4e8c\u5206\u67e5\u627e\u4f53\u73b0\u4e86\u5206\u800c\u6cbb\u4e4b\u7684\u91cd\u8981\u7b97\u6cd5\u601d\u60f3\u3002</li> <li>\u6574\u7406\u6251\u514b\u7684\u8fc7\u7a0b\u4e0e\u63d2\u5165\u6392\u5e8f\u7b97\u6cd5\u975e\u5e38\u7c7b\u4f3c\u3002\u63d2\u5165\u6392\u5e8f\u9002\u5408\u6392\u5e8f\u5c0f\u578b\u6570\u636e\u96c6\u3002</li> <li>\u8d27\u5e01\u627e\u96f6\u7684\u6b65\u9aa4\u672c\u8d28\u4e0a\u662f\u8d2a\u5fc3\u7b97\u6cd5\uff0c\u6bcf\u4e00\u6b65\u90fd\u91c7\u53d6\u5f53\u524d\u770b\u6765\u7684\u6700\u597d\u9009\u62e9\u3002</li> <li>\u7b97\u6cd5\u662f\u5728\u6709\u9650\u65f6\u95f4\u5185\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u7684\u4e00\u7ec4\u6307\u4ee4\u6216\u64cd\u4f5c\u6b65\u9aa4\uff0c\u800c\u6570\u636e\u7ed3\u6784\u662f\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u548c\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002</li> <li>\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7d27\u5bc6\u76f8\u8fde\u3002\u6570\u636e\u7ed3\u6784\u662f\u7b97\u6cd5\u7684\u57fa\u77f3\uff0c\u800c\u7b97\u6cd5\u5219\u662f\u53d1\u6325\u6570\u636e\u7ed3\u6784\u4f5c\u7528\u7684\u821e\u53f0\u3002</li> <li>\u4e50\u9ad8\u79ef\u6728\u5bf9\u5e94\u4e8e\u6570\u636e\uff0c\u79ef\u6728\u5f62\u72b6\u548c\u8fde\u63a5\u65b9\u5f0f\u4ee3\u8868\u6570\u636e\u7ed3\u6784\uff0c\u62fc\u88c5\u79ef\u6728\u7684\u6b65\u9aa4\u5219\u5bf9\u5e94\u7b97\u6cd5\u3002</li> </ul>"},{"location":"chapter_introduction/what_is_dsa/","title":"1.2. \u00a0 \u7b97\u6cd5\u662f\u4ec0\u4e48","text":""},{"location":"chapter_introduction/what_is_dsa/#121","title":"1.2.1. \u00a0 \u7b97\u6cd5\u5b9a\u4e49","text":"<p>\u300c\u7b97\u6cd5 Algorithm\u300d\u662f\u5728\u6709\u9650\u65f6\u95f4\u5185\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u7684\u4e00\u7ec4\u6307\u4ee4\u6216\u64cd\u4f5c\u6b65\u9aa4\u3002\u5b83\u5177\u6709\u4ee5\u4e0b\u7279\u6027\uff1a</p> <ul> <li>\u95ee\u9898\u662f\u660e\u786e\u7684\uff0c\u5305\u542b\u6e05\u6670\u7684\u8f93\u5165\u548c\u8f93\u51fa\u5b9a\u4e49\u3002</li> <li>\u5177\u6709\u53ef\u884c\u6027\uff0c\u80fd\u591f\u5728\u6709\u9650\u6b65\u9aa4\u3001\u65f6\u95f4\u548c\u5185\u5b58\u7a7a\u95f4\u4e0b\u5b8c\u6210\u3002</li> <li>\u5404\u6b65\u9aa4\u90fd\u6709\u786e\u5b9a\u7684\u542b\u4e49\uff0c\u76f8\u540c\u7684\u8f93\u5165\u548c\u8fd0\u884c\u6761\u4ef6\u4e0b\uff0c\u8f93\u51fa\u59cb\u7ec8\u76f8\u540c\u3002</li> </ul>"},{"location":"chapter_introduction/what_is_dsa/#122","title":"1.2.2. \u00a0 \u6570\u636e\u7ed3\u6784\u5b9a\u4e49","text":"<p>\u300c\u6570\u636e\u7ed3\u6784 Data Structure\u300d\u662f\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u548c\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002\u5b83\u7684\u8bbe\u8ba1\u76ee\u6807\u5305\u62ec\uff1a</p> <ul> <li>\u7a7a\u95f4\u5360\u7528\u5c3d\u91cf\u51cf\u5c11\uff0c\u8282\u7701\u8ba1\u7b97\u673a\u5185\u5b58\u3002</li> <li>\u6570\u636e\u64cd\u4f5c\u5c3d\u53ef\u80fd\u5feb\u901f\uff0c\u6db5\u76d6\u6570\u636e\u8bbf\u95ee\u3001\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66f4\u65b0\u7b49\u3002</li> <li>\u63d0\u4f9b\u7b80\u6d01\u7684\u6570\u636e\u8868\u793a\u548c\u903b\u8f91\u4fe1\u606f\uff0c\u4ee5\u4fbf\u4f7f\u5f97\u7b97\u6cd5\u9ad8\u6548\u8fd0\u884c\u3002</li> </ul> <p>\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5145\u6ee1\u6743\u8861\u7684\u8fc7\u7a0b\u3002\u5982\u679c\u60f3\u8981\u5728\u67d0\u65b9\u9762\u53d6\u5f97\u63d0\u5347\uff0c\u5f80\u5f80\u9700\u8981\u5728\u53e6\u4e00\u65b9\u9762\u4f5c\u51fa\u59a5\u534f\uff0c\u4f8b\u5982\uff1a</p> <ul> <li>\u94fe\u8868\u76f8\u8f83\u4e8e\u6570\u7ec4\uff0c\u5728\u6570\u636e\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u4e0a\u66f4\u52a0\u4fbf\u6377\uff0c\u4f46\u727a\u7272\u4e86\u6570\u636e\u8bbf\u95ee\u901f\u5ea6\u3002</li> <li>\u56fe\u76f8\u8f83\u4e8e\u94fe\u8868\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u903b\u8f91\u4fe1\u606f\uff0c\u4f46\u9700\u8981\u5360\u7528\u66f4\u5927\u7684\u5185\u5b58\u7a7a\u95f4\u3002</li> </ul>"},{"location":"chapter_introduction/what_is_dsa/#123","title":"1.2.3. \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb","text":"<p>\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u9ad8\u5ea6\u76f8\u5173\u3001\u7d27\u5bc6\u7ed3\u5408\uff0c\u5177\u4f53\u8868\u73b0\u5728\uff1a</p> <ul> <li>\u6570\u636e\u7ed3\u6784\u662f\u7b97\u6cd5\u7684\u57fa\u77f3\u3002\u6570\u636e\u7ed3\u6784\u4e3a\u7b97\u6cd5\u63d0\u4f9b\u4e86\u7ed3\u6784\u5316\u5b58\u50a8\u7684\u6570\u636e\uff0c\u4ee5\u53ca\u7528\u4e8e\u64cd\u4f5c\u6570\u636e\u7684\u65b9\u6cd5\u3002</li> <li>\u7b97\u6cd5\u662f\u6570\u636e\u7ed3\u6784\u53d1\u6325\u7684\u821e\u53f0\u3002\u6570\u636e\u7ed3\u6784\u672c\u8eab\u4ec5\u5b58\u50a8\u6570\u636e\u4fe1\u606f\uff0c\u901a\u8fc7\u7ed3\u5408\u7b97\u6cd5\u624d\u80fd\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u3002</li> <li>\u7279\u5b9a\u7b97\u6cd5\u901a\u5e38\u6709\u5bf9\u5e94\u6700\u4f18\u7684\u6570\u636e\u7ed3\u6784\u3002\u7b97\u6cd5\u901a\u5e38\u53ef\u4ee5\u57fa\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u5b9e\u73b0\uff0c\u4f46\u6700\u7ec8\u6267\u884c\u6548\u7387\u53ef\u80fd\u76f8\u5dee\u5f88\u5927\u3002</li> </ul> <p></p> <p> \u56fe\uff1a\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb </p> <p>\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u72b9\u5982\u62fc\u88c5\u79ef\u6728\u3002\u4e00\u5957\u79ef\u6728\uff0c\u9664\u4e86\u5305\u542b\u8bb8\u591a\u96f6\u4ef6\u4e4b\u5916\uff0c\u8fd8\u9644\u6709\u8be6\u7ec6\u7684\u7ec4\u88c5\u8bf4\u660e\u4e66\u3002\u6211\u4eec\u6309\u7167\u8bf4\u660e\u4e66\u4e00\u6b65\u6b65\u64cd\u4f5c\uff0c\u5c31\u80fd\u7ec4\u88c5\u51fa\u7cbe\u7f8e\u7684\u79ef\u6728\u6a21\u578b\u3002</p> <p></p> <p> \u56fe\uff1a\u62fc\u88c5\u79ef\u6728 </p> <p>\u4e24\u8005\u7684\u8be6\u7ec6\u5bf9\u5e94\u5173\u7cfb\u5982\u4e0b\u8868\u6240\u793a\u3002</p> <p> \u8868\uff1a\u5c06\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7c7b\u6bd4\u4e3a\u79ef\u6728 </p> \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5 \u79ef\u6728 \u8f93\u5165\u6570\u636e \u672a\u62fc\u88c5\u7684\u79ef\u6728 \u6570\u636e\u7ed3\u6784 \u79ef\u6728\u7ec4\u7ec7\u5f62\u5f0f\uff0c\u5305\u62ec\u5f62\u72b6\u3001\u5927\u5c0f\u3001\u8fde\u63a5\u65b9\u5f0f\u7b49 \u7b97\u6cd5 \u628a\u79ef\u6728\u62fc\u6210\u76ee\u6807\u5f62\u6001\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u6b65\u9aa4 \u8f93\u51fa\u6570\u636e \u79ef\u6728\u6a21\u578b <p>\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u662f\u72ec\u7acb\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u3002\u6b63\u56e0\u5982\u6b64\uff0c\u672c\u4e66\u5f97\u4ee5\u63d0\u4f9b\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u7684\u5b9e\u73b0\u3002</p> <p>\u7ea6\u5b9a\u4fd7\u6210\u7684\u7b80\u79f0</p> <p>\u5728\u5b9e\u9645\u8ba8\u8bba\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u300c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u300d\u7b80\u79f0\u4e3a\u300c\u7b97\u6cd5\u300d\u3002\u6bd4\u5982\u4f17\u6240\u5468\u77e5\u7684 LeetCode \u7b97\u6cd5\u9898\u76ee\uff0c\u5b9e\u9645\u4e0a\u540c\u65f6\u8003\u5bdf\u4e86\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e24\u65b9\u9762\u7684\u77e5\u8bc6\u3002</p>"},{"location":"chapter_preface/","title":"0. \u00a0 \u524d\u8a00","text":"<p>Abstract</p> <p>\u7b97\u6cd5\u72b9\u5982\u7f8e\u5999\u7684\u4ea4\u54cd\u4e50\uff0c\u6bcf\u4e00\u884c\u4ee3\u7801\u90fd\u50cf\u97f5\u5f8b\u822c\u6d41\u6dcc\u3002</p> <p>\u613f\u8fd9\u672c\u4e66\u5728\u4f60\u7684\u8111\u6d77\u4e2d\u8f7b\u8f7b\u54cd\u8d77\uff0c\u7559\u4e0b\u72ec\u7279\u800c\u6df1\u523b\u7684\u65cb\u5f8b\u3002</p>"},{"location":"chapter_preface/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>0.1 \u00a0 \u5173\u4e8e\u672c\u4e66</li> <li>0.2 \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u4e66</li> <li>0.3 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_preface/about_the_book/","title":"0.1. \u00a0 \u5173\u4e8e\u672c\u4e66","text":"<p>\u672c\u9879\u76ee\u65e8\u5728\u521b\u5efa\u4e00\u672c\u5f00\u6e90\u514d\u8d39\u3001\u65b0\u624b\u53cb\u597d\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5165\u95e8\u6559\u7a0b\u3002</p> <ul> <li>\u5168\u4e66\u91c7\u7528\u52a8\u753b\u56fe\u89e3\uff0c\u7ed3\u6784\u5316\u5730\u8bb2\u89e3\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u77e5\u8bc6\uff0c\u5185\u5bb9\u6e05\u6670\u6613\u61c2\u3001\u5b66\u4e60\u66f2\u7ebf\u5e73\u6ed1\u3002</li> <li>\u7b97\u6cd5\u6e90\u4ee3\u7801\u7686\u53ef\u4e00\u952e\u8fd0\u884c\uff0c\u652f\u6301 Java, C++, Python, Go, JS, TS, C#, Swift, Zig \u7b49\u8bed\u8a00\u3002</li> <li>\u9f13\u52b1\u8bfb\u8005\u5728\u7ae0\u8282\u8ba8\u8bba\u533a\u4e92\u5e2e\u4e92\u52a9\u3001\u5171\u540c\u8fdb\u6b65\uff0c\u63d0\u95ee\u4e0e\u8bc4\u8bba\u901a\u5e38\u53ef\u5728\u4e24\u65e5\u5185\u5f97\u5230\u56de\u590d\u3002</li> </ul>"},{"location":"chapter_preface/about_the_book/#011","title":"0.1.1. \u00a0 \u8bfb\u8005\u5bf9\u8c61","text":"<p>\u82e5\u60a8\u662f\u7b97\u6cd5\u521d\u5b66\u8005\uff0c\u4ece\u672a\u63a5\u89e6\u8fc7\u7b97\u6cd5\uff0c\u6216\u8005\u5df2\u7ecf\u6709\u4e00\u4e9b\u5237\u9898\u7ecf\u9a8c\uff0c\u5bf9\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u6709\u6a21\u7cca\u7684\u8ba4\u8bc6\uff0c\u5728\u4f1a\u4e0e\u4e0d\u4f1a\u4e4b\u95f4\u53cd\u590d\u6a2a\u8df3\uff0c\u90a3\u4e48\u8fd9\u672c\u4e66\u6b63\u662f\u4e3a\u60a8\u91cf\u8eab\u5b9a\u5236\uff01</p> <p>\u5982\u679c\u60a8\u5df2\u7ecf\u79ef\u7d2f\u4e00\u5b9a\u5237\u9898\u91cf\uff0c\u719f\u6089\u5927\u90e8\u5206\u9898\u578b\uff0c\u90a3\u4e48\u672c\u4e66\u53ef\u52a9\u60a8\u56de\u987e\u4e0e\u68b3\u7406\u7b97\u6cd5\u77e5\u8bc6\u4f53\u7cfb\uff0c\u4ed3\u5e93\u6e90\u4ee3\u7801\u53ef\u4ee5\u88ab\u5f53\u4f5c\u201c\u5237\u9898\u5de5\u5177\u5e93\u201d\u6216\u201c\u7b97\u6cd5\u5b57\u5178\u201d\u6765\u4f7f\u7528\u3002</p> <p>\u82e5\u60a8\u662f\u7b97\u6cd5\u5927\u795e\uff0c\u6211\u4eec\u671f\u5f85\u6536\u5230\u60a8\u7684\u5b9d\u8d35\u5efa\u8bae\uff0c\u6216\u8005\u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c\u3002</p> <p>\u524d\u7f6e\u6761\u4ef6</p> <p>\u60a8\u9700\u8981\u81f3\u5c11\u5177\u5907\u4efb\u4e00\u8bed\u8a00\u7684\u7f16\u7a0b\u57fa\u7840\uff0c\u80fd\u591f\u9605\u8bfb\u548c\u7f16\u5199\u7b80\u5355\u4ee3\u7801\u3002</p>"},{"location":"chapter_preface/about_the_book/#012","title":"0.1.2. \u00a0 \u5185\u5bb9\u7ed3\u6784","text":"<p>\u672c\u4e66\u4e3b\u8981\u5185\u5bb9\u5305\u62ec\uff1a</p> <ul> <li>\u590d\u6742\u5ea6\u5206\u6790\uff1a\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u7684\u8bc4\u4ef7\u7ef4\u5ea6\u4e0e\u65b9\u6cd5\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u3001\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u3001\u5e38\u89c1\u7c7b\u578b\u3001\u793a\u4f8b\u7b49\u3002</li> <li>\u6570\u636e\u7ed3\u6784\uff1a\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u6570\u636e\u7ed3\u6784\u7684\u5206\u7c7b\u65b9\u6cd5\u3002\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u6563\u5217\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u7684\u5b9a\u4e49\u3001\u4f18\u7f3a\u70b9\u3001\u5e38\u7528\u64cd\u4f5c\u3001\u5e38\u89c1\u7c7b\u578b\u3001\u5178\u578b\u5e94\u7528\u3001\u5b9e\u73b0\u65b9\u6cd5\u7b49\u3002</li> <li>\u7b97\u6cd5\uff1a\u641c\u7d22\u3001\u6392\u5e8f\u3001\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u7684\u5b9a\u4e49\u3001\u4f18\u7f3a\u70b9\u3001\u6548\u7387\u3001\u5e94\u7528\u573a\u666f\u3001\u89e3\u9898\u6b65\u9aa4\u3001\u793a\u4f8b\u9898\u76ee\u7b49\u3002</li> </ul> <p></p> <p> \u56fe\uff1aHello \u7b97\u6cd5\u5185\u5bb9\u7ed3\u6784 </p>"},{"location":"chapter_preface/about_the_book/#013","title":"0.1.3. \u00a0 \u81f4\u8c22","text":"<p>\u5728\u672c\u4e66\u7684\u521b\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u5f97\u5230\u4e86\u8bb8\u591a\u4eba\u7684\u5e2e\u52a9\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a</p> <ul> <li>\u611f\u8c22\u6211\u5728\u516c\u53f8\u7684\u5bfc\u5e08\u674e\u6c50\u535a\u58eb\uff0c\u5728\u4e00\u6b21\u7545\u8c08\u4e2d\u60a8\u9f13\u52b1\u6211\u201c\u5feb\u884c\u52a8\u8d77\u6765\u201d\uff0c\u575a\u5b9a\u4e86\u6211\u5199\u8fd9\u672c\u4e66\u7684\u51b3\u5fc3\u3002</li> <li>\u611f\u8c22\u6211\u7684\u5973\u670b\u53cb\u6ce1\u6ce1\u4f5c\u4e3a\u672c\u4e66\u7684\u9996\u4f4d\u8bfb\u8005\uff0c\u4ece\u7b97\u6cd5\u5c0f\u767d\u7684\u89d2\u5ea6\u63d0\u51fa\u8bb8\u591a\u5b9d\u8d35\u5efa\u8bae\uff0c\u4f7f\u5f97\u672c\u4e66\u66f4\u9002\u5408\u65b0\u624b\u9605\u8bfb\u3002</li> <li>\u611f\u8c22\u817e\u5b9d\u3001\u7426\u5b9d\u3001\u98de\u5b9d\u4e3a\u672c\u4e66\u8d77\u4e86\u4e00\u4e2a\u5bcc\u6709\u521b\u610f\u7684\u540d\u5b57\uff0c\u5524\u8d77\u5927\u5bb6\u5199\u4e0b\u7b2c\u4e00\u884c\u4ee3\u7801 \"Hello World!\" \u7684\u7f8e\u597d\u56de\u5fc6\u3002</li> <li>\u611f\u8c22\u82cf\u6f7c\u4e3a\u672c\u4e66\u8bbe\u8ba1\u4e86\u7cbe\u7f8e\u7684\u5c01\u9762\u548c LOGO\uff0c\u5e76\u5728\u6211\u7684\u5f3a\u8feb\u75c7\u4e0b\u591a\u6b21\u8010\u5fc3\u4fee\u6539\u3002</li> <li>\u611f\u8c22 @squidfunk \u63d0\u4f9b\u7684\u5199\u4f5c\u6392\u7248\u5efa\u8bae\uff0c\u4ee5\u53ca\u6770\u51fa\u7684\u5f00\u6e90\u9879\u76ee Material-for-MkDocs \u3002</li> </ul> <p>\u5728\u5199\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u9605\u8bfb\u4e86\u8bb8\u591a\u5173\u4e8e\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u6559\u6750\u548c\u6587\u7ae0\u3002\u8fd9\u4e9b\u4f5c\u54c1\u4e3a\u672c\u4e66\u63d0\u4f9b\u4e86\u4f18\u79c0\u7684\u8303\u672c\uff0c\u786e\u4fdd\u4e86\u672c\u4e66\u5185\u5bb9\u7684\u51c6\u786e\u6027\u4e0e\u54c1\u8d28\u3002\u5728\u6b64\u611f\u8c22\u6240\u6709\u8001\u5e08\u548c\u524d\u8f88\u4eec\u7684\u6770\u51fa\u8d21\u732e\uff01</p> <p>\u672c\u4e66\u5021\u5bfc\u624b\u8111\u5e76\u7528\u7684\u5b66\u4e60\u65b9\u5f0f\uff0c\u5728\u8fd9\u4e00\u70b9\u4e0a\u6df1\u53d7\u300a\u52a8\u624b\u5b66\u6df1\u5ea6\u5b66\u4e60\u300b\u7684\u542f\u53d1\u3002\u5728\u6b64\u5411\u5404\u4f4d\u8bfb\u8005\u5f3a\u70c8\u63a8\u8350\u8fd9\u672c\u4f18\u79c0\u8457\u4f5c\u3002</p> <p>\u8877\u5fc3\u611f\u8c22\u6211\u7684\u7236\u6bcd\uff0c\u6b63\u662f\u4f60\u4eec\u4e00\u76f4\u4ee5\u6765\u7684\u652f\u6301\u4e0e\u9f13\u52b1\uff0c\u8ba9\u6211\u6709\u673a\u4f1a\u505a\u8fd9\u4ef6\u5bcc\u6709\u8da3\u5473\u7684\u4e8b\u3002</p>"},{"location":"chapter_preface/suggestions/","title":"0.2. \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u4e66","text":"<p>Tip</p> <p>\u4e3a\u4e86\u83b7\u5f97\u6700\u4f73\u7684\u9605\u8bfb\u4f53\u9a8c\uff0c\u5efa\u8bae\u60a8\u901a\u8bfb\u672c\u8282\u5185\u5bb9\u3002</p>"},{"location":"chapter_preface/suggestions/#021","title":"0.2.1. \u00a0 \u884c\u6587\u98ce\u683c\u7ea6\u5b9a","text":"<ul> <li>\u6807\u9898\u540e\u6807\u6ce8 <code>*</code> \u7684\u662f\u9009\u8bfb\u7ae0\u8282\uff0c\u5185\u5bb9\u76f8\u5bf9\u56f0\u96be\u3002\u5982\u679c\u4f60\u7684\u65f6\u95f4\u6709\u9650\uff0c\u5efa\u8bae\u53ef\u4ee5\u5148\u8df3\u8fc7\u3002</li> <li>\u6587\u7ae0\u4e2d\u7684\u91cd\u8981\u540d\u8bcd\u4f1a\u7528 <code>\u300c \u300d</code> \u62ec\u53f7\u6807\u6ce8\uff0c\u4f8b\u5982 <code>\u300c\u6570\u7ec4 Array\u300d</code> \u3002\u8bf7\u52a1\u5fc5\u8bb0\u4f4f\u8fd9\u4e9b\u540d\u8bcd\uff0c\u5305\u62ec\u82f1\u6587\u7ffb\u8bd1\uff0c\u4ee5\u4fbf\u540e\u7eed\u9605\u8bfb\u6587\u732e\u65f6\u4f7f\u7528\u3002</li> <li>\u52a0\u7c97\u7684\u6587\u5b57 \u8868\u793a\u91cd\u70b9\u5185\u5bb9\u6216\u603b\u7ed3\u6027\u8bed\u53e5\uff0c\u8fd9\u7c7b\u6587\u5b57\u503c\u5f97\u7279\u522b\u5173\u6ce8\u3002</li> <li>\u4e13\u6709\u540d\u8bcd\u548c\u6709\u7279\u6307\u542b\u4e49\u7684\u8bcd\u53e5\u4f1a\u4f7f\u7528 <code>\u201c\u53cc\u5f15\u53f7\u201d</code> \u6807\u6ce8\uff0c\u4ee5\u907f\u514d\u6b67\u4e49\u3002</li> <li>\u6d89\u53ca\u5230\u7f16\u7a0b\u8bed\u8a00\u4e4b\u95f4\u4e0d\u4e00\u81f4\u7684\u540d\u8bcd\uff0c\u672c\u4e66\u5747\u4ee5 Python \u4e3a\u51c6\uff0c\u4f8b\u5982\u4f7f\u7528 \\(\\text{None}\\) \u6765\u8868\u793a\u201c\u7a7a\u201d\u3002</li> <li>\u672c\u4e66\u90e8\u5206\u653e\u5f03\u4e86\u7f16\u7a0b\u8bed\u8a00\u7684\u6ce8\u91ca\u89c4\u8303\uff0c\u4ee5\u6362\u53d6\u66f4\u52a0\u7d27\u51d1\u7684\u5185\u5bb9\u6392\u7248\u3002\u6ce8\u91ca\u4e3b\u8981\u5206\u4e3a\u4e09\u79cd\u7c7b\u578b\uff1a\u6807\u9898\u6ce8\u91ca\u3001\u5185\u5bb9\u6ce8\u91ca\u3001\u591a\u884c\u6ce8\u91ca\u3002</li> </ul> JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n</code></pre> <pre><code>/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n</code></pre> <pre><code>\"\"\"\u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49\"\"\"\n# \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n\"\"\"\n\u591a\u884c\n\u6ce8\u91ca\n\"\"\"\n</code></pre> <pre><code>/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n</code></pre> <pre><code>/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n</code></pre> <pre><code>/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n</code></pre> <pre><code>/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n</code></pre> <pre><code>/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n</code></pre> <pre><code>/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n</code></pre> <pre><code>// \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n// \u591a\u884c\n// \u6ce8\u91ca\n</code></pre> <pre><code>/* \u6807\u9898\u6ce8\u91ca\uff0c\u7528\u4e8e\u6807\u6ce8\u51fd\u6570\u3001\u7c7b\u3001\u6d4b\u8bd5\u6837\u4f8b\u7b49 */\n// \u5185\u5bb9\u6ce8\u91ca\uff0c\u7528\u4e8e\u8be6\u89e3\u4ee3\u7801\n/**\n * \u591a\u884c\n * \u6ce8\u91ca\n */\n</code></pre> <pre><code>\n</code></pre>"},{"location":"chapter_preface/suggestions/#022","title":"0.2.2. \u00a0 \u5728\u52a8\u753b\u56fe\u89e3\u4e2d\u9ad8\u6548\u5b66\u4e60","text":"<p>\u76f8\u8f83\u4e8e\u6587\u5b57\uff0c\u89c6\u9891\u548c\u56fe\u7247\u5177\u6709\u66f4\u9ad8\u7684\u4fe1\u606f\u5bc6\u5ea6\u548c\u7ed3\u6784\u5316\u7a0b\u5ea6\uff0c\u66f4\u6613\u4e8e\u7406\u89e3\u3002\u5728\u672c\u4e66\u4e2d\uff0c\u91cd\u70b9\u548c\u96be\u70b9\u77e5\u8bc6\u5c06\u4e3b\u8981\u901a\u8fc7\u52a8\u753b\u548c\u56fe\u89e3\u5f62\u5f0f\u5c55\u793a\uff0c\u800c\u6587\u5b57\u5219\u4f5c\u4e3a\u52a8\u753b\u548c\u56fe\u7247\u7684\u89e3\u91ca\u4e0e\u8865\u5145\u3002</p> <p>\u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u5982\u679c\u53d1\u73b0\u67d0\u6bb5\u5185\u5bb9\u63d0\u4f9b\u4e86\u52a8\u753b\u6216\u56fe\u89e3\uff0c\u5efa\u8bae\u4ee5\u56fe\u4e3a\u4e3b\u7ebf\uff0c\u4ee5\u6587\u5b57\uff08\u901a\u5e38\u4f4d\u4e8e\u56fe\u50cf\u4e0a\u65b9\uff09\u4e3a\u8f85\uff0c\u7efc\u5408\u4e24\u8005\u6765\u7406\u89e3\u5185\u5bb9\u3002</p> <p></p> <p> \u56fe\uff1a\u52a8\u753b\u56fe\u89e3\u793a\u4f8b </p>"},{"location":"chapter_preface/suggestions/#023","title":"0.2.3. \u00a0 \u5728\u4ee3\u7801\u5b9e\u8df5\u4e2d\u52a0\u6df1\u7406\u89e3","text":"<p>\u672c\u4e66\u7684\u914d\u5957\u4ee3\u7801\u88ab\u6258\u7ba1\u5728 GitHub \u4ed3\u5e93\u3002\u6e90\u4ee3\u7801\u9644\u6709\u6d4b\u8bd5\u6837\u4f8b\uff0c\u53ef\u4e00\u952e\u8fd0\u884c\u3002</p> <p>\u5982\u679c\u65f6\u95f4\u5141\u8bb8\uff0c\u5efa\u8bae\u4f60\u53c2\u7167\u4ee3\u7801\u81ea\u884c\u6572\u4e00\u904d\u3002\u5982\u679c\u5b66\u4e60\u65f6\u95f4\u6709\u9650\uff0c\u8bf7\u81f3\u5c11\u901a\u8bfb\u5e76\u8fd0\u884c\u6240\u6709\u4ee3\u7801\u3002</p> <p>\u4e0e\u9605\u8bfb\u4ee3\u7801\u76f8\u6bd4\uff0c\u7f16\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u5f80\u5f80\u80fd\u5e26\u6765\u66f4\u591a\u6536\u83b7\u3002\u52a8\u624b\u5b66\uff0c\u624d\u662f\u771f\u7684\u5b66\u3002</p> <p></p> <p> \u56fe\uff1a\u8fd0\u884c\u4ee3\u7801\u793a\u4f8b </p> <p>\u7b2c\u4e00\u6b65\uff1a\u5b89\u88c5\u672c\u5730\u7f16\u7a0b\u73af\u5883\u3002\u8bf7\u53c2\u7167\u9644\u5f55\u6559\u7a0b\u8fdb\u884c\u5b89\u88c5\uff0c\u5982\u679c\u5df2\u5b89\u88c5\u5219\u53ef\u8df3\u8fc7\u6b64\u6b65\u9aa4\u3002</p> <p>\u7b2c\u4e8c\u6b65\uff1a\u4e0b\u8f7d\u4ee3\u7801\u4ed3\u3002\u5982\u679c\u5df2\u7ecf\u5b89\u88c5 Git \uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u514b\u9686\u672c\u4ed3\u5e93\u3002</p> <pre><code>git clone https://github.com/krahets/hello-algo.git\n</code></pre> <p>\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u70b9\u51fb\u201cDownload ZIP\u201d\u76f4\u63a5\u4e0b\u8f7d\u4ee3\u7801\u538b\u7f29\u5305\uff0c\u7136\u540e\u5728\u672c\u5730\u89e3\u538b\u5373\u53ef\u3002</p> <p></p> <p> \u56fe\uff1a\u514b\u9686\u4ed3\u5e93\u4e0e\u4e0b\u8f7d\u4ee3\u7801 </p> <p>\u7b2c\u4e09\u6b65\uff1a\u8fd0\u884c\u6e90\u4ee3\u7801\u3002\u5982\u679c\u4ee3\u7801\u5757\u9876\u90e8\u6807\u6709\u6587\u4ef6\u540d\u79f0\uff0c\u5219\u53ef\u4ee5\u5728\u4ed3\u5e93\u7684 <code>codes</code> \u6587\u4ef6\u5939\u4e2d\u627e\u5230\u76f8\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u3002\u6e90\u4ee3\u7801\u6587\u4ef6\u5c06\u5e2e\u52a9\u4f60\u8282\u7701\u4e0d\u5fc5\u8981\u7684\u8c03\u8bd5\u65f6\u95f4\uff0c\u8ba9\u4f60\u80fd\u591f\u4e13\u6ce8\u4e8e\u5b66\u4e60\u5185\u5bb9\u3002</p> <p></p> <p> \u56fe\uff1a\u4ee3\u7801\u5757\u4e0e\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6 </p>"},{"location":"chapter_preface/suggestions/#024","title":"0.2.4. \u00a0 \u5728\u63d0\u95ee\u8ba8\u8bba\u4e2d\u5171\u540c\u6210\u957f","text":"<p>\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u8bf7\u4e0d\u8981\u201c\u60ef\u7740\u201d\u90a3\u4e9b\u6ca1\u5b66\u660e\u767d\u7684\u77e5\u8bc6\u70b9\u3002\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u63d0\u51fa\u4f60\u7684\u95ee\u9898\uff0c\u6211\u548c\u5176\u4ed6\u5c0f\u4f19\u4f34\u4eec\u5c06\u7aed\u8bda\u4e3a\u4f60\u89e3\u7b54\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u5728\u4e24\u5929\u5185\u5f97\u5230\u56de\u590d\u3002</p> <p>\u540c\u65f6\uff0c\u4e5f\u5e0c\u671b\u60a8\u80fd\u5728\u8bc4\u8bba\u533a\u591a\u82b1\u4e9b\u65f6\u95f4\u3002\u4e00\u65b9\u9762\uff0c\u60a8\u53ef\u4ee5\u4e86\u89e3\u5927\u5bb6\u9047\u5230\u7684\u95ee\u9898\uff0c\u4ece\u800c\u67e5\u6f0f\u8865\u7f3a\uff0c\u8fd9\u5c06\u6709\u52a9\u4e8e\u6fc0\u53d1\u66f4\u6df1\u5165\u7684\u601d\u8003\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u5e0c\u671b\u60a8\u80fd\u6177\u6168\u5730\u56de\u7b54\u5176\u4ed6\u5c0f\u4f19\u4f34\u7684\u95ee\u9898\u3001\u5206\u4eab\u60a8\u7684\u89c1\u89e3\uff0c\u8ba9\u5927\u5bb6\u5171\u540c\u5b66\u4e60\u548c\u8fdb\u6b65\u3002</p> <p></p> <p> \u56fe\uff1a\u8bc4\u8bba\u533a\u793a\u4f8b </p>"},{"location":"chapter_preface/suggestions/#025","title":"0.2.5. \u00a0 \u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf","text":"<p>\u4ece\u603b\u4f53\u4e0a\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8fc7\u7a0b\u5212\u5206\u4e3a\u4e09\u4e2a\u9636\u6bb5\uff1a</p> <ol> <li>\u7b97\u6cd5\u5165\u95e8\u3002\u6211\u4eec\u9700\u8981\u719f\u6089\u5404\u79cd\u6570\u636e\u7ed3\u6784\u7684\u7279\u70b9\u548c\u7528\u6cd5\uff0c\u5b66\u4e60\u4e0d\u540c\u7b97\u6cd5\u7684\u539f\u7406\u3001\u6d41\u7a0b\u3001\u7528\u9014\u548c\u6548\u7387\u7b49\u65b9\u9762\u5185\u5bb9\u3002</li> <li>\u5237\u7b97\u6cd5\u9898\u3002\u5efa\u8bae\u4ece\u70ed\u95e8\u9898\u76ee\u5f00\u5237\uff0c\u5982\u5251\u6307 Offer\u548cLeetCode Hot 100\uff0c\u5148\u79ef\u7d2f\u81f3\u5c11 100 \u9053\u9898\u76ee\uff0c\u719f\u6089\u4e3b\u6d41\u7684\u7b97\u6cd5\u95ee\u9898\u3002\u521d\u6b21\u5237\u9898\u65f6\uff0c\u201c\u77e5\u8bc6\u9057\u5fd8\u201d\u53ef\u80fd\u662f\u4e00\u4e2a\u6311\u6218\uff0c\u4f46\u8bf7\u653e\u5fc3\uff0c\u8fd9\u662f\u5f88\u6b63\u5e38\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u6309\u7167\u201c\u827e\u5bbe\u6d69\u65af\u9057\u5fd8\u66f2\u7ebf\u201d\u6765\u590d\u4e60\u9898\u76ee\uff0c\u901a\u5e38\u5728\u8fdb\u884c 3-5 \u8f6e\u7684\u91cd\u590d\u540e\uff0c\u5c31\u80fd\u5c06\u5176\u7262\u8bb0\u5728\u5fc3\u3002</li> <li>\u642d\u5efa\u77e5\u8bc6\u4f53\u7cfb\u3002\u5728\u5b66\u4e60\u65b9\u9762\uff0c\u6211\u4eec\u53ef\u4ee5\u9605\u8bfb\u7b97\u6cd5\u4e13\u680f\u6587\u7ae0\u3001\u89e3\u9898\u6846\u67b6\u548c\u7b97\u6cd5\u6559\u6750\uff0c\u4ee5\u4e0d\u65ad\u4e30\u5bcc\u77e5\u8bc6\u4f53\u7cfb\u3002\u5728\u5237\u9898\u65b9\u9762\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u91c7\u7528\u8fdb\u9636\u5237\u9898\u7b56\u7565\uff0c\u5982\u6309\u4e13\u9898\u5206\u7c7b\u3001\u4e00\u9898\u591a\u89e3\u3001\u4e00\u89e3\u591a\u9898\u7b49\uff0c\u76f8\u5173\u7684\u5237\u9898\u5fc3\u5f97\u53ef\u4ee5\u5728\u5404\u4e2a\u793e\u533a\u627e\u5230\u3002</li> </ol> <p>\u4f5c\u4e3a\u4e00\u672c\u5165\u95e8\u6559\u7a0b\uff0c\u672c\u4e66\u5185\u5bb9\u4e3b\u8981\u6db5\u76d6\u201c\u7b2c\u4e00\u9636\u6bb5\u201d\uff0c\u65e8\u5728\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5c55\u5f00\u7b2c\u4e8c\u548c\u7b2c\u4e09\u9636\u6bb5\u7684\u5b66\u4e60\u3002</p> <p></p> <p> \u56fe\uff1a\u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf </p>"},{"location":"chapter_preface/summary/","title":"0.3. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u672c\u4e66\u7684\u4e3b\u8981\u53d7\u4f17\u662f\u7b97\u6cd5\u521d\u5b66\u8005\u3002\u5982\u679c\u5df2\u6709\u4e00\u5b9a\u57fa\u7840\uff0c\u672c\u4e66\u80fd\u5e2e\u52a9\u60a8\u7cfb\u7edf\u56de\u987e\u7b97\u6cd5\u77e5\u8bc6\uff0c\u4e66\u5185\u6e90\u4ee3\u7801\u4e5f\u53ef\u4f5c\u4e3a\u201c\u5237\u9898\u5de5\u5177\u5e93\u201d\u4f7f\u7528\u3002</li> <li>\u4e66\u4e2d\u5185\u5bb9\u4e3b\u8981\u5305\u62ec\u590d\u6742\u5ea6\u5206\u6790\u3001\u6570\u636e\u7ed3\u6784\u3001\u7b97\u6cd5\u4e09\u90e8\u5206\uff0c\u6db5\u76d6\u4e86\u8be5\u9886\u57df\u7684\u5927\u90e8\u5206\u4e3b\u9898\u3002</li> <li>\u5bf9\u4e8e\u7b97\u6cd5\u65b0\u624b\uff0c\u5728\u521d\u5b66\u9636\u6bb5\u9605\u8bfb\u4e00\u672c\u5165\u95e8\u4e66\u7c4d\u81f3\u5173\u91cd\u8981\uff0c\u53ef\u4ee5\u5c11\u8d70\u8bb8\u591a\u5f2f\u8def\u3002</li> <li>\u4e66\u5185\u7684\u52a8\u753b\u548c\u56fe\u89e3\u901a\u5e38\u7528\u4e8e\u4ecb\u7ecd\u91cd\u70b9\u548c\u96be\u70b9\u77e5\u8bc6\u3002\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u5e94\u7ed9\u4e88\u8fd9\u4e9b\u5185\u5bb9\u66f4\u591a\u5173\u6ce8\u3002</li> <li>\u5b9e\u8df5\u4e43\u5b66\u4e60\u7f16\u7a0b\u4e4b\u6700\u4f73\u9014\u5f84\u3002\u5f3a\u70c8\u5efa\u8bae\u8fd0\u884c\u6e90\u4ee3\u7801\u5e76\u4eb2\u81ea\u6572\u6253\u4ee3\u7801\u3002</li> <li>\u672c\u4e66\u7f51\u9875\u7248\u7684\u6bcf\u4e2a\u7ae0\u8282\u90fd\u8bbe\u6709\u8ba8\u8bba\u533a\uff0c\u6b22\u8fce\u968f\u65f6\u5206\u4eab\u4f60\u7684\u7591\u60d1\u4e0e\u89c1\u89e3\u3002</li> </ul>"},{"location":"chapter_reference/","title":"\u53c2\u8003\u6587\u732e","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] \u4e25\u851a\u654f. \u6570\u636e\u7ed3\u6784\uff08C \u8bed\u8a00\u7248\uff09.</p> <p>[4] \u9093\u4fca\u8f89. \u6570\u636e\u7ed3\u6784\uff08C++ \u8bed\u8a00\u7248\uff0c\u7b2c\u4e09\u7248\uff09.</p> <p>[5] \u9a6c\u514b \u827e\u4f26 \u7ef4\u65af\u8457\uff0c\u9648\u8d8a\u8bd1. \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5206\u6790\uff1aJava\u8bed\u8a00\u63cf\u8ff0\uff08\u7b2c\u4e09\u7248\uff09.</p> <p>[6] \u7a0b\u6770. \u5927\u8bdd\u6570\u636e\u7ed3\u6784.</p> <p>[7] \u738b\u4e89. \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u7f8e.</p> <p>[8] Gayle Laakmann McDowell. Cracking the Coding Interview: 189 Programming Questions and Solutions (6<sup>th</sup> Edition). </p> <p>[9] Aston Zhang, et al. Dive into Deep Learning.</p>"},{"location":"chapter_searching/","title":"10. \u00a0 \u641c\u7d22","text":"<p>Abstract</p> <p>\u641c\u7d22\u662f\u4e00\u573a\u672a\u77e5\u7684\u5192\u9669\uff0c\u6211\u4eec\u6216\u8bb8\u9700\u8981\u8d70\u904d\u795e\u79d8\u7a7a\u95f4\u7684\u6bcf\u4e2a\u89d2\u843d\uff0c\u53c8\u6216\u8bb8\u53ef\u4ee5\u5feb\u901f\u9501\u5b9a\u76ee\u6807\u3002</p> <p>\u5728\u8fd9\u573a\u5bfb\u89c5\u4e4b\u65c5\u4e2d\uff0c\u6bcf\u4e00\u6b21\u63a2\u7d22\u90fd\u53ef\u80fd\u5f97\u5230\u4e00\u4e2a\u672a\u66fe\u6599\u60f3\u7684\u7b54\u6848\u3002</p>"},{"location":"chapter_searching/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>10.1 \u00a0 \u4e8c\u5206\u67e5\u627e</li> <li>10.2 \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9</li> <li>10.3 \u00a0 \u4e8c\u5206\u67e5\u627e\u8fb9\u754c</li> <li>10.4 \u00a0 \u54c8\u5e0c\u4f18\u5316\u7b56\u7565</li> <li>10.5 \u00a0 \u91cd\u8bc6\u641c\u7d22\u7b97\u6cd5</li> <li>10.6 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_searching/binary_search/","title":"10.1. \u00a0 \u4e8c\u5206\u67e5\u627e","text":"<p>\u300c\u4e8c\u5206\u67e5\u627e Binary Search\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u601d\u60f3\u7684\u9ad8\u6548\u641c\u7d22\u7b97\u6cd5\u3002\u5b83\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u6bcf\u8f6e\u51cf\u5c11\u4e00\u534a\u641c\u7d22\u8303\u56f4\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6807\u5143\u7d20\u6216\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u4e3a\u6b62\u3002</p> <p>Question</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 <code>nums</code> \uff0c\u5143\u7d20\u6309\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\uff0c\u6570\u7ec4\u4e0d\u5305\u542b\u91cd\u590d\u5143\u7d20\u3002\u8bf7\u67e5\u627e\u5e76\u8fd4\u56de\u5143\u7d20 <code>target</code> \u5728\u8be5\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002\u82e5\u6570\u7ec4\u4e0d\u5305\u542b\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002</p> <p></p> <p> \u56fe\uff1a\u4e8c\u5206\u67e5\u627e\u793a\u4f8b\u6570\u636e </p> <p>\u5bf9\u4e8e\u4e0a\u8ff0\u95ee\u9898\uff0c\u6211\u4eec\u5148\u521d\u59cb\u5316\u6307\u9488 \\(i = 0\\) \u548c \\(j = n - 1\\) \uff0c\u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u548c\u5c3e\u5143\u7d20\uff0c\u4ee3\u8868\u641c\u7d22\u533a\u95f4 \\([0, n - 1]\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u4e2d\u62ec\u53f7\u8868\u793a\u95ed\u533a\u95f4\uff0c\u5176\u5305\u542b\u8fb9\u754c\u503c\u672c\u8eab\u3002</p> <p>\u63a5\u4e0b\u6765\uff0c\u5faa\u73af\u6267\u884c\u4ee5\u4e0b\u4e24\u4e2a\u6b65\u9aa4\uff1a</p> <ol> <li>\u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m = \\lfloor {(i + j) / 2} \\rfloor\\) \uff0c\u5176\u4e2d \\(\\lfloor \\space \\rfloor\\) \u8868\u793a\u5411\u4e0b\u53d6\u6574\u64cd\u4f5c\u3002</li> <li>\u5224\u65ad <code>nums[m]</code> \u548c <code>target</code> \u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\uff1a<ol> <li>\u5f53 <code>nums[m] &lt; target</code> \u65f6\uff0c\u8bf4\u660e <code>target</code> \u5728\u533a\u95f4 \\([m + 1, j]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c \\(i = m + 1\\) \u3002</li> <li>\u5f53 <code>nums[m] &gt; target</code> \u65f6\uff0c\u8bf4\u660e <code>target</code> \u5728\u533a\u95f4 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u6267\u884c \\(j = m - 1\\) \u3002</li> <li>\u5f53 <code>nums[m] = target</code> \u65f6\uff0c\u8bf4\u660e\u627e\u5230 <code>target</code> \uff0c\u56e0\u6b64\u8fd4\u56de\u7d22\u5f15 \\(m\\) \u3002</li> </ol> </li> </ol> <p>\u82e5\u6570\u7ec4\u4e0d\u5305\u542b\u76ee\u6807\u5143\u7d20\uff0c\u641c\u7d22\u533a\u95f4\u6700\u7ec8\u4f1a\u7f29\u5c0f\u4e3a\u7a7a\u3002\u6b64\u65f6\u8fd4\u56de \\(-1\\) \u3002</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> \u56fe\uff1abinary_search_step1 </p> <p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e \\(i\\) \u548c \\(j\\) \u90fd\u662f <code>int</code> \u7c7b\u578b\uff0c\u56e0\u6b64 \\(i + j\\) \u53ef\u80fd\u4f1a\u8d85\u51fa <code>int</code> \u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u3002\u4e3a\u4e86\u907f\u514d\u5927\u6570\u8d8a\u754c\uff0c\u6211\u4eec\u901a\u5e38\u91c7\u7528\u516c\u5f0f \\(m = \\lfloor {i + (j - i) / 2} \\rfloor\\) \u6765\u8ba1\u7b97\u4e2d\u70b9\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust binary_search.java<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(int[] nums, int target) {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nint i = 0, j = nums.length - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i &gt; j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt;= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] &gt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.cpp<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(vector&lt;int&gt; &amp;nums, int target) {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nint i = 0, j = nums.size() - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i &gt; j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt;= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] &gt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.py<pre><code>def binary_search(nums: list[int], target: int) -&gt; int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09\"\"\"\n# \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\ni, j = 0, len(nums) - 1\n# \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i &gt; j \u65f6\u4e3a\u7a7a\uff09\nwhile i &lt;= j:\n# \u7406\u8bba\u4e0a Python \u7684\u6570\u5b57\u53ef\u4ee5\u65e0\u9650\u5927\uff08\u53d6\u51b3\u4e8e\u5185\u5b58\u5927\u5c0f\uff09\uff0c\u65e0\u9700\u8003\u8651\u5927\u6570\u8d8a\u754c\u95ee\u9898\nm = (i + j) // 2 # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] &lt; target:\ni = m + 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelif nums[m] &gt; target:\nj = m - 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse:\nreturn m # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn -1 # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n</code></pre> binary_search.go<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunc binarySearch(nums []int, target int) int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\ni, j := 0, len(nums)-1\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i &gt; j \u65f6\u4e3a\u7a7a\uff09\nfor i &lt;= j {\nm := i + (j-i)/2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] &lt; target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\n} else if nums[m] &gt; target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\n</code></pre> binary_search.js<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunction binarySearch(nums, target) {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nlet i = 0,\nj = nums.length - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i &gt; j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt;= j) {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\nconst m = parseInt(i + (j - i) / 2);\nif (nums[m] &lt; target)\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] &gt; target)\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse return m; // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.ts<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunction binarySearch(nums: number[], target: number): number {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nlet i = 0,\nj = nums.length - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i &gt; j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt;= j) {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nconst m = Math.floor(i + (j - i) / 2);\nif (nums[m] &lt; target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\n} else if (nums[m] &gt; target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n</code></pre> binary_search.c<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(int *nums, int len, int target) {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nint i = 0, j = len - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i &gt; j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt;= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] &gt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.cs<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(int[] nums, int target) {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nint i = 0, j = nums.Length - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i &gt; j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt;= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] &gt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.swift<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunc binarySearch(nums: [Int], target: Int) -&gt; Int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nvar i = 0\nvar j = nums.count - 1\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i &gt; j \u65f6\u4e3a\u7a7a\uff09\nwhile i &lt;= j {\nlet m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] &lt; target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\n} else if nums[m] &gt; target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\n</code></pre> binary_search.zig<pre><code>// \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09\nfn binarySearch(comptime T: type, nums: std.ArrayList(T), target: T) T {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nvar i: usize = 0;\nvar j: usize = nums.items.len - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i &gt; j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt;= j) {\nvar m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums.items[m] &lt; target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\n} else if (nums.items[m] &gt; target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn @intCast(m);\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.dart<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(List&lt;int&gt; nums, int target) {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nint i = 0, j = nums.length - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i &gt; j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt;= j) {\nint m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\n} else if (nums[m] &gt; target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.rs<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfn binary_search(nums: &amp;[i32], target: i32) -&gt; i32 {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nlet mut i = 0;\nlet mut j = nums.len() as i32 - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i &gt; j \u65f6\u4e3a\u7a7a\uff09\nwhile i &lt;= j {\nlet m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m as usize] &lt; target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\n} else if nums[m as usize] &gt; target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n} }\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> <p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002\u6bcf\u8f6e\u7f29\u5c0f\u4e00\u534a\u533a\u95f4\uff0c\u56e0\u6b64\u4e8c\u5206\u5faa\u73af\u6b21\u6570\u4e3a \\(\\log_2 n\\) \u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002\u6307\u9488 <code>i</code> , <code>j</code> \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7a7a\u95f4\u3002</p>"},{"location":"chapter_searching/binary_search/#1011","title":"10.1.1. \u00a0 \u533a\u95f4\u8868\u793a\u65b9\u6cd5","text":"<p>\u9664\u4e86\u4e0a\u8ff0\u7684\u53cc\u95ed\u533a\u95f4\u5916\uff0c\u5e38\u89c1\u7684\u533a\u95f4\u8868\u793a\u8fd8\u6709\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u533a\u95f4\uff0c\u5b9a\u4e49\u4e3a \\([0, n)\\) \uff0c\u5373\u5de6\u8fb9\u754c\u5305\u542b\u81ea\u8eab\uff0c\u53f3\u8fb9\u754c\u4e0d\u5305\u542b\u81ea\u8eab\u3002\u5728\u8be5\u8868\u793a\u4e0b\uff0c\u533a\u95f4 \\([i, j]\\) \u5728 \\(i = j\\) \u65f6\u4e3a\u7a7a\u3002</p> <p>\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u8be5\u8868\u793a\u5b9e\u73b0\u5177\u6709\u76f8\u540c\u529f\u80fd\u7684\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust binary_search.java<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearchLCRO(int[] nums, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint i = 0, j = nums.length;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt; j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] &gt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.cpp<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearchLCRO(vector&lt;int&gt; &amp;nums, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint i = 0, j = nums.size();\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt; j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] &gt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.py<pre><code>def binary_search_lcro(nums: list[int], target: int) -&gt; int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09\"\"\"\n# \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\ni, j = 0, len(nums)\n# \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile i &lt; j:\nm = (i + j) // 2 # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] &lt; target:\ni = m + 1 # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\nelif nums[m] &gt; target:\nj = m # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nelse:\nreturn m # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn -1 # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n</code></pre> binary_search.go<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunc binarySearchLCRO(nums []int, target int) int {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\ni, j := 0, len(nums)\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nfor i &lt; j {\nm := i + (j-i)/2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] &lt; target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1\n} else if nums[m] &gt; target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\n</code></pre> binary_search.js<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunction binarySearchLCRO(nums, target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nlet i = 0,\nj = nums.length;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt; j) {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u4f7f\u7528 parseInt() \u5411\u4e0b\u53d6\u6574\nconst m = parseInt(i + (j - i) / 2);\nif (nums[m] &lt; target)\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] &gt; target)\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nelse return m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.ts<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunction binarySearchLCRO(nums: number[], target: number): number {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nlet i = 0,\nj = nums.length;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt; j) {\n// \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nconst m = Math.floor(i + (j - i) / 2);\nif (nums[m] &lt; target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\n} else if (nums[m] &gt; target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n</code></pre> binary_search.c<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearchLCRO(int *nums, int len, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint i = 0, j = len;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt; j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] &gt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.cs<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearchLCRO(int[] nums, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint i = 0, j = nums.Length;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt; j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] &gt; target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.swift<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunc binarySearchLCRO(nums: [Int], target: Int) -&gt; Int {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nvar i = 0\nvar j = nums.count\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile i &lt; j {\nlet m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] &lt; target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1\n} else if nums[m] &gt; target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\n</code></pre> binary_search.zig<pre><code>// \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09\nfn binarySearchLCRO(comptime T: type, nums: std.ArrayList(T), target: T) T {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nvar i: usize = 0;\nvar j: usize = nums.items.len;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt;= j) {\nvar m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums.items[m] &lt; target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\n} else if (nums.items[m] &gt; target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn @intCast(m);\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.dart<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff09 */\nint binarySearchLCRO(List&lt;int&gt; nums, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint i = 0, j = nums.length;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i &lt; j) {\nint m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\n} else if (nums[m] &gt; target) {\n// \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\n} else {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.rs<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfn binary_search_lcro(nums: &amp;[i32], target: i32) -&gt; i32 {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nlet mut i = 0;\nlet mut j = nums.len() as i32;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile i &lt; j {\nlet m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m as usize] &lt; target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\n} else if nums[m as usize] &gt; target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m - 1;\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n} }\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5728\u4e24\u79cd\u533a\u95f4\u8868\u793a\u4e0b\uff0c\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u521d\u59cb\u5316\u3001\u5faa\u73af\u6761\u4ef6\u548c\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\u7686\u6709\u6240\u4e0d\u540c\u3002</p> <p>\u5728\u201c\u53cc\u95ed\u533a\u95f4\u201d\u8868\u793a\u6cd5\u4e2d\uff0c\u7531\u4e8e\u5de6\u53f3\u8fb9\u754c\u90fd\u88ab\u5b9a\u4e49\u4e3a\u95ed\u533a\u95f4\uff0c\u56e0\u6b64\u6307\u9488 \\(i\\) \u548c \\(j\\) \u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u8fd9\u6837\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u901a\u5e38\u91c7\u7528\u201c\u53cc\u95ed\u533a\u95f4\u201d\u7684\u5199\u6cd5\u3002</p> <p></p> <p> \u56fe\uff1a\u4e24\u79cd\u533a\u95f4\u5b9a\u4e49 </p>"},{"location":"chapter_searching/binary_search/#1012","title":"10.1.2. \u00a0 \u4f18\u70b9\u4e0e\u5c40\u9650\u6027","text":"<p>\u4e8c\u5206\u67e5\u627e\u5728\u65f6\u95f4\u548c\u7a7a\u95f4\u65b9\u9762\u90fd\u6709\u8f83\u597d\u7684\u6027\u80fd\uff1a</p> <ul> <li>\u4e8c\u5206\u67e5\u627e\u7684\u65f6\u95f4\u6548\u7387\u9ad8\u3002\u5728\u5927\u6570\u636e\u91cf\u4e0b\uff0c\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5177\u6709\u663e\u8457\u4f18\u52bf\u3002\u4f8b\u5982\uff0c\u5f53\u6570\u636e\u5927\u5c0f \\(n = 2^{20}\\) \u65f6\uff0c\u7ebf\u6027\u67e5\u627e\u9700\u8981 \\(2^{20} = 1048576\\) \u8f6e\u5faa\u73af\uff0c\u800c\u4e8c\u5206\u67e5\u627e\u4ec5\u9700 \\(\\log_2 2^{20} = 20\\) \u8f6e\u5faa\u73af\u3002</li> <li>\u4e8c\u5206\u67e5\u627e\u65e0\u9700\u989d\u5916\u7a7a\u95f4\u3002\u76f8\u8f83\u4e8e\u9700\u8981\u501f\u52a9\u989d\u5916\u7a7a\u95f4\u7684\u641c\u7d22\u7b97\u6cd5\uff08\u4f8b\u5982\u54c8\u5e0c\u67e5\u627e\uff09\uff0c\u4e8c\u5206\u67e5\u627e\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\u3002</li> </ul> <p>\u7136\u800c\uff0c\u4e8c\u5206\u67e5\u627e\u5e76\u975e\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\uff0c\u539f\u56e0\u5982\u4e0b\uff1a</p> <ul> <li>\u4e8c\u5206\u67e5\u627e\u4ec5\u9002\u7528\u4e8e\u6709\u5e8f\u6570\u636e\u3002\u82e5\u8f93\u5165\u6570\u636e\u65e0\u5e8f\uff0c\u4e3a\u4e86\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u800c\u4e13\u95e8\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u4e0d\u507f\u5931\u3002\u56e0\u4e3a\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a \\(O(n \\log n)\\) \uff0c\u6bd4\u7ebf\u6027\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u90fd\u66f4\u9ad8\u3002\u5bf9\u4e8e\u9891\u7e41\u63d2\u5165\u5143\u7d20\u7684\u573a\u666f\uff0c\u4e3a\u4fdd\u6301\u6570\u7ec4\u6709\u5e8f\u6027\uff0c\u9700\u8981\u5c06\u5143\u7d20\u63d2\u5165\u5230\u7279\u5b9a\u4f4d\u7f6e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u4e5f\u662f\u975e\u5e38\u6602\u8d35\u7684\u3002</li> <li>\u4e8c\u5206\u67e5\u627e\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u3002\u4e8c\u5206\u67e5\u627e\u9700\u8981\u8df3\u8dc3\u5f0f\uff08\u975e\u8fde\u7eed\u5730\uff09\u8bbf\u95ee\u5143\u7d20\uff0c\u800c\u5728\u94fe\u8868\u4e2d\u6267\u884c\u8df3\u8dc3\u5f0f\u8bbf\u95ee\u7684\u6548\u7387\u8f83\u4f4e\uff0c\u56e0\u6b64\u4e0d\u9002\u5408\u5e94\u7528\u5728\u94fe\u8868\u6216\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u3002</li> <li>\u5c0f\u6570\u636e\u91cf\u4e0b\uff0c\u7ebf\u6027\u67e5\u627e\u6027\u80fd\u66f4\u4f73\u3002\u5728\u7ebf\u6027\u67e5\u627e\u4e2d\uff0c\u6bcf\u8f6e\u53ea\u9700\u8981 1 \u6b21\u5224\u65ad\u64cd\u4f5c\uff1b\u800c\u5728\u4e8c\u5206\u67e5\u627e\u4e2d\uff0c\u9700\u8981 1 \u6b21\u52a0\u6cd5\u30011 \u6b21\u9664\u6cd5\u30011 ~ 3 \u6b21\u5224\u65ad\u64cd\u4f5c\u30011 \u6b21\u52a0\u6cd5\uff08\u51cf\u6cd5\uff09\uff0c\u5171 4 ~ 6 \u4e2a\u5355\u5143\u64cd\u4f5c\uff1b\u56e0\u6b64\uff0c\u5f53\u6570\u636e\u91cf \\(n\\) \u8f83\u5c0f\u65f6\uff0c\u7ebf\u6027\u67e5\u627e\u53cd\u800c\u6bd4\u4e8c\u5206\u67e5\u627e\u66f4\u5feb\u3002</li> </ul>"},{"location":"chapter_searching/binary_search_edge/","title":"10.3. \u00a0 \u4e8c\u5206\u67e5\u627e\u8fb9\u754c","text":""},{"location":"chapter_searching/binary_search_edge/#1031","title":"10.3.1. \u00a0 \u67e5\u627e\u5de6\u8fb9\u754c","text":"<p>Question</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 <code>nums</code> \uff0c\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\u3002\u8bf7\u8fd4\u56de\u6570\u7ec4\u4e2d\u6700\u5de6\u4e00\u4e2a\u5143\u7d20 <code>target</code> \u7684\u7d22\u5f15\u3002\u82e5\u6570\u7ec4\u4e2d\u4e0d\u5305\u542b\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(-1\\) \u3002</p> <p>\u56de\u5fc6\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\u7684\u65b9\u6cd5\uff0c\u641c\u7d22\u5b8c\u6210\u540e \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u4e2a <code>target</code> \uff0c\u56e0\u6b64\u67e5\u627e\u63d2\u5165\u70b9\u672c\u8d28\u4e0a\u662f\u5728\u67e5\u627e\u6700\u5de6\u4e00\u4e2a <code>target</code> \u7684\u7d22\u5f15\u3002</p> <p>\u8003\u8651\u901a\u8fc7\u67e5\u627e\u63d2\u5165\u70b9\u7684\u51fd\u6570\u5b9e\u73b0\u67e5\u627e\u5de6\u8fb9\u754c\u3002\u8bf7\u6ce8\u610f\uff0c\u6570\u7ec4\u4e2d\u53ef\u80fd\u4e0d\u5305\u542b <code>target</code> \uff0c\u6b64\u65f6\u6709\u4e24\u79cd\u53ef\u80fd\uff1a</p> <ol> <li>\u63d2\u5165\u70b9\u7684\u7d22\u5f15 \\(i\\) \u8d8a\u754c\uff1b</li> <li>\u5143\u7d20 <code>nums[i]</code> \u4e0e <code>target</code> \u4e0d\u76f8\u7b49\uff1b</li> </ol> <p>\u5f53\u9047\u5230\u4ee5\u4e0a\u4e24\u79cd\u60c5\u51b5\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de \\(-1\\) \u5373\u53ef\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust binary_search_edge.java<pre><code>/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(int[] nums, int target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nint i = binary_search_insertion.binarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i == nums.length || nums[i] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n</code></pre> binary_search_edge.cpp<pre><code>/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(vector&lt;int&gt; &amp;nums, int target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nint i = binarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i == nums.size() || nums[i] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n</code></pre> binary_search_edge.py<pre><code>def binary_search_left_edge(nums: list[int], target: int) -&gt; int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target\"\"\"\n# \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\ni = binary_search_insertion(nums, target)\n# \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif i == len(nums) or nums[i] != target:\nreturn -1\n# \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i\n</code></pre> binary_search_edge.go<pre><code>[class]{}-[func]{binarySearchLeftEdge}\n</code></pre> binary_search_edge.js<pre><code>[class]{}-[func]{binarySearchLeftEdge}\n</code></pre> binary_search_edge.ts<pre><code>[class]{}-[func]{binarySearchLeftEdge}\n</code></pre> binary_search_edge.c<pre><code>[class]{}-[func]{binarySearchLeftEdge}\n</code></pre> binary_search_edge.cs<pre><code>/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(int[] nums, int target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nint i = binary_search_insertion.binarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i == nums.Length || nums[i] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n</code></pre> binary_search_edge.swift<pre><code>/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nfunc binarySearchLeftEdge(nums: [Int], target: Int) -&gt; Int {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nlet i = binarySearchInsertion(nums: nums, target: target)\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif i == nums.count || nums[i] != target {\nreturn -1\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i\n}\n</code></pre> binary_search_edge.zig<pre><code>[class]{}-[func]{binarySearchLeftEdge}\n</code></pre> binary_search_edge.dart<pre><code>/* \u4e8c\u5206\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target */\nint binarySearchLeftEdge(List&lt;int&gt; nums, int target) {\n// \u7b49\u4ef7\u4e8e\u67e5\u627e target \u7684\u63d2\u5165\u70b9\nint i = binarySearchInsertion(nums, target);\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (i == nums.length || nums[i] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 i\nreturn i;\n}\n</code></pre> binary_search_edge.rs<pre><code>[class]{}-[func]{binary_search_left_edge}\n</code></pre>"},{"location":"chapter_searching/binary_search_edge/#1032","title":"10.3.2. \u00a0 \u67e5\u627e\u53f3\u8fb9\u754c","text":"<p>\u90a3\u4e48\u5982\u4f55\u67e5\u627e\u6700\u53f3\u4e00\u4e2a <code>target</code> \u5462\uff1f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u662f\u4fee\u6539\u4ee3\u7801\uff0c\u66ff\u6362\u5728 <code>nums[m] == target</code> \u60c5\u51b5\u4e0b\u7684\u6307\u9488\u6536\u7f29\u64cd\u4f5c\u3002\u4ee3\u7801\u5728\u6b64\u7701\u7565\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002</p> <p>\u4e0b\u9762\u6211\u4eec\u4ecb\u7ecd\u4e24\u79cd\u66f4\u52a0\u53d6\u5de7\u7684\u65b9\u6cd5\u3002</p>"},{"location":"chapter_searching/binary_search_edge/#_1","title":"\u590d\u7528\u67e5\u627e\u5de6\u8fb9\u754c","text":"<p>\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u67e5\u627e\u6700\u5de6\u5143\u7d20\u7684\u51fd\u6570\u6765\u67e5\u627e\u6700\u53f3\u5143\u7d20\uff0c\u5177\u4f53\u65b9\u6cd5\u4e3a\uff1a\u5c06\u67e5\u627e\u6700\u53f3\u4e00\u4e2a <code>target</code> \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a <code>target + 1</code>\u3002</p> <p>\u67e5\u627e\u5b8c\u6210\u540e\uff0c\u6307\u9488 \\(i\\) \u6307\u5411\u6700\u5de6\u4e00\u4e2a <code>target + 1</code>\uff08\u5982\u679c\u5b58\u5728\uff09\uff0c\u800c \\(j\\) \u6307\u5411\u6700\u53f3\u4e00\u4e2a <code>target</code> \uff0c\u56e0\u6b64\u8fd4\u56de \\(j\\) \u5373\u53ef\u3002</p> <p></p> <p> \u56fe\uff1a\u5c06\u67e5\u627e\u53f3\u8fb9\u754c\u8f6c\u5316\u4e3a\u67e5\u627e\u5de6\u8fb9\u754c </p> <p>\u8bf7\u6ce8\u610f\uff0c\u8fd4\u56de\u7684\u63d2\u5165\u70b9\u662f \\(i\\) \uff0c\u56e0\u6b64\u9700\u8981\u5c06\u5176\u51cf \\(1\\) \uff0c\u4ece\u800c\u83b7\u5f97 \\(j\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust binary_search_edge.java<pre><code>/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(int[] nums, int target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nint i = binary_search_insertion.binarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nint j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j == -1 || nums[j] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n</code></pre> binary_search_edge.cpp<pre><code>/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(vector&lt;int&gt; &amp;nums, int target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nint i = binarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nint j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j == -1 || nums[j] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n</code></pre> binary_search_edge.py<pre><code>def binary_search_right_edge(nums: list[int], target: int) -&gt; int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target\"\"\"\n# \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\ni = binary_search_insertion(nums, target + 1)\n# j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nj = i - 1\n# \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif j == -1 or nums[j] != target:\nreturn -1\n# \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j\n</code></pre> binary_search_edge.go<pre><code>[class]{}-[func]{binarySearchRightEdge}\n</code></pre> binary_search_edge.js<pre><code>[class]{}-[func]{binarySearchRightEdge}\n</code></pre> binary_search_edge.ts<pre><code>[class]{}-[func]{binarySearchRightEdge}\n</code></pre> binary_search_edge.c<pre><code>[class]{}-[func]{binarySearchRightEdge}\n</code></pre> binary_search_edge.cs<pre><code>/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(int[] nums, int target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nint i = binary_search_insertion.binarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nint j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j == -1 || nums[j] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n</code></pre> binary_search_edge.swift<pre><code>/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nfunc binarySearchRightEdge(nums: [Int], target: Int) -&gt; Int {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nlet i = binarySearchInsertion(nums: nums, target: target + 1)\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nlet j = i - 1\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif j == -1 || nums[j] != target {\nreturn -1\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j\n}\n</code></pre> binary_search_edge.zig<pre><code>[class]{}-[func]{binarySearchRightEdge}\n</code></pre> binary_search_edge.dart<pre><code>/* \u4e8c\u5206\u67e5\u627e\u6700\u53f3\u4e00\u4e2a target */\nint binarySearchRightEdge(List&lt;int&gt; nums, int target) {\n// \u8f6c\u5316\u4e3a\u67e5\u627e\u6700\u5de6\u4e00\u4e2a target + 1\nint i = binarySearchInsertion(nums, target + 1);\n// j \u6307\u5411\u6700\u53f3\u4e00\u4e2a target \uff0ci \u6307\u5411\u9996\u4e2a\u5927\u4e8e target \u7684\u5143\u7d20\nint j = i - 1;\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de -1\nif (j == -1 || nums[j] != target) {\nreturn -1;\n}\n// \u627e\u5230 target \uff0c\u8fd4\u56de\u7d22\u5f15 j\nreturn j;\n}\n</code></pre> binary_search_edge.rs<pre><code>[class]{}-[func]{binary_search_right_edge}\n</code></pre>"},{"location":"chapter_searching/binary_search_edge/#_2","title":"\u8f6c\u5316\u4e3a\u67e5\u627e\u5143\u7d20","text":"<p>\u6211\u4eec\u77e5\u9053\uff0c\u5f53\u6570\u7ec4\u4e0d\u5305\u542b <code>target</code> \u65f6\uff0c\u6700\u540e \\(i\\) , \\(j\\) \u4f1a\u5206\u522b\u6307\u5411\u9996\u4e2a\u5927\u4e8e\u3001\u5c0f\u4e8e <code>target</code> \u7684\u5143\u7d20\u3002</p> <p>\u6839\u636e\u4e0a\u8ff0\u7ed3\u8bba\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\uff0c\u7528\u4e8e\u67e5\u627e\u5de6\u53f3\u8fb9\u754c\uff1a</p> <ul> <li>\u67e5\u627e\u6700\u5de6\u4e00\u4e2a <code>target</code> \uff1a\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u627e <code>target - 0.5</code> \uff0c\u5e76\u8fd4\u56de\u6307\u9488 \\(i\\) \u3002</li> <li>\u67e5\u627e\u6700\u53f3\u4e00\u4e2a <code>target</code> \uff1a\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u627e <code>target + 0.5</code> \uff0c\u5e76\u8fd4\u56de\u6307\u9488 \\(j\\) \u3002</li> </ul> <p></p> <p> \u56fe\uff1a\u5c06\u67e5\u627e\u8fb9\u754c\u8f6c\u5316\u4e3a\u67e5\u627e\u5143\u7d20 </p> <p>\u4ee3\u7801\u5728\u6b64\u7701\u7565\uff0c\u503c\u5f97\u6ce8\u610f\u7684\u6709\uff1a</p> <ul> <li>\u7ed9\u5b9a\u6570\u7ec4\u4e0d\u5305\u542b\u5c0f\u6570\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u65e0\u9700\u5173\u5fc3\u5982\u4f55\u5904\u7406\u76f8\u7b49\u7684\u60c5\u51b5\u3002</li> <li>\u56e0\u4e3a\u8be5\u65b9\u6cd5\u5f15\u5165\u4e86\u5c0f\u6570\uff0c\u6240\u4ee5\u9700\u8981\u5c06\u51fd\u6570\u4e2d\u7684\u53d8\u91cf <code>target</code> \u6539\u4e3a\u6d6e\u70b9\u6570\u7c7b\u578b\u3002</li> </ul>"},{"location":"chapter_searching/binary_search_insertion/","title":"10.2. \u00a0 \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9","text":"<p>\u4e8c\u5206\u67e5\u627e\u4e0d\u4ec5\u53ef\u7528\u4e8e\u641c\u7d22\u76ee\u6807\u5143\u7d20\uff0c\u8fd8\u5177\u6709\u8bb8\u591a\u53d8\u79cd\u95ee\u9898\uff0c\u6bd4\u5982\u641c\u7d22\u76ee\u6807\u5143\u7d20\u7684\u63d2\u5165\u4f4d\u7f6e\u3002</p>"},{"location":"chapter_searching/binary_search_insertion/#1021","title":"10.2.1. \u00a0 \u65e0\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"<p>Question</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6709\u5e8f\u6570\u7ec4 <code>nums</code> \u548c\u4e00\u4e2a\u5143\u7d20 <code>target</code> \uff0c\u6570\u7ec4\u4e0d\u5b58\u5728\u91cd\u590d\u5143\u7d20\u3002\u73b0\u5c06 <code>target</code> \u63d2\u5165\u5230\u6570\u7ec4 <code>nums</code> \u4e2d\uff0c\u5e76\u4fdd\u6301\u5176\u6709\u5e8f\u6027\u3002\u82e5\u6570\u7ec4\u4e2d\u5df2\u5b58\u5728\u5143\u7d20 <code>target</code> \uff0c\u5219\u63d2\u5165\u5230\u5176\u5de6\u65b9\u3002\u8bf7\u8fd4\u56de\u63d2\u5165\u540e <code>target</code> \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002</p> <p></p> <p> \u56fe\uff1a\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\u793a\u4f8b\u6570\u636e </p> <p>\u5982\u679c\u60f3\u8981\u590d\u7528\u4e0a\u8282\u7684\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\uff0c\u5219\u9700\u8981\u56de\u7b54\u4ee5\u4e0b\u4e24\u4e2a\u95ee\u9898\u3002</p> <p>\u95ee\u9898\u4e00\uff1a\u5f53\u6570\u7ec4\u4e2d\u5305\u542b <code>target</code> \u65f6\uff0c\u63d2\u5165\u70b9\u7684\u7d22\u5f15\u662f\u5426\u662f\u8be5\u5143\u7d20\u7684\u7d22\u5f15\uff1f</p> <p>\u9898\u76ee\u8981\u6c42\u5c06 <code>target</code> \u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u5de6\u8fb9\uff0c\u8fd9\u610f\u5473\u7740\u65b0\u63d2\u5165\u7684 <code>target</code> \u66ff\u6362\u4e86\u539f\u6765 <code>target</code> \u7684\u4f4d\u7f6e\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u6570\u7ec4\u5305\u542b <code>target</code> \u65f6\uff0c\u63d2\u5165\u70b9\u7684\u7d22\u5f15\u5c31\u662f\u8be5 <code>target</code> \u7684\u7d22\u5f15\u3002</p> <p>\u95ee\u9898\u4e8c\uff1a\u5f53\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728 <code>target</code> \u65f6\uff0c\u63d2\u5165\u70b9\u662f\u54ea\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff1f</p> <p>\u8fdb\u4e00\u6b65\u601d\u8003\u4e8c\u5206\u67e5\u627e\u8fc7\u7a0b\uff1a\u5f53 <code>nums[m] &lt; target</code> \u65f6 \\(i\\) \u79fb\u52a8\uff0c\u8fd9\u610f\u5473\u7740\u6307\u9488 \\(i\\) \u5728\u5411\u5927\u4e8e\u7b49\u4e8e <code>target</code> \u7684\u5143\u7d20\u9760\u8fd1\u3002\u540c\u7406\uff0c\u6307\u9488 \\(j\\) \u59cb\u7ec8\u5728\u5411\u5c0f\u4e8e\u7b49\u4e8e <code>target</code> \u7684\u5143\u7d20\u9760\u8fd1\u3002</p> <p>\u56e0\u6b64\u4e8c\u5206\u7ed3\u675f\u65f6\u4e00\u5b9a\u6709\uff1a\\(i\\) \u6307\u5411\u9996\u4e2a\u5927\u4e8e <code>target</code> \u7684\u5143\u7d20\uff0c\\(j\\) \u6307\u5411\u9996\u4e2a\u5c0f\u4e8e <code>target</code> \u7684\u5143\u7d20\u3002\u6613\u5f97\u5f53\u6570\u7ec4\u4e0d\u5305\u542b <code>target</code> \u65f6\uff0c\u63d2\u5165\u7d22\u5f15\u4e3a \\(i\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust binary_search_insertion.java<pre><code>/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int[] nums, int target) {\nint i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i &lt;= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) {\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] &gt; target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n</code></pre> binary_search_insertion.cpp<pre><code>/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(vector&lt;int&gt; &amp;nums, int target) {\nint i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i &lt;= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) {\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] &gt; target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n</code></pre> binary_search_insertion.py<pre><code>def binary_search_insertion_simple(nums: list[int], target: int) -&gt; int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09\"\"\"\ni, j = 0, len(nums) - 1 # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile i &lt;= j:\nm = (i + j) // 2 # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] &lt; target:\ni = m + 1 # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelif nums[m] &gt; target:\nj = m - 1 # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse:\nreturn m # \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n# \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n</code></pre> binary_search_insertion.go<pre><code>[class]{}-[func]{binarySearchInsertionSimple}\n</code></pre> binary_search_insertion.js<pre><code>[class]{}-[func]{binarySearchInsertionSimple}\n</code></pre> binary_search_insertion.ts<pre><code>[class]{}-[func]{binarySearchInsertionSimple}\n</code></pre> binary_search_insertion.c<pre><code>[class]{}-[func]{binarySearchInsertionSimple}\n</code></pre> binary_search_insertion.cs<pre><code>/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(int[] nums, int target) {\nint i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i &lt;= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) {\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] &gt; target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n</code></pre> binary_search_insertion.swift<pre><code>/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertionSimple(nums: [Int], target: Int) -&gt; Int {\nvar i = 0, j = nums.count - 1 // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile i &lt;= j {\nlet m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] &lt; target {\ni = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if nums[m] &gt; target {\nj = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n}\n</code></pre> binary_search_insertion.zig<pre><code>[class]{}-[func]{binarySearchInsertionSimple}\n</code></pre> binary_search_insertion.dart<pre><code>/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u65e0\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertionSimple(List&lt;int&gt; nums, int target) {\nint i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i &lt;= j) {\nint m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) {\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] &gt; target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nreturn m; // \u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 m\n}\n}\n// \u672a\u627e\u5230 target \uff0c\u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n</code></pre> binary_search_insertion.rs<pre><code>[class]{}-[func]{binary_search_insertion}\n</code></pre>"},{"location":"chapter_searching/binary_search_insertion/#1022","title":"10.2.2. \u00a0 \u5b58\u5728\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5","text":"<p>Question</p> <p>\u5728\u4e0a\u4e00\u9898\u7684\u57fa\u7840\u4e0a\uff0c\u89c4\u5b9a\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\uff0c\u5176\u4f59\u4e0d\u53d8\u3002</p> <p>\u5047\u8bbe\u6570\u7ec4\u4e2d\u5b58\u5728\u591a\u4e2a <code>target</code> \uff0c\u5219\u666e\u901a\u4e8c\u5206\u67e5\u627e\u53ea\u80fd\u8fd4\u56de\u5176\u4e2d\u4e00\u4e2a <code>target</code> \u7684\u7d22\u5f15\uff0c\u800c\u65e0\u6cd5\u786e\u5b9a\u8be5\u5143\u7d20\u7684\u5de6\u8fb9\u548c\u53f3\u8fb9\u8fd8\u6709\u591a\u5c11 <code>target</code>\u3002</p> <p>\u9898\u76ee\u8981\u6c42\u5c06\u76ee\u6807\u5143\u7d20\u63d2\u5165\u5230\u6700\u5de6\u8fb9\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5de6\u4e00\u4e2a <code>target</code> \u7684\u7d22\u5f15\u3002\u521d\u6b65\u8003\u8651\u901a\u8fc7\u4ee5\u4e0b\u4e24\u6b65\u5b9e\u73b0\uff1a</p> <ol> <li>\u6267\u884c\u4e8c\u5206\u67e5\u627e\uff0c\u5f97\u5230\u4efb\u610f\u4e00\u4e2a <code>target</code> \u7684\u7d22\u5f15\uff0c\u8bb0\u4e3a \\(k\\) \u3002</li> <li>\u4ece\u7d22\u5f15 \\(k\\) \u5f00\u59cb\uff0c\u5411\u5de6\u8fdb\u884c\u7ebf\u6027\u904d\u5386\uff0c\u5f53\u627e\u5230\u6700\u5de6\u8fb9\u7684 <code>target</code> \u65f6\u8fd4\u56de\u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u7ebf\u6027\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u63d2\u5165\u70b9 </p> <p>\u6b64\u65b9\u6cd5\u867d\u7136\u53ef\u7528\uff0c\u4f46\u5176\u5305\u542b\u7ebf\u6027\u67e5\u627e\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5f53\u6570\u7ec4\u4e2d\u5b58\u5728\u5f88\u591a\u91cd\u590d\u7684 <code>target</code> \u65f6\uff0c\u8be5\u65b9\u6cd5\u6548\u7387\u5f88\u4f4e\u3002</p> <p>\u73b0\u8003\u8651\u4fee\u6539\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\u3002\u6574\u4f53\u6d41\u7a0b\u4e0d\u53d8\uff0c\u6bcf\u8f6e\u5148\u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m\\) \uff0c\u518d\u5224\u65ad <code>target</code> \u548c <code>nums[m]</code> \u5927\u5c0f\u5173\u7cfb\uff1a</p> <ol> <li>\u5f53 <code>nums[m] &lt; target</code> \u6216 <code>nums[m] &gt; target</code> \u65f6\uff0c\u8bf4\u660e\u8fd8\u6ca1\u6709\u627e\u5230 <code>target</code> \uff0c\u56e0\u6b64\u91c7\u7528\u666e\u901a\u4e8c\u5206\u67e5\u627e\u7684\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(i\\) \u548c \\(j\\) \u5411 <code>target</code> \u9760\u8fd1\u3002</li> <li>\u5f53 <code>nums[m] == target</code> \u65f6\uff0c\u8bf4\u660e\u5c0f\u4e8e <code>target</code> \u7684\u5143\u7d20\u5728\u533a\u95f4 \\([i, m - 1]\\) \u4e2d\uff0c\u56e0\u6b64\u91c7\u7528 \\(j = m - 1\\) \u6765\u7f29\u5c0f\u533a\u95f4\uff0c\u4ece\u800c\u4f7f\u6307\u9488 \\(j\\) \u5411\u5c0f\u4e8e <code>target</code> \u7684\u5143\u7d20\u9760\u8fd1\u3002</li> </ol> <p>\u5faa\u73af\u5b8c\u6210\u540e\uff0c\\(i\\) \u6307\u5411\u6700\u5de6\u8fb9\u7684 <code>target</code> \uff0c\\(j\\) \u6307\u5411\u9996\u4e2a\u5c0f\u4e8e <code>target</code> \u7684\u5143\u7d20\uff0c\u56e0\u6b64\u7d22\u5f15 \\(i\\) \u5c31\u662f\u63d2\u5165\u70b9\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> \u56fe\uff1a\u4e8c\u5206\u67e5\u627e\u91cd\u590d\u5143\u7d20\u7684\u63d2\u5165\u70b9\u7684\u6b65\u9aa4 </p> <p>\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5224\u65ad\u5206\u652f <code>nums[m] &gt; target</code> \u548c <code>nums[m] == target</code> \u7684\u64cd\u4f5c\u76f8\u540c\uff0c\u56e0\u6b64\u4e24\u8005\u53ef\u4ee5\u5408\u5e76\u3002</p> <p>\u5373\u4fbf\u5982\u6b64\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5c06\u5224\u65ad\u6761\u4ef6\u4fdd\u6301\u5c55\u5f00\uff0c\u56e0\u4e3a\u5176\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u3001\u53ef\u8bfb\u6027\u66f4\u597d\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust binary_search_insertion.java<pre><code>/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(int[] nums, int target) {\nint i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i &lt;= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) {\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] &gt; target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\n}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n</code></pre> binary_search_insertion.cpp<pre><code>/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(vector&lt;int&gt; &amp;nums, int target) {\nint i = 0, j = nums.size() - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i &lt;= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) {\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] &gt; target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\n}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n</code></pre> binary_search_insertion.py<pre><code>def binary_search_insertion(nums: list[int], target: int) -&gt; int:\n\"\"\"\u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09\"\"\"\ni, j = 0, len(nums) - 1 # \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile i &lt;= j:\nm = (i + j) // 2 # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] &lt; target:\ni = m + 1 # target \u5728\u533a\u95f4 [m+1, j] \u4e2d\nelif nums[m] &gt; target:\nj = m - 1 # target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nelse:\nj = m - 1 # \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n# \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n</code></pre> binary_search_insertion.go<pre><code>[class]{}-[func]{binarySearchInsertion}\n</code></pre> binary_search_insertion.js<pre><code>[class]{}-[func]{binarySearchInsertion}\n</code></pre> binary_search_insertion.ts<pre><code>[class]{}-[func]{binarySearchInsertion}\n</code></pre> binary_search_insertion.c<pre><code>[class]{}-[func]{binarySearchInsertion}\n</code></pre> binary_search_insertion.cs<pre><code>/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(int[] nums, int target) {\nint i = 0, j = nums.Length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i &lt;= j) {\nint m = i + (j - i) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) {\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] &gt; target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\n}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n</code></pre> binary_search_insertion.swift<pre><code>/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nfunc binarySearchInsertion(nums: [Int], target: Int) -&gt; Int {\nvar i = 0, j = nums.count - 1 // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile i &lt;= j {\nlet m = i + (j - i) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] &lt; target {\ni = m + 1 // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if nums[m] &gt; target {\nj = m - 1 // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nj = m - 1 // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\n}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i\n}\n</code></pre> binary_search_insertion.zig<pre><code>[class]{}-[func]{binarySearchInsertion}\n</code></pre> binary_search_insertion.dart<pre><code>/* \u4e8c\u5206\u67e5\u627e\u63d2\u5165\u70b9\uff08\u5b58\u5728\u91cd\u590d\u5143\u7d20\uff09 */\nint binarySearchInsertion(List&lt;int&gt; nums, int target) {\nint i = 0, j = nums.length - 1; // \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1]\nwhile (i &lt;= j) {\nint m = i + (j - i) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] &lt; target) {\ni = m + 1; // target \u5728\u533a\u95f4 [m+1, j] \u4e2d\n} else if (nums[m] &gt; target) {\nj = m - 1; // target \u5728\u533a\u95f4 [i, m-1] \u4e2d\n} else {\nj = m - 1; // \u9996\u4e2a\u5c0f\u4e8e target \u7684\u5143\u7d20\u5728\u533a\u95f4 [i, m-1] \u4e2d\n}\n}\n// \u8fd4\u56de\u63d2\u5165\u70b9 i\nreturn i;\n}\n</code></pre> binary_search_insertion.rs<pre><code>[class]{}-[func]{binary_search_insertion}\n</code></pre> <p>Tip</p> <p>\u672c\u8282\u7684\u4ee3\u7801\u90fd\u662f\u201c\u53cc\u95ed\u533a\u95f4\u201d\u5199\u6cd5\u3002\u6709\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u5199\u6cd5\u3002</p> <p>\u603b\u7684\u6765\u770b\uff0c\u4e8c\u5206\u67e5\u627e\u65e0\u975e\u5c31\u662f\u7ed9\u6307\u9488 \\(i\\) , \\(j\\) \u5206\u522b\u8bbe\u5b9a\u641c\u7d22\u76ee\u6807\uff0c\u76ee\u6807\u53ef\u80fd\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u5143\u7d20\uff08\u4f8b\u5982 <code>target</code> \uff09\uff0c\u4e5f\u53ef\u80fd\u662f\u4e00\u4e2a\u5143\u7d20\u8303\u56f4\uff08\u4f8b\u5982\u5c0f\u4e8e <code>target</code> \u7684\u5143\u7d20\uff09\u3002</p> <p>\u5728\u4e0d\u65ad\u7684\u5faa\u73af\u4e8c\u5206\u4e2d\uff0c\u6307\u9488 \\(i\\) , \\(j\\) \u90fd\u9010\u6e10\u903c\u8fd1\u9884\u5148\u8bbe\u5b9a\u7684\u76ee\u6807\u3002\u6700\u7ec8\uff0c\u5b83\u4eec\u6216\u662f\u6210\u529f\u627e\u5230\u7b54\u6848\uff0c\u6216\u662f\u8d8a\u8fc7\u8fb9\u754c\u540e\u505c\u6b62\u3002</p>"},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.4. \u00a0 \u54c8\u5e0c\u4f18\u5316\u7b56\u7565","text":"<p>\u5728\u7b97\u6cd5\u9898\u4e2d\uff0c\u6211\u4eec\u5e38\u901a\u8fc7\u5c06\u7ebf\u6027\u67e5\u627e\u66ff\u6362\u4e3a\u54c8\u5e0c\u67e5\u627e\u6765\u964d\u4f4e\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u7b97\u6cd5\u9898\u6765\u52a0\u6df1\u7406\u89e3\u3002</p> <p>Question</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 <code>nums</code> \u548c\u4e00\u4e2a\u76ee\u6807\u5143\u7d20 <code>target</code> \uff0c\u8bf7\u5728\u6570\u7ec4\u4e2d\u641c\u7d22\u201c\u548c\u201d\u4e3a <code>target</code> \u7684\u4e24\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6570\u7ec4\u7d22\u5f15\u3002\u8fd4\u56de\u4efb\u610f\u4e00\u4e2a\u89e3\u5373\u53ef\u3002</p>"},{"location":"chapter_searching/replace_linear_by_hashing/#1041","title":"10.4.1. \u00a0 \u7ebf\u6027\u67e5\u627e\uff1a\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4","text":"<p>\u8003\u8651\u76f4\u63a5\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\u3002\u5f00\u542f\u4e00\u4e2a\u4e24\u5c42\u5faa\u73af\uff0c\u5728\u6bcf\u8f6e\u4e2d\u5224\u65ad\u4e24\u4e2a\u6574\u6570\u7684\u548c\u662f\u5426\u4e3a <code>target</code> \uff0c\u82e5\u662f\uff0c\u5219\u8fd4\u56de\u5b83\u4eec\u7684\u7d22\u5f15\u3002</p> <p></p> <p> \u56fe\uff1a\u7ebf\u6027\u67e5\u627e\u6c42\u89e3\u4e24\u6570\u4e4b\u548c </p> JavaC++PythonGoJSTSCC#SwiftZigDartRust two_sum.java<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] twoSumBruteForce(int[] nums, int target) {\nint size = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (int i = 0; i &lt; size - 1; i++) {\nfor (int j = i + 1; j &lt; size; j++) {\nif (nums[i] + nums[j] == target)\nreturn new int[] { i, j };\n}\n}\nreturn new int[0];\n}\n</code></pre> two_sum.cpp<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nvector&lt;int&gt; twoSumBruteForce(vector&lt;int&gt; &amp;nums, int target) {\nint size = nums.size();\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (int i = 0; i &lt; size - 1; i++) {\nfor (int j = i + 1; j &lt; size; j++) {\nif (nums[i] + nums[j] == target)\nreturn {i, j};\n}\n}\nreturn {};\n}\n</code></pre> 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\u679a\u4e3e\"\"\"\n# \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor i in range(len(nums) - 1):\nfor j in range(i + 1, len(nums)):\nif nums[i] + nums[j] == target:\nreturn [i, j]\nreturn []\n</code></pre> two_sum.go<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunc twoSumBruteForce(nums []int, target int) []int {\nsize := len(nums)\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor i := 0; i &lt; size-1; i++ {\nfor j := i + 1; i &lt; size; j++ {\nif nums[i]+nums[j] == target {\nreturn []int{i, j}\n}\n}\n}\nreturn nil\n}\n</code></pre> two_sum.js<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums, target) {\nconst n = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (let i = 0; i &lt; n; i++) {\nfor (let j = i + 1; j &lt; n; j++) {\nif (nums[i] + nums[j] === target) {\nreturn [i, j];\n}\n}\n}\nreturn [];\n}\n</code></pre> two_sum.ts<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums: number[], target: number): number[] {\nconst n = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (let i = 0; i &lt; n; i++) {\nfor (let j = i + 1; j &lt; n; j++) {\nif (nums[i] + nums[j] === target) {\nreturn [i, j];\n}\n}\n}\nreturn [];\n}\n</code></pre> two_sum.c<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {\nfor (int i = 0; i &lt; numsSize; ++i) {\nfor (int j = i + 1; j &lt; numsSize; ++j) {\nif (nums[i] + nums[j] == target) {\nint *res = malloc(sizeof(int) * 2);\nres[0] = i, res[1] = j;\n*returnSize = 2;\nreturn res;\n}\n}\n}\n*returnSize = 0;\nreturn NULL;\n}\n</code></pre> two_sum.cs<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] twoSumBruteForce(int[] nums, int target) {\nint size = nums.Length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (int i = 0; i &lt; size - 1; i++) {\nfor (int j = i + 1; j &lt; size; j++) {\nif (nums[i] + nums[j] == target)\nreturn new int[] { i, j };\n}\n}\nreturn Array.Empty&lt;int&gt;();\n}\n</code></pre> two_sum.swift<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunc twoSumBruteForce(nums: [Int], target: Int) -&gt; [Int] {\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor i in nums.indices.dropLast() {\nfor j in nums.indices.dropFirst(i + 1) {\nif nums[i] + nums[j] == target {\nreturn [i, j]\n}\n}\n}\nreturn [0]\n}\n</code></pre> two_sum.zig<pre><code>// \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e\nfn twoSumBruteForce(nums: []i32, target: i32) ?[2]i32 {\nvar size: usize = nums.len;\nvar i: usize = 0;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nwhile (i &lt; size - 1) : (i += 1) {\nvar j = i + 1;\nwhile (j &lt; size) : (j += 1) {\nif (nums[i] + nums[j] == target) {\nreturn [_]i32{@intCast(i), @intCast(j)};\n}\n}\n}\nreturn null;\n}\n</code></pre> two_sum.dart<pre><code>/* \u65b9\u6cd5\u4e00\uff1a \u66b4\u529b\u679a\u4e3e */\nList&lt;int&gt; twoSumBruteForce(List&lt;int&gt; nums, int target) {\nint size = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (var i = 0; i &lt; size - 1; i++) {\nfor (var j = i + 1; j &lt; size; j++) {\nif (nums[i] + nums[j] == target) return [i, j];\n}\n}\nreturn [0];\n}\n</code></pre> two_sum.rs<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\npub fn two_sum_brute_force(nums: &amp;Vec&lt;i32&gt;, target: i32) -&gt; Option&lt;Vec&lt;i32&gt;&gt; {\nlet size = nums.len();\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor i in 0..size - 1 {\nfor j in i + 1..size {\nif nums[i] + nums[j] == target {\nreturn Some(vec![i as i32, j as i32]);\n}\n}\n}\nNone\n}\n</code></pre> <p>\u6b64\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u5728\u5927\u6570\u636e\u91cf\u4e0b\u975e\u5e38\u8017\u65f6\u3002</p>"},{"location":"chapter_searching/replace_linear_by_hashing/#1042","title":"10.4.2. \u00a0 \u54c8\u5e0c\u67e5\u627e\uff1a\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4","text":"<p>\u8003\u8651\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u952e\u503c\u5bf9\u5206\u522b\u4e3a\u6570\u7ec4\u5143\u7d20\u548c\u5143\u7d20\u7d22\u5f15\u3002\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u6bcf\u8f6e\u6267\u884c\uff1a</p> <ol> <li>\u5224\u65ad\u6570\u5b57 <code>target - nums[i]</code> \u662f\u5426\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u82e5\u662f\u5219\u76f4\u63a5\u8fd4\u56de\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u3002</li> <li>\u5c06\u952e\u503c\u5bf9 <code>nums[i]</code> \u548c\u7d22\u5f15 <code>i</code> \u6dfb\u52a0\u8fdb\u54c8\u5e0c\u8868\u3002</li> </ol> &lt;1&gt;&lt;2&gt;&lt;3&gt; <p></p> <p></p> <p></p> <p> \u56fe\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868\u6c42\u89e3\u4e24\u6570\u4e4b\u548c </p> <p>\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff0c\u4ec5\u9700\u5355\u5c42\u5faa\u73af\u5373\u53ef\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust two_sum.java<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] twoSumHashTable(int[] nums, int target) {\nint size = nums.length;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nMap&lt;Integer, Integer&gt; dic = new HashMap&lt;&gt;();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (int i = 0; i &lt; size; i++) {\nif (dic.containsKey(target - nums[i])) {\nreturn new int[] { dic.get(target - nums[i]), i };\n}\ndic.put(nums[i], i);\n}\nreturn new int[0];\n}\n</code></pre> two_sum.cpp<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nvector&lt;int&gt; twoSumHashTable(vector&lt;int&gt; &amp;nums, int target) {\nint size = nums.size();\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nunordered_map&lt;int, int&gt; dic;\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (int i = 0; i &lt; size; i++) {\nif (dic.find(target - nums[i]) != dic.end()) {\nreturn {dic[target - nums[i]], i};\n}\ndic.emplace(nums[i], i);\n}\nreturn {};\n}\n</code></pre> two_sum.py<pre><code>def two_sum_hash_table(nums: list[int], target: int) -&gt; list[int]:\n\"\"\"\u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868\"\"\"\n# \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\ndic = {}\n# \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor i in range(len(nums)):\nif target - nums[i] in dic:\nreturn [dic[target - nums[i]], i]\ndic[nums[i]] = i\nreturn []\n</code></pre> two_sum.go<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunc twoSumHashTable(nums []int, target int) []int {\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nhashTable := map[int]int{}\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor idx, val := range nums {\nif preIdx, ok := hashTable[target-val]; ok {\nreturn []int{preIdx, idx}\n}\nhashTable[val] = idx\n}\nreturn nil\n}\n</code></pre> two_sum.js<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunction twoSumHashTable(nums, target) {\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nlet m = {};\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (let i = 0; i &lt; nums.length; i++) {\nif (m[target - nums[i]] !== undefined) {\nreturn [m[target-nums[i]], i];\n} else {\nm[nums[i]] = i;\n}\n}\nreturn [];\n}\n</code></pre> two_sum.ts<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunction twoSumHashTable(nums: number[], target: number): number[] {\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nlet m: Map&lt;number, number&gt; = new Map();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (let i = 0; i &lt; nums.length; i++) {\nlet index = m.get(target - nums[i]);\nif (index !== undefined) {\nreturn [index, i];\n} else {\nm.set(nums[i], i);\n}\n}\nreturn [];\n}\n</code></pre> two_sum.c<pre><code>/* \u54c8\u5e0c\u8868 */\nstruct hashTable {\nint key;\nint val;\nUT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n};\ntypedef struct hashTable hashTable;\n/* \u54c8\u5e0c\u8868\u67e5\u8be2 */\nhashTable *find(hashTable *h, int key) {\nhashTable *tmp;\nHASH_FIND_INT(h, &amp;key, tmp);\nreturn tmp;\n}\n/* \u54c8\u5e0c\u8868\u5143\u7d20\u63d2\u5165 */\nvoid insert(hashTable *h, int key, int val) {\nhashTable *t = find(h, key);\nif (t == NULL) {\nhashTable *tmp = malloc(sizeof(hashTable));\ntmp-&gt;key = key, tmp-&gt;val = val;\nHASH_ADD_INT(h, key, tmp);\n} else {\nt-&gt;val = val;\n}\n}\n/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {\nhashTable *hashtable = NULL;\nfor (int i = 0; i &lt; numsSize; i++) {\nhashTable *t = find(hashtable, target - nums[i]);\nif (t != NULL) {\nint *res = malloc(sizeof(int) * 2);\nres[0] = t-&gt;val, res[1] = i;\n*returnSize = 2;\nreturn res;\n}\ninsert(hashtable, nums[i], i);\n}\n*returnSize = 0;\nreturn NULL;\n}\n</code></pre> two_sum.cs<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] twoSumHashTable(int[] nums, int target) {\nint size = nums.Length;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nDictionary&lt;int, int&gt; dic = new();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (int i = 0; i &lt; size; i++) {\nif (dic.ContainsKey(target - nums[i])) {\nreturn new int[] { dic[target - nums[i]], i };\n}\ndic.Add(nums[i], i);\n}\nreturn Array.Empty&lt;int&gt;();\n}\n</code></pre> two_sum.swift<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunc twoSumHashTable(nums: [Int], target: Int) -&gt; [Int] {\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nvar dic: [Int: Int] = [:]\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor i in nums.indices {\nif let j = dic[target - nums[i]] {\nreturn [j, i]\n}\ndic[nums[i]] = i\n}\nreturn [0]\n}\n</code></pre> two_sum.zig<pre><code>// \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868\nfn twoSumHashTable(nums: []i32, target: i32) !?[2]i32 {\nvar size: usize = nums.len;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nvar dic = std.AutoHashMap(i32, i32).init(std.heap.page_allocator);\ndefer dic.deinit();\nvar i: usize = 0;\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nwhile (i &lt; size) : (i += 1) {\nif (dic.contains(target - nums[i])) {\nreturn [_]i32{dic.get(target - nums[i]).?, @intCast(i)};\n}\ntry dic.put(nums[i], @intCast(i));\n}\nreturn null;\n}\n</code></pre> two_sum.dart<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a \u8f85\u52a9\u54c8\u5e0c\u8868 */\nList&lt;int&gt; twoSumHashTable(List&lt;int&gt; nums, int target) {\nint size = nums.length;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nMap&lt;int, int&gt; dic = HashMap();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (var i = 0; i &lt; size; i++) {\nif (dic.containsKey(target - nums[i])) {\nreturn [dic[target - nums[i]]!, i];\n}\ndic.putIfAbsent(nums[i], () =&gt; i);\n}\nreturn [0];\n}\n</code></pre> two_sum.rs<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\npub fn two_sum_hash_table(nums: &amp;Vec&lt;i32&gt;, target: i32) -&gt; Option&lt;Vec&lt;i32&gt;&gt; {\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nlet mut dic = HashMap::new();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (i, num) in nums.iter().enumerate() {\nmatch dic.get(&amp;(target - num)) {\nSome(v) =&gt; return Some(vec![*v as i32, i as i32]),\nNone =&gt; dic.insert(num, i as i32)\n};\n}\nNone\n}\n</code></pre> <p>\u6b64\u65b9\u6cd5\u901a\u8fc7\u54c8\u5e0c\u67e5\u627e\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n^2)\\) \u964d\u4f4e\u81f3 \\(O(n)\\) \uff0c\u5927\u5e45\u63d0\u5347\u8fd0\u884c\u6548\u7387\u3002</p> <p>\u7531\u4e8e\u9700\u8981\u7ef4\u62a4\u4e00\u4e2a\u989d\u5916\u7684\u54c8\u5e0c\u8868\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002\u5c3d\u7ba1\u5982\u6b64\uff0c\u8be5\u65b9\u6cd5\u7684\u6574\u4f53\u65f6\u7a7a\u6548\u7387\u66f4\u4e3a\u5747\u8861\uff0c\u56e0\u6b64\u5b83\u662f\u672c\u9898\u7684\u6700\u4f18\u89e3\u6cd5\u3002</p>"},{"location":"chapter_searching/searching_algorithm_revisited/","title":"10.5. \u00a0 \u91cd\u8bc6\u641c\u7d22\u7b97\u6cd5","text":"<p>\u300c\u641c\u7d22\u7b97\u6cd5 Searching Algorithm\u300d\u7528\u4e8e\u5728\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6811\u6216\u56fe\uff09\u4e2d\u641c\u7d22\u4e00\u4e2a\u6216\u4e00\u7ec4\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002</p> <p>\u6839\u636e\u5b9e\u73b0\u601d\u8def\uff0c\u641c\u7d22\u7b97\u6cd5\u603b\u4f53\u53ef\u5206\u4e3a\u4e24\u79cd\uff1a</p> <ul> <li>\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u6765\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\uff0c\u4f8b\u5982\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6811\u548c\u56fe\u7684\u904d\u5386\u7b49\u3002</li> <li>\u5229\u7528\u6570\u636e\u7ec4\u7ec7\u7ed3\u6784\u6216\u6570\u636e\u5305\u542b\u7684\u5148\u9a8c\u4fe1\u606f\uff0c\u5b9e\u73b0\u9ad8\u6548\u5143\u7d20\u67e5\u627e\uff0c\u4f8b\u5982\u4e8c\u5206\u67e5\u627e\u3001\u54c8\u5e0c\u67e5\u627e\u548c\u4e8c\u53c9\u641c\u7d22\u6811\u67e5\u627e\u7b49\u3002</li> </ul> <p>\u4e0d\u96be\u53d1\u73b0\uff0c\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u90fd\u5df2\u5728\u524d\u9762\u7684\u7ae0\u8282\u4e2d\u4ecb\u7ecd\u8fc7\uff0c\u56e0\u6b64\u641c\u7d22\u7b97\u6cd5\u5bf9\u4e8e\u6211\u4eec\u6765\u8bf4\u5e76\u4e0d\u964c\u751f\u3002\u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4ece\u66f4\u52a0\u7cfb\u7edf\u7684\u89c6\u89d2\u5207\u5165\uff0c\u91cd\u65b0\u5ba1\u89c6\u641c\u7d22\u7b97\u6cd5\u3002</p>"},{"location":"chapter_searching/searching_algorithm_revisited/#1051","title":"10.5.1. \u00a0 \u66b4\u529b\u641c\u7d22","text":"<p>\u66b4\u529b\u641c\u7d22\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u7684\u6bcf\u4e2a\u5143\u7d20\u6765\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002</p> <ul> <li>\u300c\u7ebf\u6027\u641c\u7d22\u300d\u9002\u7528\u4e8e\u6570\u7ec4\u548c\u94fe\u8868\u7b49\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002\u5b83\u4ece\u6570\u636e\u7ed3\u6784\u7684\u4e00\u7aef\u5f00\u59cb\uff0c\u9010\u4e2a\u8bbf\u95ee\u5143\u7d20\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u5143\u7d20\u6216\u5230\u8fbe\u53e6\u4e00\u7aef\u4ecd\u6ca1\u6709\u627e\u5230\u76ee\u6807\u5143\u7d20\u4e3a\u6b62\u3002</li> <li>\u300c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u300d\u662f\u56fe\u548c\u6811\u7684\u4e24\u79cd\u904d\u5386\u7b56\u7565\u3002\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u4ece\u521d\u59cb\u8282\u70b9\u5f00\u59cb\u9010\u5c42\u641c\u7d22\uff0c\u7531\u8fd1\u53ca\u8fdc\u5730\u8bbf\u95ee\u5404\u4e2a\u8282\u70b9\u3002\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u662f\u4ece\u521d\u59cb\u8282\u70b9\u5f00\u59cb\uff0c\u6cbf\u7740\u4e00\u6761\u8def\u5f84\u8d70\u5230\u5934\u4e3a\u6b62\uff0c\u518d\u56de\u6eaf\u5e76\u5c1d\u8bd5\u5176\u4ed6\u8def\u5f84\uff0c\u76f4\u5230\u904d\u5386\u5b8c\u6574\u4e2a\u6570\u636e\u7ed3\u6784\u3002</li> </ul> <p>\u66b4\u529b\u641c\u7d22\u7684\u4f18\u70b9\u662f\u7b80\u5355\u4e14\u901a\u7528\u6027\u597d\uff0c\u65e0\u9700\u5bf9\u6570\u636e\u505a\u9884\u5904\u7406\u548c\u501f\u52a9\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\u3002</p> <p>\u7136\u800c\uff0c\u6b64\u7c7b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u5143\u7d20\u6570\u91cf\uff0c\u56e0\u6b64\u5728\u6570\u636e\u91cf\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\u6027\u80fd\u8f83\u5dee\u3002</p>"},{"location":"chapter_searching/searching_algorithm_revisited/#1052","title":"10.5.2. \u00a0 \u81ea\u9002\u5e94\u641c\u7d22","text":"<p>\u81ea\u9002\u5e94\u641c\u7d22\u5229\u7528\u6570\u636e\u7684\u7279\u6709\u5c5e\u6027\uff08\u4f8b\u5982\u6709\u5e8f\u6027\uff09\u6765\u4f18\u5316\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ece\u800c\u66f4\u9ad8\u6548\u5730\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002</p> <ul> <li>\u300c\u4e8c\u5206\u67e5\u627e\u300d\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\u5b9e\u73b0\u9ad8\u6548\u67e5\u627e\uff0c\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u3002</li> <li>\u300c\u54c8\u5e0c\u67e5\u627e\u300d\u5229\u7528\u54c8\u5e0c\u8868\u5c06\u641c\u7d22\u6570\u636e\u548c\u76ee\u6807\u6570\u636e\u5efa\u7acb\u4e3a\u952e\u503c\u5bf9\u6620\u5c04\uff0c\u4ece\u800c\u5b9e\u73b0\u67e5\u8be2\u64cd\u4f5c\u3002</li> <li>\u300c\u6811\u67e5\u627e\u300d\u5728\u7279\u5b9a\u7684\u6811\u7ed3\u6784\uff08\u4f8b\u5982\u4e8c\u53c9\u641c\u7d22\u6811\uff09\u4e2d\uff0c\u57fa\u4e8e\u6bd4\u8f83\u8282\u70b9\u503c\u6765\u5feb\u901f\u6392\u9664\u8282\u70b9\uff0c\u4ece\u800c\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002</li> </ul> <p>\u6b64\u7c7b\u7b97\u6cd5\u7684\u4f18\u70b9\u662f\u6548\u7387\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe\u5230 \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \u3002</p> <p>\u7136\u800c\uff0c\u4f7f\u7528\u8fd9\u4e9b\u7b97\u6cd5\u5f80\u5f80\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4e8c\u5206\u67e5\u627e\u9700\u8981\u9884\u5148\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u54c8\u5e0c\u67e5\u627e\u548c\u6811\u67e5\u627e\u90fd\u9700\u8981\u501f\u52a9\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7ef4\u62a4\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u4e5f\u9700\u8981\u989d\u5916\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u5f00\u652f\u3002</p> <p>Note</p> <p>\u81ea\u9002\u5e94\u641c\u7d22\u7b97\u6cd5\u5e38\u88ab\u79f0\u4e3a\u67e5\u627e\u7b97\u6cd5\uff0c\u4e3b\u8981\u5173\u6ce8\u5728\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u4e2d\u5feb\u901f\u68c0\u7d22\u76ee\u6807\u5143\u7d20\u3002</p>"},{"location":"chapter_searching/searching_algorithm_revisited/#1053","title":"10.5.3. \u00a0 \u641c\u7d22\u65b9\u6cd5\u9009\u53d6","text":"<p>\u7ed9\u5b9a\u5927\u5c0f\u4e3a \\(n\\) \u7684\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7ebf\u6027\u641c\u7d22\u3001\u4e8c\u5206\u67e5\u627e\u3001\u6811\u67e5\u627e\u3001\u54c8\u5e0c\u67e5\u627e\u7b49\u591a\u79cd\u65b9\u6cd5\u5728\u8be5\u6570\u636e\u4e2d\u641c\u7d22\u76ee\u6807\u5143\u7d20\u3002\u5404\u4e2a\u65b9\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\u56fe\u6240\u793a\u3002</p> <p></p> <p> \u56fe\uff1a\u591a\u79cd\u641c\u7d22\u7b56\u7565 </p> <p>\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\u7684\u64cd\u4f5c\u6548\u7387\u4e0e\u7279\u6027\u5982\u4e0b\u8868\u6240\u793a\u3002</p> <p> \u8868\uff1a\u67e5\u627e\u7b97\u6cd5\u6548\u7387\u5bf9\u6bd4 </p> \u7ebf\u6027\u641c\u7d22 \u4e8c\u5206\u67e5\u627e \u6811\u67e5\u627e \u54c8\u5e0c\u67e5\u627e \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u989d\u5916\u7a7a\u95f4 \\(O(1)\\) \\(O(1)\\) \\(O(n)\\) \\(O(n)\\) \u6570\u636e\u9884\u5904\u7406 / \u6392\u5e8f \\(O(n \\log n)\\) \u5efa\u6811 \\(O(n \\log n)\\) \u5efa\u54c8\u5e0c\u8868 \\(O(n)\\) \u6570\u636e\u662f\u5426\u6709\u5e8f \u65e0\u5e8f \u6709\u5e8f \u6709\u5e8f \u65e0\u5e8f <p>\u9664\u4e86\u4ee5\u4e0a\u8868\u683c\u5185\u5bb9\uff0c\u641c\u7d22\u7b97\u6cd5\u7684\u9009\u62e9\u8fd8\u53d6\u51b3\u4e8e\u6570\u636e\u4f53\u91cf\u3001\u641c\u7d22\u6027\u80fd\u8981\u6c42\u3001\u6570\u636e\u67e5\u8be2\u4e0e\u66f4\u65b0\u9891\u7387\u7b49\u3002</p> <p>\u7ebf\u6027\u641c\u7d22</p> <ul> <li>\u901a\u7528\u6027\u8f83\u597d\uff0c\u65e0\u9700\u4efb\u4f55\u6570\u636e\u9884\u5904\u7406\u64cd\u4f5c\u3002\u5047\u5982\u6211\u4eec\u4ec5\u9700\u67e5\u8be2\u4e00\u6b21\u6570\u636e\uff0c\u90a3\u4e48\u5176\u4ed6\u4e09\u79cd\u65b9\u6cd5\u7684\u6570\u636e\u9884\u5904\u7406\u7684\u65f6\u95f4\u6bd4\u7ebf\u6027\u641c\u7d22\u7684\u65f6\u95f4\u8fd8\u8981\u66f4\u957f\u3002</li> <li>\u9002\u7528\u4e8e\u4f53\u91cf\u8f83\u5c0f\u7684\u6570\u636e\uff0c\u6b64\u60c5\u51b5\u4e0b\u65f6\u95f4\u590d\u6742\u5ea6\u5bf9\u6548\u7387\u5f71\u54cd\u8f83\u5c0f\u3002</li> <li>\u9002\u7528\u4e8e\u6570\u636e\u66f4\u65b0\u9891\u7387\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u8be5\u65b9\u6cd5\u4e0d\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u4efb\u4f55\u989d\u5916\u7ef4\u62a4\u3002</li> </ul> <p>\u4e8c\u5206\u67e5\u627e</p> <ul> <li>\u9002\u7528\u4e8e\u5927\u6570\u636e\u91cf\u7684\u60c5\u51b5\uff0c\u6548\u7387\u8868\u73b0\u7a33\u5b9a\uff0c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002</li> <li>\u6570\u636e\u91cf\u4e0d\u80fd\u8fc7\u5927\uff0c\u56e0\u4e3a\u5b58\u50a8\u6570\u7ec4\u9700\u8981\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u3002</li> <li>\u4e0d\u9002\u7528\u4e8e\u9ad8\u9891\u589e\u5220\u6570\u636e\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u7ef4\u62a4\u6709\u5e8f\u6570\u7ec4\u7684\u5f00\u9500\u8f83\u5927\u3002</li> </ul> <p>\u54c8\u5e0c\u67e5\u627e</p> <ul> <li>\u9002\u5408\u5bf9\u67e5\u8be2\u6027\u80fd\u8981\u6c42\u5f88\u9ad8\u7684\u573a\u666f\uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002</li> <li>\u4e0d\u9002\u5408\u9700\u8981\u6709\u5e8f\u6570\u636e\u6216\u8303\u56f4\u67e5\u627e\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u65e0\u6cd5\u7ef4\u62a4\u6570\u636e\u7684\u6709\u5e8f\u6027\u3002</li> <li>\u5bf9\u54c8\u5e0c\u51fd\u6570\u548c\u54c8\u5e0c\u51b2\u7a81\u5904\u7406\u7b56\u7565\u7684\u4f9d\u8d56\u6027\u8f83\u9ad8\uff0c\u5177\u6709\u8f83\u5927\u7684\u6027\u80fd\u52a3\u5316\u98ce\u9669\u3002</li> <li>\u4e0d\u9002\u5408\u6570\u636e\u91cf\u8fc7\u5927\u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u54c8\u5e0c\u8868\u9700\u8981\u989d\u5916\u7a7a\u95f4\u6765\u6700\u5927\u7a0b\u5ea6\u5730\u51cf\u5c11\u51b2\u7a81\uff0c\u4ece\u800c\u63d0\u4f9b\u826f\u597d\u7684\u67e5\u8be2\u6027\u80fd\u3002</li> </ul> <p>\u6811\u67e5\u627e</p> <ul> <li>\u9002\u7528\u4e8e\u6d77\u91cf\u6570\u636e\uff0c\u56e0\u4e3a\u6811\u8282\u70b9\u5728\u5185\u5b58\u4e2d\u662f\u79bb\u6563\u5b58\u50a8\u7684\u3002</li> <li>\u9002\u5408\u9700\u8981\u7ef4\u62a4\u6709\u5e8f\u6570\u636e\u6216\u8303\u56f4\u67e5\u627e\u7684\u573a\u666f\u3002</li> <li>\u5728\u6301\u7eed\u589e\u5220\u8282\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u53ef\u80fd\u4ea7\u751f\u503e\u659c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n)\\) \u3002</li> <li>\u82e5\u4f7f\u7528 AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c\u5219\u5404\u9879\u64cd\u4f5c\u53ef\u5728 \\(O(\\log n)\\) \u6548\u7387\u4e0b\u7a33\u5b9a\u8fd0\u884c\uff0c\u4f46\u7ef4\u62a4\u6811\u5e73\u8861\u7684\u64cd\u4f5c\u4f1a\u589e\u52a0\u989d\u5916\u5f00\u9500\u3002</li> </ul>"},{"location":"chapter_searching/summary/","title":"10.6. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u4e8c\u5206\u67e5\u627e\u4f9d\u8d56\u4e8e\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u901a\u8fc7\u5faa\u73af\u9010\u6b65\u7f29\u51cf\u4e00\u534a\u641c\u7d22\u533a\u95f4\u6765\u5b9e\u73b0\u67e5\u627e\u3002\u5b83\u8981\u6c42\u8f93\u5165\u6570\u636e\u6709\u5e8f\uff0c\u4e14\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u6216\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u3002</li> <li>\u66b4\u529b\u641c\u7d22\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784\u6765\u5b9a\u4f4d\u6570\u636e\u3002\u7ebf\u6027\u641c\u7d22\u9002\u7528\u4e8e\u6570\u7ec4\u548c\u94fe\u8868\uff0c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u548c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u9002\u7528\u4e8e\u56fe\u548c\u6811\u3002\u6b64\u7c7b\u7b97\u6cd5\u901a\u7528\u6027\u597d\uff0c\u65e0\u9700\u5bf9\u6570\u636e\u9884\u5904\u7406\uff0c\u4f46\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u8f83\u9ad8\u3002</li> <li>\u54c8\u5e0c\u67e5\u627e\u3001\u6811\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u5c5e\u4e8e\u9ad8\u6548\u641c\u7d22\u65b9\u6cd5\uff0c\u53ef\u5728\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u4e2d\u5feb\u901f\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002\u6b64\u7c7b\u7b97\u6cd5\u6548\u7387\u9ad8\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe \\(O(\\log n)\\) \u751a\u81f3 \\(O(1)\\) \uff0c\u4f46\u901a\u5e38\u9700\u8981\u501f\u52a9\u989d\u5916\u6570\u636e\u7ed3\u6784\u3002</li> <li>\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u4f53\u91cf\u3001\u641c\u7d22\u6027\u80fd\u8981\u6c42\u3001\u6570\u636e\u67e5\u8be2\u548c\u66f4\u65b0\u9891\u7387\u7b49\u56e0\u7d20\u8fdb\u884c\u5177\u4f53\u5206\u6790\uff0c\u4ece\u800c\u9009\u62e9\u5408\u9002\u7684\u641c\u7d22\u65b9\u6cd5\u3002</li> <li>\u7ebf\u6027\u641c\u7d22\u9002\u7528\u4e8e\u5c0f\u578b\u6216\u9891\u7e41\u66f4\u65b0\u7684\u6570\u636e\uff1b\u4e8c\u5206\u67e5\u627e\u9002\u7528\u4e8e\u5927\u578b\u3001\u6392\u5e8f\u7684\u6570\u636e\uff1b\u54c8\u5e0c\u67e5\u627e\u9002\u5408\u5bf9\u67e5\u8be2\u6548\u7387\u8981\u6c42\u8f83\u9ad8\u4e14\u65e0\u9700\u8303\u56f4\u67e5\u8be2\u7684\u6570\u636e\uff1b\u6811\u67e5\u627e\u9002\u7528\u4e8e\u9700\u8981\u7ef4\u62a4\u987a\u5e8f\u548c\u652f\u6301\u8303\u56f4\u67e5\u8be2\u7684\u5927\u578b\u52a8\u6001\u6570\u636e\u3002</li> <li>\u7528\u54c8\u5e0c\u67e5\u627e\u66ff\u6362\u7ebf\u6027\u67e5\u627e\u662f\u4e00\u79cd\u5e38\u7528\u7684\u4f18\u5316\u8fd0\u884c\u65f6\u95f4\u7684\u7b56\u7565\uff0c\u53ef\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u4f4e\u81f3 \\(O(1)\\) \u3002 </li> </ul>"},{"location":"chapter_sorting/","title":"11. \u00a0 \u6392\u5e8f","text":"<p>Abstract</p> <p>\u6392\u5e8f\u72b9\u5982\u4e00\u628a\u5c06\u6df7\u4e71\u53d8\u4e3a\u79e9\u5e8f\u7684\u9b54\u6cd5\u94a5\u5319\uff0c\u4f7f\u6211\u4eec\u80fd\u4ee5\u66f4\u9ad8\u6548\u7684\u65b9\u5f0f\u7406\u89e3\u4e0e\u5904\u7406\u6570\u636e\u3002</p> <p>\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5347\u5e8f\uff0c\u8fd8\u662f\u590d\u6742\u7684\u5206\u7c7b\u6392\u5217\uff0c\u6392\u5e8f\u90fd\u5411\u6211\u4eec\u5c55\u793a\u4e86\u6570\u636e\u7684\u548c\u8c10\u7f8e\u611f\u3002</p>"},{"location":"chapter_sorting/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>11.1 \u00a0 \u6392\u5e8f\u7b97\u6cd5</li> <li>11.2 \u00a0 \u9009\u62e9\u6392\u5e8f</li> <li>11.3 \u00a0 \u5192\u6ce1\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 \u5f52\u5e76\u6392\u5e8f</li> <li>11.7 \u00a0 \u5806\u6392\u5e8f</li> <li>11.8 \u00a0 \u6876\u6392\u5e8f</li> <li>11.9 \u00a0 \u8ba1\u6570\u6392\u5e8f</li> <li>11.10 \u00a0 \u57fa\u6570\u6392\u5e8f</li> <li>11.11 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_sorting/bubble_sort/","title":"11.3. \u00a0 \u5192\u6ce1\u6392\u5e8f","text":"<p>\u300c\u5192\u6ce1\u6392\u5e8f Bubble Sort\u300d\u901a\u8fc7\u8fde\u7eed\u5730\u6bd4\u8f83\u4e0e\u4ea4\u6362\u76f8\u90bb\u5143\u7d20\u5b9e\u73b0\u6392\u5e8f\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u50cf\u6c14\u6ce1\u4ece\u5e95\u90e8\u5347\u5230\u9876\u90e8\u4e00\u6837\uff0c\u56e0\u6b64\u5f97\u540d\u5192\u6ce1\u6392\u5e8f\u3002</p> <p>\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6a21\u62df\u4e0a\u8ff0\u8fc7\u7a0b\uff1a\u4ece\u6570\u7ec4\u6700\u5de6\u7aef\u5f00\u59cb\u5411\u53f3\u904d\u5386\uff0c\u4f9d\u6b21\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u5927\u5c0f\uff0c\u5982\u679c\u201c\u5de6\u5143\u7d20 &gt; \u53f3\u5143\u7d20\u201d\u5c31\u4ea4\u6362\u5b83\u4fe9\u3002\u904d\u5386\u5b8c\u6210\u540e\uff0c\u6700\u5927\u7684\u5143\u7d20\u4f1a\u88ab\u79fb\u52a8\u5230\u6570\u7ec4\u7684\u6700\u53f3\u7aef\u3002</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> \u56fe\uff1a\u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6a21\u62df\u5192\u6ce1 </p>"},{"location":"chapter_sorting/bubble_sort/#1131","title":"11.3.1. \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u6b65\u9aa4\u4e3a\uff1a</p> <ol> <li>\u9996\u5148\uff0c\u5bf9 \\(n\\) \u4e2a\u5143\u7d20\u6267\u884c\u201c\u5192\u6ce1\u201d\uff0c\u5c06\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\uff0c</li> <li>\u63a5\u4e0b\u6765\uff0c\u5bf9\u5269\u4f59 \\(n - 1\\) \u4e2a\u5143\u7d20\u6267\u884c\u201c\u5192\u6ce1\u201d\uff0c\u5c06\u7b2c\u4e8c\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\u3002</li> <li>\u4ee5\u6b64\u7c7b\u63a8\uff0c\u7ecf\u8fc7 \\(n - 1\\) \u8f6e\u201c\u5192\u6ce1\u201d\u540e\uff0c\u524d \\(n - 1\\) \u5927\u7684\u5143\u7d20\u90fd\u88ab\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\u3002</li> <li>\u4ec5\u5269\u7684\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u662f\u6700\u5c0f\u5143\u7d20\uff0c\u65e0\u9700\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u5192\u6ce1\u6392\u5e8f\u6d41\u7a0b </p> JavaC++PythonGoJSTSCC#SwiftZigDartRust bubble_sort.java<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int[] nums) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (int i = nums.length - 1; i &gt; 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (int j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\n}\n}\n}\n}\n</code></pre> bubble_sort.cpp<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(vector&lt;int&gt; &amp;nums) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (int i = nums.size() - 1; i &gt; 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (int j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n// \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\nswap(nums[j], nums[j + 1]);\n}\n}\n}\n}\n</code></pre> bubble_sort.py<pre><code>def bubble_sort(nums: list[int]):\n\"\"\"\u5192\u6ce1\u6392\u5e8f\"\"\"\nn = len(nums)\n# \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in range(n - 1, 0, -1):\n# \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor j in range(i):\nif nums[j] &gt; nums[j + 1]:\n# \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j + 1] = nums[j + 1], nums[j]\n</code></pre> bubble_sort.go<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nfunc bubbleSort(nums []int) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i := len(nums) - 1; i &gt; 0; i-- {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\nfor j := 0; j &lt; i; j++ {\nif nums[j] &gt; nums[j+1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j+1] = nums[j+1], nums[j]\n}\n}\n}\n}\n</code></pre> bubble_sort.js<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nfunction bubbleSort(nums) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (let i = nums.length - 1; i &gt; 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (let j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\n}\n}\n}\n}\n</code></pre> bubble_sort.ts<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nfunction bubbleSort(nums: number[]): void {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (let i = nums.length - 1; i &gt; 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (let j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\n}\n}\n}\n}\n</code></pre> bubble_sort.c<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int nums[], int size) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (int i = 0; i &lt; size - 1; i++) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (int j = 0; j &lt; size - 1 - i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\nint temp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = temp;\n}\n}\n}\n}\n</code></pre> bubble_sort.cs<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int[] nums) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (int i = nums.Length - 1; i &gt; 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (int j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\n}\n}\n}\n}\n</code></pre> bubble_sort.swift<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nfunc bubbleSort(nums: inout [Int]) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in stride(from: nums.count - 1, to: 0, by: -1) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor j in stride(from: 0, to: i, by: 1) {\nif nums[j] &gt; nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\n}\n}\n}\n}\n</code></pre> bubble_sort.zig<pre><code>// \u5192\u6ce1\u6392\u5e8f\nfn bubbleSort(nums: []i32) void {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nvar i: usize = nums.len - 1;\nwhile (i &gt; 0) : (i -= 1) {\nvar j: usize = 0;\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nwhile (j &lt; i) : (j += 1) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nvar tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\n}\n}\n}\n}\n</code></pre> bubble_sort.dart<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(List&lt;int&gt; nums) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (int i = nums.length - 1; i &gt; 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\nfor (int j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\n}\n}\n}\n}\n</code></pre> bubble_sort.rs<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nfn bubble_sort(nums: &amp;mut [i32]) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in (1..nums.len()).rev() {\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor j in 0..i {\nif nums[j] &gt; nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\n}\n}\n}\n}\n</code></pre>"},{"location":"chapter_sorting/bubble_sort/#1132","title":"11.3.2. \u00a0 \u6548\u7387\u4f18\u5316","text":"<p>\u6211\u4eec\u53d1\u73b0\uff0c\u5982\u679c\u67d0\u8f6e\u201c\u5192\u6ce1\u201d\u4e2d\u6ca1\u6709\u6267\u884c\u4efb\u4f55\u4ea4\u6362\u64cd\u4f5c\uff0c\u8bf4\u660e\u6570\u7ec4\u5df2\u7ecf\u5b8c\u6210\u6392\u5e8f\uff0c\u53ef\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u589e\u52a0\u4e00\u4e2a\u6807\u5fd7\u4f4d <code>flag</code> \u6765\u76d1\u6d4b\u8fd9\u79cd\u60c5\u51b5\uff0c\u4e00\u65e6\u51fa\u73b0\u5c31\u7acb\u5373\u8fd4\u56de\u3002</p> <p>\u7ecf\u8fc7\u4f18\u5316\uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u6700\u5dee\u548c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(n^2)\\) \uff1b\u4f46\u5f53\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u6709\u5e8f\u65f6\uff0c\u53ef\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust bubble_sort.java<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 */\nvoid bubbleSortWithFlag(int[] nums) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (int i = nums.length - 1; i &gt; 0; i--) {\nboolean flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (int j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag)\nbreak; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n</code></pre> bubble_sort.cpp<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(vector&lt;int&gt; &amp;nums) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (int i = nums.size() - 1; i &gt; 0; i--) {\nbool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (int j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n// \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\nswap(nums[j], nums[j + 1]);\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag)\nbreak; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n</code></pre> bubble_sort.py<pre><code>def bubble_sort_with_flag(nums: list[int]):\n\"\"\"\u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09\"\"\"\nn = len(nums)\n# \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in range(n - 1, 0, -1):\nflag = False # \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n# \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor j in range(i):\nif nums[j] &gt; nums[j + 1]:\n# \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j + 1] = nums[j + 1], nums[j]\nflag = True # \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\nif not flag:\nbreak # \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n</code></pre> bubble_sort.go<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunc bubbleSortWithFlag(nums []int) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i := len(nums) - 1; i &gt; 0; i-- {\nflag := false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\nfor j := 0; j &lt; i; j++ {\nif nums[j] &gt; nums[j+1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j+1] = nums[j+1], nums[j]\nflag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif flag == false { // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\nbreak\n}\n}\n}\n</code></pre> bubble_sort.js<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunction bubbleSortWithFlag(nums) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (let i = nums.length - 1; i &gt; 0; i--) {\nlet flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (let j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n</code></pre> bubble_sort.ts<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunction bubbleSortWithFlag(nums: number[]): void {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (let i = nums.length - 1; i &gt; 0; i--) {\nlet flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (let j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n</code></pre> bubble_sort.c<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(int nums[], int size) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (int i = 0; i &lt; size - 1; i++) {\nbool flag = false;\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (int j = 0; j &lt; size - 1 - i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\nint temp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = temp;\nflag = true;\n}\n}\nif (!flag)\nbreak;\n}\n}\n</code></pre> bubble_sort.cs<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(int[] nums) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (int i = nums.Length - 1; i &gt; 0; i--) {\nbool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor (int j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n</code></pre> bubble_sort.swift<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunc bubbleSortWithFlag(nums: inout [Int]) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in stride(from: nums.count - 1, to: 0, by: -1) {\nvar flag = false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\nfor j in stride(from: 0, to: i, by: 1) {\nif nums[j] &gt; nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\nflag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif !flag { // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\nbreak\n}\n}\n}\n</code></pre> bubble_sort.zig<pre><code>// \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09\nfn bubbleSortWithFlag(nums: []i32) void {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nvar i: usize = nums.len - 1;\nwhile (i &gt; 0) : (i -= 1) {\nvar flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\nvar j: usize = 0;\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nwhile (j &lt; i) : (j += 1) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nvar tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true;\n}\n}\nif (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n</code></pre> bubble_sort.dart<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(List&lt;int&gt; nums) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor (int i = nums.length - 1; i &gt; 0; i--) {\nbool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\nfor (int j = 0; j &lt; i; j++) {\nif (nums[j] &gt; nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n</code></pre> bubble_sort.rs<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 */\nfn bubble_sort_with_flag(nums: &amp;mut [i32]) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i in (1..nums.len()).rev() {\nlet mut flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef \nfor j in 0..i {\nif nums[j] &gt; nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif !flag {break}; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n</code></pre>"},{"location":"chapter_sorting/bubble_sort/#1133","title":"11.3.3. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3001\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u5404\u8f6e\u201c\u5192\u6ce1\u201d\u904d\u5386\u7684\u6570\u7ec4\u957f\u5ea6\u4f9d\u6b21\u4e3a \\(n - 1\\) , \\(n - 2\\) , \\(\\cdots\\) , \\(2\\) , \\(1\\) \uff0c\u603b\u548c\u4e3a \\(\\frac{(n - 1) n}{2}\\) \u3002\u5728\u5f15\u5165 <code>flag</code> \u4f18\u5316\u540e\uff0c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe\u5230 \\(O(n)\\) \u3002</li> <li>\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488 \\(i\\) , \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002</li> <li>\u7a33\u5b9a\u6392\u5e8f\uff1a\u7531\u4e8e\u5728\u201c\u5192\u6ce1\u201d\u4e2d\u9047\u5230\u76f8\u7b49\u5143\u7d20\u4e0d\u4ea4\u6362\u3002</li> </ul>"},{"location":"chapter_sorting/bucket_sort/","title":"11.8. \u00a0 \u6876\u6392\u5e8f","text":"<p>\u524d\u8ff0\u7684\u51e0\u79cd\u6392\u5e8f\u7b97\u6cd5\u90fd\u5c5e\u4e8e\u201c\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u7b97\u6cd5\u201d\uff0c\u5b83\u4eec\u901a\u8fc7\u6bd4\u8f83\u5143\u7d20\u95f4\u7684\u5927\u5c0f\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u6b64\u7c7b\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u65e0\u6cd5\u8d85\u8d8a \\(O(n \\log n)\\) \u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u51e0\u79cd\u201c\u975e\u6bd4\u8f83\u6392\u5e8f\u7b97\u6cd5\u201d\uff0c\u5b83\u4eec\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230\u7ebf\u6027\u9636\u3002</p> <p>\u300c\u6876\u6392\u5e8f Bucket Sort\u300d\u662f\u5206\u6cbb\u601d\u60f3\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u3002\u5b83\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e9b\u5177\u6709\u5927\u5c0f\u987a\u5e8f\u7684\u6876\uff0c\u6bcf\u4e2a\u6876\u5bf9\u5e94\u4e00\u4e2a\u6570\u636e\u8303\u56f4\uff0c\u5c06\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff1b\u7136\u540e\uff0c\u5728\u6bcf\u4e2a\u6876\u5185\u90e8\u5206\u522b\u6267\u884c\u6392\u5e8f\uff1b\u6700\u7ec8\u6309\u7167\u6876\u7684\u987a\u5e8f\u5c06\u6240\u6709\u6570\u636e\u5408\u5e76\u3002</p>"},{"location":"chapter_sorting/bucket_sort/#1181","title":"11.8.1. \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u8003\u8651\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u5143\u7d20\u662f\u8303\u56f4 \\([0, 1)\\) \u7684\u6d6e\u70b9\u6570\u3002\u6876\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u4e0b\uff1a</p> <ol> <li>\u521d\u59cb\u5316 \\(k\\) \u4e2a\u6876\uff0c\u5c06 \\(n\\) \u4e2a\u5143\u7d20\u5206\u914d\u5230 \\(k\\) \u4e2a\u6876\u4e2d\u3002</li> <li>\u5bf9\u6bcf\u4e2a\u6876\u5206\u522b\u6267\u884c\u6392\u5e8f\uff08\u672c\u6587\u91c7\u7528\u7f16\u7a0b\u8bed\u8a00\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff09\u3002</li> <li>\u6309\u7167\u6876\u7684\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\uff0c\u5408\u5e76\u7ed3\u679c\u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u6876\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b </p> JavaC++PythonGoJSTSCC#SwiftZigDartRust bucket_sort.java<pre><code>/* \u6876\u6392\u5e8f */\nvoid bucketSort(float[] nums) {\n// \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\nint k = nums.length / 2;\nList&lt;List&lt;Float&gt;&gt; buckets = new ArrayList&lt;&gt;();\nfor (int i = 0; i &lt; k; i++) {\nbuckets.add(new ArrayList&lt;&gt;());\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (float num : nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint i = (int) (num * k);\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets.get(i).add(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (List&lt;Float&gt; bucket : buckets) {\n// \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\nCollections.sort(bucket);\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nint i = 0;\nfor (List&lt;Float&gt; bucket : buckets) {\nfor (float num : bucket) {\nnums[i++] = num;\n}\n}\n}\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 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\nint k = nums.size() / 2;\nvector&lt;vector&lt;float&gt;&gt; buckets(k);\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (float num : nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint i = num * k;\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 bucket_idx\nbuckets[i].push_back(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (vector&lt;float&gt; &amp;bucket : buckets) {\n// \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\nsort(bucket.begin(), bucket.end());\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nint i = 0;\nfor (vector&lt;float&gt; &amp;bucket : buckets) {\nfor (float num : bucket) {\nnums[i++] = num;\n}\n}\n}\n</code></pre> bucket_sort.py<pre><code>def bucket_sort(nums: list[float]):\n\"\"\"\u6876\u6392\u5e8f\"\"\"\n# \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\nk = len(nums) // 2\nbuckets = [[] for _ in range(k)]\n# 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor num in nums:\n# \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\ni = int(num * k)\n# \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].append(num)\n# 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor bucket in buckets:\n# \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\nbucket.sort()\n# 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\ni = 0\nfor bucket in buckets:\nfor num in bucket:\nnums[i] = num\ni += 1\n</code></pre> bucket_sort.go<pre><code>/* \u6876\u6392\u5e8f */\nfunc bucketSort(nums []float64) {\n// \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\nk := len(nums) / 2\nbuckets := make([][]float64, k)\nfor i := 0; i &lt; k; i++ {\nbuckets[i] = make([]float64, 0)\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor _, num := range nums {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\ni := int(num * float64(k))\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i] = append(buckets[i], num)\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor i := 0; i &lt; k; i++ {\n// \u4f7f\u7528\u5185\u7f6e\u5207\u7247\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\nsort.Float64s(buckets[i])\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\ni := 0\nfor _, bucket := range buckets {\nfor _, num := range bucket {\nnums[i] = num\ni++\n}\n}\n}\n</code></pre> bucket_sort.js<pre><code>/* \u6876\u6392\u5e8f */\nfunction bucketSort(nums) {\n// \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\nconst k = nums.length / 2;\nconst buckets = [];\nfor (let i = 0; i &lt; k; i++) {\nbuckets.push([]);\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (const num of nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nconst i = Math.floor(num * k);\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].push(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (const bucket of buckets) {\n// \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\nbucket.sort((a, b) =&gt; a - b);\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nlet i = 0;\nfor (const bucket of buckets) {\nfor (const num of bucket) {\nnums[i++] = num;\n}\n}\n}\n</code></pre> bucket_sort.ts<pre><code>/* \u6876\u6392\u5e8f */\nfunction bucketSort(nums: number[]): void {\n// \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\nconst k = nums.length / 2;\nconst buckets: number[][] = [];\nfor (let i = 0; i &lt; k; i++) {\nbuckets.push([]);\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (const num of nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nconst i = Math.floor(num * k);\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].push(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (const bucket of buckets) {\n// \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\nbucket.sort((a, b) =&gt; a - b);\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nlet i = 0;\nfor (const bucket of buckets) {\nfor (const num of bucket) {\nnums[i++] = num;\n}\n}\n}\n</code></pre> bucket_sort.c<pre><code>/* \u6876\u6392\u5e8f */\nvoid bucketSort(float nums[], int size) {\n// \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\nint k = size / 2;\nfloat **buckets = calloc(k, sizeof(float *));\nfor (int i = 0; i &lt; k; i++) {\n// \u6bcf\u4e2a\u6876\u6700\u591a\u53ef\u4ee5\u5206\u914d k \u4e2a\u5143\u7d20\nbuckets[i] = calloc(ARRAY_SIZE, sizeof(float));\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (int i = 0; i &lt; size; i++) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint bucket_idx = nums[i] * k;\nint j = 0;\n// \u5982\u679c\u6876\u4e2d\u6709\u6570\u636e\u4e14\u6570\u636e\u5c0f\u4e8e\u5f53\u524d\u503c nums[i], \u8981\u5c06\u5176\u653e\u5230\u5f53\u524d\u6876\u7684\u540e\u9762\uff0c\u76f8\u5f53\u4e8e cpp \u4e2d\u7684 push_back\nwhile (buckets[bucket_idx][j] &gt; 0 &amp;&amp; buckets[bucket_idx][j] &lt; nums[i]) {\nj++;\n}\nfloat temp = nums[i];\nwhile (j &lt; ARRAY_SIZE &amp;&amp; buckets[bucket_idx][j] &gt; 0) {\nswap(&amp;temp, &amp;buckets[bucket_idx][j]);\nj++;\n}\nbuckets[bucket_idx][j] = temp;\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (int i = 0; i &lt; k; i++) {\nqsort(buckets[i], ARRAY_SIZE, sizeof(float), compare_float);\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nfor (int i = 0, j = 0; j &lt; k; j++) {\nfor (int l = 0; l &lt; ARRAY_SIZE; l++) {\nif (buckets[j][l] &gt; 0) {\nnums[i++] = buckets[j][l];\n}\n}\n}\n// \u91ca\u653e\u4e0a\u8ff0\u5206\u914d\u7684\u5185\u5b58\nfor (int i = 0; i &lt; k; i++) {\nfree(buckets[i]);\n}\nfree(buckets);\n}\n</code></pre> bucket_sort.cs<pre><code>/* \u6876\u6392\u5e8f */\nvoid bucketSort(float[] nums) {\n// \u521d\u59cb\u5316 k = n/2 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\nint k = nums.Length / 2;\nList&lt;List&lt;float&gt;&gt; buckets = new List&lt;List&lt;float&gt;&gt;();\nfor (int i = 0; i &lt; k; i++) {\nbuckets.Add(new List&lt;float&gt;());\n}\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nforeach (float num in nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint i = (int) (num * k);\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].Add(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nforeach (List&lt;float&gt; bucket in buckets) {\n// \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\nbucket.Sort();\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nint j = 0;\nforeach (List&lt;float&gt; bucket in buckets) {\nforeach (float num in bucket) {\nnums[j++] = num;\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 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\nlet k = nums.count / 2\nvar buckets = (0 ..&lt; k).map { _ in [Double]() }\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor num in nums {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nlet i = Int(num * Double(k))\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].append(num)\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor i in buckets.indices {\n// \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\nbuckets[i].sort()\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nvar i = nums.startIndex\nfor bucket in buckets {\nfor num in bucket {\nnums[i] = num\nnums.formIndex(after: &amp;i)\n}\n}\n}\n</code></pre> bucket_sort.zig<pre><code>[class]{}-[func]{bucketSort}\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 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\nint k = nums.length ~/ 2;\nList&lt;List&lt;double&gt;&gt; buckets = List.generate(k, (index) =&gt; []);\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor (double num in nums) {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nint i = (num * k).toInt();\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 bucket_idx\nbuckets[i].add(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor (List&lt;double&gt; bucket in buckets) {\nbucket.sort();\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nint i = 0;\nfor (List&lt;double&gt; bucket in buckets) {\nfor (double num in bucket) {\nnums[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 \u4e2a\u6876\uff0c\u9884\u671f\u5411\u6bcf\u4e2a\u6876\u5206\u914d 2 \u4e2a\u5143\u7d20\nlet k = nums.len() / 2;\nlet mut buckets = vec![vec![]; k];\n// 1. \u5c06\u6570\u7ec4\u5143\u7d20\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\nfor &amp;mut num in &amp;mut *nums {\n// \u8f93\u5165\u6570\u636e\u8303\u56f4 [0, 1)\uff0c\u4f7f\u7528 num * k \u6620\u5c04\u5230\u7d22\u5f15\u8303\u56f4 [0, k-1]\nlet i = (num * k as f64) as usize;\n// \u5c06 num \u6dfb\u52a0\u8fdb\u6876 i\nbuckets[i].push(num);\n}\n// 2. \u5bf9\u5404\u4e2a\u6876\u6267\u884c\u6392\u5e8f\nfor bucket in &amp;mut buckets {\n// \u4f7f\u7528\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u66ff\u6362\u6210\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\nbucket.sort_by(|a, b| a.partial_cmp(b).unwrap());\n}\n// 3. \u904d\u5386\u6876\u5408\u5e76\u7ed3\u679c\nlet mut i = 0;\nfor bucket in &amp;mut buckets {\nfor &amp;mut num in bucket {\nnums[i] = num;\ni += 1;\n}\n}\n}\n</code></pre> <p>\u6876\u6392\u5e8f\u7684\u9002\u7528\u573a\u666f\u662f\u4ec0\u4e48\uff1f</p> <p>\u6876\u6392\u5e8f\u9002\u7528\u4e8e\u5904\u7406\u4f53\u91cf\u5f88\u5927\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u6570\u636e\u5305\u542b 100 \u4e07\u4e2a\u5143\u7d20\uff0c\u7531\u4e8e\u7a7a\u95f4\u9650\u5236\uff0c\u7cfb\u7edf\u5185\u5b58\u65e0\u6cd5\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5206\u6210 1000 \u4e2a\u6876\uff0c\u7136\u540e\u5206\u522b\u5bf9\u6bcf\u4e2a\u6876\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u5408\u5e76\u3002</p>"},{"location":"chapter_sorting/bucket_sort/#1182","title":"11.8.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n + k)\\) \uff1a\u5047\u8bbe\u5143\u7d20\u5728\u5404\u4e2a\u6876\u5185\u5e73\u5747\u5206\u5e03\uff0c\u90a3\u4e48\u6bcf\u4e2a\u6876\u5185\u7684\u5143\u7d20\u6570\u91cf\u4e3a \\(\\frac{n}{k}\\) \u3002\u5047\u8bbe\u6392\u5e8f\u5355\u4e2a\u6876\u4f7f\u7528 \\(O(\\frac{n}{k} \\log\\frac{n}{k})\\) \u65f6\u95f4\uff0c\u5219\u6392\u5e8f\u6240\u6709\u6876\u4f7f\u7528 \\(O(n \\log\\frac{n}{k})\\) \u65f6\u95f4\u3002\u5f53\u6876\u6570\u91cf \\(k\\) \u6bd4\u8f83\u5927\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5219\u8d8b\u5411\u4e8e \\(O(n)\\) \u3002\u5408\u5e76\u7ed3\u679c\u65f6\u9700\u8981\u904d\u5386\u6240\u6709\u6876\u548c\u5143\u7d20\uff0c\u82b1\u8d39 \\(O(n + k)\\) \u65f6\u95f4\u3002</li> <li>\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5728\u6700\u574f\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u6570\u636e\u88ab\u5206\u914d\u5230\u4e00\u4e2a\u6876\u4e2d\uff0c\u4e14\u6392\u5e8f\u8be5\u6876\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002</li> <li>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n + k)\\) \u3001\u975e\u539f\u5730\u6392\u5e8f \uff1a\u9700\u8981\u501f\u52a9 \\(k\\) \u4e2a\u6876\u548c\u603b\u5171 \\(n\\) \u4e2a\u5143\u7d20\u7684\u989d\u5916\u7a7a\u95f4\u3002</li> <li>\u6876\u6392\u5e8f\u662f\u5426\u7a33\u5b9a\u53d6\u51b3\u4e8e\u6392\u5e8f\u6876\u5185\u5143\u7d20\u7684\u7b97\u6cd5\u662f\u5426\u7a33\u5b9a\u3002</li> </ul>"},{"location":"chapter_sorting/bucket_sort/#1183","title":"11.8.3. \u00a0 \u5982\u4f55\u5b9e\u73b0\u5e73\u5747\u5206\u914d","text":"<p>\u6876\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7406\u8bba\u4e0a\u53ef\u4ee5\u8fbe\u5230 \\(O(n)\\) \uff0c\u5173\u952e\u5728\u4e8e\u5c06\u5143\u7d20\u5747\u5300\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff0c\u56e0\u4e3a\u5b9e\u9645\u6570\u636e\u5f80\u5f80\u4e0d\u662f\u5747\u5300\u5206\u5e03\u7684\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u6dd8\u5b9d\u4e0a\u7684\u6240\u6709\u5546\u54c1\u6309\u4ef7\u683c\u8303\u56f4\u5e73\u5747\u5206\u914d\u5230 10 \u4e2a\u6876\u4e2d\uff0c\u4f46\u5546\u54c1\u4ef7\u683c\u5206\u5e03\u4e0d\u5747\uff0c\u4f4e\u4e8e 100 \u5143\u7684\u975e\u5e38\u591a\uff0c\u9ad8\u4e8e 1000 \u5143\u7684\u975e\u5e38\u5c11\u3002\u82e5\u5c06\u4ef7\u683c\u533a\u95f4\u5e73\u5747\u5212\u5206\u4e3a 10 \u4efd\uff0c\u5404\u4e2a\u6876\u4e2d\u7684\u5546\u54c1\u6570\u91cf\u5dee\u8ddd\u4f1a\u975e\u5e38\u5927\u3002</p> <p>\u4e3a\u5b9e\u73b0\u5e73\u5747\u5206\u914d\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u8bbe\u5b9a\u4e00\u4e2a\u5927\u81f4\u7684\u5206\u754c\u7ebf\uff0c\u5c06\u6570\u636e\u7c97\u7565\u5730\u5206\u5230 3 \u4e2a\u6876\u4e2d\u3002\u5206\u914d\u5b8c\u6bd5\u540e\uff0c\u518d\u5c06\u5546\u54c1\u8f83\u591a\u7684\u6876\u7ee7\u7eed\u5212\u5206\u4e3a 3 \u4e2a\u6876\uff0c\u76f4\u81f3\u6240\u6709\u6876\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u5927\u81f4\u76f8\u7b49\u3002\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u521b\u5efa\u4e00\u4e2a\u9012\u5f52\u6811\uff0c\u4f7f\u53f6\u8282\u70b9\u7684\u503c\u5c3d\u53ef\u80fd\u5e73\u5747\u3002\u5f53\u7136\uff0c\u4e0d\u4e00\u5b9a\u8981\u6bcf\u8f6e\u5c06\u6570\u636e\u5212\u5206\u4e3a 3 \u4e2a\u6876\uff0c\u5177\u4f53\u5212\u5206\u65b9\u5f0f\u53ef\u6839\u636e\u6570\u636e\u7279\u70b9\u7075\u6d3b\u9009\u62e9\u3002</p> <p></p> <p> \u56fe\uff1a\u9012\u5f52\u5212\u5206\u6876 </p> <p>\u5982\u679c\u6211\u4eec\u63d0\u524d\u77e5\u9053\u5546\u54c1\u4ef7\u683c\u7684\u6982\u7387\u5206\u5e03\uff0c\u5219\u53ef\u4ee5\u6839\u636e\u6570\u636e\u6982\u7387\u5206\u5e03\u8bbe\u7f6e\u6bcf\u4e2a\u6876\u7684\u4ef7\u683c\u5206\u754c\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6570\u636e\u5206\u5e03\u5e76\u4e0d\u4e00\u5b9a\u9700\u8981\u7279\u610f\u7edf\u8ba1\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7279\u70b9\u91c7\u7528\u67d0\u79cd\u6982\u7387\u6a21\u578b\u8fdb\u884c\u8fd1\u4f3c\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u6211\u4eec\u5047\u8bbe\u5546\u54c1\u4ef7\u683c\u670d\u4ece\u6b63\u6001\u5206\u5e03\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5408\u7406\u5730\u8bbe\u5b9a\u4ef7\u683c\u533a\u95f4\uff0c\u4ece\u800c\u5c06\u5546\u54c1\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\u3002</p> <p></p> <p> \u56fe\uff1a\u6839\u636e\u6982\u7387\u5206\u5e03\u5212\u5206\u6876 </p>"},{"location":"chapter_sorting/counting_sort/","title":"11.9. \u00a0 \u8ba1\u6570\u6392\u5e8f","text":"<p>\u300c\u8ba1\u6570\u6392\u5e8f Counting Sort\u300d\u901a\u8fc7\u7edf\u8ba1\u5143\u7d20\u6570\u91cf\u6765\u5b9e\u73b0\u6392\u5e8f\uff0c\u901a\u5e38\u5e94\u7528\u4e8e\u6574\u6570\u6570\u7ec4\u3002</p>"},{"location":"chapter_sorting/counting_sort/#1191","title":"11.9.1. \u00a0 \u7b80\u5355\u5b9e\u73b0","text":"<p>\u5148\u6765\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u3002\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4 <code>nums</code> \uff0c\u5176\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u201c\u975e\u8d1f\u6574\u6570\u201d\u3002\u8ba1\u6570\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u4e0b\uff1a</p> <ol> <li>\u904d\u5386\u6570\u7ec4\uff0c\u627e\u51fa\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u6570\u5b57\uff0c\u8bb0\u4e3a \\(m\\) \uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(m + 1\\) \u7684\u8f85\u52a9\u6570\u7ec4 <code>counter</code> \u3002</li> <li>\u501f\u52a9 <code>counter</code> \u7edf\u8ba1 <code>nums</code> \u4e2d\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5176\u4e2d <code>counter[num]</code> \u5bf9\u5e94\u6570\u5b57 <code>num</code> \u7684\u51fa\u73b0\u6b21\u6570\u3002\u7edf\u8ba1\u65b9\u6cd5\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u904d\u5386 <code>nums</code>\uff08\u8bbe\u5f53\u524d\u6570\u5b57\u4e3a <code>num</code>\uff09\uff0c\u6bcf\u8f6e\u5c06 <code>counter[num]</code> \u589e\u52a0 \\(1\\) \u5373\u53ef\u3002</li> <li>\u7531\u4e8e <code>counter</code> \u7684\u5404\u4e2a\u7d22\u5f15\u5929\u7136\u6709\u5e8f\uff0c\u56e0\u6b64\u76f8\u5f53\u4e8e\u6240\u6709\u6570\u5b57\u5df2\u7ecf\u88ab\u6392\u5e8f\u597d\u4e86\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u904d\u5386 <code>counter</code> \uff0c\u6839\u636e\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5c06\u5b83\u4eec\u6309\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u586b\u5165 <code>nums</code> \u5373\u53ef\u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b </p> JavaC++PythonGoJSTSCC#SwiftZigDartRust counting_sort.java<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(int[] nums) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nint m = 0;\nfor (int num : nums) {\nm = Math.max(m, num);\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nint[] counter = new int[m + 1];\nfor (int num : nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num &lt; m + 1; num++) {\nfor (int j = 0; j &lt; counter[num]; j++, i++) {\nnums[i] = num;\n}\n}\n}\n</code></pre> counting_sort.cpp<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(vector&lt;int&gt; &amp;nums) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nint m = 0;\nfor (int num : nums) {\nm = max(m, num);\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nvector&lt;int&gt; counter(m + 1, 0);\nfor (int num : nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num &lt; m + 1; num++) {\nfor (int j = 0; j &lt; counter[num]; j++, i++) {\nnums[i] = num;\n}\n}\n}\n</code></pre> counting_sort.py<pre><code>def counting_sort_naive(nums: list[int]):\n\"\"\"\u8ba1\u6570\u6392\u5e8f\"\"\"\n# \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\n# 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nm = 0\nfor num in nums:\nm = max(m, num)\n# 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n# counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\ncounter = [0] * (m + 1)\nfor num in nums:\ncounter[num] += 1\n# 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\ni = 0\nfor num in range(m + 1):\nfor _ in range(counter[num]):\nnums[i] = num\ni += 1\n</code></pre> counting_sort.go<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunc countingSortNaive(nums []int) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nm := 0\nfor _, num := range nums {\nif num &gt; m {\nm = num\n}\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\ncounter := make([]int, m+1)\nfor _, num := range nums {\ncounter[num]++\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nfor i, num := 0, 0; num &lt; m+1; num++ {\nfor j := 0; j &lt; counter[num]; j++ {\nnums[i] = num\ni++\n}\n}\n}\n</code></pre> counting_sort.js<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunction countingSortNaive(nums) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nlet m = 0;\nfor (const num of nums) {\nm = Math.max(m, num);\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nconst counter = new Array(m + 1).fill(0);\nfor (const num of nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nlet i = 0;\nfor (let num = 0; num &lt; m + 1; num++) {\nfor (let j = 0; j &lt; counter[num]; j++, i++) {\nnums[i] = num;\n}\n}\n}\n</code></pre> counting_sort.ts<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunction countingSortNaive(nums: number[]): void {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nlet m = 0;\nfor (const num of nums) {\nm = Math.max(m, num);\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nconst counter: number[] = new Array&lt;number&gt;(m + 1).fill(0);\nfor (const num of nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nlet i = 0;\nfor (let num = 0; num &lt; m + 1; num++) {\nfor (let j = 0; j &lt; counter[num]; j++, i++) {\nnums[i] = num;\n}\n}\n}\n</code></pre> counting_sort.c<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(int nums[], int size) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nint m = 0;\nfor (int i = 0; i &lt; size; i++) {\nif (nums[i] &gt; m) {\nm = nums[i];\n}\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nint *counter = malloc(sizeof(int) * m);\nfor (int i = 0; i &lt; size; i++) {\ncounter[nums[i]]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num &lt; m + 1; num++) {\nfor (int j = 0; j &lt; counter[num]; j++, i++) {\nnums[i] = num;\n}\n}\n}\n</code></pre> counting_sort.cs<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(int[] nums) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nint m = 0;\nforeach (int num in nums) {\nm = Math.Max(m, num);\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nint[] counter = new int[m + 1];\nforeach (int num in nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num &lt; m + 1; num++) {\nfor (int j = 0; j &lt; counter[num]; j++, i++) {\nnums[i] = num;\n}\n}\n}\n</code></pre> counting_sort.swift<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfunc countingSortNaive(nums: inout [Int]) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nlet m = nums.max()!\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nvar counter = Array(repeating: 0, count: m + 1)\nfor num in nums {\ncounter[num] += 1\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nvar i = 0\nfor num in stride(from: 0, to: m + 1, by: 1) {\nfor _ in stride(from: 0, to: counter[num], by: 1) {\nnums[i] = num\ni += 1\n}\n}\n}\n</code></pre> counting_sort.zig<pre><code>[class]{}-[func]{countingSortNaive}\n</code></pre> counting_sort.dart<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nvoid countingSortNaive(List&lt;int&gt; nums) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nint m = 0;\nfor (int num in nums) {\nm = max(m, num);\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nList&lt;int&gt; counter = List.filled(m + 1, 0);\nfor (int num in nums) {\ncounter[num]++;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nint i = 0;\nfor (int num = 0; num &lt; m + 1; num++) {\nfor (int j = 0; j &lt; counter[num]; j++, i++) {\nnums[i] = num;\n}\n}\n}\n</code></pre> counting_sort.rs<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u7b80\u5355\u5b9e\u73b0\uff0c\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u5bf9\u8c61\nfn counting_sort_naive(nums: &amp;mut [i32]) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nlet m = *nums.into_iter().max().unwrap();\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nlet mut counter = vec![0; m as usize + 1];\nfor &amp;num in &amp;*nums {\ncounter[num as usize] += 1;\n}\n// 3. \u904d\u5386 counter \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u539f\u6570\u7ec4 nums\nlet mut i = 0;\nfor num in 0..m + 1 {\nfor _ in 0..counter[num as usize] {\nnums[i] = num;\ni += 1;\n}\n}\n}\n</code></pre> <p>\u8ba1\u6570\u6392\u5e8f\u4e0e\u6876\u6392\u5e8f\u7684\u8054\u7cfb</p> <p>\u4ece\u6876\u6392\u5e8f\u7684\u89d2\u5ea6\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8ba1\u6570\u6392\u5e8f\u4e2d\u7684\u8ba1\u6570\u6570\u7ec4 <code>counter</code> \u7684\u6bcf\u4e2a\u7d22\u5f15\u89c6\u4e3a\u4e00\u4e2a\u6876\uff0c\u5c06\u7edf\u8ba1\u6570\u91cf\u7684\u8fc7\u7a0b\u770b\u4f5c\u662f\u5c06\u5404\u4e2a\u5143\u7d20\u5206\u914d\u5230\u5bf9\u5e94\u7684\u6876\u4e2d\u3002\u672c\u8d28\u4e0a\uff0c\u8ba1\u6570\u6392\u5e8f\u662f\u6876\u6392\u5e8f\u5728\u6574\u578b\u6570\u636e\u4e0b\u7684\u4e00\u4e2a\u7279\u4f8b\u3002</p>"},{"location":"chapter_sorting/counting_sort/#1192","title":"11.9.2. \u00a0 \u5b8c\u6574\u5b9e\u73b0","text":"<p>\u7ec6\u5fc3\u7684\u540c\u5b66\u53ef\u80fd\u53d1\u73b0\uff0c\u5982\u679c\u8f93\u5165\u6570\u636e\u662f\u5bf9\u8c61\uff0c\u4e0a\u8ff0\u6b65\u9aa4 <code>3.</code> \u5c31\u5931\u6548\u4e86\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u6570\u636e\u662f\u5546\u54c1\u5bf9\u8c61\uff0c\u6211\u4eec\u60f3\u8981\u6309\u7167\u5546\u54c1\u4ef7\u683c\uff08\u7c7b\u7684\u6210\u5458\u53d8\u91cf\uff09\u5bf9\u5546\u54c1\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u4e0a\u8ff0\u7b97\u6cd5\u53ea\u80fd\u7ed9\u51fa\u4ef7\u683c\u7684\u6392\u5e8f\u7ed3\u679c\u3002</p> <p>\u90a3\u4e48\u5982\u4f55\u624d\u80fd\u5f97\u5230\u539f\u6570\u636e\u7684\u6392\u5e8f\u7ed3\u679c\u5462\uff1f\u6211\u4eec\u9996\u5148\u8ba1\u7b97 <code>counter</code> \u7684\u300c\u524d\u7f00\u548c\u300d\u3002\u987e\u540d\u601d\u4e49\uff0c\u7d22\u5f15 <code>i</code> \u5904\u7684\u524d\u7f00\u548c <code>prefix[i]</code> \u7b49\u4e8e\u6570\u7ec4\u524d <code>i</code> \u4e2a\u5143\u7d20\u4e4b\u548c\uff0c\u5373</p> \\[ \\text{prefix}[i] = \\sum_{j=0}^i \\text{counter[j]} \\] <p>\u524d\u7f00\u548c\u5177\u6709\u660e\u786e\u7684\u610f\u4e49\uff0c<code>prefix[num] - 1</code> \u4ee3\u8868\u5143\u7d20 <code>num</code> \u5728\u7ed3\u679c\u6570\u7ec4 <code>res</code> \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002\u8fd9\u4e2a\u4fe1\u606f\u975e\u5e38\u5173\u952e\uff0c\u56e0\u4e3a\u5b83\u544a\u8bc9\u6211\u4eec\u5404\u4e2a\u5143\u7d20\u5e94\u8be5\u51fa\u73b0\u5728\u7ed3\u679c\u6570\u7ec4\u7684\u54ea\u4e2a\u4f4d\u7f6e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5012\u5e8f\u904d\u5386\u539f\u6570\u7ec4 <code>nums</code> \u7684\u6bcf\u4e2a\u5143\u7d20 <code>num</code> \uff0c\u5728\u6bcf\u8f6e\u8fed\u4ee3\u4e2d\u6267\u884c\uff1a</p> <ol> <li>\u5c06 <code>num</code> \u586b\u5165\u6570\u7ec4 <code>res</code> \u7684\u7d22\u5f15 <code>prefix[num] - 1</code> \u5904\u3002</li> <li>\u4ee4\u524d\u7f00\u548c <code>prefix[num]</code> \u51cf\u5c0f \\(1\\) \uff0c\u4ece\u800c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e <code>num</code> \u7684\u7d22\u5f15\u3002</li> </ol> <p>\u904d\u5386\u5b8c\u6210\u540e\uff0c\u6570\u7ec4 <code>res</code> \u4e2d\u5c31\u662f\u6392\u5e8f\u597d\u7684\u7ed3\u679c\uff0c\u6700\u540e\u4f7f\u7528 <code>res</code> \u8986\u76d6\u539f\u6570\u7ec4 <code>nums</code> \u5373\u53ef\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> \u56fe\uff1a\u8ba1\u6570\u6392\u5e8f\u6b65\u9aa4 </p> <p>\u8ba1\u6570\u6392\u5e8f\u7684\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust counting_sort.java<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(int[] nums) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nint m = 0;\nfor (int num : nums) {\nm = Math.max(m, num);\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nint[] counter = new int[m + 1];\nfor (int num : nums) {\ncounter[num]++;\n}\n// 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n// \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\nfor (int i = 0; i &lt; m; i++) {\ncounter[i + 1] += counter[i];\n}\n// 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n// \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\nint n = nums.length;\nint[] res = new int[n];\nfor (int i = n - 1; i &gt;= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n}\n// \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i &lt; n; i++) {\nnums[i] = res[i];\n}\n}\n</code></pre> counting_sort.cpp<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(vector&lt;int&gt; &amp;nums) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nint m = 0;\nfor (int num : nums) {\nm = max(m, num);\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nvector&lt;int&gt; counter(m + 1, 0);\nfor (int num : nums) {\ncounter[num]++;\n}\n// 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n// \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\nfor (int i = 0; i &lt; m; i++) {\ncounter[i + 1] += counter[i];\n}\n// 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n// \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\nint n = nums.size();\nvector&lt;int&gt; res(n);\nfor (int i = n - 1; i &gt;= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n}\n// \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\nnums = res;\n}\n</code></pre> counting_sort.py<pre><code>def counting_sort(nums: list[int]):\n\"\"\"\u8ba1\u6570\u6392\u5e8f\"\"\"\n# \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\n# 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nm = max(nums)\n# 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n# counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\ncounter = [0] * (m + 1)\nfor num in nums:\ncounter[num] += 1\n# 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n# \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\nfor i in range(m):\ncounter[i + 1] += counter[i]\n# 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n# \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\nn = len(nums)\nres = [0] * n\nfor i in range(n - 1, -1, -1):\nnum = nums[i]\nres[counter[num] - 1] = num # \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[num] -= 1 # \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n# \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i in range(n):\nnums[i] = res[i]\n</code></pre> counting_sort.go<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunc countingSort(nums []int) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nm := 0\nfor _, num := range nums {\nif num &gt; m {\nm = num\n}\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\ncounter := make([]int, m+1)\nfor _, num := range nums {\ncounter[num]++\n}\n// 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n// \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\nfor i := 0; i &lt; m; i++ {\ncounter[i+1] += counter[i]\n}\n// 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n// \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\nn := len(nums)\nres := make([]int, n)\nfor i := n - 1; i &gt;= 0; i-- {\nnum := nums[i]\n// \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\nres[counter[num]-1] = num\n// \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\ncounter[num]--\n}\n// \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\ncopy(nums, res)\n}\n</code></pre> counting_sort.js<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunction countingSort(nums) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nlet m = 0;\nfor (const num of nums) {\nm = Math.max(m, num);\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nconst counter = new Array(m + 1).fill(0);\nfor (const num of nums) {\ncounter[num]++;\n}\n// 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n// \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\nfor (let i = 0; i &lt; m; i++) {\ncounter[i + 1] += counter[i];\n}\n// 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n// \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\nconst n = nums.length;\nconst res = new Array(n);\nfor (let i = n - 1; i &gt;= 0; i--) {\nconst num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n}\n// \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (let i = 0; i &lt; n; i++) {\nnums[i] = res[i];\n}\n}\n</code></pre> counting_sort.ts<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunction countingSort(nums: number[]): void {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nlet m = 0;\nfor (const num of nums) {\nm = Math.max(m, num);\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nconst counter: number[] = new Array&lt;number&gt;(m + 1).fill(0);\nfor (const num of nums) {\ncounter[num]++;\n}\n// 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n// \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\nfor (let i = 0; i &lt; m; i++) {\ncounter[i + 1] += counter[i];\n}\n// 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n// \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\nconst n = nums.length;\nconst res: number[] = new Array&lt;number&gt;(n);\nfor (let i = n - 1; i &gt;= 0; i--) {\nconst num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n}\n// \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (let i = 0; i &lt; n; i++) {\nnums[i] = res[i];\n}\n}\n</code></pre> counting_sort.c<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(int nums[], int size) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nint m = 0;\nfor (int i = 0; i &lt; size; i++) {\nif (nums[i] &gt; m) {\nm = nums[i];\n}\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nint *counter = malloc(sizeof(int) * m);\nfor (int i = 0; i &lt; size; i++) {\ncounter[nums[i]]++;\n}\n// 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n// \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\nfor (int i = 0; i &lt; m; i++) {\ncounter[i + 1] += counter[i];\n}\n// 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n// \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\nint *res = malloc(sizeof(int) * size);\nfor (int i = size - 1; i &gt;= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n}\n// \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\nmemcpy(nums, res, size * sizeof(int));\n}\n</code></pre> counting_sort.cs<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(int[] nums) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nint m = 0;\nforeach (int num in nums) {\nm = Math.Max(m, num);\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nint[] counter = new int[m + 1];\nforeach (int num in nums) {\ncounter[num]++;\n}\n// 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n// \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\nfor (int i = 0; i &lt; m; i++) {\ncounter[i + 1] += counter[i];\n}\n// 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n// \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\nint n = nums.Length;\nint[] res = new int[n];\nfor (int i = n - 1; i &gt;= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n}\n// \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i &lt; n; i++) {\nnums[i] = res[i];\n}\n}\n</code></pre> counting_sort.swift<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfunc countingSort(nums: inout [Int]) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nlet m = nums.max()!\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nvar counter = Array(repeating: 0, count: m + 1)\nfor num in nums {\ncounter[num] += 1\n}\n// 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n// \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\nfor i in stride(from: 0, to: m, by: 1) {\ncounter[i + 1] += counter[i]\n}\n// 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n// \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\nvar res = Array(repeating: 0, count: nums.count)\nfor i in stride(from: nums.count - 1, through: 0, by: -1) {\nlet num = nums[i]\nres[counter[num] - 1] = num // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[num] -= 1 // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n}\n// \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i in stride(from: 0, to: nums.count, by: 1) {\nnums[i] = res[i]\n}\n}\n</code></pre> counting_sort.zig<pre><code>[class]{}-[func]{countingSort}\n</code></pre> counting_sort.dart<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nvoid countingSort(List&lt;int&gt; nums) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nint m = 0;\nfor (int num in nums) {\nm = max(m, num);\n}\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nList&lt;int&gt; counter = List.filled(m + 1, 0);\nfor (int num in nums) {\ncounter[num]++;\n}\n// 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n// \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\nfor (int i = 0; i &lt; m; i++) {\ncounter[i + 1] += counter[i];\n}\n// 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n// \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\nint n = nums.length;\nList&lt;int&gt; res = List.filled(n, 0);\nfor (int i = n - 1; i &gt;= 0; i--) {\nint num = nums[i];\nres[counter[num] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[num]--; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n}\n// \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\nnums.setAll(0, res);\n}\n</code></pre> counting_sort.rs<pre><code>/* \u8ba1\u6570\u6392\u5e8f */\n// \u5b8c\u6574\u5b9e\u73b0\uff0c\u53ef\u6392\u5e8f\u5bf9\u8c61\uff0c\u5e76\u4e14\u662f\u7a33\u5b9a\u6392\u5e8f\nfn counting_sort(nums: &amp;mut [i32]) {\n// 1. \u7edf\u8ba1\u6570\u7ec4\u6700\u5927\u5143\u7d20 m\nlet m = *nums.into_iter().max().unwrap();\n// 2. \u7edf\u8ba1\u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\n// counter[num] \u4ee3\u8868 num \u7684\u51fa\u73b0\u6b21\u6570\nlet mut counter = vec![0; m as usize + 1];\nfor &amp;num in &amp;*nums {\ncounter[num as usize] += 1;\n}\n// 3. \u6c42 counter \u7684\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u6b21\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u5c3e\u7d22\u5f15\u201d\n// \u5373 counter[num]-1 \u662f num \u5728 res \u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\nfor i in 0..m as usize {\ncounter[i + 1] += counter[i];\n}\n// 4. \u5012\u5e8f\u904d\u5386 nums \uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165\u7ed3\u679c\u6570\u7ec4 res\n// \u521d\u59cb\u5316\u6570\u7ec4 res \u7528\u4e8e\u8bb0\u5f55\u7ed3\u679c\nlet n = nums.len();\nlet mut res = vec![0; n];\nfor i in (0..n).rev() {\nlet num = nums[i];\nres[counter[num as usize] - 1] = num; // \u5c06 num \u653e\u7f6e\u5230\u5bf9\u5e94\u7d22\u5f15\u5904\ncounter[num as usize] -= 1; // \u4ee4\u524d\u7f00\u548c\u81ea\u51cf 1 \uff0c\u5f97\u5230\u4e0b\u6b21\u653e\u7f6e num \u7684\u7d22\u5f15\n}\n// \u4f7f\u7528\u7ed3\u679c\u6570\u7ec4 res \u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i in 0..n {\nnums[i] = res[i];\n}\n}\n</code></pre>"},{"location":"chapter_sorting/counting_sort/#1193","title":"11.9.3. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n + m)\\) \uff1a\u6d89\u53ca\u904d\u5386 <code>nums</code> \u548c\u904d\u5386 <code>counter</code> \uff0c\u90fd\u4f7f\u7528\u7ebf\u6027\u65f6\u95f4\u3002\u4e00\u822c\u60c5\u51b5\u4e0b \\(n \\gg m\\) \uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u4e8e \\(O(n)\\) \u3002</li> <li>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n + m)\\) \u3001\u975e\u539f\u5730\u6392\u5e8f \uff1a\u501f\u52a9\u4e86\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\) \u548c \\(m\\) \u7684\u6570\u7ec4 <code>res</code> \u548c <code>counter</code> \u3002</li> <li>\u7a33\u5b9a\u6392\u5e8f\uff1a\u7531\u4e8e\u5411 <code>res</code> \u4e2d\u586b\u5145\u5143\u7d20\u7684\u987a\u5e8f\u662f\u201c\u4ece\u53f3\u5411\u5de6\u201d\u7684\uff0c\u56e0\u6b64\u5012\u5e8f\u904d\u5386 <code>nums</code> \u53ef\u4ee5\u907f\u514d\u6539\u53d8\u76f8\u7b49\u5143\u7d20\u4e4b\u95f4\u7684\u76f8\u5bf9\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b9e\u73b0\u7a33\u5b9a\u6392\u5e8f\u3002\u5b9e\u9645\u4e0a\uff0c\u6b63\u5e8f\u904d\u5386 <code>nums</code> \u4e5f\u53ef\u4ee5\u5f97\u5230\u6b63\u786e\u7684\u6392\u5e8f\u7ed3\u679c\uff0c\u4f46\u7ed3\u679c\u662f\u975e\u7a33\u5b9a\u7684\u3002</li> </ul>"},{"location":"chapter_sorting/counting_sort/#1194","title":"11.9.4. \u00a0 \u5c40\u9650\u6027","text":"<p>\u770b\u5230\u8fd9\u91cc\uff0c\u4f60\u4e5f\u8bb8\u4f1a\u89c9\u5f97\u8ba1\u6570\u6392\u5e8f\u975e\u5e38\u5de7\u5999\uff0c\u4ec5\u901a\u8fc7\u7edf\u8ba1\u6570\u91cf\u5c31\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u6392\u5e8f\u5de5\u4f5c\u3002\u7136\u800c\uff0c\u4f7f\u7528\u8ba1\u6570\u6392\u5e8f\u7684\u524d\u7f6e\u6761\u4ef6\u76f8\u5bf9\u8f83\u4e3a\u4e25\u683c\u3002</p> <p>\u8ba1\u6570\u6392\u5e8f\u53ea\u9002\u7528\u4e8e\u975e\u8d1f\u6574\u6570\u3002\u82e5\u60f3\u8981\u5c06\u5176\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\uff0c\u9700\u8981\u786e\u4fdd\u8fd9\u4e9b\u6570\u636e\u53ef\u4ee5\u88ab\u8f6c\u6362\u4e3a\u975e\u8d1f\u6574\u6570\uff0c\u5e76\u4e14\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4e0d\u80fd\u6539\u53d8\u5404\u4e2a\u5143\u7d20\u4e4b\u95f4\u7684\u76f8\u5bf9\u5927\u5c0f\u5173\u7cfb\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5305\u542b\u8d1f\u6570\u7684\u6574\u6570\u6570\u7ec4\uff0c\u53ef\u4ee5\u5148\u7ed9\u6240\u6709\u6570\u5b57\u52a0\u4e0a\u4e00\u4e2a\u5e38\u6570\uff0c\u5c06\u5168\u90e8\u6570\u5b57\u8f6c\u5316\u4e3a\u6b63\u6570\uff0c\u6392\u5e8f\u5b8c\u6210\u540e\u518d\u8f6c\u6362\u56de\u53bb\u5373\u53ef\u3002</p> <p>\u8ba1\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u636e\u91cf\u5927\u4f46\u6570\u636e\u8303\u56f4\u8f83\u5c0f\u7684\u60c5\u51b5\u3002\u6bd4\u5982\uff0c\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d \\(m\\) \u4e0d\u80fd\u592a\u5927\uff0c\u5426\u5219\u4f1a\u5360\u7528\u8fc7\u591a\u7a7a\u95f4\u3002\u800c\u5f53 \\(n \\ll m\\) \u65f6\uff0c\u8ba1\u6570\u6392\u5e8f\u4f7f\u7528 \\(O(m)\\) \u65f6\u95f4\uff0c\u53ef\u80fd\u6bd4 \\(O(n \\log n)\\) \u7684\u6392\u5e8f\u7b97\u6cd5\u8fd8\u8981\u6162\u3002</p>"},{"location":"chapter_sorting/heap_sort/","title":"11.7. \u00a0 \u5806\u6392\u5e8f","text":"<p>Tip</p> <p>\u9605\u8bfb\u672c\u8282\u524d\uff0c\u8bf7\u786e\u4fdd\u5df2\u5b66\u5b8c\u300c\u5806\u300d\u7ae0\u8282\u3002</p> <p>\u300c\u5806\u6392\u5e8f Heap Sort\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5806\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\u7684\u9ad8\u6548\u6392\u5e8f\u7b97\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5df2\u7ecf\u5b66\u8fc7\u7684\u201c\u5efa\u5806\u64cd\u4f5c\u201d\u548c\u201c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u201d\u5b9e\u73b0\u5806\u6392\u5e8f\uff1a</p> <ol> <li>\u8f93\u5165\u6570\u7ec4\u5e76\u5efa\u7acb\u5c0f\u9876\u5806\uff0c\u6b64\u65f6\u6700\u5c0f\u5143\u7d20\u4f4d\u4e8e\u5806\u9876\u3002</li> <li>\u4e0d\u65ad\u6267\u884c\u51fa\u5806\u64cd\u4f5c\uff0c\u4f9d\u6b21\u8bb0\u5f55\u51fa\u5806\u5143\u7d20\uff0c\u5373\u53ef\u5f97\u5230\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u7684\u5e8f\u5217\u3002</li> </ol> <p>\u4ee5\u4e0a\u65b9\u6cd5\u867d\u7136\u53ef\u884c\uff0c\u4f46\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u989d\u5916\u6570\u7ec4\u6765\u4fdd\u5b58\u5f39\u51fa\u7684\u5143\u7d20\uff0c\u6bd4\u8f83\u6d6a\u8d39\u7a7a\u95f4\u3002\u5728\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u4e00\u79cd\u66f4\u52a0\u4f18\u96c5\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002</p>"},{"location":"chapter_sorting/heap_sort/#1171","title":"11.7.1. \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u5806\u6392\u5e8f\u7684\u6d41\u7a0b\u5982\u4e0b\uff1a</p> <ol> <li>\u8f93\u5165\u6570\u7ec4\u5e76\u5efa\u7acb\u5927\u9876\u5806\u3002\u5b8c\u6210\u540e\uff0c\u6700\u5927\u5143\u7d20\u4f4d\u4e8e\u5806\u9876\u3002</li> <li>\u5c06\u5806\u9876\u5143\u7d20\uff08\u7b2c\u4e00\u4e2a\u5143\u7d20\uff09\u4e0e\u5806\u5e95\u5143\u7d20\uff08\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff09\u4ea4\u6362\u3002\u5b8c\u6210\u4ea4\u6362\u540e\uff0c\u5806\u7684\u957f\u5ea6\u51cf \\(1\\) \uff0c\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u52a0 \\(1\\) \u3002</li> <li>\u4ece\u5806\u9876\u5143\u7d20\u5f00\u59cb\uff0c\u4ece\u9876\u5230\u5e95\u6267\u884c\u5806\u5316\u64cd\u4f5c\uff08Sift Down\uff09\u3002\u5b8c\u6210\u5806\u5316\u540e\uff0c\u5806\u7684\u6027\u8d28\u5f97\u5230\u4fee\u590d\u3002</li> <li>\u5faa\u73af\u6267\u884c\u7b2c <code>2.</code> \u548c <code>3.</code> \u6b65\u3002\u5faa\u73af \\(n - 1\\) \u8f6e\u540e\uff0c\u5373\u53ef\u5b8c\u6210\u6570\u7ec4\u6392\u5e8f\u3002</li> </ol> <p>\u5b9e\u9645\u4e0a\uff0c\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u4e2d\u4e5f\u5305\u542b\u7b2c <code>2.</code> \u548c <code>3.</code> \u6b65\uff0c\u53ea\u662f\u591a\u4e86\u4e00\u4e2a\u5f39\u51fa\u5143\u7d20\u7684\u6b65\u9aa4\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> \u56fe\uff1a\u5806\u6392\u5e8f\u6b65\u9aa4 </p> <p>\u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e0e\u5806\u7ae0\u8282\u76f8\u540c\u7684\u4ece\u9876\u81f3\u5e95\u5806\u5316\uff08Sift Down\uff09\u7684\u51fd\u6570\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e\u5806\u7684\u957f\u5ea6\u4f1a\u968f\u7740\u63d0\u53d6\u6700\u5927\u5143\u7d20\u800c\u51cf\u5c0f\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u7ed9 Sift Down \u51fd\u6570\u6dfb\u52a0\u4e00\u4e2a\u957f\u5ea6\u53c2\u6570 \\(n\\) \uff0c\u7528\u4e8e\u6307\u5b9a\u5806\u7684\u5f53\u524d\u6709\u6548\u957f\u5ea6\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust heap_sort.java<pre><code>/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int[] nums, int n, int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l &lt; n &amp;&amp; nums[l] &gt; nums[ma])\nma = l;\nif (r &lt; n &amp;&amp; nums[r] &gt; nums[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i)\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nint temp = nums[i];\nnums[i] = nums[ma];\nnums[ma] = temp;\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n/* \u5806\u6392\u5e8f */\nvoid heapSort(int[] nums) {\n// \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (int i = nums.length / 2 - 1; i &gt;= 0; i--) {\nsiftDown(nums, nums.length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = nums.length - 1; i &gt; 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nint tmp = nums[0];\nnums[0] = nums[i];\nnums[i] = tmp;\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums, i, 0);\n}\n}\n</code></pre> heap_sort.cpp<pre><code>/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(vector&lt;int&gt; &amp;nums, int n, int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l &lt; n &amp;&amp; nums[l] &gt; nums[ma])\nma = l;\nif (r &lt; n &amp;&amp; nums[r] &gt; nums[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nswap(nums[i], nums[ma]);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n/* \u5806\u6392\u5e8f */\nvoid heapSort(vector&lt;int&gt; &amp;nums) {\n// \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (int i = nums.size() / 2 - 1; i &gt;= 0; --i) {\nsiftDown(nums, nums.size(), i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = nums.size() - 1; i &gt; 0; --i) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(nums[0], nums[i]);\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums, i, 0);\n}\n}\n</code></pre> heap_sort.py<pre><code>def sift_down(nums: list[int], n: int, i: int):\n\"\"\"\u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\"\"\"\nwhile True:\n# \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nl = 2 * i + 1\nr = 2 * i + 2\nma = i\nif l &lt; n and nums[l] &gt; nums[ma]:\nma = l\nif r &lt; n and nums[r] &gt; nums[ma]:\nma = r\n# \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i:\nbreak\n# \u4ea4\u6362\u4e24\u8282\u70b9\nnums[i], nums[ma] = nums[ma], nums[i]\n# \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\ndef heap_sort(nums: list[int]):\n\"\"\"\u5806\u6392\u5e8f\"\"\"\n# \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i in range(len(nums) // 2 - 1, -1, -1):\nsift_down(nums, len(nums), i)\n# \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor i in range(len(nums) - 1, 0, -1):\n# \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nnums[0], nums[i] = nums[i], nums[0]\n# \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsift_down(nums, i, 0)\n</code></pre> heap_sort.go<pre><code>/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(nums *[]int, n, i int) {\nfor true {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nl := 2*i + 1\nr := 2*i + 2\nma := i\nif l &lt; n &amp;&amp; (*nums)[l] &gt; (*nums)[ma] {\nma = l\n}\nif r &lt; n &amp;&amp; (*nums)[r] &gt; (*nums)[ma] {\nma = r\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\n(*nums)[i], (*nums)[ma] = (*nums)[ma], (*nums)[i]\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\n}\n}\n/* \u5806\u6392\u5e8f */\nfunc heapSort(nums *[]int) {\n// \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i := len(*nums)/2 - 1; i &gt;= 0; i-- {\nsiftDown(nums, len(*nums), i)\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor i := len(*nums) - 1; i &gt; 0; i-- {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n(*nums)[0], (*nums)[i] = (*nums)[i], (*nums)[0]\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums, i, 0)\n}\n}\n</code></pre> heap_sort.js<pre><code>/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunction siftDown(nums, n, i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet l = 2 * i + 1;\nlet r = 2 * i + 2;\nlet ma = i;\nif (l &lt; n &amp;&amp; nums[l] &gt; nums[ma]) {\nma = l;\n}\nif (r &lt; n &amp;&amp; nums[r] &gt; nums[ma]) {\nma = r;\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma === i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\n[nums[i], nums[ma]] = [nums[ma], nums[i]];\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n/* \u5806\u6392\u5e8f */\nfunction heapSort(nums) {\n// \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (let i = Math.floor(nums.length / 2) - 1; i &gt;= 0; i--) {\nsiftDown(nums, nums.length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (let i = nums.length - 1; i &gt; 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n[nums[0], nums[i]] = [nums[i], nums[0]];\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums, i, 0);\n}\n}\n</code></pre> heap_sort.ts<pre><code>/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunction siftDown(nums: number[], n: number, i: number): void {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet l = 2 * i + 1;\nlet r = 2 * i + 2;\nlet ma = i;\nif (l &lt; n &amp;&amp; nums[l] &gt; nums[ma]) {\nma = l;\n}\nif (r &lt; n &amp;&amp; nums[r] &gt; nums[ma]) {\nma = r;\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma === i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\n[nums[i], nums[ma]] = [nums[ma], nums[i]];\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n/* \u5806\u6392\u5e8f */\nfunction heapSort(nums: number[]): void {\n// \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (let i = Math.floor(nums.length / 2) - 1; i &gt;= 0; i--) {\nsiftDown(nums, nums.length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (let i = nums.length - 1; i &gt; 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n[nums[0], nums[i]] = [nums[i], nums[0]];\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums, i, 0);\n}\n}\n</code></pre> heap_sort.c<pre><code>/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int nums[], int n, int i) {\nwhile (1) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l &lt; n &amp;&amp; nums[l] &gt; nums[ma])\nma = l;\nif (r &lt; n &amp;&amp; nums[r] &gt; nums[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nint temp = nums[i];\nnums[i] = nums[ma];\nnums[ma] = temp;\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n/* \u5806\u6392\u5e8f */\nvoid heapSort(int nums[], int n) {\n// \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (int i = n / 2 - 1; i &gt;= 0; --i) {\nsiftDown(nums, n, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = n - 1; i &gt; 0; --i) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nint tmp = nums[0];\nnums[0] = nums[i];\nnums[i] = tmp;\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums, i, 0);\n}\n}\n</code></pre> heap_sort.cs<pre><code>/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int[] nums, int n, int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l &lt; n &amp;&amp; nums[l] &gt; nums[ma])\nma = l;\nif (r &lt; n &amp;&amp; nums[r] &gt; nums[ma])\nma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i)\nbreak;\n// \u4ea4\u6362\u4e24\u8282\u70b9\n(nums[ma], nums[i]) = (nums[i], nums[ma]);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n/* \u5806\u6392\u5e8f */\nvoid heapSort(int[] nums) {\n// \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (int i = nums.Length / 2 - 1; i &gt;= 0; i--) {\nsiftDown(nums, nums.Length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = nums.Length - 1; i &gt; 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n(nums[i], nums[0]) = (nums[0], nums[i]);\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums, i, 0);\n}\n}\n</code></pre> heap_sort.swift<pre><code>/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(nums: inout [Int], n: Int, i: Int) {\nvar i = i\nwhile true {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet l = 2 * i + 1\nlet r = 2 * i + 2\nvar ma = i\nif l &lt; n, nums[l] &gt; nums[ma] {\nma = l\n}\nif r &lt; n, nums[r] &gt; nums[ma] {\nma = r\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nnums.swapAt(i, ma)\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\n}\n}\n/* \u5806\u6392\u5e8f */\nfunc heapSort(nums: inout [Int]) {\n// \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i in stride(from: nums.count / 2 - 1, through: 0, by: -1) {\nsiftDown(nums: &amp;nums, n: nums.count, i: i)\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor i in stride(from: nums.count - 1, to: 0, by: -1) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nnums.swapAt(0, i)\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums: &amp;nums, n: i, i: 0)\n}\n}\n</code></pre> heap_sort.zig<pre><code>[class]{}-[func]{siftDown}\n[class]{}-[func]{heapSort}\n</code></pre> heap_sort.dart<pre><code>/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(List&lt;int&gt; nums, int n, int i) {\nwhile (true) {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nint l = 2 * i + 1;\nint r = 2 * i + 2;\nint ma = i;\nif (l &lt; n &amp;&amp; nums[l] &gt; nums[ma]) ma = l;\nif (r &lt; n &amp;&amp; nums[r] &gt; nums[ma]) ma = r;\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u8282\u70b9\nint temp = nums[i];\nnums[i] = nums[ma];\nnums[ma] = temp;\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n/* \u5806\u6392\u5e8f */\nvoid heapSort(List&lt;int&gt; nums) {\n// \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor (int i = nums.length ~/ 2 - 1; i &gt;= 0; i--) {\nsiftDown(nums, nums.length, i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor (int i = nums.length - 1; i &gt; 0; i--) {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nint tmp = nums[0];\nnums[0] = nums[i];\nnums[i] = tmp;\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsiftDown(nums, i, 0);\n}\n}\n</code></pre> heap_sort.rs<pre><code>/* \u5806\u7684\u957f\u5ea6\u4e3a n \uff0c\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(nums: &amp;mut [i32], n: usize, mut i: usize) {\nloop {\n// \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\nlet l = 2 * i + 1;\nlet r = 2 * i + 2;\nlet mut ma = i;\nif l &lt; n &amp;&amp; nums[l] &gt; nums[ma] {\nma = l;\n}\nif r &lt; n &amp;&amp; nums[r] &gt; nums[ma] {\nma = r;\n}\n// \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak;\n}\n// \u4ea4\u6362\u4e24\u8282\u70b9\nlet temp = nums[i];\nnums[i] = nums[ma];\nnums[ma] = temp;\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n/* \u5806\u6392\u5e8f */\nfn heap_sort(nums: &amp;mut [i32]) {\n// \u5efa\u5806\u64cd\u4f5c\uff1a\u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\nfor i in (0..=nums.len() / 2 - 1).rev() {\nsift_down(nums, nums.len(), i);\n}\n// \u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\uff0c\u5faa\u73af n-1 \u8f6e\nfor i in (1..=nums.len() - 1).rev() {\n// \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nlet tmp = nums[0];\nnums[0] = nums[i];\nnums[i] = tmp;\n// \u4ee5\u6839\u8282\u70b9\u4e3a\u8d77\u70b9\uff0c\u4ece\u9876\u81f3\u5e95\u8fdb\u884c\u5806\u5316\nsift_down(nums, i, 0);\n}\n}\n</code></pre>"},{"location":"chapter_sorting/heap_sort/#1172","title":"11.7.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\log n)\\) \u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u5efa\u5806\u64cd\u4f5c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002\u4ece\u5806\u4e2d\u63d0\u53d6\u6700\u5927\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \uff0c\u5171\u5faa\u73af \\(n - 1\\) \u8f6e\u3002</li> <li>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \u3001\u539f\u5730\u6392\u5e8f \uff1a\u51e0\u4e2a\u6307\u9488\u53d8\u91cf\u4f7f\u7528 \\(O(1)\\) \u7a7a\u95f4\u3002\u5143\u7d20\u4ea4\u6362\u548c\u5806\u5316\u64cd\u4f5c\u90fd\u662f\u5728\u539f\u6570\u7ec4\u4e0a\u8fdb\u884c\u7684\u3002</li> <li>\u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u4ea4\u6362\u5806\u9876\u5143\u7d20\u548c\u5806\u5e95\u5143\u7d20\u65f6\uff0c\u76f8\u7b49\u5143\u7d20\u7684\u76f8\u5bf9\u4f4d\u7f6e\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u3002</li> </ul>"},{"location":"chapter_sorting/insertion_sort/","title":"11.4. \u00a0 \u63d2\u5165\u6392\u5e8f","text":"<p>\u300c\u63d2\u5165\u6392\u5e8f Insertion Sort\u300d\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u4e0e\u624b\u52a8\u6574\u7406\u4e00\u526f\u724c\u7684\u8fc7\u7a0b\u975e\u5e38\u76f8\u4f3c\u3002</p> <p>\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u5728\u672a\u6392\u5e8f\u533a\u95f4\u9009\u62e9\u4e00\u4e2a\u57fa\u51c6\u5143\u7d20\uff0c\u5c06\u8be5\u5143\u7d20\u4e0e\u5176\u5de6\u4fa7\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u5143\u7d20\u9010\u4e00\u6bd4\u8f83\u5927\u5c0f\uff0c\u5e76\u5c06\u8be5\u5143\u7d20\u63d2\u5165\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\u3002</p> <p>\u56de\u5fc6\u6570\u7ec4\u7684\u5143\u7d20\u63d2\u5165\u64cd\u4f5c\uff0c\u8bbe\u57fa\u51c6\u5143\u7d20\u4e3a <code>base</code> \uff0c\u6211\u4eec\u9700\u8981\u5c06\u4ece\u76ee\u6807\u7d22\u5f15\u5230 <code>base</code> \u4e4b\u95f4\u7684\u6240\u6709\u5143\u7d20\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\uff0c\u7136\u540e\u518d\u5c06 <code>base</code> \u8d4b\u503c\u7ed9\u76ee\u6807\u7d22\u5f15\u3002</p> <p></p> <p> \u56fe\uff1a\u5355\u6b21\u63d2\u5165\u64cd\u4f5c </p>"},{"location":"chapter_sorting/insertion_sort/#1141","title":"11.4.1. \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u63d2\u5165\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u4e0b\uff1a</p> <ol> <li>\u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6570\u7ec4\u7684\u7b2c 1 \u4e2a\u5143\u7d20\u5df2\u5b8c\u6210\u6392\u5e8f\u3002</li> <li>\u9009\u53d6\u6570\u7ec4\u7684\u7b2c 2 \u4e2a\u5143\u7d20\u4f5c\u4e3a <code>base</code> \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6570\u7ec4\u7684\u524d 2 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002</li> <li>\u9009\u53d6\u7b2c 3 \u4e2a\u5143\u7d20\u4f5c\u4e3a <code>base</code> \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6570\u7ec4\u7684\u524d 3 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002</li> <li>\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5728\u6700\u540e\u4e00\u8f6e\u4e2d\uff0c\u9009\u53d6\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a <code>base</code> \uff0c\u5c06\u5176\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u540e\uff0c\u6240\u6709\u5143\u7d20\u5747\u5df2\u6392\u5e8f\u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u63d2\u5165\u6392\u5e8f\u6d41\u7a0b </p> JavaC++PythonGoJSTSCC#SwiftZigDartRust insertion_sort.java<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int[] nums) {\n// \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\nfor (int i = 1; i &lt; nums.length; i++) {\nint base = nums[i], j = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j &gt;= 0 &amp;&amp; nums[j] &gt; base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.cpp<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(vector&lt;int&gt; &amp;nums) {\n// \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\nfor (int i = 1; i &lt; nums.size(); i++) {\nint base = nums[i], j = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j &gt;= 0 &amp;&amp; nums[j] &gt; base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.py<pre><code>def insertion_sort(nums: list[int]):\n\"\"\"\u63d2\u5165\u6392\u5e8f\"\"\"\n# \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u533a\u95f4\u4e3a [0, i-1]\nfor i in range(1, len(nums)):\nbase = nums[i]\nj = i - 1\n# \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4 [0, i-1] \u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile j &gt;= 0 and nums[j] &gt; base:\nnums[j + 1] = nums[j] # \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj -= 1\nnums[j + 1] = base # \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n</code></pre> insertion_sort.go<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfunc insertionSort(nums []int) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\nfor i := 1; i &lt; len(nums); i++ {\nbase := nums[i]\nj := i - 1\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nfor j &gt;= 0 &amp;&amp; nums[j] &gt; base {\nnums[j+1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--\n}\nnums[j+1] = base // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.js<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfunction insertionSort(nums) {\n// \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\nfor (let i = 1; i &lt; nums.length; i++) {\nlet base = nums[i],\nj = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j &gt;= 0 &amp;&amp; nums[j] &gt; base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.ts<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfunction insertionSort(nums: number[]): void {\n// \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\nfor (let i = 1; i &lt; nums.length; i++) {\nconst base = nums[i];\nlet j = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j &gt;= 0 &amp;&amp; nums[j] &gt; base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.c<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int nums[], int size) {\n// \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\nfor (int i = 1; i &lt; size; i++) {\nint base = nums[i], j = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j &gt;= 0 &amp;&amp; nums[j] &gt; base) {\n// \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nnums[j + 1] = nums[j];\nj--;\n}\n// \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\nnums[j + 1] = base;\n}\n}\n</code></pre> insertion_sort.cs<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int[] nums) {\n// \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\nfor (int i = 1; i &lt; nums.Length; i++) {\nint bas = nums[i], j = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j &gt;= 0 &amp;&amp; nums[j] &gt; bas) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = bas; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.swift<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfunc insertionSort(nums: inout [Int]) {\n// \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\nfor i in stride(from: 1, to: nums.count, by: 1) {\nlet base = nums[i]\nvar j = i - 1\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile j &gt;= 0, nums[j] &gt; base {\nnums[j + 1] = nums[j] // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj -= 1\n}\nnums[j + 1] = base // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.zig<pre><code>// \u63d2\u5165\u6392\u5e8f\nfn insertionSort(nums: []i32) void {\n// \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\nvar i: usize = 1;\nwhile (i &lt; nums.len) : (i += 1) {\nvar base = nums[i];\nvar j: usize = i;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j &gt;= 1 and nums[j - 1] &gt; base) : (j -= 1) {\nnums[j] = nums[j - 1]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n}\nnums[j] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.dart<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(List&lt;int&gt; nums) {\n// \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\nfor (int i = 1; i &lt; nums.length; i++) {\nint base = nums[i], j = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j &gt;= 0 &amp;&amp; nums[j] &gt; base) {\nnums[j + 1] = nums[j]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.rs<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfn insertion_sort(nums: &amp;mut [i32]) {\n// \u5916\u5faa\u73af\uff1a\u5df2\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a 1, 2, ..., n\nfor i in 1..nums.len() {\nlet (base, mut j) = (nums[i], (i - 1) as i32);\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5df2\u6392\u5e8f\u90e8\u5206\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile j &gt;= 0 &amp;&amp; nums[j as usize] &gt; base {\nnums[(j + 1) as usize] = nums[j as usize]; // \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj -= 1;\n}\nnums[(j + 1) as usize] = base; // \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre>"},{"location":"chapter_sorting/insertion_sort/#1142","title":"11.4.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n^2)\\) \u3001\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6bcf\u6b21\u63d2\u5165\u64cd\u4f5c\u5206\u522b\u9700\u8981\u5faa\u73af \\(n - 1\\) , \\(n-2\\) , \\(\\cdots\\) , \\(2\\) , \\(1\\) \u6b21\uff0c\u6c42\u548c\u5f97\u5230 \\(\\frac{(n - 1) n}{2}\\) \uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002\u5728\u9047\u5230\u6709\u5e8f\u6570\u636e\u65f6\uff0c\u63d2\u5165\u64cd\u4f5c\u4f1a\u63d0\u524d\u7ec8\u6b62\u3002\u5f53\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u6709\u5e8f\u65f6\uff0c\u63d2\u5165\u6392\u5e8f\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002</li> <li>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \u3001\u539f\u5730\u6392\u5e8f \uff1a\u6307\u9488 \\(i\\) , \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002</li> <li>\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u63d2\u5165\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u5c06\u5143\u7d20\u63d2\u5165\u5230\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u4fa7\uff0c\u4e0d\u4f1a\u6539\u53d8\u5b83\u4eec\u7684\u987a\u5e8f\u3002</li> </ul>"},{"location":"chapter_sorting/insertion_sort/#1143","title":"11.4.3. \u00a0 \u63d2\u5165\u6392\u5e8f\u4f18\u52bf","text":"<p>\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u800c\u6211\u4eec\u5373\u5c06\u5b66\u4e60\u7684\u5feb\u901f\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u5c3d\u7ba1\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u6bd4\u5feb\u901f\u6392\u5e8f\u66f4\u9ad8\uff0c\u4f46\u5728\u6570\u636e\u91cf\u8f83\u5c0f\u7684\u60c5\u51b5\u4e0b\uff0c\u63d2\u5165\u6392\u5e8f\u901a\u5e38\u66f4\u5feb\u3002</p> <p>\u8fd9\u4e2a\u7ed3\u8bba\u4e0e\u7ebf\u6027\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u7684\u9002\u7528\u60c5\u51b5\u7684\u7ed3\u8bba\u7c7b\u4f3c\u3002\u5feb\u901f\u6392\u5e8f\u8fd9\u7c7b \\(O(n \\log n)\\) \u7684\u7b97\u6cd5\u5c5e\u4e8e\u57fa\u4e8e\u5206\u6cbb\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5f80\u5f80\u5305\u542b\u66f4\u591a\u5355\u5143\u8ba1\u7b97\u64cd\u4f5c\u3002\u800c\u5728\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\\(n^2\\) \u548c \\(n \\log n\\) \u7684\u6570\u503c\u6bd4\u8f83\u63a5\u8fd1\uff0c\u590d\u6742\u5ea6\u4e0d\u5360\u4e3b\u5bfc\u4f5c\u7528\uff1b\u6bcf\u8f6e\u4e2d\u7684\u5355\u5143\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\u8d77\u5230\u51b3\u5b9a\u6027\u56e0\u7d20\u3002</p> <p>\u5b9e\u9645\u4e0a\uff0c\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\uff08\u4f8b\u5982 Java\uff09\u7684\u5185\u7f6e\u6392\u5e8f\u51fd\u6570\u90fd\u91c7\u7528\u4e86\u63d2\u5165\u6392\u5e8f\uff0c\u5927\u81f4\u601d\u8def\u4e3a\uff1a\u5bf9\u4e8e\u957f\u6570\u7ec4\uff0c\u91c7\u7528\u57fa\u4e8e\u5206\u6cbb\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\uff1b\u5bf9\u4e8e\u77ed\u6570\u7ec4\uff0c\u76f4\u63a5\u4f7f\u7528\u63d2\u5165\u6392\u5e8f\u3002</p> <p>\u867d\u7136\u5192\u6ce1\u6392\u5e8f\u3001\u9009\u62e9\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \uff0c\u4f46\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u63d2\u5165\u6392\u5e8f\u7684\u4f7f\u7528\u9891\u7387\u663e\u8457\u9ad8\u4e8e\u5192\u6ce1\u6392\u5e8f\u548c\u9009\u62e9\u6392\u5e8f\u3002\u8fd9\u662f\u56e0\u4e3a\uff1a</p> <ul> <li>\u5192\u6ce1\u6392\u5e8f\u57fa\u4e8e\u5143\u7d20\u4ea4\u6362\u5b9e\u73b0\uff0c\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\uff0c\u5171\u6d89\u53ca 3 \u4e2a\u5355\u5143\u64cd\u4f5c\uff1b\u63d2\u5165\u6392\u5e8f\u57fa\u4e8e\u5143\u7d20\u8d4b\u503c\u5b9e\u73b0\uff0c\u4ec5\u9700 1 \u4e2a\u5355\u5143\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u8ba1\u7b97\u5f00\u9500\u901a\u5e38\u6bd4\u63d2\u5165\u6392\u5e8f\u66f4\u9ad8\u3002</li> <li>\u9009\u62e9\u6392\u5e8f\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \u3002\u5982\u679c\u7ed9\u5b9a\u4e00\u7ec4\u90e8\u5206\u6709\u5e8f\u7684\u6570\u636e\uff0c\u63d2\u5165\u6392\u5e8f\u901a\u5e38\u6bd4\u9009\u62e9\u6392\u5e8f\u6548\u7387\u66f4\u9ad8\u3002</li> <li>\u9009\u62e9\u6392\u5e8f\u4e0d\u7a33\u5b9a\uff0c\u65e0\u6cd5\u5e94\u7528\u4e8e\u591a\u7ea7\u6392\u5e8f\u3002</li> </ul>"},{"location":"chapter_sorting/merge_sort/","title":"11.6. \u00a0 \u5f52\u5e76\u6392\u5e8f","text":"<p>\u300c\u5f52\u5e76\u6392\u5e8f Merge Sort\u300d\u57fa\u4e8e\u5206\u6cbb\u601d\u60f3\u5b9e\u73b0\u6392\u5e8f\uff0c\u5305\u542b\u201c\u5212\u5206\u201d\u548c\u201c\u5408\u5e76\u201d\u4e24\u4e2a\u9636\u6bb5\uff1a</p> <ol> <li>\u5212\u5206\u9636\u6bb5\uff1a\u901a\u8fc7\u9012\u5f52\u4e0d\u65ad\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5904\u5206\u5f00\uff0c\u5c06\u957f\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u8f6c\u6362\u4e3a\u77ed\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u3002</li> <li>\u5408\u5e76\u9636\u6bb5\uff1a\u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u5212\u5206\uff0c\u5f00\u59cb\u5408\u5e76\uff0c\u6301\u7eed\u5730\u5c06\u5de6\u53f3\u4e24\u4e2a\u8f83\u77ed\u7684\u6709\u5e8f\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u8f83\u957f\u7684\u6709\u5e8f\u6570\u7ec4\uff0c\u76f4\u81f3\u7ed3\u675f\u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u5f52\u5e76\u6392\u5e8f\u7684\u5212\u5206\u4e0e\u5408\u5e76\u9636\u6bb5 </p>"},{"location":"chapter_sorting/merge_sort/#1161","title":"11.6.1. \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u201c\u5212\u5206\u9636\u6bb5\u201d\u4ece\u9876\u81f3\u5e95\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5207\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff1a</p> <ol> <li>\u8ba1\u7b97\u6570\u7ec4\u4e2d\u70b9 <code>mid</code> \uff0c\u9012\u5f52\u5212\u5206\u5de6\u5b50\u6570\u7ec4\uff08\u533a\u95f4 <code>[left, mid]</code> \uff09\u548c\u53f3\u5b50\u6570\u7ec4\uff08\u533a\u95f4 <code>[mid + 1, right]</code> \uff09\u3002</li> <li>\u9012\u5f52\u6267\u884c\u6b65\u9aa4 <code>1.</code> \uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u533a\u95f4\u957f\u5ea6\u4e3a 1 \u65f6\uff0c\u7ec8\u6b62\u9012\u5f52\u5212\u5206\u3002</li> </ol> <p>\u201c\u5408\u5e76\u9636\u6bb5\u201d\u4ece\u5e95\u81f3\u9876\u5730\u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4ece\u957f\u5ea6\u4e3a 1 \u7684\u5b50\u6570\u7ec4\u5f00\u59cb\u5408\u5e76\uff0c\u5408\u5e76\u9636\u6bb5\u4e2d\u7684\u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u662f\u6709\u5e8f\u7684\u3002</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> \u56fe\uff1a\u5f52\u5e76\u6392\u5e8f\u6b65\u9aa4 </p> <p>\u89c2\u5bdf\u53d1\u73b0\uff0c\u5f52\u5e76\u6392\u5e8f\u7684\u9012\u5f52\u987a\u5e8f\u4e0e\u4e8c\u53c9\u6811\u7684\u540e\u5e8f\u904d\u5386\u76f8\u540c\uff0c\u5177\u4f53\u6765\u770b\uff1a</p> <ul> <li>\u540e\u5e8f\u904d\u5386\uff1a\u5148\u9012\u5f52\u5de6\u5b50\u6811\uff0c\u518d\u9012\u5f52\u53f3\u5b50\u6811\uff0c\u6700\u540e\u5904\u7406\u6839\u8282\u70b9\u3002</li> <li>\u5f52\u5e76\u6392\u5e8f\uff1a\u5148\u9012\u5f52\u5de6\u5b50\u6570\u7ec4\uff0c\u518d\u9012\u5f52\u53f3\u5b50\u6570\u7ec4\uff0c\u6700\u540e\u5904\u7406\u5408\u5e76\u3002</li> </ul> JavaC++PythonGoJSTSCC#SwiftZigDartRust merge_sort.java<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nvoid merge(int[] nums, int left, int mid, int right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nint[] tmp = Arrays.copyOfRange(nums, left, right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k &lt;= right; k++) {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif (i &gt; leftEnd)\nnums[k] = tmp[j++];\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &lt;= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\nelse if (j &gt; rightEnd || tmp[i] &lt;= tmp[j])\nnums[k] = tmp[i++];\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &gt; \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int[] nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left &gt;= right)\nreturn; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n</code></pre> merge_sort.cpp<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nvoid merge(vector&lt;int&gt; &amp;nums, int left, int mid, int right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nvector&lt;int&gt; tmp(nums.begin() + left, nums.begin() + right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k &lt;= right; k++) {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif (i &gt; leftEnd)\nnums[k] = tmp[j++];\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &lt;= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\nelse if (j &gt; rightEnd || tmp[i] &lt;= tmp[j])\nnums[k] = tmp[i++];\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &gt; \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(vector&lt;int&gt; &amp;nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left &gt;= right)\nreturn; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n</code></pre> merge_sort.py<pre><code>def merge(nums: list[int], left: int, mid: int, right: int):\n\"\"\"\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\"\"\"\n# \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n# \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\n# \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\ntmp = list(nums[left : right + 1])\n# \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nleft_start = 0\nleft_end = mid - left\n# \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nright_start = mid + 1 - left\nright_end = right - left\n# i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\ni = left_start\nj = right_start\n# \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor k in range(left, right + 1):\n# \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif i &gt; left_end:\nnums[k] = tmp[j]\nj += 1\n# \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &lt;= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\nelif j &gt; right_end or tmp[i] &lt;= tmp[j]:\nnums[k] = tmp[i]\ni += 1\n# \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &gt; \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nelse:\nnums[k] = tmp[j]\nj += 1\ndef merge_sort(nums: list[int], left: int, right: int):\n\"\"\"\u5f52\u5e76\u6392\u5e8f\"\"\"\n# \u7ec8\u6b62\u6761\u4ef6\nif left &gt;= right:\nreturn # \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n# \u5212\u5206\u9636\u6bb5\nmid = (left + right) // 2 # \u8ba1\u7b97\u4e2d\u70b9\nmerge_sort(nums, left, mid) # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmerge_sort(nums, mid + 1, right) # \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n# \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right)\n</code></pre> merge_sort.go<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nfunc merge(nums []int, left, mid, right int) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4 \u501f\u52a9 copy \u6a21\u5757\ntmp := make([]int, right-left+1)\nfor i := left; i &lt;= right; i++ {\ntmp[i-left] = nums[i]\n}\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nleftStart, leftEnd := left-left, mid-left\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nrightStart, rightEnd := mid+1-left, right-left\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\ni, j := leftStart, rightStart\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor k := left; k &lt;= right; k++ {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif i &gt; leftEnd {\nnums[k] = tmp[j]\nj++\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &lt;= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\n} else if j &gt; rightEnd || tmp[i] &lt;= tmp[j] {\nnums[k] = tmp[i]\ni++\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &gt; \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\n} else {\nnums[k] = tmp[j]\nj++\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums []int, left, right int) {\n// \u7ec8\u6b62\u6761\u4ef6\nif left &gt;= right {\nreturn\n}\n// \u5212\u5206\u9636\u6bb5\nmid := (left + right) / 2\nmergeSort(nums, left, mid)\nmergeSort(nums, mid+1, right)\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right)\n}\n</code></pre> merge_sort.js<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nfunction merge(nums, left, mid, right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp = nums.slice(left, right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet leftStart = left - left,\nleftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet rightStart = mid + 1 - left,\nrightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nlet i = leftStart,\nj = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (let k = left; k &lt;= right; k++) {\nif (i &gt; leftEnd) {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nnums[k] = tmp[j++];\n} else if (j &gt; rightEnd || tmp[i] &lt;= tmp[j]) {\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &lt;= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\nnums[k] = tmp[i++];\n} else {\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &gt; \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nnums[k] = tmp[j++];\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums, left, right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left &gt;= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nlet mid = Math.floor((left + right) / 2); // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n</code></pre> merge_sort.ts<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nfunction merge(nums: number[], left: number, mid: number, right: number): void {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp = nums.slice(left, right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet leftStart = left - left,\nleftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet rightStart = mid + 1 - left,\nrightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nlet i = leftStart,\nj = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (let k = left; k &lt;= right; k++) {\nif (i &gt; leftEnd) {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nnums[k] = tmp[j++];\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &lt;= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\n} else if (j &gt; rightEnd || tmp[i] &lt;= tmp[j]) {\nnums[k] = tmp[i++];\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &gt; \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\n} else {\nnums[k] = tmp[j++];\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums: number[], left: number, right: number): void {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left &gt;= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nlet mid = Math.floor((left + right) / 2); // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n</code></pre> merge_sort.c<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nvoid merge(int *nums, int left, int mid, int right) {\nint index;\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nint tmp[right + 1 - left];\nfor (index = left; index &lt; right + 1; index++) {\ntmp[index - left] = nums[index];\n}\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k &lt;= right; k++) {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif (i &gt; leftEnd)\nnums[k] = tmp[j++];\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &lt;= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\nelse if (j &gt; rightEnd || tmp[i] &lt;= tmp[j])\nnums[k] = tmp[i++];\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &gt; \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int *nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left &gt;= right)\nreturn; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n</code></pre> merge_sort.cs<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nvoid merge(int[] nums, int left, int mid, int right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nint[] tmp = nums[left..(right + 1)];\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k &lt;= right; k++) {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif (i &gt; leftEnd)\nnums[k] = tmp[j++];\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &lt;= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\nelse if (j &gt; rightEnd || tmp[i] &lt;= tmp[j])\nnums[k] = tmp[i++];\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &gt; \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int[] nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left &gt;= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n</code></pre> merge_sort.swift<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nfunc merge(nums: inout [Int], left: Int, mid: Int, right: Int) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp = Array(nums[left ..&lt; (right + 1)])\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet leftStart = left - left\nlet leftEnd = mid - left\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet rightStart = mid + 1 - left\nlet rightEnd = right - left\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nvar i = leftStart\nvar j = rightStart\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor k in left ... right {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif i &gt; leftEnd {\nnums[k] = tmp[j]\nj += 1\n}\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &lt;= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\nelse if j &gt; rightEnd || tmp[i] &lt;= tmp[j] {\nnums[k] = tmp[i]\ni += 1\n}\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &gt; \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nelse {\nnums[k] = tmp[j]\nj += 1\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums: inout [Int], left: Int, right: Int) {\n// \u7ec8\u6b62\u6761\u4ef6\nif left &gt;= right { // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nreturn\n}\n// \u5212\u5206\u9636\u6bb5\nlet mid = (left + right) / 2 // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums: &amp;nums, left: left, right: mid) // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums: &amp;nums, left: mid + 1, right: right) // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums: &amp;nums, left: left, mid: mid, right: right)\n}\n</code></pre> merge_sort.zig<pre><code>// \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nfn merge(nums: []i32, left: usize, mid: usize, right: usize) !void {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nvar mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);\ndefer mem_arena.deinit();\nconst mem_allocator = mem_arena.allocator();\nvar tmp = try mem_allocator.alloc(i32, right + 1 - left);\nstd.mem.copy(i32, tmp, nums[left..right+1]);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \nvar leftStart = left - left;\nvar leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \nvar rightStart = mid + 1 - left;\nvar rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nvar i = leftStart;\nvar j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nvar k = left;\nwhile (k &lt;= right) : (k += 1) {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif (i &gt; leftEnd) {\nnums[k] = tmp[j];\nj += 1;\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &lt;= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\n} else if (j &gt; rightEnd or tmp[i] &lt;= tmp[j]) {\nnums[k] = tmp[i];\ni += 1;\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &gt; \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\n} else {\nnums[k] = tmp[j];\nj += 1;\n}\n}\n}\n// \u5f52\u5e76\u6392\u5e8f\nfn mergeSort(nums: []i32, left: usize, right: usize) !void {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left &gt;= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nvar mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\ntry mergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\ntry mergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\ntry merge(nums, left, mid, right);\n}\n</code></pre> merge_sort.dart<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nvoid merge(List&lt;int&gt; nums, int left, int mid, int right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nList&lt;int&gt; tmp = nums.sublist(left, right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k &lt;= right; k++) {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif (i &gt; leftEnd)\nnums[k] = tmp[j++];\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &lt;= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\nelse if (j &gt; rightEnd || tmp[i] &lt;= tmp[j])\nnums[k] = tmp[i++];\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &gt; \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(List&lt;int&gt; nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left &gt;= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) ~/ 2; // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n</code></pre> merge_sort.rs<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nfn merge(nums: &amp;mut [i32], left: usize, mid: usize, right: usize) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp: Vec&lt;i32&gt; = nums[left..right + 1].to_vec();\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet (left_start, left_end) = (left - left, mid - left);\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet (right_start, right_end) = (mid + 1 - left, right-left);\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nlet (mut l_corrent, mut r_corrent) = (left_start, right_start);\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor k in left..right + 1 {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif l_corrent &gt; left_end {\nnums[k] = tmp[r_corrent];\nr_corrent += 1;\n}\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &lt;= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\nelse if r_corrent &gt; right_end || tmp[l_corrent] &lt;= tmp[r_corrent] {\nnums[k] = tmp[l_corrent];\nl_corrent += 1;\n}\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 &gt; \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nelse {\nnums[k] = tmp[r_corrent];\nr_corrent += 1;\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfn merge_sort(left: usize, right: usize, nums: &amp;mut [i32]) {\n// \u7ec8\u6b62\u6761\u4ef6\nif left &gt;= right { return; } // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nlet mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\nmerge_sort(left, mid, nums); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmerge_sort(mid + 1, right, nums); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n</code></pre> <p>\u5408\u5e76\u65b9\u6cd5 <code>merge()</code> \u4ee3\u7801\u4e2d\u7684\u96be\u70b9\u5305\u62ec\uff1a</p> <ul> <li>\u5728\u9605\u8bfb\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u5404\u4e2a\u53d8\u91cf\u7684\u542b\u4e49\u3002<code>nums</code> \u7684\u5f85\u5408\u5e76\u533a\u95f4\u4e3a <code>[left, right]</code> \uff0c\u4f46\u7531\u4e8e <code>tmp</code> \u4ec5\u590d\u5236\u4e86 <code>nums</code> \u8be5\u533a\u95f4\u7684\u5143\u7d20\uff0c\u56e0\u6b64 <code>tmp</code> \u5bf9\u5e94\u533a\u95f4\u4e3a <code>[0, right - left]</code> \u3002</li> <li>\u5728\u6bd4\u8f83 <code>tmp[i]</code> \u548c <code>tmp[j]</code> \u7684\u5927\u5c0f\u65f6\uff0c\u8fd8\u9700\u8003\u8651\u5b50\u6570\u7ec4\u904d\u5386\u5b8c\u6210\u540e\u7684\u7d22\u5f15\u8d8a\u754c\u95ee\u9898\uff0c\u5373 <code>i &gt; leftEnd</code> \u548c <code>j &gt; rightEnd</code> \u7684\u60c5\u51b5\u3002\u7d22\u5f15\u8d8a\u754c\u7684\u4f18\u5148\u7ea7\u662f\u6700\u9ad8\u7684\uff0c\u5982\u679c\u5de6\u5b50\u6570\u7ec4\u5df2\u7ecf\u88ab\u5408\u5e76\u5b8c\u4e86\uff0c\u90a3\u4e48\u4e0d\u9700\u8981\u7ee7\u7eed\u6bd4\u8f83\uff0c\u76f4\u63a5\u5408\u5e76\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u5373\u53ef\u3002</li> </ul>"},{"location":"chapter_sorting/merge_sort/#1162","title":"11.6.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\log n)\\) \u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u5212\u5206\u4ea7\u751f\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u6bcf\u5c42\u5408\u5e76\u7684\u603b\u64cd\u4f5c\u6570\u91cf\u4e3a \\(n\\) \uff0c\u56e0\u6b64\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002</li> <li>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3001\u975e\u539f\u5730\u6392\u5e8f \uff1a\u9012\u5f52\u6df1\u5ea6\u4e3a \\(\\log n\\) \uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002\u5408\u5e76\u64cd\u4f5c\u9700\u8981\u501f\u52a9\u8f85\u52a9\u6570\u7ec4\u5b9e\u73b0\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002</li> <li>\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u5408\u5e76\u8fc7\u7a0b\u4e2d\uff0c\u76f8\u7b49\u5143\u7d20\u7684\u6b21\u5e8f\u4fdd\u6301\u4e0d\u53d8\u3002</li> </ul>"},{"location":"chapter_sorting/merge_sort/#1163","title":"11.6.3. \u00a0 \u94fe\u8868\u6392\u5e8f *","text":"<p>\u5f52\u5e76\u6392\u5e8f\u5728\u6392\u5e8f\u94fe\u8868\u65f6\u5177\u6709\u663e\u8457\u4f18\u52bf\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(1)\\) \uff0c\u539f\u56e0\u5982\u4e0b\uff1a</p> <ul> <li>\u7531\u4e8e\u94fe\u8868\u4ec5\u9700\u6539\u53d8\u6307\u9488\u5c31\u53ef\u5b9e\u73b0\u8282\u70b9\u7684\u589e\u5220\u64cd\u4f5c\uff0c\u56e0\u6b64\u5408\u5e76\u9636\u6bb5\uff08\u5c06\u4e24\u4e2a\u77ed\u6709\u5e8f\u94fe\u8868\u5408\u5e76\u4e3a\u4e00\u4e2a\u957f\u6709\u5e8f\u94fe\u8868\uff09\u65e0\u9700\u521b\u5efa\u8f85\u52a9\u94fe\u8868\u3002</li> <li>\u901a\u8fc7\u4f7f\u7528\u201c\u8fed\u4ee3\u5212\u5206\u201d\u66ff\u4ee3\u201c\u9012\u5f52\u5212\u5206\u201d\uff0c\u53ef\u7701\u53bb\u9012\u5f52\u4f7f\u7528\u7684\u6808\u5e27\u7a7a\u95f4\u3002</li> </ul> <p>\u5177\u4f53\u5b9e\u73b0\u7ec6\u8282\u6bd4\u8f83\u590d\u6742\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u67e5\u9605\u76f8\u5173\u8d44\u6599\u8fdb\u884c\u5b66\u4e60\u3002</p>"},{"location":"chapter_sorting/quick_sort/","title":"11.5. \u00a0 \u5feb\u901f\u6392\u5e8f","text":"<p>\u300c\u5feb\u901f\u6392\u5e8f Quick Sort\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u601d\u60f3\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u8fd0\u884c\u9ad8\u6548\uff0c\u5e94\u7528\u5e7f\u6cdb\u3002</p> <p>\u5feb\u901f\u6392\u5e8f\u7684\u6838\u5fc3\u64cd\u4f5c\u662f\u300c\u54e8\u5175\u5212\u5206\u300d\uff0c\u5176\u76ee\u6807\u662f\uff1a\u9009\u62e9\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u4f5c\u4e3a\u201c\u57fa\u51c6\u6570\u201d\uff0c\u5c06\u6240\u6709\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u5de6\u4fa7\uff0c\u800c\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u53f3\u4fa7\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u54e8\u5175\u5212\u5206\u7684\u6d41\u7a0b\u4e3a\uff1a</p> <ol> <li>\u9009\u53d6\u6570\u7ec4\u6700\u5de6\u7aef\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\uff0c\u521d\u59cb\u5316\u4e24\u4e2a\u6307\u9488 <code>i</code> \u548c <code>j</code> \u5206\u522b\u6307\u5411\u6570\u7ec4\u7684\u4e24\u7aef\u3002</li> <li>\u8bbe\u7f6e\u4e00\u4e2a\u5faa\u73af\uff0c\u5728\u6bcf\u8f6e\u4e2d\u4f7f\u7528 <code>i</code>\uff08<code>j</code>\uff09\u5206\u522b\u5bfb\u627e\u7b2c\u4e00\u4e2a\u6bd4\u57fa\u51c6\u6570\u5927\uff08\u5c0f\uff09\u7684\u5143\u7d20\uff0c\u7136\u540e\u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\u3002</li> <li>\u5faa\u73af\u6267\u884c\u6b65\u9aa4 <code>2.</code> \uff0c\u76f4\u5230 <code>i</code> \u548c <code>j</code> \u76f8\u9047\u65f6\u505c\u6b62\uff0c\u6700\u540e\u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\u3002</li> </ol> <p>\u54e8\u5175\u5212\u5206\u5b8c\u6210\u540e\uff0c\u539f\u6570\u7ec4\u88ab\u5212\u5206\u6210\u4e09\u90e8\u5206\uff1a\u5de6\u5b50\u6570\u7ec4\u3001\u57fa\u51c6\u6570\u3001\u53f3\u5b50\u6570\u7ec4\uff0c\u4e14\u6ee1\u8db3\u201c\u5de6\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20 \\(\\leq\\) \u57fa\u51c6\u6570 \\(\\leq\\) \u53f3\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u53ea\u9700\u5bf9\u8fd9\u4e24\u4e2a\u5b50\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002</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> \u56fe\uff1a\u54e8\u5175\u5212\u5206\u6b65\u9aa4 </p> <p>\u5feb\u901f\u6392\u5e8f\u7684\u5206\u6cbb\u601d\u60f3</p> <p>\u54e8\u5175\u5212\u5206\u7684\u5b9e\u8d28\u662f\u5c06\u4e00\u4e2a\u8f83\u957f\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u7b80\u5316\u4e3a\u4e24\u4e2a\u8f83\u77ed\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust quick_sort.java<pre><code>/* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int[] nums, int i, int j) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint partition(int[] nums, int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.cpp<pre><code>/* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(vector&lt;int&gt; &amp;nums, int i, int j) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint partition(vector&lt;int&gt; &amp;nums, int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.py<pre><code>def partition(self, nums: list[int], left: int, right: int) -&gt; int:\n\"\"\"\u54e8\u5175\u5212\u5206\"\"\"\n# \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j = left, right\nwhile i &lt; j:\nwhile i &lt; j and nums[j] &gt;= nums[left]:\nj -= 1 # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile i &lt; j and nums[i] &lt;= nums[left]:\ni += 1 # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n# \u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n# \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n</code></pre> quick_sort.go<pre><code>/* \u54e8\u5175\u5212\u5206 */\nfunc (q *quickSort) partition(nums []int, left, right int) int {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j := left, right\nfor i &lt; j {\nfor i &lt; j &amp;&amp; nums[j] &gt;= nums[left] {\nj-- // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nfor i &lt; j &amp;&amp; nums[i] &lt;= nums[left] {\ni++ // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n// \u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n}\n// \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.js<pre><code>/* \u5143\u7d20\u4ea4\u6362 */\nswap(nums, i, j) {\nlet tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums, left, right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left,\nj = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) {\nj -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) {\ni += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n// \u5143\u7d20\u4ea4\u6362\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.ts<pre><code>/* \u5143\u7d20\u4ea4\u6362 */\nswap(nums: number[], i: number, j: number): void {\nlet tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums: number[], left: number, right: number): number {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left,\nj = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) {\nj -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) {\ni += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n// \u5143\u7d20\u4ea4\u6362\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.c<pre><code>/* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int nums[], int i, int j) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u5feb\u901f\u6392\u5e8f\u7c7b */\n// \u5feb\u901f\u6392\u5e8f\u7c7b-\u54e8\u5175\u5212\u5206\nint partition(int nums[], int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) {\n// \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nj--;\n}\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) {\n// \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\ni++;\n}\n// \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\nswap(nums, i, j);\n}\n// \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nswap(nums, i, left);\n// \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\nreturn i;\n}\n</code></pre> quick_sort.cs<pre><code>/* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int[] nums, int i, int j) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint partition(int[] nums, int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.swift<pre><code>/* \u5143\u7d20\u4ea4\u6362 */\nfunc swap(nums: inout [Int], i: Int, j: Int) {\nlet tmp = nums[i]\nnums[i] = nums[j]\nnums[j] = tmp\n}\n/* \u54e8\u5175\u5212\u5206 */\nfunc partition(nums: inout [Int], left: Int, right: Int) -&gt; Int {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nvar i = left\nvar j = right\nwhile i &lt; j {\nwhile i &lt; j, nums[j] &gt;= nums[left] {\nj -= 1 // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile i &lt; j, nums[i] &lt;= nums[left] {\ni += 1 // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nswap(nums: &amp;nums, i: i, j: j) // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums: &amp;nums, i: i, j: left) // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.zig<pre><code>// \u5143\u7d20\u4ea4\u6362\nfn swap(nums: []i32, i: usize, j: usize) void {\nvar tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n// \u54e8\u5175\u5212\u5206\nfn partition(nums: []i32, left: usize, right: usize) usize {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nvar i = left;\nvar j = right;\nwhile (i &lt; j) {\nwhile (i &lt; j and nums[j] &gt;= nums[left]) j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i &lt; j and nums[i] &lt;= nums[left]) i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.dart<pre><code>/* \u5143\u7d20\u4ea4\u6362 */\nvoid _swap(List&lt;int&gt; nums, int i, int j) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint _partition(List&lt;int&gt; nums, int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n_swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\n_swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.rs<pre><code>/* \u54e8\u5175\u5212\u5206 */\nfn partition(nums: &amp;mut [i32], left: usize, right: usize) -&gt; usize {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet (mut i, mut j) = (left, right);\nwhile i &lt; j {\nwhile i &lt; j &amp;&amp; nums[j] &gt;= nums[left] {\nj -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile i &lt; j &amp;&amp; nums[i] &lt;= nums[left] {\ni += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nnums.swap(i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nnums.swap(i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\ni // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre>"},{"location":"chapter_sorting/quick_sort/#1151","title":"11.5.1. \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"<ol> <li>\u9996\u5148\uff0c\u5bf9\u539f\u6570\u7ec4\u6267\u884c\u4e00\u6b21\u300c\u54e8\u5175\u5212\u5206\u300d\uff0c\u5f97\u5230\u672a\u6392\u5e8f\u7684\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u3002</li> <li>\u7136\u540e\uff0c\u5bf9\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5206\u522b\u9012\u5f52\u6267\u884c\u300c\u54e8\u5175\u5212\u5206\u300d\u3002</li> <li>\u6301\u7eed\u9012\u5f52\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\uff0c\u4ece\u800c\u5b8c\u6210\u6574\u4e2a\u6570\u7ec4\u7684\u6392\u5e8f\u3002</li> </ol> <p> \u56fe\uff1a\u5feb\u901f\u6392\u5e8f\u6d41\u7a0b </p> JavaC++PythonGoJSTSCC#SwiftZigDartRust quick_sort.java<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(int[] nums, int left, int right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif (left &gt;= right)\nreturn;\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\n</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\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif (left &gt;= right)\nreturn;\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.py<pre><code>def quick_sort(self, nums: list[int], left: int, right: int):\n\"\"\"\u5feb\u901f\u6392\u5e8f\"\"\"\n# \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif left &gt;= right:\nreturn\n# \u54e8\u5175\u5212\u5206\npivot = self.partition(nums, left, right)\n# \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nself.quick_sort(nums, left, pivot - 1)\nself.quick_sort(nums, pivot + 1, right)\n</code></pre> quick_sort.go<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nfunc (q *quickSort) quickSort(nums []int, left, right int) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif left &gt;= right {\nreturn\n}\n// \u54e8\u5175\u5212\u5206\npivot := q.partition(nums, left, right)\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nq.quickSort(nums, left, pivot-1)\nq.quickSort(nums, pivot+1, right)\n}\n</code></pre> quick_sort.js<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nquickSort(nums, left, right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif (left &gt;= right) return;\n// \u54e8\u5175\u5212\u5206\nconst pivot = this.partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nthis.quickSort(nums, left, pivot - 1);\nthis.quickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.ts<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nquickSort(nums: number[], left: number, right: number): void {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif (left &gt;= right) {\nreturn;\n}\n// \u54e8\u5175\u5212\u5206\nconst pivot = this.partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nthis.quickSort(nums, left, pivot - 1);\nthis.quickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.c<pre><code>/* \u5feb\u901f\u6392\u5e8f\u7c7b */\n// \u5feb\u901f\u6392\u5e8f\u7c7b-\u54e8\u5175\u5212\u5206\nint partition(int nums[], int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) {\n// \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nj--;\n}\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) {\n// \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\ni++;\n}\n// \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\nswap(nums, i, j);\n}\n// \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nswap(nums, i, left);\n// \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\nreturn i;\n}\n// \u5feb\u901f\u6392\u5e8f\u7c7b-\u5feb\u901f\u6392\u5e8f\nvoid quickSort(int nums[], int left, int right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif (left &gt;= right) {\nreturn;\n}\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.cs<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(int[] nums, int left, int right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif (left &gt;= right)\nreturn;\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.swift<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nfunc quickSort(nums: inout [Int], left: Int, right: Int) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif left &gt;= right {\nreturn\n}\n// \u54e8\u5175\u5212\u5206\nlet pivot = partition(nums: &amp;nums, left: left, right: right)\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums: &amp;nums, left: left, right: pivot - 1)\nquickSort(nums: &amp;nums, left: pivot + 1, right: right)\n}\n</code></pre> quick_sort.zig<pre><code>// \u5feb\u901f\u6392\u5e8f\nfn quickSort(nums: []i32, left: usize, right: usize) void {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif (left &gt;= right) return;\n// \u54e8\u5175\u5212\u5206\nvar pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.dart<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(List&lt;int&gt; nums, int left, int right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif (left &gt;= right) return;\n// \u54e8\u5175\u5212\u5206\nint pivot = _partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\n</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\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif left &gt;= right {\nreturn;\n}\n// \u54e8\u5175\u5212\u5206\nlet pivot = Self::partition(nums, left as usize, right as usize) as i32;\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nSelf::quick_sort(left, pivot - 1, nums);\nSelf::quick_sort(pivot + 1, right, nums);\n}\n</code></pre>"},{"location":"chapter_sorting/quick_sort/#1152","title":"11.5.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\log n)\\) \u3001\u81ea\u9002\u5e94\u6392\u5e8f \uff1a\u5728\u5e73\u5747\u60c5\u51b5\u4e0b\uff0c\u54e8\u5175\u5212\u5206\u7684\u9012\u5f52\u5c42\u6570\u4e3a \\(\\log n\\) \uff0c\u6bcf\u5c42\u4e2d\u7684\u603b\u5faa\u73af\u6570\u4e3a \\(n\\) \uff0c\u603b\u4f53\u4f7f\u7528 \\(O(n \\log n)\\) \u65f6\u95f4\u3002\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u90fd\u5c06\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\u5212\u5206\u4e3a\u957f\u5ea6\u4e3a \\(0\\) \u548c \\(n - 1\\) \u7684\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u6b64\u65f6\u9012\u5f52\u5c42\u6570\u8fbe\u5230 \\(n\\) \u5c42\uff0c\u6bcf\u5c42\u4e2d\u7684\u5faa\u73af\u6570\u4e3a \\(n\\) \uff0c\u603b\u4f53\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002</li> <li>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3001\u539f\u5730\u6392\u5e8f \uff1a\u5728\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u5012\u5e8f\u7684\u60c5\u51b5\u4e0b\uff0c\u8fbe\u5230\u6700\u5dee\u9012\u5f52\u6df1\u5ea6 \\(n\\) \uff0c\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002\u6392\u5e8f\u64cd\u4f5c\u662f\u5728\u539f\u6570\u7ec4\u4e0a\u8fdb\u884c\u7684\uff0c\u672a\u501f\u52a9\u989d\u5916\u6570\u7ec4\u3002</li> <li>\u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u54e8\u5175\u5212\u5206\u7684\u6700\u540e\u4e00\u6b65\uff0c\u57fa\u51c6\u6570\u53ef\u80fd\u4f1a\u88ab\u4ea4\u6362\u81f3\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u4fa7\u3002</li> </ul>"},{"location":"chapter_sorting/quick_sort/#1153","title":"11.5.3. \u00a0 \u5feb\u6392\u4e3a\u4ec0\u4e48\u5feb\uff1f","text":"<p>\u4ece\u540d\u79f0\u4e0a\u5c31\u80fd\u770b\u51fa\uff0c\u5feb\u901f\u6392\u5e8f\u5728\u6548\u7387\u65b9\u9762\u5e94\u8be5\u5177\u6709\u4e00\u5b9a\u7684\u4f18\u52bf\u3002\u5c3d\u7ba1\u5feb\u901f\u6392\u5e8f\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e0e\u300c\u5f52\u5e76\u6392\u5e8f\u300d\u548c\u300c\u5806\u6392\u5e8f\u300d\u76f8\u540c\uff0c\u4f46\u901a\u5e38\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u539f\u56e0\u5982\u4e0b\uff1a</p> <ul> <li>\u51fa\u73b0\u6700\u5dee\u60c5\u51b5\u7684\u6982\u7387\u5f88\u4f4e\uff1a\u867d\u7136\u5feb\u901f\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u6ca1\u6709\u5f52\u5e76\u6392\u5e8f\u7a33\u5b9a\uff0c\u4f46\u5728\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5feb\u901f\u6392\u5e8f\u80fd\u5728 \\(O(n \\log n)\\) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0b\u8fd0\u884c\u3002</li> <li>\u7f13\u5b58\u4f7f\u7528\u6548\u7387\u9ad8\uff1a\u5728\u6267\u884c\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u65f6\uff0c\u7cfb\u7edf\u53ef\u5c06\u6574\u4e2a\u5b50\u6570\u7ec4\u52a0\u8f7d\u5230\u7f13\u5b58\uff0c\u56e0\u6b64\u8bbf\u95ee\u5143\u7d20\u7684\u6548\u7387\u8f83\u9ad8\u3002\u800c\u50cf\u300c\u5806\u6392\u5e8f\u300d\u8fd9\u7c7b\u7b97\u6cd5\u9700\u8981\u8df3\u8dc3\u5f0f\u8bbf\u95ee\u5143\u7d20\uff0c\u4ece\u800c\u7f3a\u4e4f\u8fd9\u4e00\u7279\u6027\u3002</li> <li>\u590d\u6742\u5ea6\u7684\u5e38\u6570\u7cfb\u6570\u4f4e\uff1a\u5728\u4e0a\u8ff0\u4e09\u79cd\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u6bd4\u8f83\u3001\u8d4b\u503c\u3001\u4ea4\u6362\u7b49\u64cd\u4f5c\u7684\u603b\u6570\u91cf\u6700\u5c11\u3002\u8fd9\u4e0e\u300c\u63d2\u5165\u6392\u5e8f\u300d\u6bd4\u300c\u5192\u6ce1\u6392\u5e8f\u300d\u66f4\u5feb\u7684\u539f\u56e0\u7c7b\u4f3c\u3002</li> </ul>"},{"location":"chapter_sorting/quick_sort/#1154","title":"11.5.4. \u00a0 \u57fa\u51c6\u6570\u4f18\u5316","text":"<p>\u5feb\u901f\u6392\u5e8f\u5728\u67d0\u4e9b\u8f93\u5165\u4e0b\u7684\u65f6\u95f4\u6548\u7387\u53ef\u80fd\u964d\u4f4e\u3002\u4e3e\u4e00\u4e2a\u6781\u7aef\u4f8b\u5b50\uff0c\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u662f\u5b8c\u5168\u5012\u5e8f\u7684\uff0c\u7531\u4e8e\u6211\u4eec\u9009\u62e9\u6700\u5de6\u7aef\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\uff0c\u90a3\u4e48\u5728\u54e8\u5175\u5212\u5206\u5b8c\u6210\u540e\uff0c\u57fa\u51c6\u6570\u88ab\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u53f3\u7aef\uff0c\u5bfc\u81f4\u5de6\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n - 1\\) \u3001\u53f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(0\\) \u3002\u5982\u6b64\u9012\u5f52\u4e0b\u53bb\uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u540e\u7684\u53f3\u5b50\u6570\u7ec4\u957f\u5ea6\u90fd\u4e3a \\(0\\) \uff0c\u5206\u6cbb\u7b56\u7565\u5931\u6548\uff0c\u5feb\u901f\u6392\u5e8f\u9000\u5316\u4e3a\u300c\u5192\u6ce1\u6392\u5e8f\u300d\u3002</p> <p>\u4e3a\u4e86\u5c3d\u91cf\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f18\u5316\u54e8\u5175\u5212\u5206\u4e2d\u7684\u57fa\u51c6\u6570\u7684\u9009\u53d6\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u7136\u800c\uff0c\u5982\u679c\u8fd0\u6c14\u4e0d\u4f73\uff0c\u6bcf\u6b21\u90fd\u9009\u5230\u4e0d\u7406\u60f3\u7684\u57fa\u51c6\u6570\uff0c\u6548\u7387\u4ecd\u7136\u4e0d\u5c3d\u5982\u4eba\u610f\u3002</p> <p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7f16\u7a0b\u8bed\u8a00\u901a\u5e38\u751f\u6210\u7684\u662f\u201c\u4f2a\u968f\u673a\u6570\u201d\u3002\u5982\u679c\u6211\u4eec\u9488\u5bf9\u4f2a\u968f\u673a\u6570\u5e8f\u5217\u6784\u5efa\u4e00\u4e2a\u7279\u5b9a\u7684\u6d4b\u8bd5\u6837\u4f8b\uff0c\u90a3\u4e48\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u4ecd\u7136\u53ef\u80fd\u52a3\u5316\u3002</p> <p>\u4e3a\u4e86\u8fdb\u4e00\u6b65\u6539\u8fdb\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\uff08\u901a\u5e38\u4e3a\u6570\u7ec4\u7684\u9996\u3001\u5c3e\u3001\u4e2d\u70b9\u5143\u7d20\uff09\uff0c\u5e76\u5c06\u8fd9\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u57fa\u51c6\u6570\u201c\u65e2\u4e0d\u592a\u5c0f\u4e5f\u4e0d\u592a\u5927\u201d\u7684\u6982\u7387\u5c06\u5927\u5e45\u63d0\u5347\u3002\u5f53\u7136\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u9009\u53d6\u66f4\u591a\u5019\u9009\u5143\u7d20\uff0c\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002\u91c7\u7528\u8fd9\u79cd\u65b9\u6cd5\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u7684\u6982\u7387\u5927\u5927\u964d\u4f4e\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust quick_sort.java<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(int[] nums, int left, int mid, int right) {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif ((nums[left] &lt; nums[mid]) ^ (nums[left] &lt; nums[right]))\nreturn left;\nelse if ((nums[mid] &lt; nums[left]) ^ (nums[mid] &lt; nums[right]))\nreturn mid;\nelse\nreturn right;\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partition(int[] nums, int left, int right) {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.cpp<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(vector&lt;int&gt; &amp;nums, int left, int mid, int right) {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif ((nums[left] &lt; nums[mid]) ^ (nums[left] &lt; nums[right]))\nreturn left;\nelse if ((nums[mid] &lt; nums[left]) ^ (nums[mid] &lt; nums[right]))\nreturn mid;\nelse\nreturn right;\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partition(vector&lt;int&gt; &amp;nums, int left, int right) {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.py<pre><code>def median_three(self, nums: list[int], left: int, mid: int, right: int) -&gt; int:\n\"\"\"\u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\"\"\"\n# \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n# \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif (nums[left] &lt; nums[mid]) ^ (nums[left] &lt; nums[right]):\nreturn left\nelif (nums[mid] &lt; nums[left]) ^ (nums[mid] &lt; nums[right]):\nreturn mid\nreturn right\ndef partition(self, nums: list[int], left: int, right: int) -&gt; int:\n\"\"\"\u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09\"\"\"\n# \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nmed = self.median_three(nums, left, (left + right) // 2, right)\n# \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nnums[left], nums[med] = nums[med], nums[left]\n# \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j = left, right\nwhile i &lt; j:\nwhile i &lt; j and nums[j] &gt;= nums[left]:\nj -= 1 # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile i &lt; j and nums[i] &lt;= nums[left]:\ni += 1 # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n# \u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n# \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n</code></pre> quick_sort.go<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfunc (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\uff08!= \u5728\u8fd9\u91cc\u8d77\u5230\u5f02\u6216\u7684\u4f5c\u7528\uff09\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif (nums[left] &lt; nums[mid]) != (nums[left] &lt; nums[right]) {\nreturn left\n} else if (nums[mid] &lt; nums[left]) != (nums[mid] &lt; nums[right]) {\nreturn mid\n}\nreturn right\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09*/\nfunc (q *quickSortMedian) partition(nums []int, left, right int) int {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nmed := q.medianThree(nums, left, (left+right)/2, right)\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nnums[left], nums[med] = nums[med], nums[left]\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j := left, right\nfor i &lt; j {\nfor i &lt; j &amp;&amp; nums[j] &gt;= nums[left] {\nj-- //\u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nfor i &lt; j &amp;&amp; nums[i] &lt;= nums[left] {\ni++ //\u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n//\u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n}\n//\u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i //\u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.js<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nmedianThree(nums, left, mid, right) {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif ((nums[left] &lt; nums[mid]) ^ (nums[left] &lt; nums[right])) return left;\nelse if ((nums[mid] &lt; nums[left]) ^ (nums[mid] &lt; nums[right]))\nreturn mid;\nelse return right;\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\npartition(nums, left, right) {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nlet med = this.medianThree(\nnums,\nleft,\nMath.floor((left + right) / 2),\nright\n);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nthis.swap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left,\nj = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.ts<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nmedianThree(\nnums: number[],\nleft: number,\nmid: number,\nright: number\n): number {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif (Number(nums[left] &lt; nums[mid]) ^ Number(nums[left] &lt; nums[right])) {\nreturn left;\n} else if (\nNumber(nums[mid] &lt; nums[left]) ^ Number(nums[mid] &lt; nums[right])\n) {\nreturn mid;\n} else {\nreturn right;\n}\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\npartition(nums: number[], left: number, right: number): number {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nlet med = this.medianThree(\nnums,\nleft,\nMath.floor((left + right) / 2),\nright\n);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nthis.swap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left,\nj = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) {\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) {\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.c<pre><code>/* \u5feb\u901f\u6392\u5e8f\u7c7b\uff08\u4e2d\u4f4d\u57fa\u51c6\u6570\u4f18\u5316\uff09 */\n// \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nint medianThree(int nums[], int left, int mid, int right) {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif ((nums[left] &lt; nums[mid]) ^ (nums[left] &lt; nums[right]))\nreturn left;\nelse if ((nums[mid] &lt; nums[left]) ^ (nums[mid] &lt; nums[right]))\nreturn mid;\nelse\nreturn right;\n}\n/* \u5feb\u901f\u6392\u5e8f\u7c7b\uff08\u4e2d\u4f4d\u57fa\u51c6\u6570\u4f18\u5316\uff09 */\n// \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nint medianThree(int nums[], int left, int mid, int right) {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif ((nums[left] &lt; nums[mid]) ^ (nums[left] &lt; nums[right]))\nreturn left;\nelse if ((nums[mid] &lt; nums[left]) ^ (nums[mid] &lt; nums[right]))\nreturn mid;\nelse\nreturn right;\n}\n// \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09\nint partitionMedian(int nums[], int left, int right) {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.cs<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(int[] nums, int left, int mid, int right) {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif ((nums[left] &lt; nums[mid]) ^ (nums[left] &lt; nums[right]))\nreturn left;\nelse if ((nums[mid] &lt; nums[left]) ^ (nums[mid] &lt; nums[right]))\nreturn mid;\nelse\nreturn right;\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partition(int[] nums, int left, int right) {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left])\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.swift<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfunc medianThree(nums: [Int], left: Int, mid: Int, right: Int) -&gt; Int {\nif (nums[left] &lt; nums[mid]) != (nums[left] &lt; nums[right]) {\nreturn left\n} else if (nums[mid] &lt; nums[left]) != (nums[mid] &lt; nums[right]) {\nreturn mid\n} else {\nreturn right\n}\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfunc partitionMedian(nums: inout [Int], left: Int, right: Int) -&gt; Int {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nlet med = medianThree(nums: nums, left: left, mid: (left + right) / 2, right: right)\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums: &amp;nums, i: left, j: med)\nreturn partition(nums: &amp;nums, left: left, right: right)\n}\n</code></pre> quick_sort.zig<pre><code>// \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nfn medianThree(nums: []i32, left: usize, mid: usize, right: usize) usize {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif ((nums[left] &lt; nums[mid]) != (nums[left] &lt; nums[right])) {\nreturn left;\n} else if ((nums[mid] &lt; nums[left]) != (nums[mid] &lt; nums[right])) {\nreturn mid;\n} else {\nreturn right;\n}\n}\n// \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09\nfn partition(nums: []i32, left: usize, right: usize) usize {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nvar med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nvar i = left;\nvar j = right;\nwhile (i &lt; j) {\nwhile (i &lt; j and nums[j] &gt;= nums[left]) j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i &lt; j and nums[i] &lt;= nums[left]) i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.dart<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint _medianThree(List&lt;int&gt; nums, int left, int mid, int right) {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif ((nums[left] &lt; nums[mid]) ^ (nums[left] &lt; nums[right]))\nreturn left;\nelse if ((nums[mid] &lt; nums[left]) ^ (nums[mid] &lt; nums[right]))\nreturn mid;\nelse\nreturn right;\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint _partition(List&lt;int&gt; nums, int left, int right) {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nint med = _medianThree(nums, left, (left + right) ~/ 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\n_swap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i &lt; j) {\nwhile (i &lt; j &amp;&amp; nums[j] &gt;= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i &lt; j &amp;&amp; nums[i] &lt;= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n_swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\n_swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.rs<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfn median_three(nums: &amp;mut [i32], left: usize, mid: usize, right: usize) -&gt; usize {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif (nums[left] &lt; nums[mid]) ^ (nums[left] &lt; nums[right]) {\nreturn left;\n} else if (nums[mid] &lt; nums[left]) ^ (nums[mid] &lt; nums[right]) {\nreturn mid;\n} right\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfn partition(nums: &amp;mut [i32], left: usize, right: usize) -&gt; usize {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nlet med = Self::median_three(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nnums.swap(left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet (mut i, mut j) = (left, right);\nwhile i &lt; j {\nwhile i &lt; j &amp;&amp; nums[j] &gt;= nums[left] {\nj -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile i &lt; j &amp;&amp; nums[i] &lt;= nums[left] {\ni += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nnums.swap(i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nnums.swap(i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\ni // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre>"},{"location":"chapter_sorting/quick_sort/#1155","title":"11.5.5. \u00a0 \u5c3e\u9012\u5f52\u4f18\u5316","text":"<p>\u5728\u67d0\u4e9b\u8f93\u5165\u4e0b\uff0c\u5feb\u901f\u6392\u5e8f\u53ef\u80fd\u5360\u7528\u7a7a\u95f4\u8f83\u591a\u3002\u4ee5\u5b8c\u5168\u5012\u5e8f\u7684\u8f93\u5165\u6570\u7ec4\u4e3a\u4f8b\uff0c\u7531\u4e8e\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u540e\u53f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(0\\) \uff0c\u9012\u5f52\u6811\u7684\u9ad8\u5ea6\u4f1a\u8fbe\u5230 \\(n - 1\\) \uff0c\u6b64\u65f6\u9700\u8981\u5360\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002</p> <p>\u4e3a\u4e86\u9632\u6b62\u6808\u5e27\u7a7a\u95f4\u7684\u7d2f\u79ef\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6bcf\u8f6e\u54e8\u5175\u6392\u5e8f\u5b8c\u6210\u540e\uff0c\u6bd4\u8f83\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4ec5\u5bf9\u8f83\u77ed\u7684\u5b50\u6570\u7ec4\u8fdb\u884c\u9012\u5f52\u3002\u7531\u4e8e\u8f83\u77ed\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u4f1a\u8d85\u8fc7 \\(\\frac{n}{2}\\) \uff0c\u56e0\u6b64\u8fd9\u79cd\u65b9\u6cd5\u80fd\u786e\u4fdd\u9012\u5f52\u6df1\u5ea6\u4e0d\u8d85\u8fc7 \\(\\log n\\) \uff0c\u4ece\u800c\u5c06\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust quick_sort.java<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid quickSort(int[] nums, int left, int right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile (left &lt; right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left &lt; right - pivot) {\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.cpp<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid quickSort(vector&lt;int&gt; &amp;nums, int left, int right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile (left &lt; right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left &lt; right - pivot) {\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.py<pre><code>def quick_sort(self, nums: list[int], left: int, right: int):\n\"\"\"\u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09\"\"\"\n# \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile left &lt; right:\n# \u54e8\u5175\u5212\u5206\u64cd\u4f5c\npivot = self.partition(nums, left, right)\n# \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif pivot - left &lt; right - pivot:\nself.quick_sort(nums, left, pivot - 1) # \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1 # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\nelse:\nself.quick_sort(nums, pivot + 1, right) # \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1 # \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n</code></pre> quick_sort.go<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09*/\nfunc (q *quickSortTailCall) quickSort(nums []int, left, right int) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nfor left &lt; right {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\npivot := q.partition(nums, left, right)\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif pivot-left &lt; right-pivot {\nq.quickSort(nums, left, pivot-1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nq.quickSort(nums, pivot+1, right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.js<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nquickSort(nums, left, right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile (left &lt; right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = this.partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left &lt; right - pivot) {\nthis.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nthis.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.ts<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nquickSort(nums: number[], left: number, right: number): void {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile (left &lt; right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = this.partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left &lt; right - pivot) {\nthis.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nthis.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.c<pre><code>/* \u5feb\u901f\u6392\u5e8f\u7c7b\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\n// \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09\nvoid quickSortTailCall(int nums[], int left, int right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile (left &lt; right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left &lt; right - pivot) {\nquickSortTailCall(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSortTailCall(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.cs<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid quickSort(int[] nums, int left, int right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile (left &lt; right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left &lt; right - pivot) {\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.swift<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nfunc quickSortTailCall(nums: inout [Int], left: Int, right: Int) {\nvar left = left\nvar right = right\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile left &lt; right {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = partition(nums: &amp;nums, left: left, right: right)\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left) &lt; (right - pivot) {\nquickSortTailCall(nums: &amp;nums, left: left, right: pivot - 1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSortTailCall(nums: &amp;nums, left: pivot + 1, right: right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1 // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.zig<pre><code>// \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09\nfn quickSort(nums: []i32, left_: usize, right_: usize) void {\nvar left = left_;\nvar right = right_;\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nwhile (left &lt; right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nvar pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left &lt; right - pivot) {\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.dart<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid quickSort(List&lt;int&gt; nums, int left, int right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile (left &lt; right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = _partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left &lt; right - pivot) {\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.rs<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\npub fn quick_sort(mut left: i32, mut right: i32, nums: &amp;mut [i32]) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile left &lt; right {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = Self::partition(nums, left as usize, right as usize) as i32;\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif pivot - left &lt; right - pivot {\nSelf::quick_sort(left, pivot - 1, nums); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nSelf::quick_sort(pivot + 1, right, nums); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre>"},{"location":"chapter_sorting/radix_sort/","title":"11.10. \u00a0 \u57fa\u6570\u6392\u5e8f","text":"<p>\u4e0a\u4e00\u8282\u6211\u4eec\u4ecb\u7ecd\u4e86\u8ba1\u6570\u6392\u5e8f\uff0c\u5b83\u9002\u7528\u4e8e\u6570\u636e\u91cf \\(n\\) \u8f83\u5927\u4f46\u6570\u636e\u8303\u56f4 \\(m\\) \u8f83\u5c0f\u7684\u60c5\u51b5\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u5bf9 \\(n = 10^6\\) \u4e2a\u5b66\u53f7\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u5b66\u53f7\u662f\u4e00\u4e2a \\(8\\) \u4f4d\u6570\u5b57\uff0c\u8fd9\u610f\u5473\u7740\u6570\u636e\u8303\u56f4 \\(m = 10^8\\) \u975e\u5e38\u5927\uff0c\u4f7f\u7528\u8ba1\u6570\u6392\u5e8f\u9700\u8981\u5206\u914d\u5927\u91cf\u5185\u5b58\u7a7a\u95f4\uff0c\u800c\u57fa\u6570\u6392\u5e8f\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002</p> <p>\u300c\u57fa\u6570\u6392\u5e8f Radix Sort\u300d\u7684\u6838\u5fc3\u601d\u60f3\u4e0e\u8ba1\u6570\u6392\u5e8f\u4e00\u81f4\uff0c\u4e5f\u901a\u8fc7\u7edf\u8ba1\u4e2a\u6570\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u57fa\u6570\u6392\u5e8f\u5229\u7528\u6570\u5b57\u5404\u4f4d\u4e4b\u95f4\u7684\u9012\u8fdb\u5173\u7cfb\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e00\u4f4d\u8fdb\u884c\u6392\u5e8f\uff0c\u4ece\u800c\u5f97\u5230\u6700\u7ec8\u7684\u6392\u5e8f\u7ed3\u679c\u3002</p>"},{"location":"chapter_sorting/radix_sort/#11101","title":"11.10.1. \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u4ee5\u5b66\u53f7\u6570\u636e\u4e3a\u4f8b\uff0c\u5047\u8bbe\u6570\u5b57\u7684\u6700\u4f4e\u4f4d\u662f\u7b2c \\(1\\) \u4f4d\uff0c\u6700\u9ad8\u4f4d\u662f\u7b2c \\(8\\) \u4f4d\uff0c\u57fa\u6570\u6392\u5e8f\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a</p> <ol> <li>\u521d\u59cb\u5316\u4f4d\u6570 \\(k = 1\\) \u3002</li> <li>\u5bf9\u5b66\u53f7\u7684\u7b2c \\(k\\) \u4f4d\u6267\u884c\u300c\u8ba1\u6570\u6392\u5e8f\u300d\u3002\u5b8c\u6210\u540e\uff0c\u6570\u636e\u4f1a\u6839\u636e\u7b2c \\(k\\) \u4f4d\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u3002</li> <li>\u5c06 \\(k\\) \u589e\u52a0 \\(1\\) \uff0c\u7136\u540e\u8fd4\u56de\u6b65\u9aa4 <code>2.</code> \u7ee7\u7eed\u8fed\u4ee3\uff0c\u76f4\u5230\u6240\u6709\u4f4d\u90fd\u6392\u5e8f\u5b8c\u6210\u540e\u7ed3\u675f\u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u57fa\u6570\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b </p> <p>\u4e0b\u9762\u6765\u5256\u6790\u4ee3\u7801\u5b9e\u73b0\u3002\u5bf9\u4e8e\u4e00\u4e2a \\(d\\) \u8fdb\u5236\u7684\u6570\u5b57 \\(x\\) \uff0c\u8981\u83b7\u53d6\u5176\u7b2c \\(k\\) \u4f4d \\(x_k\\) \uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8ba1\u7b97\u516c\u5f0f\uff1a</p> \\[ x_k = \\lfloor\\frac{x}{d^{k-1}}\\rfloor \\bmod d \\] <p>\u5176\u4e2d \\(\\lfloor a \\rfloor\\) \u8868\u793a\u5bf9\u6d6e\u70b9\u6570 \\(a\\) \u5411\u4e0b\u53d6\u6574\uff0c\u800c \\(\\bmod \\space d\\) \u8868\u793a\u5bf9 \\(d\\) \u53d6\u4f59\u3002\u5bf9\u4e8e\u5b66\u53f7\u6570\u636e\uff0c\\(d = 10\\) \u4e14 \\(k \\in [1, 8]\\) \u3002</p> <p>\u6b64\u5916\uff0c\u6211\u4eec\u9700\u8981\u5c0f\u5e45\u6539\u52a8\u8ba1\u6570\u6392\u5e8f\u4ee3\u7801\uff0c\u4f7f\u4e4b\u53ef\u4ee5\u6839\u636e\u6570\u5b57\u7684\u7b2c \\(k\\) \u4f4d\u8fdb\u884c\u6392\u5e8f\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust radix_sort.java<pre><code>/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\nreturn (num / exp) % 10;\n}\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(int[] nums, int exp) {\n// \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\nint[] counter = new int[10];\nint n = nums.length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i &lt; n; i++) {\nint d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n}\n// \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\nfor (int i = 1; i &lt; 10; i++) {\ncounter[i] += counter[i - 1];\n}\n// \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\nint[] res = new int[n];\nfor (int i = n - 1; i &gt;= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i &lt; n; i++)\nnums[i] = res[i];\n}\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(int[] nums) {\n// \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nint m = Integer.MIN_VALUE;\nfor (int num : nums)\nif (num &gt; m)\nm = num;\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (int exp = 1; exp &lt;= m; exp *= 10)\n// \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n// k = 1 -&gt; exp = 1\n// k = 2 -&gt; exp = 10\n// \u5373 exp = 10^(k-1)\ncountingSortDigit(nums, exp);\n}\n</code></pre> radix_sort.cpp<pre><code>/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\nreturn (num / exp) % 10;\n}\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(vector&lt;int&gt; &amp;nums, int exp) {\n// \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\nvector&lt;int&gt; counter(10, 0);\nint n = nums.size();\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i &lt; n; i++) {\nint d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n}\n// \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\nfor (int i = 1; i &lt; 10; i++) {\ncounter[i] += counter[i - 1];\n}\n// \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\nvector&lt;int&gt; res(n, 0);\nfor (int i = n - 1; i &gt;= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i &lt; n; i++)\nnums[i] = res[i];\n}\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(vector&lt;int&gt; &amp;nums) {\n// \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nint m = *max_element(nums.begin(), nums.end());\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (int exp = 1; exp &lt;= m; exp *= 10)\n// \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n// k = 1 -&gt; exp = 1\n// k = 2 -&gt; exp = 10\n// \u5373 exp = 10^(k-1)\ncountingSortDigit(nums, exp);\n}\n</code></pre> radix_sort.py<pre><code>def digit(num: int, exp: int) -&gt; int:\n\"\"\"\u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1)\"\"\"\n# \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\nreturn (num // exp) % 10\ndef counting_sort_digit(nums: list[int], exp: int):\n\"\"\"\u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09\"\"\"\n# \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\ncounter = [0] * 10\nn = len(nums)\n# \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor i in range(n):\nd = digit(nums[i], exp) # \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[d] += 1 # \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n# \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\nfor i in range(1, 10):\ncounter[i] += counter[i - 1]\n# \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\nres = [0] * n\nfor i in range(n - 1, -1, -1):\nd = digit(nums[i], exp)\nj = counter[d] - 1 # \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i] # \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d] -= 1 # \u5c06 d \u7684\u6570\u91cf\u51cf 1\n# \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i in range(n):\nnums[i] = res[i]\ndef radix_sort(nums: list[int]):\n\"\"\"\u57fa\u6570\u6392\u5e8f\"\"\"\n# \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nm = max(nums)\n# \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nexp = 1\nwhile exp &lt;= m:\n# \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n# k = 1 -&gt; exp = 1\n# k = 2 -&gt; exp = 10\n# \u5373 exp = 10^(k-1)\ncounting_sort_digit(nums, exp)\nexp *= 10\n</code></pre> radix_sort.go<pre><code>/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunc digit(num, exp int) int {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\nreturn (num / exp) % 10\n}\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunc countingSortDigit(nums []int, exp int) {\n// \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\ncounter := make([]int, 10)\nn := len(nums)\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor i := 0; i &lt; n; i++ {\nd := digit(nums[i], exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[d]++ // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n}\n// \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\nfor i := 1; i &lt; 10; i++ {\ncounter[i] += counter[i-1]\n}\n// \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\nres := make([]int, n)\nfor i := n - 1; i &gt;= 0; i-- {\nd := digit(nums[i], exp)\nj := counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i] // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]-- // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i := 0; i &lt; n; i++ {\nnums[i] = res[i]\n}\n}\n/* \u57fa\u6570\u6392\u5e8f */\nfunc radixSort(nums []int) {\n// \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nmax := math.MinInt\nfor _, num := range nums {\nif num &gt; max {\nmax = num\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor exp := 1; max &gt;= exp; exp *= 10 {\n// \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n// k = 1 -&gt; exp = 1\n// k = 2 -&gt; exp = 10\n// \u5373 exp = 10^(k-1)\ncountingSortDigit(nums, exp)\n}\n}\n</code></pre> radix_sort.js<pre><code>/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunction digit(num, exp) {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\nreturn Math.floor(num / exp) % 10;\n}\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunction countingSortDigit(nums, exp) {\n// \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\nconst counter = new Array(10).fill(0);\nconst n = nums.length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (let i = 0; i &lt; n; i++) {\nconst d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n}\n// \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\nfor (let i = 1; i &lt; 10; i++) {\ncounter[i] += counter[i - 1];\n}\n// \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\nconst res = new Array(n).fill(0);\nfor (let i = n - 1; i &gt;= 0; i--) {\nconst d = digit(nums[i], exp);\nconst j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (let i = 0; i &lt; n; i++) {\nnums[i] = res[i];\n}\n}\n/* \u57fa\u6570\u6392\u5e8f */\nfunction radixSort(nums) {\n// \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nlet m = Number.MIN_VALUE;\nfor (const num of nums) {\nif (num &gt; m) {\nm = num;\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (let exp = 1; exp &lt;= m; exp *= 10) {\n// \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n// k = 1 -&gt; exp = 1\n// k = 2 -&gt; exp = 10\n// \u5373 exp = 10^(k-1)\ncountingSortDigit(nums, exp);\n}\n}\n</code></pre> radix_sort.ts<pre><code>/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunction digit(num: number, exp: number): number {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\nreturn Math.floor(num / exp) % 10;\n}\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunction countingSortDigit(nums: number[], exp: number): void {\n// \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\nconst counter = new Array(10).fill(0);\nconst n = nums.length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (let i = 0; i &lt; n; i++) {\nconst d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n}\n// \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\nfor (let i = 1; i &lt; 10; i++) {\ncounter[i] += counter[i - 1];\n}\n// \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\nconst res = new Array(n).fill(0);\nfor (let i = n - 1; i &gt;= 0; i--) {\nconst d = digit(nums[i], exp);\nconst j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (let i = 0; i &lt; n; i++) {\nnums[i] = res[i];\n}\n}\n/* \u57fa\u6570\u6392\u5e8f */\nfunction radixSort(nums: number[]): void {\n// \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nlet m = Number.MIN_VALUE;\nfor (const num of nums) {\nif (num &gt; m) {\nm = num;\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (let exp = 1; exp &lt;= m; exp *= 10) {\n// \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n// k = 1 -&gt; exp = 1\n// k = 2 -&gt; exp = 10\n// \u5373 exp = 10^(k-1)\ncountingSortDigit(nums, exp);\n}\n}\n</code></pre> radix_sort.c<pre><code>/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\nreturn (num / exp) % 10;\n}\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(int nums[], int size, int exp) {\n// \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\nint *counter = (int *)malloc((sizeof(int) * 10));\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i &lt; size; i++) {\n// \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\nint d = digit(nums[i], exp);\n// \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\ncounter[d]++;\n}\n// \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\nfor (int i = 1; i &lt; 10; i++) {\ncounter[i] += counter[i - 1];\n}\n// \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\nint *res = (int *)malloc(sizeof(int) * size);\nfor (int i = size - 1; i &gt;= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i &lt; size; i++) {\nnums[i] = res[i];\n}\n}\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(int nums[], int size) {\n// \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nint max = INT32_MIN;\nfor (size_t i = 0; i &lt; size - 1; i++) {\nif (nums[i] &gt; max) {\nmax = nums[i];\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (int exp = 1; max &gt;= exp; exp *= 10)\n// \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n// k = 1 -&gt; exp = 1\n// k = 2 -&gt; exp = 10\n// \u5373 exp = 10^(k-1)\ncountingSortDigit(nums, size, exp);\n}\n</code></pre> radix_sort.cs<pre><code>/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\nreturn (num / exp) % 10;\n}\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(int[] nums, int exp) {\n// \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\nint[] counter = new int[10];\nint n = nums.Length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i &lt; n; i++) {\nint d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n}\n// \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\nfor (int i = 1; i &lt; 10; i++) {\ncounter[i] += counter[i - 1];\n}\n// \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\nint[] res = new int[n];\nfor (int i = n - 1; i &gt;= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i &lt; n; i++) {\nnums[i] = res[i];\n}\n}\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(int[] nums) {\n// \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nint m = int.MinValue;\nforeach (int num in nums) {\nif (num &gt; m) m = num;\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (int exp = 1; exp &lt;= m; exp *= 10) {\n// \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n// k = 1 -&gt; exp = 1\n// k = 2 -&gt; exp = 10\n// \u5373 exp = 10^(k-1)\ncountingSortDigit(nums, exp);\n}\n}\n</code></pre> radix_sort.swift<pre><code>/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfunc digit(num: Int, exp: Int) -&gt; Int {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\n(num / exp) % 10\n}\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfunc countingSortDigit(nums: inout [Int], exp: Int) {\n// \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\nvar counter = Array(repeating: 0, count: 10)\nlet n = nums.count\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor i in nums.indices {\nlet d = digit(num: nums[i], exp: exp) // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[d] += 1 // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n}\n// \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\nfor i in 1 ..&lt; 10 {\ncounter[i] += counter[i - 1]\n}\n// \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\nvar res = Array(repeating: 0, count: n)\nfor i in stride(from: n - 1, through: 0, by: -1) {\nlet d = digit(num: nums[i], exp: exp)\nlet j = counter[d] - 1 // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i] // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d] -= 1 // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i in nums.indices {\nnums[i] = res[i]\n}\n}\n/* \u57fa\u6570\u6392\u5e8f */\nfunc radixSort(nums: inout [Int]) {\n// \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nvar m = Int.min\nfor num in nums {\nif num &gt; m {\nm = num\n}\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor exp in sequence(first: 1, next: { m &gt;= ($0 * 10) ? $0 * 10 : nil }) {\n// \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n// k = 1 -&gt; exp = 1\n// k = 2 -&gt; exp = 10\n// \u5373 exp = 10^(k-1)\ncountingSortDigit(nums: &amp;nums, exp: exp)\n}\n}\n</code></pre> radix_sort.zig<pre><code>// \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1)\nfn digit(num: i32, exp: i32) i32 {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\nreturn @mod(@divFloor(num, exp), 10);\n}\n// \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09\nfn countingSortDigit(nums: []i32, exp: i32) !void {\n// \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\nvar mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);\n// defer mem_arena.deinit();\nconst mem_allocator = mem_arena.allocator();\nvar counter = try mem_allocator.alloc(usize, 10);\n@memset(counter, 0);\nvar n = nums.len;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (nums) |num| {\nvar d: u32 = @bitCast(digit(num, exp)); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[d] += 1; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n}\n// \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\nvar i: usize = 1;\nwhile (i &lt; 10) : (i += 1) {\ncounter[i] += counter[i - 1];\n}\n// \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\nvar res = try mem_allocator.alloc(i32, n);\ni = n - 1;\nwhile (i &gt;= 0) : (i -= 1) {\nvar d: u32 = @bitCast(digit(nums[i], exp));\nvar j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d] -= 1; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\nif (i == 0) break;\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\ni = 0;\nwhile (i &lt; n) : (i += 1) {\nnums[i] = res[i];\n}\n}\n// \u57fa\u6570\u6392\u5e8f\nfn radixSort(nums: []i32) !void {\n// \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nvar m: i32 = std.math.minInt(i32);\nfor (nums) |num| {\nif (num &gt; m) m = num;\n}\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nvar exp: i32 = 1;\nwhile (exp &lt;= m) : (exp *= 10) {\n// \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n// k = 1 -&gt; exp = 1\n// k = 2 -&gt; exp = 10\n// \u5373 exp = 10^(k-1)\ntry countingSortDigit(nums, exp); }\n} </code></pre> radix_sort.dart<pre><code>/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nint digit(int num, int exp) {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\nreturn (num ~/ exp) % 10;\n}\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nvoid countingSortDigit(List&lt;int&gt; nums, int exp) {\n// \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\nList&lt;int&gt; counter = List&lt;int&gt;.filled(10, 0);\nint n = nums.length;\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor (int i = 0; i &lt; n; i++) {\nint d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[d]++; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n}\n// \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\nfor (int i = 1; i &lt; 10; i++) {\ncounter[i] += counter[i - 1];\n}\n// \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\nList&lt;int&gt; res = List&lt;int&gt;.filled(n, 0);\nfor (int i = n - 1; i &gt;= 0; i--) {\nint d = digit(nums[i], exp);\nint j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d]--; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor (int i = 0; i &lt; n; i++) nums[i] = res[i];\n}\n/* \u57fa\u6570\u6392\u5e8f */\nvoid radixSort(List&lt;int&gt; nums) {\n// \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\n// dart \u4e2d int \u7684\u957f\u5ea6\u662f 64 \u4f4d\u7684\nint m = -1 &lt;&lt; 63;\nfor (int num in nums) if (num &gt; m) m = num;\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nfor (int exp = 1; exp &lt;= m; exp *= 10)\n// \u5bf9\u6570\u7ec4\u5143\u7d20\u7684\u7b2c k \u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\n// k = 1 -&gt; exp = 1\n// k = 2 -&gt; exp = 10\n// \u5373 exp = 10^(k-1)\ncountingSortDigit(nums, exp);\n}\n</code></pre> radix_sort.rs<pre><code>/* \u83b7\u53d6\u5143\u7d20 num \u7684\u7b2c k \u4f4d\uff0c\u5176\u4e2d exp = 10^(k-1) */\nfn digit(num: i32, exp: i32) -&gt; usize {\n// \u4f20\u5165 exp \u800c\u975e k \u53ef\u4ee5\u907f\u514d\u5728\u6b64\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u6b21\u65b9\u8ba1\u7b97\nreturn ((num / exp) % 10) as usize;\n}\n/* \u8ba1\u6570\u6392\u5e8f\uff08\u6839\u636e nums \u7b2c k \u4f4d\u6392\u5e8f\uff09 */\nfn counting_sort_digit(nums: &amp;mut [i32], exp: i32) {\n// \u5341\u8fdb\u5236\u7684\u4f4d\u8303\u56f4\u4e3a 0~9 \uff0c\u56e0\u6b64\u9700\u8981\u957f\u5ea6\u4e3a 10 \u7684\u6876\nlet mut counter = [0; 10];\nlet n = nums.len();\n// \u7edf\u8ba1 0~9 \u5404\u6570\u5b57\u7684\u51fa\u73b0\u6b21\u6570\nfor i in 0..n {\nlet d = digit(nums[i], exp); // \u83b7\u53d6 nums[i] \u7b2c k \u4f4d\uff0c\u8bb0\u4e3a d\ncounter[d] += 1; // \u7edf\u8ba1\u6570\u5b57 d \u7684\u51fa\u73b0\u6b21\u6570\n}\n// \u6c42\u524d\u7f00\u548c\uff0c\u5c06\u201c\u51fa\u73b0\u4e2a\u6570\u201d\u8f6c\u6362\u4e3a\u201c\u6570\u7ec4\u7d22\u5f15\u201d\nfor i in 1..10 {\ncounter[i] += counter[i - 1];\n}\n// \u5012\u5e8f\u904d\u5386\uff0c\u6839\u636e\u6876\u5185\u7edf\u8ba1\u7ed3\u679c\uff0c\u5c06\u5404\u5143\u7d20\u586b\u5165 res\nlet mut res = vec![0; n];\nfor i in (0..n).rev() {\nlet d = digit(nums[i], exp);\nlet j = counter[d] - 1; // \u83b7\u53d6 d \u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15 j\nres[j] = nums[i]; // \u5c06\u5f53\u524d\u5143\u7d20\u586b\u5165\u7d22\u5f15 j\ncounter[d] -= 1; // \u5c06 d \u7684\u6570\u91cf\u51cf 1\n}\n// \u4f7f\u7528\u7ed3\u679c\u8986\u76d6\u539f\u6570\u7ec4 nums\nfor i in 0..n {\nnums[i] = res[i];\n}\n}\n/* \u57fa\u6570\u6392\u5e8f */\nfn radix_sort(nums: &amp;mut [i32]) {\n// \u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u5143\u7d20\uff0c\u7528\u4e8e\u5224\u65ad\u6700\u5927\u4f4d\u6570\nlet m = *nums.into_iter().max().unwrap();\n// \u6309\u7167\u4ece\u4f4e\u4f4d\u5230\u9ad8\u4f4d\u7684\u987a\u5e8f\u904d\u5386\nlet mut exp = 1;\nwhile exp &lt;= m {\ncounting_sort_digit(nums, exp);\nexp *= 10;\n}\n}\n</code></pre> <p>\u4e3a\u4ec0\u4e48\u4ece\u6700\u4f4e\u4f4d\u5f00\u59cb\u6392\u5e8f\uff1f</p> <p>\u5728\u8fde\u7eed\u7684\u6392\u5e8f\u8f6e\u6b21\u4e2d\uff0c\u540e\u4e00\u8f6e\u6392\u5e8f\u4f1a\u8986\u76d6\u524d\u4e00\u8f6e\u6392\u5e8f\u7684\u7ed3\u679c\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5982\u679c\u7b2c\u4e00\u8f6e\u6392\u5e8f\u7ed3\u679c \\(a &lt; b\\) \uff0c\u800c\u7b2c\u4e8c\u8f6e\u6392\u5e8f\u7ed3\u679c \\(a &gt; b\\) \uff0c\u90a3\u4e48\u7b2c\u4e8c\u8f6e\u7684\u7ed3\u679c\u5c06\u53d6\u4ee3\u7b2c\u4e00\u8f6e\u7684\u7ed3\u679c\u3002\u7531\u4e8e\u6570\u5b57\u7684\u9ad8\u4f4d\u4f18\u5148\u7ea7\u9ad8\u4e8e\u4f4e\u4f4d\uff0c\u6211\u4eec\u5e94\u8be5\u5148\u6392\u5e8f\u4f4e\u4f4d\u518d\u6392\u5e8f\u9ad8\u4f4d\u3002</p>"},{"location":"chapter_sorting/radix_sort/#11102","title":"11.10.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"<p>\u76f8\u8f83\u4e8e\u8ba1\u6570\u6392\u5e8f\uff0c\u57fa\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u503c\u8303\u56f4\u8f83\u5927\u7684\u60c5\u51b5\uff0c\u4f46\u524d\u63d0\u662f\u6570\u636e\u5fc5\u987b\u53ef\u4ee5\u8868\u793a\u4e3a\u56fa\u5b9a\u4f4d\u6570\u7684\u683c\u5f0f\uff0c\u4e14\u4f4d\u6570\u4e0d\u80fd\u8fc7\u5927\u3002\u4f8b\u5982\uff0c\u6d6e\u70b9\u6570\u4e0d\u9002\u5408\u4f7f\u7528\u57fa\u6570\u6392\u5e8f\uff0c\u56e0\u4e3a\u5176\u4f4d\u6570 \\(k\\) \u8fc7\u5927\uff0c\u53ef\u80fd\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(nk) \\gg O(n^2)\\) \u3002</p> <ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(nk)\\) \uff1a\u8bbe\u6570\u636e\u91cf\u4e3a \\(n\\) \u3001\u6570\u636e\u4e3a \\(d\\) \u8fdb\u5236\u3001\u6700\u5927\u4f4d\u6570\u4e3a \\(k\\) \uff0c\u5219\u5bf9\u67d0\u4e00\u4f4d\u6267\u884c\u8ba1\u6570\u6392\u5e8f\u4f7f\u7528 \\(O(n + d)\\) \u65f6\u95f4\uff0c\u6392\u5e8f\u6240\u6709 \\(k\\) \u4f4d\u4f7f\u7528 \\(O((n + d)k)\\) \u65f6\u95f4\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\\(d\\) \u548c \\(k\\) \u90fd\u76f8\u5bf9\u8f83\u5c0f\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411 \\(O(n)\\) \u3002</li> <li>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n + d)\\) \u3001\u975e\u539f\u5730\u6392\u5e8f \uff1a\u4e0e\u8ba1\u6570\u6392\u5e8f\u76f8\u540c\uff0c\u57fa\u6570\u6392\u5e8f\u9700\u8981\u501f\u52a9\u957f\u5ea6\u4e3a \\(n\\) \u548c \\(d\\) \u7684\u6570\u7ec4 <code>res</code> \u548c <code>counter</code> \u3002</li> <li>\u7a33\u5b9a\u6392\u5e8f\uff1a\u4e0e\u8ba1\u6570\u6392\u5e8f\u76f8\u540c\u3002</li> </ul>"},{"location":"chapter_sorting/selection_sort/","title":"11.2. \u00a0 \u9009\u62e9\u6392\u5e8f","text":"<p>\u300c\u9009\u62e9\u6392\u5e8f Selection Sort\u300d\u7684\u5de5\u4f5c\u539f\u7406\u975e\u5e38\u76f4\u63a5\uff1a\u5f00\u542f\u4e00\u4e2a\u5faa\u73af\uff0c\u6bcf\u8f6e\u4ece\u672a\u6392\u5e8f\u533a\u95f4\u9009\u62e9\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u5c06\u5176\u653e\u5230\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u672b\u5c3e\u3002</p> <p>\u8bbe\u6570\u7ec4\u7684\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u9009\u62e9\u6392\u5e8f\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u4e0b\uff1a</p> <ol> <li>\u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6240\u6709\u5143\u7d20\u672a\u6392\u5e8f\uff0c\u5373\u672a\u6392\u5e8f\uff08\u7d22\u5f15\uff09\u533a\u95f4\u4e3a \\([0, n-1]\\) \u3002</li> <li>\u9009\u53d6\u533a\u95f4 \\([0, n-1]\\) \u4e2d\u7684\u6700\u5c0f\u5143\u7d20\uff0c\u5c06\u5176\u4e0e\u7d22\u5f15 \\(0\\) \u5904\u5143\u7d20\u4ea4\u6362\u3002\u5b8c\u6210\u540e\uff0c\u6570\u7ec4\u524d 1 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002</li> <li>\u9009\u53d6\u533a\u95f4 \\([1, n-1]\\) \u4e2d\u7684\u6700\u5c0f\u5143\u7d20\uff0c\u5c06\u5176\u4e0e\u7d22\u5f15 \\(1\\) \u5904\u5143\u7d20\u4ea4\u6362\u3002\u5b8c\u6210\u540e\uff0c\u6570\u7ec4\u524d 2 \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002</li> <li>\u4ee5\u6b64\u7c7b\u63a8\u3002\u7ecf\u8fc7 \\(n - 1\\) \u8f6e\u9009\u62e9\u4e0e\u4ea4\u6362\u540e\uff0c\u6570\u7ec4\u524d \\(n - 1\\) \u4e2a\u5143\u7d20\u5df2\u6392\u5e8f\u3002</li> <li>\u4ec5\u5269\u7684\u4e00\u4e2a\u5143\u7d20\u5fc5\u5b9a\u662f\u6700\u5927\u5143\u7d20\uff0c\u65e0\u9700\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002</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> \u56fe\uff1a\u9009\u62e9\u6392\u5e8f\u6b65\u9aa4 </p> <p>\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7528 \\(k\\) \u6765\u8bb0\u5f55\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust selection_sort.java<pre><code>/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int[] nums) {\nint n = nums.length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (int i = 0; i &lt; n - 1; i++) {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nint k = i;\nfor (int j = i + 1; j &lt; n; j++) {\nif (nums[j] &lt; nums[k])\nk = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n}\n// \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\nint temp = nums[i];\nnums[i] = nums[k];\nnums[k] = temp;\n}\n}\n</code></pre> selection_sort.cpp<pre><code>/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(vector&lt;int&gt; &amp;nums) {\nint n = nums.size();\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (int i = 0; i &lt; n - 1; i++) {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nint k = i;\nfor (int j = i + 1; j &lt; n; j++) {\nif (nums[j] &lt; nums[k])\nk = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n}\n// \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\nswap(nums[i], nums[k]);\n}\n}\n</code></pre> selection_sort.py<pre><code>def selection_sort(nums: list[int]):\n\"\"\"\u9009\u62e9\u6392\u5e8f\"\"\"\nn = len(nums)\n# \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor i in range(n - 1):\n# \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nk = i\nfor j in range(i + 1, n):\nif nums[j] &lt; nums[k]:\nk = j # \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n# \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\nnums[i], nums[k] = nums[k], nums[i]\n</code></pre> selection_sort.go<pre><code>/* \u9009\u62e9\u6392\u5e8f */\nfunc selectionSort(nums []int) {\nn := len(nums)\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor i := 0; i &lt; n-1; i++ {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nk := i\nfor j := i + 1; j &lt; n; j++ {\nif nums[j] &lt; nums[k] {\n// \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\nk = j\n}\n}\n// \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\nnums[i], nums[k] = nums[k], nums[i]\n}\n}\n</code></pre> selection_sort.js<pre><code>/* \u9009\u62e9\u6392\u5e8f */\nfunction selectionSort(nums) {\nlet n = nums.length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (let i = 0; i &lt; n - 1; i++) {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nlet k = i;\nfor (let j = i + 1; j &lt; n; j++) {\nif (nums[j] &lt; nums[k]) {\nk = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n}\n}\n// \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n[nums[i], nums[k]] = [nums[k], nums[i]];\n}\n}\n</code></pre> selection_sort.ts<pre><code>/* \u9009\u62e9\u6392\u5e8f */\nfunction selectionSort(nums: number[]): void {\nlet n = nums.length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (let i = 0; i &lt; n - 1; i++) {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nlet k = i;\nfor (let j = i + 1; j &lt; n; j++) {\nif (nums[j] &lt; nums[k]) {\nk = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n}\n}\n// \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n[nums[i], nums[k]] = [nums[k], nums[i]];\n}\n}\n</code></pre> selection_sort.c<pre><code>/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int nums[], int n) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (int i = 0; i &lt; n - 1; i++) {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nint k = i;\nfor (int j = i + 1; j &lt; n; j++) {\nif (nums[j] &lt; nums[k])\nk = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n}\n// \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\nint temp = nums[i];\nnums[i] = nums[k];\nnums[k] = temp;\n}\n}\n</code></pre> selection_sort.cs<pre><code>/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(int[] nums) {\nint n = nums.Length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (int i = 0; i &lt; n - 1; i++) {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nint k = i;\nfor (int j = i + 1; j &lt; n; j++) {\nif (nums[j] &lt; nums[k])\nk = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n}\n// \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\n(nums[k], nums[i]) = (nums[i], nums[k]);\n}\n}\n</code></pre> selection_sort.swift<pre><code>/* \u9009\u62e9\u6392\u5e8f */\nfunc selectionSort(nums: inout [Int]) {\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor i in nums.indices.dropLast() {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nvar k = i\nfor j in nums.indices.dropFirst(i + 1) {\nif nums[j] &lt; nums[k] {\nk = j // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n}\n}\n// \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\nnums.swapAt(i, k)\n}\n}\n</code></pre> selection_sort.zig<pre><code>[class]{}-[func]{selectionSort}\n</code></pre> selection_sort.dart<pre><code>/* \u9009\u62e9\u6392\u5e8f */\nvoid selectionSort(List&lt;int&gt; nums) {\nint n = nums.length;\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor (int i = 0; i &lt; n - 1; i++) {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nint k = i;\nfor (int j = i + 1; j &lt; n; j++) {\nif (nums[j] &lt; nums[k]) k = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n}\n// \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\nint temp = nums[i];\nnums[i] = nums[k];\nnums[k] = temp;\n}\n}\n</code></pre> selection_sort.rs<pre><code>/* \u9009\u62e9\u6392\u5e8f */\nfn selection_sort(nums: &amp;mut [i32]) {\nlet n = nums.len();\n// \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [i, n-1]\nfor i in 0..n-1 {\n// \u5185\u5faa\u73af\uff1a\u627e\u5230\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u6700\u5c0f\u5143\u7d20\nlet mut k = i;\nfor j in i+1..n {\nif nums[j] &lt; nums[k] {\nk = j; // \u8bb0\u5f55\u6700\u5c0f\u5143\u7d20\u7684\u7d22\u5f15\n}\n}\n// \u5c06\u8be5\u6700\u5c0f\u5143\u7d20\u4e0e\u672a\u6392\u5e8f\u533a\u95f4\u7684\u9996\u4e2a\u5143\u7d20\u4ea4\u6362\nnums.swap(i, k);\n}\n}\n</code></pre>"},{"location":"chapter_sorting/selection_sort/#1121","title":"11.2.1. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3001\u975e\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5916\u5faa\u73af\u5171 \\(n - 1\\) \u8f6e\uff0c\u7b2c\u4e00\u8f6e\u7684\u672a\u6392\u5e8f\u533a\u95f4\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u6700\u540e\u4e00\u8f6e\u7684\u672a\u6392\u5e8f\u533a\u95f4\u957f\u5ea6\u4e3a \\(2\\) \uff0c\u5373\u5404\u8f6e\u5916\u5faa\u73af\u5206\u522b\u5305\u542b \\(n\\) , \\(n - 1\\) , \\(\\cdots\\) , \\(2\\) \u8f6e\u5185\u5faa\u73af\uff0c\u6c42\u548c\u4e3a \\(\\frac{(n - 1)(n + 2)}{2}\\) \u3002</li> <li>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488 \\(i\\) , \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002</li> <li>\u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u4ea4\u6362\u5143\u7d20\u65f6\uff0c\u6709\u53ef\u80fd\u5c06 <code>nums[i]</code> \u4ea4\u6362\u81f3\u5176\u76f8\u7b49\u5143\u7d20\u7684\u53f3\u8fb9\uff0c\u5bfc\u81f4\u4e24\u8005\u7684\u76f8\u5bf9\u987a\u5e8f\u53d1\u751f\u6539\u53d8\u3002</li> </ul> <p> \u56fe\uff1a\u9009\u62e9\u6392\u5e8f\u975e\u7a33\u5b9a\u793a\u4f8b </p>"},{"location":"chapter_sorting/sorting_algorithm/","title":"11.1. \u00a0 \u6392\u5e8f\u7b97\u6cd5","text":"<p>\u300c\u6392\u5e8f\u7b97\u6cd5 Sorting Algorithm\u300d\u7528\u4e8e\u5bf9\u4e00\u7ec4\u6570\u636e\u6309\u7167\u7279\u5b9a\u987a\u5e8f\u8fdb\u884c\u6392\u5217\u3002\u6392\u5e8f\u7b97\u6cd5\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u56e0\u4e3a\u6709\u5e8f\u6570\u636e\u901a\u5e38\u80fd\u591f\u88ab\u66f4\u6709\u6548\u5730\u67e5\u627e\u3001\u5206\u6790\u548c\u5904\u7406\u3002</p> <p>\u5728\u6392\u5e8f\u7b97\u6cd5\u4e2d\uff0c\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u662f\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7b49\uff1b\u987a\u5e8f\u7684\u5224\u65ad\u89c4\u5219\u53ef\u6839\u636e\u9700\u6c42\u8bbe\u5b9a\uff0c\u5982\u6570\u5b57\u5927\u5c0f\u3001\u5b57\u7b26 ASCII \u7801\u987a\u5e8f\u6216\u81ea\u5b9a\u4e49\u89c4\u5219\u3002</p> <p></p> <p> \u56fe\uff1a\u6570\u636e\u7c7b\u578b\u548c\u5224\u65ad\u89c4\u5219\u793a\u4f8b </p>"},{"location":"chapter_sorting/sorting_algorithm/#1111","title":"11.1.1. \u00a0 \u8bc4\u4ef7\u7ef4\u5ea6","text":"<p>\u8fd0\u884c\u6548\u7387\uff1a\u6211\u4eec\u671f\u671b\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c3d\u91cf\u4f4e\uff0c\u4e14\u603b\u4f53\u64cd\u4f5c\u6570\u91cf\u8f83\u5c11\uff08\u5373\u65f6\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u964d\u4f4e\uff09\u3002\u5bf9\u4e8e\u5927\u6570\u636e\u91cf\u60c5\u51b5\uff0c\u8fd0\u884c\u6548\u7387\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002</p> <p>\u5c31\u5730\u6027\uff1a\u987e\u540d\u601d\u4e49\uff0c\u300c\u539f\u5730\u6392\u5e8f\u300d\u901a\u8fc7\u5728\u539f\u6570\u7ec4\u4e0a\u76f4\u63a5\u64cd\u4f5c\u5b9e\u73b0\u6392\u5e8f\uff0c\u65e0\u9700\u501f\u52a9\u989d\u5916\u7684\u8f85\u52a9\u6570\u7ec4\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u539f\u5730\u6392\u5e8f\u7684\u6570\u636e\u642c\u8fd0\u64cd\u4f5c\u8f83\u5c11\uff0c\u8fd0\u884c\u901f\u5ea6\u4e5f\u66f4\u5feb\u3002</p> <p>\u7a33\u5b9a\u6027\uff1a\u300c\u7a33\u5b9a\u6392\u5e8f\u300d\u5728\u5b8c\u6210\u6392\u5e8f\u540e\uff0c\u76f8\u7b49\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u76f8\u5bf9\u987a\u5e8f\u4e0d\u53d1\u751f\u6539\u53d8\u3002\u7a33\u5b9a\u6392\u5e8f\u662f\u4f18\u826f\u7279\u6027\uff0c\u4e5f\u662f\u591a\u7ea7\u6392\u5e8f\u573a\u666f\u7684\u5fc5\u8981\u6761\u4ef6\u3002</p> <p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b58\u50a8\u5b66\u751f\u4fe1\u606f\u7684\u8868\u683c\uff0c\u7b2c 1, 2 \u5217\u5206\u522b\u662f\u59d3\u540d\u548c\u5e74\u9f84\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u300c\u975e\u7a33\u5b9a\u6392\u5e8f\u300d\u53ef\u80fd\u5bfc\u81f4\u8f93\u5165\u6570\u636e\u7684\u6709\u5e8f\u6027\u4e27\u5931\u3002</p> <pre><code># \u8f93\u5165\u6570\u636e\u662f\u6309\u7167\u59d3\u540d\u6392\u5e8f\u597d\u7684\n# (name, age)\n('A', 19)\n('B', 18)\n('C', 21)\n('D', 19)\n('E', 23)\n# \u5047\u8bbe\u4f7f\u7528\u975e\u7a33\u5b9a\u6392\u5e8f\u7b97\u6cd5\u6309\u5e74\u9f84\u6392\u5e8f\u5217\u8868\uff0c\n# \u7ed3\u679c\u4e2d ('D', 19) \u548c ('A', 19) \u7684\u76f8\u5bf9\u4f4d\u7f6e\u6539\u53d8\uff0c\n# \u8f93\u5165\u6570\u636e\u6309\u59d3\u540d\u6392\u5e8f\u7684\u6027\u8d28\u4e22\u5931\n('B', 18)\n('D', 19)\n('A', 19)\n('C', 21)\n('E', 23)\n</code></pre> <p>\u81ea\u9002\u5e94\u6027\uff1a\u300c\u81ea\u9002\u5e94\u6392\u5e8f\u300d\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u53d7\u8f93\u5165\u6570\u636e\u7684\u5f71\u54cd\uff0c\u5373\u6700\u4f73\u3001\u6700\u5dee\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5e76\u4e0d\u5b8c\u5168\u76f8\u7b49\u3002</p> <p>\u81ea\u9002\u5e94\u6027\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u6765\u8bc4\u4f30\u3002\u5982\u679c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u5dee\u4e8e\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u8bf4\u660e\u6392\u5e8f\u7b97\u6cd5\u5728\u67d0\u4e9b\u6570\u636e\u4e0b\u6027\u80fd\u53ef\u80fd\u52a3\u5316\uff0c\u56e0\u6b64\u88ab\u89c6\u4e3a\u8d1f\u9762\u5c5e\u6027\uff1b\u800c\u5982\u679c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u4e8e\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u5219\u88ab\u89c6\u4e3a\u6b63\u9762\u5c5e\u6027\u3002</p> <p>\u662f\u5426\u57fa\u4e8e\u6bd4\u8f83\uff1a\u300c\u57fa\u4e8e\u6bd4\u8f83\u7684\u6392\u5e8f\u300d\u4f9d\u8d56\u4e8e\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08\\(&lt;\\) , \\(=\\) , \\(&gt;\\)\uff09\u6765\u5224\u65ad\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\uff0c\u4ece\u800c\u6392\u5e8f\u6574\u4e2a\u6570\u7ec4\uff0c\u7406\u8bba\u6700\u4f18\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u800c\u300c\u975e\u6bd4\u8f83\u6392\u5e8f\u300d\u4e0d\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe \\(O(n)\\) \uff0c\u4f46\u5176\u901a\u7528\u6027\u76f8\u5bf9\u8f83\u5dee\u3002</p>"},{"location":"chapter_sorting/sorting_algorithm/#1112","title":"11.1.2. \u00a0 \u7406\u60f3\u6392\u5e8f\u7b97\u6cd5","text":"<p>\u8fd0\u884c\u5feb\u3001\u539f\u5730\u3001\u7a33\u5b9a\u3001\u6b63\u5411\u81ea\u9002\u5e94\u3001\u901a\u7528\u6027\u597d\u3002\u663e\u7136\uff0c\u8fc4\u4eca\u4e3a\u6b62\u5c1a\u672a\u53d1\u73b0\u517c\u5177\u4ee5\u4e0a\u6240\u6709\u7279\u6027\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u56e0\u6b64\uff0c\u5728\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u6570\u636e\u7279\u70b9\u548c\u95ee\u9898\u9700\u6c42\u6765\u51b3\u5b9a\u3002</p> <p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5171\u540c\u5b66\u4e60\u5404\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5e76\u57fa\u4e8e\u4e0a\u8ff0\u8bc4\u4ef7\u7ef4\u5ea6\u5bf9\u5404\u4e2a\u6392\u5e8f\u7b97\u6cd5\u7684\u4f18\u7f3a\u70b9\u8fdb\u884c\u5206\u6790\u3002</p>"},{"location":"chapter_sorting/summary/","title":"11.11. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u5192\u6ce1\u6392\u5e8f\u901a\u8fc7\u4ea4\u6362\u76f8\u90bb\u5143\u7d20\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u901a\u8fc7\u6dfb\u52a0\u4e00\u4e2a\u6807\u5fd7\u4f4d\u6765\u5b9e\u73b0\u63d0\u524d\u8fd4\u56de\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5192\u6ce1\u6392\u5e8f\u7684\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u5230 \\(O(n)\\) \u3002</li> <li>\u63d2\u5165\u6392\u5e8f\u6bcf\u8f6e\u5c06\u672a\u6392\u5e8f\u533a\u95f4\u5185\u7684\u5143\u7d20\u63d2\u5165\u5230\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u6b63\u786e\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b8c\u6210\u6392\u5e8f\u3002\u867d\u7136\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u4f46\u7531\u4e8e\u5355\u5143\u64cd\u4f5c\u76f8\u5bf9\u8f83\u5c11\uff0c\u5b83\u5728\u5c0f\u6570\u636e\u91cf\u7684\u6392\u5e8f\u4efb\u52a1\u4e2d\u975e\u5e38\u53d7\u6b22\u8fce\u3002</li> <li>\u5feb\u901f\u6392\u5e8f\u57fa\u4e8e\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u5b9e\u73b0\u6392\u5e8f\u3002\u5728\u54e8\u5175\u5212\u5206\u4e2d\uff0c\u6709\u53ef\u80fd\u6bcf\u6b21\u90fd\u9009\u53d6\u5230\u6700\u5dee\u7684\u57fa\u51c6\u6570\uff0c\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u3002\u5f15\u5165\u4e2d\u4f4d\u6570\u57fa\u51c6\u6570\u6216\u968f\u673a\u57fa\u51c6\u6570\u53ef\u4ee5\u964d\u4f4e\u8fd9\u79cd\u52a3\u5316\u7684\u6982\u7387\u3002\u5c3e\u9012\u5f52\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u51cf\u5c11\u9012\u5f52\u6df1\u5ea6\uff0c\u5c06\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u5230 \\(O(\\log n)\\) \u3002</li> <li>\u5f52\u5e76\u6392\u5e8f\u5305\u62ec\u5212\u5206\u548c\u5408\u5e76\u4e24\u4e2a\u9636\u6bb5\uff0c\u5178\u578b\u5730\u4f53\u73b0\u4e86\u5206\u6cbb\u7b56\u7565\u3002\u5728\u5f52\u5e76\u6392\u5e8f\u4e2d\uff0c\u6392\u5e8f\u6570\u7ec4\u9700\u8981\u521b\u5efa\u8f85\u52a9\u6570\u7ec4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1b\u7136\u800c\u6392\u5e8f\u94fe\u8868\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f18\u5316\u81f3 \\(O(1)\\) \u3002</li> <li>\u6876\u6392\u5e8f\u5305\u542b\u4e09\u4e2a\u6b65\u9aa4\uff1a\u6570\u636e\u5206\u6876\u3001\u6876\u5185\u6392\u5e8f\u548c\u5408\u5e76\u7ed3\u679c\u3002\u5b83\u540c\u6837\u4f53\u73b0\u4e86\u5206\u6cbb\u7b56\u7565\uff0c\u9002\u7528\u4e8e\u6570\u636e\u4f53\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u3002\u6876\u6392\u5e8f\u7684\u5173\u952e\u5728\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u5e73\u5747\u5206\u914d\u3002</li> <li>\u8ba1\u6570\u6392\u5e8f\u662f\u6876\u6392\u5e8f\u7684\u4e00\u4e2a\u7279\u4f8b\uff0c\u5b83\u901a\u8fc7\u7edf\u8ba1\u6570\u636e\u51fa\u73b0\u7684\u6b21\u6570\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u8ba1\u6570\u6392\u5e8f\u9002\u7528\u4e8e\u6570\u636e\u91cf\u5927\u4f46\u6570\u636e\u8303\u56f4\u6709\u9650\u7684\u60c5\u51b5\uff0c\u5e76\u4e14\u8981\u6c42\u6570\u636e\u80fd\u591f\u8f6c\u6362\u4e3a\u6b63\u6574\u6570\u3002</li> <li>\u57fa\u6570\u6392\u5e8f\u901a\u8fc7\u9010\u4f4d\u6392\u5e8f\u6765\u5b9e\u73b0\u6570\u636e\u6392\u5e8f\uff0c\u8981\u6c42\u6570\u636e\u80fd\u591f\u8868\u793a\u4e3a\u56fa\u5b9a\u4f4d\u6570\u7684\u6570\u5b57\u3002</li> <li>\u603b\u7684\u6765\u8bf4\uff0c\u6211\u4eec\u5e0c\u671b\u627e\u5230\u4e00\u79cd\u6392\u5e8f\u7b97\u6cd5\uff0c\u5177\u6709\u9ad8\u6548\u7387\u3001\u7a33\u5b9a\u3001\u539f\u5730\u4ee5\u53ca\u6b63\u5411\u81ea\u9002\u5e94\u6027\u7b49\u4f18\u70b9\u3002\u7136\u800c\uff0c\u6b63\u5982\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e00\u6837\uff0c\u6ca1\u6709\u4e00\u79cd\u6392\u5e8f\u7b97\u6cd5\u80fd\u591f\u540c\u65f6\u6ee1\u8db3\u6240\u6709\u8fd9\u4e9b\u6761\u4ef6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u6570\u636e\u7684\u7279\u6027\u6765\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u7b97\u6cd5\u3002</li> </ul> <p> \u56fe\uff1a\u6392\u5e8f\u7b97\u6cd5\u5bf9\u6bd4 </p>"},{"location":"chapter_sorting/summary/#11111-q-a","title":"11.11.1. \u00a0 Q &amp; A","text":"<p>\u6392\u5e8f\u7b97\u6cd5\u7a33\u5b9a\u6027\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u662f\u5fc5\u987b\u7684\uff1f</p> <p>\u5728\u73b0\u5b9e\u4e2d\uff0c\u6211\u4eec\u6709\u53ef\u80fd\u662f\u5728\u5bf9\u8c61\u7684\u67d0\u4e2a\u5c5e\u6027\u4e0a\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5b66\u751f\u6709\u59d3\u540d\u548c\u8eab\u9ad8\u4e24\u4e2a\u5c5e\u6027\uff0c\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u4e00\u4e2a\u591a\u7ea7\u6392\u5e8f/</p> <p>\u5148\u6309\u7167\u59d3\u540d\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u5230 <code>(A, 180) (B, 185) (C, 170) (D, 170)</code> \uff1b\u63a5\u4e0b\u6765\u5bf9\u8eab\u9ad8\u8fdb\u884c\u6392\u5e8f\u3002\u7531\u4e8e\u6392\u5e8f\u7b97\u6cd5\u4e0d\u7a33\u5b9a\uff0c\u6211\u4eec\u53ef\u80fd\u5f97\u5230 <code>(D, 170) (C, 170) (A, 180) (B, 185)</code> \u3002</p> <p>\u53ef\u4ee5\u53d1\u73b0\uff0c\u5b66\u751f D \u548c C \u7684\u4f4d\u7f6e\u53d1\u751f\u4e86\u4ea4\u6362\uff0c\u59d3\u540d\u7684\u6709\u5e8f\u6027\u88ab\u7834\u574f\u4e86\uff0c\u800c\u8fd9\u662f\u6211\u4eec\u4e0d\u5e0c\u671b\u770b\u5230\u7684\u3002</p> <p>\u54e8\u5175\u5212\u5206\u4e2d\u201c\u4ece\u53f3\u5f80\u5de6\u67e5\u627e\u201d\u4e0e\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u7684\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u5417\uff1f</p> <p>\u4e0d\u884c\uff0c\u5f53\u6211\u4eec\u4ee5\u6700\u5de6\u7aef\u5143\u7d20\u4e3a\u57fa\u51c6\u6570\u65f6\uff0c\u5fc5\u987b\u5148\u201c\u4ece\u53f3\u5f80\u5de6\u67e5\u627e\u201d\u518d\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u3002\u8fd9\u4e2a\u7ed3\u8bba\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u6211\u4eec\u6765\u5256\u6790\u4e00\u4e0b\u539f\u56e0\u3002</p> <p>\u54e8\u5175\u5212\u5206 <code>partition()</code> \u7684\u6700\u540e\u4e00\u6b65\u662f\u4ea4\u6362 <code>nums[left]</code> \u548c <code>nums[i]</code> \u3002\u5b8c\u6210\u4ea4\u6362\u540e\uff0c\u57fa\u51c6\u6570\u5de6\u8fb9\u7684\u5143\u7d20\u90fd <code>&lt;=</code> \u57fa\u51c6\u6570\uff0c\u8fd9\u5c31\u8981\u6c42\u6700\u540e\u4e00\u6b65\u4ea4\u6362\u524d <code>nums[left] &gt;= nums[i]</code> \u5fc5\u987b\u6210\u7acb\u3002\u5047\u8bbe\u6211\u4eec\u5148\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\uff0c\u90a3\u4e48\u5982\u679c\u627e\u4e0d\u5230\u6bd4\u57fa\u51c6\u6570\u66f4\u5c0f\u7684\u5143\u7d20\uff0c\u5219\u4f1a\u5728 <code>i == j</code> \u65f6\u8df3\u51fa\u5faa\u73af\uff0c\u6b64\u65f6\u53ef\u80fd <code>nums[j] == nums[i] &gt; nums[left]</code>\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6b64\u65f6\u6700\u540e\u4e00\u6b65\u4ea4\u6362\u64cd\u4f5c\u4f1a\u628a\u4e00\u4e2a\u6bd4\u57fa\u51c6\u6570\u66f4\u5927\u7684\u5143\u7d20\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\uff0c\u5bfc\u81f4\u54e8\u5175\u5212\u5206\u5931\u8d25\u3002</p> <p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u7ed9\u5b9a\u6570\u7ec4 <code>[0, 0, 0, 0, 1]</code> \uff0c\u5982\u679c\u5148\u201c\u4ece\u5de6\u5411\u53f3\u67e5\u627e\u201d\uff0c\u54e8\u5175\u5212\u5206\u540e\u6570\u7ec4\u4e3a <code>[1, 0, 0, 0, 0]</code> \uff0c\u8fd9\u4e2a\u7ed3\u679c\u662f\u4e0d\u6b63\u786e\u7684\u3002</p> <p>\u518d\u6df1\u5165\u601d\u8003\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u9009\u62e9 <code>nums[right]</code> \u4e3a\u57fa\u51c6\u6570\uff0c\u90a3\u4e48\u6b63\u597d\u53cd\u8fc7\u6765\uff0c\u5fc5\u987b\u5148\u201c\u4ece\u5de6\u5f80\u53f3\u67e5\u627e\u201d\u3002</p> <p>\u5173\u4e8e\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u4e3a\u4ec0\u4e48\u9009\u77ed\u7684\u6570\u7ec4\u80fd\u4fdd\u8bc1\u9012\u5f52\u6df1\u5ea6\u4e0d\u8d85\u8fc7 \\(\\log n\\) \uff1f</p> <p>\u9012\u5f52\u6df1\u5ea6\u5c31\u662f\u5f53\u524d\u672a\u8fd4\u56de\u7684\u9012\u5f52\u65b9\u6cd5\u7684\u6570\u91cf\u3002\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u6211\u4eec\u5c06\u539f\u6570\u7ec4\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u3002\u5728\u5c3e\u9012\u5f52\u4f18\u5316\u540e\uff0c\u5411\u4e0b\u9012\u5f52\u7684\u5b50\u6570\u7ec4\u957f\u5ea6\u6700\u5927\u4e3a\u539f\u6570\u7ec4\u7684\u4e00\u534a\u957f\u5ea6\u3002\u5047\u8bbe\u6700\u5dee\u60c5\u51b5\uff0c\u4e00\u76f4\u4e3a\u4e00\u534a\u957f\u5ea6\uff0c\u90a3\u4e48\u6700\u7ec8\u7684\u9012\u5f52\u6df1\u5ea6\u5c31\u662f \\(\\log n\\) \u3002</p> <p>\u56de\u987e\u539f\u59cb\u7684\u5feb\u901f\u6392\u5e8f\uff0c\u6211\u4eec\u6709\u53ef\u80fd\u4f1a\u8fde\u7eed\u5730\u9012\u5f52\u957f\u5ea6\u8f83\u5927\u7684\u6570\u7ec4\uff0c\u6700\u5dee\u60c5\u51b5\u4e0b\u4e3a \\(n, n - 1, n - 2, ..., 2, 1\\) \uff0c\u4ece\u800c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002\u5c3e\u9012\u5f52\u4f18\u5316\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u51fa\u73b0\u3002</p> <p>\u5f53\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u76f8\u7b49\u65f6\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(n^2)\\) \u5417\uff1f\u8be5\u5982\u4f55\u5904\u7406\u8fd9\u79cd\u9000\u5316\u60c5\u51b5\uff1f</p> <p>\u662f\u7684\u3002\u8fd9\u79cd\u60c5\u51b5\u53ef\u4ee5\u8003\u8651\u901a\u8fc7\u54e8\u5175\u5212\u5206\u5c06\u6570\u7ec4\u5212\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a\u5c0f\u4e8e\u3001\u7b49\u4e8e\u3001\u5927\u4e8e\u57fa\u51c6\u6570\u3002\u4ec5\u5411\u4e0b\u9012\u5f52\u5c0f\u4e8e\u548c\u5927\u4e8e\u7684\u4e24\u90e8\u5206\u3002\u5728\u8be5\u65b9\u6cd5\u4e0b\uff0c\u8f93\u5165\u5143\u7d20\u5168\u90e8\u76f8\u7b49\u7684\u6570\u7ec4\uff0c\u4ec5\u4e00\u8f6e\u54e8\u5175\u5212\u5206\u5373\u53ef\u5b8c\u6210\u6392\u5e8f\u3002</p> <p>\u6876\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u4ec0\u4e48\u662f \\(O(n^2)\\) \uff1f</p> <p>\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u5143\u7d20\u88ab\u5206\u81f3\u540c\u4e00\u4e2a\u6876\u4e2d\u3002\u5982\u679c\u6211\u4eec\u91c7\u7528\u4e00\u4e2a \\(O(n^2)\\) \u7b97\u6cd5\u6765\u6392\u5e8f\u8fd9\u4e9b\u5143\u7d20\uff0c\u5219\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002</p>"},{"location":"chapter_stack_and_queue/","title":"5. \u00a0 \u6808\u4e0e\u961f\u5217","text":"<p>Abstract</p> <p>\u6808\u5982\u540c\u53e0\u732b\u732b\uff0c\u800c\u961f\u5217\u5c31\u50cf\u732b\u732b\u6392\u961f\u3002</p> <p>\u4e24\u8005\u5206\u522b\u4ee3\u8868\u7740\u5148\u5165\u540e\u51fa\u548c\u5148\u5165\u5148\u51fa\u7684\u903b\u8f91\u5173\u7cfb\u3002</p>"},{"location":"chapter_stack_and_queue/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>5.1 \u00a0 \u6808</li> <li>5.2 \u00a0 \u961f\u5217</li> <li>5.3 \u00a0 \u53cc\u5411\u961f\u5217</li> <li>5.4 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_stack_and_queue/deque/","title":"5.3. \u00a0 \u53cc\u5411\u961f\u5217","text":"<p>\u5bf9\u4e8e\u961f\u5217\uff0c\u6211\u4eec\u4ec5\u80fd\u5728\u5934\u90e8\u5220\u9664\u6216\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u3002\u7136\u800c\uff0c\u300c\u53cc\u5411\u961f\u5217 Deque\u300d\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u5728\u5934\u90e8\u548c\u5c3e\u90e8\u6267\u884c\u5143\u7d20\u7684\u6dfb\u52a0\u6216\u5220\u9664\u64cd\u4f5c\u3002</p> <p></p> <p> \u56fe\uff1a\u53cc\u5411\u961f\u5217\u7684\u64cd\u4f5c </p>"},{"location":"chapter_stack_and_queue/deque/#531","title":"5.3.1. \u00a0 \u53cc\u5411\u961f\u5217\u5e38\u7528\u64cd\u4f5c","text":"<p>\u53cc\u5411\u961f\u5217\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u4e0b\u8868\u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u79f0\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002</p> <p> \u8868\uff1a\u53cc\u5411\u961f\u5217\u64cd\u4f5c\u6548\u7387 </p> \u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 pushFirst() \u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u9996 \\(O(1)\\) pushLast() \u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u5c3e \\(O(1)\\) popFirst() \u5220\u9664\u961f\u9996\u5143\u7d20 \\(O(1)\\) popLast() \u5220\u9664\u961f\u5c3e\u5143\u7d20 \\(O(1)\\) peekFirst() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\) peekLast() \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 \\(O(1)\\) <p>\u540c\u6837\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5df2\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\u7c7b\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust deque.java<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nDeque&lt;Integer&gt; deque = new LinkedList&lt;&gt;();\n/* \u5143\u7d20\u5165\u961f */\ndeque.offerLast(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.offerLast(5);\ndeque.offerLast(4);\ndeque.offerFirst(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.offerFirst(1);\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.peekFirst(); // \u961f\u9996\u5143\u7d20\nint peekLast = deque.peekLast(); // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\nint popFirst = deque.pollFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\nint popLast = deque.pollLast(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = deque.isEmpty();\n</code></pre> deque.cpp<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\ndeque&lt;int&gt; deque;\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.push_front(1);\n/* \u8bbf\u95ee\u5143\u7d20 */\nint front = deque.front(); // \u961f\u9996\u5143\u7d20\nint back = deque.back(); // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\ndeque.pop_front(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.pop_back(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = deque.empty();\n</code></pre> deque.py<pre><code># \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217\ndeque: Deque[int] = collections.deque()\n# \u5143\u7d20\u5165\u961f\ndeque.append(2) # \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.append(5)\ndeque.append(4)\ndeque.appendleft(3) # \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.appendleft(1)\n# \u8bbf\u95ee\u5143\u7d20\nfront: int = deque[0] # \u961f\u9996\u5143\u7d20\nrear: int = deque[-1] # \u961f\u5c3e\u5143\u7d20\n# \u5143\u7d20\u51fa\u961f\npop_front: int = deque.popleft() # \u961f\u9996\u5143\u7d20\u51fa\u961f\npop_rear: int = deque.pop() # \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n# \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nsize: int = len(deque)\n# \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(deque) == 0\n</code></pre> deque_test.go<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Go \u4e2d\uff0c\u5c06 list \u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u4f7f\u7528\ndeque := list.New()\n/* \u5143\u7d20\u5165\u961f */\ndeque.PushBack(2) // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.PushBack(5)\ndeque.PushBack(4)\ndeque.PushFront(3) // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.PushFront(1)\n/* \u8bbf\u95ee\u5143\u7d20 */\nfront := deque.Front() // \u961f\u9996\u5143\u7d20\nrear := deque.Back() // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\ndeque.Remove(front) // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.Remove(rear) // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize := deque.Len()\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := deque.Len() == 0\n</code></pre> deque.js<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// JavaScript \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u7aef\u961f\u5217\uff0c\u53ea\u80fd\u628a Array \u5f53\u4f5c\u53cc\u7aef\u961f\u5217\u6765\u4f7f\u7528\nconst deque = [];\n/* \u5143\u7d20\u5165\u961f */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cunshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\ndeque.unshift(3);\ndeque.unshift(1);\nconsole.log(\"\u53cc\u5411\u961f\u5217 deque = \", deque);\n/* \u8bbf\u95ee\u5143\u7d20 */\nconst peekFirst = deque[0];\nconsole.log(\"\u961f\u9996\u5143\u7d20 peekFirst = \" + peekFirst);\nconst peekLast = deque[deque.length - 1];\nconsole.log(\"\u961f\u5c3e\u5143\u7d20 peekLast = \" + peekLast);\n/* \u5143\u7d20\u51fa\u961f */\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst popFront = deque.shift();\nconsole.log(\"\u961f\u9996\u51fa\u961f\u5143\u7d20 popFront = \" + popFront + \"\uff0c\u961f\u9996\u51fa\u961f\u540e deque = \" + deque);\nconst popBack = deque.pop();\nconsole.log(\"\u961f\u5c3e\u51fa\u961f\u5143\u7d20 popBack = \" + popBack + \"\uff0c\u961f\u5c3e\u51fa\u961f\u540e deque = \" + deque);\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = deque.length;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u957f\u5ea6 size = \" + size);\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst isEmpty = size === 0;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a = \" + isEmpty);\n</code></pre> deque.ts<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// TypeScript \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u7aef\u961f\u5217\uff0c\u53ea\u80fd\u628a Array \u5f53\u4f5c\u53cc\u7aef\u961f\u5217\u6765\u4f7f\u7528\nconst deque: number[] = [];\n/* \u5143\u7d20\u5165\u961f */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cunshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\ndeque.unshift(3);\ndeque.unshift(1);\nconsole.log(\"\u53cc\u5411\u961f\u5217 deque = \", deque);\n/* \u8bbf\u95ee\u5143\u7d20 */\nconst peekFirst: number = deque[0];\nconsole.log(\"\u961f\u9996\u5143\u7d20 peekFirst = \" + peekFirst);\nconst peekLast: number = deque[deque.length - 1];\nconsole.log(\"\u961f\u5c3e\u5143\u7d20 peekLast = \" + peekLast);\n/* \u5143\u7d20\u51fa\u961f */\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst popFront: number = deque.shift() as number;\nconsole.log(\"\u961f\u9996\u51fa\u961f\u5143\u7d20 popFront = \" + popFront + \"\uff0c\u961f\u9996\u51fa\u961f\u540e deque = \" + deque);\nconst popBack: number = deque.pop() as number;\nconsole.log(\"\u961f\u5c3e\u51fa\u961f\u5143\u7d20 popBack = \" + popBack + \"\uff0c\u961f\u5c3e\u51fa\u961f\u540e deque = \" + deque);\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nconst size: number = deque.length;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u957f\u5ea6 size = \" + size);\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst isEmpty: boolean = size === 0;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a = \" + isEmpty);\n</code></pre> deque.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u53cc\u5411\u961f\u5217\n</code></pre> deque.cs<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 C# \u4e2d\uff0c\u5c06\u94fe\u8868 LinkedList \u770b\u4f5c\u53cc\u5411\u961f\u5217\u6765\u4f7f\u7528\nLinkedList&lt;int&gt; deque = new LinkedList&lt;int&gt;();\n/* \u5143\u7d20\u5165\u961f */\ndeque.AddLast(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.AddLast(5);\ndeque.AddLast(4);\ndeque.AddFirst(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.AddFirst(1);\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.First.Value; // \u961f\u9996\u5143\u7d20\nint peekLast = deque.Last.Value; // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\ndeque.RemoveFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.RemoveLast(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.Count;\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.Count == 0;\n</code></pre> deque.swift<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u5411\u961f\u5217\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u53cc\u5411\u961f\u5217\u6765\u4f7f\u7528\nvar deque: [Int] = []\n/* \u5143\u7d20\u5165\u961f */\ndeque.append(2) // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.append(5)\ndeque.append(4)\ndeque.insert(3, at: 0) // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.insert(1, at: 0)\n/* \u8bbf\u95ee\u5143\u7d20 */\nlet peekFirst = deque.first! // \u961f\u9996\u5143\u7d20\nlet peekLast = deque.last! // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\n// \u4f7f\u7528 Array \u6a21\u62df\u65f6 popFirst \u7684\u590d\u6742\u5ea6\u4e3a O(n)\nlet popFirst = deque.removeFirst() // \u961f\u9996\u5143\u7d20\u51fa\u961f\nlet popLast = deque.removeLast() // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.count\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = deque.isEmpty\n</code></pre> deque.zig<pre><code>\n</code></pre> deque.dart<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Dart \u4e2d\uff0cQueue \u88ab\u5b9a\u4e49\u4e3a\u53cc\u5411\u961f\u5217\nQueue&lt;int&gt; deque = Queue&lt;int&gt;();\n/* \u5143\u7d20\u5165\u961f */\ndeque.addLast(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.addLast(5);\ndeque.addLast(4);\ndeque.addFirst(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.addFirst(1);\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.first; // \u961f\u9996\u5143\u7d20\nint peekLast = deque.last; // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\nint popFirst = deque.removeFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\nint popLast = deque.removeLast(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.length;\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.isEmpty;W\n</code></pre> deque.rs<pre><code>\n</code></pre>"},{"location":"chapter_stack_and_queue/deque/#532","title":"5.3.2. \u00a0 \u53cc\u5411\u961f\u5217\u5b9e\u73b0 *","text":"<p>\u53cc\u5411\u961f\u5217\u7684\u5b9e\u73b0\u4e0e\u961f\u5217\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u9009\u62e9\u94fe\u8868\u6216\u6570\u7ec4\u4f5c\u4e3a\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002</p>"},{"location":"chapter_stack_and_queue/deque/#_1","title":"\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u7684\u5b9e\u73b0","text":"<p>\u56de\u987e\u4e0a\u4e00\u8282\u5185\u5bb9\uff0c\u6211\u4eec\u4f7f\u7528\u666e\u901a\u5355\u5411\u94fe\u8868\u6765\u5b9e\u73b0\u961f\u5217\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5220\u9664\u5934\u8282\u70b9\uff08\u5bf9\u5e94\u51fa\u961f\u64cd\u4f5c\uff09\u548c\u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0\u65b0\u8282\u70b9\uff08\u5bf9\u5e94\u5165\u961f\u64cd\u4f5c\uff09\u3002</p> <p>\u5bf9\u4e8e\u53cc\u5411\u961f\u5217\u800c\u8a00\uff0c\u5934\u90e8\u548c\u5c3e\u90e8\u90fd\u53ef\u4ee5\u6267\u884c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u53cc\u5411\u961f\u5217\u9700\u8981\u5b9e\u73b0\u53e6\u4e00\u4e2a\u5bf9\u79f0\u65b9\u5411\u7684\u64cd\u4f5c\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u91c7\u7528\u300c\u53cc\u5411\u94fe\u8868\u300d\u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002</p> <p>\u6211\u4eec\u5c06\u53cc\u5411\u94fe\u8868\u7684\u5934\u8282\u70b9\u548c\u5c3e\u8282\u70b9\u89c6\u4e3a\u53cc\u5411\u961f\u5217\u7684\u961f\u9996\u548c\u961f\u5c3e\uff0c\u540c\u65f6\u5b9e\u73b0\u5728\u4e24\u7aef\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u7684\u529f\u80fd\u3002</p> LinkedListDequepushLast()pushFirst()popLast()popFirst() <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe\uff1a\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c </p> <p>\u4ee5\u4e0b\u662f\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust linkedlist_deque.java<pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\nListNode prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\nListNode(int val) {\nthis.val = val;\nprev = next = null;\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nprivate int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\npublic LinkedListDeque() {\nfront = rear = null;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\nprivate void push(int num, boolean isFront) {\nListNode node = new ListNode(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (isEmpty())\nfront = rear = node;\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if (isFront) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nfront.prev = node;\nnode.next = front;\nfront = node; // \u66f4\u65b0\u5934\u8282\u70b9\n// \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nrear.next = node;\nnode.prev = rear;\nrear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\npublic void pushFirst(int num) {\npush(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\npublic void pushLast(int num) {\npush(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nprivate Integer pop(boolean isFront) {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\nif (isEmpty())\nreturn null;\nint val;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (isFront) {\nval = front.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nListNode fNext = front.next;\nif (fNext != null) {\nfNext.prev = null;\nfront.next = null;\n}\nfront = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n} else {\nval = rear.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nListNode rPrev = rear.prev;\nif (rPrev != null) {\nrPrev.next = null;\nrear.prev = null;\n}\nrear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\npublic Integer popFirst() {\nreturn pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\npublic Integer popLast() {\nreturn pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic Integer peekFirst() {\nreturn isEmpty() ? null : front.val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic Integer peekLast() {\nreturn isEmpty() ? null : rear.val;\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npublic int[] toArray() {\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i &lt; res.length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_deque.cpp<pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nstruct DoublyListNode {\nint val; // \u8282\u70b9\u503c\nDoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\u6307\u9488\nDoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\u6307\u9488\nDoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n}\n};\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate:\nDoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nint queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nLinkedListDeque() : front(nullptr), rear(nullptr) {\n}\n/* \u6790\u6784\u65b9\u6cd5 */\n~LinkedListDeque() {\n// \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\nDoublyListNode *pre, *cur = front;\nwhile (cur != nullptr) {\npre = cur;\ncur = cur-&gt;next;\ndelete pre;\n}\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\nvoid push(int num, bool isFront) {\nDoublyListNode *node = new DoublyListNode(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (isEmpty())\nfront = rear = node;\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if (isFront) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nfront-&gt;prev = node;\nnode-&gt;next = front;\nfront = node; // \u66f4\u65b0\u5934\u8282\u70b9\n// \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nrear-&gt;next = node;\nnode-&gt;prev = rear;\nrear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(int num) {\npush(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(int num) {\npush(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nint pop(bool isFront) {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de -1\nif (isEmpty())\nreturn -1;\nint val;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (isFront) {\nval = front-&gt;val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nDoublyListNode *fNext = front-&gt;next;\nif (fNext != nullptr) {\nfNext-&gt;prev = nullptr;\nfront-&gt;next = nullptr;\ndelete front;\n}\nfront = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n} else {\nval = rear-&gt;val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nDoublyListNode *rPrev = rear-&gt;prev;\nif (rPrev != nullptr) {\nrPrev-&gt;next = nullptr;\nrear-&gt;prev = nullptr;\ndelete rear;\n}\nrear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst() {\nreturn pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast() {\nreturn pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst() {\nreturn isEmpty() ? -1 : front-&gt;val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast() {\nreturn isEmpty() ? -1 : rear-&gt;val;\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nvector&lt;int&gt; toVector() {\nDoublyListNode *node = front;\nvector&lt;int&gt; res(size());\nfor (int i = 0; i &lt; res.size(); i++) {\nres[i] = node-&gt;val;\nnode = node-&gt;next;\n}\nreturn res;\n}\n};\n</code></pre> linkedlist_deque.py<pre><code>class ListNode:\n\"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\"\"\"\ndef __init__(self, val: int):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.val: int = val\nself.next: ListNode | None = None # \u540e\u7ee7\u8282\u70b9\u5f15\u7528\nself.prev: ListNode | None = None # \u524d\u9a71\u8282\u70b9\u5f15\u7528\nclass LinkedListDeque:\n\"\"\"\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.front: ListNode | None = None # \u5934\u8282\u70b9 front\nself.rear: ListNode | None = None # \u5c3e\u8282\u70b9 rear\nself.__size: int = 0 # \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\ndef size(self) -&gt; int:\n\"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -&gt; bool:\n\"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn self.size() == 0\ndef push(self, num: int, is_front: bool):\n\"\"\"\u5165\u961f\u64cd\u4f5c\"\"\"\nnode = ListNode(num)\n# \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif self.is_empty():\nself.front = self.rear = node\n# \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelif is_front:\n# \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nself.front.prev = node\nnode.next = self.front\nself.front = node # \u66f4\u65b0\u5934\u8282\u70b9\n# \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\nelse:\n# \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nself.rear.next = node\nnode.prev = self.rear\nself.rear = node # \u66f4\u65b0\u5c3e\u8282\u70b9\nself.__size += 1 # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\ndef push_first(self, num: int):\n\"\"\"\u961f\u9996\u5165\u961f\"\"\"\nself.push(num, True)\ndef push_last(self, num: int):\n\"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\nself.push(num, False)\ndef pop(self, is_front: bool) -&gt; int:\n\"\"\"\u51fa\u961f\u64cd\u4f5c\"\"\"\n# \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de None\nif self.is_empty():\nreturn None\n# \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif is_front:\nval: int = self.front.val # \u6682\u5b58\u5934\u8282\u70b9\u503c\n# \u5220\u9664\u5934\u8282\u70b9\nfnext: ListNode | None = self.front.next\nif fnext != None:\nfnext.prev = None\nself.front.next = None\nself.front = fnext # \u66f4\u65b0\u5934\u8282\u70b9\n# \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse:\nval: int = self.rear.val # \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n# \u5220\u9664\u5c3e\u8282\u70b9\nrprev: ListNode | None = self.rear.prev\nif rprev != None:\nrprev.next = None\nself.rear.prev = None\nself.rear = rprev # \u66f4\u65b0\u5c3e\u8282\u70b9\nself.__size -= 1 # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val\ndef pop_first(self) -&gt; int:\n\"\"\"\u961f\u9996\u51fa\u961f\"\"\"\nreturn self.pop(True)\ndef pop_last(self) -&gt; int:\n\"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\nreturn self.pop(False)\ndef peek_first(self) -&gt; int:\n\"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\nreturn None if self.is_empty() else self.front.val\ndef peek_last(self) -&gt; int:\n\"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\nreturn None if self.is_empty() else self.rear.val\ndef to_array(self) -&gt; list[int]:\n\"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\nnode = self.front\nres = [0] * self.size()\nfor i in range(self.size()):\nres[i] = node.val\nnode = node.next\nreturn res\n</code></pre> linkedlist_deque.go<pre><code>/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype linkedListDeque struct {\n// \u4f7f\u7528\u5185\u7f6e\u5305 list\ndata *list.List\n}\n/* \u521d\u59cb\u5316\u53cc\u7aef\u961f\u5217 */\nfunc newLinkedListDeque() *linkedListDeque {\nreturn &amp;linkedListDeque{\ndata: list.New(),\n}\n}\n/* \u961f\u9996\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushFirst(value any) {\ns.data.PushFront(value)\n}\n/* \u961f\u5c3e\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushLast(value any) {\ns.data.PushBack(value)\n}\n/* \u961f\u9996\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popFirst() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Front()\ns.data.Remove(e)\nreturn e.Value\n}\n/* \u961f\u5c3e\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popLast() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Back()\ns.data.Remove(e)\nreturn e.Value\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListDeque) peekFirst() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Front()\nreturn e.Value\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (s *linkedListDeque) peekLast() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Back()\nreturn e.Value\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListDeque) size() int {\nreturn s.data.Len()\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListDeque) isEmpty() bool {\nreturn s.data.Len() == 0\n}\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListDeque) toList() *list.List {\nreturn s.data\n}\n</code></pre> linkedlist_deque.js<pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nprev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\nnext; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\nval; // \u8282\u70b9\u503c\nconstructor(val) {\nthis.val = val;\nthis.next = null;\nthis.prev = null;\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n#front; // \u5934\u8282\u70b9 front\n#rear; // \u5c3e\u8282\u70b9 rear\n#queSize; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nconstructor() {\nthis.#front = null;\nthis.#rear = null;\nthis.#queSize = 0;\n}\n/* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\npushLast(val) {\nconst node = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.#queSize === 0) {\nthis.#front = node;\nthis.#rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nthis.#rear.next = node;\nnode.prev = this.#rear;\nthis.#rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nthis.#queSize++;\n}\n/* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\npushFirst(val) {\nconst node = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.#queSize === 0) {\nthis.#front = node;\nthis.#rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nthis.#front.prev = node;\nnode.next = this.#front;\nthis.#front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n}\nthis.#queSize++;\n}\n/* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\npopLast() {\nif (this.#queSize === 0) {\nreturn null;\n}\nconst value = this.#rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nlet temp = this.#rear.prev;\nif (temp !== null) {\ntemp.next = null;\nthis.#rear.prev = null;\n}\nthis.#rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\nthis.#queSize--;\nreturn value;\n}\n/* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\npopFirst() {\nif (this.#queSize === 0) {\nreturn null;\n}\nconst value = this.#front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nlet temp = this.#front.next;\nif (temp !== null) {\ntemp.prev = null;\nthis.#front.next = null;\n}\nthis.#front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\nthis.#queSize--;\nreturn value;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npeekLast() {\nreturn this.#queSize === 0 ? null : this.#rear.val;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeekFirst() {\nreturn this.#queSize === 0 ? null : this.#front.val;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize() {\nreturn this.#queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.#queSize === 0;\n}\n/* \u6253\u5370\u53cc\u5411\u961f\u5217 */\nprint() {\nconst arr = [];\nlet temp = this.#front;\nwhile (temp !== null) {\narr.push(temp.val);\ntemp = temp.next;\n}\nconsole.log('[' + arr.join(', ') + ']');\n}\n}\n</code></pre> linkedlist_deque.ts<pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nprev: ListNode; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\nnext: ListNode; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\nval: number; // \u8282\u70b9\u503c\nconstructor(val: number) {\nthis.val = val;\nthis.next = null;\nthis.prev = null;\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate front: ListNode; // \u5934\u8282\u70b9 front\nprivate rear: ListNode; // \u5c3e\u8282\u70b9 rear\nprivate queSize: number; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nconstructor() {\nthis.front = null;\nthis.rear = null;\nthis.queSize = 0;\n}\n/* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\npushLast(val: number): void {\nconst node: ListNode = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.queSize === 0) {\nthis.front = node;\nthis.rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nthis.rear.next = node;\nnode.prev = this.rear;\nthis.rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nthis.queSize++;\n}\n/* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\npushFirst(val: number): void {\nconst node: ListNode = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.queSize === 0) {\nthis.front = node;\nthis.rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nthis.front.prev = node;\nnode.next = this.front;\nthis.front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n}\nthis.queSize++;\n}\n/* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\npopLast(): number {\nif (this.queSize === 0) {\nreturn null;\n}\nconst value: number = this.rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nlet temp: ListNode = this.rear.prev;\nif (temp !== null) {\ntemp.next = null;\nthis.rear.prev = null;\n}\nthis.rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\nthis.queSize--;\nreturn value;\n}\n/* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\npopFirst(): number {\nif (this.queSize === 0) {\nreturn null;\n}\nconst value: number = this.front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nlet temp: ListNode = this.front.next;\nif (temp !== null) {\ntemp.prev = null;\nthis.front.next = null;\n}\nthis.front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\nthis.queSize--;\nreturn value;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npeekLast(): number {\nreturn this.queSize === 0 ? null : this.rear.val;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeekFirst(): number {\nreturn this.queSize === 0 ? null : this.front.val;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize(): number {\nreturn this.queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.queSize === 0;\n}\n/* \u6253\u5370\u53cc\u5411\u961f\u5217 */\nprint(): void {\nconst arr: number[] = [];\nlet temp: ListNode = this.front;\nwhile (temp !== null) {\narr.push(temp.val);\ntemp = temp.next;\n}\nconsole.log('[' + arr.join(', ') + ']');\n}\n}\n</code></pre> linkedlist_deque.c<pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nstruct doublyListNode {\nint val; // \u8282\u70b9\u503c\nstruct doublyListNode *next; // \u540e\u7ee7\u8282\u70b9\nstruct doublyListNode *prev; // \u524d\u9a71\u8282\u70b9\n};\ntypedef struct doublyListNode doublyListNode;\n/* \u6784\u9020\u51fd\u6570 */\ndoublyListNode *newDoublyListNode(int num) {\ndoublyListNode *new = (doublyListNode *)malloc(sizeof(doublyListNode));\nnew-&gt;val = num;\nnew-&gt;next = NULL;\nnew-&gt;prev = NULL;\nreturn new;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delDoublyListNode(doublyListNode *node) {\nfree(node);\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct linkedListDeque {\ndoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nint queSize; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n};\ntypedef struct linkedListDeque linkedListDeque;\n/* \u6784\u9020\u51fd\u6570 */\nlinkedListDeque *newLinkedListDeque() {\nlinkedListDeque *deque = (linkedListDeque *)malloc(sizeof(linkedListDeque));\ndeque-&gt;front = NULL;\ndeque-&gt;rear = NULL;\ndeque-&gt;queSize = 0;\nreturn deque;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListdeque(linkedListDeque *deque) {\n// \u91ca\u653e\u6240\u6709\u8282\u70b9\nfor (int i = 0; i &lt; deque-&gt;queSize &amp;&amp; deque-&gt;front != NULL; i++) {\ndoublyListNode *tmp = deque-&gt;front;\ndeque-&gt;front = deque-&gt;front-&gt;next;\nfree(tmp);\n}\n// \u91ca\u653e deque \u7ed3\u6784\u4f53\nfree(deque);\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(linkedListDeque *deque) {\nreturn deque-&gt;queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(linkedListDeque *deque) {\nreturn (size(deque) == 0);\n}\n/* \u5165\u961f */\nvoid push(linkedListDeque *deque, int num, bool isFront) {\ndoublyListNode *node = newDoublyListNode(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411node\nif (empty(deque)) {\ndeque-&gt;front = deque-&gt;rear = node;\n}\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if (isFront) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\ndeque-&gt;front-&gt;prev = node;\nnode-&gt;next = deque-&gt;front;\ndeque-&gt;front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u5bf9\u5c3e\u5165\u961f\u64cd\u4f5c\nelse {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\ndeque-&gt;rear-&gt;next = node;\nnode-&gt;prev = deque-&gt;rear;\ndeque-&gt;rear = node;\n}\ndeque-&gt;queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(linkedListDeque *deque, int num) {\npush(deque, num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(linkedListDeque *deque, int num) {\npush(deque, num, false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(linkedListDeque *deque) {\nassert(size(deque) &amp;&amp; deque-&gt;front);\nreturn deque-&gt;front-&gt;val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(linkedListDeque *deque) {\nassert(size(deque) &amp;&amp; deque-&gt;rear);\nreturn deque-&gt;rear-&gt;val;\n}\n/* \u51fa\u961f */\nint pop(linkedListDeque *deque, bool isFront) {\nif (empty(deque))\nreturn -1;\nint val;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (isFront) {\nval = peekFirst(deque); // \u6682\u5b58\u5934\u8282\u70b9\u503c\ndoublyListNode *fNext = deque-&gt;front-&gt;next;\nif (fNext) {\nfNext-&gt;prev = NULL;\ndeque-&gt;front-&gt;next = NULL;\ndelDoublyListNode(deque-&gt;front);\n}\ndeque-&gt;front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse {\nval = peekLast(deque); // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\ndoublyListNode *rPrev = deque-&gt;rear-&gt;prev;\nif (rPrev) {\nrPrev-&gt;next = NULL;\ndeque-&gt;rear-&gt;prev = NULL;\ndelDoublyListNode(deque-&gt;rear);\n}\ndeque-&gt;rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\ndeque-&gt;queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(linkedListDeque *deque) {\nreturn pop(deque, true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(linkedListDeque *deque) {\nreturn pop(deque, false);\n}\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListDeque(linkedListDeque *deque) {\nint arr[deque-&gt;queSize];\n// \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\nint i;\ndoublyListNode *node;\nfor (i = 0, node = deque-&gt;front; i &lt; deque-&gt;queSize; i++) {\narr[i] = node-&gt;val;\nnode = node-&gt;next;\n}\nprintArray(arr, deque-&gt;queSize);\n}\n</code></pre> linkedlist_deque.cs<pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\npublic int val; // \u8282\u70b9\u503c\npublic ListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\npublic ListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\npublic ListNode(int val) {\nthis.val = val;\nprev = null;\nnext = null;\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate ListNode? front, rear; // \u5934\u8282\u70b9 front, \u5c3e\u8282\u70b9 rear\nprivate int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\npublic LinkedListDeque() {\nfront = null;\nrear = null;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\nprivate void push(int num, bool isFront) {\nListNode node = new ListNode(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (isEmpty()) {\nfront = node;\nrear = node;\n}\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if (isFront) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nfront.prev = node;\nnode.next = front;\nfront = node; // \u66f4\u65b0\u5934\u8282\u70b9 \n}\n// \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\nelse {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nrear.next = node;\nnode.prev = rear;\nrear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\npublic void pushFirst(int num) {\npush(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\npublic void pushLast(int num) {\npush(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nprivate int? pop(bool isFront) {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\nif (isEmpty()) {\nreturn null;\n}\nint val;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (isFront) {\nval = front.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nListNode fNext = front.next;\nif (fNext != null) {\nfNext.prev = null;\nfront.next = null;\n}\nfront = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse {\nval = rear.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nListNode rPrev = rear.prev;\nif (rPrev != null) {\nrPrev.next = null;\nrear.prev = null;\n}\nrear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\npublic int? popFirst() {\nreturn pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\npublic int? popLast() {\nreturn pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int? peekFirst() {\nreturn isEmpty() ? null : front.val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic int? peekLast() {\nreturn isEmpty() ? null : rear.val;\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npublic int[] toArray() {\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i &lt; res.Length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_deque.swift<pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nvar val: Int // \u8282\u70b9\u503c\nvar next: ListNode? // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\nweak var prev: ListNode? // \u524d\u9a71\u8282\u70b9\u5f15\u7528\ninit(val: Int) {\nself.val = val\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate var front: ListNode? // \u5934\u8282\u70b9 front\nprivate var rear: ListNode? // \u5c3e\u8282\u70b9 rear\nprivate var queSize: Int // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\ninit() {\nqueSize = 0\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -&gt; Int {\nqueSize\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -&gt; Bool {\nsize() == 0\n}\n/* \u5165\u961f\u64cd\u4f5c */\nprivate func push(num: Int, isFront: Bool) {\nlet node = ListNode(val: num)\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif isEmpty() {\nfront = node\nrear = node\n}\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if isFront {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nfront?.prev = node\nnode.next = front\nfront = node // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\nelse {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nrear?.next = node\nnode.prev = rear\nrear = node // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize += 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\nfunc pushFirst(num: Int) {\npush(num: num, isFront: true)\n}\n/* \u961f\u5c3e\u5165\u961f */\nfunc pushLast(num: Int) {\npush(num: num, isFront: false)\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nprivate func pop(isFront: Bool) -&gt; Int {\nif isEmpty() {\nfatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n}\nlet val: Int\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif isFront {\nval = front!.val // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nlet fNext = front?.next\nif fNext != nil {\nfNext?.prev = nil\nfront?.next = nil\n}\nfront = fNext // \u66f4\u65b0\u5934\u8282\u70b9\n}\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse {\nval = rear!.val // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nlet rPrev = rear?.prev\nif rPrev != nil {\nrPrev?.next = nil\nrear?.prev = nil\n}\nrear = rPrev // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nqueSize -= 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val\n}\n/* \u961f\u9996\u51fa\u961f */\nfunc popFirst() -&gt; Int {\npop(isFront: true)\n}\n/* \u961f\u5c3e\u51fa\u961f */\nfunc popLast() -&gt; Int {\npop(isFront: false)\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peekFirst() -&gt; Int? {\nisEmpty() ? nil : front?.val\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc peekLast() -&gt; Int? {\nisEmpty() ? nil : rear?.val\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nfunc toArray() -&gt; [Int] {\nvar node = front\nvar res = Array(repeating: 0, count: size())\nfor i in res.indices {\nres[i] = node!.val\nnode = node?.next\n}\nreturn res\n}\n}\n</code></pre> linkedlist_deque.zig<pre><code>// \u53cc\u5411\u94fe\u8868\u8282\u70b9\nfn ListNode(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nval: T = undefined, // \u8282\u70b9\u503c\nnext: ?*Self = null, // \u540e\u7ee7\u8282\u70b9\u6307\u9488\nprev: ?*Self = null, // \u524d\u9a71\u8282\u70b9\u6307\u9488\n// Initialize a list node with specific value\npub fn init(self: *Self, x: i32) void {\nself.val = x;\nself.next = null;\nself.prev = null;\n}\n};\n}\n// \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\nfn LinkedListDeque(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nfront: ?*ListNode(T) = null, // \u5934\u8282\u70b9 front\nrear: ?*ListNode(T) = null, // \u5c3e\u8282\u70b9 rear\nque_size: usize = 0, // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.front = null;\nself.rear = null;\nself.que_size = 0;\n}\n// \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.que_size;\n}\n// \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u5165\u961f\u64cd\u4f5c\npub fn push(self: *Self, num: T, is_front: bool) !void {\nvar node = try self.mem_allocator.create(ListNode(T));\nnode.init(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (self.isEmpty()) {\nself.front = node;\nself.rear = node;\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\n} else if (is_front) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nself.front.?.prev = node;\nnode.next = self.front;\nself.front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n// \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nself.rear.?.next = node;\nnode.prev = self.rear;\nself.rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nself.que_size += 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n} // \u961f\u9996\u5165\u961f\npub fn pushFirst(self: *Self, num: T) !void {\ntry self.push(num, true);\n} // \u961f\u5c3e\u5165\u961f\npub fn pushLast(self: *Self, num: T) !void {\ntry self.push(num, false);\n} // \u51fa\u961f\u64cd\u4f5c\npub fn pop(self: *Self, is_front: bool) T {\nif (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nvar val: T = undefined;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (is_front) {\nval = self.front.?.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nvar fNext = self.front.?.next;\nif (fNext != null) {\nfNext.?.prev = null;\nself.front.?.next = null;\n}\nself.front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n} else {\nval = self.rear.?.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nvar rPrev = self.rear.?.prev;\nif (rPrev != null) {\nrPrev.?.next = null;\nself.rear.?.prev = null;\n}\nself.rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nself.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n} // \u961f\u9996\u51fa\u961f\npub fn popFirst(self: *Self) T {\nreturn self.pop(true);\n} // \u961f\u5c3e\u51fa\u961f\npub fn popLast(self: *Self) T {\nreturn self.pop(false);\n} // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\npub fn peekFirst(self: *Self) T {\nif (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nreturn self.front.?.val;\n} // \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\npub fn peekLast(self: *Self) T {\nif (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nreturn self.rear.?.val;\n}\n// \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\npub fn toArray(self: *Self) ![]T {\nvar node = self.front;\nvar res = try self.mem_allocator.alloc(T, self.size());\n@memset(res, @as(T, 0));\nvar i: usize = 0;\nwhile (i &lt; res.len) : (i += 1) {\nres[i] = node.?.val;\nnode = node.?.next;\n}\nreturn res;\n}\n};\n}\n</code></pre> linkedlist_deque.dart<pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\nint val; // \u8282\u70b9\u503c\nListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\nListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\nListNode(this.val, {this.next, this.prev});\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u5bf9\u5217 */\nclass LinkedListDeque {\nlate ListNode? _front; // \u5934\u8282\u70b9 _front\nlate ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\nint _queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nLinkedListDeque() {\nthis._front = null;\nthis._rear = null;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u957f\u5ea6 */\nint size() {\nreturn this._queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\nvoid push(int num, bool isFront) {\nfinal ListNode node = ListNode(num);\nif (isEmpty()) {\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 _front\uff0c_rear \u90fd\u6307\u5411 node\n_front = _rear = node;\n} else if (isFront) {\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n_front!.prev = node;\nnode.next = _front;\n_front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n} else {\n// \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n_rear!.next = node;\nnode.prev = _rear;\n_rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\n_queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(int num) {\npush(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(int num) {\npush(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nint? pop(bool isFront) {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\nif (isEmpty()) {\nreturn null;\n}\nfinal int val;\nif (isFront) {\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nval = _front!.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n// \u5220\u9664\u5934\u8282\u70b9\nListNode? fNext = _front!.next;\nif (fNext != null) {\nfNext.prev = null;\n_front!.next = null;\n}\n_front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n} else {\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nval = _rear!.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n// \u5220\u9664\u5c3e\u8282\u70b9\nListNode? rPrev = _rear!.prev;\nif (rPrev != null) {\nrPrev.next = null;\n_rear!.prev = null;\n}\n_rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\n_queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\nint? popFirst() {\nreturn pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint? popLast() {\nreturn pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint? peekFirst() {\nreturn _front?.val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint? peekLast() {\nreturn _rear?.val;\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nList&lt;int&gt; toArray() {\nListNode? node = _front;\nfinal List&lt;int&gt; res = [];\nfor (int i = 0; i &lt; _queSize; i++) {\nres.add(node!.val);\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_deque.rs<pre><code>/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\npub struct ListNode&lt;T&gt; {\npub val: T, // \u8282\u70b9\u503c\npub next: Option&lt;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;, // \u540e\u7ee7\u8282\u70b9\u6307\u9488\npub prev: Option&lt;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;, // \u524d\u9a71\u8282\u70b9\u6307\u9488\n}\nimpl&lt;T&gt; ListNode&lt;T&gt; {\npub fn new(val: T) -&gt; Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt; {\nRc::new(RefCell::new(ListNode {\nval,\nnext: None,\nprev: None,\n}))\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListDeque&lt;T&gt; {\nfront: Option&lt;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;, // \u5934\u8282\u70b9 front\nrear: Option&lt;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;, // \u5c3e\u8282\u70b9 rear \nque_size: usize, // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n}\nimpl&lt;T: Copy&gt; LinkedListDeque&lt;T&gt; {\npub fn new() -&gt; Self {\nSelf {\nfront: None,\nrear: None,\nque_size: 0, }\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npub fn size(&amp;self) -&gt; usize {\nreturn self.que_size;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npub fn is_empty(&amp;self) -&gt; bool {\nreturn self.size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\npub fn push(&amp;mut self, num: T, is_front: bool) {\nlet node = ListNode::new(num);\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nif is_front {\nmatch self.front.take() {\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nNone =&gt; {\nself.rear = Some(node.clone());\nself.front = Some(node);\n}\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nSome(old_front) =&gt; {\nold_front.borrow_mut().prev = Some(node.clone());\nnode.borrow_mut().next = Some(old_front);\nself.front = Some(node); // \u66f4\u65b0\u5934\u8282\u70b9\n}\n}\n} // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\nelse {\nmatch self.rear.take() {\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nNone =&gt; {\nself.front = Some(node.clone());\nself.rear = Some(node);\n}\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nSome(old_rear) =&gt; {\nold_rear.borrow_mut().next = Some(node.clone());\nnode.borrow_mut().prev = Some(old_rear);\nself.rear = Some(node); // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\n}\n}\nself.que_size += 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\npub fn push_first(&amp;mut self, num: T) {\nself.push(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\npub fn push_last(&amp;mut self, num: T) {\nself.push(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\npub fn pop(&amp;mut self, is_front: bool) -&gt; Option&lt;T&gt; {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de None\nif self.is_empty() { return None };\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif is_front {\nself.front.take().map(|old_front| {\nmatch old_front.borrow_mut().next.take() {\nSome(new_front) =&gt; {\nnew_front.borrow_mut().prev.take();\nself.front = Some(new_front); // \u66f4\u65b0\u5934\u8282\u70b9\n}\nNone =&gt; {\nself.rear.take();\n}\n}\nself.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nRc::try_unwrap(old_front).ok().unwrap().into_inner().val\n})\n} // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse {\nself.rear.take().map(|old_rear| {\nmatch old_rear.borrow_mut().prev.take() {\nSome(new_rear) =&gt; {\nnew_rear.borrow_mut().next.take();\nself.rear = Some(new_rear); // \u66f4\u65b0\u5c3e\u8282\u70b9\n}\nNone =&gt; {\nself.front.take();\n}\n}\nself.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nRc::try_unwrap(old_rear).ok().unwrap().into_inner().val\n})\n}\n}\n/* \u961f\u9996\u51fa\u961f */\npub fn pop_first(&amp;mut self) -&gt; Option&lt;T&gt; {\nreturn self.pop(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\npub fn pop_last(&amp;mut self) -&gt; Option&lt;T&gt; {\nreturn self.pop(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npub fn peek_first(&amp;self) -&gt; Option&lt;&amp;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt; {\nself.front.as_ref()\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npub fn peek_last(&amp;self) -&gt; Option&lt;&amp;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt; {\nself.rear.as_ref()\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npub 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; {\nif let Some(node) = head {\nlet mut nums = self.to_array(node.borrow().next.as_ref());\nnums.insert(0, node.borrow().val);\nreturn nums;\n}\nreturn Vec::new();\n}\n}\n</code></pre>"},{"location":"chapter_stack_and_queue/deque/#_2","title":"\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"<p>\u4e0e\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u73af\u5f62\u6570\u7ec4\u6765\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u3002\u5728\u961f\u5217\u7684\u5b9e\u73b0\u57fa\u7840\u4e0a\uff0c\u4ec5\u9700\u589e\u52a0\u201c\u961f\u9996\u5165\u961f\u201d\u548c\u201c\u961f\u5c3e\u51fa\u961f\u201d\u7684\u65b9\u6cd5\u3002</p> ArrayDequepushLast()pushFirst()popLast()popFirst() <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe\uff1a\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c </p> <p>\u4ee5\u4e0b\u662f\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust array_deque.java<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\npublic ArrayDeque(int capacity) {\nthis.nums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity() {\nreturn nums.length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn queSize == 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nprivate int index(int i) {\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\nreturn (i + capacity()) % capacity();\n}\n/* \u961f\u9996\u5165\u961f */\npublic void pushFirst(int num) {\nif (queSize == capacity()) {\nSystem.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\nfront = index(front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nnums[front] = num;\nqueSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\npublic void pushLast(int num) {\nif (queSize == capacity()) {\nSystem.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = index(front + queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\npublic int popFirst() {\nint num = peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfront = index(front + 1);\nqueSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\npublic int popLast() {\nint num = peekLast();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peekFirst() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn nums[front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic int peekLast() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nint last = index(front + queSize - 1);\nreturn nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i &lt; queSize; i++, j++) {\nres[i] = nums[index(j)];\n}\nreturn res;\n}\n}\n</code></pre> array_deque.cpp<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate:\nvector&lt;int&gt; nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nint front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nint queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nArrayDeque(int capacity) {\nnums.resize(capacity);\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity() {\nreturn nums.size();\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn queSize == 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nint index(int i) {\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\nreturn (i + capacity()) % capacity();\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(int num) {\nif (queSize == capacity()) {\ncout &lt;&lt; \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" &lt;&lt; endl;\nreturn;\n}\n// \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\nfront = index(front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nnums[front] = num;\nqueSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(int num) {\nif (queSize == capacity()) {\ncout &lt;&lt; \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" &lt;&lt; endl;\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = index(front + queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst() {\nint num = peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfront = index(front + 1);\nqueSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast() {\nint num = peekLast();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst() {\nif (isEmpty())\nthrow out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\nreturn nums[front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast() {\nif (isEmpty())\nthrow out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nint last = index(front + queSize - 1);\nreturn nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nvector&lt;int&gt; toVector() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvector&lt;int&gt; res(queSize);\nfor (int i = 0, j = front; i &lt; queSize; i++, j++) {\nres[i] = nums[index(j)];\n}\nreturn res;\n}\n};\n</code></pre> array_deque.py<pre><code>class ArrayDeque:\n\"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\ndef __init__(self, capacity: int):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__nums: list[int] = [0] * capacity\nself.__front: int = 0\nself.__size: int = 0\ndef capacity(self) -&gt; int:\n\"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\nreturn len(self.__nums)\ndef size(self) -&gt; int:\n\"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -&gt; bool:\n\"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn self.__size == 0\ndef index(self, i: int) -&gt; int:\n\"\"\"\u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15\"\"\"\n# \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n# \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n# \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\nreturn (i + self.capacity()) % self.capacity()\ndef push_first(self, num: int):\n\"\"\"\u961f\u9996\u5165\u961f\"\"\"\nif self.__size == self.capacity():\nprint(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\n# \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n# \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\nself.__front = self.index(self.__front - 1)\n# \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nself.__nums[self.__front] = num\nself.__size += 1\ndef push_last(self, num: int):\n\"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\nif self.__size == self.capacity():\nprint(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\n# \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nrear = self.index(self.__front + self.__size)\n# \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.__nums[rear] = num\nself.__size += 1\ndef pop_first(self) -&gt; int:\n\"\"\"\u961f\u9996\u51fa\u961f\"\"\"\nnum = self.peek_first()\n# \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nself.__front = self.index(self.__front + 1)\nself.__size -= 1\nreturn num\ndef pop_last(self) -&gt; int:\n\"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\nnum = self.peek_last()\nself.__size -= 1\nreturn num\ndef peek_first(self) -&gt; int:\n\"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\nreturn self.__nums[self.__front]\ndef peek_last(self) -&gt; int:\n\"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n# \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nlast = self.index(self.__front + self.__size - 1)\nreturn self.__nums[last]\ndef to_array(self) -&gt; list[int]:\n\"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\n# \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nres = []\nfor i in range(self.__size):\nres.append(self.__nums[self.index(self.__front + i)])\nreturn res\n</code></pre> array_deque.go<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype arrayDeque struct {\nnums []int // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nfront int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nqueSize int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\nqueCapacity int // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayDeque(queCapacity int) *arrayDeque {\nreturn &amp;arrayDeque{\nnums: make([]int, queCapacity),\nqueCapacity: queCapacity,\nfront: 0,\nqueSize: 0,\n}\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayDeque) size() int {\nreturn q.queSize\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayDeque) isEmpty() bool {\nreturn q.queSize == 0\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nfunc (q *arrayDeque) index(i int) int {\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\nreturn (i + q.queCapacity) % q.queCapacity\n}\n/* \u961f\u9996\u5165\u961f */\nfunc (q *arrayDeque) pushFirst(num int) {\nif q.queSize == q.queCapacity {\nfmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\n}\n// \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\nq.front = q.index(q.front - 1)\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nq.nums[q.front] = num\nq.queSize++\n}\n/* \u961f\u5c3e\u5165\u961f */\nfunc (q *arrayDeque) pushLast(num int) {\nif q.queSize == q.queCapacity {\nfmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nrear := q.index(q.front + q.queSize)\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nq.nums[rear] = num\nq.queSize++\n}\n/* \u961f\u9996\u51fa\u961f */\nfunc (q *arrayDeque) popFirst() any {\nnum := q.peekFirst()\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nq.front = q.index(q.front + 1)\nq.queSize--\nreturn num\n}\n/* \u961f\u5c3e\u51fa\u961f */\nfunc (q *arrayDeque) popLast() any {\nnum := q.peekLast()\nq.queSize--\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayDeque) peekFirst() any {\nif q.isEmpty() {\nreturn nil\n}\nreturn q.nums[q.front]\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (q *arrayDeque) peekLast() any {\nif q.isEmpty() {\nreturn nil\n}\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nlast := q.index(q.front + q.queSize - 1)\nreturn q.nums[last]\n}\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayDeque) toSlice() []int {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nres := make([]int, q.queSize)\nfor i, j := 0, q.front; i &lt; q.queSize; i++ {\nres[i] = q.nums[q.index(j)]\nj++\n}\nreturn res\n}\n</code></pre> array_deque.js<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n#nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n#front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n#queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(capacity) {\nthis.#nums = new Array(capacity);\nthis.#front = 0;\nthis.#queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\ncapacity() {\nreturn this.#nums.length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize() {\nreturn this.#queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.#queSize === 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nindex(i) {\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\nreturn (i + this.capacity()) % this.capacity();\n}\n/* \u961f\u9996\u5165\u961f */\npushFirst(num) {\nif (this.#queSize === this.capacity()) {\nconsole.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\nreturn;\n}\n// \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\nthis.#front = this.index(this.#front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nthis.#nums[this.#front] = num;\nthis.#queSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\npushLast(num) {\nif (this.#queSize === this.capacity()) {\nconsole.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nconst rear = this.index(this.#front + this.#queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nthis.#nums[rear] = num;\nthis.#queSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\npopFirst() {\nconst num = this.peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nthis.#front = this.index(this.#front + 1);\nthis.#queSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\npopLast() {\nconst num = this.peekLast();\nthis.#queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeekFirst() {\nif (this.isEmpty()) throw new Error('The Deque Is Empty.');\nreturn this.#nums[this.#front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npeekLast() {\nif (this.isEmpty()) throw new Error('The Deque Is Empty.');\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nconst last = this.index(this.#front + this.#queSize - 1);\nreturn this.#nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\ntoArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst res = [];\nfor (let i = 0, j = this.#front; i &lt; this.#queSize; i++, j++) {\nres[i] = this.#nums[this.index(j)];\n}\nreturn res;\n}\n}\n</code></pre> array_deque.ts<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate queSize: number; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(capacity: number) {\nthis.nums = new Array(capacity);\nthis.front = 0;\nthis.queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\ncapacity(): number {\nreturn this.nums.length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize(): number {\nreturn this.queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.queSize === 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nindex(i: number): number {\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\nreturn (i + this.capacity()) % this.capacity();\n}\n/* \u961f\u9996\u5165\u961f */\npushFirst(num: number): void {\nif (this.queSize === this.capacity()) {\nconsole.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\nreturn;\n}\n// \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\nthis.front = this.index(this.front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nthis.nums[this.front] = num;\nthis.queSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\npushLast(num: number): void {\nif (this.queSize === this.capacity()) {\nconsole.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nconst rear: number = this.index(this.front + this.queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nthis.nums[rear] = num;\nthis.queSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\npopFirst(): number {\nconst num: number = this.peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nthis.front = this.index(this.front + 1);\nthis.queSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\npopLast(): number {\nconst num: number = this.peekLast();\nthis.queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeekFirst(): number {\nif (this.isEmpty()) throw new Error('The Deque Is Empty.');\nreturn this.nums[this.front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npeekLast(): number {\nif (this.isEmpty()) throw new Error('The Deque Is Empty.');\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nconst last = this.index(this.front + this.queSize - 1);\nreturn this.nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\ntoArray(): number[] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst res: number[] = [];\nfor (let i = 0, j = this.front; i &lt; this.queSize; i++, j++) {\nres[i] = this.nums[this.index(j)];\n}\nreturn res;\n}\n}\n</code></pre> array_deque.c<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct arrayDeque {\nint *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nint front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nint queSize; // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\nint queCapacity; // \u961f\u5217\u5bb9\u91cf\n};\ntypedef struct arrayDeque arrayDeque;\n/* \u6784\u9020\u51fd\u6570 */\narrayDeque *newArrayDeque(int capacity) {\narrayDeque *deque = (arrayDeque *)malloc(sizeof(arrayDeque));\n// \u521d\u59cb\u5316\u6570\u7ec4\ndeque-&gt;queCapacity = capacity;\ndeque-&gt;nums = (int *)malloc(sizeof(int) * deque-&gt;queCapacity);\ndeque-&gt;front = deque-&gt;queSize = 0;\nreturn deque;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayDeque(arrayDeque *deque) {\nfree(deque-&gt;nums);\ndeque-&gt;queCapacity = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(arrayDeque *deque) {\nreturn deque-&gt;queCapacity;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size(arrayDeque *deque) {\nreturn deque-&gt;queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(arrayDeque *deque) {\nreturn deque-&gt;queSize == 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nint dequeIndex(arrayDeque *deque, int i) {\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u56de\u5230\u5934\u90e8\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\nreturn ((i + capacity(deque)) % capacity(deque));\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(arrayDeque *deque, int num) {\nif (deque-&gt;queSize == capacity(deque)) {\nprintf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\nreturn;\n}\n// \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u56de\u5230\u5c3e\u90e8\ndeque-&gt;front = dequeIndex(deque, deque-&gt;front - 1);\n// \u5c06 num \u6dfb\u52a0\u5230\u961f\u9996\ndeque-&gt;nums[deque-&gt;front] = num;\ndeque-&gt;queSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(arrayDeque *deque, int num) {\nif (deque-&gt;queSize == capacity(deque)) {\nprintf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = dequeIndex(deque, deque-&gt;front + deque-&gt;queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque-&gt;nums[rear] = num;\ndeque-&gt;queSize++;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(arrayDeque *deque) {\n// \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\nassert(empty(deque) == 0);\nreturn deque-&gt;nums[deque-&gt;front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(arrayDeque *deque) {\n// \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\nassert(empty(deque) == 0);\nint last = dequeIndex(deque, deque-&gt;front + deque-&gt;queSize - 1);\nreturn deque-&gt;nums[last];\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(arrayDeque *deque) {\nint num = peekFirst(deque);\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\ndeque-&gt;front = dequeIndex(deque, deque-&gt;front + 1);\ndeque-&gt;queSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(arrayDeque *deque) {\nint num = peekLast(deque);\ndeque-&gt;queSize--;\nreturn num;\n}\n/* \u6253\u5370\u961f\u5217 */\nvoid printArrayDeque(arrayDeque *deque) {\nint arr[deque-&gt;queSize];\n// \u62f7\u8d1d\nfor (int i = 0, j = deque-&gt;front; i &lt; deque-&gt;queSize; i++, j++) {\narr[i] = deque-&gt;nums[j % deque-&gt;queCapacity];\n}\nprintArray(arr, deque-&gt;queSize);\n}\n</code></pre> array_deque.cs<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate readonly int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\npublic ArrayDeque(int capacity) {\nthis.nums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity() {\nreturn nums.Length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn queSize == 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nprivate int index(int i) {\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\nreturn (i + capacity()) % capacity();\n}\n/* \u961f\u9996\u5165\u961f */\npublic void pushFirst(int num) {\nif (queSize == capacity()) {\nConsole.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\nfront = index(front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nnums[front] = num;\nqueSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\npublic void pushLast(int num) {\nif (queSize == capacity()) {\nConsole.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = index(front + queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\npublic int popFirst() {\nint num = peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfront = index(front + 1);\nqueSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\npublic int popLast() {\nint num = peekLast();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peekFirst() {\nif (isEmpty()) {\nthrow new InvalidOperationException();\n}\nreturn nums[front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic int peekLast() {\nif (isEmpty()) {\nthrow new InvalidOperationException();\n}\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nint last = index(front + queSize - 1);\nreturn nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i &lt; queSize; i++, j++) {\nres[i] = nums[index(j)];\n}\nreturn res;\n}\n}\n</code></pre> array_deque.swift<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate var front: Int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate var queSize: Int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\ninit(capacity: Int) {\nnums = Array(repeating: 0, count: capacity)\nfront = 0\nqueSize = 0\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nfunc capacity() -&gt; Int {\nnums.count\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -&gt; Int {\nqueSize\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -&gt; Bool {\nsize() == 0\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nprivate func index(i: Int) -&gt; Int {\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n(i + capacity()) % capacity()\n}\n/* \u961f\u9996\u5165\u961f */\nfunc pushFirst(num: Int) {\nif size() == capacity() {\nprint(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\n}\n// \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\nfront = index(i: front - 1)\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nnums[front] = num\nqueSize += 1\n}\n/* \u961f\u5c3e\u5165\u961f */\nfunc pushLast(num: Int) {\nif size() == capacity() {\nprint(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nlet rear = index(i: front + size())\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num\nqueSize += 1\n}\n/* \u961f\u9996\u51fa\u961f */\nfunc popFirst() -&gt; Int {\nlet num = peekFirst()\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfront = index(i: front + 1)\nqueSize -= 1\nreturn num\n}\n/* \u961f\u5c3e\u51fa\u961f */\nfunc popLast() -&gt; Int {\nlet num = peekLast()\nqueSize -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peekFirst() -&gt; Int {\nif isEmpty() {\nfatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n}\nreturn nums[front]\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc peekLast() -&gt; Int {\nif isEmpty() {\nfatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n}\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nlet last = index(i: front + size() - 1)\nreturn nums[last]\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nfunc toArray() -&gt; [Int] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar res = Array(repeating: 0, count: size())\nfor (i, j) in sequence(first: (0, front), next: { $0 &lt; self.size() - 1 ? ($0 + 1, $1 + 1) : nil }) {\nres[i] = nums[index(i: j)]\n}\nreturn res\n}\n}\n</code></pre> array_deque.zig<pre><code>[class]{ArrayDeque}-[func]{}\n</code></pre> array_deque.dart<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nlate List&lt;int&gt; _nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nlate int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nlate int _queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\nArrayDeque(int capacity) {\nthis._nums = List.filled(capacity, 0);\nthis._front = this._queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity() {\nreturn _nums.length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn _queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _queSize == 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nint index(int i) {\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\nreturn (i + capacity()) % capacity();\n}\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(int num) {\nif (_queSize == capacity()) {\nthrow Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n}\n// \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 _front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n_front = index(_front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n_nums[_front] = num;\n_queSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(int num) {\nif (_queSize == capacity()) {\nthrow Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = index(_front + _queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n_nums[rear] = num;\n_queSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\nint popFirst() {\nint num = peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n_front = index(_front + 1);\n_queSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast() {\nint num = peekLast();\n_queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst() {\nif (isEmpty()) {\nthrow Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n}\nreturn _nums[_front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast() {\nif (isEmpty()) {\nthrow Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n}\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nint last = index(_front + _queSize - 1);\nreturn _nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nList&lt;int&gt; toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nList&lt;int&gt; res = List.filled(_queSize, 0);\nfor (int i = 0, j = _front; i &lt; _queSize; i++, j++) {\nres[i] = _nums[index(j)];\n}\nreturn res;\n}\n}\n</code></pre> array_deque.rs<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct ArrayDeque {\nnums: Vec&lt;i32&gt;, // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nfront: usize, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nque_size: usize, // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n}\nimpl ArrayDeque {\n/* \u6784\u9020\u65b9\u6cd5 */\npub fn new(capacity: usize) -&gt; Self {\nSelf {\nnums: vec![0; capacity],\nfront: 0,\nque_size: 0,\n}\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\npub fn capacity(&amp;self) -&gt; usize {\nself.nums.len()\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npub fn size(&amp;self) -&gt; usize {\nself.que_size\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npub fn is_empty(&amp;self) -&gt; bool {\nself.que_size == 0\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nfn index(&amp;self, i: i32) -&gt; usize {\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\nreturn ((i + self.capacity() as i32) % self.capacity() as i32) as usize;\n}\n/* \u961f\u9996\u5165\u961f */\npub fn push_first(&amp;mut self, num: i32) {\nif self.que_size == self.capacity() {\nprintln!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn\n}\n// \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\nself.front = self.index(self.front as i32 - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nself.nums[self.front] = num;\nself.que_size += 1;\n}\n/* \u961f\u5c3e\u5165\u961f */\npub fn push_last(&amp;mut self, num: i32) {\nif self.que_size == self.capacity() {\nprintln!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nlet rear = self.index(self.front as i32 + self.que_size as i32);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.nums[rear] = num;\nself.que_size += 1;\n}\n/* \u961f\u9996\u51fa\u961f */\nfn pop_first(&amp;mut self) -&gt; i32 {\nlet num = self.peek_first();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nself.front = self.index(self.front as i32 + 1);\nself.que_size -= 1;\nnum\n}\n/* \u961f\u5c3e\u51fa\u961f */\nfn pop_last(&amp;mut self) -&gt; i32 {\nlet num = self.peek_last();\nself.que_size -= 1;\nnum\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfn peek_first(&amp;self) -&gt; i32 {\nif self.is_empty() { panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\") };\nself.nums[self.front]\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfn peek_last(&amp;self) -&gt; i32 {\nif self.is_empty() { panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\") };\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nlet last = self.index(self.front as i32 + self.que_size as i32 - 1);\nself.nums[last]\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nfn to_array(&amp;self) -&gt; Vec&lt;i32&gt; {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nlet mut res = vec![0; self.que_size];\nlet mut j = self.front;\nfor i in 0..self.que_size {\nres[i] = self.nums[self.index(j as i32)];\nj += 1;\n}\nres\n}\n}\n</code></pre>"},{"location":"chapter_stack_and_queue/deque/#533","title":"5.3.3. \u00a0 \u53cc\u5411\u961f\u5217\u5e94\u7528","text":"<p>\u53cc\u5411\u961f\u5217\u517c\u5177\u6808\u4e0e\u961f\u5217\u7684\u903b\u8f91\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e24\u8005\u7684\u6240\u6709\u5e94\u7528\u573a\u666f\uff0c\u540c\u65f6\u63d0\u4f9b\u66f4\u9ad8\u7684\u81ea\u7531\u5ea6\u3002</p> <p>\u6211\u4eec\u77e5\u9053\uff0c\u8f6f\u4ef6\u7684\u201c\u64a4\u9500\u201d\u529f\u80fd\u901a\u5e38\u4f7f\u7528\u6808\u6765\u5b9e\u73b0\uff1a\u7cfb\u7edf\u5c06\u6bcf\u6b21\u66f4\u6539\u64cd\u4f5c <code>push</code> \u5230\u6808\u4e2d\uff0c\u7136\u540e\u901a\u8fc7 <code>pop</code> \u5b9e\u73b0\u64a4\u9500\u3002\u7136\u800c\uff0c\u8003\u8651\u5230\u7cfb\u7edf\u8d44\u6e90\u7684\u9650\u5236\uff0c\u8f6f\u4ef6\u901a\u5e38\u4f1a\u9650\u5236\u64a4\u9500\u7684\u6b65\u6570\uff08\u4f8b\u5982\u4ec5\u5141\u8bb8\u4fdd\u5b58 \\(50\\) \u6b65\uff09\u3002\u5f53\u6808\u7684\u957f\u5ea6\u8d85\u8fc7 \\(50\\) \u65f6\uff0c\u8f6f\u4ef6\u9700\u8981\u5728\u6808\u5e95\uff08\u5373\u961f\u9996\uff09\u6267\u884c\u5220\u9664\u64cd\u4f5c\u3002\u4f46\u6808\u65e0\u6cd5\u5b9e\u73b0\u8be5\u529f\u80fd\uff0c\u6b64\u65f6\u5c31\u9700\u8981\u4f7f\u7528\u53cc\u5411\u961f\u5217\u6765\u66ff\u4ee3\u6808\u3002\u8bf7\u6ce8\u610f\uff0c\u201c\u64a4\u9500\u201d\u7684\u6838\u5fc3\u903b\u8f91\u4ecd\u7136\u9075\u5faa\u6808\u7684\u5148\u5165\u540e\u51fa\u539f\u5219\uff0c\u53ea\u662f\u53cc\u5411\u961f\u5217\u80fd\u591f\u66f4\u52a0\u7075\u6d3b\u5730\u5b9e\u73b0\u4e00\u4e9b\u989d\u5916\u903b\u8f91\u3002</p>"},{"location":"chapter_stack_and_queue/queue/","title":"5.2. \u00a0 \u961f\u5217","text":"<p>\u300c\u961f\u5217 Queue\u300d\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u5148\u51fa\uff08First In, First Out\uff09\u89c4\u5219\u7684\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002\u987e\u540d\u601d\u4e49\uff0c\u961f\u5217\u6a21\u62df\u4e86\u6392\u961f\u73b0\u8c61\uff0c\u5373\u65b0\u6765\u7684\u4eba\u4e0d\u65ad\u52a0\u5165\u961f\u5217\u7684\u5c3e\u90e8\uff0c\u800c\u4f4d\u4e8e\u961f\u5217\u5934\u90e8\u7684\u4eba\u9010\u4e2a\u79bb\u5f00\u3002</p> <p>\u6211\u4eec\u628a\u961f\u5217\u7684\u5934\u90e8\u79f0\u4e3a\u300c\u961f\u9996\u300d\uff0c\u5c3e\u90e8\u79f0\u4e3a\u300c\u961f\u5c3e\u300d\uff0c\u628a\u5c06\u5143\u7d20\u52a0\u5165\u961f\u5c3e\u7684\u64cd\u4f5c\u79f0\u4e3a\u300c\u5165\u961f\u300d\uff0c\u5220\u9664\u961f\u9996\u5143\u7d20\u7684\u64cd\u4f5c\u79f0\u4e3a\u300c\u51fa\u961f\u300d\u3002</p> <p></p> <p> \u56fe\uff1a\u961f\u5217\u7684\u5148\u5165\u5148\u51fa\u89c4\u5219 </p>"},{"location":"chapter_stack_and_queue/queue/#521","title":"5.2.1. \u00a0 \u961f\u5217\u5e38\u7528\u64cd\u4f5c","text":"<p>\u961f\u5217\u7684\u5e38\u89c1\u64cd\u4f5c\u5982\u4e0b\u8868\u6240\u793a\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u65b9\u6cd5\u540d\u79f0\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u6211\u4eec\u5728\u6b64\u91c7\u7528\u4e0e\u6808\u76f8\u540c\u7684\u65b9\u6cd5\u547d\u540d\u3002</p> <p> \u8868\uff1a\u961f\u5217\u64cd\u4f5c\u6548\u7387 </p> \u65b9\u6cd5\u540d \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u961f\uff0c\u5373\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u961f\u5c3e \\(O(1)\\) pop() \u961f\u9996\u5143\u7d20\u51fa\u961f \\(O(1)\\) peek() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\) <p>\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u4e2d\u73b0\u6210\u7684\u961f\u5217\u7c7b\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust queue.java<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\nQueue&lt;Integer&gt; queue = new LinkedList&lt;&gt;();\n/* \u5143\u7d20\u5165\u961f */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.peek();\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.poll();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = queue.isEmpty();\n</code></pre> queue.cpp<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\nqueue&lt;int&gt; queue;\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint front = queue.front();\n/* \u5143\u7d20\u51fa\u961f */\nqueue.pop();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = queue.empty();\n</code></pre> queue.py<pre><code># \u521d\u59cb\u5316\u961f\u5217\n# \u5728 Python \u4e2d\uff0c\u6211\u4eec\u4e00\u822c\u5c06\u53cc\u5411\u961f\u5217\u7c7b deque \u770b\u4f5c\u961f\u5217\u4f7f\u7528\n# \u867d\u7136 queue.Queue() \u662f\u7eaf\u6b63\u7684\u961f\u5217\u7c7b\uff0c\u4f46\u4e0d\u592a\u597d\u7528\uff0c\u56e0\u6b64\u4e0d\u5efa\u8bae\nque: Deque[int] = collections.deque()\n# \u5143\u7d20\u5165\u961f\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n# \u8bbf\u95ee\u961f\u9996\u5143\u7d20\nfront: int = que[0];\n# \u5143\u7d20\u51fa\u961f\npop: int = que.popleft()\n# \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\nsize: int = len(que)\n# \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(que) == 0\n</code></pre> queue_test.go<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\n// \u5728 Go \u4e2d\uff0c\u5c06 list \u4f5c\u4e3a\u961f\u5217\u6765\u4f7f\u7528\nqueue := list.New()\n/* \u5143\u7d20\u5165\u961f */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek := queue.Front()\n/* \u5143\u7d20\u51fa\u961f */\npop := queue.Front()\nqueue.Remove(pop)\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nsize := queue.Len()\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := queue.Len() == 0\n</code></pre> queue.js<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\n// JavaScript \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nconst queue = [];\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\n/* \u5143\u7d20\u51fa\u961f */\n// \u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 shift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst pop = queue.shift();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst empty = queue.length === 0;\n</code></pre> queue.ts<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\n// TypeScript \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528 \nconst queue: number[] = [];\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\n/* \u5143\u7d20\u51fa\u961f */\n// \u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 shift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst pop = queue.shift();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst empty = queue.length === 0;\n</code></pre> queue.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u961f\u5217\n</code></pre> queue.cs<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\nQueue&lt;int&gt; queue = new();\n/* \u5143\u7d20\u5165\u961f */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.Peek();\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.Dequeue();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.Count;\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.Count == 0;\n</code></pre> queue.swift<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nvar queue: [Int] = []\n/* \u5143\u7d20\u5165\u961f */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nlet peek = queue.first!\n/* \u5143\u7d20\u51fa\u961f */\n// \u7531\u4e8e\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 removeFirst \u7684\u590d\u6742\u5ea6\u4e3a O(n)\nlet pool = queue.removeFirst()\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = queue.count\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = queue.isEmpty\n</code></pre> queue.zig<pre><code>\n</code></pre> queue.dart<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\n// \u5728 Dart \u4e2d\uff0c\u961f\u5217\u7c7b Qeque \u662f\u53cc\u5411\u961f\u5217\uff0c\u4e5f\u53ef\u4f5c\u4e3a\u961f\u5217\u4f7f\u7528\nQueue&lt;int&gt; queue = Queue();\n/* \u5143\u7d20\u5165\u961f */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.first;\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.removeFirst();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.length;\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.isEmpty;\n</code></pre> queue.rs<pre><code>\n</code></pre>"},{"location":"chapter_stack_and_queue/queue/#522","title":"5.2.2. \u00a0 \u961f\u5217\u5b9e\u73b0","text":"<p>\u4e3a\u4e86\u5b9e\u73b0\u961f\u5217\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5728\u4e00\u7aef\u6dfb\u52a0\u5143\u7d20\uff0c\u5e76\u5728\u53e6\u4e00\u7aef\u5220\u9664\u5143\u7d20\u3002\u56e0\u6b64\uff0c\u94fe\u8868\u548c\u6570\u7ec4\u90fd\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u961f\u5217\u3002</p>"},{"location":"chapter_stack_and_queue/queue/#_1","title":"\u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"<p>\u5bf9\u4e8e\u94fe\u8868\u5b9e\u73b0\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u300c\u5934\u8282\u70b9\u300d\u548c\u300c\u5c3e\u8282\u70b9\u300d\u5206\u522b\u89c6\u4e3a\u961f\u9996\u548c\u961f\u5c3e\uff0c\u89c4\u5b9a\u961f\u5c3e\u4ec5\u53ef\u6dfb\u52a0\u8282\u70b9\uff0c\u800c\u961f\u9996\u4ec5\u53ef\u5220\u9664\u8282\u70b9\u3002</p> LinkedListQueuepush()pop() <p></p> <p></p> <p></p> <p> \u56fe\uff1a\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c </p> <p>\u4ee5\u4e0b\u662f\u7528\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u793a\u4f8b\u4ee3\u7801\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust linkedlist_queue.java<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nprivate int queSize = 0;\npublic LinkedListQueue() {\nfront = null;\nrear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f */\npublic void push(int num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nListNode node = new ListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif (front == null) {\nfront = node;\nrear = node;\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n} else {\nrear.next = node;\nrear = node;\n}\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int pop() {\nint num = peek();\n// \u5220\u9664\u5934\u8282\u70b9\nfront = front.next;\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek() {\nif (size() == 0)\nthrow new IndexOutOfBoundsException();\nreturn front.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i &lt; res.length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_queue.cpp<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate:\nListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\nint queSize;\npublic:\nLinkedListQueue() {\nfront = nullptr;\nrear = nullptr;\nqueSize = 0;\n}\n~LinkedListQueue() {\n// \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\nfreeMemoryLinkedList(front);\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn queSize == 0;\n}\n/* \u5165\u961f */\nvoid push(int num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nListNode *node = new ListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif (front == nullptr) {\nfront = node;\nrear = node;\n}\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nelse {\nrear-&gt;next = node;\nrear = node;\n}\nqueSize++;\n}\n/* \u51fa\u961f */\nvoid pop() {\nint num = peek();\n// \u5220\u9664\u5934\u8282\u70b9\nListNode *tmp = front;\nfront = front-&gt;next;\n// \u91ca\u653e\u5185\u5b58\ndelete tmp;\nqueSize--;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek() {\nif (size() == 0)\nthrow out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn front-&gt;val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\nvector&lt;int&gt; toVector() {\nListNode *node = front;\nvector&lt;int&gt; res(size());\nfor (int i = 0; i &lt; res.size(); i++) {\nres[i] = node-&gt;val;\nnode = node-&gt;next;\n}\nreturn res;\n}\n};\n</code></pre> linkedlist_queue.py<pre><code>class LinkedListQueue:\n\"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__front: ListNode | None = None # \u5934\u8282\u70b9 front\nself.__rear: ListNode | None = None # \u5c3e\u8282\u70b9 rear\nself.__size: int = 0\ndef size(self) -&gt; int:\n\"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -&gt; bool:\n\"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn not self.__front\ndef push(self, num: int):\n\"\"\"\u5165\u961f\"\"\"\n# \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nnode = ListNode(num)\n# \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif self.__front is None:\nself.__front = node\nself.__rear = node\n# \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nelse:\nself.__rear.next = node\nself.__rear = node\nself.__size += 1\ndef pop(self) -&gt; int:\n\"\"\"\u51fa\u961f\"\"\"\nnum = self.peek()\n# \u5220\u9664\u5934\u8282\u70b9\nself.__front = self.__front.next\nself.__size -= 1\nreturn num\ndef peek(self) -&gt; int:\n\"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\nif self.size() == 0:\nprint(\"\u961f\u5217\u4e3a\u7a7a\")\nreturn False\nreturn self.__front.val\ndef to_list(self) -&gt; list[int]:\n\"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\nqueue = []\ntemp = self.__front\nwhile temp:\nqueue.append(temp.val)\ntemp = temp.next\nreturn queue\n</code></pre> linkedlist_queue.go<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\ntype linkedListQueue struct {\n// \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u961f\u5217\ndata *list.List\n}\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newLinkedListQueue() *linkedListQueue {\nreturn &amp;linkedListQueue{\ndata: list.New(),\n}\n}\n/* \u5165\u961f */\nfunc (s *linkedListQueue) push(value any) {\ns.data.PushBack(value)\n}\n/* \u51fa\u961f */\nfunc (s *linkedListQueue) pop() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Front()\ns.data.Remove(e)\nreturn e.Value\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListQueue) peek() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Front()\nreturn e.Value\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListQueue) size() int {\nreturn s.data.Len()\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListQueue) isEmpty() bool {\nreturn s.data.Len() == 0\n}\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListQueue) toList() *list.List {\nreturn s.data\n}\n</code></pre> linkedlist_queue.js<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n#front; // \u5934\u8282\u70b9 #front\n#rear; // \u5c3e\u8282\u70b9 #rear\n#queSize = 0;\nconstructor() {\nthis.#front = null;\nthis.#rear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.size === 0;\n}\n/* \u5165\u961f */\npush(num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nconst node = new ListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif (!this.#front) {\nthis.#front = node;\nthis.#rear = node;\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n} else {\nthis.#rear.next = node;\nthis.#rear = node;\n}\nthis.#queSize++;\n}\n/* \u51fa\u961f */\npop() {\nconst num = this.peek();\n// \u5220\u9664\u5934\u8282\u70b9\nthis.#front = this.#front.next;\nthis.#queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek() {\nif (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\nreturn this.#front.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray() {\nlet node = this.#front;\nconst res = new Array(this.size);\nfor (let i = 0; i &lt; res.length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_queue.ts<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate front: ListNode | null; // \u5934\u8282\u70b9 front\nprivate rear: ListNode | null; // \u5c3e\u8282\u70b9 rear\nprivate queSize: number = 0;\nconstructor() {\nthis.front = null;\nthis.rear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.size === 0;\n}\n/* \u5165\u961f */\npush(num: number): void {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nconst node = new ListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif (!this.front) {\nthis.front = node;\nthis.rear = node;\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n} else {\nthis.rear!.next = node;\nthis.rear = node;\n}\nthis.queSize++;\n}\n/* \u51fa\u961f */\npop(): number {\nconst num = this.peek();\nif (!this.front) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n// \u5220\u9664\u5934\u8282\u70b9\nthis.front = this.front.next;\nthis.queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek(): number {\nif (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\nreturn this.front!.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray(): number[] {\nlet node = this.front;\nconst res = new Array&lt;number&gt;(this.size);\nfor (let i = 0; i &lt; res.length; i++) {\nres[i] = node!.val;\nnode = node!.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_queue.c<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct linkedListQueue {\nListNode *front, *rear;\nint queSize;\n};\ntypedef struct linkedListQueue linkedListQueue;\n/* \u6784\u9020\u51fd\u6570 */\nlinkedListQueue *newLinkedListQueue() {\nlinkedListQueue *queue = (linkedListQueue *)malloc(sizeof(linkedListQueue));\nqueue-&gt;front = NULL;\nqueue-&gt;rear = NULL;\nqueue-&gt;queSize = 0;\nreturn queue;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListQueue(linkedListQueue *queue) {\n// \u91ca\u653e\u6240\u6709\u8282\u70b9\nfor (int i = 0; i &lt; queue-&gt;queSize &amp;&amp; queue-&gt;front != NULL; i++) {\nListNode *tmp = queue-&gt;front;\nqueue-&gt;front = queue-&gt;front-&gt;next;\nfree(tmp);\n}\n// \u91ca\u653e queue \u7ed3\u6784\u4f53\nfree(queue);\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(linkedListQueue *queue) {\nreturn queue-&gt;queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(linkedListQueue *queue) {\nreturn (size(queue) == 0);\n}\n/* \u5165\u961f */\nvoid push(linkedListQueue *queue, int num) {\n// \u5c3e\u8282\u70b9\u5904\u6dfb\u52a0 node\nListNode *node = newListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif (queue-&gt;front == NULL) {\nqueue-&gt;front = node;\nqueue-&gt;rear = node;\n}\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nelse {\nqueue-&gt;rear-&gt;next = node;\nqueue-&gt;rear = node;\n}\nqueue-&gt;queSize++;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(linkedListQueue *queue) {\nassert(size(queue) &amp;&amp; queue-&gt;front);\nreturn queue-&gt;front-&gt;val;\n}\n/* \u51fa\u961f */\nvoid pop(linkedListQueue *queue) {\nint num = peek(queue);\nListNode *tmp = queue-&gt;front;\nqueue-&gt;front = queue-&gt;front-&gt;next;\nfree(tmp);\nqueue-&gt;queSize--;\n}\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListQueue(linkedListQueue *queue) {\nint arr[queue-&gt;queSize];\n// \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\nint i;\nListNode *node;\nfor (i = 0, node = queue-&gt;front; i &lt; queue-&gt;queSize &amp;&amp; queue-&gt;front != queue-&gt;rear; i++) {\narr[i] = node-&gt;val;\nnode = node-&gt;next;\n}\nprintArray(arr, queue-&gt;queSize);\n}\n</code></pre> linkedlist_queue.cs<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate ListNode? front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear \nprivate int queSize = 0;\npublic LinkedListQueue() {\nfront = null;\nrear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f */\npublic void push(int num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nListNode node = new ListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif (front == null) {\nfront = node;\nrear = node;\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n} else if (rear != null) {\nrear.next = node;\nrear = node;\n}\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int pop() {\nint num = peek();\n// \u5220\u9664\u5934\u8282\u70b9\nfront = front?.next;\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek() {\nif (size() == 0 || front == null)\nthrow new Exception();\nreturn front.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nif (front == null)\nreturn Array.Empty&lt;int&gt;();\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i &lt; res.Length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_queue.swift<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate var front: ListNode? // \u5934\u8282\u70b9\nprivate var rear: ListNode? // \u5c3e\u8282\u70b9\nprivate var _size = 0\ninit() {}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -&gt; Int {\n_size\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -&gt; Bool {\nsize() == 0\n}\n/* \u5165\u961f */\nfunc push(num: Int) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nlet node = ListNode(x: num)\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif front == nil {\nfront = node\nrear = node\n}\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nelse {\nrear?.next = node\nrear = node\n}\n_size += 1\n}\n/* \u51fa\u961f */\n@discardableResult\nfunc pop() -&gt; Int {\nlet num = peek()\n// \u5220\u9664\u5934\u8282\u70b9\nfront = front?.next\n_size -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peek() -&gt; Int {\nif isEmpty() {\nfatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n}\nreturn front!.val\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nfunc toArray() -&gt; [Int] {\nvar node = front\nvar res = Array(repeating: 0, count: size())\nfor i in res.indices {\nres[i] = node!.val\nnode = node?.next\n}\nreturn res\n}\n}\n</code></pre> linkedlist_queue.zig<pre><code>// \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\nfn LinkedListQueue(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nfront: ?*inc.ListNode(T) = null, // \u5934\u8282\u70b9 front\nrear: ?*inc.ListNode(T) = null, // \u5c3e\u8282\u70b9 rear\nque_size: usize = 0, // \u961f\u5217\u7684\u957f\u5ea6\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.front = null;\nself.rear = null;\nself.que_size = 0;\n}\n// \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.que_size;\n}\n// \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u8bbf\u95ee\u961f\u9996\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.size() == 0) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn self.front.?.val;\n} // \u5165\u961f\npub fn push(self: *Self, num: T) !void {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nvar node = try self.mem_allocator.create(inc.ListNode(T));\nnode.init(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif (self.front == null) {\nself.front = node;\nself.rear = node;\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n} else {\nself.rear.?.next = node;\nself.rear = node;\n}\nself.que_size += 1;\n} // \u51fa\u961f\npub fn pop(self: *Self) T {\nvar num = self.peek();\n// \u5220\u9664\u5934\u8282\u70b9\nself.front = self.front.?.next;\nself.que_size -= 1;\nreturn num;\n} // \u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\nvar node = self.front;\nvar res = try self.mem_allocator.alloc(T, self.size());\n@memset(res, @as(T, 0));\nvar i: usize = 0;\nwhile (i &lt; res.len) : (i += 1) {\nres[i] = node.?.val;\nnode = node.?.next;\n}\nreturn res;\n}\n};\n}\n</code></pre> linkedlist_queue.dart<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nListNode? _front; // \u5934\u8282\u70b9 _front\nListNode? _rear; // \u5c3e\u8282\u70b9 _rear\nint _queSize = 0; // \u961f\u5217\u957f\u5ea6\nLinkedListQueue() {\n_front = null;\n_rear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn _queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _queSize == 0;\n}\n/* \u5165\u961f */\nvoid push(int num) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nfinal node = ListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nif (_front == null) {\n_front = node;\n_rear = node;\n} else {\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n_rear!.next = node;\n_rear = node;\n}\n_queSize++;\n}\n/* \u51fa\u961f */\nint pop() {\nfinal int num = peek();\n// \u5220\u9664\u5934\u8282\u70b9\n_front = _front!.next;\n_queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek() {\nif (_queSize == 0) {\nthrow Exception('\u961f\u5217\u4e3a\u7a7a');\n}\nreturn _front!.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nList&lt;int&gt; toArray() {\nListNode? node = _front;\nfinal List&lt;int&gt; queue = [];\nwhile (node != null) {\nqueue.add(node.val);\nnode = node.next;\n}\nreturn queue;\n}\n}\n</code></pre> linkedlist_queue.rs<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListQueue&lt;T&gt; {\nfront: Option&lt;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;, // \u5934\u8282\u70b9 front\nrear: Option&lt;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;, // \u5c3e\u8282\u70b9 rear \nque_size: usize, // \u961f\u5217\u7684\u957f\u5ea6\n}\nimpl&lt;T: Copy&gt; LinkedListQueue&lt;T&gt; {\npub fn new() -&gt; Self {\nSelf {\nfront: None,\nrear: None,\nque_size: 0, }\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npub fn size(&amp;self) -&gt; usize {\nreturn self.que_size;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npub fn is_empty(&amp;self) -&gt; bool {\nreturn self.size() == 0;\n}\n/* \u5165\u961f */\npub fn push(&amp;mut self, num: T) {\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nlet new_rear = ListNode::new(num);\nmatch self.rear.take() {\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\nSome(old_rear) =&gt; {\nold_rear.borrow_mut().next = Some(new_rear.clone());\nself.rear = Some(new_rear);\n}\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\nNone =&gt; {\nself.front = Some(new_rear.clone());\nself.rear = Some(new_rear);\n}\n}\nself.que_size += 1;\n}\n/* \u51fa\u961f */\npub fn pop(&amp;mut self) -&gt; Option&lt;T&gt; {\nself.front.take().map(|old_front| {\nmatch old_front.borrow_mut().next.take() {\nSome(new_front) =&gt; {\nself.front = Some(new_front);\n}\nNone =&gt; {\nself.rear.take();\n}\n}\nself.que_size -= 1;\nRc::try_unwrap(old_front).ok().unwrap().into_inner().val\n})\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npub fn peek(&amp;self) -&gt; Option&lt;&amp;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt; {\nself.front.as_ref()\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npub 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; {\nif let Some(node) = head {\nlet mut nums = self.to_array(node.borrow().next.as_ref());\nnums.insert(0, node.borrow().val);\nreturn nums;\n}\nreturn Vec::new();\n}\n}\n</code></pre>"},{"location":"chapter_stack_and_queue/queue/#_2","title":"\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"<p>\u7531\u4e8e\u6570\u7ec4\u5220\u9664\u9996\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff0c\u8fd9\u4f1a\u5bfc\u81f4\u51fa\u961f\u64cd\u4f5c\u6548\u7387\u8f83\u4f4e\u3002\u7136\u800c\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u5de7\u5999\u65b9\u6cd5\u6765\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u3002</p> <p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u53d8\u91cf <code>front</code> \u6307\u5411\u961f\u9996\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u5e76\u7ef4\u62a4\u4e00\u4e2a\u53d8\u91cf <code>queSize</code> \u7528\u4e8e\u8bb0\u5f55\u961f\u5217\u957f\u5ea6\u3002\u5b9a\u4e49 <code>rear = front + queSize</code> \uff0c\u8fd9\u4e2a\u516c\u5f0f\u8ba1\u7b97\u51fa\u7684 <code>rear</code> \u6307\u5411\u961f\u5c3e\u5143\u7d20\u4e4b\u540e\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u3002</p> <p>\u57fa\u4e8e\u6b64\u8bbe\u8ba1\uff0c\u6570\u7ec4\u4e2d\u5305\u542b\u5143\u7d20\u7684\u6709\u6548\u533a\u95f4\u4e3a [front, rear - 1]\uff0c\u8fdb\u800c\uff1a</p> <ul> <li>\u5bf9\u4e8e\u5165\u961f\u64cd\u4f5c\uff0c\u5c06\u8f93\u5165\u5143\u7d20\u8d4b\u503c\u7ed9 <code>rear</code> \u7d22\u5f15\u5904\uff0c\u5e76\u5c06 <code>queSize</code> \u589e\u52a0 1 \u3002</li> <li>\u5bf9\u4e8e\u51fa\u961f\u64cd\u4f5c\uff0c\u53ea\u9700\u5c06 <code>front</code> \u589e\u52a0 1 \uff0c\u5e76\u5c06 <code>queSize</code> \u51cf\u5c11 1 \u3002</li> </ul> <p>\u53ef\u4ee5\u770b\u5230\uff0c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u90fd\u53ea\u9700\u8fdb\u884c\u4e00\u6b21\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(1)\\) \u3002</p> ArrayQueuepush()pop() <p></p> <p></p> <p></p> <p> \u56fe\uff1a\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c </p> <p>\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a\u5728\u4e0d\u65ad\u8fdb\u884c\u5165\u961f\u548c\u51fa\u961f\u7684\u8fc7\u7a0b\u4e2d\uff0c<code>front</code> \u548c <code>rear</code> \u90fd\u5728\u5411\u53f3\u79fb\u52a8\uff0c\u5f53\u5b83\u4eec\u5230\u8fbe\u6570\u7ec4\u5c3e\u90e8\u65f6\u5c31\u65e0\u6cd5\u7ee7\u7eed\u79fb\u52a8\u4e86\u3002\u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u89c6\u4e3a\u9996\u5c3e\u76f8\u63a5\u7684\u300c\u73af\u5f62\u6570\u7ec4\u300d\u3002</p> <p>\u5bf9\u4e8e\u73af\u5f62\u6570\u7ec4\uff0c\u6211\u4eec\u9700\u8981\u8ba9 <code>front</code> \u6216 <code>rear</code> \u5728\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u76f4\u63a5\u56de\u5230\u6570\u7ec4\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002\u8fd9\u79cd\u5468\u671f\u6027\u89c4\u5f8b\u53ef\u4ee5\u901a\u8fc7\u201c\u53d6\u4f59\u64cd\u4f5c\u201d\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust array_queue.java<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u961f\u5217\u957f\u5ea6\npublic ArrayQueue(int capacity) {\nnums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity() {\nreturn nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn queSize == 0;\n}\n/* \u5165\u961f */\npublic void push(int num) {\nif (queSize == capacity()) {\nSystem.out.println(\"\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nint rear = (front + queSize) % capacity();\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int pop() {\nint num = peek();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nfront = (front + 1) % capacity();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn nums[front];\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i &lt; queSize; i++, j++) {\nres[i] = nums[j % capacity()];\n}\nreturn res;\n}\n}\n</code></pre> array_queue.cpp<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate:\nint *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nint front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nint queSize; // \u961f\u5217\u957f\u5ea6\nint queCapacity; // \u961f\u5217\u5bb9\u91cf\npublic:\nArrayQueue(int capacity) {\n// \u521d\u59cb\u5316\u6570\u7ec4\nnums = new int[capacity];\nqueCapacity = capacity;\nfront = queSize = 0;\n}\n~ArrayQueue() {\ndelete[] nums;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity() {\nreturn queCapacity;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn size() == 0;\n}\n/* \u5165\u961f */\nvoid push(int num) {\nif (queSize == queCapacity) {\ncout &lt;&lt; \"\u961f\u5217\u5df2\u6ee1\" &lt;&lt; endl;\nreturn;\n}\n// \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nint rear = (front + queSize) % queCapacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u51fa\u961f */\nvoid pop() {\nint num = peek();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nfront = (front + 1) % queCapacity;\nqueSize--;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek() {\nif (empty())\nthrow out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn nums[front];\n}\n/* \u5c06\u6570\u7ec4\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\nvector&lt;int&gt; toVector() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvector&lt;int&gt; arr(queSize);\nfor (int i = 0, j = front; i &lt; queSize; i++, j++) {\narr[i] = nums[j % queCapacity];\n}\nreturn arr;\n}\n};\n</code></pre> array_queue.py<pre><code>class ArrayQueue:\n\"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\ndef __init__(self, size: int):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__nums: list[int] = [0] * size # \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nself.__front: int = 0 # \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nself.__size: int = 0 # \u961f\u5217\u957f\u5ea6\ndef capacity(self) -&gt; int:\n\"\"\"\u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\nreturn len(self.__nums)\ndef size(self) -&gt; int:\n\"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -&gt; bool:\n\"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn self.__size == 0\ndef push(self, num: int):\n\"\"\"\u5165\u961f\"\"\"\nif self.__size == self.capacity():\nraise IndexError(\"\u961f\u5217\u5df2\u6ee1\")\n# \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n# \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nrear: int = (self.__front + self.__size) % self.capacity()\n# \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.__nums[rear] = num\nself.__size += 1\ndef pop(self) -&gt; int:\n\"\"\"\u51fa\u961f\"\"\"\nnum: int = self.peek()\n# \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nself.__front = (self.__front + 1) % self.capacity()\nself.__size -= 1\nreturn num\ndef peek(self) -&gt; int:\n\"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\nreturn self.__nums[self.__front]\ndef to_list(self) -&gt; list[int]:\n\"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\nres = [0] * self.size()\nj: int = self.__front\nfor i in range(self.size()):\nres[i] = self.__nums[(j % self.capacity())]\nj += 1\nreturn res\n</code></pre> array_queue.go<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntype arrayQueue struct {\nnums []int // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nfront int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nqueSize int // \u961f\u5217\u957f\u5ea6\nqueCapacity int // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayQueue(queCapacity int) *arrayQueue {\nreturn &amp;arrayQueue{\nnums: make([]int, queCapacity),\nqueCapacity: queCapacity,\nfront: 0,\nqueSize: 0,\n}\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayQueue) size() int {\nreturn q.queSize\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayQueue) isEmpty() bool {\nreturn q.queSize == 0\n}\n/* \u5165\u961f */\nfunc (q *arrayQueue) push(num int) {\n// \u5f53 rear == queCapacity \u8868\u793a\u961f\u5217\u5df2\u6ee1\nif q.queSize == q.queCapacity {\nreturn\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nrear := (q.front + q.queSize) % q.queCapacity\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nq.nums[rear] = num\nq.queSize++\n}\n/* \u51fa\u961f */\nfunc (q *arrayQueue) pop() any {\nnum := q.peek()\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nq.front = (q.front + 1) % q.queCapacity\nq.queSize--\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayQueue) peek() any {\nif q.isEmpty() {\nreturn nil\n}\nreturn q.nums[q.front]\n}\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayQueue) toSlice() []int {\nrear := (q.front + q.queSize)\nif rear &gt;= q.queCapacity {\nrear %= q.queCapacity\nreturn append(q.nums[q.front:], q.nums[:rear]...)\n}\nreturn q.nums[q.front:rear]\n}\n</code></pre> array_queue.js<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n#nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n#front = 0; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n#queSize = 0; // \u961f\u5217\u957f\u5ea6\nconstructor(capacity) {\nthis.#nums = new Array(capacity);\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nget capacity() {\nreturn this.#nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nempty() {\nreturn this.#queSize === 0;\n}\n/* \u5165\u961f */\npush(num) {\nif (this.size === this.capacity) {\nconsole.log('\u961f\u5217\u5df2\u6ee1');\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nconst rear = (this.#front + this.size) % this.capacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nthis.#nums[rear] = num;\nthis.#queSize++;\n}\n/* \u51fa\u961f */\npop() {\nconst num = this.peek();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nthis.#front = (this.#front + 1) % this.capacity;\nthis.#queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek() {\nif (this.empty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\nreturn this.#nums[this.#front];\n}\n/* \u8fd4\u56de Array */\ntoArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst arr = new Array(this.size);\nfor (let i = 0, j = this.#front; i &lt; this.size; i++, j++) {\narr[i] = this.#nums[j % this.capacity];\n}\nreturn arr;\n}\n}\n</code></pre> array_queue.ts<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate queSize: number; // \u961f\u5217\u957f\u5ea6\nconstructor(capacity: number) {\nthis.nums = new Array(capacity);\nthis.front = this.queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nget capacity(): number {\nreturn this.nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nempty(): boolean {\nreturn this.queSize === 0;\n}\n/* \u5165\u961f */\npush(num: number): void {\nif (this.size === this.capacity) {\nconsole.log('\u961f\u5217\u5df2\u6ee1');\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nconst rear = (this.front + this.queSize) % this.capacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nthis.nums[rear] = num;\nthis.queSize++;\n}\n/* \u51fa\u961f */\npop(): number {\nconst num = this.peek();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nthis.front = (this.front + 1) % this.capacity;\nthis.queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek(): number {\nif (this.empty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\nreturn this.nums[this.front];\n}\n/* \u8fd4\u56de Array */\ntoArray(): number[] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst arr = new Array(this.size);\nfor (let i = 0, j = this.front; i &lt; this.size; i++, j++) {\narr[i] = this.nums[j % this.capacity];\n}\nreturn arr;\n}\n}\n</code></pre> array_queue.c<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct arrayQueue {\nint *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nint front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nint queSize; // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\nint queCapacity; // \u961f\u5217\u5bb9\u91cf\n};\ntypedef struct arrayQueue arrayQueue;\n/* \u6784\u9020\u51fd\u6570 */\narrayQueue *newArrayQueue(int capacity) {\narrayQueue *queue = (arrayQueue *)malloc(sizeof(arrayQueue));\n// \u521d\u59cb\u5316\u6570\u7ec4\nqueue-&gt;queCapacity = capacity;\nqueue-&gt;nums = (int *)malloc(sizeof(int) * queue-&gt;queCapacity);\nqueue-&gt;front = queue-&gt;queSize = 0;\nreturn queue;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayQueue(arrayQueue *queue) {\nfree(queue-&gt;nums);\nqueue-&gt;queCapacity = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(arrayQueue *queue) {\nreturn queue-&gt;queCapacity;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(arrayQueue *queue) {\nreturn queue-&gt;queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(arrayQueue *queue) {\nreturn queue-&gt;queSize == 0;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(arrayQueue *queue) {\nassert(size(queue) != 0);\nreturn queue-&gt;nums[queue-&gt;front];\n}\n/* \u5165\u961f */\nvoid push(arrayQueue *queue, int num) {\nif (size(queue) == capacity(queue)) {\nprintf(\"\u961f\u5217\u5df2\u6ee1\\r\\n\");\nreturn;\n}\n// \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nint rear = (queue-&gt;front + queue-&gt;queSize) % queue-&gt;queCapacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nqueue-&gt;nums[rear] = num;\nqueue-&gt;queSize++;\n}\n/* \u51fa\u961f */\nvoid pop(arrayQueue *queue) {\nint num = peek(queue);\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nqueue-&gt;front = (queue-&gt;front + 1) % queue-&gt;queCapacity;\nqueue-&gt;queSize--;\n}\n/* \u6253\u5370\u961f\u5217 */\nvoid printArrayQueue(arrayQueue *queue) {\nint arr[queue-&gt;queSize];\n// \u62f7\u8d1d\nfor (int i = 0, j = queue-&gt;front; i &lt; queue-&gt;queSize; i++, j++) {\narr[i] = queue-&gt;nums[j % queue-&gt;queCapacity];\n}\nprintArray(arr, queue-&gt;queSize);\n}\n</code></pre> array_queue.cs<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u961f\u5217\u957f\u5ea6\npublic ArrayQueue(int capacity) {\nnums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity() {\nreturn nums.Length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn queSize == 0;\n}\n/* \u5165\u961f */\npublic void push(int num) {\nif (queSize == capacity()) {\nConsole.WriteLine(\"\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nint rear = (front + queSize) % capacity();\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int pop() {\nint num = peek();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nfront = (front + 1) % capacity();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek() {\nif (isEmpty())\nthrow new Exception();\nreturn nums[front];\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i &lt; queSize; i++, j++) {\nres[i] = nums[j % this.capacity()];\n}\nreturn res;\n}\n}\n</code></pre> array_queue.swift<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate var front = 0 // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate var queSize = 0 // \u961f\u5217\u957f\u5ea6\ninit(capacity: Int) {\n// \u521d\u59cb\u5316\u6570\u7ec4\nnums = Array(repeating: 0, count: capacity)\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nfunc capacity() -&gt; Int {\nnums.count\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -&gt; Int {\nqueSize\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -&gt; Bool {\nqueSize == 0\n}\n/* \u5165\u961f */\nfunc push(num: Int) {\nif size() == capacity() {\nprint(\"\u961f\u5217\u5df2\u6ee1\")\nreturn\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nlet rear = (front + queSize) % capacity()\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num\nqueSize += 1\n}\n/* \u51fa\u961f */\n@discardableResult\nfunc pop() -&gt; Int {\nlet num = peek()\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nfront = (front + 1) % capacity()\nqueSize -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peek() -&gt; Int {\nif isEmpty() {\nfatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n}\nreturn nums[front]\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\nfunc toArray() -&gt; [Int] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar res = Array(repeating: 0, count: queSize)\nfor (i, j) in sequence(first: (0, front), next: { $0 &lt; self.queSize - 1 ? ($0 + 1, $1 + 1) : nil }) {\nres[i] = nums[j % capacity()]\n}\nreturn res\n}\n}\n</code></pre> array_queue.zig<pre><code>// \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\nfn ArrayQueue(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nnums: []T = undefined, // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4 \ncap: usize = 0, // \u961f\u5217\u5bb9\u91cf\nfront: usize = 0, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nqueSize: usize = 0, // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6570\u7ec4\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.cap = cap;\nself.nums = try self.mem_allocator.alloc(T, self.cap);\n@memset(self.nums, @as(T, 0));\n}\n// \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\npub fn capacity(self: *Self) usize {\nreturn self.cap;\n}\n// \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.queSize;\n}\n// \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.queSize == 0;\n}\n// \u5165\u961f\npub fn push(self: *Self, num: T) !void {\nif (self.size() == self.capacity()) {\nstd.debug.print(\"\u961f\u5217\u5df2\u6ee1\\n\", .{});\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nvar rear = (self.front + self.queSize) % self.capacity();\n// \u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\nself.nums[rear] = num;\nself.queSize += 1;\n} // \u51fa\u961f\npub fn pop(self: *Self) T {\nvar num = self.peek();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nself.front = (self.front + 1) % self.capacity();\nself.queSize -= 1;\nreturn num;\n} // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.isEmpty()) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn self.nums[self.front];\n} // \u8fd4\u56de\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar res = try self.mem_allocator.alloc(T, self.size());\n@memset(res, @as(T, 0));\nvar i: usize = 0;\nvar j: usize = self.front;\nwhile (i &lt; self.size()) : ({ i += 1; j += 1; }) {\nres[i] = self.nums[j % self.capacity()];\n}\nreturn res;\n}\n};\n}\n</code></pre> array_queue.dart<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nlate List&lt;int&gt; _nums; // \u7528\u4e8e\u50a8\u5b58\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nlate int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nlate int _queSize; // \u961f\u5217\u957f\u5ea6\nArrayQueue(int capacity) {\n_nums = List.filled(capacity, 0);\n_front = _queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capaCity() {\nreturn _nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn _queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _queSize == 0;\n}\n/* \u5165\u961f */\nvoid push(int num) {\nif (_queSize == capaCity()) {\nthrow Exception(\"\u961f\u5217\u5df2\u6ee1\");\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nint rear = (_front + _queSize) % capaCity();\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n_nums[rear] = num;\n_queSize++;\n}\n/* \u51fa\u961f */\nint pop() {\nint num = peek();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n_front = (_front + 1) % capaCity();\n_queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek() {\nif (isEmpty()) {\nthrow Exception(\"\u961f\u5217\u4e3a\u7a7a\");\n}\nreturn _nums[_front];\n}\n/* \u8fd4\u56de Array */\nList&lt;int&gt; toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nfinal List&lt;int&gt; res = List.filled(_queSize, 0);\nfor (int i = 0, j = _front; i &lt; _queSize; i++, j++) {\nres[i] = _nums[j % capaCity()];\n}\nreturn res;\n}\n}\n</code></pre> array_queue.rs<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct ArrayQueue {\nnums: Vec&lt;i32&gt;, // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nfront: i32, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nque_size: i32, // \u961f\u5217\u957f\u5ea6\nque_capacity: i32, // \u961f\u5217\u5bb9\u91cf\n}\nimpl ArrayQueue {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new(capacity: i32) -&gt; ArrayQueue {\nArrayQueue {\nnums: vec![0; capacity as usize],\nfront: 0,\nque_size: 0,\nque_capacity: capacity,\n}\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nfn capacity(&amp;self) -&gt; i32 {\nself.que_capacity\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfn size(&amp;self) -&gt; i32 {\nself.que_size\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfn is_empty(&amp;self) -&gt; bool {\nself.que_size == 0\n}\n/* \u5165\u961f */\nfn push(&amp;mut self, num: i32) {\nif self.que_size == self.capacity() {\nprintln!(\"\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nlet rear = (self.front + self.que_size) % self.que_capacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.nums[rear as usize] = num;\nself.que_size += 1;\n}\n/* \u51fa\u961f */\nfn pop(&amp;mut self) -&gt; i32 {\nlet num = self.peek();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nself.front = (self.front + 1) % self.que_capacity;\nself.que_size -= 1;\nnum\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfn peek(&amp;self) -&gt; i32 {\nif self.is_empty() {\npanic!(\"index out of bounds\");\n}\nself.nums[self.front as usize]\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\nfn to_vector(&amp;self) -&gt; Vec&lt;i32&gt; {\nlet cap = self.que_capacity;\nlet mut j = self.front;\nlet mut arr = vec![0; self.que_size as usize];\nfor i in 0..self.que_size {\narr[i as usize] = self.nums[(j % cap) as usize];\nj += 1;\n}\narr\n}\n}\n</code></pre> <p>\u4ee5\u4e0a\u5b9e\u73b0\u7684\u961f\u5217\u4ecd\u7136\u5177\u6709\u5c40\u9650\u6027\uff0c\u5373\u5176\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u7136\u800c\uff0c\u8fd9\u4e2a\u95ee\u9898\u4e0d\u96be\u89e3\u51b3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u66ff\u6362\u4e3a\u52a8\u6001\u6570\u7ec4\uff0c\u4ece\u800c\u5f15\u5165\u6269\u5bb9\u673a\u5236\u3002\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u5c1d\u8bd5\u81ea\u884c\u5b9e\u73b0\u3002</p> <p>\u4e24\u79cd\u5b9e\u73b0\u7684\u5bf9\u6bd4\u7ed3\u8bba\u4e0e\u6808\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002</p>"},{"location":"chapter_stack_and_queue/queue/#523","title":"5.2.3. \u00a0 \u961f\u5217\u5178\u578b\u5e94\u7528","text":"<ul> <li>\u6dd8\u5b9d\u8ba2\u5355\u3002\u8d2d\u7269\u8005\u4e0b\u5355\u540e\uff0c\u8ba2\u5355\u5c06\u52a0\u5165\u961f\u5217\u4e2d\uff0c\u7cfb\u7edf\u968f\u540e\u4f1a\u6839\u636e\u987a\u5e8f\u4f9d\u6b21\u5904\u7406\u961f\u5217\u4e2d\u7684\u8ba2\u5355\u3002\u5728\u53cc\u5341\u4e00\u671f\u95f4\uff0c\u77ed\u65f6\u95f4\u5185\u4f1a\u4ea7\u751f\u6d77\u91cf\u8ba2\u5355\uff0c\u9ad8\u5e76\u53d1\u6210\u4e3a\u5de5\u7a0b\u5e08\u4eec\u9700\u8981\u91cd\u70b9\u653b\u514b\u7684\u95ee\u9898\u3002</li> <li>\u5404\u7c7b\u5f85\u529e\u4e8b\u9879\u3002\u4efb\u4f55\u9700\u8981\u5b9e\u73b0\u201c\u5148\u6765\u540e\u5230\u201d\u529f\u80fd\u7684\u573a\u666f\uff0c\u4f8b\u5982\u6253\u5370\u673a\u7684\u4efb\u52a1\u961f\u5217\u3001\u9910\u5385\u7684\u51fa\u9910\u961f\u5217\u7b49\u3002\u961f\u5217\u5728\u8fd9\u4e9b\u573a\u666f\u4e2d\u53ef\u4ee5\u6709\u6548\u5730\u7ef4\u62a4\u5904\u7406\u987a\u5e8f\u3002</li> </ul>"},{"location":"chapter_stack_and_queue/stack/","title":"5.1. \u00a0 \u6808","text":"<p>\u300c\u6808 Stack\u300d\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u540e\u51fa\uff08First In, Last Out\uff09\u539f\u5219\u7684\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002</p> <p>\u6211\u4eec\u53ef\u4ee5\u5c06\u6808\u7c7b\u6bd4\u4e3a\u684c\u9762\u4e0a\u7684\u4e00\u645e\u76d8\u5b50\uff0c\u5982\u679c\u9700\u8981\u62ff\u51fa\u5e95\u90e8\u7684\u76d8\u5b50\uff0c\u5219\u9700\u8981\u5148\u5c06\u4e0a\u9762\u7684\u76d8\u5b50\u4f9d\u6b21\u53d6\u51fa\u3002\u6211\u4eec\u5c06\u76d8\u5b50\u66ff\u6362\u4e3a\u5404\u79cd\u7c7b\u578b\u7684\u5143\u7d20\uff08\u5982\u6574\u6570\u3001\u5b57\u7b26\u3001\u5bf9\u8c61\u7b49\uff09\uff0c\u5c31\u5f97\u5230\u4e86\u6808\u6570\u636e\u7ed3\u6784\u3002</p> <p>\u5728\u6808\u4e2d\uff0c\u6211\u4eec\u628a\u5806\u53e0\u5143\u7d20\u7684\u9876\u90e8\u79f0\u4e3a\u300c\u6808\u9876\u300d\uff0c\u5e95\u90e8\u79f0\u4e3a\u300c\u6808\u5e95\u300d\u3002\u5c06\u628a\u5143\u7d20\u6dfb\u52a0\u5230\u6808\u9876\u7684\u64cd\u4f5c\u53eb\u505a\u300c\u5165\u6808\u300d\uff0c\u800c\u5220\u9664\u6808\u9876\u5143\u7d20\u7684\u64cd\u4f5c\u53eb\u505a\u300c\u51fa\u6808\u300d\u3002</p> <p></p> <p> \u56fe\uff1a\u6808\u7684\u5148\u5165\u540e\u51fa\u89c4\u5219 </p>"},{"location":"chapter_stack_and_queue/stack/#511","title":"5.1.1. \u00a0 \u6808\u5e38\u7528\u64cd\u4f5c","text":"<p>\u6808\u7684\u5e38\u7528\u64cd\u4f5c\u5982\u4e0b\u8868\u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002\u5728\u6b64\uff0c\u6211\u4eec\u4ee5\u5e38\u89c1\u7684 <code>push()</code> , <code>pop()</code> , <code>peek()</code> \u547d\u540d\u4e3a\u4f8b\u3002</p> <p> \u8868\uff1a\u6808\u7684\u64cd\u4f5c\u6548\u7387 </p> \u65b9\u6cd5 \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 push() \u5143\u7d20\u5165\u6808\uff08\u6dfb\u52a0\u81f3\u6808\u9876\uff09 \\(O(1)\\) pop() \u6808\u9876\u5143\u7d20\u51fa\u6808 \\(O(1)\\) peek() \u8bbf\u95ee\u6808\u9876\u5143\u7d20 \\(O(1)\\) <p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5185\u7f6e\u7684\u6808\u7c7b\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u8bed\u8a00\u53ef\u80fd\u6ca1\u6709\u4e13\u95e8\u63d0\u4f9b\u6808\u7c7b\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u8bed\u8a00\u7684\u300c\u6570\u7ec4\u300d\u6216\u300c\u94fe\u8868\u300d\u89c6\u4f5c\u6808\u6765\u4f7f\u7528\uff0c\u5e76\u901a\u8fc7\u201c\u8111\u8865\u201d\u6765\u5ffd\u7565\u4e0e\u6808\u65e0\u5173\u7684\u64cd\u4f5c\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust stack.java<pre><code>/* \u521d\u59cb\u5316\u6808 */\nStack&lt;Integer&gt; stack = new Stack&lt;&gt;();\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.peek();\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = stack.isEmpty();\n</code></pre> stack.cpp<pre><code>/* \u521d\u59cb\u5316\u6808 */\nstack&lt;int&gt; stack;\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top = stack.top();\n/* \u5143\u7d20\u51fa\u6808 */\nstack.pop(); // \u65e0\u8fd4\u56de\u503c\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool empty = stack.empty();\n</code></pre> stack.py<pre><code># \u521d\u59cb\u5316\u6808\n# Python \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a List \u5f53\u4f5c\u6808\u6765\u4f7f\u7528 \nstack: list[int] = []\n# \u5143\u7d20\u5165\u6808\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n# \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npeek: int = stack[-1]\n# \u5143\u7d20\u51fa\u6808\npop: int = stack.pop()\n# \u83b7\u53d6\u6808\u7684\u957f\u5ea6\nsize: int = len(stack)\n# \u5224\u65ad\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = len(stack) == 0\n</code></pre> stack_test.go<pre><code>/* \u521d\u59cb\u5316\u6808 */\n// \u5728 Go \u4e2d\uff0c\u63a8\u8350\u5c06 Slice \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nvar stack []int\n/* \u5143\u7d20\u5165\u6808 */\nstack = append(stack, 1)\nstack = append(stack, 3)\nstack = append(stack, 2)\nstack = append(stack, 5)\nstack = append(stack, 4)\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek := stack[len(stack)-1]\n/* \u5143\u7d20\u51fa\u6808 */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nsize := len(stack)\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nisEmpty := len(stack) == 0\n</code></pre> stack.js<pre><code>/* \u521d\u59cb\u5316\u6808 */\n// Javascript \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528 \nconst stack = [];\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length-1];\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nconst is_empty = stack.length === 0;\n</code></pre> stack.ts<pre><code>/* \u521d\u59cb\u5316\u6808 */\n// Typescript \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528 \nconst stack: number[] = [];\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length - 1];\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nconst is_empty = stack.length === 0;\n</code></pre> stack.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u6808\n</code></pre> stack.cs<pre><code>/* \u521d\u59cb\u5316\u6808 */\nStack&lt;int&gt; stack = new ();\n/* \u5143\u7d20\u5165\u6808 */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.Peek();\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.Pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.Count;\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.Count == 0;\n</code></pre> stack.swift<pre><code>/* \u521d\u59cb\u5316\u6808 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nvar stack: [Int] = []\n/* \u5143\u7d20\u5165\u6808 */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nlet peek = stack.last!\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.removeLast()\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.count\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = stack.isEmpty\n</code></pre> stack.zig<pre><code>\n</code></pre> stack.dart<pre><code>/* \u521d\u59cb\u5316\u6808 */\n// Dart \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a List \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nList&lt;int&gt; stack = [];\n/* \u5143\u7d20\u5165\u6808 */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.last;\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.removeLast();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.length;\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.isEmpty;\n</code></pre> stack.rs<pre><code>\n</code></pre>"},{"location":"chapter_stack_and_queue/stack/#512","title":"5.1.2. \u00a0 \u6808\u7684\u5b9e\u73b0","text":"<p>\u4e3a\u4e86\u6df1\u5165\u4e86\u89e3\u6808\u7684\u8fd0\u884c\u673a\u5236\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\u6808\u7c7b\u3002</p> <p>\u6808\u9075\u5faa\u5148\u5165\u540e\u51fa\u7684\u539f\u5219\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u80fd\u5728\u6808\u9876\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002\u7136\u800c\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u90fd\u53ef\u4ee5\u5728\u4efb\u610f\u4f4d\u7f6e\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\uff0c\u56e0\u6b64\u6808\u53ef\u4ee5\u88ab\u89c6\u4e3a\u4e00\u79cd\u53d7\u9650\u5236\u7684\u6570\u7ec4\u6216\u94fe\u8868\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u201c\u5c4f\u853d\u201d\u6570\u7ec4\u6216\u94fe\u8868\u7684\u90e8\u5206\u65e0\u5173\u64cd\u4f5c\uff0c\u4f7f\u5176\u5bf9\u5916\u8868\u73b0\u7684\u903b\u8f91\u7b26\u5408\u6808\u7684\u7279\u6027\u3002</p>"},{"location":"chapter_stack_and_queue/stack/#_1","title":"\u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"<p>\u4f7f\u7528\u94fe\u8868\u6765\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u5934\u8282\u70b9\u89c6\u4e3a\u6808\u9876\uff0c\u5c3e\u8282\u70b9\u89c6\u4e3a\u6808\u5e95\u3002</p> <p>\u5bf9\u4e8e\u5165\u6808\u64cd\u4f5c\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u5143\u7d20\u63d2\u5165\u94fe\u8868\u5934\u90e8\uff0c\u8fd9\u79cd\u8282\u70b9\u63d2\u5165\u65b9\u6cd5\u88ab\u79f0\u4e3a\u201c\u5934\u63d2\u6cd5\u201d\u3002\u800c\u5bf9\u4e8e\u51fa\u6808\u64cd\u4f5c\uff0c\u53ea\u9700\u5c06\u5934\u8282\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u5373\u53ef\u3002</p> LinkedListStackpush()pop() <p></p> <p></p> <p></p> <p> \u56fe\uff1a\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u5165\u6808\u51fa\u6808\u64cd\u4f5c </p> <p>\u4ee5\u4e0b\u662f\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u793a\u4f8b\u4ee3\u7801\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust linkedlist_stack.java<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate ListNode stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate int stkSize = 0; // \u6808\u7684\u957f\u5ea6\npublic LinkedListStack() {\nstackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size() {\nreturn stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num) {\nListNode node = new ListNode(num);\nnode.next = stackPeek;\nstackPeek = node;\nstkSize++;\n}\n/* \u51fa\u6808 */\npublic int pop() {\nint num = peek();\nstackPeek = stackPeek.next;\nstkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek() {\nif (size() == 0)\nthrow new IndexOutOfBoundsException();\nreturn stackPeek.val;\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nListNode node = stackPeek;\nint[] res = new int[size()];\nfor (int i = res.length - 1; i &gt;= 0; i--) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_stack.cpp<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate:\nListNode *stackTop; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nint stkSize; // \u6808\u7684\u957f\u5ea6\npublic:\nLinkedListStack() {\nstackTop = nullptr;\nstkSize = 0;\n}\n~LinkedListStack() {\n// \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\nfreeMemoryLinkedList(stackTop);\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size() {\nreturn stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\nvoid push(int num) {\nListNode *node = new ListNode(num);\nnode-&gt;next = stackTop;\nstackTop = node;\nstkSize++;\n}\n/* \u51fa\u6808 */\nvoid pop() {\nint num = top();\nListNode *tmp = stackTop;\nstackTop = stackTop-&gt;next;\n// \u91ca\u653e\u5185\u5b58\ndelete tmp;\nstkSize--;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top() {\nif (size() == 0)\nthrow out_of_range(\"\u6808\u4e3a\u7a7a\");\nreturn stackTop-&gt;val;\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nvector&lt;int&gt; toVector() {\nListNode *node = stackTop;\nvector&lt;int&gt; res(size());\nfor (int i = res.size() - 1; i &gt;= 0; i--) {\nres[i] = node-&gt;val;\nnode = node-&gt;next;\n}\nreturn res;\n}\n};\n</code></pre> linkedlist_stack.py<pre><code>class LinkedListStack:\n\"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__peek: ListNode | None = None\nself.__size: int = 0\ndef size(self) -&gt; int:\n\"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\nreturn self.__size\ndef is_empty(self) -&gt; bool:\n\"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn not self.__peek\ndef push(self, val: int):\n\"\"\"\u5165\u6808\"\"\"\nnode = ListNode(val)\nnode.next = self.__peek\nself.__peek = node\nself.__size += 1\ndef pop(self) -&gt; int:\n\"\"\"\u51fa\u6808\"\"\"\nnum: int = self.peek()\nself.__peek = self.__peek.next\nself.__size -= 1\nreturn num\ndef peek(self) -&gt; int:\n\"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n# \u5224\u7a7a\u5904\u7406\nif not self.__peek:\nreturn None\nreturn self.__peek.val\ndef to_list(self) -&gt; list[int]:\n\"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\narr = []\nnode = self.__peek\nwhile node:\narr.append(node.val)\nnode = node.next\narr.reverse()\nreturn arr\n</code></pre> linkedlist_stack.go<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\ntype linkedListStack struct {\n// \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u6808\ndata *list.List\n}\n/* \u521d\u59cb\u5316\u6808 */\nfunc newLinkedListStack() *linkedListStack {\nreturn &amp;linkedListStack{\ndata: list.New(),\n}\n}\n/* \u5165\u6808 */\nfunc (s *linkedListStack) push(value int) {\ns.data.PushBack(value)\n}\n/* \u51fa\u6808 */\nfunc (s *linkedListStack) pop() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Back()\ns.data.Remove(e)\nreturn e.Value\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc (s *linkedListStack) peek() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Back()\nreturn e.Value\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc (s *linkedListStack) size() int {\nreturn s.data.Len()\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListStack) isEmpty() bool {\nreturn s.data.Len() == 0\n}\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListStack) toList() *list.List {\nreturn s.data\n}\n</code></pre> linkedlist_stack.js<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n#stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n#stkSize = 0; // \u6808\u7684\u957f\u5ea6\nconstructor() {\nthis.#stackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.size === 0;\n}\n/* \u5165\u6808 */\npush(num) {\nconst node = new ListNode(num);\nnode.next = this.#stackPeek;\nthis.#stackPeek = node;\nthis.#stkSize++;\n}\n/* \u51fa\u6808 */\npop() {\nconst num = this.peek();\nthis.#stackPeek = this.#stackPeek.next;\nthis.#stkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek() {\nif (!this.#stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.#stackPeek.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray() {\nlet node = this.#stackPeek;\nconst res = new Array(this.size);\nfor (let i = res.length - 1; i &gt;= 0; i--) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_stack.ts<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate stackPeek: ListNode | null; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate stkSize: number = 0; // \u6808\u7684\u957f\u5ea6\nconstructor() {\nthis.stackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.size === 0;\n}\n/* \u5165\u6808 */\npush(num: number): void {\nconst node = new ListNode(num);\nnode.next = this.stackPeek;\nthis.stackPeek = node;\nthis.stkSize++;\n}\n/* \u51fa\u6808 */\npop(): number {\nconst num = this.peek();\nif (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\nthis.stackPeek = this.stackPeek.next;\nthis.stkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek(): number {\nif (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.stackPeek.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray(): number[] {\nlet node = this.stackPeek;\nconst res = new Array&lt;number&gt;(this.size);\nfor (let i = res.length - 1; i &gt;= 0; i--) {\nres[i] = node!.val;\nnode = node!.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_stack.c<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nstruct linkedListStack {\nListNode *top; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nint size; // \u6808\u7684\u957f\u5ea6\n};\ntypedef struct linkedListStack linkedListStack;\n/* \u6784\u9020\u51fd\u6570 */\nlinkedListStack *newLinkedListStack() {\nlinkedListStack *s = malloc(sizeof(linkedListStack));\ns-&gt;top = NULL;\ns-&gt;size = 0;\nreturn s;\n}\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListStack(linkedListStack *s) {\nwhile (s-&gt;top) {\nListNode *n = s-&gt;top-&gt;next;\nfree(s-&gt;top);\ns-&gt;top = n;\n}\nfree(s);\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(linkedListStack *s) {\nassert(s);\nreturn s-&gt;size;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(linkedListStack *s) {\nassert(s);\nreturn size(s) == 0;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(linkedListStack *s) {\nassert(s);\nassert(size(s) != 0);\nreturn s-&gt;top-&gt;val;\n}\n/* \u5165\u6808 */\nvoid push(linkedListStack *s, int num) {\nassert(s);\nListNode *node = (ListNode *)malloc(sizeof(ListNode));\nnode-&gt;next = s-&gt;top; // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6307\u9488\u57df\nnode-&gt;val = num; // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6570\u636e\u57df\ns-&gt;top = node; // \u66f4\u65b0\u6808\u9876\ns-&gt;size++; // \u66f4\u65b0\u6808\u5927\u5c0f\n}\n/* \u51fa\u6808 */\nint pop(linkedListStack *s) {\nif (s-&gt;size == 0) {\nprintf(\"stack is empty.\\n\");\nreturn INT_MAX;\n}\nassert(s);\nint val = peek(s);\nListNode *tmp = s-&gt;top;\ns-&gt;top = s-&gt;top-&gt;next;\n// \u91ca\u653e\u5185\u5b58\nfree(tmp);\ns-&gt;size--;\nreturn val;\n}\n</code></pre> linkedlist_stack.cs<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate ListNode? stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate int stkSize = 0; // \u6808\u7684\u957f\u5ea6\npublic LinkedListStack() {\nstackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size() {\nreturn stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num) {\nListNode node = new ListNode(num);\nnode.next = stackPeek;\nstackPeek = node;\nstkSize++;\n}\n/* \u51fa\u6808 */\npublic int pop() {\nif (stackPeek == null)\nthrow new Exception();\nint num = peek();\nstackPeek = stackPeek.next;\nstkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek() {\nif (size() == 0 || stackPeek == null)\nthrow new Exception();\nreturn stackPeek.val;\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nif (stackPeek == null)\nreturn Array.Empty&lt;int&gt;();\nListNode node = stackPeek;\nint[] res = new int[size()];\nfor (int i = res.Length - 1; i &gt;= 0; i--) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_stack.swift<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate var _peek: ListNode? // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate var _size = 0 // \u6808\u7684\u957f\u5ea6\ninit() {}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc size() -&gt; Int {\n_size\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -&gt; Bool {\nsize() == 0\n}\n/* \u5165\u6808 */\nfunc push(num: Int) {\nlet node = ListNode(x: num)\nnode.next = _peek\n_peek = node\n_size += 1\n}\n/* \u51fa\u6808 */\n@discardableResult\nfunc pop() -&gt; Int {\nlet num = peek()\n_peek = _peek?.next\n_size -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc peek() -&gt; Int {\nif isEmpty() {\nfatalError(\"\u6808\u4e3a\u7a7a\")\n}\nreturn _peek!.val\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nfunc toArray() -&gt; [Int] {\nvar node = _peek\nvar res = Array(repeating: 0, count: _size)\nfor i in sequence(first: res.count - 1, next: { $0 &gt;= 0 + 1 ? $0 - 1 : nil }) {\nres[i] = node!.val\nnode = node?.next\n}\nreturn res\n}\n}\n</code></pre> linkedlist_stack.zig<pre><code>// \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\nfn LinkedListStack(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nstack_top: ?*inc.ListNode(T) = null, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nstk_size: usize = 0, // \u6808\u7684\u957f\u5ea6\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.stack_top = null;\nself.stk_size = 0;\n}\n// \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u6808\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.stk_size;\n}\n// \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.size() == 0) @panic(\"\u6808\u4e3a\u7a7a\");\nreturn self.stack_top.?.val;\n} // \u5165\u6808\npub fn push(self: *Self, num: T) !void {\nvar node = try self.mem_allocator.create(inc.ListNode(T));\nnode.init(num);\nnode.next = self.stack_top;\nself.stack_top = node;\nself.stk_size += 1;\n} // \u51fa\u6808\npub fn pop(self: *Self) T {\nvar num = self.peek();\nself.stack_top = self.stack_top.?.next;\nself.stk_size -= 1;\nreturn num;\n} // \u5c06\u6808\u8f6c\u6362\u4e3a\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\nvar node = self.stack_top;\nvar res = try self.mem_allocator.alloc(T, self.size());\n@memset(res, @as(T, 0));\nvar i: usize = 0;\nwhile (i &lt; res.len) : (i += 1) {\nres[res.len - i - 1] = node.?.val;\nnode = node.?.next;\n}\nreturn res;\n}\n};\n}\n</code></pre> linkedlist_stack.dart<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u7c7b\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nListNode? _stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nint _stkSize = 0; // \u6808\u7684\u957f\u5ea6\nLinkedListStack() {\n_stackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size() {\nreturn _stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _stkSize == 0;\n}\n/* \u5165\u6808 */\nvoid push(int num) {\nfinal ListNode node = ListNode(num);\nnode.next = _stackPeek;\n_stackPeek = node;\n_stkSize++;\n}\n/* \u51fa\u6808 */\nint pop() {\nfinal int num = peek();\n_stackPeek = _stackPeek!.next;\n_stkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek() {\nif (_stackPeek == null) {\nthrow Exception(\"\u6808\u4e3a\u7a7a\");\n}\nreturn _stackPeek!.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a List \u5e76\u8fd4\u56de */\nList&lt;int&gt; toList() {\nListNode? node = _stackPeek;\nList&lt;int&gt; list = [];\nwhile (node != null) {\nlist.add(node.val);\nnode = node.next;\n}\nlist = list.reversed.toList();\nreturn list;\n}\n}\n</code></pre> linkedlist_stack.rs<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\n#[allow(dead_code)]\npub struct LinkedListStack&lt;T&gt; {\nstack_peek: Option&lt;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt;, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\nstk_size: usize, // \u6808\u7684\u957f\u5ea6\n}\nimpl&lt;T: Copy&gt; LinkedListStack&lt;T&gt; {\npub fn new() -&gt; Self {\nSelf {\nstack_peek: None,\nstk_size: 0,\n}\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npub fn size(&amp;self) -&gt; usize {\nreturn self.stk_size;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npub fn is_empty(&amp;self) -&gt; bool {\nreturn self.size() == 0;\n}\n/* \u5165\u6808 */\npub fn push(&amp;mut self, num: T) {\nlet node = ListNode::new(num);\nnode.borrow_mut().next = self.stack_peek.take();\nself.stack_peek = Some(node);\nself.stk_size += 1;\n}\n/* \u51fa\u6808 */\npub fn pop(&amp;mut self) -&gt; Option&lt;T&gt; {\nself.stack_peek.take().map(|old_head| {\nmatch old_head.borrow_mut().next.take() {\nSome(new_head) =&gt; {\nself.stack_peek = Some(new_head);\n}\nNone =&gt; {\nself.stack_peek = None;\n}\n}\nself.stk_size -= 1;\nRc::try_unwrap(old_head).ok().unwrap().into_inner().val\n})\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npub fn peek(&amp;self) -&gt; Option&lt;&amp;Rc&lt;RefCell&lt;ListNode&lt;T&gt;&gt;&gt;&gt; {\nself.stack_peek.as_ref()\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npub 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; {\nif let Some(node) = head {\nlet mut nums = self.to_array(node.borrow().next.as_ref());\nnums.push(node.borrow().val);\nreturn nums;\n}\nreturn Vec::new();\n}\n}\n</code></pre>"},{"location":"chapter_stack_and_queue/stack/#_2","title":"\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"<p>\u5728\u57fa\u4e8e\u300c\u6570\u7ec4\u300d\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u5c3e\u90e8\u4f5c\u4e3a\u6808\u9876\u3002\u5728\u8fd9\u6837\u7684\u8bbe\u8ba1\u4e0b\uff0c\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u5c31\u5206\u522b\u5bf9\u5e94\u5728\u6570\u7ec4\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u4e0e\u5220\u9664\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(1)\\) \u3002</p> ArrayStackpush()pop() <p></p> <p></p> <p></p> <p> \u56fe\uff1a\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u6808\u7684\u5165\u6808\u51fa\u6808\u64cd\u4f5c </p> <p>\u7531\u4e8e\u5165\u6808\u7684\u5143\u7d20\u53ef\u80fd\u4f1a\u6e90\u6e90\u4e0d\u65ad\u5730\u589e\u52a0\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u52a8\u6001\u6570\u7ec4\uff0c\u8fd9\u6837\u5c31\u65e0\u9700\u81ea\u884c\u5904\u7406\u6570\u7ec4\u6269\u5bb9\u95ee\u9898\u3002\u4ee5\u4e0b\u4e3a\u793a\u4f8b\u4ee3\u7801\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust array_stack.java<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate ArrayList&lt;Integer&gt; stack;\npublic ArrayStack() {\n// \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\nstack = new ArrayList&lt;&gt;();\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size() {\nreturn stack.size();\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num) {\nstack.add(num);\n}\n/* \u51fa\u6808 */\npublic int pop() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn stack.remove(size() - 1);\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek() {\nif (isEmpty())\nthrow new IndexOutOfBoundsException();\nreturn stack.get(size() - 1);\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic Object[] toArray() {\nreturn stack.toArray();\n}\n}\n</code></pre> array_stack.cpp<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate:\nvector&lt;int&gt; stack;\npublic:\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size() {\nreturn stack.size();\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn stack.empty();\n}\n/* \u5165\u6808 */\nvoid push(int num) {\nstack.push_back(num);\n}\n/* \u51fa\u6808 */\nvoid pop() {\nint oldTop = top();\nstack.pop_back();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top() {\nif (empty())\nthrow out_of_range(\"\u6808\u4e3a\u7a7a\");\nreturn stack.back();\n}\n/* \u8fd4\u56de Vector */\nvector&lt;int&gt; toVector() {\nreturn stack;\n}\n};\n</code></pre> array_stack.py<pre><code>class ArrayStack:\n\"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\"\"\"\ndef __init__(self):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__stack: list[int] = []\ndef size(self) -&gt; int:\n\"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\nreturn len(self.__stack)\ndef is_empty(self) -&gt; bool:\n\"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\nreturn self.__stack == []\ndef push(self, item: int):\n\"\"\"\u5165\u6808\"\"\"\nself.__stack.append(item)\ndef pop(self) -&gt; int:\n\"\"\"\u51fa\u6808\"\"\"\nif self.is_empty():\nraise IndexError(\"\u6808\u4e3a\u7a7a\")\nreturn self.__stack.pop()\ndef peek(self) -&gt; int:\n\"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\nif self.is_empty():\nraise IndexError(\"\u6808\u4e3a\u7a7a\")\nreturn self.__stack[-1]\ndef to_list(self) -&gt; list[int]:\n\"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\nreturn self.__stack\n</code></pre> array_stack.go<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntype arrayStack struct {\ndata []int // \u6570\u636e\n}\n/* \u521d\u59cb\u5316\u6808 */\nfunc newArrayStack() *arrayStack {\nreturn &amp;arrayStack{\n// \u8bbe\u7f6e\u6808\u7684\u957f\u5ea6\u4e3a 0\uff0c\u5bb9\u91cf\u4e3a 16\ndata: make([]int, 0, 16),\n}\n}\n/* \u6808\u7684\u957f\u5ea6 */\nfunc (s *arrayStack) size() int {\nreturn len(s.data)\n}\n/* \u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *arrayStack) isEmpty() bool {\nreturn s.size() == 0\n}\n/* \u5165\u6808 */\nfunc (s *arrayStack) push(v int) {\n// \u5207\u7247\u4f1a\u81ea\u52a8\u6269\u5bb9\ns.data = append(s.data, v)\n}\n/* \u51fa\u6808 */\nfunc (s *arrayStack) pop() any {\nval := s.peek()\ns.data = s.data[:len(s.data)-1]\nreturn val\n}\n/* \u83b7\u53d6\u6808\u9876\u5143\u7d20 */\nfunc (s *arrayStack) peek() any {\nif s.isEmpty() {\nreturn nil\n}\nval := s.data[len(s.data)-1]\nreturn val\n}\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (s *arrayStack) toSlice() []int {\nreturn s.data\n}\n</code></pre> array_stack.js<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n#stack;\nconstructor() {\nthis.#stack = [];\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#stack.length;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nempty() {\nreturn this.#stack.length === 0;\n}\n/* \u5165\u6808 */\npush(num) {\nthis.#stack.push(num);\n}\n/* \u51fa\u6808 */\npop() {\nif (this.empty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.#stack.pop();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\ntop() {\nif (this.empty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.#stack[this.#stack.length - 1];\n}\n/* \u8fd4\u56de Array */\ntoArray() {\nreturn this.#stack;\n}\n}\n</code></pre> array_stack.ts<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate stack: number[];\nconstructor() {\nthis.stack = [];\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.stack.length;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nempty(): boolean {\nreturn this.stack.length === 0;\n}\n/* \u5165\u6808 */\npush(num: number): void {\nthis.stack.push(num);\n}\n/* \u51fa\u6808 */\npop(): number | undefined {\nif (this.empty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.stack.pop();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\ntop(): number | undefined {\nif (this.empty()) throw new Error('\u6808\u4e3a\u7a7a');\nreturn this.stack[this.stack.length - 1];\n}\n/* \u8fd4\u56de Array */\ntoArray() {\nreturn this.stack;\n}\n}\n</code></pre> array_stack.c<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nstruct arrayStack {\nint *data;\nint size;\n};\ntypedef struct arrayStack arrayStack;\n/* \u6784\u9020\u51fd\u6570 */\narrayStack *newArrayStack() {\narrayStack *s = malloc(sizeof(arrayStack));\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5927\u5bb9\u91cf\uff0c\u907f\u514d\u6269\u5bb9\ns-&gt;data = malloc(sizeof(int) * MAX_SIZE);\ns-&gt;size = 0;\nreturn s;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(arrayStack *s) {\nreturn s-&gt;size;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(arrayStack *s) {\nreturn s-&gt;size == 0;\n}\n/* \u5165\u6808 */\nvoid push(arrayStack *s, int num) {\nif (s-&gt;size == MAX_SIZE) {\nprintf(\"stack is full.\\n\");\nreturn;\n}\ns-&gt;data[s-&gt;size] = num;\ns-&gt;size++;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(arrayStack *s) {\nif (s-&gt;size == 0) {\nprintf(\"stack is empty.\\n\");\nreturn INT_MAX;\n}\nreturn s-&gt;data[s-&gt;size - 1];\n}\n/* \u51fa\u6808 */\nint pop(arrayStack *s) {\nif (s-&gt;size == 0) {\nprintf(\"stack is empty.\\n\");\nreturn INT_MAX;\n}\nint val = peek(s);\ns-&gt;size--;\nreturn val;\n}\n</code></pre> array_stack.cs<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate List&lt;int&gt; stack;\npublic ArrayStack() {\n// \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\nstack = new();\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size() {\nreturn stack.Count();\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num) {\nstack.Add(num);\n}\n/* \u51fa\u6808 */\npublic int pop() {\nif (isEmpty())\nthrow new Exception();\nvar val = peek();\nstack.RemoveAt(size() - 1);\nreturn val;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek() {\nif (isEmpty())\nthrow new Exception();\nreturn stack[size() - 1];\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nreturn stack.ToArray();\n}\n}\n</code></pre> array_stack.swift<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate var stack: [Int]\ninit() {\n// \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\nstack = []\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc size() -&gt; Int {\nstack.count\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -&gt; Bool {\nstack.isEmpty\n}\n/* \u5165\u6808 */\nfunc push(num: Int) {\nstack.append(num)\n}\n/* \u51fa\u6808 */\n@discardableResult\nfunc pop() -&gt; Int {\nif isEmpty() {\nfatalError(\"\u6808\u4e3a\u7a7a\")\n}\nreturn stack.removeLast()\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc peek() -&gt; Int {\nif isEmpty() {\nfatalError(\"\u6808\u4e3a\u7a7a\")\n}\nreturn stack.last!\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nfunc toArray() -&gt; [Int] {\nstack\n}\n}\n</code></pre> array_stack.zig<pre><code>// \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\nfn ArrayStack(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nstack: ?std.ArrayList(T) = null, // \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) void {\nif (self.stack == null) {\nself.stack = std.ArrayList(T).init(allocator);\n}\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.stack == null) return;\nself.stack.?.deinit();\n}\n// \u83b7\u53d6\u6808\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.stack.?.items.len;\n}\n// \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.isEmpty()) @panic(\"\u6808\u4e3a\u7a7a\");\nreturn self.stack.?.items[self.size() - 1];\n} // \u5165\u6808\npub fn push(self: *Self, num: T) !void {\ntry self.stack.?.append(num);\n} // \u51fa\u6808\npub fn pop(self: *Self) T {\nvar num = self.stack.?.pop();\nreturn num;\n} // \u8fd4\u56de ArrayList\npub fn toList(self: *Self) std.ArrayList(T) {\nreturn self.stack.?;\n}\n};\n}\n</code></pre> array_stack.dart<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nlate List&lt;int&gt; _stack;\nArrayStack() {\n_stack = [];\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size() {\nreturn _stack.length;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty() {\nreturn _stack.isEmpty;\n}\n/* \u5165\u6808 */\nvoid push(int num) {\n_stack.add(num);\n}\n/* \u51fa\u6808 */\nint pop() {\nif (isEmpty()) {\nthrow Exception(\"\u6808\u4e3a\u7a7a\");\n}\nreturn _stack.removeLast();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek() {\nif (isEmpty()) {\nthrow Exception(\"\u6808\u4e3a\u7a7a\");\n}\nreturn _stack.last;\n}\n/* \u5c06\u6808\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nList&lt;int&gt; toArray() =&gt; _stack;\n}\n</code></pre> array_stack.rs<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nstruct ArrayStack&lt;T&gt; {\nstack: Vec&lt;T&gt;,\n}\nimpl&lt;T&gt; ArrayStack&lt;T&gt; {\n/* \u521d\u59cb\u5316\u6808 */\nfn new() -&gt; ArrayStack&lt;T&gt; {\nArrayStack::&lt;T&gt; { stack: Vec::&lt;T&gt;::new() }\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfn size(&amp;self) -&gt; usize {\nself.stack.len()\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfn is_empty(&amp;self) -&gt; bool {\nself.size() == 0\n}\n/* \u5165\u6808 */\nfn push(&amp;mut self, num: T) {\nself.stack.push(num);\n}\n/* \u51fa\u6808 */\nfn pop(&amp;mut self) -&gt; Option&lt;T&gt; {\nmatch self.stack.pop() {\nSome(num) =&gt; Some(num),\nNone =&gt; None,\n}\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfn peek(&amp;self) -&gt; Option&lt;&amp;T&gt; {\nif self.is_empty() { panic!(\"\u6808\u4e3a\u7a7a\") };\nself.stack.last()\n}\n/* \u8fd4\u56de &amp;Vec */\nfn to_array(&amp;self) -&gt; &amp;Vec&lt;T&gt; {\n&amp;self.stack\n}\n}\n</code></pre>"},{"location":"chapter_stack_and_queue/stack/#513","title":"5.1.3. \u00a0 \u4e24\u79cd\u5b9e\u73b0\u5bf9\u6bd4","text":""},{"location":"chapter_stack_and_queue/stack/#_3","title":"\u652f\u6301\u64cd\u4f5c","text":"<p>\u4e24\u79cd\u5b9e\u73b0\u90fd\u652f\u6301\u6808\u5b9a\u4e49\u4e2d\u7684\u5404\u9879\u64cd\u4f5c\u3002\u6570\u7ec4\u5b9e\u73b0\u989d\u5916\u652f\u6301\u968f\u673a\u8bbf\u95ee\uff0c\u4f46\u8fd9\u5df2\u8d85\u51fa\u4e86\u6808\u7684\u5b9a\u4e49\u8303\u7574\uff0c\u56e0\u6b64\u4e00\u822c\u4e0d\u4f1a\u7528\u5230\u3002</p>"},{"location":"chapter_stack_and_queue/stack/#_4","title":"\u65f6\u95f4\u6548\u7387","text":"<p>\u5728\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0\u4e2d\uff0c\u5165\u6808\u548c\u51fa\u6808\u64cd\u4f5c\u90fd\u662f\u5728\u9884\u5148\u5206\u914d\u597d\u7684\u8fde\u7eed\u5185\u5b58\u4e2d\u8fdb\u884c\uff0c\u5177\u6709\u5f88\u597d\u7684\u7f13\u5b58\u672c\u5730\u6027\uff0c\u56e0\u6b64\u6548\u7387\u8f83\u9ad8\u3002\u7136\u800c\uff0c\u5982\u679c\u5165\u6808\u65f6\u8d85\u51fa\u6570\u7ec4\u5bb9\u91cf\uff0c\u4f1a\u89e6\u53d1\u6269\u5bb9\u673a\u5236\uff0c\u5bfc\u81f4\u8be5\u6b21\u5165\u6808\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53d8\u4e3a \\(O(n)\\) \u3002</p> <p>\u5728\u94fe\u8868\u5b9e\u73b0\u4e2d\uff0c\u94fe\u8868\u7684\u6269\u5bb9\u975e\u5e38\u7075\u6d3b\uff0c\u4e0d\u5b58\u5728\u4e0a\u8ff0\u6570\u7ec4\u6269\u5bb9\u65f6\u6548\u7387\u964d\u4f4e\u7684\u95ee\u9898\u3002\u4f46\u662f\uff0c\u5165\u6808\u64cd\u4f5c\u9700\u8981\u521d\u59cb\u5316\u8282\u70b9\u5bf9\u8c61\u5e76\u4fee\u6539\u6307\u9488\uff0c\u56e0\u6b64\u6548\u7387\u76f8\u5bf9\u8f83\u4f4e\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u5165\u6808\u5143\u7d20\u672c\u8eab\u5c31\u662f\u8282\u70b9\u5bf9\u8c61\uff0c\u90a3\u4e48\u53ef\u4ee5\u7701\u53bb\u521d\u59cb\u5316\u6b65\u9aa4\uff0c\u4ece\u800c\u63d0\u9ad8\u6548\u7387\u3002</p> <p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u5f53\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u7684\u5143\u7d20\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u5982 <code>int</code> , <code>double</code> \uff09\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\uff1a</p> <ul> <li>\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\u5728\u89e6\u53d1\u6269\u5bb9\u65f6\u6548\u7387\u4f1a\u964d\u4f4e\uff0c\u4f46\u7531\u4e8e\u6269\u5bb9\u662f\u4f4e\u9891\u64cd\u4f5c\uff0c\u56e0\u6b64\u5e73\u5747\u6548\u7387\u66f4\u9ad8\u3002</li> <li>\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u53ef\u4ee5\u63d0\u4f9b\u66f4\u52a0\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\u3002</li> </ul>"},{"location":"chapter_stack_and_queue/stack/#_5","title":"\u7a7a\u95f4\u6548\u7387","text":"<p>\u5728\u521d\u59cb\u5316\u5217\u8868\u65f6\uff0c\u7cfb\u7edf\u4f1a\u4e3a\u5217\u8868\u5206\u914d\u201c\u521d\u59cb\u5bb9\u91cf\u201d\uff0c\u8be5\u5bb9\u91cf\u53ef\u80fd\u8d85\u8fc7\u5b9e\u9645\u9700\u6c42\u3002\u5e76\u4e14\uff0c\u6269\u5bb9\u673a\u5236\u901a\u5e38\u662f\u6309\u7167\u7279\u5b9a\u500d\u7387\uff08\u4f8b\u5982 2 \u500d\uff09\u8fdb\u884c\u6269\u5bb9\uff0c\u6269\u5bb9\u540e\u7684\u5bb9\u91cf\u4e5f\u53ef\u80fd\u8d85\u51fa\u5b9e\u9645\u9700\u6c42\u3002\u56e0\u6b64\uff0c\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\u53ef\u80fd\u9020\u6210\u4e00\u5b9a\u7684\u7a7a\u95f4\u6d6a\u8d39\u3002</p> <p>\u7136\u800c\uff0c\u7531\u4e8e\u94fe\u8868\u8282\u70b9\u9700\u8981\u989d\u5916\u5b58\u50a8\u6307\u9488\uff0c\u56e0\u6b64\u94fe\u8868\u8282\u70b9\u5360\u7528\u7684\u7a7a\u95f4\u76f8\u5bf9\u8f83\u5927\u3002</p> <p>\u7efc\u4e0a\uff0c\u6211\u4eec\u4e0d\u80fd\u7b80\u5355\u5730\u786e\u5b9a\u54ea\u79cd\u5b9e\u73b0\u66f4\u52a0\u8282\u7701\u5185\u5b58\uff0c\u9700\u8981\u9488\u5bf9\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u5206\u6790\u3002</p>"},{"location":"chapter_stack_and_queue/stack/#514","title":"5.1.4. \u00a0 \u6808\u5178\u578b\u5e94\u7528","text":"<ul> <li>\u6d4f\u89c8\u5668\u4e2d\u7684\u540e\u9000\u4e0e\u524d\u8fdb\u3001\u8f6f\u4ef6\u4e2d\u7684\u64a4\u9500\u4e0e\u53cd\u64a4\u9500\u3002\u6bcf\u5f53\u6211\u4eec\u6253\u5f00\u65b0\u7684\u7f51\u9875\uff0c\u6d4f\u89c8\u5668\u5c31\u4f1a\u5c06\u4e0a\u4e00\u4e2a\u7f51\u9875\u6267\u884c\u5165\u6808\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u300c\u540e\u9000\u300d\u64cd\u4f5c\u56de\u5230\u4e0a\u4e00\u9875\u9762\u3002\u540e\u9000\u64cd\u4f5c\u5b9e\u9645\u4e0a\u662f\u5728\u6267\u884c\u51fa\u6808\u3002\u5982\u679c\u8981\u540c\u65f6\u652f\u6301\u540e\u9000\u548c\u524d\u8fdb\uff0c\u90a3\u4e48\u9700\u8981\u4e24\u4e2a\u6808\u6765\u914d\u5408\u5b9e\u73b0\u3002</li> <li>\u7a0b\u5e8f\u5185\u5b58\u7ba1\u7406\u3002\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u7cfb\u7edf\u90fd\u4f1a\u5728\u6808\u9876\u6dfb\u52a0\u4e00\u4e2a\u6808\u5e27\uff0c\u7528\u4e8e\u8bb0\u5f55\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u5411\u4e0b\u9012\u63a8\u9636\u6bb5\u4f1a\u4e0d\u65ad\u6267\u884c\u5165\u6808\u64cd\u4f5c\uff0c\u800c\u5411\u4e0a\u56de\u6eaf\u9636\u6bb5\u5219\u4f1a\u6267\u884c\u51fa\u6808\u64cd\u4f5c\u3002</li> </ul>"},{"location":"chapter_stack_and_queue/summary/","title":"5.4. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u6808\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u540e\u51fa\u539f\u5219\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u901a\u8fc7\u6570\u7ec4\u6216\u94fe\u8868\u6765\u5b9e\u73b0\u3002</li> <li>\u4ece\u65f6\u95f4\u6548\u7387\u89d2\u5ea6\u770b\uff0c\u6808\u7684\u6570\u7ec4\u5b9e\u73b0\u5177\u6709\u8f83\u9ad8\u7684\u5e73\u5747\u6548\u7387\uff0c\u4f46\u5728\u6269\u5bb9\u8fc7\u7a0b\u4e2d\uff0c\u5355\u6b21\u5165\u6808\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u964d\u4f4e\u81f3 \\(O(n)\\) \u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u5177\u6709\u66f4\u4e3a\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\u3002</li> <li>\u5728\u7a7a\u95f4\u6548\u7387\u65b9\u9762\uff0c\u6808\u7684\u6570\u7ec4\u5b9e\u73b0\u53ef\u80fd\u5bfc\u81f4\u4e00\u5b9a\u7a0b\u5ea6\u7684\u7a7a\u95f4\u6d6a\u8d39\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u94fe\u8868\u8282\u70b9\u6240\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u6bd4\u6570\u7ec4\u5143\u7d20\u66f4\u5927\u3002</li> <li>\u961f\u5217\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u5148\u51fa\u539f\u5219\u7684\u6570\u636e\u7ed3\u6784\uff0c\u540c\u6837\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u6216\u94fe\u8868\u6765\u5b9e\u73b0\u3002\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u7684\u5bf9\u6bd4\u4e0a\uff0c\u961f\u5217\u7684\u7ed3\u8bba\u4e0e\u524d\u8ff0\u6808\u7684\u7ed3\u8bba\u76f8\u4f3c\u3002</li> <li>\u53cc\u5411\u961f\u5217\u662f\u4e00\u79cd\u5177\u6709\u66f4\u9ad8\u81ea\u7531\u5ea6\u7684\u961f\u5217\uff0c\u5b83\u5141\u8bb8\u5728\u4e24\u7aef\u8fdb\u884c\u5143\u7d20\u7684\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\u3002</li> </ul>"},{"location":"chapter_stack_and_queue/summary/#541-q-a","title":"5.4.1. \u00a0 Q &amp; A","text":"<p>\u6d4f\u89c8\u5668\u7684\u524d\u8fdb\u540e\u9000\u662f\u5426\u662f\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\uff1f</p> <p>\u6d4f\u89c8\u5668\u7684\u524d\u8fdb\u540e\u9000\u529f\u80fd\u672c\u8d28\u4e0a\u662f\u201c\u6808\u201d\u7684\u4f53\u73b0\u3002\u5f53\u7528\u6237\u8bbf\u95ee\u4e00\u4e2a\u65b0\u9875\u9762\u65f6\uff0c\u8be5\u9875\u9762\u4f1a\u88ab\u6dfb\u52a0\u5230\u6808\u9876\uff1b\u5f53\u7528\u6237\u70b9\u51fb\u540e\u9000\u6309\u94ae\u65f6\uff0c\u8be5\u9875\u9762\u4f1a\u4ece\u6808\u9876\u5f39\u51fa\u3002\u4f7f\u7528\u53cc\u5411\u961f\u5217\u53ef\u4ee5\u65b9\u4fbf\u5b9e\u73b0\u4e00\u4e9b\u989d\u5916\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u5728\u53cc\u5411\u961f\u5217\u7ae0\u8282\u6709\u63d0\u5230\u3002</p> <p>\u5728\u51fa\u6808\u540e\uff0c\u662f\u5426\u9700\u8981\u91ca\u653e\u51fa\u6808\u8282\u70b9\u7684\u5185\u5b58\uff1f</p> <p>\u5982\u679c\u540e\u7eed\u4ecd\u9700\u8981\u4f7f\u7528\u5f39\u51fa\u8282\u70b9\uff0c\u5219\u4e0d\u9700\u8981\u91ca\u653e\u5185\u5b58\u3002\u82e5\u4e4b\u540e\u4e0d\u9700\u8981\u7528\u5230\uff0c<code>Java</code> \u548c <code>Python</code> \u7b49\u8bed\u8a00\u62e5\u6709\u81ea\u52a8\u5783\u573e\u56de\u6536\u673a\u5236\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u624b\u52a8\u91ca\u653e\u5185\u5b58\uff1b\u5728 <code>C</code> \u548c <code>C++</code> \u4e2d\u9700\u8981\u624b\u52a8\u91ca\u653e\u5185\u5b58\u3002</p> <p>\u53cc\u5411\u961f\u5217\u50cf\u662f\u4e24\u4e2a\u6808\u62fc\u63a5\u5728\u4e86\u4e00\u8d77\uff0c\u5b83\u7684\u7528\u9014\u662f\u4ec0\u4e48\uff1f</p> <p>\u53cc\u5411\u961f\u5217\u5c31\u50cf\u662f\u6808\u548c\u961f\u5217\u7684\u7ec4\u5408\uff0c\u6216\u8005\u662f\u4e24\u4e2a\u6808\u62fc\u5728\u4e86\u4e00\u8d77\u3002\u5b83\u8868\u73b0\u7684\u662f\u6808 + \u961f\u5217\u7684\u903b\u8f91\uff0c\u56e0\u6b64\u53ef\u4ee5\u5b9e\u73b0\u6808\u4e0e\u961f\u5217\u7684\u6240\u6709\u5e94\u7528\uff0c\u5e76\u4e14\u66f4\u52a0\u7075\u6d3b\u3002</p>"},{"location":"chapter_tree/","title":"7. \u00a0 \u6811","text":"<p>Abstract</p> <p>\u53c2\u5929\u5927\u6811\u5145\u6ee1\u751f\u547d\u529b\uff0c\u5176\u6839\u6df1\u53f6\u8302\uff0c\u5206\u679d\u6276\u758f\u3002</p> <p>\u5b83\u4e3a\u6211\u4eec\u5c55\u73b0\u4e86\u6570\u636e\u5206\u6cbb\u7684\u751f\u52a8\u5f62\u6001\u3002</p>"},{"location":"chapter_tree/#_1","title":"\u672c\u7ae0\u5185\u5bb9","text":"<ul> <li>7.1 \u00a0 \u4e8c\u53c9\u6811</li> <li>7.2 \u00a0 \u4e8c\u53c9\u6811\u904d\u5386</li> <li>7.3 \u00a0 \u4e8c\u53c9\u6811\u6570\u7ec4\u8868\u793a</li> <li>7.4 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811</li> <li>7.5 \u00a0 AVL \u6811 *</li> <li>7.6 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_tree/array_representation_of_tree/","title":"7.3. \u00a0 \u4e8c\u53c9\u6811\u6570\u7ec4\u8868\u793a","text":"<p>\u5728\u94fe\u8868\u8868\u793a\u4e0b\uff0c\u4e8c\u53c9\u6811\u7684\u5b58\u50a8\u5355\u5143\u4e3a\u8282\u70b9 <code>TreeNode</code> \uff0c\u8282\u70b9\u4e4b\u95f4\u901a\u8fc7\u6307\u9488\u76f8\u8fde\u63a5\u3002\u5728\u4e0a\u8282\u4e2d\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5728\u94fe\u8868\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7684\u5404\u9879\u57fa\u672c\u64cd\u4f5c\u3002</p> <p>\u90a3\u4e48\uff0c\u80fd\u5426\u7528\u300c\u6570\u7ec4\u300d\u6765\u8868\u793a\u4e8c\u53c9\u6811\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002</p>"},{"location":"chapter_tree/array_representation_of_tree/#731","title":"7.3.1. \u00a0 \u8868\u793a\u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"<p>\u5148\u5206\u6790\u4e00\u4e2a\u7b80\u5355\u6848\u4f8b\u3002\u7ed9\u5b9a\u4e00\u4e2a\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u6211\u4eec\u5c06\u6240\u6709\u8282\u70b9\u6309\u7167\u5c42\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u5219\u6bcf\u4e2a\u8282\u70b9\u90fd\u5bf9\u5e94\u552f\u4e00\u7684\u6570\u7ec4\u7d22\u5f15\u3002</p> <p>\u6839\u636e\u5c42\u5e8f\u904d\u5386\u7684\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u63a8\u5bfc\u51fa\u7236\u8282\u70b9\u7d22\u5f15\u4e0e\u5b50\u8282\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u201c\u6620\u5c04\u516c\u5f0f\u201d\uff1a\u82e5\u8282\u70b9\u7684\u7d22\u5f15\u4e3a \\(i\\) \uff0c\u5219\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \u3002</p> <p></p> <p> \u56fe\uff1a\u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a </p> <p>\u6620\u5c04\u516c\u5f0f\u7684\u89d2\u8272\u76f8\u5f53\u4e8e\u94fe\u8868\u4e2d\u7684\u6307\u9488\u3002\u7ed9\u5b9a\u6570\u7ec4\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u8282\u70b9\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u901a\u8fc7\u6620\u5c04\u516c\u5f0f\u6765\u8bbf\u95ee\u5b83\u7684\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u3002</p>"},{"location":"chapter_tree/array_representation_of_tree/#732","title":"7.3.2. \u00a0 \u8868\u793a\u4efb\u610f\u4e8c\u53c9\u6811","text":"<p>\u7136\u800c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u5728\u4e8c\u53c9\u6811\u7684\u4e2d\u95f4\u5c42\uff0c\u901a\u5e38\u5b58\u5728\u8bb8\u591a \\(\\text{None}\\) \u3002\u7531\u4e8e\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5e76\u4e0d\u5305\u542b\u8fd9\u4e9b \\(\\text{None}\\) \uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u6cd5\u4ec5\u51ed\u8be5\u5e8f\u5217\u6765\u63a8\u6d4b \\(\\text{None}\\) \u7684\u6570\u91cf\u548c\u5206\u5e03\u4f4d\u7f6e\u3002\u8fd9\u610f\u5473\u7740\u5b58\u5728\u591a\u79cd\u4e8c\u53c9\u6811\u7ed3\u6784\u90fd\u7b26\u5408\u8be5\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002\u663e\u7136\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e0a\u8ff0\u7684\u6570\u7ec4\u8868\u793a\u65b9\u6cd5\u5df2\u7ecf\u5931\u6548\u3002</p> <p></p> <p> \u56fe\uff1a\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5bf9\u5e94\u591a\u79cd\u4e8c\u53c9\u6811\u53ef\u80fd\u6027 </p> <p>\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u4e2d\u663e\u5f0f\u5730\u5199\u51fa\u6240\u6709 \\(\\text{None}\\) \u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u8fd9\u6837\u5904\u7406\u540e\uff0c\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5c31\u53ef\u4ee5\u552f\u4e00\u8868\u793a\u4e8c\u53c9\u6811\u4e86\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u7684\u5305\u88c5\u7c7b Integer \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c INT_MAX \u6807\u8bb0\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\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\n# \u4f7f\u7528 None \u6765\u8868\u793a\u7a7a\u4f4d\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 any \u7c7b\u578b\u7684\u5207\u7247, \u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int \u6700\u5927\u503c\u6807\u8bb0\u7a7a\u4f4d\uff0c\u56e0\u6b64\u8981\u6c42\u8282\u70b9\u503c\u4e0d\u80fd\u4e3a INT_MAX\nint tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nint?[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 Int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n</code></pre> <pre><code>\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nList&lt;int?&gt; tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n</code></pre> <pre><code>\n</code></pre> <p></p> <p> \u56fe\uff1a\u4efb\u610f\u7c7b\u578b\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a </p> <p>\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u4f7f\u7528\u6570\u7ec4\u6765\u8868\u793a\u3002\u56de\u987e\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u5b9a\u4e49\uff0c\\(\\text{None}\\) \u53ea\u51fa\u73b0\u5728\u6700\u5e95\u5c42\u4e14\u9760\u53f3\u7684\u4f4d\u7f6e\uff0c\u56e0\u6b64\u6240\u6709 \\(\\text{None}\\) \u4e00\u5b9a\u51fa\u73b0\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u672b\u5c3e\u3002\u8fd9\u610f\u5473\u7740\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u5b8c\u5168\u4e8c\u53c9\u6811\u65f6\uff0c\u53ef\u4ee5\u7701\u7565\u5b58\u50a8\u6240\u6709 \\(\\text{None}\\) \uff0c\u975e\u5e38\u65b9\u4fbf\u3002</p> <p></p> <p> \u56fe\uff1a\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a </p> <p>\u5982\u4e0b\u4ee3\u7801\u7ed9\u51fa\u4e86\u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u5305\u62ec\u4ee5\u4e0b\u64cd\u4f5c\uff1a</p> <ul> <li>\u7ed9\u5b9a\u67d0\u8282\u70b9\uff0c\u83b7\u53d6\u5b83\u7684\u503c\u3001\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u3001\u7236\u8282\u70b9\u3002</li> <li>\u83b7\u53d6\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u3001\u540e\u5e8f\u904d\u5386\u3001\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002</li> </ul> JavaC++PythonGoJSTSCC#SwiftZigDartRust array_binary_tree.java<pre><code>/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\nprivate List&lt;Integer&gt; tree;\n/* \u6784\u9020\u65b9\u6cd5 */\npublic ArrayBinaryTree(List&lt;Integer&gt; arr) {\ntree = new ArrayList&lt;&gt;(arr);\n}\n/* \u8282\u70b9\u6570\u91cf */\npublic int size() {\nreturn tree.size();\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\npublic Integer val(int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i &lt; 0 || i &gt;= size())\nreturn null;\nreturn tree.get(i);\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\npublic Integer left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\npublic Integer right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\npublic Integer parent(int i) {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\npublic List&lt;Integer&gt; levelOrder() {\nList&lt;Integer&gt; res = new ArrayList&lt;&gt;();\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i &lt; size(); i++) {\nif (val(i) != null)\nres.add(val(i));\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nprivate void dfs(Integer i, String order, List&lt;Integer&gt; res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (val(i) == null)\nreturn;\n// \u524d\u5e8f\u904d\u5386\nif (order == \"pre\")\nres.add(val(i));\ndfs(left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order == \"in\")\nres.add(val(i));\ndfs(right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order == \"post\")\nres.add(val(i));\n}\n/* \u524d\u5e8f\u904d\u5386 */\npublic List&lt;Integer&gt; preOrder() {\nList&lt;Integer&gt; res = new ArrayList&lt;&gt;();\ndfs(0, \"pre\", res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\npublic List&lt;Integer&gt; inOrder() {\nList&lt;Integer&gt; res = new ArrayList&lt;&gt;();\ndfs(0, \"in\", res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\npublic List&lt;Integer&gt; postOrder() {\nList&lt;Integer&gt; res = new ArrayList&lt;&gt;();\ndfs(0, \"post\", res);\nreturn res;\n}\n}\n</code></pre> array_binary_tree.cpp<pre><code>/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nArrayBinaryTree(vector&lt;int&gt; arr) {\ntree = arr;\n}\n/* \u8282\u70b9\u6570\u91cf */\nint size() {\nreturn tree.size();\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint val(int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i &lt; 0 || i &gt;= size())\nreturn INT_MAX;\nreturn tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(int i) {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nvector&lt;int&gt; levelOrder() {\nvector&lt;int&gt; res;\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i &lt; size(); i++) {\nif (val(i) != INT_MAX)\nres.push_back(val(i));\n}\nreturn res;\n}\n/* \u524d\u5e8f\u904d\u5386 */\nvector&lt;int&gt; preOrder() {\nvector&lt;int&gt; res;\ndfs(0, \"pre\", res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvector&lt;int&gt; inOrder() {\nvector&lt;int&gt; res;\ndfs(0, \"in\", res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvector&lt;int&gt; postOrder() {\nvector&lt;int&gt; res;\ndfs(0, \"post\", res);\nreturn res;\n}\nprivate:\nvector&lt;int&gt; tree;\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(int i, string order, vector&lt;int&gt; &amp;res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (val(i) == INT_MAX)\nreturn;\n// \u524d\u5e8f\u904d\u5386\nif (order == \"pre\")\nres.push_back(val(i));\ndfs(left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order == \"in\")\nres.push_back(val(i));\ndfs(right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order == \"post\")\nres.push_back(val(i));\n}\n};\n</code></pre> array_binary_tree.py<pre><code>class ArrayBinaryTree:\n\"\"\"\u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b\"\"\"\ndef __init__(self, arr: list[int | None]):\n\"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\nself.__tree = list(arr)\ndef size(self):\n\"\"\"\u8282\u70b9\u6570\u91cf\"\"\"\nreturn len(self.__tree)\ndef val(self, i: int) -&gt; int:\n\"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c\"\"\"\n# \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif i &lt; 0 or i &gt;= self.size():\nreturn None\nreturn self.__tree[i]\ndef left(self, i: int) -&gt; int | None:\n\"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\nreturn 2 * i + 1\ndef right(self, i: int) -&gt; int | None:\n\"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\nreturn 2 * i + 2\ndef parent(self, i: int) -&gt; int | None:\n\"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\nreturn (i - 1) // 2\ndef level_order(self) -&gt; list[int]:\n\"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\nself.res = []\n# \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor i in range(self.size()):\nif self.val(i) is not None:\nself.res.append(self.val(i))\nreturn self.res\ndef __dfs(self, i: int, order: str):\n\"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\nif self.val(i) is None:\nreturn\n# \u524d\u5e8f\u904d\u5386\nif order == \"pre\":\nself.res.append(self.val(i))\nself.__dfs(self.left(i), order)\n# \u4e2d\u5e8f\u904d\u5386\nif order == \"in\":\nself.res.append(self.val(i))\nself.__dfs(self.right(i), order)\n# \u540e\u5e8f\u904d\u5386\nif order == \"post\":\nself.res.append(self.val(i))\ndef pre_order(self) -&gt; list[int]:\n\"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\nself.res = []\nself.__dfs(0, order=\"pre\")\nreturn self.res\ndef in_order(self) -&gt; list[int]:\n\"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\nself.res = []\nself.__dfs(0, order=\"in\")\nreturn self.res\ndef post_order(self) -&gt; list[int]:\n\"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\nself.res = []\nself.__dfs(0, order=\"post\")\nreturn self.res\n</code></pre> array_binary_tree.go<pre><code>/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\ntype arrayBinaryTree struct {\ntree []any\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newArrayBinaryTree(arr []any) *arrayBinaryTree {\nreturn &amp;arrayBinaryTree{\ntree: arr,\n}\n}\n/* \u8282\u70b9\u6570\u91cf */\nfunc (abt *arrayBinaryTree) size() int {\nreturn len(abt.tree)\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfunc (abt *arrayBinaryTree) val(i int) any {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif i &lt; 0 || i &gt;= abt.size() {\nreturn nil\n}\nreturn abt.tree[i]\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) left(i int) int {\nreturn 2*i + 1\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) right(i int) int {\nreturn 2*i + 2\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) parent(i int) int {\nreturn (i - 1) / 2\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) levelOrder() []any {\nvar res []any\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor i := 0; i &lt; abt.size(); i++ {\nif abt.val(i) != nil {\nres = append(res, abt.val(i))\n}\n}\nreturn res\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nfunc (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif abt.val(i) == nil {\nreturn\n}\n// \u524d\u5e8f\u904d\u5386\nif order == \"pre\" {\n*res = append(*res, abt.val(i))\n}\nabt.dfs(abt.left(i), order, res)\n// \u4e2d\u5e8f\u904d\u5386\nif order == \"in\" {\n*res = append(*res, abt.val(i))\n}\nabt.dfs(abt.right(i), order, res)\n// \u540e\u5e8f\u904d\u5386\nif order == \"post\" {\n*res = append(*res, abt.val(i))\n}\n}\n/* \u524d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) preOrder() []any {\nvar res []any\nabt.dfs(0, \"pre\", &amp;res)\nreturn res\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) inOrder() []any {\nvar res []any\nabt.dfs(0, \"in\", &amp;res)\nreturn res\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) postOrder() []any {\nvar res []any\nabt.dfs(0, \"post\", &amp;res)\nreturn res\n}\n</code></pre> array_binary_tree.js<pre><code>/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n#tree;\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(arr) {\nthis.#tree = arr;\n}\n/* \u8282\u70b9\u6570\u91cf */\nsize() {\nreturn this.#tree.length;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nval(i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i &lt; 0 || i &gt;= this.size()) return null;\nreturn this.#tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nleft(i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nright(i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nparent(i) {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nlevelOrder() {\nlet res = [];\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (let i = 0; i &lt; this.size(); i++) {\nif (this.val(i) !== null) res.push(this.val(i));\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n#dfs(i, order, res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (this.val(i) === null) return;\n// \u524d\u5e8f\u904d\u5386\nif (order === 'pre') res.push(this.val(i));\nthis.#dfs(this.left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order === 'in') res.push(this.val(i));\nthis.#dfs(this.right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order === 'post') res.push(this.val(i));\n}\n/* \u524d\u5e8f\u904d\u5386 */\npreOrder() {\nconst res = [];\nthis.#dfs(0, 'pre', res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\ninOrder() {\nconst res = [];\nthis.#dfs(0, 'in', res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\npostOrder() {\nconst res = [];\nthis.#dfs(0, 'post', res);\nreturn res;\n}\n}\n</code></pre> array_binary_tree.ts<pre><code>/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n#tree: (number | null)[];\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(arr: (number | null)[]) {\nthis.#tree = arr;\n}\n/* \u8282\u70b9\u6570\u91cf */\nsize(): number {\nreturn this.#tree.length;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nval(i: number): number | null {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i &lt; 0 || i &gt;= this.size()) return null;\nreturn this.#tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nleft(i: number): number {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nright(i: number): number {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nparent(i: number): number {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nlevelOrder(): number[] {\nlet res = [];\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (let i = 0; i &lt; this.size(); i++) {\nif (this.val(i) !== null) res.push(this.val(i));\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n#dfs(i: number, order: Order, res: (number | null)[]): void {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (this.val(i) === null) return;\n// \u524d\u5e8f\u904d\u5386\nif (order === 'pre') res.push(this.val(i));\nthis.#dfs(this.left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order === 'in') res.push(this.val(i));\nthis.#dfs(this.right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order === 'post') res.push(this.val(i));\n}\n/* \u524d\u5e8f\u904d\u5386 */\npreOrder(): (number | null)[] {\nconst res = [];\nthis.#dfs(0, 'pre', res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\ninOrder(): (number | null)[] {\nconst res = [];\nthis.#dfs(0, 'in', res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\npostOrder(): (number | null)[] {\nconst res = [];\nthis.#dfs(0, 'post', res);\nreturn res;\n}\n}\n</code></pre> array_binary_tree.c<pre><code>/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nstruct arrayBinaryTree {\nvector *tree;\n};\ntypedef struct arrayBinaryTree arrayBinaryTree;\n/* \u6784\u9020\u51fd\u6570 */\narrayBinaryTree *newArrayBinaryTree(vector *arr) {\narrayBinaryTree *newABT = malloc(sizeof(arrayBinaryTree));\nnewABT-&gt;tree = arr;\nreturn newABT;\n}\n/* \u8282\u70b9\u6570\u91cf */\nint size(arrayBinaryTree *abt) {\nreturn abt-&gt;tree-&gt;size;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint val(arrayBinaryTree *abt, int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i &lt; 0 || i &gt;= size(abt))\nreturn INT_MAX;\nreturn *(int *)abt-&gt;tree-&gt;data[i];\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(arrayBinaryTree *abt, int i, const char *order, vector *res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (val(abt, i) == INT_MAX)\nreturn;\n// \u524d\u5e8f\u904d\u5386\nif (strcmp(order, \"pre\") == 0) {\nint tmp = val(abt, i);\nvectorPushback(res, &amp;tmp, sizeof(tmp));\n}\ndfs(abt, left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (strcmp(order, \"in\") == 0) {\nint tmp = val(abt, i);\nvectorPushback(res, &amp;tmp, sizeof(tmp));\n}\ndfs(abt, right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (strcmp(order, \"post\") == 0) {\nint tmp = val(abt, i);\nvectorPushback(res, &amp;tmp, sizeof(tmp));\n}\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nvector *levelOrder(arrayBinaryTree *abt) {\nvector *res = newVector();\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i &lt; size(abt); i++) {\nif (val(abt, i) != INT_MAX) {\nint tmp = val(abt, i);\nvectorPushback(res, &amp;tmp, sizeof(int));\n}\n}\nreturn res;\n}\n/* \u524d\u5e8f\u904d\u5386 */\nvector *preOrder(arrayBinaryTree *abt) {\nvector *res = newVector();\ndfs(abt, 0, \"pre\", res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvector *inOrder(arrayBinaryTree *abt) {\nvector *res = newVector();\ndfs(abt, 0, \"in\", res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvector *postOrder(arrayBinaryTree *abt) {\nvector *res = newVector();\ndfs(abt, 0, \"post\", res);\nreturn res;\n}\n</code></pre> array_binary_tree.cs<pre><code>/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\nprivate List&lt;int?&gt; tree;\n/* \u6784\u9020\u65b9\u6cd5 */\npublic ArrayBinaryTree(List&lt;int?&gt; arr) {\ntree = new List&lt;int?&gt;(arr);\n}\n/* \u8282\u70b9\u6570\u91cf */\npublic int size() {\nreturn tree.Count;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\npublic int? val(int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i &lt; 0 || i &gt;= size())\nreturn null;\nreturn tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\npublic int left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\npublic int right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\npublic int parent(int i) {\nreturn (i - 1) / 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\npublic List&lt;int&gt; levelOrder() {\nList&lt;int&gt; res = new List&lt;int&gt;();\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i &lt; size(); i++) {\nif (val(i).HasValue)\nres.Add(val(i).Value);\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nprivate void dfs(int i, string order, List&lt;int&gt; res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (!val(i).HasValue)\nreturn;\n// \u524d\u5e8f\u904d\u5386\nif (order == \"pre\")\nres.Add(val(i).Value);\ndfs(left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order == \"in\")\nres.Add(val(i).Value);\ndfs(right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order == \"post\")\nres.Add(val(i).Value);\n}\n/* \u524d\u5e8f\u904d\u5386 */\npublic List&lt;int&gt; preOrder() {\nList&lt;int&gt; res = new List&lt;int&gt;();\ndfs(0, \"pre\", res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\npublic List&lt;int&gt; inOrder() {\nList&lt;int&gt; res = new List&lt;int&gt;();\ndfs(0, \"in\", res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\npublic List&lt;int&gt; postOrder() {\nList&lt;int&gt; res = new List&lt;int&gt;();\ndfs(0, \"post\", res);\nreturn res;\n}\n}\n</code></pre> array_binary_tree.swift<pre><code>/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\nprivate var tree: [Int?]\n/* \u6784\u9020\u65b9\u6cd5 */\ninit(arr: [Int?]) {\ntree = arr\n}\n/* \u8282\u70b9\u6570\u91cf */\nfunc size() -&gt; Int {\ntree.count\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfunc val(i: Int) -&gt; Int? {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif i &lt; 0 || i &gt;= size() {\nreturn nil\n}\nreturn tree[i]\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc left(i: Int) -&gt; Int {\n2 * i + 1\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc right(i: Int) -&gt; Int {\n2 * i + 2\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc parent(i: Int) -&gt; Int {\n(i - 1) / 2\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder() -&gt; [Int] {\nvar res: [Int] = []\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor i in stride(from: 0, to: size(), by: 1) {\nif let val = val(i: i) {\nres.append(val)\n}\n}\nreturn res\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nprivate func dfs(i: Int, order: String, res: inout [Int]) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nguard let val = val(i: i) else {\nreturn\n}\n// \u524d\u5e8f\u904d\u5386\nif order == \"pre\" {\nres.append(val)\n}\ndfs(i: left(i: i), order: order, res: &amp;res)\n// \u4e2d\u5e8f\u904d\u5386\nif order == \"in\" {\nres.append(val)\n}\ndfs(i: right(i: i), order: order, res: &amp;res)\n// \u540e\u5e8f\u904d\u5386\nif order == \"post\" {\nres.append(val)\n}\n}\n/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder() -&gt; [Int] {\nvar res: [Int] = []\ndfs(i: 0, order: \"pre\", res: &amp;res)\nreturn res\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder() -&gt; [Int] {\nvar res: [Int] = []\ndfs(i: 0, order: \"in\", res: &amp;res)\nreturn res\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder() -&gt; [Int] {\nvar res: [Int] = []\ndfs(i: 0, order: \"post\", res: &amp;res)\nreturn res\n}\n}\n</code></pre> array_binary_tree.zig<pre><code>[class]{ArrayBinaryTree}-[func]{}\n</code></pre> array_binary_tree.dart<pre><code>/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\nlate List&lt;int?&gt; _tree;\n/* \u6784\u9020\u65b9\u6cd5 */\nArrayBinaryTree(this._tree);\n/* \u8282\u70b9\u6570\u91cf */\nint size() {\nreturn _tree.length;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint? val(int i) {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif (i &lt; 0 || i &gt;= size()) {\nreturn null;\n}\nreturn _tree[i];\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint? left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint? right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint? parent(int i) {\nreturn (i - 1) ~/ 2;\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nList&lt;int&gt; levelOrder() {\nList&lt;int&gt; res = [];\nfor (int i = 0; i &lt; size(); i++) {\nif (val(i) != null) {\nres.add(val(i)!);\n}\n}\nreturn res;\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(int i, String order, List&lt;int?&gt; res) {\n// \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\nif (val(i) == null) {\nreturn;\n}\n// \u524d\u5e8f\u904d\u5386\nif (order == 'pre') {\nres.add(val(i));\n}\ndfs(left(i)!, order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif (order == 'in') {\nres.add(val(i));\n}\ndfs(right(i)!, order, res);\n// \u540e\u5e8f\u904d\u5386\nif (order == 'post') {\nres.add(val(i));\n}\n}\n/* \u524d\u5e8f\u904d\u5386 */\nList&lt;int?&gt; preOrder() {\nList&lt;int?&gt; res = [];\ndfs(0, 'pre', res);\nreturn res;\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nList&lt;int?&gt; inOrder() {\nList&lt;int?&gt; res = [];\ndfs(0, 'in', res);\nreturn res;\n}\n/* \u540e\u5e8f\u904d\u5386 */\nList&lt;int?&gt; postOrder() {\nList&lt;int?&gt; res = [];\ndfs(0, 'post', res);\nreturn res;\n}\n}\n</code></pre> array_binary_tree.rs<pre><code>/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nstruct ArrayBinaryTree {\ntree: Vec&lt;Option&lt;i32&gt;&gt;,\n}\nimpl ArrayBinaryTree {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new(arr: Vec&lt;Option&lt;i32&gt;&gt;) -&gt; Self {\nSelf { tree: arr }\n}\n/* \u8282\u70b9\u6570\u91cf */\nfn size(&amp;self) -&gt; i32 {\nself.tree.len() as i32\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfn val(&amp;self, i: i32) -&gt; Option&lt;i32&gt; {\n// \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\nif i &lt; 0 || i &gt;= self.size() {\nNone\n} else {\nself.tree[i as usize]\n}\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfn left(&amp;self, i: i32) -&gt; i32 {\n2 * i + 1\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfn right(&amp;self, i: i32) -&gt; i32 {\n2 * i + 2\n}\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfn parent(&amp;self, i: i32) -&gt; i32 {\n(i - 1) / 2\n}\n/* \u5c42\u5e8f\u904d\u5386 */\nfn level_order(&amp;self) -&gt; Vec&lt;i32&gt; {\nlet mut res = vec![];\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor i in 0..self.size() {\nif let Some(val) = self.val(i) {\nres.push(val)\n}\n}\nres\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nfn dfs(&amp;self, i: i32, order: &amp;str, res: &amp;mut Vec&lt;i32&gt;) {\nif self.val(i).is_none() {\nreturn;\n}\nlet val = self.val(i).unwrap();\n// \u524d\u5e8f\u904d\u5386\nif order == \"pre\" {\nres.push(val);\n}\nself.dfs(self.left(i), order, res);\n// \u4e2d\u5e8f\u904d\u5386\nif order == \"in\" {\nres.push(val);\n}\nself.dfs(self.right(i), order, res);\n// \u540e\u5e8f\u904d\u5386\nif order == \"post\" {\nres.push(val);\n}\n}\n/* \u524d\u5e8f\u904d\u5386 */\nfn pre_order(&amp;self) -&gt; Vec&lt;i32&gt; {\nlet mut res = vec![];\nself.dfs(0, \"pre\", &amp;mut res);\nres\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfn in_order(&amp;self) -&gt; Vec&lt;i32&gt; {\nlet mut res = vec![];\nself.dfs(0, \"in\", &amp;mut res);\nres\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfn post_order(&amp;self) -&gt; Vec&lt;i32&gt; {\nlet mut res = vec![];\nself.dfs(0, \"post\", &amp;mut res);\nres\n}\n}\n</code></pre>"},{"location":"chapter_tree/array_representation_of_tree/#733","title":"7.3.3. \u00a0 \u4f18\u52bf\u4e0e\u5c40\u9650\u6027","text":"<p>\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a\u7684\u4f18\u70b9\u5305\u62ec\uff1a</p> <ul> <li>\u6570\u7ec4\u5b58\u50a8\u5728\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff0c\u5bf9\u7f13\u5b58\u53cb\u597d\uff0c\u8bbf\u95ee\u4e0e\u904d\u5386\u901f\u5ea6\u8f83\u5feb\u3002</li> <li>\u4e0d\u9700\u8981\u5b58\u50a8\u6307\u9488\uff0c\u6bd4\u8f83\u8282\u7701\u7a7a\u95f4\u3002</li> <li>\u5141\u8bb8\u968f\u673a\u8bbf\u95ee\u8282\u70b9\u3002</li> </ul> <p>\u7136\u800c\uff0c\u6570\u7ec4\u8868\u793a\u4e5f\u5177\u6709\u4e00\u4e9b\u5c40\u9650\u6027\uff1a</p> <ul> <li>\u6570\u7ec4\u5b58\u50a8\u9700\u8981\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\uff0c\u56e0\u6b64\u4e0d\u9002\u5408\u5b58\u50a8\u6570\u636e\u91cf\u8fc7\u5927\u7684\u6811\u3002</li> <li>\u589e\u5220\u8282\u70b9\u9700\u8981\u901a\u8fc7\u6570\u7ec4\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u5b9e\u73b0\uff0c\u6548\u7387\u8f83\u4f4e\u3002</li> <li>\u5f53\u4e8c\u53c9\u6811\u4e2d\u5b58\u5728\u5927\u91cf \\(\\text{None}\\) \u65f6\uff0c\u6570\u7ec4\u4e2d\u5305\u542b\u7684\u8282\u70b9\u6570\u636e\u6bd4\u91cd\u8f83\u4f4e\uff0c\u7a7a\u95f4\u5229\u7528\u7387\u8f83\u4f4e\u3002</li> </ul>"},{"location":"chapter_tree/avl_tree/","title":"7.5. \u00a0 AVL \u6811 *","text":"<p>\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u4e86\u5728\u591a\u6b21\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u540e\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u53ef\u80fd\u9000\u5316\u4e3a\u94fe\u8868\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5c06\u4ece \\(O(\\log n)\\) \u6076\u5316\u4e3a \\(O(n)\\)\u3002</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u7ecf\u8fc7\u4e24\u6b21\u5220\u9664\u8282\u70b9\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u4e8c\u53c9\u641c\u7d22\u6811\u4fbf\u4f1a\u9000\u5316\u4e3a\u94fe\u8868\u3002</p> <p></p> <p> \u56fe\uff1aAVL \u6811\u5728\u5220\u9664\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316 </p> <p>\u518d\u4f8b\u5982\uff0c\u5728\u4ee5\u4e0b\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e24\u4e2a\u8282\u70b9\u540e\uff0c\u6811\u5c06\u4e25\u91cd\u5411\u5de6\u503e\u659c\uff0c\u67e5\u627e\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u968f\u4e4b\u6076\u5316\u3002</p> <p></p> <p> \u56fe\uff1aAVL \u6811\u5728\u63d2\u5165\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316 </p> <p>G. M. Adelson-Velsky \u548c E. M. Landis \u5728\u5176 1962 \u5e74\u53d1\u8868\u7684\u8bba\u6587 \"An algorithm for the organization of information\" \u4e2d\u63d0\u51fa\u4e86\u300cAVL \u6811\u300d\u3002\u8bba\u6587\u4e2d\u8be6\u7ec6\u63cf\u8ff0\u4e86\u4e00\u7cfb\u5217\u64cd\u4f5c\uff0c\u786e\u4fdd\u5728\u6301\u7eed\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u540e\uff0cAVL \u6811\u4e0d\u4f1a\u9000\u5316\uff0c\u4ece\u800c\u4f7f\u5f97\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4fdd\u6301\u5728 \\(O(\\log n)\\) \u7ea7\u522b\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5728\u9700\u8981\u9891\u7e41\u8fdb\u884c\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u7684\u573a\u666f\u4e2d\uff0cAVL \u6811\u80fd\u59cb\u7ec8\u4fdd\u6301\u9ad8\u6548\u7684\u6570\u636e\u64cd\u4f5c\u6027\u80fd\uff0c\u5177\u6709\u5f88\u597d\u7684\u5e94\u7528\u4ef7\u503c\u3002</p>"},{"location":"chapter_tree/avl_tree/#751-avl","title":"7.5.1. \u00a0 AVL \u6811\u5e38\u89c1\u672f\u8bed","text":"<p>\u300cAVL \u6811\u300d\u65e2\u662f\u4e8c\u53c9\u641c\u7d22\u6811\u4e5f\u662f\u5e73\u8861\u4e8c\u53c9\u6811\uff0c\u540c\u65f6\u6ee1\u8db3\u8fd9\u4e24\u7c7b\u4e8c\u53c9\u6811\u7684\u6240\u6709\u6027\u8d28\uff0c\u56e0\u6b64\u4e5f\u88ab\u79f0\u4e3a\u300c\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811\u300d\u3002</p>"},{"location":"chapter_tree/avl_tree/#_1","title":"\u8282\u70b9\u9ad8\u5ea6","text":"<p>\u5728\u64cd\u4f5c AVL \u6811\u65f6\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u8282\u70b9\u7684\u9ad8\u5ea6\uff0c\u56e0\u6b64\u9700\u8981\u4e3a AVL \u6811\u7684\u8282\u70b9\u7c7b\u6dfb\u52a0 <code>height</code> \u53d8\u91cf\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\npublic int val; // \u8282\u70b9\u503c\npublic int height; // \u8282\u70b9\u9ad8\u5ea6\npublic TreeNode left; // \u5de6\u5b50\u8282\u70b9\npublic TreeNode right; // \u53f3\u5b50\u8282\u70b9\npublic TreeNode(int x) { val = x; }\n}\n</code></pre> <pre><code>/* AVL \u6811\u8282\u70b9\u7c7b */\nstruct TreeNode {\nint val{}; // \u8282\u70b9\u503c\nint height = 0; // \u8282\u70b9\u9ad8\u5ea6\nTreeNode *left{}; // \u5de6\u5b50\u8282\u70b9\nTreeNode *right{}; // \u53f3\u5b50\u8282\u70b9\nTreeNode() = default;\nexplicit TreeNode(int x) : val(x){}\n};\n</code></pre> <pre><code>class TreeNode:\n\"\"\"AVL \u6811\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val # \u8282\u70b9\u503c\nself.height: int = 0 # \u8282\u70b9\u9ad8\u5ea6\nself.left: Optional[TreeNode] = None # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nself.right: Optional[TreeNode] = None # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n</code></pre> <pre><code>/* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\nVal int // \u8282\u70b9\u503c\nHeight int // \u8282\u70b9\u9ad8\u5ea6\nLeft *TreeNode // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nRight *TreeNode // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n</code></pre> <pre><code>/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nval; // \u8282\u70b9\u503c\nheight; //\u8282\u70b9\u9ad8\u5ea6\nleft; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nright; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nconstructor(val, left, right, height) {\nthis.val = val === undefined ? 0 : val;\nthis.height = height === undefined ? 0 : height;\nthis.left = left === undefined ? null : left;\nthis.right = right === undefined ? null : right;\n}\n}\n</code></pre> <pre><code>/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nval: number; // \u8282\u70b9\u503c\nheight: number; // \u8282\u70b9\u9ad8\u5ea6\nleft: TreeNode | null; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nright: TreeNode | null; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nconstructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\nthis.val = val === undefined ? 0 : val;\nthis.height = height === undefined ? 0 : height; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; }\n}\n</code></pre> <pre><code>/* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nint val;\nint height;\nstruct TreeNode *left;\nstruct TreeNode *right;\n};\ntypedef struct TreeNode TreeNode;\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\nTreeNode *node;\nnode = (TreeNode *)malloc(sizeof(TreeNode));\nnode-&gt;val = val;\nnode-&gt;height = 0;\nnode-&gt;left = NULL;\nnode-&gt;right = NULL;\nreturn node;\n}\n</code></pre> <pre><code>/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\npublic int val; // \u8282\u70b9\u503c\npublic int height; // \u8282\u70b9\u9ad8\u5ea6\npublic TreeNode? left; // \u5de6\u5b50\u8282\u70b9\npublic TreeNode? right; // \u53f3\u5b50\u8282\u70b9\npublic TreeNode(int x) { val = x; }\n}\n</code></pre> <pre><code>/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nvar val: Int // \u8282\u70b9\u503c\nvar height: Int // \u8282\u70b9\u9ad8\u5ea6\nvar left: TreeNode? // \u5de6\u5b50\u8282\u70b9\nvar right: TreeNode? // \u53f3\u5b50\u8282\u70b9\ninit(x: Int) {\nval = x\nheight = 0\n}\n}\n</code></pre> <pre><code>\n</code></pre> <pre><code>/* AVL \u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val; // \u8282\u70b9\u503c\nint height; // \u8282\u70b9\u9ad8\u5ea6\nTreeNode? left; // \u5de6\u5b50\u8282\u70b9\nTreeNode? right; // \u53f3\u5b50\u8282\u70b9\nTreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n</code></pre> <pre><code>\n</code></pre> <p>\u300c\u8282\u70b9\u9ad8\u5ea6\u300d\u662f\u6307\u4ece\u8be5\u8282\u70b9\u5230\u6700\u8fdc\u53f6\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u5373\u6240\u7ecf\u8fc7\u7684\u201c\u8fb9\u201d\u7684\u6570\u91cf\u3002\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u662f\uff0c\u53f6\u8282\u70b9\u7684\u9ad8\u5ea6\u4e3a 0 \uff0c\u800c\u7a7a\u8282\u70b9\u7684\u9ad8\u5ea6\u4e3a -1 \u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e24\u4e2a\u5de5\u5177\u51fd\u6570\uff0c\u5206\u522b\u7528\u4e8e\u83b7\u53d6\u548c\u66f4\u65b0\u8282\u70b9\u7684\u9ad8\u5ea6\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust avl_tree.java<pre><code>/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode node) {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\nreturn node == null ? -1 : node.height;\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode node) {\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.height = Math.max(height(node.left), height(node.right)) + 1;\n}\n</code></pre> avl_tree.cpp<pre><code>/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\nreturn node == nullptr ? -1 : node-&gt;height;\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode-&gt;height = max(height(node-&gt;left), height(node-&gt;right)) + 1;\n}\n</code></pre> avl_tree.py<pre><code>def height(self, node: TreeNode | None) -&gt; int:\n\"\"\"\u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\"\"\"\n# \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\nif node is not None:\nreturn node.height\nreturn -1\ndef __update_height(self, node: TreeNode | None):\n\"\"\"\u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\"\"\"\n# \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.height = max([self.height(node.left), self.height(node.right)]) + 1\n</code></pre> avl_tree.go<pre><code>/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) height(node *TreeNode) int {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\nif node != nil {\nreturn node.Height\n}\nreturn -1\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) updateHeight(node *TreeNode) {\nlh := t.height(node.Left)\nrh := t.height(node.Right)\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nif lh &gt; rh {\nnode.Height = lh + 1\n} else {\nnode.Height = rh + 1\n}\n}\n</code></pre> avl_tree.js<pre><code>/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nheight(node) {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\nreturn node === null ? -1 : node.height;\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\n#updateHeight(node) {\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.height =\nMath.max(this.height(node.left), this.height(node.right)) + 1;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nheight(node: TreeNode): number {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\nreturn node === null ? -1 : node.height;\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nupdateHeight(node: TreeNode): void {\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.height =\nMath.max(this.height(node.left), this.height(node.right)) + 1;\n}\n</code></pre> avl_tree.c<pre><code>/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\nif (node != NULL) {\nreturn node-&gt;height;\n}\nreturn -1;\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\nint lh = height(node-&gt;left);\nint rh = height(node-&gt;right);\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nif (lh &gt; rh) {\nnode-&gt;height = lh + 1;\n} else {\nnode-&gt;height = rh + 1;\n}\n}\n</code></pre> avl_tree.cs<pre><code>/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode? node) {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\nreturn node == null ? -1 : node.height;\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode node) {\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.height = Math.Max(height(node.left), height(node.right)) + 1;\n}\n</code></pre> avl_tree.swift<pre><code>/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfunc height(node: TreeNode?) -&gt; Int {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\nnode == nil ? -1 : node!.height\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc updateHeight(node: TreeNode?) {\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode?.height = max(height(node: node?.left), height(node: node?.right)) + 1\n}\n</code></pre> avl_tree.zig<pre><code>// \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\nfn height(self: *Self, node: ?*inc.TreeNode(T)) i32 {\n_ = self;\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\nreturn if (node == null) -1 else node.?.height;\n}\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nfn updateHeight(self: *Self, node: ?*inc.TreeNode(T)) void {\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.?.height = @max(self.height(node.?.left), self.height(node.?.right)) + 1;\n}\n</code></pre> avl_tree.dart<pre><code>/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode? node) {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\nreturn node == null ? -1 : node.height;\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode? node) {\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode!.height = max(height(node.left), height(node.right)) + 1;\n}\n</code></pre> avl_tree.rs<pre><code>/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfn height(node: OptionTreeNodeRc) -&gt; i32 {\n// \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\nmatch node {\nSome(node) =&gt; node.borrow().height,\nNone =&gt; -1,\n}\n}\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfn update_height(node: OptionTreeNodeRc) {\nif let Some(node) = node {\nlet left = node.borrow().left.clone();\nlet right = node.borrow().right.clone();\n// \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.borrow_mut().height = std::cmp::max(Self::height(left), Self::height(right)) + 1;\n}\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#_2","title":"\u8282\u70b9\u5e73\u8861\u56e0\u5b50","text":"<p>\u8282\u70b9\u7684\u300c\u5e73\u8861\u56e0\u5b50 Balance Factor\u300d\u5b9a\u4e49\u4e3a\u8282\u70b9\u5de6\u5b50\u6811\u7684\u9ad8\u5ea6\u51cf\u53bb\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\uff0c\u540c\u65f6\u89c4\u5b9a\u7a7a\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4e3a 0 \u3002\u6211\u4eec\u540c\u6837\u5c06\u83b7\u53d6\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u7684\u529f\u80fd\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust avl_tree.java<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode node) {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node == null)\nreturn 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node.left) - height(node.right);\n}\n</code></pre> avl_tree.cpp<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node == nullptr)\nreturn 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node-&gt;left) - height(node-&gt;right);\n}\n</code></pre> avl_tree.py<pre><code>def balance_factor(self, node: TreeNode | None) -&gt; int:\n\"\"\"\u83b7\u53d6\u5e73\u8861\u56e0\u5b50\"\"\"\n# \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif node is None:\nreturn 0\n# \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn self.height(node.left) - self.height(node.right)\n</code></pre> avl_tree.go<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc (t *aVLTree) balanceFactor(node *TreeNode) int {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif node == nil {\nreturn 0\n}\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn t.height(node.Left) - t.height(node.Right)\n}\n</code></pre> avl_tree.js<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node) {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node === null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn this.height(node.left) - this.height(node.right);\n}\n</code></pre> avl_tree.ts<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node: TreeNode): number {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node === null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn this.height(node.left) - this.height(node.right);\n}\n</code></pre> avl_tree.c<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node == NULL) {\nreturn 0;\n}\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node-&gt;left) - height(node-&gt;right);\n}\n</code></pre> avl_tree.cs<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode? node) {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node == null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node.left) - height(node.right);\n}\n</code></pre> avl_tree.swift<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc balanceFactor(node: TreeNode?) -&gt; Int {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nguard let node = node else { return 0 }\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node: node.left) - height(node: node.right)\n}\n</code></pre> avl_tree.zig<pre><code>// \u83b7\u53d6\u5e73\u8861\u56e0\u5b50\nfn balanceFactor(self: *Self, node: ?*inc.TreeNode(T)) i32 {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node == null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn self.height(node.?.left) - self.height(node.?.right);\n}\n</code></pre> avl_tree.dart<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode? node) {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node == null) return 0;\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node.left) - height(node.right);\n}\n</code></pre> avl_tree.rs<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfn balance_factor(node: OptionTreeNodeRc) -&gt; i32 {\nmatch node {\n// \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nNone =&gt; 0,\n// \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nSome(node) =&gt; {\nSelf::height(node.borrow().left.clone()) - Self::height(node.borrow().right.clone())\n}\n}\n}\n</code></pre> <p>Note</p> <p>\u8bbe\u5e73\u8861\u56e0\u5b50\u4e3a \\(f\\) \uff0c\u5219\u4e00\u68f5 AVL \u6811\u7684\u4efb\u610f\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7686\u6ee1\u8db3 \\(-1 \\le f \\le 1\\) \u3002</p>"},{"location":"chapter_tree/avl_tree/#752-avl","title":"7.5.2. \u00a0 AVL \u6811\u65cb\u8f6c","text":"<p>AVL \u6811\u7684\u7279\u70b9\u5728\u4e8e\u300c\u65cb\u8f6c Rotation\u300d\u64cd\u4f5c\uff0c\u5b83\u80fd\u591f\u5728\u4e0d\u5f71\u54cd\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u524d\u63d0\u4e0b\uff0c\u4f7f\u5931\u8861\u8282\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u65cb\u8f6c\u64cd\u4f5c\u65e2\u80fd\u4fdd\u6301\u6811\u7684\u300c\u4e8c\u53c9\u641c\u7d22\u6811\u300d\u5c5e\u6027\uff0c\u4e5f\u80fd\u4f7f\u6811\u91cd\u65b0\u53d8\u4e3a\u300c\u5e73\u8861\u4e8c\u53c9\u6811\u300d\u3002</p> <p>\u6211\u4eec\u5c06\u5e73\u8861\u56e0\u5b50\u7edd\u5bf9\u503c \\(&gt; 1\\) \u7684\u8282\u70b9\u79f0\u4e3a\u300c\u5931\u8861\u8282\u70b9\u300d\u3002\u6839\u636e\u8282\u70b9\u5931\u8861\u60c5\u51b5\u7684\u4e0d\u540c\uff0c\u65cb\u8f6c\u64cd\u4f5c\u5206\u4e3a\u56db\u79cd\uff1a\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u540e\u53f3\u65cb\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65cb\u8f6c\u64cd\u4f5c\u3002</p>"},{"location":"chapter_tree/avl_tree/#_3","title":"\u53f3\u65cb","text":"<p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u8282\u70b9\u4e0b\u65b9\u4e3a\u5e73\u8861\u56e0\u5b50\u3002\u4ece\u5e95\u81f3\u9876\u770b\uff0c\u4e8c\u53c9\u6811\u4e2d\u9996\u4e2a\u5931\u8861\u8282\u70b9\u662f\u201c\u8282\u70b9 3\u201d\u3002\u6211\u4eec\u5173\u6ce8\u4ee5\u8be5\u5931\u8861\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\uff0c\u5c06\u8be5\u8282\u70b9\u8bb0\u4e3a <code>node</code> \uff0c\u5176\u5de6\u5b50\u8282\u70b9\u8bb0\u4e3a <code>child</code> \uff0c\u6267\u884c\u300c\u53f3\u65cb\u300d\u64cd\u4f5c\u3002\u5b8c\u6210\u53f3\u65cb\u540e\uff0c\u5b50\u6811\u5df2\u7ecf\u6062\u590d\u5e73\u8861\uff0c\u5e76\u4e14\u4ecd\u7136\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u7279\u6027\u3002</p> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt; <p></p> <p></p> <p></p> <p></p> <p> \u56fe\uff1a\u53f3\u65cb\u64cd\u4f5c\u6b65\u9aa4 </p> <p>\u6b64\u5916\uff0c\u5982\u679c\u8282\u70b9 <code>child</code> \u672c\u8eab\u6709\u53f3\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a <code>grandChild</code> \uff09\uff0c\u5219\u9700\u8981\u5728\u300c\u53f3\u65cb\u300d\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 <code>grandChild</code> \u4f5c\u4e3a <code>node</code> \u7684\u5de6\u5b50\u8282\u70b9\u3002</p> <p></p> <p> \u56fe\uff1a\u6709 grandChild \u7684\u53f3\u65cb\u64cd\u4f5c </p> <p>\u201c\u5411\u53f3\u65cb\u8f6c\u201d\u662f\u4e00\u79cd\u5f62\u8c61\u5316\u7684\u8bf4\u6cd5\uff0c\u5b9e\u9645\u4e0a\u9700\u8981\u901a\u8fc7\u4fee\u6539\u8282\u70b9\u6307\u9488\u6765\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust avl_tree.java<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode rightRotate(TreeNode node) {\nTreeNode child = node.left;\nTreeNode grandChild = child.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.cpp<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\nTreeNode *child = node-&gt;left;\nTreeNode *grandChild = child-&gt;right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild-&gt;right = node;\nnode-&gt;left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.py<pre><code>def __right_rotate(self, node: TreeNode | None) -&gt; TreeNode | None:\n\"\"\"\u53f3\u65cb\u64cd\u4f5c\"\"\"\nchild = node.left\ngrand_child = child.right\n# \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node\nnode.left = grand_child\n# \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.__update_height(node)\nself.__update_height(child)\n# \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n</code></pre> avl_tree.go<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {\nchild := node.Left\ngrandChild := child.Right\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.Right = node\nnode.Left = grandChild\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\nt.updateHeight(child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n}\n</code></pre> avl_tree.js<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\n#rightRotate(node) {\nconst child = node.left;\nconst grandChild = child.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nthis.#updateHeight(node);\nthis.#updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nrightRotate(node: TreeNode): TreeNode {\nconst child = node.left;\nconst grandChild = child.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nthis.updateHeight(node);\nthis.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.c<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\nTreeNode *child, *grandChild;\nchild = node-&gt;left;\ngrandChild = child-&gt;right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild-&gt;right = node;\nnode-&gt;left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.cs<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? rightRotate(TreeNode? node) {\nTreeNode? child = node.left;\nTreeNode? grandChild = child?.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.swift<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nfunc rightRotate(node: TreeNode?) -&gt; TreeNode? {\nlet child = node?.left\nlet grandChild = child?.right\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild?.right = node\nnode?.left = grandChild\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node: node)\nupdateHeight(node: child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n}\n</code></pre> avl_tree.zig<pre><code>// \u53f3\u65cb\u64cd\u4f5c\nfn rightRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\nvar child = node.?.left;\nvar grandChild = child.?.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.?.right = node;\nnode.?.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.updateHeight(node);\nself.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.dart<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? rightRotate(TreeNode? node) {\nTreeNode? child = node!.left;\nTreeNode? grandChild = child!.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.rs<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nfn right_rotate(node: OptionTreeNodeRc) -&gt; OptionTreeNodeRc {\nmatch node {\nSome(node) =&gt; {\nlet child = node.borrow().left.clone().unwrap();\nlet grand_child = child.borrow().right.clone();\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.borrow_mut().right = Some(node.clone());\nnode.borrow_mut().left = grand_child;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nSelf::update_height(Some(node));\nSelf::update_height(Some(child.clone()));\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nSome(child)\n}\nNone =&gt; None,\n}\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#_4","title":"\u5de6\u65cb","text":"<p>\u76f8\u5e94\u7684\uff0c\u5982\u679c\u8003\u8651\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u201c\u955c\u50cf\u201d\uff0c\u5219\u9700\u8981\u6267\u884c\u300c\u5de6\u65cb\u300d\u64cd\u4f5c\u3002</p> <p></p> <p> \u56fe\uff1a\u5de6\u65cb\u64cd\u4f5c </p> <p>\u540c\u7406\uff0c\u82e5\u8282\u70b9 <code>child</code> \u672c\u8eab\u6709\u5de6\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a <code>grandChild</code> \uff09\uff0c\u5219\u9700\u8981\u5728\u300c\u5de6\u65cb\u300d\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 <code>grandChild</code> \u4f5c\u4e3a <code>node</code> \u7684\u53f3\u5b50\u8282\u70b9\u3002</p> <p></p> <p> \u56fe\uff1a\u6709 grandChild \u7684\u5de6\u65cb\u64cd\u4f5c </p> <p>\u53ef\u4ee5\u89c2\u5bdf\u5230\uff0c\u53f3\u65cb\u548c\u5de6\u65cb\u64cd\u4f5c\u5728\u903b\u8f91\u4e0a\u662f\u955c\u50cf\u5bf9\u79f0\u7684\uff0c\u5b83\u4eec\u5206\u522b\u89e3\u51b3\u7684\u4e24\u79cd\u5931\u8861\u60c5\u51b5\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u57fa\u4e8e\u5bf9\u79f0\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u53f3\u65cb\u7684\u4ee3\u7801\u63a8\u5bfc\u51fa\u5de6\u65cb\u7684\u4ee3\u7801\u3002\u5177\u4f53\u5730\uff0c\u53ea\u9700\u5c06\u300c\u53f3\u65cb\u300d\u4ee3\u7801\u4e2d\u7684\u628a\u6240\u6709\u7684 <code>left</code> \u66ff\u6362\u4e3a <code>right</code> \uff0c\u5c06\u6240\u6709\u7684 <code>right</code> \u66ff\u6362\u4e3a <code>left</code> \uff0c\u5373\u53ef\u5f97\u5230\u300c\u5de6\u65cb\u300d\u4ee3\u7801\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust avl_tree.java<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode leftRotate(TreeNode node) {\nTreeNode child = node.right;\nTreeNode grandChild = child.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.cpp<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\nTreeNode *child = node-&gt;right;\nTreeNode *grandChild = child-&gt;left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild-&gt;left = node;\nnode-&gt;right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.py<pre><code>def __left_rotate(self, node: TreeNode | None) -&gt; TreeNode | None:\n\"\"\"\u5de6\u65cb\u64cd\u4f5c\"\"\"\nchild = node.right\ngrand_child = child.left\n# \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node\nnode.right = grand_child\n# \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.__update_height(node)\nself.__update_height(child)\n# \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n</code></pre> avl_tree.go<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {\nchild := node.Right\ngrandChild := child.Left\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.Left = node\nnode.Right = grandChild\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\nt.updateHeight(child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n}\n</code></pre> avl_tree.js<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\n#leftRotate(node) {\nconst child = node.right;\nconst grandChild = child.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nthis.#updateHeight(node);\nthis.#updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nleftRotate(node: TreeNode): TreeNode {\nconst child = node.right;\nconst grandChild = child.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nthis.updateHeight(node);\nthis.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.c<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\nTreeNode *child, *grandChild;\nchild = node-&gt;right;\ngrandChild = child-&gt;left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild-&gt;left = node;\nnode-&gt;right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.cs<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? leftRotate(TreeNode? node) {\nTreeNode? child = node.right;\nTreeNode? grandChild = child?.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.swift<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nfunc leftRotate(node: TreeNode?) -&gt; TreeNode? {\nlet child = node?.right\nlet grandChild = child?.left\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild?.left = node\nnode?.right = grandChild\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node: node)\nupdateHeight(node: child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child\n}\n</code></pre> avl_tree.zig<pre><code>// \u5de6\u65cb\u64cd\u4f5c\nfn leftRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\nvar child = node.?.right;\nvar grandChild = child.?.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.?.left = node;\nnode.?.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.updateHeight(node);\nself.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.dart<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? leftRotate(TreeNode? node) {\nTreeNode? child = node!.right;\nTreeNode? grandChild = child!.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn child;\n}\n</code></pre> avl_tree.rs<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nfn left_rotate(node: OptionTreeNodeRc) -&gt; OptionTreeNodeRc {\nmatch node {\nSome(node) =&gt; {\nlet child = node.borrow().right.clone().unwrap();\nlet grand_child = child.borrow().left.clone();\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.borrow_mut().left = Some(node.clone());\nnode.borrow_mut().right = grand_child;\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nSelf::update_height(Some(node));\nSelf::update_height(Some(child.clone()));\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\nSome(child)\n}\nNone =&gt; None,\n}\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#_5","title":"\u5148\u5de6\u65cb\u540e\u53f3\u65cb","text":"<p>\u5bf9\u4e8e\u4e0b\u56fe\u4e2d\u7684\u5931\u8861\u8282\u70b9 3\uff0c\u4ec5\u4f7f\u7528\u5de6\u65cb\u6216\u53f3\u65cb\u90fd\u65e0\u6cd5\u4f7f\u5b50\u6811\u6062\u590d\u5e73\u8861\u3002\u6b64\u65f6\u9700\u8981\u5148\u5de6\u65cb\u540e\u53f3\u65cb\uff0c\u5373\u5148\u5bf9 <code>child</code> \u6267\u884c\u300c\u5de6\u65cb\u300d\uff0c\u518d\u5bf9 <code>node</code> \u6267\u884c\u300c\u53f3\u65cb\u300d\u3002</p> <p></p> <p> \u56fe\uff1a\u5148\u5de6\u65cb\u540e\u53f3\u65cb </p>"},{"location":"chapter_tree/avl_tree/#_6","title":"\u5148\u53f3\u65cb\u540e\u5de6\u65cb","text":"<p>\u540c\u7406\uff0c\u5bf9\u4e8e\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u955c\u50cf\u60c5\u51b5\uff0c\u9700\u8981\u5148\u53f3\u65cb\u540e\u5de6\u65cb\uff0c\u5373\u5148\u5bf9 <code>child</code> \u6267\u884c\u300c\u53f3\u65cb\u300d\uff0c\u7136\u540e\u5bf9 <code>node</code> \u6267\u884c\u300c\u5de6\u65cb\u300d\u3002</p> <p></p> <p> \u56fe\uff1a\u5148\u53f3\u65cb\u540e\u5de6\u65cb </p>"},{"location":"chapter_tree/avl_tree/#_7","title":"\u65cb\u8f6c\u7684\u9009\u62e9","text":"<p>\u4e0b\u56fe\u5c55\u793a\u7684\u56db\u79cd\u5931\u8861\u60c5\u51b5\u4e0e\u4e0a\u8ff0\u6848\u4f8b\u9010\u4e2a\u5bf9\u5e94\uff0c\u5206\u522b\u9700\u8981\u91c7\u7528\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u540e\u5de6\u3001\u5148\u5de6\u540e\u53f3\u7684\u65cb\u8f6c\u64cd\u4f5c\u3002</p> <p></p> <p> \u56fe\uff1aAVL \u6811\u7684\u56db\u79cd\u65cb\u8f6c\u60c5\u51b5 </p> <p>\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5224\u65ad\u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4ee5\u53ca\u8f83\u9ad8\u4e00\u4fa7\u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7684\u6b63\u8d1f\u53f7\uff0c\u6765\u786e\u5b9a\u5931\u8861\u8282\u70b9\u5c5e\u4e8e\u4e0a\u56fe\u4e2d\u7684\u54ea\u79cd\u60c5\u51b5\u3002</p> <p> \u8868\uff1a\u56db\u79cd\u65cb\u8f6c\u60c5\u51b5\u7684\u9009\u62e9\u6761\u4ef6 </p> \u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5e94\u91c7\u7528\u7684\u65cb\u8f6c\u65b9\u6cd5 \\(&gt; 1\\) \uff08\u5373\u5de6\u504f\u6811\uff09 \\(\\geq 0\\) \u53f3\u65cb \\(&gt; 1\\) \uff08\u5373\u5de6\u504f\u6811\uff09 \\(&lt;0\\) \u5148\u5de6\u65cb\u540e\u53f3\u65cb \\(&lt; -1\\) \uff08\u5373\u53f3\u504f\u6811\uff09 \\(\\leq 0\\) \u5de6\u65cb \\(&lt; -1\\) \uff08\u5373\u53f3\u504f\u6811\uff09 \\(&gt;0\\) \u5148\u53f3\u65cb\u540e\u5de6\u65cb <p>\u4e3a\u4e86\u4fbf\u4e8e\u4f7f\u7528\uff0c\u6211\u4eec\u5c06\u65cb\u8f6c\u64cd\u4f5c\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u3002\u6709\u4e86\u8fd9\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u5c31\u80fd\u5bf9\u5404\u79cd\u5931\u8861\u60c5\u51b5\u8fdb\u884c\u65cb\u8f6c\uff0c\u4f7f\u5931\u8861\u8282\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust avl_tree.java<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode rotate(TreeNode node) {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nint balanceFactor = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactor &gt; 1) {\nif (balanceFactor(node.left) &gt;= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = leftRotate(node.left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactor &lt; -1) {\nif (balanceFactor(node.right) &lt;= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = rightRotate(node.right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n</code></pre> avl_tree.cpp<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nint _balanceFactor = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (_balanceFactor &gt; 1) {\nif (balanceFactor(node-&gt;left) &gt;= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode-&gt;left = leftRotate(node-&gt;left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (_balanceFactor &lt; -1) {\nif (balanceFactor(node-&gt;right) &lt;= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode-&gt;right = rightRotate(node-&gt;right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n</code></pre> avl_tree.py<pre><code>def __rotate(self, node: TreeNode | None) -&gt; TreeNode | None:\n\"\"\"\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\"\"\"\n# \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nbalance_factor = self.balance_factor(node)\n# \u5de6\u504f\u6811\nif balance_factor &gt; 1:\nif self.balance_factor(node.left) &gt;= 0:\n# \u53f3\u65cb\nreturn self.__right_rotate(node)\nelse:\n# \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = self.__left_rotate(node.left)\nreturn self.__right_rotate(node)\n# \u53f3\u504f\u6811\nelif balance_factor &lt; -1:\nif self.balance_factor(node.right) &lt;= 0:\n# \u5de6\u65cb\nreturn self.__left_rotate(node)\nelse:\n# \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = self.__right_rotate(node.right)\nreturn self.__left_rotate(node)\n# \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n</code></pre> avl_tree.go<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc (t *aVLTree) rotate(node *TreeNode) *TreeNode {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n// Go \u63a8\u8350\u77ed\u53d8\u91cf\uff0c\u8fd9\u91cc bf \u6307\u4ee3 t.balanceFactor\nbf := t.balanceFactor(node)\n// \u5de6\u504f\u6811\nif bf &gt; 1 {\nif t.balanceFactor(node.Left) &gt;= 0 {\n// \u53f3\u65cb\nreturn t.rightRotate(node)\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.Left = t.leftRotate(node.Left)\nreturn t.rightRotate(node)\n}\n}\n// \u53f3\u504f\u6811\nif bf &lt; -1 {\nif t.balanceFactor(node.Right) &lt;= 0 {\n// \u5de6\u65cb\nreturn t.leftRotate(node)\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.Right = t.rightRotate(node.Right)\nreturn t.leftRotate(node)\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n}\n</code></pre> avl_tree.js<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n#rotate(node) {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nconst balanceFactor = this.balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactor &gt; 1) {\nif (this.balanceFactor(node.left) &gt;= 0) {\n// \u53f3\u65cb\nreturn this.#rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = this.#leftRotate(node.left);\nreturn this.#rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactor &lt; -1) {\nif (this.balanceFactor(node.right) &lt;= 0) {\n// \u5de6\u65cb\nreturn this.#leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = this.#rightRotate(node.right);\nreturn this.#leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nrotate(node: TreeNode): TreeNode {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nconst balanceFactor = this.balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactor &gt; 1) {\nif (this.balanceFactor(node.left) &gt;= 0) {\n// \u53f3\u65cb\nreturn this.rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = this.leftRotate(node.left);\nreturn this.rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactor &lt; -1) {\nif (this.balanceFactor(node.right) &lt;= 0) {\n// \u5de6\u65cb\nreturn this.leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = this.rightRotate(node.right);\nreturn this.leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n</code></pre> avl_tree.c<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nint bf = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (bf &gt; 1) {\nif (balanceFactor(node-&gt;left) &gt;= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode-&gt;left = leftRotate(node-&gt;left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (bf &lt; -1) {\nif (balanceFactor(node-&gt;right) &lt;= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode-&gt;right = rightRotate(node-&gt;right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n</code></pre> avl_tree.cs<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? rotate(TreeNode? node) {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nint balanceFactorInt = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactorInt &gt; 1) {\nif (balanceFactor(node.left) &gt;= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = leftRotate(node?.left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactorInt &lt; -1) {\nif (balanceFactor(node.right) &lt;= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = rightRotate(node?.right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n</code></pre> avl_tree.swift<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc rotate(node: TreeNode?) -&gt; TreeNode? {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nlet balanceFactor = balanceFactor(node: node)\n// \u5de6\u504f\u6811\nif balanceFactor &gt; 1 {\nif self.balanceFactor(node: node?.left) &gt;= 0 {\n// \u53f3\u65cb\nreturn rightRotate(node: node)\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode?.left = leftRotate(node: node?.left)\nreturn rightRotate(node: node)\n}\n}\n// \u53f3\u504f\u6811\nif balanceFactor &lt; -1 {\nif self.balanceFactor(node: node?.right) &lt;= 0 {\n// \u5de6\u65cb\nreturn leftRotate(node: node)\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode?.right = rightRotate(node: node?.right)\nreturn leftRotate(node: node)\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n}\n</code></pre> avl_tree.zig<pre><code>// \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nfn rotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nvar balance_factor = self.balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balance_factor &gt; 1) {\nif (self.balanceFactor(node.?.left) &gt;= 0) {\n// \u53f3\u65cb\nreturn self.rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.?.left = self.leftRotate(node.?.left);\nreturn self.rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balance_factor &lt; -1) {\nif (self.balanceFactor(node.?.right) &lt;= 0) {\n// \u5de6\u65cb\nreturn self.leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.?.right = self.rightRotate(node.?.right);\nreturn self.leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n</code></pre> avl_tree.dart<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? rotate(TreeNode? node) {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nint factor = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (factor &gt; 1) {\nif (balanceFactor(node!.left) &gt;= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = leftRotate(node.left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (factor &lt; -1) {\nif (balanceFactor(node!.right) &lt;= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = rightRotate(node.right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n</code></pre> avl_tree.rs<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfn rotate(node: OptionTreeNodeRc) -&gt; OptionTreeNodeRc {\n// \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nlet balance_factor = Self::balance_factor(node.clone());\n// \u5de6\u504f\u6811\nif balance_factor &gt; 1 {\nlet node = node.unwrap();\nif Self::balance_factor(node.borrow().left.clone()) &gt;= 0 {\n// \u53f3\u65cb\nSelf::right_rotate(Some(node))\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nlet left = node.borrow().left.clone();\nnode.borrow_mut().left = Self::left_rotate(left);\nSelf::right_rotate(Some(node))\n}\n}\n// \u53f3\u504f\u6811\nelse if balance_factor &lt; -1 {\nlet node = node.unwrap();\nif Self::balance_factor(node.borrow().right.clone()) &lt;= 0 {\n// \u5de6\u65cb\nSelf::left_rotate(Some(node))\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nlet right = node.borrow().right.clone();\nnode.borrow_mut().right = Self::right_rotate(right);\nSelf::left_rotate(Some(node))\n}\n} else {\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nnode\n}\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#753-avl","title":"7.5.3. \u00a0 AVL \u6811\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_tree/avl_tree/#_8","title":"\u63d2\u5165\u8282\u70b9","text":"<p>\u300cAVL \u6811\u300d\u7684\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u4e0e\u300c\u4e8c\u53c9\u641c\u7d22\u6811\u300d\u5728\u4e3b\u4f53\u4e0a\u7c7b\u4f3c\u3002\u552f\u4e00\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5728 AVL \u6811\u4e2d\u63d2\u5165\u8282\u70b9\u540e\uff0c\u4ece\u8be5\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u7cfb\u5217\u5931\u8861\u8282\u70b9\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4ece\u8fd9\u4e2a\u8282\u70b9\u5f00\u59cb\uff0c\u81ea\u5e95\u5411\u4e0a\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust avl_tree.java<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\nroot = insertHelper(root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode insertHelper(TreeNode node, int val) {\nif (node == null)\nreturn new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val &lt; node.val)\nnode.left = insertHelper(node.left, val);\nelse if (val &gt; node.val)\nnode.right = insertHelper(node.right, val);\nelse\nreturn node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.cpp<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\nroot = insertHelper(root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\nif (node == nullptr)\nreturn new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val &lt; node-&gt;val)\nnode-&gt;left = insertHelper(node-&gt;left, val);\nelse if (val &gt; node-&gt;val)\nnode-&gt;right = insertHelper(node-&gt;right, val);\nelse\nreturn node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.py<pre><code>def insert(self, val):\n\"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\nself.root = self.__insert_helper(self.root, val)\ndef __insert_helper(self, node: TreeNode | None, val: int) -&gt; TreeNode:\n\"\"\"\u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\nif node is None:\nreturn TreeNode(val)\n# 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9\nif val &lt; node.val:\nnode.left = self.__insert_helper(node.left, val)\nelif val &gt; node.val:\nnode.right = self.__insert_helper(node.right, val)\nelse:\n# \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n# \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.__update_height(node)\n# 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nreturn self.__rotate(node)\n</code></pre> avl_tree.go<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nfunc (t *aVLTree) insert(val int) {\nt.root = t.insertHelper(t.root, val)\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nfunc (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {\nif node == nil {\nreturn NewTreeNode(val)\n}\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif val &lt; node.Val.(int) {\nnode.Left = t.insertHelper(node.Left, val)\n} else if val &gt; node.Val.(int) {\nnode.Right = t.insertHelper(node.Right, val)\n} else {\n// \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n}\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = t.rotate(node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node\n}\n</code></pre> avl_tree.js<pre><code>/* \u63d2\u5165\u8282\u70b9 */\ninsert(val) {\nthis.root = this.#insertHelper(this.root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#insertHelper(node, val) {\nif (node === null) return new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val &lt; node.val) node.left = this.#insertHelper(node.left, val);\nelse if (val &gt; node.val)\nnode.right = this.#insertHelper(node.right, val);\nelse return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nthis.#updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = this.#rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u63d2\u5165\u8282\u70b9 */\ninsert(val: number): void {\nthis.root = this.insertHelper(this.root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\ninsertHelper(node: TreeNode, val: number): TreeNode {\nif (node === null) return new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val &lt; node.val) {\nnode.left = this.insertHelper(node.left, val);\n} else if (val &gt; node.val) {\nnode.right = this.insertHelper(node.right, val);\n} else {\nreturn node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\nthis.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = this.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.c<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(aVLTree *tree, int val) {\ntree-&gt;root = insertHelper(tree-&gt;root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\nif (node == NULL) {\nreturn newTreeNode(val);\n}\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val &lt; node-&gt;val) {\nnode-&gt;left = insertHelper(node-&gt;left, val);\n} else if (val &gt; node-&gt;val) {\nnode-&gt;right = insertHelper(node-&gt;right, val);\n} else {\n// \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.cs<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\nroot = insertHelper(root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? insertHelper(TreeNode? node, int val) {\nif (node == null) return new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val &lt; node.val)\nnode.left = insertHelper(node.left, val);\nelse if (val &gt; node.val)\nnode.right = insertHelper(node.right, val);\nelse\nreturn node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.swift<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nfunc insert(val: Int) {\nroot = insertHelper(node: root, val: val)\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc insertHelper(node: TreeNode?, val: Int) -&gt; TreeNode? {\nvar node = node\nif node == nil {\nreturn TreeNode(x: val)\n}\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif val &lt; node!.val {\nnode?.left = insertHelper(node: node?.left, val: val)\n} else if val &gt; node!.val {\nnode?.right = insertHelper(node: node?.right, val: val)\n} else {\nreturn node // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\nupdateHeight(node: node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node: node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node\n}\n</code></pre> avl_tree.zig<pre><code>// \u63d2\u5165\u8282\u70b9\nfn insert(self: *Self, val: T) !void {\nself.root = (try self.insertHelper(self.root, val)).?;\n}\n// \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\nfn insertHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) !?*inc.TreeNode(T) {\nvar node = node_;\nif (node == null) {\nvar tmp_node = try self.mem_allocator.create(inc.TreeNode(T));\ntmp_node.init(val);\nreturn tmp_node;\n}\n// 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9\nif (val &lt; node.?.val) {\nnode.?.left = try self.insertHelper(node.?.left, val);\n} else if (val &gt; node.?.val) {\nnode.?.right = try self.insertHelper(node.?.right, val);\n} else {\nreturn node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\nself.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n// 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nnode = self.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.dart<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\nroot = insertHelper(root, val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? insertHelper(TreeNode? node, int val) {\nif (node == null) return TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nif (val &lt; node.val)\nnode.left = insertHelper(node.left, val);\nelse if (val &gt; node.val)\nnode.right = insertHelper(node.right, val);\nelse\nreturn node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.rs<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nfn insert(&amp;mut self, val: i32) {\nself.root = Self::insert_helper(self.root.clone(), val);\n}\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn insert_helper(node: OptionTreeNodeRc, val: i32) -&gt; OptionTreeNodeRc {\nmatch node {\nSome(mut node) =&gt; {\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u8282\u70b9 */\nmatch {\nlet node_val = node.borrow().val;\nnode_val\n}\n.cmp(&amp;val)\n{\nOrdering::Greater =&gt; {\nlet left = node.borrow().left.clone();\nnode.borrow_mut().left = Self::insert_helper(left, val);\n}\nOrdering::Less =&gt; {\nlet right = node.borrow().right.clone();\nnode.borrow_mut().right = Self::insert_helper(right, val);\n}\nOrdering::Equal =&gt; {\nreturn Some(node); // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\n}\nSelf::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = Self::rotate(Some(node)).unwrap();\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nSome(node)\n}\nNone =&gt; Some(TreeNode::new(val)),\n}\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#_9","title":"\u5220\u9664\u8282\u70b9","text":"<p>\u7c7b\u4f3c\u5730\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5220\u9664\u8282\u70b9\u65b9\u6cd5\u7684\u57fa\u7840\u4e0a\uff0c\u9700\u8981\u4ece\u5e95\u81f3\u9876\u5730\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust avl_tree.java<pre><code>/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\nroot = removeHelper(root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode removeHelper(TreeNode node, int val) {\nif (node == null)\nreturn null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val &lt; node.val)\nnode.left = removeHelper(node.left, val);\nelse if (val &gt; node.val)\nnode.right = removeHelper(node.right, val);\nelse {\nif (node.left == null || node.right == null) {\nTreeNode child = node.left != null ? node.left : node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == null)\nreturn null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse\nnode = child;\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\nTreeNode temp = node.right;\nwhile (temp.left != null) {\ntemp = temp.left;\n}\nnode.right = removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nupdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.cpp<pre><code>/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\nroot = removeHelper(root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\nif (node == nullptr)\nreturn nullptr;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val &lt; node-&gt;val)\nnode-&gt;left = removeHelper(node-&gt;left, val);\nelse if (val &gt; node-&gt;val)\nnode-&gt;right = removeHelper(node-&gt;right, val);\nelse {\nif (node-&gt;left == nullptr || node-&gt;right == nullptr) {\nTreeNode *child = node-&gt;left != nullptr ? node-&gt;left : node-&gt;right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == nullptr) {\ndelete node;\nreturn nullptr;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse {\ndelete node;\nnode = child;\n}\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\nTreeNode *temp = node-&gt;right;\nwhile (temp-&gt;left != nullptr) {\ntemp = temp-&gt;left;\n}\nint tempVal = temp-&gt;val;\nnode-&gt;right = removeHelper(node-&gt;right, temp-&gt;val);\nnode-&gt;val = tempVal;\n}\n}\nupdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.py<pre><code>def remove(self, val: int):\n\"\"\"\u5220\u9664\u8282\u70b9\"\"\"\nself.root = self.__remove_helper(self.root, val)\ndef __remove_helper(self, node: TreeNode | None, val: int) -&gt; TreeNode | None:\n\"\"\"\u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\nif node is None:\nreturn None\n# 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b\nif val &lt; node.val:\nnode.left = self.__remove_helper(node.left, val)\nelif val &gt; node.val:\nnode.right = self.__remove_helper(node.right, val)\nelse:\nif node.left is None or node.right is None:\nchild = node.left or node.right\n# \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif child is None:\nreturn None\n# \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse:\nnode = child\nelse:\n# \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\ntemp = node.right\nwhile temp.left is not None:\ntemp = temp.left\nnode.right = self.__remove_helper(node.right, temp.val)\nnode.val = temp.val\n# \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nself.__update_height(node)\n# 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nreturn self.__rotate(node)\n</code></pre> avl_tree.go<pre><code>/* \u5220\u9664\u8282\u70b9 */\nfunc (t *aVLTree) remove(val int) {\nt.root = t.removeHelper(t.root, val)\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nfunc (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {\nif node == nil {\nreturn nil\n}\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif val &lt; node.Val.(int) {\nnode.Left = t.removeHelper(node.Left, val)\n} else if val &gt; node.Val.(int) {\nnode.Right = t.removeHelper(node.Right, val)\n} else {\nif node.Left == nil || node.Right == nil {\nchild := node.Left\nif node.Right != nil {\nchild = node.Right\n}\nif child == nil {\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nreturn nil\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nnode = child\n}\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\ntemp := node.Right\nfor temp.Left != nil {\ntemp = temp.Left\n}\nnode.Right = t.removeHelper(node.Right, temp.Val.(int))\nnode.Val = temp.Val\n}\n}\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = t.rotate(node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node\n}\n</code></pre> avl_tree.js<pre><code>/* \u5220\u9664\u8282\u70b9 */\nremove(val) {\nthis.root = this.#removeHelper(this.root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#removeHelper(node, val) {\nif (node === null) return null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val &lt; node.val) node.left = this.#removeHelper(node.left, val);\nelse if (val &gt; node.val)\nnode.right = this.#removeHelper(node.right, val);\nelse {\nif (node.left === null || node.right === null) {\nconst child = node.left !== null ? node.left : node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child === null) return null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse node = child;\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\nlet temp = node.right;\nwhile (temp.left !== null) {\ntemp = temp.left;\n}\nnode.right = this.#removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nthis.#updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = this.#rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u5220\u9664\u8282\u70b9 */\nremove(val: number): void {\nthis.root = this.removeHelper(this.root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nremoveHelper(node: TreeNode, val: number): TreeNode {\nif (node === null) return null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val &lt; node.val) {\nnode.left = this.removeHelper(node.left, val);\n} else if (val &gt; node.val) {\nnode.right = this.removeHelper(node.right, val);\n} else {\nif (node.left === null || node.right === null) {\nconst child = node.left !== null ? node.left : node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child === null) {\nreturn null;\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nnode = child;\n}\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\nlet temp = node.right;\nwhile (temp.left !== null) {\ntemp = temp.left;\n}\nnode.right = this.removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nthis.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = this.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.c<pre><code>/* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeNode(aVLTree *tree, int val) {\nTreeNode *root = removeHelper(tree-&gt;root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\nTreeNode *child, *grandChild;\nif (node == NULL) {\nreturn NULL;\n}\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val &lt; node-&gt;val) {\nnode-&gt;left = removeHelper(node-&gt;left, val);\n} else if (val &gt; node-&gt;val) {\nnode-&gt;right = removeHelper(node-&gt;right, val);\n} else {\nif (node-&gt;left == NULL || node-&gt;right == NULL) {\nchild = node-&gt;left;\nif (node-&gt;right != NULL) {\nchild = node-&gt;right;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == NULL) {\nreturn NULL;\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nnode = child;\n}\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\nTreeNode *temp = node-&gt;right;\nwhile (temp-&gt;left != NULL) {\ntemp = temp-&gt;left;\n}\nint tempVal = temp-&gt;val;\nnode-&gt;right = removeHelper(node-&gt;right, temp-&gt;val);\nnode-&gt;val = tempVal;\n}\n}\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nupdateHeight(node);\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.cs<pre><code>/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\nroot = removeHelper(root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? removeHelper(TreeNode? node, int val) {\nif (node == null) return null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val &lt; node.val)\nnode.left = removeHelper(node.left, val);\nelse if (val &gt; node.val)\nnode.right = removeHelper(node.right, val);\nelse {\nif (node.left == null || node.right == null) {\nTreeNode? child = node.left != null ? node.left : node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == null)\nreturn null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse\nnode = child;\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\nTreeNode? temp = node.right;\nwhile (temp.left != null) {\ntemp = temp.left;\n}\nnode.right = removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nupdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.swift<pre><code>/* \u5220\u9664\u8282\u70b9 */\nfunc remove(val: Int) {\nroot = removeHelper(node: root, val: val)\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc removeHelper(node: TreeNode?, val: Int) -&gt; TreeNode? {\nvar node = node\nif node == nil {\nreturn nil\n}\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif val &lt; node!.val {\nnode?.left = removeHelper(node: node?.left, val: val)\n} else if val &gt; node!.val {\nnode?.right = removeHelper(node: node?.right, val: val)\n} else {\nif node?.left == nil || node?.right == nil {\nlet child = node?.left != nil ? node?.left : node?.right\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif child == nil {\nreturn nil\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse {\nnode = child\n}\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\nvar temp = node?.right\nwhile temp?.left != nil {\ntemp = temp?.left\n}\nnode?.right = removeHelper(node: node?.right, val: temp!.val)\nnode?.val = temp!.val\n}\n}\nupdateHeight(node: node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node: node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node\n}\n</code></pre> avl_tree.zig<pre><code>// \u5220\u9664\u8282\u70b9\nfn remove(self: *Self, val: T) void {\nself.root = self.removeHelper(self.root, val).?;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\nfn removeHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) ?*inc.TreeNode(T) {\nvar node = node_;\nif (node == null) return null;\n// 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b\nif (val &lt; node.?.val) {\nnode.?.left = self.removeHelper(node.?.left, val);\n} else if (val &gt; node.?.val) {\nnode.?.right = self.removeHelper(node.?.right, val);\n} else {\nif (node.?.left == null or node.?.right == null) {\nvar child = if (node.?.left != null) node.?.left else node.?.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == null) {\nreturn null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n} else {\nnode = child;\n}\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\nvar temp = node.?.right;\nwhile (temp.?.left != null) {\ntemp = temp.?.left;\n}\nnode.?.right = self.removeHelper(node.?.right, temp.?.val);\nnode.?.val = temp.?.val;\n}\n}\nself.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n// 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nnode = self.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.dart<pre><code>/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\nroot = removeHelper(root, val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? removeHelper(TreeNode? node, int val) {\nif (node == null) return null;\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val &lt; node.val)\nnode.left = removeHelper(node.left, val);\nelse if (val &gt; node.val)\nnode.right = removeHelper(node.right, val);\nelse {\nif (node.left == null || node.right == null) {\nTreeNode? child = node.left ?? node.right;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == null)\nreturn null;\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse\nnode = child;\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\nTreeNode? temp = node.right;\nwhile (temp!.left != null) {\ntemp = temp.left;\n}\nnode.right = removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nupdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nreturn node;\n}\n</code></pre> avl_tree.rs<pre><code>/* \u5220\u9664\u8282\u70b9 */\nfn remove(&amp;self, val: i32) {\nSelf::remove_helper(self.root.clone(), val);\n}\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn remove_helper(node: OptionTreeNodeRc, val: i32) -&gt; OptionTreeNodeRc {\nmatch node {\nSome(mut node) =&gt; {\n/* 1. \u67e5\u627e\u8282\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif val &lt; node.borrow().val {\nlet left = node.borrow().left.clone();\nnode.borrow_mut().left = Self::remove_helper(left, val);\n} else if val &gt; node.borrow().val {\nlet right = node.borrow().right.clone();\nnode.borrow_mut().right = Self::remove_helper(right, val);\n} else if node.borrow().left.is_none() || node.borrow().right.is_none() {\nlet child = if node.borrow().left.is_some() {\nnode.borrow().left.clone()\n} else {\nnode.borrow().right.clone()\n};\nmatch child {\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nNone =&gt; {\nreturn None;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nSome(child) =&gt; node = child,\n}\n} else {\n// \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\nlet mut temp = node.borrow().right.clone().unwrap();\nloop {\nlet temp_left = temp.borrow().left.clone();\nif temp_left.is_none() {\nbreak;\n}\ntemp = temp_left.unwrap();\n}\nlet right = node.borrow().right.clone();\nnode.borrow_mut().right = Self::remove_helper(right, temp.borrow().val);\nnode.borrow_mut().val = temp.borrow().val;\n}\nSelf::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = Self::rotate(Some(node)).unwrap();\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\nSome(node)\n}\nNone =&gt; None,\n}\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#_10","title":"\u67e5\u627e\u8282\u70b9","text":"<p>AVL \u6811\u7684\u8282\u70b9\u67e5\u627e\u64cd\u4f5c\u4e0e\u4e8c\u53c9\u641c\u7d22\u6811\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002</p>"},{"location":"chapter_tree/avl_tree/#754-avl","title":"7.5.4. \u00a0 AVL \u6811\u5178\u578b\u5e94\u7528","text":"<ul> <li>\u7ec4\u7ec7\u548c\u5b58\u50a8\u5927\u578b\u6570\u636e\uff0c\u9002\u7528\u4e8e\u9ad8\u9891\u67e5\u627e\u3001\u4f4e\u9891\u589e\u5220\u7684\u573a\u666f\u3002</li> <li>\u7528\u4e8e\u6784\u5efa\u6570\u636e\u5e93\u4e2d\u7684\u7d22\u5f15\u7cfb\u7edf\u3002</li> </ul> <p>\u4e3a\u4ec0\u4e48\u7ea2\u9ed1\u6811\u6bd4 AVL \u6811\u66f4\u53d7\u6b22\u8fce\uff1f</p> <p>\u7ea2\u9ed1\u6811\u7684\u5e73\u8861\u6761\u4ef6\u76f8\u5bf9\u5bbd\u677e\uff0c\u56e0\u6b64\u5728\u7ea2\u9ed1\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u6240\u9700\u7684\u65cb\u8f6c\u64cd\u4f5c\u76f8\u5bf9\u8f83\u5c11\uff0c\u5728\u8282\u70b9\u589e\u5220\u64cd\u4f5c\u4e0a\u7684\u5e73\u5747\u6548\u7387\u9ad8\u4e8e AVL \u6811\u3002</p>"},{"location":"chapter_tree/binary_search_tree/","title":"7.4. \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811","text":"<p>\u300c\u4e8c\u53c9\u641c\u7d22\u6811 Binary Search Tree\u300d\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a</p> <ol> <li>\u5bf9\u4e8e\u6839\u8282\u70b9\uff0c\u5de6\u5b50\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u503c \\(&lt;\\) \u6839\u8282\u70b9\u7684\u503c \\(&lt;\\) \u53f3\u5b50\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u503c\u3002</li> <li>\u4efb\u610f\u8282\u70b9\u7684\u5de6\u3001\u53f3\u5b50\u6811\u4e5f\u662f\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u5373\u540c\u6837\u6ee1\u8db3\u6761\u4ef6 <code>1.</code> \u3002</li> </ol> <p></p> <p> \u56fe\uff1a\u4e8c\u53c9\u641c\u7d22\u6811 </p>"},{"location":"chapter_tree/binary_search_tree/#741","title":"7.4.1. \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u64cd\u4f5c","text":"<p>\u6211\u4eec\u5c06\u4e8c\u53c9\u641c\u7d22\u6811\u5c01\u88c5\u4e3a\u4e00\u4e2a\u7c7b <code>ArrayBinaryTree</code> \uff0c\u5e76\u58f0\u660e\u4e00\u4e2a\u6210\u5458\u53d8\u91cf <code>root</code> \uff0c\u6307\u5411\u6811\u7684\u6839\u8282\u70b9\u3002</p>"},{"location":"chapter_tree/binary_search_tree/#_1","title":"\u67e5\u627e\u8282\u70b9","text":"<p>\u7ed9\u5b9a\u76ee\u6807\u8282\u70b9\u503c <code>num</code> \uff0c\u53ef\u4ee5\u6839\u636e\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6027\u8d28\u6765\u67e5\u627e\u3002\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u8282\u70b9 <code>cur</code> \uff0c\u4ece\u4e8c\u53c9\u6811\u7684\u6839\u8282\u70b9 <code>root</code> \u51fa\u53d1\uff0c\u5faa\u73af\u6bd4\u8f83\u8282\u70b9\u503c <code>cur.val</code> \u548c <code>num</code> \u4e4b\u95f4\u7684\u5927\u5c0f\u5173\u7cfb</p> <ul> <li>\u82e5 <code>cur.val &lt; num</code> \uff0c\u8bf4\u660e\u76ee\u6807\u8282\u70b9\u5728 <code>cur</code> \u7684\u53f3\u5b50\u6811\u4e2d\uff0c\u56e0\u6b64\u6267\u884c <code>cur = cur.right</code> \u3002</li> <li>\u82e5 <code>cur.val &gt; num</code> \uff0c\u8bf4\u660e\u76ee\u6807\u8282\u70b9\u5728 <code>cur</code> \u7684\u5de6\u5b50\u6811\u4e2d\uff0c\u56e0\u6b64\u6267\u884c <code>cur = cur.left</code> \u3002</li> <li>\u82e5 <code>cur.val = num</code> \uff0c\u8bf4\u660e\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\u5e76\u8fd4\u56de\u8be5\u8282\u70b9\u3002</li> </ul> &lt;1&gt;&lt;2&gt;&lt;3&gt;&lt;4&gt; <p></p> <p></p> <p></p> <p></p> <p> \u56fe\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u67e5\u627e\u8282\u70b9\u793a\u4f8b </p> <p>\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u67e5\u627e\u64cd\u4f5c\u4e0e\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u4e00\u81f4\uff0c\u90fd\u662f\u6bcf\u8f6e\u6392\u9664\u4e00\u534a\u60c5\u51b5\u3002\u5faa\u73af\u6b21\u6570\u6700\u591a\u4e3a\u4e8c\u53c9\u6811\u7684\u9ad8\u5ea6\uff0c\u5f53\u4e8c\u53c9\u6811\u5e73\u8861\u65f6\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust binary_search_tree.java<pre><code>/* \u67e5\u627e\u8282\u70b9 */\nTreeNode search(int num) {\nTreeNode cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val &lt; num)\ncur = cur.right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur.val &gt; num)\ncur = cur.left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse\nbreak;\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n</code></pre> binary_search_tree.cpp<pre><code>/* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(int num) {\nTreeNode *cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != nullptr) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur-&gt;val &lt; num)\ncur = cur-&gt;right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur-&gt;val &gt; num)\ncur = cur-&gt;left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse\nbreak;\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n</code></pre> binary_search_tree.py<pre><code>def search(self, num: int) -&gt; TreeNode | None:\n\"\"\"\u67e5\u627e\u8282\u70b9\"\"\"\ncur: TreeNode | None = self.root\n# \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile cur is not None:\n# \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur.val &lt; num:\ncur = cur.right\n# \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelif cur.val &gt; num:\ncur = cur.left\n# \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse:\nbreak\nreturn cur\n</code></pre> binary_search_tree.go<pre><code>/* \u67e5\u627e\u8282\u70b9 */\nfunc (bst *binarySearchTree) search(num int) *TreeNode {\nnode := bst.root\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nfor node != nil {\nif node.Val.(int) &lt; num {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nnode = node.Right\n} else if node.Val.(int) &gt; num {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nnode = node.Left\n} else {\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nbreak\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn node\n}\n</code></pre> binary_search_tree.js<pre><code>/* \u67e5\u627e\u8282\u70b9 */\nfunction search(num) {\nlet cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val &lt; num) cur = cur.right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur.val &gt; num) cur = cur.left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse break;\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n</code></pre> binary_search_tree.ts<pre><code>/* \u67e5\u627e\u8282\u70b9 */\nfunction search(num: number): TreeNode | null {\nlet cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\nif (cur.val &lt; num) {\ncur = cur.right; // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n} else if (cur.val &gt; num) {\ncur = cur.left; // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else {\nbreak; // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n</code></pre> binary_search_tree.c<pre><code>/* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(binarySearchTree *bst, int num) {\nTreeNode *cur = bst-&gt;root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != NULL) {\nif (cur-&gt;val &lt; num) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\ncur = cur-&gt;right;\n} else if (cur-&gt;val &gt; num) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\ncur = cur-&gt;left;\n} else {\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nbreak;\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n</code></pre> binary_search_tree.cs<pre><code>/* \u67e5\u627e\u8282\u70b9 */\nTreeNode? search(int num) {\nTreeNode? cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val &lt; num) cur =\ncur.right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur.val &gt; num)\ncur = cur.left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse\nbreak;\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n</code></pre> binary_search_tree.swift<pre><code>/* \u67e5\u627e\u8282\u70b9 */\nfunc search(num: Int) -&gt; TreeNode? {\nvar cur = root\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile cur != nil {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur!.val &lt; num {\ncur = cur?.right\n}\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if cur!.val &gt; num {\ncur = cur?.left\n}\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse {\nbreak\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur\n}\n</code></pre> binary_search_tree.zig<pre><code>// \u67e5\u627e\u8282\u70b9\nfn search(self: *Self, num: T) ?*inc.TreeNode(T) {\nvar cur = self.root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.?.val &lt; num) {\ncur = cur.?.right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else if (cur.?.val &gt; num) {\ncur = cur.?.left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n} else {\nbreak;\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n</code></pre> binary_search_tree.dart<pre><code>/* \u67e5\u627e\u8282\u70b9 */\nTreeNode? search(int num) {\nTreeNode? cur = _root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val &lt; num)\ncur = cur.right;\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur.val &gt; num)\ncur = cur.left;\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse\nbreak;\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\nreturn cur;\n}\n</code></pre> binary_search_tree.rs<pre><code>/* \u67e5\u627e\u8282\u70b9 */\npub fn search(&amp;self, num: i32) -&gt; Option&lt;TreeNodeRc&gt; {\nlet mut cur = self.root.clone();\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile let Some(node) = cur.clone() {\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif node.borrow().val &lt; num {\ncur = node.borrow().right.clone();\n}\n// \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if node.borrow().val &gt; num {\ncur = node.borrow().left.clone();\n}\n// \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse {\nbreak;\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u8282\u70b9\ncur\n}\n</code></pre>"},{"location":"chapter_tree/binary_search_tree/#_2","title":"\u63d2\u5165\u8282\u70b9","text":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u5f85\u63d2\u5165\u5143\u7d20 <code>num</code> \uff0c\u4e3a\u4e86\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u201c\u5de6\u5b50\u6811 &lt; \u6839\u8282\u70b9 &lt; \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\uff0c\u63d2\u5165\u64cd\u4f5c\u5206\u4e3a\u4e24\u6b65\uff1a</p> <ol> <li>\u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff1a\u4e0e\u67e5\u627e\u64cd\u4f5c\u76f8\u4f3c\uff0c\u4ece\u6839\u8282\u70b9\u51fa\u53d1\uff0c\u6839\u636e\u5f53\u524d\u8282\u70b9\u503c\u548c <code>num</code> \u7684\u5927\u5c0f\u5173\u7cfb\u5faa\u73af\u5411\u4e0b\u641c\u7d22\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\uff08\u904d\u5386\u81f3 \\(\\text{None}\\) \uff09\u65f6\u8df3\u51fa\u5faa\u73af\u3002</li> <li>\u5728\u8be5\u4f4d\u7f6e\u63d2\u5165\u8282\u70b9\uff1a\u521d\u59cb\u5316\u8282\u70b9 <code>num</code> \uff0c\u5c06\u8be5\u8282\u70b9\u7f6e\u4e8e \\(\\text{None}\\) \u7684\u4f4d\u7f6e\u3002</li> </ol> <p>\u4e8c\u53c9\u641c\u7d22\u6811\u4e0d\u5141\u8bb8\u5b58\u5728\u91cd\u590d\u8282\u70b9\uff0c\u5426\u5219\u5c06\u8fdd\u53cd\u5176\u5b9a\u4e49\u3002\u56e0\u6b64\uff0c\u82e5\u5f85\u63d2\u5165\u8282\u70b9\u5728\u6811\u4e2d\u5df2\u5b58\u5728\uff0c\u5219\u4e0d\u6267\u884c\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\u3002</p> <p></p> <p> \u56fe\uff1a\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u63d2\u5165\u8282\u70b9 </p> JavaC++PythonGoJSTSCC#SwiftZigDartRust binary_search_tree.java<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == null)\nreturn;\nTreeNode cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.val == num)\nreturn;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val &lt; num)\ncur = cur.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur.left;\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode node = new TreeNode(num);\nif (pre.val &lt; num)\npre.right = node;\nelse\npre.left = node;\n}\n</code></pre> binary_search_tree.cpp<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == nullptr)\nreturn;\nTreeNode *cur = root, *pre = nullptr;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != nullptr) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur-&gt;val == num)\nreturn;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur-&gt;val &lt; num)\ncur = cur-&gt;right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur-&gt;left;\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode *node = new TreeNode(num);\nif (pre-&gt;val &lt; num)\npre-&gt;right = node;\nelse\npre-&gt;left = node;\n}\n</code></pre> binary_search_tree.py<pre><code>def insert(self, num: int):\n\"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n# \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif self.root is None:\nreturn\n# \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\ncur, pre = self.root, None\nwhile cur is not None:\n# \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif cur.val == num:\nreturn\npre = cur\n# \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur.val &lt; num:\ncur = cur.right\n# \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse:\ncur = cur.left\n# \u63d2\u5165\u8282\u70b9\nnode = TreeNode(num)\nif pre.val &lt; num:\npre.right = node\nelse:\npre.left = node\n</code></pre> binary_search_tree.go<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nfunc (bst *binarySearchTree) insert(num int) {\ncur := bst.root\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif cur == nil {\nreturn\n}\n// \u5f85\u63d2\u5165\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\nvar pre *TreeNode = nil\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nfor cur != nil {\nif cur.Val == num {\nreturn\n}\npre = cur\nif cur.Val.(int) &lt; num {\ncur = cur.Right\n} else {\ncur = cur.Left\n}\n}\n// \u63d2\u5165\u8282\u70b9\nnode := NewTreeNode(num)\nif pre.Val.(int) &lt; num {\npre.Right = node\n} else {\npre.Left = node\n}\n}\n</code></pre> binary_search_tree.js<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nfunction insert(num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) return;\nlet cur = root,\npre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.val === num) return;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val &lt; num) cur = cur.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse cur = cur.left;\n}\n// \u63d2\u5165\u8282\u70b9\nlet node = new TreeNode(num);\nif (pre.val &lt; num) pre.right = node;\nelse pre.left = node;\n}\n</code></pre> binary_search_tree.ts<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nfunction insert(num: number): void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) {\nreturn;\n}\nlet cur = root,\npre: TreeNode | null = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\nif (cur.val === num) {\nreturn; // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n}\npre = cur;\nif (cur.val &lt; num) {\ncur = cur.right as TreeNode; // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n} else {\ncur = cur.left as TreeNode; // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n}\n}\n// \u63d2\u5165\u8282\u70b9\nlet node = new TreeNode(num);\nif (pre!.val &lt; num) {\npre!.right = node;\n} else {\npre!.left = node;\n}\n}\n</code></pre> binary_search_tree.c<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(binarySearchTree *bst, int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (bst-&gt;root == NULL)\nreturn;\nTreeNode *cur = bst-&gt;root, *pre = NULL;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != NULL) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur-&gt;val == num) {\nreturn;\n}\npre = cur;\nif (cur-&gt;val &lt; num) {\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\ncur = cur-&gt;right;\n} else {\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\ncur = cur-&gt;left;\n}\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode *node = newTreeNode(num);\nif (pre-&gt;val &lt; num) {\npre-&gt;right = node;\n} else {\npre-&gt;left = node;\n}\n}\n</code></pre> binary_search_tree.cs<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == null)\nreturn;\nTreeNode? cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.val == num)\nreturn;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val &lt; num)\ncur = cur.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur.left;\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode node = new TreeNode(num);\nif (pre != null) {\nif (pre.val &lt; num)\npre.right = node;\nelse\npre.left = node;\n}\n}\n</code></pre> binary_search_tree.swift<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nfunc insert(num: Int) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif root == nil {\nreturn\n}\nvar cur = root\nvar pre: TreeNode?\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile cur != nil {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif cur!.val == num {\nreturn\n}\npre = cur\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur!.val &lt; num {\ncur = cur?.right\n}\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse {\ncur = cur?.left\n}\n}\n// \u63d2\u5165\u8282\u70b9\nlet node = TreeNode(x: num)\nif pre!.val &lt; num {\npre?.right = node\n} else {\npre?.left = node\n}\n}\n</code></pre> binary_search_tree.zig<pre><code>// \u63d2\u5165\u8282\u70b9\nfn insert(self: *Self, num: T) !void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (self.root == null) return;\nvar cur = self.root;\nvar pre: ?*inc.TreeNode(T) = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.?.val == num) return;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.?.val &lt; num) {\ncur = cur.?.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else {\ncur = cur.?.left;\n}\n}\n// \u63d2\u5165\u8282\u70b9\nvar node = try self.mem_allocator.create(inc.TreeNode(T));\nnode.init(num);\nif (pre.?.val &lt; num) {\npre.?.right = node;\n} else {\npre.?.left = node;\n}\n}\n</code></pre> binary_search_tree.dart<pre><code>/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (_root == null) return;\nTreeNode? cur = _root;\nTreeNode? pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.val == num) return;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val &lt; num)\ncur = cur.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur.left;\n}\n// \u63d2\u5165\u8282\u70b9\nTreeNode? node = TreeNode(num);\nif (pre!.val &lt; num)\npre.right = node;\nelse\npre.left = node;\n}\n</code></pre> binary_search_tree.rs<pre><code>/* \u63d2\u5165\u8282\u70b9 */\npub fn insert(&amp;mut self, num: i32) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif self.root.is_none() {\nreturn;\n}\nlet mut cur = self.root.clone();\nlet mut pre = None;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile let Some(node) = cur.clone() {\n// \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif node.borrow().val == num {\nreturn;\n}\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\npre = cur.clone();\nif node.borrow().val &lt; num {\ncur = node.borrow().right.clone();\n}\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse {\ncur = node.borrow().left.clone();\n}\n}\n// \u63d2\u5165\u8282\u70b9\nlet node = TreeNode::new(num);\nlet pre = pre.unwrap();\nif pre.borrow().val &lt; num {\npre.borrow_mut().right = Some(Rc::clone(&amp;node));\n} else {\npre.borrow_mut().left = Some(Rc::clone(&amp;node));\n}\n}\n</code></pre> <p>\u4e3a\u4e86\u63d2\u5165\u8282\u70b9\uff0c\u6211\u4eec\u9700\u8981\u5229\u7528\u8f85\u52a9\u8282\u70b9 <code>pre</code> \u4fdd\u5b58\u4e0a\u4e00\u8f6e\u5faa\u73af\u7684\u8282\u70b9\uff0c\u8fd9\u6837\u5728\u904d\u5386\u81f3 \\(\\text{None}\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5230\u5176\u7236\u8282\u70b9\uff0c\u4ece\u800c\u5b8c\u6210\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u3002</p> <p>\u4e0e\u67e5\u627e\u8282\u70b9\u76f8\u540c\uff0c\u63d2\u5165\u8282\u70b9\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002</p>"},{"location":"chapter_tree/binary_search_tree/#_3","title":"\u5220\u9664\u8282\u70b9","text":"<p>\u4e0e\u63d2\u5165\u8282\u70b9\u7c7b\u4f3c\uff0c\u6211\u4eec\u9700\u8981\u5728\u5220\u9664\u64cd\u4f5c\u540e\u7ef4\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u201c\u5de6\u5b50\u6811 &lt; \u6839\u8282\u70b9 &lt; \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u4e8c\u53c9\u6811\u4e2d\u6267\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u83b7\u53d6\u5f85\u5220\u9664\u8282\u70b9\u3002\u63a5\u4e0b\u6765\uff0c\u6839\u636e\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf\uff0c\u5220\u9664\u64cd\u4f5c\u9700\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\uff1a</p> <p>\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \u65f6\uff0c\u8868\u793a\u5f85\u5220\u9664\u8282\u70b9\u662f\u53f6\u8282\u70b9\uff0c\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u3002</p> <p></p> <p> \u56fe\uff1a\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 0\uff09 </p> <p>\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(1\\) \u65f6\uff0c\u5c06\u5f85\u5220\u9664\u8282\u70b9\u66ff\u6362\u4e3a\u5176\u5b50\u8282\u70b9\u5373\u53ef\u3002</p> <p></p> <p> \u56fe\uff1a\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 1\uff09 </p> <p>\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(2\\) \u65f6\uff0c\u6211\u4eec\u65e0\u6cd5\u76f4\u63a5\u5220\u9664\u5b83\uff0c\u800c\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u8282\u70b9\u66ff\u6362\u8be5\u8282\u70b9\u3002\u7531\u4e8e\u8981\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u201c\u5de6 \\(&lt;\\) \u6839 \\(&lt;\\) \u53f3\u201d\u7684\u6027\u8d28\uff0c\u56e0\u6b64\u8fd9\u4e2a\u8282\u70b9\u53ef\u4ee5\u662f\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\u6216\u5de6\u5b50\u6811\u7684\u6700\u5927\u8282\u70b9\u3002</p> <p>\u5047\u8bbe\u6211\u4eec\u9009\u62e9\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\uff08\u5373\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\uff09\uff0c\u5219\u5220\u9664\u64cd\u4f5c\u4e3a\uff1a</p> <ol> <li>\u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\u5728\u201c\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u201d\u4e2d\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\uff0c\u8bb0\u4e3a <code>tmp</code> \u3002</li> <li>\u5c06 <code>tmp</code> \u7684\u503c\u8986\u76d6\u5f85\u5220\u9664\u8282\u70b9\u7684\u503c\uff0c\u5e76\u5728\u6811\u4e2d\u9012\u5f52\u5220\u9664\u8282\u70b9 <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> \u56fe\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u5220\u9664\u8282\u70b9\u793a\u4f8b </p> <p>\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u540c\u6837\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d\u67e5\u627e\u5f85\u5220\u9664\u8282\u70b9\u9700\u8981 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u540e\u7ee7\u8282\u70b9\u9700\u8981 \\(O(\\log n)\\) \u65f6\u95f4\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDart binary_search_tree.java<pre><code>/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == null)\nreturn;\nTreeNode cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val == num)\nbreak;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val &lt; num)\ncur = cur.right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur.left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == null)\nreturn;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.left == null || cur.right == null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nTreeNode child = cur.left != null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre.left == cur)\npre.left = child;\nelse\npre.right = child;\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nTreeNode tmp = cur.right;\nwhile (tmp.left != null) {\ntmp = tmp.left;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val;\n}\n}\n</code></pre> binary_search_tree.cpp<pre><code>/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == nullptr)\nreturn;\nTreeNode *cur = root, *pre = nullptr;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != nullptr) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur-&gt;val == num)\nbreak;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur-&gt;val &lt; num)\ncur = cur-&gt;right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur-&gt;left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == nullptr)\nreturn;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur-&gt;left == nullptr || cur-&gt;right == nullptr) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\nTreeNode *child = cur-&gt;left != nullptr ? cur-&gt;left : cur-&gt;right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre-&gt;left == cur)\npre-&gt;left = child;\nelse\npre-&gt;right = child;\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n// \u91ca\u653e\u5185\u5b58\ndelete cur;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nTreeNode *tmp = cur-&gt;right;\nwhile (tmp-&gt;left != nullptr) {\ntmp = tmp-&gt;left;\n}\nint tmpVal = tmp-&gt;val;\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp-&gt;val);\n// \u7528 tmp \u8986\u76d6 cur\ncur-&gt;val = tmpVal;\n}\n}\n</code></pre> binary_search_tree.py<pre><code>def remove(self, num: int):\n\"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n# \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif self.root is None:\nreturn\n# \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\ncur, pre = self.root, None\nwhile cur is not None:\n# \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif cur.val == num:\nbreak\npre = cur\n# \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur.val &lt; num:\ncur = cur.right\n# \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse:\ncur = cur.left\n# \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur is None:\nreturn\n# \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif cur.left is None or cur.right is None:\n# \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nchild = cur.left or cur.right\n# \u5220\u9664\u8282\u70b9 cur\nif cur != self.root:\nif pre.left == cur:\npre.left = child\nelse:\npre.right = child\nelse:\n# \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nself.root = child\n# \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse:\n# \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\ntmp: TreeNode = cur.right\nwhile tmp.left is not None:\ntmp = tmp.left\n# \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nself.remove(tmp.val)\n# \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val\n</code></pre> binary_search_tree.go<pre><code>/* \u5220\u9664\u8282\u70b9 */\nfunc (bst *binarySearchTree) remove(num int) {\ncur := bst.root\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif cur == nil {\nreturn\n}\n// \u5f85\u5220\u9664\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\nvar pre *TreeNode = nil\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nfor cur != nil {\nif cur.Val == num {\nbreak\n}\npre = cur\nif cur.Val.(int) &lt; num {\n// \u5f85\u5220\u9664\u8282\u70b9\u5728\u53f3\u5b50\u6811\u4e2d\ncur = cur.Right\n} else {\n// \u5f85\u5220\u9664\u8282\u70b9\u5728\u5de6\u5b50\u6811\u4e2d\ncur = cur.Left\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur == nil {\nreturn\n}\n// \u5b50\u8282\u70b9\u6570\u4e3a 0 \u6216 1\nif cur.Left == nil || cur.Right == nil {\nvar child *TreeNode = nil\n// \u53d6\u51fa\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\nif cur.Left != nil {\nchild = cur.Left\n} else {\nchild = cur.Right\n}\n// \u5220\u9664\u8282\u70b9 cur\nif cur != bst.root {\nif pre.Left == cur {\npre.Left = child\n} else {\npre.Right = child\n}\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nbst.root = child\n}\n// \u5b50\u8282\u70b9\u6570\u4e3a 2\n} else {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u5f85\u5220\u9664\u8282\u70b9 cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\ntmp := cur.Right\nfor tmp.Left != nil {\ntmp = tmp.Left\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nbst.remove(tmp.Val.(int))\n// \u7528 tmp \u8986\u76d6 cur\ncur.Val = tmp.Val\n}\n}\n</code></pre> binary_search_tree.js<pre><code>/* \u5220\u9664\u8282\u70b9 */\nfunction remove(num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) return;\nlet cur = root,\npre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val === num) break;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val &lt; num) cur = cur.right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse cur = cur.left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur === null) return;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.left === null || cur.right === null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nlet child = cur.left !== null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre.left === cur) pre.left = child;\nelse pre.right = child;\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nlet tmp = cur.right;\nwhile (tmp.left !== null) {\ntmp = tmp.left;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val;\n}\n}\n</code></pre> binary_search_tree.ts<pre><code>/* \u5220\u9664\u8282\u70b9 */\nfunction remove(num: number): void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) {\nreturn;\n}\nlet cur = root,\npre: TreeNode | null = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val === num) {\nbreak;\n}\npre = cur;\nif (cur.val &lt; num) {\ncur = cur.right as TreeNode; // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n} else {\ncur = cur.left as TreeNode; // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur === null) {\nreturn;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.left === null || cur.right === null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nlet child = cur.left !== null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre!.left === cur) {\npre!.left = child;\n} else {\npre!.right = child;\n}\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nlet tmp = cur.right;\nwhile (tmp.left !== null) {\ntmp = tmp.left;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp!.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val;\n}\n}\n</code></pre> binary_search_tree.c<pre><code>/* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeNode(binarySearchTree *bst, int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (bst-&gt;root == NULL)\nreturn;\nTreeNode *cur = bst-&gt;root, *pre = NULL;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != NULL) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur-&gt;val == num)\nbreak;\npre = cur;\nif (cur-&gt;val &lt; num) {\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u53f3\u5b50\u6811\u4e2d\ncur = cur-&gt;right;\n} else {\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u5de6\u5b50\u6811\u4e2d\ncur = cur-&gt;left;\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == NULL)\nreturn;\n// \u5224\u65ad\u5f85\u5220\u9664\u8282\u70b9\u662f\u5426\u5b58\u5728\u5b50\u8282\u70b9\nif (cur-&gt;left == NULL || cur-&gt;right == NULL) {\n/* \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1 */\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\nTreeNode *child = cur-&gt;left != NULL ? cur-&gt;left : cur-&gt;right;\n// \u5220\u9664\u8282\u70b9 cur\nif (pre-&gt;left == cur) {\npre-&gt;left = child;\n} else {\npre-&gt;right = child;\n}\n} else {\n/* \u5b50\u8282\u70b9\u6570\u91cf = 2 */\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nTreeNode *tmp = cur-&gt;right;\nwhile (tmp-&gt;left != NULL) {\ntmp = tmp-&gt;left;\n}\nint tmpVal = tmp-&gt;val;\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremoveNode(bst, tmp-&gt;val);\n// \u7528 tmp \u8986\u76d6 cur\ncur-&gt;val = tmpVal;\n}\n}\n</code></pre> binary_search_tree.cs<pre><code>/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == null)\nreturn;\nTreeNode? cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val == num)\nbreak;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val &lt; num)\ncur = cur.right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse\ncur = cur.left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == null || pre == null)\nreturn;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.left == null || cur.right == null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nTreeNode? child = cur.left != null ? cur.left : cur.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (cur != root) {\nif (pre.left == cur)\npre.left = child;\nelse\npre.right = child;\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nTreeNode? tmp = cur.right;\nwhile (tmp.left != null) {\ntmp = tmp.left;\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(tmp.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.val = tmp.val;\n}\n}\n</code></pre> binary_search_tree.swift<pre><code>/* \u5220\u9664\u8282\u70b9 */\nfunc remove(num: Int) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif root == nil {\nreturn\n}\nvar cur = root\nvar pre: TreeNode?\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile cur != nil {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif cur!.val == num {\nbreak\n}\npre = cur\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur!.val &lt; num {\ncur = cur?.right\n}\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse {\ncur = cur?.left\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur == nil {\nreturn\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif cur?.left == nil || cur?.right == nil {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nlet child = cur?.left != nil ? cur?.left : cur?.right\n// \u5220\u9664\u8282\u70b9 cur\nif cur !== root {\nif pre?.left === cur {\npre?.left = child\n} else {\npre?.right = child\n}\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nroot = child\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nvar tmp = cur?.right\nwhile tmp?.left != nil {\ntmp = tmp?.left\n}\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nremove(num: tmp!.val)\n// \u7528 tmp \u8986\u76d6 cur\ncur?.val = tmp!.val\n}\n}\n</code></pre> binary_search_tree.zig<pre><code>// \u5220\u9664\u8282\u70b9\nfn remove(self: *Self, num: T) void {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (self.root == null) return;\nvar cur = self.root;\nvar pre: ?*inc.TreeNode(T) = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.?.val == num) break;\npre = cur;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.?.val &lt; num) {\ncur = cur.?.right;\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else {\ncur = cur.?.left;\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == null) return;\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif (cur.?.left == null or cur.?.right == null) {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\nvar child = if (cur.?.left != null) cur.?.left else cur.?.right;\n// \u5220\u9664\u8282\u70b9 cur\nif (pre.?.left == cur) {\npre.?.left = child;\n} else {\npre.?.right = child;\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\n} else {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nvar tmp = cur.?.right;\nwhile (tmp.?.left != null) {\ntmp = tmp.?.left;\n}\nvar tmp_val = tmp.?.val;\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nself.remove(tmp.?.val);\n// \u7528 tmp \u8986\u76d6 cur\ncur.?.val = tmp_val;\n}\n}\n</code></pre> <p>```dart title=\"binary_search_tree.dart\" /* \u63d2\u5165\u8282\u70b9 */ void insert(int num) { // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de if (_root == null) return; TreeNode? cur = _root; TreeNode? pre = null; // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa while (cur != null) { // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de if (cur.val == num) return; pre = cur; // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d if (cur.val &lt; num) cur = cur.right; // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d else cur = cur.left; } // \u63d2\u5165\u8282\u70b9 TreeNode? node = TreeNode(num); if (pre!.val &lt; num) pre.right = node; else pre.left = node; }</p> <p>/* \u5220\u9664\u8282\u70b9 */ void remove(int num) { // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de if (_root == null) return;</p> <pre><code> TreeNode? cur = _root;\n TreeNode? pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val == num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val &lt; num)\n cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur == null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n TreeNode? child = cur.left ?? cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != _root) {\n if (pre!.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n _root = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode? tmp = cur.right;\n while (tmp!.left != null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n```\n</code></pre> Rust binary_search_tree.rs<pre><code>/* \u5220\u9664\u8282\u70b9 */\npub fn remove(&amp;mut self, num: i32) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif self.root.is_none() { return; }\nlet mut cur = self.root.clone();\nlet mut pre = None;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\nwhile let Some(node) = cur.clone() {\n// \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif node.borrow().val == num {\nbreak;\n}\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\npre = cur.clone();\nif node.borrow().val &lt; num {\ncur = node.borrow().right.clone();\n}\n// \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse {\ncur = node.borrow().left.clone();\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur.is_none() {\nreturn;\n}\nlet cur = cur.unwrap();\n// \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\nif cur.borrow().left.is_none() || cur.borrow().right.is_none() {\n// \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\nlet child = cur.borrow().left.clone().or_else(|| cur.borrow().right.clone());\nlet pre = pre.unwrap();\nlet left = pre.borrow().left.clone().unwrap();\n// \u5220\u9664\u8282\u70b9 cur\nif !Rc::ptr_eq(&amp;cur, self.root.as_ref().unwrap()) {\nif Rc::ptr_eq(&amp;left, &amp;cur) {\npre.borrow_mut().left = child;\n} else {\npre.borrow_mut().right = child;\n}\n} else {\n// \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\nself.root = child;\n}\n}\n// \u5b50\u8282\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\nlet mut tmp = cur.borrow().right.clone();\nwhile let Some(node) = tmp.clone() {\nif node.borrow().left.is_some() {\ntmp = node.borrow().left.clone();\n} else {\nbreak;\n}\n}\nlet tmpval = tmp.unwrap().borrow().val;\n// \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\nself.remove(tmpval);\n// \u7528 tmp \u8986\u76d6 cur\ncur.borrow_mut().val = tmpval;\n}\n}\n</code></pre>"},{"location":"chapter_tree/binary_search_tree/#_4","title":"\u6392\u5e8f","text":"<p>\u6211\u4eec\u77e5\u9053\uff0c\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u9075\u5faa\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u7684\u904d\u5386\u987a\u5e8f\uff0c\u800c\u4e8c\u53c9\u641c\u7d22\u6811\u6ee1\u8db3\u201c\u5de6\u5b50\u8282\u70b9 \\(&lt;\\) \u6839\u8282\u70b9 \\(&lt;\\) \u53f3\u5b50\u8282\u70b9\u201d\u7684\u5927\u5c0f\u5173\u7cfb\u3002\u56e0\u6b64\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u8fdb\u884c\u4e2d\u5e8f\u904d\u5386\u65f6\uff0c\u603b\u662f\u4f1a\u4f18\u5148\u904d\u5386\u4e0b\u4e00\u4e2a\u6700\u5c0f\u8282\u70b9\uff0c\u4ece\u800c\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u6027\u8d28\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u662f\u5347\u5e8f\u7684\u3002</p> <p>\u5229\u7528\u4e2d\u5e8f\u904d\u5386\u5347\u5e8f\u7684\u6027\u8d28\uff0c\u6211\u4eec\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u83b7\u53d6\u6709\u5e8f\u6570\u636e\u4ec5\u9700 \\(O(n)\\) \u65f6\u95f4\uff0c\u65e0\u9700\u989d\u5916\u6392\u5e8f\uff0c\u975e\u5e38\u9ad8\u6548\u3002</p> <p></p> <p> \u56fe\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217 </p>"},{"location":"chapter_tree/binary_search_tree/#742","title":"7.4.2. \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387","text":"<p>\u7ed9\u5b9a\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u8003\u8651\u4f7f\u7528\u6570\u7ec4\u6216\u4e8c\u53c9\u641c\u7d22\u6811\u5b58\u50a8\u3002</p> <p>\u89c2\u5bdf\u53ef\u77e5\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5404\u9879\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f\u5bf9\u6570\u9636\uff0c\u5177\u6709\u7a33\u5b9a\u4e14\u9ad8\u6548\u7684\u6027\u80fd\u8868\u73b0\u3002\u53ea\u6709\u5728\u9ad8\u9891\u6dfb\u52a0\u3001\u4f4e\u9891\u67e5\u627e\u5220\u9664\u7684\u6570\u636e\u9002\u7528\u573a\u666f\u4e0b\uff0c\u6570\u7ec4\u6bd4\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387\u66f4\u9ad8\u3002</p> <p> \u8868\uff1a\u6570\u7ec4\u4e0e\u641c\u7d22\u6811\u7684\u6548\u7387\u5bf9\u6bd4 </p> \u65e0\u5e8f\u6570\u7ec4 \u4e8c\u53c9\u641c\u7d22\u6811 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(\\log n)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) <p>\u5728\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u662f\u201c\u5e73\u8861\u201d\u7684\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728 \\(\\log n\\) \u8f6e\u5faa\u73af\u5185\u67e5\u627e\u4efb\u610f\u8282\u70b9\u3002</p> <p>\u7136\u800c\uff0c\u5982\u679c\u6211\u4eec\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u4e0d\u65ad\u5730\u63d2\u5165\u548c\u5220\u9664\u8282\u70b9\uff0c\u53ef\u80fd\u5bfc\u81f4\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u94fe\u8868\uff0c\u8fd9\u65f6\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4f1a\u9000\u5316\u4e3a \\(O(n)\\) \u3002</p> <p></p> <p> \u56fe\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5e73\u8861\u4e0e\u9000\u5316 </p>"},{"location":"chapter_tree/binary_search_tree/#743","title":"7.4.3. \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u5e38\u89c1\u5e94\u7528","text":"<ul> <li>\u7528\u4f5c\u7cfb\u7edf\u4e2d\u7684\u591a\u7ea7\u7d22\u5f15\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u3002</li> <li>\u4f5c\u4e3a\u67d0\u4e9b\u641c\u7d22\u7b97\u6cd5\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002</li> <li>\u7528\u4e8e\u5b58\u50a8\u6570\u636e\u6d41\uff0c\u4ee5\u4fdd\u6301\u5176\u6709\u5e8f\u72b6\u6001\u3002</li> </ul>"},{"location":"chapter_tree/binary_tree/","title":"7.1. \u00a0 \u4e8c\u53c9\u6811","text":"<p>\u300c\u4e8c\u53c9\u6811 Binary Tree\u300d\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4ee3\u8868\u7740\u7956\u5148\u4e0e\u540e\u4ee3\u4e4b\u95f4\u7684\u6d3e\u751f\u5173\u7cfb\uff0c\u4f53\u73b0\u7740\u201c\u4e00\u5206\u4e3a\u4e8c\u201d\u7684\u5206\u6cbb\u903b\u8f91\u3002\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u4e8c\u53c9\u6811\u7684\u57fa\u672c\u5355\u5143\u662f\u8282\u70b9\uff0c\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\uff1a\u503c\u3001\u5de6\u5b50\u8282\u70b9\u5f15\u7528\u3001\u53f3\u5b50\u8282\u70b9\u5f15\u7528\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust <pre><code>/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val; // \u8282\u70b9\u503c\nTreeNode left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode(int x) { val = x; }\n}\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nint val; // \u8282\u70b9\u503c\nTreeNode *left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nTreeNode *right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nTreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n</code></pre> <pre><code>class TreeNode:\n\"\"\"\u4e8c\u53c9\u6811\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val # \u8282\u70b9\u503c\nself.left: Optional[TreeNode] = None # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nself.right: Optional[TreeNode] = None # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\ntype TreeNode struct {\nVal int\nLeft *TreeNode\nRight *TreeNode\n}\n/* \u8282\u70b9\u521d\u59cb\u5316\u65b9\u6cd5 */\nfunc NewTreeNode(v int) *TreeNode {\nreturn &amp;TreeNode{\nLeft: nil, // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nRight: nil, // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nVal: v, // \u8282\u70b9\u503c\n}\n}\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nfunction TreeNode(val, left, right) {\nthis.val = (val === undefined ? 0 : val); // \u8282\u70b9\u503c\nthis.left = (left === undefined ? null : left); // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nthis.right = (right === undefined ? null : right); // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nval: number;\nleft: TreeNode | null;\nright: TreeNode | null;\nconstructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\nthis.val = val === undefined ? 0 : val; // \u8282\u70b9\u503c\nthis.left = left === undefined ? null : left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nthis.right = right === undefined ? null : right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\n}\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nint val; // \u8282\u70b9\u503c\nint height; // \u8282\u70b9\u9ad8\u5ea6\nstruct TreeNode *left; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nstruct TreeNode *right; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\n};\ntypedef struct TreeNode TreeNode;\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\nTreeNode *node;\nnode = (TreeNode *)malloc(sizeof(TreeNode));\nnode-&gt;val = val;\nnode-&gt;height = 0;\nnode-&gt;left = NULL;\nnode-&gt;right = NULL;\nreturn node;\n}\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val; // \u8282\u70b9\u503c\nTreeNode? left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode(int x) { val = x; }\n}\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nvar val: Int // \u8282\u70b9\u503c\nvar left: TreeNode? // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nvar right: TreeNode? // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\ninit(x: Int) {\nval = x\n}\n}\n</code></pre> <pre><code>\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nint val; // \u8282\u70b9\u503c\nTreeNode? left; // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode? right; // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\nTreeNode(this.val, [this.left, this.right]);\n}\n</code></pre> <pre><code>\n</code></pre> <p>\u8282\u70b9\u7684\u4e24\u4e2a\u6307\u9488\u5206\u522b\u6307\u5411\u300c\u5de6\u5b50\u8282\u70b9\u300d\u548c\u300c\u53f3\u5b50\u8282\u70b9\u300d\uff0c\u540c\u65f6\u8be5\u8282\u70b9\u88ab\u79f0\u4e3a\u8fd9\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u300c\u7236\u8282\u70b9\u300d\u3002\u5f53\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u65f6\uff0c\u6211\u4eec\u5c06\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u79f0\u4e3a\u8be5\u8282\u70b9\u7684\u300c\u5de6\u5b50\u6811\u300d\uff0c\u540c\u7406\u53ef\u5f97\u300c\u53f3\u5b50\u6811\u300d\u3002</p> <p>\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u9664\u53f6\u8282\u70b9\u5916\uff0c\u5176\u4ed6\u6240\u6709\u8282\u70b9\u90fd\u5305\u542b\u5b50\u8282\u70b9\u548c\u975e\u7a7a\u5b50\u6811\u3002\u4f8b\u5982\uff0c\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u82e5\u5c06\u201c\u8282\u70b9 2\u201d\u89c6\u4e3a\u7236\u8282\u70b9\uff0c\u5219\u5176\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u5206\u522b\u662f\u201c\u8282\u70b9 4\u201d\u548c\u201c\u8282\u70b9 5\u201d\uff0c\u5de6\u5b50\u6811\u662f\u201c\u8282\u70b9 4 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\uff0c\u53f3\u5b50\u6811\u662f\u201c\u8282\u70b9 5 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\u3002</p> <p></p> <p> \u56fe\uff1a\u7236\u8282\u70b9\u3001\u5b50\u8282\u70b9\u3001\u5b50\u6811 </p>"},{"location":"chapter_tree/binary_tree/#711","title":"7.1.1. \u00a0 \u4e8c\u53c9\u6811\u5e38\u89c1\u672f\u8bed","text":"<p>\u4e8c\u53c9\u6811\u6d89\u53ca\u7684\u672f\u8bed\u8f83\u591a\uff0c\u5efa\u8bae\u5c3d\u91cf\u7406\u89e3\u5e76\u8bb0\u4f4f\u3002</p> <ul> <li>\u300c\u6839\u8282\u70b9 Root Node\u300d\uff1a\u4f4d\u4e8e\u4e8c\u53c9\u6811\u9876\u5c42\u7684\u8282\u70b9\uff0c\u6ca1\u6709\u7236\u8282\u70b9\u3002</li> <li>\u300c\u53f6\u8282\u70b9 Leaf Node\u300d\uff1a\u6ca1\u6709\u5b50\u8282\u70b9\u7684\u8282\u70b9\uff0c\u5176\u4e24\u4e2a\u6307\u9488\u5747\u6307\u5411 \\(\\text{None}\\) \u3002</li> <li>\u8282\u70b9\u7684\u300c\u5c42 Level\u300d\uff1a\u4ece\u9876\u81f3\u5e95\u9012\u589e\uff0c\u6839\u8282\u70b9\u6240\u5728\u5c42\u4e3a 1 \u3002</li> <li>\u8282\u70b9\u7684\u300c\u5ea6 Degree\u300d\uff1a\u8282\u70b9\u7684\u5b50\u8282\u70b9\u7684\u6570\u91cf\u3002\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u5ea6\u7684\u8303\u56f4\u662f 0, 1, 2 \u3002</li> <li>\u300c\u8fb9 Edge\u300d\uff1a\u8fde\u63a5\u4e24\u4e2a\u8282\u70b9\u7684\u7ebf\u6bb5\uff0c\u5373\u8282\u70b9\u6307\u9488\u3002</li> <li>\u4e8c\u53c9\u6811\u7684\u300c\u9ad8\u5ea6\u300d\uff1a\u4ece\u6839\u8282\u70b9\u5230\u6700\u8fdc\u53f6\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u3002</li> <li>\u8282\u70b9\u7684\u300c\u6df1\u5ea6 Depth\u300d \uff1a\u4ece\u6839\u8282\u70b9\u5230\u8be5\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u3002</li> <li>\u8282\u70b9\u7684\u300c\u9ad8\u5ea6 Height\u300d\uff1a\u4ece\u6700\u8fdc\u53f6\u8282\u70b9\u5230\u8be5\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u3002</li> </ul> <p></p> <p> \u56fe\uff1a\u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed </p> <p>\u9ad8\u5ea6\u4e0e\u6df1\u5ea6\u7684\u5b9a\u4e49</p> <p>\u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u901a\u5e38\u5c06\u300c\u9ad8\u5ea6\u300d\u548c\u300c\u6df1\u5ea6\u300d\u5b9a\u4e49\u4e3a\u201c\u8d70\u8fc7\u8fb9\u7684\u6570\u91cf\u201d\uff0c\u4f46\u6709\u4e9b\u9898\u76ee\u6216\u6559\u6750\u53ef\u80fd\u4f1a\u5c06\u5176\u5b9a\u4e49\u4e3a\u201c\u8d70\u8fc7\u8282\u70b9\u7684\u6570\u91cf\u201d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u9ad8\u5ea6\u548c\u6df1\u5ea6\u90fd\u9700\u8981\u52a0 1 \u3002</p>"},{"location":"chapter_tree/binary_tree/#712","title":"7.1.2. \u00a0 \u4e8c\u53c9\u6811\u57fa\u672c\u64cd\u4f5c","text":"<p>\u521d\u59cb\u5316\u4e8c\u53c9\u6811\u3002\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u9996\u5148\u521d\u59cb\u5316\u8282\u70b9\uff0c\u7136\u540e\u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust binary_tree.java<pre><code>// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n</code></pre> binary_tree.cpp<pre><code>/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode* n1 = new TreeNode(1);\nTreeNode* n2 = new TreeNode(2);\nTreeNode* n3 = new TreeNode(3);\nTreeNode* n4 = new TreeNode(4);\nTreeNode* n5 = new TreeNode(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1-&gt;left = n2;\nn1-&gt;right = n3;\nn2-&gt;left = n4;\nn2-&gt;right = n5;\n</code></pre> binary_tree.py<pre><code># \u521d\u59cb\u5316\u4e8c\u53c9\u6811\n# \u521d\u59cb\u5316\u8282\u70b9\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n</code></pre> binary_tree.go<pre><code>/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nn1 := NewTreeNode(1)\nn2 := NewTreeNode(2)\nn3 := NewTreeNode(3)\nn4 := NewTreeNode(4)\nn5 := NewTreeNode(5)\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.Left = n2\nn1.Right = n3\nn2.Left = n4\nn2.Right = n5\n</code></pre> binary_tree.js<pre><code>/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = new TreeNode(1),\nn2 = new TreeNode(2),\nn3 = new TreeNode(3),\nn4 = new TreeNode(4),\nn5 = new TreeNode(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n</code></pre> binary_tree.ts<pre><code>/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = new TreeNode(1),\nn2 = new TreeNode(2),\nn3 = new TreeNode(3),\nn4 = new TreeNode(4),\nn5 = new TreeNode(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n</code></pre> binary_tree.c<pre><code>/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1-&gt;left = n2;\nn1-&gt;right = n3;\nn2-&gt;left = n4;\nn2-&gt;right = n5;\n</code></pre> binary_tree.cs<pre><code>/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n</code></pre> binary_tree.swift<pre><code>// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = TreeNode(x: 1)\nlet n2 = TreeNode(x: 2)\nlet n3 = TreeNode(x: 3)\nlet n4 = TreeNode(x: 4)\nlet n5 = TreeNode(x: 5)\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n</code></pre> binary_tree.zig<pre><code>\n</code></pre> binary_tree.dart<pre><code>/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u8282\u70b9\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n</code></pre> binary_tree.rs<pre><code>\n</code></pre> <p>\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u3002\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u901a\u8fc7\u4fee\u6539\u6307\u9488\u6765\u5b9e\u73b0\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u3002</p> <p></p> <p> \u56fe\uff1a\u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 </p> JavaC++PythonGoJSTSCC#SwiftZigDartRust binary_tree.java<pre><code>TreeNode P = new TreeNode(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n</code></pre> binary_tree.cpp<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode* P = new TreeNode(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1-&gt;left = P;\nP-&gt;left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1-&gt;left = n2;\n</code></pre> binary_tree.py<pre><code># \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\np = TreeNode(0)\n# \u5728 n1 -&gt; n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = p\np.left = n2\n# \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n</code></pre> binary_tree.go<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\n// \u5728 n1 -&gt; n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.Left = n2\n</code></pre> binary_tree.js<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nlet P = new TreeNode(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n</code></pre> binary_tree.ts<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nconst P = new TreeNode(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n</code></pre> binary_tree.c<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode *P = newTreeNode(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1-&gt;left = P;\nP-&gt;left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1-&gt;left = n2;\n</code></pre> binary_tree.cs<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new TreeNode(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n</code></pre> binary_tree.swift<pre><code>let P = TreeNode(x: 0)\n// \u5728 n1 -&gt; n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P\nP.left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n</code></pre> binary_tree.zig<pre><code>\n</code></pre> binary_tree.dart<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new TreeNode(0);\n// \u5728 n1 -&gt; n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n</code></pre> binary_tree.rs<pre><code>\n</code></pre> <p>Note</p> <p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u63d2\u5165\u8282\u70b9\u53ef\u80fd\u4f1a\u6539\u53d8\u4e8c\u53c9\u6811\u7684\u539f\u6709\u903b\u8f91\u7ed3\u6784\uff0c\u800c\u5220\u9664\u8282\u70b9\u901a\u5e38\u610f\u5473\u7740\u5220\u9664\u8be5\u8282\u70b9\u53ca\u5176\u6240\u6709\u5b50\u6811\u3002\u56e0\u6b64\uff0c\u5728\u4e8c\u53c9\u6811\u4e2d\uff0c\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u901a\u5e38\u662f\u7531\u4e00\u5957\u64cd\u4f5c\u914d\u5408\u5b8c\u6210\u7684\uff0c\u4ee5\u5b9e\u73b0\u6709\u5b9e\u9645\u610f\u4e49\u7684\u64cd\u4f5c\u3002</p>"},{"location":"chapter_tree/binary_tree/#713","title":"7.1.3. \u00a0 \u5e38\u89c1\u4e8c\u53c9\u6811\u7c7b\u578b","text":""},{"location":"chapter_tree/binary_tree/#_1","title":"\u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"<p>\u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811 Perfect Binary Tree\u300d\u9664\u4e86\u6700\u5e95\u5c42\u5916\uff0c\u5176\u4f59\u6240\u6709\u5c42\u7684\u8282\u70b9\u90fd\u88ab\u5b8c\u5168\u586b\u6ee1\u3002\u5728\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\uff0c\u53f6\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u7684\u5ea6\u90fd\u4e3a \\(2\\) \uff1b\u82e5\u6811\u9ad8\u5ea6\u4e3a \\(h\\) \uff0c\u5219\u8282\u70b9\u603b\u6570\u4e3a \\(2^{h+1} - 1\\) \uff0c\u5448\u73b0\u6807\u51c6\u7684\u6307\u6570\u7ea7\u5173\u7cfb\uff0c\u53cd\u6620\u4e86\u81ea\u7136\u754c\u4e2d\u5e38\u89c1\u7684\u7ec6\u80de\u5206\u88c2\u73b0\u8c61\u3002</p> <p>Tip</p> <p>\u5728\u4e2d\u6587\u793e\u533a\u4e2d\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u5e38\u88ab\u79f0\u4e3a\u300c\u6ee1\u4e8c\u53c9\u6811\u300d\uff0c\u8bf7\u6ce8\u610f\u533a\u5206\u3002</p> <p></p> <p> \u56fe\uff1a\u5b8c\u7f8e\u4e8c\u53c9\u6811 </p>"},{"location":"chapter_tree/binary_tree/#_2","title":"\u5b8c\u5168\u4e8c\u53c9\u6811","text":"<p>\u300c\u5b8c\u5168\u4e8c\u53c9\u6811 Complete Binary Tree\u300d\u53ea\u6709\u6700\u5e95\u5c42\u7684\u8282\u70b9\u672a\u88ab\u586b\u6ee1\uff0c\u4e14\u6700\u5e95\u5c42\u8282\u70b9\u5c3d\u91cf\u9760\u5de6\u586b\u5145\u3002</p> <p></p> <p> \u56fe\uff1a\u5b8c\u5168\u4e8c\u53c9\u6811 </p>"},{"location":"chapter_tree/binary_tree/#_3","title":"\u5b8c\u6ee1\u4e8c\u53c9\u6811","text":"<p>\u300c\u5b8c\u6ee1\u4e8c\u53c9\u6811 Full Binary Tree\u300d\u9664\u4e86\u53f6\u8282\u70b9\u4e4b\u5916\uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\u3002</p> <p></p> <p> \u56fe\uff1a\u5b8c\u6ee1\u4e8c\u53c9\u6811 </p>"},{"location":"chapter_tree/binary_tree/#_4","title":"\u5e73\u8861\u4e8c\u53c9\u6811","text":"<p>\u300c\u5e73\u8861\u4e8c\u53c9\u6811 Balanced Binary Tree\u300d\u4e2d\u4efb\u610f\u8282\u70b9\u7684\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\u4e4b\u5dee\u7684\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc7 1 \u3002</p> <p></p> <p> \u56fe\uff1a\u5e73\u8861\u4e8c\u53c9\u6811 </p>"},{"location":"chapter_tree/binary_tree/#714","title":"7.1.4. \u00a0 \u4e8c\u53c9\u6811\u7684\u9000\u5316","text":"<p>\u5f53\u4e8c\u53c9\u6811\u7684\u6bcf\u5c42\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u65f6\uff0c\u8fbe\u5230\u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u300d\uff1b\u800c\u5f53\u6240\u6709\u8282\u70b9\u90fd\u504f\u5411\u4e00\u4fa7\u65f6\uff0c\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u300c\u94fe\u8868\u300d\u3002</p> <ul> <li>\u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u7406\u60f3\u60c5\u51b5\uff0c\u53ef\u4ee5\u5145\u5206\u53d1\u6325\u4e8c\u53c9\u6811\u201c\u5206\u6cbb\u201d\u7684\u4f18\u52bf\u3002</li> <li>\u94fe\u8868\u5219\u662f\u53e6\u4e00\u4e2a\u6781\u7aef\uff0c\u5404\u9879\u64cd\u4f5c\u90fd\u53d8\u4e3a\u7ebf\u6027\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \u3002</li> </ul> <p></p> <p> \u56fe\uff1a\u4e8c\u53c9\u6811\u7684\u6700\u4f73\u4e0e\u6700\u5dee\u7ed3\u6784 </p> <p>\u5982\u4e0b\u8868\u6240\u793a\uff0c\u5728\u6700\u4f73\u548c\u6700\u5dee\u7ed3\u6784\u4e0b\uff0c\u4e8c\u53c9\u6811\u7684\u53f6\u8282\u70b9\u6570\u91cf\u3001\u8282\u70b9\u603b\u6570\u3001\u9ad8\u5ea6\u7b49\u8fbe\u5230\u6781\u5927\u6216\u6781\u5c0f\u503c\u3002</p> <p> \u8868\uff1a\u4e8c\u53c9\u6811\u7684\u6700\u4f73\u4e0e\u6700\u5dee\u60c5\u51b5 </p> \u5b8c\u7f8e\u4e8c\u53c9\u6811 \u94fe\u8868 \u7b2c \\(i\\) \u5c42\u7684\u8282\u70b9\u6570\u91cf \\(2^{i-1}\\) \\(1\\) \u9ad8\u5ea6 \\(h\\) \u6811\u7684\u53f6\u8282\u70b9\u6570\u91cf \\(2^h\\) \\(1\\) \u9ad8\u5ea6 \\(h\\) \u6811\u7684\u8282\u70b9\u603b\u6570 \\(2^{h+1} - 1\\) \\(h + 1\\) \u8282\u70b9\u603b\u6570 \\(n\\) \u6811\u7684\u9ad8\u5ea6 \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2. \u00a0 \u4e8c\u53c9\u6811\u904d\u5386","text":"<p>\u4ece\u7269\u7406\u7ed3\u6784\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u6811\u662f\u4e00\u79cd\u57fa\u4e8e\u94fe\u8868\u7684\u6570\u636e\u7ed3\u6784\uff0c\u56e0\u6b64\u5176\u904d\u5386\u65b9\u5f0f\u662f\u901a\u8fc7\u6307\u9488\u9010\u4e2a\u8bbf\u95ee\u8282\u70b9\u3002\u7136\u800c\uff0c\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u4f7f\u5f97\u904d\u5386\u6811\u6bd4\u904d\u5386\u94fe\u8868\u66f4\u52a0\u590d\u6742\uff0c\u9700\u8981\u501f\u52a9\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u3002</p> <p>\u4e8c\u53c9\u6811\u5e38\u89c1\u7684\u904d\u5386\u65b9\u5f0f\u5305\u62ec\u5c42\u5e8f\u904d\u5386\u3001\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u7b49\u3002</p>"},{"location":"chapter_tree/binary_tree_traversal/#721","title":"7.2.1. \u00a0 \u5c42\u5e8f\u904d\u5386","text":"<p>\u300c\u5c42\u5e8f\u904d\u5386 Level-Order Traversal\u300d\u4ece\u9876\u90e8\u5230\u5e95\u90e8\u9010\u5c42\u904d\u5386\u4e8c\u53c9\u6811\uff0c\u5e76\u5728\u6bcf\u4e00\u5c42\u6309\u7167\u4ece\u5de6\u5230\u53f3\u7684\u987a\u5e8f\u8bbf\u95ee\u8282\u70b9\u3002</p> <p>\u5c42\u5e8f\u904d\u5386\u672c\u8d28\u4e0a\u5c5e\u4e8e\u300c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 Breadth-First Traversal\u300d\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u6269\u5c55\u201d\u7684\u9010\u5c42\u641c\u7d22\u65b9\u5f0f\u3002</p> <p></p> <p> \u56fe\uff1a\u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386 </p> <p>\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u901a\u5e38\u501f\u52a9\u300c\u961f\u5217\u300d\u6765\u5b9e\u73b0\u3002\u961f\u5217\u9075\u5faa\u201c\u5148\u8fdb\u5148\u51fa\u201d\u7684\u89c4\u5219\uff0c\u800c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5219\u9075\u5faa\u201c\u9010\u5c42\u63a8\u8fdb\u201d\u7684\u89c4\u5219\uff0c\u4e24\u8005\u80cc\u540e\u7684\u601d\u60f3\u662f\u4e00\u81f4\u7684\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust binary_tree_bfs.java<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nList&lt;Integer&gt; levelOrder(TreeNode root) {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\nQueue&lt;TreeNode&gt; queue = new LinkedList&lt;&gt;();\nqueue.add(root);\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nList&lt;Integer&gt; list = new ArrayList&lt;&gt;();\nwhile (!queue.isEmpty()) {\nTreeNode node = queue.poll(); // \u961f\u5217\u51fa\u961f\nlist.add(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left != null)\nqueue.offer(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node.right != null)\nqueue.offer(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn list;\n}\n</code></pre> binary_tree_bfs.cpp<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nvector&lt;int&gt; levelOrder(TreeNode *root) {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\nqueue&lt;TreeNode *&gt; queue;\nqueue.push(root);\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nvector&lt;int&gt; vec;\nwhile (!queue.empty()) {\nTreeNode *node = queue.front();\nqueue.pop(); // \u961f\u5217\u51fa\u961f\nvec.push_back(node-&gt;val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node-&gt;left != nullptr)\nqueue.push(node-&gt;left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node-&gt;right != nullptr)\nqueue.push(node-&gt;right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn vec;\n}\n</code></pre> binary_tree_bfs.py<pre><code>def level_order(root: TreeNode | None) -&gt; list[int]:\n\"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n# \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\nqueue: deque[TreeNode] = deque()\nqueue.append(root)\n# \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nres = []\nwhile queue:\nnode: TreeNode = queue.popleft() # \u961f\u5217\u51fa\u961f\nres.append(node.val) # \u4fdd\u5b58\u8282\u70b9\u503c\nif node.left is not None:\nqueue.append(node.left) # \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif node.right is not None:\nqueue.append(node.right) # \u53f3\u5b50\u8282\u70b9\u5165\u961f\nreturn res\n</code></pre> binary_tree_bfs.go<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root *TreeNode) []any {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\nqueue := list.New()\nqueue.PushBack(root)\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5207\u7247\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nnums := make([]any, 0)\nfor queue.Len() &gt; 0 {\n// \u961f\u5217\u51fa\u961f\nnode := queue.Remove(queue.Front()).(*TreeNode)\n// \u4fdd\u5b58\u8282\u70b9\u503c\nnums = append(nums, node.Val)\nif node.Left != nil {\n// \u5de6\u5b50\u8282\u70b9\u5165\u961f\nqueue.PushBack(node.Left)\n}\nif node.Right != nil {\n// \u53f3\u5b50\u8282\u70b9\u5165\u961f\nqueue.PushBack(node.Right)\n}\n}\nreturn nums\n}\n</code></pre> binary_tree_bfs.js<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root) {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\nconst queue = [root];\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nconst list = [];\nwhile (queue.length) {\nlet node = queue.shift(); // \u961f\u5217\u51fa\u961f\nlist.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left) queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node.right) queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn list;\n}\n</code></pre> binary_tree_bfs.ts<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root: TreeNode | null): number[] {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\nconst queue = [root];\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nconst list: number[] = [];\nwhile (queue.length) {\nlet node = queue.shift() as TreeNode; // \u961f\u5217\u51fa\u961f\nlist.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left) {\nqueue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n}\nif (node.right) {\nqueue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\n}\nreturn list;\n}\n</code></pre> binary_tree_bfs.c<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(TreeNode *root, int *size) {\n/* \u8f85\u52a9\u961f\u5217 */\nint front, rear;\nint index, *arr;\nTreeNode *node;\nTreeNode **queue;\n/* \u8f85\u52a9\u961f\u5217 */\nqueue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_NODE_SIZE);\n// \u961f\u5217\u6307\u9488\nfront = 0, rear = 0;\n// \u52a0\u5165\u6839\u8282\u70b9\nqueue[rear++] = root;\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n/* \u8f85\u52a9\u6570\u7ec4 */\narr = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);\n// \u6570\u7ec4\u6307\u9488\nindex = 0;\nwhile (front &lt; rear) {\n// \u961f\u5217\u51fa\u961f\nnode = queue[front++];\n// \u4fdd\u5b58\u8282\u70b9\u503c\narr[index++] = node-&gt;val;\nif (node-&gt;left != NULL) {\n// \u5de6\u5b50\u8282\u70b9\u5165\u961f\nqueue[rear++] = node-&gt;left;\n}\nif (node-&gt;right != NULL) {\n// \u53f3\u5b50\u8282\u70b9\u5165\u961f\nqueue[rear++] = node-&gt;right;\n}\n}\n// \u66f4\u65b0\u6570\u7ec4\u957f\u5ea6\u7684\u503c\n*size = index;\narr = realloc(arr, sizeof(int) * (*size));\n// \u91ca\u653e\u8f85\u52a9\u6570\u7ec4\u7a7a\u95f4\nfree(queue);\nreturn arr;\n}\n</code></pre> binary_tree_bfs.cs<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nList&lt;int&gt; levelOrder(TreeNode root) {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\nQueue&lt;TreeNode&gt; queue = new();\nqueue.Enqueue(root);\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nList&lt;int&gt; list = new();\nwhile (queue.Count != 0) {\nTreeNode node = queue.Dequeue(); // \u961f\u5217\u51fa\u961f\nlist.Add(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left != null)\nqueue.Enqueue(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node.right != null)\nqueue.Enqueue(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn list;\n}\n</code></pre> binary_tree_bfs.swift<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root: TreeNode) -&gt; [Int] {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\nvar queue: [TreeNode] = [root]\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nvar list: [Int] = []\nwhile !queue.isEmpty {\nlet node = queue.removeFirst() // \u961f\u5217\u51fa\u961f\nlist.append(node.val) // \u4fdd\u5b58\u8282\u70b9\u503c\nif let left = node.left {\nqueue.append(left) // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n}\nif let right = node.right {\nqueue.append(right) // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\n}\nreturn list\n}\n</code></pre> binary_tree_bfs.zig<pre><code>// \u5c42\u5e8f\u904d\u5386\nfn levelOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\nconst L = std.TailQueue(*inc.TreeNode(T));\nvar queue = L{};\nvar root_node = try mem_allocator.create(L.Node);\nroot_node.data = root;\nqueue.append(root_node); // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nvar list = std.ArrayList(T).init(std.heap.page_allocator);\nwhile (queue.len &gt; 0) {\nvar queue_node = queue.popFirst().?; // \u961f\u5217\u51fa\u961f\nvar node = queue_node.data;\ntry list.append(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left != null) {\nvar tmp_node = try mem_allocator.create(L.Node);\ntmp_node.data = node.left.?;\nqueue.append(tmp_node); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n}\nif (node.right != null) {\nvar tmp_node = try mem_allocator.create(L.Node);\ntmp_node.data = node.right.?;\nqueue.append(tmp_node); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n} }\nreturn list;\n}\n</code></pre> binary_tree_bfs.dart<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nList&lt;int&gt; levelOrder(TreeNode? root) {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\nQueue&lt;TreeNode?&gt; queue = Queue();\nqueue.add(root);\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nList&lt;int&gt; res = [];\nwhile (queue.isNotEmpty) {\nTreeNode? node = queue.removeFirst(); // \u961f\u5217\u51fa\u961f\nres.add(node!.val); // \u4fdd\u5b58\u8282\u70b9\u503c\nif (node.left != null) queue.add(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\nif (node.right != null) queue.add(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n}\nreturn res;\n}\n</code></pre> binary_tree_bfs.rs<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nfn level_order(root: &amp;Rc&lt;RefCell&lt;TreeNode&gt;&gt;) -&gt; Vec&lt;i32&gt; {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u7ed3\u70b9\nlet mut que = VecDeque::new();\nque.push_back(Rc::clone(&amp;root));\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nlet mut vec = Vec::new();\nwhile let Some(node) = que.pop_front() { // \u961f\u5217\u51fa\u961f\nvec.push(node.borrow().val); // \u4fdd\u5b58\u7ed3\u70b9\u503c\nif let Some(left) = node.borrow().left.as_ref() {\nque.push_back(Rc::clone(left)); // \u5de6\u5b50\u7ed3\u70b9\u5165\u961f\n}\nif let Some(right) = node.borrow().right.as_ref() {\nque.push_back(Rc::clone(right)); // \u53f3\u5b50\u7ed3\u70b9\u5165\u961f\n};\n}\nvec\n}\n</code></pre> <p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u8282\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d \\(n\\) \u4e3a\u8282\u70b9\u6570\u91cf\u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u6ee1\u4e8c\u53c9\u6811\u65f6\uff0c\u904d\u5386\u5230\u6700\u5e95\u5c42\u4e4b\u524d\uff0c\u961f\u5217\u4e2d\u6700\u591a\u540c\u65f6\u5b58\u5728 \\(\\frac{n + 1}{2}\\) \u4e2a\u8282\u70b9\uff0c\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002</p>"},{"location":"chapter_tree/binary_tree_traversal/#722","title":"7.2.2. \u00a0 \u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386","text":"<p>\u76f8\u5e94\u5730\uff0c\u524d\u5e8f\u3001\u4e2d\u5e8f\u548c\u540e\u5e8f\u904d\u5386\u90fd\u5c5e\u4e8e\u300c\u6df1\u5ea6\u4f18\u5148\u904d\u5386 Depth-First Traversal\u300d\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u5148\u8d70\u5230\u5c3d\u5934\uff0c\u518d\u56de\u6eaf\u7ee7\u7eed\u201d\u7684\u904d\u5386\u65b9\u5f0f\u3002</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5de6\u4fa7\u662f\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u793a\u610f\u56fe\uff0c\u53f3\u4e0a\u65b9\u662f\u5bf9\u5e94\u7684\u9012\u5f52\u4ee3\u7801\u3002\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5c31\u50cf\u662f\u7ed5\u7740\u6574\u4e2a\u4e8c\u53c9\u6811\u7684\u5916\u56f4\u201c\u8d70\u201d\u4e00\u5708\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u5728\u6bcf\u4e2a\u8282\u70b9\u90fd\u4f1a\u9047\u5230\u4e09\u4e2a\u4f4d\u7f6e\uff0c\u5206\u522b\u5bf9\u5e94\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u3002</p> <p></p> <p> \u56fe\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386 </p> <p>\u4ee5\u4e0b\u7ed9\u51fa\u4e86\u5b9e\u73b0\u4ee3\u7801\uff0c\u8bf7\u914d\u5408\u4e0a\u56fe\u7406\u89e3\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u9012\u5f52\u8fc7\u7a0b\u3002</p> JavaC++PythonGoJSTSCC#SwiftZigDartRust binary_tree_dfs.java<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode root) {\nif (root == null)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -&gt; \u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811\nlist.add(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode root) {\nif (root == null)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u6839\u8282\u70b9 -&gt; \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.add(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode root) {\nif (root == null)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811 -&gt; \u6839\u8282\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.add(root.val);\n}\n</code></pre> binary_tree_dfs.cpp<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root) {\nif (root == nullptr)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -&gt; \u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811\nvec.push_back(root-&gt;val);\npreOrder(root-&gt;left);\npreOrder(root-&gt;right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root) {\nif (root == nullptr)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u6839\u8282\u70b9 -&gt; \u53f3\u5b50\u6811\ninOrder(root-&gt;left);\nvec.push_back(root-&gt;val);\ninOrder(root-&gt;right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root) {\nif (root == nullptr)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811 -&gt; \u6839\u8282\u70b9\npostOrder(root-&gt;left);\npostOrder(root-&gt;right);\nvec.push_back(root-&gt;val);\n}\n</code></pre> binary_tree_dfs.py<pre><code>def pre_order(root: TreeNode | None):\n\"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\nif root is None:\nreturn\n# \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -&gt; \u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811\nres.append(root.val)\npre_order(root=root.left)\npre_order(root=root.right)\ndef in_order(root: TreeNode | None):\n\"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\nif root is None:\nreturn\n# \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u6839\u8282\u70b9 -&gt; \u53f3\u5b50\u6811\nin_order(root=root.left)\nres.append(root.val)\nin_order(root=root.right)\ndef post_order(root: TreeNode | None):\n\"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\nif root is None:\nreturn\n# \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811 -&gt; \u6839\u8282\u70b9\npost_order(root=root.left)\npost_order(root=root.right)\nres.append(root.val)\n</code></pre> binary_tree_dfs.go<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(node *TreeNode) {\nif node == nil {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -&gt; \u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811\nnums = append(nums, node.Val)\npreOrder(node.Left)\npreOrder(node.Right)\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(node *TreeNode) {\nif node == nil {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u6839\u8282\u70b9 -&gt; \u53f3\u5b50\u6811\ninOrder(node.Left)\nnums = append(nums, node.Val)\ninOrder(node.Right)\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(node *TreeNode) {\nif node == nil {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811 -&gt; \u6839\u8282\u70b9\npostOrder(node.Left)\npostOrder(node.Right)\nnums = append(nums, node.Val)\n}\n</code></pre> binary_tree_dfs.js<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root) {\nif (root === null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -&gt; \u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811\nlist.push(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root) {\nif (root === null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u6839\u8282\u70b9 -&gt; \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.push(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root) {\nif (root === null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811 -&gt; \u6839\u8282\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.push(root.val);\n}\n</code></pre> binary_tree_dfs.ts<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root: TreeNode | null): void {\nif (root === null) {\nreturn;\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -&gt; \u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811\nlist.push(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root: TreeNode | null): void {\nif (root === null) {\nreturn;\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u6839\u8282\u70b9 -&gt; \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.push(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root: TreeNode | null): void {\nif (root === null) {\nreturn;\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811 -&gt; \u6839\u8282\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.push(root.val);\n}\n</code></pre> binary_tree_dfs.c<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root, int *size) {\nif (root == NULL)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -&gt; \u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811\narr[(*size)++] = root-&gt;val;\npreOrder(root-&gt;left, size);\npreOrder(root-&gt;right, size);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root, int *size) {\nif (root == NULL)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u6839\u8282\u70b9 -&gt; \u53f3\u5b50\u6811\ninOrder(root-&gt;left, size);\narr[(*size)++] = root-&gt;val;\ninOrder(root-&gt;right, size);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root, int *size) {\nif (root == NULL)\nreturn;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811 -&gt; \u6839\u8282\u70b9\npostOrder(root-&gt;left, size);\npostOrder(root-&gt;right, size);\narr[(*size)++] = root-&gt;val;\n}\n</code></pre> binary_tree_dfs.cs<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode? root) {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -&gt; \u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811\nlist.Add(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode? root) {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u6839\u8282\u70b9 -&gt; \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.Add(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode? root) {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811 -&gt; \u6839\u8282\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.Add(root.val);\n}\n</code></pre> binary_tree_dfs.swift<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -&gt; \u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811\nlist.append(root.val)\npreOrder(root: root.left)\npreOrder(root: root.right)\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u6839\u8282\u70b9 -&gt; \u53f3\u5b50\u6811\ninOrder(root: root.left)\nlist.append(root.val)\ninOrder(root: root.right)\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811 -&gt; \u6839\u8282\u70b9\npostOrder(root: root.left)\npostOrder(root: root.right)\nlist.append(root.val)\n}\n</code></pre> binary_tree_dfs.zig<pre><code>// \u524d\u5e8f\u904d\u5386\nfn preOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -&gt; \u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811\ntry list.append(root.?.val);\ntry preOrder(T, root.?.left);\ntry preOrder(T, root.?.right);\n}\n// \u4e2d\u5e8f\u904d\u5386\nfn inOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u6839\u8282\u70b9 -&gt; \u53f3\u5b50\u6811\ntry inOrder(T, root.?.left);\ntry list.append(root.?.val);\ntry inOrder(T, root.?.right);\n}\n// \u540e\u5e8f\u904d\u5386\nfn postOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811 -&gt; \u6839\u8282\u70b9\ntry postOrder(T, root.?.left);\ntry postOrder(T, root.?.right);\ntry list.append(root.?.val);\n}\n</code></pre> binary_tree_dfs.dart<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode? node) {\nif (node == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -&gt; \u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811\nlist.add(node.val);\npreOrder(node.left);\npreOrder(node.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode? node) {\nif (node == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u6839\u8282\u70b9 -&gt; \u53f3\u5b50\u6811\ninOrder(node.left);\nlist.add(node.val);\ninOrder(node.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode? node) {\nif (node == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811 -&gt; \u6839\u8282\u70b9\npostOrder(node.left);\npostOrder(node.right);\nlist.add(node.val);\n}\n</code></pre> binary_tree_dfs.rs<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nfn pre_order(root: Option&lt;&amp;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;) -&gt; Vec&lt;i32&gt; {\nlet mut result = vec![];\nif let Some(node) = root {\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u7ed3\u70b9 -&gt; \u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811\nresult.push(node.borrow().val);\nresult.append(&amp;mut pre_order(node.borrow().left.as_ref()));\nresult.append(&amp;mut pre_order(node.borrow().right.as_ref()));\n}\nresult\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfn in_order(root: Option&lt;&amp;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;) -&gt; Vec&lt;i32&gt; {\nlet mut result = vec![];\nif let Some(node) = root {\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u6839\u7ed3\u70b9 -&gt; \u53f3\u5b50\u6811\nresult.append(&amp;mut in_order(node.borrow().left.as_ref()));\nresult.push(node.borrow().val);\nresult.append(&amp;mut in_order(node.borrow().right.as_ref()));\n}\nresult\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfn post_order(root: Option&lt;&amp;Rc&lt;RefCell&lt;TreeNode&gt;&gt;&gt;) -&gt; Vec&lt;i32&gt; {\nlet mut result = vec![];\nif let Some(node) = root {\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -&gt; \u53f3\u5b50\u6811 -&gt; \u6839\u7ed3\u70b9\nresult.append(&amp;mut post_order(node.borrow().left.as_ref()));\nresult.append(&amp;mut post_order(node.borrow().right.as_ref()));\nresult.push(node.borrow().val);\n}\nresult\n}\n</code></pre> <p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u8282\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d \\(n\\) \u4e3a\u8282\u70b9\u6570\u91cf\u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u9012\u5f52\u6df1\u5ea6\u8fbe\u5230 \\(n\\) \uff0c\u7cfb\u7edf\u5360\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002</p> <p>Note</p> <p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u4e0d\u4f7f\u7528\u9012\u5f52\uff0c\u4ec5\u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u81ea\u884c\u7814\u7a76\u3002</p> <p>\u4e0b\u56fe\u5c55\u793a\u4e86\u524d\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5176\u53ef\u5206\u4e3a\u201c\u9012\u201d\u548c\u201c\u5f52\u201d\u4e24\u4e2a\u9006\u5411\u7684\u90e8\u5206\uff1a</p> <ol> <li>\u201c\u9012\u201d\u8868\u793a\u5f00\u542f\u65b0\u65b9\u6cd5\uff0c\u7a0b\u5e8f\u5728\u6b64\u8fc7\u7a0b\u4e2d\u8bbf\u95ee\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002</li> <li>\u201c\u5f52\u201d\u8868\u793a\u51fd\u6570\u8fd4\u56de\uff0c\u4ee3\u8868\u5f53\u524d\u8282\u70b9\u5df2\u7ecf\u8bbf\u95ee\u5b8c\u6bd5\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> \u56fe\uff1a\u524d\u5e8f\u904d\u5386\u7684\u9012\u5f52\u8fc7\u7a0b </p>"},{"location":"chapter_tree/summary/","title":"7.6. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u4e8c\u53c9\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4f53\u73b0\u201c\u4e00\u5206\u4e3a\u4e8c\u201d\u7684\u5206\u6cbb\u903b\u8f91\u3002\u6bcf\u4e2a\u4e8c\u53c9\u6811\u8282\u70b9\u5305\u542b\u4e00\u4e2a\u503c\u4ee5\u53ca\u4e24\u4e2a\u6307\u9488\uff0c\u5206\u522b\u6307\u5411\u5176\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u3002</li> <li>\u5bf9\u4e8e\u4e8c\u53c9\u6811\u4e2d\u7684\u67d0\u4e2a\u8282\u70b9\uff0c\u5176\u5de6\uff08\u53f3\uff09\u5b50\u8282\u70b9\u53ca\u5176\u4ee5\u4e0b\u5f62\u6210\u7684\u6811\u88ab\u79f0\u4e3a\u8be5\u8282\u70b9\u7684\u5de6\uff08\u53f3\uff09\u5b50\u6811\u3002</li> <li>\u4e8c\u53c9\u6811\u7684\u76f8\u5173\u672f\u8bed\u5305\u62ec\u6839\u8282\u70b9\u3001\u53f6\u8282\u70b9\u3001\u5c42\u3001\u5ea6\u3001\u8fb9\u3001\u9ad8\u5ea6\u548c\u6df1\u5ea6\u7b49\u3002</li> <li>\u4e8c\u53c9\u6811\u7684\u521d\u59cb\u5316\u3001\u8282\u70b9\u63d2\u5165\u548c\u8282\u70b9\u5220\u9664\u64cd\u4f5c\u4e0e\u94fe\u8868\u64cd\u4f5c\u65b9\u6cd5\u7c7b\u4f3c\u3002</li> <li>\u5e38\u89c1\u7684\u4e8c\u53c9\u6811\u7c7b\u578b\u6709\u5b8c\u7f8e\u4e8c\u53c9\u6811\u3001\u5b8c\u5168\u4e8c\u53c9\u6811\u3001\u6ee1\u4e8c\u53c9\u6811\u548c\u5e73\u8861\u4e8c\u53c9\u6811\u3002\u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u6700\u7406\u60f3\u7684\u72b6\u6001\uff0c\u800c\u94fe\u8868\u662f\u9000\u5316\u540e\u7684\u6700\u5dee\u72b6\u6001\u3002</li> <li>\u4e8c\u53c9\u6811\u53ef\u4ee5\u7528\u6570\u7ec4\u8868\u793a\uff0c\u65b9\u6cd5\u662f\u5c06\u8282\u70b9\u503c\u548c\u7a7a\u4f4d\u6309\u5c42\u5e8f\u904d\u5386\u987a\u5e8f\u6392\u5217\uff0c\u5e76\u6839\u636e\u7236\u8282\u70b9\u4e0e\u5b50\u8282\u70b9\u4e4b\u95f4\u7684\u7d22\u5f15\u6620\u5c04\u5173\u7cfb\u6765\u5b9e\u73b0\u6307\u9488\u3002</li> <li>\u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386\u662f\u4e00\u79cd\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u65b9\u6cd5\uff0c\u5b83\u4f53\u73b0\u4e86\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u201d\u7684\u5206\u5c42\u904d\u5386\u65b9\u5f0f\uff0c\u901a\u5e38\u901a\u8fc7\u961f\u5217\u6765\u5b9e\u73b0\u3002</li> <li>\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\u7686\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u5b83\u4eec\u4f53\u73b0\u4e86\u201c\u8d70\u5230\u5c3d\u5934\uff0c\u518d\u56de\u5934\u7ee7\u7eed\u201d\u7684\u56de\u6eaf\u904d\u5386\u65b9\u5f0f\uff0c\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0\u3002</li> <li>\u4e8c\u53c9\u641c\u7d22\u6811\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u627e\u6570\u636e\u7ed3\u6784\uff0c\u5176\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(\\log n)\\) \u3002\u5f53\u4e8c\u53c9\u641c\u7d22\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u5404\u9879\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u52a3\u5316\u81f3 \\(O(n)\\) \u3002</li> <li>AVL \u6811\uff0c\u4e5f\u79f0\u4e3a\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u5b83\u901a\u8fc7\u65cb\u8f6c\u64cd\u4f5c\uff0c\u786e\u4fdd\u5728\u4e0d\u65ad\u63d2\u5165\u548c\u5220\u9664\u8282\u70b9\u540e\uff0c\u6811\u4ecd\u7136\u4fdd\u6301\u5e73\u8861\u3002</li> <li>AVL \u6811\u7684\u65cb\u8f6c\u64cd\u4f5c\u5305\u62ec\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u518d\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u518d\u53f3\u65cb\u3002\u5728\u63d2\u5165\u6216\u5220\u9664\u8282\u70b9\u540e\uff0cAVL \u6811\u4f1a\u4ece\u5e95\u5411\u9876\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002</li> </ul>"},{"location":"chapter_tree/summary/#761-q-a","title":"7.6.1. \u00a0 Q &amp; A","text":"<p>\u5bf9\u4e8e\u53ea\u6709\u4e00\u4e2a\u8282\u70b9\u7684\u4e8c\u53c9\u6811\uff0c\u6811\u7684\u9ad8\u5ea6\u548c\u6839\u8282\u70b9\u7684\u6df1\u5ea6\u90fd\u662f \\(0\\) \u5417\uff1f</p> <p>\u662f\u7684\uff0c\u56e0\u4e3a\u9ad8\u5ea6\u548c\u6df1\u5ea6\u901a\u5e38\u5b9a\u4e49\u4e3a\u201c\u8d70\u8fc7\u8fb9\u7684\u6570\u91cf\u201d\u3002</p> <p>\u4e8c\u53c9\u6811\u4e2d\u7684\u63d2\u5165\u4e0e\u5220\u9664\u4e00\u822c\u90fd\u662f\u7531\u4e00\u5957\u64cd\u4f5c\u914d\u5408\u5b8c\u6210\u7684\uff0c\u8fd9\u91cc\u7684\u201c\u4e00\u5957\u64cd\u4f5c\u201d\u6307\u4ec0\u4e48\u5462\uff1f\u53ef\u4ee5\u7406\u89e3\u4e3a\u8d44\u6e90\u7684\u5b50\u8282\u70b9\u7684\u8d44\u6e90\u91ca\u653e\u5417\uff1f</p> <p>\u62ff\u4e8c\u53c9\u641c\u7d22\u6811\u6765\u4e3e\u4f8b\uff0c\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u8981\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\u5904\u7406\uff0c\u5176\u4e2d\u6bcf\u79cd\u60c5\u51b5\u90fd\u9700\u8981\u8fdb\u884c\u591a\u4e2a\u6b65\u9aa4\u7684\u8282\u70b9\u64cd\u4f5c\u3002</p> <p>\u4e3a\u4ec0\u4e48 DFS \u904d\u5386\u4e8c\u53c9\u6811\u6709\u524d\u3001\u4e2d\u3001\u540e\u4e09\u79cd\u987a\u5e8f\uff0c\u5206\u522b\u6709\u4ec0\u4e48\u7528\u5462\uff1f</p> <p>DFS \u7684\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386\u548c\u8bbf\u95ee\u6570\u7ec4\u7684\u987a\u5e8f\u7c7b\u4f3c\uff0c\u662f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u5229\u7528\u8fd9\u4e09\u79cd\u904d\u5386\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u7279\u5b9a\u987a\u5e8f\u7684\u904d\u5386\u7ed3\u679c\u3002\u4f8b\u5982\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\uff0c\u7531\u4e8e\u7ed3\u70b9\u5927\u5c0f\u6ee1\u8db3 <code>\u5de6\u5b50\u7ed3\u70b9\u503c &lt; \u6839\u7ed3\u70b9\u503c &lt; \u53f3\u5b50\u7ed3\u70b9\u503c</code> \uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u8981\u6309\u7167 <code>\u5de6-&gt;\u6839-&gt;\u53f3</code> \u7684\u4f18\u5148\u7ea7\u904d\u5386\u6811\uff0c\u5c31\u53ef\u4ee5\u83b7\u5f97\u6709\u5e8f\u7684\u8282\u70b9\u5e8f\u5217\u3002</p> <p>\u53f3\u65cb\u64cd\u4f5c\u662f\u5904\u7406\u5931\u8861\u8282\u70b9 <code>node</code> , <code>child</code> , <code>grand_child</code> \u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u90a3 <code>node</code> \u7684\u7236\u8282\u70b9\u548c <code>node</code> \u539f\u6765\u7684\u8fde\u63a5\u4e0d\u9700\u8981\u7ef4\u62a4\u5417\uff1f\u53f3\u65cb\u64cd\u4f5c\u540e\u5c82\u4e0d\u662f\u65ad\u6389\u4e86\uff1f</p> <p>\u6211\u4eec\u9700\u8981\u4ece\u9012\u5f52\u7684\u89c6\u89d2\u6765\u770b\u8fd9\u4e2a\u95ee\u9898\u3002\u53f3\u65cb\u64cd\u4f5c <code>right_rotate(root)</code> \u4f20\u5165\u7684\u662f\u5b50\u6811\u7684\u6839\u8282\u70b9\uff0c\u6700\u7ec8 <code>return child</code> \u8fd4\u56de\u65cb\u8f6c\u4e4b\u540e\u7684\u5b50\u6811\u7684\u6839\u8282\u70b9\u3002\u5b50\u6811\u7684\u6839\u8282\u70b9\u548c\u5176\u7236\u8282\u70b9\u7684\u8fde\u63a5\u662f\u5728\u8be5\u51fd\u6570\u8fd4\u56de\u540e\u5b8c\u6210\u7684\uff0c\u4e0d\u5c5e\u4e8e\u53f3\u65cb\u64cd\u4f5c\u7684\u7ef4\u62a4\u8303\u56f4\u3002</p> <p>\u5728 C++ \u4e2d\uff0c\u51fd\u6570\u88ab\u5212\u5206\u5230 <code>private</code> \u548c <code>public</code> \u4e2d\uff0c\u8fd9\u65b9\u9762\u6709\u4ec0\u4e48\u8003\u91cf\u5417\uff1f\u4e3a\u4ec0\u4e48\u8981\u5c06 <code>height()</code> \u51fd\u6570\u548c <code>updateHeight()</code> \u51fd\u6570\u5206\u522b\u653e\u5728 <code>public</code> \u548c <code>private</code> \u4e2d\u5462\uff1f</p> <p>\u4e3b\u8981\u770b\u65b9\u6cd5\u7684\u4f7f\u7528\u8303\u56f4\uff0c\u5982\u679c\u65b9\u6cd5\u53ea\u5728\u7c7b\u5185\u90e8\u4f7f\u7528\uff0c\u90a3\u4e48\u5c31\u8bbe\u8ba1\u4e3a <code>private</code> \u3002\u4f8b\u5982\uff0c\u7528\u6237\u5355\u72ec\u8c03\u7528 <code>updateHeight()</code> \u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u5b83\u53ea\u662f\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u4e2d\u7684\u4e00\u6b65\u3002\u800c <code>height()</code> \u662f\u8bbf\u95ee\u7ed3\u70b9\u9ad8\u5ea6\uff0c\u7c7b\u4f3c\u4e8e <code>vector.size()</code> \uff0c\u56e0\u6b64\u8bbe\u7f6e\u6210 <code>public</code> \u4ee5\u4fbf\u4f7f\u7528\u3002</p> <p>\u8bf7\u95ee\u5982\u4f55\u4ece\u4e00\u7ec4\u8f93\u5165\u6570\u636e\u6784\u5efa\u4e00\u4e2a\u4e8c\u53c9\u641c\u7d22\u6811\uff1f\u6839\u8282\u70b9\u7684\u9009\u62e9\u662f\u4e0d\u662f\u5f88\u91cd\u8981\uff1f</p> <p>\u662f\u7684\uff0c\u6784\u5efa\u6811\u7684\u65b9\u6cd5\u5df2\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4ee3\u7801\u4e2d\u7684 <code>build_tree()</code> \u65b9\u6cd5\u4e2d\u7ed9\u51fa\u3002\u81f3\u4e8e\u6839\u8282\u70b9\u7684\u9009\u62e9\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u8f93\u5165\u6570\u636e\u6392\u5e8f\uff0c\u7136\u540e\u7528\u4e2d\u70b9\u5143\u7d20\u4f5c\u4e3a\u6839\u8282\u70b9\uff0c\u518d\u9012\u5f52\u5730\u6784\u5efa\u5de6\u53f3\u5b50\u6811\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u6700\u5927\u7a0b\u5ea6\u4fdd\u8bc1\u6811\u7684\u5e73\u8861\u6027\u3002</p> <p>\u5728 Java \u4e2d\uff0c\u5b57\u7b26\u4e32\u5bf9\u6bd4\u662f\u5426\u4e00\u5b9a\u8981\u7528 <code>equals()</code> \u65b9\u6cd5\uff1f</p> <p>\u5728 Java \u4e2d\uff0c\u5bf9\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c<code>==</code> \u7528\u4e8e\u5bf9\u6bd4\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\uff0c\u4e24\u79cd\u7b26\u53f7\u7684\u5de5\u4f5c\u539f\u7406\u4e0d\u540c\uff1a</p> <ul> <li><code>==</code> \uff1a\u7528\u6765\u6bd4\u8f83\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5373\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u662f\u5426\u76f8\u540c\u3002</li> <li><code>equals()</code>\uff1a\u7528\u6765\u5bf9\u6bd4\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u662f\u5426\u76f8\u7b49\u3002</li> </ul> <p>\u56e0\u6b64\u5982\u679c\u8981\u5bf9\u6bd4\u503c\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u7528 <code>equals()</code> \u3002\u7136\u800c\uff0c\u901a\u8fc7 <code>String a = \"hi\"; String b = \"hi\";</code> \u521d\u59cb\u5316\u7684\u5b57\u7b26\u4e32\u90fd\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u6c60\u4e2d\uff0c\u5b83\u4eec\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u56e0\u6b64\u4e5f\u53ef\u4ee5\u7528 <code>a == b</code> \u6765\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u3002</p> <p>\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5230\u6700\u5e95\u5c42\u4e4b\u524d\uff0c\u961f\u5217\u4e2d\u7684\u8282\u70b9\u6570\u91cf\u662f \\(2^h\\) \u5417\uff1f</p> <p>\u662f\u7684\uff0c\u4f8b\u5982\u9ad8\u5ea6 \\(h = 2\\) \u7684\u6ee1\u4e8c\u53c9\u6811\uff0c\u5176\u8282\u70b9\u603b\u6570 \\(n = 7\\) \uff0c\u5219\u5e95\u5c42\u8282\u70b9\u6570\u91cf \\(4 = 2^h = (n + 1) / 2\\) \u3002</p>"}]}