mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-27 01:56:28 +08:00
1 line
No EOL
1,024 KiB
JSON
1 line
No EOL
1,024 KiB
JSON
{"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\u6211\u9010\u6e10\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\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\u53d1\u8868\u7684\u300a\u56fe\u89e3\u7b97\u6cd5\u6570\u636e\u7ed3\u6784\u300b\u5df2\u88ab\u8ba2\u9605 27 \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\u6392\u5217\uff09\uff1a</p> <p> </p> <p>\u672c\u4e66\u7684\u4ee3\u7801\u5ba1\u9605\u5de5\u4f5c\u7531 Gonglja, gvenusleo, justin\u2010tse, krahets, night-cruise, nuomi1, Reanon, sjinzh \u5b8c\u6210\uff08\u6309\u7167\u9996\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\uff09\u3002\u611f\u8c22\u4ed6\u4eec\u4ed8\u51fa\u7684\u65f6\u95f4\u4e0e\u7cbe\u529b\uff0c\u6b63\u662f\u4ed6\u4eec\u786e\u4fdd\u4e86\u5404\u8bed\u8a00\u4ee3\u7801\u7684\u89c4\u8303\u4e0e\u7edf\u4e00\u3002</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>night-cruise</sub><sub>Rust</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":"\u7b2c 16 \u7ae0 \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\u7ed9\u8bfb\u8005\u63d0\u4f9b\u66f4\u4f18\u8d28\u7684\u5b66\u4e60\u8d44\u6e90\u3002</p> <p>\u6240\u6709\u64b0\u7a3f\u4eba\u7684 GitHub ID \u5c06\u88ab\u5c55\u793a\u5728\u672c\u4e66\u7684\u4ed3\u5e93\u4e3b\u9875\u4e0a\uff0c\u4ee5\u611f\u8c22\u4ed6\u4eec\u5bf9\u5f00\u6e90\u793e\u533a\u7684\u65e0\u79c1\u5949\u732e\u3002</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/#1","title":"1. \u00a0 \u5185\u5bb9\u5fae\u8c03","text":"<p>\u5982\u56fe 16-1 \u6240\u793a\uff0c\u6bcf\u4e2a\u9875\u9762\u7684\u53f3\u4e0a\u89d2\u90fd\u6709\u201c\u7f16\u8f91\u56fe\u6807\u201d\u3002\u60a8\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u4fee\u6539\u6587\u672c\u6216\u4ee3\u7801\u3002</p> <ol> <li>\u70b9\u51fb\u201c\u7f16\u8f91\u56fe\u6807\u201d\uff0c\u5982\u679c\u9047\u5230\u201c\u9700\u8981 Fork \u6b64\u4ed3\u5e93\u201d\u7684\u63d0\u793a\uff0c\u8bf7\u540c\u610f\u8be5\u64cd\u4f5c\u3002</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 16-1 \u00a0 \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/#2","title":"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\u4ee5\u4e0b Pull Request \u5de5\u4f5c\u6d41\u7a0b\u3002</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/#3-docker","title":"3. \u00a0 Docker \u90e8\u7f72","text":"<p>\u5728 <code>hello-algo</code> \u6839\u76ee\u5f55\u4e0b\uff0c\u6267\u884c\u4ee5\u4e0b Docker \u811a\u672c\uff0c\u5373\u53ef\u5728 <code>http://localhost:8000</code> \u8bbf\u95ee\u672c\u9879\u76ee\u3002</p> <pre><code>docker-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/#1-vscode","title":"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/#2-java","title":"2. \u00a0 Java \u73af\u5883","text":"<ol> <li>\u4e0b\u8f7d\u5e76\u5b89\u88c5 OpenJDK\uff08\u7248\u672c\u9700\u6ee1\u8db3 > 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/#3-cc","title":"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\u987b\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/#4-python","title":"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/#5-go","title":"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/#6-javascript","title":"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/#7-c","title":"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/#8-swift","title":"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/#9-dart","title":"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/#10-rust","title":"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":"\u7b2c 4 \u7ae0 \u00a0 \u6570\u7ec4\u4e0e\u94fe\u8868","text":"<p>Abstract</p> <p>\u6570\u636e\u7ed3\u6784\u7684\u4e16\u754c\u5982\u540c\u4e00\u5835\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\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u79f0\u4e3a\u8be5\u5143\u7d20\u7684\u300c\u7d22\u5f15 index\u300d\u3002\u56fe 4-1 \u5c55\u793a\u4e86\u6570\u7ec4\u7684\u4e3b\u8981\u672f\u8bed\u548c\u6982\u5ff5\u3002</p> <p></p> <p> \u56fe 4-1 \u00a0 \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":"1. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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.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.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.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.dart<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nList<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList<int> nums = [1, 3, 2, 5, 4];\n</code></pre> array.rs<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: Vec<i32> = vec![0; 5]; // [0, 0, 0, 0, 0]\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n</code></pre> array.c<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 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>"},{"location":"chapter_array_and_linkedlist/array/#2","title":"2. \u00a0 \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\u56fe 4-2 \u6240\u793a\u7684\u516c\u5f0f\u8ba1\u7b97\u5f97\u5230\u8be5\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\uff0c\u4ece\u800c\u76f4\u63a5\u8bbf\u95ee\u6b64\u5143\u7d20\u3002</p> <p></p> <p> \u56fe 4-2 \u00a0 \u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u8ba1\u7b97 </p> <p>\u89c2\u5bdf\u56fe 4-2 \uff0c\u6211\u4eec\u53d1\u73b0\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u4e3a \\(0\\) \uff0c\u8fd9\u4f3c\u4e4e\u6709\u4e9b\u53cd\u76f4\u89c9\uff0c\u56e0\u4e3a\u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\u4f1a\u66f4\u81ea\u7136\u3002\u4f46\u4ece\u5730\u5740\u8ba1\u7b97\u516c\u5f0f\u7684\u89d2\u5ea6\u770b\uff0c\u7d22\u5f15\u7684\u542b\u4e49\u672c\u8d28\u4e0a\u662f\u5185\u5b58\u5730\u5740\u7684\u504f\u79fb\u91cf\u3002\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u504f\u79fb\u91cf\u662f \\(0\\) \uff0c\u56e0\u6b64\u5b83\u7684\u7d22\u5f15\u4e3a \\(0\\) \u4e5f\u662f\u5408\u7406\u7684\u3002</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> PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py<pre><code>[class]{}-[func]{random_access}\n</code></pre> array.cpp<pre><code>[class]{}-[func]{randomAccess}\n</code></pre> array.java<pre><code>[class]{array}-[func]{randomAccess}\n</code></pre> array.cs<pre><code>[class]{array}-[func]{randomAccess}\n</code></pre> array.go<pre><code>[class]{}-[func]{randomAccess}\n</code></pre> array.swift<pre><code>[class]{}-[func]{randomAccess}\n</code></pre> array.js<pre><code>[class]{}-[func]{randomAccess}\n</code></pre> array.ts<pre><code>[class]{}-[func]{randomAccess}\n</code></pre> array.dart<pre><code>[class]{}-[func]{randomAccess}\n</code></pre> array.rs<pre><code>[class]{}-[func]{random_access}\n</code></pre> array.c<pre><code>[class]{}-[func]{randomAccess}\n</code></pre> array.zig<pre><code>[class]{}-[func]{randomAccess}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/array/#3","title":"3. \u00a0 \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\u5982\u56fe 4-3 \u6240\u793a\uff0c\u5982\u679c\u60f3\u8981\u5728\u6570\u7ec4\u4e2d\u95f4\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u5219\u9700\u8981\u5c06\u8be5\u5143\u7d20\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u4e4b\u540e\u518d\u628a\u5143\u7d20\u8d4b\u503c\u7ed9\u8be5\u7d22\u5f15\u3002</p> <p></p> <p> \u56fe 4-3 \u00a0 \u6570\u7ec4\u63d2\u5165\u5143\u7d20\u793a\u4f8b </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> PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py<pre><code>[class]{}-[func]{insert}\n</code></pre> array.cpp<pre><code>[class]{}-[func]{insert}\n</code></pre> array.java<pre><code>[class]{array}-[func]{insert}\n</code></pre> array.cs<pre><code>[class]{array}-[func]{insert}\n</code></pre> array.go<pre><code>[class]{}-[func]{insert}\n</code></pre> array.swift<pre><code>[class]{}-[func]{insert}\n</code></pre> array.js<pre><code>[class]{}-[func]{insert}\n</code></pre> array.ts<pre><code>[class]{}-[func]{insert}\n</code></pre> array.dart<pre><code>[class]{}-[func]{insert}\n</code></pre> array.rs<pre><code>[class]{}-[func]{insert}\n</code></pre> array.c<pre><code>[class]{}-[func]{insert}\n</code></pre> array.zig<pre><code>[class]{}-[func]{insert}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/array/#4","title":"4. \u00a0 \u5220\u9664\u5143\u7d20","text":"<p>\u540c\u7406\uff0c\u5982\u56fe 4-4 \u6240\u793a\uff0c\u82e5\u60f3\u8981\u5220\u9664\u7d22\u5f15 \\(i\\) \u5904\u7684\u5143\u7d20\uff0c\u5219\u9700\u8981\u628a\u7d22\u5f15 \\(i\\) \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\u3002</p> <p></p> <p> \u56fe 4-4 \u00a0 \u6570\u7ec4\u5220\u9664\u5143\u7d20\u793a\u4f8b </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\u987b\u7279\u610f\u53bb\u4fee\u6539\u5b83\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py<pre><code>[class]{}-[func]{remove}\n</code></pre> array.cpp<pre><code>[class]{}-[func]{remove}\n</code></pre> array.java<pre><code>[class]{array}-[func]{remove}\n</code></pre> array.cs<pre><code>[class]{array}-[func]{remove}\n</code></pre> array.go<pre><code>[class]{}-[func]{remove}\n</code></pre> array.swift<pre><code>[class]{}-[func]{remove}\n</code></pre> array.js<pre><code>[class]{}-[func]{remove}\n</code></pre> array.ts<pre><code>[class]{}-[func]{remove}\n</code></pre> array.dart<pre><code>[class]{}-[func]{remove}\n</code></pre> array.rs<pre><code>[class]{}-[func]{remove}\n</code></pre> array.c<pre><code>[class]{}-[func]{removeItem}\n</code></pre> array.zig<pre><code>[class]{}-[func]{remove}\n</code></pre> <p>\u603b\u7684\u6765\u770b\uff0c\u6570\u7ec4\u7684\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u6709\u4ee5\u4e0b\u7f3a\u70b9\u3002</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":"5. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py<pre><code>[class]{}-[func]{traverse}\n</code></pre> array.cpp<pre><code>[class]{}-[func]{traverse}\n</code></pre> array.java<pre><code>[class]{array}-[func]{traverse}\n</code></pre> array.cs<pre><code>[class]{array}-[func]{traverse}\n</code></pre> array.go<pre><code>[class]{}-[func]{traverse}\n</code></pre> array.swift<pre><code>[class]{}-[func]{traverse}\n</code></pre> array.js<pre><code>[class]{}-[func]{traverse}\n</code></pre> array.ts<pre><code>[class]{}-[func]{traverse}\n</code></pre> array.dart<pre><code>[class]{}-[func]{traverse}\n</code></pre> array.rs<pre><code>[class]{}-[func]{traverse}\n</code></pre> array.c<pre><code>[class]{}-[func]{traverse}\n</code></pre> array.zig<pre><code>[class]{}-[func]{traverse}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/array/#6","title":"6. \u00a0 \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\u201c\u7ebf\u6027\u67e5\u627e\u201d\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py<pre><code>[class]{}-[func]{find}\n</code></pre> array.cpp<pre><code>[class]{}-[func]{find}\n</code></pre> array.java<pre><code>[class]{array}-[func]{find}\n</code></pre> array.cs<pre><code>[class]{array}-[func]{find}\n</code></pre> array.go<pre><code>[class]{}-[func]{find}\n</code></pre> array.swift<pre><code>[class]{}-[func]{find}\n</code></pre> array.js<pre><code>[class]{}-[func]{find}\n</code></pre> array.ts<pre><code>[class]{}-[func]{find}\n</code></pre> array.dart<pre><code>[class]{}-[func]{find}\n</code></pre> array.rs<pre><code>[class]{}-[func]{find}\n</code></pre> array.c<pre><code>[class]{}-[func]{find}\n</code></pre> array.zig<pre><code>[class]{}-[func]{find}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/array/#7","title":"7. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig array.py<pre><code>[class]{}-[func]{extend}\n</code></pre> array.cpp<pre><code>[class]{}-[func]{extend}\n</code></pre> array.java<pre><code>[class]{array}-[func]{extend}\n</code></pre> array.cs<pre><code>[class]{array}-[func]{extend}\n</code></pre> array.go<pre><code>[class]{}-[func]{extend}\n</code></pre> array.swift<pre><code>[class]{}-[func]{extend}\n</code></pre> array.js<pre><code>[class]{}-[func]{extend}\n</code></pre> array.ts<pre><code>[class]{}-[func]{extend}\n</code></pre> array.dart<pre><code>[class]{}-[func]{extend}\n</code></pre> array.rs<pre><code>[class]{}-[func]{extend}\n</code></pre> array.c<pre><code>[class]{}-[func]{extend}\n</code></pre> array.zig<pre><code>[class]{}-[func]{extend}\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\u79cd\u505a\u6cd5\u5305\u542b\u4e30\u5bcc\u7684\u5148\u9a8c\u4fe1\u606f\uff0c\u7cfb\u7edf\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u4fe1\u606f\u6765\u4f18\u5316\u6570\u636e\u7ed3\u6784\u7684\u64cd\u4f5c\u6548\u7387\u3002</p> <ul> <li>\u7a7a\u95f4\u6548\u7387\u9ad8: \u6570\u7ec4\u4e3a\u6570\u636e\u5206\u914d\u4e86\u8fde\u7eed\u7684\u5185\u5b58\u5757\uff0c\u65e0\u987b\u989d\u5916\u7684\u7ed3\u6784\u5f00\u9500\u3002</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\u5176\u5b58\u5728\u4ee5\u4e0b\u7f3a\u70b9\u3002</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\u3002</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\u901a\u8fc7\u5b83\u53ef\u4ee5\u4ece\u5f53\u524d\u8282\u70b9\u8bbf\u95ee\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002</p> <p>\u94fe\u8868\u7684\u8bbe\u8ba1\u4f7f\u5f97\u5404\u4e2a\u8282\u70b9\u53ef\u4ee5\u88ab\u5206\u6563\u5b58\u50a8\u5728\u5185\u5b58\u5404\u5904\uff0c\u5b83\u4eec\u7684\u5185\u5b58\u5730\u5740\u662f\u65e0\u987b\u8fde\u7eed\u7684\u3002</p> <p></p> <p> \u56fe 4-5 \u00a0 \u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f </p> <p>\u89c2\u5bdf\u56fe 4-5 \uff0c\u94fe\u8868\u7684\u7ec4\u6210\u5355\u4f4d\u662f\u300c\u8282\u70b9 node\u300d\u5bf9\u8c61\u3002\u6bcf\u4e2a\u8282\u70b9\u90fd\u5305\u542b\u4e24\u9879\u6570\u636e\uff1a\u8282\u70b9\u7684\u201c\u503c\u201d\u548c\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u201c\u5f15\u7528\u201d\u3002</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\u3001C++ \u548c Python \u4e2d\u5206\u522b\u88ab\u8bb0\u4e3a \\(\\text{null}\\)\u3001\\(\\text{nullptr}\\) \u548c \\(\\text{None}\\) \u3002</li> <li>\u5728 C\u3001C++\u3001Go \u548c Rust \u7b49\u652f\u6301\u6307\u9488\u7684\u8bed\u8a00\u4e2d\uff0c\u4e0a\u8ff0\u7684\u201c\u5f15\u7528\u201d\u5e94\u88ab\u66ff\u6362\u4e3a\u201c\u6307\u9488\u201d\u3002</li> </ul> <p>\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u94fe\u8868\u8282\u70b9 <code>ListNode</code> \u9664\u4e86\u5305\u542b\u503c\uff0c\u8fd8\u9700\u989d\u5916\u4fdd\u5b58\u4e00\u4e2a\u5f15\u7528\uff08\u6307\u9488\uff09\u3002\u56e0\u6b64\u5728\u76f8\u540c\u6570\u636e\u91cf\u4e0b\uff0c\u94fe\u8868\u6bd4\u6570\u7ec4\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig <pre><code>class ListNode:\n\"\"\"\u94fe\u8868\u8282\u70b9\u7c7b\"\"\"\ndef __init__(self, val: int):\nself.val: int = val # \u8282\u70b9\u503c\nself.next: ListNode | None = None # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\n</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>/* \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\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 */\ntype ListNode struct {\nVal int // \u8282\u70b9\u503c\nNext *ListNode // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\n}\n// NewListNode \u6784\u9020\u51fd\u6570\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u94fe\u8868\nfunc NewListNode(val int) *ListNode {\nreturn &ListNode{\nVal: val,\nNext: nil,\n}\n}\n</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 */\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\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<Rc<RefCell<ListNode>>>, // \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u6307\u9488\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->val = val;\nnode->next = NULL;\nreturn node;\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>"},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1 \u00a0 \u94fe\u8868\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u94fe\u8868","text":"<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> PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.py<pre><code># \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4\n# \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n</code></pre> linked_list.cpp<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode* n0 = new ListNode(1);\nListNode* n1 = new ListNode(3);\nListNode* n2 = new ListNode(2);\nListNode* n3 = new ListNode(5);\nListNode* n4 = new ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n</code></pre> linked_list.java<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.cs<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.go<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n</code></pre> linked_list.swift<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nlet n0 = ListNode(x: 1)\nlet n1 = ListNode(x: 3)\nlet n2 = ListNode(x: 2)\nlet n3 = ListNode(x: 5)\nlet n4 = ListNode(x: 4)\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n</code></pre> linked_list.js<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.ts<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.dart<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\\\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode n0 = ListNode(1);\nListNode n1 = ListNode(3);\nListNode n2 = ListNode(2);\nListNode n3 = ListNode(5);\nListNode n4 = ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.rs<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n</code></pre> linked_list.c<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u8282\u70b9\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n</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 = &n1;\nn1.next = &n2;\nn2.next = &n3;\nn3.next = &n4;\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> \u548c <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":"2. \u00a0 \u63d2\u5165\u8282\u70b9","text":"<p>\u5728\u94fe\u8868\u4e2d\u63d2\u5165\u8282\u70b9\u975e\u5e38\u5bb9\u6613\u3002\u5982\u56fe 4-6 \u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u5728\u76f8\u90bb\u7684\u4e24\u4e2a\u8282\u70b9 <code>n0</code> \u548c <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 4-6 \u00a0 \u94fe\u8868\u63d2\u5165\u8282\u70b9\u793a\u4f8b </p> PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.py<pre><code>[class]{}-[func]{insert}\n</code></pre> linked_list.cpp<pre><code>[class]{}-[func]{insert}\n</code></pre> linked_list.java<pre><code>[class]{linked_list}-[func]{insert}\n</code></pre> linked_list.cs<pre><code>[class]{linked_list}-[func]{insert}\n</code></pre> linked_list.go<pre><code>[class]{}-[func]{insertNode}\n</code></pre> linked_list.swift<pre><code>[class]{}-[func]{insert}\n</code></pre> linked_list.js<pre><code>[class]{}-[func]{insert}\n</code></pre> linked_list.ts<pre><code>[class]{}-[func]{insert}\n</code></pre> linked_list.dart<pre><code>[class]{}-[func]{insert}\n</code></pre> linked_list.rs<pre><code>[class]{}-[func]{insert}\n</code></pre> linked_list.c<pre><code>[class]{}-[func]{insert}\n</code></pre> linked_list.zig<pre><code>[class]{}-[func]{insert}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/linked_list/#3","title":"3. \u00a0 \u5220\u9664\u8282\u70b9","text":"<p>\u5982\u56fe 4-7 \u6240\u793a\uff0c\u5728\u94fe\u8868\u4e2d\u5220\u9664\u8282\u70b9\u4e5f\u975e\u5e38\u65b9\u4fbf\uff0c\u53ea\u9700\u6539\u53d8\u4e00\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\u3002</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 4-7 \u00a0 \u94fe\u8868\u5220\u9664\u8282\u70b9 </p> PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.py<pre><code>[class]{}-[func]{remove}\n</code></pre> linked_list.cpp<pre><code>[class]{}-[func]{remove}\n</code></pre> linked_list.java<pre><code>[class]{linked_list}-[func]{remove}\n</code></pre> linked_list.cs<pre><code>[class]{linked_list}-[func]{remove}\n</code></pre> linked_list.go<pre><code>[class]{}-[func]{removeNode}\n</code></pre> linked_list.swift<pre><code>[class]{}-[func]{remove}\n</code></pre> linked_list.js<pre><code>[class]{}-[func]{remove}\n</code></pre> linked_list.ts<pre><code>[class]{}-[func]{remove}\n</code></pre> linked_list.dart<pre><code>[class]{}-[func]{remove}\n</code></pre> linked_list.rs<pre><code>[class]{}-[func]{remove}\n</code></pre> linked_list.c<pre><code>[class]{}-[func]{removeNode}\n</code></pre> linked_list.zig<pre><code>[class]{}-[func]{remove}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/linked_list/#4","title":"4. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.py<pre><code>[class]{}-[func]{access}\n</code></pre> linked_list.cpp<pre><code>[class]{}-[func]{access}\n</code></pre> linked_list.java<pre><code>[class]{linked_list}-[func]{access}\n</code></pre> linked_list.cs<pre><code>[class]{linked_list}-[func]{access}\n</code></pre> linked_list.go<pre><code>[class]{}-[func]{access}\n</code></pre> linked_list.swift<pre><code>[class]{}-[func]{access}\n</code></pre> linked_list.js<pre><code>[class]{}-[func]{access}\n</code></pre> linked_list.ts<pre><code>[class]{}-[func]{access}\n</code></pre> linked_list.dart<pre><code>[class]{}-[func]{access}\n</code></pre> linked_list.rs<pre><code>[class]{}-[func]{access}\n</code></pre> linked_list.c<pre><code>[class]{}-[func]{access}\n</code></pre> linked_list.zig<pre><code>[class]{}-[func]{access}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/linked_list/#5","title":"5. \u00a0 \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\u7ebf\u6027\u67e5\u627e\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig linked_list.py<pre><code>[class]{}-[func]{find}\n</code></pre> linked_list.cpp<pre><code>[class]{}-[func]{find}\n</code></pre> linked_list.java<pre><code>[class]{linked_list}-[func]{find}\n</code></pre> linked_list.cs<pre><code>[class]{linked_list}-[func]{find}\n</code></pre> linked_list.go<pre><code>[class]{}-[func]{findNode}\n</code></pre> linked_list.swift<pre><code>[class]{}-[func]{find}\n</code></pre> linked_list.js<pre><code>[class]{}-[func]{find}\n</code></pre> linked_list.ts<pre><code>[class]{}-[func]{find}\n</code></pre> linked_list.dart<pre><code>[class]{}-[func]{find}\n</code></pre> linked_list.rs<pre><code>[class]{}-[func]{find}\n</code></pre> linked_list.c<pre><code>[class]{}-[func]{find}\n</code></pre> linked_list.zig<pre><code>[class]{}-[func]{find}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/linked_list/#422-vs","title":"4.2.2 \u00a0 \u6570\u7ec4 VS \u94fe\u8868","text":"<p>\u8868 4-1 \u603b\u7ed3\u5bf9\u6bd4\u4e86\u6570\u7ec4\u548c\u94fe\u8868\u7684\u5404\u9879\u7279\u70b9\u4e0e\u64cd\u4f5c\u6548\u7387\u3002\u7531\u4e8e\u5b83\u4eec\u91c7\u7528\u4e24\u79cd\u76f8\u53cd\u7684\u5b58\u50a8\u7b56\u7565\uff0c\u56e0\u6b64\u5404\u79cd\u6027\u8d28\u548c\u64cd\u4f5c\u6548\u7387\u4e5f\u5448\u73b0\u5bf9\u7acb\u7684\u7279\u70b9\u3002</p> <p> \u8868 4-1 \u00a0 \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 \u5206\u6563\u5185\u5b58\u7a7a\u95f4 \u7f13\u5b58\u5c40\u90e8\u6027 \u53cb\u597d \u4e0d\u53cb\u597d \u5bb9\u91cf\u6269\u5c55 \u957f\u5ea6\u4e0d\u53ef\u53d8 \u53ef\u7075\u6d3b\u6269\u5c55 \u5185\u5b58\u6548\u7387 \u5360\u7528\u5185\u5b58\u5c11\u3001\u6d6a\u8d39\u90e8\u5206\u7a7a\u95f4 \u5360\u7528\u5185\u5b58\u591a \u8bbf\u95ee\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \u6dfb\u52a0\u5143\u7d20 \\(O(n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3 \u00a0 \u5e38\u89c1\u94fe\u8868\u7c7b\u578b","text":"<p>\u5982\u56fe 4-8 \u6240\u793a\uff0c\u5e38\u89c1\u7684\u94fe\u8868\u7c7b\u578b\u5305\u62ec\u4e09\u79cd\u3002</p> <ul> <li>\u5355\u5411\u94fe\u8868\uff1a\u5373\u4e0a\u8ff0\u4ecb\u7ecd\u7684\u666e\u901a\u94fe\u8868\u3002\u5355\u5411\u94fe\u8868\u7684\u8282\u70b9\u5305\u542b\u503c\u548c\u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\u4e24\u9879\u6570\u636e\u3002\u6211\u4eec\u5c06\u9996\u4e2a\u8282\u70b9\u79f0\u4e3a\u5934\u8282\u70b9\uff0c\u5c06\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u79f0\u4e3a\u5c3e\u8282\u70b9\uff0c\u5c3e\u8282\u70b9\u6307\u5411\u7a7a \\(\\text{None}\\) \u3002</li> <li>\u73af\u5f62\u94fe\u8868\uff1a\u5982\u679c\u6211\u4eec\u4ee4\u5355\u5411\u94fe\u8868\u7684\u5c3e\u8282\u70b9\u6307\u5411\u5934\u8282\u70b9\uff08\u5373\u9996\u5c3e\u76f8\u63a5\uff09\uff0c\u5219\u5f97\u5230\u4e00\u4e2a\u73af\u5f62\u94fe\u8868\u3002\u5728\u73af\u5f62\u94fe\u8868\u4e2d\uff0c\u4efb\u610f\u8282\u70b9\u90fd\u53ef\u4ee5\u89c6\u4f5c\u5934\u8282\u70b9\u3002</li> <li>\u53cc\u5411\u94fe\u8868\uff1a\u4e0e\u5355\u5411\u94fe\u8868\u76f8\u6bd4\uff0c\u53cc\u5411\u94fe\u8868\u8bb0\u5f55\u4e86\u4e24\u4e2a\u65b9\u5411\u7684\u5f15\u7528\u3002\u53cc\u5411\u94fe\u8868\u7684\u8282\u70b9\u5b9a\u4e49\u540c\u65f6\u5305\u542b\u6307\u5411\u540e\u7ee7\u8282\u70b9\uff08\u4e0b\u4e00\u4e2a\u8282\u70b9\uff09\u548c\u524d\u9a71\u8282\u70b9\uff08\u4e0a\u4e00\u4e2a\u8282\u70b9\uff09\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\u3002\u76f8\u8f83\u4e8e\u5355\u5411\u94fe\u8868\uff0c\u53cc\u5411\u94fe\u8868\u66f4\u5177\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u671d\u4e24\u4e2a\u65b9\u5411\u904d\u5386\u94fe\u8868\uff0c\u4f46\u76f8\u5e94\u5730\u4e5f\u9700\u8981\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002</li> </ul> PythonC++JavaC#GoSwiftJSTSDartRustCZig <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: ListNode | None = None # \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u5f15\u7528\nself.prev: ListNode | None = None # \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u5f15\u7528\n</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>/* \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\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 */\ntype DoublyListNode struct {\nVal int // \u8282\u70b9\u503c\nNext *DoublyListNode // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\nPrev *DoublyListNode // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n}\n// NewDoublyListNode \u521d\u59cb\u5316\nfunc NewDoublyListNode(val int) *DoublyListNode {\nreturn &DoublyListNode{\nVal: val,\nNext: nil,\nPrev: nil,\n}\n}\n</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 */\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\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<Rc<RefCell<ListNode>>>, // \u6307\u5411\u540e\u7ee7\u8282\u70b9\u7684\u6307\u9488\nprev: Option<Rc<RefCell<ListNode>>>, // \u6307\u5411\u524d\u9a71\u8282\u70b9\u7684\u6307\u9488\n}\n/* \u6784\u9020\u51fd\u6570 */\nimpl ListNode {\nfn new(val: i32) -> Self {\nListNode {\nval,\nnext: None,\nprev: None,\n}\n}\n}\n</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->val = val;\nnode->next = NULL;\nnode->prev = NULL;\nreturn node;\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> <p></p> <p> \u56fe 4-8 \u00a0 \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\u54c8\u5e0c\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>\u54c8\u5e0c\u8868\uff1a\u94fe\u5730\u5740\u6cd5\u662f\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u7684\u4e3b\u6d41\u65b9\u6848\u4e4b\u4e00\uff0c\u5728\u8be5\u65b9\u6848\u4e2d\uff0c\u6240\u6709\u51b2\u7a81\u7684\u5143\u7d20\u90fd\u4f1a\u88ab\u653e\u5230\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002</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\u987b\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":"1. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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<int> list1;\n// \u6709\u521d\u59cb\u503c\nvector<int> list = { 1, 3, 2, 5, 4 };\n</code></pre> list.java<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<Integer> list1 = new ArrayList<>();\n// \u6709\u521d\u59cb\u503c\uff08\u6ce8\u610f\u6570\u7ec4\u7684\u5143\u7d20\u7c7b\u578b\u9700\u4e3a int[] \u7684\u5305\u88c5\u7c7b Integer[]\uff09\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList<Integer> list = new ArrayList<>(Arrays.asList(numbers));\n</code></pre> list.cs<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> list1 = new ();\n// \u6709\u521d\u59cb\u503c\nint[] numbers = new int[] { 1, 3, 2, 5, 4 };\nList<int> list = numbers.ToList();\n</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.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.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.dart<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nList<int> list1 = [];\n// \u6709\u521d\u59cb\u503c\nList<int> list = [1, 3, 2, 5, 4];\n</code></pre> list.rs<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet list1: Vec<i32> = Vec::new();\n// \u6709\u521d\u59cb\u503c\nlet list2: Vec<i32> = vec![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.zig<pre><code>// \u521d\u59cb\u5316\u5217\u8868\nvar list = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer list.deinit();\ntry list.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });\n</code></pre>"},{"location":"chapter_array_and_linkedlist/list/#2","title":"2. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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_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.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.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.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> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\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>"},{"location":"chapter_array_and_linkedlist/list/#3","title":"3. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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_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.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.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.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> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\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>"},{"location":"chapter_array_and_linkedlist/list/#4","title":"4. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.cpp<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < list.size(); i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int n : list) {\ncount++;\n}\n</code></pre> list.java<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < list.size(); i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int n : list) {\ncount++;\n}\n</code></pre> list.cs<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < 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_test.go<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\ncount := 0\nfor i := 0; i < len(list); i++ {\ncount++\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor range list {\ncount++\n}\n</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.js<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < list.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const n of list) {\ncount++;\n}\n</code></pre> list.ts<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < list.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (const n of list) {\ncount++;\n}\n</code></pre> list.dart<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nint count = 0;\nfor (int i = 0; i < list.length; i++) {\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nfor (int n in list) {\ncount++;\n}\n</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> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\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 < 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>"},{"location":"chapter_array_and_linkedlist/list/#5","title":"5. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.cpp<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nvector<int> list1 = { 6, 8, 7, 10, 9 };\n// \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\nlist.insert(list.end(), list1.begin(), list1.end());\n</code></pre> list.java<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<Integer> list1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nlist.addAll(list1); // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list.cs<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> list1 = new() { 6, 8, 7, 10, 9 };\nlist.AddRange(list1); // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</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.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.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.dart<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nList<int> list1 = [6, 8, 7, 10, 9];\nlist.addAll(list1); // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list.rs<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet list1: Vec<i32> = vec![6, 8, 7, 10, 9];\nlist.extend(list1);\n</code></pre> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\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(&[_]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>"},{"location":"chapter_array_and_linkedlist/list/#6","title":"6. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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.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.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_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.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.js<pre><code>/* \u6392\u5e8f\u5217\u8868 */ list.sort((a, b) => a - b); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.ts<pre><code>/* \u6392\u5e8f\u5217\u8868 */\nlist.sort((a, b) => a - b); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</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> list.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u52a8\u6001\u6570\u7ec4\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>"},{"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\u3001C++\u3001Python \u7b49\u3002\u5b83\u4eec\u7684\u5b9e\u73b0\u6bd4\u8f83\u590d\u6742\uff0c\u5404\u4e2a\u53c2\u6570\u7684\u8bbe\u5b9a\u4e5f\u975e\u5e38\u6709\u8003\u7a76\uff0c\u4f8b\u5982\u521d\u59cb\u5bb9\u91cf\u3001\u6269\u5bb9\u500d\u6570\u7b49\u3002\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u67e5\u9605\u6e90\u7801\u8fdb\u884c\u5b66\u4e60\u3002</p> <p>\u4e3a\u4e86\u52a0\u6df1\u5bf9\u5217\u8868\u5de5\u4f5c\u539f\u7406\u7684\u7406\u89e3\uff0c\u6211\u4eec\u5c1d\u8bd5\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7248\u5217\u8868\uff0c\u5305\u62ec\u4ee5\u4e0b\u4e09\u4e2a\u91cd\u70b9\u8bbe\u8ba1\u3002</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 <code>size</code> \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig my_list.py<pre><code>[class]{MyList}-[func]{}\n</code></pre> my_list.cpp<pre><code>[class]{MyList}-[func]{}\n</code></pre> my_list.java<pre><code>[class]{MyList}-[func]{}\n</code></pre> my_list.cs<pre><code>[class]{MyList}-[func]{}\n</code></pre> my_list.go<pre><code>[class]{myList}-[func]{}\n</code></pre> my_list.swift<pre><code>[class]{MyList}-[func]{}\n</code></pre> my_list.js<pre><code>[class]{MyList}-[func]{}\n</code></pre> my_list.ts<pre><code>[class]{MyList}-[func]{}\n</code></pre> my_list.dart<pre><code>[class]{MyList}-[func]{}\n</code></pre> my_list.rs<pre><code>[class]{MyList}-[func]{}\n</code></pre> my_list.c<pre><code>[class]{myList}-[func]{}\n</code></pre> my_list.zig<pre><code>[class]{MyList}-[func]{}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/summary/","title":"4.4 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_array_and_linkedlist/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"<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\u5206\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/#2-q-a","title":"2. \u00a0 Q & 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>\u5b58\u50a8\u5728\u6808\u4e0a\u548c\u5806\u4e0a\u7684\u6570\u7ec4\u90fd\u88ab\u5b58\u50a8\u5728\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\u5185\uff0c\u6570\u636e\u64cd\u4f5c\u6548\u7387\u662f\u57fa\u672c\u4e00\u81f4\u7684\u3002\u7136\u800c\uff0c\u6808\u548c\u5806\u5177\u6709\u5404\u81ea\u7684\u7279\u70b9\uff0c\u4ece\u800c\u5bfc\u81f4\u4ee5\u4e0b\u4e0d\u540c\u70b9\u3002</p> <ol> <li>\u5206\u914d\u548c\u91ca\u653e\u6548\u7387\uff1a\u6808\u662f\u4e00\u5757\u8f83\u5c0f\u7684\u5185\u5b58\uff0c\u5206\u914d\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u5b8c\u6210\uff1b\u800c\u5806\u5185\u5b58\u76f8\u5bf9\u66f4\u5927\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u52a8\u6001\u5206\u914d\uff0c\u66f4\u5bb9\u6613\u788e\u7247\u5316\u3002\u56e0\u6b64\uff0c\u5806\u4e0a\u7684\u5206\u914d\u548c\u91ca\u653e\u64cd\u4f5c\u901a\u5e38\u6bd4\u6808\u4e0a\u7684\u6162\u3002</li> <li>\u5927\u5c0f\u9650\u5236\uff1a\u6808\u5185\u5b58\u76f8\u5bf9\u8f83\u5c0f\uff0c\u5806\u7684\u5927\u5c0f\u4e00\u822c\u53d7\u9650\u4e8e\u53ef\u7528\u5185\u5b58\u3002\u56e0\u6b64\u5806\u66f4\u52a0\u9002\u5408\u5b58\u50a8\u5927\u578b\u6570\u7ec4\u3002</li> <li>\u7075\u6d3b\u6027\uff1a\u6808\u4e0a\u7684\u6570\u7ec4\u7684\u5927\u5c0f\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u786e\u5b9a\uff0c\u800c\u5806\u4e0a\u7684\u6570\u7ec4\u7684\u5927\u5c0f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u786e\u5b9a\u3002</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\u3001double\u3001string\u3001object \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\u957f\u5ea6\u7684\u5143\u7d20\u3002</p> <pre><code># \u5143\u7d20\u5185\u5b58\u5730\u5740 = \u6570\u7ec4\u5185\u5b58\u5730\u5740 + \u5143\u7d20\u957f\u5ea6 * \u5143\u7d20\u7d22\u5f15\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\u3001Python\u3001Go \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 \u548c 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\u7684\u793a\u610f\u56fe\u53ea\u662f\u5b9a\u6027\u8868\u793a\uff0c\u5b9a\u91cf\u8868\u793a\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u5206\u6790\u3002</p> <ul> <li>\u4e0d\u540c\u7c7b\u578b\u7684\u7ed3\u70b9\u503c\u5360\u7528\u7684\u7a7a\u95f4\u662f\u4e0d\u540c\u7684\uff0c\u6bd4\u5982 int\u3001long\u3001double \u548c\u5b9e\u4f8b\u5bf9\u8c61\u7b49\u3002</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\u987b\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\uff0c\u4e3b\u8981\u6709\u4e24\u4e2a\u539f\u56e0\u3002</p> <ul> <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> </ul> <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":"\u7b2c 13 \u7ae0 \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\u201c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u201d\u6765\u904d\u5386\u89e3\u7a7a\u95f4\u3002\u5728\u4e8c\u53c9\u6811\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u63d0\u5230\u524d\u5e8f\u3001\u4e2d\u5e8f\u548c\u540e\u5e8f\u904d\u5386\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5229\u7528\u524d\u5e8f\u904d\u5386\u6784\u9020\u4e00\u4e2a\u56de\u6eaf\u95ee\u9898\uff0c\u9010\u6b65\u4e86\u89e3\u56de\u6eaf\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002</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\u76f8\u5173\u8fc7\u7a0b\u5b9e\u73b0\u5982\u56fe 13-1 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig preorder_traversal_i_compact.py<pre><code>[class]{}-[func]{pre_order}\n</code></pre> preorder_traversal_i_compact.cpp<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_i_compact.java<pre><code>[class]{preorder_traversal_i_compact}-[func]{preOrder}\n</code></pre> preorder_traversal_i_compact.cs<pre><code>[class]{preorder_traversal_i_compact}-[func]{preOrder}\n</code></pre> preorder_traversal_i_compact.go<pre><code>[class]{}-[func]{preOrderI}\n</code></pre> preorder_traversal_i_compact.swift<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_i_compact.js<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_i_compact.ts<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_i_compact.dart<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_i_compact.rs<pre><code>[class]{}-[func]{pre_order}\n</code></pre> preorder_traversal_i_compact.c<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_i_compact.zig<pre><code>[class]{}-[func]{preOrder}\n</code></pre> <p></p> <p> \u56fe 13-1 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig preorder_traversal_ii_compact.py<pre><code>[class]{}-[func]{pre_order}\n</code></pre> preorder_traversal_ii_compact.cpp<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_ii_compact.java<pre><code>[class]{preorder_traversal_ii_compact}-[func]{preOrder}\n</code></pre> preorder_traversal_ii_compact.cs<pre><code>[class]{preorder_traversal_ii_compact}-[func]{preOrder}\n</code></pre> preorder_traversal_ii_compact.go<pre><code>[class]{}-[func]{preOrderII}\n</code></pre> preorder_traversal_ii_compact.swift<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_ii_compact.js<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_ii_compact.ts<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_ii_compact.dart<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_ii_compact.rs<pre><code>[class]{}-[func]{pre_order}\n</code></pre> preorder_traversal_ii_compact.c<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_ii_compact.zig<pre><code>[class]{}-[func]{preOrder}\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\u56fe 13-2 \u6240\u793a\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5c1d\u8bd5\u548c\u56de\u9000\u7406\u89e3\u4e3a\u201c\u524d\u8fdb\u201d\u4e0e\u201c\u64a4\u9500\u201d\uff0c\u4e24\u4e2a\u64cd\u4f5c\u662f\u4e92\u4e3a\u9006\u5411\u7684\u3002</p> <1><2><3><4><5><6><7><8><9><10><11> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 13-2 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig preorder_traversal_iii_compact.py<pre><code>[class]{}-[func]{pre_order}\n</code></pre> preorder_traversal_iii_compact.cpp<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_iii_compact.java<pre><code>[class]{preorder_traversal_iii_compact}-[func]{preOrder}\n</code></pre> preorder_traversal_iii_compact.cs<pre><code>[class]{preorder_traversal_iii_compact}-[func]{preOrder}\n</code></pre> preorder_traversal_iii_compact.go<pre><code>[class]{}-[func]{preOrderIII}\n</code></pre> preorder_traversal_iii_compact.swift<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_iii_compact.js<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_iii_compact.ts<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_iii_compact.dart<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_iii_compact.rs<pre><code>[class]{}-[func]{pre_order}\n</code></pre> preorder_traversal_iii_compact.c<pre><code>[class]{}-[func]{preOrder}\n</code></pre> preorder_traversal_iii_compact.zig<pre><code>[class]{}-[func]{preOrder}\n</code></pre> <p>\u526a\u679d\u662f\u4e00\u4e2a\u975e\u5e38\u5f62\u8c61\u7684\u540d\u8bcd\u3002\u5982\u56fe 13-3 \u6240\u793a\uff0c\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u201c\u526a\u6389\u201d\u4e86\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\u7684\u641c\u7d22\u5206\u652f\uff0c\u907f\u514d\u8bb8\u591a\u65e0\u610f\u4e49\u7684\u5c1d\u8bd5\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u641c\u7d22\u6548\u7387\u3002</p> <p></p> <p> \u56fe 13-3 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig <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 */\nvoid backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Choice choice : choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List<Choice> choices, List<State> res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Choice choice : choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nvoid backtrack(State state, List<Choice> choices, List<State> res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nforeach (Choice choice in choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</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 */\nfunc backtrack(state: inout State, choices: [Choice], res: inout [State]) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif isSolution(state: state) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state: state, res: &res)\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif isValid(state: state, choice: choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state: &state, choice: choice)\nbacktrack(state: &state, choices: choices, res: &res)\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state: &state, choice: choice)\n}\n}\n}\n</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, List<Choice>, List<State> res) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif (isSolution(state)) {\n// \u8bb0\u5f55\u89e3\nrecordSolution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor (Choice choice in choices) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, choice)) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, choice);\n}\n}\n}\n</code></pre> <pre><code>/* \u56de\u6eaf\u7b97\u6cd5\u6846\u67b6 */\nfn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {\n// \u5224\u65ad\u662f\u5426\u4e3a\u89e3\nif is_solution(state) {\n// \u8bb0\u5f55\u89e3\nrecord_solution(state, res);\n// \u505c\u6b62\u7ee7\u7eed\u641c\u7d22\nreturn;\n}\n// \u904d\u5386\u6240\u6709\u9009\u62e9\nfor choice in choices {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif is_valid(state, choice) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmake_choice(state, choice);\nbacktrack(state, choices, res);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundo_choice(state, choice);\n}\n}\n}\n</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 < numChoices; i++) {\n// \u526a\u679d\uff1a\u5224\u65ad\u9009\u62e9\u662f\u5426\u5408\u6cd5\nif (isValid(state, &choices[i])) {\n// \u5c1d\u8bd5\uff1a\u505a\u51fa\u9009\u62e9\uff0c\u66f4\u65b0\u72b6\u6001\nmakeChoice(state, &choices[i]);\nbacktrack(state, choices, numChoices, res, numRes);\n// \u56de\u9000\uff1a\u64a4\u9500\u9009\u62e9\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\nundoChoice(state, &choices[i]);\n}\n}\n}\n</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> PythonC++JavaC#GoSwiftJSTSDartRustCZig preorder_traversal_iii_template.py<pre><code>[class]{}-[func]{is_solution}\n[class]{}-[func]{record_solution}\n[class]{}-[func]{is_valid}\n[class]{}-[func]{make_choice}\n[class]{}-[func]{undo_choice}\n[class]{}-[func]{backtrack}\n</code></pre> preorder_traversal_iii_template.cpp<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.java<pre><code>[class]{preorder_traversal_iii_template}-[func]{isSolution}\n[class]{preorder_traversal_iii_template}-[func]{recordSolution}\n[class]{preorder_traversal_iii_template}-[func]{isValid}\n[class]{preorder_traversal_iii_template}-[func]{makeChoice}\n[class]{preorder_traversal_iii_template}-[func]{undoChoice}\n[class]{preorder_traversal_iii_template}-[func]{backtrack}\n</code></pre> preorder_traversal_iii_template.cs<pre><code>[class]{preorder_traversal_iii_template}-[func]{isSolution}\n[class]{preorder_traversal_iii_template}-[func]{recordSolution}\n[class]{preorder_traversal_iii_template}-[func]{isValid}\n[class]{preorder_traversal_iii_template}-[func]{makeChoice}\n[class]{preorder_traversal_iii_template}-[func]{undoChoice}\n[class]{preorder_traversal_iii_template}-[func]{backtrack}\n</code></pre> preorder_traversal_iii_template.go<pre><code>[class]{}-[func]{isSolution}\n[class]{}-[func]{recordSolution}\n[class]{}-[func]{isValid}\n[class]{}-[func]{makeChoice}\n[class]{}-[func]{undoChoice}\n[class]{}-[func]{backtrackIII}\n</code></pre> preorder_traversal_iii_template.swift<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.js<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.ts<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>[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.rs<pre><code>[class]{}-[func]{is_solution}\n[class]{}-[func]{record_solution}\n[class]{}-[func]{is_valid}\n[class]{}-[func]{make_choice}\n[class]{}-[func]{undo_choice}\n[class]{}-[func]{backtrack}\n</code></pre> preorder_traversal_iii_template.c<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.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> <p>\u6839\u636e\u9898\u610f\uff0c\u6211\u4eec\u5728\u627e\u5230\u503c\u4e3a \\(7\\) \u7684\u8282\u70b9\u540e\u5e94\u8be5\u7ee7\u7eed\u641c\u7d22\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u8bb0\u5f55\u89e3\u4e4b\u540e\u7684 <code>return</code> \u8bed\u53e5\u5220\u9664\u3002\u56fe 13-4 \u5bf9\u6bd4\u4e86\u4fdd\u7559\u6216\u5220\u9664 <code>return</code> \u8bed\u53e5\u7684\u641c\u7d22\u8fc7\u7a0b\u3002</p> <p></p> <p> \u56fe 13-4 \u00a0 \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> <p> \u8868 13-1 \u00a0 \u5e38\u89c1\u7684\u56de\u6eaf\u7b97\u6cd5\u672f\u8bed </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\\) \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\u7684\u6548\u7387\u4f18\u5316\u65b9\u6cd5\u6709\u4e24\u79cd\u3002</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\u3002</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\u56fe 13-15 \u6240\u793a\uff0c\u5f53 \\(n = 4\\) \u65f6\uff0c\u5171\u53ef\u4ee5\u627e\u5230\u4e24\u4e2a\u89e3\u3002\u4ece\u56de\u6eaf\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\\(n \\times n\\) \u5927\u5c0f\u7684\u68cb\u76d8\u5171\u6709 \\(n^2\\) \u4e2a\u683c\u5b50\uff0c\u7ed9\u51fa\u4e86\u6240\u6709\u7684\u9009\u62e9 <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 13-15 \u00a0 4 \u7687\u540e\u95ee\u9898\u7684\u89e3 </p> <p>\u56fe 13-16 \u5c55\u793a\u4e86\u672c\u9898\u7684\u4e09\u4e2a\u7ea6\u675f\u6761\u4ef6\uff1a\u591a\u4e2a\u7687\u540e\u4e0d\u80fd\u5728\u540c\u4e00\u884c\u3001\u540c\u4e00\u5217\u3001\u540c\u4e00\u5bf9\u89d2\u7ebf\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u89d2\u7ebf\u5206\u4e3a\u4e3b\u5bf9\u89d2\u7ebf <code>\\</code> \u548c\u6b21\u5bf9\u89d2\u7ebf <code>/</code> \u4e24\u79cd\u3002</p> <p></p> <p> \u56fe 13-16 \u00a0 n \u7687\u540e\u95ee\u9898\u7684\u7ea6\u675f\u6761\u4ef6 </p>"},{"location":"chapter_backtracking/n_queens_problem/#1","title":"1. \u00a0 \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\u56fe 13-17 \u6240\u793a\uff0c\u4e3a \\(4\\) \u7687\u540e\u95ee\u9898\u7684\u9010\u884c\u653e\u7f6e\u8fc7\u7a0b\u3002\u53d7\u753b\u5e45\u9650\u5236\uff0c\u56fe 13-17 \u4ec5\u5c55\u5f00\u4e86\u7b2c\u4e00\u884c\u7684\u5176\u4e2d\u4e00\u4e2a\u641c\u7d22\u5206\u652f\uff0c\u5e76\u4e14\u5c06\u4e0d\u6ee1\u8db3\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u65b9\u6848\u90fd\u8fdb\u884c\u4e86\u526a\u679d\u3002</p> <p></p> <p> \u56fe 13-17 \u00a0 \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":"2. \u00a0 \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\u56fe 13-18 \u6240\u793a\u7684\u6570\u7ec4 <code>diag1</code> \uff0c\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\u540c\u6837\u4e5f\u53ef\u4ee5\u501f\u52a9\u6570\u7ec4 <code>diag2</code> \u6765\u5904\u7406\u6b21\u5bf9\u89d2\u7ebf\u7ea6\u675f\u3002</p> <p></p> <p> \u56fe 13-18 \u00a0 \u5904\u7406\u5217\u7ea6\u675f\u548c\u5bf9\u89d2\u7ebf\u7ea6\u675f </p>"},{"location":"chapter_backtracking/n_queens_problem/#3","title":"3. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig n_queens.py<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{n_queens}\n</code></pre> n_queens.cpp<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n</code></pre> n_queens.java<pre><code>[class]{n_queens}-[func]{backtrack}\n[class]{n_queens}-[func]{nQueens}\n</code></pre> n_queens.cs<pre><code>[class]{n_queens}-[func]{backtrack}\n[class]{n_queens}-[func]{nQueens}\n</code></pre> n_queens.go<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n</code></pre> n_queens.swift<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n</code></pre> n_queens.js<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n</code></pre> n_queens.ts<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n</code></pre> n_queens.dart<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n</code></pre> n_queens.rs<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{n_queens}\n</code></pre> n_queens.c<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\n</code></pre> n_queens.zig<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{nQueens}\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\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u4e2a\u9009\u62e9\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n!)\\) \u3002\u5b9e\u9645\u4e0a\uff0c\u6839\u636e\u5bf9\u89d2\u7ebf\u7ea6\u675f\u7684\u526a\u679d\u4e5f\u80fd\u591f\u5927\u5e45\u5730\u7f29\u5c0f\u641c\u7d22\u7a7a\u95f4\uff0c\u56e0\u800c\u641c\u7d22\u6548\u7387\u5f80\u5f80\u4f18\u4e8e\u4ee5\u4e0a\u65f6\u95f4\u590d\u6742\u5ea6\u3002</p> <p>\u6570\u7ec4 <code>state</code> \u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff0c\u6570\u7ec4 <code>cols</code>\u3001<code>diags1</code> \u548c <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>\u8868 13-2 \u5217\u4e3e\u4e86\u51e0\u4e2a\u793a\u4f8b\u6570\u636e\uff0c\u5305\u62ec\u8f93\u5165\u6570\u7ec4\u548c\u5bf9\u5e94\u7684\u6240\u6709\u6392\u5217\u3002</p> <p> \u8868 13-2 \u00a0 \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\u56fe 13-5 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u641c\u7d22\u8fc7\u7a0b\u5c55\u5f00\u6210\u4e00\u4e2a\u9012\u5f52\u6811\uff0c\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u5f53\u524d\u72b6\u6001 <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 13-5 \u00a0 \u5168\u6392\u5217\u7684\u9012\u5f52\u6811 </p>"},{"location":"chapter_backtracking/permutations_problem/#1","title":"1. \u00a0 \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\uff0c\u5e76\u57fa\u4e8e\u5b83\u5b9e\u73b0\u4ee5\u4e0b\u526a\u679d\u64cd\u4f5c\u3002</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\u56fe 13-6 \u6240\u793a\uff0c\u5047\u8bbe\u6211\u4eec\u7b2c\u4e00\u8f6e\u9009\u62e9 1 \uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9 3 \uff0c\u7b2c\u4e09\u8f6e\u9009\u62e9 2 \uff0c\u5219\u9700\u8981\u5728\u7b2c\u4e8c\u8f6e\u526a\u6389\u5143\u7d20 1 \u7684\u5206\u652f\uff0c\u5728\u7b2c\u4e09\u8f6e\u526a\u6389\u5143\u7d20 1 \u548c\u5143\u7d20 3 \u7684\u5206\u652f\u3002</p> <p></p> <p> \u56fe 13-6 \u00a0 \u5168\u6392\u5217\u526a\u679d\u793a\u4f8b </p> <p>\u89c2\u5bdf\u56fe 13-6 \u53d1\u73b0\uff0c\u8be5\u526a\u679d\u64cd\u4f5c\u5c06\u641c\u7d22\u7a7a\u95f4\u5927\u5c0f\u4ece \\(O(n^n)\\) \u964d\u4f4e\u81f3 \\(O(n!)\\) \u3002</p>"},{"location":"chapter_backtracking/permutations_problem/#2","title":"2. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig permutations_i.py<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutations_i}\n</code></pre> permutations_i.cpp<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\n</code></pre> permutations_i.java<pre><code>[class]{permutations_i}-[func]{backtrack}\n[class]{permutations_i}-[func]{permutationsI}\n</code></pre> permutations_i.cs<pre><code>[class]{permutations_i}-[func]{backtrack}\n[class]{permutations_i}-[func]{permutationsI}\n</code></pre> permutations_i.go<pre><code>[class]{}-[func]{backtrackI}\n[class]{}-[func]{permutationsI}\n</code></pre> permutations_i.swift<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\n</code></pre> permutations_i.js<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\n</code></pre> permutations_i.ts<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\n</code></pre> permutations_i.dart<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\n</code></pre> permutations_i.rs<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutations_i}\n</code></pre> permutations_i.c<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\n</code></pre> permutations_i.zig<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsI}\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\u56fe 13-7 \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 13-7 \u00a0 \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/#1_1","title":"1. \u00a0 \u76f8\u7b49\u5143\u7d20\u526a\u679d","text":"<p>\u89c2\u5bdf\u56fe 13-8 \uff0c\u5728\u7b2c\u4e00\u8f6e\u4e2d\uff0c\u9009\u62e9 \\(1\\) \u6216\u9009\u62e9 \\(\\hat{1}\\) \u662f\u7b49\u4ef7\u7684\uff0c\u5728\u8fd9\u4e24\u4e2a\u9009\u62e9\u4e4b\u4e0b\u751f\u6210\u7684\u6240\u6709\u6392\u5217\u90fd\u662f\u91cd\u590d\u7684\u3002\u56e0\u6b64\u5e94\u8be5\u628a \\(\\hat{1}\\) \u526a\u679d\u6389\u3002</p> <p>\u540c\u7406\uff0c\u5728\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(2\\) \u4e4b\u540e\uff0c\u7b2c\u4e8c\u8f6e\u9009\u62e9\u4e2d\u7684 \\(1\\) \u548c \\(\\hat{1}\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5206\u652f\uff0c\u56e0\u6b64\u4e5f\u5e94\u5c06\u7b2c\u4e8c\u8f6e\u7684 \\(\\hat{1}\\) \u526a\u679d\u3002</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 13-8 \u00a0 \u91cd\u590d\u6392\u5217\u526a\u679d </p>"},{"location":"chapter_backtracking/permutations_problem/#2_1","title":"2. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig permutations_ii.py<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutations_ii}\n</code></pre> permutations_ii.cpp<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n</code></pre> permutations_ii.java<pre><code>[class]{permutations_ii}-[func]{backtrack}\n[class]{permutations_ii}-[func]{permutationsII}\n</code></pre> permutations_ii.cs<pre><code>[class]{permutations_ii}-[func]{backtrack}\n[class]{permutations_ii}-[func]{permutationsII}\n</code></pre> permutations_ii.go<pre><code>[class]{}-[func]{backtrackII}\n[class]{}-[func]{permutationsII}\n</code></pre> permutations_ii.swift<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n</code></pre> permutations_ii.js<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n</code></pre> permutations_ii.ts<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n</code></pre> permutations_ii.dart<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n</code></pre> permutations_ii.rs<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutations_ii}\n</code></pre> permutations_ii.c<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\n</code></pre> permutations_ii.zig<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{permutationsII}\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\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/#3","title":"3. \u00a0 \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\u662f\u4e0d\u540c\u7684\u3002</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>\u56fe 13-9 \u5c55\u793a\u4e86\u4e24\u4e2a\u526a\u679d\u6761\u4ef6\u7684\u751f\u6548\u8303\u56f4\u3002\u6ce8\u610f\uff0c\u6811\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u9009\u62e9\uff0c\u4ece\u6839\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u7684\u5404\u4e2a\u8282\u70b9\u6784\u6210\u4e00\u4e2a\u6392\u5217\u3002</p> <p></p> <p> \u56fe 13-9 \u00a0 \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\u4ee5\u4e0b\u4e24\u70b9\u3002</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":"1. \u00a0 \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\u987b\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> PythonC++JavaC#GoSwiftJSTSDartRustCZig subset_sum_i_naive.py<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subset_sum_i_naive}\n</code></pre> subset_sum_i_naive.cpp<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumINaive}\n</code></pre> subset_sum_i_naive.java<pre><code>[class]{subset_sum_i_naive}-[func]{backtrack}\n[class]{subset_sum_i_naive}-[func]{subsetSumINaive}\n</code></pre> subset_sum_i_naive.cs<pre><code>[class]{subset_sum_i_naive}-[func]{backtrack}\n[class]{subset_sum_i_naive}-[func]{subsetSumINaive}\n</code></pre> subset_sum_i_naive.go<pre><code>[class]{}-[func]{backtrackSubsetSumINaive}\n[class]{}-[func]{subsetSumINaive}\n</code></pre> subset_sum_i_naive.swift<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumINaive}\n</code></pre> subset_sum_i_naive.js<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumINaive}\n</code></pre> subset_sum_i_naive.ts<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumINaive}\n</code></pre> subset_sum_i_naive.dart<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumINaive}\n</code></pre> subset_sum_i_naive.rs<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subset_sum_i_naive}\n</code></pre> subset_sum_i_naive.c<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumINaive}\n</code></pre> subset_sum_i_naive.zig<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumINaive}\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\u56fe 13-10 \u6240\u793a\uff0c\u5148\u9009 \\(4\\) \u540e\u9009 \\(5\\) \u4e0e\u5148\u9009 \\(5\\) \u540e\u9009 \\(4\\) \u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u5206\u652f\uff0c\u4f46\u4e24\u8005\u5bf9\u5e94\u540c\u4e00\u4e2a\u5b50\u96c6\u3002</p> <p></p> <p> \u56fe 13-10 \u00a0 \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\u6709\u4e24\u65b9\u9762\u539f\u56e0\u3002</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":"2. \u00a0 \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\u56fe 13-11 \uff0c\u91cd\u590d\u5b50\u96c6\u662f\u5728\u4ee5\u4e0d\u540c\u987a\u5e8f\u9009\u62e9\u6570\u7ec4\u5143\u7d20\u65f6\u4ea7\u751f\u7684\uff0c\u4f8b\u5982\u4ee5\u4e0b\u60c5\u51b5\u3002</p> <ol> <li>\u5f53\u7b2c\u4e00\u8f6e\u548c\u7b2c\u4e8c\u8f6e\u5206\u522b\u9009\u62e9 \\(3\\) \u548c \\(4\\) \u65f6\uff0c\u4f1a\u751f\u6210\u5305\u542b\u8fd9\u4e24\u4e2a\u5143\u7d20\u7684\u6240\u6709\u5b50\u96c6\uff0c\u8bb0\u4e3a \\([3, 4, \\dots]\\) \u3002</li> <li>\u4e4b\u540e\uff0c\u5f53\u7b2c\u4e00\u8f6e\u9009\u62e9 \\(4\\) \u65f6\uff0c\u5219\u7b2c\u4e8c\u8f6e\u5e94\u8be5\u8df3\u8fc7 \\(3\\) \uff0c\u56e0\u4e3a\u8be5\u9009\u62e9\u4ea7\u751f\u7684\u5b50\u96c6 \\([4, 3, \\dots]\\) \u548c <code>1.</code> \u4e2d\u751f\u6210\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002</li> </ol> <p>\u5728\u641c\u7d22\u4e2d\uff0c\u6bcf\u4e00\u5c42\u7684\u9009\u62e9\u90fd\u662f\u4ece\u5de6\u5230\u53f3\u88ab\u9010\u4e2a\u5c1d\u8bd5\u7684\uff0c\u56e0\u6b64\u8d8a\u9760\u53f3\u7684\u5206\u652f\u88ab\u526a\u6389\u7684\u8d8a\u591a\u3002</p> <ol> <li>\u524d\u4e24\u8f6e\u9009\u62e9 \\(3\\) \u548c \\(5\\) \uff0c\u751f\u6210\u5b50\u96c6 \\([3, 5, \\dots]\\) \u3002</li> <li>\u524d\u4e24\u8f6e\u9009\u62e9 \\(4\\) \u548c \\(5\\) \uff0c\u751f\u6210\u5b50\u96c6 \\([4, 5, \\dots]\\) \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, \\dots]\\) \u548c \\([5, 4, \\dots]\\) \u4e0e\u7b2c <code>1.</code> \u548c <code>2.</code> \u6b65\u4e2d\u63cf\u8ff0\u7684\u5b50\u96c6\u5b8c\u5168\u91cd\u590d\u3002</li> </ol> <p></p> <p> \u56fe 13-11 \u00a0 \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, \\dots, x_n]\\) \uff0c\u8bbe\u641c\u7d22\u8fc7\u7a0b\u4e2d\u7684\u9009\u62e9\u5e8f\u5217\u4e3a \\([x_{i_1}, x_{i_2}, \\dots, x_{i_m}]\\) \uff0c\u5219\u8be5\u9009\u62e9\u5e8f\u5217\u9700\u8981\u6ee1\u8db3 \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) \uff0c\u4e0d\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u9009\u62e9\u5e8f\u5217\u90fd\u4f1a\u9020\u6210\u91cd\u590d\uff0c\u5e94\u5f53\u526a\u679d\u3002</p>"},{"location":"chapter_backtracking/subset_sum_problem/#3","title":"3. \u00a0 \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 \\dots \\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\u4ee5\u4e0b\u4e24\u9879\u4f18\u5316\u3002</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> PythonC++JavaC#GoSwiftJSTSDartRustCZig subset_sum_i.py<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subset_sum_i}\n</code></pre> subset_sum_i.cpp<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumI}\n</code></pre> subset_sum_i.java<pre><code>[class]{subset_sum_i}-[func]{backtrack}\n[class]{subset_sum_i}-[func]{subsetSumI}\n</code></pre> subset_sum_i.cs<pre><code>[class]{subset_sum_i}-[func]{backtrack}\n[class]{subset_sum_i}-[func]{subsetSumI}\n</code></pre> subset_sum_i.go<pre><code>[class]{}-[func]{backtrackSubsetSumI}\n[class]{}-[func]{subsetSumI}\n</code></pre> subset_sum_i.swift<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumI}\n</code></pre> subset_sum_i.js<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumI}\n</code></pre> subset_sum_i.ts<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumI}\n</code></pre> subset_sum_i.dart<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumI}\n</code></pre> subset_sum_i.rs<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subset_sum_i}\n</code></pre> subset_sum_i.c<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumI}\n</code></pre> subset_sum_i.zig<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumI}\n</code></pre> <p>\u5982\u56fe 13-12 \u6240\u793a\uff0c\u4e3a\u5c06\u6570\u7ec4 \\([3, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u8f93\u5165\u5230\u4ee5\u4e0a\u4ee3\u7801\u540e\u7684\u6574\u4f53\u56de\u6eaf\u8fc7\u7a0b\u3002</p> <p></p> <p> \u56fe 13-12 \u00a0 \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\u5728\u56fe 13-13 \u4e2d\uff0c\u7b2c\u4e00\u8f6e\u5171\u6709\u4e09\u4e2a\u9009\u62e9\uff0c\u5176\u4e2d\u4e24\u4e2a\u90fd\u4e3a \\(4\\) \uff0c\u4f1a\u4ea7\u751f\u4e24\u4e2a\u91cd\u590d\u7684\u641c\u7d22\u5206\u652f\uff0c\u4ece\u800c\u8f93\u51fa\u91cd\u590d\u5b50\u96c6\uff1b\u540c\u7406\uff0c\u7b2c\u4e8c\u8f6e\u7684\u4e24\u4e2a \\(4\\) \u4e5f\u4f1a\u4ea7\u751f\u91cd\u590d\u5b50\u96c6\u3002</p> <p></p> <p> \u56fe 13-13 \u00a0 \u76f8\u7b49\u5143\u7d20\u5bfc\u81f4\u7684\u91cd\u590d\u5b50\u96c6 </p>"},{"location":"chapter_backtracking/subset_sum_problem/#1_1","title":"1. \u00a0 \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/#2_1","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCZig subset_sum_ii.py<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subset_sum_ii}\n</code></pre> subset_sum_ii.cpp<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumII}\n</code></pre> subset_sum_ii.java<pre><code>[class]{subset_sum_ii}-[func]{backtrack}\n[class]{subset_sum_ii}-[func]{subsetSumII}\n</code></pre> subset_sum_ii.cs<pre><code>[class]{subset_sum_ii}-[func]{backtrack}\n[class]{subset_sum_ii}-[func]{subsetSumII}\n</code></pre> subset_sum_ii.go<pre><code>[class]{}-[func]{backtrackSubsetSumII}\n[class]{}-[func]{subsetSumII}\n</code></pre> subset_sum_ii.swift<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumII}\n</code></pre> subset_sum_ii.js<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumII}\n</code></pre> subset_sum_ii.ts<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumII}\n</code></pre> subset_sum_ii.dart<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumII}\n</code></pre> subset_sum_ii.rs<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subset_sum_ii}\n</code></pre> subset_sum_ii.c<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumII}\n</code></pre> subset_sum_ii.zig<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{subsetSumII}\n</code></pre> <p>\u56fe 13-14 \u5c55\u793a\u4e86\u6570\u7ec4 \\([4, 4, 5]\\) \u548c\u76ee\u6807\u5143\u7d20 \\(9\\) \u7684\u56de\u6eaf\u8fc7\u7a0b\uff0c\u5171\u5305\u542b\u56db\u79cd\u526a\u679d\u64cd\u4f5c\u3002\u8bf7\u4f60\u5c06\u56fe\u793a\u4e0e\u4ee3\u7801\u6ce8\u91ca\u76f8\u7ed3\u5408\uff0c\u7406\u89e3\u6574\u4e2a\u641c\u7d22\u8fc7\u7a0b\uff0c\u4ee5\u53ca\u6bcf\u79cd\u526a\u679d\u64cd\u4f5c\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002</p> <p></p> <p> \u56fe 13-14 \u00a0 \u5b50\u96c6\u548c II \u56de\u6eaf\u8fc7\u7a0b </p>"},{"location":"chapter_backtracking/summary/","title":"13.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_backtracking/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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_backtracking/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"<p>\u600e\u4e48\u7406\u89e3\u56de\u6eaf\u548c\u9012\u5f52\u7684\u5173\u7cfb\uff1f</p> <p>\u603b\u7684\u6765\u770b\uff0c\u56de\u6eaf\u662f\u4e00\u79cd\u201c\u7b97\u6cd5\u7b56\u7565\u201d\uff0c\u800c\u9012\u5f52\u66f4\u50cf\u662f\u4e00\u4e2a\u201c\u5de5\u5177\u201d\u3002</p> <ul> <li>\u56de\u6eaf\u7b97\u6cd5\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\u3002\u7136\u800c\uff0c\u56de\u6eaf\u662f\u9012\u5f52\u7684\u5e94\u7528\u573a\u666f\u4e4b\u4e00\uff0c\u662f\u9012\u5f52\u5728\u641c\u7d22\u95ee\u9898\u4e2d\u7684\u5e94\u7528\u3002</li> <li>\u9012\u5f52\u7684\u7ed3\u6784\u4f53\u73b0\u4e86\u201c\u5b50\u95ee\u9898\u5206\u89e3\u201d\u7684\u89e3\u9898\u8303\u5f0f\uff0c\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u6cbb\u3001\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\uff08\u8bb0\u5fc6\u5316\u9012\u5f52\uff09\u7b49\u95ee\u9898\u3002</li> </ul>"},{"location":"chapter_computational_complexity/","title":"\u7b2c 2 \u7ae0 \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"<p>Abstract</p> <p>\u590d\u6742\u5ea6\u5206\u6790\u72b9\u5982\u6d69\u701a\u7684\u7b97\u6cd5\u5b87\u5b99\u4e2d\u7684\u65f6\u7a7a\u5411\u5bfc\u3002</p> <p>\u5b83\u5e26\u9886\u6211\u4eec\u5728\u65f6\u95f4\u4e0e\u7a7a\u95f4\u8fd9\u4e24\u4e2a\u7ef4\u5ea6\u4e0a\u6df1\u5165\u63a2\u7d22\uff0c\u5bfb\u627e\u66f4\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002</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 \u8fed\u4ee3\u4e0e\u9012\u5f52</li> <li>2.3 \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6</li> <li>2.4 \u00a0 \u7a7a\u95f4\u590d\u6742\u5ea6</li> <li>2.5 \u00a0 \u5c0f\u7ed3</li> </ul>"},{"location":"chapter_computational_complexity/iteration_and_recursion/","title":"2.2 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52","text":"<p>\u5728\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e2d\uff0c\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u662f\u5f88\u5e38\u89c1\u7684\uff0c\u5176\u4e0e\u7b97\u6cd5\u7684\u590d\u6742\u5ea6\u5bc6\u5207\u76f8\u5173\u3002\u800c\u8981\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u9009\u7528\u4e24\u79cd\u57fa\u672c\u7684\u7a0b\u5e8f\u7ed3\u6784\uff1a\u8fed\u4ee3\u548c\u9012\u5f52\u3002</p>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#221","title":"2.2.1 \u00a0 \u8fed\u4ee3","text":"<p>\u300c\u8fed\u4ee3 iteration\u300d\u662f\u4e00\u79cd\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u7684\u63a7\u5236\u7ed3\u6784\u3002\u5728\u8fed\u4ee3\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u5728\u6ee1\u8db3\u4e00\u5b9a\u7684\u6761\u4ef6\u4e0b\u91cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\uff0c\u76f4\u5230\u8fd9\u4e2a\u6761\u4ef6\u4e0d\u518d\u6ee1\u8db3\u3002</p>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for","title":"1. \u00a0 for \u5faa\u73af","text":"<p><code>for</code> \u5faa\u73af\u662f\u6700\u5e38\u89c1\u7684\u8fed\u4ee3\u5f62\u5f0f\u4e4b\u4e00\uff0c\u9002\u5408\u9884\u5148\u77e5\u9053\u8fed\u4ee3\u6b21\u6570\u65f6\u4f7f\u7528\u3002</p> <p>\u4ee5\u4e0b\u51fd\u6570\u57fa\u4e8e <code>for</code> \u5faa\u73af\u5b9e\u73b0\u4e86\u6c42\u548c \\(1 + 2 + \\dots + n\\) \uff0c\u6c42\u548c\u7ed3\u679c\u4f7f\u7528\u53d8\u91cf <code>res</code> \u8bb0\u5f55\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cPython \u4e2d <code>range(a, b)</code> \u5bf9\u5e94\u7684\u533a\u95f4\u662f\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u7684\uff0c\u5bf9\u5e94\u7684\u904d\u5386\u8303\u56f4\u4e3a \\(a, a + 1, \\dots, b-1\\) \u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig iteration.py<pre><code>[class]{}-[func]{for_loop}\n</code></pre> iteration.cpp<pre><code>[class]{}-[func]{forLoop}\n</code></pre> iteration.java<pre><code>[class]{iteration}-[func]{forLoop}\n</code></pre> iteration.cs<pre><code>[class]{iteration}-[func]{forLoop}\n</code></pre> iteration.go<pre><code>[class]{}-[func]{forLoop}\n</code></pre> iteration.swift<pre><code>[class]{}-[func]{forLoop}\n</code></pre> iteration.js<pre><code>[class]{}-[func]{forLoop}\n</code></pre> iteration.ts<pre><code>[class]{}-[func]{forLoop}\n</code></pre> iteration.dart<pre><code>[class]{}-[func]{forLoop}\n</code></pre> iteration.rs<pre><code>[class]{}-[func]{for_loop}\n</code></pre> iteration.c<pre><code>[class]{}-[func]{forLoop}\n</code></pre> iteration.zig<pre><code>[class]{}-[func]{forLoop}\n</code></pre> <p>\u56fe 2-1 \u5c55\u793a\u4e86\u8be5\u6c42\u548c\u51fd\u6570\u7684\u6d41\u7a0b\u6846\u56fe\u3002</p> <p></p> <p> \u56fe 2-1 \u00a0 \u6c42\u548c\u51fd\u6570\u7684\u6d41\u7a0b\u6846\u56fe </p> <p>\u6b64\u6c42\u548c\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8bf4\u6210\u201c\u7ebf\u6027\u5173\u7cfb\u201d\u3002\u5b9e\u9645\u4e0a\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u63cf\u8ff0\u7684\u5c31\u662f\u8fd9\u4e2a\u201c\u7ebf\u6027\u5173\u7cfb\u201d\u3002\u76f8\u5173\u5185\u5bb9\u5c06\u4f1a\u5728\u4e0b\u4e00\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u3002</p>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while","title":"2. \u00a0 while \u5faa\u73af","text":"<p>\u4e0e <code>for</code> \u5faa\u73af\u7c7b\u4f3c\uff0c<code>while</code> \u5faa\u73af\u4e5f\u662f\u4e00\u79cd\u5b9e\u73b0\u8fed\u4ee3\u7684\u65b9\u6cd5\u3002\u5728 <code>while</code> \u5faa\u73af\u4e2d\uff0c\u7a0b\u5e8f\u6bcf\u8f6e\u90fd\u4f1a\u5148\u68c0\u67e5\u6761\u4ef6\uff0c\u5982\u679c\u6761\u4ef6\u4e3a\u771f\u5219\u7ee7\u7eed\u6267\u884c\uff0c\u5426\u5219\u5c31\u7ed3\u675f\u5faa\u73af\u3002</p> <p>\u4e0b\u9762\uff0c\u6211\u4eec\u7528 <code>while</code> \u5faa\u73af\u6765\u5b9e\u73b0\u6c42\u548c \\(1 + 2 + \\dots + n\\) \u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig iteration.py<pre><code>[class]{}-[func]{while_loop}\n</code></pre> iteration.cpp<pre><code>[class]{}-[func]{whileLoop}\n</code></pre> iteration.java<pre><code>[class]{iteration}-[func]{whileLoop}\n</code></pre> iteration.cs<pre><code>[class]{iteration}-[func]{whileLoop}\n</code></pre> iteration.go<pre><code>[class]{}-[func]{whileLoop}\n</code></pre> iteration.swift<pre><code>[class]{}-[func]{whileLoop}\n</code></pre> iteration.js<pre><code>[class]{}-[func]{whileLoop}\n</code></pre> iteration.ts<pre><code>[class]{}-[func]{whileLoop}\n</code></pre> iteration.dart<pre><code>[class]{}-[func]{whileLoop}\n</code></pre> iteration.rs<pre><code>[class]{}-[func]{while_loop}\n</code></pre> iteration.c<pre><code>[class]{}-[func]{whileLoop}\n</code></pre> iteration.zig<pre><code>[class]{}-[func]{whileLoop}\n</code></pre> <p>\u5728 <code>while</code> \u5faa\u73af\u4e2d\uff0c\u7531\u4e8e\u521d\u59cb\u5316\u548c\u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\u7684\u6b65\u9aa4\u662f\u72ec\u7acb\u5728\u5faa\u73af\u7ed3\u6784\u4e4b\u5916\u7684\uff0c\u56e0\u6b64\u5b83\u6bd4 <code>for</code> \u5faa\u73af\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\u3002</p> <p>\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u6761\u4ef6\u53d8\u91cf \\(i\\) \u6bcf\u8f6e\u8fdb\u884c\u4e86\u4e24\u6b21\u66f4\u65b0\uff0c\u8fd9\u79cd\u60c5\u51b5\u5c31\u4e0d\u592a\u65b9\u4fbf\u7528 <code>for</code> \u5faa\u73af\u5b9e\u73b0\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig iteration.py<pre><code>[class]{}-[func]{while_loop_ii}\n</code></pre> iteration.cpp<pre><code>[class]{}-[func]{whileLoopII}\n</code></pre> iteration.java<pre><code>[class]{iteration}-[func]{whileLoopII}\n</code></pre> iteration.cs<pre><code>[class]{iteration}-[func]{whileLoopII}\n</code></pre> iteration.go<pre><code>[class]{}-[func]{whileLoopII}\n</code></pre> iteration.swift<pre><code>[class]{}-[func]{whileLoopII}\n</code></pre> iteration.js<pre><code>[class]{}-[func]{whileLoopII}\n</code></pre> iteration.ts<pre><code>[class]{}-[func]{whileLoopII}\n</code></pre> iteration.dart<pre><code>[class]{}-[func]{whileLoopII}\n</code></pre> iteration.rs<pre><code>[class]{}-[func]{while_loop_ii}\n</code></pre> iteration.c<pre><code>[class]{}-[func]{whileLoopII}\n</code></pre> iteration.zig<pre><code>[class]{}-[func]{whileLoopII}\n</code></pre> <p>\u603b\u7684\u6765\u8bf4\uff0c<code>for</code> \u5faa\u73af\u7684\u4ee3\u7801\u66f4\u52a0\u7d27\u51d1\uff0c<code>while</code> \u5faa\u73af\u66f4\u52a0\u7075\u6d3b\uff0c\u4e24\u8005\u90fd\u53ef\u4ee5\u5b9e\u73b0\u8fed\u4ee3\u7ed3\u6784\u3002\u9009\u62e9\u4f7f\u7528\u54ea\u4e00\u4e2a\u5e94\u8be5\u6839\u636e\u7279\u5b9a\u95ee\u9898\u7684\u9700\u6c42\u6765\u51b3\u5b9a\u3002</p>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#3","title":"3. \u00a0 \u5d4c\u5957\u5faa\u73af","text":"<p>\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\u5185\u5d4c\u5957\u53e6\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\uff0c\u4ee5 <code>for</code> \u5faa\u73af\u4e3a\u4f8b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig iteration.py<pre><code>[class]{}-[func]{nested_for_loop}\n</code></pre> iteration.cpp<pre><code>[class]{}-[func]{nestedForLoop}\n</code></pre> iteration.java<pre><code>[class]{iteration}-[func]{nestedForLoop}\n</code></pre> iteration.cs<pre><code>[class]{iteration}-[func]{nestedForLoop}\n</code></pre> iteration.go<pre><code>[class]{}-[func]{nestedForLoop}\n</code></pre> iteration.swift<pre><code>[class]{}-[func]{nestedForLoop}\n</code></pre> iteration.js<pre><code>[class]{}-[func]{nestedForLoop}\n</code></pre> iteration.ts<pre><code>[class]{}-[func]{nestedForLoop}\n</code></pre> iteration.dart<pre><code>[class]{}-[func]{nestedForLoop}\n</code></pre> iteration.rs<pre><code>[class]{}-[func]{nested_for_loop}\n</code></pre> iteration.c<pre><code>[class]{}-[func]{nestedForLoop}\n</code></pre> iteration.zig<pre><code>[class]{}-[func]{nestedForLoop}\n</code></pre> <p>\u56fe 2-2 \u7ed9\u51fa\u4e86\u8be5\u5d4c\u5957\u5faa\u73af\u7684\u6d41\u7a0b\u6846\u56fe\u3002</p> <p></p> <p> \u56fe 2-2 \u00a0 \u5d4c\u5957\u5faa\u73af\u7684\u6d41\u7a0b\u6846\u56fe </p> <p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e \\(n^2\\) \u6210\u6b63\u6bd4\uff0c\u6216\u8005\u8bf4\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u548c\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6210\u201c\u5e73\u65b9\u5173\u7cfb\u201d\u3002</p> <p>\u6211\u4eec\u53ef\u4ee5\u7ee7\u7eed\u6dfb\u52a0\u5d4c\u5957\u5faa\u73af\uff0c\u6bcf\u4e00\u6b21\u5d4c\u5957\u90fd\u662f\u4e00\u6b21\u201c\u5347\u7ef4\u201d\uff0c\u5c06\u4f1a\u4f7f\u65f6\u95f4\u590d\u6742\u5ea6\u63d0\u9ad8\u81f3\u201c\u7acb\u65b9\u5173\u7cfb\u201d\u3001\u201c\u56db\u6b21\u65b9\u5173\u7cfb\u201d\u3001\u4ee5\u6b64\u7c7b\u63a8\u3002</p>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#222","title":"2.2.2 \u00a0 \u9012\u5f52","text":"<p>\u300c\u9012\u5f52 recursion\u300d\u662f\u4e00\u79cd\u7b97\u6cd5\u7b56\u7565\uff0c\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u95ee\u9898\u3002\u5b83\u4e3b\u8981\u5305\u542b\u4e24\u4e2a\u9636\u6bb5\u3002</p> <ol> <li>\u9012\uff1a\u7a0b\u5e8f\u4e0d\u65ad\u6df1\u5165\u5730\u8c03\u7528\u81ea\u8eab\uff0c\u901a\u5e38\u4f20\u5165\u66f4\u5c0f\u6216\u66f4\u7b80\u5316\u7684\u53c2\u6570\uff0c\u76f4\u5230\u8fbe\u5230\u201c\u7ec8\u6b62\u6761\u4ef6\u201d\u3002</li> <li>\u5f52\uff1a\u89e6\u53d1\u201c\u7ec8\u6b62\u6761\u4ef6\u201d\u540e\uff0c\u7a0b\u5e8f\u4ece\u6700\u6df1\u5c42\u7684\u9012\u5f52\u51fd\u6570\u5f00\u59cb\u9010\u5c42\u8fd4\u56de\uff0c\u6c47\u805a\u6bcf\u4e00\u5c42\u7684\u7ed3\u679c\u3002</li> </ol> <p>\u800c\u4ece\u5b9e\u73b0\u7684\u89d2\u5ea6\u770b\uff0c\u9012\u5f52\u4ee3\u7801\u4e3b\u8981\u5305\u542b\u4e09\u4e2a\u8981\u7d20\u3002</p> <ol> <li>\u7ec8\u6b62\u6761\u4ef6\uff1a\u7528\u4e8e\u51b3\u5b9a\u4ec0\u4e48\u65f6\u5019\u7531\u201c\u9012\u201d\u8f6c\u201c\u5f52\u201d\u3002</li> <li>\u9012\u5f52\u8c03\u7528\uff1a\u5bf9\u5e94\u201c\u9012\u201d\uff0c\u51fd\u6570\u8c03\u7528\u81ea\u8eab\uff0c\u901a\u5e38\u8f93\u5165\u66f4\u5c0f\u6216\u66f4\u7b80\u5316\u7684\u53c2\u6570\u3002</li> <li>\u8fd4\u56de\u7ed3\u679c\uff1a\u5bf9\u5e94\u201c\u5f52\u201d\uff0c\u5c06\u5f53\u524d\u9012\u5f52\u5c42\u7ea7\u7684\u7ed3\u679c\u8fd4\u56de\u81f3\u4e0a\u4e00\u5c42\u3002</li> </ol> <p>\u89c2\u5bdf\u4ee5\u4e0b\u4ee3\u7801\uff0c\u6211\u4eec\u53ea\u9700\u8c03\u7528\u51fd\u6570 <code>recur(n)</code> \uff0c\u5c31\u53ef\u4ee5\u5b8c\u6210 \\(1 + 2 + \\dots + n\\) \u7684\u8ba1\u7b97\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig recursion.py<pre><code>[class]{}-[func]{recur}\n</code></pre> recursion.cpp<pre><code>[class]{}-[func]{recur}\n</code></pre> recursion.java<pre><code>[class]{recursion}-[func]{recur}\n</code></pre> recursion.cs<pre><code>[class]{recursion}-[func]{recur}\n</code></pre> recursion.go<pre><code>[class]{}-[func]{recur}\n</code></pre> recursion.swift<pre><code>[class]{}-[func]{recur}\n</code></pre> recursion.js<pre><code>[class]{}-[func]{recur}\n</code></pre> recursion.ts<pre><code>[class]{}-[func]{recur}\n</code></pre> recursion.dart<pre><code>[class]{}-[func]{recur}\n</code></pre> recursion.rs<pre><code>[class]{}-[func]{recur}\n</code></pre> recursion.c<pre><code>[class]{}-[func]{recur}\n</code></pre> recursion.zig<pre><code>[class]{}-[func]{recur}\n</code></pre> <p>\u56fe 2-3 \u5c55\u793a\u4e86\u8be5\u51fd\u6570\u7684\u9012\u5f52\u8fc7\u7a0b\u3002</p> <p></p> <p> \u56fe 2-3 \u00a0 \u6c42\u548c\u51fd\u6570\u7684\u9012\u5f52\u8fc7\u7a0b </p> <p>\u867d\u7136\u4ece\u8ba1\u7b97\u89d2\u5ea6\u770b\uff0c\u8fed\u4ee3\u4e0e\u9012\u5f52\u53ef\u4ee5\u5f97\u5230\u76f8\u540c\u7684\u7ed3\u679c\uff0c\u4f46\u5b83\u4eec\u4ee3\u8868\u4e86\u4e24\u79cd\u5b8c\u5168\u4e0d\u540c\u7684\u601d\u8003\u548c\u89e3\u51b3\u95ee\u9898\u7684\u8303\u5f0f\u3002</p> <ul> <li>\u8fed\u4ee3\uff1a\u201c\u81ea\u4e0b\u800c\u4e0a\u201d\u5730\u89e3\u51b3\u95ee\u9898\u3002\u4ece\u6700\u57fa\u7840\u7684\u6b65\u9aa4\u5f00\u59cb\uff0c\u7136\u540e\u4e0d\u65ad\u91cd\u590d\u6216\u7d2f\u52a0\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u76f4\u5230\u4efb\u52a1\u5b8c\u6210\u3002</li> <li>\u9012\u5f52\uff1a\u201c\u81ea\u4e0a\u800c\u4e0b\u201d\u5730\u89e3\u51b3\u95ee\u9898\u3002\u5c06\u539f\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u8fd9\u4e9b\u5b50\u95ee\u9898\u548c\u539f\u95ee\u9898\u5177\u6709\u76f8\u540c\u7684\u5f62\u5f0f\u3002\u63a5\u4e0b\u6765\u5c06\u5b50\u95ee\u9898\u7ee7\u7eed\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u5230\u57fa\u672c\u60c5\u51b5\u65f6\u505c\u6b62\uff08\u57fa\u672c\u60c5\u51b5\u7684\u89e3\u662f\u5df2\u77e5\u7684\uff09\u3002</li> </ul> <p>\u4ee5\u4e0a\u8ff0\u7684\u6c42\u548c\u51fd\u6570\u4e3a\u4f8b\uff0c\u8bbe\u95ee\u9898 \\(f(n) = 1 + 2 + \\dots + n\\) \u3002</p> <ul> <li>\u8fed\u4ee3\uff1a\u5728\u5faa\u73af\u4e2d\u6a21\u62df\u6c42\u548c\u8fc7\u7a0b\uff0c\u4ece \\(1\\) \u904d\u5386\u5230 \\(n\\) \uff0c\u6bcf\u8f6e\u6267\u884c\u6c42\u548c\u64cd\u4f5c\uff0c\u5373\u53ef\u6c42\u5f97 \\(f(n)\\) \u3002</li> <li>\u9012\u5f52\uff1a\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u5b50\u95ee\u9898 \\(f(n) = n + f(n-1)\\) \uff0c\u4e0d\u65ad\uff08\u9012\u5f52\u5730\uff09\u5206\u89e3\u4e0b\u53bb\uff0c\u76f4\u81f3\u57fa\u672c\u60c5\u51b5 \\(f(1) = 1\\) \u65f6\u7ec8\u6b62\u3002</li> </ul>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#1","title":"1. \u00a0 \u8c03\u7528\u6808","text":"<p>\u9012\u5f52\u51fd\u6570\u6bcf\u6b21\u8c03\u7528\u81ea\u8eab\u65f6\uff0c\u7cfb\u7edf\u90fd\u4f1a\u4e3a\u65b0\u5f00\u542f\u7684\u51fd\u6570\u5206\u914d\u5185\u5b58\uff0c\u4ee5\u5b58\u50a8\u5c40\u90e8\u53d8\u91cf\u3001\u8c03\u7528\u5730\u5740\u548c\u5176\u4ed6\u4fe1\u606f\u7b49\u3002\u8fd9\u5c06\u5bfc\u81f4\u4e24\u65b9\u9762\u7684\u7ed3\u679c\u3002</p> <ul> <li>\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u6570\u636e\u90fd\u5b58\u50a8\u5728\u79f0\u4e3a\u201c\u6808\u5e27\u7a7a\u95f4\u201d\u7684\u5185\u5b58\u533a\u57df\u4e2d\uff0c\u76f4\u81f3\u51fd\u6570\u8fd4\u56de\u540e\u624d\u4f1a\u88ab\u91ca\u653e\u3002\u56e0\u6b64\uff0c\u9012\u5f52\u901a\u5e38\u6bd4\u8fed\u4ee3\u66f4\u52a0\u8017\u8d39\u5185\u5b58\u7a7a\u95f4\u3002</li> <li>\u9012\u5f52\u8c03\u7528\u51fd\u6570\u4f1a\u4ea7\u751f\u989d\u5916\u7684\u5f00\u9500\u3002\u56e0\u6b64\u9012\u5f52\u901a\u5e38\u6bd4\u5faa\u73af\u7684\u65f6\u95f4\u6548\u7387\u66f4\u4f4e\u3002</li> </ul> <p>\u5982\u56fe 2-4 \u6240\u793a\uff0c\u5728\u89e6\u53d1\u7ec8\u6b62\u6761\u4ef6\u524d\uff0c\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684\u9012\u5f52\u51fd\u6570\uff0c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002</p> <p></p> <p> \u56fe 2-4 \u00a0 \u9012\u5f52\u8c03\u7528\u6df1\u5ea6 </p> <p>\u5728\u5b9e\u9645\u4e2d\uff0c\u7f16\u7a0b\u8bed\u8a00\u5141\u8bb8\u7684\u9012\u5f52\u6df1\u5ea6\u901a\u5e38\u662f\u6709\u9650\u7684\uff0c\u8fc7\u6df1\u7684\u9012\u5f52\u53ef\u80fd\u5bfc\u81f4\u6808\u6ea2\u51fa\u62a5\u9519\u3002</p>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2","title":"2. \u00a0 \u5c3e\u9012\u5f52","text":"<p>\u6709\u8da3\u7684\u662f\uff0c\u5982\u679c\u51fd\u6570\u5728\u8fd4\u56de\u524d\u7684\u6700\u540e\u4e00\u6b65\u624d\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u5219\u8be5\u51fd\u6570\u53ef\u4ee5\u88ab\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u4f18\u5316\uff0c\u4f7f\u5176\u5728\u7a7a\u95f4\u6548\u7387\u4e0a\u4e0e\u8fed\u4ee3\u76f8\u5f53\u3002\u8fd9\u79cd\u60c5\u51b5\u88ab\u79f0\u4e3a\u300c\u5c3e\u9012\u5f52 tail recursion\u300d\u3002</p> <ul> <li>\u666e\u901a\u9012\u5f52\uff1a\u5f53\u51fd\u6570\u8fd4\u56de\u5230\u4e0a\u4e00\u5c42\u7ea7\u7684\u51fd\u6570\u540e\uff0c\u9700\u8981\u7ee7\u7eed\u6267\u884c\u4ee3\u7801\uff0c\u56e0\u6b64\u7cfb\u7edf\u9700\u8981\u4fdd\u5b58\u4e0a\u4e00\u5c42\u8c03\u7528\u7684\u4e0a\u4e0b\u6587\u3002</li> <li>\u5c3e\u9012\u5f52\uff1a\u9012\u5f52\u8c03\u7528\u662f\u51fd\u6570\u8fd4\u56de\u524d\u7684\u6700\u540e\u4e00\u4e2a\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u51fd\u6570\u8fd4\u56de\u5230\u4e0a\u4e00\u5c42\u7ea7\u540e\uff0c\u65e0\u9700\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\uff0c\u56e0\u6b64\u7cfb\u7edf\u65e0\u9700\u4fdd\u5b58\u4e0a\u4e00\u5c42\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u3002</li> </ul> <p>\u4ee5\u8ba1\u7b97 \\(1 + 2 + \\dots + n\\) \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u7ed3\u679c\u53d8\u91cf <code>res</code> \u8bbe\u4e3a\u51fd\u6570\u53c2\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u5c3e\u9012\u5f52\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig recursion.py<pre><code>[class]{}-[func]{tail_recur}\n</code></pre> recursion.cpp<pre><code>[class]{}-[func]{tailRecur}\n</code></pre> recursion.java<pre><code>[class]{recursion}-[func]{tailRecur}\n</code></pre> recursion.cs<pre><code>[class]{recursion}-[func]{tailRecur}\n</code></pre> recursion.go<pre><code>[class]{}-[func]{tailRecur}\n</code></pre> recursion.swift<pre><code>[class]{}-[func]{tailRecur}\n</code></pre> recursion.js<pre><code>[class]{}-[func]{tailRecur}\n</code></pre> recursion.ts<pre><code>[class]{}-[func]{tailRecur}\n</code></pre> recursion.dart<pre><code>[class]{}-[func]{tailRecur}\n</code></pre> recursion.rs<pre><code>[class]{}-[func]{tail_recur}\n</code></pre> recursion.c<pre><code>[class]{}-[func]{tailRecur}\n</code></pre> recursion.zig<pre><code>[class]{}-[func]{tailRecur}\n</code></pre> <p>\u5c3e\u9012\u5f52\u7684\u6267\u884c\u8fc7\u7a0b\u5982\u56fe 2-5 \u6240\u793a\u3002\u5bf9\u6bd4\u666e\u901a\u9012\u5f52\u548c\u5c3e\u9012\u5f52\uff0c\u6c42\u548c\u64cd\u4f5c\u7684\u6267\u884c\u70b9\u662f\u4e0d\u540c\u7684\u3002</p> <ul> <li>\u666e\u901a\u9012\u5f52\uff1a\u6c42\u548c\u64cd\u4f5c\u662f\u5728\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u4e2d\u6267\u884c\u7684\uff0c\u6bcf\u5c42\u8fd4\u56de\u540e\u90fd\u8981\u518d\u6267\u884c\u4e00\u6b21\u6c42\u548c\u64cd\u4f5c\u3002</li> <li>\u5c3e\u9012\u5f52\uff1a\u6c42\u548c\u64cd\u4f5c\u662f\u5728\u201c\u9012\u201d\u7684\u8fc7\u7a0b\u4e2d\u6267\u884c\u7684\uff0c\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u53ea\u9700\u5c42\u5c42\u8fd4\u56de\u3002</li> </ul> <p></p> <p> \u56fe 2-5 \u00a0 \u5c3e\u9012\u5f52\u8fc7\u7a0b </p> <p>Tip</p> <p>\u8bf7\u6ce8\u610f\uff0c\u8bb8\u591a\u7f16\u8bd1\u5668\u6216\u89e3\u91ca\u5668\u5e76\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\u3002\u4f8b\u5982\uff0cPython \u9ed8\u8ba4\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u56e0\u6b64\u5373\u4f7f\u51fd\u6570\u662f\u5c3e\u9012\u5f52\u5f62\u5f0f\uff0c\u4f46\u4ecd\u7136\u53ef\u80fd\u4f1a\u9047\u5230\u6808\u6ea2\u51fa\u95ee\u9898\u3002</p>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#3_1","title":"3. \u00a0 \u9012\u5f52\u6811","text":"<p>\u5f53\u5904\u7406\u4e0e\u201c\u5206\u6cbb\u201d\u76f8\u5173\u7684\u7b97\u6cd5\u95ee\u9898\u65f6\uff0c\u9012\u5f52\u5f80\u5f80\u6bd4\u8fed\u4ee3\u7684\u601d\u8def\u66f4\u52a0\u76f4\u89c2\u3001\u4ee3\u7801\u66f4\u52a0\u6613\u8bfb\u3002\u4ee5\u201c\u6590\u6ce2\u90a3\u5951\u6570\u5217\u201d\u4e3a\u4f8b\u3002</p> <p>Question</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u5217 \\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\) \uff0c\u6c42\u8be5\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u3002</p> <p>\u8bbe\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u4e3a \\(f(n)\\) \uff0c\u6613\u5f97\u4e24\u4e2a\u7ed3\u8bba\u3002</p> <ul> <li>\u6570\u5217\u7684\u524d\u4e24\u4e2a\u6570\u5b57\u4e3a \\(f(1) = 0\\) \u548c \\(f(2) = 1\\) \u3002</li> <li>\u6570\u5217\u4e2d\u7684\u6bcf\u4e2a\u6570\u5b57\u662f\u524d\u4e24\u4e2a\u6570\u5b57\u7684\u548c\uff0c\u5373 \\(f(n) = f(n - 1) + f(n - 2)\\) \u3002</li> </ul> <p>\u6309\u7167\u9012\u63a8\u5173\u7cfb\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u5c06\u524d\u4e24\u4e2a\u6570\u5b57\u4f5c\u4e3a\u7ec8\u6b62\u6761\u4ef6\uff0c\u4fbf\u53ef\u5199\u51fa\u9012\u5f52\u4ee3\u7801\u3002\u8c03\u7528 <code>fib(n)</code> \u5373\u53ef\u5f97\u5230\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u7b2c \\(n\\) \u4e2a\u6570\u5b57\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig recursion.py<pre><code>[class]{}-[func]{fib}\n</code></pre> recursion.cpp<pre><code>[class]{}-[func]{fib}\n</code></pre> recursion.java<pre><code>[class]{recursion}-[func]{fib}\n</code></pre> recursion.cs<pre><code>[class]{recursion}-[func]{fib}\n</code></pre> recursion.go<pre><code>[class]{}-[func]{fib}\n</code></pre> recursion.swift<pre><code>[class]{}-[func]{fib}\n</code></pre> recursion.js<pre><code>[class]{}-[func]{fib}\n</code></pre> recursion.ts<pre><code>[class]{}-[func]{fib}\n</code></pre> recursion.dart<pre><code>[class]{}-[func]{fib}\n</code></pre> recursion.rs<pre><code>[class]{}-[func]{fib}\n</code></pre> recursion.c<pre><code>[class]{}-[func]{fib}\n</code></pre> recursion.zig<pre><code>[class]{}-[func]{fib}\n</code></pre> <p>\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u6211\u4eec\u5728\u51fd\u6570\u5185\u9012\u5f52\u8c03\u7528\u4e86\u4e24\u4e2a\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u4ece\u4e00\u4e2a\u8c03\u7528\u4ea7\u751f\u4e86\u4e24\u4e2a\u8c03\u7528\u5206\u652f\u3002\u5982\u56fe 2-6 \u6240\u793a\uff0c\u8fd9\u6837\u4e0d\u65ad\u9012\u5f52\u8c03\u7528\u4e0b\u53bb\uff0c\u6700\u7ec8\u5c06\u4ea7\u751f\u4e00\u4e2a\u5c42\u6570\u4e3a \\(n\\) \u7684\u300c\u9012\u5f52\u6811 recursion tree\u300d\u3002</p> <p></p> <p> \u56fe 2-6 \u00a0 \u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u9012\u5f52\u6811 </p> <p>\u672c\u8d28\u4e0a\u770b\uff0c\u9012\u5f52\u4f53\u73b0\u201c\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u5b50\u95ee\u9898\u201d\u7684\u601d\u7ef4\u8303\u5f0f\uff0c\u8fd9\u79cd\u5206\u6cbb\u7b56\u7565\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002</p> <ul> <li>\u4ece\u7b97\u6cd5\u89d2\u5ea6\u770b\uff0c\u641c\u7d22\u3001\u6392\u5e8f\u3001\u56de\u6eaf\u3001\u5206\u6cbb\u3001\u52a8\u6001\u89c4\u5212\u7b49\u8bb8\u591a\u91cd\u8981\u7b97\u6cd5\u7b56\u7565\u90fd\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u5e94\u7528\u8fd9\u79cd\u601d\u7ef4\u65b9\u5f0f\u3002</li> <li>\u4ece\u6570\u636e\u7ed3\u6784\u89d2\u5ea6\u770b\uff0c\u9012\u5f52\u5929\u7136\u9002\u5408\u5904\u7406\u94fe\u8868\u3001\u6811\u548c\u56fe\u7684\u76f8\u5173\u95ee\u9898\uff0c\u56e0\u4e3a\u5b83\u4eec\u975e\u5e38\u9002\u5408\u7528\u5206\u6cbb\u601d\u60f3\u8fdb\u884c\u5206\u6790\u3002</li> </ul>"},{"location":"chapter_computational_complexity/iteration_and_recursion/#223","title":"2.2.3 \u00a0 \u4e24\u8005\u5bf9\u6bd4","text":"<p>\u603b\u7ed3\u4ee5\u4e0a\u5185\u5bb9\uff0c\u5982\u8868 2-1 \u6240\u793a\uff0c\u8fed\u4ee3\u548c\u9012\u5f52\u5728\u5b9e\u73b0\u3001\u6027\u80fd\u548c\u9002\u7528\u6027\u4e0a\u6709\u6240\u4e0d\u540c\u3002</p> <p> \u8868 2-1 \u00a0 \u8fed\u4ee3\u4e0e\u9012\u5f52\u7279\u70b9\u5bf9\u6bd4 </p> \u8fed\u4ee3 \u9012\u5f52 \u5b9e\u73b0\u65b9\u5f0f \u5faa\u73af\u7ed3\u6784 \u51fd\u6570\u8c03\u7528\u81ea\u8eab \u65f6\u95f4\u6548\u7387 \u6548\u7387\u901a\u5e38\u8f83\u9ad8\uff0c\u65e0\u51fd\u6570\u8c03\u7528\u5f00\u9500 \u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u4f1a\u4ea7\u751f\u5f00\u9500 \u5185\u5b58\u4f7f\u7528 \u901a\u5e38\u4f7f\u7528\u56fa\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\u7a7a\u95f4 \u7d2f\u79ef\u51fd\u6570\u8c03\u7528\u53ef\u80fd\u4f7f\u7528\u5927\u91cf\u7684\u6808\u5e27\u7a7a\u95f4 \u9002\u7528\u95ee\u9898 \u9002\u7528\u4e8e\u7b80\u5355\u5faa\u73af\u4efb\u52a1\uff0c\u4ee3\u7801\u76f4\u89c2\u3001\u53ef\u8bfb\u6027\u597d \u9002\u7528\u4e8e\u5b50\u95ee\u9898\u5206\u89e3\uff0c\u5982\u6811\u3001\u56fe\u3001\u5206\u6cbb\u3001\u56de\u6eaf\u7b49\uff0c\u4ee3\u7801\u7ed3\u6784\u7b80\u6d01\u3001\u6e05\u6670 <p>Tip</p> <p>\u5982\u679c\u611f\u89c9\u4ee5\u4e0b\u5185\u5bb9\u7406\u89e3\u56f0\u96be\uff0c\u53ef\u4ee5\u5728\u8bfb\u5b8c\u201c\u6808\u201d\u7ae0\u8282\u540e\u518d\u6765\u590d\u4e60\u3002</p> <p>\u90a3\u4e48\uff0c\u8fed\u4ee3\u548c\u9012\u5f52\u5177\u6709\u4ec0\u4e48\u5185\u5728\u8054\u7cfb\u5462\uff1f\u4ee5\u4e0a\u8ff0\u7684\u9012\u5f52\u51fd\u6570\u4e3a\u4f8b\uff0c\u6c42\u548c\u64cd\u4f5c\u5728\u9012\u5f52\u7684\u201c\u5f52\u201d\u9636\u6bb5\u8fdb\u884c\u3002\u8fd9\u610f\u5473\u7740\u6700\u521d\u88ab\u8c03\u7528\u7684\u51fd\u6570\u5b9e\u9645\u4e0a\u662f\u6700\u540e\u5b8c\u6210\u5176\u6c42\u548c\u64cd\u4f5c\u7684\uff0c\u8fd9\u79cd\u5de5\u4f5c\u673a\u5236\u4e0e\u6808\u7684\u201c\u5148\u5165\u540e\u51fa\u201d\u539f\u5219\u662f\u5f02\u66f2\u540c\u5de5\u7684\u3002</p> <p>\u4e8b\u5b9e\u4e0a\uff0c\u201c\u8c03\u7528\u6808\u201d\u548c\u201c\u6808\u5e27\u7a7a\u95f4\u201d\u8fd9\u7c7b\u9012\u5f52\u672f\u8bed\u5df2\u7ecf\u6697\u793a\u4e86\u9012\u5f52\u4e0e\u6808\u4e4b\u95f4\u7684\u5bc6\u5207\u5173\u7cfb\u3002</p> <ol> <li>\u9012\uff1a\u5f53\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5728\u201c\u8c03\u7528\u6808\u201d\u4e0a\u4e3a\u8be5\u51fd\u6570\u5206\u914d\u65b0\u7684\u6808\u5e27\uff0c\u7528\u4e8e\u5b58\u50a8\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u3001\u53c2\u6570\u3001\u8fd4\u56de\u5730\u5740\u7b49\u6570\u636e\u3002</li> <li>\u5f52\uff1a\u5f53\u51fd\u6570\u5b8c\u6210\u6267\u884c\u5e76\u8fd4\u56de\u65f6\uff0c\u5bf9\u5e94\u7684\u6808\u5e27\u4f1a\u4ece\u201c\u8c03\u7528\u6808\u201d\u4e0a\u88ab\u79fb\u9664\uff0c\u6062\u590d\u4e4b\u524d\u51fd\u6570\u7684\u6267\u884c\u73af\u5883\u3002</li> </ol> <p>\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u8c03\u7528\u6808\u7684\u884c\u4e3a\uff0c\u4ece\u800c\u5c06\u9012\u5f52\u8f6c\u5316\u4e3a\u8fed\u4ee3\u5f62\u5f0f\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig recursion.py<pre><code>[class]{}-[func]{for_loop_recur}\n</code></pre> recursion.cpp<pre><code>[class]{}-[func]{forLoopRecur}\n</code></pre> recursion.java<pre><code>[class]{recursion}-[func]{forLoopRecur}\n</code></pre> recursion.cs<pre><code>[class]{recursion}-[func]{forLoopRecur}\n</code></pre> recursion.go<pre><code>[class]{}-[func]{forLoopRecur}\n</code></pre> recursion.swift<pre><code>[class]{}-[func]{forLoopRecur}\n</code></pre> recursion.js<pre><code>[class]{}-[func]{forLoopRecur}\n</code></pre> recursion.ts<pre><code>[class]{}-[func]{forLoopRecur}\n</code></pre> recursion.dart<pre><code>[class]{}-[func]{forLoopRecur}\n</code></pre> recursion.rs<pre><code>[class]{}-[func]{for_loop_recur}\n</code></pre> recursion.c<pre><code>[class]{}-[func]{forLoopRecur}\n</code></pre> recursion.zig<pre><code>[class]{}-[func]{forLoopRecur}\n</code></pre> <p>\u89c2\u5bdf\u4ee5\u4e0a\u4ee3\u7801\uff0c\u5f53\u9012\u5f52\u88ab\u8f6c\u6362\u4e3a\u8fed\u4ee3\u540e\uff0c\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u4e86\u3002\u5c3d\u7ba1\u8fed\u4ee3\u548c\u9012\u5f52\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\uff0c\u4f46\u4e5f\u4e0d\u4e00\u5b9a\u503c\u5f97\u8fd9\u6837\u505a\uff0c\u6709\u4ee5\u4e0b\u4e24\u70b9\u539f\u56e0\u3002</p> <ul> <li>\u8f6c\u5316\u540e\u7684\u4ee3\u7801\u53ef\u80fd\u66f4\u52a0\u96be\u4ee5\u7406\u89e3\uff0c\u53ef\u8bfb\u6027\u66f4\u5dee\u3002</li> <li>\u5bf9\u4e8e\u67d0\u4e9b\u590d\u6742\u95ee\u9898\uff0c\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\u7684\u884c\u4e3a\u53ef\u80fd\u975e\u5e38\u56f0\u96be\u3002</li> </ul> <p>\u603b\u4e4b\uff0c\u9009\u62e9\u8fed\u4ee3\u8fd8\u662f\u9012\u5f52\u53d6\u51b3\u4e8e\u7279\u5b9a\u95ee\u9898\u7684\u6027\u8d28\u3002\u5728\u7f16\u7a0b\u5b9e\u8df5\u4e2d\uff0c\u6743\u8861\u4e24\u8005\u7684\u4f18\u52a3\u5e76\u6839\u636e\u60c5\u5883\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002</p>"},{"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\u3002</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>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u80fd\u591f\u89e3\u51b3\u95ee\u9898\u7684\u524d\u63d0\u4e0b\uff0c\u7b97\u6cd5\u6548\u7387\u5df2\u6210\u4e3a\u8861\u91cf\u7b97\u6cd5\u4f18\u52a3\u7684\u4e3b\u8981\u8bc4\u4ef7\u6307\u6807\uff0c\u5b83\u5305\u62ec\u4ee5\u4e0b\u4e24\u4e2a\u7ef4\u5ea6\u3002</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\u3001\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>\u4e00\u65b9\u9762\uff0c\u96be\u4ee5\u6392\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5e72\u6270\u56e0\u7d20\u3002\u786c\u4ef6\u914d\u7f6e\u4f1a\u5f71\u54cd\u7b97\u6cd5\u7684\u6027\u80fd\u8868\u73b0\u3002\u6bd4\u5982\u5728\u67d0\u53f0\u8ba1\u7b97\u673a\u4e2d\uff0c\u7b97\u6cd5 <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>\u53e6\u4e00\u65b9\u9762\uff0c\u5c55\u5f00\u5b8c\u6574\u6d4b\u8bd5\u975e\u5e38\u8017\u8d39\u8d44\u6e90\u3002\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u7684\u53d8\u5316\uff0c\u7b97\u6cd5\u4f1a\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 <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\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\u300c\u590d\u6742\u5ea6\u5206\u6790\u300d\u3002</p> <p>\u590d\u6742\u5ea6\u5206\u6790\u4f53\u73b0\u7b97\u6cd5\u8fd0\u884c\u6240\u9700\u7684\u65f6\u95f4\uff08\u7a7a\u95f4\uff09\u8d44\u6e90\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5b83\u63cf\u8ff0\u4e86\u968f\u7740\u8f93\u5165\u6570\u636e\u5927\u5c0f\u7684\u589e\u52a0\uff0c\u7b97\u6cd5\u6267\u884c\u6240\u9700\u65f6\u95f4\u548c\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u5b9a\u4e49\u6709\u4e9b\u62d7\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u5206\u4e3a\u4e09\u4e2a\u91cd\u70b9\u6765\u7406\u89e3\u3002</p> <ul> <li>\u201c\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\u201d\u5206\u522b\u5bf9\u5e94\u300c\u65f6\u95f4\u590d\u6742\u5ea6 time complexity\u300d\u548c\u300c\u7a7a\u95f4\u590d\u6742\u5ea6 space complexity\u300d\u3002</li> <li>\u201c\u968f\u7740\u8f93\u5165\u6570\u636e\u5927\u5c0f\u7684\u589e\u52a0\u201d\u610f\u5473\u7740\u590d\u6742\u5ea6\u53cd\u6620\u4e86\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u4e0e\u8f93\u5165\u6570\u636e\u4f53\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u3002</li> <li>\u201c\u65f6\u95f4\u548c\u7a7a\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u201d\u8868\u793a\u590d\u6742\u5ea6\u5206\u6790\u5173\u6ce8\u7684\u4e0d\u662f\u8fd0\u884c\u65f6\u95f4\u6216\u5360\u7528\u7a7a\u95f4\u7684\u5177\u4f53\u503c\uff0c\u800c\u662f\u65f6\u95f4\u6216\u7a7a\u95f4\u589e\u957f\u7684\u201c\u5feb\u6162\u201d\u3002</li> </ul> <p>\u590d\u6742\u5ea6\u5206\u6790\u514b\u670d\u4e86\u5b9e\u9645\u6d4b\u8bd5\u65b9\u6cd5\u7684\u5f0a\u7aef\uff0c\u4f53\u73b0\u5728\u4ee5\u4e0b\u4e24\u4e2a\u65b9\u9762\u3002</p> <ul> <li>\u5b83\u72ec\u7acb\u4e8e\u6d4b\u8bd5\u73af\u5883\uff0c\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\u3002</li> <li>\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</li> </ul> <p>Tip</p> <p>\u5982\u679c\u4f60\u4ecd\u5bf9\u590d\u6742\u5ea6\u7684\u6982\u5ff5\u611f\u5230\u56f0\u60d1\uff0c\u65e0\u987b\u62c5\u5fc3\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u7eed\u7ae0\u8282\u4e2d\u8be6\u7ec6\u4ecb\u7ecd\u3002</p> <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\u4f7f\u6211\u4eec\u53ef\u4ee5\u8861\u91cf\u6267\u884c\u67d0\u4e2a\u7b97\u6cd5\u6240\u9700\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\uff0c\u5bf9\u6bd4\u4e0d\u540c\u7b97\u6cd5\u4e4b\u95f4\u7684\u6548\u7387\u3002</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\u6700\u5148\u4ecb\u7ecd\u7684\u5185\u5bb9\u3002\u7136\u800c\uff0c\u5f53\u6211\u4eec\u8ba8\u8bba\u67d0\u4e2a\u6570\u636e\u7ed3\u6784\u6216\u7b97\u6cd5\u7684\u7279\u70b9\u65f6\uff0c\u96be\u4ee5\u907f\u514d\u8981\u5206\u6790\u5176\u8fd0\u884c\u901f\u5ea6\u548c\u7a7a\u95f4\u4f7f\u7528\u60c5\u51b5\u3002</p> <p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u5efa\u8bae\u4f60\u5728\u6df1\u5165\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u524d\uff0c\u5148\u5bf9\u590d\u6742\u5ea6\u5206\u6790\u5efa\u7acb\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u4ee5\u4fbf\u80fd\u591f\u5b8c\u6210\u7b80\u5355\u7b97\u6cd5\u7684\u590d\u6742\u5ea6\u5206\u6790\u3002</p>"},{"location":"chapter_computational_complexity/space_complexity/","title":"2.4 \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/#241","title":"2.4.1 \u00a0 \u7b97\u6cd5\u76f8\u5173\u7a7a\u95f4","text":"<p>\u7b97\u6cd5\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u3002</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\u5728\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\u3002</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>\u5728\u5206\u6790\u4e00\u6bb5\u7a0b\u5e8f\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u7edf\u8ba1\u6682\u5b58\u6570\u636e\u3001\u6808\u5e27\u7a7a\u95f4\u548c\u8f93\u51fa\u6570\u636e\u4e09\u90e8\u5206\u3002</p> <p></p> <p> \u56fe 2-15 \u00a0 \u7b97\u6cd5\u4f7f\u7528\u7684\u76f8\u5173\u7a7a\u95f4 </p> PythonC++JavaC#GoSwiftJSTSDartRustCZig <pre><code>class Node:\n\"\"\"\u7c7b\"\"\"\ndef __init__(self, x: int):\nself.val: int = x # \u8282\u70b9\u503c\nself.next: Node | None = None # \u6307\u5411\u4e0b\u4e00\u8282\u70b9\u7684\u5f15\u7528\ndef function() -> int:\n\"\"\"\u51fd\u6570\"\"\"\n# \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\nreturn 0\ndef algorithm(n) -> int: # \u8f93\u5165\u6570\u636e\nA = 0 # \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff0c\u4e00\u822c\u7528\u5927\u5199\u5b57\u6bcd\u8868\u793a\uff09\nb = 0 # \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nnode = Node(0) # \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nc = function() # \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn A + b + c # \u8f93\u51fa\u6570\u636e\n</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// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\nreturn 0;\n}\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\nconst int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode* node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = func(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>/* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(int x) { val = x; }\n}\n/* \u51fd\u6570 */\nint function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\nreturn 0;\n}\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\nfinal int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>/* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(int x) { val = x; }\n}\n/* \u51fd\u6570 */\nint function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\nreturn 0;\n}\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\nconst int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode node = new 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 */\ntype node struct {\nval int\nnext *node\n}\n/* \u521b\u5efa node \u7ed3\u6784\u4f53 */\nfunc newNode(val int) *node {\nreturn &node{val: val}\n}\n/* \u51fd\u6570 */\nfunc function() int {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\nreturn 0\n}\nfunc algorithm(n int) int { // \u8f93\u5165\u6570\u636e\nconst a = 0 // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nb := 0 // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nnewNode(0) // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nc := function() // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c // \u8f93\u51fa\u6570\u636e\n}\n</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() -> Int {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\nreturn 0\n}\nfunc algorithm(n: Int) -> Int { // \u8f93\u5165\u6570\u636e\nlet a = 0 // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nvar b = 0 // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nlet node = Node(x: 0) // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nlet c = function() // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c // \u8f93\u51fa\u6570\u636e\n}\n</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// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\nfunction algorithm(n) { // \u8f93\u5165\u6570\u636e\nconst a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nlet b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nconst node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nconst c = constFunc(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</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// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\nfunction algorithm(n: number): number { // \u8f93\u5165\u6570\u636e\nconst a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nlet b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nconst node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nconst c = constFunc(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>/* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(this.val, [this.next]);\n}\n/* \u51fd\u6570 */\nint function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\nreturn 0;\n}\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\nconst int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode node = Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u7ed3\u6784\u4f53 */\nstruct Node {\nval: i32,\nnext: Option<Rc<RefCell<Node>>>,\n}\n/* \u521b\u5efa Node \u7ed3\u6784\u4f53 */\nimpl Node {\nfn new(val: i32) -> Self {\nSelf { val: val, next: None }\n}\n}\n/* \u51fd\u6570 */\nfn function() -> i32 { // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\nreturn 0;\n}\nfn algorithm(n: i32) -> i32 { // \u8f93\u5165\u6570\u636e\nconst a: i32 = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nlet mut b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nlet node = Node::new(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nlet c = function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>/* \u51fd\u6570 */\nint func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\nreturn 0;\n}\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\nconst int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nint c = func(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>\n</code></pre>"},{"location":"chapter_computational_complexity/space_complexity/#242","title":"2.4.2 \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"<p>\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u5927\u81f4\u76f8\u540c\uff0c\u53ea\u9700\u5c06\u7edf\u8ba1\u5bf9\u8c61\u4ece\u201c\u64cd\u4f5c\u6570\u91cf\u201d\u8f6c\u4e3a\u201c\u4f7f\u7528\u7a7a\u95f4\u5927\u5c0f\u201d\u3002</p> <p>\u800c\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u901a\u5e38\u53ea\u5173\u6ce8\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u3002\u8fd9\u662f\u56e0\u4e3a\u5185\u5b58\u7a7a\u95f4\u662f\u4e00\u9879\u786c\u6027\u8981\u6c42\uff0c\u6211\u4eec\u5fc5\u987b\u786e\u4fdd\u5728\u6240\u6709\u8f93\u5165\u6570\u636e\u4e0b\u90fd\u6709\u8db3\u591f\u7684\u5185\u5b58\u7a7a\u95f4\u9884\u7559\u3002</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 < 10\\) \u65f6\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff1b\u4f46\u5f53 \\(n > 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> PythonC++JavaC#GoSwiftJSTSDartRustCZig <pre><code>def algorithm(n: int):\na = 0 # O(1)\nb = [0] * 10000 # O(1)\nif n > 10:\nnums = [0] * n # O(n)\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 0; // O(1)\nvector<int> b(10000); // O(1)\nif (n > 10)\nvector<int> nums(n); // O(n)\n}\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 0; // O(1)\nint[] b = new int[10000]; // O(1)\nif (n > 10)\nint[] nums = new int[n]; // O(n)\n}\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 0; // O(1)\nint[] b = new int[10000]; // O(1)\nif (n > 10) {\nint[] nums = new int[n]; // O(n)\n}\n}\n</code></pre> <pre><code>func algorithm(n int) {\na := 0 // O(1)\nb := make([]int, 10000) // O(1)\nvar nums []int\nif n > 10 {\nnums := make([]int, n) // O(n)\n}\nfmt.Println(a, b, nums)\n}\n</code></pre> <pre><code>func algorithm(n: Int) {\nlet a = 0 // O(1)\nlet b = Array(repeating: 0, count: 10000) // O(1)\nif n > 10 {\nlet nums = Array(repeating: 0, count: n) // O(n)\n}\n}\n</code></pre> <pre><code>function algorithm(n) {\nconst a = 0; // O(1)\nconst b = new Array(10000); // O(1)\nif (n > 10) {\nconst nums = new Array(n); // O(n)\n}\n}\n</code></pre> <pre><code>function algorithm(n: number): void {\nconst a = 0; // O(1)\nconst b = new Array(10000); // O(1)\nif (n > 10) {\nconst nums = new Array(n); // O(n)\n}\n}\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 0; // O(1)\nList<int> b = List.filled(10000, 0); // O(1)\nif (n > 10) {\nList<int> nums = List.filled(n, 0); // O(n)\n}\n}\n</code></pre> <pre><code>fn algorithm(n: i32) {\nlet a = 0; // O(1)\nlet b = [0; 10000]; // O(1)\nif n > 10 {\nlet nums = vec![0; n as usize]; // O(n)\n}\n}\n</code></pre> <pre><code>void algorithm(int n) {\nint a = 0; // O(1)\nint b[10000]; // O(1)\nif (n > 10)\nint nums[n] = {0}; // O(n)\n}\n</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\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\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> PythonC++JavaC#GoSwiftJSTSDartRustCZig <pre><code>def function() -> int:\n# \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0\ndef loop(n: int):\n\"\"\"\u5faa\u73af O(1)\"\"\"\nfor _ in range(n):\nfunction()\ndef recur(n: int) -> int:\n\"\"\"\u9012\u5f52 O(n)\"\"\"\nif n == 1: return\nreturn recur(n - 1)\n</code></pre> <pre><code>int func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>int function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>int function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nint recur(int n) {\nif (n == 1) return 1;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>func function() int {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0\n}\n/* \u5faa\u73af O(1) */\nfunc loop(n int) {\nfor i := 0; i < n; i++ {\nfunction()\n}\n}\n/* \u9012\u5f52 O(n) */\nfunc recur(n int) {\nif n == 1 {\nreturn\n}\nrecur(n - 1)\n}\n</code></pre> <pre><code>@discardableResult\nfunc function() -> Int {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0\n}\n/* \u5faa\u73af O(1) */\nfunc loop(n: Int) {\nfor _ in 0 ..< n {\nfunction()\n}\n}\n/* \u9012\u5f52 O(n) */\nfunc recur(n: Int) {\nif n == 1 {\nreturn\n}\nrecur(n: n - 1)\n}\n</code></pre> <pre><code>function constFunc() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nfunction loop(n) {\nfor (let i = 0; i < n; i++) {\nconstFunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nfunction recur(n) {\nif (n === 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>function constFunc(): number {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nfunction loop(n: number): void {\nfor (let i = 0; i < n; i++) {\nconstFunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nfunction recur(n: number): void {\nif (n === 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>int function() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>fn function() -> i32 {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nfn loop(n: i32) {\nfor i in 0..n {\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(n: i32) {\nif n == 1 {\nreturn;\n}\nrecur(n - 1);\n}\n</code></pre> <pre><code>int func() {\n// \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>\n</code></pre>"},{"location":"chapter_computational_complexity/space_complexity/#243","title":"2.4.3 \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"<p>\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u56fe 2-16 \u5c55\u793a\u4e86\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\uff08\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\uff09\u3002</p> \\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n^2) < O(2^n) \\newline \\text{\u5e38\u6570\u9636} < \\text{\u5bf9\u6570\u9636} < \\text{\u7ebf\u6027\u9636} < \\text{\u5e73\u65b9\u9636} < \\text{\u6307\u6570\u9636} \\end{aligned} \\] <p></p> <p> \u56fe 2-16 \u00a0 \u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b </p>"},{"location":"chapter_computational_complexity/space_complexity/#1-o1","title":"1. \u00a0 \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\u56e0\u6b64\u4e0d\u4f1a\u7d2f\u79ef\u5360\u7528\u7a7a\u95f4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.py<pre><code>[class]{}-[func]{function}\n[class]{}-[func]{constant}\n</code></pre> space_complexity.cpp<pre><code>[class]{}-[func]{func}\n[class]{}-[func]{constant}\n</code></pre> space_complexity.java<pre><code>[class]{space_complexity}-[func]{function}\n[class]{space_complexity}-[func]{constant}\n</code></pre> space_complexity.cs<pre><code>[class]{space_complexity}-[func]{function}\n[class]{space_complexity}-[func]{constant}\n</code></pre> space_complexity.go<pre><code>[class]{}-[func]{function}\n[class]{}-[func]{spaceConstant}\n</code></pre> space_complexity.swift<pre><code>[class]{}-[func]{function}\n[class]{}-[func]{constant}\n</code></pre> space_complexity.js<pre><code>[class]{}-[func]{constFunc}\n[class]{}-[func]{constant}\n</code></pre> space_complexity.ts<pre><code>[class]{}-[func]{constFunc}\n[class]{}-[func]{constant}\n</code></pre> space_complexity.dart<pre><code>[class]{}-[func]{function}\n[class]{}-[func]{constant}\n</code></pre> space_complexity.rs<pre><code>[class]{}-[func]{function}\n[class]{}-[func]{constant}\n</code></pre> space_complexity.c<pre><code>[class]{}-[func]{func}\n[class]{}-[func]{constant}\n</code></pre> space_complexity.zig<pre><code>[class]{}-[func]{function}\n[class]{}-[func]{constant}\n</code></pre>"},{"location":"chapter_computational_complexity/space_complexity/#2-on","title":"2. \u00a0 \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\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.py<pre><code>[class]{}-[func]{linear}\n</code></pre> space_complexity.cpp<pre><code>[class]{}-[func]{linear}\n</code></pre> space_complexity.java<pre><code>[class]{space_complexity}-[func]{linear}\n</code></pre> space_complexity.cs<pre><code>[class]{space_complexity}-[func]{linear}\n</code></pre> space_complexity.go<pre><code>[class]{}-[func]{spaceLinear}\n</code></pre> space_complexity.swift<pre><code>[class]{}-[func]{linear}\n</code></pre> space_complexity.js<pre><code>[class]{}-[func]{linear}\n</code></pre> space_complexity.ts<pre><code>[class]{}-[func]{linear}\n</code></pre> space_complexity.dart<pre><code>[class]{}-[func]{linear}\n</code></pre> space_complexity.rs<pre><code>[class]{}-[func]{linear}\n</code></pre> space_complexity.c<pre><code>[class]{hashTable}-[func]{}\n[class]{}-[func]{linear}\n</code></pre> space_complexity.zig<pre><code>[class]{}-[func]{linear}\n</code></pre> <p>\u5982\u56fe 2-17 \u6240\u793a\uff0c\u6b64\u51fd\u6570\u7684\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u5373\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 <code>linear_recur()</code> \u51fd\u6570\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.py<pre><code>[class]{}-[func]{linear_recur}\n</code></pre> space_complexity.cpp<pre><code>[class]{}-[func]{linearRecur}\n</code></pre> space_complexity.java<pre><code>[class]{space_complexity}-[func]{linearRecur}\n</code></pre> space_complexity.cs<pre><code>[class]{space_complexity}-[func]{linearRecur}\n</code></pre> space_complexity.go<pre><code>[class]{}-[func]{spaceLinearRecur}\n</code></pre> space_complexity.swift<pre><code>[class]{}-[func]{linearRecur}\n</code></pre> space_complexity.js<pre><code>[class]{}-[func]{linearRecur}\n</code></pre> space_complexity.ts<pre><code>[class]{}-[func]{linearRecur}\n</code></pre> space_complexity.dart<pre><code>[class]{}-[func]{linearRecur}\n</code></pre> space_complexity.rs<pre><code>[class]{}-[func]{linear_recur}\n</code></pre> space_complexity.c<pre><code>[class]{}-[func]{linearRecur}\n</code></pre> space_complexity.zig<pre><code>[class]{}-[func]{linearRecur}\n</code></pre> <p></p> <p> \u56fe 2-17 \u00a0 \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u7ebf\u6027\u9636\u7a7a\u95f4\u590d\u6742\u5ea6 </p>"},{"location":"chapter_computational_complexity/space_complexity/#3-on2","title":"3. \u00a0 \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\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.py<pre><code>[class]{}-[func]{quadratic}\n</code></pre> space_complexity.cpp<pre><code>[class]{}-[func]{quadratic}\n</code></pre> space_complexity.java<pre><code>[class]{space_complexity}-[func]{quadratic}\n</code></pre> space_complexity.cs<pre><code>[class]{space_complexity}-[func]{quadratic}\n</code></pre> space_complexity.go<pre><code>[class]{}-[func]{spaceQuadratic}\n</code></pre> space_complexity.swift<pre><code>[class]{}-[func]{quadratic}\n</code></pre> space_complexity.js<pre><code>[class]{}-[func]{quadratic}\n</code></pre> space_complexity.ts<pre><code>[class]{}-[func]{quadratic}\n</code></pre> space_complexity.dart<pre><code>[class]{}-[func]{quadratic}\n</code></pre> space_complexity.rs<pre><code>[class]{}-[func]{quadratic}\n</code></pre> space_complexity.c<pre><code>[class]{}-[func]{quadratic}\n</code></pre> space_complexity.zig<pre><code>[class]{}-[func]{quadratic}\n</code></pre> <p>\u5982\u56fe 2-18 \u6240\u793a\uff0c\u8be5\u51fd\u6570\u7684\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u5728\u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u4e2d\u90fd\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u6570\u7ec4\uff0c\u957f\u5ea6\u5206\u522b\u4e3a \\(n\\)\u3001\\(n-1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u5e73\u5747\u957f\u5ea6\u4e3a \\(n / 2\\) \uff0c\u56e0\u6b64\u603b\u4f53\u5360\u7528 \\(O(n^2)\\) \u7a7a\u95f4\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.py<pre><code>[class]{}-[func]{quadratic_recur}\n</code></pre> space_complexity.cpp<pre><code>[class]{}-[func]{quadraticRecur}\n</code></pre> space_complexity.java<pre><code>[class]{space_complexity}-[func]{quadraticRecur}\n</code></pre> space_complexity.cs<pre><code>[class]{space_complexity}-[func]{quadraticRecur}\n</code></pre> space_complexity.go<pre><code>[class]{}-[func]{spaceQuadraticRecur}\n</code></pre> space_complexity.swift<pre><code>[class]{}-[func]{quadraticRecur}\n</code></pre> space_complexity.js<pre><code>[class]{}-[func]{quadraticRecur}\n</code></pre> space_complexity.ts<pre><code>[class]{}-[func]{quadraticRecur}\n</code></pre> space_complexity.dart<pre><code>[class]{}-[func]{quadraticRecur}\n</code></pre> space_complexity.rs<pre><code>[class]{}-[func]{quadratic_recur}\n</code></pre> space_complexity.c<pre><code>[class]{}-[func]{quadraticRecur}\n</code></pre> space_complexity.zig<pre><code>[class]{}-[func]{quadraticRecur}\n</code></pre> <p></p> <p> \u56fe 2-18 \u00a0 \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u5e73\u65b9\u9636\u7a7a\u95f4\u590d\u6742\u5ea6 </p>"},{"location":"chapter_computational_complexity/space_complexity/#4-o2n","title":"4. \u00a0 \u6307\u6570\u9636 \\(O(2^n)\\)","text":"<p>\u6307\u6570\u9636\u5e38\u89c1\u4e8e\u4e8c\u53c9\u6811\u3002\u89c2\u5bdf\u56fe 2-19 \uff0c\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u201c\u6ee1\u4e8c\u53c9\u6811\u201d\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(2^n - 1\\) \uff0c\u5360\u7528 \\(O(2^n)\\) \u7a7a\u95f4\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig space_complexity.py<pre><code>[class]{}-[func]{build_tree}\n</code></pre> space_complexity.cpp<pre><code>[class]{}-[func]{buildTree}\n</code></pre> space_complexity.java<pre><code>[class]{space_complexity}-[func]{buildTree}\n</code></pre> space_complexity.cs<pre><code>[class]{space_complexity}-[func]{buildTree}\n</code></pre> space_complexity.go<pre><code>[class]{}-[func]{buildTree}\n</code></pre> space_complexity.swift<pre><code>[class]{}-[func]{buildTree}\n</code></pre> space_complexity.js<pre><code>[class]{}-[func]{buildTree}\n</code></pre> space_complexity.ts<pre><code>[class]{}-[func]{buildTree}\n</code></pre> space_complexity.dart<pre><code>[class]{}-[func]{buildTree}\n</code></pre> space_complexity.rs<pre><code>[class]{}-[func]{build_tree}\n</code></pre> space_complexity.c<pre><code>[class]{}-[func]{buildTree}\n</code></pre> space_complexity.zig<pre><code>[class]{}-[func]{buildTree}\n</code></pre> <p></p> <p> \u56fe 2-19 \u00a0 \u6ee1\u4e8c\u53c9\u6811\u4ea7\u751f\u7684\u6307\u6570\u9636\u7a7a\u95f4\u590d\u6742\u5ea6 </p>"},{"location":"chapter_computational_complexity/space_complexity/#5-olog-n","title":"5. \u00a0 \u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"<p>\u5bf9\u6570\u9636\u5e38\u89c1\u4e8e\u5206\u6cbb\u7b97\u6cd5\u3002\u4f8b\u5982\u5f52\u5e76\u6392\u5e8f\uff0c\u8f93\u5165\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\uff0c\u6bcf\u8f6e\u9012\u5f52\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5212\u5206\u4e3a\u4e24\u534a\uff0c\u5f62\u6210\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u6808\u5e27\u7a7a\u95f4\u3002</p> <p>\u518d\u4f8b\u5982\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u8f93\u5165\u4e00\u4e2a\u6b63\u6574\u6570 \\(n\\) \uff0c\u5b83\u7684\u4f4d\u6570\u4e3a \\(\\log_{10} n + 1\\) \uff0c\u5373\u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a \\(\\log_{10} n + 1\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_{10} n + 1) = O(\\log n)\\) \u3002</p>"},{"location":"chapter_computational_complexity/space_complexity/#244","title":"2.4.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\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u901a\u5e38\u662f\u66f4\u5e38\u7528\u7684\u7b56\u7565\u3002\u5f53\u7136\uff0c\u5728\u6570\u636e\u91cf\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u63a7\u5236\u7a7a\u95f4\u590d\u6742\u5ea6\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002</p>"},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_computational_complexity/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"<p>\u7b97\u6cd5\u6548\u7387\u8bc4\u4f30</p> <ul> <li>\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u662f\u8861\u91cf\u7b97\u6cd5\u4f18\u52a3\u7684\u4e24\u4e2a\u4e3b\u8981\u8bc4\u4ef7\u6307\u6807\u3002</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\u7684\u6570\u636e\u91cf\u8f83\u5c0f\u6216\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u540c\u65f6\uff0c\u65e0\u6cd5\u7cbe\u786e\u5bf9\u6bd4\u7b97\u6cd5\u6548\u7387\u7684\u4f18\u52a3\u3002</li> <li>\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4f7f\u7528\u5927 \\(O\\) \u7b26\u53f7\u8868\u793a\uff0c\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u53cd\u6620\u5f53 \\(n\\) \u8d8b\u5411\u6b63\u65e0\u7a77\u65f6\uff0c\u64cd\u4f5c\u6570\u91cf \\(T(n)\\) \u7684\u589e\u957f\u7ea7\u522b\u3002</li> <li>\u63a8\u7b97\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u4e24\u6b65\uff0c\u9996\u5148\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\uff0c\u7136\u540e\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c\u3002</li> <li>\u5e38\u89c1\u65f6\u95f4\u590d\u6742\u5ea6\u4ece\u5c0f\u5230\u5927\u6392\u5217\u6709 \\(O(1)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n \\log n)\\)\u3001\\(O(n^2)\\)\u3001\\(O(2^n)\\) \u548c \\(O(n!)\\) \u7b49\u3002</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>\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u4f5c\u7528\u7c7b\u4f3c\u4e8e\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u7528\u4e8e\u8861\u91cf\u7b97\u6cd5\u5360\u7528\u7a7a\u95f4\u968f\u6570\u636e\u91cf\u589e\u957f\u7684\u8d8b\u52bf\u3002</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)\\)\u3001\\(O(\\log n)\\)\u3001\\(O(n)\\)\u3001\\(O(n^2)\\) \u548c \\(O(2^n)\\) \u7b49\u3002</li> </ul>"},{"location":"chapter_computational_complexity/summary/#2-q-a","title":"2. \u00a0 Q & 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\u3001Python\u3001C++\u3001Go\u3001C# \u7b49\uff09\u90fd\u4e0d\u652f\u6301\u81ea\u52a8\u4f18\u5316\u5c3e\u9012\u5f52\uff0c\u56e0\u6b64\u901a\u5e38\u8ba4\u4e3a\u7a7a\u95f4\u590d\u6742\u5ea6\u662f \\(O(n)\\) \u3002</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\u88ab\u72ec\u7acb\u6267\u884c\uff0c\u6240\u6709\u53c2\u6570\u90fd\u4ee5\u663e\u5f0f\u4f20\u9012\u3002\u65b9\u6cd5\uff08method\uff09\u4e0e\u4e00\u4e2a\u5bf9\u8c61\u5173\u8054\uff0c\u88ab\u9690\u5f0f\u4f20\u9012\u7ed9\u8c03\u7528\u5b83\u7684\u5bf9\u8c61\uff0c\u80fd\u591f\u5bf9\u7c7b\u7684\u5b9e\u4f8b\u4e2d\u5305\u542b\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002</p> <p>\u4e0b\u9762\u4ee5\u51e0\u4e2a\u5e38\u89c1\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u8bf4\u660e\u3002</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\u4f53\uff08struct\uff09\u6765\u6a21\u62df\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u4e0e\u7ed3\u6784\u4f53\u76f8\u5173\u8054\u7684\u51fd\u6570\u5c31\u76f8\u5f53\u4e8e\u5176\u4ed6\u8bed\u8a00\u4e2d\u7684\u65b9\u6cd5\u3002</li> <li>Java \u548c C# \u662f\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4ee3\u7801\u5757\uff08\u65b9\u6cd5\uff09\u901a\u5e38\u90fd\u662f\u4f5c\u4e3a\u67d0\u4e2a\u7c7b\u7684\u4e00\u90e8\u5206\u3002\u9759\u6001\u65b9\u6cd5\u7684\u884c\u4e3a\u7c7b\u4f3c\u4e8e\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u88ab\u7ed1\u5b9a\u5728\u7c7b\u4e0a\uff0c\u4e0d\u80fd\u8bbf\u95ee\u7279\u5b9a\u7684\u5b9e\u4f8b\u53d8\u91cf\u3002</li> <li>C++ \u548c Python \u65e2\u652f\u6301\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\uff08\u51fd\u6570\uff09\uff0c\u4e5f\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08\u65b9\u6cd5\uff09\u3002</li> </ul> <p>\u56fe\u201c\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u201d\u53cd\u6620\u7684\u662f\u5426\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\uff1f</p> <p>\u4e0d\u662f\uff0c\u8be5\u56fe\u7247\u5c55\u793a\u7684\u662f\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5176\u53cd\u6620\u7684\u662f\u589e\u957f\u8d8b\u52bf\uff0c\u800c\u4e0d\u662f\u5360\u7528\u7a7a\u95f4\u7684\u7edd\u5bf9\u5927\u5c0f\u3002</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.3 \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 <code>print()</code> \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\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig <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\nvoid algorithm(int n) {\nint a = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\ncout << 0 << endl; // 5 ns\n}\n}\n</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 < 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 < 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) {\na := 2 // 1 ns\na = a + 1 // 1 ns\na = a * 2 // 10 ns\n// \u5faa\u73af n \u6b21\nfor i := 0; i < n; i++ { // 1 ns\nfmt.Println(a) // 5 ns\n}\n}\n</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 ..< n { // 1 ns\nprint(0) // 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 < 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 < 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 < 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> <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 < n; i++) { // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nprintf(\"%d\", 0); // 5 ns\n}\n}\n</code></pre> <pre><code>\n</code></pre> <p>\u6839\u636e\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f97\u5230\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u4e3a \\(6n + 12\\) ns \uff1a</p> \\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\] <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\u5c06\u9884\u4f30\u65f6\u95f4\u548c\u8fd0\u884c\u5e73\u53f0\u7ed1\u5b9a\uff0c\u56e0\u4e3a\u7b97\u6cd5\u9700\u8981\u5728\u5404\u79cd\u4e0d\u540c\u7684\u5e73\u53f0\u4e0a\u8fd0\u884c\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5f88\u96be\u83b7\u77e5\u6bcf\u79cd\u64cd\u4f5c\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u8fd9\u7ed9\u9884\u4f30\u8fc7\u7a0b\u5e26\u6765\u4e86\u6781\u5927\u7684\u96be\u5ea6\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#231","title":"2.3.1 \u00a0 \u7edf\u8ba1\u65f6\u95f4\u589e\u957f\u8d8b\u52bf","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u7edf\u8ba1\u7684\u4e0d\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u800c\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002</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>\u3001<code>B</code> \u548c <code>C</code> \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig <pre><code># \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_A(n: int):\nprint(0)\n# \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\ndef algorithm_B(n: int):\nfor _ in range(n):\nprint(0)\n# \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_C(n: int):\nfor _ in range(1000000):\nprint(0)\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\ncout << 0 << endl;\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i < n; i++) {\ncout << 0 << endl;\n}\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i < 1000000; i++) {\ncout << 0 << endl;\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nSystem.out.println(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i < n; i++) {\nSystem.out.println(0);\n}\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i < 1000000; i++) {\nSystem.out.println(0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nConsole.WriteLine(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i < n; i++) {\nConsole.WriteLine(0);\n}\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i < 1000000; i++) {\nConsole.WriteLine(0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_A(n int) {\nfmt.Println(0)\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithm_B(n int) {\nfor i := 0; i < n; i++ {\nfmt.Println(0)\n}\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_C(n int) {\nfor i := 0; i < 1000000; i++ {\nfmt.Println(0)\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmA(n: Int) {\nprint(0)\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithmB(n: Int) {\nfor _ in 0 ..< n {\nprint(0)\n}\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmC(n: Int) {\nfor _ in 0 ..< 1000000 {\nprint(0)\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n) {\nconsole.log(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n) {\nfor (let i = 0; i < n; i++) {\nconsole.log(0);\n}\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n) {\nfor (let i = 0; i < 1000000; i++) {\nconsole.log(0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n: number): void {\nconsole.log(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n: number): void {\nfor (let i = 0; i < n; i++) {\nconsole.log(0);\n}\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n: number): void {\nfor (let i = 0; i < 1000000; i++) {\nconsole.log(0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmA(int n) {\nprint(0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithmB(int n) {\nfor (int i = 0; i < n; i++) {\nprint(0);\n}\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithmC(int n) {\nfor (int i = 0; i < 1000000; i++) {\nprint(0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_A(n: i32) {\nprintln!(\"{}\", 0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfn algorithm_B(n: i32) {\nfor _ in 0..n {\nprintln!(\"{}\", 0);\n}\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfn algorithm_C(n: i32) {\nfor _ in 0..1000000 {\nprintln!(\"{}\", 0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nprintf(\"%d\", 0);\n}\n// \u7b97\u6cd5 B \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i < n; i++) {\nprintf(\"%d\", 0);\n}\n}\n// \u7b97\u6cd5 C \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i < 1000000; i++) {\nprintf(\"%d\", 0);\n}\n}\n</code></pre> <pre><code>\n</code></pre> <p>\u56fe 2-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4e09\u4e2a\u7b97\u6cd5\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002</p> <ul> <li>\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\u201c\u5e38\u6570\u9636\u201d\u3002</li> <li>\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\u201c\u7ebf\u6027\u9636\u201d\u3002</li> <li>\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\u201c\u5e38\u6570\u9636\u201d\u3002</li> </ul> <p></p> <p> \u56fe 2-7 \u00a0 \u7b97\u6cd5 A\u3001B \u548c 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> <ul> <li>\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 > 1\\) \u65f6\u6bd4\u7b97\u6cd5 <code>A</code> \u66f4\u6162\uff0c\u5728 \\(n > 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</li> <li>\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</li> <li>\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\u7684\u9ad8\u4f4e\u3002\u5f53\u7136\uff0c\u5c3d\u7ba1\u5b58\u5728\u4e0a\u8ff0\u95ee\u9898\uff0c\u590d\u6742\u5ea6\u5206\u6790\u4ecd\u7136\u662f\u8bc4\u5224\u7b97\u6cd5\u6548\u7387\u6700\u6709\u6548\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002</li> </ul>"},{"location":"chapter_computational_complexity/time_complexity/#232","title":"2.3.2 \u00a0 \u51fd\u6570\u6e10\u8fd1\u4e0a\u754c","text":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u8f93\u5165\u5927\u5c0f\u4e3a \\(n\\) \u7684\u51fd\u6570\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig <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>void algorithm(int n) {\nint a = 1; // +1\na = a + 1; // +1\na = a * 2; // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\ncout << 0 << endl; // +1\n}\n}\n</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 < 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 < 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) {\na := 1 // +1\na = a + 1 // +1\na = a * 2 // +1\n// \u5faa\u73af n \u6b21\nfor i := 0; i < n; i++ { // +1\nfmt.Println(a) // +1\n}\n}\n</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 ..< n { // +1\nprint(0) // +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 < 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 < 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 < 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> <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 < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nprintf(\"%d\", 0); // +1\n}\n} </code></pre> <pre><code>\n</code></pre> <p>\u8bbe\u7b97\u6cd5\u7684\u64cd\u4f5c\u6570\u91cf\u662f\u4e00\u4e2a\u5173\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u7684\u51fd\u6570\uff0c\u8bb0\u4e3a \\(T(n)\\) \uff0c\u5219\u4ee5\u4e0a\u51fd\u6570\u7684\u7684\u64cd\u4f5c\u6570\u91cf\u4e3a\uff1a</p> \\[ T(n) = 3 + 2n \\] <p>\\(T(n)\\) \u662f\u4e00\u6b21\u51fd\u6570\uff0c\u8bf4\u660e\u5176\u8fd0\u884c\u65f6\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u662f\u7ebf\u6027\u7684\uff0c\u56e0\u6b64\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u7ebf\u6027\u9636\u3002</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\uff0c\u5176\u5177\u6709\u660e\u786e\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 > n_0\\) \uff0c\u5747\u6709 \\(T(n) \\leq c \\cdot f(n)\\) \uff0c\u5219\u53ef\u8ba4\u4e3a \\(f(n)\\) \u7ed9\u51fa\u4e86 \\(T(n)\\) \u7684\u4e00\u4e2a\u6e10\u8fd1\u4e0a\u754c\uff0c\u8bb0\u4e3a \\(T(n) = O(f(n))\\) \u3002</p> <p>\u5982\u56fe 2-8 \u6240\u793a\uff0c\u8ba1\u7b97\u6e10\u8fd1\u4e0a\u754c\u5c31\u662f\u5bfb\u627e\u4e00\u4e2a\u51fd\u6570 \\(f(n)\\) \uff0c\u4f7f\u5f97\u5f53 \\(n\\) \u8d8b\u5411\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\\(T(n)\\) \u548c \\(f(n)\\) \u5904\u4e8e\u76f8\u540c\u7684\u589e\u957f\u7ea7\u522b\uff0c\u4ec5\u76f8\u5dee\u4e00\u4e2a\u5e38\u6570\u9879 \\(c\\) \u7684\u500d\u6570\u3002</p> <p></p> <p> \u56fe 2-8 \u00a0 \u51fd\u6570\u7684\u6e10\u8fd1\u4e0a\u754c </p>"},{"location":"chapter_computational_complexity/time_complexity/#233","title":"2.3.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\u987b\u62c5\u5fc3\u3002\u56e0\u4e3a\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u638c\u63e1\u63a8\u7b97\u65b9\u6cd5\uff0c\u6570\u5b66\u610f\u4e49\u5c31\u53ef\u4ee5\u9010\u6e10\u9886\u609f\u3002</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":"1. \u00a0 \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\u3002</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\u7b2c <code>1.</code> \u70b9\u548c\u7b2c <code>2.</code> \u70b9\u7684\u6280\u5de7\u3002</li> </ol> <p>\u7ed9\u5b9a\u4e00\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e0a\u8ff0\u6280\u5de7\u6765\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig <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>void algorithm(int n) {\nint a = 1; // +0\uff08\u6280\u5de7 1\uff09\na = a + n; // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i < 5 * n + 1; i++) {\ncout << 0 << endl;\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++) {\nfor (int j = 0; j < n + 1; j++) {\ncout << 0 << endl;\n}\n}\n}\n</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 < 5 * n + 1; i++) {\nSystem.out.println(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++) {\nfor (int j = 0; j < n + 1; j++) {\nSystem.out.println(0);\n}\n}\n}\n</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 < 5 * n + 1; i++) {\nConsole.WriteLine(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++) {\nfor (int j = 0; j < n + 1; j++) {\nConsole.WriteLine(0);\n}\n}\n}\n</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 < 5 * n + 1; i++ {\nfmt.Println(0)\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor i := 0; i < 2 * n; i++ {\nfor j := 0; j < n + 1; j++ {\nfmt.Println(0)\n}\n}\n}\n</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 ..< (5 * n + 1) {\nprint(0)\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor _ in 0 ..< (2 * n) {\nfor _ in 0 ..< (n + 1) {\nprint(0)\n}\n}\n}\n</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 < 5 * n + 1; i++) {\nconsole.log(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (let i = 0; i < 2 * n; i++) {\nfor (let j = 0; j < n + 1; j++) {\nconsole.log(0);\n}\n}\n}\n</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 < 5 * n + 1; i++) {\nconsole.log(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (let i = 0; i < 2 * n; i++) {\nfor (let j = 0; j < n + 1; j++) {\nconsole.log(0);\n}\n}\n}\n</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 < 5 * n + 1; i++) {\nprint(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++) {\nfor (int j = 0; j < n + 1; j++) {\nprint(0);\n}\n}\n}\n</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> <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 < 5 * n + 1; i++) {\nprintf(\"%d\", 0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++) {\nfor (int j = 0; j < n + 1; j++) {\nprintf(\"%d\", 0);\n}\n}\n}\n</code></pre> <pre><code>\n</code></pre> <p>\u4ee5\u4e0b\u516c\u5f0f\u5c55\u793a\u4e86\u4f7f\u7528\u4e0a\u8ff0\u6280\u5de7\u524d\u540e\u7684\u7edf\u8ba1\u7ed3\u679c\uff0c\u4e24\u8005\u63a8\u51fa\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n^2)\\) \u3002</p> \\[ \\begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \\text{\u5b8c\u6574\u7edf\u8ba1 (-.-|||)} \\newline & = 2n^2 + 7n + 3 \\newline T(n) & = n^2 + n & \\text{\u5077\u61d2\u7edf\u8ba1 (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2","title":"2. \u00a0 \u7b2c\u4e8c\u6b65\uff1a\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c","text":"<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>\u8868 2-2 \u5c55\u793a\u4e86\u4e00\u4e9b\u4f8b\u5b50\uff0c\u5176\u4e2d\u4e00\u4e9b\u5938\u5f20\u7684\u503c\u662f\u4e3a\u4e86\u5f3a\u8c03\u201c\u7cfb\u6570\u65e0\u6cd5\u64bc\u52a8\u9636\u6570\u201d\u8fd9\u4e00\u7ed3\u8bba\u3002\u5f53 \\(n\\) \u8d8b\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\u8fd9\u4e9b\u5e38\u6570\u53d8\u5f97\u65e0\u8db3\u8f7b\u91cd\u3002</p> <p> \u8868 2-2 \u00a0 \u4e0d\u540c\u64cd\u4f5c\u6570\u91cf\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6 </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/#234","title":"2.3.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\u5982\u56fe 2-9 \u6240\u793a\uff08\u6309\u7167\u4ece\u4f4e\u5230\u9ad8\u7684\u987a\u5e8f\u6392\u5217\uff09\u3002</p> \\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n \\log n) < O(n^2) < O(2^n) < O(n!) \\newline \\text{\u5e38\u6570\u9636} < \\text{\u5bf9\u6570\u9636} < \\text{\u7ebf\u6027\u9636} < \\text{\u7ebf\u6027\u5bf9\u6570\u9636} < \\text{\u5e73\u65b9\u9636} < \\text{\u6307\u6570\u9636} < \\text{\u9636\u4e58\u9636} \\end{aligned} \\] <p></p> <p> \u56fe 2-9 \u00a0 \u5e38\u89c1\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7c7b\u578b </p>"},{"location":"chapter_computational_complexity/time_complexity/#1-o1","title":"1. \u00a0 \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>\u5728\u4ee5\u4e0b\u51fd\u6570\u4e2d\uff0c\u5c3d\u7ba1\u64cd\u4f5c\u6570\u91cf <code>size</code> \u53ef\u80fd\u5f88\u5927\uff0c\u4f46\u7531\u4e8e\u5176\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py<pre><code>[class]{}-[func]{constant}\n</code></pre> time_complexity.cpp<pre><code>[class]{}-[func]{constant}\n</code></pre> time_complexity.java<pre><code>[class]{time_complexity}-[func]{constant}\n</code></pre> time_complexity.cs<pre><code>[class]{time_complexity}-[func]{constant}\n</code></pre> time_complexity.go<pre><code>[class]{}-[func]{constant}\n</code></pre> time_complexity.swift<pre><code>[class]{}-[func]{constant}\n</code></pre> time_complexity.js<pre><code>[class]{}-[func]{constant}\n</code></pre> time_complexity.ts<pre><code>[class]{}-[func]{constant}\n</code></pre> time_complexity.dart<pre><code>[class]{}-[func]{constant}\n</code></pre> time_complexity.rs<pre><code>[class]{}-[func]{constant}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{constant}\n</code></pre> time_complexity.zig<pre><code>[class]{}-[func]{constant}\n</code></pre>"},{"location":"chapter_computational_complexity/time_complexity/#2-on","title":"2. \u00a0 \u7ebf\u6027\u9636 \\(O(n)\\)","text":"<p>\u7ebf\u6027\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u4ee5\u7ebf\u6027\u7ea7\u522b\u589e\u957f\u3002\u7ebf\u6027\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5355\u5c42\u5faa\u73af\u4e2d\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py<pre><code>[class]{}-[func]{linear}\n</code></pre> time_complexity.cpp<pre><code>[class]{}-[func]{linear}\n</code></pre> time_complexity.java<pre><code>[class]{time_complexity}-[func]{linear}\n</code></pre> time_complexity.cs<pre><code>[class]{time_complexity}-[func]{linear}\n</code></pre> time_complexity.go<pre><code>[class]{}-[func]{linear}\n</code></pre> time_complexity.swift<pre><code>[class]{}-[func]{linear}\n</code></pre> time_complexity.js<pre><code>[class]{}-[func]{linear}\n</code></pre> time_complexity.ts<pre><code>[class]{}-[func]{linear}\n</code></pre> time_complexity.dart<pre><code>[class]{}-[func]{linear}\n</code></pre> time_complexity.rs<pre><code>[class]{}-[func]{linear}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{linear}\n</code></pre> time_complexity.zig<pre><code>[class]{}-[func]{linear}\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\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py<pre><code>[class]{}-[func]{array_traversal}\n</code></pre> time_complexity.cpp<pre><code>[class]{}-[func]{arrayTraversal}\n</code></pre> time_complexity.java<pre><code>[class]{time_complexity}-[func]{arrayTraversal}\n</code></pre> time_complexity.cs<pre><code>[class]{time_complexity}-[func]{arrayTraversal}\n</code></pre> time_complexity.go<pre><code>[class]{}-[func]{arrayTraversal}\n</code></pre> time_complexity.swift<pre><code>[class]{}-[func]{arrayTraversal}\n</code></pre> time_complexity.js<pre><code>[class]{}-[func]{arrayTraversal}\n</code></pre> time_complexity.ts<pre><code>[class]{}-[func]{arrayTraversal}\n</code></pre> time_complexity.dart<pre><code>[class]{}-[func]{arrayTraversal}\n</code></pre> time_complexity.rs<pre><code>[class]{}-[func]{array_traversal}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{arrayTraversal}\n</code></pre> time_complexity.zig<pre><code>[class]{}-[func]{arrayTraversal}\n</code></pre> <p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u9700\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\u6765\u5177\u4f53\u786e\u5b9a\u3002\u6bd4\u5982\u5728\u7b2c\u4e00\u4e2a\u793a\u4f8b\u4e2d\uff0c\u53d8\u91cf \\(n\\) \u4e3a\u8f93\u5165\u6570\u636e\u5927\u5c0f\uff1b\u5728\u7b2c\u4e8c\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6570\u7ec4\u957f\u5ea6 \\(n\\) \u4e3a\u6570\u636e\u5927\u5c0f\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#3-on2","title":"3. \u00a0 \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 \\(n\\) \u4ee5\u5e73\u65b9\u7ea7\u522b\u589e\u957f\u3002\u5e73\u65b9\u9636\u901a\u5e38\u51fa\u73b0\u5728\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u90fd\u4e3a \\(O(n)\\) \uff0c\u56e0\u6b64\u603b\u4f53\u4e3a \\(O(n^2)\\) \uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py<pre><code>[class]{}-[func]{quadratic}\n</code></pre> time_complexity.cpp<pre><code>[class]{}-[func]{quadratic}\n</code></pre> time_complexity.java<pre><code>[class]{time_complexity}-[func]{quadratic}\n</code></pre> time_complexity.cs<pre><code>[class]{time_complexity}-[func]{quadratic}\n</code></pre> time_complexity.go<pre><code>[class]{}-[func]{quadratic}\n</code></pre> time_complexity.swift<pre><code>[class]{}-[func]{quadratic}\n</code></pre> time_complexity.js<pre><code>[class]{}-[func]{quadratic}\n</code></pre> time_complexity.ts<pre><code>[class]{}-[func]{quadratic}\n</code></pre> time_complexity.dart<pre><code>[class]{}-[func]{quadratic}\n</code></pre> time_complexity.rs<pre><code>[class]{}-[func]{quadratic}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{quadratic}\n</code></pre> time_complexity.zig<pre><code>[class]{}-[func]{quadratic}\n</code></pre> <p>\u56fe 2-10 \u5bf9\u6bd4\u4e86\u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u548c\u5e73\u65b9\u9636\u4e09\u79cd\u65f6\u95f4\u590d\u6742\u5ea6\u3002</p> <p></p> <p> \u56fe 2-10 \u00a0 \u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u548c\u5e73\u65b9\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6 </p> <p>\u4ee5\u5192\u6ce1\u6392\u5e8f\u4e3a\u4f8b\uff0c\u5916\u5c42\u5faa\u73af\u6267\u884c \\(n - 1\\) \u6b21\uff0c\u5185\u5c42\u5faa\u73af\u6267\u884c \\(n-1\\)\u3001\\(n-2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u6b21\uff0c\u5e73\u5747\u4e3a \\(n / 2\\) \u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O((n - 1) n / 2) = O(n^2)\\) \u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py<pre><code>[class]{}-[func]{bubble_sort}\n</code></pre> time_complexity.cpp<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> time_complexity.java<pre><code>[class]{time_complexity}-[func]{bubbleSort}\n</code></pre> time_complexity.cs<pre><code>[class]{time_complexity}-[func]{bubbleSort}\n</code></pre> time_complexity.go<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> time_complexity.swift<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> time_complexity.js<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> time_complexity.ts<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> time_complexity.dart<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> time_complexity.rs<pre><code>[class]{}-[func]{bubble_sort}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> time_complexity.zig<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre>"},{"location":"chapter_computational_complexity/time_complexity/#4-o2n","title":"4. \u00a0 \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>\u56fe 2-11 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u7ec6\u80de\u5206\u88c2\u7684\u8fc7\u7a0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py<pre><code>[class]{}-[func]{exponential}\n</code></pre> time_complexity.cpp<pre><code>[class]{}-[func]{exponential}\n</code></pre> time_complexity.java<pre><code>[class]{time_complexity}-[func]{exponential}\n</code></pre> time_complexity.cs<pre><code>[class]{time_complexity}-[func]{exponential}\n</code></pre> time_complexity.go<pre><code>[class]{}-[func]{exponential}\n</code></pre> time_complexity.swift<pre><code>[class]{}-[func]{exponential}\n</code></pre> time_complexity.js<pre><code>[class]{}-[func]{exponential}\n</code></pre> time_complexity.ts<pre><code>[class]{}-[func]{exponential}\n</code></pre> time_complexity.dart<pre><code>[class]{}-[func]{exponential}\n</code></pre> time_complexity.rs<pre><code>[class]{}-[func]{exponential}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{exponential}\n</code></pre> time_complexity.zig<pre><code>[class]{}-[func]{exponential}\n</code></pre> <p></p> <p> \u56fe 2-11 \u00a0 \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\u4e2d\u3002\u4f8b\u5982\u5728\u4ee5\u4e0b\u4ee3\u7801\u4e2d\uff0c\u5176\u9012\u5f52\u5730\u4e00\u5206\u4e3a\u4e8c\uff0c\u7ecf\u8fc7 \\(n\\) \u6b21\u5206\u88c2\u540e\u505c\u6b62\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py<pre><code>[class]{}-[func]{exp_recur}\n</code></pre> time_complexity.cpp<pre><code>[class]{}-[func]{expRecur}\n</code></pre> time_complexity.java<pre><code>[class]{time_complexity}-[func]{expRecur}\n</code></pre> time_complexity.cs<pre><code>[class]{time_complexity}-[func]{expRecur}\n</code></pre> time_complexity.go<pre><code>[class]{}-[func]{expRecur}\n</code></pre> time_complexity.swift<pre><code>[class]{}-[func]{expRecur}\n</code></pre> time_complexity.js<pre><code>[class]{}-[func]{expRecur}\n</code></pre> time_complexity.ts<pre><code>[class]{}-[func]{expRecur}\n</code></pre> time_complexity.dart<pre><code>[class]{}-[func]{expRecur}\n</code></pre> time_complexity.rs<pre><code>[class]{}-[func]{exp_recur}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{expRecur}\n</code></pre> time_complexity.zig<pre><code>[class]{}-[func]{expRecur}\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\u52a8\u6001\u89c4\u5212\u6216\u8d2a\u5fc3\u7b49\u7b97\u6cd5\u6765\u89e3\u51b3\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#5-olog-n","title":"5. \u00a0 \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> <p>\u56fe 2-12 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6a21\u62df\u4e86\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u201d\u7684\u8fc7\u7a0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_2 n)\\) \uff0c\u7b80\u8bb0\u4e3a \\(O(\\log n)\\) \u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py<pre><code>[class]{}-[func]{logarithmic}\n</code></pre> time_complexity.cpp<pre><code>[class]{}-[func]{logarithmic}\n</code></pre> time_complexity.java<pre><code>[class]{time_complexity}-[func]{logarithmic}\n</code></pre> time_complexity.cs<pre><code>[class]{time_complexity}-[func]{logarithmic}\n</code></pre> time_complexity.go<pre><code>[class]{}-[func]{logarithmic}\n</code></pre> time_complexity.swift<pre><code>[class]{}-[func]{logarithmic}\n</code></pre> time_complexity.js<pre><code>[class]{}-[func]{logarithmic}\n</code></pre> time_complexity.ts<pre><code>[class]{}-[func]{logarithmic}\n</code></pre> time_complexity.dart<pre><code>[class]{}-[func]{logarithmic}\n</code></pre> time_complexity.rs<pre><code>[class]{}-[func]{logarithmic}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{logarithmic}\n</code></pre> time_complexity.zig<pre><code>[class]{}-[func]{logarithmic}\n</code></pre> <p></p> <p> \u56fe 2-12 \u00a0 \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\u4e2d\u3002\u4ee5\u4e0b\u4ee3\u7801\u5f62\u6210\u4e86\u4e00\u4e2a\u9ad8\u5ea6\u4e3a \\(\\log_2 n\\) \u7684\u9012\u5f52\u6811\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py<pre><code>[class]{}-[func]{log_recur}\n</code></pre> time_complexity.cpp<pre><code>[class]{}-[func]{logRecur}\n</code></pre> time_complexity.java<pre><code>[class]{time_complexity}-[func]{logRecur}\n</code></pre> time_complexity.cs<pre><code>[class]{time_complexity}-[func]{logRecur}\n</code></pre> time_complexity.go<pre><code>[class]{}-[func]{logRecur}\n</code></pre> time_complexity.swift<pre><code>[class]{}-[func]{logRecur}\n</code></pre> time_complexity.js<pre><code>[class]{}-[func]{logRecur}\n</code></pre> time_complexity.ts<pre><code>[class]{}-[func]{logRecur}\n</code></pre> time_complexity.dart<pre><code>[class]{}-[func]{logRecur}\n</code></pre> time_complexity.rs<pre><code>[class]{}-[func]{log_recur}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{logRecur}\n</code></pre> time_complexity.zig<pre><code>[class]{}-[func]{logRecur}\n</code></pre> <p>\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u7b97\u6cd5\u4e2d\uff0c\u4f53\u73b0\u4e86\u201c\u4e00\u5206\u4e3a\u591a\u201d\u548c\u201c\u5316\u7e41\u4e3a\u7b80\u201d\u7684\u7b97\u6cd5\u601d\u60f3\u3002\u5b83\u589e\u957f\u7f13\u6162\uff0c\u662f\u4ec5\u6b21\u4e8e\u5e38\u6570\u9636\u7684\u7406\u60f3\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002</p> <p>\\(O(\\log n)\\) \u7684\u5e95\u6570\u662f\u591a\u5c11\uff1f</p> <p>\u51c6\u786e\u6765\u8bf4\uff0c\u201c\u4e00\u5206\u4e3a \\(m\\)\u201d\u5bf9\u5e94\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(\\log_m n)\\) \u3002\u800c\u901a\u8fc7\u5bf9\u6570\u6362\u5e95\u516c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u5177\u6709\u4e0d\u540c\u5e95\u6570\u7684\u3001\u76f8\u7b49\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff1a</p> \\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\] <p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5e95\u6570 \\(m\\) \u53ef\u4ee5\u5728\u4e0d\u5f71\u54cd\u590d\u6742\u5ea6\u7684\u524d\u63d0\u4e0b\u8f6c\u6362\u3002\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u7701\u7565\u5e95\u6570 \\(m\\) \uff0c\u5c06\u5bf9\u6570\u9636\u76f4\u63a5\u8bb0\u4e3a \\(O(\\log n)\\) \u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#6-on-log-n","title":"6. \u00a0 \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\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py<pre><code>[class]{}-[func]{linear_log_recur}\n</code></pre> time_complexity.cpp<pre><code>[class]{}-[func]{linearLogRecur}\n</code></pre> time_complexity.java<pre><code>[class]{time_complexity}-[func]{linearLogRecur}\n</code></pre> time_complexity.cs<pre><code>[class]{time_complexity}-[func]{linearLogRecur}\n</code></pre> time_complexity.go<pre><code>[class]{}-[func]{linearLogRecur}\n</code></pre> time_complexity.swift<pre><code>[class]{}-[func]{linearLogRecur}\n</code></pre> time_complexity.js<pre><code>[class]{}-[func]{linearLogRecur}\n</code></pre> time_complexity.ts<pre><code>[class]{}-[func]{linearLogRecur}\n</code></pre> time_complexity.dart<pre><code>[class]{}-[func]{linearLogRecur}\n</code></pre> time_complexity.rs<pre><code>[class]{}-[func]{linear_log_recur}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{linearLogRecur}\n</code></pre> time_complexity.zig<pre><code>[class]{}-[func]{linearLogRecur}\n</code></pre> <p>\u56fe 2-13 \u5c55\u793a\u4e86\u7ebf\u6027\u5bf9\u6570\u9636\u7684\u751f\u6210\u65b9\u5f0f\u3002\u4e8c\u53c9\u6811\u7684\u6bcf\u4e00\u5c42\u7684\u64cd\u4f5c\u603b\u6570\u90fd\u4e3a \\(n\\) \uff0c\u6811\u5171\u6709 \\(\\log_2 n + 1\\) \u5c42\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002</p> <p></p> <p> \u56fe 2-13 \u00a0 \u7ebf\u6027\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6 </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>"},{"location":"chapter_computational_complexity/time_complexity/#7-on","title":"7. \u00a0 \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 \\dots \\times 2 \\times 1 \\] <p>\u9636\u4e58\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u5982\u56fe 2-14 \u548c\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u7b2c\u4e00\u5c42\u5206\u88c2\u51fa \\(n\\) \u4e2a\uff0c\u7b2c\u4e8c\u5c42\u5206\u88c2\u51fa \\(n - 1\\) \u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u7b2c \\(n\\) \u5c42\u65f6\u505c\u6b62\u5206\u88c2\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig time_complexity.py<pre><code>[class]{}-[func]{factorial_recur}\n</code></pre> time_complexity.cpp<pre><code>[class]{}-[func]{factorialRecur}\n</code></pre> time_complexity.java<pre><code>[class]{time_complexity}-[func]{factorialRecur}\n</code></pre> time_complexity.cs<pre><code>[class]{time_complexity}-[func]{factorialRecur}\n</code></pre> time_complexity.go<pre><code>[class]{}-[func]{factorialRecur}\n</code></pre> time_complexity.swift<pre><code>[class]{}-[func]{factorialRecur}\n</code></pre> time_complexity.js<pre><code>[class]{}-[func]{factorialRecur}\n</code></pre> time_complexity.ts<pre><code>[class]{}-[func]{factorialRecur}\n</code></pre> time_complexity.dart<pre><code>[class]{}-[func]{factorialRecur}\n</code></pre> time_complexity.rs<pre><code>[class]{}-[func]{factorial_recur}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{factorialRecur}\n</code></pre> time_complexity.zig<pre><code>[class]{}-[func]{factorialRecur}\n</code></pre> <p></p> <p> \u56fe 2-14 \u00a0 \u9636\u4e58\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6 </p> <p>\u8bf7\u6ce8\u610f\uff0c\u56e0\u4e3a\u5f53 \\(n \\geq 4\\) \u65f6\u6052\u6709 \\(n! > 2^n\\) \uff0c\u6240\u4ee5\u9636\u4e58\u9636\u6bd4\u6307\u6570\u9636\u589e\u957f\u5f97\u66f4\u5feb\uff0c\u5728 \\(n\\) \u8f83\u5927\u65f6\u4e5f\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#235","title":"2.3.5 \u00a0 \u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6","text":"<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\u6bcf\u4e2a\u6570\u5b57\u53ea\u51fa\u73b0\u4e00\u6b21\uff1b\u4f46\u5143\u7d20\u987a\u5e8f\u662f\u968f\u673a\u6253\u4e71\u7684\uff0c\u4efb\u52a1\u76ee\u6807\u662f\u8fd4\u56de\u5143\u7d20 \\(1\\) \u7684\u7d22\u5f15\u3002\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\u3002</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\u5143\u7d20\u4e3a \\(1\\) \u65f6\uff0c\u65e0\u8bba\u6570\u7ec4\u591a\u957f\u90fd\u4e0d\u9700\u8981\u7ee7\u7eed\u904d\u5386\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(\\Omega(1)\\) \u3002</li> </ul> <p>\u201c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u201d\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u4f7f\u7528\u5927 \\(O\\) \u8bb0\u53f7\u8868\u793a\u3002\u76f8\u5e94\u5730\uff0c\u201c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u201d\u5bf9\u5e94\u51fd\u6570\u6e10\u8fd1\u4e0b\u754c\uff0c\u7528 \\(\\Omega\\) \u8bb0\u53f7\u8868\u793a\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig worst_best_time_complexity.py<pre><code>[class]{}-[func]{random_numbers}\n[class]{}-[func]{find_one}\n</code></pre> worst_best_time_complexity.cpp<pre><code>[class]{}-[func]{randomNumbers}\n[class]{}-[func]{findOne}\n</code></pre> worst_best_time_complexity.java<pre><code>[class]{worst_best_time_complexity}-[func]{randomNumbers}\n[class]{worst_best_time_complexity}-[func]{findOne}\n</code></pre> worst_best_time_complexity.cs<pre><code>[class]{worst_best_time_complexity}-[func]{randomNumbers}\n[class]{worst_best_time_complexity}-[func]{findOne}\n</code></pre> worst_best_time_complexity.go<pre><code>[class]{}-[func]{randomNumbers}\n[class]{}-[func]{findOne}\n</code></pre> worst_best_time_complexity.swift<pre><code>[class]{}-[func]{randomNumbers}\n[class]{}-[func]{findOne}\n</code></pre> worst_best_time_complexity.js<pre><code>[class]{}-[func]{randomNumbers}\n[class]{}-[func]{findOne}\n</code></pre> worst_best_time_complexity.ts<pre><code>[class]{}-[func]{randomNumbers}\n[class]{}-[func]{findOne}\n</code></pre> worst_best_time_complexity.dart<pre><code>[class]{}-[func]{randomNumbers}\n[class]{}-[func]{findOne}\n</code></pre> worst_best_time_complexity.rs<pre><code>[class]{}-[func]{random_numbers}\n[class]{}-[func]{find_one}\n</code></pre> worst_best_time_complexity.c<pre><code>[class]{}-[func]{randomNumbers}\n[class]{}-[func]{findOne}\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, &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> <p>\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6211\u4eec\u5728\u5b9e\u9645\u4e2d\u5f88\u5c11\u4f7f\u7528\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u56e0\u4e3a\u901a\u5e38\u53ea\u6709\u5728\u5f88\u5c0f\u6982\u7387\u4e0b\u624d\u80fd\u8fbe\u5230\uff0c\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u8bef\u5bfc\u6027\u3002\u800c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u66f4\u4e3a\u5b9e\u7528\uff0c\u56e0\u4e3a\u5b83\u7ed9\u51fa\u4e86\u4e00\u4e2a\u6548\u7387\u5b89\u5168\u503c\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u653e\u5fc3\u5730\u4f7f\u7528\u7b97\u6cd5\u3002</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\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u4f53\u73b0\u7b97\u6cd5\u5728\u968f\u673a\u8f93\u5165\u6570\u636e\u4e0b\u7684\u8fd0\u884c\u6548\u7387\uff0c\u7528 \\(\\Theta\\) \u8bb0\u53f7\u6765\u8868\u793a\u3002</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\u5c31\u662f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a \\(n / 2\\) \uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(\\Theta(n / 2) = \\Theta(n)\\) \u3002</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\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u4f46\u4ece\u4e25\u683c\u610f\u4e49\u4e0a\u770b\uff0c\u8fd9\u79cd\u505a\u6cd5\u5e76\u4e0d\u89c4\u8303\u3002\u5728\u672c\u4e66\u548c\u5176\u4ed6\u8d44\u6599\u4e2d\uff0c\u82e5\u9047\u5230\u7c7b\u4f3c\u201c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\)\u201d\u7684\u8868\u8ff0\uff0c\u8bf7\u5c06\u5176\u76f4\u63a5\u7406\u89e3\u4e3a \\(\\Theta(n)\\) \u3002</p>"},{"location":"chapter_data_structure/","title":"\u7b2c 3 \u7ae0 \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\uff0c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u7c7b\u578b\u3002</p> <ul> <li>\u6574\u6570\u7c7b\u578b <code>byte</code>\u3001<code>short</code>\u3001<code>int</code>\u3001<code>long</code> \u3002</li> <li>\u6d6e\u70b9\u6570\u7c7b\u578b <code>float</code>\u3001<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\u3002\u4e0b\u9762\u4ee5 Java \u4e3a\u4f8b\u3002</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>\u8868 3-1 \u5217\u4e3e\u4e86\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5360\u7528\u7a7a\u95f4\u3001\u53d6\u503c\u8303\u56f4\u548c\u9ed8\u8ba4\u503c\u3002\u6b64\u8868\u683c\u65e0\u987b\u786c\u80cc\uff0c\u5927\u81f4\u7406\u89e3\u5373\u53ef\uff0c\u9700\u8981\u65f6\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u6765\u56de\u5fc6\u3002</p> <p> \u8868 3-1 \u00a0 \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\u8868 3-1 \uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\u3002</p> <ul> <li>C \u548c C++ \u672a\u660e\u786e\u89c4\u5b9a\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5927\u5c0f\uff0c\u800c\u56e0\u5b9e\u73b0\u548c\u5e73\u53f0\u5404\u5f02\u3002\u8868 3-1 \u9075\u5faa LP64 \u6570\u636e\u6a21\u578b\uff0c\u5176\u7528\u4e8e\u5305\u62ec Linux \u548c macOS \u5728\u5185\u7684 Unix 64 \u4f4d\u64cd\u4f5c\u7cfb\u7edf\u3002</li> <li>\u5b57\u7b26 <code>char</code> \u7684\u5927\u5c0f\u5728 C \u548c C++ \u4e2d\u4e3a 1 \u5b57\u8282\uff0c\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\u53d6\u51b3\u4e8e\u7279\u5b9a\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6cd5\uff0c\u8be6\u89c1\u201c\u5b57\u7b26\u7f16\u7801\u201d\u7ae0\u8282\u3002</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> \u6216\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\uff0c\u5305\u62ec <code>int</code>\u3001<code>float</code>\u3001<code>char</code>\u3001<code>bool</code> \u7b49\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig <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\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\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 = 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\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\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>// 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\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> bools = List.filled(5, false);\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6765\u521d\u59cb\u5316\u6570\u7ec4\nlet numbers: Vec<i32> = vec![0; 5];\nlet decimals: Vec<float> = vec![0.0, 5];\nlet characters: Vec<char> = vec!['0'; 5];\nlet bools: Vec<bool> = vec![false; 5];\n</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>\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\u5982\u56fe 3-6 \u6240\u793a\uff0cASCII \u7801\u5305\u62ec\u82f1\u6587\u5b57\u6bcd\u7684\u5927\u5c0f\u5199\u3001\u6570\u5b57 0 ~ 9\u3001\u4e00\u4e9b\u6807\u70b9\u7b26\u53f7\uff0c\u4ee5\u53ca\u4e00\u4e9b\u63a7\u5236\u5b57\u7b26\uff08\u5982\u6362\u884c\u7b26\u548c\u5236\u8868\u7b26\uff09\u3002</p> <p></p> <p> \u56fe 3-6 \u00a0 ASCII \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\u56fe 3-7 \u6240\u793a\uff0c\u201cHello\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u5b57\u8282\uff0c\u201c\u7b97\u6cd5\u201d\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u5b57\u8282\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9ad8\u4f4d\u586b 0 \uff0c\u5c06\u201cHello \u7b97\u6cd5\u201d\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u90fd\u7f16\u7801\u4e3a 2 \u5b57\u8282\u957f\u5ea6\u3002\u8fd9\u6837\u7cfb\u7edf\u5c31\u53ef\u4ee5\u6bcf\u9694 2 \u5b57\u8282\u89e3\u6790\u4e00\u4e2a\u5b57\u7b26\uff0c\u6062\u590d\u51fa\u8fd9\u4e2a\u77ed\u8bed\u7684\u5185\u5bb9\u4e86\u3002</p> <p></p> <p> \u56fe 3-7 \u00a0 Unicode \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\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u3002</p> <ul> <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 > 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> </ul> <p>\u56fe 3-8 \u5c55\u793a\u4e86\u201cHello\u7b97\u6cd5\u201d\u5bf9\u5e94\u7684 UTF-8 \u7f16\u7801\u3002\u89c2\u5bdf\u53d1\u73b0\uff0c\u7531\u4e8e\u6700\u9ad8 \\(n\\) \u4f4d\u90fd\u88ab\u8bbe\u7f6e\u4e3a \\(1\\) \uff0c\u56e0\u6b64\u7cfb\u7edf\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6700\u9ad8\u4f4d \\(1\\) \u7684\u4e2a\u6570\u6765\u89e3\u6790\u51fa\u5b57\u7b26\u7684\u957f\u5ea6\u4e3a \\(n\\) \u3002</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 3-8 \u00a0 UTF-8 \u7f16\u7801\u793a\u4f8b </p> <p>\u9664\u4e86 UTF-8 \u4e4b\u5916\uff0c\u5e38\u89c1\u7684\u7f16\u7801\u65b9\u5f0f\u8fd8\u5305\u62ec\u4ee5\u4e0b\u4e24\u79cd\u3002</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\u5728\u7b49\u957f\u7f16\u7801\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u770b\u4f5c\u6570\u7ec4\u6765\u5904\u7406\uff0c\u8fd9\u79cd\u505a\u6cd5\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\u3002</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\u3002</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\u4e00\u4e9b\u4e0d\u540c\u7684\u7f16\u7801\u65b9\u6848\u3002</p> <ul> <li>Python \u4e2d\u7684 <code>str</code> \u4f7f\u7528 Unicode \u7f16\u7801\uff0c\u5e76\u91c7\u7528\u4e00\u79cd\u7075\u6d3b\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u5b58\u50a8\u7684\u5b57\u7b26\u957f\u5ea6\u53d6\u51b3\u4e8e\u5b57\u7b26\u4e32\u4e2d\u6700\u5927\u7684 Unicode \u7801\u70b9\u3002\u82e5\u5b57\u7b26\u4e32\u4e2d\u5168\u90e8\u662f ASCII \u5b57\u7b26\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 1 \u4e2a\u5b57\u8282\uff1b\u5982\u679c\u6709\u5b57\u7b26\u8d85\u51fa\u4e86 ASCII \u8303\u56f4\uff0c\u4f46\u5168\u90e8\u5728\u57fa\u672c\u591a\u8bed\u8a00\u5e73\u9762\uff08BMP\uff09\u5185\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 2 \u4e2a\u5b57\u8282\uff1b\u5982\u679c\u6709\u8d85\u51fa BMP \u7684\u5b57\u7b26\uff0c\u5219\u6bcf\u4e2a\u5b57\u7b26\u5360\u7528 4 \u4e2a\u5b57\u8282\u3002</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 <code>char</code> \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>\u903b\u8f91\u7ed3\u6784\u63ed\u793a\u4e86\u6570\u636e\u5143\u7d20\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\u3002\u5728\u6570\u7ec4\u548c\u94fe\u8868\u4e2d\uff0c\u6570\u636e\u6309\u7167\u987a\u5e8f\u4f9d\u6b21\u6392\u5217\uff0c\u4f53\u73b0\u4e86\u6570\u636e\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\uff1b\u800c\u5728\u6811\u4e2d\uff0c\u6570\u636e\u4ece\u9876\u90e8\u5411\u4e0b\u6309\u5c42\u6b21\u6392\u5217\uff0c\u8868\u73b0\u51fa\u7956\u5148\u4e0e\u540e\u4ee3\u4e4b\u95f4\u7684\u6d3e\u751f\u5173\u7cfb\uff1b\u56fe\u5219\u7531\u8282\u70b9\u548c\u8fb9\u6784\u6210\uff0c\u53cd\u6620\u4e86\u590d\u6742\u7684\u7f51\u7edc\u5173\u7cfb\u3002</p> <p>\u5982\u56fe 3-1 \u6240\u793a\uff0c\u903b\u8f91\u7ed3\u6784\u53ef\u88ab\u5206\u4e3a\u201c\u7ebf\u6027\u201d\u548c\u201c\u975e\u7ebf\u6027\u201d\u4e24\u5927\u7c7b\u3002\u7ebf\u6027\u7ed3\u6784\u6bd4\u8f83\u76f4\u89c2\uff0c\u6307\u6570\u636e\u5728\u903b\u8f91\u5173\u7cfb\u4e0a\u5448\u7ebf\u6027\u6392\u5217\uff1b\u975e\u7ebf\u6027\u7ed3\u6784\u5219\u76f8\u53cd\uff0c\u5448\u975e\u7ebf\u6027\u6392\u5217\u3002</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 3-1 \u00a0 \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\u5206\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\u56fe 3-2 \u5c55\u793a\u4e86\u4e00\u4e2a\u8ba1\u7b97\u673a\u5185\u5b58\u6761\uff0c\u5176\u4e2d\u6bcf\u4e2a\u9ed1\u8272\u65b9\u5757\u90fd\u5305\u542b\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5185\u5b58\u60f3\u8c61\u6210\u4e00\u4e2a\u5de8\u5927\u7684 Excel \u8868\u683c\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5355\u5143\u683c\u90fd\u53ef\u4ee5\u5b58\u50a8\u4e00\u5b9a\u5927\u5c0f\u7684\u6570\u636e\uff0c\u5728\u7b97\u6cd5\u8fd0\u884c\u65f6\uff0c\u6240\u6709\u6570\u636e\u90fd\u88ab\u5b58\u50a8\u5728\u8fd9\u4e9b\u5355\u5143\u683c\u4e2d\u3002</p> <p>\u7cfb\u7edf\u901a\u8fc7\u5185\u5b58\u5730\u5740\u6765\u8bbf\u95ee\u76ee\u6807\u4f4d\u7f6e\u7684\u6570\u636e\u3002\u5982\u56fe 3-2 \u6240\u793a\uff0c\u8ba1\u7b97\u673a\u6839\u636e\u7279\u5b9a\u89c4\u5219\u4e3a\u8868\u683c\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u5206\u914d\u7f16\u53f7\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5185\u5b58\u7a7a\u95f4\u90fd\u6709\u552f\u4e00\u7684\u5185\u5b58\u5730\u5740\u3002\u6709\u4e86\u8fd9\u4e9b\u5730\u5740\uff0c\u7a0b\u5e8f\u4fbf\u53ef\u4ee5\u8bbf\u95ee\u5185\u5b58\u4e2d\u7684\u6570\u636e\u3002</p> <p></p> <p> \u56fe 3-2 \u00a0 \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\u5206\u6563\u7684\u5185\u5b58\u7a7a\u95f4\u5185\u3002</p> <p>\u5982\u56fe 3-3 \u6240\u793a\uff0c\u7269\u7406\u7ed3\u6784\u53cd\u6620\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u53ef\u5206\u4e3a\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\uff08\u6570\u7ec4\uff09\u548c\u5206\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\u3002\u7269\u7406\u7ed3\u6784\u4ece\u5e95\u5c42\u51b3\u5b9a\u4e86\u6570\u636e\u7684\u8bbf\u95ee\u3001\u66f4\u65b0\u3001\u589e\u5220\u7b49\u64cd\u4f5c\u65b9\u6cd5\uff0c\u540c\u65f6\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u65b9\u9762\u5448\u73b0\u51fa\u4e92\u8865\u7684\u7279\u70b9\u3002</p> <p></p> <p> \u56fe 3-3 \u00a0 \u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u4e0e\u5206\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\u679c\u4f60\u611f\u89c9\u7269\u7406\u7ed3\u6784\u7406\u89e3\u8d77\u6765\u6709\u56f0\u96be\uff0c\u5efa\u8bae\u5148\u9605\u8bfb\u4e0b\u4e00\u7ae0\u201c\u6570\u7ec4\u4e0e\u94fe\u8868\u201d\uff0c\u7136\u540e\u518d\u56de\u987e\u672c\u8282\u5185\u5bb9\u3002</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 \u6574\u6570\u7f16\u7801","text":"<p>\u5728\u4e0a\u4e00\u8282\u7684\u8868\u683c\u4e2d\u6211\u4eec\u53d1\u73b0\uff0c\u6240\u6709\u6574\u6570\u7c7b\u578b\u80fd\u591f\u8868\u793a\u7684\u8d1f\u6570\u90fd\u6bd4\u6b63\u6570\u591a\u4e00\u4e2a\uff0c\u4f8b\u5982 <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>\u9996\u5148\u9700\u8981\u6307\u51fa\uff0c\u6570\u5b57\u662f\u4ee5\u201c\u8865\u7801\u201d\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u4e2d\u7684\u3002\u5728\u5206\u6790\u8fd9\u6837\u505a\u7684\u539f\u56e0\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u7ed9\u51fa\u4e09\u8005\u7684\u5b9a\u4e49\u3002</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>\u56fe 3-4 \u5c55\u793a\u4e86\u539f\u7801\u3001\u53cd\u7801\u548c\u8865\u7801\u4e4b\u95f4\u7684\u8f6c\u6362\u65b9\u6cd5\u3002</p> <p></p> <p> \u56fe 3-4 \u00a0 \u539f\u7801\u3001\u53cd\u7801\u4e0e\u8865\u7801\u4e4b\u95f4\u7684\u76f8\u4e92\u8f6c\u6362 </p> <p>\u300c\u539f\u7801 true form\u300d\u867d\u7136\u6700\u76f4\u89c2\uff0c\u4f46\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002\u4e00\u65b9\u9762\uff0c\u8d1f\u6570\u7684\u539f\u7801\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u8fd0\u7b97\u3002\u4f8b\u5982\u5728\u539f\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f \\(-3\\) \uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002</p> \\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline & = 1000 \\; 0011 \\newline & \\rightarrow -3 \\end{aligned} \\] <p>\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8ba1\u7b97\u673a\u5f15\u5165\u4e86\u300c\u53cd\u7801 1's complement code\u300d\u3002\u5982\u679c\u6211\u4eec\u5148\u5c06\u539f\u7801\u8f6c\u6362\u4e3a\u53cd\u7801\uff0c\u5e76\u5728\u53cd\u7801\u4e0b\u8ba1\u7b97 \\(1 + (-2)\\) \uff0c\u6700\u540e\u5c06\u7ed3\u679c\u4ece\u53cd\u7801\u8f6c\u5316\u56de\u539f\u7801\uff0c\u5219\u53ef\u5f97\u5230\u6b63\u786e\u7ed3\u679c \\(-1\\) \u3002</p> \\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 \\; \\text{(\u539f\u7801)} + 1000 \\; 0010 \\; \\text{(\u539f\u7801)} \\newline & = 0000 \\; 0001 \\; \\text{(\u53cd\u7801)} + 1111 \\; 1101 \\; \\text{(\u53cd\u7801)} \\newline & = 1111 \\; 1110 \\; \\text{(\u53cd\u7801)} \\newline & = 1000 \\; 0001 \\; \\text{(\u539f\u7801)} \\newline & \\rightarrow -1 \\end{aligned} \\] <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 & \\rightarrow 0000 \\; 0000 \\newline -0 & \\rightarrow 1000 \\; 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 2's complement code\u300d\u3002\u6211\u4eec\u5148\u6765\u89c2\u5bdf\u4e00\u4e0b\u8d1f\u96f6\u7684\u539f\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u7684\u8f6c\u6362\u8fc7\u7a0b\uff1a</p> \\[ \\begin{aligned} -0 \\rightarrow \\; & 1000 \\; 0000 \\; \\text{(\u539f\u7801)} \\newline = \\; & 1111 \\; 1111 \\; \\text{(\u53cd\u7801)} \\newline = 1 \\; & 0000 \\; 0000 \\; \\text{(\u8865\u7801)} \\newline \\end{aligned} \\] <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 \\; 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 \\; 0000\\) \u662f\u4e00\u4e2a\u4f8b\u5916\uff0c\u5b83\u5e76\u6ca1\u6709\u5bf9\u5e94\u7684\u539f\u7801\u3002\u6839\u636e\u8f6c\u6362\u65b9\u6cd5\uff0c\u6211\u4eec\u5f97\u5230\u8be5\u8865\u7801\u7684\u539f\u7801\u4e3a \\(0000 \\; 0000\\) \u3002\u8fd9\u663e\u7136\u662f\u77db\u76fe\u7684\uff0c\u56e0\u4e3a\u8be5\u539f\u7801\u8868\u793a\u6570\u5b57 \\(0\\) \uff0c\u5b83\u7684\u8865\u7801\u5e94\u8be5\u662f\u81ea\u8eab\u3002\u8ba1\u7b97\u673a\u89c4\u5b9a\u8fd9\u4e2a\u7279\u6b8a\u7684\u8865\u7801 \\(1000 \\; 0000\\) \u4ee3\u8868 \\(-128\\) \u3002\u5b9e\u9645\u4e0a\uff0c\\((-1) + (-127)\\) \u5728\u8865\u7801\u4e0b\u7684\u8ba1\u7b97\u7ed3\u679c\u5c31\u662f \\(-128\\) \u3002</p> \\[ \\begin{aligned} & (-127) + (-1) \\newline & \\rightarrow 1111 \\; 1111 \\; \\text{(\u539f\u7801)} + 1000 \\; 0001 \\; \\text{(\u539f\u7801)} \\newline & = 1000 \\; 0000 \\; \\text{(\u53cd\u7801)} + 1111 \\; 1110 \\; \\text{(\u53cd\u7801)} \\newline & = 1000 \\; 0001 \\; \\text{(\u8865\u7801)} + 1111 \\; 1111 \\; \\text{(\u8865\u7801)} \\newline & = 1000 \\; 0000 \\; \\text{(\u8865\u7801)} \\newline & \\rightarrow -128 \\end{aligned} \\] <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\u987b\u7279\u522b\u5904\u7406\u6b63\u8d1f\u96f6\u7684\u6b67\u4e49\u95ee\u9898\u3002\u8fd9\u5927\u5927\u7b80\u5316\u4e86\u786c\u4ef6\u8bbe\u8ba1\uff0c\u63d0\u9ad8\u4e86\u8fd0\u7b97\u6548\u7387\u3002</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\u4e09\u4e2a\u90e8\u5206\u6784\u6210\u3002</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 & \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = & (1 + \\sum_{i=1}^{23} b_{23-i} 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\] <p></p> <p> \u56fe 3-5 \u00a0 IEEE 754 \u6807\u51c6\u4e0b\u7684 float \u7684\u8ba1\u7b97\u793a\u4f8b </p> <p>\u89c2\u5bdf\u56fe 3-5 \uff0c\u7ed9\u5b9a\u4e00\u4e2a\u793a\u4f8b\u6570\u636e \\(\\mathrm{S} = 0\\) \uff0c \\(\\mathrm{E} = 124\\) \uff0c\\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\) \uff0c\u5219\u6709\uff1a</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>\u5982\u8868 3-2 \u6240\u793a\uff0c\u6307\u6570\u4f4d \\(E = 0\\) \u548c \\(E = 255\\) \u5177\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u7528\u4e8e\u8868\u793a\u96f6\u3001\u65e0\u7a77\u5927\u3001\\(\\mathrm{NaN}\\) \u7b49\u3002</p> <p> \u8868 3-2 \u00a0 \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>\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u6b21\u6b63\u89c4\u6570\u663e\u8457\u63d0\u5347\u4e86\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u3002\u6700\u5c0f\u6b63\u6b63\u89c4\u6570\u4e3a \\(2^{-126}\\) \uff0c\u6700\u5c0f\u6b63\u6b21\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\times 2^{-23}\\) \u3002</p> <p>\u53cc\u7cbe\u5ea6 <code>double</code> \u4e5f\u91c7\u7528\u7c7b\u4f3c <code>float</code> \u7684\u8868\u793a\u65b9\u6cd5\uff0c\u5728\u6b64\u4e0d\u505a\u8d58\u8ff0\u3002</p>"},{"location":"chapter_data_structure/summary/","title":"3.5 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_data_structure/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","text":"<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\u5206\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\u3002\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u7531\u6570\u7ec4\u3001\u94fe\u8868\u6216\u4e24\u8005\u7684\u7ec4\u5408\u5b9e\u73b0\u7684\u3002</li> <li>\u8ba1\u7b97\u673a\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5305\u62ec\u6574\u6570 <code>byte</code>\u3001<code>short</code>\u3001<code>int</code>\u3001<code>long</code> \uff0c\u6d6e\u70b9\u6570 <code>float</code>\u3001<code>double</code> \uff0c\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 \u548c C# \u7b49\u7f16\u7a0b\u8bed\u8a00\u9ed8\u8ba4\u4f7f\u7528 UTF-16 \u7f16\u7801\u3002</li> </ul>"},{"location":"chapter_data_structure/summary/#2-q-a","title":"2. \u00a0 Q & 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\u54c8\u5e0c\u8868\u7ae0\u8282\u4f1a\u8bb2\uff09\u3002\u5728\u62c9\u94fe\u6cd5\u4e2d\uff0c\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5730\u5740\uff08\u6876\uff09\u6307\u5411\u4e00\u4e2a\u94fe\u8868\uff1b\u5f53\u8fd9\u4e2a\u94fe\u8868\u957f\u5ea6\u8d85\u8fc7\u4e00\u5b9a\u9608\u503c\u65f6\uff0c\u53c8\u53ef\u80fd\u88ab\u8f6c\u5316\u4e3a\u6811\uff08\u901a\u5e38\u4e3a\u7ea2\u9ed1\u6811\uff09\u3002\u56e0\u6b64\uff0c\u54c8\u5e0c\u8868\u53ef\u80fd\u540c\u65f6\u5305\u542b\u7ebf\u6027\uff08\u6570\u7ec4\u3001\u94fe\u8868\uff09\u548c\u975e\u7ebf\u6027\uff08\u6811\uff09\u6570\u636e\u7ed3\u6784\u3002</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\u3001JS\u3001TS\u3001C# \u90fd\u91c7\u7528 UTF-16 \u7f16\u7801\uff08\u4fdd\u5b58 Unicode \u7801\u70b9\uff09\uff0c\u56e0\u6b64 char \u7c7b\u578b\u7684\u957f\u5ea6\u4e3a 2 bytes \u3002</p>"},{"location":"chapter_divide_and_conquer/","title":"\u7b2c 12 \u7ae0 \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\u3002</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\u4e8c\u5206\u67e5\u627e\u548c\u6811\u3002</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>\u4e8c\u5206\u67e5\u627e\u7684\u5206\u6cbb\u7b56\u7565\u5982\u4e0b\u6240\u793a\u3002</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\u987b\u5408\u5e76\uff1a\u4e8c\u5206\u67e5\u627e\u65e8\u5728\u67e5\u627e\u4e00\u4e2a\u7279\u5b9a\u5143\u7d20\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\u3002\u5f53\u5b50\u95ee\u9898\u5f97\u5230\u89e3\u51b3\u65f6\uff0c\u539f\u95ee\u9898\u4e5f\u4f1a\u540c\u65f6\u5f97\u5230\u89e3\u51b3\u3002</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":"1. \u00a0 \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\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u4e8c\u5206\u67e5\u627e\u3002</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> \u548c <code>2.</code> \u6b65\uff0c\u76f4\u81f3\u627e\u5230 <code>target</code> \u6216\u533a\u95f4\u4e3a\u7a7a\u65f6\u8fd4\u56de\u3002</li> </ol> <p>\u56fe 12-4 \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 12-4 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_recur.py<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{binary_search}\n</code></pre> binary_search_recur.cpp<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{binarySearch}\n</code></pre> binary_search_recur.java<pre><code>[class]{binary_search_recur}-[func]{dfs}\n[class]{binary_search_recur}-[func]{binarySearch}\n</code></pre> binary_search_recur.cs<pre><code>[class]{binary_search_recur}-[func]{dfs}\n[class]{binary_search_recur}-[func]{binarySearch}\n</code></pre> binary_search_recur.go<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{binarySearch}\n</code></pre> binary_search_recur.swift<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{binarySearch}\n</code></pre> binary_search_recur.js<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{binarySearch}\n</code></pre> binary_search_recur.ts<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{binarySearch}\n</code></pre> binary_search_recur.dart<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{binarySearch}\n</code></pre> binary_search_recur.rs<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{binary_search}\n</code></pre> binary_search_recur.c<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>"},{"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 12-5 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u7684\u793a\u4f8b\u6570\u636e </p>"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#1","title":"1. \u00a0 \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\uff0c\u5176\u662f\u4e00\u4e2a\u5178\u578b\u7684\u5206\u6cbb\u95ee\u9898\u3002</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":"2. \u00a0 \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\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\u3002</p> <ul> <li>\u524d\u5e8f\u904d\u5386\uff1a<code>[ \u6839\u8282\u70b9 | \u5de6\u5b50\u6811 | \u53f3\u5b50\u6811 ]</code> \uff0c\u4f8b\u5982\u56fe 12-5 \u7684\u6811\u5bf9\u5e94 <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\u56fe 12-5 \u7684\u6811\u5bf9\u5e94 <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\u56fe 12-6 \u6240\u793a\u7684\u6b65\u9aa4\u5f97\u5230\u5212\u5206\u7ed3\u679c\u3002</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 12-6 \u00a0 \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":"3. \u00a0 \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\u3002</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\u8868 12-1 \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 12-1 \u00a0 \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\u56fe 12-7 \u7406\u89e3\u3002</p> <p></p> <p> \u56fe 12-7 \u00a0 \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":"4. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig build_tree.py<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{build_tree}\n</code></pre> build_tree.cpp<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{buildTree}\n</code></pre> build_tree.java<pre><code>[class]{build_tree}-[func]{dfs}\n[class]{build_tree}-[func]{buildTree}\n</code></pre> build_tree.cs<pre><code>[class]{build_tree}-[func]{dfs}\n[class]{build_tree}-[func]{buildTree}\n</code></pre> build_tree.go<pre><code>[class]{}-[func]{dfsBuildTree}\n[class]{}-[func]{buildTree}\n</code></pre> build_tree.swift<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{buildTree}\n</code></pre> build_tree.js<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{buildTree}\n</code></pre> build_tree.ts<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{buildTree}\n</code></pre> build_tree.dart<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{buildTree}\n</code></pre> build_tree.rs<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{build_tree}\n</code></pre> build_tree.c<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> <p>\u56fe 12-8 \u5c55\u793a\u4e86\u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5404\u4e2a\u8282\u70b9\u662f\u5728\u5411\u4e0b\u201c\u9012\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\uff0c\u800c\u5404\u6761\u8fb9\uff08\u5373\u5f15\u7528\uff09\u662f\u5728\u5411\u4e0a\u201c\u5f52\u201d\u7684\u8fc7\u7a0b\u4e2d\u5efa\u7acb\u7684\u3002</p> <1><2><3><4><5><6><7><8><9> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 12-8 \u00a0 \u6784\u5efa\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b </p> <p>\u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u5185\u7684\u524d\u5e8f\u904d\u5386 <code>preorder</code> \u548c\u4e2d\u5e8f\u904d\u5386 <code>inorder</code> \u7684\u5212\u5206\u7ed3\u679c\u5982\u56fe 12-9 \u6240\u793a\u3002</p> <p></p> <p> \u56fe 12-9 \u00a0 \u6bcf\u4e2a\u9012\u5f52\u51fd\u6570\u4e2d\u7684\u5212\u5206\u7ed3\u679c </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\u4e2a\u6b65\u9aa4\u3002</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>\u5982\u56fe 12-1 \u6240\u793a\uff0c\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u662f\u5206\u6cbb\u7b56\u7565\u7684\u5178\u578b\u5e94\u7528\u4e4b\u4e00\u3002</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 12-1 \u00a0 \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\u3002</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\uff0c\u5f52\u5e76\u6392\u5e8f\u662f\u6ee1\u8db3\u4ee5\u4e0a\u4e09\u6761\u5224\u65ad\u4f9d\u636e\u7684\u3002</p> <ol> <li>\u95ee\u9898\u53ef\u4ee5\u88ab\u5206\u89e3\uff1a\u9012\u5f52\u5730\u5c06\u6570\u7ec4\uff08\u539f\u95ee\u9898\uff09\u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\uff09\u3002</li> <li>\u5b50\u95ee\u9898\u662f\u72ec\u7acb\u7684\uff1a\u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u53ef\u4ee5\u72ec\u7acb\u5730\u8fdb\u884c\u6392\u5e8f\uff08\u5b50\u95ee\u9898\u53ef\u4ee5\u72ec\u7acb\u8fdb\u884c\u6c42\u89e3\uff09\u3002</li> <li>\u5b50\u95ee\u9898\u7684\u89e3\u53ef\u4ee5\u88ab\u5408\u5e76\uff1a\u4e24\u4e2a\u6709\u5e8f\u5b50\u6570\u7ec4\uff08\u5b50\u95ee\u9898\u7684\u89e3\uff09\u53ef\u4ee5\u88ab\u5408\u5e76\u4e3a\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\uff08\u539f\u95ee\u9898\u7684\u89e3\uff09\u3002</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":"1. \u00a0 \u64cd\u4f5c\u6570\u91cf\u4f18\u5316","text":"<p>\u4ee5\u201c\u5192\u6ce1\u6392\u5e8f\u201d\u4e3a\u4f8b\uff0c\u5176\u5904\u7406\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\u9700\u8981 \\(O(n^2)\\) \u65f6\u95f4\u3002\u5047\u8bbe\u6211\u4eec\u6309\u7167\u56fe 12-2 \u6240\u793a\u7684\u65b9\u5f0f\uff0c\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u5219\u5212\u5206\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u6392\u5e8f\u6bcf\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O((n / 2)^2)\\) \u65f6\u95f4\uff0c\u5408\u5e76\u4e24\u4e2a\u5b50\u6570\u7ec4\u9700\u8981 \\(O(n)\\) \u65f6\u95f4\uff0c\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a</p> \\[ O(n + (\\frac{n}{2})^2 \\times 2 + n) = O(\\frac{n^2}{2} + 2n) \\] <p></p> <p> \u56fe 12-2 \u00a0 \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 & > \\frac{n^2}{2} + 2n \\newline n^2 - \\frac{n^2}{2} - 2n & > 0 \\newline n(n - 4) & > 0 \\end{aligned} \\] <p>\u8fd9\u610f\u5473\u7740\u5f53 \\(n > 4\\) \u65f6\uff0c\u5212\u5206\u540e\u7684\u64cd\u4f5c\u6570\u91cf\u66f4\u5c11\uff0c\u6392\u5e8f\u6548\u7387\u5e94\u8be5\u66f4\u9ad8\u3002\u8bf7\u6ce8\u610f\uff0c\u5212\u5206\u540e\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u7136\u662f\u5e73\u65b9\u9636 \\(O(n^2)\\) \uff0c\u53ea\u662f\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\u53d8\u5c0f\u4e86\u3002</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\u201c\u5f52\u5e76\u6392\u5e8f\u201d\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\u201c\u6876\u6392\u5e8f\u201d\u975e\u5e38\u7c7b\u4f3c\uff0c\u5b83\u975e\u5e38\u9002\u5408\u6392\u5e8f\u6d77\u91cf\u6570\u636e\uff0c\u7406\u8bba\u4e0a\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8fbe\u5230 \\(O(n + k)\\) \u3002</p>"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#2","title":"2. \u00a0 \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\u56fe 12-3 \u6240\u793a\u7684\u201c\u6876\u6392\u5e8f\u201d\u4e2d\uff0c\u6211\u4eec\u5c06\u6d77\u91cf\u7684\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff0c\u5219\u53ef\u6240\u6709\u6876\u7684\u6392\u5e8f\u4efb\u52a1\u5206\u6563\u5230\u5404\u4e2a\u8ba1\u7b97\u5355\u5143\uff0c\u5b8c\u6210\u540e\u518d\u8fdb\u884c\u7ed3\u679c\u5408\u5e76\u3002</p> <p></p> <p> \u56fe 12-3 \u00a0 \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\u3002</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\u3002</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>\u3001<code>B</code> \u548c <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\u3002</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 12-10 \u00a0 \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":"1. \u00a0 \u8003\u8651\u57fa\u672c\u60c5\u51b5","text":"<p>\u5982\u56fe 12-11 \u6240\u793a\uff0c\u5bf9\u4e8e\u95ee\u9898 \\(f(1)\\) \uff0c\u5373\u5f53\u53ea\u6709\u4e00\u4e2a\u5706\u76d8\u65f6\uff0c\u6211\u4eec\u5c06\u5b83\u76f4\u63a5\u4ece <code>A</code> \u79fb\u52a8\u81f3 <code>C</code> \u5373\u53ef\u3002</p> <1><2> <p></p> <p></p> <p> \u56fe 12-11 \u00a0 \u89c4\u6a21\u4e3a 1 \u95ee\u9898\u7684\u89e3 </p> <p>\u5982\u56fe 12-12 \u6240\u793a\uff0c\u5bf9\u4e8e\u95ee\u9898 \\(f(2)\\) \uff0c\u5373\u5f53\u6709\u4e24\u4e2a\u5706\u76d8\u65f6\uff0c\u7531\u4e8e\u8981\u65f6\u523b\u6ee1\u8db3\u5c0f\u5706\u76d8\u5728\u5927\u5706\u76d8\u4e4b\u4e0a\uff0c\u56e0\u6b64\u9700\u8981\u501f\u52a9 <code>B</code> \u6765\u5b8c\u6210\u79fb\u52a8\u3002</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> <1><2><3><4> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 12-12 \u00a0 \u89c4\u6a21\u4e3a 2 \u95ee\u9898\u7684\u89e3 </p> <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>"},{"location":"chapter_divide_and_conquer/hanota_problem/#2","title":"2. \u00a0 \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</p> <p>\u56e0\u4e3a\u5df2\u77e5 \\(f(1)\\) \u548c \\(f(2)\\) \u7684\u89e3\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ece\u5206\u6cbb\u89d2\u5ea6\u601d\u8003\uff0c\u5c06 <code>A</code> \u9876\u90e8\u7684\u4e24\u4e2a\u5706\u76d8\u770b\u505a\u4e00\u4e2a\u6574\u4f53\uff0c\u6267\u884c\u56fe 12-13 \u6240\u793a\u7684\u6b65\u9aa4\u3002\u8fd9\u6837\u4e09\u4e2a\u5706\u76d8\u5c31\u88ab\u987a\u5229\u5730\u4ece <code>A</code> \u79fb\u52a8\u81f3 <code>C</code> \u4e86\u3002</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> <1><2><3><4> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 12-13 \u00a0 \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\u56fe 12-14 \u6240\u793a\u7684\u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565\uff1a\u5c06\u539f\u95ee\u9898 \\(f(n)\\) \u5212\u5206\u4e3a\u4e24\u4e2a\u5b50\u95ee\u9898 \\(f(n-1)\\) \u548c\u4e00\u4e2a\u5b50\u95ee\u9898 \\(f(1)\\) \uff0c\u5e76\u6309\u7167\u4ee5\u4e0b\u987a\u5e8f\u89e3\u51b3\u8fd9\u4e09\u4e2a\u5b50\u95ee\u9898\u3002</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 12-14 \u00a0 \u6c49\u8bfa\u5854\u95ee\u9898\u7684\u5206\u6cbb\u7b56\u7565 </p>"},{"location":"chapter_divide_and_conquer/hanota_problem/#3","title":"3. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig hanota.py<pre><code>[class]{}-[func]{move}\n[class]{}-[func]{dfs}\n[class]{}-[func]{solve_hanota}\n</code></pre> hanota.cpp<pre><code>[class]{}-[func]{move}\n[class]{}-[func]{dfs}\n[class]{}-[func]{solveHanota}\n</code></pre> hanota.java<pre><code>[class]{hanota}-[func]{move}\n[class]{hanota}-[func]{dfs}\n[class]{hanota}-[func]{solveHanota}\n</code></pre> hanota.cs<pre><code>[class]{hanota}-[func]{move}\n[class]{hanota}-[func]{dfs}\n[class]{hanota}-[func]{solveHanota}\n</code></pre> hanota.go<pre><code>[class]{}-[func]{move}\n[class]{}-[func]{dfsHanota}\n[class]{}-[func]{solveHanota}\n</code></pre> hanota.swift<pre><code>[class]{}-[func]{move}\n[class]{}-[func]{dfs}\n[class]{}-[func]{solveHanota}\n</code></pre> hanota.js<pre><code>[class]{}-[func]{move}\n[class]{}-[func]{dfs}\n[class]{}-[func]{solveHanota}\n</code></pre> hanota.ts<pre><code>[class]{}-[func]{move}\n[class]{}-[func]{dfs}\n[class]{}-[func]{solveHanota}\n</code></pre> hanota.dart<pre><code>[class]{}-[func]{move}\n[class]{}-[func]{dfs}\n[class]{}-[func]{solveHanota}\n</code></pre> hanota.rs<pre><code>[class]{}-[func]{move_pan}\n[class]{}-[func]{dfs}\n[class]{}-[func]{solve_hanota}\n</code></pre> hanota.c<pre><code>[class]{}-[func]{move}\n[class]{}-[func]{dfs}\n[class]{}-[func]{solveHanota}\n</code></pre> hanota.zig<pre><code>[class]{}-[func]{move}\n[class]{}-[func]{dfs}\n[class]{}-[func]{solveHanota}\n</code></pre> <p>\u5982\u56fe 12-15 \u6240\u793a\uff0c\u6c49\u8bfa\u5854\u95ee\u9898\u5f62\u6210\u4e00\u4e2a\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u9012\u5f52\u6811\uff0c\u6bcf\u4e2a\u8282\u70b9\u4ee3\u8868\u4e00\u4e2a\u5b50\u95ee\u9898\u3001\u5bf9\u5e94\u4e00\u4e2a\u5f00\u542f\u7684 <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 12-15 \u00a0 \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\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\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\u7b56\u7565\u7684\u53e6\u4e00\u4e2a\u5178\u578b\u5e94\u7528\uff0c\u5b83\u4e0d\u5305\u542b\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u8fdb\u884c\u5408\u5e76\u7684\u6b65\u9aa4\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u5206\u6cbb\u5b9e\u73b0\u4e8c\u5206\u67e5\u627e\u3002</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":"\u7b2c 14 \u7ae0 \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\u3002</p> <ul> <li>\u5206\u6cbb\u7b97\u6cd5\u9012\u5f52\u5730\u5c06\u539f\u95ee\u9898\u5212\u5206\u4e3a\u591a\u4e2a\u76f8\u4e92\u72ec\u7acb\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u81f3\u6700\u5c0f\u5b50\u95ee\u9898\uff0c\u5e76\u5728\u56de\u6eaf\u4e2d\u5408\u5e76\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u6700\u7ec8\u5f97\u5230\u539f\u95ee\u9898\u7684\u89e3\u3002</li> <li>\u52a8\u6001\u89c4\u5212\u4e5f\u5bf9\u95ee\u9898\u8fdb\u884c\u9012\u5f52\u5206\u89e3\uff0c\u4f46\u4e0e\u5206\u6cbb\u7b97\u6cd5\u7684\u4e3b\u8981\u533a\u522b\u662f\uff0c\u52a8\u6001\u89c4\u5212\u4e2d\u7684\u5b50\u95ee\u9898\u662f\u76f8\u4e92\u4f9d\u8d56\u7684\uff0c\u5728\u5206\u89e3\u8fc7\u7a0b\u4e2d\u4f1a\u51fa\u73b0\u8bb8\u591a\u91cd\u53e0\u5b50\u95ee\u9898\u3002</li> <li>\u56de\u6eaf\u7b97\u6cd5\u5728\u5c1d\u8bd5\u548c\u56de\u9000\u4e2d\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u7684\u89e3\uff0c\u5e76\u901a\u8fc7\u526a\u679d\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u641c\u7d22\u5206\u652f\u3002\u539f\u95ee\u9898\u7684\u89e3\u7531\u4e00\u7cfb\u5217\u51b3\u7b56\u6b65\u9aa4\u6784\u6210\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u51b3\u7b56\u6b65\u9aa4\u4e4b\u524d\u7684\u5b50\u5e8f\u5217\u770b\u4f5c\u4e3a\u4e00\u4e2a\u5b50\u95ee\u9898\u3002</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\u56fe 14-6 \u6240\u793a\uff0c\u82e5\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(3\\) \u9636\u7684\u4ee3\u4ef7\u5206\u522b\u4e3a \\(1\\)\u3001\\(10\\)\u3001\\(1\\) \uff0c\u5219\u4ece\u5730\u9762\u722c\u5230\u7b2c \\(3\\) \u9636\u7684\u6700\u5c0f\u4ee3\u4ef7\u4e3a \\(2\\) \u3002</p> <p></p> <p> \u56fe 14-6 \u00a0 \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\uff1a</p> \\[ dp[i] = \\min(dp[i-1], dp[i-2]) + cost[i] \\] <p>\u8fd9\u4fbf\u53ef\u4ee5\u5f15\u51fa\u6700\u4f18\u5b50\u7ed3\u6784\u7684\u542b\u4e49\uff1a\u539f\u95ee\u9898\u7684\u6700\u4f18\u89e3\u662f\u4ece\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u6784\u5efa\u5f97\u6765\u7684\u3002</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]\\) \u548c \\(dp[i-2]\\) \u4e2d\u6311\u9009\u51fa\u8f83\u4f18\u7684\u90a3\u4e00\u4e2a\uff0c\u5e76\u7528\u5b83\u6784\u5efa\u51fa\u539f\u95ee\u9898 \\(dp[i]\\) \u7684\u6700\u4f18\u89e3\u3002</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]\\) \u548c \\(dp[2] = cost[2]\\) \uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230\u52a8\u6001\u89c4\u5212\u4ee3\u7801\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig min_cost_climbing_stairs_dp.py<pre><code>[class]{}-[func]{min_cost_climbing_stairs_dp}\n</code></pre> min_cost_climbing_stairs_dp.cpp<pre><code>[class]{}-[func]{minCostClimbingStairsDP}\n</code></pre> min_cost_climbing_stairs_dp.java<pre><code>[class]{min_cost_climbing_stairs_dp}-[func]{minCostClimbingStairsDP}\n</code></pre> min_cost_climbing_stairs_dp.cs<pre><code>[class]{min_cost_climbing_stairs_dp}-[func]{minCostClimbingStairsDP}\n</code></pre> min_cost_climbing_stairs_dp.go<pre><code>[class]{}-[func]{minCostClimbingStairsDP}\n</code></pre> min_cost_climbing_stairs_dp.swift<pre><code>[class]{}-[func]{minCostClimbingStairsDP}\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.dart<pre><code>[class]{}-[func]{minCostClimbingStairsDP}\n</code></pre> min_cost_climbing_stairs_dp.rs<pre><code>[class]{}-[func]{min_cost_climbing_stairs_dp}\n</code></pre> min_cost_climbing_stairs_dp.c<pre><code>[class]{}-[func]{minCostClimbingStairsDP}\n</code></pre> min_cost_climbing_stairs_dp.zig<pre><code>[class]{}-[func]{minCostClimbingStairsDP}\n</code></pre> <p>\u56fe 14-7 \u5c55\u793a\u4e86\u4ee5\u4e0a\u4ee3\u7801\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\u3002</p> <p></p> <p> \u56fe 14-7 \u00a0 \u722c\u697c\u68af\u6700\u5c0f\u4ee3\u4ef7\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b </p> <p>\u672c\u9898\u4e5f\u53ef\u4ee5\u8fdb\u884c\u7a7a\u95f4\u4f18\u5316\uff0c\u5c06\u4e00\u7ef4\u538b\u7f29\u81f3\u96f6\u7ef4\uff0c\u4f7f\u5f97\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece \\(O(n)\\) \u964d\u4f4e\u81f3 \\(O(1)\\) \u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig min_cost_climbing_stairs_dp.py<pre><code>[class]{}-[func]{min_cost_climbing_stairs_dp_comp}\n</code></pre> min_cost_climbing_stairs_dp.cpp<pre><code>[class]{}-[func]{minCostClimbingStairsDPComp}\n</code></pre> min_cost_climbing_stairs_dp.java<pre><code>[class]{min_cost_climbing_stairs_dp}-[func]{minCostClimbingStairsDPComp}\n</code></pre> min_cost_climbing_stairs_dp.cs<pre><code>[class]{min_cost_climbing_stairs_dp}-[func]{minCostClimbingStairsDPComp}\n</code></pre> min_cost_climbing_stairs_dp.go<pre><code>[class]{}-[func]{minCostClimbingStairsDPComp}\n</code></pre> min_cost_climbing_stairs_dp.swift<pre><code>[class]{}-[func]{minCostClimbingStairsDPComp}\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.dart<pre><code>[class]{}-[func]{minCostClimbingStairsDPComp}\n</code></pre> min_cost_climbing_stairs_dp.rs<pre><code>[class]{}-[func]{min_cost_climbing_stairs_dp_comp}\n</code></pre> min_cost_climbing_stairs_dp.c<pre><code>[class]{}-[func]{minCostClimbingStairsDPComp}\n</code></pre> min_cost_climbing_stairs_dp.zig<pre><code>[class]{}-[func]{minCostClimbingStairsDPComp}\n</code></pre>"},{"location":"chapter_dynamic_programming/dp_problem_features/#1422","title":"14.2.2 \u00a0 \u65e0\u540e\u6548\u6027","text":"<p>\u65e0\u540e\u6548\u6027\u662f\u52a8\u6001\u89c4\u5212\u80fd\u591f\u6709\u6548\u89e3\u51b3\u95ee\u9898\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c\u5b9a\u4e49\u4e3a\uff1a\u7ed9\u5b9a\u4e00\u4e2a\u786e\u5b9a\u7684\u72b6\u6001\uff0c\u5b83\u7684\u672a\u6765\u53d1\u5c55\u53ea\u4e0e\u5f53\u524d\u72b6\u6001\u6709\u5173\uff0c\u800c\u4e0e\u5f53\u524d\u72b6\u6001\u8fc7\u53bb\u6240\u7ecf\u5386\u8fc7\u7684\u6240\u6709\u72b6\u6001\u65e0\u5173\u3002</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\u987b\u8003\u8651\u72b6\u6001 \\(i\\) \u4e4b\u524d\u7684\u72b6\u6001\uff0c\u5b83\u4eec\u5bf9\u72b6\u6001 \\(i\\) \u7684\u672a\u6765\u6ca1\u6709\u5f71\u54cd\u3002</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\u56fe 14-8 \uff0c\u722c\u4e0a\u7b2c \\(3\\) \u9636\u4ec5\u5269 \\(2\\) \u79cd\u53ef\u884c\u65b9\u6848\uff0c\u5176\u4e2d\u8fde\u7eed\u4e09\u6b21\u8df3 \\(1\\) \u9636\u7684\u65b9\u6848\u4e0d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\uff0c\u56e0\u6b64\u88ab\u820d\u5f03\u3002</p> <p></p> <p> \u56fe 14-8 \u00a0 \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\u5224\u65ad\u5f53\u524d\u72b6\u6001\u662f\u4ece\u4f55\u800c\u6765\u7684\u3002</p> <ul> <li>\u5f53\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(1\\) \u9636\u65f6\uff0c\u4e0a\u4e0a\u4e00\u8f6e\u53ea\u80fd\u9009\u62e9\u8df3 \\(2\\) \u9636\uff0c\u5373 \\(dp[i, 1]\\) \u53ea\u80fd\u4ece \\(dp[i-1, 2]\\) \u8f6c\u79fb\u8fc7\u6765\u3002</li> <li>\u5f53\u4e0a\u4e00\u8f6e\u8df3\u4e86 \\(2\\) \u9636\u65f6\uff0c\u4e0a\u4e0a\u4e00\u8f6e\u53ef\u9009\u62e9\u8df3 \\(1\\) \u9636\u6216\u8df3 \\(2\\) \u9636\uff0c\u5373 \\(dp[i, 2]\\) \u53ef\u4ee5\u4ece \\(dp[i-2, 1]\\) \u6216 \\(dp[i-2, 2]\\) \u8f6c\u79fb\u8fc7\u6765\u3002</li> </ul> <p>\u5982\u56fe 14-9 \u6240\u793a\uff0c\u5728\u8be5\u5b9a\u4e49\u4e0b\uff0c\\(dp[i, j]\\) \u8868\u793a\u72b6\u6001 \\([i, j]\\) \u5bf9\u5e94\u7684\u65b9\u6848\u6570\u3002\u6b64\u65f6\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a</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 14-9 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_constraint_dp.py<pre><code>[class]{}-[func]{climbing_stairs_constraint_dp}\n</code></pre> climbing_stairs_constraint_dp.cpp<pre><code>[class]{}-[func]{climbingStairsConstraintDP}\n</code></pre> climbing_stairs_constraint_dp.java<pre><code>[class]{climbing_stairs_constraint_dp}-[func]{climbingStairsConstraintDP}\n</code></pre> climbing_stairs_constraint_dp.cs<pre><code>[class]{climbing_stairs_constraint_dp}-[func]{climbingStairsConstraintDP}\n</code></pre> climbing_stairs_constraint_dp.go<pre><code>[class]{}-[func]{climbingStairsConstraintDP}\n</code></pre> climbing_stairs_constraint_dp.swift<pre><code>[class]{}-[func]{climbingStairsConstraintDP}\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.dart<pre><code>[class]{}-[func]{climbingStairsConstraintDP}\n</code></pre> climbing_stairs_constraint_dp.rs<pre><code>[class]{}-[func]{climbing_stairs_constraint_dp}\n</code></pre> climbing_stairs_constraint_dp.c<pre><code>[class]{}-[func]{climbingStairsConstraintDP}\n</code></pre> climbing_stairs_constraint_dp.zig<pre><code>[class]{}-[func]{climbingStairsConstraintDP}\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\u91cd\u65b0\u6ee1\u8db3\u65e0\u540e\u6548\u6027\u3002\u7136\u800c\uff0c\u67d0\u4e9b\u95ee\u9898\u5177\u6709\u975e\u5e38\u4e25\u91cd\u7684\u201c\u6709\u540e\u6548\u6027\u201d\u3002</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\\)\u3001\\(3\\) \u9636\u4e0a\uff0c\u5219\u4e4b\u540e\u5c31\u4e0d\u80fd\u8df3\u5230\u7b2c \\(4\\)\u3001\\(6\\) \u9636\u4e0a\u3002\u8bf7\u95ee\u6709\u591a\u5c11\u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002</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\u3002</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\u52a8\u6001\u89c4\u5212\u95ee\u9898\u8fd8\u6709\u4e00\u4e9b\u5224\u65ad\u7684\u201c\u52a0\u5206\u9879\u201d\u3002</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>\u76f8\u5e94\u5730\uff0c\u4e5f\u5b58\u5728\u4e00\u4e9b\u201c\u51cf\u5206\u9879\u201d\u3002</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\u201c\u6700\u5c0f\u8def\u5f84\u548c\u201d\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>\u56fe 14-10 \u5c55\u793a\u4e86\u4e00\u4e2a\u4f8b\u5b50\uff0c\u7ed9\u5b9a\u7f51\u683c\u7684\u6700\u5c0f\u8def\u5f84\u548c\u4e3a \\(13\\) \u3002</p> <p></p> <p> \u56fe 14-10 \u00a0 \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\u56fe 14-11 \u6240\u793a\u7684\u4e8c\u7ef4 \\(dp\\) \u77e9\u9635\uff0c\u5176\u5c3a\u5bf8\u4e0e\u8f93\u5165\u7f51\u683c \\(grid\\) \u76f8\u540c\u3002</p> <p></p> <p> \u56fe 14-11 \u00a0 \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\u56fe 14-12 \u6240\u793a\u7684\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 14-12 \u00a0 \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\u9996\u884c\u7684\u72b6\u6001\u53ea\u80fd\u4ece\u5176\u5de6\u8fb9\u7684\u72b6\u6001\u5f97\u6765\uff0c\u9996\u5217\u7684\u72b6\u6001\u53ea\u80fd\u4ece\u5176\u4e0a\u8fb9\u7684\u72b6\u6001\u5f97\u6765\uff0c\u56e0\u6b64\u9996\u884c \\(i = 0\\) \u548c\u9996\u5217 \\(j = 0\\) \u662f\u8fb9\u754c\u6761\u4ef6\u3002</p> <p>\u5982\u56fe 14-13 \u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u683c\u5b50\u662f\u7531\u5176\u5de6\u65b9\u683c\u5b50\u548c\u4e0a\u65b9\u683c\u5b50\u8f6c\u79fb\u800c\u6765\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528\u91c7\u7528\u5faa\u73af\u6765\u904d\u5386\u77e9\u9635\uff0c\u5916\u5faa\u73af\u904d\u5386\u5404\u884c\u3001\u5185\u5faa\u73af\u904d\u5386\u5404\u5217\u3002</p> <p></p> <p> \u56fe 14-13 \u00a0 \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":"1. \u00a0 \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\u9012\u5f52\u51fd\u6570\u5305\u62ec\u4ee5\u4e0b\u8981\u7d20\u3002</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 < 0\\) \u65f6\u6216 \\(j < 0\\) \u65f6\u7d22\u5f15\u8d8a\u754c\uff0c\u6b64\u65f6\u8fd4\u56de\u4ee3\u4ef7 \\(+\\infty\\) \uff0c\u4ee3\u8868\u4e0d\u53ef\u884c\u3002</li> </ul> PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.py<pre><code>[class]{}-[func]{min_path_sum_dfs}\n</code></pre> min_path_sum.cpp<pre><code>[class]{}-[func]{minPathSumDFS}\n</code></pre> min_path_sum.java<pre><code>[class]{min_path_sum}-[func]{minPathSumDFS}\n</code></pre> min_path_sum.cs<pre><code>[class]{min_path_sum}-[func]{minPathSumDFS}\n</code></pre> min_path_sum.go<pre><code>[class]{}-[func]{minPathSumDFS}\n</code></pre> min_path_sum.swift<pre><code>[class]{}-[func]{minPathSumDFS}\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.dart<pre><code>[class]{}-[func]{minPathSumDFS}\n</code></pre> min_path_sum.rs<pre><code>[class]{}-[func]{min_path_sum_dfs}\n</code></pre> min_path_sum.c<pre><code>[class]{}-[func]{minPathSumDFS}\n</code></pre> min_path_sum.zig<pre><code>[class]{}-[func]{minPathSumDFS}\n</code></pre> <p>\u56fe 14-14 \u7ed9\u51fa\u4e86\u4ee5 \\(dp[2, 1]\\) \u4e3a\u6839\u8282\u70b9\u7684\u9012\u5f52\u6811\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e9b\u91cd\u53e0\u5b50\u95ee\u9898\uff0c\u5176\u6570\u91cf\u4f1a\u968f\u7740\u7f51\u683c <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 14-14 \u00a0 \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":"2. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.py<pre><code>[class]{}-[func]{min_path_sum_dfs_mem}\n</code></pre> min_path_sum.cpp<pre><code>[class]{}-[func]{minPathSumDFSMem}\n</code></pre> min_path_sum.java<pre><code>[class]{min_path_sum}-[func]{minPathSumDFSMem}\n</code></pre> min_path_sum.cs<pre><code>[class]{min_path_sum}-[func]{minPathSumDFSMem}\n</code></pre> min_path_sum.go<pre><code>[class]{}-[func]{minPathSumDFSMem}\n</code></pre> min_path_sum.swift<pre><code>[class]{}-[func]{minPathSumDFSMem}\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.dart<pre><code>[class]{}-[func]{minPathSumDFSMem}\n</code></pre> min_path_sum.rs<pre><code>[class]{}-[func]{min_path_sum_dfs_mem}\n</code></pre> min_path_sum.c<pre><code>[class]{}-[func]{minPathSumDFSMem}\n</code></pre> min_path_sum.zig<pre><code>[class]{}-[func]{minPathSumDFSMem}\n</code></pre> <p>\u5982\u56fe 14-15 \u6240\u793a\uff0c\u5728\u5f15\u5165\u8bb0\u5fc6\u5316\u540e\uff0c\u6240\u6709\u5b50\u95ee\u9898\u7684\u89e3\u53ea\u9700\u8ba1\u7b97\u4e00\u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u53d6\u51b3\u4e8e\u72b6\u6001\u603b\u6570\uff0c\u5373\u7f51\u683c\u5c3a\u5bf8 \\(O(nm)\\) \u3002</p> <p></p> <p> \u56fe 14-15 \u00a0 \u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811 </p>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#3","title":"3. \u00a0 \u65b9\u6cd5\u4e09\uff1a\u52a8\u6001\u89c4\u5212","text":"<p>\u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\u52a8\u6001\u89c4\u5212\u89e3\u6cd5\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.py<pre><code>[class]{}-[func]{min_path_sum_dp}\n</code></pre> min_path_sum.cpp<pre><code>[class]{}-[func]{minPathSumDP}\n</code></pre> min_path_sum.java<pre><code>[class]{min_path_sum}-[func]{minPathSumDP}\n</code></pre> min_path_sum.cs<pre><code>[class]{min_path_sum}-[func]{minPathSumDP}\n</code></pre> min_path_sum.go<pre><code>[class]{}-[func]{minPathSumDP}\n</code></pre> min_path_sum.swift<pre><code>[class]{}-[func]{minPathSumDP}\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.dart<pre><code>[class]{}-[func]{minPathSumDP}\n</code></pre> min_path_sum.rs<pre><code>[class]{}-[func]{min_path_sum_dp}\n</code></pre> min_path_sum.c<pre><code>[class]{}-[func]{minPathSumDP}\n</code></pre> min_path_sum.zig<pre><code>[class]{}-[func]{minPathSumDP}\n</code></pre> <p>\u56fe 14-16 \u5c55\u793a\u4e86\u6700\u5c0f\u8def\u5f84\u548c\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\uff0c\u5176\u904d\u5386\u4e86\u6574\u4e2a\u7f51\u683c\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nm)\\) \u3002</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> <1><2><3><4><5><6><7><8><9><10><11><12> <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 14-16 \u00a0 \u6700\u5c0f\u8def\u5f84\u548c\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b </p>"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#4","title":"4. \u00a0 \u7a7a\u95f4\u4f18\u5316","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> PythonC++JavaC#GoSwiftJSTSDartRustCZig min_path_sum.py<pre><code>[class]{}-[func]{min_path_sum_dp_comp}\n</code></pre> min_path_sum.cpp<pre><code>[class]{}-[func]{minPathSumDPComp}\n</code></pre> min_path_sum.java<pre><code>[class]{min_path_sum}-[func]{minPathSumDPComp}\n</code></pre> min_path_sum.cs<pre><code>[class]{min_path_sum}-[func]{minPathSumDPComp}\n</code></pre> min_path_sum.go<pre><code>[class]{}-[func]{minPathSumDPComp}\n</code></pre> min_path_sum.swift<pre><code>[class]{}-[func]{minPathSumDPComp}\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.dart<pre><code>[class]{}-[func]{minPathSumDPComp}\n</code></pre> min_path_sum.rs<pre><code>[class]{}-[func]{min_path_sum_dp_comp}\n</code></pre> min_path_sum.c<pre><code>[class]{}-[func]{minPathSumDPComp}\n</code></pre> min_path_sum.zig<pre><code>[class]{}-[func]{minPathSumDPComp}\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\u56fe 14-27 \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 14-27 \u00a0 \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\u56fe 14-28 \u6240\u793a\uff0c\u5728\u4e0d\u9650\u5236\u64cd\u4f5c\u7684\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ef\u4ee5\u6d3e\u751f\u51fa\u8bb8\u591a\u6761\u8fb9\uff0c\u6bcf\u6761\u8fb9\u5bf9\u5e94\u4e00\u79cd\u64cd\u4f5c\uff0c\u8fd9\u610f\u5473\u7740\u4ece <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 14-28 \u00a0 \u57fa\u4e8e\u51b3\u7b56\u6811\u6a21\u578b\u8868\u793a\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898 </p>"},{"location":"chapter_dynamic_programming/edit_distance_problem/#1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"<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]\\) \u3002</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\\) \u548c \\(t\\) \u4e2d\u8003\u8651\u7684\u7b2c \\(i\\) \u548c \\(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\u56fe 14-29 \u6240\u793a\u7684\u4e09\u79cd\u60c5\u51b5\u3002</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 14-29 \u00a0 \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]\\)\u3001\\(dp[i-1, j]\\)\u3001\\(dp[i-1, j-1]\\) \u4e09\u8005\u4e2d\u7684\u6700\u5c11\u7f16\u8f91\u6b65\u6570\uff0c\u518d\u52a0\u4e0a\u672c\u6b21\u7684\u7f16\u8f91\u6b65\u6570 \\(1\\) \u3002\u5bf9\u5e94\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a\uff1a</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\u987b\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/#2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCZig edit_distance.py<pre><code>[class]{}-[func]{edit_distance_dp}\n</code></pre> edit_distance.cpp<pre><code>[class]{}-[func]{editDistanceDP}\n</code></pre> edit_distance.java<pre><code>[class]{edit_distance}-[func]{editDistanceDP}\n</code></pre> edit_distance.cs<pre><code>[class]{edit_distance}-[func]{editDistanceDP}\n</code></pre> edit_distance.go<pre><code>[class]{}-[func]{editDistanceDP}\n</code></pre> edit_distance.swift<pre><code>[class]{}-[func]{editDistanceDP}\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.dart<pre><code>[class]{}-[func]{editDistanceDP}\n</code></pre> edit_distance.rs<pre><code>[class]{}-[func]{edit_distance_dp}\n</code></pre> edit_distance.c<pre><code>[class]{}-[func]{editDistanceDP}\n</code></pre> edit_distance.zig<pre><code>[class]{}-[func]{editDistanceDP}\n</code></pre> <p>\u5982\u56fe 14-30 \u6240\u793a\uff0c\u7f16\u8f91\u8ddd\u79bb\u95ee\u9898\u7684\u72b6\u6001\u8f6c\u79fb\u8fc7\u7a0b\u4e0e\u80cc\u5305\u95ee\u9898\u975e\u5e38\u7c7b\u4f3c\uff0c\u90fd\u53ef\u4ee5\u770b\u4f5c\u662f\u586b\u5199\u4e00\u4e2a\u4e8c\u7ef4\u7f51\u683c\u7684\u8fc7\u7a0b\u3002</p> <1><2><3><4><5><6><7><8><9><10><11><12><13><14><15> <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 14-30 \u00a0 \u7f16\u8f91\u8ddd\u79bb\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b </p>"},{"location":"chapter_dynamic_programming/edit_distance_problem/#3","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","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> PythonC++JavaC#GoSwiftJSTSDartRustCZig edit_distance.py<pre><code>[class]{}-[func]{edit_distance_dp_comp}\n</code></pre> edit_distance.cpp<pre><code>[class]{}-[func]{editDistanceDPComp}\n</code></pre> edit_distance.java<pre><code>[class]{edit_distance}-[func]{editDistanceDPComp}\n</code></pre> edit_distance.cs<pre><code>[class]{edit_distance}-[func]{editDistanceDPComp}\n</code></pre> edit_distance.go<pre><code>[class]{}-[func]{editDistanceDPComp}\n</code></pre> edit_distance.swift<pre><code>[class]{}-[func]{editDistanceDPComp}\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.dart<pre><code>[class]{}-[func]{editDistanceDPComp}\n</code></pre> edit_distance.rs<pre><code>[class]{}-[func]{edit_distance_dp_comp}\n</code></pre> edit_distance.c<pre><code>[class]{}-[func]{editDistanceDPComp}\n</code></pre> edit_distance.zig<pre><code>[class]{}-[func]{editDistanceDPComp}\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\u56fe 14-1 \u6240\u793a\uff0c\u5bf9\u4e8e\u4e00\u4e2a \\(3\\) \u9636\u697c\u68af\uff0c\u5171\u6709 \\(3\\) \u79cd\u65b9\u6848\u53ef\u4ee5\u722c\u5230\u697c\u9876\u3002</p> <p></p> <p> \u56fe 14-1 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_backtrack.py<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{climbing_stairs_backtrack}\n</code></pre> climbing_stairs_backtrack.cpp<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{climbingStairsBacktrack}\n</code></pre> climbing_stairs_backtrack.java<pre><code>[class]{climbing_stairs_backtrack}-[func]{backtrack}\n[class]{climbing_stairs_backtrack}-[func]{climbingStairsBacktrack}\n</code></pre> climbing_stairs_backtrack.cs<pre><code>[class]{climbing_stairs_backtrack}-[func]{backtrack}\n[class]{climbing_stairs_backtrack}-[func]{climbingStairsBacktrack}\n</code></pre> climbing_stairs_backtrack.go<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{climbingStairsBacktrack}\n</code></pre> climbing_stairs_backtrack.swift<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{climbingStairsBacktrack}\n</code></pre> climbing_stairs_backtrack.js<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{climbingStairsBacktrack}\n</code></pre> climbing_stairs_backtrack.ts<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{climbingStairsBacktrack}\n</code></pre> climbing_stairs_backtrack.dart<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{climbingStairsBacktrack}\n</code></pre> climbing_stairs_backtrack.rs<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{climbing_stairs_backtrack}\n</code></pre> climbing_stairs_backtrack.c<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{climbingStairsBacktrack}\n</code></pre> climbing_stairs_backtrack.zig<pre><code>[class]{}-[func]{backtrack}\n[class]{}-[func]{climbingStairsBacktrack}\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], \\dots, 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\u56fe 14-2 \u5c55\u793a\u4e86\u8be5\u9012\u63a8\u5173\u7cfb\u3002</p> <p></p> <p> \u56fe 14-2 \u00a0 \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\u3002\u4ee5 \\(dp[n]\\) \u4e3a\u8d77\u59cb\u70b9\uff0c\u9012\u5f52\u5730\u5c06\u4e00\u4e2a\u8f83\u5927\u95ee\u9898\u62c6\u89e3\u4e3a\u4e24\u4e2a\u8f83\u5c0f\u95ee\u9898\u7684\u548c\uff0c\u76f4\u81f3\u5230\u8fbe\u6700\u5c0f\u5b50\u95ee\u9898 \\(dp[1]\\) \u548c \\(dp[2]\\) \u65f6\u8fd4\u56de\u3002\u5176\u4e2d\uff0c\u6700\u5c0f\u5b50\u95ee\u9898\u7684\u89e3\u662f\u5df2\u77e5\u7684\uff0c\u5373 \\(dp[1] = 1\\)\u3001\\(dp[2] = 2\\) \uff0c\u8868\u793a\u722c\u5230\u7b2c \\(1\\)\u3001\\(2\\) \u9636\u5206\u522b\u6709 \\(1\\)\u3001\\(2\\) \u79cd\u65b9\u6848\u3002</p> <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> PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dfs.py<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbing_stairs_dfs}\n</code></pre> climbing_stairs_dfs.cpp<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFS}\n</code></pre> climbing_stairs_dfs.java<pre><code>[class]{climbing_stairs_dfs}-[func]{dfs}\n[class]{climbing_stairs_dfs}-[func]{climbingStairsDFS}\n</code></pre> climbing_stairs_dfs.cs<pre><code>[class]{climbing_stairs_dfs}-[func]{dfs}\n[class]{climbing_stairs_dfs}-[func]{climbingStairsDFS}\n</code></pre> climbing_stairs_dfs.go<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFS}\n</code></pre> climbing_stairs_dfs.swift<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFS}\n</code></pre> climbing_stairs_dfs.js<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFS}\n</code></pre> climbing_stairs_dfs.ts<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFS}\n</code></pre> climbing_stairs_dfs.dart<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFS}\n</code></pre> climbing_stairs_dfs.rs<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbing_stairs_dfs}\n</code></pre> climbing_stairs_dfs.c<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFS}\n</code></pre> climbing_stairs_dfs.zig<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFS}\n</code></pre> <p>\u56fe 14-3 \u5c55\u793a\u4e86\u66b4\u529b\u641c\u7d22\u5f62\u6210\u7684\u9012\u5f52\u6811\u3002\u5bf9\u4e8e\u95ee\u9898 \\(dp[n]\\) \uff0c\u5176\u9012\u5f52\u6811\u7684\u6df1\u5ea6\u4e3a \\(n\\) \uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002\u6307\u6570\u9636\u5c5e\u4e8e\u7206\u70b8\u5f0f\u589e\u957f\uff0c\u5982\u679c\u6211\u4eec\u8f93\u5165\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684 \\(n\\) \uff0c\u5219\u4f1a\u9677\u5165\u6f2b\u957f\u7684\u7b49\u5f85\u4e4b\u4e2d\u3002</p> <p></p> <p> \u56fe 14-3 \u00a0 \u722c\u697c\u68af\u5bf9\u5e94\u9012\u5f52\u6811 </p> <p>\u89c2\u5bdf\u56fe 14-3 \uff0c\u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u7531\u4e8e\u201c\u91cd\u53e0\u5b50\u95ee\u9898\u201d\u5bfc\u81f4\u7684\u3002\u4f8b\u5982 \\(dp[9]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[8]\\) \u548c \\(dp[7]\\) \uff0c\\(dp[8]\\) \u88ab\u5206\u89e3\u4e3a \\(dp[7]\\) \u548c \\(dp[6]\\) \uff0c\u4e24\u8005\u90fd\u5305\u542b\u5b50\u95ee\u9898 \\(dp[7]\\) \u3002</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\u5c06\u91cd\u53e0\u5b50\u95ee\u9898\u526a\u679d\u3002</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\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u8be5\u5b50\u95ee\u9898\u3002</li> </ol> PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dfs_mem.py<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbing_stairs_dfs_mem}\n</code></pre> climbing_stairs_dfs_mem.cpp<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFSMem}\n</code></pre> climbing_stairs_dfs_mem.java<pre><code>[class]{climbing_stairs_dfs_mem}-[func]{dfs}\n[class]{climbing_stairs_dfs_mem}-[func]{climbingStairsDFSMem}\n</code></pre> climbing_stairs_dfs_mem.cs<pre><code>[class]{climbing_stairs_dfs_mem}-[func]{dfs}\n[class]{climbing_stairs_dfs_mem}-[func]{climbingStairsDFSMem}\n</code></pre> climbing_stairs_dfs_mem.go<pre><code>[class]{}-[func]{dfsMem}\n[class]{}-[func]{climbingStairsDFSMem}\n</code></pre> climbing_stairs_dfs_mem.swift<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFSMem}\n</code></pre> climbing_stairs_dfs_mem.js<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFSMem}\n</code></pre> climbing_stairs_dfs_mem.ts<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFSMem}\n</code></pre> climbing_stairs_dfs_mem.dart<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFSMem}\n</code></pre> climbing_stairs_dfs_mem.rs<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbing_stairs_dfs_mem}\n</code></pre> climbing_stairs_dfs_mem.c<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFSMem}\n</code></pre> climbing_stairs_dfs_mem.zig<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{climbingStairsDFSMem}\n</code></pre> <p>\u89c2\u5bdf\u56fe 14-4 \uff0c\u7ecf\u8fc7\u8bb0\u5fc6\u5316\u5904\u7406\u540e\uff0c\u6240\u6709\u91cd\u53e0\u5b50\u95ee\u9898\u90fd\u53ea\u9700\u88ab\u8ba1\u7b97\u4e00\u6b21\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u88ab\u4f18\u5316\u81f3 \\(O(n)\\) \uff0c\u8fd9\u662f\u4e00\u4e2a\u5de8\u5927\u7684\u98de\u8dc3\u3002</p> <p></p> <p> \u56fe 14-4 \u00a0 \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\u987b\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> PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dp.py<pre><code>[class]{}-[func]{climbing_stairs_dp}\n</code></pre> climbing_stairs_dp.cpp<pre><code>[class]{}-[func]{climbingStairsDP}\n</code></pre> climbing_stairs_dp.java<pre><code>[class]{climbing_stairs_dp}-[func]{climbingStairsDP}\n</code></pre> climbing_stairs_dp.cs<pre><code>[class]{climbing_stairs_dp}-[func]{climbingStairsDP}\n</code></pre> climbing_stairs_dp.go<pre><code>[class]{}-[func]{climbingStairsDP}\n</code></pre> climbing_stairs_dp.swift<pre><code>[class]{}-[func]{climbingStairsDP}\n</code></pre> climbing_stairs_dp.js<pre><code>[class]{}-[func]{climbingStairsDP}\n</code></pre> climbing_stairs_dp.ts<pre><code>[class]{}-[func]{climbingStairsDP}\n</code></pre> climbing_stairs_dp.dart<pre><code>[class]{}-[func]{climbingStairsDP}\n</code></pre> climbing_stairs_dp.rs<pre><code>[class]{}-[func]{climbing_stairs_dp}\n</code></pre> climbing_stairs_dp.c<pre><code>[class]{}-[func]{climbingStairsDP}\n</code></pre> climbing_stairs_dp.zig<pre><code>[class]{}-[func]{climbingStairsDP}\n</code></pre> <p>\u56fe 14-5 \u6a21\u62df\u4e86\u4ee5\u4e0a\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\u3002</p> <p></p> <p> \u56fe 14-5 \u00a0 \u722c\u697c\u68af\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b </p> <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>\u6839\u636e\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u51fa\u52a8\u6001\u89c4\u5212\u7684\u5e38\u7528\u672f\u8bed\u3002</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\\) \u548c \\(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>"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1414","title":"14.1.4 \u00a0 \u7a7a\u95f4\u4f18\u5316","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\u987b\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> PythonC++JavaC#GoSwiftJSTSDartRustCZig climbing_stairs_dp.py<pre><code>[class]{}-[func]{climbing_stairs_dp_comp}\n</code></pre> climbing_stairs_dp.cpp<pre><code>[class]{}-[func]{climbingStairsDPComp}\n</code></pre> climbing_stairs_dp.java<pre><code>[class]{climbing_stairs_dp}-[func]{climbingStairsDPComp}\n</code></pre> climbing_stairs_dp.cs<pre><code>[class]{climbing_stairs_dp}-[func]{climbingStairsDPComp}\n</code></pre> climbing_stairs_dp.go<pre><code>[class]{}-[func]{climbingStairsDPComp}\n</code></pre> climbing_stairs_dp.swift<pre><code>[class]{}-[func]{climbingStairsDPComp}\n</code></pre> climbing_stairs_dp.js<pre><code>[class]{}-[func]{climbingStairsDPComp}\n</code></pre> climbing_stairs_dp.ts<pre><code>[class]{}-[func]{climbingStairsDPComp}\n</code></pre> climbing_stairs_dp.dart<pre><code>[class]{}-[func]{climbingStairsDPComp}\n</code></pre> climbing_stairs_dp.rs<pre><code>[class]{}-[func]{climbing_stairs_dp_comp}\n</code></pre> climbing_stairs_dp.c<pre><code>[class]{}-[func]{climbingStairsDPComp}\n</code></pre> climbing_stairs_dp.zig<pre><code>[class]{}-[func]{climbingStairsDPComp}\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>\u5728\u52a8\u6001\u89c4\u5212\u95ee\u9898\u4e2d\uff0c\u5f53\u524d\u72b6\u6001\u5f80\u5f80\u4ec5\u4e0e\u524d\u9762\u6709\u9650\u4e2a\u72b6\u6001\u6709\u5173\uff0c\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u53ea\u4fdd\u7559\u5fc5\u8981\u7684\u72b6\u6001\uff0c\u901a\u8fc7\u201c\u964d\u7ef4\u201d\u6765\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u3002\u8fd9\u79cd\u7a7a\u95f4\u4f18\u5316\u6280\u5de7\u88ab\u79f0\u4e3a\u201c\u6eda\u52a8\u53d8\u91cf\u201d\u6216\u201c\u6eda\u52a8\u6570\u7ec4\u201d\u3002</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>\u89c2\u5bdf\u56fe 14-17 \uff0c\u7531\u4e8e\u7269\u54c1\u7f16\u53f7 \\(i\\) \u4ece \\(1\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u6570\u7ec4\u7d22\u5f15\u4ece \\(0\\) \u5f00\u59cb\u8ba1\u6570\uff0c\u56e0\u6b64\u7269\u54c1 \\(i\\) \u5bf9\u5e94\u91cd\u91cf \\(wgt[i-1]\\) \u548c\u4ef7\u503c \\(val[i-1]\\) \u3002</p> <p></p> <p> \u56fe 14-17 \u00a0 0-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\uff0c\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\u3002</p> <ul> <li>\u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \uff1a\u80cc\u5305\u5bb9\u91cf\u4e0d\u53d8\uff0c\u72b6\u6001\u53d8\u5316\u4e3a \\([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\u53d8\u5316\u4e3a \\([i-1, c-wgt[i-1]]\\) \u3002</li> </ul> <p>\u4e0a\u8ff0\u5206\u6790\u5411\u6211\u4eec\u63ed\u793a\u4e86\u672c\u9898\u7684\u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u6700\u5927\u4ef7\u503c \\(dp[i, c]\\) \u7b49\u4e8e\u4e0d\u653e\u5165\u7269\u54c1 \\(i\\) \u548c\u653e\u5165\u7269\u54c1 \\(i\\) \u4e24\u79cd\u65b9\u6848\u4e2d\u7684\u4ef7\u503c\u66f4\u5927\u7684\u90a3\u4e00\u4e2a\u3002\u7531\u6b64\u53ef\u63a8\u51fa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a</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":"1. \u00a0 \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u641c\u7d22","text":"<p>\u641c\u7d22\u4ee3\u7801\u5305\u542b\u4ee5\u4e0b\u8981\u7d20\u3002</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> PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.py<pre><code>[class]{}-[func]{knapsack_dfs}\n</code></pre> knapsack.cpp<pre><code>[class]{}-[func]{knapsackDFS}\n</code></pre> knapsack.java<pre><code>[class]{knapsack}-[func]{knapsackDFS}\n</code></pre> knapsack.cs<pre><code>[class]{knapsack}-[func]{knapsackDFS}\n</code></pre> knapsack.go<pre><code>[class]{}-[func]{knapsackDFS}\n</code></pre> knapsack.swift<pre><code>[class]{}-[func]{knapsackDFS}\n</code></pre> knapsack.js<pre><code>[class]{}-[func]{knapsackDFS}\n</code></pre> knapsack.ts<pre><code>[class]{}-[func]{knapsackDFS}\n</code></pre> knapsack.dart<pre><code>[class]{}-[func]{knapsackDFS}\n</code></pre> knapsack.rs<pre><code>[class]{}-[func]{knapsack_dfs}\n</code></pre> knapsack.c<pre><code>[class]{}-[func]{knapsackDFS}\n</code></pre> knapsack.zig<pre><code>[class]{}-[func]{knapsackDFS}\n</code></pre> <p>\u5982\u56fe 14-18 \u6240\u793a\uff0c\u7531\u4e8e\u6bcf\u4e2a\u7269\u54c1\u90fd\u4f1a\u4ea7\u751f\u4e0d\u9009\u548c\u9009\u4e24\u6761\u641c\u7d22\u5206\u652f\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(2^n)\\) \u3002</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 14-18 \u00a0 0-1 \u80cc\u5305\u7684\u66b4\u529b\u641c\u7d22\u9012\u5f52\u6811 </p>"},{"location":"chapter_dynamic_programming/knapsack_problem/#2","title":"2. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.py<pre><code>[class]{}-[func]{knapsack_dfs_mem}\n</code></pre> knapsack.cpp<pre><code>[class]{}-[func]{knapsackDFSMem}\n</code></pre> knapsack.java<pre><code>[class]{knapsack}-[func]{knapsackDFSMem}\n</code></pre> knapsack.cs<pre><code>[class]{knapsack}-[func]{knapsackDFSMem}\n</code></pre> knapsack.go<pre><code>[class]{}-[func]{knapsackDFSMem}\n</code></pre> knapsack.swift<pre><code>[class]{}-[func]{knapsackDFSMem}\n</code></pre> knapsack.js<pre><code>[class]{}-[func]{knapsackDFSMem}\n</code></pre> knapsack.ts<pre><code>[class]{}-[func]{knapsackDFSMem}\n</code></pre> knapsack.dart<pre><code>[class]{}-[func]{knapsackDFSMem}\n</code></pre> knapsack.rs<pre><code>[class]{}-[func]{knapsack_dfs_mem}\n</code></pre> knapsack.c<pre><code>[class]{}-[func]{knapsackDFSMem}\n</code></pre> knapsack.zig<pre><code>[class]{}-[func]{knapsackDFSMem}\n</code></pre> <p>\u56fe 14-19 \u5c55\u793a\u4e86\u5728\u8bb0\u5fc6\u5316\u9012\u5f52\u4e2d\u88ab\u526a\u6389\u7684\u641c\u7d22\u5206\u652f\u3002</p> <p></p> <p> \u56fe 14-19 \u00a0 0-1 \u80cc\u5305\u7684\u8bb0\u5fc6\u5316\u641c\u7d22\u9012\u5f52\u6811 </p>"},{"location":"chapter_dynamic_programming/knapsack_problem/#3","title":"3. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.py<pre><code>[class]{}-[func]{knapsack_dp}\n</code></pre> knapsack.cpp<pre><code>[class]{}-[func]{knapsackDP}\n</code></pre> knapsack.java<pre><code>[class]{knapsack}-[func]{knapsackDP}\n</code></pre> knapsack.cs<pre><code>[class]{knapsack}-[func]{knapsackDP}\n</code></pre> knapsack.go<pre><code>[class]{}-[func]{knapsackDP}\n</code></pre> knapsack.swift<pre><code>[class]{}-[func]{knapsackDP}\n</code></pre> knapsack.js<pre><code>[class]{}-[func]{knapsackDP}\n</code></pre> knapsack.ts<pre><code>[class]{}-[func]{knapsackDP}\n</code></pre> knapsack.dart<pre><code>[class]{}-[func]{knapsackDP}\n</code></pre> knapsack.rs<pre><code>[class]{}-[func]{knapsack_dp}\n</code></pre> knapsack.c<pre><code>[class]{}-[func]{knapsackDP}\n</code></pre> knapsack.zig<pre><code>[class]{}-[func]{knapsackDP}\n</code></pre> <p>\u5982\u56fe 14-20 \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> <1><2><3><4><5><6><7><8><9><10><11><12><13><14> <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 14-20 \u00a0 0-1 \u80cc\u5305\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b </p>"},{"location":"chapter_dynamic_programming/knapsack_problem/#4","title":"4. \u00a0 \u7a7a\u95f4\u4f18\u5316","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\u7a7a\u95f4\u4f18\u5316\u5462\uff1f\u89c2\u5bdf\u53ef\u77e5\uff0c\u6bcf\u4e2a\u72b6\u6001\u90fd\u662f\u7531\u6b63\u4e0a\u65b9\u6216\u5de6\u4e0a\u65b9\u7684\u683c\u5b50\u8f6c\u79fb\u8fc7\u6765\u7684\u3002\u5047\u8bbe\u53ea\u6709\u4e00\u4e2a\u6570\u7ec4\uff0c\u5f53\u5f00\u59cb\u904d\u5386\u7b2c \\(i\\) \u884c\u65f6\uff0c\u8be5\u6570\u7ec4\u5b58\u50a8\u7684\u4ecd\u7136\u662f\u7b2c \\(i-1\\) \u884c\u7684\u72b6\u6001\u3002</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>\u56fe 14-21 \u5c55\u793a\u4e86\u5728\u5355\u4e2a\u6570\u7ec4\u4e0b\u4ece\u7b2c \\(i = 1\\) \u884c\u8f6c\u6362\u81f3\u7b2c \\(i = 2\\) \u884c\u7684\u8fc7\u7a0b\u3002\u8bf7\u601d\u8003\u6b63\u5e8f\u904d\u5386\u548c\u5012\u5e8f\u904d\u5386\u7684\u533a\u522b\u3002</p> <1><2><3><4><5><6> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 14-21 \u00a0 0-1 \u80cc\u5305\u7684\u7a7a\u95f4\u4f18\u5316\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> PythonC++JavaC#GoSwiftJSTSDartRustCZig knapsack.py<pre><code>[class]{}-[func]{knapsack_dp_comp}\n</code></pre> knapsack.cpp<pre><code>[class]{}-[func]{knapsackDPComp}\n</code></pre> knapsack.java<pre><code>[class]{knapsack}-[func]{knapsackDPComp}\n</code></pre> knapsack.cs<pre><code>[class]{knapsack}-[func]{knapsackDPComp}\n</code></pre> knapsack.go<pre><code>[class]{}-[func]{knapsackDPComp}\n</code></pre> knapsack.swift<pre><code>[class]{}-[func]{knapsackDPComp}\n</code></pre> knapsack.js<pre><code>[class]{}-[func]{knapsackDPComp}\n</code></pre> knapsack.ts<pre><code>[class]{}-[func]{knapsackDPComp}\n</code></pre> knapsack.dart<pre><code>[class]{}-[func]{knapsackDPComp}\n</code></pre> knapsack.rs<pre><code>[class]{}-[func]{knapsack_dp_comp}\n</code></pre> knapsack.c<pre><code>[class]{}-[func]{knapsackDPComp}\n</code></pre> knapsack.zig<pre><code>[class]{}-[func]{knapsackDPComp}\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\u7a7a\u95f4\u4f18\u5316\u4e2d\uff0c\u7531\u4e8e\u6bcf\u4e2a\u72b6\u6001\u4f9d\u8d56\u6b63\u4e0a\u65b9\u548c\u5de6\u4e0a\u65b9\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u9700\u8981\u5012\u5e8f\u904d\u5386\u5217\u8868\uff0c\u907f\u514d\u5de6\u4e0a\u65b9\u72b6\u6001\u88ab\u8986\u76d6\u3002</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\u7a7a\u95f4\u4f18\u5316\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\u987b\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\u7a7a\u95f4\u4f18\u5316\u540e\u6b63\u5e8f\u6216\u5012\u5e8f\u904d\u5386\u90fd\u65e0\u6cd5\u6b63\u786e\u5730\u8fdb\u884c\u72b6\u6001\u8f6c\u79fb\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5229\u7528\u4e00\u4e2a\u53d8\u91cf\u6682\u5b58\u5de6\u4e0a\u65b9\u72b6\u6001\uff0c\u4ece\u800c\u8f6c\u5316\u5230\u4e0e\u5b8c\u5168\u80cc\u5305\u7b49\u4ef7\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u7a7a\u95f4\u4f18\u5316\u540e\u8fdb\u884c\u6b63\u5e8f\u904d\u5386\u3002</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 14-22 \u00a0 \u5b8c\u5168\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e </p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"<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>\u5728\u5b8c\u5168\u80cc\u5305\u7684\u89c4\u5b9a\u4e0b\uff0c\u72b6\u6001 \\([i, c]\\) \u7684\u53d8\u5316\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5\u3002</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/#2","title":"2. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig unbounded_knapsack.py<pre><code>[class]{}-[func]{unbounded_knapsack_dp}\n</code></pre> unbounded_knapsack.cpp<pre><code>[class]{}-[func]{unboundedKnapsackDP}\n</code></pre> unbounded_knapsack.java<pre><code>[class]{unbounded_knapsack}-[func]{unboundedKnapsackDP}\n</code></pre> unbounded_knapsack.cs<pre><code>[class]{unbounded_knapsack}-[func]{unboundedKnapsackDP}\n</code></pre> unbounded_knapsack.go<pre><code>[class]{}-[func]{unboundedKnapsackDP}\n</code></pre> unbounded_knapsack.swift<pre><code>[class]{}-[func]{unboundedKnapsackDP}\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.dart<pre><code>[class]{}-[func]{unboundedKnapsackDP}\n</code></pre> unbounded_knapsack.rs<pre><code>[class]{}-[func]{unbounded_knapsack_dp}\n</code></pre> unbounded_knapsack.c<pre><code>[class]{}-[func]{unboundedKnapsackDP}\n</code></pre> unbounded_knapsack.zig<pre><code>[class]{}-[func]{unboundedKnapsackDP}\n</code></pre>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","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\u7a7a\u95f4\u4f18\u5316\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\u501f\u52a9\u56fe 14-23 \u6765\u7406\u89e3\u4e24\u8005\u7684\u533a\u522b\u3002</p> <1><2><3><4><5><6> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 14-23 \u00a0 \u5b8c\u5168\u80cc\u5305\u7684\u7a7a\u95f4\u4f18\u5316\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> PythonC++JavaC#GoSwiftJSTSDartRustCZig unbounded_knapsack.py<pre><code>[class]{}-[func]{unbounded_knapsack_dp_comp}\n</code></pre> unbounded_knapsack.cpp<pre><code>[class]{}-[func]{unboundedKnapsackDPComp}\n</code></pre> unbounded_knapsack.java<pre><code>[class]{unbounded_knapsack}-[func]{unboundedKnapsackDPComp}\n</code></pre> unbounded_knapsack.cs<pre><code>[class]{unbounded_knapsack}-[func]{unboundedKnapsackDPComp}\n</code></pre> unbounded_knapsack.go<pre><code>[class]{}-[func]{unboundedKnapsackDPComp}\n</code></pre> unbounded_knapsack.swift<pre><code>[class]{}-[func]{unboundedKnapsackDPComp}\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.dart<pre><code>[class]{}-[func]{unboundedKnapsackDPComp}\n</code></pre> unbounded_knapsack.rs<pre><code>[class]{}-[func]{unbounded_knapsack_dp_comp}\n</code></pre> unbounded_knapsack.c<pre><code>[class]{}-[func]{unboundedKnapsackDPComp}\n</code></pre> unbounded_knapsack.zig<pre><code>[class]{}-[func]{unboundedKnapsackDPComp}\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 14-24 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e </p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_1","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"<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\u3002</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>\u672c\u9898\u4e0e\u5b8c\u5168\u80cc\u5305\u7684\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u5b58\u5728\u4ee5\u4e0b\u4e24\u4e2a\u5dee\u5f02\u3002</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 \\(> 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/#2_1","title":"2. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change.py<pre><code>[class]{}-[func]{coin_change_dp}\n</code></pre> coin_change.cpp<pre><code>[class]{}-[func]{coinChangeDP}\n</code></pre> coin_change.java<pre><code>[class]{coin_change}-[func]{coinChangeDP}\n</code></pre> coin_change.cs<pre><code>[class]{coin_change}-[func]{coinChangeDP}\n</code></pre> coin_change.go<pre><code>[class]{}-[func]{coinChangeDP}\n</code></pre> coin_change.swift<pre><code>[class]{}-[func]{coinChangeDP}\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.dart<pre><code>[class]{}-[func]{coinChangeDP}\n</code></pre> coin_change.rs<pre><code>[class]{}-[func]{coin_change_dp}\n</code></pre> coin_change.c<pre><code>[class]{}-[func]{coinChangeDP}\n</code></pre> coin_change.zig<pre><code>[class]{}-[func]{coinChangeDP}\n</code></pre> <p>\u56fe 14-25 \u5c55\u793a\u4e86\u96f6\u94b1\u5151\u6362\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b\uff0c\u548c\u5b8c\u5168\u80cc\u5305\u975e\u5e38\u76f8\u4f3c\u3002</p> <1><2><3><4><5><6><7><8><9><10><11><12><13><14><15> <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 14-25 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898\u7684\u52a8\u6001\u89c4\u5212\u8fc7\u7a0b </p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_1","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"<p>\u96f6\u94b1\u5151\u6362\u7684\u7a7a\u95f4\u4f18\u5316\u7684\u5904\u7406\u65b9\u5f0f\u548c\u5b8c\u5168\u80cc\u5305\u4e00\u81f4\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change.py<pre><code>[class]{}-[func]{coin_change_dp_comp}\n</code></pre> coin_change.cpp<pre><code>[class]{}-[func]{coinChangeDPComp}\n</code></pre> coin_change.java<pre><code>[class]{coin_change}-[func]{coinChangeDPComp}\n</code></pre> coin_change.cs<pre><code>[class]{coin_change}-[func]{coinChangeDPComp}\n</code></pre> coin_change.go<pre><code>[class]{}-[func]{coinChangeDPComp}\n</code></pre> coin_change.swift<pre><code>[class]{}-[func]{coinChangeDPComp}\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.dart<pre><code>[class]{}-[func]{coinChangeDPComp}\n</code></pre> coin_change.rs<pre><code>[class]{}-[func]{coin_change_dp_comp}\n</code></pre> coin_change.c<pre><code>[class]{}-[func]{coinChangeDPComp}\n</code></pre> coin_change.zig<pre><code>[class]{}-[func]{coinChangeDPComp}\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 14-26 \u00a0 \u96f6\u94b1\u5151\u6362\u95ee\u9898 II \u7684\u793a\u4f8b\u6570\u636e </p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_2","title":"1. \u00a0 \u52a8\u6001\u89c4\u5212\u601d\u8def","text":"<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\u987b\u9009\u62e9\u4efb\u4f55\u786c\u5e01\u5373\u53ef\u51d1\u51fa\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u5e94\u5c06\u9996\u5217\u6240\u6709 \\(dp[i, 0]\\) \u90fd\u521d\u59cb\u5316\u4e3a \\(1\\) \u3002\u5f53\u65e0\u786c\u5e01\u65f6\uff0c\u65e0\u6cd5\u51d1\u51fa\u4efb\u4f55 \\(>0\\) \u7684\u76ee\u6807\u91d1\u989d\uff0c\u56e0\u6b64\u9996\u884c\u6240\u6709 \\(dp[0, a]\\) \u90fd\u7b49\u4e8e \\(0\\) \u3002</p>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_2","title":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_ii.py<pre><code>[class]{}-[func]{coin_change_ii_dp}\n</code></pre> coin_change_ii.cpp<pre><code>[class]{}-[func]{coinChangeIIDP}\n</code></pre> coin_change_ii.java<pre><code>[class]{coin_change_ii}-[func]{coinChangeIIDP}\n</code></pre> coin_change_ii.cs<pre><code>[class]{coin_change_ii}-[func]{coinChangeIIDP}\n</code></pre> coin_change_ii.go<pre><code>[class]{}-[func]{coinChangeIIDP}\n</code></pre> coin_change_ii.swift<pre><code>[class]{}-[func]{coinChangeIIDP}\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.dart<pre><code>[class]{}-[func]{coinChangeIIDP}\n</code></pre> coin_change_ii.rs<pre><code>[class]{}-[func]{coin_change_ii_dp}\n</code></pre> coin_change_ii.c<pre><code>[class]{}-[func]{coinChangeIIDP}\n</code></pre> coin_change_ii.zig<pre><code>[class]{}-[func]{coinChangeIIDP}\n</code></pre>"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_2","title":"3. \u00a0 \u7a7a\u95f4\u4f18\u5316","text":"<p>\u7a7a\u95f4\u4f18\u5316\u5904\u7406\u65b9\u5f0f\u76f8\u540c\uff0c\u5220\u9664\u786c\u5e01\u7ef4\u5ea6\u5373\u53ef\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_ii.py<pre><code>[class]{}-[func]{coin_change_ii_dp_comp}\n</code></pre> coin_change_ii.cpp<pre><code>[class]{}-[func]{coinChangeIIDPComp}\n</code></pre> coin_change_ii.java<pre><code>[class]{coin_change_ii}-[func]{coinChangeIIDPComp}\n</code></pre> coin_change_ii.cs<pre><code>[class]{coin_change_ii}-[func]{coinChangeIIDPComp}\n</code></pre> coin_change_ii.go<pre><code>[class]{}-[func]{coinChangeIIDPComp}\n</code></pre> coin_change_ii.swift<pre><code>[class]{}-[func]{coinChangeIIDPComp}\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.dart<pre><code>[class]{}-[func]{coinChangeIIDPComp}\n</code></pre> coin_change_ii.rs<pre><code>[class]{}-[func]{coin_change_ii_dp_comp}\n</code></pre> coin_change_ii.c<pre><code>[class]{}-[func]{coinChangeIIDPComp}\n</code></pre> coin_change_ii.zig<pre><code>[class]{}-[func]{coinChangeIIDPComp}\n</code></pre>"},{"location":"chapter_graph/","title":"\u7b2c 9 \u7ae0 \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 & = \\{ 1, 2, 3, 4, 5 \\} \\newline E & = \\{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \\} \\newline G & = \\{ V, E \\} \\newline \\end{aligned} \\] <p>\u5982\u679c\u5c06\u9876\u70b9\u770b\u4f5c\u8282\u70b9\uff0c\u5c06\u8fb9\u770b\u4f5c\u8fde\u63a5\u5404\u4e2a\u8282\u70b9\u7684\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u56fe\u770b\u4f5c\u662f\u4e00\u79cd\u4ece\u94fe\u8868\u62d3\u5c55\u800c\u6765\u7684\u6570\u636e\u7ed3\u6784\u3002\u5982\u56fe 9-1 \u6240\u793a\uff0c\u76f8\u8f83\u4e8e\u7ebf\u6027\u5173\u7cfb\uff08\u94fe\u8868\uff09\u548c\u5206\u6cbb\u5173\u7cfb\uff08\u6811\uff09\uff0c\u7f51\u7edc\u5173\u7cfb\uff08\u56fe\uff09\u7684\u81ea\u7531\u5ea6\u66f4\u9ad8\uff0c\u4ece\u800c\u66f4\u4e3a\u590d\u6742\u3002</p> <p></p> <p> \u56fe 9-1 \u00a0 \u94fe\u8868\u3001\u6811\u3001\u56fe\u4e4b\u95f4\u7684\u5173\u7cfb </p>"},{"location":"chapter_graph/graph/#911","title":"9.1.1 \u00a0 \u56fe\u5e38\u89c1\u7c7b\u578b\u4e0e\u672f\u8bed","text":"<p>\u6839\u636e\u8fb9\u662f\u5426\u5177\u6709\u65b9\u5411\uff0c\u53ef\u5206\u4e3a\u56fe 9-2 \u6240\u793a\u7684\u300c\u65e0\u5411\u56fe undirected graph\u300d\u548c\u300c\u6709\u5411\u56fe directed graph\u300d\u3002</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 9-2 \u00a0 \u6709\u5411\u56fe\u4e0e\u65e0\u5411\u56fe </p> <p>\u6839\u636e\u6240\u6709\u9876\u70b9\u662f\u5426\u8fde\u901a\uff0c\u53ef\u5206\u4e3a\u56fe 9-3 \u6240\u793a\u7684\u300c\u8fde\u901a\u56fe connected graph\u300d\u548c\u300c\u975e\u8fde\u901a\u56fe disconnected graph\u300d\u3002</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 9-3 \u00a0 \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\u56fe 9-4 \u6240\u793a\u7684\u300c\u6709\u6743\u56fe weighted graph\u300d\u3002\u4f8b\u5982\u5728\u738b\u8005\u8363\u8000\u7b49\u624b\u6e38\u4e2d\uff0c\u7cfb\u7edf\u4f1a\u6839\u636e\u5171\u540c\u6e38\u620f\u65f6\u95f4\u6765\u8ba1\u7b97\u73a9\u5bb6\u4e4b\u95f4\u7684\u201c\u4eb2\u5bc6\u5ea6\u201d\uff0c\u8fd9\u79cd\u4eb2\u5bc6\u5ea6\u7f51\u7edc\u5c31\u53ef\u4ee5\u7528\u6709\u6743\u56fe\u6765\u8868\u793a\u3002</p> <p></p> <p> \u56fe 9-4 \u00a0 \u6709\u6743\u56fe\u4e0e\u65e0\u6743\u56fe </p> <p>\u56fe\u6570\u636e\u7ed3\u6784\u5305\u542b\u4ee5\u4e0b\u5e38\u7528\u672f\u8bed\u3002</p> <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\u56fe 9-4 \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\u56fe 9-4 \u4e2d\uff0c\u8fb9\u5e8f\u5217 1-5-2-4 \u662f\u9876\u70b9 1 \u5230\u9876\u70b9 4 \u7684\u4e00\u6761\u8def\u5f84\u3002</li> <li>\u300c\u5ea6 degree\u300d\uff1a\u4e00\u4e2a\u9876\u70b9\u62e5\u6709\u7684\u8fb9\u6570\u3002\u5bf9\u4e8e\u6709\u5411\u56fe\uff0c\u300c\u5165\u5ea6 In-Degree\u300d\u8868\u793a\u6709\u591a\u5c11\u6761\u8fb9\u6307\u5411\u8be5\u9876\u70b9\uff0c\u300c\u51fa\u5ea6 Out-Degree\u300d\u8868\u793a\u6709\u591a\u5c11\u6761\u8fb9\u4ece\u8be5\u9876\u70b9\u6307\u51fa\u3002</li> </ul>"},{"location":"chapter_graph/graph/#912","title":"9.1.2 \u00a0 \u56fe\u7684\u8868\u793a","text":"<p>\u56fe\u7684\u5e38\u7528\u8868\u793a\u65b9\u5f0f\u5305\u62ec\u201c\u90bb\u63a5\u77e9\u9635\u201d\u548c\u201c\u90bb\u63a5\u8868\u201d\u3002\u4ee5\u4e0b\u4f7f\u7528\u65e0\u5411\u56fe\u8fdb\u884c\u4e3e\u4f8b\u3002</p>"},{"location":"chapter_graph/graph/#1","title":"1. \u00a0 \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\u56fe 9-5 \u6240\u793a\uff0c\u8bbe\u90bb\u63a5\u77e9\u9635\u4e3a \\(M\\)\u3001\u9876\u70b9\u5217\u8868\u4e3a \\(V\\) \uff0c\u90a3\u4e48\u77e9\u9635\u5143\u7d20 \\(M[i, j] = 1\\) \u8868\u793a\u9876\u70b9 \\(V[i]\\) \u5230\u9876\u70b9 \\(V[j]\\) \u4e4b\u95f4\u5b58\u5728\u8fb9\uff0c\u53cd\u4e4b \\(M[i, j] = 0\\) \u8868\u793a\u4e24\u9876\u70b9\u4e4b\u95f4\u65e0\u8fb9\u3002</p> <p></p> <p> \u56fe 9-5 \u00a0 \u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a </p> <p>\u90bb\u63a5\u77e9\u9635\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002</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\\) \u548c \\(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":"2. \u00a0 \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\u56fe 9-6 \u5c55\u793a\u4e86\u4e00\u4e2a\u4f7f\u7528\u90bb\u63a5\u8868\u5b58\u50a8\u7684\u56fe\u7684\u793a\u4f8b\u3002</p> <p></p> <p> \u56fe 9-6 \u00a0 \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\u56fe 9-6 \uff0c\u90bb\u63a5\u8868\u7ed3\u6784\u4e0e\u54c8\u5e0c\u8868\u4e2d\u7684\u201c\u94fe\u5f0f\u5730\u5740\u201d\u975e\u5e38\u76f8\u4f3c\uff0c\u56e0\u6b64\u6211\u4eec\u4e5f\u53ef\u4ee5\u91c7\u7528\u7c7b\u4f3c\u65b9\u6cd5\u6765\u4f18\u5316\u6548\u7387\u3002\u6bd4\u5982\u5f53\u94fe\u8868\u8f83\u957f\u65f6\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u5316\u4e3a AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u6548\u7387\u4ece \\(O(n)\\) \u4f18\u5316\u81f3 \\(O(\\log n)\\) \uff1b\u8fd8\u53ef\u4ee5\u628a\u94fe\u8868\u8f6c\u6362\u4e3a\u54c8\u5e0c\u8868\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4f4e\u81f3 \\(O(1)\\) \u3002</p>"},{"location":"chapter_graph/graph/#913","title":"9.1.3 \u00a0 \u56fe\u5e38\u89c1\u5e94\u7528","text":"<p>\u5982\u8868 9-1 \u6240\u793a\uff0c\u8bb8\u591a\u73b0\u5b9e\u7cfb\u7edf\u90fd\u53ef\u4ee5\u7528\u56fe\u6765\u5efa\u6a21\uff0c\u76f8\u5e94\u7684\u95ee\u9898\u4e5f\u53ef\u4ee5\u7ea6\u5316\u4e3a\u56fe\u8ba1\u7b97\u95ee\u9898\u3002</p> <p> \u8868 9-1 \u00a0 \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\u201c\u8fb9\u201d\u7684\u64cd\u4f5c\u548c\u5bf9\u201c\u9876\u70b9\u201d\u7684\u64cd\u4f5c\u3002\u5728\u201c\u90bb\u63a5\u77e9\u9635\u201d\u548c\u201c\u90bb\u63a5\u8868\u201d\u4e24\u79cd\u8868\u793a\u65b9\u6cd5\u4e0b\uff0c\u5b9e\u73b0\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\u3002</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\u5404\u79cd\u64cd\u4f5c\u7684\u5b9e\u73b0\u65b9\u5f0f\u5982\u56fe 9-7 \u6240\u793a\u3002</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 9-7 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig graph_adjacency_matrix.py<pre><code>[class]{GraphAdjMat}-[func]{}\n</code></pre> graph_adjacency_matrix.cpp<pre><code>[class]{GraphAdjMat}-[func]{}\n</code></pre> graph_adjacency_matrix.java<pre><code>[class]{GraphAdjMat}-[func]{}\n</code></pre> graph_adjacency_matrix.cs<pre><code>[class]{GraphAdjMat}-[func]{}\n</code></pre> graph_adjacency_matrix.go<pre><code>[class]{graphAdjMat}-[func]{}\n</code></pre> graph_adjacency_matrix.swift<pre><code>[class]{GraphAdjMat}-[func]{}\n</code></pre> graph_adjacency_matrix.js<pre><code>[class]{GraphAdjMat}-[func]{}\n</code></pre> graph_adjacency_matrix.ts<pre><code>[class]{GraphAdjMat}-[func]{}\n</code></pre> graph_adjacency_matrix.dart<pre><code>[class]{GraphAdjMat}-[func]{}\n</code></pre> graph_adjacency_matrix.rs<pre><code>[class]{GraphAdjMat}-[func]{}\n</code></pre> graph_adjacency_matrix.c<pre><code>[class]{graphAdjMat}-[func]{}\n</code></pre> graph_adjacency_matrix.zig<pre><code>\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\u53ef\u6839\u636e\u56fe 9-8 \u6240\u793a\u7684\u65b9\u6cd5\u5b9e\u73b0\u5404\u79cd\u64cd\u4f5c\u3002</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 9-8 \u00a0 \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\u800c\u8fd9\u6837\u505a\u662f\u6709\u539f\u56e0\u7684\u3002</p> <ol> <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 \\(> 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\u987b\u6539\u52a8\u5176\u4f59\u9876\u70b9\u4e86\u3002</li> </ol> PythonC++JavaC#GoSwiftJSTSDartRustCZig graph_adjacency_list.py<pre><code>[class]{GraphAdjList}-[func]{}\n</code></pre> graph_adjacency_list.cpp<pre><code>[class]{GraphAdjList}-[func]{}\n</code></pre> graph_adjacency_list.java<pre><code>[class]{GraphAdjList}-[func]{}\n</code></pre> graph_adjacency_list.cs<pre><code>[class]{GraphAdjList}-[func]{}\n</code></pre> graph_adjacency_list.go<pre><code>[class]{graphAdjList}-[func]{}\n</code></pre> graph_adjacency_list.swift<pre><code>[class]{GraphAdjList}-[func]{}\n</code></pre> graph_adjacency_list.js<pre><code>[class]{GraphAdjList}-[func]{}\n</code></pre> graph_adjacency_list.ts<pre><code>[class]{GraphAdjList}-[func]{}\n</code></pre> graph_adjacency_list.dart<pre><code>[class]{GraphAdjList}-[func]{}\n</code></pre> graph_adjacency_list.rs<pre><code>[class]{GraphAdjList}-[func]{}\n</code></pre> graph_adjacency_list.c<pre><code>[class]{graphAdjList}-[func]{}\n</code></pre> graph_adjacency_list.zig<pre><code>[class]{GraphAdjList}-[func]{}\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\u8868 9-2 \u5bf9\u6bd4\u4e86\u90bb\u63a5\u77e9\u9635\u548c\u90bb\u63a5\u8868\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u6548\u7387\u3002</p> <p> \u8868 9-2 \u00a0 \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\u8868 9-2 \uff0c\u4f3c\u4e4e\u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09\u7684\u65f6\u95f4\u4e0e\u7a7a\u95f4\u6548\u7387\u6700\u4f18\u3002\u4f46\u5b9e\u9645\u4e0a\uff0c\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u64cd\u4f5c\u8fb9\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u53ea\u9700\u8981\u4e00\u6b21\u6570\u7ec4\u8bbf\u95ee\u6216\u8d4b\u503c\u64cd\u4f5c\u5373\u53ef\u3002\u7efc\u5408\u6765\u770b\uff0c\u90bb\u63a5\u77e9\u9635\u4f53\u73b0\u4e86\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u7684\u539f\u5219\uff0c\u800c\u90bb\u63a5\u8868\u4f53\u73b0\u4e86\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d\u7684\u539f\u5219\u3002</p>"},{"location":"chapter_graph/graph_traversal/","title":"9.3 \u00a0 \u56fe\u7684\u904d\u5386","text":"<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\u3002</p> <p>\u56fe\u548c\u6811\u90fd\u9700\u8981\u5e94\u7528\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u904d\u5386\u64cd\u4f5c\u3002\u56fe\u7684\u904d\u5386\u65b9\u5f0f\u53ef\u5206\u4e3a\u4e24\u79cd\uff1a\u300c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 breadth-first traversal\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u904d\u5386 depth-first traversal\u300d\u3002\u5b83\u4eec\u4e5f\u5e38\u88ab\u79f0\u4e3a\u300c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 breadth-first search\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22 depth-first search\u300d\uff0c\u7b80\u79f0 BFS \u548c DFS \u3002</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\u67d0\u4e2a\u8282\u70b9\u51fa\u53d1\uff0c\u59cb\u7ec8\u4f18\u5148\u8bbf\u95ee\u8ddd\u79bb\u6700\u8fd1\u7684\u9876\u70b9\uff0c\u5e76\u4e00\u5c42\u5c42\u5411\u5916\u6269\u5f20\u3002\u5982\u56fe 9-9 \u6240\u793a\uff0c\u4ece\u5de6\u4e0a\u89d2\u9876\u70b9\u51fa\u53d1\uff0c\u5148\u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u7136\u540e\u904d\u5386\u4e0b\u4e00\u4e2a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u8bbf\u95ee\u5b8c\u6bd5\u3002</p> <p></p> <p> \u56fe 9-9 \u00a0 \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 </p>"},{"location":"chapter_graph/graph_traversal/#1","title":"1. \u00a0 \u7b97\u6cd5\u5b9e\u73b0","text":"<p>BFS \u901a\u5e38\u501f\u52a9\u961f\u5217\u6765\u5b9e\u73b0\u3002\u961f\u5217\u5177\u6709\u201c\u5148\u5165\u5148\u51fa\u201d\u7684\u6027\u8d28\uff0c\u8fd9\u4e0e BFS \u7684\u201c\u7531\u8fd1\u53ca\u8fdc\u201d\u7684\u601d\u60f3\u5f02\u66f2\u540c\u5de5\u3002</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> PythonC++JavaC#GoSwiftJSTSDartRustCZig graph_bfs.py<pre><code>[class]{}-[func]{graph_bfs}\n</code></pre> graph_bfs.cpp<pre><code>[class]{}-[func]{graphBFS}\n</code></pre> graph_bfs.java<pre><code>[class]{graph_bfs}-[func]{graphBFS}\n</code></pre> graph_bfs.cs<pre><code>[class]{graph_bfs}-[func]{graphBFS}\n</code></pre> graph_bfs.go<pre><code>[class]{}-[func]{graphBFS}\n</code></pre> graph_bfs.swift<pre><code>[class]{}-[func]{graphBFS}\n</code></pre> graph_bfs.js<pre><code>[class]{}-[func]{graphBFS}\n</code></pre> graph_bfs.ts<pre><code>[class]{}-[func]{graphBFS}\n</code></pre> graph_bfs.dart<pre><code>[class]{}-[func]{graphBFS}\n</code></pre> graph_bfs.rs<pre><code>[class]{}-[func]{graph_bfs}\n</code></pre> graph_bfs.c<pre><code>[class]{}-[func]{graphBFS}\n</code></pre> graph_bfs.zig<pre><code>[class]{}-[func]{graphBFS}\n</code></pre> <p>\u4ee3\u7801\u76f8\u5bf9\u62bd\u8c61\uff0c\u5efa\u8bae\u5bf9\u7167\u56fe 9-10 \u6765\u52a0\u6df1\u7406\u89e3\u3002</p> <1><2><3><4><5><6><7><8><9><10><11> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 9-10 \u00a0 \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\u56fe 9-10 \u4e3a\u4f8b\uff0c\u9876\u70b9 \\(1\\)\u3001\\(3\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u3001\u9876\u70b9 \\(2\\)\u3001\\(4\\)\u3001\\(6\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u4e5f\u53ef\u4ee5\u4efb\u610f\u4ea4\u6362\u3002</p>"},{"location":"chapter_graph/graph_traversal/#2","title":"2. \u00a0 \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\u5982\u56fe 9-11 \u6240\u793a\uff0c\u4ece\u5de6\u4e0a\u89d2\u9876\u70b9\u51fa\u53d1\uff0c\u8bbf\u95ee\u5f53\u524d\u9876\u70b9\u7684\u67d0\u4e2a\u90bb\u63a5\u9876\u70b9\uff0c\u76f4\u5230\u8d70\u5230\u5c3d\u5934\u65f6\u8fd4\u56de\uff0c\u518d\u7ee7\u7eed\u8d70\u5230\u5c3d\u5934\u5e76\u8fd4\u56de\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u9876\u70b9\u904d\u5386\u5b8c\u6210\u3002</p> <p></p> <p> \u56fe 9-11 \u00a0 \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386 </p>"},{"location":"chapter_graph/graph_traversal/#1_1","title":"1. \u00a0 \u7b97\u6cd5\u5b9e\u73b0","text":"<p>\u8fd9\u79cd\u201c\u8d70\u5230\u5c3d\u5934\u518d\u8fd4\u56de\u201d\u7684\u7b97\u6cd5\u8303\u5f0f\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002\u4e0e\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7c7b\u4f3c\uff0c\u5728\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u4e2d\u6211\u4eec\u4e5f\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 <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> PythonC++JavaC#GoSwiftJSTSDartRustCZig graph_dfs.py<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{graph_dfs}\n</code></pre> graph_dfs.cpp<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n</code></pre> graph_dfs.java<pre><code>[class]{graph_dfs}-[func]{dfs}\n[class]{graph_dfs}-[func]{graphDFS}\n</code></pre> graph_dfs.cs<pre><code>[class]{graph_dfs}-[func]{dfs}\n[class]{graph_dfs}-[func]{graphDFS}\n</code></pre> graph_dfs.go<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n</code></pre> graph_dfs.swift<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n</code></pre> graph_dfs.js<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n</code></pre> graph_dfs.ts<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n</code></pre> graph_dfs.dart<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n</code></pre> graph_dfs.rs<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{graph_dfs}\n</code></pre> graph_dfs.c<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n</code></pre> graph_dfs.zig<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n</code></pre> <p>\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u7b97\u6cd5\u6d41\u7a0b\u5982\u56fe 9-12 \u6240\u793a\u3002</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> <1><2><3><4><5><6><7><8><9><10><11> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 9-12 \u00a0 \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/#2_1","title":"2. \u00a0 \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":""},{"location":"chapter_graph/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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/#2-q-a","title":"2. \u00a0 Q & 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":"\u7b2c 15 \u7ae0 \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>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 15-3 \u00a0 \u5206\u6570\u80cc\u5305\u95ee\u9898\u7684\u793a\u4f8b\u6570\u636e </p> <p>\u5206\u6570\u80cc\u5305\u548c 0-1 \u80cc\u5305\u6574\u4f53\u4e0a\u975e\u5e38\u76f8\u4f3c\uff0c\u72b6\u6001\u5305\u542b\u5f53\u524d\u7269\u54c1 \\(i\\) \u548c\u5bb9\u91cf \\(c\\) \uff0c\u76ee\u6807\u662f\u6c42\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u4e0b\u7684\u6700\u5927\u4ef7\u503c\u3002</p> <p>\u4e0d\u540c\u70b9\u5728\u4e8e\uff0c\u672c\u9898\u5141\u8bb8\u53ea\u9009\u62e9\u7269\u54c1\u7684\u4e00\u90e8\u5206\u3002\u5982\u56fe 15-4 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u7269\u54c1\u4efb\u610f\u5730\u8fdb\u884c\u5207\u5206\uff0c\u5e76\u6309\u7167\u91cd\u91cf\u6bd4\u4f8b\u6765\u8ba1\u7b97\u7269\u54c1\u4ef7\u503c\u3002</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 15-4 \u00a0 \u7269\u54c1\u5728\u5355\u4f4d\u91cd\u91cf\u4e0b\u7684\u4ef7\u503c </p>"},{"location":"chapter_greedy/fractional_knapsack_problem/#1","title":"1. \u00a0 \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\u56fe 15-5 \u6240\u793a\u7684\u8d2a\u5fc3\u7b56\u7565\u3002</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 15-5 \u00a0 \u5206\u6570\u80cc\u5305\u7684\u8d2a\u5fc3\u7b56\u7565 </p>"},{"location":"chapter_greedy/fractional_knapsack_problem/#2","title":"2. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig fractional_knapsack.py<pre><code>[class]{Item}-[func]{}\n[class]{}-[func]{fractional_knapsack}\n</code></pre> fractional_knapsack.cpp<pre><code>[class]{Item}-[func]{}\n[class]{}-[func]{fractionalKnapsack}\n</code></pre> fractional_knapsack.java<pre><code>[class]{Item}-[func]{}\n[class]{fractional_knapsack}-[func]{fractionalKnapsack}\n</code></pre> fractional_knapsack.cs<pre><code>[class]{Item}-[func]{}\n[class]{fractional_knapsack}-[func]{fractionalKnapsack}\n</code></pre> fractional_knapsack.go<pre><code>[class]{Item}-[func]{}\n[class]{}-[func]{fractionalKnapsack}\n</code></pre> fractional_knapsack.swift<pre><code>[class]{Item}-[func]{}\n[class]{}-[func]{fractionalKnapsack}\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.dart<pre><code>[class]{Item}-[func]{}\n[class]{}-[func]{fractionalKnapsack}\n</code></pre> fractional_knapsack.rs<pre><code>[class]{Item}-[func]{}\n[class]{}-[func]{fractional_knapsack}\n</code></pre> fractional_knapsack.c<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> <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":"3. \u00a0 \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\u56fe 15-6 \u6240\u793a\uff0c\u5982\u679c\u5c06\u7269\u54c1\u91cd\u91cf\u548c\u7269\u54c1\u5355\u4f4d\u4ef7\u503c\u5206\u522b\u770b\u4f5c\u4e00\u4e2a 2D \u56fe\u8868\u7684\u6a2a\u8f74\u548c\u7eb5\u8f74\uff0c\u5219\u5206\u6570\u80cc\u5305\u95ee\u9898\u53ef\u88ab\u8f6c\u5316\u4e3a\u201c\u6c42\u5728\u6709\u9650\u6a2a\u8f74\u533a\u95f4\u4e0b\u7684\u6700\u5927\u56f4\u6210\u9762\u79ef\u201d\u3002\u8fd9\u4e2a\u7c7b\u6bd4\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4ece\u51e0\u4f55\u89d2\u5ea6\u7406\u89e3\u8d2a\u5fc3\u7b56\u7565\u7684\u6709\u6548\u6027\u3002</p> <p></p> <p> \u56fe 15-6 \u00a0 \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>\u300c\u8d2a\u5fc3\u7b97\u6cd5 greedy algorithm\u300d\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u7684\u7b97\u6cd5\uff0c\u5176\u57fa\u672c\u601d\u60f3\u662f\u5728\u95ee\u9898\u7684\u6bcf\u4e2a\u51b3\u7b56\u9636\u6bb5\uff0c\u90fd\u9009\u62e9\u5f53\u524d\u770b\u8d77\u6765\u6700\u4f18\u7684\u9009\u62e9\uff0c\u5373\u8d2a\u5fc3\u5730\u505a\u51fa\u5c40\u90e8\u6700\u4f18\u7684\u51b3\u7b56\uff0c\u4ee5\u671f\u671b\u83b7\u5f97\u5168\u5c40\u6700\u4f18\u89e3\u3002\u8d2a\u5fc3\u7b97\u6cd5\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u5728\u8bb8\u591a\u5b9e\u9645\u95ee\u9898\u4e2d\u90fd\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002</p> <p>\u8d2a\u5fc3\u7b97\u6cd5\u548c\u52a8\u6001\u89c4\u5212\u90fd\u5e38\u7528\u4e8e\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u3002\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u4e00\u4e9b\u76f8\u4f3c\u4e4b\u5904\uff0c\u6bd4\u5982\u90fd\u4f9d\u8d56\u6700\u4f18\u5b50\u7ed3\u6784\u6027\u8d28\uff0c\u4f46\u5de5\u4f5c\u539f\u7406\u662f\u4e0d\u540c\u7684\u3002</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>\u672c\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\u5982\u56fe 15-1 \u6240\u793a\u3002\u7ed9\u5b9a\u76ee\u6807\u91d1\u989d\uff0c\u6211\u4eec\u8d2a\u5fc3\u5730\u9009\u62e9\u4e0d\u5927\u4e8e\u4e14\u6700\u63a5\u8fd1\u5b83\u7684\u786c\u5e01\uff0c\u4e0d\u65ad\u5faa\u73af\u8be5\u6b65\u9aa4\uff0c\u76f4\u81f3\u51d1\u51fa\u76ee\u6807\u91d1\u989d\u4e3a\u6b62\u3002</p> <p></p> <p> \u56fe 15-1 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig coin_change_greedy.py<pre><code>[class]{}-[func]{coin_change_greedy}\n</code></pre> coin_change_greedy.cpp<pre><code>[class]{}-[func]{coinChangeGreedy}\n</code></pre> coin_change_greedy.java<pre><code>[class]{coin_change_greedy}-[func]{coinChangeGreedy}\n</code></pre> coin_change_greedy.cs<pre><code>[class]{coin_change_greedy}-[func]{coinChangeGreedy}\n</code></pre> coin_change_greedy.go<pre><code>[class]{}-[func]{coinChangeGreedy}\n</code></pre> coin_change_greedy.swift<pre><code>[class]{}-[func]{coinChangeGreedy}\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.dart<pre><code>[class]{}-[func]{coinChangeGreedy}\n</code></pre> coin_change_greedy.rs<pre><code>[class]{}-[func]{coin_change_greedy}\n</code></pre> coin_change_greedy.c<pre><code>[class]{}-[func]{coinChangeGreedy}\n</code></pre> coin_change_greedy.zig<pre><code>[class]{}-[func]{coinChangeGreedy}\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\u56fe 15-2 \u7ed9\u51fa\u4e86\u4e24\u4e2a\u793a\u4f8b\u3002</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 15-2 \u00a0 \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\u3002</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\u3002</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\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\u4ee5\u4e0b\u4e09\u6b65\u3002</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\u4e3b\u8981\u5305\u542b\u4ee5\u4e0b\u539f\u56e0\u3002</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\u5217\u4e3e\u4e86\u4e00\u4e9b\u5178\u578b\u7684\u8d2a\u5fc3\u7b97\u6cd5\u95ee\u9898\u3002</p> <ul> <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> </ul>"},{"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 15-7 \u00a0 \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":"1. \u00a0 \u8d2a\u5fc3\u7b56\u7565\u786e\u5b9a","text":"<p>\u8fd9\u9053\u9898\u8fd8\u6709\u66f4\u9ad8\u6548\u7387\u7684\u89e3\u6cd5\u3002\u5982\u56fe 15-8 \u6240\u793a\uff0c\u73b0\u9009\u53d6\u4e00\u4e2a\u72b6\u6001 \\([i, j]\\) \uff0c\u5176\u6ee1\u8db3\u7d22\u5f15 \\(i < j\\) \u4e14\u9ad8\u5ea6 \\(ht[i] < ht[j]\\) \uff0c\u5373 \\(i\\) \u4e3a\u77ed\u677f\u3001\\(j\\) \u4e3a\u957f\u677f\u3002</p> <p></p> <p> \u56fe 15-8 \u00a0 \u521d\u59cb\u72b6\u6001 </p> <p>\u5982\u56fe 15-9 \u6240\u793a\uff0c\u82e5\u6b64\u65f6\u5c06\u957f\u677f \\(j\\) \u5411\u77ed\u677f \\(i\\) \u9760\u8fd1\uff0c\u5219\u5bb9\u91cf\u4e00\u5b9a\u53d8\u5c0f\u3002</p> <p>\u8fd9\u662f\u56e0\u4e3a\u5728\u79fb\u52a8\u957f\u677f \\(j\\) \u540e\uff0c\u5bbd\u5ea6 \\(j-i\\) \u80af\u5b9a\u53d8\u5c0f\uff1b\u800c\u9ad8\u5ea6\u7531\u77ed\u677f\u51b3\u5b9a\uff0c\u56e0\u6b64\u9ad8\u5ea6\u53ea\u53ef\u80fd\u4e0d\u53d8\uff08 \\(i\\) \u4ecd\u4e3a\u77ed\u677f\uff09\u6216\u53d8\u5c0f\uff08\u79fb\u52a8\u540e\u7684 \\(j\\) \u6210\u4e3a\u77ed\u677f\uff09\u3002</p> <p></p> <p> \u56fe 15-9 \u00a0 \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\u4f8b\u5982\u5728\u56fe 15-10 \u4e2d\uff0c\u79fb\u52a8\u77ed\u677f\u540e\u9762\u79ef\u53d8\u5927\u3002</p> <p></p> <p> \u56fe 15-10 \u00a0 \u5411\u5185\u79fb\u52a8\u77ed\u677f\u540e\u7684\u72b6\u6001 </p> <p>\u7531\u6b64\u4fbf\u53ef\u63a8\u51fa\u672c\u9898\u7684\u8d2a\u5fc3\u7b56\u7565\uff1a\u521d\u59cb\u5316\u4e24\u6307\u9488\u5206\u88c2\u5bb9\u5668\u4e24\u7aef\uff0c\u6bcf\u8f6e\u5411\u5185\u6536\u7f29\u77ed\u677f\u5bf9\u5e94\u7684\u6307\u9488\uff0c\u76f4\u81f3\u4e24\u6307\u9488\u76f8\u9047\u3002</p> <p>\u56fe 15-11 \u5c55\u793a\u4e86\u8d2a\u5fc3\u7b56\u7565\u7684\u6267\u884c\u8fc7\u7a0b\u3002</p> <ol> <li>\u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6307\u9488 \\(i\\) \u548c \\(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> \u548c <code>3.</code> \u6b65\uff0c\u76f4\u81f3 \\(i\\) \u548c \\(j\\) \u76f8\u9047\u65f6\u7ed3\u675f\u3002</li> </ol> <1><2><3><4><5><6><7><8><9> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 15-11 \u00a0 \u6700\u5927\u5bb9\u91cf\u95ee\u9898\u7684\u8d2a\u5fc3\u8fc7\u7a0b </p>"},{"location":"chapter_greedy/max_capacity_problem/#2","title":"2. \u00a0 \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\\)\u3001\\(j\\)\u3001\\(res\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig max_capacity.py<pre><code>[class]{}-[func]{max_capacity}\n</code></pre> max_capacity.cpp<pre><code>[class]{}-[func]{maxCapacity}\n</code></pre> max_capacity.java<pre><code>[class]{max_capacity}-[func]{maxCapacity}\n</code></pre> max_capacity.cs<pre><code>[class]{max_capacity}-[func]{maxCapacity}\n</code></pre> max_capacity.go<pre><code>[class]{}-[func]{maxCapacity}\n</code></pre> max_capacity.swift<pre><code>[class]{}-[func]{maxCapacity}\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.dart<pre><code>[class]{}-[func]{maxCapacity}\n</code></pre> max_capacity.rs<pre><code>[class]{}-[func]{max_capacity}\n</code></pre> max_capacity.c<pre><code>[class]{}-[func]{maxCapacity}\n</code></pre> max_capacity.zig<pre><code>[class]{}-[func]{maxCapacity}\n</code></pre>"},{"location":"chapter_greedy/max_capacity_problem/#3","title":"3. \u00a0 \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\u56fe 15-12 \u6240\u793a\u7684\u72b6\u6001\u88ab\u201c\u8df3\u8fc7\u201d\u3002\u8fd9\u610f\u5473\u7740\u4e4b\u540e\u65e0\u6cd5\u9a8c\u8bc1\u8fd9\u4e9b\u72b6\u6001\u7684\u5bb9\u91cf\u5927\u5c0f\u3002</p> \\[ cap[i, i+1], cap[i, i+2], \\dots, cap[i, j-2], cap[i, j-1] \\] <p></p> <p> \u56fe 15-12 \u00a0 \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 15-13 \u00a0 \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":"1. \u00a0 \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) & \\geq n \\newline 2n - n - 4 & \\geq 0 \\newline n & \\geq 4 \\end{aligned} \\] <p>\u5982\u56fe 15-14 \u6240\u793a\uff0c\u5f53 \\(n \\geq 4\\) \u65f6\uff0c\u5207\u5206\u51fa\u4e00\u4e2a \\(2\\) \u540e\u4e58\u79ef\u4f1a\u53d8\u5927\uff0c\u8fd9\u8bf4\u660e\u5927\u4e8e\u7b49\u4e8e \\(4\\) \u7684\u6574\u6570\u90fd\u5e94\u8be5\u88ab\u5207\u5206\u3002</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\\)\u3001\\(2\\)\u3001\\(3\\) \u8fd9\u4e09\u79cd\u56e0\u5b50\u3002</p> <p></p> <p> \u56fe 15-14 \u00a0 \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\\)\u3001\\(2\\)\u3001\\(3\\) \u8fd9\u4e09\u4e2a\u56e0\u5b50\u4e2d\uff0c\u663e\u7136 \\(1\\) \u662f\u6700\u5dee\u7684\uff0c\u56e0\u4e3a \\(1 \\times (n-1) < n\\) \u6052\u6210\u7acb\uff0c\u5373\u5207\u5206\u51fa \\(1\\) \u53cd\u800c\u4f1a\u5bfc\u81f4\u4e58\u79ef\u51cf\u5c0f\u3002</p> <p>\u5982\u56fe 15-15 \u6240\u793a\uff0c\u5f53 \\(n = 6\\) \u65f6\uff0c\u6709 \\(3 \\times 3 > 2 \\times 2 \\times 2\\) \u3002\u8fd9\u610f\u5473\u7740\u5207\u5206\u51fa \\(3\\) \u6bd4\u5207\u5206\u51fa \\(2\\) \u66f4\u4f18\u3002</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 15-15 \u00a0 \u6700\u4f18\u5207\u5206\u56e0\u5b50 </p> <p>\u603b\u7ed3\u4ee5\u4e0a\uff0c\u53ef\u63a8\u51fa\u4ee5\u4e0b\u8d2a\u5fc3\u7b56\u7565\u3002</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\\)\u3001\\(1\\)\u3001\\(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 > 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":"2. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"<p>\u5982\u56fe 15-16 \u6240\u793a\uff0c\u6211\u4eec\u65e0\u987b\u901a\u8fc7\u5faa\u73af\u6765\u5207\u5206\u6574\u6570\uff0c\u800c\u53ef\u4ee5\u5229\u7528\u5411\u4e0b\u6574\u9664\u8fd0\u7b97\u5f97\u5230 \\(3\\) \u7684\u4e2a\u6570 \\(a\\) \uff0c\u7528\u53d6\u6a21\u8fd0\u7b97\u5f97\u5230\u4f59\u6570 \\(b\\) \uff0c\u6b64\u65f6\u6709\uff1a</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> PythonC++JavaC#GoSwiftJSTSDartRustCZig max_product_cutting.py<pre><code>[class]{}-[func]{max_product_cutting}\n</code></pre> max_product_cutting.cpp<pre><code>[class]{}-[func]{maxProductCutting}\n</code></pre> max_product_cutting.java<pre><code>[class]{max_product_cutting}-[func]{maxProductCutting}\n</code></pre> max_product_cutting.cs<pre><code>[class]{max_product_cutting}-[func]{maxProductCutting}\n</code></pre> max_product_cutting.go<pre><code>[class]{}-[func]{maxProductCutting}\n</code></pre> max_product_cutting.swift<pre><code>[class]{}-[func]{maxProductCutting}\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.dart<pre><code>[class]{}-[func]{maxProductCutting}\n</code></pre> max_product_cutting.rs<pre><code>[class]{}-[func]{max_product_cutting}\n</code></pre> max_product_cutting.c<pre><code>[class]{}-[func]{maxProductCutting}\n</code></pre> max_product_cutting.zig<pre><code>[class]{}-[func]{maxProductCutting}\n</code></pre> <p></p> <p> \u56fe 15-16 \u00a0 \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\u3002</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\\) \u548c \\(b\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002</p>"},{"location":"chapter_greedy/max_product_cutting_problem/#3","title":"3. \u00a0 \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\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5b58\u5728 \\(\\geq 4\\) \u7684\u56e0\u5b50 \\(x\\) \uff0c\u90a3\u4e48\u4e00\u5b9a\u53ef\u4ee5\u5c06\u5176\u7ee7\u7eed\u5212\u5206\u4e3a \\(2(x-2)\\) \uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u5927\u7684\u4e58\u79ef\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002</li> <li>\u5207\u5206\u65b9\u6848\u4e0d\u5305\u542b \\(1\\) \uff1a\u5047\u8bbe\u6700\u4f18\u5207\u5206\u65b9\u6848\u4e2d\u5b58\u5728\u4e00\u4e2a\u56e0\u5b50 \\(1\\) \uff0c\u90a3\u4e48\u5b83\u4e00\u5b9a\u53ef\u4ee5\u5408\u5e76\u5165\u53e6\u5916\u4e00\u4e2a\u56e0\u5b50\u4e2d\uff0c\u4ee5\u83b7\u53d6\u66f4\u5927\u4e58\u79ef\u3002\u8fd9\u4e0e\u5047\u8bbe\u77db\u76fe\u3002</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":"\u7b2c 6 \u7ae0 \u00a0 \u54c8\u5e0c\u8868","text":"<p>Abstract</p> <p>\u5728\u8ba1\u7b97\u673a\u4e16\u754c\u4e2d\uff0c\u54c8\u5e0c\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\u5982\u56fe 6-8 \u6240\u793a\uff0c\u5bf9\u4e8e\u94fe\u5730\u5740\u54c8\u5e0c\u8868\uff0c\u7406\u60f3\u60c5\u51b5\u4e0b\u952e\u503c\u5bf9\u5e73\u5747\u5206\u5e03\u5728\u5404\u4e2a\u6876\u4e2d\uff0c\u8fbe\u5230\u6700\u4f73\u67e5\u8be2\u6548\u7387\uff1b\u6700\u5dee\u60c5\u51b5\u4e0b\u6240\u6709\u952e\u503c\u5bf9\u90fd\u88ab\u5b58\u50a8\u5230\u540c\u4e00\u4e2a\u6876\u4e2d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u9000\u5316\u81f3 \\(O(n)\\) \u3002</p> <p></p> <p> \u56fe 6-8 \u00a0 \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\u3002</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</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\u4e3a\u4e86\u9632\u6b62\u4ece\u54c8\u5e0c\u503c\u63a8\u5bfc\u51fa\u539f\u59cb\u5bc6\u7801\u7b49\u9006\u5411\u5de5\u7a0b\uff0c\u54c8\u5e0c\u7b97\u6cd5\u9700\u8981\u5177\u5907\u66f4\u9ad8\u7b49\u7ea7\u7684\u5b89\u5168\u7279\u6027\u3002</p> <ul> <li>\u5355\u5411\u6027\uff1a\u65e0\u6cd5\u901a\u8fc7\u54c8\u5e0c\u503c\u53cd\u63a8\u51fa\u5173\u4e8e\u8f93\u5165\u6570\u636e\u7684\u4efb\u4f55\u4fe1\u606f\u3002</li> <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\u9700\u8981\u8003\u8651\u8bb8\u591a\u56e0\u7d20\u7684\u590d\u6742\u95ee\u9898\u3002\u7136\u800c\u5bf9\u4e8e\u67d0\u4e9b\u8981\u6c42\u4e0d\u9ad8\u7684\u573a\u666f\uff0c\u6211\u4eec\u4e5f\u80fd\u8bbe\u8ba1\u4e00\u4e9b\u7b80\u5355\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002</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> PythonC++JavaC#GoSwiftJSTSDartRustCZig simple_hash.py<pre><code>[class]{}-[func]{add_hash}\n[class]{}-[func]{mul_hash}\n[class]{}-[func]{xor_hash}\n[class]{}-[func]{rot_hash}\n</code></pre> simple_hash.cpp<pre><code>[class]{}-[func]{addHash}\n[class]{}-[func]{mulHash}\n[class]{}-[func]{xorHash}\n[class]{}-[func]{rotHash}\n</code></pre> simple_hash.java<pre><code>[class]{simple_hash}-[func]{addHash}\n[class]{simple_hash}-[func]{mulHash}\n[class]{simple_hash}-[func]{xorHash}\n[class]{simple_hash}-[func]{rotHash}\n</code></pre> simple_hash.cs<pre><code>[class]{simple_hash}-[func]{addHash}\n[class]{simple_hash}-[func]{mulHash}\n[class]{simple_hash}-[func]{xorHash}\n[class]{simple_hash}-[func]{rotHash}\n</code></pre> simple_hash.go<pre><code>[class]{}-[func]{addHash}\n[class]{}-[func]{mulHash}\n[class]{}-[func]{xorHash}\n[class]{}-[func]{rotHash}\n</code></pre> simple_hash.swift<pre><code>[class]{}-[func]{addHash}\n[class]{}-[func]{mulHash}\n[class]{}-[func]{xorHash}\n[class]{}-[func]{rotHash}\n</code></pre> simple_hash.js<pre><code>[class]{}-[func]{addHash}\n[class]{}-[func]{mulHash}\n[class]{}-[func]{xorHash}\n[class]{}-[func]{rotHash}\n</code></pre> simple_hash.ts<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>[class]{}-[func]{addHash}\n[class]{}-[func]{mulHash}\n[class]{}-[func]{xorHash}\n[class]{}-[func]{rotHash}\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> 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.zig<pre><code>[class]{}-[func]{addHash}\n[class]{}-[func]{mulHash}\n[class]{}-[func]{xorHash}\n[class]{}-[func]{rotHash}\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\\)\u3001\\(3\\)\u3001\\(6\\) \u8fd9\u4e09\u4e2a\u54c8\u5e0c\u503c\u3002</p> \\[ \\begin{aligned} \\text{modulus} & = 9 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 0, 3, 6, 0, 3, 6, 0, 3, 6,\\dots \\} \\end{aligned} \\] <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} & = 13 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 9, 12, 2, 5, 8, 11, 1, 4, 7, \\dots \\} \\end{aligned} \\] <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\u3001SHA-1\u3001SHA-2\u3001SHA3 \u7b49\u3002\u5b83\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u957f\u5ea6\u7684\u8f93\u5165\u6570\u636e\u6620\u5c04\u5230\u6052\u5b9a\u957f\u5ea6\u7684\u54c8\u5e0c\u503c\u3002</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\u8868 6-2 \u5c55\u793a\u4e86\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002</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> <p> \u8868 6-2 \u00a0 \u5e38\u89c1\u7684\u54c8\u5e0c\u7b97\u6cd5 </p> 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\u3002</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> PythonC++JavaC#GoSwiftJSTSDartRustCZig 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 <ListNode object at 0x1058fd810> \u7684\u54c8\u5e0c\u503c\u4e3a 274267521\n</code></pre> built_in_hash.cpp<pre><code>int num = 3;\nsize_t hashNum = hash<int>()(num);\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 3\nbool bol = true;\nsize_t hashBol = hash<bool>()(bol);\n// \u5e03\u5c14\u91cf 1 \u7684\u54c8\u5e0c\u503c\u4e3a 1\ndouble dec = 3.14159;\nsize_t hashDec = hash<double>()(dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 4614256650576692846\nstring str = \"Hello \u7b97\u6cd5\";\nsize_t hashStr = hash<string>()(str);\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a 15466937326284535026\n// \u5728 C++ \u4e2d\uff0c\u5185\u7f6e std:hash() \u4ec5\u63d0\u4f9b\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\n// \u6570\u7ec4\u3001\u5bf9\u8c61\u7684\u54c8\u5e0c\u503c\u8ba1\u7b97\u9700\u8981\u81ea\u884c\u5b9e\u73b0\n</code></pre> 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.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.go<pre><code>\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.js<pre><code>// JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n</code></pre> built_in_hash.ts<pre><code>// TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\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>use std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&mut num_hasher);\nlet hash_num = num_hasher.finish();\n// \u6574\u6570 3 \u7684\u54c8\u5e0c\u503c\u4e3a 568126464209439262\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// \u5e03\u5c14\u91cf true \u7684\u54c8\u5e0c\u503c\u4e3a 4952851536318644461\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\nprintln!(\"\u5c0f\u6570 {} \u7684\u54c8\u5e0c\u503c\u4e3a {}\", dec, hash_dec);\n// \u5c0f\u6570 3.14159 \u7684\u54c8\u5e0c\u503c\u4e3a 2566941990314602357\nlet str = \"Hello \u7b97\u6cd5\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&mut str_hasher);\nlet hash_str = str_hasher.finish();\n// \u5b57\u7b26\u4e32 Hello \u7b97\u6cd5 \u7684\u54c8\u5e0c\u503c\u4e3a 16092673739211250988\nlet arr = (&12836, &\"\u5c0f\u54c8\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// \u5143\u7ec4 (12836, \"\u5c0f\u54c8\") \u7684\u54c8\u5e0c\u503c\u4e3a 1885128010422702749\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&mut hasher);\nlet hash = hasher.finish();\n// \u8282\u70b9\u5bf9\u8c61 RefCell { value: ListNode { val: 42, next: None } } \u7684\u54c8\u5e0c\u503c\u4e3a15387811073369036852\n</code></pre> built_in_hash.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e hash code \u51fd\u6570\n</code></pre> built_in_hash.zig<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\u6876\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\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u7b56\u7565\u3002</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\u201c\u94fe\u5f0f\u5730\u5740\u201d\u548c\u201c\u5f00\u653e\u5bfb\u5740\u201d\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\u56fe 6-5 \u5c55\u793a\u4e86\u4e00\u4e2a\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u4f8b\u5b50\u3002</p> <p></p> <p> \u56fe 6-5 \u00a0 \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 </p> <p>\u57fa\u4e8e\u94fe\u5f0f\u5730\u5740\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\u7684\u64cd\u4f5c\u65b9\u6cd5\u53d1\u751f\u4e86\u4ee5\u4e0b\u53d8\u5316\u3002</p> <ul> <li>\u67e5\u8be2\u5143\u7d20\uff1a\u8f93\u5165 <code>key</code> \uff0c\u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u6876\u7d22\u5f15\uff0c\u5373\u53ef\u8bbf\u95ee\u94fe\u8868\u5934\u8282\u70b9\uff0c\u7136\u540e\u904d\u5386\u94fe\u8868\u5e76\u5bf9\u6bd4 <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>\u94fe\u5f0f\u5730\u5740\u5b58\u5728\u4ee5\u4e0b\u5c40\u9650\u6027\u3002</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\u4ee3\u7801\u7ed9\u51fa\u4e86\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u9700\u8981\u6ce8\u610f\u4e24\u70b9\u3002</p> <ul> <li>\u4f7f\u7528\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\u4ee3\u66ff\u94fe\u8868\uff0c\u4ece\u800c\u7b80\u5316\u4ee3\u7801\u3002\u5728\u8fd9\u79cd\u8bbe\u5b9a\u4e0b\uff0c\u54c8\u5e0c\u8868\uff08\u6570\u7ec4\uff09\u5305\u542b\u591a\u4e2a\u6876\uff0c\u6bcf\u4e2a\u6876\u90fd\u662f\u4e00\u4e2a\u5217\u8868\u3002</li> <li>\u4ee5\u4e0b\u5b9e\u73b0\u5305\u542b\u54c8\u5e0c\u8868\u6269\u5bb9\u65b9\u6cd5\u3002\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7 \\(\\frac{2}{3}\\) \u65f6\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u6269\u5bb9\u81f3 \\(2\\) \u500d\u3002</li> </ul> PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map_chaining.py<pre><code>[class]{HashMapChaining}-[func]{}\n</code></pre> hash_map_chaining.cpp<pre><code>[class]{HashMapChaining}-[func]{}\n</code></pre> hash_map_chaining.java<pre><code>[class]{HashMapChaining}-[func]{}\n</code></pre> hash_map_chaining.cs<pre><code>[class]{HashMapChaining}-[func]{}\n</code></pre> hash_map_chaining.go<pre><code>[class]{hashMapChaining}-[func]{}\n</code></pre> hash_map_chaining.swift<pre><code>[class]{HashMapChaining}-[func]{}\n</code></pre> hash_map_chaining.js<pre><code>[class]{HashMapChaining}-[func]{}\n</code></pre> hash_map_chaining.ts<pre><code>[class]{HashMapChaining}-[func]{}\n</code></pre> hash_map_chaining.dart<pre><code>[class]{HashMapChaining}-[func]{}\n</code></pre> hash_map_chaining.rs<pre><code>[class]{HashMapChaining}-[func]{}\n</code></pre> hash_map_chaining.c<pre><code>[class]{hashMapChaining}-[func]{}\n</code></pre> hash_map_chaining.zig<pre><code>[class]{HashMapChaining}-[func]{}\n</code></pre> <p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u94fe\u8868\u5f88\u957f\u65f6\uff0c\u67e5\u8be2\u6548\u7387 \\(O(n)\\) \u5f88\u5dee\u3002\u6b64\u65f6\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u201cAVL \u6811\u201d\u6216\u201c\u7ea2\u9ed1\u6811\u201d\uff0c\u4ece\u800c\u5c06\u67e5\u8be2\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002</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> <p>\u4e0b\u9762\u5c06\u4e3b\u8981\u4ee5\u7ebf\u6027\u63a2\u6d4b\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\u7684\u5de5\u4f5c\u673a\u5236\u4e0e\u4ee3\u7801\u5b9e\u73b0\u3002</p>"},{"location":"chapter_hashing/hash_collision/#1","title":"1. \u00a0 \u7ebf\u6027\u63a2\u6d4b","text":"<p>\u7ebf\u6027\u63a2\u6d4b\u91c7\u7528\u56fa\u5b9a\u6b65\u957f\u7684\u7ebf\u6027\u641c\u7d22\u6765\u8fdb\u884c\u63a2\u6d4b\uff0c\u5176\u64cd\u4f5c\u65b9\u6cd5\u4e0e\u666e\u901a\u54c8\u5e0c\u8868\u6709\u6240\u4e0d\u540c\u3002</p> <ul> <li>\u63d2\u5165\u5143\u7d20\uff1a\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u82e5\u53d1\u73b0\u6876\u5185\u5df2\u6709\u5143\u7d20\uff0c\u5219\u4ece\u51b2\u7a81\u4f4d\u7f6e\u5411\u540e\u7ebf\u6027\u904d\u5386\uff08\u6b65\u957f\u901a\u5e38\u4e3a \\(1\\) \uff09\uff0c\u76f4\u81f3\u627e\u5230\u7a7a\u6876\uff0c\u5c06\u5143\u7d20\u63d2\u5165\u5176\u4e2d\u3002</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\u6876\uff0c\u8bf4\u660e\u76ee\u6807\u5143\u7d20\u4e0d\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u8fd4\u56de \\(\\text{None}\\) \u3002</li> </ul> <p>\u56fe 6-6 \u5c55\u793a\u4e86\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u7684\u952e\u503c\u5bf9\u5206\u5e03\u3002\u6839\u636e\u6b64\u54c8\u5e0c\u51fd\u6570\uff0c\u6700\u540e\u4e24\u4f4d\u76f8\u540c\u7684 <code>key</code> \u90fd\u4f1a\u88ab\u6620\u5c04\u5230\u76f8\u540c\u7684\u6876\u3002\u800c\u901a\u8fc7\u7ebf\u6027\u63a2\u6d4b\uff0c\u5b83\u4eec\u88ab\u4f9d\u6b21\u5b58\u50a8\u5728\u8be5\u6876\u4ee5\u53ca\u4e4b\u4e0b\u7684\u6876\u4e2d\u3002</p> <p></p> <p> \u56fe 6-6 \u00a0 \u5f00\u653e\u5bfb\u5740\u548c\u7ebf\u6027\u63a2\u6d4b </p> <p>\u7136\u800c\uff0c\u7ebf\u6027\u63a2\u6d4b\u5bb9\u6613\u4ea7\u751f\u201c\u805a\u96c6\u73b0\u8c61\u201d\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6570\u7ec4\u4e2d\u8fde\u7eed\u88ab\u5360\u7528\u7684\u4f4d\u7f6e\u8d8a\u957f\uff0c\u8fd9\u4e9b\u8fde\u7eed\u4f4d\u7f6e\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\u7684\u53ef\u80fd\u6027\u8d8a\u5927\uff0c\u4ece\u800c\u8fdb\u4e00\u6b65\u4fc3\u4f7f\u8be5\u4f4d\u7f6e\u7684\u805a\u5806\u751f\u957f\uff0c\u5f62\u6210\u6076\u6027\u5faa\u73af\uff0c\u6700\u7ec8\u5bfc\u81f4\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u6548\u7387\u52a3\u5316\u3002</p> <p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u4e0d\u80fd\u5728\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\u4e2d\u76f4\u63a5\u5220\u9664\u5143\u7d20\u3002\u8fd9\u662f\u56e0\u4e3a\u5220\u9664\u5143\u7d20\u4f1a\u5728\u6570\u7ec4\u5185\u4ea7\u751f\u4e00\u4e2a\u7a7a\u6876 \\(\\text{None}\\) \uff0c\u800c\u5f53\u67e5\u8be2\u5143\u7d20\u65f6\uff0c\u7ebf\u6027\u63a2\u6d4b\u5230\u8be5\u7a7a\u6876\u5c31\u4f1a\u8fd4\u56de\uff0c\u56e0\u6b64\u5728\u8be5\u7a7a\u6876\u4e4b\u4e0b\u7684\u5143\u7d20\u90fd\u65e0\u6cd5\u518d\u88ab\u8bbf\u95ee\u5230\uff0c\u7a0b\u5e8f\u53ef\u80fd\u8bef\u5224\u8fd9\u4e9b\u5143\u7d20\u4e0d\u5b58\u5728\u3002</p> <p></p> <p> \u56fe 6-7 \u00a0 \u5728\u5f00\u653e\u5bfb\u5740\u4e2d\u5220\u9664\u5143\u7d20\u5bfc\u81f4\u7684\u67e5\u8be2\u95ee\u9898 </p> <p>\u4e3a\u4e86\u89e3\u51b3\u8be5\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u300c\u61d2\u5220\u9664 lazy deletion\u300d\u673a\u5236\uff1a\u5b83\u4e0d\u76f4\u63a5\u4ece\u54c8\u5e0c\u8868\u4e2d\u79fb\u9664\u5143\u7d20\uff0c\u800c\u662f\u5229\u7528\u4e00\u4e2a\u5e38\u91cf <code>TOMBSTONE</code> \u6765\u6807\u8bb0\u8fd9\u4e2a\u6876\u3002\u5728\u8be5\u673a\u5236\u4e0b\uff0c\\(\\text{None}\\) \u548c <code>TOMBSTONE</code> \u90fd\u4ee3\u8868\u7a7a\u6876\uff0c\u90fd\u53ef\u4ee5\u653e\u7f6e\u952e\u503c\u5bf9\u3002\u4f46\u4e0d\u540c\u7684\u662f\uff0c\u7ebf\u6027\u63a2\u6d4b\u5230 <code>TOMBSTONE</code> \u65f6\u5e94\u8be5\u7ee7\u7eed\u904d\u5386\uff0c\u56e0\u4e3a\u5176\u4e4b\u4e0b\u53ef\u80fd\u8fd8\u5b58\u5728\u952e\u503c\u5bf9\u3002</p> <p>\u7136\u800c\uff0c\u61d2\u5220\u9664\u53ef\u80fd\u4f1a\u52a0\u901f\u54c8\u5e0c\u8868\u7684\u6027\u80fd\u9000\u5316\u3002\u8fd9\u662f\u56e0\u4e3a\u6bcf\u6b21\u5220\u9664\u64cd\u4f5c\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e2a\u5220\u9664\u6807\u8bb0\uff0c\u968f\u7740 <code>TOMBSTONE</code> \u7684\u589e\u52a0\uff0c\u641c\u7d22\u65f6\u95f4\u4e5f\u4f1a\u589e\u52a0\uff0c\u56e0\u4e3a\u7ebf\u6027\u63a2\u6d4b\u53ef\u80fd\u9700\u8981\u8df3\u8fc7\u591a\u4e2a <code>TOMBSTONE</code> \u624d\u80fd\u627e\u5230\u76ee\u6807\u5143\u7d20\u3002</p> <p>\u4e3a\u6b64\uff0c\u8003\u8651\u5728\u7ebf\u6027\u63a2\u6d4b\u4e2d\u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a <code>TOMBSTONE</code> \u7684\u7d22\u5f15\uff0c\u5e76\u5c06\u641c\u7d22\u5230\u7684\u76ee\u6807\u5143\u7d20\u4e0e\u8be5 <code>TOMBSTONE</code> \u4ea4\u6362\u4f4d\u7f6e\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u5f53\u6bcf\u6b21\u67e5\u8be2\u6216\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u5143\u7d20\u4f1a\u88ab\u79fb\u52a8\u81f3\u8ddd\u79bb\u7406\u60f3\u4f4d\u7f6e\uff08\u63a2\u6d4b\u8d77\u59cb\u70b9\uff09\u66f4\u8fd1\u7684\u6876\uff0c\u4ece\u800c\u4f18\u5316\u67e5\u8be2\u6548\u7387\u3002</p> <p>\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5305\u542b\u61d2\u5220\u9664\u7684\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\uff09\u54c8\u5e0c\u8868\u3002\u4e3a\u4e86\u66f4\u52a0\u5145\u5206\u5730\u4f7f\u7528\u54c8\u5e0c\u8868\u7684\u7a7a\u95f4\uff0c\u6211\u4eec\u5c06\u54c8\u5e0c\u8868\u770b\u4f5c\u662f\u4e00\u4e2a\u201c\u73af\u5f62\u6570\u7ec4\u201d\uff0c\u5f53\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u56de\u5230\u5934\u90e8\u7ee7\u7eed\u904d\u5386\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map_open_addressing.py<pre><code>[class]{HashMapOpenAddressing}-[func]{}\n</code></pre> hash_map_open_addressing.cpp<pre><code>[class]{HashMapOpenAddressing}-[func]{}\n</code></pre> hash_map_open_addressing.java<pre><code>[class]{HashMapOpenAddressing}-[func]{}\n</code></pre> hash_map_open_addressing.cs<pre><code>[class]{HashMapOpenAddressing}-[func]{}\n</code></pre> hash_map_open_addressing.go<pre><code>[class]{hashMapOpenAddressing}-[func]{}\n</code></pre> hash_map_open_addressing.swift<pre><code>[class]{HashMapOpenAddressing}-[func]{}\n</code></pre> hash_map_open_addressing.js<pre><code>[class]{HashMapOpenAddressing}-[func]{}\n</code></pre> hash_map_open_addressing.ts<pre><code>[class]{HashMapOpenAddressing}-[func]{}\n</code></pre> hash_map_open_addressing.dart<pre><code>[class]{HashMapOpenAddressing}-[func]{}\n</code></pre> hash_map_open_addressing.rs<pre><code>[class]{HashMapOpenAddressing}-[func]{}\n</code></pre> hash_map_open_addressing.c<pre><code>[class]{hashMapOpenAddressing}-[func]{}\n</code></pre> hash_map_open_addressing.zig<pre><code>[class]{HashMapOpenAddressing}-[func]{}\n</code></pre>"},{"location":"chapter_hashing/hash_collision/#2","title":"2. \u00a0 \u5e73\u65b9\u63a2\u6d4b","text":"<p>\u5e73\u65b9\u63a2\u6d4b\u4e0e\u7ebf\u6027\u63a2\u6d4b\u7c7b\u4f3c\uff0c\u90fd\u662f\u5f00\u653e\u5bfb\u5740\u7684\u5e38\u89c1\u7b56\u7565\u4e4b\u4e00\u3002\u5f53\u53d1\u751f\u51b2\u7a81\u65f6\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e0d\u662f\u7b80\u5355\u5730\u8df3\u8fc7\u4e00\u4e2a\u56fa\u5b9a\u7684\u6b65\u6570\uff0c\u800c\u662f\u8df3\u8fc7\u201c\u63a2\u6d4b\u6b21\u6570\u7684\u5e73\u65b9\u201d\u7684\u6b65\u6570\uff0c\u5373 \\(1, 4, 9, \\dots\\) \u6b65\u3002</p> <p>\u5e73\u65b9\u63a2\u6d4b\u901a\u4e3b\u8981\u5177\u6709\u4ee5\u4e0b\u4f18\u52bf\u3002</p> <ul> <li>\u5e73\u65b9\u63a2\u6d4b\u901a\u8fc7\u8df3\u8fc7\u5e73\u65b9\u7684\u8ddd\u79bb\uff0c\u8bd5\u56fe\u7f13\u89e3\u7ebf\u6027\u63a2\u6d4b\u7684\u805a\u96c6\u6548\u5e94\u3002</li> <li>\u5e73\u65b9\u63a2\u6d4b\u4f1a\u8df3\u8fc7\u66f4\u5927\u7684\u8ddd\u79bb\u6765\u5bfb\u627e\u7a7a\u4f4d\u7f6e\uff0c\u6709\u52a9\u4e8e\u6570\u636e\u5206\u5e03\u5f97\u66f4\u52a0\u5747\u5300\u3002</li> </ul> <p>\u7136\u800c\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e5f\u5e76\u4e0d\u662f\u5b8c\u7f8e\u7684\u3002</p> <ul> <li>\u4ecd\u7136\u5b58\u5728\u805a\u96c6\u73b0\u8c61\uff0c\u5373\u67d0\u4e9b\u4f4d\u7f6e\u6bd4\u5176\u4ed6\u4f4d\u7f6e\u66f4\u5bb9\u6613\u88ab\u5360\u7528\u3002</li> <li>\u7531\u4e8e\u5e73\u65b9\u7684\u589e\u957f\uff0c\u5e73\u65b9\u63a2\u6d4b\u53ef\u80fd\u4e0d\u4f1a\u63a2\u6d4b\u6574\u4e2a\u54c8\u5e0c\u8868\uff0c\u8fd9\u610f\u5473\u7740\u5373\u4f7f\u54c8\u5e0c\u8868\u4e2d\u6709\u7a7a\u6876\uff0c\u5e73\u65b9\u63a2\u6d4b\u4e5f\u53ef\u80fd\u65e0\u6cd5\u8bbf\u95ee\u5230\u5b83\u3002</li> </ul>"},{"location":"chapter_hashing/hash_collision/#3","title":"3. \u00a0 \u591a\u6b21\u54c8\u5e0c","text":"<p>\u591a\u6b21\u54c8\u5e0c\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\)\u3001\\(f_2(x)\\)\u3001\\(f_3(x)\\)\u3001\\(\\dots\\) \u8fdb\u884c\u63a2\u6d4b\u3002</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\u6876\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\u5f53\u9047\u5230\u7a7a\u6876\u6216\u5df2\u5c1d\u8bd5\u6240\u6709\u54c8\u5e0c\u51fd\u6570\uff0c\u8bf4\u660e\u54c8\u5e0c\u8868\u4e2d\u4e0d\u5b58\u5728\u8be5\u5143\u7d20\uff0c\u5219\u8fd4\u56de \\(\\text{None}\\) \u3002</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> <p>Tip</p> <p>\u8bf7\u6ce8\u610f\uff0c\u5f00\u653e\u5bfb\u5740\uff08\u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u548c\u591a\u6b21\u54c8\u5e0c\uff09\u54c8\u5e0c\u8868\u90fd\u5b58\u5728\u201c\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u201d\u7684\u95ee\u9898\u3002</p>"},{"location":"chapter_hashing/hash_collision/#623","title":"6.2.3 \u00a0 \u7f16\u7a0b\u8bed\u8a00\u7684\u9009\u62e9","text":"<p>\u5404\u4e2a\u7f16\u7a0b\u8bed\u8a00\u91c7\u53d6\u4e86\u4e0d\u540c\u7684\u54c8\u5e0c\u8868\u5b9e\u73b0\u7b56\u7565\uff0c\u4ee5\u4e0b\u4e3e\u51e0\u4e2a\u4f8b\u5b50\u3002</p> <ul> <li>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</li> <li>Python \u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002\u5b57\u5178 dict \u4f7f\u7528\u4f2a\u968f\u673a\u6570\u8fdb\u884c\u63a2\u6d4b\u3002</li> <li>Golang \u91c7\u7528\u94fe\u5f0f\u5730\u5740\u3002Go \u89c4\u5b9a\u6bcf\u4e2a\u6876\u6700\u591a\u5b58\u50a8 8 \u4e2a\u952e\u503c\u5bf9\uff0c\u8d85\u51fa\u5bb9\u91cf\u5219\u8fde\u63a5\u4e00\u4e2a\u6ea2\u51fa\u6876\u3002\u5f53\u6ea2\u51fa\u6876\u8fc7\u591a\u65f6\uff0c\u4f1a\u6267\u884c\u4e00\u6b21\u7279\u6b8a\u7684\u7b49\u91cf\u6269\u5bb9\u64cd\u4f5c\uff0c\u4ee5\u786e\u4fdd\u6027\u80fd\u3002</li> </ul>"},{"location":"chapter_hashing/hash_map/","title":"6.1 \u00a0 \u54c8\u5e0c\u8868","text":"<p>\u300c\u54c8\u5e0c\u8868 hash table\u300d\uff0c\u53c8\u79f0\u300c\u6563\u5217\u8868\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>\u5982\u56fe 6-1 \u6240\u793a\uff0c\u7ed9\u5b9a \\(n\\) \u4e2a\u5b66\u751f\uff0c\u6bcf\u4e2a\u5b66\u751f\u90fd\u6709\u201c\u59d3\u540d\u201d\u548c\u201c\u5b66\u53f7\u201d\u4e24\u9879\u6570\u636e\u3002\u5047\u5982\u6211\u4eec\u5e0c\u671b\u5b9e\u73b0\u201c\u8f93\u5165\u4e00\u4e2a\u5b66\u53f7\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u59d3\u540d\u201d\u7684\u67e5\u8be2\u529f\u80fd\uff0c\u5219\u53ef\u4ee5\u91c7\u7528\u56fe 6-1 \u6240\u793a\u7684\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u3002</p> <p></p> <p> \u56fe 6-1 \u00a0 \u54c8\u5e0c\u8868\u7684\u62bd\u8c61\u8868\u793a </p> <p>\u9664\u54c8\u5e0c\u8868\u5916\uff0c\u6570\u7ec4\u548c\u94fe\u8868\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u67e5\u8be2\u529f\u80fd\uff0c\u5b83\u4eec\u7684\u6548\u7387\u5bf9\u6bd4\u5982\u8868 6-1 \u6240\u793a\u3002</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\uff08\u94fe\u8868\uff09\u4e2d\u5220\u9664\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002</li> </ul> <p> \u8868 6-1 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.cpp<pre><code>/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nunordered_map<int, string> map;\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nstring name = map[15937];\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.erase(10583);\n</code></pre> hash_map.java<pre><code>/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap<Integer, String> map = new HashMap<>();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.put(12836, \"\u5c0f\u54c8\"); map.put(15937, \"\u5c0f\u5570\"); map.put(16750, \"\u5c0f\u7b97\"); map.put(13276, \"\u5c0f\u6cd5\");\nmap.put(10583, \"\u5c0f\u9e2d\");\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map.get(15937);\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n</code></pre> hash_map.cs<pre><code>/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nDictionary<int, String> map = new ();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.Add(12836, \"\u5c0f\u54c8\");\nmap.Add(15937, \"\u5c0f\u5570\");\nmap.Add(16750, \"\u5c0f\u7b97\");\nmap.Add(13276, \"\u5c0f\u6cd5\");\nmap.Add(10583, \"\u5c0f\u9e2d\");\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map[15937];\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.Remove(10583);\n</code></pre> hash_map_test.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.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.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<number, string>();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.set(12836, '\u5c0f\u54c8');\nmap.set(15937, '\u5c0f\u5570');\nmap.set(16750, '\u5c0f\u7b97');\nmap.set(13276, '\u5c0f\u6cd5');\nmap.set(10583, '\u5c0f\u9e2d');\nconsole.info('\\n\u6dfb\u52a0\u5b8c\u6210\u540e\uff0c\u54c8\u5e0c\u8868\u4e3a\\nKey -> Value');\nconsole.info(map);\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map.get(15937);\nconsole.info('\\n\u8f93\u5165\u5b66\u53f7 15937 \uff0c\u67e5\u8be2\u5230\u59d3\u540d ' + name);\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.delete(10583);\nconsole.info('\\n\u5220\u9664 10583 \u540e\uff0c\u54c8\u5e0c\u8868\u4e3a\\nKey -> Value');\nconsole.info(map);\n</code></pre> hash_map.dart<pre><code>/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nMap<int, String> map = {};\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\";\nmap[15937] = \"\u5c0f\u5570\";\nmap[16750] = \"\u5c0f\u7b97\";\nmap[13276] = \"\u5c0f\u6cd5\";\nmap[10583] = \"\u5c0f\u9e2d\";\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nString name = map[15937];\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.remove(10583);\n</code></pre> hash_map.rs<pre><code>use std::collections::HashMap;\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nlet mut map: HashMap<i32, String> = HashMap::new();\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap.insert(12836, \"\u5c0f\u54c8\".to_string());\nmap.insert(15937, \"\u5c0f\u5570\".to_string());\nmap.insert(16750, \"\u5c0f\u7b97\".to_string());\nmap.insert(13279, \"\u5c0f\u6cd5\".to_string());\nmap.insert(10583, \"\u5c0f\u9e2d\".to_string());\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet _name: Option<&String> = map.get(&15937);\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nlet _removed_value: Option<String> = map.remove(&10583);\n</code></pre> hash_map.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n</code></pre> hash_map.zig<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> PythonC++JavaC#GoSwiftJSTSDartRustCZig hash_map.py<pre><code># \u904d\u5386\u54c8\u5e0c\u8868\n# \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value in hmap.items():\nprint(key, \"->\", value)\n# \u5355\u72ec\u904d\u5386\u952e key\nfor key in hmap.keys():\nprint(key)\n# \u5355\u72ec\u904d\u5386\u503c value\nfor value in hmap.values():\nprint(value)\n</code></pre> hash_map.cpp<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (auto kv: map) {\ncout << kv.first << \" -> \" << kv.second << endl;\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor (auto kv: map) {\ncout << kv.first << endl;\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (auto kv: map) {\ncout << kv.second << endl;\n}\n</code></pre> hash_map.java<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor (Map.Entry <Integer, String> kv: map.entrySet()) {\nSystem.out.println(kv.getKey() + \" -> \" + kv.getValue());\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor (int key: map.keySet()) {\nSystem.out.println(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (String val: map.values()) {\nSystem.out.println(val);\n}\n</code></pre> hash_map.cs<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nforeach (var kv in map) {\nConsole.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nforeach (int key in map.Keys) {\nConsole.WriteLine(key);\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nforeach (String val in map.Values) {\nConsole.WriteLine(val);\n}\n</code></pre> hash_map_test.go<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value := range hmap {\nfmt.Println(key, \"->\", value)\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor key := range hmap {\nfmt.Println(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor _, value := range hmap {\nfmt.Println(value)\n}\n</code></pre> hash_map.swift<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in map {\nprint(\"\\(key) -> \\(value)\")\n}\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys {\nprint(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values {\nprint(value)\n}\n</code></pre> hash_map.js<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\nconsole.info('\\n\u904d\u5386\u952e\u503c\u5bf9 Key->Value');\nfor (const [k, v] of map.entries()) {\nconsole.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\nconsole.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\nconsole.info(v);\n}\n</code></pre> hash_map.ts<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\nconsole.info('\\n\u904d\u5386\u952e\u503c\u5bf9 Key->Value');\nfor (const [k, v] of map.entries()) {\nconsole.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u952e Key');\nfor (const k of map.keys()) {\nconsole.info(k);\n}\nconsole.info('\\n\u5355\u72ec\u904d\u5386\u503c Value');\nfor (const v of map.values()) {\nconsole.info(v);\n}\n</code></pre> hash_map.dart<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nmap.forEach((key, value) {\nprint('$key -> $value');\n});\n// \u5355\u72ec\u904d\u5386\u952e Key\nmap.keys.forEach((key) {\nprint(key);\n});\n// \u5355\u72ec\u904d\u5386\u503c Value\nmap.values.forEach((value) {\nprint(value);\n});\n</code></pre> hash_map.rs<pre><code>/* \u904d\u5386\u54c8\u5e0c\u8868 */\n// \u904d\u5386\u952e\u503c\u5bf9 Key->Value\nfor (key, value) in &map {\nprintln!(\"{key} -> {value}\");\n}\n// \u5355\u72ec\u904d\u5386\u952e Key\nfor key in map.keys() {\nprintln!(\"{key}\"); }\n// \u5355\u72ec\u904d\u5386\u503c Value\nfor value in map.values() {\nprintln!(\"{value}\");\n}\n</code></pre> hash_map.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u54c8\u5e0c\u8868\n</code></pre> hash_map.zig<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\u4ee5\u4e0b\u4e24\u6b65\u3002</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\u56fe 6-2 \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 6-2 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig array_hash_map.py<pre><code>[class]{Pair}-[func]{}\n[class]{ArrayHashMap}-[func]{}\n</code></pre> array_hash_map.cpp<pre><code>[class]{Pair}-[func]{}\n[class]{ArrayHashMap}-[func]{}\n</code></pre> array_hash_map.java<pre><code>[class]{Pair}-[func]{}\n[class]{ArrayHashMap}-[func]{}\n</code></pre> array_hash_map.cs<pre><code>[class]{Pair}-[func]{}\n[class]{ArrayHashMap}-[func]{}\n</code></pre> array_hash_map.go<pre><code>[class]{pair}-[func]{}\n[class]{arrayHashMap}-[func]{}\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[class]{ArrayHashMap}-[func]{}\n</code></pre> array_hash_map.js<pre><code>[class]{Pair}-[func]{}\n[class]{ArrayHashMap}-[func]{}\n</code></pre> array_hash_map.ts<pre><code>[class]{Pair}-[func]{}\n[class]{ArrayHashMap}-[func]{}\n</code></pre> array_hash_map.dart<pre><code>[class]{Pair}-[func]{}\n[class]{ArrayHashMap}-[func]{}\n</code></pre> array_hash_map.rs<pre><code>[class]{Pair}-[func]{}\n[class]{ArrayHashMap}-[func]{}\n</code></pre> array_hash_map.c<pre><code>[class]{pair}-[func]{}\n[class]{arrayHashMap}-[func]{}\n</code></pre> array_hash_map.zig<pre><code>[class]{Pair}-[func]{}\n[class]{ArrayHashMap}-[func]{}\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\u56fe 6-3 \u6240\u793a\uff0c\u4e24\u4e2a\u5b66\u53f7\u6307\u5411\u4e86\u540c\u4e00\u4e2a\u59d3\u540d\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002\u6211\u4eec\u5c06\u8fd9\u79cd\u591a\u4e2a\u8f93\u5165\u5bf9\u5e94\u540c\u4e00\u8f93\u51fa\u7684\u60c5\u51b5\u79f0\u4e3a\u300c\u54c8\u5e0c\u51b2\u7a81 hash collision\u300d\u3002</p> <p></p> <p> \u56fe 6-3 \u00a0 \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</p> <p>\u5982\u56fe 6-4 \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 6-4 \u00a0 \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":""},{"location":"chapter_hashing/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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\u3001SHA-1\u3001SHA-2 \u548c SHA3 \u7b49\u3002MD5 \u5e38\u7528\u4e8e\u6821\u9a8c\u6587\u4ef6\u5b8c\u6574\u6027\uff0cSHA-2 \u5e38\u7528\u4e8e\u5b89\u5168\u5e94\u7528\u4e0e\u534f\u8bae\u3002</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/#2-q-a","title":"2. \u00a0 Q & 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":"\u7b2c 8 \u7ae0 \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>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u4f7f\u7528\u4e00\u4e2a\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u6765\u6784\u5efa\u4e00\u4e2a\u5806\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u88ab\u79f0\u4e3a\u201c\u5efa\u5806\u64cd\u4f5c\u201d\u3002</p>"},{"location":"chapter_heap/build_heap/#821","title":"8.2.1 \u00a0 \u501f\u52a9\u5165\u5806\u64cd\u4f5c\u5b9e\u73b0","text":"<p>\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5806\uff0c\u7136\u540e\u904d\u5386\u5217\u8868\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u201c\u5165\u5806\u64cd\u4f5c\u201d\uff0c\u5373\u5148\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u5806\u7684\u5c3e\u90e8\uff0c\u518d\u5bf9\u8be5\u5143\u7d20\u6267\u884c\u201c\u4ece\u5e95\u81f3\u9876\u201d\u5806\u5316\u3002</p> <p>\u6bcf\u5f53\u4e00\u4e2a\u5143\u7d20\u5165\u5806\uff0c\u5806\u7684\u957f\u5ea6\u5c31\u52a0\u4e00\u3002\u7531\u4e8e\u8282\u70b9\u662f\u4ece\u9876\u5230\u5e95\u4f9d\u6b21\u88ab\u6dfb\u52a0\u8fdb\u4e8c\u53c9\u6811\u7684\uff0c\u56e0\u6b64\u5806\u662f\u201c\u81ea\u4e0a\u800c\u4e0b\u201d\u5730\u6784\u5efa\u7684\u3002</p> <p>\u8bbe\u5143\u7d20\u6570\u91cf\u4e3a \\(n\\) \uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u5165\u5806\u64cd\u4f5c\u4f7f\u7528 \\(O(\\log{n})\\) \u65f6\u95f4\uff0c\u56e0\u6b64\u8be5\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002</p>"},{"location":"chapter_heap/build_heap/#822","title":"8.2.2 \u00a0 \u901a\u8fc7\u904d\u5386\u5806\u5316\u5b9e\u73b0","text":"<p>\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u79cd\u66f4\u4e3a\u9ad8\u6548\u7684\u5efa\u5806\u65b9\u6cd5\uff0c\u5171\u5206\u4e3a\u4e24\u6b65\u3002</p> <ol> <li>\u5c06\u5217\u8868\u6240\u6709\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u5230\u5806\u4e2d\uff0c\u6b64\u65f6\u5806\u7684\u6027\u8d28\u5c1a\u672a\u5f97\u5230\u6ee1\u8db3\u3002</li> <li>\u5012\u5e8f\u904d\u5386\u5806\uff08\u5373\u5c42\u5e8f\u904d\u5386\u7684\u5012\u5e8f\uff09\uff0c\u4f9d\u6b21\u5bf9\u6bcf\u4e2a\u975e\u53f6\u8282\u70b9\u6267\u884c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u3002</li> </ol> <p>\u6bcf\u5f53\u5806\u5316\u4e00\u4e2a\u8282\u70b9\u540e\uff0c\u4ee5\u8be5\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\u5c31\u5f62\u6210\u4e00\u4e2a\u5408\u6cd5\u7684\u5b50\u5806\u3002\u800c\u7531\u4e8e\u662f\u5012\u5e8f\u904d\u5386\uff0c\u56e0\u6b64\u5806\u662f\u201c\u81ea\u4e0b\u800c\u4e0a\u201d\u5730\u88ab\u6784\u5efa\u7684\u3002</p> <p>\u4e4b\u6240\u4ee5\u9009\u62e9\u5012\u5e8f\u904d\u5386\uff0c\u662f\u56e0\u4e3a\u8fd9\u6837\u80fd\u591f\u4fdd\u8bc1\u5f53\u524d\u8282\u70b9\u4e4b\u4e0b\u7684\u5b50\u6811\u5df2\u7ecf\u662f\u5408\u6cd5\u7684\u5b50\u5806\uff0c\u8fd9\u6837\u5806\u5316\u5f53\u524d\u8282\u70b9\u624d\u662f\u6709\u6548\u7684\u3002</p> <p>\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u53f6\u8282\u70b9\u6ca1\u6709\u5b50\u8282\u70b9\uff0c\u5929\u7136\u5c31\u662f\u5408\u6cd5\u7684\u5b50\u5806\uff0c\u56e0\u6b64\u65e0\u9700\u5806\u5316\u3002\u5982\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff0c\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u8282\u70b9\u662f\u6700\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u7236\u8282\u70b9\uff0c\u6211\u4eec\u4ece\u5b83\u5f00\u59cb\u5012\u5e8f\u904d\u5386\u5e76\u6267\u884c\u5806\u5316\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.py<pre><code>[class]{MaxHeap}-[func]{__init__}\n</code></pre> my_heap.cpp<pre><code>[class]{MaxHeap}-[func]{MaxHeap}\n</code></pre> my_heap.java<pre><code>[class]{MaxHeap}-[func]{MaxHeap}\n</code></pre> my_heap.cs<pre><code>[class]{MaxHeap}-[func]{MaxHeap}\n</code></pre> my_heap.go<pre><code>[class]{maxHeap}-[func]{newMaxHeap}\n</code></pre> my_heap.swift<pre><code>[class]{MaxHeap}-[func]{init}\n</code></pre> my_heap.js<pre><code>[class]{MaxHeap}-[func]{constructor}\n</code></pre> my_heap.ts<pre><code>[class]{MaxHeap}-[func]{constructor}\n</code></pre> my_heap.dart<pre><code>[class]{MaxHeap}-[func]{MaxHeap}\n</code></pre> my_heap.rs<pre><code>[class]{MaxHeap}-[func]{new}\n</code></pre> my_heap.c<pre><code>[class]{maxHeap}-[func]{newMaxHeap}\n</code></pre> my_heap.zig<pre><code>[class]{MaxHeap}-[func]{init}\n</code></pre>"},{"location":"chapter_heap/build_heap/#823","title":"8.2.3 \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"<p>\u4e0b\u9762\uff0c\u6211\u4eec\u6765\u5c1d\u8bd5\u63a8\u7b97\u7b2c\u4e8c\u79cd\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002</p> <ul> <li>\u5047\u8bbe\u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u5219\u53f6\u8282\u70b9\u6570\u91cf\u4e3a \\((n + 1) / 2\\) \uff0c\u5176\u4e2d \\(/\\) \u4e3a\u5411\u4e0b\u6574\u9664\u3002\u56e0\u6b64\u9700\u8981\u5806\u5316\u7684\u8282\u70b9\u6570\u91cf\u4e3a \\((n - 1) / 2\\) \u3002</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 \\(\\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\u4f46\u8fd9\u4e2a\u4f30\u7b97\u7ed3\u679c\u5e76\u4e0d\u51c6\u786e\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u8003\u8651\u5230\u4e8c\u53c9\u6811\u5e95\u5c42\u8282\u70b9\u6570\u91cf\u8fdc\u591a\u4e8e\u9876\u5c42\u8282\u70b9\u7684\u6027\u8d28\u3002</p> <p>\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u8fdb\u884c\u66f4\u4e3a\u51c6\u786e\u7684\u8ba1\u7b97\u3002\u4e3a\u4e86\u51cf\u5c0f\u8ba1\u7b97\u96be\u5ea6\uff0c\u5047\u8bbe\u7ed9\u5b9a\u4e00\u4e2a\u8282\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff0c\u8be5\u5047\u8bbe\u4e0d\u4f1a\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002</p> <p></p> <p> \u56fe 8-5 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u5404\u5c42\u8282\u70b9\u6570\u91cf </p> <p>\u5982\u56fe 8-5 \u6240\u793a\uff0c\u8282\u70b9\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u7684\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u7b49\u4e8e\u8be5\u8282\u70b9\u5230\u53f6\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u800c\u8be5\u8ddd\u79bb\u6b63\u662f\u201c\u8282\u70b9\u9ad8\u5ea6\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5404\u5c42\u7684\u201c\u8282\u70b9\u6570\u91cf \\(\\times\\) \u8282\u70b9\u9ad8\u5ea6\u201d\u6c42\u548c\uff0c\u4ece\u800c\u5f97\u5230\u6240\u6709\u8282\u70b9\u7684\u5806\u5316\u8fed\u4ee3\u6b21\u6570\u7684\u603b\u548c\u3002</p> \\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 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\uff1a</p> \\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{h-1}\\times1 \\newline 2 T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\dots + 2^{h}\\times1 \\newline \\end{aligned} \\] <p>\u4f7f\u7528\u9519\u4f4d\u76f8\u51cf\u6cd5\uff0c\u7528\u4e0b\u5f0f \\(2 T(h)\\) \u51cf\u53bb\u4e0a\u5f0f \\(T(h)\\) \uff0c\u53ef\u5f97\uff1a</p> \\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\] <p>\u89c2\u5bdf\u4e0a\u5f0f\uff0c\u53d1\u73b0 \\(T(h)\\) \u662f\u4e00\u4e2a\u7b49\u6bd4\u6570\u5217\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528\u6c42\u548c\u516c\u5f0f\uff0c\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\uff1a</p> \\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h - 2 \\newline & = 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\u4e3b\u8981\u53ef\u5206\u4e3a\u56fe 8-1 \u6240\u793a\u7684\u4e24\u79cd\u7c7b\u578b\u3002</p> <ul> <li>\u300c\u5927\u9876\u5806 max heap\u300d\uff1a\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\geq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\u3002</li> <li>\u300c\u5c0f\u9876\u5806 min heap\u300d\uff1a\u4efb\u610f\u8282\u70b9\u7684\u503c \\(\\leq\\) \u5176\u5b50\u8282\u70b9\u7684\u503c\u3002</li> </ul> <p></p> <p> \u56fe 8-1 \u00a0 \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\u3002</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\u201c\u5806\u9876\u201d\uff0c\u5c06\u5e95\u5c42\u6700\u9760\u53f3\u7684\u8282\u70b9\u79f0\u4e3a\u201c\u5806\u5e95\u201d\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\u201c\u4f18\u5148\u961f\u5217\u201d\u548c\u201c\u5806\u201d\u770b\u4f5c\u7b49\u4ef7\u7684\u6570\u636e\u7ed3\u6784\u3002\u56e0\u6b64\uff0c\u672c\u4e66\u5bf9\u4e24\u8005\u4e0d\u505a\u7279\u522b\u533a\u5206\uff0c\u7edf\u4e00\u4f7f\u7528\u201c\u5806\u201c\u6765\u547d\u540d\u3002</p> <p>\u5806\u7684\u5e38\u7528\u64cd\u4f5c\u89c1\u8868 8-1 \uff0c\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002</p> <p> \u8868 8-1 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.cpp<pre><code>/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\npriority_queue<int, vector<int>, greater<int>> minHeap;\n// \u521d\u59cb\u5316\u5927\u9876\u5806\npriority_queue<int, vector<int>, less<int>> maxHeap;\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.top(); // 5\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nmaxHeap.pop(); // 5\nmaxHeap.pop(); // 4\nmaxHeap.pop(); // 3\nmaxHeap.pop(); // 2\nmaxHeap.pop(); // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.empty();\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nvector<int> input{1, 3, 2, 5, 4};\npriority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());\n</code></pre> heap.java<pre><code>/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nQueue<Integer> minHeap = new PriorityQueue<>();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.peek(); // 5\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.poll(); // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = maxHeap.isEmpty();\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\n</code></pre> heap.cs<pre><code>/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nPriorityQueue<int, int> minHeap = new PriorityQueue<int, int>();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nPriorityQueue<int, int> maxHeap = new PriorityQueue<int, int>(Comparer<int>.Create((x, y) => y - x));\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.Enqueue(1, 1);\nmaxHeap.Enqueue(3, 3);\nmaxHeap.Enqueue(2, 2);\nmaxHeap.Enqueue(5, 5);\nmaxHeap.Enqueue(4, 4);\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.Peek();//5\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.Dequeue(); // 5\npeek = maxHeap.Dequeue(); // 4\npeek = maxHeap.Dequeue(); // 3\npeek = maxHeap.Dequeue(); // 2\npeek = maxHeap.Dequeue(); // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.Count;\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.Count == 0;\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<int, int>(new List<(int, int)> { (1, 1), (3, 3), (2, 2), (5, 5), (4, 4), });\n</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) > (*h)[j].(int)\n}\n// Swap sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Swap(i, j int) {\n(*h)[i], (*h)[j] = (*h)[j], (*h)[i]\n}\n// Top \u83b7\u53d6\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Top() any {\nreturn (*h)[0]\n}\n/* Driver Code */\nfunc TestHeap(t *testing.T) {\n/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5927\u9876\u5806\nmaxHeap := &intHeap{}\nheap.Init(maxHeap)\n/* \u5143\u7d20\u5165\u5806 */\n// \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u6dfb\u52a0\u5143\u7d20\nheap.Push(maxHeap, 1)\nheap.Push(maxHeap, 3)\nheap.Push(maxHeap, 2)\nheap.Push(maxHeap, 4)\nheap.Push(maxHeap, 5)\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\ntop := maxHeap.Top()\nfmt.Printf(\"\u5806\u9876\u5143\u7d20\u4e3a %d\\n\", top)\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u79fb\u9664\u5143\u7d20\nheap.Pop(maxHeap) // 5\nheap.Pop(maxHeap) // 4\nheap.Pop(maxHeap) // 3\nheap.Pop(maxHeap) // 2\nheap.Pop(maxHeap) // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nsize := len(*maxHeap)\nfmt.Printf(\"\u5806\u5143\u7d20\u6570\u91cf\u4e3a %d\\n\", size)\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nisEmpty := len(*maxHeap) == 0\nfmt.Printf(\"\u5806\u662f\u5426\u4e3a\u7a7a %t\\n\", isEmpty)\n}\n</code></pre> heap.swift<pre><code>// Swift \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\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.dart<pre><code>// Dart \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n</code></pre> heap.rs<pre><code>use std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nlet mut min_heap = BinaryHeap::<Reverse<i32>>::new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\nlet mut max_heap = BinaryHeap::new();\n/* \u5143\u7d20\u5165\u5806 */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nlet peek = max_heap.peek().unwrap(); // 5\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nlet peek = max_heap.pop().unwrap(); // 5\nlet peek = max_heap.pop().unwrap(); // 4\nlet peek = max_heap.pop().unwrap(); // 3\nlet peek = max_heap.pop().unwrap(); // 2\nlet peek = max_heap.pop().unwrap(); // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nlet size = max_heap.len();\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = max_heap.is_empty();\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n</code></pre> heap.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n</code></pre> heap.zig<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":"1. \u00a0 \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>\u5982\u56fe 8-2 \u6240\u793a\uff0c\u7ed9\u5b9a\u7d22\u5f15 \\(i\\) \uff0c\u5176\u5de6\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \uff0c\u53f3\u5b50\u8282\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \uff0c\u7236\u8282\u70b9\u7d22\u5f15\u4e3a \\((i - 1) / 2\\)\uff08\u5411\u4e0b\u53d6\u6574\uff09\u3002\u5f53\u7d22\u5f15\u8d8a\u754c\u65f6\uff0c\u8868\u793a\u7a7a\u8282\u70b9\u6216\u8282\u70b9\u4e0d\u5b58\u5728\u3002</p> <p></p> <p> \u56fe 8-2 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.py<pre><code>[class]{MaxHeap}-[func]{left}\n[class]{MaxHeap}-[func]{right}\n[class]{MaxHeap}-[func]{parent}\n</code></pre> my_heap.cpp<pre><code>[class]{MaxHeap}-[func]{left}\n[class]{MaxHeap}-[func]{right}\n[class]{MaxHeap}-[func]{parent}\n</code></pre> my_heap.java<pre><code>[class]{MaxHeap}-[func]{left}\n[class]{MaxHeap}-[func]{right}\n[class]{MaxHeap}-[func]{parent}\n</code></pre> my_heap.cs<pre><code>[class]{MaxHeap}-[func]{left}\n[class]{MaxHeap}-[func]{right}\n[class]{MaxHeap}-[func]{parent}\n</code></pre> my_heap.go<pre><code>[class]{maxHeap}-[func]{left}\n[class]{maxHeap}-[func]{right}\n[class]{maxHeap}-[func]{parent}\n</code></pre> my_heap.swift<pre><code>[class]{MaxHeap}-[func]{left}\n[class]{MaxHeap}-[func]{right}\n[class]{MaxHeap}-[func]{parent}\n</code></pre> my_heap.js<pre><code>[class]{MaxHeap}-[func]{#left}\n[class]{MaxHeap}-[func]{#right}\n[class]{MaxHeap}-[func]{#parent}\n</code></pre> my_heap.ts<pre><code>[class]{MaxHeap}-[func]{left}\n[class]{MaxHeap}-[func]{right}\n[class]{MaxHeap}-[func]{parent}\n</code></pre> my_heap.dart<pre><code>[class]{MaxHeap}-[func]{_left}\n[class]{MaxHeap}-[func]{_right}\n[class]{MaxHeap}-[func]{_parent}\n</code></pre> my_heap.rs<pre><code>[class]{MaxHeap}-[func]{left}\n[class]{MaxHeap}-[func]{right}\n[class]{MaxHeap}-[func]{parent}\n</code></pre> my_heap.c<pre><code>[class]{maxHeap}-[func]{left}\n[class]{maxHeap}-[func]{right}\n[class]{maxHeap}-[func]{parent}\n</code></pre> my_heap.zig<pre><code>[class]{MaxHeap}-[func]{left}\n[class]{MaxHeap}-[func]{right}\n[class]{MaxHeap}-[func]{parent}\n</code></pre>"},{"location":"chapter_heap/heap/#2","title":"2. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.py<pre><code>[class]{MaxHeap}-[func]{peek}\n</code></pre> my_heap.cpp<pre><code>[class]{MaxHeap}-[func]{peek}\n</code></pre> my_heap.java<pre><code>[class]{MaxHeap}-[func]{peek}\n</code></pre> my_heap.cs<pre><code>[class]{MaxHeap}-[func]{peek}\n</code></pre> my_heap.go<pre><code>[class]{maxHeap}-[func]{peek}\n</code></pre> my_heap.swift<pre><code>[class]{MaxHeap}-[func]{peek}\n</code></pre> my_heap.js<pre><code>[class]{MaxHeap}-[func]{peek}\n</code></pre> my_heap.ts<pre><code>[class]{MaxHeap}-[func]{peek}\n</code></pre> my_heap.dart<pre><code>[class]{MaxHeap}-[func]{peek}\n</code></pre> my_heap.rs<pre><code>[class]{MaxHeap}-[func]{peek}\n</code></pre> my_heap.c<pre><code>[class]{maxHeap}-[func]{peek}\n</code></pre> my_heap.zig<pre><code>[class]{MaxHeap}-[func]{peek}\n</code></pre>"},{"location":"chapter_heap/heap/#3","title":"3. \u00a0 \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\u5982\u56fe 8-3 \u6240\u793a\uff0c\u6211\u4eec\u6bd4\u8f83\u63d2\u5165\u8282\u70b9\u4e0e\u5176\u7236\u8282\u70b9\u7684\u503c\uff0c\u5982\u679c\u63d2\u5165\u8282\u70b9\u66f4\u5927\uff0c\u5219\u5c06\u5b83\u4eec\u4ea4\u6362\u3002\u7136\u540e\u7ee7\u7eed\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u4ece\u5e95\u81f3\u9876\u4fee\u590d\u5806\u4e2d\u7684\u5404\u4e2a\u8282\u70b9\uff0c\u76f4\u81f3\u8d8a\u8fc7\u6839\u8282\u70b9\u6216\u9047\u5230\u65e0\u987b\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002</p> <1><2><3><4><5><6><7><8><9> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 8-3 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.py<pre><code>[class]{MaxHeap}-[func]{push}\n[class]{MaxHeap}-[func]{sift_up}\n</code></pre> my_heap.cpp<pre><code>[class]{MaxHeap}-[func]{push}\n[class]{MaxHeap}-[func]{siftUp}\n</code></pre> my_heap.java<pre><code>[class]{MaxHeap}-[func]{push}\n[class]{MaxHeap}-[func]{siftUp}\n</code></pre> my_heap.cs<pre><code>[class]{MaxHeap}-[func]{push}\n[class]{MaxHeap}-[func]{siftUp}\n</code></pre> my_heap.go<pre><code>[class]{maxHeap}-[func]{push}\n[class]{maxHeap}-[func]{siftUp}\n</code></pre> my_heap.swift<pre><code>[class]{MaxHeap}-[func]{push}\n[class]{MaxHeap}-[func]{siftUp}\n</code></pre> my_heap.js<pre><code>[class]{MaxHeap}-[func]{push}\n[class]{MaxHeap}-[func]{#siftUp}\n</code></pre> my_heap.ts<pre><code>[class]{MaxHeap}-[func]{push}\n[class]{MaxHeap}-[func]{siftUp}\n</code></pre> my_heap.dart<pre><code>[class]{MaxHeap}-[func]{push}\n[class]{MaxHeap}-[func]{siftUp}\n</code></pre> my_heap.rs<pre><code>[class]{MaxHeap}-[func]{push}\n[class]{MaxHeap}-[func]{sift_up}\n</code></pre> my_heap.c<pre><code>[class]{maxHeap}-[func]{push}\n[class]{maxHeap}-[func]{siftUp}\n</code></pre> my_heap.zig<pre><code>[class]{MaxHeap}-[func]{push}\n[class]{MaxHeap}-[func]{siftUp}\n</code></pre>"},{"location":"chapter_heap/heap/#4","title":"4. \u00a0 \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\u7528\u4ee5\u4e0b\u64cd\u4f5c\u6b65\u9aa4\u3002</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>\u5982\u56fe 8-4 \u6240\u793a\uff0c\u201c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u201d\u7684\u64cd\u4f5c\u65b9\u5411\u4e0e\u201c\u4ece\u5e95\u81f3\u9876\u5806\u5316\u201d\u76f8\u53cd\uff0c\u6211\u4eec\u5c06\u6839\u8282\u70b9\u7684\u503c\u4e0e\u5176\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u5c06\u6700\u5927\u7684\u5b50\u8282\u70b9\u4e0e\u6839\u8282\u70b9\u4ea4\u6362\u3002\u7136\u540e\u5faa\u73af\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u8282\u70b9\u6216\u9047\u5230\u65e0\u987b\u4ea4\u6362\u7684\u8282\u70b9\u65f6\u7ed3\u675f\u3002</p> <1><2><3><4><5><6><7><8><9><10> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 8-4 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig my_heap.py<pre><code>[class]{MaxHeap}-[func]{pop}\n[class]{MaxHeap}-[func]{sift_down}\n</code></pre> my_heap.cpp<pre><code>[class]{MaxHeap}-[func]{pop}\n[class]{MaxHeap}-[func]{siftDown}\n</code></pre> my_heap.java<pre><code>[class]{MaxHeap}-[func]{pop}\n[class]{MaxHeap}-[func]{siftDown}\n</code></pre> my_heap.cs<pre><code>[class]{MaxHeap}-[func]{pop}\n[class]{MaxHeap}-[func]{siftDown}\n</code></pre> my_heap.go<pre><code>[class]{maxHeap}-[func]{pop}\n[class]{maxHeap}-[func]{siftDown}\n</code></pre> my_heap.swift<pre><code>[class]{MaxHeap}-[func]{pop}\n[class]{MaxHeap}-[func]{siftDown}\n</code></pre> my_heap.js<pre><code>[class]{MaxHeap}-[func]{pop}\n[class]{MaxHeap}-[func]{#siftDown}\n</code></pre> my_heap.ts<pre><code>[class]{MaxHeap}-[func]{pop}\n[class]{MaxHeap}-[func]{siftDown}\n</code></pre> my_heap.dart<pre><code>[class]{MaxHeap}-[func]{pop}\n[class]{MaxHeap}-[func]{siftDown}\n</code></pre> my_heap.rs<pre><code>[class]{MaxHeap}-[func]{pop}\n[class]{MaxHeap}-[func]{sift_down}\n</code></pre> my_heap.c<pre><code>[class]{maxHeap}-[func]{pop}\n[class]{maxHeap}-[func]{siftDown}\n</code></pre> my_heap.zig<pre><code>[class]{MaxHeap}-[func]{pop}\n[class]{MaxHeap}-[func]{siftDown}\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":""},{"location":"chapter_heap/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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/#2-q-a","title":"2. \u00a0 Q & 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\u56fe 8-6 \u6240\u793a\u7684 \\(k\\) \u8f6e\u904d\u5386\uff0c\u5206\u522b\u5728\u6bcf\u8f6e\u4e2d\u63d0\u53d6\u7b2c \\(1\\)\u3001\\(2\\)\u3001\\(\\dots\\)\u3001\\(k\\) \u5927\u7684\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(nk)\\) \u3002</p> <p>\u6b64\u65b9\u6cd5\u53ea\u9002\u7528\u4e8e \\(k \\ll n\\) \u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u5f53 \\(k\\) \u4e0e \\(n\\) \u6bd4\u8f83\u63a5\u8fd1\u65f6\uff0c\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u8d8b\u5411\u4e8e \\(O(n^2)\\) \uff0c\u975e\u5e38\u8017\u65f6\u3002</p> <p></p> <p> \u56fe 8-6 \u00a0 \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\u5b8c\u6574\u7684\u6709\u5e8f\u5e8f\u5217\uff0c\u6b64\u65f6\u7b49\u4ef7\u4e8e\u201c\u9009\u62e9\u6392\u5e8f\u201d\u7b97\u6cd5\u3002</p>"},{"location":"chapter_heap/top_k/#832","title":"8.3.2 \u00a0 \u65b9\u6cd5\u4e8c\uff1a\u6392\u5e8f","text":"<p>\u5982\u56fe 8-7 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u5bf9\u6570\u7ec4 <code>nums</code> \u8fdb\u884c\u6392\u5e8f\uff0c\u518d\u8fd4\u56de\u6700\u53f3\u8fb9\u7684 \\(k\\) \u4e2a\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002</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 8-7 \u00a0 \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\u56fe 8-8 \u6240\u793a\u3002</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> <1><2><3><4><5><6><7><8><9> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 8-8 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig top_k.py<pre><code>[class]{}-[func]{top_k_heap}\n</code></pre> top_k.cpp<pre><code>[class]{}-[func]{topKHeap}\n</code></pre> top_k.java<pre><code>[class]{top_k}-[func]{topKHeap}\n</code></pre> top_k.cs<pre><code>[class]{top_k}-[func]{topKHeap}\n</code></pre> top_k.go<pre><code>[class]{}-[func]{topKHeap}\n</code></pre> top_k.swift<pre><code>[class]{}-[func]{topKHeap}\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.dart<pre><code>[class]{}-[func]{topKHeap}\n</code></pre> top_k.rs<pre><code>[class]{}-[func]{top_k_heap}\n</code></pre> top_k.c<pre><code>[class]{}-[func]{topKHeap}\n</code></pre> top_k.zig<pre><code>[class]{}-[func]{topKHeap}\n</code></pre>"},{"location":"chapter_introduction/","title":"\u7b2c 1 \u7ae0 \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\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\u7684\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u67e5\u627e\u4e00\u4e2a\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u5b57\uff0c\u901a\u5e38\u4f1a\u6309\u7167\u56fe 1-1 \u6240\u793a\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002</p> <ol> <li>\u7ffb\u5f00\u5b57\u5178\u7ea6\u4e00\u534a\u7684\u9875\u6570\uff0c\u67e5\u770b\u8be5\u9875\u7684\u9996\u5b57\u6bcd\u662f\u4ec0\u4e48\uff0c\u5047\u8bbe\u9996\u5b57\u6bcd\u4e3a \\(m\\) \u3002</li> <li>\u7531\u4e8e\u5728\u62fc\u97f3\u5b57\u6bcd\u8868\u4e2d \\(r\\) \u4f4d\u4e8e \\(m\\) \u4e4b\u540e\uff0c\u6240\u4ee5\u6392\u9664\u5b57\u5178\u524d\u534a\u90e8\u5206\uff0c\u67e5\u627e\u8303\u56f4\u7f29\u5c0f\u5230\u540e\u534a\u90e8\u5206\u3002</li> <li>\u4e0d\u65ad\u91cd\u590d\u6b65\u9aa4 <code>1.</code> \u548c \u6b65\u9aa4 <code>2.</code> \uff0c\u76f4\u81f3\u627e\u5230\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u9875\u7801\u4e3a\u6b62\u3002</li> </ol> <1><2><3><4><5> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 1-1 \u00a0 \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\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u3002\u4ece\u6570\u636e\u7ed3\u6784\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u5b57\u5178\u89c6\u4e3a\u4e00\u4e2a\u5df2\u6392\u5e8f\u7684\u201c\u6570\u7ec4\u201d\uff1b\u4ece\u7b97\u6cd5\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u67e5\u5b57\u5178\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u770b\u4f5c\u662f\u201c\u4e8c\u5206\u67e5\u627e\u201d\u3002</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\u56fe 1-2 \u6240\u793a\u3002</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 1-2 \u00a0 \u6251\u514b\u6392\u5e8f\u6b65\u9aa4 </p> <p>\u4e0a\u8ff0\u6574\u7406\u6251\u514b\u724c\u7684\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u201c\u63d2\u5165\u6392\u5e8f\u201d\u7b97\u6cd5\uff0c\u5b83\u5728\u5904\u7406\u5c0f\u578b\u6570\u636e\u96c6\u65f6\u975e\u5e38\u9ad8\u6548\u3002\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u7684\u6392\u5e8f\u5e93\u51fd\u6570\u4e2d\u90fd\u5b58\u5728\u63d2\u5165\u6392\u5e8f\u7684\u8eab\u5f71\u3002</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\u4e86\u6536\u94f6\u5458 \\(100\\) \u5143\uff0c\u5219\u6536\u94f6\u5458\u9700\u8981\u627e\u6211\u4eec \\(31\\) \u5143\u3002\u4ed6\u4f1a\u5f88\u81ea\u7136\u5730\u5b8c\u6210\u5982\u56fe 1-3 \u6240\u793a\u7684\u601d\u8003\u3002</p> <ol> <li>\u53ef\u9009\u9879\u662f\u6bd4 \\(31\\) \u5143\u9762\u503c\u66f4\u5c0f\u7684\u8d27\u5e01\uff0c\u5305\u62ec \\(1\\) \u5143\u3001\\(5\\) \u5143\u3001\\(10\\) \u5143\u3001\\(20\\) \u5143\u3002</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 1-3 \u00a0 \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\u201c\u8d2a\u5fc3\u201d\u7b97\u6cd5\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>\u5982\u679c\u4f60\u5bf9\u6570\u636e\u7ed3\u6784\u3001\u7b97\u6cd5\u3001\u6570\u7ec4\u548c\u4e8c\u5206\u67e5\u627e\u7b49\u6982\u5ff5\u4ecd\u611f\u5230\u4e00\u77e5\u534a\u89e3\uff0c\u8bf7\u7ee7\u7eed\u5f80\u4e0b\u9605\u8bfb\uff0c\u8fd9\u672c\u4e66\u5c06\u5f15\u5bfc\u4f60\u8fc8\u5165\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u77e5\u8bc6\u6bbf\u5802\u3002</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\u7b97\u6cd5\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\u7b97\u6cd5\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\u7684\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>\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7c7b\u6bd4\u4e3a\u62fc\u88c5\u79ef\u6728\uff0c\u79ef\u6728\u4ee3\u8868\u6570\u636e\uff0c\u79ef\u6728\u7684\u5f62\u72b6\u548c\u8fde\u63a5\u65b9\u5f0f\u4ee3\u8868\u6570\u636e\u7ed3\u6784\uff0c\u62fc\u88c5\u79ef\u6728\u7684\u6b65\u9aa4\u5219\u5bf9\u5e94\u7b97\u6cd5\u3002</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\uff0c\u5b83\u5177\u6709\u4ee5\u4e0b\u7279\u6027\u3002</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\uff0c\u5177\u6709\u4ee5\u4e0b\u8bbe\u8ba1\u76ee\u6807\u3002</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\u3002\u4e0b\u9762\u4e3e\u4e24\u4e2a\u4f8b\u5b50\u3002</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>\u5982\u56fe 1-4 \u6240\u793a\uff0c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u9ad8\u5ea6\u76f8\u5173\u3001\u7d27\u5bc6\u7ed3\u5408\uff0c\u5177\u4f53\u8868\u73b0\u4ee5\u4e0b\u4e09\u4e2a\u65b9\u9762\u3002</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\u4f5c\u7528\u7684\u821e\u53f0\u3002\u6570\u636e\u7ed3\u6784\u672c\u8eab\u4ec5\u5b58\u50a8\u6570\u636e\u4fe1\u606f\uff0c\u7ed3\u5408\u7b97\u6cd5\u624d\u80fd\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u3002</li> <li>\u7b97\u6cd5\u901a\u5e38\u53ef\u4ee5\u57fa\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u5b9e\u73b0\uff0c\u4f46\u6267\u884c\u6548\u7387\u53ef\u80fd\u76f8\u5dee\u5f88\u5927\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u662f\u5173\u952e\u3002</li> </ul> <p></p> <p> \u56fe 1-4 \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb </p> <p>\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u72b9\u5982\u56fe 1-5 \u6240\u793a\u7684\u62fc\u88c5\u79ef\u6728\u3002\u4e00\u5957\u79ef\u6728\uff0c\u9664\u4e86\u5305\u542b\u8bb8\u591a\u96f6\u4ef6\u4e4b\u5916\uff0c\u8fd8\u9644\u6709\u8be6\u7ec6\u7684\u7ec4\u88c5\u8bf4\u660e\u4e66\u3002\u6211\u4eec\u6309\u7167\u8bf4\u660e\u4e66\u4e00\u6b65\u6b65\u64cd\u4f5c\uff0c\u5c31\u80fd\u7ec4\u88c5\u51fa\u7cbe\u7f8e\u7684\u79ef\u6728\u6a21\u578b\u3002</p> <p></p> <p> \u56fe 1-5 \u00a0 \u62fc\u88c5\u79ef\u6728 </p> <p>\u4e24\u8005\u7684\u8be6\u7ec6\u5bf9\u5e94\u5173\u7cfb\u5982\u8868 1-1 \u6240\u793a\u3002</p> <p> \u8868 1-1 \u00a0 \u5c06\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7c7b\u6bd4\u4e3a\u79ef\u6728 </p> \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5 \u62fc\u88c5\u79ef\u6728 \u8f93\u5165\u6570\u636e \u672a\u62fc\u88c5\u7684\u79ef\u6728 \u6570\u636e\u7ed3\u6784 \u79ef\u6728\u7ec4\u7ec7\u5f62\u5f0f\uff0c\u5305\u62ec\u5f62\u72b6\u3001\u5927\u5c0f\u3001\u8fde\u63a5\u65b9\u5f0f\u7b49 \u7b97\u6cd5 \u628a\u79ef\u6728\u62fc\u6210\u76ee\u6807\u5f62\u6001\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u6b65\u9aa4 \u8f93\u51fa\u6570\u636e \u79ef\u6728\u6a21\u578b <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\u201c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u201d\u7b80\u79f0\u4e3a\u201c\u7b97\u6cd5\u201d\u3002\u6bd4\u5982\u4f17\u6240\u5468\u77e5\u7684 LeetCode \u7b97\u6cd5\u9898\u76ee\uff0c\u5b9e\u9645\u4e0a\u540c\u65f6\u8003\u5bdf\u4e86\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e24\u65b9\u9762\u7684\u77e5\u8bc6\u3002</p>"},{"location":"chapter_preface/","title":"\u7b2c 0 \u7ae0 \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\u3001C++\u3001Python\u3001Go\u3001JS\u3001TS\u3001C#\u3001Swift\u3001Rust\u3001Dart\u3001Zig \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\u5982\u56fe 0-1 \u6240\u793a\u3002</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\u54c8\u5e0c\u8868\u3001\u6811\u3001\u5806\u3001\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u7684\u5b9a\u4e49\u3001\u4f18\u7f3a\u70b9\u3001\u5e38\u7528\u64cd\u4f5c\u3001\u5e38\u89c1\u7c7b\u578b\u3001\u5178\u578b\u5e94\u7528\u3001\u5b9e\u73b0\u65b9\u6cd5\u7b49\u3002</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 0-1 \u00a0 Hello \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\u4ed6\u5f00\u53d1\u7684\u5f00\u6e90\u6587\u6863\u4e3b\u9898 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>\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>\u91cd\u8981\u4e13\u6709\u540d\u8bcd\u53ca\u5176\u82f1\u6587\u7ffb\u8bd1\u4f1a\u7528 <code>\u300c \u300d</code> \u62ec\u53f7\u6807\u6ce8\uff0c\u4f8b\u5982 <code>\u300c\u6570\u7ec4 array\u300d</code> \u3002\u5efa\u8bae\u8bb0\u4f4f\u5b83\u4eec\uff0c\u4ee5\u4fbf\u9605\u8bfb\u6587\u732e\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>\u5f53\u6d89\u53ca\u5230\u7f16\u7a0b\u8bed\u8a00\u4e4b\u95f4\u4e0d\u4e00\u81f4\u7684\u540d\u8bcd\u65f6\uff0c\u672c\u4e66\u5747\u4ee5 Python \u4e3a\u51c6\uff0c\u4f8b\u5982\u4f7f\u7528 \\(\\text{None}\\) \u6765\u8868\u793a\u201c\u7a7a\u201d\u3002</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> PythonC++JavaC#GoSwiftJSTSDartRustCZig <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/**\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>"},{"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>\u5982\u679c\u4f60\u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u53d1\u73b0\u67d0\u6bb5\u5185\u5bb9\u63d0\u4f9b\u4e86\u56fe 0-2 \u6240\u793a\u7684\u52a8\u753b\u6216\u56fe\u89e3\uff0c\u8bf7\u4ee5\u56fe\u4e3a\u4e3b\u3001\u4ee5\u6587\u5b57\u4e3a\u8f85\uff0c\u7efc\u5408\u4e24\u8005\u6765\u7406\u89e3\u5185\u5bb9\u3002</p> <p></p> <p> \u56fe 0-2 \u00a0 \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\u5982\u56fe 0-3 \u6240\u793a\uff0c\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 0-3 \u00a0 \u8fd0\u884c\u4ee3\u7801\u793a\u4f8b </p> <p>\u8fd0\u884c\u4ee3\u7801\u7684\u524d\u7f6e\u5de5\u4f5c\u4e3b\u8981\u5206\u4e3a\u4e09\u6b65\u3002</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\u514b\u9686\u6216\u4e0b\u8f7d\u4ee3\u7801\u4ed3\u3002\u5982\u679c\u5df2\u7ecf\u5b89\u88c5 Git \uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u514b\u9686\u672c\u4ed3\u5e93\u3002</p> <pre><code>git clone https://github.com/krahets/hello-algo.git\n</code></pre> <p>\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728\u56fe 0-4 \u6240\u793a\u7684\u4f4d\u7f6e\uff0c\u70b9\u51fb\u201cDownload ZIP\u201d\u76f4\u63a5\u4e0b\u8f7d\u4ee3\u7801\u538b\u7f29\u5305\uff0c\u7136\u540e\u5728\u672c\u5730\u89e3\u538b\u5373\u53ef\u3002</p> <p></p> <p> \u56fe 0-4 \u00a0 \u514b\u9686\u4ed3\u5e93\u4e0e\u4e0b\u8f7d\u4ee3\u7801 </p> <p>\u7b2c\u4e09\u6b65\uff1a\u8fd0\u884c\u6e90\u4ee3\u7801\u3002\u5982\u56fe 0-5 \u6240\u793a\uff0c\u5bf9\u4e8e\u9876\u90e8\u6807\u6709\u6587\u4ef6\u540d\u79f0\u7684\u4ee3\u7801\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4ed3\u5e93\u7684 <code>codes</code> \u6587\u4ef6\u5939\u5185\u627e\u5230\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u3002\u6e90\u4ee3\u7801\u6587\u4ef6\u53ef\u4e00\u952e\u8fd0\u884c\uff0c\u5c06\u5e2e\u52a9\u4f60\u8282\u7701\u4e0d\u5fc5\u8981\u7684\u8c03\u8bd5\u65f6\u95f4\uff0c\u8ba9\u4f60\u80fd\u591f\u4e13\u6ce8\u4e8e\u5b66\u4e60\u5185\u5bb9\u3002</p> <p></p> <p> \u56fe 0-5 \u00a0 \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>\u5728\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u8bf7\u4e0d\u8981\u8f7b\u6613\u8df3\u8fc7\u90a3\u4e9b\u6ca1\u5b66\u660e\u767d\u7684\u77e5\u8bc6\u70b9\u3002\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u63d0\u51fa\u4f60\u7684\u95ee\u9898\uff0c\u6211\u548c\u5c0f\u4f19\u4f34\u4eec\u5c06\u7aed\u8bda\u4e3a\u4f60\u89e3\u7b54\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u5728\u4e24\u5929\u5185\u56de\u590d\u3002</p> <p>\u5982\u56fe 0-6 \u6240\u793a\uff0c\u6bcf\u7bc7\u6587\u7ae0\u7684\u5e95\u90e8\u90fd\u914d\u6709\u8bc4\u8bba\u533a\u3002\u5e0c\u671b\u4f60\u80fd\u591a\u5173\u6ce8\u8bc4\u8bba\u533a\u7684\u5185\u5bb9\u3002\u4e00\u65b9\u9762\uff0c\u4f60\u53ef\u4ee5\u4e86\u89e3\u5927\u5bb6\u9047\u5230\u7684\u95ee\u9898\uff0c\u4ece\u800c\u67e5\u6f0f\u8865\u7f3a\uff0c\u6fc0\u53d1\u66f4\u6df1\u5165\u7684\u601d\u8003\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u671f\u5f85\u4f60\u80fd\u6177\u6168\u5730\u56de\u7b54\u5176\u4ed6\u5c0f\u4f19\u4f34\u7684\u95ee\u9898\uff0c\u5206\u4eab\u60a8\u7684\u89c1\u89e3\uff0c\u5e2e\u52a9\u4ed6\u4eba\u8fdb\u6b65\u3002</p> <p></p> <p> \u56fe 0-6 \u00a0 \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\u3002</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>\u5982\u56fe 0-7 \u6240\u793a\uff0c\u672c\u4e66\u5185\u5bb9\u4e3b\u8981\u6db5\u76d6\u201c\u7b2c\u4e00\u9636\u6bb5\u201d\uff0c\u65e8\u5728\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5c55\u5f00\u7b2c\u4e8c\u548c\u7b2c\u4e09\u9636\u6bb5\u7684\u5b66\u4e60\u3002</p> <p></p> <p> \u56fe 0-7 \u00a0 \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":"\u7b2c 10 \u7ae0 \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\u7b56\u7565\u7684\u9ad8\u6548\u641c\u7d22\u7b97\u6cd5\u3002\u5b83\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u6bcf\u8f6e\u51cf\u5c11\u4e00\u534a\u641c\u7d22\u8303\u56f4\uff0c\u76f4\u81f3\u627e\u5230\u76ee\u6807\u5143\u7d20\u6216\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u4e3a\u6b62\u3002</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 10-1 \u00a0 \u4e8c\u5206\u67e5\u627e\u793a\u4f8b\u6570\u636e </p> <p>\u5982\u56fe 10-2 \u6240\u793a\uff0c\u6211\u4eec\u5148\u521d\u59cb\u5316\u6307\u9488 \\(i = 0\\) \u548c \\(j = n - 1\\) \uff0c\u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u548c\u5c3e\u5143\u7d20\uff0c\u4ee3\u8868\u641c\u7d22\u533a\u95f4 \\([0, n - 1]\\) \u3002\u8bf7\u6ce8\u610f\uff0c\u4e2d\u62ec\u53f7\u8868\u793a\u95ed\u533a\u95f4\uff0c\u5176\u5305\u542b\u8fb9\u754c\u503c\u672c\u8eab\u3002</p> <p>\u63a5\u4e0b\u6765\uff0c\u5faa\u73af\u6267\u884c\u4ee5\u4e0b\u4e24\u6b65\u3002</p> <ol> <li>\u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m = \\lfloor {(i + j) / 2} \\rfloor\\) \uff0c\u5176\u4e2d \\(\\lfloor \\: \\rfloor\\) \u8868\u793a\u5411\u4e0b\u53d6\u6574\u64cd\u4f5c\u3002</li> <li>\u5224\u65ad <code>nums[m]</code> \u548c <code>target</code> \u7684\u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4ee5\u4e0b\u4e09\u79cd\u60c5\u51b5\u3002<ol> <li>\u5f53 <code>nums[m] < 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] > 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> <1><2><3><4><5><6><7> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 10-2 \u00a0 \u4e8c\u5206\u67e5\u627e\u6d41\u7a0b </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> PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search.py<pre><code>[class]{}-[func]{binary_search}\n</code></pre> binary_search.cpp<pre><code>[class]{}-[func]{binarySearch}\n</code></pre> binary_search.java<pre><code>[class]{binary_search}-[func]{binarySearch}\n</code></pre> binary_search.cs<pre><code>[class]{binary_search}-[func]{binarySearch}\n</code></pre> binary_search.go<pre><code>[class]{}-[func]{binarySearch}\n</code></pre> binary_search.swift<pre><code>[class]{}-[func]{binarySearch}\n</code></pre> binary_search.js<pre><code>[class]{}-[func]{binarySearch}\n</code></pre> binary_search.ts<pre><code>[class]{}-[func]{binarySearch}\n</code></pre> binary_search.dart<pre><code>[class]{}-[func]{binarySearch}\n</code></pre> binary_search.rs<pre><code>[class]{}-[func]{binary_search}\n</code></pre> binary_search.c<pre><code>[class]{}-[func]{binarySearch}\n</code></pre> binary_search.zig<pre><code>[class]{}-[func]{binarySearch}\n</code></pre> <p>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(\\log n)\\) \uff1a\u5728\u4e8c\u5206\u5faa\u73af\u4e2d\uff0c\u533a\u95f4\u6bcf\u8f6e\u7f29\u5c0f\u4e00\u534a\uff0c\u5faa\u73af\u6b21\u6570\u4e3a \\(\\log_2 n\\) \u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \uff1a\u6307\u9488 \\(i\\) \u548c \\(j\\) \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search.py<pre><code>[class]{}-[func]{binary_search_lcro}\n</code></pre> binary_search.cpp<pre><code>[class]{}-[func]{binarySearchLCRO}\n</code></pre> binary_search.java<pre><code>[class]{binary_search}-[func]{binarySearchLCRO}\n</code></pre> binary_search.cs<pre><code>[class]{binary_search}-[func]{binarySearchLCRO}\n</code></pre> binary_search.go<pre><code>[class]{}-[func]{binarySearchLCRO}\n</code></pre> binary_search.swift<pre><code>[class]{}-[func]{binarySearchLCRO}\n</code></pre> binary_search.js<pre><code>[class]{}-[func]{binarySearchLCRO}\n</code></pre> binary_search.ts<pre><code>[class]{}-[func]{binarySearchLCRO}\n</code></pre> binary_search.dart<pre><code>[class]{}-[func]{binarySearchLCRO}\n</code></pre> binary_search.rs<pre><code>[class]{}-[func]{binary_search_lcro}\n</code></pre> binary_search.c<pre><code>[class]{}-[func]{binarySearchLCRO}\n</code></pre> binary_search.zig<pre><code>[class]{}-[func]{binarySearchLCRO}\n</code></pre> <p>\u5982\u56fe 10-3 \u6240\u793a\uff0c\u5728\u4e24\u79cd\u533a\u95f4\u8868\u793a\u4e0b\uff0c\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u521d\u59cb\u5316\u3001\u5faa\u73af\u6761\u4ef6\u548c\u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\u7686\u6709\u6240\u4e0d\u540c\u3002</p> <p>\u7531\u4e8e\u201c\u53cc\u95ed\u533a\u95f4\u201d\u8868\u793a\u4e2d\u7684\u5de6\u53f3\u8fb9\u754c\u90fd\u88ab\u5b9a\u4e49\u4e3a\u95ed\u533a\u95f4\uff0c\u56e0\u6b64\u6307\u9488 \\(i\\) \u548c \\(j\\) \u7f29\u5c0f\u533a\u95f4\u64cd\u4f5c\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u8fd9\u6837\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\uff0c\u56e0\u6b64\u4e00\u822c\u5efa\u8bae\u91c7\u7528\u201c\u53cc\u95ed\u533a\u95f4\u201d\u7684\u5199\u6cd5\u3002</p> <p></p> <p> \u56fe 10-3 \u00a0 \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\u3002</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\u987b\u989d\u5916\u7a7a\u95f4\u3002\u76f8\u8f83\u4e8e\u9700\u8981\u501f\u52a9\u989d\u5916\u7a7a\u95f4\u7684\u641c\u7d22\u7b97\u6cd5\uff08\u4f8b\u5982\u54c8\u5e0c\u67e5\u627e\uff09\uff0c\u4e8c\u5206\u67e5\u627e\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\u3002</li> </ul> <p>\u7136\u800c\uff0c\u4e8c\u5206\u67e5\u627e\u5e76\u975e\u9002\u7528\u4e8e\u6240\u6709\u60c5\u51b5\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002</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\u8fd9\u79cd\u60c5\u51b5\u53ef\u80fd\u5bfc\u81f4\u4ee5\u4e0b\u4e24\u79cd\u7ed3\u679c\u3002</p> <ul> <li>\u63d2\u5165\u70b9\u7684\u7d22\u5f15 \\(i\\) \u8d8a\u754c\u3002</li> <li>\u5143\u7d20 <code>nums[i]</code> \u4e0e <code>target</code> \u4e0d\u76f8\u7b49\u3002</li> </ul> <p>\u5f53\u9047\u5230\u4ee5\u4e0a\u4e24\u79cd\u60c5\u51b5\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de \\(-1\\) \u5373\u53ef\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_edge.py<pre><code>[class]{}-[func]{binary_search_left_edge}\n</code></pre> binary_search_edge.cpp<pre><code>[class]{}-[func]{binarySearchLeftEdge}\n</code></pre> binary_search_edge.java<pre><code>[class]{binary_search_edge}-[func]{binarySearchLeftEdge}\n</code></pre> binary_search_edge.cs<pre><code>[class]{binary_search_edge}-[func]{binarySearchLeftEdge}\n</code></pre> binary_search_edge.go<pre><code>[class]{}-[func]{binarySearchLeftEdge}\n</code></pre> binary_search_edge.swift<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.dart<pre><code>[class]{}-[func]{binarySearchLeftEdge}\n</code></pre> binary_search_edge.rs<pre><code>[class]{}-[func]{binary_search_left_edge}\n</code></pre> binary_search_edge.c<pre><code>[class]{}-[func]{binarySearchLeftEdge}\n</code></pre> binary_search_edge.zig<pre><code>[class]{}-[func]{binarySearchLeftEdge}\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":"1. \u00a0 \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>\u5982\u56fe 10-7 \u6240\u793a\uff0c\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 10-7 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_edge.py<pre><code>[class]{}-[func]{binary_search_right_edge}\n</code></pre> binary_search_edge.cpp<pre><code>[class]{}-[func]{binarySearchRightEdge}\n</code></pre> binary_search_edge.java<pre><code>[class]{binary_search_edge}-[func]{binarySearchRightEdge}\n</code></pre> binary_search_edge.cs<pre><code>[class]{binary_search_edge}-[func]{binarySearchRightEdge}\n</code></pre> binary_search_edge.go<pre><code>[class]{}-[func]{binarySearchRightEdge}\n</code></pre> binary_search_edge.swift<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.dart<pre><code>[class]{}-[func]{binarySearchRightEdge}\n</code></pre> binary_search_edge.rs<pre><code>[class]{}-[func]{binary_search_right_edge}\n</code></pre> binary_search_edge.c<pre><code>[class]{}-[func]{binarySearchRightEdge}\n</code></pre> binary_search_edge.zig<pre><code>[class]{}-[func]{binarySearchRightEdge}\n</code></pre>"},{"location":"chapter_searching/binary_search_edge/#2","title":"2. \u00a0 \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\u7ec8 \\(i\\) \u548c \\(j\\) \u4f1a\u5206\u522b\u6307\u5411\u9996\u4e2a\u5927\u4e8e\u3001\u5c0f\u4e8e <code>target</code> \u7684\u5143\u7d20\u3002</p> <p>\u56e0\u6b64\uff0c\u5982\u56fe 10-8 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u7684\u5143\u7d20\uff0c\u7528\u4e8e\u67e5\u627e\u5de6\u53f3\u8fb9\u754c\u3002</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 10-8 \u00a0 \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\u4ee5\u4e0b\u4e24\u70b9\u3002</p> <ul> <li>\u7ed9\u5b9a\u6570\u7ec4\u4e0d\u5305\u542b\u5c0f\u6570\uff0c\u8fd9\u610f\u5473\u7740\u6211\u4eec\u65e0\u987b\u5173\u5fc3\u5982\u4f55\u5904\u7406\u76f8\u7b49\u7684\u60c5\u51b5\u3002</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 10-4 \u00a0 \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] < 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> PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_insertion.py<pre><code>[class]{}-[func]{binary_search_insertion_simple}\n</code></pre> binary_search_insertion.cpp<pre><code>[class]{}-[func]{binarySearchInsertionSimple}\n</code></pre> binary_search_insertion.java<pre><code>[class]{binary_search_insertion}-[func]{binarySearchInsertionSimple}\n</code></pre> binary_search_insertion.cs<pre><code>[class]{binary_search_insertion}-[func]{binarySearchInsertionSimple}\n</code></pre> binary_search_insertion.go<pre><code>[class]{}-[func]{binarySearchInsertionSimple}\n</code></pre> binary_search_insertion.swift<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.dart<pre><code>[class]{}-[func]{binarySearchInsertionSimple}\n</code></pre> binary_search_insertion.rs<pre><code>[class]{}-[func]{binary_search_insertion}\n</code></pre> binary_search_insertion.c<pre><code>[class]{}-[func]{binarySearchInsertionSimple}\n</code></pre> binary_search_insertion.zig<pre><code>[class]{}-[func]{binarySearchInsertionSimple}\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\u56fe 10-5 \u6240\u793a\u7684\u6b65\u9aa4\u5b9e\u73b0\u3002</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 10-5 \u00a0 \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\u62d3\u5c55\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\u3002\u5982\u56fe 10-6 \u6240\u793a\uff0c\u6574\u4f53\u6d41\u7a0b\u4fdd\u6301\u4e0d\u53d8\uff0c\u6bcf\u8f6e\u5148\u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 \\(m\\) \uff0c\u518d\u5224\u65ad <code>target</code> \u548c <code>nums[m]</code> \u5927\u5c0f\u5173\u7cfb\uff0c\u5206\u4e3a\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\u3002</p> <ul> <li>\u5f53 <code>nums[m] < target</code> \u6216 <code>nums[m] > 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> </ul> <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> <1><2><3><4><5><6><7><8> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 10-6 \u00a0 \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] > 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> PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_insertion.py<pre><code>[class]{}-[func]{binary_search_insertion}\n</code></pre> binary_search_insertion.cpp<pre><code>[class]{}-[func]{binarySearchInsertion}\n</code></pre> binary_search_insertion.java<pre><code>[class]{binary_search_insertion}-[func]{binarySearchInsertion}\n</code></pre> binary_search_insertion.cs<pre><code>[class]{binary_search_insertion}-[func]{binarySearchInsertion}\n</code></pre> binary_search_insertion.go<pre><code>[class]{}-[func]{binarySearchInsertion}\n</code></pre> binary_search_insertion.swift<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.dart<pre><code>[class]{}-[func]{binarySearchInsertion}\n</code></pre> binary_search_insertion.rs<pre><code>[class]{}-[func]{binary_search_insertion}\n</code></pre> binary_search_insertion.c<pre><code>[class]{}-[func]{binarySearchInsertion}\n</code></pre> binary_search_insertion.zig<pre><code>[class]{}-[func]{binarySearchInsertion}\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\\) \u548c \\(j\\) \u5206\u522b\u8bbe\u5b9a\u641c\u7d22\u76ee\u6807\uff0c\u76ee\u6807\u53ef\u80fd\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u5143\u7d20\uff08\u4f8b\u5982 <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\\) \u548c \\(j\\) \u90fd\u9010\u6e10\u903c\u8fd1\u9884\u5148\u8bbe\u5b9a\u7684\u76ee\u6807\u3002\u6700\u7ec8\uff0c\u5b83\u4eec\u6216\u662f\u6210\u529f\u627e\u5230\u7b54\u6848\uff0c\u6216\u662f\u8d8a\u8fc7\u8fb9\u754c\u540e\u505c\u6b62\u3002</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\u5982\u56fe 10-9 \u6240\u793a\uff0c\u6211\u4eec\u5f00\u542f\u4e00\u4e2a\u4e24\u5c42\u5faa\u73af\uff0c\u5728\u6bcf\u8f6e\u4e2d\u5224\u65ad\u4e24\u4e2a\u6574\u6570\u7684\u548c\u662f\u5426\u4e3a <code>target</code> \uff0c\u82e5\u662f\u5219\u8fd4\u56de\u5b83\u4eec\u7684\u7d22\u5f15\u3002</p> <p></p> <p> \u56fe 10-9 \u00a0 \u7ebf\u6027\u67e5\u627e\u6c42\u89e3\u4e24\u6570\u4e4b\u548c </p> PythonC++JavaC#GoSwiftJSTSDartRustCZig two_sum.py<pre><code>[class]{}-[func]{two_sum_brute_force}\n</code></pre> two_sum.cpp<pre><code>[class]{}-[func]{twoSumBruteForce}\n</code></pre> two_sum.java<pre><code>[class]{two_sum}-[func]{twoSumBruteForce}\n</code></pre> two_sum.cs<pre><code>[class]{two_sum}-[func]{twoSumBruteForce}\n</code></pre> two_sum.go<pre><code>[class]{}-[func]{twoSumBruteForce}\n</code></pre> two_sum.swift<pre><code>[class]{}-[func]{twoSumBruteForce}\n</code></pre> two_sum.js<pre><code>[class]{}-[func]{twoSumBruteForce}\n</code></pre> two_sum.ts<pre><code>[class]{}-[func]{twoSumBruteForce}\n</code></pre> two_sum.dart<pre><code>[class]{}-[func]{twoSumBruteForce}\n</code></pre> two_sum.rs<pre><code>[class]{}-[func]{two_sum_brute_force}\n</code></pre> two_sum.c<pre><code>[class]{}-[func]{twoSumBruteForce}\n</code></pre> two_sum.zig<pre><code>[class]{}-[func]{twoSumBruteForce}\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\u56fe 10-10 \u6240\u793a\u7684\u6b65\u9aa4\u3002</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> <1><2><3> <p></p> <p></p> <p></p> <p> \u56fe 10-10 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig two_sum.py<pre><code>[class]{}-[func]{two_sum_hash_table}\n</code></pre> two_sum.cpp<pre><code>[class]{}-[func]{twoSumHashTable}\n</code></pre> two_sum.java<pre><code>[class]{two_sum}-[func]{twoSumHashTable}\n</code></pre> two_sum.cs<pre><code>[class]{two_sum}-[func]{twoSumHashTable}\n</code></pre> two_sum.go<pre><code>[class]{}-[func]{twoSumHashTable}\n</code></pre> two_sum.swift<pre><code>[class]{}-[func]{twoSumHashTable}\n</code></pre> two_sum.js<pre><code>[class]{}-[func]{twoSumHashTable}\n</code></pre> two_sum.ts<pre><code>[class]{}-[func]{twoSumHashTable}\n</code></pre> two_sum.dart<pre><code>[class]{}-[func]{twoSumHashTable}\n</code></pre> two_sum.rs<pre><code>[class]{}-[func]{two_sum_hash_table}\n</code></pre> two_sum.c<pre><code>[class]{hashTable}-[func]{}\n[class]{}-[func]{twoSumHashTable}\n</code></pre> two_sum.zig<pre><code>[class]{}-[func]{twoSumHashTable}\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>\u641c\u7d22\u7b97\u6cd5\u53ef\u6839\u636e\u5b9e\u73b0\u601d\u8def\u5206\u4e3a\u4ee5\u4e0b\u4e24\u7c7b\u3002</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>\u201c\u7ebf\u6027\u641c\u7d22\u201d\u9002\u7528\u4e8e\u6570\u7ec4\u548c\u94fe\u8868\u7b49\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002\u5b83\u4ece\u6570\u636e\u7ed3\u6784\u7684\u4e00\u7aef\u5f00\u59cb\uff0c\u9010\u4e2a\u8bbf\u95ee\u5143\u7d20\uff0c\u76f4\u5230\u627e\u5230\u76ee\u6807\u5143\u7d20\u6216\u5230\u8fbe\u53e6\u4e00\u7aef\u4ecd\u6ca1\u6709\u627e\u5230\u76ee\u6807\u5143\u7d20\u4e3a\u6b62\u3002</li> <li>\u201c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u201d\u548c\u201c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u201d\u662f\u56fe\u548c\u6811\u7684\u4e24\u79cd\u904d\u5386\u7b56\u7565\u3002\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u4ece\u521d\u59cb\u8282\u70b9\u5f00\u59cb\u9010\u5c42\u641c\u7d22\uff0c\u7531\u8fd1\u53ca\u8fdc\u5730\u8bbf\u95ee\u5404\u4e2a\u8282\u70b9\u3002\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u662f\u4ece\u521d\u59cb\u8282\u70b9\u5f00\u59cb\uff0c\u6cbf\u7740\u4e00\u6761\u8def\u5f84\u8d70\u5230\u5934\u4e3a\u6b62\uff0c\u518d\u56de\u6eaf\u5e76\u5c1d\u8bd5\u5176\u4ed6\u8def\u5f84\uff0c\u76f4\u5230\u904d\u5386\u5b8c\u6574\u4e2a\u6570\u636e\u7ed3\u6784\u3002</li> </ul> <p>\u66b4\u529b\u641c\u7d22\u7684\u4f18\u70b9\u662f\u7b80\u5355\u4e14\u901a\u7528\u6027\u597d\uff0c\u65e0\u987b\u5bf9\u6570\u636e\u505a\u9884\u5904\u7406\u548c\u501f\u52a9\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\u3002</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>\u201c\u4e8c\u5206\u67e5\u627e\u201d\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\u5b9e\u73b0\u9ad8\u6548\u67e5\u627e\uff0c\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u3002</li> <li>\u201c\u54c8\u5e0c\u67e5\u627e\u201d\u5229\u7528\u54c8\u5e0c\u8868\u5c06\u641c\u7d22\u6570\u636e\u548c\u76ee\u6807\u6570\u636e\u5efa\u7acb\u4e3a\u952e\u503c\u5bf9\u6620\u5c04\uff0c\u4ece\u800c\u5b9e\u73b0\u67e5\u8be2\u64cd\u4f5c\u3002</li> <li>\u201c\u6811\u67e5\u627e\u201d\u5728\u7279\u5b9a\u7684\u6811\u7ed3\u6784\uff08\u4f8b\u5982\u4e8c\u53c9\u641c\u7d22\u6811\uff09\u4e2d\uff0c\u57fa\u4e8e\u6bd4\u8f83\u8282\u70b9\u503c\u6765\u5feb\u901f\u6392\u9664\u8282\u70b9\uff0c\u4ece\u800c\u5b9a\u4f4d\u76ee\u6807\u5143\u7d20\u3002</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\u56fe 10-11 \u6240\u793a\u3002</p> <p></p> <p> \u56fe 10-11 \u00a0 \u591a\u79cd\u641c\u7d22\u7b56\u7565 </p> <p>\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\u7684\u64cd\u4f5c\u6548\u7387\u4e0e\u7279\u6027\u5982\u8868 10-1 \u6240\u793a\u3002</p> <p> \u8868 10-1 \u00a0 \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>\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\u987b\u4efb\u4f55\u6570\u636e\u9884\u5904\u7406\u64cd\u4f5c\u3002\u5047\u5982\u6211\u4eec\u4ec5\u9700\u67e5\u8be2\u4e00\u6b21\u6570\u636e\uff0c\u90a3\u4e48\u5176\u4ed6\u4e09\u79cd\u65b9\u6cd5\u7684\u6570\u636e\u9884\u5904\u7406\u7684\u65f6\u95f4\u6bd4\u7ebf\u6027\u641c\u7d22\u7684\u65f6\u95f4\u8fd8\u8981\u66f4\u957f\u3002</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\u5206\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\u987b\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\u987b\u8303\u56f4\u67e5\u8be2\u7684\u6570\u636e\uff1b\u6811\u67e5\u627e\u9002\u7528\u4e8e\u9700\u8981\u7ef4\u62a4\u987a\u5e8f\u548c\u652f\u6301\u8303\u56f4\u67e5\u8be2\u7684\u5927\u578b\u52a8\u6001\u6570\u636e\u3002</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":"\u7b2c 11 \u7ae0 \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>\u5982\u56fe 11-4 \u6240\u793a\uff0c\u5192\u6ce1\u8fc7\u7a0b\u53ef\u4ee5\u5229\u7528\u5143\u7d20\u4ea4\u6362\u64cd\u4f5c\u6765\u6a21\u62df\uff1a\u4ece\u6570\u7ec4\u6700\u5de6\u7aef\u5f00\u59cb\u5411\u53f3\u904d\u5386\uff0c\u4f9d\u6b21\u6bd4\u8f83\u76f8\u90bb\u5143\u7d20\u5927\u5c0f\uff0c\u5982\u679c\u201c\u5de6\u5143\u7d20 > \u53f3\u5143\u7d20\u201d\u5c31\u4ea4\u6362\u5b83\u4fe9\u3002\u904d\u5386\u5b8c\u6210\u540e\uff0c\u6700\u5927\u7684\u5143\u7d20\u4f1a\u88ab\u79fb\u52a8\u5230\u6570\u7ec4\u7684\u6700\u53f3\u7aef\u3002</p> <1><2><3><4><5><6><7> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 11-4 \u00a0 \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\u5982\u56fe 11-5 \u6240\u793a\u3002</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\u987b\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002</li> </ol> <p></p> <p> \u56fe 11-5 \u00a0 \u5192\u6ce1\u6392\u5e8f\u6d41\u7a0b </p> PythonC++JavaC#GoSwiftJSTSDartRustCZig bubble_sort.py<pre><code>[class]{}-[func]{bubble_sort}\n</code></pre> bubble_sort.cpp<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> bubble_sort.java<pre><code>[class]{bubble_sort}-[func]{bubbleSort}\n</code></pre> bubble_sort.cs<pre><code>[class]{bubble_sort}-[func]{bubbleSort}\n</code></pre> bubble_sort.go<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> bubble_sort.swift<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> bubble_sort.js<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> bubble_sort.ts<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> bubble_sort.dart<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> bubble_sort.rs<pre><code>[class]{}-[func]{bubble_sort}\n</code></pre> bubble_sort.c<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> bubble_sort.zig<pre><code>[class]{}-[func]{bubbleSort}\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> PythonC++JavaC#GoSwiftJSTSDartRustCZig bubble_sort.py<pre><code>[class]{}-[func]{bubble_sort_with_flag}\n</code></pre> bubble_sort.cpp<pre><code>[class]{}-[func]{bubbleSortWithFlag}\n</code></pre> bubble_sort.java<pre><code>[class]{bubble_sort}-[func]{bubbleSortWithFlag}\n</code></pre> bubble_sort.cs<pre><code>[class]{bubble_sort}-[func]{bubbleSortWithFlag}\n</code></pre> bubble_sort.go<pre><code>[class]{}-[func]{bubbleSortWithFlag}\n</code></pre> bubble_sort.swift<pre><code>[class]{}-[func]{bubbleSortWithFlag}\n</code></pre> bubble_sort.js<pre><code>[class]{}-[func]{bubbleSortWithFlag}\n</code></pre> bubble_sort.ts<pre><code>[class]{}-[func]{bubbleSortWithFlag}\n</code></pre> bubble_sort.dart<pre><code>[class]{}-[func]{bubbleSortWithFlag}\n</code></pre> bubble_sort.rs<pre><code>[class]{}-[func]{bubble_sort_with_flag}\n</code></pre> bubble_sort.c<pre><code>[class]{}-[func]{bubbleSortWithFlag}\n</code></pre> bubble_sort.zig<pre><code>[class]{}-[func]{bubbleSortWithFlag}\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\\)\u3001\\(n - 2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u603b\u548c\u4e3a \\((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\\) \u548c \\(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\u7b56\u7565\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u3002\u5b83\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e9b\u5177\u6709\u5927\u5c0f\u987a\u5e8f\u7684\u6876\uff0c\u6bcf\u4e2a\u6876\u5bf9\u5e94\u4e00\u4e2a\u6570\u636e\u8303\u56f4\uff0c\u5c06\u6570\u636e\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\uff1b\u7136\u540e\uff0c\u5728\u6bcf\u4e2a\u6876\u5185\u90e8\u5206\u522b\u6267\u884c\u6392\u5e8f\uff1b\u6700\u7ec8\u6309\u7167\u6876\u7684\u987a\u5e8f\u5c06\u6240\u6709\u6570\u636e\u5408\u5e76\u3002</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\u56fe 11-13 \u6240\u793a\u3002</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 11-13 \u00a0 \u6876\u6392\u5e8f\u7b97\u6cd5\u6d41\u7a0b </p> PythonC++JavaC#GoSwiftJSTSDartRustCZig bucket_sort.py<pre><code>[class]{}-[func]{bucket_sort}\n</code></pre> bucket_sort.cpp<pre><code>[class]{}-[func]{bucketSort}\n</code></pre> bucket_sort.java<pre><code>[class]{bucket_sort}-[func]{bucketSort}\n</code></pre> bucket_sort.cs<pre><code>[class]{bucket_sort}-[func]{bucketSort}\n</code></pre> bucket_sort.go<pre><code>[class]{}-[func]{bucketSort}\n</code></pre> bucket_sort.swift<pre><code>[class]{}-[func]{bucketSort}\n</code></pre> bucket_sort.js<pre><code>[class]{}-[func]{bucketSort}\n</code></pre> bucket_sort.ts<pre><code>[class]{}-[func]{bucketSort}\n</code></pre> bucket_sort.dart<pre><code>[class]{}-[func]{bucketSort}\n</code></pre> bucket_sort.rs<pre><code>[class]{}-[func]{bucket_sort}\n</code></pre> bucket_sort.c<pre><code>[class]{}-[func]{bucketSort}\n</code></pre> bucket_sort.zig<pre><code>[class]{}-[func]{bucketSort}\n</code></pre>"},{"location":"chapter_sorting/bucket_sort/#1182","title":"11.8.2 \u00a0 \u7b97\u6cd5\u7279\u6027","text":"<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> <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</p> <p>\u5982\u56fe 11-14 \u6240\u793a\uff0c\u8fd9\u79cd\u65b9\u6cd5\u672c\u8d28\u4e0a\u662f\u521b\u5efa\u4e00\u4e2a\u9012\u5f52\u6811\uff0c\u76ee\u6807\u662f\u8ba9\u53f6\u8282\u70b9\u7684\u503c\u5c3d\u53ef\u80fd\u5e73\u5747\u3002\u5f53\u7136\uff0c\u4e0d\u4e00\u5b9a\u8981\u6bcf\u8f6e\u5c06\u6570\u636e\u5212\u5206\u4e3a 3 \u4e2a\u6876\uff0c\u5177\u4f53\u5212\u5206\u65b9\u5f0f\u53ef\u6839\u636e\u6570\u636e\u7279\u70b9\u7075\u6d3b\u9009\u62e9\u3002</p> <p></p> <p> \u56fe 11-14 \u00a0 \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</p> <p>\u5982\u56fe 11-15 \u6240\u793a\uff0c\u6211\u4eec\u5047\u8bbe\u5546\u54c1\u4ef7\u683c\u670d\u4ece\u6b63\u6001\u5206\u5e03\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5408\u7406\u5730\u8bbe\u5b9a\u4ef7\u683c\u533a\u95f4\uff0c\u4ece\u800c\u5c06\u5546\u54c1\u5e73\u5747\u5206\u914d\u5230\u5404\u4e2a\u6876\u4e2d\u3002</p> <p></p> <p> \u56fe 11-15 \u00a0 \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\uff0c\u8ba1\u6570\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-16 \u6240\u793a\u3002</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 11-16 \u00a0 \u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b </p> PythonC++JavaC#GoSwiftJSTSDartRustCZig counting_sort.py<pre><code>[class]{}-[func]{counting_sort_naive}\n</code></pre> counting_sort.cpp<pre><code>[class]{}-[func]{countingSortNaive}\n</code></pre> counting_sort.java<pre><code>[class]{counting_sort}-[func]{countingSortNaive}\n</code></pre> counting_sort.cs<pre><code>[class]{counting_sort}-[func]{countingSortNaive}\n</code></pre> counting_sort.go<pre><code>[class]{}-[func]{countingSortNaive}\n</code></pre> counting_sort.swift<pre><code>[class]{}-[func]{countingSortNaive}\n</code></pre> counting_sort.js<pre><code>[class]{}-[func]{countingSortNaive}\n</code></pre> counting_sort.ts<pre><code>[class]{}-[func]{countingSortNaive}\n</code></pre> counting_sort.dart<pre><code>[class]{}-[func]{countingSortNaive}\n</code></pre> counting_sort.rs<pre><code>[class]{}-[func]{counting_sort_naive}\n</code></pre> counting_sort.c<pre><code>[class]{}-[func]{countingSortNaive}\n</code></pre> counting_sort.zig<pre><code>[class]{}-[func]{countingSortNaive}\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\u5047\u8bbe\u8f93\u5165\u6570\u636e\u662f\u5546\u54c1\u5bf9\u8c61\uff0c\u6211\u4eec\u60f3\u8981\u6309\u7167\u5546\u54c1\u4ef7\u683c\uff08\u7c7b\u7684\u6210\u5458\u53d8\u91cf\uff09\u5bf9\u5546\u54c1\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u4e0a\u8ff0\u7b97\u6cd5\u53ea\u80fd\u7ed9\u51fa\u4ef7\u683c\u7684\u6392\u5e8f\u7ed3\u679c\u3002</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\u201c\u524d\u7f00\u548c\u201d\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\uff1a</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\u4ee5\u4e0b\u4e24\u6b65\u3002</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\u56fe 11-17 \u5c55\u793a\u4e86\u5b8c\u6574\u7684\u8ba1\u6570\u6392\u5e8f\u6d41\u7a0b\u3002</p> <1><2><3><4><5><6><7><8> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 11-17 \u00a0 \u8ba1\u6570\u6392\u5e8f\u6b65\u9aa4 </p> <p>\u8ba1\u6570\u6392\u5e8f\u7684\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig counting_sort.py<pre><code>[class]{}-[func]{counting_sort}\n</code></pre> counting_sort.cpp<pre><code>[class]{}-[func]{countingSort}\n</code></pre> counting_sort.java<pre><code>[class]{counting_sort}-[func]{countingSort}\n</code></pre> counting_sort.cs<pre><code>[class]{counting_sort}-[func]{countingSort}\n</code></pre> counting_sort.go<pre><code>[class]{}-[func]{countingSort}\n</code></pre> counting_sort.swift<pre><code>[class]{}-[func]{countingSort}\n</code></pre> counting_sort.js<pre><code>[class]{}-[func]{countingSort}\n</code></pre> counting_sort.ts<pre><code>[class]{}-[func]{countingSort}\n</code></pre> counting_sort.dart<pre><code>[class]{}-[func]{countingSort}\n</code></pre> counting_sort.rs<pre><code>[class]{}-[func]{counting_sort}\n</code></pre> counting_sort.c<pre><code>[class]{}-[func]{countingSort}\n</code></pre> counting_sort.zig<pre><code>[class]{}-[func]{countingSort}\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\u201c\u5806\u201c\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\u3002</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\u56fe 11-12 \u6240\u793a\u3002</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>Tip</p> <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> <1><2><3><4><5><6><7><8><9><10><11><12> <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 11-12 \u00a0 \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 <code>sift_down()</code> \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 <code>sift_down()</code> \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig heap_sort.py<pre><code>[class]{}-[func]{sift_down}\n[class]{}-[func]{heap_sort}\n</code></pre> heap_sort.cpp<pre><code>[class]{}-[func]{siftDown}\n[class]{}-[func]{heapSort}\n</code></pre> heap_sort.java<pre><code>[class]{heap_sort}-[func]{siftDown}\n[class]{heap_sort}-[func]{heapSort}\n</code></pre> heap_sort.cs<pre><code>[class]{heap_sort}-[func]{siftDown}\n[class]{heap_sort}-[func]{heapSort}\n</code></pre> heap_sort.go<pre><code>[class]{}-[func]{siftDown}\n[class]{}-[func]{heapSort}\n</code></pre> heap_sort.swift<pre><code>[class]{}-[func]{siftDown}\n[class]{}-[func]{heapSort}\n</code></pre> heap_sort.js<pre><code>[class]{}-[func]{siftDown}\n[class]{}-[func]{heapSort}\n</code></pre> heap_sort.ts<pre><code>[class]{}-[func]{siftDown}\n[class]{}-[func]{heapSort}\n</code></pre> heap_sort.dart<pre><code>[class]{}-[func]{siftDown}\n[class]{}-[func]{heapSort}\n</code></pre> heap_sort.rs<pre><code>[class]{}-[func]{sift_down}\n[class]{}-[func]{heap_sort}\n</code></pre> heap_sort.c<pre><code>[class]{}-[func]{siftDown}\n[class]{}-[func]{heapSort}\n</code></pre> heap_sort.zig<pre><code>[class]{}-[func]{siftDown}\n[class]{}-[func]{heapSort}\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>\u56fe 11-6 \u5c55\u793a\u4e86\u6570\u7ec4\u63d2\u5165\u5143\u7d20\u7684\u64cd\u4f5c\u6d41\u7a0b\u3002\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 11-6 \u00a0 \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\u56fe 11-7 \u6240\u793a\u3002</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 11-7 \u00a0 \u63d2\u5165\u6392\u5e8f\u6d41\u7a0b </p> PythonC++JavaC#GoSwiftJSTSDartRustCZig insertion_sort.py<pre><code>[class]{}-[func]{insertion_sort}\n</code></pre> insertion_sort.cpp<pre><code>[class]{}-[func]{insertionSort}\n</code></pre> insertion_sort.java<pre><code>[class]{insertion_sort}-[func]{insertionSort}\n</code></pre> insertion_sort.cs<pre><code>[class]{insertion_sort}-[func]{insertionSort}\n</code></pre> insertion_sort.go<pre><code>[class]{}-[func]{insertionSort}\n</code></pre> insertion_sort.swift<pre><code>[class]{}-[func]{insertionSort}\n</code></pre> insertion_sort.js<pre><code>[class]{}-[func]{insertionSort}\n</code></pre> insertion_sort.ts<pre><code>[class]{}-[func]{insertionSort}\n</code></pre> insertion_sort.dart<pre><code>[class]{}-[func]{insertionSort}\n</code></pre> insertion_sort.rs<pre><code>[class]{}-[func]{insertion_sort}\n</code></pre> insertion_sort.c<pre><code>[class]{}-[func]{insertionSort}\n</code></pre> insertion_sort.zig<pre><code>[class]{}-[func]{insertionSort}\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\\)\u3001\\(n-2\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \u6b21\uff0c\u6c42\u548c\u5f97\u5230 \\((n - 1) n / 2\\) \uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002\u5728\u9047\u5230\u6709\u5e8f\u6570\u636e\u65f6\uff0c\u63d2\u5165\u64cd\u4f5c\u4f1a\u63d0\u524d\u7ec8\u6b62\u3002\u5f53\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u6709\u5e8f\u65f6\uff0c\u63d2\u5165\u6392\u5e8f\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002</li> <li>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\)\u3001\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488 \\(i\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002</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\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\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002</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\u662f\u4e00\u79cd\u57fa\u4e8e\u5206\u6cbb\u7b56\u7565\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u5305\u542b\u56fe 11-10 \u6240\u793a\u7684\u201c\u5212\u5206\u201d\u548c\u201c\u5408\u5e76\u201d\u9636\u6bb5\u3002</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 11-10 \u00a0 \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>\u5982\u56fe 11-11 \u6240\u793a\uff0c\u201c\u5212\u5206\u9636\u6bb5\u201d\u4ece\u9876\u81f3\u5e95\u9012\u5f52\u5730\u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5207\u5206\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\u3002</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> <1><2><3><4><5><6><7><8><9><10> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 11-11 \u00a0 \u5f52\u5e76\u6392\u5e8f\u6b65\u9aa4 </p> <p>\u89c2\u5bdf\u53d1\u73b0\uff0c\u5f52\u5e76\u6392\u5e8f\u4e0e\u4e8c\u53c9\u6811\u540e\u5e8f\u904d\u5386\u7684\u9012\u5f52\u987a\u5e8f\u662f\u4e00\u81f4\u7684\u3002</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> PythonC++JavaC#GoSwiftJSTSDartRustCZig merge_sort.py<pre><code>[class]{}-[func]{merge}\n[class]{}-[func]{merge_sort}\n</code></pre> merge_sort.cpp<pre><code>[class]{}-[func]{merge}\n[class]{}-[func]{mergeSort}\n</code></pre> merge_sort.java<pre><code>[class]{merge_sort}-[func]{merge}\n[class]{merge_sort}-[func]{mergeSort}\n</code></pre> merge_sort.cs<pre><code>[class]{merge_sort}-[func]{merge}\n[class]{merge_sort}-[func]{mergeSort}\n</code></pre> merge_sort.go<pre><code>[class]{}-[func]{merge}\n[class]{}-[func]{mergeSort}\n</code></pre> merge_sort.swift<pre><code>[class]{}-[func]{merge}\n[class]{}-[func]{mergeSort}\n</code></pre> merge_sort.js<pre><code>[class]{}-[func]{merge}\n[class]{}-[func]{mergeSort}\n</code></pre> merge_sort.ts<pre><code>[class]{}-[func]{merge}\n[class]{}-[func]{mergeSort}\n</code></pre> merge_sort.dart<pre><code>[class]{}-[func]{merge}\n[class]{}-[func]{mergeSort}\n</code></pre> merge_sort.rs<pre><code>[class]{}-[func]{merge}\n[class]{}-[func]{merge_sort}\n</code></pre> merge_sort.c<pre><code>[class]{}-[func]{merge}\n[class]{}-[func]{mergeSort}\n</code></pre> merge_sort.zig<pre><code>[class]{}-[func]{merge}\n[class]{}-[func]{mergeSort}\n</code></pre> <p>\u5b9e\u73b0\u5408\u5e76\u51fd\u6570 <code>merge()</code> \u5b58\u5728\u4ee5\u4e0b\u96be\u70b9\u3002</p> <ul> <li>\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 > leftEnd</code> \u548c <code>j > 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>\u5bf9\u4e8e\u94fe\u8868\uff0c\u5f52\u5e76\u6392\u5e8f\u76f8\u8f83\u4e8e\u5176\u4ed6\u6392\u5e8f\u7b97\u6cd5\u5177\u6709\u663e\u8457\u4f18\u52bf\uff0c\u53ef\u4ee5\u5c06\u94fe\u8868\u6392\u5e8f\u4efb\u52a1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(1)\\) \u3002</p> <ul> <li>\u5212\u5206\u9636\u6bb5\uff1a\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u201c\u8fed\u4ee3\u201d\u66ff\u4ee3\u201c\u9012\u5f52\u201d\u6765\u5b9e\u73b0\u94fe\u8868\u5212\u5206\u5de5\u4f5c\uff0c\u4ece\u800c\u7701\u53bb\u9012\u5f52\u4f7f\u7528\u7684\u6808\u5e27\u7a7a\u95f4\u3002</li> <li>\u5408\u5e76\u9636\u6bb5\uff1a\u5728\u94fe\u8868\u4e2d\uff0c\u8282\u70b9\u589e\u5220\u64cd\u4f5c\u4ec5\u9700\u6539\u53d8\u5f15\u7528\uff08\u6307\u9488\uff09\u5373\u53ef\u5b9e\u73b0\uff0c\u56e0\u6b64\u5408\u5e76\u9636\u6bb5\uff08\u5c06\u4e24\u4e2a\u77ed\u6709\u5e8f\u94fe\u8868\u5408\u5e76\u4e3a\u4e00\u4e2a\u957f\u6709\u5e8f\u94fe\u8868\uff09\u65e0\u987b\u521b\u5efa\u989d\u5916\u94fe\u8868\u3002</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\u7b56\u7565\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\u201c\u54e8\u5175\u5212\u5206\u201d\uff0c\u5176\u76ee\u6807\u662f\uff1a\u9009\u62e9\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u4f5c\u4e3a\u201c\u57fa\u51c6\u6570\u201d\uff0c\u5c06\u6240\u6709\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u5de6\u4fa7\uff0c\u800c\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u5230\u5176\u53f3\u4fa7\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u54e8\u5175\u5212\u5206\u7684\u6d41\u7a0b\u5982\u56fe 11-8 \u6240\u793a\u3002</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> <1><2><3><4><5><6><7><8><9> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 11-8 \u00a0 \u54e8\u5175\u5212\u5206\u6b65\u9aa4 </p> <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> <p>\u5feb\u901f\u6392\u5e8f\u7684\u5206\u6cbb\u7b56\u7565</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> PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.py<pre><code>[class]{QuickSort}-[func]{partition}\n</code></pre> quick_sort.cpp<pre><code>[class]{QuickSort}-[func]{swap}\n[class]{QuickSort}-[func]{partition}\n</code></pre> quick_sort.java<pre><code>[class]{QuickSort}-[func]{swap}\n[class]{QuickSort}-[func]{partition}\n</code></pre> quick_sort.cs<pre><code>[class]{QuickSort}-[func]{swap}\n[class]{QuickSort}-[func]{partition}\n</code></pre> quick_sort.go<pre><code>[class]{quickSort}-[func]{partition}\n</code></pre> quick_sort.swift<pre><code>[class]{}-[func]{swap}\n[class]{}-[func]{partition}\n</code></pre> quick_sort.js<pre><code>[class]{QuickSort}-[func]{swap}\n[class]{QuickSort}-[func]{partition}\n</code></pre> quick_sort.ts<pre><code>[class]{QuickSort}-[func]{swap}\n[class]{QuickSort}-[func]{partition}\n</code></pre> quick_sort.dart<pre><code>[class]{QuickSort}-[func]{_swap}\n[class]{QuickSort}-[func]{_partition}\n</code></pre> quick_sort.rs<pre><code>[class]{QuickSort}-[func]{partition}\n</code></pre> quick_sort.c<pre><code>[class]{}-[func]{swap}\n[class]{}-[func]{partition}\n</code></pre> quick_sort.zig<pre><code>[class]{QuickSort}-[func]{swap}\n[class]{QuickSort}-[func]{partition}\n</code></pre>"},{"location":"chapter_sorting/quick_sort/#1151","title":"11.5.1 \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u5feb\u901f\u6392\u5e8f\u7684\u6574\u4f53\u6d41\u7a0b\u5982\u56fe 11-9 \u6240\u793a\u3002</p> <ol> <li>\u9996\u5148\uff0c\u5bf9\u539f\u6570\u7ec4\u6267\u884c\u4e00\u6b21\u201c\u54e8\u5175\u5212\u5206\u201d\uff0c\u5f97\u5230\u672a\u6392\u5e8f\u7684\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u3002</li> <li>\u7136\u540e\uff0c\u5bf9\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5206\u522b\u9012\u5f52\u6267\u884c\u201c\u54e8\u5175\u5212\u5206\u201d\u3002</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></p> <p> \u56fe 11-9 \u00a0 \u5feb\u901f\u6392\u5e8f\u6d41\u7a0b </p> PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.py<pre><code>[class]{QuickSort}-[func]{quick_sort}\n</code></pre> quick_sort.cpp<pre><code>[class]{QuickSort}-[func]{quickSort}\n</code></pre> quick_sort.java<pre><code>[class]{QuickSort}-[func]{quickSort}\n</code></pre> quick_sort.cs<pre><code>[class]{QuickSort}-[func]{quickSort}\n</code></pre> quick_sort.go<pre><code>[class]{quickSort}-[func]{quickSort}\n</code></pre> quick_sort.swift<pre><code>[class]{}-[func]{quickSort}\n</code></pre> quick_sort.js<pre><code>[class]{QuickSort}-[func]{quickSort}\n</code></pre> quick_sort.ts<pre><code>[class]{QuickSort}-[func]{quickSort}\n</code></pre> quick_sort.dart<pre><code>[class]{QuickSort}-[func]{quickSort}\n</code></pre> quick_sort.rs<pre><code>[class]{QuickSort}-[func]{quick_sort}\n</code></pre> quick_sort.c<pre><code>[class]{}-[func]{quickSort}\n</code></pre> quick_sort.zig<pre><code>[class]{QuickSort}-[func]{quickSort}\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\u201c\u5f52\u5e76\u6392\u5e8f\u201d\u548c\u201c\u5806\u6392\u5e8f\u201d\u76f8\u540c\uff0c\u4f46\u901a\u5e38\u5feb\u901f\u6392\u5e8f\u7684\u6548\u7387\u66f4\u9ad8\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u539f\u56e0\u3002</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\u201c\u5806\u6392\u5e8f\u201d\u8fd9\u7c7b\u7b97\u6cd5\u9700\u8981\u8df3\u8dc3\u5f0f\u8bbf\u95ee\u5143\u7d20\uff0c\u4ece\u800c\u7f3a\u4e4f\u8fd9\u4e00\u7279\u6027\u3002</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\u201c\u63d2\u5165\u6392\u5e8f\u201d\u6bd4\u201c\u5192\u6ce1\u6392\u5e8f\u201d\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\u201c\u5192\u6ce1\u6392\u5e8f\u201d\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> PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.py<pre><code>[class]{QuickSortMedian}-[func]{median_three}\n[class]{QuickSortMedian}-[func]{partition}\n</code></pre> quick_sort.cpp<pre><code>[class]{QuickSortMedian}-[func]{medianThree}\n[class]{QuickSortMedian}-[func]{partition}\n</code></pre> quick_sort.java<pre><code>[class]{QuickSortMedian}-[func]{medianThree}\n[class]{QuickSortMedian}-[func]{partition}\n</code></pre> quick_sort.cs<pre><code>[class]{QuickSortMedian}-[func]{medianThree}\n[class]{QuickSortMedian}-[func]{partition}\n</code></pre> quick_sort.go<pre><code>[class]{quickSortMedian}-[func]{medianThree}\n[class]{quickSortMedian}-[func]{partition}\n</code></pre> quick_sort.swift<pre><code>[class]{}-[func]{medianThree}\n[class]{}-[func]{partitionMedian}\n</code></pre> quick_sort.js<pre><code>[class]{QuickSortMedian}-[func]{medianThree}\n[class]{QuickSortMedian}-[func]{partition}\n</code></pre> quick_sort.ts<pre><code>[class]{QuickSortMedian}-[func]{medianThree}\n[class]{QuickSortMedian}-[func]{partition}\n</code></pre> quick_sort.dart<pre><code>[class]{QuickSortMedian}-[func]{_medianThree}\n[class]{QuickSortMedian}-[func]{_partition}\n</code></pre> quick_sort.rs<pre><code>[class]{QuickSortMedian}-[func]{median_three}\n[class]{QuickSortMedian}-[func]{partition}\n</code></pre> quick_sort.c<pre><code>[class]{}-[func]{medianThree}\n[class]{}-[func]{partitionMedian}\n</code></pre> quick_sort.zig<pre><code>[class]{QuickSortMedian}-[func]{medianThree}\n[class]{QuickSortMedian}-[func]{partition}\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 \\(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> PythonC++JavaC#GoSwiftJSTSDartRustCZig quick_sort.py<pre><code>[class]{QuickSortTailCall}-[func]{quick_sort}\n</code></pre> quick_sort.cpp<pre><code>[class]{QuickSortTailCall}-[func]{quickSort}\n</code></pre> quick_sort.java<pre><code>[class]{QuickSortTailCall}-[func]{quickSort}\n</code></pre> quick_sort.cs<pre><code>[class]{QuickSortTailCall}-[func]{quickSort}\n</code></pre> quick_sort.go<pre><code>[class]{quickSortTailCall}-[func]{quickSort}\n</code></pre> quick_sort.swift<pre><code>[class]{}-[func]{quickSortTailCall}\n</code></pre> quick_sort.js<pre><code>[class]{QuickSortTailCall}-[func]{quickSort}\n</code></pre> quick_sort.ts<pre><code>[class]{QuickSortTailCall}-[func]{quickSort}\n</code></pre> quick_sort.dart<pre><code>[class]{QuickSortTailCall}-[func]{quickSort}\n</code></pre> quick_sort.rs<pre><code>[class]{QuickSortTailCall}-[func]{quick_sort}\n</code></pre> quick_sort.c<pre><code>[class]{}-[func]{quickSortTailCall}\n</code></pre> quick_sort.zig<pre><code>[class]{QuickSortTailCall}-[func]{quickSort}\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\u6d41\u7a0b\u5982\u56fe 11-18 \u6240\u793a\u3002</p> <ol> <li>\u521d\u59cb\u5316\u4f4d\u6570 \\(k = 1\\) \u3002</li> <li>\u5bf9\u5b66\u53f7\u7684\u7b2c \\(k\\) \u4f4d\u6267\u884c\u201c\u8ba1\u6570\u6392\u5e8f\u201d\u3002\u5b8c\u6210\u540e\uff0c\u6570\u636e\u4f1a\u6839\u636e\u7b2c \\(k\\) \u4f4d\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\u3002</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 11-18 \u00a0 \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 \\: 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> PythonC++JavaC#GoSwiftJSTSDartRustCZig radix_sort.py<pre><code>[class]{}-[func]{digit}\n[class]{}-[func]{counting_sort_digit}\n[class]{}-[func]{radix_sort}\n</code></pre> radix_sort.cpp<pre><code>[class]{}-[func]{digit}\n[class]{}-[func]{countingSortDigit}\n[class]{}-[func]{radixSort}\n</code></pre> radix_sort.java<pre><code>[class]{radix_sort}-[func]{digit}\n[class]{radix_sort}-[func]{countingSortDigit}\n[class]{radix_sort}-[func]{radixSort}\n</code></pre> radix_sort.cs<pre><code>[class]{radix_sort}-[func]{digit}\n[class]{radix_sort}-[func]{countingSortDigit}\n[class]{radix_sort}-[func]{radixSort}\n</code></pre> radix_sort.go<pre><code>[class]{}-[func]{digit}\n[class]{}-[func]{countingSortDigit}\n[class]{}-[func]{radixSort}\n</code></pre> radix_sort.swift<pre><code>[class]{}-[func]{digit}\n[class]{}-[func]{countingSortDigit}\n[class]{}-[func]{radixSort}\n</code></pre> radix_sort.js<pre><code>[class]{}-[func]{digit}\n[class]{}-[func]{countingSortDigit}\n[class]{}-[func]{radixSort}\n</code></pre> radix_sort.ts<pre><code>[class]{}-[func]{digit}\n[class]{}-[func]{countingSortDigit}\n[class]{}-[func]{radixSort}\n</code></pre> radix_sort.dart<pre><code>[class]{}-[func]{digit}\n[class]{}-[func]{countingSortDigit}\n[class]{}-[func]{radixSort}\n</code></pre> radix_sort.rs<pre><code>[class]{}-[func]{digit}\n[class]{}-[func]{counting_sort_digit}\n[class]{}-[func]{radix_sort}\n</code></pre> radix_sort.c<pre><code>[class]{}-[func]{digit}\n[class]{}-[func]{countingSortDigit}\n[class]{}-[func]{radixSort}\n</code></pre> radix_sort.zig<pre><code>[class]{}-[func]{digit}\n[class]{}-[func]{countingSortDigit}\n[class]{}-[func]{radixSort}\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 < b\\) \uff0c\u800c\u7b2c\u4e8c\u8f6e\u6392\u5e8f\u7ed3\u679c \\(a > b\\) \uff0c\u90a3\u4e48\u7b2c\u4e8c\u8f6e\u7684\u7ed3\u679c\u5c06\u53d6\u4ee3\u7b2c\u4e00\u8f6e\u7684\u7ed3\u679c\u3002\u7531\u4e8e\u6570\u5b57\u7684\u9ad8\u4f4d\u4f18\u5148\u7ea7\u9ad8\u4e8e\u4f4e\u4f4d\uff0c\u6211\u4eec\u5e94\u8be5\u5148\u6392\u5e8f\u4f4e\u4f4d\u518d\u6392\u5e8f\u9ad8\u4f4d\u3002</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\u56fe 11-2 \u6240\u793a\u3002</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\u987b\u6392\u5e8f\uff0c\u56e0\u6b64\u6570\u7ec4\u6392\u5e8f\u5b8c\u6210\u3002</li> </ol> <1><2><3><4><5><6><7><8><9><10><11> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 11-2 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig selection_sort.py<pre><code>[class]{}-[func]{selection_sort}\n</code></pre> selection_sort.cpp<pre><code>[class]{}-[func]{selectionSort}\n</code></pre> selection_sort.java<pre><code>[class]{selection_sort}-[func]{selectionSort}\n</code></pre> selection_sort.cs<pre><code>[class]{selection_sort}-[func]{selectionSort}\n</code></pre> selection_sort.go<pre><code>[class]{}-[func]{selectionSort}\n</code></pre> selection_sort.swift<pre><code>[class]{}-[func]{selectionSort}\n</code></pre> selection_sort.js<pre><code>[class]{}-[func]{selectionSort}\n</code></pre> selection_sort.ts<pre><code>[class]{}-[func]{selectionSort}\n</code></pre> selection_sort.dart<pre><code>[class]{}-[func]{selectionSort}\n</code></pre> selection_sort.rs<pre><code>[class]{}-[func]{selection_sort}\n</code></pre> selection_sort.c<pre><code>[class]{}-[func]{selectionSort}\n</code></pre> selection_sort.zig<pre><code>[class]{}-[func]{selectionSort}\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\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(3\\)\u3001\\(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\\) \u548c \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002</li> <li>\u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u5982\u56fe 11-3 \u6240\u793a\uff0c\u5143\u7d20 <code>nums[i]</code> \u6709\u53ef\u80fd\u88ab\u4ea4\u6362\u81f3\u4e0e\u5176\u76f8\u7b49\u7684\u5143\u7d20\u7684\u53f3\u8fb9\uff0c\u5bfc\u81f4\u4e24\u8005\u76f8\u5bf9\u987a\u5e8f\u53d1\u751f\u6539\u53d8\u3002</li> </ul> <p> \u56fe 11-3 \u00a0 \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>\u5982\u56fe 11-1 \u6240\u793a\uff0c\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u662f\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u7b49\u3002\u6392\u5e8f\u7684\u5224\u65ad\u89c4\u5219\u53ef\u6839\u636e\u9700\u6c42\u8bbe\u5b9a\uff0c\u5982\u6570\u5b57\u5927\u5c0f\u3001\u5b57\u7b26 ASCII \u7801\u987a\u5e8f\u6216\u81ea\u5b9a\u4e49\u89c4\u5219\u3002</p> <p></p> <p> \u56fe 11-1 \u00a0 \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\u987b\u501f\u52a9\u989d\u5916\u7684\u8f85\u52a9\u6570\u7ec4\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u539f\u5730\u6392\u5e8f\u7684\u6570\u636e\u642c\u8fd0\u64cd\u4f5c\u8f83\u5c11\uff0c\u8fd0\u884c\u901f\u5ea6\u4e5f\u66f4\u5feb\u3002</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</p> <p>\u7a33\u5b9a\u6392\u5e8f\u662f\u591a\u7ea7\u6392\u5e8f\u573a\u666f\u7684\u5fc5\u8981\u6761\u4ef6\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b58\u50a8\u5b66\u751f\u4fe1\u606f\u7684\u8868\u683c\uff0c\u7b2c 1 \u5217\u548c\u7b2c 2 \u5217\u5206\u522b\u662f\u59d3\u540d\u548c\u5e74\u9f84\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u300c\u975e\u7a33\u5b9a\u6392\u5e8f\u300d\u53ef\u80fd\u5bfc\u81f4\u8f93\u5165\u6570\u636e\u7684\u6709\u5e8f\u6027\u4e27\u5931\u3002</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\\(<\\)\u3001\\(=\\)\u3001\\(>\\)\uff09\u6765\u5224\u65ad\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\uff0c\u4ece\u800c\u6392\u5e8f\u6574\u4e2a\u6570\u7ec4\uff0c\u7406\u8bba\u6700\u4f18\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u800c\u300c\u975e\u6bd4\u8f83\u6392\u5e8f\u300d\u4e0d\u4f7f\u7528\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u8fbe \\(O(n)\\) \uff0c\u4f46\u5176\u901a\u7528\u6027\u76f8\u5bf9\u8f83\u5dee\u3002</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":""},{"location":"chapter_sorting/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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> <li>\u56fe 11-19 \u5bf9\u6bd4\u4e86\u4e3b\u6d41\u6392\u5e8f\u7b97\u6cd5\u7684\u6548\u7387\u3001\u7a33\u5b9a\u6027\u3001\u5c31\u5730\u6027\u548c\u81ea\u9002\u5e94\u6027\u7b49\u3002</li> </ul> <p> \u56fe 11-19 \u00a0 \u6392\u5e8f\u7b97\u6cd5\u5bf9\u6bd4 </p>"},{"location":"chapter_sorting/summary/#2-q-a","title":"2. \u00a0 Q & 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><=</code> \u57fa\u51c6\u6570\uff0c\u8fd9\u5c31\u8981\u6c42\u6700\u540e\u4e00\u6b65\u4ea4\u6362\u524d <code>nums[left] >= 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] > 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\\)\u3001\\(n - 1\\)\u3001\\(\\dots\\)\u3001\\(2\\)\u3001\\(1\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u4e3a \\(n\\) \u3002\u5c3e\u9012\u5f52\u4f18\u5316\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u7684\u51fa\u73b0\u3002</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":"\u7b2c 5 \u7ae0 \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>\u5728\u961f\u5217\u4e2d\uff0c\u6211\u4eec\u4ec5\u80fd\u5728\u5934\u90e8\u5220\u9664\u6216\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u3002\u5982\u56fe 5-7 \u6240\u793a\uff0c\u300c\u53cc\u5411\u961f\u5217 double-ended queue\u300d\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u5728\u5934\u90e8\u548c\u5c3e\u90e8\u6267\u884c\u5143\u7d20\u7684\u6dfb\u52a0\u6216\u5220\u9664\u64cd\u4f5c\u3002</p> <p></p> <p> \u56fe 5-7 \u00a0 \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\u8868 5-3 \u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u79f0\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002</p> <p> \u8868 5-3 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.cpp<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\ndeque<int> deque;\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.push_front(1);\n/* \u8bbf\u95ee\u5143\u7d20 */\nint front = deque.front(); // \u961f\u9996\u5143\u7d20\nint back = deque.back(); // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\ndeque.pop_front(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.pop_back(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = deque.empty();\n</code></pre> deque.java<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nDeque<Integer> deque = new LinkedList<>();\n/* \u5143\u7d20\u5165\u961f */\ndeque.offerLast(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.offerLast(5);\ndeque.offerLast(4);\ndeque.offerFirst(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.offerFirst(1);\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.peekFirst(); // \u961f\u9996\u5143\u7d20\nint peekLast = deque.peekLast(); // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\nint popFirst = deque.pollFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\nint popLast = deque.pollLast(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = deque.isEmpty();\n</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<int> deque = new LinkedList<int>();\n/* \u5143\u7d20\u5165\u961f */\ndeque.AddLast(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.AddLast(5);\ndeque.AddLast(4);\ndeque.AddFirst(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.AddFirst(1);\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.First.Value; // \u961f\u9996\u5143\u7d20\nint peekLast = deque.Last.Value; // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\ndeque.RemoveFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.RemoveLast(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.Count;\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.Count == 0;\n</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.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.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.dart<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Dart \u4e2d\uff0cQueue \u88ab\u5b9a\u4e49\u4e3a\u53cc\u5411\u961f\u5217\nQueue<int> deque = Queue<int>();\n/* \u5143\u7d20\u5165\u961f */\ndeque.addLast(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.addLast(5);\ndeque.addLast(4);\ndeque.addFirst(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.addFirst(1);\n/* \u8bbf\u95ee\u5143\u7d20 */\nint peekFirst = deque.first; // \u961f\u9996\u5143\u7d20\nint peekLast = deque.last; // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\nint popFirst = deque.removeFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\nint popLast = deque.removeLast(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.length;\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = deque.isEmpty;W\n</code></pre> deque.rs<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\nlet mut deque: VecDeque<u32> = VecDeque::new();\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(2); // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.push_front(1);\n/* \u8bbf\u95ee\u5143\u7d20 */\nif let Some(front) = deque.front() { // \u961f\u9996\u5143\u7d20\n}\nif let Some(rear) = deque.back() { // \u961f\u5c3e\u5143\u7d20\n}\n/* \u5143\u7d20\u51fa\u961f */\nif let Some(pop_front) = deque.pop_front() { // \u961f\u9996\u5143\u7d20\u51fa\u961f\n}\nif let Some(pop_rear) = deque.pop_back() { // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.len();\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = deque.is_empty();\n</code></pre> deque.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u53cc\u5411\u961f\u5217\n</code></pre> deque.zig<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":"1. \u00a0 \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\u201c\u53cc\u5411\u94fe\u8868\u201d\u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002</p> <p>\u5982\u56fe 5-8 \u6240\u793a\uff0c\u6211\u4eec\u5c06\u53cc\u5411\u94fe\u8868\u7684\u5934\u8282\u70b9\u548c\u5c3e\u8282\u70b9\u89c6\u4e3a\u53cc\u5411\u961f\u5217\u7684\u961f\u9996\u548c\u961f\u5c3e\uff0c\u540c\u65f6\u5b9e\u73b0\u5728\u4e24\u7aef\u6dfb\u52a0\u548c\u5220\u9664\u8282\u70b9\u7684\u529f\u80fd\u3002</p> LinkedListDequepushLast()pushFirst()popLast()popFirst() <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 5-8 \u00a0 \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c </p> <p>\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig linkedlist_deque.py<pre><code>[class]{ListNode}-[func]{}\n[class]{LinkedListDeque}-[func]{}\n</code></pre> linkedlist_deque.cpp<pre><code>[class]{DoublyListNode}-[func]{}\n[class]{LinkedListDeque}-[func]{}\n</code></pre> linkedlist_deque.java<pre><code>[class]{ListNode}-[func]{}\n[class]{LinkedListDeque}-[func]{}\n</code></pre> linkedlist_deque.cs<pre><code>[class]{ListNode}-[func]{}\n[class]{LinkedListDeque}-[func]{}\n</code></pre> linkedlist_deque.go<pre><code>[class]{linkedListDeque}-[func]{}\n</code></pre> linkedlist_deque.swift<pre><code>[class]{ListNode}-[func]{}\n[class]{LinkedListDeque}-[func]{}\n</code></pre> linkedlist_deque.js<pre><code>[class]{ListNode}-[func]{}\n[class]{LinkedListDeque}-[func]{}\n</code></pre> linkedlist_deque.ts<pre><code>[class]{ListNode}-[func]{}\n[class]{LinkedListDeque}-[func]{}\n</code></pre> linkedlist_deque.dart<pre><code>[class]{ListNode}-[func]{}\n[class]{LinkedListDeque}-[func]{}\n</code></pre> linkedlist_deque.rs<pre><code>[class]{ListNode}-[func]{}\n[class]{LinkedListDeque}-[func]{}\n</code></pre> linkedlist_deque.c<pre><code>[class]{doublyListNode}-[func]{}\n[class]{linkedListDeque}-[func]{}\n</code></pre> linkedlist_deque.zig<pre><code>[class]{ListNode}-[func]{}\n[class]{LinkedListDeque}-[func]{}\n</code></pre>"},{"location":"chapter_stack_and_queue/deque/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"<p>\u5982\u56fe 5-9 \u6240\u793a\uff0c\u4e0e\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u73af\u5f62\u6570\u7ec4\u6765\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u3002</p> ArrayDequepushLast()pushFirst()popLast()popFirst() <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 5-9 \u00a0 \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u7684\u5165\u961f\u51fa\u961f\u64cd\u4f5c </p> <p>\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> PythonC++JavaC#GoSwiftJSTSDartRustCZig array_deque.py<pre><code>[class]{ArrayDeque}-[func]{}\n</code></pre> array_deque.cpp<pre><code>[class]{ArrayDeque}-[func]{}\n</code></pre> array_deque.java<pre><code>[class]{ArrayDeque}-[func]{}\n</code></pre> array_deque.cs<pre><code>[class]{ArrayDeque}-[func]{}\n</code></pre> array_deque.go<pre><code>[class]{arrayDeque}-[func]{}\n</code></pre> array_deque.swift<pre><code>[class]{ArrayDeque}-[func]{}\n</code></pre> array_deque.js<pre><code>[class]{ArrayDeque}-[func]{}\n</code></pre> array_deque.ts<pre><code>[class]{ArrayDeque}-[func]{}\n</code></pre> array_deque.dart<pre><code>[class]{ArrayDeque}-[func]{}\n</code></pre> array_deque.rs<pre><code>[class]{ArrayDeque}-[func]{}\n</code></pre> array_deque.c<pre><code>[class]{arrayDeque}-[func]{}\n</code></pre> array_deque.zig<pre><code>[class]{ArrayDeque}-[func]{}\n</code></pre>"},{"location":"chapter_stack_and_queue/deque/#533","title":"5.3.3 \u00a0 \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\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>\u5982\u56fe 5-4 \u6240\u793a\uff0c\u6211\u4eec\u5c06\u961f\u5217\u7684\u5934\u90e8\u79f0\u4e3a\u201c\u961f\u9996\u201d\uff0c\u5c3e\u90e8\u79f0\u4e3a\u201c\u961f\u5c3e\u201d\uff0c\u5c06\u628a\u5143\u7d20\u52a0\u5165\u961f\u5c3e\u7684\u64cd\u4f5c\u79f0\u4e3a\u201c\u5165\u961f\u201d\uff0c\u5220\u9664\u961f\u9996\u5143\u7d20\u7684\u64cd\u4f5c\u79f0\u4e3a\u201c\u51fa\u961f\u201d\u3002</p> <p></p> <p> \u56fe 5-4 \u00a0 \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\u8868 5-2 \u6240\u793a\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u7684\u65b9\u6cd5\u540d\u79f0\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u6211\u4eec\u5728\u6b64\u91c7\u7528\u4e0e\u6808\u76f8\u540c\u7684\u65b9\u6cd5\u547d\u540d\u3002</p> <p> \u8868 5-2 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.cpp<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\nqueue<int> queue;\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint front = queue.front();\n/* \u5143\u7d20\u51fa\u961f */\nqueue.pop();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty = queue.empty();\n</code></pre> queue.java<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\nQueue<Integer> queue = new LinkedList<>();\n/* \u5143\u7d20\u5165\u961f */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.peek();\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.poll();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = queue.isEmpty();\n</code></pre> queue.cs<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\nQueue<int> queue = new();\n/* \u5143\u7d20\u5165\u961f */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.Peek();\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.Dequeue();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.Count;\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.Count == 0;\n</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.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.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.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<int> queue = Queue();\n/* \u5143\u7d20\u5165\u961f */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek = queue.first;\n/* \u5143\u7d20\u51fa\u961f */\nint pop = queue.removeFirst();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.length;\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.isEmpty;\n</code></pre> queue.rs<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Rust \u4e2d\u4f7f\u7528\u53cc\u5411\u961f\u5217\u4f5c\u4e3a\u666e\u901a\u961f\u5217\u6765\u4f7f\u7528\nlet mut deque: VecDeque<u32> = VecDeque::new();\n/* \u5143\u7d20\u5165\u961f */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nif let Some(front) = deque.front() {\n}\n/* \u5143\u7d20\u51fa\u961f */\nif let Some(pop) = deque.pop_front() {\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.len();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = deque.is_empty();\n</code></pre> queue.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u961f\u5217\n</code></pre> queue.zig<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":"1. \u00a0 \u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"<p>\u5982\u56fe 5-5 \u6240\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u94fe\u8868\u7684\u201c\u5934\u8282\u70b9\u201d\u548c\u201c\u5c3e\u8282\u70b9\u201d\u5206\u522b\u89c6\u4e3a\u201c\u961f\u9996\u201d\u548c\u201c\u961f\u5c3e\u201d\uff0c\u89c4\u5b9a\u961f\u5c3e\u4ec5\u53ef\u6dfb\u52a0\u8282\u70b9\uff0c\u961f\u9996\u4ec5\u53ef\u5220\u9664\u8282\u70b9\u3002</p> LinkedListQueuepush()pop() <p></p> <p></p> <p></p> <p> \u56fe 5-5 \u00a0 \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\u4ee3\u7801\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig linkedlist_queue.py<pre><code>[class]{LinkedListQueue}-[func]{}\n</code></pre> linkedlist_queue.cpp<pre><code>[class]{LinkedListQueue}-[func]{}\n</code></pre> linkedlist_queue.java<pre><code>[class]{LinkedListQueue}-[func]{}\n</code></pre> linkedlist_queue.cs<pre><code>[class]{LinkedListQueue}-[func]{}\n</code></pre> linkedlist_queue.go<pre><code>[class]{linkedListQueue}-[func]{}\n</code></pre> linkedlist_queue.swift<pre><code>[class]{LinkedListQueue}-[func]{}\n</code></pre> linkedlist_queue.js<pre><code>[class]{LinkedListQueue}-[func]{}\n</code></pre> linkedlist_queue.ts<pre><code>[class]{LinkedListQueue}-[func]{}\n</code></pre> linkedlist_queue.dart<pre><code>[class]{LinkedListQueue}-[func]{}\n</code></pre> linkedlist_queue.rs<pre><code>[class]{LinkedListQueue}-[func]{}\n</code></pre> linkedlist_queue.c<pre><code>[class]{linkedListQueue}-[func]{}\n</code></pre> linkedlist_queue.zig<pre><code>[class]{LinkedListQueue}-[func]{}\n</code></pre>"},{"location":"chapter_stack_and_queue/queue/#2","title":"2. \u00a0 \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>size</code> \u7528\u4e8e\u8bb0\u5f55\u961f\u5217\u957f\u5ea6\u3002\u5b9a\u4e49 <code>rear = front + size</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 <code>[front, rear - 1]</code>\uff0c\u5404\u79cd\u64cd\u4f5c\u7684\u5b9e\u73b0\u65b9\u6cd5\u5982\u56fe 5-6 \u6240\u793a\u3002</p> <ul> <li>\u5165\u961f\u64cd\u4f5c\uff1a\u5c06\u8f93\u5165\u5143\u7d20\u8d4b\u503c\u7ed9 <code>rear</code> \u7d22\u5f15\u5904\uff0c\u5e76\u5c06 <code>size</code> \u589e\u52a0 1 \u3002</li> <li>\u51fa\u961f\u64cd\u4f5c\uff1a\u53ea\u9700\u5c06 <code>front</code> \u589e\u52a0 1 \uff0c\u5e76\u5c06 <code>size</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 5-6 \u00a0 \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\u201c\u73af\u5f62\u6570\u7ec4\u201d\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> PythonC++JavaC#GoSwiftJSTSDartRustCZig array_queue.py<pre><code>[class]{ArrayQueue}-[func]{}\n</code></pre> array_queue.cpp<pre><code>[class]{ArrayQueue}-[func]{}\n</code></pre> array_queue.java<pre><code>[class]{ArrayQueue}-[func]{}\n</code></pre> array_queue.cs<pre><code>[class]{ArrayQueue}-[func]{}\n</code></pre> array_queue.go<pre><code>[class]{arrayQueue}-[func]{}\n</code></pre> array_queue.swift<pre><code>[class]{ArrayQueue}-[func]{}\n</code></pre> array_queue.js<pre><code>[class]{ArrayQueue}-[func]{}\n</code></pre> array_queue.ts<pre><code>[class]{ArrayQueue}-[func]{}\n</code></pre> array_queue.dart<pre><code>[class]{ArrayQueue}-[func]{}\n</code></pre> array_queue.rs<pre><code>[class]{ArrayQueue}-[func]{}\n</code></pre> array_queue.c<pre><code>[class]{arrayQueue}-[func]{}\n</code></pre> array_queue.zig<pre><code>[class]{ArrayQueue}-[func]{}\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\u7684\u903b\u8f91\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>\u5982\u56fe 5-1 \u6240\u793a\uff0c\u6211\u4eec\u628a\u5806\u53e0\u5143\u7d20\u7684\u9876\u90e8\u79f0\u4e3a\u201c\u6808\u9876\u201d\uff0c\u5e95\u90e8\u79f0\u4e3a\u201c\u6808\u5e95\u201d\u3002\u5c06\u628a\u5143\u7d20\u6dfb\u52a0\u5230\u6808\u9876\u7684\u64cd\u4f5c\u53eb\u505a\u201c\u5165\u6808\u201d\uff0c\u5220\u9664\u6808\u9876\u5143\u7d20\u7684\u64cd\u4f5c\u53eb\u505a\u201c\u51fa\u6808\u201d\u3002</p> <p></p> <p> \u56fe 5-1 \u00a0 \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\u8868 5-1 \u6240\u793a\uff0c\u5177\u4f53\u7684\u65b9\u6cd5\u540d\u9700\u8981\u6839\u636e\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\u6765\u786e\u5b9a\u3002\u5728\u6b64\uff0c\u6211\u4eec\u4ee5\u5e38\u89c1\u7684 <code>push()</code>\u3001<code>pop()</code>\u3001<code>peek()</code> \u547d\u540d\u4e3a\u4f8b\u3002</p> <p> \u8868 5-1 \u00a0 \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\u201c\u6570\u7ec4\u201d\u6216\u201c\u94fe\u8868\u201d\u89c6\u4f5c\u6808\u6765\u4f7f\u7528\uff0c\u5e76\u5728\u7a0b\u5e8f\u903b\u8f91\u4e0a\u5ffd\u7565\u4e0e\u6808\u65e0\u5173\u7684\u64cd\u4f5c\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.cpp<pre><code>/* \u521d\u59cb\u5316\u6808 */\nstack<int> stack;\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top = stack.top();\n/* \u5143\u7d20\u51fa\u6808 */\nstack.pop(); // \u65e0\u8fd4\u56de\u503c\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool empty = stack.empty();\n</code></pre> stack.java<pre><code>/* \u521d\u59cb\u5316\u6808 */\nStack<Integer> stack = new Stack<>();\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.peek();\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = stack.isEmpty();\n</code></pre> stack.cs<pre><code>/* \u521d\u59cb\u5316\u6808 */\nStack<int> stack = new ();\n/* \u5143\u7d20\u5165\u6808 */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.Peek();\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.Pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.Count;\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.Count == 0;\n</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.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.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.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<int> stack = [];\n/* \u5143\u7d20\u5165\u6808 */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek = stack.last;\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.removeLast();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.length;\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = stack.isEmpty;\n</code></pre> stack.rs<pre><code>/* \u521d\u59cb\u5316\u6808 */\n// \u628a Vec \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nlet mut stack: Vec<i32> = Vec::new();\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nlet top = stack[stack.len() - 1];\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.pop().unwrap();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.len();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = stack.is_empty();\n</code></pre> stack.c<pre><code>// C \u672a\u63d0\u4f9b\u5185\u7f6e\u6808\n</code></pre> stack.zig<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":"1. \u00a0 \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>\u5982\u56fe 5-2 \u6240\u793a\uff0c\u5bf9\u4e8e\u5165\u6808\u64cd\u4f5c\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u5143\u7d20\u63d2\u5165\u94fe\u8868\u5934\u90e8\uff0c\u8fd9\u79cd\u8282\u70b9\u63d2\u5165\u65b9\u6cd5\u88ab\u79f0\u4e3a\u201c\u5934\u63d2\u6cd5\u201d\u3002\u800c\u5bf9\u4e8e\u51fa\u6808\u64cd\u4f5c\uff0c\u53ea\u9700\u5c06\u5934\u8282\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u5373\u53ef\u3002</p> LinkedListStackpush()pop() <p></p> <p></p> <p></p> <p> \u56fe 5-2 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig linkedlist_stack.py<pre><code>[class]{LinkedListStack}-[func]{}\n</code></pre> linkedlist_stack.cpp<pre><code>[class]{LinkedListStack}-[func]{}\n</code></pre> linkedlist_stack.java<pre><code>[class]{LinkedListStack}-[func]{}\n</code></pre> linkedlist_stack.cs<pre><code>[class]{LinkedListStack}-[func]{}\n</code></pre> linkedlist_stack.go<pre><code>[class]{linkedListStack}-[func]{}\n</code></pre> linkedlist_stack.swift<pre><code>[class]{LinkedListStack}-[func]{}\n</code></pre> linkedlist_stack.js<pre><code>[class]{LinkedListStack}-[func]{}\n</code></pre> linkedlist_stack.ts<pre><code>[class]{LinkedListStack}-[func]{}\n</code></pre> linkedlist_stack.dart<pre><code>[class]{LinkedListStack}-[func]{}\n</code></pre> linkedlist_stack.rs<pre><code>[class]{LinkedListStack}-[func]{}\n</code></pre> linkedlist_stack.c<pre><code>[class]{linkedListStack}-[func]{}\n</code></pre> linkedlist_stack.zig<pre><code>[class]{LinkedListStack}-[func]{}\n</code></pre>"},{"location":"chapter_stack_and_queue/stack/#2","title":"2. \u00a0 \u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"<p>\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u6808\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u5c3e\u90e8\u4f5c\u4e3a\u6808\u9876\u3002\u5982\u56fe 5-3 \u6240\u793a\uff0c\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u5206\u522b\u5bf9\u5e94\u5728\u6570\u7ec4\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\u4e0e\u5220\u9664\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(1)\\) \u3002</p> ArrayStackpush()pop() <p></p> <p></p> <p></p> <p> \u56fe 5-3 \u00a0 \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\u987b\u81ea\u884c\u5904\u7406\u6570\u7ec4\u6269\u5bb9\u95ee\u9898\u3002\u4ee5\u4e0b\u4e3a\u793a\u4f8b\u4ee3\u7801\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig array_stack.py<pre><code>[class]{ArrayStack}-[func]{}\n</code></pre> array_stack.cpp<pre><code>[class]{ArrayStack}-[func]{}\n</code></pre> array_stack.java<pre><code>[class]{ArrayStack}-[func]{}\n</code></pre> array_stack.cs<pre><code>[class]{ArrayStack}-[func]{}\n</code></pre> array_stack.go<pre><code>[class]{arrayStack}-[func]{}\n</code></pre> array_stack.swift<pre><code>[class]{ArrayStack}-[func]{}\n</code></pre> array_stack.js<pre><code>[class]{ArrayStack}-[func]{}\n</code></pre> array_stack.ts<pre><code>[class]{ArrayStack}-[func]{}\n</code></pre> array_stack.dart<pre><code>[class]{ArrayStack}-[func]{}\n</code></pre> array_stack.rs<pre><code>[class]{ArrayStack}-[func]{}\n</code></pre> array_stack.c<pre><code>[class]{arrayStack}-[func]{}\n</code></pre> array_stack.zig<pre><code>[class]{ArrayStack}-[func]{}\n</code></pre>"},{"location":"chapter_stack_and_queue/stack/#513","title":"5.1.3 \u00a0 \u4e24\u79cd\u5b9e\u73b0\u5bf9\u6bd4","text":"<p>\u652f\u6301\u64cd\u4f5c</p> <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> <p>\u65f6\u95f4\u6548\u7387</p> <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\u65f6\uff0c\u4f8b\u5982 <code>int</code> \u6216 <code>double</code> \uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\u3002</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> <p>\u7a7a\u95f4\u6548\u7387</p> <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\u540e\u9000\u64cd\u4f5c\u56de\u5230\u4e0a\u4e00\u9875\u9762\u3002\u540e\u9000\u64cd\u4f5c\u5b9e\u9645\u4e0a\u662f\u5728\u6267\u884c\u51fa\u6808\u3002\u5982\u679c\u8981\u540c\u65f6\u652f\u6301\u540e\u9000\u548c\u524d\u8fdb\uff0c\u90a3\u4e48\u9700\u8981\u4e24\u4e2a\u6808\u6765\u914d\u5408\u5b9e\u73b0\u3002</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":""},{"location":"chapter_stack_and_queue/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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\u52a3\u5316\u81f3 \\(O(n)\\) \u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u5177\u6709\u66f4\u4e3a\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\u3002</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/#2-q-a","title":"2. \u00a0 Q & 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> <p>\u64a4\u9500\uff08undo\uff09\u548c\u53cd\u64a4\u9500\uff08redo\uff09\u5177\u4f53\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f</p> <p>\u4f7f\u7528\u4e24\u4e2a\u5806\u6808\uff0c\u6808 <code>A</code> \u7528\u4e8e\u64a4\u9500\uff0c\u6808 <code>B</code> \u7528\u4e8e\u53cd\u64a4\u9500\u3002</p> <ol> <li>\u6bcf\u5f53\u7528\u6237\u6267\u884c\u4e00\u4e2a\u64cd\u4f5c\uff0c\u5c06\u8fd9\u4e2a\u64cd\u4f5c\u538b\u5165\u6808 <code>A</code> \uff0c\u5e76\u6e05\u7a7a\u6808 <code>B</code> \u3002</li> <li>\u5f53\u7528\u6237\u6267\u884c\u201c\u64a4\u9500\u201d\u65f6\uff0c\u4ece\u6808 <code>A</code> \u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u5176\u538b\u5165\u6808 <code>B</code> \u3002</li> <li>\u5f53\u7528\u6237\u6267\u884c\u201c\u53cd\u64a4\u9500\u201d\u65f6\uff0c\u4ece\u6808 <code>B</code> \u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u5176\u538b\u5165\u6808 <code>A</code> \u3002</li> </ol>"},{"location":"chapter_tree/","title":"\u7b2c 7 \u7ae0 \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\u6211\u4eec\u80fd\u5426\u7528\u6570\u7ec4\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\u56fe 7-12 \u5c55\u793a\u4e86\u5404\u4e2a\u8282\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u3002</p> <p></p> <p> \u56fe 7-12 \u00a0 \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>\u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u4e00\u4e2a\u7279\u4f8b\uff0c\u5728\u4e8c\u53c9\u6811\u7684\u4e2d\u95f4\u5c42\u901a\u5e38\u5b58\u5728\u8bb8\u591a \\(\\text{None}\\) \u3002\u7531\u4e8e\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5e76\u4e0d\u5305\u542b\u8fd9\u4e9b \\(\\text{None}\\) \uff0c\u56e0\u6b64\u6211\u4eec\u65e0\u6cd5\u4ec5\u51ed\u8be5\u5e8f\u5217\u6765\u63a8\u6d4b \\(\\text{None}\\) \u7684\u6570\u91cf\u548c\u5206\u5e03\u4f4d\u7f6e\u3002\u8fd9\u610f\u5473\u7740\u5b58\u5728\u591a\u79cd\u4e8c\u53c9\u6811\u7ed3\u6784\u90fd\u7b26\u5408\u8be5\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002</p> <p>\u5982\u56fe 7-13 \u6240\u793a\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u975e\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c\u4e0a\u8ff0\u7684\u6570\u7ec4\u8868\u793a\u65b9\u6cd5\u5df2\u7ecf\u5931\u6548\u3002</p> <p></p> <p> \u56fe 7-13 \u00a0 \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\u56fe 7-14 \u6240\u793a\uff0c\u8fd9\u6837\u5904\u7406\u540e\uff0c\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5c31\u53ef\u4ee5\u552f\u4e00\u8868\u793a\u4e8c\u53c9\u6811\u4e86\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig <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 int \u6700\u5927\u503c INT_MAX \u6807\u8bb0\u7a7a\u4f4d\nvector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n</code></pre> <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? \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 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 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>/* \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? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nList<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 None \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];\n</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>\n</code></pre> <p></p> <p> \u56fe 7-14 \u00a0 \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</p> <p>\u8fd9\u610f\u5473\u7740\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u5b8c\u5168\u4e8c\u53c9\u6811\u65f6\uff0c\u53ef\u4ee5\u7701\u7565\u5b58\u50a8\u6240\u6709 \\(\\text{None}\\) \uff0c\u975e\u5e38\u65b9\u4fbf\u3002\u56fe 7-15 \u7ed9\u51fa\u4e86\u4e00\u4e2a\u4f8b\u5b50\u3002</p> <p></p> <p> \u56fe 7-15 \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a </p> <p>\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e00\u4e2a\u57fa\u4e8e\u6570\u7ec4\u8868\u793a\u7684\u4e8c\u53c9\u6811\uff0c\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\u64cd\u4f5c\u3002</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> PythonC++JavaC#GoSwiftJSTSDartRustCZig array_binary_tree.py<pre><code>[class]{ArrayBinaryTree}-[func]{}\n</code></pre> array_binary_tree.cpp<pre><code>[class]{ArrayBinaryTree}-[func]{}\n</code></pre> array_binary_tree.java<pre><code>[class]{ArrayBinaryTree}-[func]{}\n</code></pre> array_binary_tree.cs<pre><code>[class]{ArrayBinaryTree}-[func]{}\n</code></pre> array_binary_tree.go<pre><code>[class]{arrayBinaryTree}-[func]{}\n</code></pre> array_binary_tree.swift<pre><code>[class]{ArrayBinaryTree}-[func]{}\n</code></pre> array_binary_tree.js<pre><code>[class]{ArrayBinaryTree}-[func]{}\n</code></pre> array_binary_tree.ts<pre><code>[class]{ArrayBinaryTree}-[func]{}\n</code></pre> array_binary_tree.dart<pre><code>[class]{ArrayBinaryTree}-[func]{}\n</code></pre> array_binary_tree.rs<pre><code>[class]{ArrayBinaryTree}-[func]{}\n</code></pre> array_binary_tree.c<pre><code>[class]{arrayBinaryTree}-[func]{}\n</code></pre> array_binary_tree.zig<pre><code>[class]{ArrayBinaryTree}-[func]{}\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\u4e3b\u8981\u6709\u4ee5\u4e0b\u4f18\u70b9\u3002</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\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002</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\u56fe 7-24 \u6240\u793a\uff0c\u7ecf\u8fc7\u4e24\u6b21\u5220\u9664\u8282\u70b9\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u4e8c\u53c9\u641c\u7d22\u6811\u4fbf\u4f1a\u9000\u5316\u4e3a\u94fe\u8868\u3002</p> <p></p> <p> \u56fe 7-24 \u00a0 AVL \u6811\u5728\u5220\u9664\u8282\u70b9\u540e\u53d1\u751f\u9000\u5316 </p> <p>\u518d\u4f8b\u5982\uff0c\u5728\u56fe 7-25 \u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e24\u4e2a\u8282\u70b9\u540e\uff0c\u6811\u5c06\u4e25\u91cd\u5411\u5de6\u503e\u659c\uff0c\u67e5\u627e\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u968f\u4e4b\u6076\u5316\u3002</p> <p></p> <p> \u56fe 7-25 \u00a0 AVL \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>AVL \u6811\u65e2\u662f\u4e8c\u53c9\u641c\u7d22\u6811\u4e5f\u662f\u5e73\u8861\u4e8c\u53c9\u6811\uff0c\u540c\u65f6\u6ee1\u8db3\u8fd9\u4e24\u7c7b\u4e8c\u53c9\u6811\u7684\u6240\u6709\u6027\u8d28\uff0c\u56e0\u6b64\u4e5f\u88ab\u79f0\u4e3a\u300c\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811 balanced binary search tree\u300d\u3002</p>"},{"location":"chapter_tree/avl_tree/#1","title":"1. \u00a0 \u8282\u70b9\u9ad8\u5ea6","text":"<p>\u7531\u4e8e AVL \u6811\u7684\u76f8\u5173\u64cd\u4f5c\u9700\u8981\u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u4e3a\u8282\u70b9\u7c7b\u6dfb\u52a0 <code>height</code> \u53d8\u91cf\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig <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: TreeNode | None = None # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nself.right: TreeNode | None = None # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\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>/* 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 {\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\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 {\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>/* 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\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>use std::rc::Rc;\nuse std::cell::RefCell;\n/* AVL \u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nval: i32, // \u8282\u70b9\u503c\nheight: i32, // \u8282\u70b9\u9ad8\u5ea6\nleft: Option<Rc<RefCell<TreeNode>>>, // \u5de6\u5b50\u8282\u70b9\nright: Option<Rc<RefCell<TreeNode>>>, // \u53f3\u5b50\u8282\u70b9\n}\nimpl TreeNode {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new(val: i32) -> Rc<RefCell<Self>> {\nRc::new(RefCell::new(Self {\nval,\nheight: 0,\nleft: None,\nright: None\n}))\n}\n}\n</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->val = val;\nnode->height = 0;\nnode->left = NULL;\nnode->right = NULL;\nreturn node;\n}\n</code></pre> <pre><code>\n</code></pre> <p>\u201c\u8282\u70b9\u9ad8\u5ea6\u201d\u662f\u6307\u4ece\u8be5\u8282\u70b9\u5230\u6700\u8fdc\u53f6\u8282\u70b9\u7684\u8ddd\u79bb\uff0c\u5373\u6240\u7ecf\u8fc7\u7684\u201c\u8fb9\u201d\u7684\u6570\u91cf\u3002\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u662f\uff0c\u53f6\u8282\u70b9\u7684\u9ad8\u5ea6\u4e3a 0 \uff0c\u800c\u7a7a\u8282\u70b9\u7684\u9ad8\u5ea6\u4e3a -1 \u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e24\u4e2a\u5de5\u5177\u51fd\u6570\uff0c\u5206\u522b\u7528\u4e8e\u83b7\u53d6\u548c\u66f4\u65b0\u8282\u70b9\u7684\u9ad8\u5ea6\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.py<pre><code>[class]{AVLTree}-[func]{height}\n[class]{AVLTree}-[func]{__update_height}\n</code></pre> avl_tree.cpp<pre><code>[class]{AVLTree}-[func]{height}\n[class]{AVLTree}-[func]{updateHeight}\n</code></pre> avl_tree.java<pre><code>[class]{AVLTree}-[func]{height}\n[class]{AVLTree}-[func]{updateHeight}\n</code></pre> avl_tree.cs<pre><code>[class]{AVLTree}-[func]{height}\n[class]{AVLTree}-[func]{updateHeight}\n</code></pre> avl_tree.go<pre><code>[class]{aVLTree}-[func]{height}\n[class]{aVLTree}-[func]{updateHeight}\n</code></pre> avl_tree.swift<pre><code>[class]{AVLTree}-[func]{height}\n[class]{AVLTree}-[func]{updateHeight}\n</code></pre> avl_tree.js<pre><code>[class]{AVLTree}-[func]{height}\n[class]{AVLTree}-[func]{#updateHeight}\n</code></pre> avl_tree.ts<pre><code>[class]{AVLTree}-[func]{height}\n[class]{AVLTree}-[func]{updateHeight}\n</code></pre> avl_tree.dart<pre><code>[class]{AVLTree}-[func]{height}\n[class]{AVLTree}-[func]{updateHeight}\n</code></pre> avl_tree.rs<pre><code>[class]{AVLTree}-[func]{height}\n[class]{AVLTree}-[func]{update_height}\n</code></pre> avl_tree.c<pre><code>[class]{}-[func]{height}\n[class]{}-[func]{updateHeight}\n</code></pre> avl_tree.zig<pre><code>[class]{AVLTree}-[func]{height}\n[class]{AVLTree}-[func]{updateHeight}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#2","title":"2. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.py<pre><code>[class]{AVLTree}-[func]{balance_factor}\n</code></pre> avl_tree.cpp<pre><code>[class]{AVLTree}-[func]{balanceFactor}\n</code></pre> avl_tree.java<pre><code>[class]{AVLTree}-[func]{balanceFactor}\n</code></pre> avl_tree.cs<pre><code>[class]{AVLTree}-[func]{balanceFactor}\n</code></pre> avl_tree.go<pre><code>[class]{aVLTree}-[func]{balanceFactor}\n</code></pre> avl_tree.swift<pre><code>[class]{AVLTree}-[func]{balanceFactor}\n</code></pre> avl_tree.js<pre><code>[class]{AVLTree}-[func]{balanceFactor}\n</code></pre> avl_tree.ts<pre><code>[class]{AVLTree}-[func]{balanceFactor}\n</code></pre> avl_tree.dart<pre><code>[class]{AVLTree}-[func]{balanceFactor}\n</code></pre> avl_tree.rs<pre><code>[class]{AVLTree}-[func]{balance_factor}\n</code></pre> avl_tree.c<pre><code>[class]{}-[func]{balanceFactor}\n</code></pre> avl_tree.zig<pre><code>[class]{AVLTree}-[func]{balanceFactor}\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\u201c\u65cb\u8f6c\u201d\u64cd\u4f5c\uff0c\u5b83\u80fd\u591f\u5728\u4e0d\u5f71\u54cd\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u524d\u63d0\u4e0b\uff0c\u4f7f\u5931\u8861\u8282\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u65cb\u8f6c\u64cd\u4f5c\u65e2\u80fd\u4fdd\u6301\u201c\u4e8c\u53c9\u641c\u7d22\u6811\u201d\u7684\u6027\u8d28\uff0c\u4e5f\u80fd\u4f7f\u6811\u91cd\u65b0\u53d8\u4e3a\u201c\u5e73\u8861\u4e8c\u53c9\u6811\u201d\u3002</p> <p>\u6211\u4eec\u5c06\u5e73\u8861\u56e0\u5b50\u7edd\u5bf9\u503c \\(> 1\\) \u7684\u8282\u70b9\u79f0\u4e3a\u201c\u5931\u8861\u8282\u70b9\u201d\u3002\u6839\u636e\u8282\u70b9\u5931\u8861\u60c5\u51b5\u7684\u4e0d\u540c\uff0c\u65cb\u8f6c\u64cd\u4f5c\u5206\u4e3a\u56db\u79cd\uff1a\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u540e\u53f3\u65cb\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65cb\u8f6c\u64cd\u4f5c\u3002</p>"},{"location":"chapter_tree/avl_tree/#1_1","title":"1. \u00a0 \u53f3\u65cb","text":"<p>\u5982\u56fe 7-26 \u6240\u793a\uff0c\u8282\u70b9\u4e0b\u65b9\u4e3a\u5e73\u8861\u56e0\u5b50\u3002\u4ece\u5e95\u81f3\u9876\u770b\uff0c\u4e8c\u53c9\u6811\u4e2d\u9996\u4e2a\u5931\u8861\u8282\u70b9\u662f\u201c\u8282\u70b9 3\u201d\u3002\u6211\u4eec\u5173\u6ce8\u4ee5\u8be5\u5931\u8861\u8282\u70b9\u4e3a\u6839\u8282\u70b9\u7684\u5b50\u6811\uff0c\u5c06\u8be5\u8282\u70b9\u8bb0\u4e3a <code>node</code> \uff0c\u5176\u5de6\u5b50\u8282\u70b9\u8bb0\u4e3a <code>child</code> \uff0c\u6267\u884c\u201c\u53f3\u65cb\u201d\u64cd\u4f5c\u3002\u5b8c\u6210\u53f3\u65cb\u540e\uff0c\u5b50\u6811\u5df2\u7ecf\u6062\u590d\u5e73\u8861\uff0c\u5e76\u4e14\u4ecd\u7136\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u7279\u6027\u3002</p> <1><2><3><4> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 7-26 \u00a0 \u53f3\u65cb\u64cd\u4f5c\u6b65\u9aa4 </p> <p>\u5982\u56fe 7-27 \u6240\u793a\uff0c\u5f53\u8282\u70b9 <code>child</code> \u6709\u53f3\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a <code>grandChild</code> \uff09\u65f6\uff0c\u9700\u8981\u5728\u53f3\u65cb\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 7-27 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.py<pre><code>[class]{AVLTree}-[func]{__right_rotate}\n</code></pre> avl_tree.cpp<pre><code>[class]{AVLTree}-[func]{rightRotate}\n</code></pre> avl_tree.java<pre><code>[class]{AVLTree}-[func]{rightRotate}\n</code></pre> avl_tree.cs<pre><code>[class]{AVLTree}-[func]{rightRotate}\n</code></pre> avl_tree.go<pre><code>[class]{aVLTree}-[func]{rightRotate}\n</code></pre> avl_tree.swift<pre><code>[class]{AVLTree}-[func]{rightRotate}\n</code></pre> avl_tree.js<pre><code>[class]{AVLTree}-[func]{#rightRotate}\n</code></pre> avl_tree.ts<pre><code>[class]{AVLTree}-[func]{rightRotate}\n</code></pre> avl_tree.dart<pre><code>[class]{AVLTree}-[func]{rightRotate}\n</code></pre> avl_tree.rs<pre><code>[class]{AVLTree}-[func]{right_rotate}\n</code></pre> avl_tree.c<pre><code>[class]{}-[func]{rightRotate}\n</code></pre> avl_tree.zig<pre><code>[class]{AVLTree}-[func]{rightRotate}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#2_1","title":"2. \u00a0 \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\u56fe 7-28 \u6240\u793a\u7684\u201c\u5de6\u65cb\u201d\u64cd\u4f5c\u3002</p> <p></p> <p> \u56fe 7-28 \u00a0 \u5de6\u65cb\u64cd\u4f5c </p> <p>\u540c\u7406\uff0c\u5982\u56fe 7-29 \u6240\u793a\uff0c\u5f53\u8282\u70b9 <code>child</code> \u6709\u5de6\u5b50\u8282\u70b9\uff08\u8bb0\u4e3a <code>grandChild</code> \uff09\u65f6\uff0c\u9700\u8981\u5728\u5de6\u65cb\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 7-29 \u00a0 \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\u53ea\u9700\u5c06\u53f3\u65cb\u7684\u5b9e\u73b0\u4ee3\u7801\u4e2d\u7684\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\u5de6\u65cb\u7684\u5b9e\u73b0\u4ee3\u7801\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.py<pre><code>[class]{AVLTree}-[func]{__left_rotate}\n</code></pre> avl_tree.cpp<pre><code>[class]{AVLTree}-[func]{leftRotate}\n</code></pre> avl_tree.java<pre><code>[class]{AVLTree}-[func]{leftRotate}\n</code></pre> avl_tree.cs<pre><code>[class]{AVLTree}-[func]{leftRotate}\n</code></pre> avl_tree.go<pre><code>[class]{aVLTree}-[func]{leftRotate}\n</code></pre> avl_tree.swift<pre><code>[class]{AVLTree}-[func]{leftRotate}\n</code></pre> avl_tree.js<pre><code>[class]{AVLTree}-[func]{#leftRotate}\n</code></pre> avl_tree.ts<pre><code>[class]{AVLTree}-[func]{leftRotate}\n</code></pre> avl_tree.dart<pre><code>[class]{AVLTree}-[func]{leftRotate}\n</code></pre> avl_tree.rs<pre><code>[class]{AVLTree}-[func]{left_rotate}\n</code></pre> avl_tree.c<pre><code>[class]{}-[func]{leftRotate}\n</code></pre> avl_tree.zig<pre><code>[class]{AVLTree}-[func]{leftRotate}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#3","title":"3. \u00a0 \u5148\u5de6\u65cb\u540e\u53f3\u65cb","text":"<p>\u5bf9\u4e8e\u56fe 7-30 \u4e2d\u7684\u5931\u8861\u8282\u70b9 3 \uff0c\u4ec5\u4f7f\u7528\u5de6\u65cb\u6216\u53f3\u65cb\u90fd\u65e0\u6cd5\u4f7f\u5b50\u6811\u6062\u590d\u5e73\u8861\u3002\u6b64\u65f6\u9700\u8981\u5148\u5bf9 <code>child</code> \u6267\u884c\u201c\u5de6\u65cb\u201d\uff0c\u518d\u5bf9 <code>node</code> \u6267\u884c\u201c\u53f3\u65cb\u201d\u3002</p> <p></p> <p> \u56fe 7-30 \u00a0 \u5148\u5de6\u65cb\u540e\u53f3\u65cb </p>"},{"location":"chapter_tree/avl_tree/#4","title":"4. \u00a0 \u5148\u53f3\u65cb\u540e\u5de6\u65cb","text":"<p>\u5982\u56fe 7-31 \u6240\u793a\uff0c\u5bf9\u4e8e\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u955c\u50cf\u60c5\u51b5\uff0c\u9700\u8981\u5148\u5bf9 <code>child</code> \u6267\u884c\u201c\u53f3\u65cb\u201d\uff0c\u7136\u540e\u5bf9 <code>node</code> \u6267\u884c\u201c\u5de6\u65cb\u201d\u3002</p> <p></p> <p> \u56fe 7-31 \u00a0 \u5148\u53f3\u65cb\u540e\u5de6\u65cb </p>"},{"location":"chapter_tree/avl_tree/#5","title":"5. \u00a0 \u65cb\u8f6c\u7684\u9009\u62e9","text":"<p>\u56fe 7-32 \u5c55\u793a\u7684\u56db\u79cd\u5931\u8861\u60c5\u51b5\u4e0e\u4e0a\u8ff0\u6848\u4f8b\u9010\u4e2a\u5bf9\u5e94\uff0c\u5206\u522b\u9700\u8981\u91c7\u7528\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u540e\u5de6\u3001\u5148\u5de6\u540e\u53f3\u7684\u65cb\u8f6c\u64cd\u4f5c\u3002</p> <p></p> <p> \u56fe 7-32 \u00a0 AVL \u6811\u7684\u56db\u79cd\u65cb\u8f6c\u60c5\u51b5 </p> <p>\u5982\u4e0b\u8868\u6240\u793a\uff0c\u6211\u4eec\u901a\u8fc7\u5224\u65ad\u5931\u8861\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4ee5\u53ca\u8f83\u9ad8\u4e00\u4fa7\u5b50\u8282\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7684\u6b63\u8d1f\u53f7\uff0c\u6765\u786e\u5b9a\u5931\u8861\u8282\u70b9\u5c5e\u4e8e\u56fe 7-32 \u4e2d\u7684\u54ea\u79cd\u60c5\u51b5\u3002</p> <p> \u8868 7-3 \u00a0 \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 \\(> 1\\) \uff08\u5373\u5de6\u504f\u6811\uff09 \\(\\geq 0\\) \u53f3\u65cb \\(> 1\\) \uff08\u5373\u5de6\u504f\u6811\uff09 \\(<0\\) \u5148\u5de6\u65cb\u540e\u53f3\u65cb \\(< -1\\) \uff08\u5373\u53f3\u504f\u6811\uff09 \\(\\leq 0\\) \u5de6\u65cb \\(< -1\\) \uff08\u5373\u53f3\u504f\u6811\uff09 \\(>0\\) \u5148\u53f3\u65cb\u540e\u5de6\u65cb <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> PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.py<pre><code>[class]{AVLTree}-[func]{__rotate}\n</code></pre> avl_tree.cpp<pre><code>[class]{AVLTree}-[func]{rotate}\n</code></pre> avl_tree.java<pre><code>[class]{AVLTree}-[func]{rotate}\n</code></pre> avl_tree.cs<pre><code>[class]{AVLTree}-[func]{rotate}\n</code></pre> avl_tree.go<pre><code>[class]{aVLTree}-[func]{rotate}\n</code></pre> avl_tree.swift<pre><code>[class]{AVLTree}-[func]{rotate}\n</code></pre> avl_tree.js<pre><code>[class]{AVLTree}-[func]{#rotate}\n</code></pre> avl_tree.ts<pre><code>[class]{AVLTree}-[func]{rotate}\n</code></pre> avl_tree.dart<pre><code>[class]{AVLTree}-[func]{rotate}\n</code></pre> avl_tree.rs<pre><code>[class]{AVLTree}-[func]{rotate}\n</code></pre> avl_tree.c<pre><code>[class]{}-[func]{rotate}\n</code></pre> avl_tree.zig<pre><code>[class]{AVLTree}-[func]{rotate}\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/#1_2","title":"1. \u00a0 \u63d2\u5165\u8282\u70b9","text":"<p>AVL \u6811\u7684\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u4e0e\u4e8c\u53c9\u641c\u7d22\u6811\u5728\u4e3b\u4f53\u4e0a\u7c7b\u4f3c\u3002\u552f\u4e00\u7684\u533a\u522b\u5728\u4e8e\uff0c\u5728 AVL \u6811\u4e2d\u63d2\u5165\u8282\u70b9\u540e\uff0c\u4ece\u8be5\u8282\u70b9\u5230\u6839\u8282\u70b9\u7684\u8def\u5f84\u4e0a\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u7cfb\u5217\u5931\u8861\u8282\u70b9\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u4ece\u8fd9\u4e2a\u8282\u70b9\u5f00\u59cb\uff0c\u81ea\u5e95\u5411\u4e0a\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u8282\u70b9\u6062\u590d\u5e73\u8861\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.py<pre><code>[class]{AVLTree}-[func]{insert}\n[class]{AVLTree}-[func]{__insert_helper}\n</code></pre> avl_tree.cpp<pre><code>[class]{AVLTree}-[func]{insert}\n[class]{AVLTree}-[func]{insertHelper}\n</code></pre> avl_tree.java<pre><code>[class]{AVLTree}-[func]{insert}\n[class]{AVLTree}-[func]{insertHelper}\n</code></pre> avl_tree.cs<pre><code>[class]{AVLTree}-[func]{insert}\n[class]{AVLTree}-[func]{insertHelper}\n</code></pre> avl_tree.go<pre><code>[class]{aVLTree}-[func]{insert}\n[class]{aVLTree}-[func]{insertHelper}\n</code></pre> avl_tree.swift<pre><code>[class]{AVLTree}-[func]{insert}\n[class]{AVLTree}-[func]{insertHelper}\n</code></pre> avl_tree.js<pre><code>[class]{AVLTree}-[func]{insert}\n[class]{AVLTree}-[func]{#insertHelper}\n</code></pre> avl_tree.ts<pre><code>[class]{AVLTree}-[func]{insert}\n[class]{AVLTree}-[func]{insertHelper}\n</code></pre> avl_tree.dart<pre><code>[class]{AVLTree}-[func]{insert}\n[class]{AVLTree}-[func]{insertHelper}\n</code></pre> avl_tree.rs<pre><code>[class]{AVLTree}-[func]{insert}\n[class]{AVLTree}-[func]{insert_helper}\n</code></pre> avl_tree.c<pre><code>[class]{aVLTree}-[func]{insert}\n[class]{}-[func]{insertHelper}\n</code></pre> avl_tree.zig<pre><code>[class]{AVLTree}-[func]{insert}\n[class]{AVLTree}-[func]{insertHelper}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#2_2","title":"2. \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig avl_tree.py<pre><code>[class]{AVLTree}-[func]{remove}\n[class]{AVLTree}-[func]{__remove_helper}\n</code></pre> avl_tree.cpp<pre><code>[class]{AVLTree}-[func]{remove}\n[class]{AVLTree}-[func]{removeHelper}\n</code></pre> avl_tree.java<pre><code>[class]{AVLTree}-[func]{remove}\n[class]{AVLTree}-[func]{removeHelper}\n</code></pre> avl_tree.cs<pre><code>[class]{AVLTree}-[func]{remove}\n[class]{AVLTree}-[func]{removeHelper}\n</code></pre> avl_tree.go<pre><code>[class]{aVLTree}-[func]{remove}\n[class]{aVLTree}-[func]{removeHelper}\n</code></pre> avl_tree.swift<pre><code>[class]{AVLTree}-[func]{remove}\n[class]{AVLTree}-[func]{removeHelper}\n</code></pre> avl_tree.js<pre><code>[class]{AVLTree}-[func]{remove}\n[class]{AVLTree}-[func]{#removeHelper}\n</code></pre> avl_tree.ts<pre><code>[class]{AVLTree}-[func]{remove}\n[class]{AVLTree}-[func]{removeHelper}\n</code></pre> avl_tree.dart<pre><code>[class]{AVLTree}-[func]{remove}\n[class]{AVLTree}-[func]{removeHelper}\n</code></pre> avl_tree.rs<pre><code>[class]{AVLTree}-[func]{remove}\n[class]{AVLTree}-[func]{remove_helper}\n</code></pre> avl_tree.c<pre><code>[class]{aVLTree}-[func]{removeNode}\n[class]{}-[func]{removeHelper}\n</code></pre> avl_tree.zig<pre><code>[class]{AVLTree}-[func]{remove}\n[class]{AVLTree}-[func]{removeHelper}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#3_1","title":"3. \u00a0 \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> <li>\u7ea2\u9ed1\u6811\u5728\u8bb8\u591a\u5e94\u7528\u4e2d\u6bd4 AVL \u6811\u66f4\u53d7\u6b22\u8fce\u3002\u8fd9\u662f\u56e0\u4e3a\u7ea2\u9ed1\u6811\u7684\u5e73\u8861\u6761\u4ef6\u76f8\u5bf9\u5bbd\u677e\uff0c\u5728\u7ea2\u9ed1\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u6240\u9700\u7684\u65cb\u8f6c\u64cd\u4f5c\u76f8\u5bf9\u8f83\u5c11\uff0c\u5176\u8282\u70b9\u589e\u5220\u64cd\u4f5c\u7684\u5e73\u5747\u6548\u7387\u66f4\u9ad8\u3002</li> </ul>"},{"location":"chapter_tree/binary_search_tree/","title":"7.4 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811","text":"<p>\u5982\u56fe 7-16 \u6240\u793a\uff0c\u300c\u4e8c\u53c9\u641c\u7d22\u6811 binary search tree\u300d\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\u3002</p> <ol> <li>\u5bf9\u4e8e\u6839\u8282\u70b9\uff0c\u5de6\u5b50\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u503c \\(<\\) \u6839\u8282\u70b9\u7684\u503c \\(<\\) \u53f3\u5b50\u6811\u4e2d\u6240\u6709\u8282\u70b9\u7684\u503c\u3002</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 7-16 \u00a0 \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":"1. \u00a0 \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\u5982\u56fe 7-17 \u6240\u793a\uff0c\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\u3002</p> <ul> <li>\u82e5 <code>cur.val < 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 > 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> <1><2><3><4> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 7-17 \u00a0 \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> PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_tree.py<pre><code>[class]{BinarySearchTree}-[func]{search}\n</code></pre> binary_search_tree.cpp<pre><code>[class]{BinarySearchTree}-[func]{search}\n</code></pre> binary_search_tree.java<pre><code>[class]{BinarySearchTree}-[func]{search}\n</code></pre> binary_search_tree.cs<pre><code>[class]{BinarySearchTree}-[func]{search}\n</code></pre> binary_search_tree.go<pre><code>[class]{binarySearchTree}-[func]{search}\n</code></pre> binary_search_tree.swift<pre><code>[class]{BinarySearchTree}-[func]{search}\n</code></pre> binary_search_tree.js<pre><code>[class]{BinarySearchTree}-[func]{search}\n</code></pre> binary_search_tree.ts<pre><code>[class]{BinarySearchTree}-[func]{search}\n</code></pre> binary_search_tree.dart<pre><code>[class]{BinarySearchTree}-[func]{search}\n</code></pre> binary_search_tree.rs<pre><code>[class]{BinarySearchTree}-[func]{search}\n</code></pre> binary_search_tree.c<pre><code>[class]{binarySearchTree}-[func]{search}\n</code></pre> binary_search_tree.zig<pre><code>[class]{BinarySearchTree}-[func]{search}\n</code></pre>"},{"location":"chapter_tree/binary_search_tree/#2","title":"2. \u00a0 \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 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\uff0c\u63d2\u5165\u64cd\u4f5c\u6d41\u7a0b\u5982\u56fe 7-18 \u6240\u793a\u3002</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></p> <p> \u56fe 7-18 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u63d2\u5165\u8282\u70b9 </p> <p>\u5728\u4ee3\u7801\u5b9e\u73b0\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\u3002</p> <ul> <li>\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</li> <li>\u4e3a\u4e86\u5b9e\u73b0\u63d2\u5165\u8282\u70b9\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9\u8282\u70b9 <code>pre</code> \u4fdd\u5b58\u4e0a\u4e00\u8f6e\u5faa\u73af\u7684\u8282\u70b9\u3002\u8fd9\u6837\u5728\u904d\u5386\u81f3 \\(\\text{None}\\) \u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5230\u5176\u7236\u8282\u70b9\uff0c\u4ece\u800c\u5b8c\u6210\u8282\u70b9\u63d2\u5165\u64cd\u4f5c\u3002</li> </ul> PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_tree.py<pre><code>[class]{BinarySearchTree}-[func]{insert}\n</code></pre> binary_search_tree.cpp<pre><code>[class]{BinarySearchTree}-[func]{insert}\n</code></pre> binary_search_tree.java<pre><code>[class]{BinarySearchTree}-[func]{insert}\n</code></pre> binary_search_tree.cs<pre><code>[class]{BinarySearchTree}-[func]{insert}\n</code></pre> binary_search_tree.go<pre><code>[class]{binarySearchTree}-[func]{insert}\n</code></pre> binary_search_tree.swift<pre><code>[class]{BinarySearchTree}-[func]{insert}\n</code></pre> binary_search_tree.js<pre><code>[class]{BinarySearchTree}-[func]{insert}\n</code></pre> binary_search_tree.ts<pre><code>[class]{BinarySearchTree}-[func]{insert}\n</code></pre> binary_search_tree.dart<pre><code>[class]{BinarySearchTree}-[func]{insert}\n</code></pre> binary_search_tree.rs<pre><code>[class]{BinarySearchTree}-[func]{insert}\n</code></pre> binary_search_tree.c<pre><code>[class]{binarySearchTree}-[func]{insert}\n</code></pre> binary_search_tree.zig<pre><code>[class]{BinarySearchTree}-[func]{insert}\n</code></pre> <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":"3. \u00a0 \u5220\u9664\u8282\u70b9","text":"<p>\u5148\u5728\u4e8c\u53c9\u6811\u4e2d\u67e5\u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u518d\u5c06\u5176\u4ece\u4e8c\u53c9\u6811\u4e2d\u5220\u9664\u3002</p> <p>\u4e0e\u63d2\u5165\u8282\u70b9\u7c7b\u4f3c\uff0c\u6211\u4eec\u9700\u8981\u4fdd\u8bc1\u5728\u5220\u9664\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u201c\u5de6\u5b50\u6811 < \u6839\u8282\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\u4ecd\u7136\u6ee1\u8db3\u3002</p> <p>\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u76ee\u6807\u8282\u70b9\u7684\u5b50\u8282\u70b9\u6570\u91cf\uff0c\u5171\u5206\u4e3a 0\u30011 \u548c 2 \u8fd9\u4e09\u79cd\u60c5\u51b5\uff0c\u6267\u884c\u5bf9\u5e94\u7684\u5220\u9664\u8282\u70b9\u64cd\u4f5c\u3002</p> <p>\u5982\u56fe 7-19 \u6240\u793a\uff0c\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \u65f6\uff0c\u8868\u793a\u8be5\u8282\u70b9\u662f\u53f6\u8282\u70b9\uff0c\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u3002</p> <p></p> <p> \u56fe 7-19 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 0 \uff09 </p> <p>\u5982\u56fe 7-20 \u6240\u793a\uff0c\u5f53\u5f85\u5220\u9664\u8282\u70b9\u7684\u5ea6\u4e3a \\(1\\) \u65f6\uff0c\u5c06\u5f85\u5220\u9664\u8282\u70b9\u66ff\u6362\u4e3a\u5176\u5b50\u8282\u70b9\u5373\u53ef\u3002</p> <p></p> <p> \u56fe 7-20 \u00a0 \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 \\(<\\) \u6839 \\(<\\) \u53f3\u201d\u7684\u6027\u8d28\uff0c\u56e0\u6b64\u8fd9\u4e2a\u8282\u70b9\u53ef\u4ee5\u662f\u53f3\u5b50\u6811\u7684\u6700\u5c0f\u8282\u70b9\u6216\u5de6\u5b50\u6811\u7684\u6700\u5927\u8282\u70b9\u3002</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\u6d41\u7a0b\u5982\u56fe 7-21 \u6240\u793a\u3002</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> <1><2><3><4> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 7-21 \u00a0 \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u8282\u70b9\uff08\u5ea6\u4e3a 2 \uff09 </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> PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_search_tree.py<pre><code>[class]{BinarySearchTree}-[func]{remove}\n</code></pre> binary_search_tree.cpp<pre><code>[class]{BinarySearchTree}-[func]{remove}\n</code></pre> binary_search_tree.java<pre><code>[class]{BinarySearchTree}-[func]{remove}\n</code></pre> binary_search_tree.cs<pre><code>[class]{BinarySearchTree}-[func]{remove}\n</code></pre> binary_search_tree.go<pre><code>[class]{binarySearchTree}-[func]{remove}\n</code></pre> binary_search_tree.swift<pre><code>[class]{BinarySearchTree}-[func]{remove}\n</code></pre> binary_search_tree.js<pre><code>[class]{BinarySearchTree}-[func]{remove}\n</code></pre> binary_search_tree.ts<pre><code>[class]{BinarySearchTree}-[func]{remove}\n</code></pre> binary_search_tree.dart<pre><code>[class]{BinarySearchTree}-[func]{remove}\n</code></pre> binary_search_tree.rs<pre><code>[class]{BinarySearchTree}-[func]{remove}\n</code></pre> binary_search_tree.c<pre><code>[class]{binarySearchTree}-[func]{removeNode}\n</code></pre> binary_search_tree.zig<pre><code>[class]{BinarySearchTree}-[func]{remove}\n</code></pre>"},{"location":"chapter_tree/binary_search_tree/#4","title":"4. \u00a0 \u4e2d\u5e8f\u904d\u5386\u6709\u5e8f","text":"<p>\u5982\u56fe 7-22 \u6240\u793a\uff0c\u4e8c\u53c9\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u9075\u5faa\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u7684\u904d\u5386\u987a\u5e8f\uff0c\u800c\u4e8c\u53c9\u641c\u7d22\u6811\u6ee1\u8db3\u201c\u5de6\u5b50\u8282\u70b9 \\(<\\) \u6839\u8282\u70b9 \\(<\\) \u53f3\u5b50\u8282\u70b9\u201d\u7684\u5927\u5c0f\u5173\u7cfb\u3002</p> <p>\u8fd9\u610f\u5473\u7740\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u8fdb\u884c\u4e2d\u5e8f\u904d\u5386\u65f6\uff0c\u603b\u662f\u4f1a\u4f18\u5148\u904d\u5386\u4e0b\u4e00\u4e2a\u6700\u5c0f\u8282\u70b9\uff0c\u4ece\u800c\u5f97\u51fa\u4e00\u4e2a\u91cd\u8981\u6027\u8d28\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u662f\u5347\u5e8f\u7684\u3002</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\u987b\u8fdb\u884c\u989d\u5916\u7684\u6392\u5e8f\u64cd\u4f5c\uff0c\u975e\u5e38\u9ad8\u6548\u3002</p> <p></p> <p> \u56fe 7-22 \u00a0 \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\u89c2\u5bdf\u8868 7-2 \uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5404\u9879\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f\u5bf9\u6570\u9636\uff0c\u5177\u6709\u7a33\u5b9a\u4e14\u9ad8\u6548\u7684\u6027\u80fd\u8868\u73b0\u3002\u53ea\u6709\u5728\u9ad8\u9891\u6dfb\u52a0\u3001\u4f4e\u9891\u67e5\u627e\u5220\u9664\u7684\u6570\u636e\u9002\u7528\u573a\u666f\u4e0b\uff0c\u6570\u7ec4\u6bd4\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387\u66f4\u9ad8\u3002</p> <p> \u8868 7-2 \u00a0 \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\u56fe 7-23 \u6240\u793a\u7684\u94fe\u8868\uff0c\u8fd9\u65f6\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4f1a\u9000\u5316\u4e3a \\(O(n)\\) \u3002</p> <p></p> <p> \u56fe 7-23 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\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> PythonC++JavaC#GoSwiftJSTSDartRustCZig <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: TreeNode | None = None # \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nself.right: TreeNode | None = None # \u53f3\u5b50\u8282\u70b9\u5f15\u7528\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>/* \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 {\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 */\ntype TreeNode struct {\nVal int\nLeft *TreeNode\nRight *TreeNode\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc NewTreeNode(v int) *TreeNode {\nreturn &TreeNode{\nLeft: nil, // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nRight: nil, // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nVal: v, // \u8282\u70b9\u503c\n}\n}\n</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>/* \u4e8c\u53c9\u6811\u8282\u70b9\u7c7b */\nclass TreeNode {\nval; // \u8282\u70b9\u503c\nleft; // \u5de6\u5b50\u8282\u70b9\u6307\u9488\nright; // \u53f3\u5b50\u8282\u70b9\u6307\u9488\nconstructor(val, left, right) {\nthis.val = val === undefined ? 0 : val;\nthis.left = left === undefined ? null : left;\nthis.right = right === undefined ? null : right;\n}\n}\n</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\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>use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u4e8c\u53c9\u6811\u8282\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nval: i32, // \u8282\u70b9\u503c\nleft: Option<Rc<RefCell<TreeNode>>>, // \u5de6\u5b50\u8282\u70b9\u5f15\u7528\nright: Option<Rc<RefCell<TreeNode>>>, // \u53f3\u5b50\u8282\u70b9\u5f15\u7528\n}\nimpl TreeNode {\n/* \u6784\u9020\u65b9\u6cd5 */\nfn new(val: i32) -> Rc<RefCell<Self>> {\nRc::new(RefCell::new(Self {\nval,\nleft: None,\nright: None\n}))\n}\n}\n</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->val = val;\nnode->height = 0;\nnode->left = NULL;\nnode->right = NULL;\nreturn node;\n}\n</code></pre> <pre><code>\n</code></pre> <p>\u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5f15\u7528\uff08\u6307\u9488\uff09\uff0c\u5206\u522b\u6307\u5411\u300c\u5de6\u5b50\u8282\u70b9 left-child node\u300d\u548c\u300c\u53f3\u5b50\u8282\u70b9 right-child node\u300d\uff0c\u8be5\u8282\u70b9\u88ab\u79f0\u4e3a\u8fd9\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u300c\u7236\u8282\u70b9 parent node\u300d\u3002\u5f53\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u8282\u70b9\u65f6\uff0c\u6211\u4eec\u5c06\u8be5\u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u79f0\u4e3a\u8be5\u8282\u70b9\u7684\u300c\u5de6\u5b50\u6811 left subtree\u300d\uff0c\u540c\u7406\u53ef\u5f97\u300c\u53f3\u5b50\u6811 right subtree\u300d\u3002</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\u5982\u56fe 7-1 \u6240\u793a\uff0c\u5982\u679c\u5c06\u201c\u8282\u70b9 2\u201d\u89c6\u4e3a\u7236\u8282\u70b9\uff0c\u5219\u5176\u5de6\u5b50\u8282\u70b9\u548c\u53f3\u5b50\u8282\u70b9\u5206\u522b\u662f\u201c\u8282\u70b9 4\u201d\u548c\u201c\u8282\u70b9 5\u201d\uff0c\u5de6\u5b50\u6811\u662f\u201c\u8282\u70b9 4 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\uff0c\u53f3\u5b50\u6811\u662f\u201c\u8282\u70b9 5 \u53ca\u5176\u4ee5\u4e0b\u8282\u70b9\u5f62\u6210\u7684\u6811\u201d\u3002</p> <p></p> <p> \u56fe 7-1 \u00a0 \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\u7684\u5e38\u7528\u672f\u8bed\u5982\u56fe 7-2 \u6240\u793a\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>\u300c\u8fb9 edge\u300d\uff1a\u8fde\u63a5\u4e24\u4e2a\u8282\u70b9\u7684\u7ebf\u6bb5\uff0c\u5373\u8282\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\u3002</li> <li>\u8282\u70b9\u6240\u5728\u7684\u300c\u5c42 level\u300d\uff1a\u4ece\u9876\u81f3\u5e95\u9012\u589e\uff0c\u6839\u8282\u70b9\u6240\u5728\u5c42\u4e3a 1 \u3002</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\u53d6\u503c\u8303\u56f4\u662f 0\u30011\u30012 \u3002</li> <li>\u4e8c\u53c9\u6811\u7684\u300c\u9ad8\u5ea6 height\u300d\uff1a\u4ece\u6839\u8282\u70b9\u5230\u6700\u8fdc\u53f6\u8282\u70b9\u6240\u7ecf\u8fc7\u7684\u8fb9\u7684\u6570\u91cf\u3002</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 7-2 \u00a0 \u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed </p> <p>Tip</p> <p>\u8bf7\u6ce8\u610f\uff0c\u6211\u4eec\u901a\u5e38\u5c06\u201c\u9ad8\u5ea6\u201d\u548c\u201c\u6df1\u5ea6\u201d\u5b9a\u4e49\u4e3a\u201c\u8d70\u8fc7\u8fb9\u7684\u6570\u91cf\u201d\uff0c\u4f46\u6709\u4e9b\u9898\u76ee\u6216\u6559\u6750\u53ef\u80fd\u4f1a\u5c06\u5176\u5b9a\u4e49\u4e3a\u201c\u8d70\u8fc7\u8282\u70b9\u7684\u6570\u91cf\u201d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u9ad8\u5ea6\u548c\u6df1\u5ea6\u90fd\u9700\u8981\u52a0 1 \u3002</p>"},{"location":"chapter_tree/binary_tree/#712","title":"7.1.2 \u00a0 \u4e8c\u53c9\u6811\u57fa\u672c\u64cd\u4f5c","text":""},{"location":"chapter_tree/binary_tree/#1","title":"1. \u00a0 \u521d\u59cb\u5316\u4e8c\u53c9\u6811","text":"<p>\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u9996\u5148\u521d\u59cb\u5316\u8282\u70b9\uff0c\u7136\u540e\u6784\u5efa\u5f15\u7528\uff08\u6307\u9488\uff09\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig 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.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->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n</code></pre> 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.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.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.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.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.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>// \u521d\u59cb\u5316\u8282\u70b9\nlet n1 = TreeNode::new(1);\nlet n2 = TreeNode::new(2);\nlet n3 = TreeNode::new(3);\nlet n4 = TreeNode::new(4);\nlet n5 = TreeNode::new(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.borrow_mut().left = Some(n2.clone());\nn1.borrow_mut().right = Some(n3);\nn2.borrow_mut().left = Some(n4);\nn2.borrow_mut().right = Some(n5);\n</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->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n</code></pre> binary_tree.zig<pre><code>\n</code></pre>"},{"location":"chapter_tree/binary_tree/#2","title":"2. \u00a0 \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9","text":"<p>\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u6307\u9488\u6765\u5b9e\u73b0\u3002\u56fe 7-3 \u7ed9\u51fa\u4e86\u4e00\u4e2a\u793a\u4f8b\u3002</p> <p></p> <p> \u56fe 7-3 \u00a0 \u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 </p> PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree.py<pre><code># \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9\np = TreeNode(0)\n# \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = p\np.left = n2\n# \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n</code></pre> binary_tree.cpp<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode* P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1->left = n2;\n</code></pre> binary_tree.java<pre><code>TreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n</code></pre> binary_tree.cs<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n</code></pre> binary_tree.go<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.Left = n2\n</code></pre> binary_tree.swift<pre><code>let P = TreeNode(x: 0)\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P\nP.left = n2\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2\n</code></pre> binary_tree.js<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nlet P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n</code></pre> binary_tree.ts<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nconst P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n</code></pre> binary_tree.dart<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1.left = n2;\n</code></pre> binary_tree.rs<pre><code>let p = TreeNode::new(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// \u5220\u9664\u8282\u70b9 p\nn1.borrow_mut().left = Some(n2);\n</code></pre> binary_tree.c<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u8282\u70b9 */\nTreeNode *P = newTreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u8282\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u8282\u70b9 P\nn1->left = n2;\n</code></pre> binary_tree.zig<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_1","title":"1. \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"<p>\u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811 perfect binary tree\u300d\u6240\u6709\u5c42\u7684\u8282\u70b9\u90fd\u88ab\u5b8c\u5168\u586b\u6ee1\u3002\u5728\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\uff0c\u53f6\u8282\u70b9\u7684\u5ea6\u4e3a \\(0\\) \uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u7684\u5ea6\u90fd\u4e3a \\(2\\) \uff1b\u82e5\u6811\u9ad8\u5ea6\u4e3a \\(h\\) \uff0c\u5219\u8282\u70b9\u603b\u6570\u4e3a \\(2^{h+1} - 1\\) \uff0c\u5448\u73b0\u6807\u51c6\u7684\u6307\u6570\u7ea7\u5173\u7cfb\uff0c\u53cd\u6620\u4e86\u81ea\u7136\u754c\u4e2d\u5e38\u89c1\u7684\u7ec6\u80de\u5206\u88c2\u73b0\u8c61\u3002</p> <p>Tip</p> <p>\u8bf7\u6ce8\u610f\uff0c\u5728\u4e2d\u6587\u793e\u533a\u4e2d\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u5e38\u88ab\u79f0\u4e3a\u300c\u6ee1\u4e8c\u53c9\u6811\u300d\u3002</p> <p></p> <p> \u56fe 7-4 \u00a0 \u5b8c\u7f8e\u4e8c\u53c9\u6811 </p>"},{"location":"chapter_tree/binary_tree/#2_1","title":"2. \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811","text":"<p>\u5982\u56fe 7-5 \u6240\u793a\uff0c\u300c\u5b8c\u5168\u4e8c\u53c9\u6811 complete binary tree\u300d\u53ea\u6709\u6700\u5e95\u5c42\u7684\u8282\u70b9\u672a\u88ab\u586b\u6ee1\uff0c\u4e14\u6700\u5e95\u5c42\u8282\u70b9\u5c3d\u91cf\u9760\u5de6\u586b\u5145\u3002</p> <p></p> <p> \u56fe 7-5 \u00a0 \u5b8c\u5168\u4e8c\u53c9\u6811 </p>"},{"location":"chapter_tree/binary_tree/#3","title":"3. \u00a0 \u5b8c\u6ee1\u4e8c\u53c9\u6811","text":"<p>\u5982\u56fe 7-6 \u6240\u793a\uff0c\u300c\u5b8c\u6ee1\u4e8c\u53c9\u6811 full binary tree\u300d\u9664\u4e86\u53f6\u8282\u70b9\u4e4b\u5916\uff0c\u5176\u4f59\u6240\u6709\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5b50\u8282\u70b9\u3002</p> <p></p> <p> \u56fe 7-6 \u00a0 \u5b8c\u6ee1\u4e8c\u53c9\u6811 </p>"},{"location":"chapter_tree/binary_tree/#4","title":"4. \u00a0 \u5e73\u8861\u4e8c\u53c9\u6811","text":"<p>\u5982\u56fe 7-7 \u6240\u793a\uff0c\u300c\u5e73\u8861\u4e8c\u53c9\u6811 balanced binary tree\u300d\u4e2d\u4efb\u610f\u8282\u70b9\u7684\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\u4e4b\u5dee\u7684\u7edd\u5bf9\u503c\u4e0d\u8d85\u8fc7 1 \u3002</p> <p></p> <p> \u56fe 7-7 \u00a0 \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>\u56fe 7-8 \u5c55\u793a\u4e86\u4e8c\u53c9\u6811\u7684\u7406\u60f3\u4e0e\u9000\u5316\u72b6\u6001\u3002\u5f53\u4e8c\u53c9\u6811\u7684\u6bcf\u5c42\u8282\u70b9\u90fd\u88ab\u586b\u6ee1\u65f6\uff0c\u8fbe\u5230\u201c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u201d\uff1b\u800c\u5f53\u6240\u6709\u8282\u70b9\u90fd\u504f\u5411\u4e00\u4fa7\u65f6\uff0c\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u201c\u94fe\u8868\u201d\u3002</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 7-8 \u00a0 \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u4e0e\u6700\u5dee\u7ed3\u6784 </p> <p>\u5982\u8868 7-1 \u6240\u793a\uff0c\u5728\u6700\u4f73\u548c\u6700\u5dee\u7ed3\u6784\u4e0b\uff0c\u4e8c\u53c9\u6811\u7684\u53f6\u8282\u70b9\u6570\u91cf\u3001\u8282\u70b9\u603b\u6570\u3001\u9ad8\u5ea6\u7b49\u8fbe\u5230\u6781\u5927\u6216\u6781\u5c0f\u503c\u3002</p> <p> \u8868 7-1 \u00a0 \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>\u5982\u56fe 7-9 \u6240\u793a\uff0c\u300c\u5c42\u5e8f\u904d\u5386 level-order traversal\u300d\u4ece\u9876\u90e8\u5230\u5e95\u90e8\u9010\u5c42\u904d\u5386\u4e8c\u53c9\u6811\uff0c\u5e76\u5728\u6bcf\u4e00\u5c42\u6309\u7167\u4ece\u5de6\u5230\u53f3\u7684\u987a\u5e8f\u8bbf\u95ee\u8282\u70b9\u3002</p> <p>\u5c42\u5e8f\u904d\u5386\u672c\u8d28\u4e0a\u5c5e\u4e8e\u300c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 breadth-first traversal\u300d\uff0c\u5b83\u4f53\u73b0\u4e86\u4e00\u79cd\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u6269\u5c55\u201d\u7684\u9010\u5c42\u904d\u5386\u65b9\u5f0f\u3002</p> <p></p> <p> \u56fe 7-9 \u00a0 \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386 </p>"},{"location":"chapter_tree/binary_tree_traversal/#1","title":"1. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"<p>\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u901a\u5e38\u501f\u52a9\u201c\u961f\u5217\u201d\u6765\u5b9e\u73b0\u3002\u961f\u5217\u9075\u5faa\u201c\u5148\u8fdb\u5148\u51fa\u201d\u7684\u89c4\u5219\uff0c\u800c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u5219\u9075\u5faa\u201c\u9010\u5c42\u63a8\u8fdb\u201d\u7684\u89c4\u5219\uff0c\u4e24\u8005\u80cc\u540e\u7684\u601d\u60f3\u662f\u4e00\u81f4\u7684\u3002</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree_bfs.py<pre><code>[class]{}-[func]{level_order}\n</code></pre> binary_tree_bfs.cpp<pre><code>[class]{}-[func]{levelOrder}\n</code></pre> binary_tree_bfs.java<pre><code>[class]{binary_tree_bfs}-[func]{levelOrder}\n</code></pre> binary_tree_bfs.cs<pre><code>[class]{binary_tree_bfs}-[func]{levelOrder}\n</code></pre> binary_tree_bfs.go<pre><code>[class]{}-[func]{levelOrder}\n</code></pre> binary_tree_bfs.swift<pre><code>[class]{}-[func]{levelOrder}\n</code></pre> binary_tree_bfs.js<pre><code>[class]{}-[func]{levelOrder}\n</code></pre> binary_tree_bfs.ts<pre><code>[class]{}-[func]{levelOrder}\n</code></pre> binary_tree_bfs.dart<pre><code>[class]{}-[func]{levelOrder}\n</code></pre> binary_tree_bfs.rs<pre><code>[class]{}-[func]{level_order}\n</code></pre> binary_tree_bfs.c<pre><code>[class]{}-[func]{levelOrder}\n</code></pre> binary_tree_bfs.zig<pre><code>[class]{}-[func]{levelOrder}\n</code></pre>"},{"location":"chapter_tree/binary_tree_traversal/#2","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"<ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \uff1a\u6240\u6709\u8282\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d \\(n\\) \u4e3a\u8282\u70b9\u6570\u91cf\u3002</li> <li>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \uff1a\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u6ee1\u4e8c\u53c9\u6811\u65f6\uff0c\u904d\u5386\u5230\u6700\u5e95\u5c42\u4e4b\u524d\uff0c\u961f\u5217\u4e2d\u6700\u591a\u540c\u65f6\u5b58\u5728 \\((n + 1) / 2\\) \u4e2a\u8282\u70b9\uff0c\u5360\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002</li> </ul>"},{"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>\u56fe 7-10 \u5c55\u793a\u4e86\u5bf9\u4e8c\u53c9\u6811\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u5de5\u4f5c\u539f\u7406\u3002\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5c31\u50cf\u662f\u7ed5\u7740\u6574\u4e2a\u4e8c\u53c9\u6811\u7684\u5916\u56f4\u201c\u8d70\u201d\u4e00\u5708\uff0c\u5728\u6bcf\u4e2a\u8282\u70b9\u90fd\u4f1a\u9047\u5230\u4e09\u4e2a\u4f4d\u7f6e\uff0c\u5206\u522b\u5bf9\u5e94\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u548c\u540e\u5e8f\u904d\u5386\u3002</p> <p></p> <p> \u56fe 7-10 \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386 </p>"},{"location":"chapter_tree/binary_tree_traversal/#1_1","title":"1. \u00a0 \u4ee3\u7801\u5b9e\u73b0","text":"<p>\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u901a\u5e38\u57fa\u4e8e\u9012\u5f52\u5b9e\u73b0\uff1a</p> PythonC++JavaC#GoSwiftJSTSDartRustCZig binary_tree_dfs.py<pre><code>[class]{}-[func]{pre_order}\n[class]{}-[func]{in_order}\n[class]{}-[func]{post_order}\n</code></pre> binary_tree_dfs.cpp<pre><code>[class]{}-[func]{preOrder}\n[class]{}-[func]{inOrder}\n[class]{}-[func]{postOrder}\n</code></pre> binary_tree_dfs.java<pre><code>[class]{binary_tree_dfs}-[func]{preOrder}\n[class]{binary_tree_dfs}-[func]{inOrder}\n[class]{binary_tree_dfs}-[func]{postOrder}\n</code></pre> binary_tree_dfs.cs<pre><code>[class]{binary_tree_dfs}-[func]{preOrder}\n[class]{binary_tree_dfs}-[func]{inOrder}\n[class]{binary_tree_dfs}-[func]{postOrder}\n</code></pre> binary_tree_dfs.go<pre><code>[class]{}-[func]{preOrder}\n[class]{}-[func]{inOrder}\n[class]{}-[func]{postOrder}\n</code></pre> binary_tree_dfs.swift<pre><code>[class]{}-[func]{preOrder}\n[class]{}-[func]{inOrder}\n[class]{}-[func]{postOrder}\n</code></pre> binary_tree_dfs.js<pre><code>[class]{}-[func]{preOrder}\n[class]{}-[func]{inOrder}\n[class]{}-[func]{postOrder}\n</code></pre> binary_tree_dfs.ts<pre><code>[class]{}-[func]{preOrder}\n[class]{}-[func]{inOrder}\n[class]{}-[func]{postOrder}\n</code></pre> binary_tree_dfs.dart<pre><code>[class]{}-[func]{preOrder}\n[class]{}-[func]{inOrder}\n[class]{}-[func]{postOrder}\n</code></pre> binary_tree_dfs.rs<pre><code>[class]{}-[func]{pre_order}\n[class]{}-[func]{in_order}\n[class]{}-[func]{post_order}\n</code></pre> binary_tree_dfs.c<pre><code>[class]{}-[func]{preOrder}\n[class]{}-[func]{inOrder}\n[class]{}-[func]{postOrder}\n</code></pre> binary_tree_dfs.zig<pre><code>[class]{}-[func]{preOrder}\n[class]{}-[func]{inOrder}\n[class]{}-[func]{postOrder}\n</code></pre> <p>Note</p> <p>\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u4e5f\u53ef\u4ee5\u57fa\u4e8e\u8fed\u4ee3\u5b9e\u73b0\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u81ea\u884c\u7814\u7a76\u3002</p> <p>\u56fe 7-11 \u5c55\u793a\u4e86\u524d\u5e8f\u904d\u5386\u4e8c\u53c9\u6811\u7684\u9012\u5f52\u8fc7\u7a0b\uff0c\u5176\u53ef\u5206\u4e3a\u201c\u9012\u201d\u548c\u201c\u5f52\u201d\u4e24\u4e2a\u9006\u5411\u7684\u90e8\u5206\u3002</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> <1><2><3><4><5><6><7><8><9><10><11> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p> \u56fe 7-11 \u00a0 \u524d\u5e8f\u904d\u5386\u7684\u9012\u5f52\u8fc7\u7a0b </p>"},{"location":"chapter_tree/binary_tree_traversal/#2_1","title":"2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"<ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \uff1a\u6240\u6709\u8282\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\u3002</li> <li>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \uff1a\u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5373\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\uff0c\u9012\u5f52\u6df1\u5ea6\u8fbe\u5230 \\(n\\) \uff0c\u7cfb\u7edf\u5360\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002</li> </ul>"},{"location":"chapter_tree/summary/","title":"7.6 \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_tree/summary/#1","title":"1. \u00a0 \u91cd\u70b9\u56de\u987e","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/#2-q-a","title":"2. \u00a0 Q & 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 < \u6839\u7ed3\u70b9\u503c < \u53f3\u5b50\u7ed3\u70b9\u503c</code> \uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u8981\u6309\u7167 <code>\u5de6->\u6839->\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>\u3001<code>child</code>\u3001<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\u662f\u4e0d\u540c\u7684\u3002</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>"}]} |